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 |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5 | *f |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 58 | #include "buf.h" |
| 59 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 60 | #ifdef LIBXML_PATTERN_ENABLED |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 61 | #define XPATH_STREAMING |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 62 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 64 | #define TODO \ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 65 | xmlGenericError(xmlGenericErrorContext, \ |
| 66 | "Unimplemented block at %s:%d\n", \ |
| 67 | __FILE__, __LINE__); |
| 68 | |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 69 | /* |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 70 | * XP_OPTIMIZED_NON_ELEM_COMPARISON: |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 71 | * If defined, this will use xmlXPathCmpNodesExt() instead of |
| 72 | * xmlXPathCmpNodes(). The new function is optimized comparison of |
| 73 | * non-element nodes; actually it will speed up comparison only if |
| 74 | * xmlXPathOrderDocElems() was called in order to index the elements of |
| 75 | * a tree in document order; Libxslt does such an indexing, thus it will |
| 76 | * benefit from this optimization. |
| 77 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 78 | #define XP_OPTIMIZED_NON_ELEM_COMPARISON |
| 79 | |
| 80 | /* |
| 81 | * XP_OPTIMIZED_FILTER_FIRST: |
| 82 | * If defined, this will optimize expressions like "key('foo', 'val')[b][1]" |
| 83 | * in a way, that it stop evaluation at the first node. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 84 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 85 | #define XP_OPTIMIZED_FILTER_FIRST |
| 86 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 87 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 88 | * XP_DEBUG_OBJ_USAGE: |
| 89 | * Internal flag to enable tracking of how much XPath objects have been |
| 90 | * created. |
| 91 | */ |
| 92 | /* #define XP_DEBUG_OBJ_USAGE */ |
| 93 | |
| 94 | /* |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 95 | * TODO: |
| 96 | * There are a few spots where some tests are done which depend upon ascii |
| 97 | * data. These should be enhanced for full UTF8 support (see particularly |
| 98 | * any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT) |
| 99 | */ |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 100 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 101 | #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 102 | |
| 103 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 104 | * * |
| 105 | * Floating point stuff * |
| 106 | * * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 107 | ************************************************************************/ |
| 108 | |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 109 | #ifndef TRIO_REPLACE_STDIO |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 110 | #define TRIO_PUBLIC static |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 111 | #endif |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 112 | #include "trionan.c" |
| 113 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 114 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 115 | * The lack of portability of this section of the libc is annoying ! |
| 116 | */ |
| 117 | double xmlXPathNAN = 0; |
| 118 | double xmlXPathPINF = 1; |
| 119 | double xmlXPathNINF = -1; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 120 | static double xmlXPathNZERO = 0; /* not exported from headers */ |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 121 | static int xmlXPathInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 122 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 123 | /** |
| 124 | * xmlXPathInit: |
| 125 | * |
| 126 | * Initialize the XPath environment |
| 127 | */ |
| 128 | void |
| 129 | xmlXPathInit(void) { |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 130 | if (xmlXPathInitialized) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 131 | |
Bjorn Reese | 4502960 | 2001-08-21 09:23:53 +0000 | [diff] [blame] | 132 | xmlXPathPINF = trio_pinf(); |
| 133 | xmlXPathNINF = trio_ninf(); |
| 134 | xmlXPathNAN = trio_nan(); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 135 | xmlXPathNZERO = trio_nzero(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 136 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 137 | xmlXPathInitialized = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 140 | /** |
| 141 | * xmlXPathIsNaN: |
| 142 | * @val: a double value |
| 143 | * |
| 144 | * Provides a portable isnan() function to detect whether a double |
| 145 | * is a NotaNumber. Based on trio code |
| 146 | * http://sourceforge.net/projects/ctrio/ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 147 | * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 148 | * Returns 1 if the value is a NaN, 0 otherwise |
| 149 | */ |
| 150 | int |
| 151 | xmlXPathIsNaN(double val) { |
| 152 | return(trio_isnan(val)); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * xmlXPathIsInf: |
| 157 | * @val: a double value |
| 158 | * |
| 159 | * Provides a portable isinf() function to detect whether a double |
| 160 | * is a +Infinite or -Infinite. Based on trio code |
| 161 | * http://sourceforge.net/projects/ctrio/ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 162 | * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 163 | * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise |
| 164 | */ |
| 165 | int |
| 166 | xmlXPathIsInf(double val) { |
| 167 | return(trio_isinf(val)); |
| 168 | } |
| 169 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 170 | #endif /* SCHEMAS or XPATH */ |
| 171 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 172 | /** |
| 173 | * xmlXPathGetSign: |
| 174 | * @val: a double value |
| 175 | * |
| 176 | * Provides a portable function to detect the sign of a double |
| 177 | * Modified from trio code |
| 178 | * http://sourceforge.net/projects/ctrio/ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 179 | * |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 180 | * Returns 1 if the value is Negative, 0 if positive |
| 181 | */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 182 | static int |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 183 | xmlXPathGetSign(double val) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 184 | return(trio_signbit(val)); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 188 | /* |
| 189 | * TODO: when compatibility allows remove all "fake node libxslt" strings |
| 190 | * the test should just be name[0] = ' ' |
| 191 | */ |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 192 | #ifdef DEBUG_XPATH_EXPRESSION |
| 193 | #define DEBUG_STEP |
| 194 | #define DEBUG_EXPR |
| 195 | #define DEBUG_EVAL_COUNTS |
| 196 | #endif |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 197 | |
| 198 | static xmlNs xmlXPathXMLNamespaceStruct = { |
| 199 | NULL, |
| 200 | XML_NAMESPACE_DECL, |
| 201 | XML_XML_NAMESPACE, |
| 202 | BAD_CAST "xml", |
William M. Brack | ee0b982 | 2007-03-07 08:15:01 +0000 | [diff] [blame] | 203 | NULL, |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 204 | NULL |
| 205 | }; |
| 206 | static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct; |
| 207 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 208 | /* |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 209 | * Optimizer is disabled only when threaded apps are detected while |
| 210 | * the library ain't compiled for thread safety. |
| 211 | */ |
| 212 | static int xmlXPathDisableOptimizer = 0; |
| 213 | #endif |
| 214 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 215 | /************************************************************************ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 216 | * * |
| 217 | * Error handling routines * |
| 218 | * * |
| 219 | ************************************************************************/ |
| 220 | |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 221 | /** |
| 222 | * XP_ERRORNULL: |
| 223 | * @X: the error code |
| 224 | * |
| 225 | * Macro to raise an XPath error and return NULL. |
| 226 | */ |
| 227 | #define XP_ERRORNULL(X) \ |
| 228 | { xmlXPathErr(ctxt, X); return(NULL); } |
| 229 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 230 | /* |
| 231 | * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError |
| 232 | */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 233 | static const char *xmlXPathErrorMessages[] = { |
| 234 | "Ok\n", |
| 235 | "Number encoding\n", |
| 236 | "Unfinished literal\n", |
| 237 | "Start of literal\n", |
| 238 | "Expected $ for variable reference\n", |
| 239 | "Undefined variable\n", |
| 240 | "Invalid predicate\n", |
| 241 | "Invalid expression\n", |
| 242 | "Missing closing curly brace\n", |
| 243 | "Unregistered function\n", |
| 244 | "Invalid operand\n", |
| 245 | "Invalid type\n", |
| 246 | "Invalid number of arguments\n", |
| 247 | "Invalid context size\n", |
| 248 | "Invalid context position\n", |
| 249 | "Memory allocation error\n", |
| 250 | "Syntax error\n", |
| 251 | "Resource error\n", |
| 252 | "Sub resource error\n", |
| 253 | "Undefined namespace prefix\n", |
| 254 | "Encoding error\n", |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 255 | "Char out of XML range\n", |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 256 | "Invalid or incomplete context\n", |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 257 | "Stack usage errror\n", |
William M. Brack | cd65bc9 | 2005-01-06 09:39:18 +0000 | [diff] [blame] | 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, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 340 | "%s", xmlXPathErrorMessages[error]); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 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, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 351 | "%s", xmlXPathErrorMessages[error]); |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 352 | return; |
| 353 | } |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 354 | |
| 355 | /* cleanup current last error */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 356 | xmlResetError(&ctxt->context->lastError); |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 357 | |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 358 | ctxt->context->lastError.domain = XML_FROM_XPATH; |
| 359 | ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK - |
| 360 | XPATH_EXPRESSION_OK; |
Daniel Veillard | fcf719c | 2003-10-10 11:42:17 +0000 | [diff] [blame] | 361 | ctxt->context->lastError.level = XML_ERR_ERROR; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 362 | ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base); |
| 363 | ctxt->context->lastError.int1 = ctxt->cur - ctxt->base; |
| 364 | ctxt->context->lastError.node = ctxt->context->debugNode; |
| 365 | if (ctxt->context->error != NULL) { |
| 366 | ctxt->context->error(ctxt->context->userData, |
| 367 | &ctxt->context->lastError); |
| 368 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 369 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 370 | NULL, ctxt->context->debugNode, XML_FROM_XPATH, |
| 371 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 372 | XML_ERR_ERROR, NULL, 0, |
| 373 | (const char *) ctxt->base, NULL, NULL, |
| 374 | ctxt->cur - ctxt->base, 0, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 375 | "%s", xmlXPathErrorMessages[error]); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * xmlXPatherror: |
| 382 | * @ctxt: the XPath Parser context |
| 383 | * @file: the file name |
| 384 | * @line: the line number |
| 385 | * @no: the error number |
| 386 | * |
| 387 | * Formats an error message. |
| 388 | */ |
| 389 | void |
| 390 | xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED, |
| 391 | int line ATTRIBUTE_UNUSED, int no) { |
| 392 | xmlXPathErr(ctxt, no); |
| 393 | } |
| 394 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 395 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 396 | * * |
| 397 | * Utilities * |
| 398 | * * |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 399 | ************************************************************************/ |
| 400 | |
| 401 | /** |
| 402 | * xsltPointerList: |
| 403 | * |
| 404 | * Pointer-list for various purposes. |
| 405 | */ |
| 406 | typedef struct _xmlPointerList xmlPointerList; |
| 407 | typedef xmlPointerList *xmlPointerListPtr; |
| 408 | struct _xmlPointerList { |
| 409 | void **items; |
| 410 | int number; |
| 411 | int size; |
| 412 | }; |
| 413 | /* |
| 414 | * TODO: Since such a list-handling is used in xmlschemas.c and libxslt |
| 415 | * and here, we should make the functions public. |
| 416 | */ |
| 417 | static int |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 418 | xmlPointerListAddSize(xmlPointerListPtr list, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 419 | void *item, |
| 420 | int initialSize) |
| 421 | { |
| 422 | if (list->items == NULL) { |
| 423 | if (initialSize <= 0) |
| 424 | initialSize = 1; |
| 425 | list->items = (void **) xmlMalloc( |
| 426 | initialSize * sizeof(void *)); |
| 427 | if (list->items == NULL) { |
| 428 | xmlXPathErrMemory(NULL, |
| 429 | "xmlPointerListCreate: allocating item\n"); |
| 430 | return(-1); |
| 431 | } |
| 432 | list->number = 0; |
| 433 | list->size = initialSize; |
| 434 | } else if (list->size <= list->number) { |
| 435 | list->size *= 2; |
| 436 | list->items = (void **) xmlRealloc(list->items, |
| 437 | list->size * sizeof(void *)); |
| 438 | if (list->items == NULL) { |
| 439 | xmlXPathErrMemory(NULL, |
| 440 | "xmlPointerListCreate: re-allocating item\n"); |
| 441 | list->size = 0; |
| 442 | return(-1); |
| 443 | } |
| 444 | } |
| 445 | list->items[list->number++] = item; |
| 446 | return(0); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * xsltPointerListCreate: |
| 451 | * |
| 452 | * Creates an xsltPointerList structure. |
| 453 | * |
| 454 | * Returns a xsltPointerList structure or NULL in case of an error. |
| 455 | */ |
| 456 | static xmlPointerListPtr |
| 457 | xmlPointerListCreate(int initialSize) |
| 458 | { |
| 459 | xmlPointerListPtr ret; |
| 460 | |
| 461 | ret = xmlMalloc(sizeof(xmlPointerList)); |
| 462 | if (ret == NULL) { |
| 463 | xmlXPathErrMemory(NULL, |
| 464 | "xmlPointerListCreate: allocating item\n"); |
| 465 | return (NULL); |
| 466 | } |
| 467 | memset(ret, 0, sizeof(xmlPointerList)); |
| 468 | if (initialSize > 0) { |
| 469 | xmlPointerListAddSize(ret, NULL, initialSize); |
| 470 | ret->number = 0; |
| 471 | } |
| 472 | return (ret); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * xsltPointerListFree: |
| 477 | * |
| 478 | * Frees the xsltPointerList structure. This does not free |
| 479 | * the content of the list. |
| 480 | */ |
| 481 | static void |
| 482 | xmlPointerListFree(xmlPointerListPtr list) |
| 483 | { |
| 484 | if (list == NULL) |
| 485 | return; |
| 486 | if (list->items != NULL) |
| 487 | xmlFree(list->items); |
| 488 | xmlFree(list); |
| 489 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 490 | |
| 491 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 492 | * * |
| 493 | * Parser Types * |
| 494 | * * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 495 | ************************************************************************/ |
| 496 | |
| 497 | /* |
| 498 | * Types are private: |
| 499 | */ |
| 500 | |
| 501 | typedef enum { |
| 502 | XPATH_OP_END=0, |
| 503 | XPATH_OP_AND, |
| 504 | XPATH_OP_OR, |
| 505 | XPATH_OP_EQUAL, |
| 506 | XPATH_OP_CMP, |
| 507 | XPATH_OP_PLUS, |
| 508 | XPATH_OP_MULT, |
| 509 | XPATH_OP_UNION, |
| 510 | XPATH_OP_ROOT, |
| 511 | XPATH_OP_NODE, |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 512 | XPATH_OP_RESET, /* 10 */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 513 | XPATH_OP_COLLECT, |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 514 | XPATH_OP_VALUE, /* 12 */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 515 | XPATH_OP_VARIABLE, |
| 516 | XPATH_OP_FUNCTION, |
| 517 | XPATH_OP_ARG, |
| 518 | XPATH_OP_PREDICATE, |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 519 | XPATH_OP_FILTER, /* 17 */ |
| 520 | XPATH_OP_SORT /* 18 */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 521 | #ifdef LIBXML_XPTR_ENABLED |
| 522 | ,XPATH_OP_RANGETO |
| 523 | #endif |
| 524 | } xmlXPathOp; |
| 525 | |
| 526 | typedef enum { |
| 527 | AXIS_ANCESTOR = 1, |
| 528 | AXIS_ANCESTOR_OR_SELF, |
| 529 | AXIS_ATTRIBUTE, |
| 530 | AXIS_CHILD, |
| 531 | AXIS_DESCENDANT, |
| 532 | AXIS_DESCENDANT_OR_SELF, |
| 533 | AXIS_FOLLOWING, |
| 534 | AXIS_FOLLOWING_SIBLING, |
| 535 | AXIS_NAMESPACE, |
| 536 | AXIS_PARENT, |
| 537 | AXIS_PRECEDING, |
| 538 | AXIS_PRECEDING_SIBLING, |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 539 | AXIS_SELF |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 540 | } xmlXPathAxisVal; |
| 541 | |
| 542 | typedef enum { |
| 543 | NODE_TEST_NONE = 0, |
| 544 | NODE_TEST_TYPE = 1, |
| 545 | NODE_TEST_PI = 2, |
| 546 | NODE_TEST_ALL = 3, |
| 547 | NODE_TEST_NS = 4, |
| 548 | NODE_TEST_NAME = 5 |
| 549 | } xmlXPathTestVal; |
| 550 | |
| 551 | typedef enum { |
| 552 | NODE_TYPE_NODE = 0, |
| 553 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 554 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 555 | NODE_TYPE_PI = XML_PI_NODE |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 556 | } xmlXPathTypeVal; |
| 557 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 558 | #define XP_REWRITE_DOS_CHILD_ELEM 1 |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 559 | |
| 560 | typedef struct _xmlXPathStepOp xmlXPathStepOp; |
| 561 | typedef xmlXPathStepOp *xmlXPathStepOpPtr; |
| 562 | struct _xmlXPathStepOp { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 563 | xmlXPathOp op; /* The identifier of the operation */ |
| 564 | int ch1; /* First child */ |
| 565 | int ch2; /* Second child */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 566 | int value; |
| 567 | int value2; |
| 568 | int value3; |
| 569 | void *value4; |
| 570 | void *value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 571 | void *cache; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 572 | void *cacheURI; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 573 | int rewriteType; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 574 | }; |
| 575 | |
| 576 | struct _xmlXPathCompExpr { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 577 | int nbStep; /* Number of steps in this expression */ |
| 578 | int maxStep; /* Maximum number of steps allocated */ |
| 579 | xmlXPathStepOp *steps; /* ops for computation of this expression */ |
| 580 | int last; /* index of last step in expression */ |
| 581 | xmlChar *expr; /* the expression being computed */ |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 582 | xmlDictPtr dict; /* the dictionnary to use if any */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 583 | #ifdef DEBUG_EVAL_COUNTS |
| 584 | int nb; |
| 585 | xmlChar *string; |
| 586 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 587 | #ifdef XPATH_STREAMING |
| 588 | xmlPatternPtr stream; |
| 589 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 593 | * * |
| 594 | * Forward declarations * |
| 595 | * * |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 596 | ************************************************************************/ |
| 597 | static void |
| 598 | xmlXPathFreeValueTree(xmlNodeSetPtr obj); |
| 599 | static void |
| 600 | xmlXPathReleaseObject(xmlXPathContextPtr ctxt, xmlXPathObjectPtr obj); |
| 601 | static int |
| 602 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 603 | xmlXPathStepOpPtr op, xmlNodePtr *first); |
| 604 | static int |
| 605 | xmlXPathCompOpEvalToBoolean(xmlXPathParserContextPtr ctxt, |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 606 | xmlXPathStepOpPtr op, |
| 607 | int isPredicate); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 608 | |
| 609 | /************************************************************************ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 610 | * * |
| 611 | * Parser Type functions * |
| 612 | * * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 613 | ************************************************************************/ |
| 614 | |
| 615 | /** |
| 616 | * xmlXPathNewCompExpr: |
| 617 | * |
| 618 | * Create a new Xpath component |
| 619 | * |
| 620 | * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error |
| 621 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 622 | static xmlXPathCompExprPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 623 | xmlXPathNewCompExpr(void) { |
| 624 | xmlXPathCompExprPtr cur; |
| 625 | |
| 626 | cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr)); |
| 627 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 628 | xmlXPathErrMemory(NULL, "allocating component\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 629 | return(NULL); |
| 630 | } |
| 631 | memset(cur, 0, sizeof(xmlXPathCompExpr)); |
| 632 | cur->maxStep = 10; |
| 633 | cur->nbStep = 0; |
| 634 | cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep * |
| 635 | sizeof(xmlXPathStepOp)); |
| 636 | if (cur->steps == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 637 | xmlXPathErrMemory(NULL, "allocating steps\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 638 | xmlFree(cur); |
| 639 | return(NULL); |
| 640 | } |
| 641 | memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp)); |
| 642 | cur->last = -1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 643 | #ifdef DEBUG_EVAL_COUNTS |
| 644 | cur->nb = 0; |
| 645 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 646 | return(cur); |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * xmlXPathFreeCompExpr: |
| 651 | * @comp: an XPATH comp |
| 652 | * |
| 653 | * Free up the memory allocated by @comp |
| 654 | */ |
| 655 | void |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 656 | xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp) |
| 657 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 658 | xmlXPathStepOpPtr op; |
| 659 | int i; |
| 660 | |
| 661 | if (comp == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 662 | return; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 663 | if (comp->dict == NULL) { |
| 664 | for (i = 0; i < comp->nbStep; i++) { |
| 665 | op = &comp->steps[i]; |
| 666 | if (op->value4 != NULL) { |
| 667 | if (op->op == XPATH_OP_VALUE) |
| 668 | xmlXPathFreeObject(op->value4); |
| 669 | else |
| 670 | xmlFree(op->value4); |
| 671 | } |
| 672 | if (op->value5 != NULL) |
| 673 | xmlFree(op->value5); |
| 674 | } |
| 675 | } else { |
| 676 | for (i = 0; i < comp->nbStep; i++) { |
| 677 | op = &comp->steps[i]; |
| 678 | if (op->value4 != NULL) { |
| 679 | if (op->op == XPATH_OP_VALUE) |
| 680 | xmlXPathFreeObject(op->value4); |
| 681 | } |
| 682 | } |
| 683 | xmlDictFree(comp->dict); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 684 | } |
| 685 | if (comp->steps != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 686 | xmlFree(comp->steps); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 687 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 688 | #ifdef DEBUG_EVAL_COUNTS |
| 689 | if (comp->string != NULL) { |
| 690 | xmlFree(comp->string); |
| 691 | } |
| 692 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 693 | #ifdef XPATH_STREAMING |
| 694 | if (comp->stream != NULL) { |
| 695 | xmlFreePatternList(comp->stream); |
| 696 | } |
| 697 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 698 | if (comp->expr != NULL) { |
| 699 | xmlFree(comp->expr); |
| 700 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 701 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 702 | xmlFree(comp); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * xmlXPathCompExprAdd: |
| 707 | * @comp: the compiled expression |
| 708 | * @ch1: first child index |
| 709 | * @ch2: second child index |
| 710 | * @op: an op |
| 711 | * @value: the first int value |
| 712 | * @value2: the second int value |
| 713 | * @value3: the third int value |
| 714 | * @value4: the first string value |
| 715 | * @value5: the second string value |
| 716 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 717 | * Add a step to an XPath Compiled Expression |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 718 | * |
| 719 | * Returns -1 in case of failure, the index otherwise |
| 720 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 721 | static int |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 722 | xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2, |
| 723 | xmlXPathOp op, int value, |
| 724 | int value2, int value3, void *value4, void *value5) { |
| 725 | if (comp->nbStep >= comp->maxStep) { |
| 726 | xmlXPathStepOp *real; |
| 727 | |
| 728 | comp->maxStep *= 2; |
| 729 | real = (xmlXPathStepOp *) xmlRealloc(comp->steps, |
| 730 | comp->maxStep * sizeof(xmlXPathStepOp)); |
| 731 | if (real == NULL) { |
| 732 | comp->maxStep /= 2; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 733 | xmlXPathErrMemory(NULL, "adding step\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 734 | return(-1); |
| 735 | } |
| 736 | comp->steps = real; |
| 737 | } |
| 738 | comp->last = comp->nbStep; |
Kasimier T. Buchcik | 6422d91 | 2006-06-26 14:31:53 +0000 | [diff] [blame] | 739 | comp->steps[comp->nbStep].rewriteType = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 740 | comp->steps[comp->nbStep].ch1 = ch1; |
| 741 | comp->steps[comp->nbStep].ch2 = ch2; |
| 742 | comp->steps[comp->nbStep].op = op; |
| 743 | comp->steps[comp->nbStep].value = value; |
| 744 | comp->steps[comp->nbStep].value2 = value2; |
| 745 | comp->steps[comp->nbStep].value3 = value3; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 746 | if ((comp->dict != NULL) && |
| 747 | ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) || |
| 748 | (op == XPATH_OP_COLLECT))) { |
| 749 | if (value4 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 750 | comp->steps[comp->nbStep].value4 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 751 | (void *)xmlDictLookup(comp->dict, value4, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 752 | xmlFree(value4); |
| 753 | } else |
| 754 | comp->steps[comp->nbStep].value4 = NULL; |
| 755 | if (value5 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 756 | comp->steps[comp->nbStep].value5 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 757 | (void *)xmlDictLookup(comp->dict, value5, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 758 | xmlFree(value5); |
| 759 | } else |
| 760 | comp->steps[comp->nbStep].value5 = NULL; |
| 761 | } else { |
| 762 | comp->steps[comp->nbStep].value4 = value4; |
| 763 | comp->steps[comp->nbStep].value5 = value5; |
| 764 | } |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 765 | comp->steps[comp->nbStep].cache = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 766 | return(comp->nbStep++); |
| 767 | } |
| 768 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 769 | /** |
| 770 | * xmlXPathCompSwap: |
| 771 | * @comp: the compiled expression |
| 772 | * @op: operation index |
| 773 | * |
| 774 | * Swaps 2 operations in the compiled expression |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 775 | */ |
| 776 | static void |
| 777 | xmlXPathCompSwap(xmlXPathStepOpPtr op) { |
| 778 | int tmp; |
| 779 | |
Daniel Veillard | bc6f759 | 2002-04-16 07:49:59 +0000 | [diff] [blame] | 780 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 781 | /* |
| 782 | * Since this manipulates possibly shared variables, this is |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 783 | * disabled if one detects that the library is used in a multithreaded |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 784 | * application |
| 785 | */ |
| 786 | if (xmlXPathDisableOptimizer) |
| 787 | return; |
| 788 | #endif |
| 789 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 790 | tmp = op->ch1; |
| 791 | op->ch1 = op->ch2; |
| 792 | op->ch2 = tmp; |
| 793 | } |
| 794 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 795 | #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \ |
| 796 | xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \ |
| 797 | (op), (val), (val2), (val3), (val4), (val5)) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 798 | #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \ |
| 799 | xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \ |
| 800 | (op), (val), (val2), (val3), (val4), (val5)) |
| 801 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 802 | #define PUSH_LEAVE_EXPR(op, val, val2) \ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 803 | xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 804 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 805 | #define PUSH_UNARY_EXPR(op, ch, val, val2) \ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 806 | xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 807 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 808 | #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 809 | xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \ |
| 810 | (val), (val2), 0 ,NULL ,NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 811 | |
| 812 | /************************************************************************ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 813 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 814 | * XPath object cache structures * |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 815 | * * |
| 816 | ************************************************************************/ |
| 817 | |
| 818 | /* #define XP_DEFAULT_CACHE_ON */ |
| 819 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 820 | #define XP_HAS_CACHE(c) ((c != NULL) && ((c)->cache != NULL)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 821 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 822 | typedef struct _xmlXPathContextCache xmlXPathContextCache; |
| 823 | typedef xmlXPathContextCache *xmlXPathContextCachePtr; |
| 824 | struct _xmlXPathContextCache { |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 825 | xmlPointerListPtr nodesetObjs; /* contains xmlXPathObjectPtr */ |
| 826 | xmlPointerListPtr stringObjs; /* contains xmlXPathObjectPtr */ |
| 827 | xmlPointerListPtr booleanObjs; /* contains xmlXPathObjectPtr */ |
| 828 | xmlPointerListPtr numberObjs; /* contains xmlXPathObjectPtr */ |
| 829 | xmlPointerListPtr miscObjs; /* contains xmlXPathObjectPtr */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 830 | int maxNodeset; |
| 831 | int maxString; |
| 832 | int maxBoolean; |
| 833 | int maxNumber; |
| 834 | int maxMisc; |
| 835 | #ifdef XP_DEBUG_OBJ_USAGE |
| 836 | int dbgCachedAll; |
| 837 | int dbgCachedNodeset; |
| 838 | int dbgCachedString; |
| 839 | int dbgCachedBool; |
| 840 | int dbgCachedNumber; |
| 841 | int dbgCachedPoint; |
| 842 | int dbgCachedRange; |
| 843 | int dbgCachedLocset; |
| 844 | int dbgCachedUsers; |
| 845 | int dbgCachedXSLTTree; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 846 | int dbgCachedUndefined; |
| 847 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 848 | |
| 849 | int dbgReusedAll; |
| 850 | int dbgReusedNodeset; |
| 851 | int dbgReusedString; |
| 852 | int dbgReusedBool; |
| 853 | int dbgReusedNumber; |
| 854 | int dbgReusedPoint; |
| 855 | int dbgReusedRange; |
| 856 | int dbgReusedLocset; |
| 857 | int dbgReusedUsers; |
| 858 | int dbgReusedXSLTTree; |
| 859 | int dbgReusedUndefined; |
| 860 | |
| 861 | #endif |
| 862 | }; |
| 863 | |
| 864 | /************************************************************************ |
| 865 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 866 | * Debugging related functions * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 867 | * * |
| 868 | ************************************************************************/ |
| 869 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 870 | #define STRANGE \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 871 | xmlGenericError(xmlGenericErrorContext, \ |
| 872 | "Internal error at %s:%d\n", \ |
| 873 | __FILE__, __LINE__); |
| 874 | |
| 875 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 876 | static void |
| 877 | xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 878 | int i; |
| 879 | char shift[100]; |
| 880 | |
| 881 | for (i = 0;((i < depth) && (i < 25));i++) |
| 882 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 883 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 884 | if (cur == NULL) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 885 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 886 | fprintf(output, "Node is NULL !\n"); |
| 887 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 888 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 892 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 893 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 894 | fprintf(output, " /\n"); |
| 895 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 896 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 897 | else |
| 898 | xmlDebugDumpOneNode(output, cur, depth); |
| 899 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 900 | static void |
| 901 | xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 902 | xmlNodePtr tmp; |
| 903 | int i; |
| 904 | char shift[100]; |
| 905 | |
| 906 | for (i = 0;((i < depth) && (i < 25));i++) |
| 907 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 908 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 909 | if (cur == NULL) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 910 | fprintf(output, "%s", shift); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 911 | fprintf(output, "Node is NULL !\n"); |
| 912 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 913 | |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | while (cur != NULL) { |
| 917 | tmp = cur; |
| 918 | cur = cur->next; |
| 919 | xmlDebugDumpOneNode(output, tmp, depth); |
| 920 | } |
| 921 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 922 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 923 | static void |
| 924 | xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 925 | int i; |
| 926 | char shift[100]; |
| 927 | |
| 928 | for (i = 0;((i < depth) && (i < 25));i++) |
| 929 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 930 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 931 | |
| 932 | if (cur == NULL) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 933 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 934 | fprintf(output, "NodeSet is NULL !\n"); |
| 935 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 936 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 939 | if (cur != NULL) { |
| 940 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 941 | for (i = 0;i < cur->nodeNr;i++) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 942 | fprintf(output, "%s", shift); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 943 | fprintf(output, "%d", i + 1); |
| 944 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 945 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 949 | static void |
| 950 | xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 951 | int i; |
| 952 | char shift[100]; |
| 953 | |
| 954 | for (i = 0;((i < depth) && (i < 25));i++) |
| 955 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 956 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 957 | |
| 958 | if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 959 | fprintf(output, "%s", shift); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 960 | fprintf(output, "Value Tree is NULL !\n"); |
| 961 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 962 | |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 965 | fprintf(output, "%s", shift); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 966 | fprintf(output, "%d", i + 1); |
| 967 | xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1); |
| 968 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 969 | #if defined(LIBXML_XPTR_ENABLED) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 970 | static void |
| 971 | xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 972 | int i; |
| 973 | char shift[100]; |
| 974 | |
| 975 | for (i = 0;((i < depth) && (i < 25));i++) |
| 976 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 977 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 978 | |
| 979 | if (cur == NULL) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 980 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 981 | fprintf(output, "LocationSet is NULL !\n"); |
| 982 | return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 983 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | for (i = 0;i < cur->locNr;i++) { |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 987 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 988 | fprintf(output, "%d : ", i + 1); |
| 989 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 990 | } |
| 991 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 992 | #endif /* LIBXML_XPTR_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 993 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 994 | /** |
| 995 | * xmlXPathDebugDumpObject: |
| 996 | * @output: the FILE * to dump the output |
| 997 | * @cur: the object to inspect |
| 998 | * @depth: indentation level |
| 999 | * |
| 1000 | * Dump the content of the object for debugging purposes |
| 1001 | */ |
| 1002 | void |
| 1003 | xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1004 | int i; |
| 1005 | char shift[100]; |
| 1006 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1007 | if (output == NULL) return; |
| 1008 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1009 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1010 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1011 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1012 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1013 | |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1014 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1015 | |
| 1016 | if (cur == NULL) { |
| 1017 | fprintf(output, "Object is empty (NULL)\n"); |
| 1018 | return; |
| 1019 | } |
| 1020 | switch(cur->type) { |
| 1021 | case XPATH_UNDEFINED: |
| 1022 | fprintf(output, "Object is uninitialized\n"); |
| 1023 | break; |
| 1024 | case XPATH_NODESET: |
| 1025 | fprintf(output, "Object is a Node Set :\n"); |
| 1026 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 1027 | break; |
| 1028 | case XPATH_XSLT_TREE: |
| 1029 | fprintf(output, "Object is an XSLT value tree :\n"); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1030 | xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1031 | break; |
| 1032 | case XPATH_BOOLEAN: |
| 1033 | fprintf(output, "Object is a Boolean : "); |
| 1034 | if (cur->boolval) fprintf(output, "true\n"); |
| 1035 | else fprintf(output, "false\n"); |
| 1036 | break; |
| 1037 | case XPATH_NUMBER: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1038 | switch (xmlXPathIsInf(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 1039 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1040 | fprintf(output, "Object is a number : Infinity\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 1041 | break; |
| 1042 | case -1: |
| 1043 | fprintf(output, "Object is a number : -Infinity\n"); |
| 1044 | break; |
| 1045 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1046 | if (xmlXPathIsNaN(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 1047 | fprintf(output, "Object is a number : NaN\n"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 1048 | } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) { |
| 1049 | fprintf(output, "Object is a number : 0\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 1050 | } else { |
| 1051 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 1052 | } |
| 1053 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1054 | break; |
| 1055 | case XPATH_STRING: |
| 1056 | fprintf(output, "Object is a string : "); |
| 1057 | xmlDebugDumpString(output, cur->stringval); |
| 1058 | fprintf(output, "\n"); |
| 1059 | break; |
| 1060 | case XPATH_POINT: |
| 1061 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 1062 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 1063 | fprintf(output, "\n"); |
| 1064 | break; |
| 1065 | case XPATH_RANGE: |
| 1066 | if ((cur->user2 == NULL) || |
| 1067 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 1068 | fprintf(output, "Object is a collapsed range :\n"); |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1069 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1070 | if (cur->index >= 0) |
| 1071 | fprintf(output, "index %d in ", cur->index); |
| 1072 | fprintf(output, "node\n"); |
| 1073 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 1074 | depth + 1); |
| 1075 | } else { |
| 1076 | fprintf(output, "Object is a range :\n"); |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1077 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1078 | fprintf(output, "From "); |
| 1079 | if (cur->index >= 0) |
| 1080 | fprintf(output, "index %d in ", cur->index); |
| 1081 | fprintf(output, "node\n"); |
| 1082 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 1083 | depth + 1); |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1084 | fprintf(output, "%s", shift); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1085 | fprintf(output, "To "); |
| 1086 | if (cur->index2 >= 0) |
| 1087 | fprintf(output, "index %d in ", cur->index2); |
| 1088 | fprintf(output, "node\n"); |
| 1089 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 1090 | depth + 1); |
| 1091 | fprintf(output, "\n"); |
| 1092 | } |
| 1093 | break; |
| 1094 | case XPATH_LOCATIONSET: |
| 1095 | #if defined(LIBXML_XPTR_ENABLED) |
| 1096 | fprintf(output, "Object is a Location Set:\n"); |
| 1097 | xmlXPathDebugDumpLocationSet(output, |
| 1098 | (xmlLocationSetPtr) cur->user, depth); |
| 1099 | #endif |
| 1100 | break; |
| 1101 | case XPATH_USERS: |
| 1102 | fprintf(output, "Object is user defined\n"); |
| 1103 | break; |
| 1104 | } |
| 1105 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1106 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1107 | static void |
| 1108 | xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1109 | xmlXPathStepOpPtr op, int depth) { |
| 1110 | int i; |
| 1111 | char shift[100]; |
| 1112 | |
| 1113 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1114 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1115 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1116 | |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1117 | fprintf(output, "%s", shift); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1118 | if (op == NULL) { |
| 1119 | fprintf(output, "Step is NULL\n"); |
| 1120 | return; |
| 1121 | } |
| 1122 | switch (op->op) { |
| 1123 | case XPATH_OP_END: |
| 1124 | fprintf(output, "END"); break; |
| 1125 | case XPATH_OP_AND: |
| 1126 | fprintf(output, "AND"); break; |
| 1127 | case XPATH_OP_OR: |
| 1128 | fprintf(output, "OR"); break; |
| 1129 | case XPATH_OP_EQUAL: |
| 1130 | if (op->value) |
| 1131 | fprintf(output, "EQUAL ="); |
| 1132 | else |
| 1133 | fprintf(output, "EQUAL !="); |
| 1134 | break; |
| 1135 | case XPATH_OP_CMP: |
| 1136 | if (op->value) |
| 1137 | fprintf(output, "CMP <"); |
| 1138 | else |
| 1139 | fprintf(output, "CMP >"); |
| 1140 | if (!op->value2) |
| 1141 | fprintf(output, "="); |
| 1142 | break; |
| 1143 | case XPATH_OP_PLUS: |
| 1144 | if (op->value == 0) |
| 1145 | fprintf(output, "PLUS -"); |
| 1146 | else if (op->value == 1) |
| 1147 | fprintf(output, "PLUS +"); |
| 1148 | else if (op->value == 2) |
| 1149 | fprintf(output, "PLUS unary -"); |
| 1150 | else if (op->value == 3) |
| 1151 | fprintf(output, "PLUS unary - -"); |
| 1152 | break; |
| 1153 | case XPATH_OP_MULT: |
| 1154 | if (op->value == 0) |
| 1155 | fprintf(output, "MULT *"); |
| 1156 | else if (op->value == 1) |
| 1157 | fprintf(output, "MULT div"); |
| 1158 | else |
| 1159 | fprintf(output, "MULT mod"); |
| 1160 | break; |
| 1161 | case XPATH_OP_UNION: |
| 1162 | fprintf(output, "UNION"); break; |
| 1163 | case XPATH_OP_ROOT: |
| 1164 | fprintf(output, "ROOT"); break; |
| 1165 | case XPATH_OP_NODE: |
| 1166 | fprintf(output, "NODE"); break; |
| 1167 | case XPATH_OP_RESET: |
| 1168 | fprintf(output, "RESET"); break; |
| 1169 | case XPATH_OP_SORT: |
| 1170 | fprintf(output, "SORT"); break; |
| 1171 | case XPATH_OP_COLLECT: { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1172 | xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value; |
| 1173 | xmlXPathTestVal test = (xmlXPathTestVal)op->value2; |
| 1174 | xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1175 | const xmlChar *prefix = op->value4; |
| 1176 | const xmlChar *name = op->value5; |
| 1177 | |
| 1178 | fprintf(output, "COLLECT "); |
| 1179 | switch (axis) { |
| 1180 | case AXIS_ANCESTOR: |
| 1181 | fprintf(output, " 'ancestors' "); break; |
| 1182 | case AXIS_ANCESTOR_OR_SELF: |
| 1183 | fprintf(output, " 'ancestors-or-self' "); break; |
| 1184 | case AXIS_ATTRIBUTE: |
| 1185 | fprintf(output, " 'attributes' "); break; |
| 1186 | case AXIS_CHILD: |
| 1187 | fprintf(output, " 'child' "); break; |
| 1188 | case AXIS_DESCENDANT: |
| 1189 | fprintf(output, " 'descendant' "); break; |
| 1190 | case AXIS_DESCENDANT_OR_SELF: |
| 1191 | fprintf(output, " 'descendant-or-self' "); break; |
| 1192 | case AXIS_FOLLOWING: |
| 1193 | fprintf(output, " 'following' "); break; |
| 1194 | case AXIS_FOLLOWING_SIBLING: |
| 1195 | fprintf(output, " 'following-siblings' "); break; |
| 1196 | case AXIS_NAMESPACE: |
| 1197 | fprintf(output, " 'namespace' "); break; |
| 1198 | case AXIS_PARENT: |
| 1199 | fprintf(output, " 'parent' "); break; |
| 1200 | case AXIS_PRECEDING: |
| 1201 | fprintf(output, " 'preceding' "); break; |
| 1202 | case AXIS_PRECEDING_SIBLING: |
| 1203 | fprintf(output, " 'preceding-sibling' "); break; |
| 1204 | case AXIS_SELF: |
| 1205 | fprintf(output, " 'self' "); break; |
| 1206 | } |
| 1207 | switch (test) { |
| 1208 | case NODE_TEST_NONE: |
| 1209 | fprintf(output, "'none' "); break; |
| 1210 | case NODE_TEST_TYPE: |
| 1211 | fprintf(output, "'type' "); break; |
| 1212 | case NODE_TEST_PI: |
| 1213 | fprintf(output, "'PI' "); break; |
| 1214 | case NODE_TEST_ALL: |
| 1215 | fprintf(output, "'all' "); break; |
| 1216 | case NODE_TEST_NS: |
| 1217 | fprintf(output, "'namespace' "); break; |
| 1218 | case NODE_TEST_NAME: |
| 1219 | fprintf(output, "'name' "); break; |
| 1220 | } |
| 1221 | switch (type) { |
| 1222 | case NODE_TYPE_NODE: |
| 1223 | fprintf(output, "'node' "); break; |
| 1224 | case NODE_TYPE_COMMENT: |
| 1225 | fprintf(output, "'comment' "); break; |
| 1226 | case NODE_TYPE_TEXT: |
| 1227 | fprintf(output, "'text' "); break; |
| 1228 | case NODE_TYPE_PI: |
| 1229 | fprintf(output, "'PI' "); break; |
| 1230 | } |
| 1231 | if (prefix != NULL) |
| 1232 | fprintf(output, "%s:", prefix); |
| 1233 | if (name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1234 | fprintf(output, "%s", (const char *) name); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1235 | break; |
| 1236 | |
| 1237 | } |
| 1238 | case XPATH_OP_VALUE: { |
| 1239 | xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4; |
| 1240 | |
| 1241 | fprintf(output, "ELEM "); |
| 1242 | xmlXPathDebugDumpObject(output, object, 0); |
| 1243 | goto finish; |
| 1244 | } |
| 1245 | case XPATH_OP_VARIABLE: { |
| 1246 | const xmlChar *prefix = op->value5; |
| 1247 | const xmlChar *name = op->value4; |
| 1248 | |
| 1249 | if (prefix != NULL) |
| 1250 | fprintf(output, "VARIABLE %s:%s", prefix, name); |
| 1251 | else |
| 1252 | fprintf(output, "VARIABLE %s", name); |
| 1253 | break; |
| 1254 | } |
| 1255 | case XPATH_OP_FUNCTION: { |
| 1256 | int nbargs = op->value; |
| 1257 | const xmlChar *prefix = op->value5; |
| 1258 | const xmlChar *name = op->value4; |
| 1259 | |
| 1260 | if (prefix != NULL) |
| 1261 | fprintf(output, "FUNCTION %s:%s(%d args)", |
| 1262 | prefix, name, nbargs); |
| 1263 | else |
| 1264 | fprintf(output, "FUNCTION %s(%d args)", name, nbargs); |
| 1265 | break; |
| 1266 | } |
| 1267 | case XPATH_OP_ARG: fprintf(output, "ARG"); break; |
| 1268 | case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1269 | case XPATH_OP_FILTER: fprintf(output, "FILTER"); break; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 1270 | #ifdef LIBXML_XPTR_ENABLED |
| 1271 | case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break; |
| 1272 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1273 | default: |
| 1274 | fprintf(output, "UNKNOWN %d\n", op->op); return; |
| 1275 | } |
| 1276 | fprintf(output, "\n"); |
| 1277 | finish: |
| 1278 | if (op->ch1 >= 0) |
| 1279 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1); |
| 1280 | if (op->ch2 >= 0) |
| 1281 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1); |
| 1282 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1283 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1284 | /** |
| 1285 | * xmlXPathDebugDumpCompExpr: |
| 1286 | * @output: the FILE * for the output |
| 1287 | * @comp: the precompiled XPath expression |
| 1288 | * @depth: the indentation level. |
| 1289 | * |
| 1290 | * Dumps the tree of the compiled XPath expression. |
| 1291 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1292 | void |
| 1293 | xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, |
| 1294 | int depth) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1295 | int i; |
| 1296 | char shift[100]; |
| 1297 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1298 | if ((output == NULL) || (comp == NULL)) return; |
| 1299 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1300 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1301 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1302 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1303 | |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 1304 | fprintf(output, "%s", shift); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1305 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1306 | fprintf(output, "Compiled Expression : %d elements\n", |
| 1307 | comp->nbStep); |
| 1308 | i = comp->last; |
| 1309 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1); |
| 1310 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1311 | |
| 1312 | #ifdef XP_DEBUG_OBJ_USAGE |
| 1313 | |
| 1314 | /* |
| 1315 | * XPath object usage related debugging variables. |
| 1316 | */ |
| 1317 | static int xmlXPathDebugObjCounterUndefined = 0; |
| 1318 | static int xmlXPathDebugObjCounterNodeset = 0; |
| 1319 | static int xmlXPathDebugObjCounterBool = 0; |
| 1320 | static int xmlXPathDebugObjCounterNumber = 0; |
| 1321 | static int xmlXPathDebugObjCounterString = 0; |
| 1322 | static int xmlXPathDebugObjCounterPoint = 0; |
| 1323 | static int xmlXPathDebugObjCounterRange = 0; |
| 1324 | static int xmlXPathDebugObjCounterLocset = 0; |
| 1325 | static int xmlXPathDebugObjCounterUsers = 0; |
| 1326 | static int xmlXPathDebugObjCounterXSLTTree = 0; |
| 1327 | static int xmlXPathDebugObjCounterAll = 0; |
| 1328 | |
| 1329 | static int xmlXPathDebugObjTotalUndefined = 0; |
| 1330 | static int xmlXPathDebugObjTotalNodeset = 0; |
| 1331 | static int xmlXPathDebugObjTotalBool = 0; |
| 1332 | static int xmlXPathDebugObjTotalNumber = 0; |
| 1333 | static int xmlXPathDebugObjTotalString = 0; |
| 1334 | static int xmlXPathDebugObjTotalPoint = 0; |
| 1335 | static int xmlXPathDebugObjTotalRange = 0; |
| 1336 | static int xmlXPathDebugObjTotalLocset = 0; |
| 1337 | static int xmlXPathDebugObjTotalUsers = 0; |
| 1338 | static int xmlXPathDebugObjTotalXSLTTree = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1339 | static int xmlXPathDebugObjTotalAll = 0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1340 | |
| 1341 | static int xmlXPathDebugObjMaxUndefined = 0; |
| 1342 | static int xmlXPathDebugObjMaxNodeset = 0; |
| 1343 | static int xmlXPathDebugObjMaxBool = 0; |
| 1344 | static int xmlXPathDebugObjMaxNumber = 0; |
| 1345 | static int xmlXPathDebugObjMaxString = 0; |
| 1346 | static int xmlXPathDebugObjMaxPoint = 0; |
| 1347 | static int xmlXPathDebugObjMaxRange = 0; |
| 1348 | static int xmlXPathDebugObjMaxLocset = 0; |
| 1349 | static int xmlXPathDebugObjMaxUsers = 0; |
| 1350 | static int xmlXPathDebugObjMaxXSLTTree = 0; |
| 1351 | static int xmlXPathDebugObjMaxAll = 0; |
| 1352 | |
| 1353 | /* REVISIT TODO: Make this static when committing */ |
| 1354 | static void |
| 1355 | xmlXPathDebugObjUsageReset(xmlXPathContextPtr ctxt) |
| 1356 | { |
| 1357 | if (ctxt != NULL) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1358 | if (ctxt->cache != NULL) { |
| 1359 | xmlXPathContextCachePtr cache = |
| 1360 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1361 | |
| 1362 | cache->dbgCachedAll = 0; |
| 1363 | cache->dbgCachedNodeset = 0; |
| 1364 | cache->dbgCachedString = 0; |
| 1365 | cache->dbgCachedBool = 0; |
| 1366 | cache->dbgCachedNumber = 0; |
| 1367 | cache->dbgCachedPoint = 0; |
| 1368 | cache->dbgCachedRange = 0; |
| 1369 | cache->dbgCachedLocset = 0; |
| 1370 | cache->dbgCachedUsers = 0; |
| 1371 | cache->dbgCachedXSLTTree = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1372 | cache->dbgCachedUndefined = 0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1373 | |
| 1374 | cache->dbgReusedAll = 0; |
| 1375 | cache->dbgReusedNodeset = 0; |
| 1376 | cache->dbgReusedString = 0; |
| 1377 | cache->dbgReusedBool = 0; |
| 1378 | cache->dbgReusedNumber = 0; |
| 1379 | cache->dbgReusedPoint = 0; |
| 1380 | cache->dbgReusedRange = 0; |
| 1381 | cache->dbgReusedLocset = 0; |
| 1382 | cache->dbgReusedUsers = 0; |
| 1383 | cache->dbgReusedXSLTTree = 0; |
| 1384 | cache->dbgReusedUndefined = 0; |
| 1385 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1386 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1387 | |
| 1388 | xmlXPathDebugObjCounterUndefined = 0; |
| 1389 | xmlXPathDebugObjCounterNodeset = 0; |
| 1390 | xmlXPathDebugObjCounterBool = 0; |
| 1391 | xmlXPathDebugObjCounterNumber = 0; |
| 1392 | xmlXPathDebugObjCounterString = 0; |
| 1393 | xmlXPathDebugObjCounterPoint = 0; |
| 1394 | xmlXPathDebugObjCounterRange = 0; |
| 1395 | xmlXPathDebugObjCounterLocset = 0; |
| 1396 | xmlXPathDebugObjCounterUsers = 0; |
| 1397 | xmlXPathDebugObjCounterXSLTTree = 0; |
| 1398 | xmlXPathDebugObjCounterAll = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1399 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1400 | xmlXPathDebugObjTotalUndefined = 0; |
| 1401 | xmlXPathDebugObjTotalNodeset = 0; |
| 1402 | xmlXPathDebugObjTotalBool = 0; |
| 1403 | xmlXPathDebugObjTotalNumber = 0; |
| 1404 | xmlXPathDebugObjTotalString = 0; |
| 1405 | xmlXPathDebugObjTotalPoint = 0; |
| 1406 | xmlXPathDebugObjTotalRange = 0; |
| 1407 | xmlXPathDebugObjTotalLocset = 0; |
| 1408 | xmlXPathDebugObjTotalUsers = 0; |
| 1409 | xmlXPathDebugObjTotalXSLTTree = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1410 | xmlXPathDebugObjTotalAll = 0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1411 | |
| 1412 | xmlXPathDebugObjMaxUndefined = 0; |
| 1413 | xmlXPathDebugObjMaxNodeset = 0; |
| 1414 | xmlXPathDebugObjMaxBool = 0; |
| 1415 | xmlXPathDebugObjMaxNumber = 0; |
| 1416 | xmlXPathDebugObjMaxString = 0; |
| 1417 | xmlXPathDebugObjMaxPoint = 0; |
| 1418 | xmlXPathDebugObjMaxRange = 0; |
| 1419 | xmlXPathDebugObjMaxLocset = 0; |
| 1420 | xmlXPathDebugObjMaxUsers = 0; |
| 1421 | xmlXPathDebugObjMaxXSLTTree = 0; |
| 1422 | xmlXPathDebugObjMaxAll = 0; |
| 1423 | |
| 1424 | } |
| 1425 | |
| 1426 | static void |
| 1427 | xmlXPathDebugObjUsageRequested(xmlXPathContextPtr ctxt, |
| 1428 | xmlXPathObjectType objType) |
| 1429 | { |
| 1430 | int isCached = 0; |
| 1431 | |
| 1432 | if (ctxt != NULL) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1433 | if (ctxt->cache != NULL) { |
| 1434 | xmlXPathContextCachePtr cache = |
| 1435 | (xmlXPathContextCachePtr) ctxt->cache; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1436 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1437 | isCached = 1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1438 | |
| 1439 | cache->dbgReusedAll++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1440 | switch (objType) { |
| 1441 | case XPATH_UNDEFINED: |
| 1442 | cache->dbgReusedUndefined++; |
| 1443 | break; |
| 1444 | case XPATH_NODESET: |
| 1445 | cache->dbgReusedNodeset++; |
| 1446 | break; |
| 1447 | case XPATH_BOOLEAN: |
| 1448 | cache->dbgReusedBool++; |
| 1449 | break; |
| 1450 | case XPATH_NUMBER: |
| 1451 | cache->dbgReusedNumber++; |
| 1452 | break; |
| 1453 | case XPATH_STRING: |
| 1454 | cache->dbgReusedString++; |
| 1455 | break; |
| 1456 | case XPATH_POINT: |
| 1457 | cache->dbgReusedPoint++; |
| 1458 | break; |
| 1459 | case XPATH_RANGE: |
| 1460 | cache->dbgReusedRange++; |
| 1461 | break; |
| 1462 | case XPATH_LOCATIONSET: |
| 1463 | cache->dbgReusedLocset++; |
| 1464 | break; |
| 1465 | case XPATH_USERS: |
| 1466 | cache->dbgReusedUsers++; |
| 1467 | break; |
| 1468 | case XPATH_XSLT_TREE: |
| 1469 | cache->dbgReusedXSLTTree++; |
| 1470 | break; |
| 1471 | default: |
| 1472 | break; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1473 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | switch (objType) { |
| 1478 | case XPATH_UNDEFINED: |
| 1479 | if (! isCached) |
| 1480 | xmlXPathDebugObjTotalUndefined++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1481 | xmlXPathDebugObjCounterUndefined++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1482 | if (xmlXPathDebugObjCounterUndefined > |
| 1483 | xmlXPathDebugObjMaxUndefined) |
| 1484 | xmlXPathDebugObjMaxUndefined = |
| 1485 | xmlXPathDebugObjCounterUndefined; |
| 1486 | break; |
| 1487 | case XPATH_NODESET: |
| 1488 | if (! isCached) |
| 1489 | xmlXPathDebugObjTotalNodeset++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1490 | xmlXPathDebugObjCounterNodeset++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1491 | if (xmlXPathDebugObjCounterNodeset > |
| 1492 | xmlXPathDebugObjMaxNodeset) |
| 1493 | xmlXPathDebugObjMaxNodeset = |
| 1494 | xmlXPathDebugObjCounterNodeset; |
| 1495 | break; |
| 1496 | case XPATH_BOOLEAN: |
| 1497 | if (! isCached) |
| 1498 | xmlXPathDebugObjTotalBool++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1499 | xmlXPathDebugObjCounterBool++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1500 | if (xmlXPathDebugObjCounterBool > |
| 1501 | xmlXPathDebugObjMaxBool) |
| 1502 | xmlXPathDebugObjMaxBool = |
| 1503 | xmlXPathDebugObjCounterBool; |
| 1504 | break; |
| 1505 | case XPATH_NUMBER: |
| 1506 | if (! isCached) |
| 1507 | xmlXPathDebugObjTotalNumber++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1508 | xmlXPathDebugObjCounterNumber++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1509 | if (xmlXPathDebugObjCounterNumber > |
| 1510 | xmlXPathDebugObjMaxNumber) |
| 1511 | xmlXPathDebugObjMaxNumber = |
| 1512 | xmlXPathDebugObjCounterNumber; |
| 1513 | break; |
| 1514 | case XPATH_STRING: |
| 1515 | if (! isCached) |
| 1516 | xmlXPathDebugObjTotalString++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1517 | xmlXPathDebugObjCounterString++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1518 | if (xmlXPathDebugObjCounterString > |
| 1519 | xmlXPathDebugObjMaxString) |
| 1520 | xmlXPathDebugObjMaxString = |
| 1521 | xmlXPathDebugObjCounterString; |
| 1522 | break; |
| 1523 | case XPATH_POINT: |
| 1524 | if (! isCached) |
| 1525 | xmlXPathDebugObjTotalPoint++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1526 | xmlXPathDebugObjCounterPoint++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1527 | if (xmlXPathDebugObjCounterPoint > |
| 1528 | xmlXPathDebugObjMaxPoint) |
| 1529 | xmlXPathDebugObjMaxPoint = |
| 1530 | xmlXPathDebugObjCounterPoint; |
| 1531 | break; |
| 1532 | case XPATH_RANGE: |
| 1533 | if (! isCached) |
| 1534 | xmlXPathDebugObjTotalRange++; |
| 1535 | xmlXPathDebugObjCounterRange++; |
| 1536 | if (xmlXPathDebugObjCounterRange > |
| 1537 | xmlXPathDebugObjMaxRange) |
| 1538 | xmlXPathDebugObjMaxRange = |
| 1539 | xmlXPathDebugObjCounterRange; |
| 1540 | break; |
| 1541 | case XPATH_LOCATIONSET: |
| 1542 | if (! isCached) |
| 1543 | xmlXPathDebugObjTotalLocset++; |
| 1544 | xmlXPathDebugObjCounterLocset++; |
| 1545 | if (xmlXPathDebugObjCounterLocset > |
| 1546 | xmlXPathDebugObjMaxLocset) |
| 1547 | xmlXPathDebugObjMaxLocset = |
| 1548 | xmlXPathDebugObjCounterLocset; |
| 1549 | break; |
| 1550 | case XPATH_USERS: |
| 1551 | if (! isCached) |
| 1552 | xmlXPathDebugObjTotalUsers++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1553 | xmlXPathDebugObjCounterUsers++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1554 | if (xmlXPathDebugObjCounterUsers > |
| 1555 | xmlXPathDebugObjMaxUsers) |
| 1556 | xmlXPathDebugObjMaxUsers = |
| 1557 | xmlXPathDebugObjCounterUsers; |
| 1558 | break; |
| 1559 | case XPATH_XSLT_TREE: |
| 1560 | if (! isCached) |
| 1561 | xmlXPathDebugObjTotalXSLTTree++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1562 | xmlXPathDebugObjCounterXSLTTree++; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1563 | if (xmlXPathDebugObjCounterXSLTTree > |
| 1564 | xmlXPathDebugObjMaxXSLTTree) |
| 1565 | xmlXPathDebugObjMaxXSLTTree = |
| 1566 | xmlXPathDebugObjCounterXSLTTree; |
| 1567 | break; |
| 1568 | default: |
| 1569 | break; |
| 1570 | } |
| 1571 | if (! isCached) |
| 1572 | xmlXPathDebugObjTotalAll++; |
| 1573 | xmlXPathDebugObjCounterAll++; |
| 1574 | if (xmlXPathDebugObjCounterAll > |
| 1575 | xmlXPathDebugObjMaxAll) |
| 1576 | xmlXPathDebugObjMaxAll = |
| 1577 | xmlXPathDebugObjCounterAll; |
| 1578 | } |
| 1579 | |
| 1580 | static void |
| 1581 | xmlXPathDebugObjUsageReleased(xmlXPathContextPtr ctxt, |
| 1582 | xmlXPathObjectType objType) |
| 1583 | { |
| 1584 | int isCached = 0; |
| 1585 | |
| 1586 | if (ctxt != NULL) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1587 | if (ctxt->cache != NULL) { |
| 1588 | xmlXPathContextCachePtr cache = |
| 1589 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1590 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1591 | isCached = 1; |
| 1592 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1593 | cache->dbgCachedAll++; |
| 1594 | switch (objType) { |
| 1595 | case XPATH_UNDEFINED: |
| 1596 | cache->dbgCachedUndefined++; |
| 1597 | break; |
| 1598 | case XPATH_NODESET: |
| 1599 | cache->dbgCachedNodeset++; |
| 1600 | break; |
| 1601 | case XPATH_BOOLEAN: |
| 1602 | cache->dbgCachedBool++; |
| 1603 | break; |
| 1604 | case XPATH_NUMBER: |
| 1605 | cache->dbgCachedNumber++; |
| 1606 | break; |
| 1607 | case XPATH_STRING: |
| 1608 | cache->dbgCachedString++; |
| 1609 | break; |
| 1610 | case XPATH_POINT: |
| 1611 | cache->dbgCachedPoint++; |
| 1612 | break; |
| 1613 | case XPATH_RANGE: |
| 1614 | cache->dbgCachedRange++; |
| 1615 | break; |
| 1616 | case XPATH_LOCATIONSET: |
| 1617 | cache->dbgCachedLocset++; |
| 1618 | break; |
| 1619 | case XPATH_USERS: |
| 1620 | cache->dbgCachedUsers++; |
| 1621 | break; |
| 1622 | case XPATH_XSLT_TREE: |
| 1623 | cache->dbgCachedXSLTTree++; |
| 1624 | break; |
| 1625 | default: |
| 1626 | break; |
| 1627 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1628 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1629 | } |
| 1630 | } |
| 1631 | switch (objType) { |
| 1632 | case XPATH_UNDEFINED: |
| 1633 | xmlXPathDebugObjCounterUndefined--; |
| 1634 | break; |
| 1635 | case XPATH_NODESET: |
| 1636 | xmlXPathDebugObjCounterNodeset--; |
| 1637 | break; |
| 1638 | case XPATH_BOOLEAN: |
| 1639 | xmlXPathDebugObjCounterBool--; |
| 1640 | break; |
| 1641 | case XPATH_NUMBER: |
| 1642 | xmlXPathDebugObjCounterNumber--; |
| 1643 | break; |
| 1644 | case XPATH_STRING: |
| 1645 | xmlXPathDebugObjCounterString--; |
| 1646 | break; |
| 1647 | case XPATH_POINT: |
| 1648 | xmlXPathDebugObjCounterPoint--; |
| 1649 | break; |
| 1650 | case XPATH_RANGE: |
| 1651 | xmlXPathDebugObjCounterRange--; |
| 1652 | break; |
| 1653 | case XPATH_LOCATIONSET: |
| 1654 | xmlXPathDebugObjCounterLocset--; |
| 1655 | break; |
| 1656 | case XPATH_USERS: |
| 1657 | xmlXPathDebugObjCounterUsers--; |
| 1658 | break; |
| 1659 | case XPATH_XSLT_TREE: |
| 1660 | xmlXPathDebugObjCounterXSLTTree--; |
| 1661 | break; |
| 1662 | default: |
| 1663 | break; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1664 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1665 | xmlXPathDebugObjCounterAll--; |
| 1666 | } |
| 1667 | |
| 1668 | /* REVISIT TODO: Make this static when committing */ |
| 1669 | static void |
| 1670 | xmlXPathDebugObjUsageDisplay(xmlXPathContextPtr ctxt) |
| 1671 | { |
| 1672 | int reqAll, reqNodeset, reqString, reqBool, reqNumber, |
| 1673 | reqXSLTTree, reqUndefined; |
| 1674 | int caAll = 0, caNodeset = 0, caString = 0, caBool = 0, |
| 1675 | caNumber = 0, caXSLTTree = 0, caUndefined = 0; |
| 1676 | int reAll = 0, reNodeset = 0, reString = 0, reBool = 0, |
| 1677 | reNumber = 0, reXSLTTree = 0, reUndefined = 0; |
| 1678 | int leftObjs = xmlXPathDebugObjCounterAll; |
| 1679 | |
| 1680 | reqAll = xmlXPathDebugObjTotalAll; |
| 1681 | reqNodeset = xmlXPathDebugObjTotalNodeset; |
| 1682 | reqString = xmlXPathDebugObjTotalString; |
| 1683 | reqBool = xmlXPathDebugObjTotalBool; |
| 1684 | reqNumber = xmlXPathDebugObjTotalNumber; |
| 1685 | reqXSLTTree = xmlXPathDebugObjTotalXSLTTree; |
| 1686 | reqUndefined = xmlXPathDebugObjTotalUndefined; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1687 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1688 | printf("# XPath object usage:\n"); |
| 1689 | |
| 1690 | if (ctxt != NULL) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1691 | if (ctxt->cache != NULL) { |
| 1692 | xmlXPathContextCachePtr cache = |
| 1693 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1694 | |
| 1695 | reAll = cache->dbgReusedAll; |
| 1696 | reqAll += reAll; |
| 1697 | reNodeset = cache->dbgReusedNodeset; |
| 1698 | reqNodeset += reNodeset; |
| 1699 | reString = cache->dbgReusedString; |
| 1700 | reqString += reString; |
| 1701 | reBool = cache->dbgReusedBool; |
| 1702 | reqBool += reBool; |
| 1703 | reNumber = cache->dbgReusedNumber; |
| 1704 | reqNumber += reNumber; |
| 1705 | reXSLTTree = cache->dbgReusedXSLTTree; |
| 1706 | reqXSLTTree += reXSLTTree; |
| 1707 | reUndefined = cache->dbgReusedUndefined; |
| 1708 | reqUndefined += reUndefined; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1709 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1710 | caAll = cache->dbgCachedAll; |
| 1711 | caBool = cache->dbgCachedBool; |
| 1712 | caNodeset = cache->dbgCachedNodeset; |
| 1713 | caString = cache->dbgCachedString; |
| 1714 | caNumber = cache->dbgCachedNumber; |
| 1715 | caXSLTTree = cache->dbgCachedXSLTTree; |
| 1716 | caUndefined = cache->dbgCachedUndefined; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1717 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1718 | if (cache->nodesetObjs) |
| 1719 | leftObjs -= cache->nodesetObjs->number; |
| 1720 | if (cache->stringObjs) |
| 1721 | leftObjs -= cache->stringObjs->number; |
| 1722 | if (cache->booleanObjs) |
| 1723 | leftObjs -= cache->booleanObjs->number; |
| 1724 | if (cache->numberObjs) |
| 1725 | leftObjs -= cache->numberObjs->number; |
| 1726 | if (cache->miscObjs) |
| 1727 | leftObjs -= cache->miscObjs->number; |
| 1728 | } |
| 1729 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1730 | |
| 1731 | printf("# all\n"); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1732 | printf("# total : %d\n", reqAll); |
| 1733 | printf("# left : %d\n", leftObjs); |
| 1734 | printf("# created: %d\n", xmlXPathDebugObjTotalAll); |
| 1735 | printf("# reused : %d\n", reAll); |
| 1736 | printf("# max : %d\n", xmlXPathDebugObjMaxAll); |
| 1737 | |
| 1738 | printf("# node-sets\n"); |
| 1739 | printf("# total : %d\n", reqNodeset); |
| 1740 | printf("# created: %d\n", xmlXPathDebugObjTotalNodeset); |
| 1741 | printf("# reused : %d\n", reNodeset); |
| 1742 | printf("# max : %d\n", xmlXPathDebugObjMaxNodeset); |
| 1743 | |
| 1744 | printf("# strings\n"); |
| 1745 | printf("# total : %d\n", reqString); |
| 1746 | printf("# created: %d\n", xmlXPathDebugObjTotalString); |
| 1747 | printf("# reused : %d\n", reString); |
| 1748 | printf("# max : %d\n", xmlXPathDebugObjMaxString); |
| 1749 | |
| 1750 | printf("# booleans\n"); |
| 1751 | printf("# total : %d\n", reqBool); |
| 1752 | printf("# created: %d\n", xmlXPathDebugObjTotalBool); |
| 1753 | printf("# reused : %d\n", reBool); |
| 1754 | printf("# max : %d\n", xmlXPathDebugObjMaxBool); |
| 1755 | |
| 1756 | printf("# numbers\n"); |
| 1757 | printf("# total : %d\n", reqNumber); |
| 1758 | printf("# created: %d\n", xmlXPathDebugObjTotalNumber); |
| 1759 | printf("# reused : %d\n", reNumber); |
| 1760 | printf("# max : %d\n", xmlXPathDebugObjMaxNumber); |
| 1761 | |
| 1762 | printf("# XSLT result tree fragments\n"); |
| 1763 | printf("# total : %d\n", reqXSLTTree); |
| 1764 | printf("# created: %d\n", xmlXPathDebugObjTotalXSLTTree); |
| 1765 | printf("# reused : %d\n", reXSLTTree); |
| 1766 | printf("# max : %d\n", xmlXPathDebugObjMaxXSLTTree); |
| 1767 | |
| 1768 | printf("# undefined\n"); |
| 1769 | printf("# total : %d\n", reqUndefined); |
| 1770 | printf("# created: %d\n", xmlXPathDebugObjTotalUndefined); |
| 1771 | printf("# reused : %d\n", reUndefined); |
| 1772 | printf("# max : %d\n", xmlXPathDebugObjMaxUndefined); |
| 1773 | |
| 1774 | } |
| 1775 | |
| 1776 | #endif /* XP_DEBUG_OBJ_USAGE */ |
| 1777 | |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 1778 | #endif /* LIBXML_DEBUG_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1779 | |
| 1780 | /************************************************************************ |
| 1781 | * * |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1782 | * XPath object caching * |
| 1783 | * * |
| 1784 | ************************************************************************/ |
| 1785 | |
| 1786 | /** |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1787 | * xmlXPathNewCache: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1788 | * |
| 1789 | * Create a new object cache |
| 1790 | * |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1791 | * Returns the xmlXPathCache just allocated. |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1792 | */ |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1793 | static xmlXPathContextCachePtr |
| 1794 | xmlXPathNewCache(void) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1795 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1796 | xmlXPathContextCachePtr ret; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1797 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1798 | ret = (xmlXPathContextCachePtr) xmlMalloc(sizeof(xmlXPathContextCache)); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1799 | if (ret == NULL) { |
| 1800 | xmlXPathErrMemory(NULL, "creating object cache\n"); |
| 1801 | return(NULL); |
| 1802 | } |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1803 | memset(ret, 0 , (size_t) sizeof(xmlXPathContextCache)); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1804 | ret->maxNodeset = 100; |
| 1805 | ret->maxString = 100; |
| 1806 | ret->maxBoolean = 100; |
| 1807 | ret->maxNumber = 100; |
| 1808 | ret->maxMisc = 100; |
| 1809 | return(ret); |
| 1810 | } |
| 1811 | |
| 1812 | static void |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1813 | xmlXPathCacheFreeObjectList(xmlPointerListPtr list) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1814 | { |
| 1815 | int i; |
| 1816 | xmlXPathObjectPtr obj; |
| 1817 | |
| 1818 | if (list == NULL) |
| 1819 | return; |
| 1820 | |
| 1821 | for (i = 0; i < list->number; i++) { |
| 1822 | obj = list->items[i]; |
| 1823 | /* |
| 1824 | * Note that it is already assured that we don't need to |
| 1825 | * look out for namespace nodes in the node-set. |
| 1826 | */ |
| 1827 | if (obj->nodesetval != NULL) { |
| 1828 | if (obj->nodesetval->nodeTab != NULL) |
| 1829 | xmlFree(obj->nodesetval->nodeTab); |
| 1830 | xmlFree(obj->nodesetval); |
| 1831 | } |
| 1832 | xmlFree(obj); |
| 1833 | #ifdef XP_DEBUG_OBJ_USAGE |
| 1834 | xmlXPathDebugObjCounterAll--; |
| 1835 | #endif |
| 1836 | } |
| 1837 | xmlPointerListFree(list); |
| 1838 | } |
| 1839 | |
| 1840 | static void |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1841 | xmlXPathFreeCache(xmlXPathContextCachePtr cache) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1842 | { |
| 1843 | if (cache == NULL) |
| 1844 | return; |
| 1845 | if (cache->nodesetObjs) |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1846 | xmlXPathCacheFreeObjectList(cache->nodesetObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1847 | if (cache->stringObjs) |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1848 | xmlXPathCacheFreeObjectList(cache->stringObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1849 | if (cache->booleanObjs) |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1850 | xmlXPathCacheFreeObjectList(cache->booleanObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1851 | if (cache->numberObjs) |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1852 | xmlXPathCacheFreeObjectList(cache->numberObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1853 | if (cache->miscObjs) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1854 | xmlXPathCacheFreeObjectList(cache->miscObjs); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1855 | xmlFree(cache); |
| 1856 | } |
| 1857 | |
| 1858 | /** |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1859 | * xmlXPathContextSetCache: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1860 | * |
| 1861 | * @ctxt: the XPath context |
| 1862 | * @active: enables/disables (creates/frees) the cache |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1863 | * @value: a value with semantics dependant on @options |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1864 | * @options: options (currently only the value 0 is used) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1865 | * |
| 1866 | * Creates/frees an object cache on the XPath context. |
| 1867 | * If activates XPath objects (xmlXPathObject) will be cached internally |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 1868 | * to be reused. |
| 1869 | * @options: |
| 1870 | * 0: This will set the XPath object caching: |
| 1871 | * @value: |
| 1872 | * This will set the maximum number of XPath objects |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1873 | * to be cached per slot |
| 1874 | * There are 5 slots for: node-set, string, number, boolean, and |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 1875 | * misc objects. Use <0 for the default number (100). |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1876 | * Other values for @options have currently no effect. |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1877 | * |
| 1878 | * Returns 0 if the setting succeeded, and -1 on API or internal errors. |
| 1879 | */ |
| 1880 | int |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1881 | xmlXPathContextSetCache(xmlXPathContextPtr ctxt, |
| 1882 | int active, |
| 1883 | int value, |
| 1884 | int options) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1885 | { |
| 1886 | if (ctxt == NULL) |
| 1887 | return(-1); |
| 1888 | if (active) { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1889 | xmlXPathContextCachePtr cache; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1890 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1891 | if (ctxt->cache == NULL) { |
| 1892 | ctxt->cache = xmlXPathNewCache(); |
| 1893 | if (ctxt->cache == NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1894 | return(-1); |
| 1895 | } |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 1896 | cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1897 | if (options == 0) { |
| 1898 | if (value < 0) |
| 1899 | value = 100; |
| 1900 | cache->maxNodeset = value; |
| 1901 | cache->maxString = value; |
| 1902 | cache->maxNumber = value; |
| 1903 | cache->maxBoolean = value; |
Aleksey Sanin | 1b2be10 | 2006-05-31 20:53:43 +0000 | [diff] [blame] | 1904 | cache->maxMisc = value; |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1905 | } |
| 1906 | } else if (ctxt->cache != NULL) { |
| 1907 | xmlXPathFreeCache((xmlXPathContextCachePtr) ctxt->cache); |
| 1908 | ctxt->cache = NULL; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1909 | } |
| 1910 | return(0); |
| 1911 | } |
| 1912 | |
| 1913 | /** |
| 1914 | * xmlXPathCacheWrapNodeSet: |
| 1915 | * @ctxt: the XPath context |
| 1916 | * @val: the NodePtr value |
| 1917 | * |
| 1918 | * This is the cached version of xmlXPathWrapNodeSet(). |
| 1919 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 1920 | * |
| 1921 | * Returns the created or reused object. |
| 1922 | */ |
| 1923 | static xmlXPathObjectPtr |
| 1924 | xmlXPathCacheWrapNodeSet(xmlXPathContextPtr ctxt, xmlNodeSetPtr val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1925 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1926 | if ((ctxt != NULL) && (ctxt->cache != NULL)) { |
| 1927 | xmlXPathContextCachePtr cache = |
| 1928 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1929 | |
| 1930 | if ((cache->miscObjs != NULL) && |
| 1931 | (cache->miscObjs->number != 0)) |
| 1932 | { |
| 1933 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1934 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1935 | ret = (xmlXPathObjectPtr) |
| 1936 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 1937 | ret->type = XPATH_NODESET; |
| 1938 | ret->nodesetval = val; |
| 1939 | #ifdef XP_DEBUG_OBJ_USAGE |
| 1940 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET); |
| 1941 | #endif |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1942 | return(ret); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1943 | } |
| 1944 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1945 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1946 | return(xmlXPathWrapNodeSet(val)); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1947 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * xmlXPathCacheWrapString: |
| 1952 | * @ctxt: the XPath context |
| 1953 | * @val: the xmlChar * value |
| 1954 | * |
| 1955 | * This is the cached version of xmlXPathWrapString(). |
| 1956 | * Wraps the @val string into an XPath object. |
| 1957 | * |
| 1958 | * Returns the created or reused object. |
| 1959 | */ |
| 1960 | static xmlXPathObjectPtr |
| 1961 | xmlXPathCacheWrapString(xmlXPathContextPtr ctxt, xmlChar *val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1962 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 1963 | if ((ctxt != NULL) && (ctxt->cache != NULL)) { |
| 1964 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1965 | |
| 1966 | if ((cache->stringObjs != NULL) && |
| 1967 | (cache->stringObjs->number != 0)) |
| 1968 | { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1969 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1970 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 1971 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 1972 | ret = (xmlXPathObjectPtr) |
| 1973 | cache->stringObjs->items[--cache->stringObjs->number]; |
| 1974 | ret->type = XPATH_STRING; |
| 1975 | ret->stringval = val; |
| 1976 | #ifdef XP_DEBUG_OBJ_USAGE |
| 1977 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 1978 | #endif |
| 1979 | return(ret); |
| 1980 | } else if ((cache->miscObjs != NULL) && |
| 1981 | (cache->miscObjs->number != 0)) |
| 1982 | { |
| 1983 | xmlXPathObjectPtr ret; |
| 1984 | /* |
| 1985 | * Fallback to misc-cache. |
| 1986 | */ |
| 1987 | ret = (xmlXPathObjectPtr) |
| 1988 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 1989 | |
| 1990 | ret->type = XPATH_STRING; |
| 1991 | ret->stringval = val; |
| 1992 | #ifdef XP_DEBUG_OBJ_USAGE |
| 1993 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 1994 | #endif |
| 1995 | return(ret); |
| 1996 | } |
| 1997 | } |
| 1998 | return(xmlXPathWrapString(val)); |
| 1999 | } |
| 2000 | |
| 2001 | /** |
| 2002 | * xmlXPathCacheNewNodeSet: |
| 2003 | * @ctxt: the XPath context |
| 2004 | * @val: the NodePtr value |
| 2005 | * |
| 2006 | * This is the cached version of xmlXPathNewNodeSet(). |
| 2007 | * Acquire an xmlXPathObjectPtr of type NodeSet and initialize |
| 2008 | * it with the single Node @val |
| 2009 | * |
| 2010 | * Returns the created or reused object. |
| 2011 | */ |
| 2012 | static xmlXPathObjectPtr |
| 2013 | xmlXPathCacheNewNodeSet(xmlXPathContextPtr ctxt, xmlNodePtr val) |
| 2014 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2015 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2016 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2017 | |
| 2018 | if ((cache->nodesetObjs != NULL) && |
| 2019 | (cache->nodesetObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2020 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2021 | xmlXPathObjectPtr ret; |
| 2022 | /* |
| 2023 | * Use the nodset-cache. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2024 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2025 | ret = (xmlXPathObjectPtr) |
| 2026 | cache->nodesetObjs->items[--cache->nodesetObjs->number]; |
| 2027 | ret->type = XPATH_NODESET; |
| 2028 | ret->boolval = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2029 | if (val) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2030 | if ((ret->nodesetval->nodeMax == 0) || |
| 2031 | (val->type == XML_NAMESPACE_DECL)) |
| 2032 | { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2033 | xmlXPathNodeSetAddUnique(ret->nodesetval, val); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2034 | } else { |
| 2035 | ret->nodesetval->nodeTab[0] = val; |
| 2036 | ret->nodesetval->nodeNr = 1; |
| 2037 | } |
| 2038 | } |
| 2039 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2040 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET); |
| 2041 | #endif |
| 2042 | return(ret); |
| 2043 | } else if ((cache->miscObjs != NULL) && |
| 2044 | (cache->miscObjs->number != 0)) |
| 2045 | { |
| 2046 | xmlXPathObjectPtr ret; |
| 2047 | /* |
| 2048 | * Fallback to misc-cache. |
| 2049 | */ |
| 2050 | |
| 2051 | ret = (xmlXPathObjectPtr) |
| 2052 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2053 | |
| 2054 | ret->type = XPATH_NODESET; |
| 2055 | ret->boolval = 0; |
| 2056 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 2057 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2058 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET); |
| 2059 | #endif |
| 2060 | return(ret); |
| 2061 | } |
| 2062 | } |
| 2063 | return(xmlXPathNewNodeSet(val)); |
| 2064 | } |
| 2065 | |
| 2066 | /** |
| 2067 | * xmlXPathCacheNewCString: |
| 2068 | * @ctxt: the XPath context |
| 2069 | * @val: the char * value |
| 2070 | * |
| 2071 | * This is the cached version of xmlXPathNewCString(). |
| 2072 | * Acquire an xmlXPathObjectPtr of type string and of value @val |
| 2073 | * |
| 2074 | * Returns the created or reused object. |
| 2075 | */ |
| 2076 | static xmlXPathObjectPtr |
| 2077 | xmlXPathCacheNewCString(xmlXPathContextPtr ctxt, const char *val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2078 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2079 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2080 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2081 | |
| 2082 | if ((cache->stringObjs != NULL) && |
| 2083 | (cache->stringObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2084 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2085 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2086 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2087 | ret = (xmlXPathObjectPtr) |
| 2088 | cache->stringObjs->items[--cache->stringObjs->number]; |
| 2089 | |
| 2090 | ret->type = XPATH_STRING; |
| 2091 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 2092 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2093 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2094 | #endif |
| 2095 | return(ret); |
| 2096 | } else if ((cache->miscObjs != NULL) && |
| 2097 | (cache->miscObjs->number != 0)) |
| 2098 | { |
| 2099 | xmlXPathObjectPtr ret; |
| 2100 | |
| 2101 | ret = (xmlXPathObjectPtr) |
| 2102 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2103 | |
| 2104 | ret->type = XPATH_STRING; |
| 2105 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 2106 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2107 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2108 | #endif |
| 2109 | return(ret); |
| 2110 | } |
| 2111 | } |
| 2112 | return(xmlXPathNewCString(val)); |
| 2113 | } |
| 2114 | |
| 2115 | /** |
| 2116 | * xmlXPathCacheNewString: |
| 2117 | * @ctxt: the XPath context |
| 2118 | * @val: the xmlChar * value |
| 2119 | * |
| 2120 | * This is the cached version of xmlXPathNewString(). |
| 2121 | * Acquire an xmlXPathObjectPtr of type string and of value @val |
| 2122 | * |
| 2123 | * Returns the created or reused object. |
| 2124 | */ |
| 2125 | static xmlXPathObjectPtr |
| 2126 | xmlXPathCacheNewString(xmlXPathContextPtr ctxt, const xmlChar *val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2127 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2128 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2129 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2130 | |
| 2131 | if ((cache->stringObjs != NULL) && |
| 2132 | (cache->stringObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2133 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2134 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2135 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2136 | ret = (xmlXPathObjectPtr) |
| 2137 | cache->stringObjs->items[--cache->stringObjs->number]; |
| 2138 | ret->type = XPATH_STRING; |
| 2139 | if (val != NULL) |
| 2140 | ret->stringval = xmlStrdup(val); |
| 2141 | else |
| 2142 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 2143 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2144 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2145 | #endif |
| 2146 | return(ret); |
| 2147 | } else if ((cache->miscObjs != NULL) && |
| 2148 | (cache->miscObjs->number != 0)) |
| 2149 | { |
| 2150 | xmlXPathObjectPtr ret; |
| 2151 | |
| 2152 | ret = (xmlXPathObjectPtr) |
| 2153 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2154 | |
| 2155 | ret->type = XPATH_STRING; |
| 2156 | if (val != NULL) |
| 2157 | ret->stringval = xmlStrdup(val); |
| 2158 | else |
| 2159 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 2160 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2161 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_STRING); |
| 2162 | #endif |
| 2163 | return(ret); |
| 2164 | } |
| 2165 | } |
| 2166 | return(xmlXPathNewString(val)); |
| 2167 | } |
| 2168 | |
| 2169 | /** |
| 2170 | * xmlXPathCacheNewBoolean: |
| 2171 | * @ctxt: the XPath context |
| 2172 | * @val: the boolean value |
| 2173 | * |
| 2174 | * This is the cached version of xmlXPathNewBoolean(). |
| 2175 | * Acquires an xmlXPathObjectPtr of type boolean and of value @val |
| 2176 | * |
| 2177 | * Returns the created or reused object. |
| 2178 | */ |
| 2179 | static xmlXPathObjectPtr |
| 2180 | xmlXPathCacheNewBoolean(xmlXPathContextPtr ctxt, int val) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2181 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2182 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2183 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2184 | |
| 2185 | if ((cache->booleanObjs != NULL) && |
| 2186 | (cache->booleanObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2187 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2188 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2189 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2190 | ret = (xmlXPathObjectPtr) |
| 2191 | cache->booleanObjs->items[--cache->booleanObjs->number]; |
| 2192 | ret->type = XPATH_BOOLEAN; |
| 2193 | ret->boolval = (val != 0); |
| 2194 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2195 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_BOOLEAN); |
| 2196 | #endif |
| 2197 | return(ret); |
| 2198 | } else if ((cache->miscObjs != NULL) && |
| 2199 | (cache->miscObjs->number != 0)) |
| 2200 | { |
| 2201 | xmlXPathObjectPtr ret; |
| 2202 | |
| 2203 | ret = (xmlXPathObjectPtr) |
| 2204 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2205 | |
| 2206 | ret->type = XPATH_BOOLEAN; |
| 2207 | ret->boolval = (val != 0); |
| 2208 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2209 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_BOOLEAN); |
| 2210 | #endif |
| 2211 | return(ret); |
| 2212 | } |
| 2213 | } |
| 2214 | return(xmlXPathNewBoolean(val)); |
| 2215 | } |
| 2216 | |
| 2217 | /** |
| 2218 | * xmlXPathCacheNewFloat: |
| 2219 | * @ctxt: the XPath context |
| 2220 | * @val: the double value |
| 2221 | * |
| 2222 | * This is the cached version of xmlXPathNewFloat(). |
| 2223 | * Acquires an xmlXPathObjectPtr of type double and of value @val |
| 2224 | * |
| 2225 | * Returns the created or reused object. |
| 2226 | */ |
| 2227 | static xmlXPathObjectPtr |
| 2228 | xmlXPathCacheNewFloat(xmlXPathContextPtr ctxt, double val) |
| 2229 | { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 2230 | if ((ctxt != NULL) && (ctxt->cache)) { |
| 2231 | xmlXPathContextCachePtr cache = (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2232 | |
| 2233 | if ((cache->numberObjs != NULL) && |
| 2234 | (cache->numberObjs->number != 0)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2235 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2236 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2237 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2238 | ret = (xmlXPathObjectPtr) |
| 2239 | cache->numberObjs->items[--cache->numberObjs->number]; |
| 2240 | ret->type = XPATH_NUMBER; |
| 2241 | ret->floatval = val; |
| 2242 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2243 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NUMBER); |
| 2244 | #endif |
| 2245 | return(ret); |
| 2246 | } else if ((cache->miscObjs != NULL) && |
| 2247 | (cache->miscObjs->number != 0)) |
| 2248 | { |
| 2249 | xmlXPathObjectPtr ret; |
| 2250 | |
| 2251 | ret = (xmlXPathObjectPtr) |
| 2252 | cache->miscObjs->items[--cache->miscObjs->number]; |
| 2253 | |
| 2254 | ret->type = XPATH_NUMBER; |
| 2255 | ret->floatval = val; |
| 2256 | #ifdef XP_DEBUG_OBJ_USAGE |
| 2257 | xmlXPathDebugObjUsageRequested(ctxt, XPATH_NUMBER); |
| 2258 | #endif |
| 2259 | return(ret); |
| 2260 | } |
| 2261 | } |
| 2262 | return(xmlXPathNewFloat(val)); |
| 2263 | } |
| 2264 | |
| 2265 | /** |
| 2266 | * xmlXPathCacheConvertString: |
| 2267 | * @ctxt: the XPath context |
| 2268 | * @val: an XPath object |
| 2269 | * |
| 2270 | * This is the cached version of xmlXPathConvertString(). |
| 2271 | * Converts an existing object to its string() equivalent |
| 2272 | * |
| 2273 | * Returns a created or reused object, the old one is freed (cached) |
| 2274 | * (or the operation is done directly on @val) |
| 2275 | */ |
| 2276 | |
| 2277 | static xmlXPathObjectPtr |
| 2278 | xmlXPathCacheConvertString(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2279 | xmlChar *res = NULL; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2280 | |
| 2281 | if (val == NULL) |
| 2282 | return(xmlXPathCacheNewCString(ctxt, "")); |
| 2283 | |
| 2284 | switch (val->type) { |
| 2285 | case XPATH_UNDEFINED: |
| 2286 | #ifdef DEBUG_EXPR |
| 2287 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 2288 | #endif |
| 2289 | break; |
| 2290 | case XPATH_NODESET: |
| 2291 | case XPATH_XSLT_TREE: |
| 2292 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 2293 | break; |
| 2294 | case XPATH_STRING: |
| 2295 | return(val); |
| 2296 | case XPATH_BOOLEAN: |
| 2297 | res = xmlXPathCastBooleanToString(val->boolval); |
| 2298 | break; |
| 2299 | case XPATH_NUMBER: |
| 2300 | res = xmlXPathCastNumberToString(val->floatval); |
| 2301 | break; |
| 2302 | case XPATH_USERS: |
| 2303 | case XPATH_POINT: |
| 2304 | case XPATH_RANGE: |
| 2305 | case XPATH_LOCATIONSET: |
| 2306 | TODO; |
| 2307 | break; |
| 2308 | } |
| 2309 | xmlXPathReleaseObject(ctxt, val); |
| 2310 | if (res == NULL) |
| 2311 | return(xmlXPathCacheNewCString(ctxt, "")); |
| 2312 | return(xmlXPathCacheWrapString(ctxt, res)); |
| 2313 | } |
| 2314 | |
| 2315 | /** |
| 2316 | * xmlXPathCacheObjectCopy: |
| 2317 | * @ctxt: the XPath context |
| 2318 | * @val: the original object |
| 2319 | * |
| 2320 | * This is the cached version of xmlXPathObjectCopy(). |
| 2321 | * Acquire a copy of a given object |
| 2322 | * |
| 2323 | * Returns a created or reused created object. |
| 2324 | */ |
| 2325 | static xmlXPathObjectPtr |
| 2326 | xmlXPathCacheObjectCopy(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) |
| 2327 | { |
| 2328 | if (val == NULL) |
| 2329 | return(NULL); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2330 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 2331 | if (XP_HAS_CACHE(ctxt)) { |
| 2332 | switch (val->type) { |
| 2333 | case XPATH_NODESET: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2334 | return(xmlXPathCacheWrapNodeSet(ctxt, |
| 2335 | xmlXPathNodeSetMerge(NULL, val->nodesetval))); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 2336 | case XPATH_STRING: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2337 | return(xmlXPathCacheNewString(ctxt, val->stringval)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 2338 | case XPATH_BOOLEAN: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2339 | return(xmlXPathCacheNewBoolean(ctxt, val->boolval)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 2340 | case XPATH_NUMBER: |
| 2341 | return(xmlXPathCacheNewFloat(ctxt, val->floatval)); |
| 2342 | default: |
| 2343 | break; |
| 2344 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2345 | } |
| 2346 | return(xmlXPathObjectCopy(val)); |
| 2347 | } |
| 2348 | |
| 2349 | /** |
| 2350 | * xmlXPathCacheConvertBoolean: |
| 2351 | * @ctxt: the XPath context |
| 2352 | * @val: an XPath object |
| 2353 | * |
| 2354 | * This is the cached version of xmlXPathConvertBoolean(). |
| 2355 | * Converts an existing object to its boolean() equivalent |
| 2356 | * |
| 2357 | * Returns a created or reused object, the old one is freed (or the operation |
| 2358 | * is done directly on @val) |
| 2359 | */ |
| 2360 | static xmlXPathObjectPtr |
| 2361 | xmlXPathCacheConvertBoolean(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) { |
| 2362 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2363 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2364 | if (val == NULL) |
| 2365 | return(xmlXPathCacheNewBoolean(ctxt, 0)); |
| 2366 | if (val->type == XPATH_BOOLEAN) |
| 2367 | return(val); |
| 2368 | ret = xmlXPathCacheNewBoolean(ctxt, xmlXPathCastToBoolean(val)); |
| 2369 | xmlXPathReleaseObject(ctxt, val); |
| 2370 | return(ret); |
| 2371 | } |
| 2372 | |
| 2373 | /** |
| 2374 | * xmlXPathCacheConvertNumber: |
| 2375 | * @ctxt: the XPath context |
| 2376 | * @val: an XPath object |
| 2377 | * |
| 2378 | * This is the cached version of xmlXPathConvertNumber(). |
| 2379 | * Converts an existing object to its number() equivalent |
| 2380 | * |
| 2381 | * Returns a created or reused object, the old one is freed (or the operation |
| 2382 | * is done directly on @val) |
| 2383 | */ |
| 2384 | static xmlXPathObjectPtr |
| 2385 | xmlXPathCacheConvertNumber(xmlXPathContextPtr ctxt, xmlXPathObjectPtr val) { |
| 2386 | xmlXPathObjectPtr ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2387 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2388 | if (val == NULL) |
| 2389 | return(xmlXPathCacheNewFloat(ctxt, 0.0)); |
| 2390 | if (val->type == XPATH_NUMBER) |
| 2391 | return(val); |
| 2392 | ret = xmlXPathCacheNewFloat(ctxt, xmlXPathCastToNumber(val)); |
| 2393 | xmlXPathReleaseObject(ctxt, val); |
| 2394 | return(ret); |
| 2395 | } |
| 2396 | |
| 2397 | /************************************************************************ |
| 2398 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2399 | * Parser stacks related functions and macros * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2400 | * * |
| 2401 | ************************************************************************/ |
| 2402 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2403 | /** |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 2404 | * xmlXPathSetFrame: |
| 2405 | * @ctxt: an XPath parser context |
| 2406 | * |
| 2407 | * Set the callee evaluation frame |
| 2408 | * |
| 2409 | * Returns the previous frame value to be restored once done |
| 2410 | */ |
| 2411 | static int |
| 2412 | xmlXPathSetFrame(xmlXPathParserContextPtr ctxt) { |
| 2413 | int ret; |
| 2414 | |
| 2415 | if (ctxt == NULL) |
| 2416 | return(0); |
| 2417 | ret = ctxt->valueFrame; |
| 2418 | ctxt->valueFrame = ctxt->valueNr; |
| 2419 | return(ret); |
| 2420 | } |
| 2421 | |
| 2422 | /** |
| 2423 | * xmlXPathPopFrame: |
| 2424 | * @ctxt: an XPath parser context |
| 2425 | * @frame: the previous frame value |
| 2426 | * |
| 2427 | * Remove the callee evaluation frame |
| 2428 | */ |
| 2429 | static void |
| 2430 | xmlXPathPopFrame(xmlXPathParserContextPtr ctxt, int frame) { |
| 2431 | if (ctxt == NULL) |
| 2432 | return; |
| 2433 | if (ctxt->valueNr < ctxt->valueFrame) { |
| 2434 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_STACK_ERROR); |
| 2435 | } |
| 2436 | ctxt->valueFrame = frame; |
| 2437 | } |
| 2438 | |
| 2439 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2440 | * valuePop: |
| 2441 | * @ctxt: an XPath evaluation context |
| 2442 | * |
| 2443 | * Pops the top XPath object from the value stack |
| 2444 | * |
| 2445 | * Returns the XPath object just removed |
| 2446 | */ |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 2447 | xmlXPathObjectPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2448 | valuePop(xmlXPathParserContextPtr ctxt) |
| 2449 | { |
| 2450 | xmlXPathObjectPtr ret; |
| 2451 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2452 | if ((ctxt == NULL) || (ctxt->valueNr <= 0)) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 2453 | return (NULL); |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 2454 | |
| 2455 | if (ctxt->valueNr <= ctxt->valueFrame) { |
| 2456 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_STACK_ERROR); |
| 2457 | return (NULL); |
| 2458 | } |
| 2459 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2460 | ctxt->valueNr--; |
| 2461 | if (ctxt->valueNr > 0) |
| 2462 | ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; |
| 2463 | else |
| 2464 | ctxt->value = NULL; |
| 2465 | ret = ctxt->valueTab[ctxt->valueNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 2466 | ctxt->valueTab[ctxt->valueNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2467 | return (ret); |
| 2468 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2469 | /** |
| 2470 | * valuePush: |
| 2471 | * @ctxt: an XPath evaluation context |
| 2472 | * @value: the XPath object |
| 2473 | * |
| 2474 | * Pushes a new XPath object on top of the value stack |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2475 | * |
| 2476 | * returns the number of items on the value stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2477 | */ |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 2478 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2479 | valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value) |
| 2480 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2481 | if ((ctxt == NULL) || (value == NULL)) return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2482 | if (ctxt->valueNr >= ctxt->valueMax) { |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 2483 | xmlXPathObjectPtr *tmp; |
| 2484 | |
| 2485 | tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, |
| 2486 | 2 * ctxt->valueMax * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2487 | sizeof(ctxt->valueTab[0])); |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 2488 | if (tmp == NULL) { |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2489 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 2490 | ctxt->error = XPATH_MEMORY_ERROR; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2491 | return (0); |
| 2492 | } |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 2493 | ctxt->valueMax *= 2; |
| 2494 | ctxt->valueTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 2495 | } |
| 2496 | ctxt->valueTab[ctxt->valueNr] = value; |
| 2497 | ctxt->value = value; |
| 2498 | return (ctxt->valueNr++); |
| 2499 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2500 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2501 | /** |
| 2502 | * xmlXPathPopBoolean: |
| 2503 | * @ctxt: an XPath parser context |
| 2504 | * |
| 2505 | * Pops a boolean from the stack, handling conversion if needed. |
| 2506 | * Check error with #xmlXPathCheckError. |
| 2507 | * |
| 2508 | * Returns the boolean |
| 2509 | */ |
| 2510 | int |
| 2511 | xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) { |
| 2512 | xmlXPathObjectPtr obj; |
| 2513 | int ret; |
| 2514 | |
| 2515 | obj = valuePop(ctxt); |
| 2516 | if (obj == NULL) { |
| 2517 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 2518 | return(0); |
| 2519 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2520 | if (obj->type != XPATH_BOOLEAN) |
| 2521 | ret = xmlXPathCastToBoolean(obj); |
| 2522 | else |
| 2523 | ret = obj->boolval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2524 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2525 | return(ret); |
| 2526 | } |
| 2527 | |
| 2528 | /** |
| 2529 | * xmlXPathPopNumber: |
| 2530 | * @ctxt: an XPath parser context |
| 2531 | * |
| 2532 | * Pops a number from the stack, handling conversion if needed. |
| 2533 | * Check error with #xmlXPathCheckError. |
| 2534 | * |
| 2535 | * Returns the number |
| 2536 | */ |
| 2537 | double |
| 2538 | xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) { |
| 2539 | xmlXPathObjectPtr obj; |
| 2540 | double ret; |
| 2541 | |
| 2542 | obj = valuePop(ctxt); |
| 2543 | if (obj == NULL) { |
| 2544 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 2545 | return(0); |
| 2546 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2547 | if (obj->type != XPATH_NUMBER) |
| 2548 | ret = xmlXPathCastToNumber(obj); |
| 2549 | else |
| 2550 | ret = obj->floatval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2551 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2552 | return(ret); |
| 2553 | } |
| 2554 | |
| 2555 | /** |
| 2556 | * xmlXPathPopString: |
| 2557 | * @ctxt: an XPath parser context |
| 2558 | * |
| 2559 | * Pops a string from the stack, handling conversion if needed. |
| 2560 | * Check error with #xmlXPathCheckError. |
| 2561 | * |
| 2562 | * Returns the string |
| 2563 | */ |
| 2564 | xmlChar * |
| 2565 | xmlXPathPopString (xmlXPathParserContextPtr ctxt) { |
| 2566 | xmlXPathObjectPtr obj; |
| 2567 | xmlChar * ret; |
| 2568 | |
| 2569 | obj = valuePop(ctxt); |
| 2570 | if (obj == NULL) { |
| 2571 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 2572 | return(NULL); |
| 2573 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2574 | ret = xmlXPathCastToString(obj); /* this does required strdup */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2575 | /* TODO: needs refactoring somewhere else */ |
| 2576 | if (obj->stringval == ret) |
| 2577 | obj->stringval = NULL; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2578 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2579 | return(ret); |
| 2580 | } |
| 2581 | |
| 2582 | /** |
| 2583 | * xmlXPathPopNodeSet: |
| 2584 | * @ctxt: an XPath parser context |
| 2585 | * |
| 2586 | * Pops a node-set from the stack, handling conversion if needed. |
| 2587 | * Check error with #xmlXPathCheckError. |
| 2588 | * |
| 2589 | * Returns the node-set |
| 2590 | */ |
| 2591 | xmlNodeSetPtr |
| 2592 | xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) { |
| 2593 | xmlXPathObjectPtr obj; |
| 2594 | xmlNodeSetPtr ret; |
| 2595 | |
Daniel Veillard | f2a36f9 | 2004-11-08 17:55:01 +0000 | [diff] [blame] | 2596 | if (ctxt == NULL) return(NULL); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2597 | if (ctxt->value == NULL) { |
| 2598 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 2599 | return(NULL); |
| 2600 | } |
| 2601 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
| 2602 | xmlXPathSetTypeError(ctxt); |
| 2603 | return(NULL); |
| 2604 | } |
| 2605 | obj = valuePop(ctxt); |
| 2606 | ret = obj->nodesetval; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 2607 | #if 0 |
Daniel Veillard | 9deb242 | 2003-07-28 20:40:59 +0000 | [diff] [blame] | 2608 | /* to fix memory leak of not clearing obj->user */ |
| 2609 | if (obj->boolval && obj->user != NULL) |
| 2610 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 2611 | #endif |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2612 | obj->nodesetval = NULL; |
| 2613 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2614 | return(ret); |
| 2615 | } |
| 2616 | |
| 2617 | /** |
| 2618 | * xmlXPathPopExternal: |
| 2619 | * @ctxt: an XPath parser context |
| 2620 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2621 | * Pops an external object from the stack, handling conversion if needed. |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2622 | * Check error with #xmlXPathCheckError. |
| 2623 | * |
| 2624 | * Returns the object |
| 2625 | */ |
| 2626 | void * |
| 2627 | xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) { |
| 2628 | xmlXPathObjectPtr obj; |
| 2629 | void * ret; |
| 2630 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2631 | if ((ctxt == NULL) || (ctxt->value == NULL)) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2632 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 2633 | return(NULL); |
| 2634 | } |
| 2635 | if (ctxt->value->type != XPATH_USERS) { |
| 2636 | xmlXPathSetTypeError(ctxt); |
| 2637 | return(NULL); |
| 2638 | } |
| 2639 | obj = valuePop(ctxt); |
| 2640 | ret = obj->user; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 2641 | obj->user = NULL; |
| 2642 | xmlXPathReleaseObject(ctxt->context, obj); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2643 | return(ret); |
| 2644 | } |
| 2645 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2646 | /* |
| 2647 | * Macros for accessing the content. Those should be used only by the parser, |
| 2648 | * and not exported. |
| 2649 | * |
| 2650 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 2651 | * |
| 2652 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 2653 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 2654 | * in ISO-Latin or UTF-8. |
| 2655 | * This should be used internally by the parser |
| 2656 | * only to compare to ASCII values otherwise it would break when |
| 2657 | * running with UTF-8 encoding. |
| 2658 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 2659 | * to compare on ASCII based substring. |
| 2660 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
| 2661 | * strings within the parser. |
| 2662 | * CURRENT Returns the current char value, with the full decoding of |
| 2663 | * UTF-8 if we are using this mode. It returns an int. |
| 2664 | * NEXT Skip to the next character, this does the proper decoding |
| 2665 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
| 2666 | * It returns the pointer to the current xmlChar. |
| 2667 | */ |
| 2668 | |
| 2669 | #define CUR (*ctxt->cur) |
| 2670 | #define SKIP(val) ctxt->cur += (val) |
| 2671 | #define NXT(val) ctxt->cur[(val)] |
| 2672 | #define CUR_PTR ctxt->cur |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 2673 | #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l) |
| 2674 | |
| 2675 | #define COPY_BUF(l,b,i,v) \ |
| 2676 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 2677 | else i += xmlCopyChar(l,&b[i],v) |
| 2678 | |
| 2679 | #define NEXTL(l) ctxt->cur += l |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2680 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 2681 | #define SKIP_BLANKS \ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2682 | while (IS_BLANK_CH(*(ctxt->cur))) NEXT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2683 | |
| 2684 | #define CURRENT (*ctxt->cur) |
| 2685 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 2686 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2687 | |
| 2688 | #ifndef DBL_DIG |
| 2689 | #define DBL_DIG 16 |
| 2690 | #endif |
| 2691 | #ifndef DBL_EPSILON |
| 2692 | #define DBL_EPSILON 1E-9 |
| 2693 | #endif |
| 2694 | |
| 2695 | #define UPPER_DOUBLE 1E9 |
| 2696 | #define LOWER_DOUBLE 1E-5 |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 2697 | #define LOWER_DOUBLE_EXP 5 |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2698 | |
| 2699 | #define INTEGER_DIGITS DBL_DIG |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 2700 | #define FRACTION_DIGITS (DBL_DIG + 1 + (LOWER_DOUBLE_EXP)) |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2701 | #define EXPONENT_DIGITS (3 + 2) |
| 2702 | |
| 2703 | /** |
| 2704 | * xmlXPathFormatNumber: |
| 2705 | * @number: number to format |
| 2706 | * @buffer: output buffer |
| 2707 | * @buffersize: size of output buffer |
| 2708 | * |
| 2709 | * Convert the number into a string representation. |
| 2710 | */ |
| 2711 | static void |
| 2712 | xmlXPathFormatNumber(double number, char buffer[], int buffersize) |
| 2713 | { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2714 | switch (xmlXPathIsInf(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2715 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 2716 | if (buffersize > (int)sizeof("Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 2717 | snprintf(buffer, buffersize, "Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2718 | break; |
| 2719 | case -1: |
| 2720 | if (buffersize > (int)sizeof("-Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 2721 | snprintf(buffer, buffersize, "-Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2722 | break; |
| 2723 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2724 | if (xmlXPathIsNaN(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2725 | if (buffersize > (int)sizeof("NaN")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 2726 | snprintf(buffer, buffersize, "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 2727 | } else if (number == 0 && xmlXPathGetSign(number) != 0) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 2728 | snprintf(buffer, buffersize, "0"); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 2729 | } else if (number == ((int) number)) { |
| 2730 | char work[30]; |
| 2731 | char *ptr, *cur; |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 2732 | int value = (int) number; |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 2733 | |
| 2734 | ptr = &buffer[0]; |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 2735 | if (value == 0) { |
| 2736 | *ptr++ = '0'; |
| 2737 | } else { |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 2738 | snprintf(work, 29, "%d", value); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 2739 | cur = &work[0]; |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 2740 | while ((*cur) && (ptr - buffer < buffersize)) { |
| 2741 | *ptr++ = *cur++; |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 2742 | } |
| 2743 | } |
| 2744 | if (ptr - buffer < buffersize) { |
| 2745 | *ptr = 0; |
| 2746 | } else if (buffersize > 0) { |
| 2747 | ptr--; |
| 2748 | *ptr = 0; |
| 2749 | } |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2750 | } else { |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 2751 | /* |
| 2752 | For the dimension of work, |
| 2753 | DBL_DIG is number of significant digits |
| 2754 | EXPONENT is only needed for "scientific notation" |
| 2755 | 3 is sign, decimal point, and terminating zero |
| 2756 | LOWER_DOUBLE_EXP is max number of leading zeroes in fraction |
| 2757 | Note that this dimension is slightly (a few characters) |
| 2758 | larger than actually necessary. |
| 2759 | */ |
| 2760 | char work[DBL_DIG + EXPONENT_DIGITS + 3 + LOWER_DOUBLE_EXP]; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 2761 | int integer_place, fraction_place; |
| 2762 | char *ptr; |
| 2763 | char *after_fraction; |
| 2764 | double absolute_value; |
| 2765 | int size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2766 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 2767 | absolute_value = fabs(number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2768 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 2769 | /* |
| 2770 | * First choose format - scientific or regular floating point. |
| 2771 | * In either case, result is in work, and after_fraction points |
| 2772 | * just past the fractional part. |
| 2773 | */ |
| 2774 | if ( ((absolute_value > UPPER_DOUBLE) || |
| 2775 | (absolute_value < LOWER_DOUBLE)) && |
| 2776 | (absolute_value != 0.0) ) { |
| 2777 | /* Use scientific notation */ |
| 2778 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 2779 | fraction_place = DBL_DIG - 1; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 2780 | size = snprintf(work, sizeof(work),"%*.*e", |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 2781 | integer_place, fraction_place, number); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 2782 | while ((size > 0) && (work[size] != 'e')) size--; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 2783 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2784 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 2785 | else { |
| 2786 | /* Use regular notation */ |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 2787 | if (absolute_value > 0.0) { |
| 2788 | integer_place = (int)log10(absolute_value); |
| 2789 | if (integer_place > 0) |
| 2790 | fraction_place = DBL_DIG - integer_place - 1; |
| 2791 | else |
| 2792 | fraction_place = DBL_DIG - integer_place; |
| 2793 | } else { |
| 2794 | fraction_place = 1; |
| 2795 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 2796 | size = snprintf(work, sizeof(work), "%0.*f", |
| 2797 | fraction_place, number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 2800 | /* Remove fractional trailing zeroes */ |
William M. Brack | ca79788 | 2007-05-11 14:45:53 +0000 | [diff] [blame] | 2801 | after_fraction = work + size; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 2802 | ptr = after_fraction; |
| 2803 | while (*(--ptr) == '0') |
| 2804 | ; |
| 2805 | if (*ptr != '.') |
| 2806 | ptr++; |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 2807 | while ((*ptr++ = *after_fraction++) != 0); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 2808 | |
| 2809 | /* Finally copy result back to caller */ |
| 2810 | size = strlen(work) + 1; |
| 2811 | if (size > buffersize) { |
| 2812 | work[buffersize - 1] = 0; |
| 2813 | size = buffersize; |
| 2814 | } |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 2815 | memmove(buffer, work, size); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 2816 | } |
| 2817 | break; |
| 2818 | } |
| 2819 | } |
| 2820 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2821 | |
| 2822 | /************************************************************************ |
| 2823 | * * |
| 2824 | * Routines to handle NodeSets * |
| 2825 | * * |
| 2826 | ************************************************************************/ |
| 2827 | |
| 2828 | /** |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 2829 | * xmlXPathOrderDocElems: |
| 2830 | * @doc: an input document |
| 2831 | * |
| 2832 | * Call this routine to speed up XPath computation on static documents. |
| 2833 | * This stamps all the element nodes with the document order |
| 2834 | * 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] | 2835 | * field, the value stored is actually - the node number (starting at -1) |
| 2836 | * to be able to differentiate from line numbers. |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 2837 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2838 | * 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] | 2839 | * of error. |
| 2840 | */ |
| 2841 | long |
| 2842 | xmlXPathOrderDocElems(xmlDocPtr doc) { |
| 2843 | long count = 0; |
| 2844 | xmlNodePtr cur; |
| 2845 | |
| 2846 | if (doc == NULL) |
| 2847 | return(-1); |
| 2848 | cur = doc->children; |
| 2849 | while (cur != NULL) { |
| 2850 | if (cur->type == XML_ELEMENT_NODE) { |
| 2851 | cur->content = (void *) (-(++count)); |
| 2852 | if (cur->children != NULL) { |
| 2853 | cur = cur->children; |
| 2854 | continue; |
| 2855 | } |
| 2856 | } |
| 2857 | if (cur->next != NULL) { |
| 2858 | cur = cur->next; |
| 2859 | continue; |
| 2860 | } |
| 2861 | do { |
| 2862 | cur = cur->parent; |
| 2863 | if (cur == NULL) |
| 2864 | break; |
| 2865 | if (cur == (xmlNodePtr) doc) { |
| 2866 | cur = NULL; |
| 2867 | break; |
| 2868 | } |
| 2869 | if (cur->next != NULL) { |
| 2870 | cur = cur->next; |
| 2871 | break; |
| 2872 | } |
| 2873 | } while (cur != NULL); |
| 2874 | } |
| 2875 | return(count); |
| 2876 | } |
| 2877 | |
| 2878 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2879 | * xmlXPathCmpNodes: |
| 2880 | * @node1: the first node |
| 2881 | * @node2: the second node |
| 2882 | * |
| 2883 | * Compare two nodes w.r.t document order |
| 2884 | * |
| 2885 | * 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] | 2886 | * it's the same node, -1 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2887 | */ |
| 2888 | int |
| 2889 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 2890 | int depth1, depth2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 2891 | int attr1 = 0, attr2 = 0; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 2892 | xmlNodePtr attrNode1 = NULL, attrNode2 = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2893 | xmlNodePtr cur, root; |
| 2894 | |
| 2895 | if ((node1 == NULL) || (node2 == NULL)) |
| 2896 | return(-2); |
| 2897 | /* |
| 2898 | * a couple of optimizations which will avoid computations in most cases |
| 2899 | */ |
William M. Brack | ee0b982 | 2007-03-07 08:15:01 +0000 | [diff] [blame] | 2900 | if (node1 == node2) /* trivial case */ |
| 2901 | return(0); |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 2902 | if (node1->type == XML_ATTRIBUTE_NODE) { |
| 2903 | attr1 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 2904 | attrNode1 = node1; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 2905 | node1 = node1->parent; |
| 2906 | } |
| 2907 | if (node2->type == XML_ATTRIBUTE_NODE) { |
| 2908 | attr2 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 2909 | attrNode2 = node2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 2910 | node2 = node2->parent; |
| 2911 | } |
| 2912 | if (node1 == node2) { |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 2913 | if (attr1 == attr2) { |
| 2914 | /* not required, but we keep attributes in order */ |
| 2915 | if (attr1 != 0) { |
| 2916 | cur = attrNode2->prev; |
| 2917 | while (cur != NULL) { |
| 2918 | if (cur == attrNode1) |
| 2919 | return (1); |
| 2920 | cur = cur->prev; |
| 2921 | } |
| 2922 | return (-1); |
| 2923 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 2924 | return(0); |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 2925 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 2926 | if (attr2 == 1) |
| 2927 | return(1); |
| 2928 | return(-1); |
| 2929 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 2930 | if ((node1->type == XML_NAMESPACE_DECL) || |
| 2931 | (node2->type == XML_NAMESPACE_DECL)) |
| 2932 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2933 | if (node1 == node2->prev) |
| 2934 | return(1); |
| 2935 | if (node1 == node2->next) |
| 2936 | return(-1); |
| 2937 | |
| 2938 | /* |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 2939 | * Speedup using document order if availble. |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 2940 | */ |
| 2941 | if ((node1->type == XML_ELEMENT_NODE) && |
| 2942 | (node2->type == XML_ELEMENT_NODE) && |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 2943 | (0 > (long) node1->content) && |
| 2944 | (0 > (long) node2->content) && |
| 2945 | (node1->doc == node2->doc)) { |
| 2946 | long l1, l2; |
| 2947 | |
| 2948 | l1 = -((long) node1->content); |
| 2949 | l2 = -((long) node2->content); |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 2950 | if (l1 < l2) |
| 2951 | return(1); |
| 2952 | if (l1 > l2) |
| 2953 | return(-1); |
| 2954 | } |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 2955 | |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 2956 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2957 | * compute depth to root |
| 2958 | */ |
| 2959 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 2960 | if (cur == node1) |
| 2961 | return(1); |
| 2962 | depth2++; |
| 2963 | } |
| 2964 | root = cur; |
| 2965 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 2966 | if (cur == node2) |
| 2967 | return(-1); |
| 2968 | depth1++; |
| 2969 | } |
| 2970 | /* |
| 2971 | * Distinct document (or distinct entities :-( ) case. |
| 2972 | */ |
| 2973 | if (root != cur) { |
| 2974 | return(-2); |
| 2975 | } |
| 2976 | /* |
| 2977 | * get the nearest common ancestor. |
| 2978 | */ |
| 2979 | while (depth1 > depth2) { |
| 2980 | depth1--; |
| 2981 | node1 = node1->parent; |
| 2982 | } |
| 2983 | while (depth2 > depth1) { |
| 2984 | depth2--; |
| 2985 | node2 = node2->parent; |
| 2986 | } |
| 2987 | while (node1->parent != node2->parent) { |
| 2988 | node1 = node1->parent; |
| 2989 | node2 = node2->parent; |
| 2990 | /* should not happen but just in case ... */ |
| 2991 | if ((node1 == NULL) || (node2 == NULL)) |
| 2992 | return(-2); |
| 2993 | } |
| 2994 | /* |
| 2995 | * Find who's first. |
| 2996 | */ |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 2997 | if (node1 == node2->prev) |
| 2998 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2999 | if (node1 == node2->next) |
| 3000 | return(-1); |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 3001 | /* |
| 3002 | * Speedup using document order if availble. |
| 3003 | */ |
| 3004 | if ((node1->type == XML_ELEMENT_NODE) && |
| 3005 | (node2->type == XML_ELEMENT_NODE) && |
| 3006 | (0 > (long) node1->content) && |
| 3007 | (0 > (long) node2->content) && |
| 3008 | (node1->doc == node2->doc)) { |
| 3009 | long l1, l2; |
| 3010 | |
| 3011 | l1 = -((long) node1->content); |
| 3012 | l2 = -((long) node2->content); |
| 3013 | if (l1 < l2) |
| 3014 | return(1); |
| 3015 | if (l1 > l2) |
| 3016 | return(-1); |
| 3017 | } |
| 3018 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3019 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 3020 | if (cur == node2) |
| 3021 | return(1); |
| 3022 | return(-1); /* assume there is no sibling list corruption */ |
| 3023 | } |
| 3024 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3025 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3026 | /** |
| 3027 | * xmlXPathCmpNodesExt: |
| 3028 | * @node1: the first node |
| 3029 | * @node2: the second node |
| 3030 | * |
| 3031 | * Compare two nodes w.r.t document order. |
| 3032 | * This one is optimized for handling of non-element nodes. |
| 3033 | * |
| 3034 | * Returns -2 in case of error 1 if first point < second point, 0 if |
| 3035 | * it's the same node, -1 otherwise |
| 3036 | */ |
| 3037 | static int |
| 3038 | xmlXPathCmpNodesExt(xmlNodePtr node1, xmlNodePtr node2) { |
| 3039 | int depth1, depth2; |
| 3040 | int misc = 0, precedence1 = 0, precedence2 = 0; |
| 3041 | xmlNodePtr miscNode1 = NULL, miscNode2 = NULL; |
| 3042 | xmlNodePtr cur, root; |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 3043 | long l1, l2; |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3044 | |
| 3045 | if ((node1 == NULL) || (node2 == NULL)) |
| 3046 | return(-2); |
| 3047 | |
| 3048 | if (node1 == node2) |
| 3049 | return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3050 | |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3051 | /* |
| 3052 | * a couple of optimizations which will avoid computations in most cases |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3053 | */ |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3054 | switch (node1->type) { |
| 3055 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 3056 | if (node2->type == XML_ELEMENT_NODE) { |
| 3057 | if ((0 > (long) node1->content) && /* TODO: Would a != 0 suffice here? */ |
| 3058 | (0 > (long) node2->content) && |
| 3059 | (node1->doc == node2->doc)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3060 | { |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 3061 | l1 = -((long) node1->content); |
| 3062 | l2 = -((long) node2->content); |
| 3063 | if (l1 < l2) |
| 3064 | return(1); |
| 3065 | if (l1 > l2) |
| 3066 | return(-1); |
| 3067 | } else |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3068 | goto turtle_comparison; |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 3069 | } |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3070 | break; |
| 3071 | case XML_ATTRIBUTE_NODE: |
| 3072 | precedence1 = 1; /* element is owner */ |
| 3073 | miscNode1 = node1; |
| 3074 | node1 = node1->parent; |
| 3075 | misc = 1; |
| 3076 | break; |
| 3077 | case XML_TEXT_NODE: |
| 3078 | case XML_CDATA_SECTION_NODE: |
| 3079 | case XML_COMMENT_NODE: |
| 3080 | case XML_PI_NODE: { |
| 3081 | miscNode1 = node1; |
| 3082 | /* |
| 3083 | * Find nearest element node. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3084 | */ |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3085 | if (node1->prev != NULL) { |
| 3086 | do { |
| 3087 | node1 = node1->prev; |
| 3088 | if (node1->type == XML_ELEMENT_NODE) { |
| 3089 | precedence1 = 3; /* element in prev-sibl axis */ |
| 3090 | break; |
| 3091 | } |
| 3092 | if (node1->prev == NULL) { |
| 3093 | precedence1 = 2; /* element is parent */ |
| 3094 | /* |
| 3095 | * URGENT TODO: Are there any cases, where the |
| 3096 | * parent of such a node is not an element node? |
| 3097 | */ |
| 3098 | node1 = node1->parent; |
| 3099 | break; |
| 3100 | } |
| 3101 | } while (1); |
| 3102 | } else { |
| 3103 | precedence1 = 2; /* element is parent */ |
| 3104 | node1 = node1->parent; |
| 3105 | } |
William M. Brack | 31700e6 | 2007-06-13 20:33:02 +0000 | [diff] [blame] | 3106 | if ((node1 == NULL) || (node1->type != XML_ELEMENT_NODE) || |
| 3107 | (0 <= (long) node1->content)) { |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3108 | /* |
| 3109 | * Fallback for whatever case. |
| 3110 | */ |
| 3111 | node1 = miscNode1; |
| 3112 | precedence1 = 0; |
| 3113 | } else |
| 3114 | misc = 1; |
| 3115 | } |
| 3116 | break; |
| 3117 | case XML_NAMESPACE_DECL: |
| 3118 | /* |
| 3119 | * TODO: why do we return 1 for namespace nodes? |
| 3120 | */ |
| 3121 | return(1); |
| 3122 | default: |
| 3123 | break; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3124 | } |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3125 | switch (node2->type) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3126 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3127 | break; |
| 3128 | case XML_ATTRIBUTE_NODE: |
| 3129 | precedence2 = 1; /* element is owner */ |
| 3130 | miscNode2 = node2; |
| 3131 | node2 = node2->parent; |
| 3132 | misc = 1; |
| 3133 | break; |
| 3134 | case XML_TEXT_NODE: |
| 3135 | case XML_CDATA_SECTION_NODE: |
| 3136 | case XML_COMMENT_NODE: |
| 3137 | case XML_PI_NODE: { |
| 3138 | miscNode2 = node2; |
| 3139 | if (node2->prev != NULL) { |
| 3140 | do { |
| 3141 | node2 = node2->prev; |
| 3142 | if (node2->type == XML_ELEMENT_NODE) { |
| 3143 | precedence2 = 3; /* element in prev-sibl axis */ |
| 3144 | break; |
| 3145 | } |
| 3146 | if (node2->prev == NULL) { |
| 3147 | precedence2 = 2; /* element is parent */ |
| 3148 | node2 = node2->parent; |
| 3149 | break; |
| 3150 | } |
| 3151 | } while (1); |
| 3152 | } else { |
| 3153 | precedence2 = 2; /* element is parent */ |
| 3154 | node2 = node2->parent; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3155 | } |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3156 | if ((node2 == NULL) || (node2->type != XML_ELEMENT_NODE) || |
| 3157 | (0 <= (long) node1->content)) |
| 3158 | { |
| 3159 | node2 = miscNode2; |
| 3160 | precedence2 = 0; |
| 3161 | } else |
| 3162 | misc = 1; |
| 3163 | } |
| 3164 | break; |
| 3165 | case XML_NAMESPACE_DECL: |
| 3166 | return(1); |
| 3167 | default: |
| 3168 | break; |
| 3169 | } |
| 3170 | if (misc) { |
| 3171 | if (node1 == node2) { |
| 3172 | if (precedence1 == precedence2) { |
| 3173 | /* |
| 3174 | * The ugly case; but normally there aren't many |
| 3175 | * adjacent non-element nodes around. |
| 3176 | */ |
| 3177 | cur = miscNode2->prev; |
| 3178 | while (cur != NULL) { |
| 3179 | if (cur == miscNode1) |
| 3180 | return(1); |
| 3181 | if (cur->type == XML_ELEMENT_NODE) |
| 3182 | return(-1); |
| 3183 | cur = cur->prev; |
| 3184 | } |
| 3185 | return (-1); |
| 3186 | } else { |
| 3187 | /* |
| 3188 | * Evaluate based on higher precedence wrt to the element. |
| 3189 | * TODO: This assumes attributes are sorted before content. |
| 3190 | * Is this 100% correct? |
| 3191 | */ |
| 3192 | if (precedence1 < precedence2) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3193 | return(1); |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3194 | else |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3195 | return(-1); |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3196 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3197 | } |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3198 | /* |
| 3199 | * Special case: One of the helper-elements is contained by the other. |
| 3200 | * <foo> |
| 3201 | * <node2> |
| 3202 | * <node1>Text-1(precedence1 == 2)</node1> |
| 3203 | * </node2> |
| 3204 | * Text-6(precedence2 == 3) |
| 3205 | * </foo> |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3206 | */ |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3207 | if ((precedence2 == 3) && (precedence1 > 1)) { |
| 3208 | cur = node1->parent; |
| 3209 | while (cur) { |
| 3210 | if (cur == node2) |
| 3211 | return(1); |
| 3212 | cur = cur->parent; |
| 3213 | } |
| 3214 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3215 | if ((precedence1 == 3) && (precedence2 > 1)) { |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3216 | cur = node2->parent; |
| 3217 | while (cur) { |
| 3218 | if (cur == node1) |
| 3219 | return(-1); |
| 3220 | cur = cur->parent; |
| 3221 | } |
| 3222 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3223 | } |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3224 | |
| 3225 | /* |
| 3226 | * Speedup using document order if availble. |
| 3227 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3228 | if ((node1->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3229 | (node2->type == XML_ELEMENT_NODE) && |
| 3230 | (0 > (long) node1->content) && |
| 3231 | (0 > (long) node2->content) && |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3232 | (node1->doc == node2->doc)) { |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3233 | |
| 3234 | l1 = -((long) node1->content); |
| 3235 | l2 = -((long) node2->content); |
| 3236 | if (l1 < l2) |
| 3237 | return(1); |
| 3238 | if (l1 > l2) |
| 3239 | return(-1); |
| 3240 | } |
| 3241 | |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 3242 | turtle_comparison: |
| 3243 | |
| 3244 | if (node1 == node2->prev) |
| 3245 | return(1); |
| 3246 | if (node1 == node2->next) |
| 3247 | return(-1); |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3248 | /* |
| 3249 | * compute depth to root |
| 3250 | */ |
| 3251 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 3252 | if (cur == node1) |
| 3253 | return(1); |
| 3254 | depth2++; |
| 3255 | } |
| 3256 | root = cur; |
| 3257 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 3258 | if (cur == node2) |
| 3259 | return(-1); |
| 3260 | depth1++; |
| 3261 | } |
| 3262 | /* |
| 3263 | * Distinct document (or distinct entities :-( ) case. |
| 3264 | */ |
| 3265 | if (root != cur) { |
| 3266 | return(-2); |
| 3267 | } |
| 3268 | /* |
| 3269 | * get the nearest common ancestor. |
| 3270 | */ |
| 3271 | while (depth1 > depth2) { |
| 3272 | depth1--; |
| 3273 | node1 = node1->parent; |
| 3274 | } |
| 3275 | while (depth2 > depth1) { |
| 3276 | depth2--; |
| 3277 | node2 = node2->parent; |
| 3278 | } |
| 3279 | while (node1->parent != node2->parent) { |
| 3280 | node1 = node1->parent; |
| 3281 | node2 = node2->parent; |
| 3282 | /* should not happen but just in case ... */ |
| 3283 | if ((node1 == NULL) || (node2 == NULL)) |
| 3284 | return(-2); |
| 3285 | } |
| 3286 | /* |
| 3287 | * Find who's first. |
| 3288 | */ |
| 3289 | if (node1 == node2->prev) |
| 3290 | return(1); |
| 3291 | if (node1 == node2->next) |
| 3292 | return(-1); |
| 3293 | /* |
| 3294 | * Speedup using document order if availble. |
| 3295 | */ |
| 3296 | if ((node1->type == XML_ELEMENT_NODE) && |
| 3297 | (node2->type == XML_ELEMENT_NODE) && |
| 3298 | (0 > (long) node1->content) && |
| 3299 | (0 > (long) node2->content) && |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3300 | (node1->doc == node2->doc)) { |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3301 | |
| 3302 | l1 = -((long) node1->content); |
| 3303 | l2 = -((long) node2->content); |
| 3304 | if (l1 < l2) |
| 3305 | return(1); |
| 3306 | if (l1 > l2) |
| 3307 | return(-1); |
| 3308 | } |
| 3309 | |
| 3310 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 3311 | if (cur == node2) |
| 3312 | return(1); |
| 3313 | return(-1); /* assume there is no sibling list corruption */ |
| 3314 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3315 | #endif /* XP_OPTIMIZED_NON_ELEM_COMPARISON */ |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3316 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3317 | /** |
| 3318 | * xmlXPathNodeSetSort: |
| 3319 | * @set: the node set |
| 3320 | * |
| 3321 | * Sort the node set in document order |
| 3322 | */ |
| 3323 | void |
| 3324 | xmlXPathNodeSetSort(xmlNodeSetPtr set) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3325 | int i, j, incr, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3326 | xmlNodePtr tmp; |
| 3327 | |
| 3328 | if (set == NULL) |
| 3329 | return; |
| 3330 | |
| 3331 | /* Use Shell's sort to sort the node-set */ |
| 3332 | len = set->nodeNr; |
| 3333 | for (incr = len / 2; incr > 0; incr /= 2) { |
| 3334 | for (i = incr; i < len; i++) { |
| 3335 | j = i - incr; |
| 3336 | while (j >= 0) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3337 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3338 | if (xmlXPathCmpNodesExt(set->nodeTab[j], |
| 3339 | set->nodeTab[j + incr]) == -1) |
| 3340 | #else |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 3341 | if (xmlXPathCmpNodes(set->nodeTab[j], |
Kasimier T. Buchcik | 2bdabbd | 2006-05-19 11:26:15 +0000 | [diff] [blame] | 3342 | set->nodeTab[j + incr]) == -1) |
| 3343 | #endif |
| 3344 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3345 | tmp = set->nodeTab[j]; |
| 3346 | set->nodeTab[j] = set->nodeTab[j + incr]; |
| 3347 | set->nodeTab[j + incr] = tmp; |
| 3348 | j -= incr; |
| 3349 | } else |
| 3350 | break; |
| 3351 | } |
| 3352 | } |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | #define XML_NODESET_DEFAULT 10 |
| 3357 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3358 | * xmlXPathNodeSetDupNs: |
| 3359 | * @node: the parent node of the namespace XPath node |
| 3360 | * @ns: the libxml namespace declaration node. |
| 3361 | * |
| 3362 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 3363 | * the namespace nodes are duplicated and the next pointer is set to the |
| 3364 | * parent node in the XPath semantic. |
| 3365 | * |
| 3366 | * Returns the newly created object. |
| 3367 | */ |
| 3368 | static xmlNodePtr |
| 3369 | xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) { |
| 3370 | xmlNsPtr cur; |
| 3371 | |
| 3372 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 3373 | return(NULL); |
| 3374 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 3375 | return((xmlNodePtr) ns); |
| 3376 | |
| 3377 | /* |
| 3378 | * Allocate a new Namespace and fill the fields. |
| 3379 | */ |
| 3380 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 3381 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3382 | xmlXPathErrMemory(NULL, "duplicating namespace\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3383 | return(NULL); |
| 3384 | } |
| 3385 | memset(cur, 0, sizeof(xmlNs)); |
| 3386 | cur->type = XML_NAMESPACE_DECL; |
| 3387 | if (ns->href != NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3388 | cur->href = xmlStrdup(ns->href); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3389 | if (ns->prefix != NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3390 | cur->prefix = xmlStrdup(ns->prefix); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3391 | cur->next = (xmlNsPtr) node; |
| 3392 | return((xmlNodePtr) cur); |
| 3393 | } |
| 3394 | |
| 3395 | /** |
| 3396 | * xmlXPathNodeSetFreeNs: |
| 3397 | * @ns: the XPath namespace node found in a nodeset. |
| 3398 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3399 | * 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] | 3400 | * 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] | 3401 | * 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] | 3402 | */ |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 3403 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3404 | xmlXPathNodeSetFreeNs(xmlNsPtr ns) { |
| 3405 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 3406 | return; |
| 3407 | |
| 3408 | if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) { |
| 3409 | if (ns->href != NULL) |
| 3410 | xmlFree((xmlChar *)ns->href); |
| 3411 | if (ns->prefix != NULL) |
| 3412 | xmlFree((xmlChar *)ns->prefix); |
| 3413 | xmlFree(ns); |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3418 | * xmlXPathNodeSetCreate: |
| 3419 | * @val: an initial xmlNodePtr, or NULL |
| 3420 | * |
| 3421 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 3422 | * |
| 3423 | * Returns the newly created object. |
| 3424 | */ |
| 3425 | xmlNodeSetPtr |
| 3426 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 3427 | xmlNodeSetPtr ret; |
| 3428 | |
| 3429 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 3430 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3431 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3432 | return(NULL); |
| 3433 | } |
| 3434 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 3435 | if (val != NULL) { |
| 3436 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3437 | sizeof(xmlNodePtr)); |
| 3438 | if (ret->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3439 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 3440 | xmlFree(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3441 | return(NULL); |
| 3442 | } |
| 3443 | memset(ret->nodeTab, 0 , |
| 3444 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3445 | ret->nodeMax = XML_NODESET_DEFAULT; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3446 | if (val->type == XML_NAMESPACE_DECL) { |
| 3447 | xmlNsPtr ns = (xmlNsPtr) val; |
| 3448 | |
| 3449 | ret->nodeTab[ret->nodeNr++] = |
| 3450 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3451 | } else |
| 3452 | ret->nodeTab[ret->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3453 | } |
| 3454 | return(ret); |
| 3455 | } |
| 3456 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3457 | /** |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3458 | * xmlXPathNodeSetCreateSize: |
| 3459 | * @size: the initial size of the set |
| 3460 | * |
| 3461 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 3462 | * |
| 3463 | * Returns the newly created object. |
| 3464 | */ |
| 3465 | static xmlNodeSetPtr |
| 3466 | xmlXPathNodeSetCreateSize(int size) { |
| 3467 | xmlNodeSetPtr ret; |
| 3468 | |
| 3469 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 3470 | if (ret == NULL) { |
| 3471 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 3472 | return(NULL); |
| 3473 | } |
| 3474 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 3475 | if (size < XML_NODESET_DEFAULT) |
| 3476 | size = XML_NODESET_DEFAULT; |
| 3477 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(size * sizeof(xmlNodePtr)); |
| 3478 | if (ret->nodeTab == NULL) { |
| 3479 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 3480 | xmlFree(ret); |
| 3481 | return(NULL); |
| 3482 | } |
| 3483 | memset(ret->nodeTab, 0 , size * (size_t) sizeof(xmlNodePtr)); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3484 | ret->nodeMax = size; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3485 | return(ret); |
| 3486 | } |
| 3487 | |
| 3488 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3489 | * xmlXPathNodeSetContains: |
| 3490 | * @cur: the node-set |
| 3491 | * @val: the node |
| 3492 | * |
| 3493 | * checks whether @cur contains @val |
| 3494 | * |
| 3495 | * Returns true (1) if @cur contains @val, false (0) otherwise |
| 3496 | */ |
| 3497 | int |
| 3498 | xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) { |
| 3499 | int i; |
| 3500 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 3501 | if ((cur == NULL) || (val == NULL)) return(0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3502 | if (val->type == XML_NAMESPACE_DECL) { |
| 3503 | for (i = 0; i < cur->nodeNr; i++) { |
| 3504 | if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 3505 | xmlNsPtr ns1, ns2; |
| 3506 | |
| 3507 | ns1 = (xmlNsPtr) val; |
| 3508 | ns2 = (xmlNsPtr) cur->nodeTab[i]; |
| 3509 | if (ns1 == ns2) |
| 3510 | return(1); |
| 3511 | if ((ns1->next != NULL) && (ns2->next == ns1->next) && |
| 3512 | (xmlStrEqual(ns1->prefix, ns2->prefix))) |
| 3513 | return(1); |
| 3514 | } |
| 3515 | } |
| 3516 | } else { |
| 3517 | for (i = 0; i < cur->nodeNr; i++) { |
| 3518 | if (cur->nodeTab[i] == val) |
| 3519 | return(1); |
| 3520 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3521 | } |
| 3522 | return(0); |
| 3523 | } |
| 3524 | |
| 3525 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3526 | * xmlXPathNodeSetAddNs: |
| 3527 | * @cur: the initial node set |
| 3528 | * @node: the hosting node |
| 3529 | * @ns: a the namespace node |
| 3530 | * |
| 3531 | * add a new namespace node to an existing NodeSet |
| 3532 | */ |
Aleksey Sanin | 79376ba | 2002-05-14 06:41:32 +0000 | [diff] [blame] | 3533 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3534 | xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { |
| 3535 | int i; |
| 3536 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3537 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 3538 | if ((cur == NULL) || (ns == NULL) || (node == NULL) || |
| 3539 | (ns->type != XML_NAMESPACE_DECL) || |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3540 | (node->type != XML_ELEMENT_NODE)) |
| 3541 | return; |
| 3542 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3543 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3544 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3545 | * prevent duplicates |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3546 | */ |
| 3547 | for (i = 0;i < cur->nodeNr;i++) { |
| 3548 | if ((cur->nodeTab[i] != NULL) && |
| 3549 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | c62a147 | 2002-03-19 18:35:12 +0000 | [diff] [blame] | 3550 | (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3551 | (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) |
| 3552 | return; |
| 3553 | } |
| 3554 | |
| 3555 | /* |
| 3556 | * grow the nodeTab if needed |
| 3557 | */ |
| 3558 | if (cur->nodeMax == 0) { |
| 3559 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3560 | sizeof(xmlNodePtr)); |
| 3561 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3562 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3563 | return; |
| 3564 | } |
| 3565 | memset(cur->nodeTab, 0 , |
| 3566 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3567 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 3568 | } else if (cur->nodeNr == cur->nodeMax) { |
| 3569 | xmlNodePtr *temp; |
| 3570 | |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3571 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * 2 * |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3572 | sizeof(xmlNodePtr)); |
| 3573 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3574 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3575 | return; |
| 3576 | } |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3577 | cur->nodeMax *= 2; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3578 | cur->nodeTab = temp; |
| 3579 | } |
| 3580 | cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); |
| 3581 | } |
| 3582 | |
| 3583 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3584 | * xmlXPathNodeSetAdd: |
| 3585 | * @cur: the initial node set |
| 3586 | * @val: a new xmlNodePtr |
| 3587 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3588 | * add a new xmlNodePtr to an existing NodeSet |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3589 | */ |
| 3590 | void |
| 3591 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 3592 | int i; |
| 3593 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 3594 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3595 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3596 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 3597 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 3598 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3599 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 3600 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3601 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3602 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3603 | * prevent duplcates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3604 | */ |
| 3605 | for (i = 0;i < cur->nodeNr;i++) |
| 3606 | if (cur->nodeTab[i] == val) return; |
| 3607 | |
| 3608 | /* |
| 3609 | * grow the nodeTab if needed |
| 3610 | */ |
| 3611 | if (cur->nodeMax == 0) { |
| 3612 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3613 | sizeof(xmlNodePtr)); |
| 3614 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3615 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3616 | return; |
| 3617 | } |
| 3618 | memset(cur->nodeTab, 0 , |
| 3619 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3620 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 3621 | } else if (cur->nodeNr == cur->nodeMax) { |
| 3622 | xmlNodePtr *temp; |
| 3623 | |
Daniel Veillard | 0cbeb50 | 2010-11-15 12:06:29 +0100 | [diff] [blame] | 3624 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * 2 * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3625 | sizeof(xmlNodePtr)); |
| 3626 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3627 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3628 | return; |
| 3629 | } |
Daniel Veillard | 0cbeb50 | 2010-11-15 12:06:29 +0100 | [diff] [blame] | 3630 | cur->nodeMax *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3631 | cur->nodeTab = temp; |
| 3632 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3633 | if (val->type == XML_NAMESPACE_DECL) { |
| 3634 | xmlNsPtr ns = (xmlNsPtr) val; |
| 3635 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3636 | cur->nodeTab[cur->nodeNr++] = |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3637 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3638 | } else |
| 3639 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | /** |
| 3643 | * xmlXPathNodeSetAddUnique: |
| 3644 | * @cur: the initial node set |
| 3645 | * @val: a new xmlNodePtr |
| 3646 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3647 | * add a new xmlNodePtr to an existing NodeSet, optimized version |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3648 | * when we are sure the node is not already in the set. |
| 3649 | */ |
| 3650 | void |
| 3651 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 3652 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3653 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3654 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 3655 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 3656 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3657 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 3658 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3659 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3660 | /* |
| 3661 | * grow the nodeTab if needed |
| 3662 | */ |
| 3663 | if (cur->nodeMax == 0) { |
| 3664 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3665 | sizeof(xmlNodePtr)); |
| 3666 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3667 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3668 | return; |
| 3669 | } |
| 3670 | memset(cur->nodeTab, 0 , |
| 3671 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3672 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 3673 | } else if (cur->nodeNr == cur->nodeMax) { |
| 3674 | xmlNodePtr *temp; |
| 3675 | |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3676 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * 2 * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3677 | sizeof(xmlNodePtr)); |
| 3678 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3679 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3680 | return; |
| 3681 | } |
| 3682 | cur->nodeTab = temp; |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3683 | cur->nodeMax *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3684 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3685 | if (val->type == XML_NAMESPACE_DECL) { |
| 3686 | xmlNsPtr ns = (xmlNsPtr) val; |
| 3687 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3688 | cur->nodeTab[cur->nodeNr++] = |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3689 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3690 | } else |
| 3691 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | /** |
| 3695 | * xmlXPathNodeSetMerge: |
| 3696 | * @val1: the first NodeSet or NULL |
| 3697 | * @val2: the second NodeSet |
| 3698 | * |
| 3699 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 3700 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 3701 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3702 | * Returns @val1 once extended or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3703 | */ |
| 3704 | xmlNodeSetPtr |
| 3705 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 3706 | int i, j, initNr, skip; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3707 | xmlNodePtr n1, n2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3708 | |
| 3709 | if (val2 == NULL) return(val1); |
| 3710 | if (val1 == NULL) { |
Kasimier T. Buchcik | 984a9ae | 2006-05-24 09:02:35 +0000 | [diff] [blame] | 3711 | val1 = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 3712 | if (val1 == NULL) |
| 3713 | return (NULL); |
Kasimier T. Buchcik | 984a9ae | 2006-05-24 09:02:35 +0000 | [diff] [blame] | 3714 | #if 0 |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3715 | /* |
| 3716 | * TODO: The optimization won't work in every case, since |
| 3717 | * those nasty namespace nodes need to be added with |
| 3718 | * xmlXPathNodeSetDupNs() to the set; thus a pure |
| 3719 | * memcpy is not possible. |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 3720 | * If there was a flag on the nodesetval, indicating that |
| 3721 | * some temporary nodes are in, that would be helpfull. |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3722 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3723 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3724 | * Optimization: Create an equally sized node-set |
| 3725 | * and memcpy the content. |
| 3726 | */ |
| 3727 | val1 = xmlXPathNodeSetCreateSize(val2->nodeNr); |
| 3728 | if (val1 == NULL) |
| 3729 | return(NULL); |
| 3730 | if (val2->nodeNr != 0) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3731 | if (val2->nodeNr == 1) |
| 3732 | *(val1->nodeTab) = *(val2->nodeTab); |
| 3733 | else { |
| 3734 | memcpy(val1->nodeTab, val2->nodeTab, |
| 3735 | val2->nodeNr * sizeof(xmlNodePtr)); |
| 3736 | } |
| 3737 | val1->nodeNr = val2->nodeNr; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3738 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3739 | return(val1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 3740 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3743 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3744 | initNr = val1->nodeNr; |
| 3745 | |
| 3746 | for (i = 0;i < val2->nodeNr;i++) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3747 | n2 = val2->nodeTab[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3748 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3749 | * check against duplicates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3750 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 3751 | skip = 0; |
| 3752 | for (j = 0; j < initNr; j++) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3753 | n1 = val1->nodeTab[j]; |
| 3754 | if (n1 == n2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 3755 | skip = 1; |
| 3756 | break; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3757 | } else if ((n1->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3758 | (n2->type == XML_NAMESPACE_DECL)) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3759 | if ((((xmlNsPtr) n1)->next == ((xmlNsPtr) n2)->next) && |
| 3760 | (xmlStrEqual(((xmlNsPtr) n1)->prefix, |
| 3761 | ((xmlNsPtr) n2)->prefix))) |
| 3762 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3763 | skip = 1; |
| 3764 | break; |
| 3765 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 3766 | } |
| 3767 | } |
| 3768 | if (skip) |
| 3769 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3770 | |
| 3771 | /* |
| 3772 | * grow the nodeTab if needed |
| 3773 | */ |
| 3774 | if (val1->nodeMax == 0) { |
| 3775 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3776 | sizeof(xmlNodePtr)); |
| 3777 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3778 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3779 | return(NULL); |
| 3780 | } |
| 3781 | memset(val1->nodeTab, 0 , |
| 3782 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3783 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 3784 | } else if (val1->nodeNr == val1->nodeMax) { |
| 3785 | xmlNodePtr *temp; |
| 3786 | |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3787 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * 2 * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3788 | sizeof(xmlNodePtr)); |
| 3789 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3790 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3791 | return(NULL); |
| 3792 | } |
| 3793 | val1->nodeTab = temp; |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3794 | val1->nodeMax *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3795 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3796 | if (n2->type == XML_NAMESPACE_DECL) { |
| 3797 | xmlNsPtr ns = (xmlNsPtr) n2; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3798 | |
| 3799 | val1->nodeTab[val1->nodeNr++] = |
| 3800 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3801 | } else |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 3802 | val1->nodeTab[val1->nodeNr++] = n2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | return(val1); |
| 3806 | } |
| 3807 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3808 | #if 0 /* xmlXPathNodeSetMergeUnique() is currently not used anymore */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3809 | /** |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 3810 | * xmlXPathNodeSetMergeUnique: |
| 3811 | * @val1: the first NodeSet or NULL |
| 3812 | * @val2: the second NodeSet |
| 3813 | * |
| 3814 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 3815 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 3816 | * |
| 3817 | * Returns @val1 once extended or NULL in case of error. |
| 3818 | */ |
| 3819 | static xmlNodeSetPtr |
| 3820 | xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 3821 | int i; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 3822 | |
| 3823 | if (val2 == NULL) return(val1); |
| 3824 | if (val1 == NULL) { |
| 3825 | val1 = xmlXPathNodeSetCreate(NULL); |
| 3826 | } |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 3827 | if (val1 == NULL) |
| 3828 | return (NULL); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 3829 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3830 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 3831 | |
| 3832 | for (i = 0;i < val2->nodeNr;i++) { |
| 3833 | /* |
| 3834 | * grow the nodeTab if needed |
| 3835 | */ |
| 3836 | if (val1->nodeMax == 0) { |
| 3837 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 3838 | sizeof(xmlNodePtr)); |
| 3839 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3840 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 3841 | return(NULL); |
| 3842 | } |
| 3843 | memset(val1->nodeTab, 0 , |
| 3844 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3845 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 3846 | } else if (val1->nodeNr == val1->nodeMax) { |
| 3847 | xmlNodePtr *temp; |
| 3848 | |
| 3849 | val1->nodeMax *= 2; |
| 3850 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 3851 | sizeof(xmlNodePtr)); |
| 3852 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3853 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 3854 | return(NULL); |
| 3855 | } |
| 3856 | val1->nodeTab = temp; |
| 3857 | } |
| 3858 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 3859 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 3860 | |
| 3861 | val1->nodeTab[val1->nodeNr++] = |
| 3862 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3863 | } else |
| 3864 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 3865 | } |
| 3866 | |
| 3867 | return(val1); |
| 3868 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3869 | #endif /* xmlXPathNodeSetMergeUnique() is currently not used anymore */ |
| 3870 | |
| 3871 | /** |
| 3872 | * xmlXPathNodeSetMergeAndClear: |
| 3873 | * @set1: the first NodeSet or NULL |
| 3874 | * @set2: the second NodeSet |
| 3875 | * @hasSet2NsNodes: 1 if set2 contains namespaces nodes |
| 3876 | * |
| 3877 | * Merges two nodesets, all nodes from @set2 are added to @set1 |
| 3878 | * if @set1 is NULL, a new set is created and copied from @set2. |
| 3879 | * Checks for duplicate nodes. Clears set2. |
| 3880 | * |
| 3881 | * Returns @set1 once extended or NULL in case of error. |
| 3882 | */ |
| 3883 | static xmlNodeSetPtr |
| 3884 | xmlXPathNodeSetMergeAndClear(xmlNodeSetPtr set1, xmlNodeSetPtr set2, |
| 3885 | int hasNullEntries) |
| 3886 | { |
| 3887 | if ((set1 == NULL) && (hasNullEntries == 0)) { |
| 3888 | /* |
| 3889 | * Note that doing a memcpy of the list, namespace nodes are |
| 3890 | * just assigned to set1, since set2 is cleared anyway. |
| 3891 | */ |
| 3892 | set1 = xmlXPathNodeSetCreateSize(set2->nodeNr); |
| 3893 | if (set1 == NULL) |
| 3894 | return(NULL); |
| 3895 | if (set2->nodeNr != 0) { |
| 3896 | memcpy(set1->nodeTab, set2->nodeTab, |
| 3897 | set2->nodeNr * sizeof(xmlNodePtr)); |
| 3898 | set1->nodeNr = set2->nodeNr; |
| 3899 | } |
| 3900 | } else { |
| 3901 | int i, j, initNbSet1; |
| 3902 | xmlNodePtr n1, n2; |
| 3903 | |
| 3904 | if (set1 == NULL) |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 3905 | set1 = xmlXPathNodeSetCreate(NULL); |
| 3906 | if (set1 == NULL) |
| 3907 | return (NULL); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3908 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3909 | initNbSet1 = set1->nodeNr; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3910 | for (i = 0;i < set2->nodeNr;i++) { |
| 3911 | n2 = set2->nodeTab[i]; |
| 3912 | /* |
| 3913 | * Skip NULLed entries. |
| 3914 | */ |
| 3915 | if (n2 == NULL) |
| 3916 | continue; |
| 3917 | /* |
| 3918 | * Skip duplicates. |
| 3919 | */ |
| 3920 | for (j = 0; j < initNbSet1; j++) { |
| 3921 | n1 = set1->nodeTab[j]; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3922 | if (n1 == n2) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3923 | goto skip_node; |
| 3924 | } else if ((n1->type == XML_NAMESPACE_DECL) && |
| 3925 | (n2->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3926 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3927 | if ((((xmlNsPtr) n1)->next == ((xmlNsPtr) n2)->next) && |
| 3928 | (xmlStrEqual(((xmlNsPtr) n1)->prefix, |
| 3929 | ((xmlNsPtr) n2)->prefix))) |
| 3930 | { |
| 3931 | /* |
| 3932 | * Free the namespace node. |
| 3933 | */ |
| 3934 | set2->nodeTab[i] = NULL; |
| 3935 | xmlXPathNodeSetFreeNs((xmlNsPtr) n2); |
| 3936 | goto skip_node; |
| 3937 | } |
| 3938 | } |
| 3939 | } |
| 3940 | /* |
| 3941 | * grow the nodeTab if needed |
| 3942 | */ |
| 3943 | if (set1->nodeMax == 0) { |
| 3944 | set1->nodeTab = (xmlNodePtr *) xmlMalloc( |
| 3945 | XML_NODESET_DEFAULT * sizeof(xmlNodePtr)); |
| 3946 | if (set1->nodeTab == NULL) { |
| 3947 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
| 3948 | return(NULL); |
| 3949 | } |
| 3950 | memset(set1->nodeTab, 0, |
| 3951 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 3952 | set1->nodeMax = XML_NODESET_DEFAULT; |
| 3953 | } else if (set1->nodeNr >= set1->nodeMax) { |
| 3954 | xmlNodePtr *temp; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3955 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3956 | temp = (xmlNodePtr *) xmlRealloc( |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3957 | set1->nodeTab, set1->nodeMax * 2 * sizeof(xmlNodePtr)); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3958 | if (temp == NULL) { |
| 3959 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
| 3960 | return(NULL); |
| 3961 | } |
| 3962 | set1->nodeTab = temp; |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 3963 | set1->nodeMax *= 2; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3964 | } |
| 3965 | if (n2->type == XML_NAMESPACE_DECL) { |
| 3966 | xmlNsPtr ns = (xmlNsPtr) n2; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3967 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3968 | set1->nodeTab[set1->nodeNr++] = |
| 3969 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 3970 | } else |
| 3971 | set1->nodeTab[set1->nodeNr++] = n2; |
| 3972 | skip_node: |
| 3973 | {} |
| 3974 | } |
| 3975 | } |
| 3976 | set2->nodeNr = 0; |
| 3977 | return(set1); |
| 3978 | } |
| 3979 | |
| 3980 | /** |
| 3981 | * xmlXPathNodeSetMergeAndClearNoDupls: |
| 3982 | * @set1: the first NodeSet or NULL |
| 3983 | * @set2: the second NodeSet |
| 3984 | * @hasSet2NsNodes: 1 if set2 contains namespaces nodes |
| 3985 | * |
| 3986 | * Merges two nodesets, all nodes from @set2 are added to @set1 |
| 3987 | * if @set1 is NULL, a new set is created and copied from @set2. |
| 3988 | * Doesn't chack for duplicate nodes. Clears set2. |
| 3989 | * |
| 3990 | * Returns @set1 once extended or NULL in case of error. |
| 3991 | */ |
| 3992 | static xmlNodeSetPtr |
| 3993 | xmlXPathNodeSetMergeAndClearNoDupls(xmlNodeSetPtr set1, xmlNodeSetPtr set2, |
| 3994 | int hasNullEntries) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 3995 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 3996 | if (set2 == NULL) |
| 3997 | return(set1); |
| 3998 | if ((set1 == NULL) && (hasNullEntries == 0)) { |
| 3999 | /* |
| 4000 | * Note that doing a memcpy of the list, namespace nodes are |
| 4001 | * just assigned to set1, since set2 is cleared anyway. |
| 4002 | */ |
| 4003 | set1 = xmlXPathNodeSetCreateSize(set2->nodeNr); |
| 4004 | if (set1 == NULL) |
| 4005 | return(NULL); |
| 4006 | if (set2->nodeNr != 0) { |
| 4007 | memcpy(set1->nodeTab, set2->nodeTab, |
| 4008 | set2->nodeNr * sizeof(xmlNodePtr)); |
| 4009 | set1->nodeNr = set2->nodeNr; |
| 4010 | } |
| 4011 | } else { |
| 4012 | int i; |
| 4013 | xmlNodePtr n2; |
| 4014 | |
| 4015 | if (set1 == NULL) |
| 4016 | set1 = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4017 | if (set1 == NULL) |
| 4018 | return (NULL); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4019 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4020 | for (i = 0;i < set2->nodeNr;i++) { |
| 4021 | n2 = set2->nodeTab[i]; |
| 4022 | /* |
| 4023 | * Skip NULLed entries. |
| 4024 | */ |
| 4025 | if (n2 == NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4026 | continue; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4027 | if (set1->nodeMax == 0) { |
| 4028 | set1->nodeTab = (xmlNodePtr *) xmlMalloc( |
| 4029 | XML_NODESET_DEFAULT * sizeof(xmlNodePtr)); |
| 4030 | if (set1->nodeTab == NULL) { |
| 4031 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
| 4032 | return(NULL); |
| 4033 | } |
| 4034 | memset(set1->nodeTab, 0, |
| 4035 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 4036 | set1->nodeMax = XML_NODESET_DEFAULT; |
| 4037 | } else if (set1->nodeNr >= set1->nodeMax) { |
| 4038 | xmlNodePtr *temp; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4039 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4040 | temp = (xmlNodePtr *) xmlRealloc( |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 4041 | set1->nodeTab, set1->nodeMax * 2 * sizeof(xmlNodePtr)); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4042 | if (temp == NULL) { |
| 4043 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
| 4044 | return(NULL); |
| 4045 | } |
| 4046 | set1->nodeTab = temp; |
Chris Evans | d7958b2 | 2011-03-23 08:13:06 +0800 | [diff] [blame] | 4047 | set1->nodeMax *= 2; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4048 | } |
| 4049 | set1->nodeTab[set1->nodeNr++] = n2; |
| 4050 | } |
| 4051 | } |
| 4052 | set2->nodeNr = 0; |
| 4053 | return(set1); |
| 4054 | } |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 4055 | |
| 4056 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4057 | * xmlXPathNodeSetDel: |
| 4058 | * @cur: the initial node set |
| 4059 | * @val: an xmlNodePtr |
| 4060 | * |
| 4061 | * Removes an xmlNodePtr from an existing NodeSet |
| 4062 | */ |
| 4063 | void |
| 4064 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 4065 | int i; |
| 4066 | |
| 4067 | if (cur == NULL) return; |
| 4068 | if (val == NULL) return; |
| 4069 | |
| 4070 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4071 | * find node in nodeTab |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4072 | */ |
| 4073 | for (i = 0;i < cur->nodeNr;i++) |
| 4074 | if (cur->nodeTab[i] == val) break; |
| 4075 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4076 | if (i >= cur->nodeNr) { /* not found */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4077 | #ifdef DEBUG |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4078 | xmlGenericError(xmlGenericErrorContext, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4079 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 4080 | val->name); |
| 4081 | #endif |
| 4082 | return; |
| 4083 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4084 | if ((cur->nodeTab[i] != NULL) && |
| 4085 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 4086 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4087 | cur->nodeNr--; |
| 4088 | for (;i < cur->nodeNr;i++) |
| 4089 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 4090 | cur->nodeTab[cur->nodeNr] = NULL; |
| 4091 | } |
| 4092 | |
| 4093 | /** |
| 4094 | * xmlXPathNodeSetRemove: |
| 4095 | * @cur: the initial node set |
| 4096 | * @val: the index to remove |
| 4097 | * |
| 4098 | * Removes an entry from an existing NodeSet list. |
| 4099 | */ |
| 4100 | void |
| 4101 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 4102 | if (cur == NULL) return; |
| 4103 | if (val >= cur->nodeNr) return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4104 | if ((cur->nodeTab[val] != NULL) && |
| 4105 | (cur->nodeTab[val]->type == XML_NAMESPACE_DECL)) |
| 4106 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4107 | cur->nodeNr--; |
| 4108 | for (;val < cur->nodeNr;val++) |
| 4109 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 4110 | cur->nodeTab[cur->nodeNr] = NULL; |
| 4111 | } |
| 4112 | |
| 4113 | /** |
| 4114 | * xmlXPathFreeNodeSet: |
| 4115 | * @obj: the xmlNodeSetPtr to free |
| 4116 | * |
| 4117 | * Free the NodeSet compound (not the actual nodes !). |
| 4118 | */ |
| 4119 | void |
| 4120 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 4121 | if (obj == NULL) return; |
| 4122 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4123 | int i; |
| 4124 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4125 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4126 | for (i = 0;i < obj->nodeNr;i++) |
| 4127 | if ((obj->nodeTab[i] != NULL) && |
| 4128 | (obj->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 4129 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4130 | xmlFree(obj->nodeTab); |
| 4131 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4132 | xmlFree(obj); |
| 4133 | } |
| 4134 | |
| 4135 | /** |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4136 | * xmlXPathNodeSetClear: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4137 | * @set: the node set to clear |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4138 | * |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4139 | * Clears the list from all temporary XPath objects (e.g. namespace nodes |
| 4140 | * are feed), but does *not* free the list itself. Sets the length of the |
| 4141 | * list to 0. |
| 4142 | */ |
| 4143 | static void |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4144 | xmlXPathNodeSetClear(xmlNodeSetPtr set, int hasNsNodes) |
| 4145 | { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4146 | if ((set == NULL) || (set->nodeNr <= 0)) |
| 4147 | return; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4148 | else if (hasNsNodes) { |
| 4149 | int i; |
| 4150 | xmlNodePtr node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4151 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4152 | for (i = 0; i < set->nodeNr; i++) { |
| 4153 | node = set->nodeTab[i]; |
| 4154 | if ((node != NULL) && |
| 4155 | (node->type == XML_NAMESPACE_DECL)) |
| 4156 | xmlXPathNodeSetFreeNs((xmlNsPtr) node); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4157 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4158 | } |
| 4159 | set->nodeNr = 0; |
| 4160 | } |
| 4161 | |
| 4162 | /** |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4163 | * xmlXPathNodeSetClearFromPos: |
| 4164 | * @set: the node set to be cleared |
| 4165 | * @pos: the start position to clear from |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4166 | * |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4167 | * Clears the list from temporary XPath objects (e.g. namespace nodes |
| 4168 | * are feed) starting with the entry at @pos, but does *not* free the list |
| 4169 | * itself. Sets the length of the list to @pos. |
| 4170 | */ |
| 4171 | static void |
| 4172 | xmlXPathNodeSetClearFromPos(xmlNodeSetPtr set, int pos, int hasNsNodes) |
| 4173 | { |
| 4174 | if ((set == NULL) || (set->nodeNr <= 0) || (pos >= set->nodeNr)) |
| 4175 | return; |
| 4176 | else if ((hasNsNodes)) { |
| 4177 | int i; |
| 4178 | xmlNodePtr node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4179 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4180 | for (i = pos; i < set->nodeNr; i++) { |
| 4181 | node = set->nodeTab[i]; |
| 4182 | if ((node != NULL) && |
| 4183 | (node->type == XML_NAMESPACE_DECL)) |
| 4184 | xmlXPathNodeSetFreeNs((xmlNsPtr) node); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4185 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 4186 | } |
| 4187 | set->nodeNr = pos; |
| 4188 | } |
| 4189 | |
| 4190 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4191 | * xmlXPathFreeValueTree: |
| 4192 | * @obj: the xmlNodeSetPtr to free |
| 4193 | * |
| 4194 | * Free the NodeSet compound and the actual tree, this is different |
| 4195 | * from xmlXPathFreeNodeSet() |
| 4196 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4197 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4198 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 4199 | int i; |
| 4200 | |
| 4201 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4202 | |
| 4203 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4204 | for (i = 0;i < obj->nodeNr;i++) { |
| 4205 | if (obj->nodeTab[i] != NULL) { |
| 4206 | if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 4207 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
| 4208 | } else { |
| 4209 | xmlFreeNodeList(obj->nodeTab[i]); |
| 4210 | } |
| 4211 | } |
| 4212 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4213 | xmlFree(obj->nodeTab); |
| 4214 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4215 | xmlFree(obj); |
| 4216 | } |
| 4217 | |
| 4218 | #if defined(DEBUG) || defined(DEBUG_STEP) |
| 4219 | /** |
| 4220 | * xmlGenericErrorContextNodeSet: |
| 4221 | * @output: a FILE * for the output |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4222 | * @obj: the xmlNodeSetPtr to display |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4223 | * |
| 4224 | * Quick display of a NodeSet |
| 4225 | */ |
| 4226 | void |
| 4227 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
| 4228 | int i; |
| 4229 | |
| 4230 | if (output == NULL) output = xmlGenericErrorContext; |
| 4231 | if (obj == NULL) { |
| 4232 | fprintf(output, "NodeSet == NULL !\n"); |
| 4233 | return; |
| 4234 | } |
| 4235 | if (obj->nodeNr == 0) { |
| 4236 | fprintf(output, "NodeSet is empty\n"); |
| 4237 | return; |
| 4238 | } |
| 4239 | if (obj->nodeTab == NULL) { |
| 4240 | fprintf(output, " nodeTab == NULL !\n"); |
| 4241 | return; |
| 4242 | } |
| 4243 | for (i = 0; i < obj->nodeNr; i++) { |
| 4244 | if (obj->nodeTab[i] == NULL) { |
| 4245 | fprintf(output, " NULL !\n"); |
| 4246 | return; |
| 4247 | } |
| 4248 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 4249 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
| 4250 | fprintf(output, " /"); |
| 4251 | else if (obj->nodeTab[i]->name == NULL) |
| 4252 | fprintf(output, " noname!"); |
| 4253 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 4254 | } |
| 4255 | fprintf(output, "\n"); |
| 4256 | } |
| 4257 | #endif |
| 4258 | |
| 4259 | /** |
| 4260 | * xmlXPathNewNodeSet: |
| 4261 | * @val: the NodePtr value |
| 4262 | * |
| 4263 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 4264 | * it with the single Node @val |
| 4265 | * |
| 4266 | * Returns the newly created object. |
| 4267 | */ |
| 4268 | xmlXPathObjectPtr |
| 4269 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 4270 | xmlXPathObjectPtr ret; |
| 4271 | |
| 4272 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 4273 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4274 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4275 | return(NULL); |
| 4276 | } |
| 4277 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 4278 | ret->type = XPATH_NODESET; |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 4279 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4280 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4281 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4282 | #ifdef XP_DEBUG_OBJ_USAGE |
| 4283 | xmlXPathDebugObjUsageRequested(NULL, XPATH_NODESET); |
| 4284 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4285 | return(ret); |
| 4286 | } |
| 4287 | |
| 4288 | /** |
| 4289 | * xmlXPathNewValueTree: |
| 4290 | * @val: the NodePtr value |
| 4291 | * |
| 4292 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 4293 | * it with the tree root @val |
| 4294 | * |
| 4295 | * Returns the newly created object. |
| 4296 | */ |
| 4297 | xmlXPathObjectPtr |
| 4298 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 4299 | xmlXPathObjectPtr ret; |
| 4300 | |
| 4301 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 4302 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4303 | xmlXPathErrMemory(NULL, "creating result value tree\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4304 | return(NULL); |
| 4305 | } |
| 4306 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 4307 | ret->type = XPATH_XSLT_TREE; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 4308 | ret->boolval = 1; |
| 4309 | ret->user = (void *) val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4310 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4311 | #ifdef XP_DEBUG_OBJ_USAGE |
| 4312 | xmlXPathDebugObjUsageRequested(NULL, XPATH_XSLT_TREE); |
| 4313 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4314 | return(ret); |
| 4315 | } |
| 4316 | |
| 4317 | /** |
| 4318 | * xmlXPathNewNodeSetList: |
| 4319 | * @val: an existing NodeSet |
| 4320 | * |
| 4321 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 4322 | * it with the Nodeset @val |
| 4323 | * |
| 4324 | * Returns the newly created object. |
| 4325 | */ |
| 4326 | xmlXPathObjectPtr |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4327 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) |
| 4328 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4329 | xmlXPathObjectPtr ret; |
| 4330 | int i; |
| 4331 | |
| 4332 | if (val == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4333 | ret = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4334 | else if (val->nodeTab == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4335 | ret = xmlXPathNewNodeSet(NULL); |
| 4336 | else { |
| 4337 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4338 | if (ret) |
| 4339 | for (i = 1; i < val->nodeNr; ++i) |
| 4340 | xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4341 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4342 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4343 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | /** |
| 4347 | * xmlXPathWrapNodeSet: |
| 4348 | * @val: the NodePtr value |
| 4349 | * |
| 4350 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 4351 | * |
| 4352 | * Returns the newly created object. |
| 4353 | */ |
| 4354 | xmlXPathObjectPtr |
| 4355 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 4356 | xmlXPathObjectPtr ret; |
| 4357 | |
| 4358 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 4359 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4360 | xmlXPathErrMemory(NULL, "creating node set object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4361 | return(NULL); |
| 4362 | } |
| 4363 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 4364 | ret->type = XPATH_NODESET; |
| 4365 | ret->nodesetval = val; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4366 | #ifdef XP_DEBUG_OBJ_USAGE |
| 4367 | xmlXPathDebugObjUsageRequested(NULL, XPATH_NODESET); |
| 4368 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4369 | return(ret); |
| 4370 | } |
| 4371 | |
| 4372 | /** |
| 4373 | * xmlXPathFreeNodeSetList: |
| 4374 | * @obj: an existing NodeSetList object |
| 4375 | * |
| 4376 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 4377 | * the list contrary to xmlXPathFreeObject(). |
| 4378 | */ |
| 4379 | void |
| 4380 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 4381 | if (obj == NULL) return; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 4382 | #ifdef XP_DEBUG_OBJ_USAGE |
| 4383 | xmlXPathDebugObjUsageReleased(NULL, obj->type); |
| 4384 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4385 | xmlFree(obj); |
| 4386 | } |
| 4387 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4388 | /** |
| 4389 | * xmlXPathDifference: |
| 4390 | * @nodes1: a node-set |
| 4391 | * @nodes2: a node-set |
| 4392 | * |
| 4393 | * Implements the EXSLT - Sets difference() function: |
| 4394 | * node-set set:difference (node-set, node-set) |
| 4395 | * |
| 4396 | * Returns the difference between the two node sets, or nodes1 if |
| 4397 | * nodes2 is empty |
| 4398 | */ |
| 4399 | xmlNodeSetPtr |
| 4400 | xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4401 | xmlNodeSetPtr ret; |
| 4402 | int i, l1; |
| 4403 | xmlNodePtr cur; |
| 4404 | |
| 4405 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4406 | return(nodes1); |
| 4407 | |
| 4408 | ret = xmlXPathNodeSetCreate(NULL); |
| 4409 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 4410 | return(ret); |
| 4411 | |
| 4412 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 4413 | |
| 4414 | for (i = 0; i < l1; i++) { |
| 4415 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 4416 | if (!xmlXPathNodeSetContains(nodes2, cur)) |
| 4417 | xmlXPathNodeSetAddUnique(ret, cur); |
| 4418 | } |
| 4419 | return(ret); |
| 4420 | } |
| 4421 | |
| 4422 | /** |
| 4423 | * xmlXPathIntersection: |
| 4424 | * @nodes1: a node-set |
| 4425 | * @nodes2: a node-set |
| 4426 | * |
| 4427 | * Implements the EXSLT - Sets intersection() function: |
| 4428 | * node-set set:intersection (node-set, node-set) |
| 4429 | * |
| 4430 | * Returns a node set comprising the nodes that are within both the |
| 4431 | * node sets passed as arguments |
| 4432 | */ |
| 4433 | xmlNodeSetPtr |
| 4434 | xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4435 | xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL); |
| 4436 | int i, l1; |
| 4437 | xmlNodePtr cur; |
| 4438 | |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4439 | if (ret == NULL) |
| 4440 | return(ret); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4441 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 4442 | return(ret); |
| 4443 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4444 | return(ret); |
| 4445 | |
| 4446 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 4447 | |
| 4448 | for (i = 0; i < l1; i++) { |
| 4449 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 4450 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 4451 | xmlXPathNodeSetAddUnique(ret, cur); |
| 4452 | } |
| 4453 | return(ret); |
| 4454 | } |
| 4455 | |
| 4456 | /** |
| 4457 | * xmlXPathDistinctSorted: |
| 4458 | * @nodes: a node-set, sorted by document order |
| 4459 | * |
| 4460 | * Implements the EXSLT - Sets distinct() function: |
| 4461 | * node-set set:distinct (node-set) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4462 | * |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4463 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 4464 | * it is empty |
| 4465 | */ |
| 4466 | xmlNodeSetPtr |
| 4467 | xmlXPathDistinctSorted (xmlNodeSetPtr nodes) { |
| 4468 | xmlNodeSetPtr ret; |
| 4469 | xmlHashTablePtr hash; |
| 4470 | int i, l; |
| 4471 | xmlChar * strval; |
| 4472 | xmlNodePtr cur; |
| 4473 | |
| 4474 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 4475 | return(nodes); |
| 4476 | |
| 4477 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4478 | if (ret == NULL) |
| 4479 | return(ret); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4480 | l = xmlXPathNodeSetGetLength(nodes); |
| 4481 | hash = xmlHashCreate (l); |
| 4482 | for (i = 0; i < l; i++) { |
| 4483 | cur = xmlXPathNodeSetItem(nodes, i); |
| 4484 | strval = xmlXPathCastNodeToString(cur); |
| 4485 | if (xmlHashLookup(hash, strval) == NULL) { |
| 4486 | xmlHashAddEntry(hash, strval, strval); |
| 4487 | xmlXPathNodeSetAddUnique(ret, cur); |
| 4488 | } else { |
| 4489 | xmlFree(strval); |
| 4490 | } |
| 4491 | } |
| 4492 | xmlHashFree(hash, (xmlHashDeallocator) xmlFree); |
| 4493 | return(ret); |
| 4494 | } |
| 4495 | |
| 4496 | /** |
| 4497 | * xmlXPathDistinct: |
| 4498 | * @nodes: a node-set |
| 4499 | * |
| 4500 | * Implements the EXSLT - Sets distinct() function: |
| 4501 | * node-set set:distinct (node-set) |
| 4502 | * @nodes is sorted by document order, then #exslSetsDistinctSorted |
| 4503 | * is called with the sorted node-set |
| 4504 | * |
| 4505 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 4506 | * it is empty |
| 4507 | */ |
| 4508 | xmlNodeSetPtr |
| 4509 | xmlXPathDistinct (xmlNodeSetPtr nodes) { |
| 4510 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 4511 | return(nodes); |
| 4512 | |
| 4513 | xmlXPathNodeSetSort(nodes); |
| 4514 | return(xmlXPathDistinctSorted(nodes)); |
| 4515 | } |
| 4516 | |
| 4517 | /** |
| 4518 | * xmlXPathHasSameNodes: |
| 4519 | * @nodes1: a node-set |
| 4520 | * @nodes2: a node-set |
| 4521 | * |
| 4522 | * Implements the EXSLT - Sets has-same-nodes function: |
| 4523 | * boolean set:has-same-node(node-set, node-set) |
| 4524 | * |
| 4525 | * Returns true (1) if @nodes1 shares any node with @nodes2, false (0) |
| 4526 | * otherwise |
| 4527 | */ |
| 4528 | int |
| 4529 | xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4530 | int i, l; |
| 4531 | xmlNodePtr cur; |
| 4532 | |
| 4533 | if (xmlXPathNodeSetIsEmpty(nodes1) || |
| 4534 | xmlXPathNodeSetIsEmpty(nodes2)) |
| 4535 | return(0); |
| 4536 | |
| 4537 | l = xmlXPathNodeSetGetLength(nodes1); |
| 4538 | for (i = 0; i < l; i++) { |
| 4539 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 4540 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 4541 | return(1); |
| 4542 | } |
| 4543 | return(0); |
| 4544 | } |
| 4545 | |
| 4546 | /** |
| 4547 | * xmlXPathNodeLeadingSorted: |
| 4548 | * @nodes: a node-set, sorted by document order |
| 4549 | * @node: a node |
| 4550 | * |
| 4551 | * Implements the EXSLT - Sets leading() function: |
| 4552 | * node-set set:leading (node-set, node-set) |
| 4553 | * |
| 4554 | * Returns the nodes in @nodes that precede @node in document order, |
| 4555 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 4556 | * doesn't contain @node |
| 4557 | */ |
| 4558 | xmlNodeSetPtr |
| 4559 | xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 4560 | int i, l; |
| 4561 | xmlNodePtr cur; |
| 4562 | xmlNodeSetPtr ret; |
| 4563 | |
| 4564 | if (node == NULL) |
| 4565 | return(nodes); |
| 4566 | |
| 4567 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4568 | if (ret == NULL) |
| 4569 | return(ret); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4570 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 4571 | (!xmlXPathNodeSetContains(nodes, node))) |
| 4572 | return(ret); |
| 4573 | |
| 4574 | l = xmlXPathNodeSetGetLength(nodes); |
| 4575 | for (i = 0; i < l; i++) { |
| 4576 | cur = xmlXPathNodeSetItem(nodes, i); |
| 4577 | if (cur == node) |
| 4578 | break; |
| 4579 | xmlXPathNodeSetAddUnique(ret, cur); |
| 4580 | } |
| 4581 | return(ret); |
| 4582 | } |
| 4583 | |
| 4584 | /** |
| 4585 | * xmlXPathNodeLeading: |
| 4586 | * @nodes: a node-set |
| 4587 | * @node: a node |
| 4588 | * |
| 4589 | * Implements the EXSLT - Sets leading() function: |
| 4590 | * node-set set:leading (node-set, node-set) |
| 4591 | * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted |
| 4592 | * is called. |
| 4593 | * |
| 4594 | * Returns the nodes in @nodes that precede @node in document order, |
| 4595 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 4596 | * doesn't contain @node |
| 4597 | */ |
| 4598 | xmlNodeSetPtr |
| 4599 | xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 4600 | xmlXPathNodeSetSort(nodes); |
| 4601 | return(xmlXPathNodeLeadingSorted(nodes, node)); |
| 4602 | } |
| 4603 | |
| 4604 | /** |
| 4605 | * xmlXPathLeadingSorted: |
| 4606 | * @nodes1: a node-set, sorted by document order |
| 4607 | * @nodes2: a node-set, sorted by document order |
| 4608 | * |
| 4609 | * Implements the EXSLT - Sets leading() function: |
| 4610 | * node-set set:leading (node-set, node-set) |
| 4611 | * |
| 4612 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 4613 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 4614 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 4615 | */ |
| 4616 | xmlNodeSetPtr |
| 4617 | xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4618 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4619 | return(nodes1); |
| 4620 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 4621 | xmlXPathNodeSetItem(nodes2, 1))); |
| 4622 | } |
| 4623 | |
| 4624 | /** |
| 4625 | * xmlXPathLeading: |
| 4626 | * @nodes1: a node-set |
| 4627 | * @nodes2: a node-set |
| 4628 | * |
| 4629 | * Implements the EXSLT - Sets leading() function: |
| 4630 | * node-set set:leading (node-set, node-set) |
| 4631 | * @nodes1 and @nodes2 are sorted by document order, then |
| 4632 | * #exslSetsLeadingSorted is called. |
| 4633 | * |
| 4634 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 4635 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 4636 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 4637 | */ |
| 4638 | xmlNodeSetPtr |
| 4639 | xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4640 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4641 | return(nodes1); |
| 4642 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 4643 | return(xmlXPathNodeSetCreate(NULL)); |
| 4644 | xmlXPathNodeSetSort(nodes1); |
| 4645 | xmlXPathNodeSetSort(nodes2); |
| 4646 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 4647 | xmlXPathNodeSetItem(nodes2, 1))); |
| 4648 | } |
| 4649 | |
| 4650 | /** |
| 4651 | * xmlXPathNodeTrailingSorted: |
| 4652 | * @nodes: a node-set, sorted by document order |
| 4653 | * @node: a node |
| 4654 | * |
| 4655 | * Implements the EXSLT - Sets trailing() function: |
| 4656 | * node-set set:trailing (node-set, node-set) |
| 4657 | * |
| 4658 | * Returns the nodes in @nodes that follow @node in document order, |
| 4659 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 4660 | * doesn't contain @node |
| 4661 | */ |
| 4662 | xmlNodeSetPtr |
| 4663 | xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 4664 | int i, l; |
| 4665 | xmlNodePtr cur; |
| 4666 | xmlNodeSetPtr ret; |
| 4667 | |
| 4668 | if (node == NULL) |
| 4669 | return(nodes); |
| 4670 | |
| 4671 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 4672 | if (ret == NULL) |
| 4673 | return(ret); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4674 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 4675 | (!xmlXPathNodeSetContains(nodes, node))) |
| 4676 | return(ret); |
| 4677 | |
| 4678 | l = xmlXPathNodeSetGetLength(nodes); |
William M. Brack | 97ac819 | 2007-06-06 17:19:24 +0000 | [diff] [blame] | 4679 | for (i = l - 1; i >= 0; i--) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4680 | cur = xmlXPathNodeSetItem(nodes, i); |
| 4681 | if (cur == node) |
| 4682 | break; |
| 4683 | xmlXPathNodeSetAddUnique(ret, cur); |
| 4684 | } |
William M. Brack | 97ac819 | 2007-06-06 17:19:24 +0000 | [diff] [blame] | 4685 | xmlXPathNodeSetSort(ret); /* bug 413451 */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 4686 | return(ret); |
| 4687 | } |
| 4688 | |
| 4689 | /** |
| 4690 | * xmlXPathNodeTrailing: |
| 4691 | * @nodes: a node-set |
| 4692 | * @node: a node |
| 4693 | * |
| 4694 | * Implements the EXSLT - Sets trailing() function: |
| 4695 | * node-set set:trailing (node-set, node-set) |
| 4696 | * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted |
| 4697 | * is called. |
| 4698 | * |
| 4699 | * Returns the nodes in @nodes that follow @node in document order, |
| 4700 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 4701 | * doesn't contain @node |
| 4702 | */ |
| 4703 | xmlNodeSetPtr |
| 4704 | xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 4705 | xmlXPathNodeSetSort(nodes); |
| 4706 | return(xmlXPathNodeTrailingSorted(nodes, node)); |
| 4707 | } |
| 4708 | |
| 4709 | /** |
| 4710 | * xmlXPathTrailingSorted: |
| 4711 | * @nodes1: a node-set, sorted by document order |
| 4712 | * @nodes2: a node-set, sorted by document order |
| 4713 | * |
| 4714 | * Implements the EXSLT - Sets trailing() function: |
| 4715 | * node-set set:trailing (node-set, node-set) |
| 4716 | * |
| 4717 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 4718 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 4719 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 4720 | */ |
| 4721 | xmlNodeSetPtr |
| 4722 | xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4723 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4724 | return(nodes1); |
| 4725 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 4726 | xmlXPathNodeSetItem(nodes2, 0))); |
| 4727 | } |
| 4728 | |
| 4729 | /** |
| 4730 | * xmlXPathTrailing: |
| 4731 | * @nodes1: a node-set |
| 4732 | * @nodes2: a node-set |
| 4733 | * |
| 4734 | * Implements the EXSLT - Sets trailing() function: |
| 4735 | * node-set set:trailing (node-set, node-set) |
| 4736 | * @nodes1 and @nodes2 are sorted by document order, then |
| 4737 | * #xmlXPathTrailingSorted is called. |
| 4738 | * |
| 4739 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 4740 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 4741 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 4742 | */ |
| 4743 | xmlNodeSetPtr |
| 4744 | xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 4745 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 4746 | return(nodes1); |
| 4747 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 4748 | return(xmlXPathNodeSetCreate(NULL)); |
| 4749 | xmlXPathNodeSetSort(nodes1); |
| 4750 | xmlXPathNodeSetSort(nodes2); |
| 4751 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 4752 | xmlXPathNodeSetItem(nodes2, 0))); |
| 4753 | } |
| 4754 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4755 | /************************************************************************ |
| 4756 | * * |
| 4757 | * Routines to handle extra functions * |
| 4758 | * * |
| 4759 | ************************************************************************/ |
| 4760 | |
| 4761 | /** |
| 4762 | * xmlXPathRegisterFunc: |
| 4763 | * @ctxt: the XPath context |
| 4764 | * @name: the function name |
| 4765 | * @f: the function implementation or NULL |
| 4766 | * |
| 4767 | * Register a new function. If @f is NULL it unregisters the function |
| 4768 | * |
| 4769 | * Returns 0 in case of success, -1 in case of error |
| 4770 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4771 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4772 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 4773 | xmlXPathFunction f) { |
| 4774 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 4775 | } |
| 4776 | |
| 4777 | /** |
| 4778 | * xmlXPathRegisterFuncNS: |
| 4779 | * @ctxt: the XPath context |
| 4780 | * @name: the function name |
| 4781 | * @ns_uri: the function namespace URI |
| 4782 | * @f: the function implementation or NULL |
| 4783 | * |
| 4784 | * Register a new function. If @f is NULL it unregisters the function |
| 4785 | * |
| 4786 | * Returns 0 in case of success, -1 in case of error |
| 4787 | */ |
| 4788 | int |
| 4789 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 4790 | const xmlChar *ns_uri, xmlXPathFunction f) { |
| 4791 | if (ctxt == NULL) |
| 4792 | return(-1); |
| 4793 | if (name == NULL) |
| 4794 | return(-1); |
| 4795 | |
| 4796 | if (ctxt->funcHash == NULL) |
| 4797 | ctxt->funcHash = xmlHashCreate(0); |
| 4798 | if (ctxt->funcHash == NULL) |
| 4799 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 4800 | if (f == NULL) |
| 4801 | return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL)); |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 4802 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, XML_CAST_FPTR(f))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | /** |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4806 | * xmlXPathRegisterFuncLookup: |
| 4807 | * @ctxt: the XPath context |
| 4808 | * @f: the lookup function |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4809 | * @funcCtxt: the lookup data |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4810 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4811 | * Registers an external mechanism to do function lookup. |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4812 | */ |
| 4813 | void |
| 4814 | xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt, |
| 4815 | xmlXPathFuncLookupFunc f, |
| 4816 | void *funcCtxt) { |
| 4817 | if (ctxt == NULL) |
| 4818 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 4819 | ctxt->funcLookupFunc = f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4820 | ctxt->funcLookupData = funcCtxt; |
| 4821 | } |
| 4822 | |
| 4823 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4824 | * xmlXPathFunctionLookup: |
| 4825 | * @ctxt: the XPath context |
| 4826 | * @name: the function name |
| 4827 | * |
| 4828 | * Search in the Function array of the context for the given |
| 4829 | * function. |
| 4830 | * |
| 4831 | * Returns the xmlXPathFunction or NULL if not found |
| 4832 | */ |
| 4833 | xmlXPathFunction |
| 4834 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4835 | if (ctxt == NULL) |
| 4836 | return (NULL); |
| 4837 | |
| 4838 | if (ctxt->funcLookupFunc != NULL) { |
| 4839 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 4840 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4841 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 4842 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 4843 | ret = f(ctxt->funcLookupData, name, NULL); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4844 | if (ret != NULL) |
| 4845 | return(ret); |
| 4846 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4847 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 4848 | } |
| 4849 | |
| 4850 | /** |
| 4851 | * xmlXPathFunctionLookupNS: |
| 4852 | * @ctxt: the XPath context |
| 4853 | * @name: the function name |
| 4854 | * @ns_uri: the function namespace URI |
| 4855 | * |
| 4856 | * Search in the Function array of the context for the given |
| 4857 | * function. |
| 4858 | * |
| 4859 | * Returns the xmlXPathFunction or NULL if not found |
| 4860 | */ |
| 4861 | xmlXPathFunction |
| 4862 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 4863 | const xmlChar *ns_uri) { |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 4864 | xmlXPathFunction ret; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4865 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4866 | if (ctxt == NULL) |
| 4867 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4868 | if (name == NULL) |
| 4869 | return(NULL); |
| 4870 | |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4871 | if (ctxt->funcLookupFunc != NULL) { |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 4872 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4873 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 4874 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 4875 | ret = f(ctxt->funcLookupData, name, ns_uri); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 4876 | if (ret != NULL) |
| 4877 | return(ret); |
| 4878 | } |
| 4879 | |
| 4880 | if (ctxt->funcHash == NULL) |
| 4881 | return(NULL); |
| 4882 | |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 4883 | XML_CAST_FPTR(ret) = xmlHashLookup2(ctxt->funcHash, name, ns_uri); |
| 4884 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | /** |
| 4888 | * xmlXPathRegisteredFuncsCleanup: |
| 4889 | * @ctxt: the XPath context |
| 4890 | * |
| 4891 | * Cleanup the XPath context data associated to registered functions |
| 4892 | */ |
| 4893 | void |
| 4894 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
| 4895 | if (ctxt == NULL) |
| 4896 | return; |
| 4897 | |
| 4898 | xmlHashFree(ctxt->funcHash, NULL); |
| 4899 | ctxt->funcHash = NULL; |
| 4900 | } |
| 4901 | |
| 4902 | /************************************************************************ |
| 4903 | * * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4904 | * Routines to handle Variables * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4905 | * * |
| 4906 | ************************************************************************/ |
| 4907 | |
| 4908 | /** |
| 4909 | * xmlXPathRegisterVariable: |
| 4910 | * @ctxt: the XPath context |
| 4911 | * @name: the variable name |
| 4912 | * @value: the variable value or NULL |
| 4913 | * |
| 4914 | * Register a new variable value. If @value is NULL it unregisters |
| 4915 | * the variable |
| 4916 | * |
| 4917 | * Returns 0 in case of success, -1 in case of error |
| 4918 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4919 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4920 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 4921 | xmlXPathObjectPtr value) { |
| 4922 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 4923 | } |
| 4924 | |
| 4925 | /** |
| 4926 | * xmlXPathRegisterVariableNS: |
| 4927 | * @ctxt: the XPath context |
| 4928 | * @name: the variable name |
| 4929 | * @ns_uri: the variable namespace URI |
| 4930 | * @value: the variable value or NULL |
| 4931 | * |
| 4932 | * Register a new variable value. If @value is NULL it unregisters |
| 4933 | * the variable |
| 4934 | * |
| 4935 | * Returns 0 in case of success, -1 in case of error |
| 4936 | */ |
| 4937 | int |
| 4938 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 4939 | const xmlChar *ns_uri, |
| 4940 | xmlXPathObjectPtr value) { |
| 4941 | if (ctxt == NULL) |
| 4942 | return(-1); |
| 4943 | if (name == NULL) |
| 4944 | return(-1); |
| 4945 | |
| 4946 | if (ctxt->varHash == NULL) |
| 4947 | ctxt->varHash = xmlHashCreate(0); |
| 4948 | if (ctxt->varHash == NULL) |
| 4949 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 4950 | if (value == NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 4951 | return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri, |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 4952 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4953 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 4954 | (void *) value, |
| 4955 | (xmlHashDeallocator)xmlXPathFreeObject)); |
| 4956 | } |
| 4957 | |
| 4958 | /** |
| 4959 | * xmlXPathRegisterVariableLookup: |
| 4960 | * @ctxt: the XPath context |
| 4961 | * @f: the lookup function |
| 4962 | * @data: the lookup data |
| 4963 | * |
| 4964 | * register an external mechanism to do variable lookup |
| 4965 | */ |
| 4966 | void |
| 4967 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 4968 | xmlXPathVariableLookupFunc f, void *data) { |
| 4969 | if (ctxt == NULL) |
| 4970 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 4971 | ctxt->varLookupFunc = f; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4972 | ctxt->varLookupData = data; |
| 4973 | } |
| 4974 | |
| 4975 | /** |
| 4976 | * xmlXPathVariableLookup: |
| 4977 | * @ctxt: the XPath context |
| 4978 | * @name: the variable name |
| 4979 | * |
| 4980 | * Search in the Variable array of the context for the given |
| 4981 | * variable value. |
| 4982 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 4983 | * Returns a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4984 | */ |
| 4985 | xmlXPathObjectPtr |
| 4986 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
| 4987 | if (ctxt == NULL) |
| 4988 | return(NULL); |
| 4989 | |
| 4990 | if (ctxt->varLookupFunc != NULL) { |
| 4991 | xmlXPathObjectPtr ret; |
| 4992 | |
| 4993 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 4994 | (ctxt->varLookupData, name, NULL); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 4995 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4996 | } |
| 4997 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 4998 | } |
| 4999 | |
| 5000 | /** |
| 5001 | * xmlXPathVariableLookupNS: |
| 5002 | * @ctxt: the XPath context |
| 5003 | * @name: the variable name |
| 5004 | * @ns_uri: the variable namespace URI |
| 5005 | * |
| 5006 | * Search in the Variable array of the context for the given |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5007 | * variable value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5008 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 5009 | * Returns the a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5010 | */ |
| 5011 | xmlXPathObjectPtr |
| 5012 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 5013 | const xmlChar *ns_uri) { |
| 5014 | if (ctxt == NULL) |
| 5015 | return(NULL); |
| 5016 | |
| 5017 | if (ctxt->varLookupFunc != NULL) { |
| 5018 | xmlXPathObjectPtr ret; |
| 5019 | |
| 5020 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 5021 | (ctxt->varLookupData, name, ns_uri); |
| 5022 | if (ret != NULL) return(ret); |
| 5023 | } |
| 5024 | |
| 5025 | if (ctxt->varHash == NULL) |
| 5026 | return(NULL); |
| 5027 | if (name == NULL) |
| 5028 | return(NULL); |
| 5029 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5030 | return(xmlXPathCacheObjectCopy(ctxt, (xmlXPathObjectPtr) |
Daniel Veillard | 8c357d5 | 2001-07-03 23:43:33 +0000 | [diff] [blame] | 5031 | xmlHashLookup2(ctxt->varHash, name, ns_uri))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | /** |
| 5035 | * xmlXPathRegisteredVariablesCleanup: |
| 5036 | * @ctxt: the XPath context |
| 5037 | * |
| 5038 | * Cleanup the XPath context data associated to registered variables |
| 5039 | */ |
| 5040 | void |
| 5041 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
| 5042 | if (ctxt == NULL) |
| 5043 | return; |
| 5044 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5045 | xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5046 | ctxt->varHash = NULL; |
| 5047 | } |
| 5048 | |
| 5049 | /** |
| 5050 | * xmlXPathRegisterNs: |
| 5051 | * @ctxt: the XPath context |
Daniel Veillard | db3ce96 | 2009-03-25 09:43:49 +0000 | [diff] [blame] | 5052 | * @prefix: the namespace prefix cannot be NULL or empty string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5053 | * @ns_uri: the namespace name |
| 5054 | * |
| 5055 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 5056 | * the namespace |
| 5057 | * |
| 5058 | * Returns 0 in case of success, -1 in case of error |
| 5059 | */ |
| 5060 | int |
| 5061 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 5062 | const xmlChar *ns_uri) { |
| 5063 | if (ctxt == NULL) |
| 5064 | return(-1); |
| 5065 | if (prefix == NULL) |
| 5066 | return(-1); |
Daniel Veillard | db3ce96 | 2009-03-25 09:43:49 +0000 | [diff] [blame] | 5067 | if (prefix[0] == 0) |
| 5068 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5069 | |
| 5070 | if (ctxt->nsHash == NULL) |
| 5071 | ctxt->nsHash = xmlHashCreate(10); |
| 5072 | if (ctxt->nsHash == NULL) |
| 5073 | return(-1); |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 5074 | if (ns_uri == NULL) |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 5075 | return(xmlHashRemoveEntry(ctxt->nsHash, prefix, |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 5076 | (xmlHashDeallocator)xmlFree)); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 5077 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri), |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5078 | (xmlHashDeallocator)xmlFree)); |
| 5079 | } |
| 5080 | |
| 5081 | /** |
| 5082 | * xmlXPathNsLookup: |
| 5083 | * @ctxt: the XPath context |
| 5084 | * @prefix: the namespace prefix value |
| 5085 | * |
| 5086 | * Search in the namespace declaration array of the context for the given |
| 5087 | * namespace name associated to the given prefix |
| 5088 | * |
| 5089 | * Returns the value or NULL if not found |
| 5090 | */ |
| 5091 | const xmlChar * |
| 5092 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 5093 | if (ctxt == NULL) |
| 5094 | return(NULL); |
| 5095 | if (prefix == NULL) |
| 5096 | return(NULL); |
| 5097 | |
| 5098 | #ifdef XML_XML_NAMESPACE |
| 5099 | if (xmlStrEqual(prefix, (const xmlChar *) "xml")) |
| 5100 | return(XML_XML_NAMESPACE); |
| 5101 | #endif |
| 5102 | |
Daniel Veillard | c8f620b | 2001-04-30 20:31:33 +0000 | [diff] [blame] | 5103 | if (ctxt->namespaces != NULL) { |
| 5104 | int i; |
| 5105 | |
| 5106 | for (i = 0;i < ctxt->nsNr;i++) { |
| 5107 | if ((ctxt->namespaces[i] != NULL) && |
| 5108 | (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix))) |
| 5109 | return(ctxt->namespaces[i]->href); |
| 5110 | } |
| 5111 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5112 | |
| 5113 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 5114 | } |
| 5115 | |
| 5116 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 5117 | * xmlXPathRegisteredNsCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5118 | * @ctxt: the XPath context |
| 5119 | * |
| 5120 | * Cleanup the XPath context data associated to registered variables |
| 5121 | */ |
| 5122 | void |
| 5123 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 5124 | if (ctxt == NULL) |
| 5125 | return; |
| 5126 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 5127 | xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5128 | ctxt->nsHash = NULL; |
| 5129 | } |
| 5130 | |
| 5131 | /************************************************************************ |
| 5132 | * * |
| 5133 | * Routines to handle Values * |
| 5134 | * * |
| 5135 | ************************************************************************/ |
| 5136 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5137 | /* Allocations are terrible, one needs to optimize all this !!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5138 | |
| 5139 | /** |
| 5140 | * xmlXPathNewFloat: |
| 5141 | * @val: the double value |
| 5142 | * |
| 5143 | * Create a new xmlXPathObjectPtr of type double and of value @val |
| 5144 | * |
| 5145 | * Returns the newly created object. |
| 5146 | */ |
| 5147 | xmlXPathObjectPtr |
| 5148 | xmlXPathNewFloat(double val) { |
| 5149 | xmlXPathObjectPtr ret; |
| 5150 | |
| 5151 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5152 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5153 | xmlXPathErrMemory(NULL, "creating float object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5154 | return(NULL); |
| 5155 | } |
| 5156 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5157 | ret->type = XPATH_NUMBER; |
| 5158 | ret->floatval = val; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5159 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5160 | xmlXPathDebugObjUsageRequested(NULL, XPATH_NUMBER); |
| 5161 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5162 | return(ret); |
| 5163 | } |
| 5164 | |
| 5165 | /** |
| 5166 | * xmlXPathNewBoolean: |
| 5167 | * @val: the boolean value |
| 5168 | * |
| 5169 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 5170 | * |
| 5171 | * Returns the newly created object. |
| 5172 | */ |
| 5173 | xmlXPathObjectPtr |
| 5174 | xmlXPathNewBoolean(int val) { |
| 5175 | xmlXPathObjectPtr ret; |
| 5176 | |
| 5177 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5178 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5179 | xmlXPathErrMemory(NULL, "creating boolean object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5180 | return(NULL); |
| 5181 | } |
| 5182 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5183 | ret->type = XPATH_BOOLEAN; |
| 5184 | ret->boolval = (val != 0); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5185 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5186 | xmlXPathDebugObjUsageRequested(NULL, XPATH_BOOLEAN); |
| 5187 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5188 | return(ret); |
| 5189 | } |
| 5190 | |
| 5191 | /** |
| 5192 | * xmlXPathNewString: |
| 5193 | * @val: the xmlChar * value |
| 5194 | * |
| 5195 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 5196 | * |
| 5197 | * Returns the newly created object. |
| 5198 | */ |
| 5199 | xmlXPathObjectPtr |
| 5200 | xmlXPathNewString(const xmlChar *val) { |
| 5201 | xmlXPathObjectPtr ret; |
| 5202 | |
| 5203 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5204 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5205 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5206 | return(NULL); |
| 5207 | } |
| 5208 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5209 | ret->type = XPATH_STRING; |
| 5210 | if (val != NULL) |
| 5211 | ret->stringval = xmlStrdup(val); |
| 5212 | else |
| 5213 | ret->stringval = xmlStrdup((const xmlChar *)""); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5214 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5215 | xmlXPathDebugObjUsageRequested(NULL, XPATH_STRING); |
| 5216 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5217 | return(ret); |
| 5218 | } |
| 5219 | |
| 5220 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5221 | * xmlXPathWrapString: |
| 5222 | * @val: the xmlChar * value |
| 5223 | * |
| 5224 | * Wraps the @val string into an XPath object. |
| 5225 | * |
| 5226 | * Returns the newly created object. |
| 5227 | */ |
| 5228 | xmlXPathObjectPtr |
| 5229 | xmlXPathWrapString (xmlChar *val) { |
| 5230 | xmlXPathObjectPtr ret; |
| 5231 | |
| 5232 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5233 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5234 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5235 | return(NULL); |
| 5236 | } |
| 5237 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5238 | ret->type = XPATH_STRING; |
| 5239 | ret->stringval = val; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5240 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5241 | xmlXPathDebugObjUsageRequested(NULL, XPATH_STRING); |
| 5242 | #endif |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5243 | return(ret); |
| 5244 | } |
| 5245 | |
| 5246 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5247 | * xmlXPathNewCString: |
| 5248 | * @val: the char * value |
| 5249 | * |
| 5250 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 5251 | * |
| 5252 | * Returns the newly created object. |
| 5253 | */ |
| 5254 | xmlXPathObjectPtr |
| 5255 | xmlXPathNewCString(const char *val) { |
| 5256 | xmlXPathObjectPtr ret; |
| 5257 | |
| 5258 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5259 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5260 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5261 | return(NULL); |
| 5262 | } |
| 5263 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5264 | ret->type = XPATH_STRING; |
| 5265 | ret->stringval = xmlStrdup(BAD_CAST val); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5266 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5267 | xmlXPathDebugObjUsageRequested(NULL, XPATH_STRING); |
| 5268 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5269 | return(ret); |
| 5270 | } |
| 5271 | |
| 5272 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5273 | * xmlXPathWrapCString: |
| 5274 | * @val: the char * value |
| 5275 | * |
| 5276 | * Wraps a string into an XPath object. |
| 5277 | * |
| 5278 | * Returns the newly created object. |
| 5279 | */ |
| 5280 | xmlXPathObjectPtr |
| 5281 | xmlXPathWrapCString (char * val) { |
| 5282 | return(xmlXPathWrapString((xmlChar *)(val))); |
| 5283 | } |
| 5284 | |
| 5285 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 5286 | * xmlXPathWrapExternal: |
| 5287 | * @val: the user data |
| 5288 | * |
| 5289 | * Wraps the @val data into an XPath object. |
| 5290 | * |
| 5291 | * Returns the newly created object. |
| 5292 | */ |
| 5293 | xmlXPathObjectPtr |
| 5294 | xmlXPathWrapExternal (void *val) { |
| 5295 | xmlXPathObjectPtr ret; |
| 5296 | |
| 5297 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5298 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5299 | xmlXPathErrMemory(NULL, "creating user object\n"); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 5300 | return(NULL); |
| 5301 | } |
| 5302 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 5303 | ret->type = XPATH_USERS; |
| 5304 | ret->user = val; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5305 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5306 | xmlXPathDebugObjUsageRequested(NULL, XPATH_USERS); |
| 5307 | #endif |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 5308 | return(ret); |
| 5309 | } |
| 5310 | |
| 5311 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5312 | * xmlXPathObjectCopy: |
| 5313 | * @val: the original object |
| 5314 | * |
| 5315 | * allocate a new copy of a given object |
| 5316 | * |
| 5317 | * Returns the newly created object. |
| 5318 | */ |
| 5319 | xmlXPathObjectPtr |
| 5320 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 5321 | xmlXPathObjectPtr ret; |
| 5322 | |
| 5323 | if (val == NULL) |
| 5324 | return(NULL); |
| 5325 | |
| 5326 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 5327 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 5328 | xmlXPathErrMemory(NULL, "copying object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5329 | return(NULL); |
| 5330 | } |
| 5331 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5332 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5333 | xmlXPathDebugObjUsageRequested(NULL, val->type); |
| 5334 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5335 | switch (val->type) { |
| 5336 | case XPATH_BOOLEAN: |
| 5337 | case XPATH_NUMBER: |
| 5338 | case XPATH_POINT: |
| 5339 | case XPATH_RANGE: |
| 5340 | break; |
| 5341 | case XPATH_STRING: |
| 5342 | ret->stringval = xmlStrdup(val->stringval); |
| 5343 | break; |
| 5344 | case XPATH_XSLT_TREE: |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5345 | #if 0 |
| 5346 | /* |
| 5347 | Removed 11 July 2004 - the current handling of xslt tmpRVT nodes means that |
| 5348 | this previous handling is no longer correct, and can cause some serious |
| 5349 | problems (ref. bug 145547) |
| 5350 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5351 | if ((val->nodesetval != NULL) && |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5352 | (val->nodesetval->nodeTab != NULL)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5353 | xmlNodePtr cur, tmp; |
| 5354 | xmlDocPtr top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 5355 | |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5356 | ret->boolval = 1; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5357 | top = xmlNewDoc(NULL); |
| 5358 | top->name = (char *) |
| 5359 | xmlStrdup(val->nodesetval->nodeTab[0]->name); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 5360 | ret->user = top; |
| 5361 | if (top != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5362 | top->doc = top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 5363 | cur = val->nodesetval->nodeTab[0]->children; |
| 5364 | while (cur != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5365 | tmp = xmlDocCopyNode(cur, top, 1); |
| 5366 | xmlAddChild((xmlNodePtr) top, tmp); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 5367 | cur = cur->next; |
| 5368 | } |
| 5369 | } |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5370 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5371 | ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5372 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5373 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5374 | /* Deallocate the copied tree value */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5375 | break; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5376 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5377 | case XPATH_NODESET: |
| 5378 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5379 | /* Do not deallocate the copied tree value */ |
| 5380 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5381 | break; |
| 5382 | case XPATH_LOCATIONSET: |
| 5383 | #ifdef LIBXML_XPTR_ENABLED |
| 5384 | { |
| 5385 | xmlLocationSetPtr loc = val->user; |
| 5386 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 5387 | break; |
| 5388 | } |
| 5389 | #endif |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 5390 | case XPATH_USERS: |
| 5391 | ret->user = val->user; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5392 | break; |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 5393 | case XPATH_UNDEFINED: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5394 | xmlGenericError(xmlGenericErrorContext, |
| 5395 | "xmlXPathObjectCopy: unsupported type %d\n", |
| 5396 | val->type); |
| 5397 | break; |
| 5398 | } |
| 5399 | return(ret); |
| 5400 | } |
| 5401 | |
| 5402 | /** |
| 5403 | * xmlXPathFreeObject: |
| 5404 | * @obj: the object to free |
| 5405 | * |
| 5406 | * Free up an xmlXPathObjectPtr object. |
| 5407 | */ |
| 5408 | void |
| 5409 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 5410 | if (obj == NULL) return; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5411 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5412 | if (obj->boolval) { |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5413 | #if 0 |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5414 | if (obj->user != NULL) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 5415 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | 38bf6f0 | 2002-03-16 22:03:31 +0000 | [diff] [blame] | 5416 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5417 | } else |
| 5418 | #endif |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5419 | obj->type = XPATH_XSLT_TREE; /* TODO: Just for debugging. */ |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 5420 | if (obj->nodesetval != NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5421 | xmlXPathFreeValueTree(obj->nodesetval); |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5422 | } else { |
| 5423 | if (obj->nodesetval != NULL) |
| 5424 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 5425 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5426 | #ifdef LIBXML_XPTR_ENABLED |
| 5427 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 5428 | if (obj->user != NULL) |
| 5429 | xmlXPtrFreeLocationSet(obj->user); |
| 5430 | #endif |
| 5431 | } else if (obj->type == XPATH_STRING) { |
| 5432 | if (obj->stringval != NULL) |
| 5433 | xmlFree(obj->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5434 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5435 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5436 | xmlXPathDebugObjUsageReleased(NULL, obj->type); |
| 5437 | #endif |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5438 | xmlFree(obj); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5439 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5440 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5441 | /** |
| 5442 | * xmlXPathReleaseObject: |
| 5443 | * @obj: the xmlXPathObjectPtr to free or to cache |
| 5444 | * |
| 5445 | * Depending on the state of the cache this frees the given |
| 5446 | * XPath object or stores it in the cache. |
| 5447 | */ |
| 5448 | static void |
| 5449 | xmlXPathReleaseObject(xmlXPathContextPtr ctxt, xmlXPathObjectPtr obj) |
| 5450 | { |
| 5451 | #define XP_CACHE_ADD(sl, o) if (sl == NULL) { \ |
| 5452 | sl = xmlPointerListCreate(10); if (sl == NULL) goto free_obj; } \ |
| 5453 | if (xmlPointerListAddSize(sl, obj, 0) == -1) goto free_obj; |
| 5454 | |
| 5455 | #define XP_CACHE_WANTS(sl, n) ((sl == NULL) || ((sl)->number < n)) |
| 5456 | |
| 5457 | if (obj == NULL) |
| 5458 | return; |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 5459 | if ((ctxt == NULL) || (ctxt->cache == NULL)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5460 | xmlXPathFreeObject(obj); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5461 | } else { |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 5462 | xmlXPathContextCachePtr cache = |
| 5463 | (xmlXPathContextCachePtr) ctxt->cache; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5464 | |
| 5465 | switch (obj->type) { |
| 5466 | case XPATH_NODESET: |
| 5467 | case XPATH_XSLT_TREE: |
| 5468 | if (obj->nodesetval != NULL) { |
| 5469 | if (obj->boolval) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5470 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5471 | * It looks like the @boolval is used for |
| 5472 | * evaluation if this an XSLT Result Tree Fragment. |
| 5473 | * TODO: Check if this assumption is correct. |
| 5474 | */ |
| 5475 | obj->type = XPATH_XSLT_TREE; /* just for debugging */ |
| 5476 | xmlXPathFreeValueTree(obj->nodesetval); |
| 5477 | obj->nodesetval = NULL; |
| 5478 | } else if ((obj->nodesetval->nodeMax <= 40) && |
| 5479 | (XP_CACHE_WANTS(cache->nodesetObjs, |
| 5480 | cache->maxNodeset))) |
| 5481 | { |
| 5482 | XP_CACHE_ADD(cache->nodesetObjs, obj); |
| 5483 | goto obj_cached; |
| 5484 | } else { |
| 5485 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 5486 | obj->nodesetval = NULL; |
| 5487 | } |
| 5488 | } |
| 5489 | break; |
| 5490 | case XPATH_STRING: |
| 5491 | if (obj->stringval != NULL) |
| 5492 | xmlFree(obj->stringval); |
| 5493 | |
| 5494 | if (XP_CACHE_WANTS(cache->stringObjs, cache->maxString)) { |
| 5495 | XP_CACHE_ADD(cache->stringObjs, obj); |
| 5496 | goto obj_cached; |
| 5497 | } |
| 5498 | break; |
| 5499 | case XPATH_BOOLEAN: |
| 5500 | if (XP_CACHE_WANTS(cache->booleanObjs, cache->maxBoolean)) { |
| 5501 | XP_CACHE_ADD(cache->booleanObjs, obj); |
| 5502 | goto obj_cached; |
| 5503 | } |
| 5504 | break; |
| 5505 | case XPATH_NUMBER: |
| 5506 | if (XP_CACHE_WANTS(cache->numberObjs, cache->maxNumber)) { |
| 5507 | XP_CACHE_ADD(cache->numberObjs, obj); |
| 5508 | goto obj_cached; |
| 5509 | } |
| 5510 | break; |
| 5511 | #ifdef LIBXML_XPTR_ENABLED |
| 5512 | case XPATH_LOCATIONSET: |
| 5513 | if (obj->user != NULL) { |
| 5514 | xmlXPtrFreeLocationSet(obj->user); |
| 5515 | } |
| 5516 | goto free_obj; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5517 | #endif |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5518 | default: |
| 5519 | goto free_obj; |
| 5520 | } |
| 5521 | |
| 5522 | /* |
| 5523 | * Fallback to adding to the misc-objects slot. |
| 5524 | */ |
| 5525 | if (XP_CACHE_WANTS(cache->miscObjs, cache->maxMisc)) { |
| 5526 | XP_CACHE_ADD(cache->miscObjs, obj); |
| 5527 | } else |
| 5528 | goto free_obj; |
| 5529 | |
| 5530 | obj_cached: |
| 5531 | |
| 5532 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5533 | xmlXPathDebugObjUsageReleased(ctxt, obj->type); |
| 5534 | #endif |
| 5535 | |
| 5536 | if (obj->nodesetval != NULL) { |
| 5537 | xmlNodeSetPtr tmpset = obj->nodesetval; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5538 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5539 | /* |
| 5540 | * TODO: Due to those nasty ns-nodes, we need to traverse |
| 5541 | * the list and free the ns-nodes. |
| 5542 | * URGENT TODO: Check if it's actually slowing things down. |
| 5543 | * Maybe we shouldn't try to preserve the list. |
| 5544 | */ |
| 5545 | if (tmpset->nodeNr > 1) { |
| 5546 | int i; |
| 5547 | xmlNodePtr node; |
| 5548 | |
| 5549 | for (i = 0; i < tmpset->nodeNr; i++) { |
| 5550 | node = tmpset->nodeTab[i]; |
| 5551 | if ((node != NULL) && |
| 5552 | (node->type == XML_NAMESPACE_DECL)) |
| 5553 | { |
| 5554 | xmlXPathNodeSetFreeNs((xmlNsPtr) node); |
| 5555 | } |
| 5556 | } |
| 5557 | } else if (tmpset->nodeNr == 1) { |
| 5558 | if ((tmpset->nodeTab[0] != NULL) && |
| 5559 | (tmpset->nodeTab[0]->type == XML_NAMESPACE_DECL)) |
| 5560 | xmlXPathNodeSetFreeNs((xmlNsPtr) tmpset->nodeTab[0]); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5561 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5562 | tmpset->nodeNr = 0; |
| 5563 | memset(obj, 0, sizeof(xmlXPathObject)); |
| 5564 | obj->nodesetval = tmpset; |
| 5565 | } else |
| 5566 | memset(obj, 0, sizeof(xmlXPathObject)); |
| 5567 | |
| 5568 | return; |
| 5569 | |
| 5570 | free_obj: |
| 5571 | /* |
| 5572 | * Cache is full; free the object. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5573 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 5574 | if (obj->nodesetval != NULL) |
| 5575 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 5576 | #ifdef XP_DEBUG_OBJ_USAGE |
| 5577 | xmlXPathDebugObjUsageReleased(NULL, obj->type); |
| 5578 | #endif |
| 5579 | xmlFree(obj); |
| 5580 | } |
| 5581 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5584 | |
| 5585 | /************************************************************************ |
| 5586 | * * |
| 5587 | * Type Casting Routines * |
| 5588 | * * |
| 5589 | ************************************************************************/ |
| 5590 | |
| 5591 | /** |
| 5592 | * xmlXPathCastBooleanToString: |
| 5593 | * @val: a boolean |
| 5594 | * |
| 5595 | * Converts a boolean to its string value. |
| 5596 | * |
| 5597 | * Returns a newly allocated string. |
| 5598 | */ |
| 5599 | xmlChar * |
| 5600 | xmlXPathCastBooleanToString (int val) { |
| 5601 | xmlChar *ret; |
| 5602 | if (val) |
| 5603 | ret = xmlStrdup((const xmlChar *) "true"); |
| 5604 | else |
| 5605 | ret = xmlStrdup((const xmlChar *) "false"); |
| 5606 | return(ret); |
| 5607 | } |
| 5608 | |
| 5609 | /** |
| 5610 | * xmlXPathCastNumberToString: |
| 5611 | * @val: a number |
| 5612 | * |
| 5613 | * Converts a number to its string value. |
| 5614 | * |
| 5615 | * Returns a newly allocated string. |
| 5616 | */ |
| 5617 | xmlChar * |
| 5618 | xmlXPathCastNumberToString (double val) { |
| 5619 | xmlChar *ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 5620 | switch (xmlXPathIsInf(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5621 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5622 | ret = xmlStrdup((const xmlChar *) "Infinity"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5623 | break; |
| 5624 | case -1: |
| 5625 | ret = xmlStrdup((const xmlChar *) "-Infinity"); |
| 5626 | break; |
| 5627 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 5628 | if (xmlXPathIsNaN(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5629 | ret = xmlStrdup((const xmlChar *) "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5630 | } else if (val == 0 && xmlXPathGetSign(val) != 0) { |
| 5631 | ret = xmlStrdup((const xmlChar *) "0"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5632 | } else { |
| 5633 | /* could be improved */ |
| 5634 | char buf[100]; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5635 | xmlXPathFormatNumber(val, buf, 99); |
| 5636 | buf[99] = 0; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5637 | ret = xmlStrdup((const xmlChar *) buf); |
| 5638 | } |
| 5639 | } |
| 5640 | return(ret); |
| 5641 | } |
| 5642 | |
| 5643 | /** |
| 5644 | * xmlXPathCastNodeToString: |
| 5645 | * @node: a node |
| 5646 | * |
| 5647 | * Converts a node to its string value. |
| 5648 | * |
| 5649 | * Returns a newly allocated string. |
| 5650 | */ |
| 5651 | xmlChar * |
| 5652 | xmlXPathCastNodeToString (xmlNodePtr node) { |
William M. Brack | d611c88 | 2007-05-31 05:07:17 +0000 | [diff] [blame] | 5653 | xmlChar *ret; |
| 5654 | if ((ret = xmlNodeGetContent(node)) == NULL) |
| 5655 | ret = xmlStrdup((const xmlChar *) ""); |
| 5656 | return(ret); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | /** |
| 5660 | * xmlXPathCastNodeSetToString: |
| 5661 | * @ns: a node-set |
| 5662 | * |
| 5663 | * Converts a node-set to its string value. |
| 5664 | * |
| 5665 | * Returns a newly allocated string. |
| 5666 | */ |
| 5667 | xmlChar * |
| 5668 | xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) { |
| 5669 | if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL)) |
| 5670 | return(xmlStrdup((const xmlChar *) "")); |
| 5671 | |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 5672 | if (ns->nodeNr > 1) |
| 5673 | xmlXPathNodeSetSort(ns); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5674 | return(xmlXPathCastNodeToString(ns->nodeTab[0])); |
| 5675 | } |
| 5676 | |
| 5677 | /** |
| 5678 | * xmlXPathCastToString: |
| 5679 | * @val: an XPath object |
| 5680 | * |
| 5681 | * Converts an existing object to its string() equivalent |
| 5682 | * |
Kasimier T. Buchcik | 50128ad | 2006-08-15 13:04:07 +0000 | [diff] [blame] | 5683 | * Returns the allocated string value of the object, NULL in case of error. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 5684 | * It's up to the caller to free the string memory with xmlFree(). |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5685 | */ |
| 5686 | xmlChar * |
| 5687 | xmlXPathCastToString(xmlXPathObjectPtr val) { |
| 5688 | xmlChar *ret = NULL; |
| 5689 | |
| 5690 | if (val == NULL) |
| 5691 | return(xmlStrdup((const xmlChar *) "")); |
| 5692 | switch (val->type) { |
| 5693 | case XPATH_UNDEFINED: |
| 5694 | #ifdef DEBUG_EXPR |
| 5695 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
| 5696 | #endif |
| 5697 | ret = xmlStrdup((const xmlChar *) ""); |
| 5698 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5699 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5700 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5701 | ret = xmlXPathCastNodeSetToString(val->nodesetval); |
| 5702 | break; |
| 5703 | case XPATH_STRING: |
Daniel Veillard | 4e2df54 | 2002-03-22 12:23:14 +0000 | [diff] [blame] | 5704 | return(xmlStrdup(val->stringval)); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5705 | case XPATH_BOOLEAN: |
| 5706 | ret = xmlXPathCastBooleanToString(val->boolval); |
| 5707 | break; |
| 5708 | case XPATH_NUMBER: { |
| 5709 | ret = xmlXPathCastNumberToString(val->floatval); |
| 5710 | break; |
| 5711 | } |
| 5712 | case XPATH_USERS: |
| 5713 | case XPATH_POINT: |
| 5714 | case XPATH_RANGE: |
| 5715 | case XPATH_LOCATIONSET: |
| 5716 | TODO |
| 5717 | ret = xmlStrdup((const xmlChar *) ""); |
| 5718 | break; |
| 5719 | } |
| 5720 | return(ret); |
| 5721 | } |
| 5722 | |
| 5723 | /** |
| 5724 | * xmlXPathConvertString: |
| 5725 | * @val: an XPath object |
| 5726 | * |
| 5727 | * Converts an existing object to its string() equivalent |
| 5728 | * |
| 5729 | * Returns the new object, the old one is freed (or the operation |
| 5730 | * is done directly on @val) |
| 5731 | */ |
| 5732 | xmlXPathObjectPtr |
| 5733 | xmlXPathConvertString(xmlXPathObjectPtr val) { |
| 5734 | xmlChar *res = NULL; |
| 5735 | |
| 5736 | if (val == NULL) |
| 5737 | return(xmlXPathNewCString("")); |
| 5738 | |
| 5739 | switch (val->type) { |
| 5740 | case XPATH_UNDEFINED: |
| 5741 | #ifdef DEBUG_EXPR |
| 5742 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 5743 | #endif |
| 5744 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5745 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5746 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5747 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 5748 | break; |
| 5749 | case XPATH_STRING: |
| 5750 | return(val); |
| 5751 | case XPATH_BOOLEAN: |
| 5752 | res = xmlXPathCastBooleanToString(val->boolval); |
| 5753 | break; |
| 5754 | case XPATH_NUMBER: |
| 5755 | res = xmlXPathCastNumberToString(val->floatval); |
| 5756 | break; |
| 5757 | case XPATH_USERS: |
| 5758 | case XPATH_POINT: |
| 5759 | case XPATH_RANGE: |
| 5760 | case XPATH_LOCATIONSET: |
| 5761 | TODO; |
| 5762 | break; |
| 5763 | } |
| 5764 | xmlXPathFreeObject(val); |
| 5765 | if (res == NULL) |
| 5766 | return(xmlXPathNewCString("")); |
| 5767 | return(xmlXPathWrapString(res)); |
| 5768 | } |
| 5769 | |
| 5770 | /** |
| 5771 | * xmlXPathCastBooleanToNumber: |
| 5772 | * @val: a boolean |
| 5773 | * |
| 5774 | * Converts a boolean to its number value |
| 5775 | * |
| 5776 | * Returns the number value |
| 5777 | */ |
| 5778 | double |
| 5779 | xmlXPathCastBooleanToNumber(int val) { |
| 5780 | if (val) |
| 5781 | return(1.0); |
| 5782 | return(0.0); |
| 5783 | } |
| 5784 | |
| 5785 | /** |
| 5786 | * xmlXPathCastStringToNumber: |
| 5787 | * @val: a string |
| 5788 | * |
| 5789 | * Converts a string to its number value |
| 5790 | * |
| 5791 | * Returns the number value |
| 5792 | */ |
| 5793 | double |
| 5794 | xmlXPathCastStringToNumber(const xmlChar * val) { |
| 5795 | return(xmlXPathStringEvalNumber(val)); |
| 5796 | } |
| 5797 | |
| 5798 | /** |
| 5799 | * xmlXPathCastNodeToNumber: |
| 5800 | * @node: a node |
| 5801 | * |
| 5802 | * Converts a node to its number value |
| 5803 | * |
| 5804 | * Returns the number value |
| 5805 | */ |
| 5806 | double |
| 5807 | xmlXPathCastNodeToNumber (xmlNodePtr node) { |
| 5808 | xmlChar *strval; |
| 5809 | double ret; |
| 5810 | |
| 5811 | if (node == NULL) |
| 5812 | return(xmlXPathNAN); |
| 5813 | strval = xmlXPathCastNodeToString(node); |
| 5814 | if (strval == NULL) |
| 5815 | return(xmlXPathNAN); |
| 5816 | ret = xmlXPathCastStringToNumber(strval); |
| 5817 | xmlFree(strval); |
| 5818 | |
| 5819 | return(ret); |
| 5820 | } |
| 5821 | |
| 5822 | /** |
| 5823 | * xmlXPathCastNodeSetToNumber: |
| 5824 | * @ns: a node-set |
| 5825 | * |
| 5826 | * Converts a node-set to its number value |
| 5827 | * |
| 5828 | * Returns the number value |
| 5829 | */ |
| 5830 | double |
| 5831 | xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) { |
| 5832 | xmlChar *str; |
| 5833 | double ret; |
| 5834 | |
| 5835 | if (ns == NULL) |
| 5836 | return(xmlXPathNAN); |
| 5837 | str = xmlXPathCastNodeSetToString(ns); |
| 5838 | ret = xmlXPathCastStringToNumber(str); |
| 5839 | xmlFree(str); |
| 5840 | return(ret); |
| 5841 | } |
| 5842 | |
| 5843 | /** |
| 5844 | * xmlXPathCastToNumber: |
| 5845 | * @val: an XPath object |
| 5846 | * |
| 5847 | * Converts an XPath object to its number value |
| 5848 | * |
| 5849 | * Returns the number value |
| 5850 | */ |
| 5851 | double |
| 5852 | xmlXPathCastToNumber(xmlXPathObjectPtr val) { |
| 5853 | double ret = 0.0; |
| 5854 | |
| 5855 | if (val == NULL) |
| 5856 | return(xmlXPathNAN); |
| 5857 | switch (val->type) { |
| 5858 | case XPATH_UNDEFINED: |
| 5859 | #ifdef DEGUB_EXPR |
| 5860 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
| 5861 | #endif |
| 5862 | ret = xmlXPathNAN; |
| 5863 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5864 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5865 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5866 | ret = xmlXPathCastNodeSetToNumber(val->nodesetval); |
| 5867 | break; |
| 5868 | case XPATH_STRING: |
| 5869 | ret = xmlXPathCastStringToNumber(val->stringval); |
| 5870 | break; |
| 5871 | case XPATH_NUMBER: |
| 5872 | ret = val->floatval; |
| 5873 | break; |
| 5874 | case XPATH_BOOLEAN: |
| 5875 | ret = xmlXPathCastBooleanToNumber(val->boolval); |
| 5876 | break; |
| 5877 | case XPATH_USERS: |
| 5878 | case XPATH_POINT: |
| 5879 | case XPATH_RANGE: |
| 5880 | case XPATH_LOCATIONSET: |
| 5881 | TODO; |
| 5882 | ret = xmlXPathNAN; |
| 5883 | break; |
| 5884 | } |
| 5885 | return(ret); |
| 5886 | } |
| 5887 | |
| 5888 | /** |
| 5889 | * xmlXPathConvertNumber: |
| 5890 | * @val: an XPath object |
| 5891 | * |
| 5892 | * Converts an existing object to its number() equivalent |
| 5893 | * |
| 5894 | * Returns the new object, the old one is freed (or the operation |
| 5895 | * is done directly on @val) |
| 5896 | */ |
| 5897 | xmlXPathObjectPtr |
| 5898 | xmlXPathConvertNumber(xmlXPathObjectPtr val) { |
| 5899 | xmlXPathObjectPtr ret; |
| 5900 | |
| 5901 | if (val == NULL) |
| 5902 | return(xmlXPathNewFloat(0.0)); |
| 5903 | if (val->type == XPATH_NUMBER) |
| 5904 | return(val); |
| 5905 | ret = xmlXPathNewFloat(xmlXPathCastToNumber(val)); |
| 5906 | xmlXPathFreeObject(val); |
| 5907 | return(ret); |
| 5908 | } |
| 5909 | |
| 5910 | /** |
| 5911 | * xmlXPathCastNumberToBoolean: |
| 5912 | * @val: a number |
| 5913 | * |
| 5914 | * Converts a number to its boolean value |
| 5915 | * |
| 5916 | * Returns the boolean value |
| 5917 | */ |
| 5918 | int |
| 5919 | xmlXPathCastNumberToBoolean (double val) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 5920 | if (xmlXPathIsNaN(val) || (val == 0.0)) |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5921 | return(0); |
| 5922 | return(1); |
| 5923 | } |
| 5924 | |
| 5925 | /** |
| 5926 | * xmlXPathCastStringToBoolean: |
| 5927 | * @val: a string |
| 5928 | * |
| 5929 | * Converts a string to its boolean value |
| 5930 | * |
| 5931 | * Returns the boolean value |
| 5932 | */ |
| 5933 | int |
| 5934 | xmlXPathCastStringToBoolean (const xmlChar *val) { |
| 5935 | if ((val == NULL) || (xmlStrlen(val) == 0)) |
| 5936 | return(0); |
| 5937 | return(1); |
| 5938 | } |
| 5939 | |
| 5940 | /** |
| 5941 | * xmlXPathCastNodeSetToBoolean: |
| 5942 | * @ns: a node-set |
| 5943 | * |
| 5944 | * Converts a node-set to its boolean value |
| 5945 | * |
| 5946 | * Returns the boolean value |
| 5947 | */ |
| 5948 | int |
| 5949 | xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) { |
| 5950 | if ((ns == NULL) || (ns->nodeNr == 0)) |
| 5951 | return(0); |
| 5952 | return(1); |
| 5953 | } |
| 5954 | |
| 5955 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 5956 | * xmlXPathCastToBoolean: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5957 | * @val: an XPath object |
| 5958 | * |
| 5959 | * Converts an XPath object to its boolean value |
| 5960 | * |
| 5961 | * Returns the boolean value |
| 5962 | */ |
| 5963 | int |
| 5964 | xmlXPathCastToBoolean (xmlXPathObjectPtr val) { |
| 5965 | int ret = 0; |
| 5966 | |
| 5967 | if (val == NULL) |
| 5968 | return(0); |
| 5969 | switch (val->type) { |
| 5970 | case XPATH_UNDEFINED: |
| 5971 | #ifdef DEBUG_EXPR |
| 5972 | xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n"); |
| 5973 | #endif |
| 5974 | ret = 0; |
| 5975 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5976 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5977 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5978 | ret = xmlXPathCastNodeSetToBoolean(val->nodesetval); |
| 5979 | break; |
| 5980 | case XPATH_STRING: |
| 5981 | ret = xmlXPathCastStringToBoolean(val->stringval); |
| 5982 | break; |
| 5983 | case XPATH_NUMBER: |
| 5984 | ret = xmlXPathCastNumberToBoolean(val->floatval); |
| 5985 | break; |
| 5986 | case XPATH_BOOLEAN: |
| 5987 | ret = val->boolval; |
| 5988 | break; |
| 5989 | case XPATH_USERS: |
| 5990 | case XPATH_POINT: |
| 5991 | case XPATH_RANGE: |
| 5992 | case XPATH_LOCATIONSET: |
| 5993 | TODO; |
| 5994 | ret = 0; |
| 5995 | break; |
| 5996 | } |
| 5997 | return(ret); |
| 5998 | } |
| 5999 | |
| 6000 | |
| 6001 | /** |
| 6002 | * xmlXPathConvertBoolean: |
| 6003 | * @val: an XPath object |
| 6004 | * |
| 6005 | * Converts an existing object to its boolean() equivalent |
| 6006 | * |
| 6007 | * Returns the new object, the old one is freed (or the operation |
| 6008 | * is done directly on @val) |
| 6009 | */ |
| 6010 | xmlXPathObjectPtr |
| 6011 | xmlXPathConvertBoolean(xmlXPathObjectPtr val) { |
| 6012 | xmlXPathObjectPtr ret; |
| 6013 | |
| 6014 | if (val == NULL) |
| 6015 | return(xmlXPathNewBoolean(0)); |
| 6016 | if (val->type == XPATH_BOOLEAN) |
| 6017 | return(val); |
| 6018 | ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val)); |
| 6019 | xmlXPathFreeObject(val); |
| 6020 | return(ret); |
| 6021 | } |
| 6022 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6023 | /************************************************************************ |
| 6024 | * * |
| 6025 | * Routines to handle XPath contexts * |
| 6026 | * * |
| 6027 | ************************************************************************/ |
| 6028 | |
| 6029 | /** |
| 6030 | * xmlXPathNewContext: |
| 6031 | * @doc: the XML document |
| 6032 | * |
| 6033 | * Create a new xmlXPathContext |
| 6034 | * |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 6035 | * Returns the xmlXPathContext just allocated. The caller will need to free it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6036 | */ |
| 6037 | xmlXPathContextPtr |
| 6038 | xmlXPathNewContext(xmlDocPtr doc) { |
| 6039 | xmlXPathContextPtr ret; |
| 6040 | |
| 6041 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
| 6042 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6043 | xmlXPathErrMemory(NULL, "creating context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6044 | return(NULL); |
| 6045 | } |
| 6046 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
| 6047 | ret->doc = doc; |
| 6048 | ret->node = NULL; |
| 6049 | |
| 6050 | ret->varHash = NULL; |
| 6051 | |
| 6052 | ret->nb_types = 0; |
| 6053 | ret->max_types = 0; |
| 6054 | ret->types = NULL; |
| 6055 | |
| 6056 | ret->funcHash = xmlHashCreate(0); |
| 6057 | |
| 6058 | ret->nb_axis = 0; |
| 6059 | ret->max_axis = 0; |
| 6060 | ret->axis = NULL; |
| 6061 | |
| 6062 | ret->nsHash = NULL; |
| 6063 | ret->user = NULL; |
| 6064 | |
| 6065 | ret->contextSize = -1; |
| 6066 | ret->proximityPosition = -1; |
| 6067 | |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6068 | #ifdef XP_DEFAULT_CACHE_ON |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 6069 | if (xmlXPathContextSetCache(ret, 1, -1, 0) == -1) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6070 | xmlXPathFreeContext(ret); |
| 6071 | return(NULL); |
| 6072 | } |
| 6073 | #endif |
| 6074 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6075 | xmlXPathRegisterAllFunctions(ret); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6076 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6077 | return(ret); |
| 6078 | } |
| 6079 | |
| 6080 | /** |
| 6081 | * xmlXPathFreeContext: |
| 6082 | * @ctxt: the context to free |
| 6083 | * |
| 6084 | * Free up an xmlXPathContext |
| 6085 | */ |
| 6086 | void |
| 6087 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 6088 | if (ctxt == NULL) return; |
| 6089 | |
Kasimier T. Buchcik | 5869469 | 2006-05-31 12:37:28 +0000 | [diff] [blame] | 6090 | if (ctxt->cache != NULL) |
| 6091 | xmlXPathFreeCache((xmlXPathContextCachePtr) ctxt->cache); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6092 | xmlXPathRegisteredNsCleanup(ctxt); |
| 6093 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 6094 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Daniel Veillard | 7eca35f | 2004-11-29 13:08:03 +0000 | [diff] [blame] | 6095 | xmlResetError(&ctxt->lastError); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6096 | xmlFree(ctxt); |
| 6097 | } |
| 6098 | |
| 6099 | /************************************************************************ |
| 6100 | * * |
| 6101 | * Routines to handle XPath parser contexts * |
| 6102 | * * |
| 6103 | ************************************************************************/ |
| 6104 | |
| 6105 | #define CHECK_CTXT(ctxt) \ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6106 | if (ctxt == NULL) { \ |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 6107 | __xmlRaiseError(NULL, NULL, NULL, \ |
| 6108 | NULL, NULL, XML_FROM_XPATH, \ |
| 6109 | XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \ |
| 6110 | __FILE__, __LINE__, \ |
| 6111 | NULL, NULL, NULL, 0, 0, \ |
| 6112 | "NULL context pointer\n"); \ |
| 6113 | return(NULL); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6114 | } \ |
| 6115 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 6116 | #define CHECK_CTXT_NEG(ctxt) \ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6117 | if (ctxt == NULL) { \ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 6118 | __xmlRaiseError(NULL, NULL, NULL, \ |
| 6119 | NULL, NULL, XML_FROM_XPATH, \ |
| 6120 | XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \ |
| 6121 | __FILE__, __LINE__, \ |
| 6122 | NULL, NULL, NULL, 0, 0, \ |
| 6123 | "NULL context pointer\n"); \ |
| 6124 | return(-1); \ |
| 6125 | } \ |
| 6126 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6127 | |
| 6128 | #define CHECK_CONTEXT(ctxt) \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 6129 | if ((ctxt == NULL) || (ctxt->doc == NULL) || \ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6130 | (ctxt->doc->children == NULL)) { \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 6131 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \ |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 6132 | return(NULL); \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 6133 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6134 | |
| 6135 | |
| 6136 | /** |
| 6137 | * xmlXPathNewParserContext: |
| 6138 | * @str: the XPath expression |
| 6139 | * @ctxt: the XPath context |
| 6140 | * |
| 6141 | * Create a new xmlXPathParserContext |
| 6142 | * |
| 6143 | * Returns the xmlXPathParserContext just allocated. |
| 6144 | */ |
| 6145 | xmlXPathParserContextPtr |
| 6146 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 6147 | xmlXPathParserContextPtr ret; |
| 6148 | |
| 6149 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 6150 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6151 | xmlXPathErrMemory(ctxt, "creating parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6152 | return(NULL); |
| 6153 | } |
| 6154 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 6155 | ret->cur = ret->base = str; |
| 6156 | ret->context = ctxt; |
| 6157 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6158 | ret->comp = xmlXPathNewCompExpr(); |
| 6159 | if (ret->comp == NULL) { |
| 6160 | xmlFree(ret->valueTab); |
| 6161 | xmlFree(ret); |
| 6162 | return(NULL); |
| 6163 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 6164 | if ((ctxt != NULL) && (ctxt->dict != NULL)) { |
| 6165 | ret->comp->dict = ctxt->dict; |
| 6166 | xmlDictReference(ret->comp->dict); |
| 6167 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6168 | |
| 6169 | return(ret); |
| 6170 | } |
| 6171 | |
| 6172 | /** |
| 6173 | * xmlXPathCompParserContext: |
| 6174 | * @comp: the XPath compiled expression |
| 6175 | * @ctxt: the XPath context |
| 6176 | * |
| 6177 | * Create a new xmlXPathParserContext when processing a compiled expression |
| 6178 | * |
| 6179 | * Returns the xmlXPathParserContext just allocated. |
| 6180 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6181 | static xmlXPathParserContextPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6182 | xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) { |
| 6183 | xmlXPathParserContextPtr ret; |
| 6184 | |
| 6185 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 6186 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6187 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6188 | return(NULL); |
| 6189 | } |
| 6190 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 6191 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6192 | /* Allocate the value stack */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6193 | ret->valueTab = (xmlXPathObjectPtr *) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6194 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6195 | if (ret->valueTab == NULL) { |
| 6196 | xmlFree(ret); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6197 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6198 | return(NULL); |
| 6199 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6200 | ret->valueNr = 0; |
| 6201 | ret->valueMax = 10; |
| 6202 | ret->value = NULL; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 6203 | ret->valueFrame = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6204 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6205 | ret->context = ctxt; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6206 | ret->comp = comp; |
| 6207 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6208 | return(ret); |
| 6209 | } |
| 6210 | |
| 6211 | /** |
| 6212 | * xmlXPathFreeParserContext: |
| 6213 | * @ctxt: the context to free |
| 6214 | * |
| 6215 | * Free up an xmlXPathParserContext |
| 6216 | */ |
| 6217 | void |
| 6218 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 6219 | if (ctxt->valueTab != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6220 | xmlFree(ctxt->valueTab); |
| 6221 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 6222 | if (ctxt->comp != NULL) { |
| 6223 | #ifdef XPATH_STREAMING |
| 6224 | if (ctxt->comp->stream != NULL) { |
| 6225 | xmlFreePatternList(ctxt->comp->stream); |
| 6226 | ctxt->comp->stream = NULL; |
| 6227 | } |
| 6228 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 6229 | xmlXPathFreeCompExpr(ctxt->comp); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 6230 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6231 | xmlFree(ctxt); |
| 6232 | } |
| 6233 | |
| 6234 | /************************************************************************ |
| 6235 | * * |
| 6236 | * The implicit core function library * |
| 6237 | * * |
| 6238 | ************************************************************************/ |
| 6239 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6240 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6241 | * xmlXPathNodeValHash: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6242 | * @node: a node pointer |
| 6243 | * |
| 6244 | * Function computing the beginning of the string value of the node, |
| 6245 | * used to speed up comparisons |
| 6246 | * |
| 6247 | * Returns an int usable as a hash |
| 6248 | */ |
| 6249 | static unsigned int |
| 6250 | xmlXPathNodeValHash(xmlNodePtr node) { |
| 6251 | int len = 2; |
| 6252 | const xmlChar * string = NULL; |
| 6253 | xmlNodePtr tmp = NULL; |
| 6254 | unsigned int ret = 0; |
| 6255 | |
| 6256 | if (node == NULL) |
| 6257 | return(0); |
| 6258 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 6259 | if (node->type == XML_DOCUMENT_NODE) { |
| 6260 | tmp = xmlDocGetRootElement((xmlDocPtr) node); |
| 6261 | if (tmp == NULL) |
| 6262 | node = node->children; |
| 6263 | else |
| 6264 | node = tmp; |
| 6265 | |
| 6266 | if (node == NULL) |
| 6267 | return(0); |
| 6268 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6269 | |
| 6270 | switch (node->type) { |
| 6271 | case XML_COMMENT_NODE: |
| 6272 | case XML_PI_NODE: |
| 6273 | case XML_CDATA_SECTION_NODE: |
| 6274 | case XML_TEXT_NODE: |
| 6275 | string = node->content; |
| 6276 | if (string == NULL) |
| 6277 | return(0); |
| 6278 | if (string[0] == 0) |
| 6279 | return(0); |
| 6280 | return(((unsigned int) string[0]) + |
| 6281 | (((unsigned int) string[1]) << 8)); |
| 6282 | case XML_NAMESPACE_DECL: |
| 6283 | string = ((xmlNsPtr)node)->href; |
| 6284 | if (string == NULL) |
| 6285 | return(0); |
| 6286 | if (string[0] == 0) |
| 6287 | return(0); |
| 6288 | return(((unsigned int) string[0]) + |
| 6289 | (((unsigned int) string[1]) << 8)); |
| 6290 | case XML_ATTRIBUTE_NODE: |
| 6291 | tmp = ((xmlAttrPtr) node)->children; |
| 6292 | break; |
| 6293 | case XML_ELEMENT_NODE: |
| 6294 | tmp = node->children; |
| 6295 | break; |
| 6296 | default: |
| 6297 | return(0); |
| 6298 | } |
| 6299 | while (tmp != NULL) { |
| 6300 | switch (tmp->type) { |
| 6301 | case XML_COMMENT_NODE: |
| 6302 | case XML_PI_NODE: |
| 6303 | case XML_CDATA_SECTION_NODE: |
| 6304 | case XML_TEXT_NODE: |
| 6305 | string = tmp->content; |
| 6306 | break; |
| 6307 | case XML_NAMESPACE_DECL: |
| 6308 | string = ((xmlNsPtr)tmp)->href; |
| 6309 | break; |
| 6310 | default: |
| 6311 | break; |
| 6312 | } |
| 6313 | if ((string != NULL) && (string[0] != 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6314 | if (len == 1) { |
| 6315 | return(ret + (((unsigned int) string[0]) << 8)); |
| 6316 | } |
| 6317 | if (string[1] == 0) { |
| 6318 | len = 1; |
| 6319 | ret = (unsigned int) string[0]; |
| 6320 | } else { |
| 6321 | return(((unsigned int) string[0]) + |
| 6322 | (((unsigned int) string[1]) << 8)); |
| 6323 | } |
| 6324 | } |
| 6325 | /* |
| 6326 | * Skip to next node |
| 6327 | */ |
| 6328 | if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) { |
| 6329 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 6330 | tmp = tmp->children; |
| 6331 | continue; |
| 6332 | } |
| 6333 | } |
| 6334 | if (tmp == node) |
| 6335 | break; |
| 6336 | |
| 6337 | if (tmp->next != NULL) { |
| 6338 | tmp = tmp->next; |
| 6339 | continue; |
| 6340 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6341 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6342 | do { |
| 6343 | tmp = tmp->parent; |
| 6344 | if (tmp == NULL) |
| 6345 | break; |
| 6346 | if (tmp == node) { |
| 6347 | tmp = NULL; |
| 6348 | break; |
| 6349 | } |
| 6350 | if (tmp->next != NULL) { |
| 6351 | tmp = tmp->next; |
| 6352 | break; |
| 6353 | } |
| 6354 | } while (tmp != NULL); |
| 6355 | } |
| 6356 | return(ret); |
| 6357 | } |
| 6358 | |
| 6359 | /** |
| 6360 | * xmlXPathStringHash: |
| 6361 | * @string: a string |
| 6362 | * |
| 6363 | * Function computing the beginning of the string value of the node, |
| 6364 | * used to speed up comparisons |
| 6365 | * |
| 6366 | * Returns an int usable as a hash |
| 6367 | */ |
| 6368 | static unsigned int |
| 6369 | xmlXPathStringHash(const xmlChar * string) { |
| 6370 | if (string == NULL) |
| 6371 | return((unsigned int) 0); |
| 6372 | if (string[0] == 0) |
| 6373 | return(0); |
| 6374 | return(((unsigned int) string[0]) + |
| 6375 | (((unsigned int) string[1]) << 8)); |
| 6376 | } |
| 6377 | |
| 6378 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6379 | * xmlXPathCompareNodeSetFloat: |
| 6380 | * @ctxt: the XPath Parser context |
| 6381 | * @inf: less than (1) or greater than (0) |
| 6382 | * @strict: is the comparison strict |
| 6383 | * @arg: the node set |
| 6384 | * @f: the value |
| 6385 | * |
| 6386 | * Implement the compare operation between a nodeset and a number |
| 6387 | * @ns < @val (1, 1, ... |
| 6388 | * @ns <= @val (1, 0, ... |
| 6389 | * @ns > @val (0, 1, ... |
| 6390 | * @ns >= @val (0, 0, ... |
| 6391 | * |
| 6392 | * If one object to be compared is a node-set and the other is a number, |
| 6393 | * then the comparison will be true if and only if there is a node in the |
| 6394 | * node-set such that the result of performing the comparison on the number |
| 6395 | * to be compared and on the result of converting the string-value of that |
| 6396 | * node to a number using the number function is true. |
| 6397 | * |
| 6398 | * Returns 0 or 1 depending on the results of the test. |
| 6399 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6400 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6401 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 6402 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 6403 | int i, ret = 0; |
| 6404 | xmlNodeSetPtr ns; |
| 6405 | xmlChar *str2; |
| 6406 | |
| 6407 | if ((f == NULL) || (arg == NULL) || |
| 6408 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6409 | xmlXPathReleaseObject(ctxt->context, arg); |
| 6410 | xmlXPathReleaseObject(ctxt->context, f); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6411 | return(0); |
| 6412 | } |
| 6413 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6414 | if (ns != NULL) { |
| 6415 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6416 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6417 | if (str2 != NULL) { |
| 6418 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6419 | xmlXPathCacheNewString(ctxt->context, str2)); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6420 | xmlFree(str2); |
| 6421 | xmlXPathNumberFunction(ctxt, 1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6422 | valuePush(ctxt, xmlXPathCacheObjectCopy(ctxt->context, f)); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6423 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 6424 | if (ret) |
| 6425 | break; |
| 6426 | } |
| 6427 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6428 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6429 | xmlXPathReleaseObject(ctxt->context, arg); |
| 6430 | xmlXPathReleaseObject(ctxt->context, f); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6431 | return(ret); |
| 6432 | } |
| 6433 | |
| 6434 | /** |
| 6435 | * xmlXPathCompareNodeSetString: |
| 6436 | * @ctxt: the XPath Parser context |
| 6437 | * @inf: less than (1) or greater than (0) |
| 6438 | * @strict: is the comparison strict |
| 6439 | * @arg: the node set |
| 6440 | * @s: the value |
| 6441 | * |
| 6442 | * Implement the compare operation between a nodeset and a string |
| 6443 | * @ns < @val (1, 1, ... |
| 6444 | * @ns <= @val (1, 0, ... |
| 6445 | * @ns > @val (0, 1, ... |
| 6446 | * @ns >= @val (0, 0, ... |
| 6447 | * |
| 6448 | * If one object to be compared is a node-set and the other is a string, |
| 6449 | * then the comparison will be true if and only if there is a node in |
| 6450 | * the node-set such that the result of performing the comparison on the |
| 6451 | * string-value of the node and the other string is true. |
| 6452 | * |
| 6453 | * Returns 0 or 1 depending on the results of the test. |
| 6454 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6455 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6456 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 6457 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 6458 | int i, ret = 0; |
| 6459 | xmlNodeSetPtr ns; |
| 6460 | xmlChar *str2; |
| 6461 | |
| 6462 | if ((s == NULL) || (arg == NULL) || |
| 6463 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6464 | xmlXPathReleaseObject(ctxt->context, arg); |
| 6465 | xmlXPathReleaseObject(ctxt->context, s); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6466 | return(0); |
| 6467 | } |
| 6468 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6469 | if (ns != NULL) { |
| 6470 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6471 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6472 | if (str2 != NULL) { |
| 6473 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6474 | xmlXPathCacheNewString(ctxt->context, str2)); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6475 | xmlFree(str2); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6476 | valuePush(ctxt, xmlXPathCacheObjectCopy(ctxt->context, s)); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6477 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 6478 | if (ret) |
| 6479 | break; |
| 6480 | } |
| 6481 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6482 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6483 | xmlXPathReleaseObject(ctxt->context, arg); |
| 6484 | xmlXPathReleaseObject(ctxt->context, s); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6485 | return(ret); |
| 6486 | } |
| 6487 | |
| 6488 | /** |
| 6489 | * xmlXPathCompareNodeSets: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6490 | * @inf: less than (1) or greater than (0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6491 | * @strict: is the comparison strict |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6492 | * @arg1: the first node set object |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6493 | * @arg2: the second node set object |
| 6494 | * |
| 6495 | * Implement the compare operation on nodesets: |
| 6496 | * |
| 6497 | * If both objects to be compared are node-sets, then the comparison |
| 6498 | * will be true if and only if there is a node in the first node-set |
| 6499 | * and a node in the second node-set such that the result of performing |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6500 | * the comparison on the string-values of the two nodes is true. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6501 | * .... |
| 6502 | * When neither object to be compared is a node-set and the operator |
| 6503 | * is <=, <, >= or >, then the objects are compared by converting both |
| 6504 | * objects to numbers and comparing the numbers according to IEEE 754. |
| 6505 | * .... |
| 6506 | * The number function converts its argument to a number as follows: |
| 6507 | * - a string that consists of optional whitespace followed by an |
| 6508 | * optional minus sign followed by a Number followed by whitespace |
| 6509 | * is converted to the IEEE 754 number that is nearest (according |
| 6510 | * to the IEEE 754 round-to-nearest rule) to the mathematical value |
| 6511 | * represented by the string; any other string is converted to NaN |
| 6512 | * |
| 6513 | * Conclusion all nodes need to be converted first to their string value |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6514 | * and then the comparison must be done when possible |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6515 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6516 | static int |
| 6517 | xmlXPathCompareNodeSets(int inf, int strict, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6518 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 6519 | int i, j, init = 0; |
| 6520 | double val1; |
| 6521 | double *values2; |
| 6522 | int ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6523 | xmlNodeSetPtr ns1; |
| 6524 | xmlNodeSetPtr ns2; |
| 6525 | |
| 6526 | if ((arg1 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6527 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) { |
| 6528 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6529 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6530 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6531 | if ((arg2 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6532 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) { |
| 6533 | xmlXPathFreeObject(arg1); |
| 6534 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6535 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6536 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6537 | |
| 6538 | ns1 = arg1->nodesetval; |
| 6539 | ns2 = arg2->nodesetval; |
| 6540 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 6541 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6542 | xmlXPathFreeObject(arg1); |
| 6543 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6544 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6545 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 6546 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6547 | xmlXPathFreeObject(arg1); |
| 6548 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6549 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6550 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6551 | |
| 6552 | values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double)); |
| 6553 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6554 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6555 | xmlXPathFreeObject(arg1); |
| 6556 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6557 | return(0); |
| 6558 | } |
| 6559 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6560 | val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 6561 | if (xmlXPathIsNaN(val1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6562 | continue; |
| 6563 | for (j = 0;j < ns2->nodeNr;j++) { |
| 6564 | if (init == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6565 | values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6566 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 6567 | if (xmlXPathIsNaN(values2[j])) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6568 | continue; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6569 | if (inf && strict) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6570 | ret = (val1 < values2[j]); |
| 6571 | else if (inf && !strict) |
| 6572 | ret = (val1 <= values2[j]); |
| 6573 | else if (!inf && strict) |
| 6574 | ret = (val1 > values2[j]); |
| 6575 | else if (!inf && !strict) |
| 6576 | ret = (val1 >= values2[j]); |
| 6577 | if (ret) |
| 6578 | break; |
| 6579 | } |
| 6580 | if (ret) |
| 6581 | break; |
| 6582 | init = 1; |
| 6583 | } |
| 6584 | xmlFree(values2); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 6585 | xmlXPathFreeObject(arg1); |
| 6586 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6587 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | /** |
| 6591 | * xmlXPathCompareNodeSetValue: |
| 6592 | * @ctxt: the XPath Parser context |
| 6593 | * @inf: less than (1) or greater than (0) |
| 6594 | * @strict: is the comparison strict |
| 6595 | * @arg: the node set |
| 6596 | * @val: the value |
| 6597 | * |
| 6598 | * Implement the compare operation between a nodeset and a value |
| 6599 | * @ns < @val (1, 1, ... |
| 6600 | * @ns <= @val (1, 0, ... |
| 6601 | * @ns > @val (0, 1, ... |
| 6602 | * @ns >= @val (0, 0, ... |
| 6603 | * |
| 6604 | * If one object to be compared is a node-set and the other is a boolean, |
| 6605 | * then the comparison will be true if and only if the result of performing |
| 6606 | * the comparison on the boolean and on the result of converting |
| 6607 | * the node-set to a boolean using the boolean function is true. |
| 6608 | * |
| 6609 | * Returns 0 or 1 depending on the results of the test. |
| 6610 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6611 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6612 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 6613 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
| 6614 | if ((val == NULL) || (arg == NULL) || |
| 6615 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 6616 | return(0); |
| 6617 | |
| 6618 | switch(val->type) { |
| 6619 | case XPATH_NUMBER: |
| 6620 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 6621 | case XPATH_NODESET: |
| 6622 | case XPATH_XSLT_TREE: |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6623 | return(xmlXPathCompareNodeSets(inf, strict, arg, val)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6624 | case XPATH_STRING: |
| 6625 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 6626 | case XPATH_BOOLEAN: |
| 6627 | valuePush(ctxt, arg); |
| 6628 | xmlXPathBooleanFunction(ctxt, 1); |
| 6629 | valuePush(ctxt, val); |
| 6630 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 6631 | default: |
| 6632 | TODO |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6633 | } |
| 6634 | return(0); |
| 6635 | } |
| 6636 | |
| 6637 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6638 | * xmlXPathEqualNodeSetString: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6639 | * @arg: the nodeset object argument |
| 6640 | * @str: the string to compare to. |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6641 | * @neq: flag to show whether for '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6642 | * |
| 6643 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 6644 | * If one object to be compared is a node-set and the other is a string, |
| 6645 | * then the comparison will be true if and only if there is a node in |
| 6646 | * the node-set such that the result of performing the comparison on the |
| 6647 | * string-value of the node and the other string is true. |
| 6648 | * |
| 6649 | * Returns 0 or 1 depending on the results of the test. |
| 6650 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6651 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6652 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6653 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6654 | int i; |
| 6655 | xmlNodeSetPtr ns; |
| 6656 | xmlChar *str2; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6657 | unsigned int hash; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6658 | |
| 6659 | if ((str == NULL) || (arg == NULL) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6660 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 6661 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6662 | ns = arg->nodesetval; |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 6663 | /* |
| 6664 | * A NULL nodeset compared with a string is always false |
| 6665 | * (since there is no node equal, and no node not equal) |
| 6666 | */ |
| 6667 | if ((ns == NULL) || (ns->nodeNr <= 0) ) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6668 | return (0); |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 6669 | hash = xmlXPathStringHash(str); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6670 | for (i = 0; i < ns->nodeNr; i++) { |
| 6671 | if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) { |
| 6672 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 6673 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
| 6674 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6675 | if (neq) |
| 6676 | continue; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6677 | return (1); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 6678 | } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) { |
| 6679 | if (neq) |
| 6680 | continue; |
| 6681 | return (1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6682 | } else if (neq) { |
| 6683 | if (str2 != NULL) |
| 6684 | xmlFree(str2); |
| 6685 | return (1); |
| 6686 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6687 | if (str2 != NULL) |
| 6688 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6689 | } else if (neq) |
| 6690 | return (1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6691 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6692 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
| 6695 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6696 | * xmlXPathEqualNodeSetFloat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6697 | * @arg: the nodeset object argument |
| 6698 | * @f: the float to compare to |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6699 | * @neq: flag to show whether to compare '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6700 | * |
| 6701 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 6702 | * If one object to be compared is a node-set and the other is a number, |
| 6703 | * then the comparison will be true if and only if there is a node in |
| 6704 | * the node-set such that the result of performing the comparison on the |
| 6705 | * number to be compared and on the result of converting the string-value |
| 6706 | * of that node to a number using the number function is true. |
| 6707 | * |
| 6708 | * Returns 0 or 1 depending on the results of the test. |
| 6709 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6710 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6711 | xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt, |
| 6712 | xmlXPathObjectPtr arg, double f, int neq) { |
| 6713 | int i, ret=0; |
| 6714 | xmlNodeSetPtr ns; |
| 6715 | xmlChar *str2; |
| 6716 | xmlXPathObjectPtr val; |
| 6717 | double v; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6718 | |
| 6719 | if ((arg == NULL) || |
| 6720 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 6721 | return(0); |
| 6722 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6723 | ns = arg->nodesetval; |
| 6724 | if (ns != NULL) { |
| 6725 | for (i=0;i<ns->nodeNr;i++) { |
| 6726 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
| 6727 | if (str2 != NULL) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6728 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, str2)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6729 | xmlFree(str2); |
| 6730 | xmlXPathNumberFunction(ctxt, 1); |
| 6731 | val = valuePop(ctxt); |
| 6732 | v = val->floatval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 6733 | xmlXPathReleaseObject(ctxt->context, val); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6734 | if (!xmlXPathIsNaN(v)) { |
| 6735 | if ((!neq) && (v==f)) { |
| 6736 | ret = 1; |
| 6737 | break; |
| 6738 | } else if ((neq) && (v!=f)) { |
| 6739 | ret = 1; |
| 6740 | break; |
| 6741 | } |
William M. Brack | 32f0f71 | 2005-07-14 07:00:33 +0000 | [diff] [blame] | 6742 | } else { /* NaN is unequal to any value */ |
| 6743 | if (neq) |
| 6744 | ret = 1; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6745 | } |
| 6746 | } |
| 6747 | } |
| 6748 | } |
| 6749 | |
| 6750 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
| 6753 | |
| 6754 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6755 | * xmlXPathEqualNodeSets: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6756 | * @arg1: first nodeset object argument |
| 6757 | * @arg2: second nodeset object argument |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6758 | * @neq: flag to show whether to test '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6759 | * |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6760 | * Implement the equal / not equal operation on XPath nodesets: |
| 6761 | * @arg1 == @arg2 or @arg1 != @arg2 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6762 | * If both objects to be compared are node-sets, then the comparison |
| 6763 | * will be true if and only if there is a node in the first node-set and |
| 6764 | * a node in the second node-set such that the result of performing the |
| 6765 | * comparison on the string-values of the two nodes is true. |
| 6766 | * |
| 6767 | * (needless to say, this is a costly operation) |
| 6768 | * |
| 6769 | * Returns 0 or 1 depending on the results of the test. |
| 6770 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6771 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6772 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6773 | int i, j; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6774 | unsigned int *hashs1; |
| 6775 | unsigned int *hashs2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6776 | xmlChar **values1; |
| 6777 | xmlChar **values2; |
| 6778 | int ret = 0; |
| 6779 | xmlNodeSetPtr ns1; |
| 6780 | xmlNodeSetPtr ns2; |
| 6781 | |
| 6782 | if ((arg1 == NULL) || |
| 6783 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
| 6784 | return(0); |
| 6785 | if ((arg2 == NULL) || |
| 6786 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
| 6787 | return(0); |
| 6788 | |
| 6789 | ns1 = arg1->nodesetval; |
| 6790 | ns2 = arg2->nodesetval; |
| 6791 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6792 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6793 | return(0); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6794 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6795 | return(0); |
| 6796 | |
| 6797 | /* |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6798 | * for equal, check if there is a node pertaining to both sets |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6799 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6800 | if (neq == 0) |
| 6801 | for (i = 0;i < ns1->nodeNr;i++) |
| 6802 | for (j = 0;j < ns2->nodeNr;j++) |
| 6803 | if (ns1->nodeTab[i] == ns2->nodeTab[j]) |
| 6804 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6805 | |
| 6806 | values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *)); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6807 | if (values1 == NULL) { |
| 6808 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6809 | return(0); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6810 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6811 | hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int)); |
| 6812 | if (hashs1 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6813 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6814 | xmlFree(values1); |
| 6815 | return(0); |
| 6816 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6817 | memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *)); |
| 6818 | values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *)); |
| 6819 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6820 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6821 | xmlFree(hashs1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6822 | xmlFree(values1); |
| 6823 | return(0); |
| 6824 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6825 | hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int)); |
| 6826 | if (hashs2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 6827 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6828 | xmlFree(hashs1); |
| 6829 | xmlFree(values1); |
| 6830 | xmlFree(values2); |
| 6831 | return(0); |
| 6832 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6833 | memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *)); |
| 6834 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6835 | hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6836 | for (j = 0;j < ns2->nodeNr;j++) { |
| 6837 | if (i == 0) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6838 | hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6839 | if (hashs1[i] != hashs2[j]) { |
| 6840 | if (neq) { |
| 6841 | ret = 1; |
| 6842 | break; |
| 6843 | } |
| 6844 | } |
| 6845 | else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6846 | if (values1[i] == NULL) |
| 6847 | values1[i] = xmlNodeGetContent(ns1->nodeTab[i]); |
| 6848 | if (values2[j] == NULL) |
| 6849 | values2[j] = xmlNodeGetContent(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6850 | ret = xmlStrEqual(values1[i], values2[j]) ^ neq; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6851 | if (ret) |
| 6852 | break; |
| 6853 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6854 | } |
| 6855 | if (ret) |
| 6856 | break; |
| 6857 | } |
| 6858 | for (i = 0;i < ns1->nodeNr;i++) |
| 6859 | if (values1[i] != NULL) |
| 6860 | xmlFree(values1[i]); |
| 6861 | for (j = 0;j < ns2->nodeNr;j++) |
| 6862 | if (values2[j] != NULL) |
| 6863 | xmlFree(values2[j]); |
| 6864 | xmlFree(values1); |
| 6865 | xmlFree(values2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 6866 | xmlFree(hashs1); |
| 6867 | xmlFree(hashs2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6868 | return(ret); |
| 6869 | } |
| 6870 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6871 | static int |
| 6872 | xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt, |
| 6873 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6874 | int ret = 0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6875 | /* |
| 6876 | *At this point we are assured neither arg1 nor arg2 |
| 6877 | *is a nodeset, so we can just pick the appropriate routine. |
| 6878 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6879 | switch (arg1->type) { |
| 6880 | case XPATH_UNDEFINED: |
| 6881 | #ifdef DEBUG_EXPR |
| 6882 | xmlGenericError(xmlGenericErrorContext, |
| 6883 | "Equal: undefined\n"); |
| 6884 | #endif |
| 6885 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6886 | case XPATH_BOOLEAN: |
| 6887 | switch (arg2->type) { |
| 6888 | case XPATH_UNDEFINED: |
| 6889 | #ifdef DEBUG_EXPR |
| 6890 | xmlGenericError(xmlGenericErrorContext, |
| 6891 | "Equal: undefined\n"); |
| 6892 | #endif |
| 6893 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6894 | case XPATH_BOOLEAN: |
| 6895 | #ifdef DEBUG_EXPR |
| 6896 | xmlGenericError(xmlGenericErrorContext, |
| 6897 | "Equal: %d boolean %d \n", |
| 6898 | arg1->boolval, arg2->boolval); |
| 6899 | #endif |
| 6900 | ret = (arg1->boolval == arg2->boolval); |
| 6901 | break; |
| 6902 | case XPATH_NUMBER: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 6903 | ret = (arg1->boolval == |
| 6904 | xmlXPathCastNumberToBoolean(arg2->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6905 | break; |
| 6906 | case XPATH_STRING: |
| 6907 | if ((arg2->stringval == NULL) || |
| 6908 | (arg2->stringval[0] == 0)) ret = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6909 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6910 | ret = 1; |
| 6911 | ret = (arg1->boolval == ret); |
| 6912 | break; |
| 6913 | case XPATH_USERS: |
| 6914 | case XPATH_POINT: |
| 6915 | case XPATH_RANGE: |
| 6916 | case XPATH_LOCATIONSET: |
| 6917 | TODO |
| 6918 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6919 | case XPATH_NODESET: |
| 6920 | case XPATH_XSLT_TREE: |
| 6921 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6922 | } |
| 6923 | break; |
| 6924 | case XPATH_NUMBER: |
| 6925 | switch (arg2->type) { |
| 6926 | case XPATH_UNDEFINED: |
| 6927 | #ifdef DEBUG_EXPR |
| 6928 | xmlGenericError(xmlGenericErrorContext, |
| 6929 | "Equal: undefined\n"); |
| 6930 | #endif |
| 6931 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6932 | case XPATH_BOOLEAN: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 6933 | ret = (arg2->boolval== |
| 6934 | xmlXPathCastNumberToBoolean(arg1->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6935 | break; |
| 6936 | case XPATH_STRING: |
| 6937 | valuePush(ctxt, arg2); |
| 6938 | xmlXPathNumberFunction(ctxt, 1); |
| 6939 | arg2 = valuePop(ctxt); |
| 6940 | /* no break on purpose */ |
| 6941 | case XPATH_NUMBER: |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 6942 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6943 | if (xmlXPathIsNaN(arg1->floatval) || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6944 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 6945 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 6946 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 6947 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 6948 | ret = 1; |
| 6949 | else |
| 6950 | ret = 0; |
| 6951 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 6952 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 6953 | ret = 1; |
| 6954 | else |
| 6955 | ret = 0; |
| 6956 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 6957 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 6958 | ret = 1; |
| 6959 | else |
| 6960 | ret = 0; |
| 6961 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 6962 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 6963 | ret = 1; |
| 6964 | else |
| 6965 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 6966 | } else { |
| 6967 | ret = (arg1->floatval == arg2->floatval); |
| 6968 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6969 | break; |
| 6970 | case XPATH_USERS: |
| 6971 | case XPATH_POINT: |
| 6972 | case XPATH_RANGE: |
| 6973 | case XPATH_LOCATIONSET: |
| 6974 | TODO |
| 6975 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6976 | case XPATH_NODESET: |
| 6977 | case XPATH_XSLT_TREE: |
| 6978 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6979 | } |
| 6980 | break; |
| 6981 | case XPATH_STRING: |
| 6982 | switch (arg2->type) { |
| 6983 | case XPATH_UNDEFINED: |
| 6984 | #ifdef DEBUG_EXPR |
| 6985 | xmlGenericError(xmlGenericErrorContext, |
| 6986 | "Equal: undefined\n"); |
| 6987 | #endif |
| 6988 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6989 | case XPATH_BOOLEAN: |
| 6990 | if ((arg1->stringval == NULL) || |
| 6991 | (arg1->stringval[0] == 0)) ret = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 6992 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6993 | ret = 1; |
| 6994 | ret = (arg2->boolval == ret); |
| 6995 | break; |
| 6996 | case XPATH_STRING: |
| 6997 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
| 6998 | break; |
| 6999 | case XPATH_NUMBER: |
| 7000 | valuePush(ctxt, arg1); |
| 7001 | xmlXPathNumberFunction(ctxt, 1); |
| 7002 | arg1 = valuePop(ctxt); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7003 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7004 | if (xmlXPathIsNaN(arg1->floatval) || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7005 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7006 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7007 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 7008 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 7009 | ret = 1; |
| 7010 | else |
| 7011 | ret = 0; |
| 7012 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 7013 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 7014 | ret = 1; |
| 7015 | else |
| 7016 | ret = 0; |
| 7017 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 7018 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 7019 | ret = 1; |
| 7020 | else |
| 7021 | ret = 0; |
| 7022 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 7023 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 7024 | ret = 1; |
| 7025 | else |
| 7026 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7027 | } else { |
| 7028 | ret = (arg1->floatval == arg2->floatval); |
| 7029 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7030 | break; |
| 7031 | case XPATH_USERS: |
| 7032 | case XPATH_POINT: |
| 7033 | case XPATH_RANGE: |
| 7034 | case XPATH_LOCATIONSET: |
| 7035 | TODO |
| 7036 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7037 | case XPATH_NODESET: |
| 7038 | case XPATH_XSLT_TREE: |
| 7039 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7040 | } |
| 7041 | break; |
| 7042 | case XPATH_USERS: |
| 7043 | case XPATH_POINT: |
| 7044 | case XPATH_RANGE: |
| 7045 | case XPATH_LOCATIONSET: |
| 7046 | TODO |
| 7047 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7048 | case XPATH_NODESET: |
| 7049 | case XPATH_XSLT_TREE: |
| 7050 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7051 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7052 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7053 | xmlXPathReleaseObject(ctxt->context, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7054 | return(ret); |
| 7055 | } |
| 7056 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7057 | /** |
| 7058 | * xmlXPathEqualValues: |
| 7059 | * @ctxt: the XPath Parser context |
| 7060 | * |
| 7061 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 7062 | * |
| 7063 | * Returns 0 or 1 depending on the results of the test. |
| 7064 | */ |
| 7065 | int |
| 7066 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 7067 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 7068 | int ret = 0; |
| 7069 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 7070 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7071 | arg2 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7072 | arg1 = valuePop(ctxt); |
| 7073 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 7074 | if (arg1 != NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7075 | xmlXPathReleaseObject(ctxt->context, arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7076 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7077 | xmlXPathReleaseObject(ctxt->context, arg2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7078 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7079 | } |
| 7080 | |
| 7081 | if (arg1 == arg2) { |
| 7082 | #ifdef DEBUG_EXPR |
| 7083 | xmlGenericError(xmlGenericErrorContext, |
| 7084 | "Equal: by pointer\n"); |
| 7085 | #endif |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 7086 | xmlXPathFreeObject(arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7087 | return(1); |
| 7088 | } |
| 7089 | |
| 7090 | /* |
| 7091 | *If either argument is a nodeset, it's a 'special case' |
| 7092 | */ |
| 7093 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 7094 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 7095 | /* |
| 7096 | *Hack it to assure arg1 is the nodeset |
| 7097 | */ |
| 7098 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 7099 | argtmp = arg2; |
| 7100 | arg2 = arg1; |
| 7101 | arg1 = argtmp; |
| 7102 | } |
| 7103 | switch (arg2->type) { |
| 7104 | case XPATH_UNDEFINED: |
| 7105 | #ifdef DEBUG_EXPR |
| 7106 | xmlGenericError(xmlGenericErrorContext, |
| 7107 | "Equal: undefined\n"); |
| 7108 | #endif |
| 7109 | break; |
| 7110 | case XPATH_NODESET: |
| 7111 | case XPATH_XSLT_TREE: |
| 7112 | ret = xmlXPathEqualNodeSets(arg1, arg2, 0); |
| 7113 | break; |
| 7114 | case XPATH_BOOLEAN: |
| 7115 | if ((arg1->nodesetval == NULL) || |
| 7116 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7117 | else |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7118 | ret = 1; |
| 7119 | ret = (ret == arg2->boolval); |
| 7120 | break; |
| 7121 | case XPATH_NUMBER: |
| 7122 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0); |
| 7123 | break; |
| 7124 | case XPATH_STRING: |
| 7125 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0); |
| 7126 | break; |
| 7127 | case XPATH_USERS: |
| 7128 | case XPATH_POINT: |
| 7129 | case XPATH_RANGE: |
| 7130 | case XPATH_LOCATIONSET: |
| 7131 | TODO |
| 7132 | break; |
| 7133 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7134 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7135 | xmlXPathReleaseObject(ctxt->context, arg2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7136 | return(ret); |
| 7137 | } |
| 7138 | |
| 7139 | return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 7140 | } |
| 7141 | |
| 7142 | /** |
| 7143 | * xmlXPathNotEqualValues: |
| 7144 | * @ctxt: the XPath Parser context |
| 7145 | * |
| 7146 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 7147 | * |
| 7148 | * Returns 0 or 1 depending on the results of the test. |
| 7149 | */ |
| 7150 | int |
| 7151 | xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) { |
| 7152 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 7153 | int ret = 0; |
| 7154 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 7155 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7156 | arg2 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7157 | arg1 = valuePop(ctxt); |
| 7158 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 7159 | if (arg1 != NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7160 | xmlXPathReleaseObject(ctxt->context, arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7161 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7162 | xmlXPathReleaseObject(ctxt->context, arg2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7163 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7164 | } |
| 7165 | |
| 7166 | if (arg1 == arg2) { |
| 7167 | #ifdef DEBUG_EXPR |
| 7168 | xmlGenericError(xmlGenericErrorContext, |
| 7169 | "NotEqual: by pointer\n"); |
| 7170 | #endif |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7171 | xmlXPathReleaseObject(ctxt->context, arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7172 | return(0); |
| 7173 | } |
| 7174 | |
| 7175 | /* |
| 7176 | *If either argument is a nodeset, it's a 'special case' |
| 7177 | */ |
| 7178 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 7179 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 7180 | /* |
| 7181 | *Hack it to assure arg1 is the nodeset |
| 7182 | */ |
| 7183 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 7184 | argtmp = arg2; |
| 7185 | arg2 = arg1; |
| 7186 | arg1 = argtmp; |
| 7187 | } |
| 7188 | switch (arg2->type) { |
| 7189 | case XPATH_UNDEFINED: |
| 7190 | #ifdef DEBUG_EXPR |
| 7191 | xmlGenericError(xmlGenericErrorContext, |
| 7192 | "NotEqual: undefined\n"); |
| 7193 | #endif |
| 7194 | break; |
| 7195 | case XPATH_NODESET: |
| 7196 | case XPATH_XSLT_TREE: |
| 7197 | ret = xmlXPathEqualNodeSets(arg1, arg2, 1); |
| 7198 | break; |
| 7199 | case XPATH_BOOLEAN: |
| 7200 | if ((arg1->nodesetval == NULL) || |
| 7201 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7202 | else |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7203 | ret = 1; |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 7204 | ret = (ret != arg2->boolval); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7205 | break; |
| 7206 | case XPATH_NUMBER: |
| 7207 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1); |
| 7208 | break; |
| 7209 | case XPATH_STRING: |
| 7210 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1); |
| 7211 | break; |
| 7212 | case XPATH_USERS: |
| 7213 | case XPATH_POINT: |
| 7214 | case XPATH_RANGE: |
| 7215 | case XPATH_LOCATIONSET: |
| 7216 | TODO |
| 7217 | break; |
| 7218 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7219 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7220 | xmlXPathReleaseObject(ctxt->context, arg2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7221 | return(ret); |
| 7222 | } |
| 7223 | |
| 7224 | return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 7225 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7226 | |
| 7227 | /** |
| 7228 | * xmlXPathCompareValues: |
| 7229 | * @ctxt: the XPath Parser context |
| 7230 | * @inf: less than (1) or greater than (0) |
| 7231 | * @strict: is the comparison strict |
| 7232 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7233 | * Implement the compare operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7234 | * @arg1 < @arg2 (1, 1, ... |
| 7235 | * @arg1 <= @arg2 (1, 0, ... |
| 7236 | * @arg1 > @arg2 (0, 1, ... |
| 7237 | * @arg1 >= @arg2 (0, 0, ... |
| 7238 | * |
| 7239 | * When neither object to be compared is a node-set and the operator is |
| 7240 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 7241 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 7242 | * comparison will be true if and only if the first number is less than the |
| 7243 | * second number. The <= comparison will be true if and only if the first |
| 7244 | * number is less than or equal to the second number. The > comparison |
| 7245 | * will be true if and only if the first number is greater than the second |
| 7246 | * number. The >= comparison will be true if and only if the first number |
| 7247 | * is greater than or equal to the second number. |
| 7248 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7249 | * Returns 1 if the comparison succeeded, 0 if it failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7250 | */ |
| 7251 | int |
| 7252 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7253 | int ret = 0, arg1i = 0, arg2i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7254 | xmlXPathObjectPtr arg1, arg2; |
| 7255 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 7256 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7257 | arg2 = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7258 | arg1 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7259 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 7260 | if (arg1 != NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7261 | xmlXPathReleaseObject(ctxt->context, arg1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7262 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7263 | xmlXPathReleaseObject(ctxt->context, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7264 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7265 | } |
| 7266 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7267 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 7268 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
William M. Brack | d6e347e | 2005-04-15 01:34:41 +0000 | [diff] [blame] | 7269 | /* |
| 7270 | * If either argument is a XPATH_NODESET or XPATH_XSLT_TREE the two arguments |
| 7271 | * are not freed from within this routine; they will be freed from the |
| 7272 | * called routine, e.g. xmlXPathCompareNodeSets or xmlXPathCompareNodeSetValue |
| 7273 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7274 | if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) && |
| 7275 | ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7276 | ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7277 | } else { |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 7278 | if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 7279 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, |
| 7280 | arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7281 | } else { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 7282 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict, |
| 7283 | arg2, arg1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7284 | } |
| 7285 | } |
| 7286 | return(ret); |
| 7287 | } |
| 7288 | |
| 7289 | if (arg1->type != XPATH_NUMBER) { |
| 7290 | valuePush(ctxt, arg1); |
| 7291 | xmlXPathNumberFunction(ctxt, 1); |
| 7292 | arg1 = valuePop(ctxt); |
| 7293 | } |
| 7294 | if (arg1->type != XPATH_NUMBER) { |
| 7295 | xmlXPathFreeObject(arg1); |
| 7296 | xmlXPathFreeObject(arg2); |
| 7297 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7298 | } |
| 7299 | if (arg2->type != XPATH_NUMBER) { |
| 7300 | valuePush(ctxt, arg2); |
| 7301 | xmlXPathNumberFunction(ctxt, 1); |
| 7302 | arg2 = valuePop(ctxt); |
| 7303 | } |
| 7304 | if (arg2->type != XPATH_NUMBER) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7305 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7306 | xmlXPathReleaseObject(ctxt->context, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7307 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 7308 | } |
| 7309 | /* |
| 7310 | * Add tests for infinity and nan |
| 7311 | * => feedback on 3.4 for Inf and NaN |
| 7312 | */ |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7313 | /* Hand check NaN and Infinity comparisons */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7314 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7315 | ret=0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7316 | } else { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 7317 | arg1i=xmlXPathIsInf(arg1->floatval); |
| 7318 | arg2i=xmlXPathIsInf(arg2->floatval); |
| 7319 | if (inf && strict) { |
| 7320 | if ((arg1i == -1 && arg2i != -1) || |
| 7321 | (arg2i == 1 && arg1i != 1)) { |
| 7322 | ret = 1; |
| 7323 | } else if (arg1i == 0 && arg2i == 0) { |
| 7324 | ret = (arg1->floatval < arg2->floatval); |
| 7325 | } else { |
| 7326 | ret = 0; |
| 7327 | } |
| 7328 | } |
| 7329 | else if (inf && !strict) { |
| 7330 | if (arg1i == -1 || arg2i == 1) { |
| 7331 | ret = 1; |
| 7332 | } else if (arg1i == 0 && arg2i == 0) { |
| 7333 | ret = (arg1->floatval <= arg2->floatval); |
| 7334 | } else { |
| 7335 | ret = 0; |
| 7336 | } |
| 7337 | } |
| 7338 | else if (!inf && strict) { |
| 7339 | if ((arg1i == 1 && arg2i != 1) || |
| 7340 | (arg2i == -1 && arg1i != -1)) { |
| 7341 | ret = 1; |
| 7342 | } else if (arg1i == 0 && arg2i == 0) { |
| 7343 | ret = (arg1->floatval > arg2->floatval); |
| 7344 | } else { |
| 7345 | ret = 0; |
| 7346 | } |
| 7347 | } |
| 7348 | else if (!inf && !strict) { |
| 7349 | if (arg1i == 1 || arg2i == -1) { |
| 7350 | ret = 1; |
| 7351 | } else if (arg1i == 0 && arg2i == 0) { |
| 7352 | ret = (arg1->floatval >= arg2->floatval); |
| 7353 | } else { |
| 7354 | ret = 0; |
| 7355 | } |
| 7356 | } |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 7357 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7358 | xmlXPathReleaseObject(ctxt->context, arg1); |
| 7359 | xmlXPathReleaseObject(ctxt->context, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7360 | return(ret); |
| 7361 | } |
| 7362 | |
| 7363 | /** |
| 7364 | * xmlXPathValueFlipSign: |
| 7365 | * @ctxt: the XPath Parser context |
| 7366 | * |
| 7367 | * Implement the unary - operation on an XPath object |
| 7368 | * The numeric operators convert their operands to numbers as if |
| 7369 | * by calling the number function. |
| 7370 | */ |
| 7371 | void |
| 7372 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7373 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7374 | CAST_TO_NUMBER; |
| 7375 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7376 | if (xmlXPathIsNaN(ctxt->value->floatval)) |
| 7377 | ctxt->value->floatval=xmlXPathNAN; |
| 7378 | else if (xmlXPathIsInf(ctxt->value->floatval) == 1) |
| 7379 | ctxt->value->floatval=xmlXPathNINF; |
| 7380 | else if (xmlXPathIsInf(ctxt->value->floatval) == -1) |
| 7381 | ctxt->value->floatval=xmlXPathPINF; |
| 7382 | else if (ctxt->value->floatval == 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7383 | if (xmlXPathGetSign(ctxt->value->floatval) == 0) |
| 7384 | ctxt->value->floatval = xmlXPathNZERO; |
| 7385 | else |
| 7386 | ctxt->value->floatval = 0; |
| 7387 | } |
| 7388 | else |
| 7389 | ctxt->value->floatval = - ctxt->value->floatval; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7390 | } |
| 7391 | |
| 7392 | /** |
| 7393 | * xmlXPathAddValues: |
| 7394 | * @ctxt: the XPath Parser context |
| 7395 | * |
| 7396 | * Implement the add operation on XPath objects: |
| 7397 | * The numeric operators convert their operands to numbers as if |
| 7398 | * by calling the number function. |
| 7399 | */ |
| 7400 | void |
| 7401 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 7402 | xmlXPathObjectPtr arg; |
| 7403 | double val; |
| 7404 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7405 | arg = valuePop(ctxt); |
| 7406 | if (arg == NULL) |
| 7407 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 7408 | val = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7409 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7410 | CAST_TO_NUMBER; |
| 7411 | CHECK_TYPE(XPATH_NUMBER); |
| 7412 | ctxt->value->floatval += val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7413 | } |
| 7414 | |
| 7415 | /** |
| 7416 | * xmlXPathSubValues: |
| 7417 | * @ctxt: the XPath Parser context |
| 7418 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7419 | * Implement the subtraction operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7420 | * The numeric operators convert their operands to numbers as if |
| 7421 | * by calling the number function. |
| 7422 | */ |
| 7423 | void |
| 7424 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 7425 | xmlXPathObjectPtr arg; |
| 7426 | double val; |
| 7427 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7428 | arg = valuePop(ctxt); |
| 7429 | if (arg == NULL) |
| 7430 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 7431 | val = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7432 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7433 | CAST_TO_NUMBER; |
| 7434 | CHECK_TYPE(XPATH_NUMBER); |
| 7435 | ctxt->value->floatval -= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7436 | } |
| 7437 | |
| 7438 | /** |
| 7439 | * xmlXPathMultValues: |
| 7440 | * @ctxt: the XPath Parser context |
| 7441 | * |
| 7442 | * Implement the multiply operation on XPath objects: |
| 7443 | * The numeric operators convert their operands to numbers as if |
| 7444 | * by calling the number function. |
| 7445 | */ |
| 7446 | void |
| 7447 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 7448 | xmlXPathObjectPtr arg; |
| 7449 | double val; |
| 7450 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7451 | arg = valuePop(ctxt); |
| 7452 | if (arg == NULL) |
| 7453 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 7454 | val = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7455 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7456 | CAST_TO_NUMBER; |
| 7457 | CHECK_TYPE(XPATH_NUMBER); |
| 7458 | ctxt->value->floatval *= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7459 | } |
| 7460 | |
| 7461 | /** |
| 7462 | * xmlXPathDivValues: |
| 7463 | * @ctxt: the XPath Parser context |
| 7464 | * |
| 7465 | * Implement the div operation on XPath objects @arg1 / @arg2: |
| 7466 | * The numeric operators convert their operands to numbers as if |
| 7467 | * by calling the number function. |
| 7468 | */ |
| 7469 | void |
| 7470 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 7471 | xmlXPathObjectPtr arg; |
| 7472 | double val; |
| 7473 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7474 | arg = valuePop(ctxt); |
| 7475 | if (arg == NULL) |
| 7476 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 7477 | val = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7478 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7479 | CAST_TO_NUMBER; |
| 7480 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7481 | if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval)) |
| 7482 | ctxt->value->floatval = xmlXPathNAN; |
| 7483 | else if (val == 0 && xmlXPathGetSign(val) != 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7484 | if (ctxt->value->floatval == 0) |
| 7485 | ctxt->value->floatval = xmlXPathNAN; |
| 7486 | else if (ctxt->value->floatval > 0) |
| 7487 | ctxt->value->floatval = xmlXPathNINF; |
| 7488 | else if (ctxt->value->floatval < 0) |
| 7489 | ctxt->value->floatval = xmlXPathPINF; |
| 7490 | } |
| 7491 | else if (val == 0) { |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 7492 | if (ctxt->value->floatval == 0) |
| 7493 | ctxt->value->floatval = xmlXPathNAN; |
| 7494 | else if (ctxt->value->floatval > 0) |
| 7495 | ctxt->value->floatval = xmlXPathPINF; |
| 7496 | else if (ctxt->value->floatval < 0) |
| 7497 | ctxt->value->floatval = xmlXPathNINF; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7498 | } else |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 7499 | ctxt->value->floatval /= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7500 | } |
| 7501 | |
| 7502 | /** |
| 7503 | * xmlXPathModValues: |
| 7504 | * @ctxt: the XPath Parser context |
| 7505 | * |
| 7506 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
| 7507 | * The numeric operators convert their operands to numbers as if |
| 7508 | * by calling the number function. |
| 7509 | */ |
| 7510 | void |
| 7511 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 7512 | xmlXPathObjectPtr arg; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7513 | double arg1, arg2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7514 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7515 | arg = valuePop(ctxt); |
| 7516 | if (arg == NULL) |
| 7517 | XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7518 | arg2 = xmlXPathCastToNumber(arg); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 7519 | xmlXPathReleaseObject(ctxt->context, arg); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7520 | CAST_TO_NUMBER; |
| 7521 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7522 | arg1 = ctxt->value->floatval; |
Daniel Veillard | 268fd1b | 2001-08-26 18:46:36 +0000 | [diff] [blame] | 7523 | if (arg2 == 0) |
| 7524 | ctxt->value->floatval = xmlXPathNAN; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7525 | else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7526 | ctxt->value->floatval = fmod(arg1, arg2); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7527 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
| 7530 | /************************************************************************ |
| 7531 | * * |
| 7532 | * The traversal functions * |
| 7533 | * * |
| 7534 | ************************************************************************/ |
| 7535 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7536 | /* |
| 7537 | * A traversal function enumerates nodes along an axis. |
| 7538 | * Initially it must be called with NULL, and it indicates |
| 7539 | * termination on the axis by returning NULL. |
| 7540 | */ |
| 7541 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 7542 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 7543 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7544 | /* |
| 7545 | * xmlXPathTraversalFunctionExt: |
| 7546 | * A traversal function enumerates nodes along an axis. |
| 7547 | * Initially it must be called with NULL, and it indicates |
| 7548 | * termination on the axis by returning NULL. |
| 7549 | * The context node of the traversal is specified via @contextNode. |
| 7550 | */ |
| 7551 | typedef xmlNodePtr (*xmlXPathTraversalFunctionExt) |
| 7552 | (xmlNodePtr cur, xmlNodePtr contextNode); |
| 7553 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 7554 | /* |
| 7555 | * xmlXPathNodeSetMergeFunction: |
| 7556 | * Used for merging node sets in xmlXPathCollectAndTest(). |
| 7557 | */ |
| 7558 | typedef xmlNodeSetPtr (*xmlXPathNodeSetMergeFunction) |
| 7559 | (xmlNodeSetPtr, xmlNodeSetPtr, int); |
| 7560 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7561 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7562 | /** |
| 7563 | * xmlXPathNextSelf: |
| 7564 | * @ctxt: the XPath Parser context |
| 7565 | * @cur: the current node in the traversal |
| 7566 | * |
| 7567 | * Traversal function for the "self" direction |
| 7568 | * The self axis contains just the context node itself |
| 7569 | * |
| 7570 | * Returns the next element following that axis |
| 7571 | */ |
| 7572 | xmlNodePtr |
| 7573 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7574 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7575 | if (cur == NULL) |
| 7576 | return(ctxt->context->node); |
| 7577 | return(NULL); |
| 7578 | } |
| 7579 | |
| 7580 | /** |
| 7581 | * xmlXPathNextChild: |
| 7582 | * @ctxt: the XPath Parser context |
| 7583 | * @cur: the current node in the traversal |
| 7584 | * |
| 7585 | * Traversal function for the "child" direction |
| 7586 | * The child axis contains the children of the context node in document order. |
| 7587 | * |
| 7588 | * Returns the next element following that axis |
| 7589 | */ |
| 7590 | xmlNodePtr |
| 7591 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7592 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7593 | if (cur == NULL) { |
| 7594 | if (ctxt->context->node == NULL) return(NULL); |
| 7595 | switch (ctxt->context->node->type) { |
| 7596 | case XML_ELEMENT_NODE: |
| 7597 | case XML_TEXT_NODE: |
| 7598 | case XML_CDATA_SECTION_NODE: |
| 7599 | case XML_ENTITY_REF_NODE: |
| 7600 | case XML_ENTITY_NODE: |
| 7601 | case XML_PI_NODE: |
| 7602 | case XML_COMMENT_NODE: |
| 7603 | case XML_NOTATION_NODE: |
| 7604 | case XML_DTD_NODE: |
| 7605 | return(ctxt->context->node->children); |
| 7606 | case XML_DOCUMENT_NODE: |
| 7607 | case XML_DOCUMENT_TYPE_NODE: |
| 7608 | case XML_DOCUMENT_FRAG_NODE: |
| 7609 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 7610 | #ifdef LIBXML_DOCB_ENABLED |
| 7611 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7612 | #endif |
| 7613 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 7614 | case XML_ELEMENT_DECL: |
| 7615 | case XML_ATTRIBUTE_DECL: |
| 7616 | case XML_ENTITY_DECL: |
| 7617 | case XML_ATTRIBUTE_NODE: |
| 7618 | case XML_NAMESPACE_DECL: |
| 7619 | case XML_XINCLUDE_START: |
| 7620 | case XML_XINCLUDE_END: |
| 7621 | return(NULL); |
| 7622 | } |
| 7623 | return(NULL); |
| 7624 | } |
| 7625 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 7626 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
| 7627 | return(NULL); |
| 7628 | return(cur->next); |
| 7629 | } |
| 7630 | |
| 7631 | /** |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 7632 | * xmlXPathNextChildElement: |
| 7633 | * @ctxt: the XPath Parser context |
| 7634 | * @cur: the current node in the traversal |
| 7635 | * |
| 7636 | * Traversal function for the "child" direction and nodes of type element. |
| 7637 | * The child axis contains the children of the context node in document order. |
| 7638 | * |
| 7639 | * Returns the next element following that axis |
| 7640 | */ |
| 7641 | static xmlNodePtr |
| 7642 | xmlXPathNextChildElement(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 7643 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
| 7644 | if (cur == NULL) { |
| 7645 | cur = ctxt->context->node; |
| 7646 | if (cur == NULL) return(NULL); |
| 7647 | /* |
| 7648 | * Get the first element child. |
| 7649 | */ |
| 7650 | switch (cur->type) { |
| 7651 | case XML_ELEMENT_NODE: |
| 7652 | case XML_DOCUMENT_FRAG_NODE: |
| 7653 | case XML_ENTITY_REF_NODE: /* URGENT TODO: entify-refs as well? */ |
| 7654 | case XML_ENTITY_NODE: |
| 7655 | cur = cur->children; |
| 7656 | if (cur != NULL) { |
| 7657 | if (cur->type == XML_ELEMENT_NODE) |
| 7658 | return(cur); |
| 7659 | do { |
| 7660 | cur = cur->next; |
| 7661 | } while ((cur != NULL) && |
| 7662 | (cur->type != XML_ELEMENT_NODE)); |
| 7663 | return(cur); |
| 7664 | } |
| 7665 | return(NULL); |
| 7666 | case XML_DOCUMENT_NODE: |
| 7667 | case XML_HTML_DOCUMENT_NODE: |
| 7668 | #ifdef LIBXML_DOCB_ENABLED |
| 7669 | case XML_DOCB_DOCUMENT_NODE: |
| 7670 | #endif |
| 7671 | return(xmlDocGetRootElement((xmlDocPtr) cur)); |
| 7672 | default: |
| 7673 | return(NULL); |
| 7674 | } |
| 7675 | return(NULL); |
| 7676 | } |
| 7677 | /* |
| 7678 | * Get the next sibling element node. |
| 7679 | */ |
| 7680 | switch (cur->type) { |
| 7681 | case XML_ELEMENT_NODE: |
| 7682 | case XML_TEXT_NODE: |
| 7683 | case XML_ENTITY_REF_NODE: |
| 7684 | case XML_ENTITY_NODE: |
| 7685 | case XML_CDATA_SECTION_NODE: |
| 7686 | case XML_PI_NODE: |
| 7687 | case XML_COMMENT_NODE: |
| 7688 | case XML_XINCLUDE_END: |
| 7689 | break; |
| 7690 | /* case XML_DTD_NODE: */ /* URGENT TODO: DTD-node as well? */ |
| 7691 | default: |
| 7692 | return(NULL); |
| 7693 | } |
| 7694 | if (cur->next != NULL) { |
| 7695 | if (cur->next->type == XML_ELEMENT_NODE) |
| 7696 | return(cur->next); |
| 7697 | cur = cur->next; |
| 7698 | do { |
| 7699 | cur = cur->next; |
| 7700 | } while ((cur != NULL) && (cur->type != XML_ELEMENT_NODE)); |
| 7701 | return(cur); |
| 7702 | } |
| 7703 | return(NULL); |
| 7704 | } |
| 7705 | |
| 7706 | /** |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7707 | * xmlXPathNextDescendantOrSelfElemParent: |
| 7708 | * @ctxt: the XPath Parser context |
| 7709 | * @cur: the current node in the traversal |
| 7710 | * |
| 7711 | * Traversal function for the "descendant-or-self" axis. |
| 7712 | * Additionally it returns only nodes which can be parents of |
| 7713 | * element nodes. |
| 7714 | * |
| 7715 | * |
| 7716 | * Returns the next element following that axis |
| 7717 | */ |
| 7718 | static xmlNodePtr |
| 7719 | xmlXPathNextDescendantOrSelfElemParent(xmlNodePtr cur, |
| 7720 | xmlNodePtr contextNode) |
| 7721 | { |
| 7722 | if (cur == NULL) { |
| 7723 | if (contextNode == NULL) |
| 7724 | return(NULL); |
| 7725 | switch (contextNode->type) { |
| 7726 | case XML_ELEMENT_NODE: |
| 7727 | case XML_XINCLUDE_START: |
| 7728 | case XML_DOCUMENT_FRAG_NODE: |
| 7729 | case XML_DOCUMENT_NODE: |
| 7730 | #ifdef LIBXML_DOCB_ENABLED |
| 7731 | case XML_DOCB_DOCUMENT_NODE: |
| 7732 | #endif |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7733 | case XML_HTML_DOCUMENT_NODE: |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7734 | return(contextNode); |
| 7735 | default: |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7736 | return(NULL); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7737 | } |
| 7738 | return(NULL); |
| 7739 | } else { |
| 7740 | xmlNodePtr start = cur; |
| 7741 | |
| 7742 | while (cur != NULL) { |
| 7743 | switch (cur->type) { |
| 7744 | case XML_ELEMENT_NODE: |
| 7745 | /* TODO: OK to have XInclude here? */ |
| 7746 | case XML_XINCLUDE_START: |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7747 | case XML_DOCUMENT_FRAG_NODE: |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7748 | if (cur != start) |
| 7749 | return(cur); |
| 7750 | if (cur->children != NULL) { |
| 7751 | cur = cur->children; |
| 7752 | continue; |
| 7753 | } |
| 7754 | break; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7755 | /* Not sure if we need those here. */ |
| 7756 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 17970a7 | 2006-10-26 08:55:47 +0000 | [diff] [blame] | 7757 | #ifdef LIBXML_DOCB_ENABLED |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7758 | case XML_DOCB_DOCUMENT_NODE: |
| 7759 | #endif |
| 7760 | case XML_HTML_DOCUMENT_NODE: |
| 7761 | if (cur != start) |
| 7762 | return(cur); |
| 7763 | return(xmlDocGetRootElement((xmlDocPtr) cur)); |
| 7764 | default: |
| 7765 | break; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7766 | } |
| 7767 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7768 | next_sibling: |
| 7769 | if ((cur == NULL) || (cur == contextNode)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7770 | return(NULL); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7771 | if (cur->next != NULL) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7772 | cur = cur->next; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7773 | } else { |
| 7774 | cur = cur->parent; |
| 7775 | goto next_sibling; |
| 7776 | } |
| 7777 | } |
| 7778 | } |
| 7779 | return(NULL); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7780 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 7781 | |
| 7782 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7783 | * xmlXPathNextDescendant: |
| 7784 | * @ctxt: the XPath Parser context |
| 7785 | * @cur: the current node in the traversal |
| 7786 | * |
| 7787 | * Traversal function for the "descendant" direction |
| 7788 | * the descendant axis contains the descendants of the context node in document |
| 7789 | * order; a descendant is a child or a child of a child and so on. |
| 7790 | * |
| 7791 | * Returns the next element following that axis |
| 7792 | */ |
| 7793 | xmlNodePtr |
| 7794 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7795 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7796 | if (cur == NULL) { |
| 7797 | if (ctxt->context->node == NULL) |
| 7798 | return(NULL); |
| 7799 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 7800 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 7801 | return(NULL); |
| 7802 | |
| 7803 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 7804 | return(ctxt->context->doc->children); |
| 7805 | return(ctxt->context->node->children); |
| 7806 | } |
| 7807 | |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 7808 | if (cur->children != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 7809 | /* |
| 7810 | * Do not descend on entities declarations |
| 7811 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7812 | if (cur->children->type != XML_ENTITY_DECL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 7813 | cur = cur->children; |
| 7814 | /* |
| 7815 | * Skip DTDs |
| 7816 | */ |
| 7817 | if (cur->type != XML_DTD_NODE) |
| 7818 | return(cur); |
| 7819 | } |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 7820 | } |
| 7821 | |
| 7822 | if (cur == ctxt->context->node) return(NULL); |
| 7823 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 7824 | while (cur->next != NULL) { |
| 7825 | cur = cur->next; |
| 7826 | if ((cur->type != XML_ENTITY_DECL) && |
| 7827 | (cur->type != XML_DTD_NODE)) |
| 7828 | return(cur); |
| 7829 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7830 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7831 | do { |
| 7832 | cur = cur->parent; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 7833 | if (cur == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7834 | if (cur == ctxt->context->node) return(NULL); |
| 7835 | if (cur->next != NULL) { |
| 7836 | cur = cur->next; |
| 7837 | return(cur); |
| 7838 | } |
| 7839 | } while (cur != NULL); |
| 7840 | return(cur); |
| 7841 | } |
| 7842 | |
| 7843 | /** |
| 7844 | * xmlXPathNextDescendantOrSelf: |
| 7845 | * @ctxt: the XPath Parser context |
| 7846 | * @cur: the current node in the traversal |
| 7847 | * |
| 7848 | * Traversal function for the "descendant-or-self" direction |
| 7849 | * the descendant-or-self axis contains the context node and the descendants |
| 7850 | * of the context node in document order; thus the context node is the first |
| 7851 | * node on the axis, and the first child of the context node is the second node |
| 7852 | * on the axis |
| 7853 | * |
| 7854 | * Returns the next element following that axis |
| 7855 | */ |
| 7856 | xmlNodePtr |
| 7857 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7858 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7859 | if (cur == NULL) { |
| 7860 | if (ctxt->context->node == NULL) |
| 7861 | return(NULL); |
| 7862 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 7863 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 7864 | return(NULL); |
| 7865 | return(ctxt->context->node); |
| 7866 | } |
| 7867 | |
| 7868 | return(xmlXPathNextDescendant(ctxt, cur)); |
| 7869 | } |
| 7870 | |
| 7871 | /** |
| 7872 | * xmlXPathNextParent: |
| 7873 | * @ctxt: the XPath Parser context |
| 7874 | * @cur: the current node in the traversal |
| 7875 | * |
| 7876 | * Traversal function for the "parent" direction |
| 7877 | * The parent axis contains the parent of the context node, if there is one. |
| 7878 | * |
| 7879 | * Returns the next element following that axis |
| 7880 | */ |
| 7881 | xmlNodePtr |
| 7882 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7883 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7884 | /* |
| 7885 | * the parent of an attribute or namespace node is the element |
| 7886 | * to which the attribute or namespace node is attached |
| 7887 | * Namespace handling !!! |
| 7888 | */ |
| 7889 | if (cur == NULL) { |
| 7890 | if (ctxt->context->node == NULL) return(NULL); |
| 7891 | switch (ctxt->context->node->type) { |
| 7892 | case XML_ELEMENT_NODE: |
| 7893 | case XML_TEXT_NODE: |
| 7894 | case XML_CDATA_SECTION_NODE: |
| 7895 | case XML_ENTITY_REF_NODE: |
| 7896 | case XML_ENTITY_NODE: |
| 7897 | case XML_PI_NODE: |
| 7898 | case XML_COMMENT_NODE: |
| 7899 | case XML_NOTATION_NODE: |
| 7900 | case XML_DTD_NODE: |
| 7901 | case XML_ELEMENT_DECL: |
| 7902 | case XML_ATTRIBUTE_DECL: |
| 7903 | case XML_XINCLUDE_START: |
| 7904 | case XML_XINCLUDE_END: |
| 7905 | case XML_ENTITY_DECL: |
| 7906 | if (ctxt->context->node->parent == NULL) |
| 7907 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 7908 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 7909 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 7910 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 7911 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 7912 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7913 | return(ctxt->context->node->parent); |
| 7914 | case XML_ATTRIBUTE_NODE: { |
| 7915 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 7916 | |
| 7917 | return(att->parent); |
| 7918 | } |
| 7919 | case XML_DOCUMENT_NODE: |
| 7920 | case XML_DOCUMENT_TYPE_NODE: |
| 7921 | case XML_DOCUMENT_FRAG_NODE: |
| 7922 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 7923 | #ifdef LIBXML_DOCB_ENABLED |
| 7924 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7925 | #endif |
| 7926 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 7927 | case XML_NAMESPACE_DECL: { |
| 7928 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 7929 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 7930 | if ((ns->next != NULL) && |
| 7931 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 7932 | return((xmlNodePtr) ns->next); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7933 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 7934 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7935 | } |
| 7936 | } |
| 7937 | return(NULL); |
| 7938 | } |
| 7939 | |
| 7940 | /** |
| 7941 | * xmlXPathNextAncestor: |
| 7942 | * @ctxt: the XPath Parser context |
| 7943 | * @cur: the current node in the traversal |
| 7944 | * |
| 7945 | * Traversal function for the "ancestor" direction |
| 7946 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 7947 | * of the context node consist of the parent of context node and the parent's |
| 7948 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 7949 | * parent is the first node on the axis, and the parent's parent is the second |
| 7950 | * node on the axis |
| 7951 | * |
| 7952 | * Returns the next element following that axis |
| 7953 | */ |
| 7954 | xmlNodePtr |
| 7955 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7956 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7957 | /* |
| 7958 | * the parent of an attribute or namespace node is the element |
| 7959 | * to which the attribute or namespace node is attached |
| 7960 | * !!!!!!!!!!!!! |
| 7961 | */ |
| 7962 | if (cur == NULL) { |
| 7963 | if (ctxt->context->node == NULL) return(NULL); |
| 7964 | switch (ctxt->context->node->type) { |
| 7965 | case XML_ELEMENT_NODE: |
| 7966 | case XML_TEXT_NODE: |
| 7967 | case XML_CDATA_SECTION_NODE: |
| 7968 | case XML_ENTITY_REF_NODE: |
| 7969 | case XML_ENTITY_NODE: |
| 7970 | case XML_PI_NODE: |
| 7971 | case XML_COMMENT_NODE: |
| 7972 | case XML_DTD_NODE: |
| 7973 | case XML_ELEMENT_DECL: |
| 7974 | case XML_ATTRIBUTE_DECL: |
| 7975 | case XML_ENTITY_DECL: |
| 7976 | case XML_NOTATION_NODE: |
| 7977 | case XML_XINCLUDE_START: |
| 7978 | case XML_XINCLUDE_END: |
| 7979 | if (ctxt->context->node->parent == NULL) |
| 7980 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 7981 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 7982 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 7983 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 7984 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 7985 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7986 | return(ctxt->context->node->parent); |
| 7987 | case XML_ATTRIBUTE_NODE: { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7988 | xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7989 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7990 | return(tmp->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7991 | } |
| 7992 | case XML_DOCUMENT_NODE: |
| 7993 | case XML_DOCUMENT_TYPE_NODE: |
| 7994 | case XML_DOCUMENT_FRAG_NODE: |
| 7995 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 7996 | #ifdef LIBXML_DOCB_ENABLED |
| 7997 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7998 | #endif |
| 7999 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8000 | case XML_NAMESPACE_DECL: { |
| 8001 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8002 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8003 | if ((ns->next != NULL) && |
| 8004 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 8005 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8006 | /* Bad, how did that namespace end up here ? */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8007 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8008 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8009 | } |
| 8010 | return(NULL); |
| 8011 | } |
| 8012 | if (cur == ctxt->context->doc->children) |
| 8013 | return((xmlNodePtr) ctxt->context->doc); |
| 8014 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 8015 | return(NULL); |
| 8016 | switch (cur->type) { |
| 8017 | case XML_ELEMENT_NODE: |
| 8018 | case XML_TEXT_NODE: |
| 8019 | case XML_CDATA_SECTION_NODE: |
| 8020 | case XML_ENTITY_REF_NODE: |
| 8021 | case XML_ENTITY_NODE: |
| 8022 | case XML_PI_NODE: |
| 8023 | case XML_COMMENT_NODE: |
| 8024 | case XML_NOTATION_NODE: |
| 8025 | case XML_DTD_NODE: |
| 8026 | case XML_ELEMENT_DECL: |
| 8027 | case XML_ATTRIBUTE_DECL: |
| 8028 | case XML_ENTITY_DECL: |
| 8029 | case XML_XINCLUDE_START: |
| 8030 | case XML_XINCLUDE_END: |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 8031 | if (cur->parent == NULL) |
| 8032 | return(NULL); |
| 8033 | if ((cur->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8034 | ((cur->parent->name[0] == ' ') || |
| 8035 | (xmlStrEqual(cur->parent->name, |
| 8036 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 8037 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8038 | return(cur->parent); |
| 8039 | case XML_ATTRIBUTE_NODE: { |
| 8040 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 8041 | |
| 8042 | return(att->parent); |
| 8043 | } |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 8044 | case XML_NAMESPACE_DECL: { |
| 8045 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8046 | |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 8047 | if ((ns->next != NULL) && |
| 8048 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 8049 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8050 | /* Bad, how did that namespace end up here ? */ |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 8051 | return(NULL); |
| 8052 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8053 | case XML_DOCUMENT_NODE: |
| 8054 | case XML_DOCUMENT_TYPE_NODE: |
| 8055 | case XML_DOCUMENT_FRAG_NODE: |
| 8056 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 8057 | #ifdef LIBXML_DOCB_ENABLED |
| 8058 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8059 | #endif |
| 8060 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8061 | } |
| 8062 | return(NULL); |
| 8063 | } |
| 8064 | |
| 8065 | /** |
| 8066 | * xmlXPathNextAncestorOrSelf: |
| 8067 | * @ctxt: the XPath Parser context |
| 8068 | * @cur: the current node in the traversal |
| 8069 | * |
| 8070 | * Traversal function for the "ancestor-or-self" direction |
| 8071 | * he ancestor-or-self axis contains the context node and ancestors of |
| 8072 | * the context node in reverse document order; thus the context node is |
| 8073 | * the first node on the axis, and the context node's parent the second; |
| 8074 | * parent here is defined the same as with the parent axis. |
| 8075 | * |
| 8076 | * Returns the next element following that axis |
| 8077 | */ |
| 8078 | xmlNodePtr |
| 8079 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8080 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8081 | if (cur == NULL) |
| 8082 | return(ctxt->context->node); |
| 8083 | return(xmlXPathNextAncestor(ctxt, cur)); |
| 8084 | } |
| 8085 | |
| 8086 | /** |
| 8087 | * xmlXPathNextFollowingSibling: |
| 8088 | * @ctxt: the XPath Parser context |
| 8089 | * @cur: the current node in the traversal |
| 8090 | * |
| 8091 | * Traversal function for the "following-sibling" direction |
| 8092 | * The following-sibling axis contains the following siblings of the context |
| 8093 | * node in document order. |
| 8094 | * |
| 8095 | * Returns the next element following that axis |
| 8096 | */ |
| 8097 | xmlNodePtr |
| 8098 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8099 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8100 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 8101 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 8102 | return(NULL); |
| 8103 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 8104 | return(NULL); |
| 8105 | if (cur == NULL) |
| 8106 | return(ctxt->context->node->next); |
| 8107 | return(cur->next); |
| 8108 | } |
| 8109 | |
| 8110 | /** |
| 8111 | * xmlXPathNextPrecedingSibling: |
| 8112 | * @ctxt: the XPath Parser context |
| 8113 | * @cur: the current node in the traversal |
| 8114 | * |
| 8115 | * Traversal function for the "preceding-sibling" direction |
| 8116 | * The preceding-sibling axis contains the preceding siblings of the context |
| 8117 | * node in reverse document order; the first preceding sibling is first on the |
| 8118 | * axis; the sibling preceding that node is the second on the axis and so on. |
| 8119 | * |
| 8120 | * Returns the next element following that axis |
| 8121 | */ |
| 8122 | xmlNodePtr |
| 8123 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8124 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8125 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 8126 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 8127 | return(NULL); |
| 8128 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 8129 | return(NULL); |
| 8130 | if (cur == NULL) |
| 8131 | return(ctxt->context->node->prev); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8132 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) { |
| 8133 | cur = cur->prev; |
| 8134 | if (cur == NULL) |
| 8135 | return(ctxt->context->node->prev); |
| 8136 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8137 | return(cur->prev); |
| 8138 | } |
| 8139 | |
| 8140 | /** |
| 8141 | * xmlXPathNextFollowing: |
| 8142 | * @ctxt: the XPath Parser context |
| 8143 | * @cur: the current node in the traversal |
| 8144 | * |
| 8145 | * Traversal function for the "following" direction |
| 8146 | * The following axis contains all nodes in the same document as the context |
| 8147 | * node that are after the context node in document order, excluding any |
| 8148 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 8149 | * are ordered in document order |
| 8150 | * |
| 8151 | * Returns the next element following that axis |
| 8152 | */ |
| 8153 | xmlNodePtr |
| 8154 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8155 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8156 | if ((cur != NULL) && (cur->type != XML_ATTRIBUTE_NODE) && |
| 8157 | (cur->type != XML_NAMESPACE_DECL) && (cur->children != NULL)) |
| 8158 | return(cur->children); |
| 8159 | |
| 8160 | if (cur == NULL) { |
| 8161 | cur = ctxt->context->node; |
| 8162 | if (cur->type == XML_NAMESPACE_DECL) |
Daniel Veillard | 91d1975 | 2010-10-15 14:30:52 +0200 | [diff] [blame] | 8163 | return(NULL); |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8164 | if (cur->type == XML_ATTRIBUTE_NODE) |
| 8165 | cur = cur->parent; |
Daniel Veillard | 91d1975 | 2010-10-15 14:30:52 +0200 | [diff] [blame] | 8166 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8167 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 8168 | if (cur->next != NULL) return(cur->next) ; |
| 8169 | do { |
| 8170 | cur = cur->parent; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8171 | if (cur == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8172 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
| 8173 | if (cur->next != NULL) return(cur->next); |
| 8174 | } while (cur != NULL); |
| 8175 | return(cur); |
| 8176 | } |
| 8177 | |
| 8178 | /* |
| 8179 | * xmlXPathIsAncestor: |
| 8180 | * @ancestor: the ancestor node |
| 8181 | * @node: the current node |
| 8182 | * |
| 8183 | * Check that @ancestor is a @node's ancestor |
| 8184 | * |
| 8185 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 8186 | */ |
| 8187 | static int |
| 8188 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
| 8189 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 8190 | /* nodes need to be in the same document */ |
| 8191 | if (ancestor->doc != node->doc) return(0); |
| 8192 | /* avoid searching if ancestor or node is the root node */ |
| 8193 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 8194 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 8195 | while (node->parent != NULL) { |
| 8196 | if (node->parent == ancestor) |
| 8197 | return(1); |
| 8198 | node = node->parent; |
| 8199 | } |
| 8200 | return(0); |
| 8201 | } |
| 8202 | |
| 8203 | /** |
| 8204 | * xmlXPathNextPreceding: |
| 8205 | * @ctxt: the XPath Parser context |
| 8206 | * @cur: the current node in the traversal |
| 8207 | * |
| 8208 | * Traversal function for the "preceding" direction |
| 8209 | * the preceding axis contains all nodes in the same document as the context |
| 8210 | * node that are before the context node in document order, excluding any |
| 8211 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 8212 | * ordered in reverse document order |
| 8213 | * |
| 8214 | * Returns the next element following that axis |
| 8215 | */ |
| 8216 | xmlNodePtr |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8217 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) |
| 8218 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8219 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8220 | if (cur == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8221 | cur = ctxt->context->node; |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8222 | if (cur->type == XML_NAMESPACE_DECL) |
| 8223 | return(NULL); |
| 8224 | if (cur->type == XML_ATTRIBUTE_NODE) |
| 8225 | return(cur->parent); |
| 8226 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8227 | if (cur == NULL) |
| 8228 | return (NULL); |
| 8229 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 8230 | cur = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8231 | do { |
| 8232 | if (cur->prev != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8233 | for (cur = cur->prev; cur->last != NULL; cur = cur->last) ; |
| 8234 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8235 | } |
| 8236 | |
| 8237 | cur = cur->parent; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8238 | if (cur == NULL) |
| 8239 | return (NULL); |
| 8240 | if (cur == ctxt->context->doc->children) |
| 8241 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8242 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8243 | return (cur); |
| 8244 | } |
| 8245 | |
| 8246 | /** |
| 8247 | * xmlXPathNextPrecedingInternal: |
| 8248 | * @ctxt: the XPath Parser context |
| 8249 | * @cur: the current node in the traversal |
| 8250 | * |
| 8251 | * Traversal function for the "preceding" direction |
| 8252 | * the preceding axis contains all nodes in the same document as the context |
| 8253 | * node that are before the context node in document order, excluding any |
| 8254 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 8255 | * ordered in reverse document order |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8256 | * This is a faster implementation but internal only since it requires a |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8257 | * state kept in the parser context: ctxt->ancestor. |
| 8258 | * |
| 8259 | * Returns the next element following that axis |
| 8260 | */ |
| 8261 | static xmlNodePtr |
| 8262 | xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt, |
| 8263 | xmlNodePtr cur) |
| 8264 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8265 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8266 | if (cur == NULL) { |
| 8267 | cur = ctxt->context->node; |
| 8268 | if (cur == NULL) |
| 8269 | return (NULL); |
Daniel Veillard | ea90b89 | 2010-10-22 15:50:50 +0200 | [diff] [blame] | 8270 | if (cur->type == XML_NAMESPACE_DECL) |
| 8271 | return (NULL); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 8272 | ctxt->ancestor = cur->parent; |
| 8273 | } |
| 8274 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 8275 | cur = cur->prev; |
| 8276 | while (cur->prev == NULL) { |
| 8277 | cur = cur->parent; |
| 8278 | if (cur == NULL) |
| 8279 | return (NULL); |
| 8280 | if (cur == ctxt->context->doc->children) |
| 8281 | return (NULL); |
| 8282 | if (cur != ctxt->ancestor) |
| 8283 | return (cur); |
| 8284 | ctxt->ancestor = cur->parent; |
| 8285 | } |
| 8286 | cur = cur->prev; |
| 8287 | while (cur->last != NULL) |
| 8288 | cur = cur->last; |
| 8289 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8290 | } |
| 8291 | |
| 8292 | /** |
| 8293 | * xmlXPathNextNamespace: |
| 8294 | * @ctxt: the XPath Parser context |
| 8295 | * @cur: the current attribute in the traversal |
| 8296 | * |
| 8297 | * Traversal function for the "namespace" direction |
| 8298 | * the namespace axis contains the namespace nodes of the context node; |
| 8299 | * the order of nodes on this axis is implementation-defined; the axis will |
| 8300 | * be empty unless the context node is an element |
| 8301 | * |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 8302 | * We keep the XML namespace node at the end of the list. |
| 8303 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8304 | * Returns the next element following that axis |
| 8305 | */ |
| 8306 | xmlNodePtr |
| 8307 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8308 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8309 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8310 | if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 8311 | if (ctxt->context->tmpNsList != NULL) |
| 8312 | xmlFree(ctxt->context->tmpNsList); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8313 | ctxt->context->tmpNsList = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8314 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 8315 | ctxt->context->tmpNsNr = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8316 | if (ctxt->context->tmpNsList != NULL) { |
| 8317 | while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) { |
| 8318 | ctxt->context->tmpNsNr++; |
| 8319 | } |
| 8320 | } |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 8321 | return((xmlNodePtr) xmlXPathXMLNamespace); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 8322 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8323 | if (ctxt->context->tmpNsNr > 0) { |
| 8324 | return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr]; |
| 8325 | } else { |
| 8326 | if (ctxt->context->tmpNsList != NULL) |
| 8327 | xmlFree(ctxt->context->tmpNsList); |
| 8328 | ctxt->context->tmpNsList = NULL; |
| 8329 | return(NULL); |
| 8330 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8331 | } |
| 8332 | |
| 8333 | /** |
| 8334 | * xmlXPathNextAttribute: |
| 8335 | * @ctxt: the XPath Parser context |
| 8336 | * @cur: the current attribute in the traversal |
| 8337 | * |
| 8338 | * Traversal function for the "attribute" direction |
| 8339 | * TODO: support DTD inherited default attributes |
| 8340 | * |
| 8341 | * Returns the next element following that axis |
| 8342 | */ |
| 8343 | xmlNodePtr |
| 8344 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8345 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 8346 | if (ctxt->context->node == NULL) |
| 8347 | return(NULL); |
| 8348 | if (ctxt->context->node->type != XML_ELEMENT_NODE) |
| 8349 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8350 | if (cur == NULL) { |
| 8351 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 8352 | return(NULL); |
| 8353 | return((xmlNodePtr)ctxt->context->node->properties); |
| 8354 | } |
| 8355 | return((xmlNodePtr)cur->next); |
| 8356 | } |
| 8357 | |
| 8358 | /************************************************************************ |
| 8359 | * * |
| 8360 | * NodeTest Functions * |
| 8361 | * * |
| 8362 | ************************************************************************/ |
| 8363 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8364 | #define IS_FUNCTION 200 |
| 8365 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8366 | |
| 8367 | /************************************************************************ |
| 8368 | * * |
| 8369 | * Implicit tree core function library * |
| 8370 | * * |
| 8371 | ************************************************************************/ |
| 8372 | |
| 8373 | /** |
| 8374 | * xmlXPathRoot: |
| 8375 | * @ctxt: the XPath Parser context |
| 8376 | * |
| 8377 | * Initialize the context to the root of the document |
| 8378 | */ |
| 8379 | void |
| 8380 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8381 | if ((ctxt == NULL) || (ctxt->context == NULL)) |
| 8382 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8383 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8384 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8385 | ctxt->context->node)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8386 | } |
| 8387 | |
| 8388 | /************************************************************************ |
| 8389 | * * |
| 8390 | * The explicit core function library * |
| 8391 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 8392 | * * |
| 8393 | ************************************************************************/ |
| 8394 | |
| 8395 | |
| 8396 | /** |
| 8397 | * xmlXPathLastFunction: |
| 8398 | * @ctxt: the XPath Parser context |
| 8399 | * @nargs: the number of arguments |
| 8400 | * |
| 8401 | * Implement the last() XPath function |
| 8402 | * number last() |
| 8403 | * The last function returns the number of nodes in the context node list. |
| 8404 | */ |
| 8405 | void |
| 8406 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8407 | CHECK_ARITY(0); |
| 8408 | if (ctxt->context->contextSize >= 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8409 | valuePush(ctxt, |
| 8410 | xmlXPathCacheNewFloat(ctxt->context, |
| 8411 | (double) ctxt->context->contextSize)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8412 | #ifdef DEBUG_EXPR |
| 8413 | xmlGenericError(xmlGenericErrorContext, |
| 8414 | "last() : %d\n", ctxt->context->contextSize); |
| 8415 | #endif |
| 8416 | } else { |
| 8417 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
| 8418 | } |
| 8419 | } |
| 8420 | |
| 8421 | /** |
| 8422 | * xmlXPathPositionFunction: |
| 8423 | * @ctxt: the XPath Parser context |
| 8424 | * @nargs: the number of arguments |
| 8425 | * |
| 8426 | * Implement the position() XPath function |
| 8427 | * number position() |
| 8428 | * The position function returns the position of the context node in the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8429 | * 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] | 8430 | * will be equal to last(). |
| 8431 | */ |
| 8432 | void |
| 8433 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8434 | CHECK_ARITY(0); |
| 8435 | if (ctxt->context->proximityPosition >= 0) { |
| 8436 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8437 | xmlXPathCacheNewFloat(ctxt->context, |
| 8438 | (double) ctxt->context->proximityPosition)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8439 | #ifdef DEBUG_EXPR |
| 8440 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
| 8441 | ctxt->context->proximityPosition); |
| 8442 | #endif |
| 8443 | } else { |
| 8444 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
| 8445 | } |
| 8446 | } |
| 8447 | |
| 8448 | /** |
| 8449 | * xmlXPathCountFunction: |
| 8450 | * @ctxt: the XPath Parser context |
| 8451 | * @nargs: the number of arguments |
| 8452 | * |
| 8453 | * Implement the count() XPath function |
| 8454 | * number count(node-set) |
| 8455 | */ |
| 8456 | void |
| 8457 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8458 | xmlXPathObjectPtr cur; |
| 8459 | |
| 8460 | CHECK_ARITY(1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8461 | if ((ctxt->value == NULL) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8462 | ((ctxt->value->type != XPATH_NODESET) && |
| 8463 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 8464 | XP_ERROR(XPATH_INVALID_TYPE); |
| 8465 | cur = valuePop(ctxt); |
| 8466 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8467 | if ((cur == NULL) || (cur->nodesetval == NULL)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8468 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, (double) 0)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 8469 | else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8470 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, |
| 8471 | (double) cur->nodesetval->nodeNr)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 8472 | } else { |
| 8473 | if ((cur->nodesetval->nodeNr != 1) || |
| 8474 | (cur->nodesetval->nodeTab == NULL)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8475 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, (double) 0)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 8476 | } else { |
| 8477 | xmlNodePtr tmp; |
| 8478 | int i = 0; |
| 8479 | |
| 8480 | tmp = cur->nodesetval->nodeTab[0]; |
| 8481 | if (tmp != NULL) { |
| 8482 | tmp = tmp->children; |
| 8483 | while (tmp != NULL) { |
| 8484 | tmp = tmp->next; |
| 8485 | i++; |
| 8486 | } |
| 8487 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8488 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, (double) i)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 8489 | } |
| 8490 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8491 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8492 | } |
| 8493 | |
| 8494 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8495 | * xmlXPathGetElementsByIds: |
| 8496 | * @doc: the document |
| 8497 | * @ids: a whitespace separated list of IDs |
| 8498 | * |
| 8499 | * Selects elements by their unique ID. |
| 8500 | * |
| 8501 | * Returns a node-set of selected elements. |
| 8502 | */ |
| 8503 | static xmlNodeSetPtr |
| 8504 | xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) { |
| 8505 | xmlNodeSetPtr ret; |
| 8506 | const xmlChar *cur = ids; |
| 8507 | xmlChar *ID; |
| 8508 | xmlAttrPtr attr; |
| 8509 | xmlNodePtr elem = NULL; |
| 8510 | |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 8511 | if (ids == NULL) return(NULL); |
| 8512 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8513 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 8514 | if (ret == NULL) |
| 8515 | return(ret); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8516 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8517 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8518 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8519 | while ((!IS_BLANK_CH(*cur)) && (*cur != 0)) |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 8520 | cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8521 | |
| 8522 | ID = xmlStrndup(ids, cur - ids); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 8523 | if (ID != NULL) { |
Daniel Veillard | 68cb4b2 | 2004-04-18 20:55:39 +0000 | [diff] [blame] | 8524 | /* |
| 8525 | * We used to check the fact that the value passed |
| 8526 | * was an NCName, but this generated much troubles for |
| 8527 | * me and Aleksey Sanin, people blatantly violated that |
| 8528 | * constaint, like Visa3D spec. |
| 8529 | * if (xmlValidateNCName(ID, 1) == 0) |
| 8530 | */ |
| 8531 | attr = xmlGetID(doc, ID); |
| 8532 | if (attr != NULL) { |
| 8533 | if (attr->type == XML_ATTRIBUTE_NODE) |
| 8534 | elem = attr->parent; |
| 8535 | else if (attr->type == XML_ELEMENT_NODE) |
| 8536 | elem = (xmlNodePtr) attr; |
| 8537 | else |
| 8538 | elem = NULL; |
| 8539 | if (elem != NULL) |
| 8540 | xmlXPathNodeSetAdd(ret, elem); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 8541 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8542 | xmlFree(ID); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 8543 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8544 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8545 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8546 | ids = cur; |
| 8547 | } |
| 8548 | return(ret); |
| 8549 | } |
| 8550 | |
| 8551 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8552 | * xmlXPathIdFunction: |
| 8553 | * @ctxt: the XPath Parser context |
| 8554 | * @nargs: the number of arguments |
| 8555 | * |
| 8556 | * Implement the id() XPath function |
| 8557 | * node-set id(object) |
| 8558 | * The id function selects elements by their unique ID |
| 8559 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 8560 | * then the result is the union of the result of applying id to the |
| 8561 | * string value of each of the nodes in the argument node-set. When the |
| 8562 | * argument to id is of any other type, the argument is converted to a |
| 8563 | * string as if by a call to the string function; the string is split |
| 8564 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 8565 | * of characters matching the production S); the result is a node-set |
| 8566 | * containing the elements in the same document as the context node that |
| 8567 | * have a unique ID equal to any of the tokens in the list. |
| 8568 | */ |
| 8569 | void |
| 8570 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8571 | xmlChar *tokens; |
| 8572 | xmlNodeSetPtr ret; |
| 8573 | xmlXPathObjectPtr obj; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8574 | |
| 8575 | CHECK_ARITY(1); |
| 8576 | obj = valuePop(ctxt); |
| 8577 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 8578 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8579 | xmlNodeSetPtr ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8580 | int i; |
| 8581 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8582 | ret = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 8583 | /* |
| 8584 | * FIXME -- in an out-of-memory condition this will behave badly. |
| 8585 | * The solution is not clear -- we already popped an item from |
| 8586 | * ctxt, so the object is in a corrupt state. |
| 8587 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8588 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8589 | if (obj->nodesetval != NULL) { |
| 8590 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8591 | tokens = |
| 8592 | xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
| 8593 | ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens); |
| 8594 | ret = xmlXPathNodeSetMerge(ret, ns); |
| 8595 | xmlXPathFreeNodeSet(ns); |
| 8596 | if (tokens != NULL) |
| 8597 | xmlFree(tokens); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8598 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8599 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8600 | xmlXPathReleaseObject(ctxt->context, obj); |
| 8601 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(ctxt->context, ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8602 | return; |
| 8603 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8604 | obj = xmlXPathCacheConvertString(ctxt->context, obj); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8605 | ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8606 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(ctxt->context, ret)); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8607 | xmlXPathReleaseObject(ctxt->context, obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8608 | return; |
| 8609 | } |
| 8610 | |
| 8611 | /** |
| 8612 | * xmlXPathLocalNameFunction: |
| 8613 | * @ctxt: the XPath Parser context |
| 8614 | * @nargs: the number of arguments |
| 8615 | * |
| 8616 | * Implement the local-name() XPath function |
| 8617 | * string local-name(node-set?) |
| 8618 | * The local-name function returns a string containing the local part |
| 8619 | * of the name of the node in the argument node-set that is first in |
| 8620 | * document order. If the node-set is empty or the first node has no |
| 8621 | * name, an empty string is returned. If the argument is omitted it |
| 8622 | * defaults to the context node. |
| 8623 | */ |
| 8624 | void |
| 8625 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8626 | xmlXPathObjectPtr cur; |
| 8627 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8628 | if (ctxt == NULL) return; |
| 8629 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8630 | if (nargs == 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8631 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8632 | ctxt->context->node)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8633 | nargs = 1; |
| 8634 | } |
| 8635 | |
| 8636 | CHECK_ARITY(1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8637 | if ((ctxt->value == NULL) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8638 | ((ctxt->value->type != XPATH_NODESET) && |
| 8639 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 8640 | XP_ERROR(XPATH_INVALID_TYPE); |
| 8641 | cur = valuePop(ctxt); |
| 8642 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8643 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8644 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8645 | } else { |
| 8646 | int i = 0; /* Should be first in document order !!!!! */ |
| 8647 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 8648 | case XML_ELEMENT_NODE: |
| 8649 | case XML_ATTRIBUTE_NODE: |
| 8650 | case XML_PI_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8651 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8652 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8653 | else |
| 8654 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8655 | xmlXPathCacheNewString(ctxt->context, |
| 8656 | cur->nodesetval->nodeTab[i]->name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8657 | break; |
| 8658 | case XML_NAMESPACE_DECL: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8659 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8660 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 8661 | break; |
| 8662 | default: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8663 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8664 | } |
| 8665 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8666 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8667 | } |
| 8668 | |
| 8669 | /** |
| 8670 | * xmlXPathNamespaceURIFunction: |
| 8671 | * @ctxt: the XPath Parser context |
| 8672 | * @nargs: the number of arguments |
| 8673 | * |
| 8674 | * Implement the namespace-uri() XPath function |
| 8675 | * string namespace-uri(node-set?) |
| 8676 | * The namespace-uri function returns a string containing the |
| 8677 | * namespace URI of the expanded name of the node in the argument |
| 8678 | * node-set that is first in document order. If the node-set is empty, |
| 8679 | * the first node has no name, or the expanded name has no namespace |
| 8680 | * URI, an empty string is returned. If the argument is omitted it |
| 8681 | * defaults to the context node. |
| 8682 | */ |
| 8683 | void |
| 8684 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8685 | xmlXPathObjectPtr cur; |
| 8686 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8687 | if (ctxt == NULL) return; |
| 8688 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8689 | if (nargs == 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8690 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8691 | ctxt->context->node)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8692 | nargs = 1; |
| 8693 | } |
| 8694 | CHECK_ARITY(1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8695 | if ((ctxt->value == NULL) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8696 | ((ctxt->value->type != XPATH_NODESET) && |
| 8697 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 8698 | XP_ERROR(XPATH_INVALID_TYPE); |
| 8699 | cur = valuePop(ctxt); |
| 8700 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8701 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8702 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8703 | } else { |
| 8704 | int i = 0; /* Should be first in document order !!!!! */ |
| 8705 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 8706 | case XML_ELEMENT_NODE: |
| 8707 | case XML_ATTRIBUTE_NODE: |
| 8708 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8709 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8710 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8711 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8712 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 8713 | break; |
| 8714 | default: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8715 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8716 | } |
| 8717 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8718 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8719 | } |
| 8720 | |
| 8721 | /** |
| 8722 | * xmlXPathNameFunction: |
| 8723 | * @ctxt: the XPath Parser context |
| 8724 | * @nargs: the number of arguments |
| 8725 | * |
| 8726 | * Implement the name() XPath function |
| 8727 | * string name(node-set?) |
| 8728 | * The name function returns a string containing a QName representing |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8729 | * 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] | 8730 | * order. The QName must represent the name with respect to the namespace |
| 8731 | * declarations in effect on the node whose name is being represented. |
| 8732 | * Typically, this will be the form in which the name occurred in the XML |
| 8733 | * source. This need not be the case if there are namespace declarations |
| 8734 | * in effect on the node that associate multiple prefixes with the same |
| 8735 | * namespace. However, an implementation may include information about |
| 8736 | * the original prefix in its representation of nodes; in this case, an |
| 8737 | * implementation can ensure that the returned string is always the same |
| 8738 | * as the QName used in the XML source. If the argument it omitted it |
| 8739 | * defaults to the context node. |
| 8740 | * Libxml keep the original prefix so the "real qualified name" used is |
| 8741 | * returned. |
| 8742 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8743 | static void |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8744 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) |
| 8745 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8746 | xmlXPathObjectPtr cur; |
| 8747 | |
| 8748 | if (nargs == 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8749 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8750 | ctxt->context->node)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8751 | nargs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8752 | } |
| 8753 | |
| 8754 | CHECK_ARITY(1); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8755 | if ((ctxt->value == NULL) || |
| 8756 | ((ctxt->value->type != XPATH_NODESET) && |
| 8757 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 8758 | XP_ERROR(XPATH_INVALID_TYPE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8759 | cur = valuePop(ctxt); |
| 8760 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 8761 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8762 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8763 | } else { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8764 | int i = 0; /* Should be first in document order !!!!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8765 | |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8766 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 8767 | case XML_ELEMENT_NODE: |
| 8768 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8769 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8770 | valuePush(ctxt, |
| 8771 | xmlXPathCacheNewCString(ctxt->context, "")); |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8772 | else if ((cur->nodesetval->nodeTab[i]->ns == NULL) || |
| 8773 | (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8774 | valuePush(ctxt, |
| 8775 | xmlXPathCacheNewString(ctxt->context, |
| 8776 | cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 8777 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 8778 | xmlChar *fullname; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8779 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 8780 | fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name, |
| 8781 | cur->nodesetval->nodeTab[i]->ns->prefix, |
| 8782 | NULL, 0); |
| 8783 | if (fullname == cur->nodesetval->nodeTab[i]->name) |
| 8784 | fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name); |
| 8785 | if (fullname == NULL) { |
| 8786 | XP_ERROR(XPATH_MEMORY_ERROR); |
| 8787 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8788 | valuePush(ctxt, xmlXPathCacheWrapString( |
| 8789 | ctxt->context, fullname)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8790 | } |
| 8791 | break; |
| 8792 | default: |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8793 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 8794 | cur->nodesetval->nodeTab[i])); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 8795 | xmlXPathLocalNameFunction(ctxt, 1); |
| 8796 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8797 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8798 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8799 | } |
| 8800 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8801 | |
| 8802 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8803 | * xmlXPathStringFunction: |
| 8804 | * @ctxt: the XPath Parser context |
| 8805 | * @nargs: the number of arguments |
| 8806 | * |
| 8807 | * Implement the string() XPath function |
| 8808 | * string string(object?) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8809 | * The string function converts an object to a string as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8810 | * - A node-set is converted to a string by returning the value of |
| 8811 | * the node in the node-set that is first in document order. |
| 8812 | * If the node-set is empty, an empty string is returned. |
| 8813 | * - A number is converted to a string as follows |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8814 | * + NaN is converted to the string NaN |
| 8815 | * + positive zero is converted to the string 0 |
| 8816 | * + negative zero is converted to the string 0 |
| 8817 | * + positive infinity is converted to the string Infinity |
| 8818 | * + negative infinity is converted to the string -Infinity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8819 | * + if the number is an integer, the number is represented in |
| 8820 | * decimal form as a Number with no decimal point and no leading |
| 8821 | * zeros, preceded by a minus sign (-) if the number is negative |
| 8822 | * + otherwise, the number is represented in decimal form as a |
| 8823 | * Number including a decimal point with at least one digit |
| 8824 | * before the decimal point and at least one digit after the |
| 8825 | * decimal point, preceded by a minus sign (-) if the number |
| 8826 | * is negative; there must be no leading zeros before the decimal |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8827 | * point apart possibly from the one required digit immediately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8828 | * before the decimal point; beyond the one required digit |
| 8829 | * after the decimal point there must be as many, but only as |
| 8830 | * many, more digits as are needed to uniquely distinguish the |
| 8831 | * number from all other IEEE 754 numeric values. |
| 8832 | * - The boolean false value is converted to the string false. |
| 8833 | * The boolean true value is converted to the string true. |
| 8834 | * |
| 8835 | * If the argument is omitted, it defaults to a node-set with the |
| 8836 | * context node as its only member. |
| 8837 | */ |
| 8838 | void |
| 8839 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8840 | xmlXPathObjectPtr cur; |
| 8841 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8842 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8843 | if (nargs == 0) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8844 | valuePush(ctxt, |
| 8845 | xmlXPathCacheWrapString(ctxt->context, |
| 8846 | xmlXPathCastNodeToString(ctxt->context->node))); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8847 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8848 | } |
| 8849 | |
| 8850 | CHECK_ARITY(1); |
| 8851 | cur = valuePop(ctxt); |
| 8852 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8853 | valuePush(ctxt, xmlXPathCacheConvertString(ctxt->context, cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8854 | } |
| 8855 | |
| 8856 | /** |
| 8857 | * xmlXPathStringLengthFunction: |
| 8858 | * @ctxt: the XPath Parser context |
| 8859 | * @nargs: the number of arguments |
| 8860 | * |
| 8861 | * Implement the string-length() XPath function |
| 8862 | * number string-length(string?) |
| 8863 | * The string-length returns the number of characters in the string |
| 8864 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 8865 | * the context node converted to a string, in other words the value |
| 8866 | * of the context node. |
| 8867 | */ |
| 8868 | void |
| 8869 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8870 | xmlXPathObjectPtr cur; |
| 8871 | |
| 8872 | if (nargs == 0) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8873 | if ((ctxt == NULL) || (ctxt->context == NULL)) |
| 8874 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8875 | if (ctxt->context->node == NULL) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8876 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8877 | } else { |
| 8878 | xmlChar *content; |
| 8879 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 8880 | content = xmlXPathCastNodeToString(ctxt->context->node); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8881 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, |
| 8882 | xmlUTF8Strlen(content))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8883 | xmlFree(content); |
| 8884 | } |
| 8885 | return; |
| 8886 | } |
| 8887 | CHECK_ARITY(1); |
| 8888 | CAST_TO_STRING; |
| 8889 | CHECK_TYPE(XPATH_STRING); |
| 8890 | cur = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8891 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 8892 | xmlUTF8Strlen(cur->stringval))); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8893 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8894 | } |
| 8895 | |
| 8896 | /** |
| 8897 | * xmlXPathConcatFunction: |
| 8898 | * @ctxt: the XPath Parser context |
| 8899 | * @nargs: the number of arguments |
| 8900 | * |
| 8901 | * Implement the concat() XPath function |
| 8902 | * string concat(string, string, string*) |
| 8903 | * The concat function returns the concatenation of its arguments. |
| 8904 | */ |
| 8905 | void |
| 8906 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8907 | xmlXPathObjectPtr cur, newobj; |
| 8908 | xmlChar *tmp; |
| 8909 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 8910 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8911 | if (nargs < 2) { |
| 8912 | CHECK_ARITY(2); |
| 8913 | } |
| 8914 | |
| 8915 | CAST_TO_STRING; |
| 8916 | cur = valuePop(ctxt); |
| 8917 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8918 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8919 | return; |
| 8920 | } |
| 8921 | nargs--; |
| 8922 | |
| 8923 | while (nargs > 0) { |
| 8924 | CAST_TO_STRING; |
| 8925 | newobj = valuePop(ctxt); |
| 8926 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8927 | xmlXPathReleaseObject(ctxt->context, newobj); |
| 8928 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8929 | XP_ERROR(XPATH_INVALID_TYPE); |
| 8930 | } |
| 8931 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 8932 | newobj->stringval = cur->stringval; |
| 8933 | cur->stringval = tmp; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8934 | xmlXPathReleaseObject(ctxt->context, newobj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8935 | nargs--; |
| 8936 | } |
| 8937 | valuePush(ctxt, cur); |
| 8938 | } |
| 8939 | |
| 8940 | /** |
| 8941 | * xmlXPathContainsFunction: |
| 8942 | * @ctxt: the XPath Parser context |
| 8943 | * @nargs: the number of arguments |
| 8944 | * |
| 8945 | * Implement the contains() XPath function |
| 8946 | * boolean contains(string, string) |
| 8947 | * The contains function returns true if the first argument string |
| 8948 | * contains the second argument string, and otherwise returns false. |
| 8949 | */ |
| 8950 | void |
| 8951 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8952 | xmlXPathObjectPtr hay, needle; |
| 8953 | |
| 8954 | CHECK_ARITY(2); |
| 8955 | CAST_TO_STRING; |
| 8956 | CHECK_TYPE(XPATH_STRING); |
| 8957 | needle = valuePop(ctxt); |
| 8958 | CAST_TO_STRING; |
| 8959 | hay = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8960 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8961 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8962 | xmlXPathReleaseObject(ctxt->context, hay); |
| 8963 | xmlXPathReleaseObject(ctxt->context, needle); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8964 | XP_ERROR(XPATH_INVALID_TYPE); |
| 8965 | } |
| 8966 | if (xmlStrstr(hay->stringval, needle->stringval)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8967 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8968 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8969 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 0)); |
| 8970 | xmlXPathReleaseObject(ctxt->context, hay); |
| 8971 | xmlXPathReleaseObject(ctxt->context, needle); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8972 | } |
| 8973 | |
| 8974 | /** |
| 8975 | * xmlXPathStartsWithFunction: |
| 8976 | * @ctxt: the XPath Parser context |
| 8977 | * @nargs: the number of arguments |
| 8978 | * |
| 8979 | * Implement the starts-with() XPath function |
| 8980 | * boolean starts-with(string, string) |
| 8981 | * The starts-with function returns true if the first argument string |
| 8982 | * starts with the second argument string, and otherwise returns false. |
| 8983 | */ |
| 8984 | void |
| 8985 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 8986 | xmlXPathObjectPtr hay, needle; |
| 8987 | int n; |
| 8988 | |
| 8989 | CHECK_ARITY(2); |
| 8990 | CAST_TO_STRING; |
| 8991 | CHECK_TYPE(XPATH_STRING); |
| 8992 | needle = valuePop(ctxt); |
| 8993 | CAST_TO_STRING; |
| 8994 | hay = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8995 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8996 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 8997 | xmlXPathReleaseObject(ctxt->context, hay); |
| 8998 | xmlXPathReleaseObject(ctxt->context, needle); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8999 | XP_ERROR(XPATH_INVALID_TYPE); |
| 9000 | } |
| 9001 | n = xmlStrlen(needle->stringval); |
| 9002 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9003 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9004 | else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9005 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 1)); |
| 9006 | xmlXPathReleaseObject(ctxt->context, hay); |
| 9007 | xmlXPathReleaseObject(ctxt->context, needle); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9008 | } |
| 9009 | |
| 9010 | /** |
| 9011 | * xmlXPathSubstringFunction: |
| 9012 | * @ctxt: the XPath Parser context |
| 9013 | * @nargs: the number of arguments |
| 9014 | * |
| 9015 | * Implement the substring() XPath function |
| 9016 | * string substring(string, number, number?) |
| 9017 | * The substring function returns the substring of the first argument |
| 9018 | * starting at the position specified in the second argument with |
| 9019 | * length specified in the third argument. For example, |
| 9020 | * substring("12345",2,3) returns "234". If the third argument is not |
| 9021 | * specified, it returns the substring starting at the position specified |
| 9022 | * in the second argument and continuing to the end of the string. For |
| 9023 | * example, substring("12345",2) returns "2345". More precisely, each |
| 9024 | * character in the string (see [3.6 Strings]) is considered to have a |
| 9025 | * numeric position: the position of the first character is 1, the position |
| 9026 | * of the second character is 2 and so on. The returned substring contains |
| 9027 | * those characters for which the position of the character is greater than |
| 9028 | * or equal to the second argument and, if the third argument is specified, |
| 9029 | * less than the sum of the second and third arguments; the comparisons |
| 9030 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9031 | * - substring("12345", 1.5, 2.6) returns "234" |
| 9032 | * - substring("12345", 0, 3) returns "12" |
| 9033 | * - substring("12345", 0 div 0, 3) returns "" |
| 9034 | * - substring("12345", 1, 0 div 0) returns "" |
| 9035 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 9036 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9037 | */ |
| 9038 | void |
| 9039 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9040 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9041 | double le=0, in; |
| 9042 | int i, l, m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9043 | xmlChar *ret; |
| 9044 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9045 | if (nargs < 2) { |
| 9046 | CHECK_ARITY(2); |
| 9047 | } |
| 9048 | if (nargs > 3) { |
| 9049 | CHECK_ARITY(3); |
| 9050 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9051 | /* |
| 9052 | * take care of possible last (position) argument |
| 9053 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9054 | if (nargs == 3) { |
| 9055 | CAST_TO_NUMBER; |
| 9056 | CHECK_TYPE(XPATH_NUMBER); |
| 9057 | len = valuePop(ctxt); |
| 9058 | le = len->floatval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9059 | xmlXPathReleaseObject(ctxt->context, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9060 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9061 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9062 | CAST_TO_NUMBER; |
| 9063 | CHECK_TYPE(XPATH_NUMBER); |
| 9064 | start = valuePop(ctxt); |
| 9065 | in = start->floatval; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9066 | xmlXPathReleaseObject(ctxt->context, start); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9067 | CAST_TO_STRING; |
| 9068 | CHECK_TYPE(XPATH_STRING); |
| 9069 | str = valuePop(ctxt); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9070 | m = xmlUTF8Strlen((const unsigned char *)str->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9071 | |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9072 | /* |
| 9073 | * If last pos not present, calculate last position |
| 9074 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9075 | if (nargs != 3) { |
| 9076 | le = (double)m; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9077 | if (in < 1.0) |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9078 | in = 1.0; |
| 9079 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9080 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9081 | /* Need to check for the special cases where either |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9082 | * the index is NaN, the length is NaN, or both |
| 9083 | * arguments are infinity (relying on Inf + -Inf = NaN) |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9084 | */ |
Daniel Veillard | 48b3eb2 | 2009-03-25 09:51:19 +0000 | [diff] [blame] | 9085 | if (!xmlXPathIsInf(in) && !xmlXPathIsNaN(in + le)) { |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9086 | /* |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9087 | * To meet the requirements of the spec, the arguments |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9088 | * must be converted to integer format before |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9089 | * initial index calculations are done |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9090 | * |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9091 | * First we go to integer form, rounding up |
| 9092 | * and checking for special cases |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9093 | */ |
| 9094 | i = (int) in; |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9095 | if (((double)i)+0.5 <= in) i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9096 | |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 9097 | if (xmlXPathIsInf(le) == 1) { |
| 9098 | l = m; |
| 9099 | if (i < 1) |
| 9100 | i = 1; |
| 9101 | } |
| 9102 | else if (xmlXPathIsInf(le) == -1 || le < 0.0) |
| 9103 | l = 0; |
| 9104 | else { |
| 9105 | l = (int) le; |
| 9106 | if (((double)l)+0.5 <= le) l++; |
| 9107 | } |
| 9108 | |
| 9109 | /* Now we normalize inidices */ |
| 9110 | i -= 1; |
| 9111 | l += i; |
| 9112 | if (i < 0) |
| 9113 | i = 0; |
| 9114 | if (l > m) |
| 9115 | l = m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9116 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9117 | /* number of chars to copy */ |
| 9118 | l -= i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9119 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 9120 | ret = xmlUTF8Strsub(str->stringval, i, l); |
| 9121 | } |
| 9122 | else { |
| 9123 | ret = NULL; |
| 9124 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9125 | if (ret == NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9126 | valuePush(ctxt, xmlXPathCacheNewCString(ctxt->context, "")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9127 | else { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9128 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9129 | xmlFree(ret); |
| 9130 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9131 | xmlXPathReleaseObject(ctxt->context, str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9132 | } |
| 9133 | |
| 9134 | /** |
| 9135 | * xmlXPathSubstringBeforeFunction: |
| 9136 | * @ctxt: the XPath Parser context |
| 9137 | * @nargs: the number of arguments |
| 9138 | * |
| 9139 | * Implement the substring-before() XPath function |
| 9140 | * string substring-before(string, string) |
| 9141 | * The substring-before function returns the substring of the first |
| 9142 | * argument string that precedes the first occurrence of the second |
| 9143 | * argument string in the first argument string, or the empty string |
| 9144 | * if the first argument string does not contain the second argument |
| 9145 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 9146 | */ |
| 9147 | void |
| 9148 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9149 | xmlXPathObjectPtr str; |
| 9150 | xmlXPathObjectPtr find; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9151 | xmlBufPtr target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9152 | const xmlChar *point; |
| 9153 | int offset; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9154 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9155 | CHECK_ARITY(2); |
| 9156 | CAST_TO_STRING; |
| 9157 | find = valuePop(ctxt); |
| 9158 | CAST_TO_STRING; |
| 9159 | str = valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9160 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9161 | target = xmlBufCreate(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9162 | if (target) { |
| 9163 | point = xmlStrstr(str->stringval, find->stringval); |
| 9164 | if (point) { |
| 9165 | offset = (int)(point - str->stringval); |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9166 | xmlBufAdd(target, str->stringval, offset); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9167 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9168 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9169 | xmlBufContent(target))); |
| 9170 | xmlBufFree(target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9171 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9172 | xmlXPathReleaseObject(ctxt->context, str); |
| 9173 | xmlXPathReleaseObject(ctxt->context, find); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9174 | } |
| 9175 | |
| 9176 | /** |
| 9177 | * xmlXPathSubstringAfterFunction: |
| 9178 | * @ctxt: the XPath Parser context |
| 9179 | * @nargs: the number of arguments |
| 9180 | * |
| 9181 | * Implement the substring-after() XPath function |
| 9182 | * string substring-after(string, string) |
| 9183 | * The substring-after function returns the substring of the first |
| 9184 | * argument string that follows the first occurrence of the second |
| 9185 | * argument string in the first argument string, or the empty stringi |
| 9186 | * if the first argument string does not contain the second argument |
| 9187 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 9188 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 9189 | */ |
| 9190 | void |
| 9191 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9192 | xmlXPathObjectPtr str; |
| 9193 | xmlXPathObjectPtr find; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9194 | xmlBufPtr target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9195 | const xmlChar *point; |
| 9196 | int offset; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9197 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9198 | CHECK_ARITY(2); |
| 9199 | CAST_TO_STRING; |
| 9200 | find = valuePop(ctxt); |
| 9201 | CAST_TO_STRING; |
| 9202 | str = valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9203 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9204 | target = xmlBufCreate(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9205 | if (target) { |
| 9206 | point = xmlStrstr(str->stringval, find->stringval); |
| 9207 | if (point) { |
| 9208 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9209 | xmlBufAdd(target, &str->stringval[offset], |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9210 | xmlStrlen(str->stringval) - offset); |
| 9211 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9212 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9213 | xmlBufContent(target))); |
| 9214 | xmlBufFree(target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9215 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9216 | xmlXPathReleaseObject(ctxt->context, str); |
| 9217 | xmlXPathReleaseObject(ctxt->context, find); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9218 | } |
| 9219 | |
| 9220 | /** |
| 9221 | * xmlXPathNormalizeFunction: |
| 9222 | * @ctxt: the XPath Parser context |
| 9223 | * @nargs: the number of arguments |
| 9224 | * |
| 9225 | * Implement the normalize-space() XPath function |
| 9226 | * string normalize-space(string?) |
| 9227 | * The normalize-space function returns the argument string with white |
| 9228 | * space normalized by stripping leading and trailing whitespace |
| 9229 | * and replacing sequences of whitespace characters by a single |
| 9230 | * space. Whitespace characters are the same allowed by the S production |
| 9231 | * in XML. If the argument is omitted, it defaults to the context |
| 9232 | * node converted to a string, in other words the value of the context node. |
| 9233 | */ |
| 9234 | void |
| 9235 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9236 | xmlXPathObjectPtr obj = NULL; |
| 9237 | xmlChar *source = NULL; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9238 | xmlBufPtr target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9239 | xmlChar blank; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9240 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 9241 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9242 | if (nargs == 0) { |
| 9243 | /* Use current context node */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9244 | valuePush(ctxt, |
| 9245 | xmlXPathCacheWrapString(ctxt->context, |
| 9246 | xmlXPathCastNodeToString(ctxt->context->node))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9247 | nargs = 1; |
| 9248 | } |
| 9249 | |
| 9250 | CHECK_ARITY(1); |
| 9251 | CAST_TO_STRING; |
| 9252 | CHECK_TYPE(XPATH_STRING); |
| 9253 | obj = valuePop(ctxt); |
| 9254 | source = obj->stringval; |
| 9255 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9256 | target = xmlBufCreate(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9257 | if (target && source) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9258 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9259 | /* Skip leading whitespaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9260 | while (IS_BLANK_CH(*source)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9261 | source++; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9262 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9263 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 9264 | blank = 0; |
| 9265 | while (*source) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9266 | if (IS_BLANK_CH(*source)) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9267 | blank = 0x20; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9268 | } else { |
| 9269 | if (blank) { |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9270 | xmlBufAdd(target, &blank, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9271 | blank = 0; |
| 9272 | } |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9273 | xmlBufAdd(target, source, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9274 | } |
| 9275 | source++; |
| 9276 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9277 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9278 | xmlBufContent(target))); |
| 9279 | xmlBufFree(target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9280 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9281 | xmlXPathReleaseObject(ctxt->context, obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9282 | } |
| 9283 | |
| 9284 | /** |
| 9285 | * xmlXPathTranslateFunction: |
| 9286 | * @ctxt: the XPath Parser context |
| 9287 | * @nargs: the number of arguments |
| 9288 | * |
| 9289 | * Implement the translate() XPath function |
| 9290 | * string translate(string, string, string) |
| 9291 | * The translate function returns the first argument string with |
| 9292 | * occurrences of characters in the second argument string replaced |
| 9293 | * by the character at the corresponding position in the third argument |
| 9294 | * string. For example, translate("bar","abc","ABC") returns the string |
| 9295 | * BAr. If there is a character in the second argument string with no |
| 9296 | * character at a corresponding position in the third argument string |
| 9297 | * (because the second argument string is longer than the third argument |
| 9298 | * string), then occurrences of that character in the first argument |
| 9299 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 9300 | * returns "AAA". If a character occurs more than once in second |
| 9301 | * argument string, then the first occurrence determines the replacement |
| 9302 | * character. If the third argument string is longer than the second |
| 9303 | * argument string, then excess characters are ignored. |
| 9304 | */ |
| 9305 | void |
| 9306 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9307 | xmlXPathObjectPtr str; |
| 9308 | xmlXPathObjectPtr from; |
| 9309 | xmlXPathObjectPtr to; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9310 | xmlBufPtr target; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9311 | int offset, max; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9312 | xmlChar ch; |
William M. Brack | b031cef | 2004-11-05 16:34:22 +0000 | [diff] [blame] | 9313 | const xmlChar *point; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9314 | xmlChar *cptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9315 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9316 | CHECK_ARITY(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9317 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9318 | CAST_TO_STRING; |
| 9319 | to = valuePop(ctxt); |
| 9320 | CAST_TO_STRING; |
| 9321 | from = valuePop(ctxt); |
| 9322 | CAST_TO_STRING; |
| 9323 | str = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9324 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9325 | target = xmlBufCreate(); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9326 | if (target) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9327 | max = xmlUTF8Strlen(to->stringval); |
| 9328 | for (cptr = str->stringval; (ch=*cptr); ) { |
| 9329 | offset = xmlUTF8Strloc(from->stringval, cptr); |
| 9330 | if (offset >= 0) { |
| 9331 | if (offset < max) { |
| 9332 | point = xmlUTF8Strpos(to->stringval, offset); |
| 9333 | if (point) |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9334 | xmlBufAdd(target, point, xmlUTF8Strsize(point, 1)); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9335 | } |
| 9336 | } else |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9337 | xmlBufAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9338 | |
| 9339 | /* Step to next character in input */ |
| 9340 | cptr++; |
| 9341 | if ( ch & 0x80 ) { |
| 9342 | /* if not simple ascii, verify proper format */ |
| 9343 | if ( (ch & 0xc0) != 0xc0 ) { |
| 9344 | xmlGenericError(xmlGenericErrorContext, |
| 9345 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 9346 | /* not asserting an XPath error is probably better */ |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9347 | break; |
| 9348 | } |
| 9349 | /* then skip over remaining bytes for this char */ |
| 9350 | while ( (ch <<= 1) & 0x80 ) |
| 9351 | if ( (*cptr++ & 0xc0) != 0x80 ) { |
| 9352 | xmlGenericError(xmlGenericErrorContext, |
| 9353 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 9354 | /* not asserting an XPath error is probably better */ |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 9355 | break; |
| 9356 | } |
| 9357 | if (ch & 0x80) /* must have had error encountered */ |
| 9358 | break; |
| 9359 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9360 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9361 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9362 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 9363 | xmlBufContent(target))); |
| 9364 | xmlBufFree(target); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9365 | xmlXPathReleaseObject(ctxt->context, str); |
| 9366 | xmlXPathReleaseObject(ctxt->context, from); |
| 9367 | xmlXPathReleaseObject(ctxt->context, to); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9368 | } |
| 9369 | |
| 9370 | /** |
| 9371 | * xmlXPathBooleanFunction: |
| 9372 | * @ctxt: the XPath Parser context |
| 9373 | * @nargs: the number of arguments |
| 9374 | * |
| 9375 | * Implement the boolean() XPath function |
| 9376 | * boolean boolean(object) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9377 | * The boolean function converts its argument to a boolean as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9378 | * - a number is true if and only if it is neither positive or |
| 9379 | * negative zero nor NaN |
| 9380 | * - a node-set is true if and only if it is non-empty |
| 9381 | * - a string is true if and only if its length is non-zero |
| 9382 | */ |
| 9383 | void |
| 9384 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9385 | xmlXPathObjectPtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9386 | |
| 9387 | CHECK_ARITY(1); |
| 9388 | cur = valuePop(ctxt); |
| 9389 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9390 | cur = xmlXPathCacheConvertBoolean(ctxt->context, cur); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 9391 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9392 | } |
| 9393 | |
| 9394 | /** |
| 9395 | * xmlXPathNotFunction: |
| 9396 | * @ctxt: the XPath Parser context |
| 9397 | * @nargs: the number of arguments |
| 9398 | * |
| 9399 | * Implement the not() XPath function |
| 9400 | * boolean not(boolean) |
| 9401 | * The not function returns true if its argument is false, |
| 9402 | * and false otherwise. |
| 9403 | */ |
| 9404 | void |
| 9405 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9406 | CHECK_ARITY(1); |
| 9407 | CAST_TO_BOOLEAN; |
| 9408 | CHECK_TYPE(XPATH_BOOLEAN); |
| 9409 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 9410 | } |
| 9411 | |
| 9412 | /** |
| 9413 | * xmlXPathTrueFunction: |
| 9414 | * @ctxt: the XPath Parser context |
| 9415 | * @nargs: the number of arguments |
| 9416 | * |
| 9417 | * Implement the true() XPath function |
| 9418 | * boolean true() |
| 9419 | */ |
| 9420 | void |
| 9421 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9422 | CHECK_ARITY(0); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9423 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9424 | } |
| 9425 | |
| 9426 | /** |
| 9427 | * xmlXPathFalseFunction: |
| 9428 | * @ctxt: the XPath Parser context |
| 9429 | * @nargs: the number of arguments |
| 9430 | * |
| 9431 | * Implement the false() XPath function |
| 9432 | * boolean false() |
| 9433 | */ |
| 9434 | void |
| 9435 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9436 | CHECK_ARITY(0); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9437 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9438 | } |
| 9439 | |
| 9440 | /** |
| 9441 | * xmlXPathLangFunction: |
| 9442 | * @ctxt: the XPath Parser context |
| 9443 | * @nargs: the number of arguments |
| 9444 | * |
| 9445 | * Implement the lang() XPath function |
| 9446 | * boolean lang(string) |
| 9447 | * The lang function returns true or false depending on whether the |
| 9448 | * language of the context node as specified by xml:lang attributes |
| 9449 | * is the same as or is a sublanguage of the language specified by |
| 9450 | * the argument string. The language of the context node is determined |
| 9451 | * by the value of the xml:lang attribute on the context node, or, if |
| 9452 | * the context node has no xml:lang attribute, by the value of the |
| 9453 | * xml:lang attribute on the nearest ancestor of the context node that |
| 9454 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 9455 | * returns false. If there is such an attribute, then lang returns |
| 9456 | * true if the attribute value is equal to the argument ignoring case, |
| 9457 | * or if there is some suffix starting with - such that the attribute |
| 9458 | * value is equal to the argument ignoring that suffix of the attribute |
| 9459 | * value and ignoring case. |
| 9460 | */ |
| 9461 | void |
| 9462 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 9463 | xmlXPathObjectPtr val = NULL; |
| 9464 | const xmlChar *theLang = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9465 | const xmlChar *lang; |
| 9466 | int ret = 0; |
| 9467 | int i; |
| 9468 | |
| 9469 | CHECK_ARITY(1); |
| 9470 | CAST_TO_STRING; |
| 9471 | CHECK_TYPE(XPATH_STRING); |
| 9472 | val = valuePop(ctxt); |
| 9473 | lang = val->stringval; |
| 9474 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 9475 | if ((theLang != NULL) && (lang != NULL)) { |
| 9476 | for (i = 0;lang[i] != 0;i++) |
| 9477 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 9478 | goto not_equal; |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 9479 | if ((theLang[i] == 0) || (theLang[i] == '-')) |
| 9480 | ret = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9481 | } |
| 9482 | not_equal: |
Daniel Veillard | 4ddaa56 | 2005-04-06 14:09:08 +0000 | [diff] [blame] | 9483 | if (theLang != NULL) |
| 9484 | xmlFree((void *)theLang); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9485 | |
| 9486 | xmlXPathReleaseObject(ctxt->context, val); |
| 9487 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9488 | } |
| 9489 | |
| 9490 | /** |
| 9491 | * xmlXPathNumberFunction: |
| 9492 | * @ctxt: the XPath Parser context |
| 9493 | * @nargs: the number of arguments |
| 9494 | * |
| 9495 | * Implement the number() XPath function |
| 9496 | * number number(object?) |
| 9497 | */ |
| 9498 | void |
| 9499 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9500 | xmlXPathObjectPtr cur; |
| 9501 | double res; |
| 9502 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 9503 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9504 | if (nargs == 0) { |
| 9505 | if (ctxt->context->node == NULL) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9506 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, 0.0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9507 | } else { |
| 9508 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 9509 | |
| 9510 | res = xmlXPathStringEvalNumber(content); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9511 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, res)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9512 | xmlFree(content); |
| 9513 | } |
| 9514 | return; |
| 9515 | } |
| 9516 | |
| 9517 | CHECK_ARITY(1); |
| 9518 | cur = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9519 | valuePush(ctxt, xmlXPathCacheConvertNumber(ctxt->context, cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9520 | } |
| 9521 | |
| 9522 | /** |
| 9523 | * xmlXPathSumFunction: |
| 9524 | * @ctxt: the XPath Parser context |
| 9525 | * @nargs: the number of arguments |
| 9526 | * |
| 9527 | * Implement the sum() XPath function |
| 9528 | * number sum(node-set) |
| 9529 | * The sum function returns the sum of the values of the nodes in |
| 9530 | * the argument node-set. |
| 9531 | */ |
| 9532 | void |
| 9533 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9534 | xmlXPathObjectPtr cur; |
| 9535 | int i; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 9536 | double res = 0.0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9537 | |
| 9538 | CHECK_ARITY(1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9539 | if ((ctxt->value == NULL) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9540 | ((ctxt->value->type != XPATH_NODESET) && |
| 9541 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 9542 | XP_ERROR(XPATH_INVALID_TYPE); |
| 9543 | cur = valuePop(ctxt); |
| 9544 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9545 | if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 9546 | for (i = 0; i < cur->nodesetval->nodeNr; i++) { |
| 9547 | res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9548 | } |
| 9549 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 9550 | valuePush(ctxt, xmlXPathCacheNewFloat(ctxt->context, res)); |
| 9551 | xmlXPathReleaseObject(ctxt->context, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9552 | } |
| 9553 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 9554 | /* |
| 9555 | * To assure working code on multiple platforms, we want to only depend |
| 9556 | * upon the characteristic truncation of converting a floating point value |
| 9557 | * to an integer. Unfortunately, because of the different storage sizes |
| 9558 | * of our internal floating point value (double) and integer (int), we |
| 9559 | * can't directly convert (see bug 301162). This macro is a messy |
| 9560 | * 'workaround' |
| 9561 | */ |
| 9562 | #define XTRUNC(f, v) \ |
| 9563 | f = fmod((v), INT_MAX); \ |
| 9564 | f = (v) - (f) + (double)((int)(f)); |
| 9565 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9566 | /** |
| 9567 | * xmlXPathFloorFunction: |
| 9568 | * @ctxt: the XPath Parser context |
| 9569 | * @nargs: the number of arguments |
| 9570 | * |
| 9571 | * Implement the floor() XPath function |
| 9572 | * number floor(number) |
| 9573 | * The floor function returns the largest (closest to positive infinity) |
| 9574 | * number that is not greater than the argument and that is an integer. |
| 9575 | */ |
| 9576 | void |
| 9577 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9578 | double f; |
| 9579 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9580 | CHECK_ARITY(1); |
| 9581 | CAST_TO_NUMBER; |
| 9582 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9583 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 9584 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9585 | if (f != ctxt->value->floatval) { |
| 9586 | if (ctxt->value->floatval > 0) |
| 9587 | ctxt->value->floatval = f; |
| 9588 | else |
| 9589 | ctxt->value->floatval = f - 1; |
| 9590 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9591 | } |
| 9592 | |
| 9593 | /** |
| 9594 | * xmlXPathCeilingFunction: |
| 9595 | * @ctxt: the XPath Parser context |
| 9596 | * @nargs: the number of arguments |
| 9597 | * |
| 9598 | * Implement the ceiling() XPath function |
| 9599 | * number ceiling(number) |
| 9600 | * The ceiling function returns the smallest (closest to negative infinity) |
| 9601 | * number that is not less than the argument and that is an integer. |
| 9602 | */ |
| 9603 | void |
| 9604 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9605 | double f; |
| 9606 | |
| 9607 | CHECK_ARITY(1); |
| 9608 | CAST_TO_NUMBER; |
| 9609 | CHECK_TYPE(XPATH_NUMBER); |
| 9610 | |
| 9611 | #if 0 |
| 9612 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 9613 | #else |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 9614 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9615 | if (f != ctxt->value->floatval) { |
| 9616 | if (ctxt->value->floatval > 0) |
| 9617 | ctxt->value->floatval = f + 1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 9618 | else { |
| 9619 | if (ctxt->value->floatval < 0 && f == 0) |
| 9620 | ctxt->value->floatval = xmlXPathNZERO; |
| 9621 | else |
| 9622 | ctxt->value->floatval = f; |
| 9623 | } |
| 9624 | |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9625 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9626 | #endif |
| 9627 | } |
| 9628 | |
| 9629 | /** |
| 9630 | * xmlXPathRoundFunction: |
| 9631 | * @ctxt: the XPath Parser context |
| 9632 | * @nargs: the number of arguments |
| 9633 | * |
| 9634 | * Implement the round() XPath function |
| 9635 | * number round(number) |
| 9636 | * The round function returns the number that is closest to the |
| 9637 | * argument and that is an integer. If there are two such numbers, |
| 9638 | * then the one that is even is returned. |
| 9639 | */ |
| 9640 | void |
| 9641 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 9642 | double f; |
| 9643 | |
| 9644 | CHECK_ARITY(1); |
| 9645 | CAST_TO_NUMBER; |
| 9646 | CHECK_TYPE(XPATH_NUMBER); |
| 9647 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 9648 | if ((xmlXPathIsNaN(ctxt->value->floatval)) || |
| 9649 | (xmlXPathIsInf(ctxt->value->floatval) == 1) || |
| 9650 | (xmlXPathIsInf(ctxt->value->floatval) == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9651 | (ctxt->value->floatval == 0.0)) |
| 9652 | return; |
| 9653 | |
William M. Brack | 3d42666 | 2005-04-19 14:40:28 +0000 | [diff] [blame] | 9654 | XTRUNC(f, ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9655 | if (ctxt->value->floatval < 0) { |
| 9656 | if (ctxt->value->floatval < f - 0.5) |
| 9657 | ctxt->value->floatval = f - 1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9658 | else |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9659 | ctxt->value->floatval = f; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 9660 | if (ctxt->value->floatval == 0) |
| 9661 | ctxt->value->floatval = xmlXPathNZERO; |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9662 | } else { |
| 9663 | if (ctxt->value->floatval < f + 0.5) |
| 9664 | ctxt->value->floatval = f; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9665 | else |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 9666 | ctxt->value->floatval = f + 1; |
| 9667 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9668 | } |
| 9669 | |
| 9670 | /************************************************************************ |
| 9671 | * * |
| 9672 | * The Parser * |
| 9673 | * * |
| 9674 | ************************************************************************/ |
| 9675 | |
| 9676 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9677 | * a few forward declarations since we use a recursive call based |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9678 | * implementation. |
| 9679 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 9680 | static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt, int sort); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9681 | static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9682 | static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9683 | static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9684 | static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, |
| 9685 | int qualified); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9686 | |
| 9687 | /** |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9688 | * xmlXPathCurrentChar: |
| 9689 | * @ctxt: the XPath parser context |
| 9690 | * @cur: pointer to the beginning of the char |
| 9691 | * @len: pointer to the length of the char read |
| 9692 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9693 | * 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] | 9694 | * bytes in the input buffer. |
| 9695 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 9696 | * Returns the current char value and its length |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9697 | */ |
| 9698 | |
| 9699 | static int |
| 9700 | xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) { |
| 9701 | unsigned char c; |
| 9702 | unsigned int val; |
| 9703 | const xmlChar *cur; |
| 9704 | |
| 9705 | if (ctxt == NULL) |
| 9706 | return(0); |
| 9707 | cur = ctxt->cur; |
| 9708 | |
| 9709 | /* |
| 9710 | * We are supposed to handle UTF8, check it's valid |
| 9711 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 9712 | * |
| 9713 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 9714 | * 0000 0000-0000 007F 0xxxxxxx |
| 9715 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9716 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9717 | * |
| 9718 | * Check for the 0x110000 limit too |
| 9719 | */ |
| 9720 | c = *cur; |
| 9721 | if (c & 0x80) { |
| 9722 | if ((cur[1] & 0xc0) != 0x80) |
| 9723 | goto encoding_error; |
| 9724 | if ((c & 0xe0) == 0xe0) { |
| 9725 | |
| 9726 | if ((cur[2] & 0xc0) != 0x80) |
| 9727 | goto encoding_error; |
| 9728 | if ((c & 0xf0) == 0xf0) { |
| 9729 | if (((c & 0xf8) != 0xf0) || |
| 9730 | ((cur[3] & 0xc0) != 0x80)) |
| 9731 | goto encoding_error; |
| 9732 | /* 4-byte code */ |
| 9733 | *len = 4; |
| 9734 | val = (cur[0] & 0x7) << 18; |
| 9735 | val |= (cur[1] & 0x3f) << 12; |
| 9736 | val |= (cur[2] & 0x3f) << 6; |
| 9737 | val |= cur[3] & 0x3f; |
| 9738 | } else { |
| 9739 | /* 3-byte code */ |
| 9740 | *len = 3; |
| 9741 | val = (cur[0] & 0xf) << 12; |
| 9742 | val |= (cur[1] & 0x3f) << 6; |
| 9743 | val |= cur[2] & 0x3f; |
| 9744 | } |
| 9745 | } else { |
| 9746 | /* 2-byte code */ |
| 9747 | *len = 2; |
| 9748 | val = (cur[0] & 0x1f) << 6; |
| 9749 | val |= cur[1] & 0x3f; |
| 9750 | } |
| 9751 | if (!IS_CHAR(val)) { |
| 9752 | XP_ERROR0(XPATH_INVALID_CHAR_ERROR); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9753 | } |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9754 | return(val); |
| 9755 | } else { |
| 9756 | /* 1-byte code */ |
| 9757 | *len = 1; |
| 9758 | return((int) *cur); |
| 9759 | } |
| 9760 | encoding_error: |
| 9761 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9762 | * If we detect an UTF8 error that probably means that the |
| 9763 | * input encoding didn't get properly advertised in the |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9764 | * declaration header. Report the error and switch the encoding |
| 9765 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 9766 | * encoding !) |
| 9767 | */ |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 9768 | *len = 0; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9769 | XP_ERROR0(XPATH_ENCODING_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9770 | } |
| 9771 | |
| 9772 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9773 | * xmlXPathParseNCName: |
| 9774 | * @ctxt: the XPath Parser context |
| 9775 | * |
| 9776 | * parse an XML namespace non qualified name. |
| 9777 | * |
| 9778 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 9779 | * |
| 9780 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 9781 | * CombiningChar | Extender |
| 9782 | * |
| 9783 | * Returns the namespace name or NULL |
| 9784 | */ |
| 9785 | |
| 9786 | xmlChar * |
| 9787 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9788 | const xmlChar *in; |
| 9789 | xmlChar *ret; |
| 9790 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9791 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 9792 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9793 | /* |
| 9794 | * Accelerator for simple ASCII names |
| 9795 | */ |
| 9796 | in = ctxt->cur; |
| 9797 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 9798 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 9799 | (*in == '_')) { |
| 9800 | in++; |
| 9801 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 9802 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 9803 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 9a89a8a | 2001-06-27 11:13:35 +0000 | [diff] [blame] | 9804 | (*in == '_') || (*in == '.') || |
| 9805 | (*in == '-')) |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9806 | in++; |
| 9807 | if ((*in == ' ') || (*in == '>') || (*in == '/') || |
| 9808 | (*in == '[') || (*in == ']') || (*in == ':') || |
| 9809 | (*in == '@') || (*in == '*')) { |
| 9810 | count = in - ctxt->cur; |
| 9811 | if (count == 0) |
| 9812 | return(NULL); |
| 9813 | ret = xmlStrndup(ctxt->cur, count); |
| 9814 | ctxt->cur = in; |
| 9815 | return(ret); |
| 9816 | } |
| 9817 | } |
| 9818 | return(xmlXPathParseNameComplex(ctxt, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9819 | } |
| 9820 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9821 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9822 | /** |
| 9823 | * xmlXPathParseQName: |
| 9824 | * @ctxt: the XPath Parser context |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9825 | * @prefix: a xmlChar ** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9826 | * |
| 9827 | * parse an XML qualified name |
| 9828 | * |
| 9829 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 9830 | * |
| 9831 | * [NS 6] Prefix ::= NCName |
| 9832 | * |
| 9833 | * [NS 7] LocalPart ::= NCName |
| 9834 | * |
| 9835 | * Returns the function returns the local part, and prefix is updated |
| 9836 | * to get the Prefix if any. |
| 9837 | */ |
| 9838 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 9839 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9840 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 9841 | xmlChar *ret = NULL; |
| 9842 | |
| 9843 | *prefix = NULL; |
| 9844 | ret = xmlXPathParseNCName(ctxt); |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 9845 | if (ret && CUR == ':') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9846 | *prefix = ret; |
| 9847 | NEXT; |
| 9848 | ret = xmlXPathParseNCName(ctxt); |
| 9849 | } |
| 9850 | return(ret); |
| 9851 | } |
| 9852 | |
| 9853 | /** |
| 9854 | * xmlXPathParseName: |
| 9855 | * @ctxt: the XPath Parser context |
| 9856 | * |
| 9857 | * parse an XML name |
| 9858 | * |
| 9859 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 9860 | * CombiningChar | Extender |
| 9861 | * |
| 9862 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 9863 | * |
| 9864 | * Returns the namespace name or NULL |
| 9865 | */ |
| 9866 | |
| 9867 | xmlChar * |
| 9868 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9869 | const xmlChar *in; |
| 9870 | xmlChar *ret; |
| 9871 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9872 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 9873 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9874 | /* |
| 9875 | * Accelerator for simple ASCII names |
| 9876 | */ |
| 9877 | in = ctxt->cur; |
| 9878 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 9879 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 9880 | (*in == '_') || (*in == ':')) { |
| 9881 | in++; |
| 9882 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 9883 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 9884 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 9885 | (*in == '_') || (*in == '-') || |
| 9886 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9887 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 9888 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9889 | count = in - ctxt->cur; |
| 9890 | ret = xmlStrndup(ctxt->cur, count); |
| 9891 | ctxt->cur = in; |
| 9892 | return(ret); |
| 9893 | } |
| 9894 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9895 | return(xmlXPathParseNameComplex(ctxt, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9896 | } |
| 9897 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9898 | static xmlChar * |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9899 | xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9900 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 9901 | int len = 0, l; |
| 9902 | int c; |
| 9903 | |
| 9904 | /* |
| 9905 | * Handler for more complex cases |
| 9906 | */ |
| 9907 | c = CUR_CHAR(l); |
| 9908 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9909 | (c == '[') || (c == ']') || (c == '@') || /* accelerators */ |
| 9910 | (c == '*') || /* accelerators */ |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9911 | (!IS_LETTER(c) && (c != '_') && |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9912 | ((qualified) && (c != ':')))) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9913 | return(NULL); |
| 9914 | } |
| 9915 | |
| 9916 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 9917 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 9918 | (c == '.') || (c == '-') || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9919 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9920 | (IS_COMBINING(c)) || |
| 9921 | (IS_EXTENDER(c)))) { |
| 9922 | COPY_BUF(l,buf,len,c); |
| 9923 | NEXTL(l); |
| 9924 | c = CUR_CHAR(l); |
| 9925 | if (len >= XML_MAX_NAMELEN) { |
| 9926 | /* |
| 9927 | * Okay someone managed to make a huge name, so he's ready to pay |
| 9928 | * for the processing speed. |
| 9929 | */ |
| 9930 | xmlChar *buffer; |
| 9931 | int max = len * 2; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9932 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9933 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9934 | if (buffer == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 9935 | XP_ERRORNULL(XPATH_MEMORY_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9936 | } |
| 9937 | memcpy(buffer, buf, len); |
| 9938 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */ |
| 9939 | (c == '.') || (c == '-') || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9940 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9941 | (IS_COMBINING(c)) || |
| 9942 | (IS_EXTENDER(c))) { |
| 9943 | if (len + 10 > max) { |
| 9944 | max *= 2; |
| 9945 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 9946 | max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9947 | if (buffer == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 9948 | XP_ERRORNULL(XPATH_MEMORY_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9949 | } |
| 9950 | } |
| 9951 | COPY_BUF(l,buffer,len,c); |
| 9952 | NEXTL(l); |
| 9953 | c = CUR_CHAR(l); |
| 9954 | } |
| 9955 | buffer[len] = 0; |
| 9956 | return(buffer); |
| 9957 | } |
| 9958 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 9959 | if (len == 0) |
| 9960 | return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 9961 | return(xmlStrndup(buf, len)); |
| 9962 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 9963 | |
| 9964 | #define MAX_FRAC 20 |
| 9965 | |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 9966 | /* |
| 9967 | * These are used as divisors for the fractional part of a number. |
| 9968 | * Since the table includes 1.0 (representing '0' fractional digits), |
| 9969 | * it must be dimensioned at MAX_FRAC+1 (bug 133921) |
| 9970 | */ |
| 9971 | static double my_pow10[MAX_FRAC+1] = { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 9972 | 1.0, 10.0, 100.0, 1000.0, 10000.0, |
| 9973 | 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, |
| 9974 | 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, |
| 9975 | 100000000000000.0, |
| 9976 | 1000000000000000.0, 10000000000000000.0, 100000000000000000.0, |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 9977 | 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0 |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 9978 | }; |
| 9979 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9980 | /** |
| 9981 | * xmlXPathStringEvalNumber: |
| 9982 | * @str: A string to scan |
| 9983 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 9984 | * [30a] Float ::= Number ('e' Digits?)? |
| 9985 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9986 | * [30] Number ::= Digits ('.' Digits?)? |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 9987 | * | '.' Digits |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9988 | * [31] Digits ::= [0-9]+ |
| 9989 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9990 | * Compile a Number in the string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9991 | * In complement of the Number expression, this function also handles |
| 9992 | * negative values : '-' Number. |
| 9993 | * |
| 9994 | * Returns the double value. |
| 9995 | */ |
| 9996 | double |
| 9997 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 9998 | const xmlChar *cur = str; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 9999 | double ret; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10000 | int ok = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10001 | int isneg = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10002 | int exponent = 0; |
| 10003 | int is_exponent_negative = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10004 | #ifdef __GNUC__ |
| 10005 | unsigned long tmp = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10006 | double temp; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10007 | #endif |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 10008 | if (cur == NULL) return(0); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10009 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10010 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
| 10011 | return(xmlXPathNAN); |
| 10012 | } |
| 10013 | if (*cur == '-') { |
| 10014 | isneg = 1; |
| 10015 | cur++; |
| 10016 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10017 | |
| 10018 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10019 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10020 | * tmp/temp is a workaround against a gcc compiler bug |
| 10021 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10022 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10023 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10024 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10025 | ret = ret * 10; |
| 10026 | tmp = (*cur - '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10027 | ok = 1; |
| 10028 | cur++; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10029 | temp = (double) tmp; |
| 10030 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10031 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10032 | #else |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10033 | ret = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 10034 | while ((*cur >= '0') && (*cur <= '9')) { |
| 10035 | ret = ret * 10 + (*cur - '0'); |
| 10036 | ok = 1; |
| 10037 | cur++; |
| 10038 | } |
| 10039 | #endif |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10040 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10041 | if (*cur == '.') { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 10042 | int v, frac = 0; |
| 10043 | double fraction = 0; |
| 10044 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10045 | cur++; |
| 10046 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
| 10047 | return(xmlXPathNAN); |
| 10048 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 10049 | while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) { |
| 10050 | v = (*cur - '0'); |
| 10051 | fraction = fraction * 10 + v; |
| 10052 | frac = frac + 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10053 | cur++; |
| 10054 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 10055 | fraction /= my_pow10[frac]; |
| 10056 | ret = ret + fraction; |
| 10057 | while ((*cur >= '0') && (*cur <= '9')) |
| 10058 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10059 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10060 | if ((*cur == 'e') || (*cur == 'E')) { |
| 10061 | cur++; |
| 10062 | if (*cur == '-') { |
| 10063 | is_exponent_negative = 1; |
| 10064 | cur++; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 10065 | } else if (*cur == '+') { |
| 10066 | cur++; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10067 | } |
| 10068 | while ((*cur >= '0') && (*cur <= '9')) { |
| 10069 | exponent = exponent * 10 + (*cur - '0'); |
| 10070 | cur++; |
| 10071 | } |
| 10072 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10073 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10074 | if (*cur != 0) return(xmlXPathNAN); |
| 10075 | if (isneg) ret = -ret; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10076 | if (is_exponent_negative) exponent = -exponent; |
| 10077 | ret *= pow(10.0, (double)exponent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10078 | return(ret); |
| 10079 | } |
| 10080 | |
| 10081 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10082 | * xmlXPathCompNumber: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10083 | * @ctxt: the XPath Parser context |
| 10084 | * |
| 10085 | * [30] Number ::= Digits ('.' Digits?)? |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10086 | * | '.' Digits |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10087 | * [31] Digits ::= [0-9]+ |
| 10088 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10089 | * Compile a Number, then push it on the stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10090 | * |
| 10091 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10092 | static void |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10093 | xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) |
| 10094 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10095 | double ret = 0.0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10096 | int ok = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10097 | int exponent = 0; |
| 10098 | int is_exponent_negative = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10099 | #ifdef __GNUC__ |
| 10100 | unsigned long tmp = 0; |
| 10101 | double temp; |
| 10102 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10103 | |
| 10104 | CHECK_ERROR; |
| 10105 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
| 10106 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 10107 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10108 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10109 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10110 | * tmp/temp is a workaround against a gcc compiler bug |
| 10111 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10112 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10113 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10114 | while ((CUR >= '0') && (CUR <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10115 | ret = ret * 10; |
| 10116 | tmp = (CUR - '0'); |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10117 | ok = 1; |
| 10118 | NEXT; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10119 | temp = (double) tmp; |
| 10120 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10121 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 10122 | #else |
| 10123 | ret = 0; |
| 10124 | while ((CUR >= '0') && (CUR <= '9')) { |
| 10125 | ret = ret * 10 + (CUR - '0'); |
| 10126 | ok = 1; |
| 10127 | NEXT; |
| 10128 | } |
| 10129 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10130 | if (CUR == '.') { |
Phil Shafer | ee32ad3 | 2010-11-03 20:53:55 +0100 | [diff] [blame] | 10131 | int v, frac = 0; |
| 10132 | double fraction = 0; |
| 10133 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10134 | NEXT; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10135 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
| 10136 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 10137 | } |
Phil Shafer | ee32ad3 | 2010-11-03 20:53:55 +0100 | [diff] [blame] | 10138 | while ((CUR >= '0') && (CUR <= '9') && (frac < MAX_FRAC)) { |
| 10139 | v = (CUR - '0'); |
| 10140 | fraction = fraction * 10 + v; |
| 10141 | frac = frac + 1; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10142 | NEXT; |
| 10143 | } |
Phil Shafer | ee32ad3 | 2010-11-03 20:53:55 +0100 | [diff] [blame] | 10144 | fraction /= my_pow10[frac]; |
| 10145 | ret = ret + fraction; |
| 10146 | while ((CUR >= '0') && (CUR <= '9')) |
| 10147 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10148 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10149 | if ((CUR == 'e') || (CUR == 'E')) { |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10150 | NEXT; |
| 10151 | if (CUR == '-') { |
| 10152 | is_exponent_negative = 1; |
| 10153 | NEXT; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 10154 | } else if (CUR == '+') { |
| 10155 | NEXT; |
| 10156 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 10157 | while ((CUR >= '0') && (CUR <= '9')) { |
| 10158 | exponent = exponent * 10 + (CUR - '0'); |
| 10159 | NEXT; |
| 10160 | } |
| 10161 | if (is_exponent_negative) |
| 10162 | exponent = -exponent; |
| 10163 | ret *= pow(10.0, (double) exponent); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10164 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10165 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 10166 | xmlXPathCacheNewFloat(ctxt->context, ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10167 | } |
| 10168 | |
| 10169 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10170 | * xmlXPathParseLiteral: |
| 10171 | * @ctxt: the XPath Parser context |
| 10172 | * |
| 10173 | * Parse a Literal |
| 10174 | * |
| 10175 | * [29] Literal ::= '"' [^"]* '"' |
| 10176 | * | "'" [^']* "'" |
| 10177 | * |
| 10178 | * Returns the value found or NULL in case of error |
| 10179 | */ |
| 10180 | static xmlChar * |
| 10181 | xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) { |
| 10182 | const xmlChar *q; |
| 10183 | xmlChar *ret = NULL; |
| 10184 | |
| 10185 | if (CUR == '"') { |
| 10186 | NEXT; |
| 10187 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10188 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10189 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10190 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 10191 | XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10192 | } else { |
| 10193 | ret = xmlStrndup(q, CUR_PTR - q); |
| 10194 | NEXT; |
| 10195 | } |
| 10196 | } else if (CUR == '\'') { |
| 10197 | NEXT; |
| 10198 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10199 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10200 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10201 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 10202 | XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10203 | } else { |
| 10204 | ret = xmlStrndup(q, CUR_PTR - q); |
| 10205 | NEXT; |
| 10206 | } |
| 10207 | } else { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 10208 | XP_ERRORNULL(XPATH_START_LITERAL_ERROR); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10209 | } |
| 10210 | return(ret); |
| 10211 | } |
| 10212 | |
| 10213 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10214 | * xmlXPathCompLiteral: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10215 | * @ctxt: the XPath Parser context |
| 10216 | * |
| 10217 | * Parse a Literal and push it on the stack. |
| 10218 | * |
| 10219 | * [29] Literal ::= '"' [^"]* '"' |
| 10220 | * | "'" [^']* "'" |
| 10221 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10222 | * TODO: xmlXPathCompLiteral memory allocation could be improved. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10223 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10224 | static void |
| 10225 | xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10226 | const xmlChar *q; |
| 10227 | xmlChar *ret = NULL; |
| 10228 | |
| 10229 | if (CUR == '"') { |
| 10230 | NEXT; |
| 10231 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10232 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10233 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10234 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10235 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 10236 | } else { |
| 10237 | ret = xmlStrndup(q, CUR_PTR - q); |
| 10238 | NEXT; |
| 10239 | } |
| 10240 | } else if (CUR == '\'') { |
| 10241 | NEXT; |
| 10242 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10243 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10244 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10245 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10246 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 10247 | } else { |
| 10248 | ret = xmlStrndup(q, CUR_PTR - q); |
| 10249 | NEXT; |
| 10250 | } |
| 10251 | } else { |
| 10252 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
| 10253 | } |
| 10254 | if (ret == NULL) return; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10255 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 10256 | xmlXPathCacheNewString(ctxt->context, ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10257 | xmlFree(ret); |
| 10258 | } |
| 10259 | |
| 10260 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10261 | * xmlXPathCompVariableReference: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10262 | * @ctxt: the XPath Parser context |
| 10263 | * |
| 10264 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 10265 | * |
| 10266 | * The variable bindings consist of a mapping from variable names |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10267 | * 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] | 10268 | * of any of the types that are possible for the value of an expression, |
| 10269 | * and may also be of additional types not specified here. |
| 10270 | * |
| 10271 | * Early evaluation is possible since: |
| 10272 | * The variable bindings [...] used to evaluate a subexpression are |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10273 | * always the same as those used to evaluate the containing expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10274 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10275 | * [36] VariableReference ::= '$' QName |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10276 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10277 | static void |
| 10278 | xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10279 | xmlChar *name; |
| 10280 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10281 | |
| 10282 | SKIP_BLANKS; |
| 10283 | if (CUR != '$') { |
| 10284 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 10285 | } |
| 10286 | NEXT; |
| 10287 | name = xmlXPathParseQName(ctxt, &prefix); |
| 10288 | if (name == NULL) { |
| 10289 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 10290 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10291 | ctxt->comp->last = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10292 | PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0, |
| 10293 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10294 | SKIP_BLANKS; |
Daniel Veillard | b3d1491 | 2005-09-04 20:47:39 +0000 | [diff] [blame] | 10295 | if ((ctxt->context != NULL) && (ctxt->context->flags & XML_XPATH_NOVAR)) { |
| 10296 | XP_ERROR(XPATH_UNDEF_VARIABLE_ERROR); |
| 10297 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10298 | } |
| 10299 | |
| 10300 | /** |
| 10301 | * xmlXPathIsNodeType: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10302 | * @name: a name string |
| 10303 | * |
| 10304 | * Is the name given a NodeType one. |
| 10305 | * |
| 10306 | * [38] NodeType ::= 'comment' |
| 10307 | * | 'text' |
| 10308 | * | 'processing-instruction' |
| 10309 | * | 'node' |
| 10310 | * |
| 10311 | * Returns 1 if true 0 otherwise |
| 10312 | */ |
| 10313 | int |
| 10314 | xmlXPathIsNodeType(const xmlChar *name) { |
| 10315 | if (name == NULL) |
| 10316 | return(0); |
| 10317 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 10318 | if (xmlStrEqual(name, BAD_CAST "node")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10319 | return(1); |
| 10320 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 10321 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 10322 | if (xmlStrEqual(name, BAD_CAST "comment")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10323 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 10324 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10325 | return(1); |
| 10326 | return(0); |
| 10327 | } |
| 10328 | |
| 10329 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10330 | * xmlXPathCompFunctionCall: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10331 | * @ctxt: the XPath Parser context |
| 10332 | * |
| 10333 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10334 | * [17] Argument ::= Expr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10335 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10336 | * Compile a function call, the evaluation of all arguments are |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10337 | * pushed on the stack |
| 10338 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10339 | static void |
| 10340 | xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10341 | xmlChar *name; |
| 10342 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10343 | int nbargs = 0; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10344 | int sort = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10345 | |
| 10346 | name = xmlXPathParseQName(ctxt, &prefix); |
| 10347 | if (name == NULL) { |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 10348 | xmlFree(prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10349 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10350 | } |
| 10351 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10352 | #ifdef DEBUG_EXPR |
| 10353 | if (prefix == NULL) |
| 10354 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 10355 | name); |
| 10356 | else |
| 10357 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 10358 | prefix, name); |
| 10359 | #endif |
| 10360 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10361 | if (CUR != '(') { |
| 10362 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10363 | } |
| 10364 | NEXT; |
| 10365 | SKIP_BLANKS; |
| 10366 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10367 | /* |
| 10368 | * Optimization for count(): we don't need the node-set to be sorted. |
| 10369 | */ |
| 10370 | if ((prefix == NULL) && (name[0] == 'c') && |
| 10371 | xmlStrEqual(name, BAD_CAST "count")) |
| 10372 | { |
| 10373 | sort = 0; |
| 10374 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10375 | ctxt->comp->last = -1; |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 10376 | if (CUR != ')') { |
| 10377 | while (CUR != 0) { |
| 10378 | int op1 = ctxt->comp->last; |
| 10379 | ctxt->comp->last = -1; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10380 | xmlXPathCompileExpr(ctxt, sort); |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 10381 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 10382 | xmlFree(name); |
| 10383 | xmlFree(prefix); |
| 10384 | return; |
| 10385 | } |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 10386 | PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0); |
| 10387 | nbargs++; |
| 10388 | if (CUR == ')') break; |
| 10389 | if (CUR != ',') { |
| 10390 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10391 | } |
| 10392 | NEXT; |
| 10393 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10394 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10395 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10396 | PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0, |
| 10397 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10398 | NEXT; |
| 10399 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10400 | } |
| 10401 | |
| 10402 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10403 | * xmlXPathCompPrimaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10404 | * @ctxt: the XPath Parser context |
| 10405 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10406 | * [15] PrimaryExpr ::= VariableReference |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10407 | * | '(' Expr ')' |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10408 | * | Literal |
| 10409 | * | Number |
| 10410 | * | FunctionCall |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10411 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10412 | * Compile a primary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10413 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10414 | static void |
| 10415 | xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10416 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10417 | if (CUR == '$') xmlXPathCompVariableReference(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10418 | else if (CUR == '(') { |
| 10419 | NEXT; |
| 10420 | SKIP_BLANKS; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10421 | xmlXPathCompileExpr(ctxt, 1); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 10422 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10423 | if (CUR != ')') { |
| 10424 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10425 | } |
| 10426 | NEXT; |
| 10427 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 10428 | } else if (IS_ASCII_DIGIT(CUR) || (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10429 | xmlXPathCompNumber(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10430 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10431 | xmlXPathCompLiteral(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10432 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10433 | xmlXPathCompFunctionCall(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10434 | } |
| 10435 | SKIP_BLANKS; |
| 10436 | } |
| 10437 | |
| 10438 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10439 | * xmlXPathCompFilterExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10440 | * @ctxt: the XPath Parser context |
| 10441 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10442 | * [20] FilterExpr ::= PrimaryExpr |
| 10443 | * | FilterExpr Predicate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10444 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10445 | * Compile a filter expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10446 | * Square brackets are used to filter expressions in the same way that |
| 10447 | * they are used in location paths. It is an error if the expression to |
| 10448 | * be filtered does not evaluate to a node-set. The context node list |
| 10449 | * used for evaluating the expression in square brackets is the node-set |
| 10450 | * to be filtered listed in document order. |
| 10451 | */ |
| 10452 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10453 | static void |
| 10454 | xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) { |
| 10455 | xmlXPathCompPrimaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10456 | CHECK_ERROR; |
| 10457 | SKIP_BLANKS; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10458 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10459 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10460 | xmlXPathCompPredicate(ctxt, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10461 | SKIP_BLANKS; |
| 10462 | } |
| 10463 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10464 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10465 | } |
| 10466 | |
| 10467 | /** |
| 10468 | * xmlXPathScanName: |
| 10469 | * @ctxt: the XPath Parser context |
| 10470 | * |
| 10471 | * Trickery: parse an XML name but without consuming the input flow |
| 10472 | * Needed to avoid insanity in the parser state. |
| 10473 | * |
| 10474 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 10475 | * CombiningChar | Extender |
| 10476 | * |
| 10477 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 10478 | * |
| 10479 | * [6] Names ::= Name (S Name)* |
| 10480 | * |
| 10481 | * Returns the Name parsed or NULL |
| 10482 | */ |
| 10483 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10484 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10485 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10486 | int len = 0, l; |
| 10487 | int c; |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10488 | const xmlChar *cur; |
| 10489 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10490 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10491 | cur = ctxt->cur; |
| 10492 | |
| 10493 | c = CUR_CHAR(l); |
| 10494 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 10495 | (!IS_LETTER(c) && (c != '_') && |
| 10496 | (c != ':'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10497 | return(NULL); |
| 10498 | } |
| 10499 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10500 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 10501 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 10502 | (c == '.') || (c == '-') || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10503 | (c == '_') || (c == ':') || |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10504 | (IS_COMBINING(c)) || |
| 10505 | (IS_EXTENDER(c)))) { |
| 10506 | len += l; |
| 10507 | NEXTL(l); |
| 10508 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10509 | } |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 10510 | ret = xmlStrndup(cur, ctxt->cur - cur); |
| 10511 | ctxt->cur = cur; |
| 10512 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10513 | } |
| 10514 | |
| 10515 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10516 | * xmlXPathCompPathExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10517 | * @ctxt: the XPath Parser context |
| 10518 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10519 | * [19] PathExpr ::= LocationPath |
| 10520 | * | FilterExpr |
| 10521 | * | FilterExpr '/' RelativeLocationPath |
| 10522 | * | FilterExpr '//' RelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10523 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10524 | * Compile a path expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10525 | * The / operator and // operators combine an arbitrary expression |
| 10526 | * and a relative location path. It is an error if the expression |
| 10527 | * does not evaluate to a node-set. |
| 10528 | * The / operator does composition in the same way as when / is |
| 10529 | * used in a location path. As in location paths, // is short for |
| 10530 | * /descendant-or-self::node()/. |
| 10531 | */ |
| 10532 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10533 | static void |
| 10534 | xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10535 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 10536 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 10537 | |
| 10538 | SKIP_BLANKS; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10539 | if ((CUR == '$') || (CUR == '(') || |
| 10540 | (IS_ASCII_DIGIT(CUR)) || |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 10541 | (CUR == '\'') || (CUR == '"') || |
| 10542 | (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10543 | lc = 0; |
| 10544 | } else if (CUR == '*') { |
| 10545 | /* relative or absolute location path */ |
| 10546 | lc = 1; |
| 10547 | } else if (CUR == '/') { |
| 10548 | /* relative or absolute location path */ |
| 10549 | lc = 1; |
| 10550 | } else if (CUR == '@') { |
| 10551 | /* relative abbreviated attribute location path */ |
| 10552 | lc = 1; |
| 10553 | } else if (CUR == '.') { |
| 10554 | /* relative abbreviated attribute location path */ |
| 10555 | lc = 1; |
| 10556 | } else { |
| 10557 | /* |
| 10558 | * Problem is finding if we have a name here whether it's: |
| 10559 | * - a nodetype |
| 10560 | * - a function call in which case it's followed by '(' |
| 10561 | * - an axis in which case it's followed by ':' |
| 10562 | * - a element name |
| 10563 | * We do an a priori analysis here rather than having to |
| 10564 | * maintain parsed token content through the recursive function |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10565 | * calls. This looks uglier but makes the code easier to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10566 | * read/write/debug. |
| 10567 | */ |
| 10568 | SKIP_BLANKS; |
| 10569 | name = xmlXPathScanName(ctxt); |
| 10570 | if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) { |
| 10571 | #ifdef DEBUG_STEP |
| 10572 | xmlGenericError(xmlGenericErrorContext, |
| 10573 | "PathExpr: Axis\n"); |
| 10574 | #endif |
| 10575 | lc = 1; |
| 10576 | xmlFree(name); |
| 10577 | } else if (name != NULL) { |
| 10578 | int len =xmlStrlen(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10579 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10580 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10581 | while (NXT(len) != 0) { |
| 10582 | if (NXT(len) == '/') { |
| 10583 | /* element name */ |
| 10584 | #ifdef DEBUG_STEP |
| 10585 | xmlGenericError(xmlGenericErrorContext, |
| 10586 | "PathExpr: AbbrRelLocation\n"); |
| 10587 | #endif |
| 10588 | lc = 1; |
| 10589 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10590 | } else if (IS_BLANK_CH(NXT(len))) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 10591 | /* ignore blanks */ |
| 10592 | ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10593 | } else if (NXT(len) == ':') { |
| 10594 | #ifdef DEBUG_STEP |
| 10595 | xmlGenericError(xmlGenericErrorContext, |
| 10596 | "PathExpr: AbbrRelLocation\n"); |
| 10597 | #endif |
| 10598 | lc = 1; |
| 10599 | break; |
| 10600 | } else if ((NXT(len) == '(')) { |
| 10601 | /* Note Type or Function */ |
| 10602 | if (xmlXPathIsNodeType(name)) { |
| 10603 | #ifdef DEBUG_STEP |
| 10604 | xmlGenericError(xmlGenericErrorContext, |
| 10605 | "PathExpr: Type search\n"); |
| 10606 | #endif |
| 10607 | lc = 1; |
| 10608 | } else { |
| 10609 | #ifdef DEBUG_STEP |
| 10610 | xmlGenericError(xmlGenericErrorContext, |
| 10611 | "PathExpr: function call\n"); |
| 10612 | #endif |
| 10613 | lc = 0; |
| 10614 | } |
| 10615 | break; |
| 10616 | } else if ((NXT(len) == '[')) { |
| 10617 | /* element name */ |
| 10618 | #ifdef DEBUG_STEP |
| 10619 | xmlGenericError(xmlGenericErrorContext, |
| 10620 | "PathExpr: AbbrRelLocation\n"); |
| 10621 | #endif |
| 10622 | lc = 1; |
| 10623 | break; |
| 10624 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 10625 | (NXT(len) == '=')) { |
| 10626 | lc = 1; |
| 10627 | break; |
| 10628 | } else { |
| 10629 | lc = 1; |
| 10630 | break; |
| 10631 | } |
| 10632 | len++; |
| 10633 | } |
| 10634 | if (NXT(len) == 0) { |
| 10635 | #ifdef DEBUG_STEP |
| 10636 | xmlGenericError(xmlGenericErrorContext, |
| 10637 | "PathExpr: AbbrRelLocation\n"); |
| 10638 | #endif |
| 10639 | /* element name */ |
| 10640 | lc = 1; |
| 10641 | } |
| 10642 | xmlFree(name); |
| 10643 | } else { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10644 | /* make sure all cases are covered explicitly */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10645 | XP_ERROR(XPATH_EXPR_ERROR); |
| 10646 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10647 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10648 | |
| 10649 | if (lc) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10650 | if (CUR == '/') { |
| 10651 | PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0); |
| 10652 | } else { |
| 10653 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10654 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10655 | xmlXPathCompLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10656 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10657 | xmlXPathCompFilterExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10658 | CHECK_ERROR; |
| 10659 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 10660 | SKIP(2); |
| 10661 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10662 | |
| 10663 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 10664 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
| 10665 | PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0); |
| 10666 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10667 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10668 | } else if (CUR == '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10669 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10670 | } |
| 10671 | } |
| 10672 | SKIP_BLANKS; |
| 10673 | } |
| 10674 | |
| 10675 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10676 | * xmlXPathCompUnionExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10677 | * @ctxt: the XPath Parser context |
| 10678 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10679 | * [18] UnionExpr ::= PathExpr |
| 10680 | * | UnionExpr '|' PathExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10681 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10682 | * Compile an union expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10683 | */ |
| 10684 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10685 | static void |
| 10686 | xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 10687 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10688 | CHECK_ERROR; |
| 10689 | SKIP_BLANKS; |
| 10690 | while (CUR == '|') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10691 | int op1 = ctxt->comp->last; |
| 10692 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10693 | |
| 10694 | NEXT; |
| 10695 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10696 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10697 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10698 | PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0); |
| 10699 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10700 | SKIP_BLANKS; |
| 10701 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10702 | } |
| 10703 | |
| 10704 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10705 | * xmlXPathCompUnaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10706 | * @ctxt: the XPath Parser context |
| 10707 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10708 | * [27] UnaryExpr ::= UnionExpr |
| 10709 | * | '-' UnaryExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10710 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10711 | * Compile an unary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10712 | */ |
| 10713 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10714 | static void |
| 10715 | xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10716 | int minus = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10717 | int found = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10718 | |
| 10719 | SKIP_BLANKS; |
Daniel Veillard | 68d7b67 | 2001-03-12 18:22:04 +0000 | [diff] [blame] | 10720 | while (CUR == '-') { |
| 10721 | minus = 1 - minus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10722 | found = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10723 | NEXT; |
| 10724 | SKIP_BLANKS; |
| 10725 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10726 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10727 | xmlXPathCompUnionExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10728 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10729 | if (found) { |
| 10730 | if (minus) |
| 10731 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0); |
| 10732 | else |
| 10733 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10734 | } |
| 10735 | } |
| 10736 | |
| 10737 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10738 | * xmlXPathCompMultiplicativeExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10739 | * @ctxt: the XPath Parser context |
| 10740 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10741 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 10742 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 10743 | * | MultiplicativeExpr 'div' UnaryExpr |
| 10744 | * | MultiplicativeExpr 'mod' UnaryExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10745 | * [34] MultiplyOperator ::= '*' |
| 10746 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10747 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10748 | */ |
| 10749 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10750 | static void |
| 10751 | xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 10752 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10753 | CHECK_ERROR; |
| 10754 | SKIP_BLANKS; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10755 | while ((CUR == '*') || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10756 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 10757 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 10758 | int op = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10759 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10760 | |
| 10761 | if (CUR == '*') { |
| 10762 | op = 0; |
| 10763 | NEXT; |
| 10764 | } else if (CUR == 'd') { |
| 10765 | op = 1; |
| 10766 | SKIP(3); |
| 10767 | } else if (CUR == 'm') { |
| 10768 | op = 2; |
| 10769 | SKIP(3); |
| 10770 | } |
| 10771 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10772 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10773 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10774 | PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10775 | SKIP_BLANKS; |
| 10776 | } |
| 10777 | } |
| 10778 | |
| 10779 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10780 | * xmlXPathCompAdditiveExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10781 | * @ctxt: the XPath Parser context |
| 10782 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10783 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 10784 | * | AdditiveExpr '+' MultiplicativeExpr |
| 10785 | * | AdditiveExpr '-' MultiplicativeExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10786 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10787 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10788 | */ |
| 10789 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10790 | static void |
| 10791 | xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10792 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10793 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10794 | CHECK_ERROR; |
| 10795 | SKIP_BLANKS; |
| 10796 | while ((CUR == '+') || (CUR == '-')) { |
| 10797 | int plus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10798 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10799 | |
| 10800 | if (CUR == '+') plus = 1; |
| 10801 | else plus = 0; |
| 10802 | NEXT; |
| 10803 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10804 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10805 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10806 | PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10807 | SKIP_BLANKS; |
| 10808 | } |
| 10809 | } |
| 10810 | |
| 10811 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10812 | * xmlXPathCompRelationalExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10813 | * @ctxt: the XPath Parser context |
| 10814 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10815 | * [24] RelationalExpr ::= AdditiveExpr |
| 10816 | * | RelationalExpr '<' AdditiveExpr |
| 10817 | * | RelationalExpr '>' AdditiveExpr |
| 10818 | * | RelationalExpr '<=' AdditiveExpr |
| 10819 | * | RelationalExpr '>=' AdditiveExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10820 | * |
| 10821 | * A <= B > C is allowed ? Answer from James, yes with |
| 10822 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 10823 | * which is basically what got implemented. |
| 10824 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10825 | * Compile a Relational expression, then push the result |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10826 | * on the stack |
| 10827 | */ |
| 10828 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10829 | static void |
| 10830 | xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 10831 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10832 | CHECK_ERROR; |
| 10833 | SKIP_BLANKS; |
| 10834 | while ((CUR == '<') || |
| 10835 | (CUR == '>') || |
| 10836 | ((CUR == '<') && (NXT(1) == '=')) || |
| 10837 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10838 | int inf, strict; |
| 10839 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10840 | |
| 10841 | if (CUR == '<') inf = 1; |
| 10842 | else inf = 0; |
| 10843 | if (NXT(1) == '=') strict = 0; |
| 10844 | else strict = 1; |
| 10845 | NEXT; |
| 10846 | if (!strict) NEXT; |
| 10847 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10848 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10849 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10850 | PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10851 | SKIP_BLANKS; |
| 10852 | } |
| 10853 | } |
| 10854 | |
| 10855 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10856 | * xmlXPathCompEqualityExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10857 | * @ctxt: the XPath Parser context |
| 10858 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10859 | * [23] EqualityExpr ::= RelationalExpr |
| 10860 | * | EqualityExpr '=' RelationalExpr |
| 10861 | * | EqualityExpr '!=' RelationalExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10862 | * |
| 10863 | * A != B != C is allowed ? Answer from James, yes with |
| 10864 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 10865 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 10866 | * which is basically what got implemented. |
| 10867 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10868 | * Compile an Equality expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10869 | * |
| 10870 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10871 | static void |
| 10872 | xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 10873 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10874 | CHECK_ERROR; |
| 10875 | SKIP_BLANKS; |
| 10876 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10877 | int eq; |
| 10878 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10879 | |
| 10880 | if (CUR == '=') eq = 1; |
| 10881 | else eq = 0; |
| 10882 | NEXT; |
| 10883 | if (!eq) NEXT; |
| 10884 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10885 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10886 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10887 | PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10888 | SKIP_BLANKS; |
| 10889 | } |
| 10890 | } |
| 10891 | |
| 10892 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10893 | * xmlXPathCompAndExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10894 | * @ctxt: the XPath Parser context |
| 10895 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10896 | * [22] AndExpr ::= EqualityExpr |
| 10897 | * | AndExpr 'and' EqualityExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10898 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10899 | * Compile an AND expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10900 | * |
| 10901 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10902 | static void |
| 10903 | xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) { |
| 10904 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10905 | CHECK_ERROR; |
| 10906 | SKIP_BLANKS; |
| 10907 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10908 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10909 | SKIP(3); |
| 10910 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10911 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10912 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10913 | PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10914 | SKIP_BLANKS; |
| 10915 | } |
| 10916 | } |
| 10917 | |
| 10918 | /** |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 10919 | * xmlXPathCompileExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10920 | * @ctxt: the XPath Parser context |
| 10921 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10922 | * [14] Expr ::= OrExpr |
| 10923 | * [21] OrExpr ::= AndExpr |
| 10924 | * | OrExpr 'or' AndExpr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10925 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10926 | * Parse and compile an expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10927 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10928 | static void |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10929 | xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt, int sort) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10930 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10931 | CHECK_ERROR; |
| 10932 | SKIP_BLANKS; |
| 10933 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10934 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10935 | SKIP(2); |
| 10936 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10937 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10938 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10939 | PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10940 | SKIP_BLANKS; |
| 10941 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10942 | if ((sort) && (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE)) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10943 | /* more ops could be optimized too */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 10944 | /* |
| 10945 | * This is the main place to eliminate sorting for |
| 10946 | * operations which don't require a sorted node-set. |
| 10947 | * E.g. count(). |
| 10948 | */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10949 | PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0); |
| 10950 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10951 | } |
| 10952 | |
| 10953 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10954 | * xmlXPathCompPredicate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10955 | * @ctxt: the XPath Parser context |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10956 | * @filter: act as a filter |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10957 | * |
| 10958 | * [8] Predicate ::= '[' PredicateExpr ']' |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 10959 | * [9] PredicateExpr ::= Expr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10960 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10961 | * Compile a predicate expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10962 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10963 | static void |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10964 | xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10965 | int op1 = ctxt->comp->last; |
| 10966 | |
| 10967 | SKIP_BLANKS; |
| 10968 | if (CUR != '[') { |
| 10969 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 10970 | } |
| 10971 | NEXT; |
| 10972 | SKIP_BLANKS; |
| 10973 | |
| 10974 | ctxt->comp->last = -1; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 10975 | /* |
| 10976 | * This call to xmlXPathCompileExpr() will deactivate sorting |
| 10977 | * of the predicate result. |
| 10978 | * TODO: Sorting is still activated for filters, since I'm not |
| 10979 | * sure if needed. Normally sorting should not be needed, since |
| 10980 | * a filter can only diminish the number of items in a sequence, |
| 10981 | * but won't change its order; so if the initial sequence is sorted, |
| 10982 | * subsequent sorting is not needed. |
| 10983 | */ |
| 10984 | if (! filter) |
| 10985 | xmlXPathCompileExpr(ctxt, 0); |
| 10986 | else |
| 10987 | xmlXPathCompileExpr(ctxt, 1); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10988 | CHECK_ERROR; |
| 10989 | |
| 10990 | if (CUR != ']') { |
| 10991 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 10992 | } |
| 10993 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10994 | if (filter) |
| 10995 | PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0); |
| 10996 | else |
| 10997 | PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10998 | |
| 10999 | NEXT; |
| 11000 | SKIP_BLANKS; |
| 11001 | } |
| 11002 | |
| 11003 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11004 | * xmlXPathCompNodeTest: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11005 | * @ctxt: the XPath Parser context |
| 11006 | * @test: pointer to a xmlXPathTestVal |
| 11007 | * @type: pointer to a xmlXPathTypeVal |
| 11008 | * @prefix: placeholder for a possible name prefix |
| 11009 | * |
| 11010 | * [7] NodeTest ::= NameTest |
| 11011 | * | NodeType '(' ')' |
| 11012 | * | 'processing-instruction' '(' Literal ')' |
| 11013 | * |
| 11014 | * [37] NameTest ::= '*' |
| 11015 | * | NCName ':' '*' |
| 11016 | * | QName |
| 11017 | * [38] NodeType ::= 'comment' |
| 11018 | * | 'text' |
| 11019 | * | 'processing-instruction' |
| 11020 | * | 'node' |
| 11021 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11022 | * Returns the name found and updates @test, @type and @prefix appropriately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11023 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 11024 | static xmlChar * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11025 | xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
| 11026 | xmlXPathTypeVal *type, const xmlChar **prefix, |
| 11027 | xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11028 | int blanks; |
| 11029 | |
| 11030 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 11031 | STRANGE; |
| 11032 | return(NULL); |
| 11033 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 11034 | *type = (xmlXPathTypeVal) 0; |
| 11035 | *test = (xmlXPathTestVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11036 | *prefix = NULL; |
| 11037 | SKIP_BLANKS; |
| 11038 | |
| 11039 | if ((name == NULL) && (CUR == '*')) { |
| 11040 | /* |
| 11041 | * All elements |
| 11042 | */ |
| 11043 | NEXT; |
| 11044 | *test = NODE_TEST_ALL; |
| 11045 | return(NULL); |
| 11046 | } |
| 11047 | |
| 11048 | if (name == NULL) |
| 11049 | name = xmlXPathParseNCName(ctxt); |
| 11050 | if (name == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11051 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11052 | } |
| 11053 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 11054 | blanks = IS_BLANK_CH(CUR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11055 | SKIP_BLANKS; |
| 11056 | if (CUR == '(') { |
| 11057 | NEXT; |
| 11058 | /* |
| 11059 | * NodeType or PI search |
| 11060 | */ |
| 11061 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 11062 | *type = NODE_TYPE_COMMENT; |
| 11063 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 11064 | *type = NODE_TYPE_NODE; |
| 11065 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 11066 | *type = NODE_TYPE_PI; |
| 11067 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 11068 | *type = NODE_TYPE_TEXT; |
| 11069 | else { |
| 11070 | if (name != NULL) |
| 11071 | xmlFree(name); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11072 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11073 | } |
| 11074 | |
| 11075 | *test = NODE_TEST_TYPE; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11076 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11077 | SKIP_BLANKS; |
| 11078 | if (*type == NODE_TYPE_PI) { |
| 11079 | /* |
| 11080 | * Specific case: search a PI by name. |
| 11081 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11082 | if (name != NULL) |
| 11083 | xmlFree(name); |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 11084 | name = NULL; |
| 11085 | if (CUR != ')') { |
| 11086 | name = xmlXPathParseLiteral(ctxt); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11087 | CHECK_ERROR NULL; |
Daniel Veillard | ed23b7d | 2002-05-27 12:16:02 +0000 | [diff] [blame] | 11088 | *test = NODE_TEST_PI; |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 11089 | SKIP_BLANKS; |
| 11090 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11091 | } |
| 11092 | if (CUR != ')') { |
| 11093 | if (name != NULL) |
| 11094 | xmlFree(name); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11095 | XP_ERRORNULL(XPATH_UNCLOSED_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11096 | } |
| 11097 | NEXT; |
| 11098 | return(name); |
| 11099 | } |
| 11100 | *test = NODE_TEST_NAME; |
| 11101 | if ((!blanks) && (CUR == ':')) { |
| 11102 | NEXT; |
| 11103 | |
| 11104 | /* |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11105 | * Since currently the parser context don't have a |
| 11106 | * namespace list associated: |
| 11107 | * The namespace name for this prefix can be computed |
| 11108 | * only at evaluation time. The compilation is done |
| 11109 | * outside of any context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11110 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11111 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11112 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 11113 | if (name != NULL) |
| 11114 | xmlFree(name); |
| 11115 | if (*prefix == NULL) { |
| 11116 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 11117 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11118 | #else |
| 11119 | *prefix = name; |
| 11120 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11121 | |
| 11122 | if (CUR == '*') { |
| 11123 | /* |
| 11124 | * All elements |
| 11125 | */ |
| 11126 | NEXT; |
| 11127 | *test = NODE_TEST_ALL; |
| 11128 | return(NULL); |
| 11129 | } |
| 11130 | |
| 11131 | name = xmlXPathParseNCName(ctxt); |
| 11132 | if (name == NULL) { |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 11133 | XP_ERRORNULL(XPATH_EXPR_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11134 | } |
| 11135 | } |
| 11136 | return(name); |
| 11137 | } |
| 11138 | |
| 11139 | /** |
| 11140 | * xmlXPathIsAxisName: |
| 11141 | * @name: a preparsed name token |
| 11142 | * |
| 11143 | * [6] AxisName ::= 'ancestor' |
| 11144 | * | 'ancestor-or-self' |
| 11145 | * | 'attribute' |
| 11146 | * | 'child' |
| 11147 | * | 'descendant' |
| 11148 | * | 'descendant-or-self' |
| 11149 | * | 'following' |
| 11150 | * | 'following-sibling' |
| 11151 | * | 'namespace' |
| 11152 | * | 'parent' |
| 11153 | * | 'preceding' |
| 11154 | * | 'preceding-sibling' |
| 11155 | * | 'self' |
| 11156 | * |
| 11157 | * Returns the axis or 0 |
| 11158 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 11159 | static xmlXPathAxisVal |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11160 | xmlXPathIsAxisName(const xmlChar *name) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 11161 | xmlXPathAxisVal ret = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11162 | switch (name[0]) { |
| 11163 | case 'a': |
| 11164 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 11165 | ret = AXIS_ANCESTOR; |
| 11166 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 11167 | ret = AXIS_ANCESTOR_OR_SELF; |
| 11168 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 11169 | ret = AXIS_ATTRIBUTE; |
| 11170 | break; |
| 11171 | case 'c': |
| 11172 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 11173 | ret = AXIS_CHILD; |
| 11174 | break; |
| 11175 | case 'd': |
| 11176 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 11177 | ret = AXIS_DESCENDANT; |
| 11178 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 11179 | ret = AXIS_DESCENDANT_OR_SELF; |
| 11180 | break; |
| 11181 | case 'f': |
| 11182 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 11183 | ret = AXIS_FOLLOWING; |
| 11184 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 11185 | ret = AXIS_FOLLOWING_SIBLING; |
| 11186 | break; |
| 11187 | case 'n': |
| 11188 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 11189 | ret = AXIS_NAMESPACE; |
| 11190 | break; |
| 11191 | case 'p': |
| 11192 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 11193 | ret = AXIS_PARENT; |
| 11194 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 11195 | ret = AXIS_PRECEDING; |
| 11196 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 11197 | ret = AXIS_PRECEDING_SIBLING; |
| 11198 | break; |
| 11199 | case 's': |
| 11200 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 11201 | ret = AXIS_SELF; |
| 11202 | break; |
| 11203 | } |
| 11204 | return(ret); |
| 11205 | } |
| 11206 | |
| 11207 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11208 | * xmlXPathCompStep: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11209 | * @ctxt: the XPath Parser context |
| 11210 | * |
| 11211 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11212 | * | AbbreviatedStep |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11213 | * |
| 11214 | * [12] AbbreviatedStep ::= '.' | '..' |
| 11215 | * |
| 11216 | * [5] AxisSpecifier ::= AxisName '::' |
| 11217 | * | AbbreviatedAxisSpecifier |
| 11218 | * |
| 11219 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
| 11220 | * |
| 11221 | * Modified for XPtr range support as: |
| 11222 | * |
| 11223 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 11224 | * | AbbreviatedStep |
| 11225 | * | 'range-to' '(' Expr ')' Predicate* |
| 11226 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11227 | * Compile one step in a Location Path |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11228 | * A location step of . is short for self::node(). This is |
| 11229 | * particularly useful in conjunction with //. For example, the |
| 11230 | * location path .//para is short for |
| 11231 | * self::node()/descendant-or-self::node()/child::para |
| 11232 | * and so will select all para descendant elements of the context |
| 11233 | * node. |
| 11234 | * Similarly, a location step of .. is short for parent::node(). |
| 11235 | * For example, ../title is short for parent::node()/child::title |
| 11236 | * and so will select the title children of the parent of the context |
| 11237 | * node. |
| 11238 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11239 | static void |
| 11240 | xmlXPathCompStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11241 | #ifdef LIBXML_XPTR_ENABLED |
| 11242 | int rangeto = 0; |
| 11243 | int op2 = -1; |
| 11244 | #endif |
| 11245 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11246 | SKIP_BLANKS; |
| 11247 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 11248 | SKIP(2); |
| 11249 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11250 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT, |
| 11251 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11252 | } else if (CUR == '.') { |
| 11253 | NEXT; |
| 11254 | SKIP_BLANKS; |
| 11255 | } else { |
| 11256 | xmlChar *name = NULL; |
| 11257 | const xmlChar *prefix = NULL; |
Daniel Veillard | aac7c68 | 2006-03-10 13:40:16 +0000 | [diff] [blame] | 11258 | xmlXPathTestVal test = (xmlXPathTestVal) 0; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 11259 | xmlXPathAxisVal axis = (xmlXPathAxisVal) 0; |
Daniel Veillard | aac7c68 | 2006-03-10 13:40:16 +0000 | [diff] [blame] | 11260 | xmlXPathTypeVal type = (xmlXPathTypeVal) 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11261 | int op1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11262 | |
| 11263 | /* |
| 11264 | * The modification needed for XPointer change to the production |
| 11265 | */ |
| 11266 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 11267 | if (ctxt->xptr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11268 | name = xmlXPathParseNCName(ctxt); |
| 11269 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11270 | op2 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11271 | xmlFree(name); |
| 11272 | SKIP_BLANKS; |
| 11273 | if (CUR != '(') { |
| 11274 | XP_ERROR(XPATH_EXPR_ERROR); |
| 11275 | } |
| 11276 | NEXT; |
| 11277 | SKIP_BLANKS; |
| 11278 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 11279 | xmlXPathCompileExpr(ctxt, 1); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11280 | /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11281 | CHECK_ERROR; |
| 11282 | |
| 11283 | SKIP_BLANKS; |
| 11284 | if (CUR != ')') { |
| 11285 | XP_ERROR(XPATH_EXPR_ERROR); |
| 11286 | } |
| 11287 | NEXT; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11288 | rangeto = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11289 | goto eval_predicates; |
| 11290 | } |
| 11291 | } |
| 11292 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 11293 | if (CUR == '*') { |
| 11294 | axis = AXIS_CHILD; |
| 11295 | } else { |
| 11296 | if (name == NULL) |
| 11297 | name = xmlXPathParseNCName(ctxt); |
| 11298 | if (name != NULL) { |
| 11299 | axis = xmlXPathIsAxisName(name); |
| 11300 | if (axis != 0) { |
| 11301 | SKIP_BLANKS; |
| 11302 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 11303 | SKIP(2); |
| 11304 | xmlFree(name); |
| 11305 | name = NULL; |
| 11306 | } else { |
| 11307 | /* an element name can conflict with an axis one :-\ */ |
| 11308 | axis = AXIS_CHILD; |
| 11309 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11310 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11311 | axis = AXIS_CHILD; |
| 11312 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 11313 | } else if (CUR == '@') { |
| 11314 | NEXT; |
| 11315 | axis = AXIS_ATTRIBUTE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11316 | } else { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 11317 | axis = AXIS_CHILD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11318 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11319 | } |
| 11320 | |
Daniel Veillard | 2f3523f | 2010-10-15 18:30:29 +0200 | [diff] [blame] | 11321 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11322 | xmlFree(name); |
| 11323 | return; |
| 11324 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11325 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11326 | name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11327 | if (test == 0) |
| 11328 | return; |
| 11329 | |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 11330 | if ((prefix != NULL) && (ctxt->context != NULL) && |
| 11331 | (ctxt->context->flags & XML_XPATH_CHECKNS)) { |
| 11332 | if (xmlXPathNsLookup(ctxt->context, prefix) == NULL) { |
| 11333 | xmlXPathErr(ctxt, XPATH_UNDEF_PREFIX_ERROR); |
| 11334 | } |
| 11335 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11336 | #ifdef DEBUG_STEP |
| 11337 | xmlGenericError(xmlGenericErrorContext, |
| 11338 | "Basis : computing new set\n"); |
| 11339 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11340 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11341 | #ifdef DEBUG_STEP |
| 11342 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11343 | if (ctxt->value == NULL) |
| 11344 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 11345 | else if (ctxt->value->nodesetval == NULL) |
| 11346 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 11347 | else |
| 11348 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11349 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11350 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 11351 | #ifdef LIBXML_XPTR_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11352 | eval_predicates: |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 11353 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11354 | op1 = ctxt->comp->last; |
| 11355 | ctxt->comp->last = -1; |
| 11356 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11357 | SKIP_BLANKS; |
| 11358 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11359 | xmlXPathCompPredicate(ctxt, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11360 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11361 | |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11362 | #ifdef LIBXML_XPTR_ENABLED |
| 11363 | if (rangeto) { |
| 11364 | PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0); |
| 11365 | } else |
| 11366 | #endif |
| 11367 | PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis, |
| 11368 | test, type, (void *)prefix, (void *)name); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11369 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11370 | } |
| 11371 | #ifdef DEBUG_STEP |
| 11372 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 11373 | if (ctxt->value == NULL) |
| 11374 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 11375 | else if (ctxt->value->nodesetval == NULL) |
| 11376 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 11377 | else |
| 11378 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 11379 | ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11380 | #endif |
| 11381 | } |
| 11382 | |
| 11383 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11384 | * xmlXPathCompRelativeLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11385 | * @ctxt: the XPath Parser context |
| 11386 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11387 | * [3] RelativeLocationPath ::= Step |
| 11388 | * | RelativeLocationPath '/' Step |
| 11389 | * | AbbreviatedRelativeLocationPath |
| 11390 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11391 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11392 | * Compile a relative location path. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11393 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11394 | static void |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11395 | xmlXPathCompRelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11396 | (xmlXPathParserContextPtr ctxt) { |
| 11397 | SKIP_BLANKS; |
| 11398 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 11399 | SKIP(2); |
| 11400 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11401 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 11402 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11403 | } else if (CUR == '/') { |
| 11404 | NEXT; |
| 11405 | SKIP_BLANKS; |
| 11406 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11407 | xmlXPathCompStep(ctxt); |
Martin | 729601f | 2009-10-12 22:42:26 +0200 | [diff] [blame] | 11408 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11409 | SKIP_BLANKS; |
| 11410 | while (CUR == '/') { |
| 11411 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 11412 | SKIP(2); |
| 11413 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11414 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11415 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11416 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11417 | } else if (CUR == '/') { |
| 11418 | NEXT; |
| 11419 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11420 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11421 | } |
| 11422 | SKIP_BLANKS; |
| 11423 | } |
| 11424 | } |
| 11425 | |
| 11426 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11427 | * xmlXPathCompLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11428 | * @ctxt: the XPath Parser context |
| 11429 | * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11430 | * [1] LocationPath ::= RelativeLocationPath |
| 11431 | * | AbsoluteLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11432 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11433 | * | AbbreviatedAbsoluteLocationPath |
| 11434 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 11435 | * '//' RelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11436 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11437 | * Compile a location path |
| 11438 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11439 | * // is short for /descendant-or-self::node()/. For example, |
| 11440 | * //para is short for /descendant-or-self::node()/child::para and |
| 11441 | * so will select any para element in the document (even a para element |
| 11442 | * that is a document element will be selected by //para since the |
| 11443 | * document element node is a child of the root node); div//para is |
| 11444 | * short for div/descendant-or-self::node()/child::para and so will |
| 11445 | * select all para descendants of div children. |
| 11446 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11447 | static void |
| 11448 | xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11449 | SKIP_BLANKS; |
| 11450 | if (CUR != '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11451 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11452 | } else { |
| 11453 | while (CUR == '/') { |
| 11454 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 11455 | SKIP(2); |
| 11456 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11457 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 11458 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11459 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11460 | } else if (CUR == '/') { |
| 11461 | NEXT; |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 11462 | SKIP_BLANKS; |
| 11463 | if ((CUR != 0 ) && |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 11464 | ((IS_ASCII_LETTER(CUR)) || (CUR == '_') || (CUR == '.') || |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 11465 | (CUR == '@') || (CUR == '*'))) |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11466 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11467 | } |
Martin | 729601f | 2009-10-12 22:42:26 +0200 | [diff] [blame] | 11468 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11469 | } |
| 11470 | } |
| 11471 | } |
| 11472 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11473 | /************************************************************************ |
| 11474 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11475 | * XPath precompiled expression evaluation * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11476 | * * |
| 11477 | ************************************************************************/ |
| 11478 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11479 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11480 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op); |
| 11481 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11482 | #ifdef DEBUG_STEP |
| 11483 | static void |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11484 | xmlXPathDebugDumpStepAxis(xmlXPathStepOpPtr op, |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11485 | int nbNodes) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11486 | { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11487 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11488 | switch (op->value) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11489 | case AXIS_ANCESTOR: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11490 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11491 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11492 | case AXIS_ANCESTOR_OR_SELF: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11493 | xmlGenericError(xmlGenericErrorContext, |
| 11494 | "axis 'ancestors-or-self' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11495 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11496 | case AXIS_ATTRIBUTE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11497 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11498 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11499 | case AXIS_CHILD: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11500 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11501 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11502 | case AXIS_DESCENDANT: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11503 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11504 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11505 | case AXIS_DESCENDANT_OR_SELF: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11506 | xmlGenericError(xmlGenericErrorContext, |
| 11507 | "axis 'descendant-or-self' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11508 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11509 | case AXIS_FOLLOWING: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11510 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11511 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11512 | case AXIS_FOLLOWING_SIBLING: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11513 | xmlGenericError(xmlGenericErrorContext, |
| 11514 | "axis 'following-siblings' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11515 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11516 | case AXIS_NAMESPACE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11517 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11518 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11519 | case AXIS_PARENT: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11520 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11521 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11522 | case AXIS_PRECEDING: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11523 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11524 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11525 | case AXIS_PRECEDING_SIBLING: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11526 | xmlGenericError(xmlGenericErrorContext, |
| 11527 | "axis 'preceding-sibling' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11528 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11529 | case AXIS_SELF: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11530 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11531 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11532 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11533 | xmlGenericError(xmlGenericErrorContext, |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11534 | " context contains %d nodes\n", nbNodes); |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11535 | switch (op->value2) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11536 | case NODE_TEST_NONE: |
| 11537 | xmlGenericError(xmlGenericErrorContext, |
| 11538 | " searching for none !!!\n"); |
| 11539 | break; |
| 11540 | case NODE_TEST_TYPE: |
| 11541 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11542 | " searching for type %d\n", op->value3); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11543 | break; |
| 11544 | case NODE_TEST_PI: |
| 11545 | xmlGenericError(xmlGenericErrorContext, |
| 11546 | " searching for PI !!!\n"); |
| 11547 | break; |
| 11548 | case NODE_TEST_ALL: |
| 11549 | xmlGenericError(xmlGenericErrorContext, |
| 11550 | " searching for *\n"); |
| 11551 | break; |
| 11552 | case NODE_TEST_NS: |
| 11553 | xmlGenericError(xmlGenericErrorContext, |
| 11554 | " searching for namespace %s\n", |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11555 | op->value5); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11556 | break; |
| 11557 | case NODE_TEST_NAME: |
| 11558 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11559 | " searching for name %s\n", op->value5); |
| 11560 | if (op->value4) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11561 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 11562 | " with namespace %s\n", op->value4); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11563 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11564 | } |
| 11565 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11566 | } |
| 11567 | #endif /* DEBUG_STEP */ |
| 11568 | |
| 11569 | static int |
| 11570 | xmlXPathCompOpEvalPredicate(xmlXPathParserContextPtr ctxt, |
| 11571 | xmlXPathStepOpPtr op, |
| 11572 | xmlNodeSetPtr set, |
| 11573 | int contextSize, |
| 11574 | int hasNsNodes) |
| 11575 | { |
| 11576 | if (op->ch1 != -1) { |
| 11577 | xmlXPathCompExprPtr comp = ctxt->comp; |
| 11578 | /* |
| 11579 | * Process inner predicates first. |
| 11580 | */ |
| 11581 | if (comp->steps[op->ch1].op != XPATH_OP_PREDICATE) { |
| 11582 | /* |
| 11583 | * TODO: raise an internal error. |
| 11584 | */ |
| 11585 | } |
| 11586 | contextSize = xmlXPathCompOpEvalPredicate(ctxt, |
| 11587 | &comp->steps[op->ch1], set, contextSize, hasNsNodes); |
| 11588 | CHECK_ERROR0; |
| 11589 | if (contextSize <= 0) |
| 11590 | return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11591 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11592 | if (op->ch2 != -1) { |
| 11593 | xmlXPathContextPtr xpctxt = ctxt->context; |
| 11594 | xmlNodePtr contextNode, oldContextNode; |
| 11595 | xmlDocPtr oldContextDoc; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11596 | int i, res, contextPos = 0, newContextSize; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11597 | xmlXPathStepOpPtr exprOp; |
| 11598 | xmlXPathObjectPtr contextObj = NULL, exprRes = NULL; |
| 11599 | |
| 11600 | #ifdef LIBXML_XPTR_ENABLED |
| 11601 | /* |
| 11602 | * URGENT TODO: Check the following: |
| 11603 | * We don't expect location sets if evaluating prediates, right? |
| 11604 | * Only filters should expect location sets, right? |
| 11605 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 11606 | #endif |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11607 | /* |
| 11608 | * SPEC XPath 1.0: |
| 11609 | * "For each node in the node-set to be filtered, the |
| 11610 | * PredicateExpr is evaluated with that node as the |
| 11611 | * context node, with the number of nodes in the |
| 11612 | * node-set as the context size, and with the proximity |
| 11613 | * position of the node in the node-set with respect to |
| 11614 | * the axis as the context position;" |
| 11615 | * @oldset is the node-set" to be filtered. |
| 11616 | * |
| 11617 | * SPEC XPath 1.0: |
| 11618 | * "only predicates change the context position and |
| 11619 | * context size (see [2.4 Predicates])." |
| 11620 | * Example: |
| 11621 | * node-set context pos |
| 11622 | * nA 1 |
| 11623 | * nB 2 |
| 11624 | * nC 3 |
| 11625 | * After applying predicate [position() > 1] : |
| 11626 | * node-set context pos |
| 11627 | * nB 1 |
| 11628 | * nC 2 |
| 11629 | */ |
| 11630 | oldContextNode = xpctxt->node; |
| 11631 | oldContextDoc = xpctxt->doc; |
| 11632 | /* |
| 11633 | * Get the expression of this predicate. |
| 11634 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11635 | exprOp = &ctxt->comp->steps[op->ch2]; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11636 | newContextSize = 0; |
| 11637 | for (i = 0; i < set->nodeNr; i++) { |
| 11638 | if (set->nodeTab[i] == NULL) |
| 11639 | continue; |
| 11640 | |
| 11641 | contextNode = set->nodeTab[i]; |
| 11642 | xpctxt->node = contextNode; |
| 11643 | xpctxt->contextSize = contextSize; |
| 11644 | xpctxt->proximityPosition = ++contextPos; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11645 | |
| 11646 | /* |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11647 | * Also set the xpath document in case things like |
| 11648 | * key() are evaluated in the predicate. |
| 11649 | */ |
| 11650 | if ((contextNode->type != XML_NAMESPACE_DECL) && |
| 11651 | (contextNode->doc != NULL)) |
| 11652 | xpctxt->doc = contextNode->doc; |
| 11653 | /* |
| 11654 | * Evaluate the predicate expression with 1 context node |
| 11655 | * at a time; this node is packaged into a node set; this |
| 11656 | * node set is handed over to the evaluation mechanism. |
| 11657 | */ |
| 11658 | if (contextObj == NULL) |
| 11659 | contextObj = xmlXPathCacheNewNodeSet(xpctxt, contextNode); |
| 11660 | else |
| 11661 | xmlXPathNodeSetAddUnique(contextObj->nodesetval, |
| 11662 | contextNode); |
| 11663 | |
| 11664 | valuePush(ctxt, contextObj); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11665 | |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 11666 | res = xmlXPathCompOpEvalToBoolean(ctxt, exprOp, 1); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11667 | |
William M. Brack | 0bcec06 | 2007-02-14 02:15:19 +0000 | [diff] [blame] | 11668 | if ((ctxt->error != XPATH_EXPRESSION_OK) || (res == -1)) { |
| 11669 | xmlXPathNodeSetClear(set, hasNsNodes); |
| 11670 | newContextSize = 0; |
| 11671 | goto evaluation_exit; |
| 11672 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11673 | |
| 11674 | if (res != 0) { |
| 11675 | newContextSize++; |
| 11676 | } else { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11677 | /* |
| 11678 | * Remove the entry from the initial node set. |
| 11679 | */ |
| 11680 | set->nodeTab[i] = NULL; |
| 11681 | if (contextNode->type == XML_NAMESPACE_DECL) |
| 11682 | xmlXPathNodeSetFreeNs((xmlNsPtr) contextNode); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11683 | } |
| 11684 | if (ctxt->value == contextObj) { |
| 11685 | /* |
| 11686 | * Don't free the temporary XPath object holding the |
| 11687 | * context node, in order to avoid massive recreation |
| 11688 | * inside this loop. |
| 11689 | */ |
| 11690 | valuePop(ctxt); |
| 11691 | xmlXPathNodeSetClear(contextObj->nodesetval, hasNsNodes); |
| 11692 | } else { |
| 11693 | /* |
| 11694 | * TODO: The object was lost in the evaluation machinery. |
| 11695 | * Can this happen? Maybe in internal-error cases. |
| 11696 | */ |
| 11697 | contextObj = NULL; |
| 11698 | } |
| 11699 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11700 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11701 | if (contextObj != NULL) { |
| 11702 | if (ctxt->value == contextObj) |
| 11703 | valuePop(ctxt); |
| 11704 | xmlXPathReleaseObject(xpctxt, contextObj); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11705 | } |
William M. Brack | 0bcec06 | 2007-02-14 02:15:19 +0000 | [diff] [blame] | 11706 | evaluation_exit: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11707 | if (exprRes != NULL) |
| 11708 | xmlXPathReleaseObject(ctxt->context, exprRes); |
| 11709 | /* |
| 11710 | * Reset/invalidate the context. |
| 11711 | */ |
| 11712 | xpctxt->node = oldContextNode; |
| 11713 | xpctxt->doc = oldContextDoc; |
| 11714 | xpctxt->contextSize = -1; |
| 11715 | xpctxt->proximityPosition = -1; |
| 11716 | return(newContextSize); |
| 11717 | } |
| 11718 | return(contextSize); |
| 11719 | } |
| 11720 | |
| 11721 | static int |
| 11722 | xmlXPathCompOpEvalPositionalPredicate(xmlXPathParserContextPtr ctxt, |
| 11723 | xmlXPathStepOpPtr op, |
| 11724 | xmlNodeSetPtr set, |
| 11725 | int contextSize, |
| 11726 | int minPos, |
| 11727 | int maxPos, |
| 11728 | int hasNsNodes) |
| 11729 | { |
| 11730 | if (op->ch1 != -1) { |
| 11731 | xmlXPathCompExprPtr comp = ctxt->comp; |
| 11732 | if (comp->steps[op->ch1].op != XPATH_OP_PREDICATE) { |
| 11733 | /* |
| 11734 | * TODO: raise an internal error. |
| 11735 | */ |
| 11736 | } |
| 11737 | contextSize = xmlXPathCompOpEvalPredicate(ctxt, |
| 11738 | &comp->steps[op->ch1], set, contextSize, hasNsNodes); |
| 11739 | CHECK_ERROR0; |
| 11740 | if (contextSize <= 0) |
| 11741 | return(0); |
| 11742 | } |
| 11743 | /* |
| 11744 | * Check if the node set contains a sufficient number of nodes for |
| 11745 | * the requested range. |
| 11746 | */ |
| 11747 | if (contextSize < minPos) { |
| 11748 | xmlXPathNodeSetClear(set, hasNsNodes); |
| 11749 | return(0); |
| 11750 | } |
| 11751 | if (op->ch2 == -1) { |
| 11752 | /* |
| 11753 | * TODO: Can this ever happen? |
| 11754 | */ |
| 11755 | return (contextSize); |
| 11756 | } else { |
| 11757 | xmlDocPtr oldContextDoc; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11758 | int i, pos = 0, newContextSize = 0, contextPos = 0, res; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11759 | xmlXPathStepOpPtr exprOp; |
| 11760 | xmlXPathObjectPtr contextObj = NULL, exprRes = NULL; |
| 11761 | xmlNodePtr oldContextNode, contextNode = NULL; |
| 11762 | xmlXPathContextPtr xpctxt = ctxt->context; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11763 | int frame; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11764 | |
| 11765 | #ifdef LIBXML_XPTR_ENABLED |
| 11766 | /* |
| 11767 | * URGENT TODO: Check the following: |
| 11768 | * We don't expect location sets if evaluating prediates, right? |
| 11769 | * Only filters should expect location sets, right? |
| 11770 | */ |
| 11771 | #endif /* LIBXML_XPTR_ENABLED */ |
| 11772 | |
| 11773 | /* |
| 11774 | * Save old context. |
| 11775 | */ |
| 11776 | oldContextNode = xpctxt->node; |
| 11777 | oldContextDoc = xpctxt->doc; |
| 11778 | /* |
| 11779 | * Get the expression of this predicate. |
| 11780 | */ |
| 11781 | exprOp = &ctxt->comp->steps[op->ch2]; |
| 11782 | for (i = 0; i < set->nodeNr; i++) { |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11783 | xmlXPathObjectPtr tmp; |
| 11784 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11785 | if (set->nodeTab[i] == NULL) |
| 11786 | continue; |
| 11787 | |
| 11788 | contextNode = set->nodeTab[i]; |
| 11789 | xpctxt->node = contextNode; |
| 11790 | xpctxt->contextSize = contextSize; |
| 11791 | xpctxt->proximityPosition = ++contextPos; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11792 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11793 | /* |
| 11794 | * Initialize the new set. |
| 11795 | * Also set the xpath document in case things like |
| 11796 | * key() evaluation are attempted on the predicate |
| 11797 | */ |
| 11798 | if ((contextNode->type != XML_NAMESPACE_DECL) && |
| 11799 | (contextNode->doc != NULL)) |
| 11800 | xpctxt->doc = contextNode->doc; |
| 11801 | /* |
| 11802 | * Evaluate the predicate expression with 1 context node |
| 11803 | * at a time; this node is packaged into a node set; this |
| 11804 | * node set is handed over to the evaluation mechanism. |
| 11805 | */ |
| 11806 | if (contextObj == NULL) |
| 11807 | contextObj = xmlXPathCacheNewNodeSet(xpctxt, contextNode); |
| 11808 | else |
| 11809 | xmlXPathNodeSetAddUnique(contextObj->nodesetval, |
| 11810 | contextNode); |
| 11811 | |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11812 | frame = xmlXPathSetFrame(ctxt); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11813 | valuePush(ctxt, contextObj); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 11814 | res = xmlXPathCompOpEvalToBoolean(ctxt, exprOp, 1); |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11815 | tmp = valuePop(ctxt); |
| 11816 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11817 | |
William M. Brack | f179456 | 2007-08-23 12:58:13 +0000 | [diff] [blame] | 11818 | if ((ctxt->error != XPATH_EXPRESSION_OK) || (res == -1)) { |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11819 | while (tmp != contextObj) { |
Daniel Veillard | df83c17 | 2010-11-17 14:12:14 +0100 | [diff] [blame] | 11820 | /* |
| 11821 | * Free up the result |
| 11822 | * then pop off contextObj, which will be freed later |
| 11823 | */ |
| 11824 | xmlXPathReleaseObject(xpctxt, tmp); |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11825 | tmp = valuePop(ctxt); |
Daniel Veillard | fec31bc | 2010-11-18 11:07:24 +0100 | [diff] [blame] | 11826 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11827 | goto evaluation_error; |
William M. Brack | f179456 | 2007-08-23 12:58:13 +0000 | [diff] [blame] | 11828 | } |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 11829 | /* push the result back onto the stack */ |
| 11830 | valuePush(ctxt, tmp); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11831 | |
| 11832 | if (res) |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11833 | pos++; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11834 | |
| 11835 | if (res && (pos >= minPos) && (pos <= maxPos)) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11836 | /* |
| 11837 | * Fits in the requested range. |
| 11838 | */ |
| 11839 | newContextSize++; |
| 11840 | if (minPos == maxPos) { |
| 11841 | /* |
| 11842 | * Only 1 node was requested. |
| 11843 | */ |
| 11844 | if (contextNode->type == XML_NAMESPACE_DECL) { |
| 11845 | /* |
| 11846 | * As always: take care of those nasty |
| 11847 | * namespace nodes. |
| 11848 | */ |
| 11849 | set->nodeTab[i] = NULL; |
| 11850 | } |
| 11851 | xmlXPathNodeSetClear(set, hasNsNodes); |
| 11852 | set->nodeNr = 1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11853 | set->nodeTab[0] = contextNode; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11854 | goto evaluation_exit; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11855 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11856 | if (pos == maxPos) { |
| 11857 | /* |
| 11858 | * We are done. |
| 11859 | */ |
| 11860 | xmlXPathNodeSetClearFromPos(set, i +1, hasNsNodes); |
| 11861 | goto evaluation_exit; |
| 11862 | } |
| 11863 | } else { |
| 11864 | /* |
| 11865 | * Remove the entry from the initial node set. |
| 11866 | */ |
| 11867 | set->nodeTab[i] = NULL; |
| 11868 | if (contextNode->type == XML_NAMESPACE_DECL) |
| 11869 | xmlXPathNodeSetFreeNs((xmlNsPtr) contextNode); |
| 11870 | } |
| 11871 | if (exprRes != NULL) { |
| 11872 | xmlXPathReleaseObject(ctxt->context, exprRes); |
| 11873 | exprRes = NULL; |
| 11874 | } |
| 11875 | if (ctxt->value == contextObj) { |
| 11876 | /* |
| 11877 | * Don't free the temporary XPath object holding the |
| 11878 | * context node, in order to avoid massive recreation |
| 11879 | * inside this loop. |
| 11880 | */ |
| 11881 | valuePop(ctxt); |
| 11882 | xmlXPathNodeSetClear(contextObj->nodesetval, hasNsNodes); |
| 11883 | } else { |
| 11884 | /* |
| 11885 | * The object was lost in the evaluation machinery. |
| 11886 | * Can this happen? Maybe in case of internal-errors. |
| 11887 | */ |
| 11888 | contextObj = NULL; |
| 11889 | } |
| 11890 | } |
| 11891 | goto evaluation_exit; |
| 11892 | |
| 11893 | evaluation_error: |
| 11894 | xmlXPathNodeSetClear(set, hasNsNodes); |
| 11895 | newContextSize = 0; |
| 11896 | |
| 11897 | evaluation_exit: |
| 11898 | if (contextObj != NULL) { |
| 11899 | if (ctxt->value == contextObj) |
| 11900 | valuePop(ctxt); |
| 11901 | xmlXPathReleaseObject(xpctxt, contextObj); |
| 11902 | } |
| 11903 | if (exprRes != NULL) |
| 11904 | xmlXPathReleaseObject(ctxt->context, exprRes); |
| 11905 | /* |
| 11906 | * Reset/invalidate the context. |
| 11907 | */ |
| 11908 | xpctxt->node = oldContextNode; |
| 11909 | xpctxt->doc = oldContextDoc; |
| 11910 | xpctxt->contextSize = -1; |
| 11911 | xpctxt->proximityPosition = -1; |
| 11912 | return(newContextSize); |
| 11913 | } |
| 11914 | return(contextSize); |
| 11915 | } |
| 11916 | |
| 11917 | static int |
| 11918 | xmlXPathIsPositionalPredicate(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11919 | xmlXPathStepOpPtr op, |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11920 | int *maxPos) |
| 11921 | { |
| 11922 | |
| 11923 | xmlXPathStepOpPtr exprOp; |
| 11924 | |
| 11925 | /* |
| 11926 | * BIG NOTE: This is not intended for XPATH_OP_FILTER yet! |
| 11927 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11928 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11929 | /* |
| 11930 | * If not -1, then ch1 will point to: |
| 11931 | * 1) For predicates (XPATH_OP_PREDICATE): |
| 11932 | * - an inner predicate operator |
| 11933 | * 2) For filters (XPATH_OP_FILTER): |
| 11934 | * - an inner filter operater OR |
| 11935 | * - an expression selecting the node set. |
| 11936 | * E.g. "key('a', 'b')" or "(//foo | //bar)". |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11937 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11938 | if ((op->op != XPATH_OP_PREDICATE) && (op->op != XPATH_OP_FILTER)) |
| 11939 | return(0); |
| 11940 | |
| 11941 | if (op->ch2 != -1) { |
| 11942 | exprOp = &ctxt->comp->steps[op->ch2]; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11943 | } else |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11944 | return(0); |
| 11945 | |
| 11946 | if ((exprOp != NULL) && |
| 11947 | (exprOp->op == XPATH_OP_VALUE) && |
| 11948 | (exprOp->value4 != NULL) && |
| 11949 | (((xmlXPathObjectPtr) exprOp->value4)->type == XPATH_NUMBER)) |
| 11950 | { |
| 11951 | /* |
| 11952 | * We have a "[n]" predicate here. |
| 11953 | * TODO: Unfortunately this simplistic test here is not |
| 11954 | * able to detect a position() predicate in compound |
| 11955 | * expressions like "[@attr = 'a" and position() = 1], |
| 11956 | * and even not the usage of position() in |
| 11957 | * "[position() = 1]"; thus - obviously - a position-range, |
| 11958 | * like it "[position() < 5]", is also not detected. |
| 11959 | * Maybe we could rewrite the AST to ease the optimization. |
| 11960 | */ |
| 11961 | *maxPos = (int) ((xmlXPathObjectPtr) exprOp->value4)->floatval; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11962 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11963 | if (((xmlXPathObjectPtr) exprOp->value4)->floatval == |
| 11964 | (float) *maxPos) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 11965 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11966 | return(1); |
| 11967 | } |
| 11968 | } |
| 11969 | return(0); |
| 11970 | } |
| 11971 | |
| 11972 | static int |
| 11973 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, |
| 11974 | xmlXPathStepOpPtr op, |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11975 | xmlNodePtr * first, xmlNodePtr * last, |
| 11976 | int toBool) |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11977 | { |
| 11978 | |
| 11979 | #define XP_TEST_HIT \ |
| 11980 | if (hasAxisRange != 0) { \ |
| 11981 | if (++pos == maxPos) { \ |
| 11982 | addNode(seq, cur); \ |
| 11983 | goto axis_range_end; } \ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11984 | } else { \ |
| 11985 | addNode(seq, cur); \ |
| 11986 | if (breakOnFirstHit) goto first_hit; } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11987 | |
| 11988 | #define XP_TEST_HIT_NS \ |
| 11989 | if (hasAxisRange != 0) { \ |
| 11990 | if (++pos == maxPos) { \ |
| 11991 | hasNsNodes = 1; \ |
| 11992 | xmlXPathNodeSetAddNs(seq, xpctxt->node, (xmlNsPtr) cur); \ |
| 11993 | goto axis_range_end; } \ |
| 11994 | } else { \ |
| 11995 | hasNsNodes = 1; \ |
| 11996 | xmlXPathNodeSetAddNs(seq, \ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 11997 | xpctxt->node, (xmlNsPtr) cur); \ |
| 11998 | if (breakOnFirstHit) goto first_hit; } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 11999 | |
| 12000 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 12001 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 12002 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
| 12003 | const xmlChar *prefix = op->value4; |
| 12004 | const xmlChar *name = op->value5; |
| 12005 | const xmlChar *URI = NULL; |
| 12006 | |
| 12007 | #ifdef DEBUG_STEP |
| 12008 | int nbMatches = 0, prevMatches = 0; |
| 12009 | #endif |
| 12010 | int total = 0, hasNsNodes = 0; |
| 12011 | /* The popped object holding the context nodes */ |
| 12012 | xmlXPathObjectPtr obj; |
| 12013 | /* The set of context nodes for the node tests */ |
| 12014 | xmlNodeSetPtr contextSeq; |
| 12015 | int contextIdx; |
| 12016 | xmlNodePtr contextNode; |
| 12017 | /* The context node for a compound traversal */ |
| 12018 | xmlNodePtr outerContextNode; |
| 12019 | /* The final resulting node set wrt to all context nodes */ |
| 12020 | xmlNodeSetPtr outSeq; |
| 12021 | /* |
| 12022 | * The temporary resulting node set wrt 1 context node. |
| 12023 | * Used to feed predicate evaluation. |
| 12024 | */ |
| 12025 | xmlNodeSetPtr seq; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12026 | xmlNodePtr cur; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12027 | /* First predicate operator */ |
| 12028 | xmlXPathStepOpPtr predOp; |
| 12029 | int maxPos; /* The requested position() (when a "[n]" predicate) */ |
| 12030 | int hasPredicateRange, hasAxisRange, pos, size, newSize; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12031 | int breakOnFirstHit; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12032 | |
| 12033 | xmlXPathTraversalFunction next = NULL; |
| 12034 | /* compound axis traversal */ |
| 12035 | xmlXPathTraversalFunctionExt outerNext = NULL; |
| 12036 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
| 12037 | xmlXPathNodeSetMergeFunction mergeAndClear; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12038 | xmlNodePtr oldContextNode; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12039 | xmlXPathContextPtr xpctxt = ctxt->context; |
| 12040 | |
| 12041 | |
| 12042 | CHECK_TYPE0(XPATH_NODESET); |
| 12043 | obj = valuePop(ctxt); |
| 12044 | /* |
| 12045 | * Setup namespaces. |
| 12046 | */ |
| 12047 | if (prefix != NULL) { |
| 12048 | URI = xmlXPathNsLookup(xpctxt, prefix); |
| 12049 | if (URI == NULL) { |
| 12050 | xmlXPathReleaseObject(xpctxt, obj); |
| 12051 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 12052 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12053 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12054 | /* |
| 12055 | * Setup axis. |
| 12056 | * |
| 12057 | * MAYBE FUTURE TODO: merging optimizations: |
| 12058 | * - If the nodes to be traversed wrt to the initial nodes and |
| 12059 | * the current axis cannot overlap, then we could avoid searching |
| 12060 | * for duplicates during the merge. |
| 12061 | * But the question is how/when to evaluate if they cannot overlap. |
| 12062 | * Example: if we know that for two initial nodes, the one is |
| 12063 | * not in the ancestor-or-self axis of the other, then we could safely |
| 12064 | * avoid a duplicate-aware merge, if the axis to be traversed is e.g. |
| 12065 | * the descendant-or-self axis. |
| 12066 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12067 | mergeAndClear = xmlXPathNodeSetMergeAndClear; |
| 12068 | switch (axis) { |
| 12069 | case AXIS_ANCESTOR: |
| 12070 | first = NULL; |
| 12071 | next = xmlXPathNextAncestor; |
| 12072 | break; |
| 12073 | case AXIS_ANCESTOR_OR_SELF: |
| 12074 | first = NULL; |
| 12075 | next = xmlXPathNextAncestorOrSelf; |
| 12076 | break; |
| 12077 | case AXIS_ATTRIBUTE: |
| 12078 | first = NULL; |
| 12079 | last = NULL; |
| 12080 | next = xmlXPathNextAttribute; |
| 12081 | mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls; |
| 12082 | break; |
| 12083 | case AXIS_CHILD: |
| 12084 | last = NULL; |
| 12085 | if (op->rewriteType == XP_REWRITE_DOS_CHILD_ELEM) { |
| 12086 | /* |
| 12087 | * This iterator will give us only nodes which can |
| 12088 | * hold element nodes. |
| 12089 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12090 | outerNext = xmlXPathNextDescendantOrSelfElemParent; |
| 12091 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12092 | if (((test == NODE_TEST_NAME) || (test == NODE_TEST_ALL)) && |
| 12093 | (type == NODE_TYPE_NODE)) |
| 12094 | { |
| 12095 | /* |
| 12096 | * Optimization if an element node type is 'element'. |
| 12097 | */ |
| 12098 | next = xmlXPathNextChildElement; |
| 12099 | } else |
| 12100 | next = xmlXPathNextChild; |
| 12101 | mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls; |
| 12102 | break; |
| 12103 | case AXIS_DESCENDANT: |
| 12104 | last = NULL; |
| 12105 | next = xmlXPathNextDescendant; |
| 12106 | break; |
| 12107 | case AXIS_DESCENDANT_OR_SELF: |
| 12108 | last = NULL; |
| 12109 | next = xmlXPathNextDescendantOrSelf; |
| 12110 | break; |
| 12111 | case AXIS_FOLLOWING: |
| 12112 | last = NULL; |
| 12113 | next = xmlXPathNextFollowing; |
| 12114 | break; |
| 12115 | case AXIS_FOLLOWING_SIBLING: |
| 12116 | last = NULL; |
| 12117 | next = xmlXPathNextFollowingSibling; |
| 12118 | break; |
| 12119 | case AXIS_NAMESPACE: |
| 12120 | first = NULL; |
| 12121 | last = NULL; |
| 12122 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
| 12123 | mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls; |
| 12124 | break; |
| 12125 | case AXIS_PARENT: |
| 12126 | first = NULL; |
| 12127 | next = xmlXPathNextParent; |
| 12128 | break; |
| 12129 | case AXIS_PRECEDING: |
| 12130 | first = NULL; |
| 12131 | next = xmlXPathNextPrecedingInternal; |
| 12132 | break; |
| 12133 | case AXIS_PRECEDING_SIBLING: |
| 12134 | first = NULL; |
| 12135 | next = xmlXPathNextPrecedingSibling; |
| 12136 | break; |
| 12137 | case AXIS_SELF: |
| 12138 | first = NULL; |
| 12139 | last = NULL; |
| 12140 | next = xmlXPathNextSelf; |
| 12141 | mergeAndClear = xmlXPathNodeSetMergeAndClearNoDupls; |
| 12142 | break; |
| 12143 | } |
| 12144 | |
| 12145 | #ifdef DEBUG_STEP |
Daniel Veillard | 074f37e | 2008-09-01 13:38:22 +0000 | [diff] [blame] | 12146 | xmlXPathDebugDumpStepAxis(op, |
| 12147 | (obj->nodesetval != NULL) ? obj->nodesetval->nodeNr : 0); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12148 | #endif |
| 12149 | |
| 12150 | if (next == NULL) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12151 | xmlXPathReleaseObject(xpctxt, obj); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12152 | return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12153 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12154 | contextSeq = obj->nodesetval; |
| 12155 | if ((contextSeq == NULL) || (contextSeq->nodeNr <= 0)) { |
| 12156 | xmlXPathReleaseObject(xpctxt, obj); |
| 12157 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(xpctxt, NULL)); |
| 12158 | return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12159 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12160 | /* |
| 12161 | * Predicate optimization --------------------------------------------- |
| 12162 | * If this step has a last predicate, which contains a position(), |
| 12163 | * then we'll optimize (although not exactly "position()", but only |
| 12164 | * the short-hand form, i.e., "[n]". |
| 12165 | * |
| 12166 | * Example - expression "/foo[parent::bar][1]": |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12167 | * |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12168 | * COLLECT 'child' 'name' 'node' foo -- op (we are here) |
| 12169 | * ROOT -- op->ch1 |
| 12170 | * PREDICATE -- op->ch2 (predOp) |
| 12171 | * PREDICATE -- predOp->ch1 = [parent::bar] |
| 12172 | * SORT |
| 12173 | * COLLECT 'parent' 'name' 'node' bar |
| 12174 | * NODE |
| 12175 | * ELEM Object is a number : 1 -- predOp->ch2 = [1] |
| 12176 | * |
| 12177 | */ |
| 12178 | maxPos = 0; |
| 12179 | predOp = NULL; |
| 12180 | hasPredicateRange = 0; |
| 12181 | hasAxisRange = 0; |
| 12182 | if (op->ch2 != -1) { |
| 12183 | /* |
| 12184 | * There's at least one predicate. 16 == XPATH_OP_PREDICATE |
| 12185 | */ |
| 12186 | predOp = &ctxt->comp->steps[op->ch2]; |
| 12187 | if (xmlXPathIsPositionalPredicate(ctxt, predOp, &maxPos)) { |
| 12188 | if (predOp->ch1 != -1) { |
| 12189 | /* |
| 12190 | * Use the next inner predicate operator. |
| 12191 | */ |
| 12192 | predOp = &ctxt->comp->steps[predOp->ch1]; |
| 12193 | hasPredicateRange = 1; |
| 12194 | } else { |
| 12195 | /* |
| 12196 | * There's no other predicate than the [n] predicate. |
| 12197 | */ |
| 12198 | predOp = NULL; |
| 12199 | hasAxisRange = 1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12200 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12201 | } |
| 12202 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12203 | breakOnFirstHit = ((toBool) && (predOp == NULL)) ? 1 : 0; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12204 | /* |
| 12205 | * Axis traversal ----------------------------------------------------- |
| 12206 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12207 | /* |
| 12208 | * 2.3 Node Tests |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12209 | * - For the attribute axis, the principal node type is attribute. |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12210 | * - For the namespace axis, the principal node type is namespace. |
| 12211 | * - For other axes, the principal node type is element. |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12212 | * |
| 12213 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 12214 | * principal node type. For example, child::* will |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12215 | * select all element children of the context node |
| 12216 | */ |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12217 | oldContextNode = xpctxt->node; |
| 12218 | addNode = xmlXPathNodeSetAddUnique; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12219 | outSeq = NULL; |
| 12220 | seq = NULL; |
| 12221 | outerContextNode = NULL; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12222 | contextNode = NULL; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12223 | contextIdx = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12224 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12225 | |
| 12226 | while ((contextIdx < contextSeq->nodeNr) || (contextNode != NULL)) { |
| 12227 | if (outerNext != NULL) { |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12228 | /* |
| 12229 | * This is a compound traversal. |
| 12230 | */ |
| 12231 | if (contextNode == NULL) { |
| 12232 | /* |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12233 | * Set the context for the outer traversal. |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12234 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12235 | outerContextNode = contextSeq->nodeTab[contextIdx++]; |
| 12236 | contextNode = outerNext(NULL, outerContextNode); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12237 | } else |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12238 | contextNode = outerNext(contextNode, outerContextNode); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12239 | if (contextNode == NULL) |
| 12240 | continue; |
| 12241 | /* |
| 12242 | * Set the context for the main traversal. |
| 12243 | */ |
| 12244 | xpctxt->node = contextNode; |
| 12245 | } else |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12246 | xpctxt->node = contextSeq->nodeTab[contextIdx++]; |
| 12247 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12248 | if (seq == NULL) { |
| 12249 | seq = xmlXPathNodeSetCreate(NULL); |
| 12250 | if (seq == NULL) { |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12251 | total = 0; |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12252 | goto error; |
| 12253 | } |
| 12254 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12255 | /* |
| 12256 | * Traverse the axis and test the nodes. |
| 12257 | */ |
| 12258 | pos = 0; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12259 | cur = NULL; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12260 | hasNsNodes = 0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12261 | do { |
| 12262 | cur = next(ctxt, cur); |
| 12263 | if (cur == NULL) |
| 12264 | break; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12265 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12266 | /* |
| 12267 | * QUESTION TODO: What does the "first" and "last" stuff do? |
| 12268 | */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12269 | if ((first != NULL) && (*first != NULL)) { |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12270 | if (*first == cur) |
| 12271 | break; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12272 | if (((total % 256) == 0) && |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12273 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12274 | (xmlXPathCmpNodesExt(*first, cur) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12275 | #else |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12276 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12277 | #endif |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12278 | { |
| 12279 | break; |
| 12280 | } |
| 12281 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12282 | if ((last != NULL) && (*last != NULL)) { |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12283 | if (*last == cur) |
| 12284 | break; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12285 | if (((total % 256) == 0) && |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12286 | #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12287 | (xmlXPathCmpNodesExt(cur, *last) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12288 | #else |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12289 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12290 | #endif |
Kasimier T. Buchcik | 75af2a8 | 2006-05-30 09:29:23 +0000 | [diff] [blame] | 12291 | { |
| 12292 | break; |
| 12293 | } |
| 12294 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12295 | |
| 12296 | total++; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12297 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12298 | #ifdef DEBUG_STEP |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12299 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
| 12300 | #endif |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12301 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12302 | switch (test) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12303 | case NODE_TEST_NONE: |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12304 | total = 0; |
| 12305 | STRANGE |
| 12306 | goto error; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12307 | case NODE_TEST_TYPE: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12308 | /* |
| 12309 | * TODO: Don't we need to use |
| 12310 | * xmlXPathNodeSetAddNs() for namespace nodes here? |
| 12311 | * Surprisingly, some c14n tests fail, if we do this. |
| 12312 | */ |
| 12313 | if (type == NODE_TYPE_NODE) { |
| 12314 | switch (cur->type) { |
| 12315 | case XML_DOCUMENT_NODE: |
| 12316 | case XML_HTML_DOCUMENT_NODE: |
| 12317 | #ifdef LIBXML_DOCB_ENABLED |
| 12318 | case XML_DOCB_DOCUMENT_NODE: |
| 12319 | #endif |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12320 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12321 | case XML_ATTRIBUTE_NODE: |
| 12322 | case XML_PI_NODE: |
| 12323 | case XML_COMMENT_NODE: |
| 12324 | case XML_CDATA_SECTION_NODE: |
| 12325 | case XML_TEXT_NODE: |
| 12326 | case XML_NAMESPACE_DECL: |
| 12327 | XP_TEST_HIT |
| 12328 | break; |
| 12329 | default: |
| 12330 | break; |
| 12331 | } |
| 12332 | } else if (cur->type == type) { |
Kasimier T. Buchcik | 889b762 | 2006-07-03 11:44:13 +0000 | [diff] [blame] | 12333 | if (type == XML_NAMESPACE_DECL) |
| 12334 | XP_TEST_HIT_NS |
| 12335 | else |
| 12336 | XP_TEST_HIT |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12337 | } else if ((type == NODE_TYPE_TEXT) && |
| 12338 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 12339 | { |
| 12340 | XP_TEST_HIT |
| 12341 | } |
| 12342 | break; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12343 | case NODE_TEST_PI: |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12344 | if ((cur->type == XML_PI_NODE) && |
| 12345 | ((name == NULL) || xmlStrEqual(name, cur->name))) |
| 12346 | { |
| 12347 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12348 | } |
| 12349 | break; |
| 12350 | case NODE_TEST_ALL: |
| 12351 | if (axis == AXIS_ATTRIBUTE) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12352 | if (cur->type == XML_ATTRIBUTE_NODE) |
| 12353 | { |
| 12354 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12355 | } |
| 12356 | } else if (axis == AXIS_NAMESPACE) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12357 | if (cur->type == XML_NAMESPACE_DECL) |
| 12358 | { |
| 12359 | XP_TEST_HIT_NS |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12360 | } |
| 12361 | } else { |
| 12362 | if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12363 | if (prefix == NULL) |
| 12364 | { |
| 12365 | XP_TEST_HIT |
| 12366 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12367 | } else if ((cur->ns != NULL) && |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12368 | (xmlStrEqual(URI, cur->ns->href))) |
| 12369 | { |
| 12370 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12371 | } |
| 12372 | } |
| 12373 | } |
| 12374 | break; |
| 12375 | case NODE_TEST_NS:{ |
| 12376 | TODO; |
| 12377 | break; |
| 12378 | } |
| 12379 | case NODE_TEST_NAME: |
Daniel Veillard | fe3970e | 2006-11-23 16:08:30 +0000 | [diff] [blame] | 12380 | if (axis == AXIS_ATTRIBUTE) { |
| 12381 | if (cur->type != XML_ATTRIBUTE_NODE) |
| 12382 | break; |
| 12383 | } else if (axis == AXIS_NAMESPACE) { |
| 12384 | if (cur->type != XML_NAMESPACE_DECL) |
| 12385 | break; |
| 12386 | } else { |
| 12387 | if (cur->type != XML_ELEMENT_NODE) |
| 12388 | break; |
| 12389 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12390 | switch (cur->type) { |
| 12391 | case XML_ELEMENT_NODE: |
| 12392 | if (xmlStrEqual(name, cur->name)) { |
| 12393 | if (prefix == NULL) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12394 | if (cur->ns == NULL) |
| 12395 | { |
| 12396 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12397 | } |
| 12398 | } else { |
| 12399 | if ((cur->ns != NULL) && |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12400 | (xmlStrEqual(URI, cur->ns->href))) |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12401 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12402 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12403 | } |
| 12404 | } |
| 12405 | } |
| 12406 | break; |
| 12407 | case XML_ATTRIBUTE_NODE:{ |
| 12408 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 12409 | |
| 12410 | if (xmlStrEqual(name, attr->name)) { |
| 12411 | if (prefix == NULL) { |
| 12412 | if ((attr->ns == NULL) || |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12413 | (attr->ns->prefix == NULL)) |
| 12414 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12415 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12416 | } |
| 12417 | } else { |
| 12418 | if ((attr->ns != NULL) && |
| 12419 | (xmlStrEqual(URI, |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12420 | attr->ns->href))) |
| 12421 | { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12422 | XP_TEST_HIT |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12423 | } |
| 12424 | } |
| 12425 | } |
| 12426 | break; |
| 12427 | } |
| 12428 | case XML_NAMESPACE_DECL: |
| 12429 | if (cur->type == XML_NAMESPACE_DECL) { |
| 12430 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 12431 | |
| 12432 | if ((ns->prefix != NULL) && (name != NULL) |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12433 | && (xmlStrEqual(ns->prefix, name))) |
| 12434 | { |
| 12435 | XP_TEST_HIT_NS |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12436 | } |
| 12437 | } |
| 12438 | break; |
| 12439 | default: |
| 12440 | break; |
| 12441 | } |
| 12442 | break; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12443 | } /* switch(test) */ |
| 12444 | } while (cur != NULL); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12445 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12446 | goto apply_predicates; |
| 12447 | |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12448 | axis_range_end: /* ----------------------------------------------------- */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12449 | /* |
| 12450 | * We have a "/foo[n]", and position() = n was reached. |
| 12451 | * Note that we can have as well "/foo/::parent::foo[1]", so |
| 12452 | * a duplicate-aware merge is still needed. |
| 12453 | * Merge with the result. |
| 12454 | */ |
| 12455 | if (outSeq == NULL) { |
| 12456 | outSeq = seq; |
| 12457 | seq = NULL; |
| 12458 | } else |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12459 | outSeq = mergeAndClear(outSeq, seq, 0); |
| 12460 | /* |
| 12461 | * Break if only a true/false result was requested. |
| 12462 | */ |
| 12463 | if (toBool) |
| 12464 | break; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12465 | continue; |
| 12466 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12467 | first_hit: /* ---------------------------------------------------------- */ |
| 12468 | /* |
| 12469 | * Break if only a true/false result was requested and |
| 12470 | * no predicates existed and a node test succeeded. |
| 12471 | */ |
| 12472 | if (outSeq == NULL) { |
| 12473 | outSeq = seq; |
| 12474 | seq = NULL; |
| 12475 | } else |
| 12476 | outSeq = mergeAndClear(outSeq, seq, 0); |
| 12477 | break; |
| 12478 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12479 | #ifdef DEBUG_STEP |
| 12480 | if (seq != NULL) |
| 12481 | nbMatches += seq->nodeNr; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12482 | #endif |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12483 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12484 | apply_predicates: /* --------------------------------------------------- */ |
| 12485 | /* |
| 12486 | * Apply predicates. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12487 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12488 | if ((predOp != NULL) && (seq->nodeNr > 0)) { |
| 12489 | /* |
| 12490 | * E.g. when we have a "/foo[some expression][n]". |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12491 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12492 | /* |
| 12493 | * QUESTION TODO: The old predicate evaluation took into |
| 12494 | * account location-sets. |
| 12495 | * (E.g. ctxt->value->type == XPATH_LOCATIONSET) |
| 12496 | * Do we expect such a set here? |
| 12497 | * All what I learned now from the evaluation semantics |
| 12498 | * does not indicate that a location-set will be processed |
| 12499 | * here, so this looks OK. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12500 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12501 | /* |
| 12502 | * Iterate over all predicates, starting with the outermost |
| 12503 | * predicate. |
| 12504 | * TODO: Problem: we cannot execute the inner predicates first |
| 12505 | * since we cannot go back *up* the operator tree! |
| 12506 | * Options we have: |
| 12507 | * 1) Use of recursive functions (like is it currently done |
| 12508 | * via xmlXPathCompOpEval()) |
| 12509 | * 2) Add a predicate evaluation information stack to the |
| 12510 | * context struct |
| 12511 | * 3) Change the way the operators are linked; we need a |
| 12512 | * "parent" field on xmlXPathStepOp |
| 12513 | * |
| 12514 | * For the moment, I'll try to solve this with a recursive |
| 12515 | * function: xmlXPathCompOpEvalPredicate(). |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12516 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12517 | size = seq->nodeNr; |
| 12518 | if (hasPredicateRange != 0) |
| 12519 | newSize = xmlXPathCompOpEvalPositionalPredicate(ctxt, |
| 12520 | predOp, seq, size, maxPos, maxPos, hasNsNodes); |
| 12521 | else |
| 12522 | newSize = xmlXPathCompOpEvalPredicate(ctxt, |
| 12523 | predOp, seq, size, hasNsNodes); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12524 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12525 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 12526 | total = 0; |
| 12527 | goto error; |
| 12528 | } |
| 12529 | /* |
| 12530 | * Add the filtered set of nodes to the result node set. |
| 12531 | */ |
| 12532 | if (newSize == 0) { |
| 12533 | /* |
| 12534 | * The predicates filtered all nodes out. |
| 12535 | */ |
| 12536 | xmlXPathNodeSetClear(seq, hasNsNodes); |
| 12537 | } else if (seq->nodeNr > 0) { |
| 12538 | /* |
| 12539 | * Add to result set. |
| 12540 | */ |
| 12541 | if (outSeq == NULL) { |
| 12542 | if (size != newSize) { |
| 12543 | /* |
| 12544 | * We need to merge and clear here, since |
| 12545 | * the sequence will contained NULLed entries. |
| 12546 | */ |
| 12547 | outSeq = mergeAndClear(NULL, seq, 1); |
| 12548 | } else { |
| 12549 | outSeq = seq; |
| 12550 | seq = NULL; |
| 12551 | } |
| 12552 | } else |
| 12553 | outSeq = mergeAndClear(outSeq, seq, |
| 12554 | (size != newSize) ? 1: 0); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12555 | /* |
| 12556 | * Break if only a true/false result was requested. |
| 12557 | */ |
| 12558 | if (toBool) |
| 12559 | break; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12560 | } |
| 12561 | } else if (seq->nodeNr > 0) { |
| 12562 | /* |
| 12563 | * Add to result set. |
| 12564 | */ |
| 12565 | if (outSeq == NULL) { |
| 12566 | outSeq = seq; |
| 12567 | seq = NULL; |
| 12568 | } else { |
| 12569 | outSeq = mergeAndClear(outSeq, seq, 0); |
| 12570 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12571 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12572 | } |
| 12573 | |
| 12574 | error: |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 12575 | if ((obj->boolval) && (obj->user != NULL)) { |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12576 | /* |
| 12577 | * QUESTION TODO: What does this do and why? |
| 12578 | * TODO: Do we have to do this also for the "error" |
| 12579 | * cleanup further down? |
| 12580 | */ |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 12581 | ctxt->value->boolval = 1; |
| 12582 | ctxt->value->user = obj->user; |
| 12583 | obj->user = NULL; |
| 12584 | obj->boolval = 0; |
| 12585 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12586 | xmlXPathReleaseObject(xpctxt, obj); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12587 | |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12588 | /* |
| 12589 | * Ensure we return at least an emtpy set. |
| 12590 | */ |
| 12591 | if (outSeq == NULL) { |
| 12592 | if ((seq != NULL) && (seq->nodeNr == 0)) |
| 12593 | outSeq = seq; |
| 12594 | else |
| 12595 | outSeq = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 12596 | /* XXX what if xmlXPathNodeSetCreate returned NULL here? */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12597 | } |
| 12598 | if ((seq != NULL) && (seq != outSeq)) { |
| 12599 | xmlXPathFreeNodeSet(seq); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12600 | } |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12601 | /* |
| 12602 | * Hand over the result. Better to push the set also in |
| 12603 | * case of errors. |
| 12604 | */ |
| 12605 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(xpctxt, outSeq)); |
| 12606 | /* |
| 12607 | * Reset the context node. |
| 12608 | */ |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12609 | xpctxt->node = oldContextNode; |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 12610 | |
| 12611 | #ifdef DEBUG_STEP |
| 12612 | xmlGenericError(xmlGenericErrorContext, |
| 12613 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 12614 | total, nbMatches); |
| 12615 | #endif |
| 12616 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 12617 | return(total); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12618 | } |
| 12619 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12620 | static int |
| 12621 | xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt, |
| 12622 | xmlXPathStepOpPtr op, xmlNodePtr * first); |
| 12623 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12624 | /** |
| 12625 | * xmlXPathCompOpEvalFirst: |
| 12626 | * @ctxt: the XPath parser context with the compiled expression |
| 12627 | * @op: an XPath compiled operation |
| 12628 | * @first: the first elem found so far |
| 12629 | * |
| 12630 | * Evaluate the Precompiled XPath operation searching only the first |
| 12631 | * element in document order |
| 12632 | * |
| 12633 | * Returns the number of examined objects. |
| 12634 | */ |
| 12635 | static int |
| 12636 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 12637 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 12638 | { |
| 12639 | int total = 0, cur; |
| 12640 | xmlXPathCompExprPtr comp; |
| 12641 | xmlXPathObjectPtr arg1, arg2; |
| 12642 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12643 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12644 | comp = ctxt->comp; |
| 12645 | switch (op->op) { |
| 12646 | case XPATH_OP_END: |
| 12647 | return (0); |
| 12648 | case XPATH_OP_UNION: |
| 12649 | total = |
| 12650 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 12651 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12652 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12653 | if ((ctxt->value != NULL) |
| 12654 | && (ctxt->value->type == XPATH_NODESET) |
| 12655 | && (ctxt->value->nodesetval != NULL) |
| 12656 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 12657 | /* |
| 12658 | * limit tree traversing to first node in the result |
| 12659 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12660 | /* |
| 12661 | * OPTIMIZE TODO: This implicitely sorts |
| 12662 | * the result, even if not needed. E.g. if the argument |
| 12663 | * of the count() function, no sorting is needed. |
| 12664 | * OPTIMIZE TODO: How do we know if the node-list wasn't |
| 12665 | * aready sorted? |
| 12666 | */ |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12667 | if (ctxt->value->nodesetval->nodeNr > 1) |
| 12668 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12669 | *first = ctxt->value->nodesetval->nodeTab[0]; |
| 12670 | } |
| 12671 | cur = |
| 12672 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2], |
| 12673 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12674 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12675 | CHECK_TYPE0(XPATH_NODESET); |
| 12676 | arg2 = valuePop(ctxt); |
| 12677 | |
| 12678 | CHECK_TYPE0(XPATH_NODESET); |
| 12679 | arg1 = valuePop(ctxt); |
| 12680 | |
| 12681 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 12682 | arg2->nodesetval); |
| 12683 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12684 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12685 | /* optimizer */ |
| 12686 | if (total > cur) |
| 12687 | xmlXPathCompSwap(op); |
| 12688 | return (total + cur); |
| 12689 | case XPATH_OP_ROOT: |
| 12690 | xmlXPathRoot(ctxt); |
| 12691 | return (0); |
| 12692 | case XPATH_OP_NODE: |
| 12693 | if (op->ch1 != -1) |
| 12694 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12695 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12696 | if (op->ch2 != -1) |
| 12697 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12698 | CHECK_ERROR0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12699 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 12700 | ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12701 | return (total); |
| 12702 | case XPATH_OP_RESET: |
| 12703 | if (op->ch1 != -1) |
| 12704 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12705 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12706 | if (op->ch2 != -1) |
| 12707 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12708 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12709 | ctxt->context->node = NULL; |
| 12710 | return (total); |
| 12711 | case XPATH_OP_COLLECT:{ |
| 12712 | if (op->ch1 == -1) |
| 12713 | return (total); |
| 12714 | |
| 12715 | total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12716 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12717 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12718 | total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL, 0); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12719 | return (total); |
| 12720 | } |
| 12721 | case XPATH_OP_VALUE: |
| 12722 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12723 | xmlXPathCacheObjectCopy(ctxt->context, |
| 12724 | (xmlXPathObjectPtr) op->value4)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12725 | return (0); |
| 12726 | case XPATH_OP_SORT: |
| 12727 | if (op->ch1 != -1) |
| 12728 | total += |
| 12729 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 12730 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12731 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12732 | if ((ctxt->value != NULL) |
| 12733 | && (ctxt->value->type == XPATH_NODESET) |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12734 | && (ctxt->value->nodesetval != NULL) |
| 12735 | && (ctxt->value->nodesetval->nodeNr > 1)) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12736 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 12737 | return (total); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12738 | #ifdef XP_OPTIMIZED_FILTER_FIRST |
| 12739 | case XPATH_OP_FILTER: |
Marius Wachtler | 2ddecc2 | 2010-10-12 09:09:07 +0200 | [diff] [blame] | 12740 | total += xmlXPathCompOpEvalFilterFirst(ctxt, op, first); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12741 | return (total); |
| 12742 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12743 | default: |
| 12744 | return (xmlXPathCompOpEval(ctxt, op)); |
| 12745 | } |
| 12746 | } |
| 12747 | |
| 12748 | /** |
| 12749 | * xmlXPathCompOpEvalLast: |
| 12750 | * @ctxt: the XPath parser context with the compiled expression |
| 12751 | * @op: an XPath compiled operation |
| 12752 | * @last: the last elem found so far |
| 12753 | * |
| 12754 | * Evaluate the Precompiled XPath operation searching only the last |
| 12755 | * element in document order |
| 12756 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 12757 | * Returns the number of nodes traversed |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12758 | */ |
| 12759 | static int |
| 12760 | xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op, |
| 12761 | xmlNodePtr * last) |
| 12762 | { |
| 12763 | int total = 0, cur; |
| 12764 | xmlXPathCompExprPtr comp; |
| 12765 | xmlXPathObjectPtr arg1, arg2; |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 12766 | xmlNodePtr bak; |
| 12767 | xmlDocPtr bakd; |
| 12768 | int pp; |
| 12769 | int cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12770 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12771 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12772 | comp = ctxt->comp; |
| 12773 | switch (op->op) { |
| 12774 | case XPATH_OP_END: |
| 12775 | return (0); |
| 12776 | case XPATH_OP_UNION: |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 12777 | bakd = ctxt->context->doc; |
| 12778 | bak = ctxt->context->node; |
| 12779 | pp = ctxt->context->proximityPosition; |
| 12780 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12781 | total = |
| 12782 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12783 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12784 | if ((ctxt->value != NULL) |
| 12785 | && (ctxt->value->type == XPATH_NODESET) |
| 12786 | && (ctxt->value->nodesetval != NULL) |
| 12787 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 12788 | /* |
| 12789 | * limit tree traversing to first node in the result |
| 12790 | */ |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12791 | if (ctxt->value->nodesetval->nodeNr > 1) |
| 12792 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12793 | *last = |
| 12794 | ctxt->value->nodesetval->nodeTab[ctxt->value-> |
| 12795 | nodesetval->nodeNr - |
| 12796 | 1]; |
| 12797 | } |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 12798 | ctxt->context->doc = bakd; |
| 12799 | ctxt->context->node = bak; |
| 12800 | ctxt->context->proximityPosition = pp; |
| 12801 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12802 | cur = |
| 12803 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12804 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12805 | if ((ctxt->value != NULL) |
| 12806 | && (ctxt->value->type == XPATH_NODESET) |
| 12807 | && (ctxt->value->nodesetval != NULL) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12808 | && (ctxt->value->nodesetval->nodeNr >= 1)) { /* TODO: NOP ? */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12809 | } |
| 12810 | CHECK_TYPE0(XPATH_NODESET); |
| 12811 | arg2 = valuePop(ctxt); |
| 12812 | |
| 12813 | CHECK_TYPE0(XPATH_NODESET); |
| 12814 | arg1 = valuePop(ctxt); |
| 12815 | |
| 12816 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 12817 | arg2->nodesetval); |
| 12818 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12819 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12820 | /* optimizer */ |
| 12821 | if (total > cur) |
| 12822 | xmlXPathCompSwap(op); |
| 12823 | return (total + cur); |
| 12824 | case XPATH_OP_ROOT: |
| 12825 | xmlXPathRoot(ctxt); |
| 12826 | return (0); |
| 12827 | case XPATH_OP_NODE: |
| 12828 | if (op->ch1 != -1) |
| 12829 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12830 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12831 | if (op->ch2 != -1) |
| 12832 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12833 | CHECK_ERROR0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12834 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 12835 | ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12836 | return (total); |
| 12837 | case XPATH_OP_RESET: |
| 12838 | if (op->ch1 != -1) |
| 12839 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12840 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12841 | if (op->ch2 != -1) |
| 12842 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12843 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12844 | ctxt->context->node = NULL; |
| 12845 | return (total); |
| 12846 | case XPATH_OP_COLLECT:{ |
| 12847 | if (op->ch1 == -1) |
| 12848 | return (0); |
| 12849 | |
| 12850 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12851 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12852 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 12853 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last, 0); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12854 | return (total); |
| 12855 | } |
| 12856 | case XPATH_OP_VALUE: |
| 12857 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12858 | xmlXPathCacheObjectCopy(ctxt->context, |
| 12859 | (xmlXPathObjectPtr) op->value4)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12860 | return (0); |
| 12861 | case XPATH_OP_SORT: |
| 12862 | if (op->ch1 != -1) |
| 12863 | total += |
| 12864 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], |
| 12865 | last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 12866 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12867 | if ((ctxt->value != NULL) |
| 12868 | && (ctxt->value->type == XPATH_NODESET) |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 12869 | && (ctxt->value->nodesetval != NULL) |
| 12870 | && (ctxt->value->nodesetval->nodeNr > 1)) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 12871 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 12872 | return (total); |
| 12873 | default: |
| 12874 | return (xmlXPathCompOpEval(ctxt, op)); |
| 12875 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 12876 | } |
| 12877 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12878 | #ifdef XP_OPTIMIZED_FILTER_FIRST |
| 12879 | static int |
| 12880 | xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt, |
| 12881 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 12882 | { |
| 12883 | int total = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12884 | xmlXPathCompExprPtr comp; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12885 | xmlXPathObjectPtr res; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12886 | xmlXPathObjectPtr obj; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12887 | xmlNodeSetPtr oldset; |
| 12888 | xmlNodePtr oldnode; |
| 12889 | xmlDocPtr oldDoc; |
| 12890 | int i; |
| 12891 | |
| 12892 | CHECK_ERROR0; |
| 12893 | comp = ctxt->comp; |
| 12894 | /* |
| 12895 | * Optimization for ()[last()] selection i.e. the last elem |
| 12896 | */ |
| 12897 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 12898 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 12899 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 12900 | int f = comp->steps[op->ch2].ch1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12901 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12902 | if ((f != -1) && |
| 12903 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 12904 | (comp->steps[f].value5 == NULL) && |
| 12905 | (comp->steps[f].value == 0) && |
| 12906 | (comp->steps[f].value4 != NULL) && |
| 12907 | (xmlStrEqual |
| 12908 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 12909 | xmlNodePtr last = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12910 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12911 | total += |
| 12912 | xmlXPathCompOpEvalLast(ctxt, |
| 12913 | &comp->steps[op->ch1], |
| 12914 | &last); |
| 12915 | CHECK_ERROR0; |
| 12916 | /* |
| 12917 | * The nodeset should be in document order, |
| 12918 | * Keep only the last value |
| 12919 | */ |
| 12920 | if ((ctxt->value != NULL) && |
| 12921 | (ctxt->value->type == XPATH_NODESET) && |
| 12922 | (ctxt->value->nodesetval != NULL) && |
| 12923 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 12924 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 12925 | ctxt->value->nodesetval->nodeTab[0] = |
| 12926 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 12927 | value-> |
| 12928 | nodesetval-> |
| 12929 | nodeNr - |
| 12930 | 1]; |
| 12931 | ctxt->value->nodesetval->nodeNr = 1; |
| 12932 | *first = *(ctxt->value->nodesetval->nodeTab); |
| 12933 | } |
| 12934 | return (total); |
| 12935 | } |
| 12936 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12937 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12938 | if (op->ch1 != -1) |
| 12939 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 12940 | CHECK_ERROR0; |
| 12941 | if (op->ch2 == -1) |
| 12942 | return (total); |
| 12943 | if (ctxt->value == NULL) |
| 12944 | return (total); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12945 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12946 | #ifdef LIBXML_XPTR_ENABLED |
| 12947 | oldnode = ctxt->context->node; |
| 12948 | /* |
| 12949 | * Hum are we filtering the result of an XPointer expression |
| 12950 | */ |
| 12951 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 12952 | xmlXPathObjectPtr tmp = NULL; |
| 12953 | xmlLocationSetPtr newlocset = NULL; |
| 12954 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12955 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12956 | /* |
| 12957 | * Extract the old locset, and then evaluate the result of the |
| 12958 | * expression for all the element in the locset. use it to grow |
| 12959 | * up a new locset. |
| 12960 | */ |
| 12961 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 12962 | obj = valuePop(ctxt); |
| 12963 | oldlocset = obj->user; |
| 12964 | ctxt->context->node = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12965 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12966 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 12967 | ctxt->context->contextSize = 0; |
| 12968 | ctxt->context->proximityPosition = 0; |
| 12969 | if (op->ch2 != -1) |
| 12970 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
| 12971 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12972 | if (res != NULL) { |
| 12973 | xmlXPathReleaseObject(ctxt->context, res); |
| 12974 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12975 | valuePush(ctxt, obj); |
| 12976 | CHECK_ERROR0; |
| 12977 | return (total); |
| 12978 | } |
| 12979 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12980 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12981 | for (i = 0; i < oldlocset->locNr; i++) { |
| 12982 | /* |
| 12983 | * Run the evaluation with a node list made of a |
| 12984 | * single item in the nodelocset. |
| 12985 | */ |
| 12986 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 12987 | ctxt->context->contextSize = oldlocset->locNr; |
| 12988 | ctxt->context->proximityPosition = i + 1; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 12989 | if (tmp == NULL) { |
| 12990 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 12991 | ctxt->context->node); |
| 12992 | } else { |
| 12993 | xmlXPathNodeSetAddUnique(tmp->nodesetval, |
| 12994 | ctxt->context->node); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 12995 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 12996 | valuePush(ctxt, tmp); |
| 12997 | if (op->ch2 != -1) |
| 12998 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
| 12999 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13000 | xmlXPathFreeObject(obj); |
| 13001 | return(0); |
| 13002 | } |
| 13003 | /* |
| 13004 | * The result of the evaluation need to be tested to |
| 13005 | * decided whether the filter succeeded or not |
| 13006 | */ |
| 13007 | res = valuePop(ctxt); |
| 13008 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 13009 | xmlXPtrLocationSetAdd(newlocset, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13010 | xmlXPathCacheObjectCopy(ctxt->context, |
| 13011 | oldlocset->locTab[i])); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13012 | } |
| 13013 | /* |
| 13014 | * Cleanup |
| 13015 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13016 | if (res != NULL) { |
| 13017 | xmlXPathReleaseObject(ctxt->context, res); |
| 13018 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13019 | if (ctxt->value == tmp) { |
| 13020 | valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13021 | xmlXPathNodeSetClear(tmp->nodesetval, 1); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13022 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13023 | * REVISIT TODO: Don't create a temporary nodeset |
| 13024 | * for everly iteration. |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13025 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13026 | /* OLD: xmlXPathFreeObject(res); */ |
| 13027 | } else |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13028 | tmp = NULL; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13029 | ctxt->context->node = NULL; |
| 13030 | /* |
| 13031 | * Only put the first node in the result, then leave. |
| 13032 | */ |
| 13033 | if (newlocset->locNr > 0) { |
| 13034 | *first = (xmlNodePtr) oldlocset->locTab[i]->user; |
| 13035 | break; |
| 13036 | } |
| 13037 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13038 | if (tmp != NULL) { |
| 13039 | xmlXPathReleaseObject(ctxt->context, tmp); |
| 13040 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13041 | /* |
| 13042 | * The result is used as the new evaluation locset. |
| 13043 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13044 | xmlXPathReleaseObject(ctxt->context, obj); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13045 | ctxt->context->node = NULL; |
| 13046 | ctxt->context->contextSize = -1; |
| 13047 | ctxt->context->proximityPosition = -1; |
| 13048 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 13049 | ctxt->context->node = oldnode; |
| 13050 | return (total); |
| 13051 | } |
| 13052 | #endif /* LIBXML_XPTR_ENABLED */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13053 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13054 | /* |
| 13055 | * Extract the old set, and then evaluate the result of the |
| 13056 | * expression for all the element in the set. use it to grow |
| 13057 | * up a new set. |
| 13058 | */ |
| 13059 | CHECK_TYPE0(XPATH_NODESET); |
| 13060 | obj = valuePop(ctxt); |
| 13061 | oldset = obj->nodesetval; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13062 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13063 | oldnode = ctxt->context->node; |
| 13064 | oldDoc = ctxt->context->doc; |
| 13065 | ctxt->context->node = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13066 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13067 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 13068 | ctxt->context->contextSize = 0; |
| 13069 | ctxt->context->proximityPosition = 0; |
| 13070 | /* QUESTION TODO: Why was this code commented out? |
| 13071 | if (op->ch2 != -1) |
| 13072 | total += |
| 13073 | xmlXPathCompOpEval(ctxt, |
| 13074 | &comp->steps[op->ch2]); |
| 13075 | CHECK_ERROR0; |
| 13076 | res = valuePop(ctxt); |
| 13077 | if (res != NULL) |
| 13078 | xmlXPathFreeObject(res); |
| 13079 | */ |
| 13080 | valuePush(ctxt, obj); |
| 13081 | ctxt->context->node = oldnode; |
| 13082 | CHECK_ERROR0; |
| 13083 | } else { |
| 13084 | xmlNodeSetPtr newset; |
| 13085 | xmlXPathObjectPtr tmp = NULL; |
| 13086 | /* |
| 13087 | * Initialize the new set. |
| 13088 | * Also set the xpath document in case things like |
| 13089 | * key() evaluation are attempted on the predicate |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13090 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13091 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | f88d849 | 2008-04-01 08:00:31 +0000 | [diff] [blame] | 13092 | /* XXX what if xmlXPathNodeSetCreate returned NULL? */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13093 | |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13094 | for (i = 0; i < oldset->nodeNr; i++) { |
| 13095 | /* |
| 13096 | * Run the evaluation with a node list made of |
| 13097 | * a single item in the nodeset. |
| 13098 | */ |
| 13099 | ctxt->context->node = oldset->nodeTab[i]; |
| 13100 | if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) && |
| 13101 | (oldset->nodeTab[i]->doc != NULL)) |
| 13102 | ctxt->context->doc = oldset->nodeTab[i]->doc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13103 | if (tmp == NULL) { |
| 13104 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 13105 | ctxt->context->node); |
| 13106 | } else { |
| 13107 | xmlXPathNodeSetAddUnique(tmp->nodesetval, |
| 13108 | ctxt->context->node); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13109 | } |
| 13110 | valuePush(ctxt, tmp); |
| 13111 | ctxt->context->contextSize = oldset->nodeNr; |
| 13112 | ctxt->context->proximityPosition = i + 1; |
| 13113 | if (op->ch2 != -1) |
| 13114 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
| 13115 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13116 | xmlXPathFreeNodeSet(newset); |
| 13117 | xmlXPathFreeObject(obj); |
| 13118 | return(0); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13119 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13120 | /* |
| 13121 | * The result of the evaluation needs to be tested to |
| 13122 | * decide whether the filter succeeded or not |
| 13123 | */ |
| 13124 | res = valuePop(ctxt); |
| 13125 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 13126 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13127 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13128 | /* |
| 13129 | * Cleanup |
| 13130 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13131 | if (res != NULL) { |
| 13132 | xmlXPathReleaseObject(ctxt->context, res); |
| 13133 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13134 | if (ctxt->value == tmp) { |
| 13135 | valuePop(ctxt); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13136 | /* |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13137 | * Don't free the temporary nodeset |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13138 | * in order to avoid massive recreation inside this |
| 13139 | * loop. |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13140 | */ |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 13141 | xmlXPathNodeSetClear(tmp->nodesetval, 1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13142 | } else |
| 13143 | tmp = NULL; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13144 | ctxt->context->node = NULL; |
| 13145 | /* |
| 13146 | * Only put the first node in the result, then leave. |
| 13147 | */ |
| 13148 | if (newset->nodeNr > 0) { |
| 13149 | *first = *(newset->nodeTab); |
| 13150 | break; |
| 13151 | } |
| 13152 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13153 | if (tmp != NULL) { |
| 13154 | xmlXPathReleaseObject(ctxt->context, tmp); |
| 13155 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13156 | /* |
| 13157 | * The result is used as the new evaluation set. |
| 13158 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13159 | xmlXPathReleaseObject(ctxt->context, obj); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13160 | ctxt->context->node = NULL; |
| 13161 | ctxt->context->contextSize = -1; |
| 13162 | ctxt->context->proximityPosition = -1; |
| 13163 | /* may want to move this past the '}' later */ |
| 13164 | ctxt->context->doc = oldDoc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13165 | valuePush(ctxt, xmlXPathCacheWrapNodeSet(ctxt->context, newset)); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13166 | } |
| 13167 | ctxt->context->node = oldnode; |
| 13168 | return(total); |
| 13169 | } |
| 13170 | #endif /* XP_OPTIMIZED_FILTER_FIRST */ |
| 13171 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13172 | /** |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13173 | * xmlXPathCompOpEval: |
| 13174 | * @ctxt: the XPath parser context with the compiled expression |
| 13175 | * @op: an XPath compiled operation |
| 13176 | * |
| 13177 | * Evaluate the Precompiled XPath operation |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13178 | * Returns the number of nodes traversed |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13179 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13180 | static int |
| 13181 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op) |
| 13182 | { |
| 13183 | int total = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13184 | int equal, ret; |
| 13185 | xmlXPathCompExprPtr comp; |
| 13186 | xmlXPathObjectPtr arg1, arg2; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13187 | xmlNodePtr bak; |
| 13188 | xmlDocPtr bakd; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13189 | int pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13190 | int cs; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13191 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13192 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13193 | comp = ctxt->comp; |
| 13194 | switch (op->op) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13195 | case XPATH_OP_END: |
| 13196 | return (0); |
| 13197 | case XPATH_OP_AND: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13198 | bakd = ctxt->context->doc; |
| 13199 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13200 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13201 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13202 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13203 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13204 | xmlXPathBooleanFunction(ctxt, 1); |
| 13205 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 0)) |
| 13206 | return (total); |
| 13207 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13208 | ctxt->context->doc = bakd; |
| 13209 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13210 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13211 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13212 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13213 | if (ctxt->error) { |
| 13214 | xmlXPathFreeObject(arg2); |
| 13215 | return(0); |
| 13216 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13217 | xmlXPathBooleanFunction(ctxt, 1); |
| 13218 | arg1 = valuePop(ctxt); |
| 13219 | arg1->boolval &= arg2->boolval; |
| 13220 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13221 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13222 | return (total); |
| 13223 | case XPATH_OP_OR: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13224 | bakd = ctxt->context->doc; |
| 13225 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13226 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13227 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13228 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13229 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13230 | xmlXPathBooleanFunction(ctxt, 1); |
| 13231 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 1)) |
| 13232 | return (total); |
| 13233 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13234 | ctxt->context->doc = bakd; |
| 13235 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13236 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13237 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13238 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13239 | if (ctxt->error) { |
| 13240 | xmlXPathFreeObject(arg2); |
| 13241 | return(0); |
| 13242 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13243 | xmlXPathBooleanFunction(ctxt, 1); |
| 13244 | arg1 = valuePop(ctxt); |
| 13245 | arg1->boolval |= arg2->boolval; |
| 13246 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13247 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13248 | return (total); |
| 13249 | case XPATH_OP_EQUAL: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13250 | bakd = ctxt->context->doc; |
| 13251 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13252 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13253 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13254 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13255 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13256 | ctxt->context->doc = bakd; |
| 13257 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13258 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13259 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13260 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13261 | CHECK_ERROR0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 13262 | if (op->value) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13263 | equal = xmlXPathEqualValues(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 13264 | else |
| 13265 | equal = xmlXPathNotEqualValues(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13266 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, equal)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13267 | return (total); |
| 13268 | case XPATH_OP_CMP: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13269 | bakd = ctxt->context->doc; |
| 13270 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13271 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13272 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13273 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13274 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13275 | ctxt->context->doc = bakd; |
| 13276 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13277 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13278 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13279 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13280 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13281 | ret = xmlXPathCompareValues(ctxt, op->value, op->value2); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13282 | valuePush(ctxt, xmlXPathCacheNewBoolean(ctxt->context, ret)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13283 | return (total); |
| 13284 | case XPATH_OP_PLUS: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13285 | bakd = ctxt->context->doc; |
| 13286 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13287 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13288 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13289 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13290 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13291 | if (op->ch2 != -1) { |
| 13292 | ctxt->context->doc = bakd; |
| 13293 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13294 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13295 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13296 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13297 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13298 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13299 | if (op->value == 0) |
| 13300 | xmlXPathSubValues(ctxt); |
| 13301 | else if (op->value == 1) |
| 13302 | xmlXPathAddValues(ctxt); |
| 13303 | else if (op->value == 2) |
| 13304 | xmlXPathValueFlipSign(ctxt); |
| 13305 | else if (op->value == 3) { |
| 13306 | CAST_TO_NUMBER; |
| 13307 | CHECK_TYPE0(XPATH_NUMBER); |
| 13308 | } |
| 13309 | return (total); |
| 13310 | case XPATH_OP_MULT: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13311 | bakd = ctxt->context->doc; |
| 13312 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13313 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13314 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13315 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13316 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13317 | ctxt->context->doc = bakd; |
| 13318 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13319 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13320 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13321 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13322 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13323 | if (op->value == 0) |
| 13324 | xmlXPathMultValues(ctxt); |
| 13325 | else if (op->value == 1) |
| 13326 | xmlXPathDivValues(ctxt); |
| 13327 | else if (op->value == 2) |
| 13328 | xmlXPathModValues(ctxt); |
| 13329 | return (total); |
| 13330 | case XPATH_OP_UNION: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13331 | bakd = ctxt->context->doc; |
| 13332 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13333 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13334 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13335 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13336 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 13337 | ctxt->context->doc = bakd; |
| 13338 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 13339 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 13340 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13341 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13342 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13343 | CHECK_TYPE0(XPATH_NODESET); |
| 13344 | arg2 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13345 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13346 | CHECK_TYPE0(XPATH_NODESET); |
| 13347 | arg1 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13348 | |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 13349 | if ((arg1->nodesetval == NULL) || |
| 13350 | ((arg2->nodesetval != NULL) && |
| 13351 | (arg2->nodesetval->nodeNr != 0))) |
| 13352 | { |
| 13353 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 13354 | arg2->nodesetval); |
| 13355 | } |
| 13356 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13357 | valuePush(ctxt, arg1); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13358 | xmlXPathReleaseObject(ctxt->context, arg2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13359 | return (total); |
| 13360 | case XPATH_OP_ROOT: |
| 13361 | xmlXPathRoot(ctxt); |
| 13362 | return (total); |
| 13363 | case XPATH_OP_NODE: |
| 13364 | if (op->ch1 != -1) |
| 13365 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13366 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13367 | if (op->ch2 != -1) |
| 13368 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13369 | CHECK_ERROR0; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13370 | valuePush(ctxt, xmlXPathCacheNewNodeSet(ctxt->context, |
| 13371 | ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13372 | return (total); |
| 13373 | case XPATH_OP_RESET: |
| 13374 | if (op->ch1 != -1) |
| 13375 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13376 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13377 | if (op->ch2 != -1) |
| 13378 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13379 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13380 | ctxt->context->node = NULL; |
| 13381 | return (total); |
| 13382 | case XPATH_OP_COLLECT:{ |
| 13383 | if (op->ch1 == -1) |
| 13384 | return (total); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 13385 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13386 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13387 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13388 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 13389 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL, 0); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13390 | return (total); |
| 13391 | } |
| 13392 | case XPATH_OP_VALUE: |
| 13393 | valuePush(ctxt, |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13394 | xmlXPathCacheObjectCopy(ctxt->context, |
| 13395 | (xmlXPathObjectPtr) op->value4)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13396 | return (total); |
| 13397 | case XPATH_OP_VARIABLE:{ |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13398 | xmlXPathObjectPtr val; |
| 13399 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13400 | if (op->ch1 != -1) |
| 13401 | total += |
| 13402 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13403 | if (op->value5 == NULL) { |
| 13404 | val = xmlXPathVariableLookup(ctxt->context, op->value4); |
| 13405 | if (val == NULL) { |
| 13406 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 13407 | return(0); |
| 13408 | } |
| 13409 | valuePush(ctxt, val); |
| 13410 | } else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13411 | const xmlChar *URI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13412 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13413 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 13414 | if (URI == NULL) { |
| 13415 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f63085d | 2009-01-18 20:53:59 +0000 | [diff] [blame] | 13416 | "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", |
| 13417 | (char *) op->value4, (char *)op->value5); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 13418 | ctxt->error = XPATH_UNDEF_PREFIX_ERROR; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13419 | return (total); |
| 13420 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13421 | val = xmlXPathVariableLookupNS(ctxt->context, |
| 13422 | op->value4, URI); |
| 13423 | if (val == NULL) { |
| 13424 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 13425 | return(0); |
| 13426 | } |
| 13427 | valuePush(ctxt, val); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13428 | } |
| 13429 | return (total); |
| 13430 | } |
| 13431 | case XPATH_OP_FUNCTION:{ |
| 13432 | xmlXPathFunction func; |
| 13433 | const xmlChar *oldFunc, *oldFuncURI; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13434 | int i; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13435 | int frame; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13436 | |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13437 | frame = xmlXPathSetFrame(ctxt); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13438 | if (op->ch1 != -1) |
| 13439 | total += |
| 13440 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13441 | if (ctxt->valueNr < op->value) { |
| 13442 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 13443 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13444 | ctxt->error = XPATH_INVALID_OPERAND; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13445 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13446 | return (total); |
| 13447 | } |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13448 | for (i = 0; i < op->value; i++) { |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13449 | if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { |
| 13450 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 13451 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13452 | ctxt->error = XPATH_INVALID_OPERAND; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13453 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13454 | return (total); |
| 13455 | } |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13456 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13457 | if (op->cache != NULL) |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 13458 | XML_CAST_FPTR(func) = op->cache; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13459 | else { |
| 13460 | const xmlChar *URI = NULL; |
| 13461 | |
| 13462 | if (op->value5 == NULL) |
| 13463 | func = |
| 13464 | xmlXPathFunctionLookup(ctxt->context, |
| 13465 | op->value4); |
| 13466 | else { |
| 13467 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 13468 | if (URI == NULL) { |
| 13469 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f63085d | 2009-01-18 20:53:59 +0000 | [diff] [blame] | 13470 | "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", |
| 13471 | (char *)op->value4, (char *)op->value5); |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13472 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 13473 | ctxt->error = XPATH_UNDEF_PREFIX_ERROR; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13474 | return (total); |
| 13475 | } |
| 13476 | func = xmlXPathFunctionLookupNS(ctxt->context, |
| 13477 | op->value4, URI); |
| 13478 | } |
| 13479 | if (func == NULL) { |
| 13480 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f63085d | 2009-01-18 20:53:59 +0000 | [diff] [blame] | 13481 | "xmlXPathCompOpEval: function %s not found\n", |
| 13482 | (char *)op->value4); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13483 | XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13484 | } |
William M. Brack | ad0e67c | 2004-12-01 14:35:10 +0000 | [diff] [blame] | 13485 | op->cache = XML_CAST_FPTR(func); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13486 | op->cacheURI = (void *) URI; |
| 13487 | } |
| 13488 | oldFunc = ctxt->context->function; |
| 13489 | oldFuncURI = ctxt->context->functionURI; |
| 13490 | ctxt->context->function = op->value4; |
| 13491 | ctxt->context->functionURI = op->cacheURI; |
| 13492 | func(ctxt, op->value); |
| 13493 | ctxt->context->function = oldFunc; |
| 13494 | ctxt->context->functionURI = oldFuncURI; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 13495 | xmlXPathPopFrame(ctxt, frame); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13496 | return (total); |
| 13497 | } |
| 13498 | case XPATH_OP_ARG: |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 13499 | bakd = ctxt->context->doc; |
| 13500 | bak = ctxt->context->node; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 13501 | pp = ctxt->context->proximityPosition; |
| 13502 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13503 | if (op->ch1 != -1) |
| 13504 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 13505 | ctxt->context->contextSize = cs; |
| 13506 | ctxt->context->proximityPosition = pp; |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 13507 | ctxt->context->node = bak; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 13508 | ctxt->context->doc = bakd; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13509 | CHECK_ERROR0; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13510 | if (op->ch2 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13511 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13512 | ctxt->context->doc = bakd; |
| 13513 | ctxt->context->node = bak; |
| 13514 | CHECK_ERROR0; |
| 13515 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13516 | return (total); |
| 13517 | case XPATH_OP_PREDICATE: |
| 13518 | case XPATH_OP_FILTER:{ |
| 13519 | xmlXPathObjectPtr res; |
| 13520 | xmlXPathObjectPtr obj, tmp; |
| 13521 | xmlNodeSetPtr newset = NULL; |
| 13522 | xmlNodeSetPtr oldset; |
| 13523 | xmlNodePtr oldnode; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13524 | xmlDocPtr oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13525 | int i; |
| 13526 | |
| 13527 | /* |
| 13528 | * Optimization for ()[1] selection i.e. the first elem |
| 13529 | */ |
| 13530 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13531 | #ifdef XP_OPTIMIZED_FILTER_FIRST |
| 13532 | /* |
| 13533 | * FILTER TODO: Can we assume that the inner processing |
| 13534 | * will result in an ordered list if we have an |
| 13535 | * XPATH_OP_FILTER? |
| 13536 | * What about an additional field or flag on |
| 13537 | * xmlXPathObject like @sorted ? This way we wouln'd need |
| 13538 | * to assume anything, so it would be more robust and |
| 13539 | * easier to optimize. |
| 13540 | */ |
| 13541 | ((comp->steps[op->ch1].op == XPATH_OP_SORT) || /* 18 */ |
| 13542 | (comp->steps[op->ch1].op == XPATH_OP_FILTER)) && /* 17 */ |
| 13543 | #else |
| 13544 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 13545 | #endif |
| 13546 | (comp->steps[op->ch2].op == XPATH_OP_VALUE)) { /* 12 */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13547 | xmlXPathObjectPtr val; |
| 13548 | |
| 13549 | val = comp->steps[op->ch2].value4; |
| 13550 | if ((val != NULL) && (val->type == XPATH_NUMBER) && |
| 13551 | (val->floatval == 1.0)) { |
| 13552 | xmlNodePtr first = NULL; |
| 13553 | |
| 13554 | total += |
| 13555 | xmlXPathCompOpEvalFirst(ctxt, |
| 13556 | &comp->steps[op->ch1], |
| 13557 | &first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13558 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13559 | /* |
| 13560 | * The nodeset should be in document order, |
| 13561 | * Keep only the first value |
| 13562 | */ |
| 13563 | if ((ctxt->value != NULL) && |
| 13564 | (ctxt->value->type == XPATH_NODESET) && |
| 13565 | (ctxt->value->nodesetval != NULL) && |
| 13566 | (ctxt->value->nodesetval->nodeNr > 1)) |
| 13567 | ctxt->value->nodesetval->nodeNr = 1; |
| 13568 | return (total); |
| 13569 | } |
| 13570 | } |
| 13571 | /* |
| 13572 | * Optimization for ()[last()] selection i.e. the last elem |
| 13573 | */ |
| 13574 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 13575 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 13576 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 13577 | int f = comp->steps[op->ch2].ch1; |
| 13578 | |
| 13579 | if ((f != -1) && |
| 13580 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 13581 | (comp->steps[f].value5 == NULL) && |
| 13582 | (comp->steps[f].value == 0) && |
| 13583 | (comp->steps[f].value4 != NULL) && |
| 13584 | (xmlStrEqual |
| 13585 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 13586 | xmlNodePtr last = NULL; |
| 13587 | |
| 13588 | total += |
| 13589 | xmlXPathCompOpEvalLast(ctxt, |
| 13590 | &comp->steps[op->ch1], |
| 13591 | &last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13592 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13593 | /* |
| 13594 | * The nodeset should be in document order, |
| 13595 | * Keep only the last value |
| 13596 | */ |
| 13597 | if ((ctxt->value != NULL) && |
| 13598 | (ctxt->value->type == XPATH_NODESET) && |
| 13599 | (ctxt->value->nodesetval != NULL) && |
| 13600 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 13601 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 13602 | ctxt->value->nodesetval->nodeTab[0] = |
| 13603 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 13604 | value-> |
| 13605 | nodesetval-> |
| 13606 | nodeNr - |
| 13607 | 1]; |
| 13608 | ctxt->value->nodesetval->nodeNr = 1; |
| 13609 | } |
| 13610 | return (total); |
| 13611 | } |
| 13612 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13613 | /* |
| 13614 | * Process inner predicates first. |
| 13615 | * Example "index[parent::book][1]": |
| 13616 | * ... |
| 13617 | * PREDICATE <-- we are here "[1]" |
| 13618 | * PREDICATE <-- process "[parent::book]" first |
| 13619 | * SORT |
| 13620 | * COLLECT 'parent' 'name' 'node' book |
| 13621 | * NODE |
| 13622 | * ELEM Object is a number : 1 |
| 13623 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13624 | if (op->ch1 != -1) |
| 13625 | total += |
| 13626 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13627 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13628 | if (op->ch2 == -1) |
| 13629 | return (total); |
| 13630 | if (ctxt->value == NULL) |
| 13631 | return (total); |
| 13632 | |
| 13633 | oldnode = ctxt->context->node; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13634 | |
| 13635 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13636 | /* |
| 13637 | * Hum are we filtering the result of an XPointer expression |
| 13638 | */ |
| 13639 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 13640 | xmlLocationSetPtr newlocset = NULL; |
| 13641 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13642 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13643 | /* |
| 13644 | * Extract the old locset, and then evaluate the result of the |
| 13645 | * expression for all the element in the locset. use it to grow |
| 13646 | * up a new locset. |
| 13647 | */ |
| 13648 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 13649 | obj = valuePop(ctxt); |
| 13650 | oldlocset = obj->user; |
| 13651 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13652 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13653 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 13654 | ctxt->context->contextSize = 0; |
| 13655 | ctxt->context->proximityPosition = 0; |
| 13656 | if (op->ch2 != -1) |
| 13657 | total += |
| 13658 | xmlXPathCompOpEval(ctxt, |
| 13659 | &comp->steps[op->ch2]); |
| 13660 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13661 | if (res != NULL) { |
| 13662 | xmlXPathReleaseObject(ctxt->context, res); |
| 13663 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13664 | valuePush(ctxt, obj); |
| 13665 | CHECK_ERROR0; |
| 13666 | return (total); |
| 13667 | } |
| 13668 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13669 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13670 | for (i = 0; i < oldlocset->locNr; i++) { |
| 13671 | /* |
| 13672 | * Run the evaluation with a node list made of a |
| 13673 | * single item in the nodelocset. |
| 13674 | */ |
| 13675 | ctxt->context->node = oldlocset->locTab[i]->user; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13676 | ctxt->context->contextSize = oldlocset->locNr; |
| 13677 | ctxt->context->proximityPosition = i + 1; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13678 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 13679 | ctxt->context->node); |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 13680 | valuePush(ctxt, tmp); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13681 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13682 | if (op->ch2 != -1) |
| 13683 | total += |
| 13684 | xmlXPathCompOpEval(ctxt, |
| 13685 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 13686 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13687 | xmlXPathFreeObject(obj); |
| 13688 | return(0); |
| 13689 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13690 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13691 | /* |
| 13692 | * The result of the evaluation need to be tested to |
| 13693 | * decided whether the filter succeeded or not |
| 13694 | */ |
| 13695 | res = valuePop(ctxt); |
| 13696 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 13697 | xmlXPtrLocationSetAdd(newlocset, |
| 13698 | xmlXPathObjectCopy |
| 13699 | (oldlocset->locTab[i])); |
| 13700 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13701 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13702 | /* |
| 13703 | * Cleanup |
| 13704 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13705 | if (res != NULL) { |
| 13706 | xmlXPathReleaseObject(ctxt->context, res); |
| 13707 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13708 | if (ctxt->value == tmp) { |
| 13709 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13710 | xmlXPathReleaseObject(ctxt->context, res); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13711 | } |
| 13712 | |
| 13713 | ctxt->context->node = NULL; |
| 13714 | } |
| 13715 | |
| 13716 | /* |
| 13717 | * The result is used as the new evaluation locset. |
| 13718 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13719 | xmlXPathReleaseObject(ctxt->context, obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13720 | ctxt->context->node = NULL; |
| 13721 | ctxt->context->contextSize = -1; |
| 13722 | ctxt->context->proximityPosition = -1; |
| 13723 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 13724 | ctxt->context->node = oldnode; |
| 13725 | return (total); |
| 13726 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13727 | #endif /* LIBXML_XPTR_ENABLED */ |
| 13728 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13729 | /* |
| 13730 | * Extract the old set, and then evaluate the result of the |
| 13731 | * expression for all the element in the set. use it to grow |
| 13732 | * up a new set. |
| 13733 | */ |
| 13734 | CHECK_TYPE0(XPATH_NODESET); |
| 13735 | obj = valuePop(ctxt); |
| 13736 | oldset = obj->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 13737 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13738 | oldnode = ctxt->context->node; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13739 | oldDoc = ctxt->context->doc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13740 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13741 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13742 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 13743 | ctxt->context->contextSize = 0; |
| 13744 | ctxt->context->proximityPosition = 0; |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 13745 | /* |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13746 | if (op->ch2 != -1) |
| 13747 | total += |
| 13748 | xmlXPathCompOpEval(ctxt, |
| 13749 | &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13750 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13751 | res = valuePop(ctxt); |
| 13752 | if (res != NULL) |
| 13753 | xmlXPathFreeObject(res); |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 13754 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13755 | valuePush(ctxt, obj); |
| 13756 | ctxt->context->node = oldnode; |
| 13757 | CHECK_ERROR0; |
| 13758 | } else { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13759 | tmp = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13760 | /* |
| 13761 | * Initialize the new set. |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13762 | * Also set the xpath document in case things like |
| 13763 | * key() evaluation are attempted on the predicate |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13764 | */ |
| 13765 | newset = xmlXPathNodeSetCreate(NULL); |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13766 | /* |
| 13767 | * SPEC XPath 1.0: |
| 13768 | * "For each node in the node-set to be filtered, the |
| 13769 | * PredicateExpr is evaluated with that node as the |
| 13770 | * context node, with the number of nodes in the |
| 13771 | * node-set as the context size, and with the proximity |
| 13772 | * position of the node in the node-set with respect to |
| 13773 | * the axis as the context position;" |
| 13774 | * @oldset is the node-set" to be filtered. |
| 13775 | * |
| 13776 | * SPEC XPath 1.0: |
| 13777 | * "only predicates change the context position and |
| 13778 | * context size (see [2.4 Predicates])." |
| 13779 | * Example: |
| 13780 | * node-set context pos |
| 13781 | * nA 1 |
| 13782 | * nB 2 |
| 13783 | * nC 3 |
| 13784 | * After applying predicate [position() > 1] : |
| 13785 | * node-set context pos |
| 13786 | * nB 1 |
| 13787 | * nC 2 |
| 13788 | * |
| 13789 | * removed the first node in the node-set, then |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13790 | * the context position of the |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13791 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13792 | for (i = 0; i < oldset->nodeNr; i++) { |
| 13793 | /* |
| 13794 | * Run the evaluation with a node list made of |
| 13795 | * a single item in the nodeset. |
| 13796 | */ |
| 13797 | ctxt->context->node = oldset->nodeTab[i]; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13798 | if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) && |
| 13799 | (oldset->nodeTab[i]->doc != NULL)) |
| 13800 | ctxt->context->doc = oldset->nodeTab[i]->doc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13801 | if (tmp == NULL) { |
| 13802 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 13803 | ctxt->context->node); |
| 13804 | } else { |
| 13805 | xmlXPathNodeSetAddUnique(tmp->nodesetval, |
| 13806 | ctxt->context->node); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13807 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13808 | valuePush(ctxt, tmp); |
| 13809 | ctxt->context->contextSize = oldset->nodeNr; |
| 13810 | ctxt->context->proximityPosition = i + 1; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13811 | /* |
| 13812 | * Evaluate the predicate against the context node. |
| 13813 | * Can/should we optimize position() predicates |
| 13814 | * here (e.g. "[1]")? |
| 13815 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13816 | if (op->ch2 != -1) |
| 13817 | total += |
| 13818 | xmlXPathCompOpEval(ctxt, |
| 13819 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 13820 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13821 | xmlXPathFreeNodeSet(newset); |
| 13822 | xmlXPathFreeObject(obj); |
| 13823 | return(0); |
| 13824 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13825 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13826 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13827 | * The result of the evaluation needs to be tested to |
| 13828 | * decide whether the filter succeeded or not |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13829 | */ |
| 13830 | /* |
| 13831 | * OPTIMIZE TODO: Can we use |
| 13832 | * xmlXPathNodeSetAdd*Unique()* instead? |
| 13833 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13834 | res = valuePop(ctxt); |
| 13835 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 13836 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 13837 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13838 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13839 | /* |
| 13840 | * Cleanup |
| 13841 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13842 | if (res != NULL) { |
| 13843 | xmlXPathReleaseObject(ctxt->context, res); |
| 13844 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13845 | if (ctxt->value == tmp) { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13846 | valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13847 | xmlXPathNodeSetClear(tmp->nodesetval, 1); |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13848 | /* |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13849 | * Don't free the temporary nodeset |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13850 | * in order to avoid massive recreation inside this |
| 13851 | * loop. |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 13852 | */ |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13853 | } else |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13854 | tmp = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13855 | ctxt->context->node = NULL; |
| 13856 | } |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 13857 | if (tmp != NULL) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13858 | xmlXPathReleaseObject(ctxt->context, tmp); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13859 | /* |
| 13860 | * The result is used as the new evaluation set. |
| 13861 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13862 | xmlXPathReleaseObject(ctxt->context, obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13863 | ctxt->context->node = NULL; |
| 13864 | ctxt->context->contextSize = -1; |
| 13865 | ctxt->context->proximityPosition = -1; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 13866 | /* may want to move this past the '}' later */ |
| 13867 | ctxt->context->doc = oldDoc; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13868 | valuePush(ctxt, |
| 13869 | xmlXPathCacheWrapNodeSet(ctxt->context, newset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13870 | } |
| 13871 | ctxt->context->node = oldnode; |
| 13872 | return (total); |
| 13873 | } |
| 13874 | case XPATH_OP_SORT: |
| 13875 | if (op->ch1 != -1) |
| 13876 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 13877 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13878 | if ((ctxt->value != NULL) && |
| 13879 | (ctxt->value->type == XPATH_NODESET) && |
Kasimier T. Buchcik | 64f7e1a | 2006-05-19 19:59:54 +0000 | [diff] [blame] | 13880 | (ctxt->value->nodesetval != NULL) && |
| 13881 | (ctxt->value->nodesetval->nodeNr > 1)) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13882 | { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13883 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13884 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13885 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13886 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13887 | case XPATH_OP_RANGETO:{ |
| 13888 | xmlXPathObjectPtr range; |
| 13889 | xmlXPathObjectPtr res, obj; |
| 13890 | xmlXPathObjectPtr tmp; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13891 | xmlLocationSetPtr newlocset = NULL; |
| 13892 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13893 | xmlNodeSetPtr oldset; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13894 | int i, j; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13895 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13896 | if (op->ch1 != -1) |
| 13897 | total += |
| 13898 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 13899 | if (op->ch2 == -1) |
| 13900 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13901 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13902 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 13903 | /* |
| 13904 | * Extract the old locset, and then evaluate the result of the |
| 13905 | * expression for all the element in the locset. use it to grow |
| 13906 | * up a new locset. |
| 13907 | */ |
| 13908 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 13909 | obj = valuePop(ctxt); |
| 13910 | oldlocset = obj->user; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13911 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13912 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13913 | ctxt->context->node = NULL; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13914 | ctxt->context->contextSize = 0; |
| 13915 | ctxt->context->proximityPosition = 0; |
| 13916 | total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]); |
| 13917 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13918 | if (res != NULL) { |
| 13919 | xmlXPathReleaseObject(ctxt->context, res); |
| 13920 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13921 | valuePush(ctxt, obj); |
| 13922 | CHECK_ERROR0; |
| 13923 | return (total); |
| 13924 | } |
| 13925 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13926 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13927 | for (i = 0; i < oldlocset->locNr; i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13928 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13929 | * Run the evaluation with a node list made of a |
| 13930 | * single item in the nodelocset. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13931 | */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 13932 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 13933 | ctxt->context->contextSize = oldlocset->locNr; |
| 13934 | ctxt->context->proximityPosition = i + 1; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13935 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 13936 | ctxt->context->node); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13937 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13938 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13939 | if (op->ch2 != -1) |
| 13940 | total += |
| 13941 | xmlXPathCompOpEval(ctxt, |
| 13942 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 13943 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 13944 | xmlXPathFreeObject(obj); |
| 13945 | return(0); |
| 13946 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13947 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13948 | res = valuePop(ctxt); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13949 | if (res->type == XPATH_LOCATIONSET) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 13950 | xmlLocationSetPtr rloc = |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13951 | (xmlLocationSetPtr)res->user; |
| 13952 | for (j=0; j<rloc->locNr; j++) { |
| 13953 | range = xmlXPtrNewRange( |
| 13954 | oldlocset->locTab[i]->user, |
| 13955 | oldlocset->locTab[i]->index, |
| 13956 | rloc->locTab[j]->user2, |
| 13957 | rloc->locTab[j]->index2); |
| 13958 | if (range != NULL) { |
| 13959 | xmlXPtrLocationSetAdd(newlocset, range); |
| 13960 | } |
| 13961 | } |
| 13962 | } else { |
| 13963 | range = xmlXPtrNewRangeNodeObject( |
| 13964 | (xmlNodePtr)oldlocset->locTab[i]->user, res); |
| 13965 | if (range != NULL) { |
| 13966 | xmlXPtrLocationSetAdd(newlocset,range); |
| 13967 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13968 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 13969 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13970 | /* |
| 13971 | * Cleanup |
| 13972 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13973 | if (res != NULL) { |
| 13974 | xmlXPathReleaseObject(ctxt->context, res); |
| 13975 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13976 | if (ctxt->value == tmp) { |
| 13977 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13978 | xmlXPathReleaseObject(ctxt->context, res); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 13979 | } |
| 13980 | |
| 13981 | ctxt->context->node = NULL; |
| 13982 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13983 | } else { /* Not a location set */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 13984 | CHECK_TYPE0(XPATH_NODESET); |
| 13985 | obj = valuePop(ctxt); |
| 13986 | oldset = obj->nodesetval; |
| 13987 | ctxt->context->node = NULL; |
| 13988 | |
| 13989 | newlocset = xmlXPtrLocationSetCreate(NULL); |
| 13990 | |
| 13991 | if (oldset != NULL) { |
| 13992 | for (i = 0; i < oldset->nodeNr; i++) { |
| 13993 | /* |
| 13994 | * Run the evaluation with a node list made of a single item |
| 13995 | * in the nodeset. |
| 13996 | */ |
| 13997 | ctxt->context->node = oldset->nodeTab[i]; |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 13998 | /* |
| 13999 | * OPTIMIZE TODO: Avoid recreation for every iteration. |
| 14000 | */ |
| 14001 | tmp = xmlXPathCacheNewNodeSet(ctxt->context, |
| 14002 | ctxt->context->node); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14003 | valuePush(ctxt, tmp); |
| 14004 | |
| 14005 | if (op->ch2 != -1) |
| 14006 | total += |
| 14007 | xmlXPathCompOpEval(ctxt, |
| 14008 | &comp->steps[op->ch2]); |
William M. Brack | 2c19a7b | 2005-04-10 01:03:23 +0000 | [diff] [blame] | 14009 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 14010 | xmlXPathFreeObject(obj); |
| 14011 | return(0); |
| 14012 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14013 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14014 | res = valuePop(ctxt); |
| 14015 | range = |
| 14016 | xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], |
| 14017 | res); |
| 14018 | if (range != NULL) { |
| 14019 | xmlXPtrLocationSetAdd(newlocset, range); |
| 14020 | } |
| 14021 | |
| 14022 | /* |
| 14023 | * Cleanup |
| 14024 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14025 | if (res != NULL) { |
| 14026 | xmlXPathReleaseObject(ctxt->context, res); |
| 14027 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14028 | if (ctxt->value == tmp) { |
| 14029 | res = valuePop(ctxt); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14030 | xmlXPathReleaseObject(ctxt->context, res); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14031 | } |
| 14032 | |
| 14033 | ctxt->context->node = NULL; |
| 14034 | } |
| 14035 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14036 | } |
| 14037 | |
| 14038 | /* |
| 14039 | * The result is used as the new evaluation set. |
| 14040 | */ |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14041 | xmlXPathReleaseObject(ctxt->context, obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14042 | ctxt->context->node = NULL; |
| 14043 | ctxt->context->contextSize = -1; |
| 14044 | ctxt->context->proximityPosition = -1; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14045 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14046 | return (total); |
| 14047 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14048 | #endif /* LIBXML_XPTR_ENABLED */ |
| 14049 | } |
| 14050 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14051 | "XPath: unknown precompiled operation %d\n", op->op); |
Daniel Veillard | 1d4526f | 2011-10-11 16:34:34 +0800 | [diff] [blame] | 14052 | ctxt->error = XPATH_INVALID_OPERAND; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14053 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14054 | } |
| 14055 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14056 | /** |
| 14057 | * xmlXPathCompOpEvalToBoolean: |
| 14058 | * @ctxt: the XPath parser context |
| 14059 | * |
| 14060 | * Evaluates if the expression evaluates to true. |
| 14061 | * |
| 14062 | * Returns 1 if true, 0 if false and -1 on API or internal errors. |
| 14063 | */ |
| 14064 | static int |
| 14065 | xmlXPathCompOpEvalToBoolean(xmlXPathParserContextPtr ctxt, |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14066 | xmlXPathStepOpPtr op, |
| 14067 | int isPredicate) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14068 | { |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14069 | xmlXPathObjectPtr resObj = NULL; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14070 | |
| 14071 | start: |
| 14072 | /* comp = ctxt->comp; */ |
| 14073 | switch (op->op) { |
| 14074 | case XPATH_OP_END: |
| 14075 | return (0); |
| 14076 | case XPATH_OP_VALUE: |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14077 | resObj = (xmlXPathObjectPtr) op->value4; |
Kasimier T. Buchcik | 2bdb12f | 2006-06-29 10:49:59 +0000 | [diff] [blame] | 14078 | if (isPredicate) |
| 14079 | return(xmlXPathEvaluatePredicateResult(ctxt, resObj)); |
| 14080 | return(xmlXPathCastToBoolean(resObj)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14081 | case XPATH_OP_SORT: |
| 14082 | /* |
| 14083 | * We don't need sorting for boolean results. Skip this one. |
| 14084 | */ |
| 14085 | if (op->ch1 != -1) { |
| 14086 | op = &ctxt->comp->steps[op->ch1]; |
| 14087 | goto start; |
| 14088 | } |
| 14089 | return(0); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14090 | case XPATH_OP_COLLECT: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14091 | if (op->ch1 == -1) |
| 14092 | return(0); |
| 14093 | |
| 14094 | xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch1]); |
| 14095 | if (ctxt->error != XPATH_EXPRESSION_OK) |
| 14096 | return(-1); |
| 14097 | |
| 14098 | xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL, 1); |
| 14099 | if (ctxt->error != XPATH_EXPRESSION_OK) |
| 14100 | return(-1); |
| 14101 | |
| 14102 | resObj = valuePop(ctxt); |
| 14103 | if (resObj == NULL) |
| 14104 | return(-1); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14105 | break; |
| 14106 | default: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14107 | /* |
| 14108 | * Fallback to call xmlXPathCompOpEval(). |
| 14109 | */ |
| 14110 | xmlXPathCompOpEval(ctxt, op); |
| 14111 | if (ctxt->error != XPATH_EXPRESSION_OK) |
| 14112 | return(-1); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14113 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14114 | resObj = valuePop(ctxt); |
| 14115 | if (resObj == NULL) |
Kasimier T. Buchcik | 2bdb12f | 2006-06-29 10:49:59 +0000 | [diff] [blame] | 14116 | return(-1); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14117 | break; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14118 | } |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14119 | |
| 14120 | if (resObj) { |
| 14121 | int res; |
| 14122 | |
| 14123 | if (resObj->type == XPATH_BOOLEAN) { |
| 14124 | res = resObj->boolval; |
| 14125 | } else if (isPredicate) { |
| 14126 | /* |
| 14127 | * For predicates a result of type "number" is handled |
| 14128 | * differently: |
| 14129 | * SPEC XPath 1.0: |
| 14130 | * "If the result is a number, the result will be converted |
| 14131 | * to true if the number is equal to the context position |
| 14132 | * and will be converted to false otherwise;" |
| 14133 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14134 | res = xmlXPathEvaluatePredicateResult(ctxt, resObj); |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14135 | } else { |
| 14136 | res = xmlXPathCastToBoolean(resObj); |
| 14137 | } |
| 14138 | xmlXPathReleaseObject(ctxt->context, resObj); |
| 14139 | return(res); |
| 14140 | } |
| 14141 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14142 | return(0); |
| 14143 | } |
| 14144 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14145 | #ifdef XPATH_STREAMING |
| 14146 | /** |
| 14147 | * xmlXPathRunStreamEval: |
| 14148 | * @ctxt: the XPath parser context with the compiled expression |
| 14149 | * |
| 14150 | * Evaluate the Precompiled Streamable XPath expression in the given context. |
| 14151 | */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14152 | static int |
| 14153 | xmlXPathRunStreamEval(xmlXPathContextPtr ctxt, xmlPatternPtr comp, |
| 14154 | xmlXPathObjectPtr *resultSeq, int toBool) |
| 14155 | { |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14156 | int max_depth, min_depth; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14157 | int from_root; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14158 | int ret, depth; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14159 | int eval_all_nodes; |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 14160 | xmlNodePtr cur = NULL, limit = NULL; |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14161 | xmlStreamCtxtPtr patstream = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14162 | |
| 14163 | int nb_nodes = 0; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14164 | |
| 14165 | if ((ctxt == NULL) || (comp == NULL)) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14166 | return(-1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14167 | max_depth = xmlPatternMaxDepth(comp); |
| 14168 | if (max_depth == -1) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14169 | return(-1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14170 | if (max_depth == -2) |
| 14171 | max_depth = 10000; |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14172 | min_depth = xmlPatternMinDepth(comp); |
| 14173 | if (min_depth == -1) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14174 | return(-1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14175 | from_root = xmlPatternFromRoot(comp); |
| 14176 | if (from_root < 0) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14177 | return(-1); |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 14178 | #if 0 |
| 14179 | printf("stream eval: depth %d from root %d\n", max_depth, from_root); |
| 14180 | #endif |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14181 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14182 | if (! toBool) { |
| 14183 | if (resultSeq == NULL) |
| 14184 | return(-1); |
| 14185 | *resultSeq = xmlXPathCacheNewNodeSet(ctxt, NULL); |
| 14186 | if (*resultSeq == NULL) |
| 14187 | return(-1); |
| 14188 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14189 | |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14190 | /* |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14191 | * handle the special cases of "/" amd "." being matched |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14192 | */ |
| 14193 | if (min_depth == 0) { |
| 14194 | if (from_root) { |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14195 | /* Select "/" */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14196 | if (toBool) |
| 14197 | return(1); |
| 14198 | xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, |
| 14199 | (xmlNodePtr) ctxt->doc); |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14200 | } else { |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14201 | /* Select "self::node()" */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14202 | if (toBool) |
| 14203 | return(1); |
| 14204 | xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, ctxt->node); |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14205 | } |
| 14206 | } |
| 14207 | if (max_depth == 0) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14208 | return(0); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14209 | } |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 14210 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14211 | if (from_root) { |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 14212 | cur = (xmlNodePtr)ctxt->doc; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14213 | } else if (ctxt->node != NULL) { |
| 14214 | switch (ctxt->node->type) { |
| 14215 | case XML_ELEMENT_NODE: |
| 14216 | case XML_DOCUMENT_NODE: |
| 14217 | case XML_DOCUMENT_FRAG_NODE: |
| 14218 | case XML_HTML_DOCUMENT_NODE: |
| 14219 | #ifdef LIBXML_DOCB_ENABLED |
| 14220 | case XML_DOCB_DOCUMENT_NODE: |
| 14221 | #endif |
| 14222 | cur = ctxt->node; |
| 14223 | break; |
| 14224 | case XML_ATTRIBUTE_NODE: |
| 14225 | case XML_TEXT_NODE: |
| 14226 | case XML_CDATA_SECTION_NODE: |
| 14227 | case XML_ENTITY_REF_NODE: |
| 14228 | case XML_ENTITY_NODE: |
| 14229 | case XML_PI_NODE: |
| 14230 | case XML_COMMENT_NODE: |
| 14231 | case XML_NOTATION_NODE: |
| 14232 | case XML_DTD_NODE: |
| 14233 | case XML_DOCUMENT_TYPE_NODE: |
| 14234 | case XML_ELEMENT_DECL: |
| 14235 | case XML_ATTRIBUTE_DECL: |
| 14236 | case XML_ENTITY_DECL: |
| 14237 | case XML_NAMESPACE_DECL: |
| 14238 | case XML_XINCLUDE_START: |
| 14239 | case XML_XINCLUDE_END: |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14240 | break; |
| 14241 | } |
| 14242 | limit = cur; |
| 14243 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14244 | if (cur == NULL) { |
| 14245 | return(0); |
| 14246 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14247 | |
| 14248 | patstream = xmlPatternGetStreamCtxt(comp); |
| 14249 | if (patstream == NULL) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14250 | /* |
| 14251 | * QUESTION TODO: Is this an error? |
| 14252 | */ |
| 14253 | return(0); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14254 | } |
| 14255 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14256 | eval_all_nodes = xmlStreamWantsAnyNode(patstream); |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14257 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14258 | if (from_root) { |
| 14259 | ret = xmlStreamPush(patstream, NULL, NULL); |
| 14260 | if (ret < 0) { |
| 14261 | } else if (ret == 1) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14262 | if (toBool) |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14263 | goto return_1; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14264 | xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, cur); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14265 | } |
| 14266 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14267 | depth = 0; |
| 14268 | goto scan_children; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14269 | next_node: |
Daniel Veillard | d3ff7ef | 2006-02-27 19:43:17 +0000 | [diff] [blame] | 14270 | do { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14271 | nb_nodes++; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14272 | |
| 14273 | switch (cur->type) { |
| 14274 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14275 | case XML_TEXT_NODE: |
| 14276 | case XML_CDATA_SECTION_NODE: |
| 14277 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14278 | case XML_PI_NODE: |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14279 | if (cur->type == XML_ELEMENT_NODE) { |
| 14280 | ret = xmlStreamPush(patstream, cur->name, |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14281 | (cur->ns ? cur->ns->href : NULL)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14282 | } else if (eval_all_nodes) |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14283 | ret = xmlStreamPushNode(patstream, NULL, NULL, cur->type); |
| 14284 | else |
| 14285 | break; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14286 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14287 | if (ret < 0) { |
| 14288 | /* NOP. */ |
| 14289 | } else if (ret == 1) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14290 | if (toBool) |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14291 | goto return_1; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14292 | xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, cur); |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14293 | } |
| 14294 | if ((cur->children == NULL) || (depth >= max_depth)) { |
| 14295 | ret = xmlStreamPop(patstream); |
| 14296 | while (cur->next != NULL) { |
| 14297 | cur = cur->next; |
| 14298 | if ((cur->type != XML_ENTITY_DECL) && |
| 14299 | (cur->type != XML_DTD_NODE)) |
| 14300 | goto next_node; |
| 14301 | } |
| 14302 | } |
| 14303 | default: |
| 14304 | break; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14305 | } |
| 14306 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14307 | scan_children: |
| 14308 | if ((cur->children != NULL) && (depth < max_depth)) { |
| 14309 | /* |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14310 | * Do not descend on entities declarations |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14311 | */ |
| 14312 | if (cur->children->type != XML_ENTITY_DECL) { |
| 14313 | cur = cur->children; |
| 14314 | depth++; |
| 14315 | /* |
| 14316 | * Skip DTDs |
| 14317 | */ |
| 14318 | if (cur->type != XML_DTD_NODE) |
| 14319 | continue; |
| 14320 | } |
| 14321 | } |
| 14322 | |
| 14323 | if (cur == limit) |
| 14324 | break; |
| 14325 | |
| 14326 | while (cur->next != NULL) { |
| 14327 | cur = cur->next; |
| 14328 | if ((cur->type != XML_ENTITY_DECL) && |
| 14329 | (cur->type != XML_DTD_NODE)) |
| 14330 | goto next_node; |
| 14331 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14332 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14333 | do { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14334 | cur = cur->parent; |
| 14335 | depth--; |
| 14336 | if ((cur == NULL) || (cur == limit)) |
| 14337 | goto done; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14338 | if (cur->type == XML_ELEMENT_NODE) { |
| 14339 | ret = xmlStreamPop(patstream); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14340 | } else if ((eval_all_nodes) && |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 14341 | ((cur->type == XML_TEXT_NODE) || |
| 14342 | (cur->type == XML_CDATA_SECTION_NODE) || |
| 14343 | (cur->type == XML_COMMENT_NODE) || |
| 14344 | (cur->type == XML_PI_NODE))) |
| 14345 | { |
| 14346 | ret = xmlStreamPop(patstream); |
| 14347 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14348 | if (cur->next != NULL) { |
| 14349 | cur = cur->next; |
| 14350 | break; |
| 14351 | } |
| 14352 | } while (cur != NULL); |
| 14353 | |
| 14354 | } while ((cur != NULL) && (depth >= 0)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14355 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14356 | done: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14357 | |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 14358 | #if 0 |
| 14359 | printf("stream eval: checked %d nodes selected %d\n", |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14360 | nb_nodes, retObj->nodesetval->nodeNr); |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 14361 | #endif |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14362 | |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14363 | if (patstream) |
| 14364 | xmlFreeStreamCtxt(patstream); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14365 | return(0); |
Kasimier T. Buchcik | 8af1f0b | 2006-06-28 17:13:19 +0000 | [diff] [blame] | 14366 | |
| 14367 | return_1: |
| 14368 | if (patstream) |
| 14369 | xmlFreeStreamCtxt(patstream); |
| 14370 | return(1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14371 | } |
| 14372 | #endif /* XPATH_STREAMING */ |
| 14373 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14374 | /** |
| 14375 | * xmlXPathRunEval: |
| 14376 | * @ctxt: the XPath parser context with the compiled expression |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14377 | * @toBool: evaluate to a boolean result |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14378 | * |
| 14379 | * Evaluate the Precompiled XPath expression in the given context. |
| 14380 | */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14381 | static int |
| 14382 | xmlXPathRunEval(xmlXPathParserContextPtr ctxt, int toBool) |
| 14383 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14384 | xmlXPathCompExprPtr comp; |
| 14385 | |
| 14386 | if ((ctxt == NULL) || (ctxt->comp == NULL)) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14387 | return(-1); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14388 | |
| 14389 | if (ctxt->valueTab == NULL) { |
| 14390 | /* Allocate the value stack */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14391 | ctxt->valueTab = (xmlXPathObjectPtr *) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14392 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
| 14393 | if (ctxt->valueTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 14394 | xmlXPathPErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14395 | xmlFree(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14396 | } |
| 14397 | ctxt->valueNr = 0; |
| 14398 | ctxt->valueMax = 10; |
| 14399 | ctxt->value = NULL; |
Daniel Veillard | f5048b3 | 2011-08-18 17:10:13 +0800 | [diff] [blame] | 14400 | ctxt->valueFrame = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14401 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14402 | #ifdef XPATH_STREAMING |
| 14403 | if (ctxt->comp->stream) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14404 | int res; |
| 14405 | |
| 14406 | if (toBool) { |
| 14407 | /* |
| 14408 | * Evaluation to boolean result. |
| 14409 | */ |
| 14410 | res = xmlXPathRunStreamEval(ctxt->context, |
| 14411 | ctxt->comp->stream, NULL, 1); |
| 14412 | if (res != -1) |
| 14413 | return(res); |
| 14414 | } else { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14415 | xmlXPathObjectPtr resObj = NULL; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14416 | |
| 14417 | /* |
| 14418 | * Evaluation to a sequence. |
| 14419 | */ |
| 14420 | res = xmlXPathRunStreamEval(ctxt->context, |
| 14421 | ctxt->comp->stream, &resObj, 0); |
| 14422 | |
| 14423 | if ((res != -1) && (resObj != NULL)) { |
| 14424 | valuePush(ctxt, resObj); |
| 14425 | return(0); |
| 14426 | } |
| 14427 | if (resObj != NULL) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14428 | xmlXPathReleaseObject(ctxt->context, resObj); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14429 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14430 | /* |
| 14431 | * QUESTION TODO: This falls back to normal XPath evaluation |
| 14432 | * if res == -1. Is this intended? |
| 14433 | */ |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14434 | } |
| 14435 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14436 | comp = ctxt->comp; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14437 | if (comp->last < 0) { |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 14438 | xmlGenericError(xmlGenericErrorContext, |
| 14439 | "xmlXPathRunEval: last is less than zero\n"); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14440 | return(-1); |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 14441 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14442 | if (toBool) |
Kasimier T. Buchcik | 324c75b | 2006-06-29 10:31:35 +0000 | [diff] [blame] | 14443 | return(xmlXPathCompOpEvalToBoolean(ctxt, |
| 14444 | &comp->steps[comp->last], 0)); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14445 | else |
| 14446 | xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]); |
| 14447 | |
| 14448 | return(0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14449 | } |
| 14450 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14451 | /************************************************************************ |
| 14452 | * * |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14453 | * Public interfaces * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14454 | * * |
| 14455 | ************************************************************************/ |
| 14456 | |
| 14457 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14458 | * xmlXPathEvalPredicate: |
| 14459 | * @ctxt: the XPath context |
| 14460 | * @res: the Predicate Expression evaluation result |
| 14461 | * |
| 14462 | * Evaluate a predicate result for the current node. |
| 14463 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 14464 | * the result to a boolean. If the result is a number, the result will |
| 14465 | * be converted to true if the number is equal to the position of the |
| 14466 | * context node in the context node list (as returned by the position |
| 14467 | * function) and will be converted to false otherwise; if the result |
| 14468 | * is not a number, then the result will be converted as if by a call |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14469 | * to the boolean function. |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14470 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14471 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14472 | */ |
| 14473 | int |
| 14474 | xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 14475 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14476 | switch (res->type) { |
| 14477 | case XPATH_BOOLEAN: |
| 14478 | return(res->boolval); |
| 14479 | case XPATH_NUMBER: |
| 14480 | return(res->floatval == ctxt->proximityPosition); |
| 14481 | case XPATH_NODESET: |
| 14482 | case XPATH_XSLT_TREE: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 14483 | if (res->nodesetval == NULL) |
| 14484 | return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 14485 | return(res->nodesetval->nodeNr != 0); |
| 14486 | case XPATH_STRING: |
| 14487 | return((res->stringval != NULL) && |
| 14488 | (xmlStrlen(res->stringval) != 0)); |
| 14489 | default: |
| 14490 | STRANGE |
| 14491 | } |
| 14492 | return(0); |
| 14493 | } |
| 14494 | |
| 14495 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14496 | * xmlXPathEvaluatePredicateResult: |
| 14497 | * @ctxt: the XPath Parser context |
| 14498 | * @res: the Predicate Expression evaluation result |
| 14499 | * |
| 14500 | * Evaluate a predicate result for the current node. |
| 14501 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 14502 | * the result to a boolean. If the result is a number, the result will |
| 14503 | * be converted to true if the number is equal to the position of the |
| 14504 | * context node in the context node list (as returned by the position |
| 14505 | * function) and will be converted to false otherwise; if the result |
| 14506 | * is not a number, then the result will be converted as if by a call |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14507 | * to the boolean function. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14508 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14509 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14510 | */ |
| 14511 | int |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14512 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14513 | xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 14514 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14515 | switch (res->type) { |
| 14516 | case XPATH_BOOLEAN: |
| 14517 | return(res->boolval); |
| 14518 | case XPATH_NUMBER: |
Daniel Veillard | 9ea6231 | 2004-04-29 14:04:09 +0000 | [diff] [blame] | 14519 | #if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200)) |
Daniel Veillard | 7c4eb63 | 2004-04-19 21:29:12 +0000 | [diff] [blame] | 14520 | return((res->floatval == ctxt->context->proximityPosition) && |
| 14521 | (!xmlXPathIsNaN(res->floatval))); /* MSC pbm Mark Vakoc !*/ |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 14522 | #else |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14523 | return(res->floatval == ctxt->context->proximityPosition); |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 14524 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14525 | case XPATH_NODESET: |
| 14526 | case XPATH_XSLT_TREE: |
Daniel Veillard | 73639a7 | 2001-04-10 14:31:39 +0000 | [diff] [blame] | 14527 | if (res->nodesetval == NULL) |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 14528 | return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14529 | return(res->nodesetval->nodeNr != 0); |
| 14530 | case XPATH_STRING: |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14531 | return((res->stringval != NULL) && (res->stringval[0] != 0)); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 14532 | #ifdef LIBXML_XPTR_ENABLED |
| 14533 | case XPATH_LOCATIONSET:{ |
| 14534 | xmlLocationSetPtr ptr = res->user; |
| 14535 | if (ptr == NULL) |
| 14536 | return(0); |
| 14537 | return (ptr->locNr != 0); |
| 14538 | } |
| 14539 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14540 | default: |
| 14541 | STRANGE |
| 14542 | } |
| 14543 | return(0); |
| 14544 | } |
| 14545 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14546 | #ifdef XPATH_STREAMING |
| 14547 | /** |
| 14548 | * xmlXPathTryStreamCompile: |
| 14549 | * @ctxt: an XPath context |
| 14550 | * @str: the XPath expression |
| 14551 | * |
| 14552 | * Try to compile the XPath expression as a streamable subset. |
| 14553 | * |
| 14554 | * Returns the compiled expression or NULL if failed to compile. |
| 14555 | */ |
| 14556 | static xmlXPathCompExprPtr |
| 14557 | xmlXPathTryStreamCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 14558 | /* |
| 14559 | * Optimization: use streaming patterns when the XPath expression can |
| 14560 | * be compiled to a stream lookup |
| 14561 | */ |
| 14562 | xmlPatternPtr stream; |
| 14563 | xmlXPathCompExprPtr comp; |
| 14564 | xmlDictPtr dict = NULL; |
| 14565 | const xmlChar **namespaces = NULL; |
| 14566 | xmlNsPtr ns; |
| 14567 | int i, j; |
| 14568 | |
| 14569 | if ((!xmlStrchr(str, '[')) && (!xmlStrchr(str, '(')) && |
| 14570 | (!xmlStrchr(str, '@'))) { |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 14571 | const xmlChar *tmp; |
| 14572 | |
| 14573 | /* |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 14574 | * We don't try to handle expressions using the verbose axis |
| 14575 | * specifiers ("::"), just the simplied form at this point. |
| 14576 | * Additionally, if there is no list of namespaces available and |
| 14577 | * there's a ":" in the expression, indicating a prefixed QName, |
| 14578 | * then we won't try to compile either. xmlPatterncompile() needs |
| 14579 | * to have a list of namespaces at compilation time in order to |
| 14580 | * compile prefixed name tests. |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 14581 | */ |
| 14582 | tmp = xmlStrchr(str, ':'); |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 14583 | if ((tmp != NULL) && |
| 14584 | ((ctxt == NULL) || (ctxt->nsNr == 0) || (tmp[1] == ':'))) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14585 | return(NULL); |
Daniel Veillard | 1f33c4d | 2005-07-10 21:38:31 +0000 | [diff] [blame] | 14586 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14587 | if (ctxt != NULL) { |
| 14588 | dict = ctxt->dict; |
| 14589 | if (ctxt->nsNr > 0) { |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 14590 | namespaces = xmlMalloc(2 * (ctxt->nsNr + 1) * sizeof(xmlChar*)); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14591 | if (namespaces == NULL) { |
| 14592 | xmlXPathErrMemory(ctxt, "allocating namespaces array\n"); |
| 14593 | return(NULL); |
| 14594 | } |
| 14595 | for (i = 0, j = 0; (j < ctxt->nsNr); j++) { |
| 14596 | ns = ctxt->namespaces[j]; |
| 14597 | namespaces[i++] = ns->href; |
| 14598 | namespaces[i++] = ns->prefix; |
| 14599 | } |
| 14600 | namespaces[i++] = NULL; |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 14601 | namespaces[i] = NULL; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14602 | } |
| 14603 | } |
| 14604 | |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 14605 | stream = xmlPatterncompile(str, dict, XML_PATTERN_XPATH, |
| 14606 | &namespaces[0]); |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 14607 | if (namespaces != NULL) { |
| 14608 | xmlFree((xmlChar **)namespaces); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14609 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14610 | if ((stream != NULL) && (xmlPatternStreamable(stream) == 1)) { |
| 14611 | comp = xmlXPathNewCompExpr(); |
| 14612 | if (comp == NULL) { |
| 14613 | xmlXPathErrMemory(ctxt, "allocating streamable expression\n"); |
| 14614 | return(NULL); |
| 14615 | } |
| 14616 | comp->stream = stream; |
| 14617 | comp->dict = dict; |
| 14618 | if (comp->dict) |
| 14619 | xmlDictReference(comp->dict); |
| 14620 | return(comp); |
| 14621 | } |
| 14622 | xmlFreePattern(stream); |
| 14623 | } |
| 14624 | return(NULL); |
| 14625 | } |
| 14626 | #endif /* XPATH_STREAMING */ |
| 14627 | |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14628 | static int |
| 14629 | xmlXPathCanRewriteDosExpression(xmlChar *expr) |
| 14630 | { |
| 14631 | if (expr == NULL) |
| 14632 | return(0); |
| 14633 | do { |
| 14634 | if ((*expr == '/') && (*(++expr) == '/')) |
| 14635 | return(1); |
| 14636 | } while (*expr++); |
| 14637 | return(0); |
| 14638 | } |
| 14639 | static void |
| 14640 | xmlXPathRewriteDOSExpression(xmlXPathCompExprPtr comp, xmlXPathStepOpPtr op) |
| 14641 | { |
| 14642 | /* |
| 14643 | * Try to rewrite "descendant-or-self::node()/foo" to an optimized |
| 14644 | * internal representation. |
| 14645 | */ |
| 14646 | if (op->ch1 != -1) { |
| 14647 | if ((op->op == XPATH_OP_COLLECT /* 11 */) && |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14648 | ((xmlXPathAxisVal) op->value == AXIS_CHILD /* 4 */) && |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14649 | ((xmlXPathTestVal) op->value2 == NODE_TEST_NAME /* 5 */) && |
| 14650 | ((xmlXPathTypeVal) op->value3 == NODE_TYPE_NODE /* 0 */)) |
| 14651 | { |
| 14652 | /* |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 14653 | * This is a "child::foo" |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14654 | */ |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14655 | xmlXPathStepOpPtr prevop = &comp->steps[op->ch1]; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14656 | |
| 14657 | if ((prevop->op == XPATH_OP_COLLECT /* 11 */) && |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14658 | (prevop->ch1 != -1) && |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14659 | ((xmlXPathAxisVal) prevop->value == |
| 14660 | AXIS_DESCENDANT_OR_SELF) && |
| 14661 | (prevop->ch2 == -1) && |
| 14662 | ((xmlXPathTestVal) prevop->value2 == NODE_TEST_TYPE) && |
Kasimier T. Buchcik | 080152c | 2006-06-06 09:42:15 +0000 | [diff] [blame] | 14663 | ((xmlXPathTypeVal) prevop->value3 == NODE_TYPE_NODE) && |
| 14664 | (comp->steps[prevop->ch1].op == XPATH_OP_ROOT)) |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14665 | { |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14666 | /* |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 14667 | * This is a "/descendant-or-self::node()" without predicates. |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14668 | * Eliminate it. |
| 14669 | */ |
| 14670 | op->ch1 = prevop->ch1; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14671 | op->rewriteType = XP_REWRITE_DOS_CHILD_ELEM; |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14672 | } |
| 14673 | } |
| 14674 | if (op->ch1 != -1) |
| 14675 | xmlXPathRewriteDOSExpression(comp, &comp->steps[op->ch1]); |
| 14676 | } |
| 14677 | if (op->ch2 != -1) |
| 14678 | xmlXPathRewriteDOSExpression(comp, &comp->steps[op->ch2]); |
| 14679 | } |
| 14680 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14681 | /** |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14682 | * xmlXPathCtxtCompile: |
| 14683 | * @ctxt: an XPath context |
| 14684 | * @str: the XPath expression |
| 14685 | * |
| 14686 | * Compile an XPath expression |
| 14687 | * |
| 14688 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
| 14689 | * the caller has to free the object. |
| 14690 | */ |
| 14691 | xmlXPathCompExprPtr |
| 14692 | xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 14693 | xmlXPathParserContextPtr pctxt; |
| 14694 | xmlXPathCompExprPtr comp; |
| 14695 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14696 | #ifdef XPATH_STREAMING |
| 14697 | comp = xmlXPathTryStreamCompile(ctxt, str); |
| 14698 | if (comp != NULL) |
| 14699 | return(comp); |
| 14700 | #endif |
| 14701 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14702 | xmlXPathInit(); |
| 14703 | |
| 14704 | pctxt = xmlXPathNewParserContext(str, ctxt); |
William M. Brack | 11be2d0 | 2007-01-24 19:17:19 +0000 | [diff] [blame] | 14705 | if (pctxt == NULL) |
| 14706 | return NULL; |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 14707 | xmlXPathCompileExpr(pctxt, 1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14708 | |
| 14709 | if( pctxt->error != XPATH_EXPRESSION_OK ) |
| 14710 | { |
| 14711 | xmlXPathFreeParserContext(pctxt); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 14712 | return(NULL); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14713 | } |
| 14714 | |
| 14715 | if (*pctxt->cur != 0) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14716 | /* |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14717 | * aleksey: in some cases this line prints *second* error message |
| 14718 | * (see bug #78858) and probably this should be fixed. |
| 14719 | * However, we are not sure that all error messages are printed |
| 14720 | * out in other places. It's not critical so we leave it as-is for now |
| 14721 | */ |
| 14722 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 14723 | comp = NULL; |
| 14724 | } else { |
| 14725 | comp = pctxt->comp; |
| 14726 | pctxt->comp = NULL; |
| 14727 | } |
| 14728 | xmlXPathFreeParserContext(pctxt); |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14729 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14730 | if (comp != NULL) { |
| 14731 | comp->expr = xmlStrdup(str); |
| 14732 | #ifdef DEBUG_EVAL_COUNTS |
| 14733 | comp->string = xmlStrdup(str); |
| 14734 | comp->nb = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14735 | #endif |
Kasimier T. Buchcik | 766ed7e | 2006-06-23 16:32:41 +0000 | [diff] [blame] | 14736 | if ((comp->expr != NULL) && |
| 14737 | (comp->nbStep > 2) && |
| 14738 | (comp->last >= 0) && |
Kasimier T. Buchcik | 7cb3fa9 | 2006-06-06 15:27:46 +0000 | [diff] [blame] | 14739 | (xmlXPathCanRewriteDosExpression(comp->expr) == 1)) |
| 14740 | { |
| 14741 | xmlXPathRewriteDOSExpression(comp, &comp->steps[comp->last]); |
Kasimier T. Buchcik | 9bca933 | 2006-06-19 10:26:42 +0000 | [diff] [blame] | 14742 | } |
Kasimier T. Buchcik | df0ba26 | 2006-05-30 19:45:37 +0000 | [diff] [blame] | 14743 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14744 | return(comp); |
| 14745 | } |
| 14746 | |
| 14747 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14748 | * xmlXPathCompile: |
| 14749 | * @str: the XPath expression |
| 14750 | * |
| 14751 | * Compile an XPath expression |
| 14752 | * |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 14753 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14754 | * the caller has to free the object. |
| 14755 | */ |
| 14756 | xmlXPathCompExprPtr |
| 14757 | xmlXPathCompile(const xmlChar *str) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 14758 | return(xmlXPathCtxtCompile(NULL, str)); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14759 | } |
| 14760 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14761 | /** |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14762 | * xmlXPathCompiledEvalInternal: |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14763 | * @comp: the compiled XPath expression |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14764 | * @ctxt: the XPath context |
| 14765 | * @resObj: the resulting XPath object or NULL |
| 14766 | * @toBool: 1 if only a boolean result is requested |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14767 | * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14768 | * Evaluate the Precompiled XPath expression in the given context. |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14769 | * The caller has to free @resObj. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14770 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14771 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14772 | * the caller has to free the object. |
| 14773 | */ |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14774 | static int |
| 14775 | xmlXPathCompiledEvalInternal(xmlXPathCompExprPtr comp, |
| 14776 | xmlXPathContextPtr ctxt, |
| 14777 | xmlXPathObjectPtr *resObj, |
| 14778 | int toBool) |
| 14779 | { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14780 | xmlXPathParserContextPtr pctxt; |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 14781 | #ifndef LIBXML_THREAD_ENABLED |
| 14782 | static int reentance = 0; |
| 14783 | #endif |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14784 | int res; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14785 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14786 | CHECK_CTXT_NEG(ctxt) |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 14787 | |
| 14788 | if (comp == NULL) |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14789 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14790 | xmlXPathInit(); |
| 14791 | |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 14792 | #ifndef LIBXML_THREAD_ENABLED |
| 14793 | reentance++; |
| 14794 | if (reentance > 1) |
| 14795 | xmlXPathDisableOptimizer = 1; |
| 14796 | #endif |
| 14797 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 14798 | #ifdef DEBUG_EVAL_COUNTS |
| 14799 | comp->nb++; |
| 14800 | if ((comp->string != NULL) && (comp->nb > 100)) { |
| 14801 | fprintf(stderr, "100 x %s\n", comp->string); |
| 14802 | comp->nb = 0; |
| 14803 | } |
| 14804 | #endif |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14805 | pctxt = xmlXPathCompParserContext(comp, ctxt); |
| 14806 | res = xmlXPathRunEval(pctxt, toBool); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14807 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14808 | if (resObj) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14809 | if (pctxt->value == NULL) { |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14810 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14811 | "xmlXPathCompiledEval: evaluation failed\n"); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14812 | *resObj = NULL; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14813 | } else { |
| 14814 | *resObj = valuePop(pctxt); |
| 14815 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14816 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14817 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14818 | /* |
| 14819 | * Pop all remaining objects from the stack. |
| 14820 | */ |
| 14821 | if (pctxt->valueNr > 0) { |
| 14822 | xmlXPathObjectPtr tmp; |
| 14823 | int stack = 0; |
| 14824 | |
| 14825 | do { |
| 14826 | tmp = valuePop(pctxt); |
| 14827 | if (tmp != NULL) { |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14828 | stack++; |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14829 | xmlXPathReleaseObject(ctxt, tmp); |
| 14830 | } |
| 14831 | } while (tmp != NULL); |
| 14832 | if ((stack != 0) && |
| 14833 | ((toBool) || ((resObj) && (*resObj)))) |
| 14834 | { |
| 14835 | xmlGenericError(xmlGenericErrorContext, |
| 14836 | "xmlXPathCompiledEval: %d objects left on the stack.\n", |
| 14837 | stack); |
| 14838 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14839 | } |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14840 | |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14841 | if ((pctxt->error != XPATH_EXPRESSION_OK) && (resObj) && (*resObj)) { |
| 14842 | xmlXPathFreeObject(*resObj); |
| 14843 | *resObj = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14844 | } |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14845 | pctxt->comp = NULL; |
| 14846 | xmlXPathFreeParserContext(pctxt); |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 14847 | #ifndef LIBXML_THREAD_ENABLED |
| 14848 | reentance--; |
| 14849 | #endif |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14850 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14851 | return(res); |
| 14852 | } |
| 14853 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14854 | /** |
Kasimier T. Buchcik | 631ea81 | 2006-06-26 16:47:25 +0000 | [diff] [blame] | 14855 | * xmlXPathCompiledEval: |
| 14856 | * @comp: the compiled XPath expression |
| 14857 | * @ctx: the XPath context |
| 14858 | * |
| 14859 | * Evaluate the Precompiled XPath expression in the given context. |
| 14860 | * |
| 14861 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 14862 | * the caller has to free the object. |
| 14863 | */ |
| 14864 | xmlXPathObjectPtr |
| 14865 | xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) |
| 14866 | { |
| 14867 | xmlXPathObjectPtr res = NULL; |
| 14868 | |
| 14869 | xmlXPathCompiledEvalInternal(comp, ctx, &res, 0); |
| 14870 | return(res); |
| 14871 | } |
| 14872 | |
| 14873 | /** |
| 14874 | * xmlXPathCompiledEvalToBoolean: |
| 14875 | * @comp: the compiled XPath expression |
| 14876 | * @ctxt: the XPath context |
| 14877 | * |
| 14878 | * Applies the XPath boolean() function on the result of the given |
| 14879 | * compiled expression. |
| 14880 | * |
| 14881 | * Returns 1 if the expression evaluated to true, 0 if to false and |
| 14882 | * -1 in API and internal errors. |
| 14883 | */ |
| 14884 | int |
| 14885 | xmlXPathCompiledEvalToBoolean(xmlXPathCompExprPtr comp, |
| 14886 | xmlXPathContextPtr ctxt) |
| 14887 | { |
| 14888 | return(xmlXPathCompiledEvalInternal(comp, ctxt, NULL, 1)); |
| 14889 | } |
| 14890 | |
| 14891 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14892 | * xmlXPathEvalExpr: |
| 14893 | * @ctxt: the XPath Parser context |
| 14894 | * |
| 14895 | * Parse and evaluate an XPath expression in the given context, |
| 14896 | * then push the result on the context stack |
| 14897 | */ |
| 14898 | void |
| 14899 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14900 | #ifdef XPATH_STREAMING |
| 14901 | xmlXPathCompExprPtr comp; |
| 14902 | #endif |
| 14903 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 14904 | if (ctxt == NULL) return; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14905 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14906 | #ifdef XPATH_STREAMING |
| 14907 | comp = xmlXPathTryStreamCompile(ctxt->context, ctxt->base); |
| 14908 | if (comp != NULL) { |
| 14909 | if (ctxt->comp != NULL) |
| 14910 | xmlXPathFreeCompExpr(ctxt->comp); |
| 14911 | ctxt->comp = comp; |
| 14912 | if (ctxt->cur != NULL) |
| 14913 | while (*ctxt->cur != 0) ctxt->cur++; |
| 14914 | } else |
| 14915 | #endif |
| 14916 | { |
Kasimier T. Buchcik | 5691f43 | 2006-05-22 15:19:55 +0000 | [diff] [blame] | 14917 | xmlXPathCompileExpr(ctxt, 1); |
Kasimier T. Buchcik | 766ed7e | 2006-06-23 16:32:41 +0000 | [diff] [blame] | 14918 | /* |
| 14919 | * In this scenario the expression string will sit in ctxt->base. |
| 14920 | */ |
| 14921 | if ((ctxt->error == XPATH_EXPRESSION_OK) && |
| 14922 | (ctxt->comp != NULL) && |
| 14923 | (ctxt->base != NULL) && |
Kasimier T. Buchcik | 7cb3fa9 | 2006-06-06 15:27:46 +0000 | [diff] [blame] | 14924 | (ctxt->comp->nbStep > 2) && |
Kasimier T. Buchcik | 766ed7e | 2006-06-23 16:32:41 +0000 | [diff] [blame] | 14925 | (ctxt->comp->last >= 0) && |
| 14926 | (xmlXPathCanRewriteDosExpression((xmlChar *) ctxt->base) == 1)) |
Kasimier T. Buchcik | 080152c | 2006-06-06 09:42:15 +0000 | [diff] [blame] | 14927 | { |
| 14928 | xmlXPathRewriteDOSExpression(ctxt->comp, |
Kasimier T. Buchcik | 766ed7e | 2006-06-23 16:32:41 +0000 | [diff] [blame] | 14929 | &ctxt->comp->steps[ctxt->comp->last]); |
Kasimier T. Buchcik | 080152c | 2006-06-06 09:42:15 +0000 | [diff] [blame] | 14930 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14931 | } |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 14932 | CHECK_ERROR; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14933 | xmlXPathRunEval(ctxt, 0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 14934 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14935 | |
| 14936 | /** |
| 14937 | * xmlXPathEval: |
| 14938 | * @str: the XPath expression |
| 14939 | * @ctx: the XPath context |
| 14940 | * |
| 14941 | * Evaluate the XPath Location Path in the given context. |
| 14942 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14943 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14944 | * the caller has to free the object. |
| 14945 | */ |
| 14946 | xmlXPathObjectPtr |
| 14947 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 14948 | xmlXPathParserContextPtr ctxt; |
| 14949 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 14950 | int stack = 0; |
| 14951 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 14952 | CHECK_CTXT(ctx) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14953 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 14954 | xmlXPathInit(); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14955 | |
| 14956 | ctxt = xmlXPathNewParserContext(str, ctx); |
William M. Brack | 11be2d0 | 2007-01-24 19:17:19 +0000 | [diff] [blame] | 14957 | if (ctxt == NULL) |
| 14958 | return NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14959 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14960 | |
| 14961 | if (ctxt->value == NULL) { |
| 14962 | xmlGenericError(xmlGenericErrorContext, |
| 14963 | "xmlXPathEval: evaluation failed\n"); |
| 14964 | res = NULL; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 14965 | } else if ((*ctxt->cur != 0) && (ctxt->comp != NULL) |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 14966 | #ifdef XPATH_STREAMING |
| 14967 | && (ctxt->comp->stream == NULL) |
| 14968 | #endif |
| 14969 | ) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14970 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 14971 | res = NULL; |
| 14972 | } else { |
| 14973 | res = valuePop(ctxt); |
| 14974 | } |
| 14975 | |
| 14976 | do { |
| 14977 | tmp = valuePop(ctxt); |
| 14978 | if (tmp != NULL) { |
| 14979 | if (tmp != init) |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 14980 | stack++; |
| 14981 | xmlXPathReleaseObject(ctx, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 14982 | } |
| 14983 | } while (tmp != NULL); |
| 14984 | if ((stack != 0) && (res != NULL)) { |
| 14985 | xmlGenericError(xmlGenericErrorContext, |
| 14986 | "xmlXPathEval: %d object left on the stack\n", |
| 14987 | stack); |
| 14988 | } |
| 14989 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 14990 | xmlXPathFreeObject(res); |
| 14991 | res = NULL; |
| 14992 | } |
| 14993 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14994 | xmlXPathFreeParserContext(ctxt); |
| 14995 | return(res); |
| 14996 | } |
| 14997 | |
| 14998 | /** |
| 14999 | * xmlXPathEvalExpression: |
| 15000 | * @str: the XPath expression |
| 15001 | * @ctxt: the XPath context |
| 15002 | * |
| 15003 | * Evaluate the XPath expression in the given context. |
| 15004 | * |
| 15005 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 15006 | * the caller has to free the object. |
| 15007 | */ |
| 15008 | xmlXPathObjectPtr |
| 15009 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 15010 | xmlXPathParserContextPtr pctxt; |
| 15011 | xmlXPathObjectPtr res, tmp; |
| 15012 | int stack = 0; |
| 15013 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 15014 | CHECK_CTXT(ctxt) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15015 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 15016 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15017 | |
| 15018 | pctxt = xmlXPathNewParserContext(str, ctxt); |
William M. Brack | 11be2d0 | 2007-01-24 19:17:19 +0000 | [diff] [blame] | 15019 | if (pctxt == NULL) |
| 15020 | return NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15021 | xmlXPathEvalExpr(pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15022 | |
Daniel Veillard | c465ffc | 2006-10-17 19:39:33 +0000 | [diff] [blame] | 15023 | if ((*pctxt->cur != 0) || (pctxt->error != XPATH_EXPRESSION_OK)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15024 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 15025 | res = NULL; |
| 15026 | } else { |
| 15027 | res = valuePop(pctxt); |
| 15028 | } |
| 15029 | do { |
| 15030 | tmp = valuePop(pctxt); |
| 15031 | if (tmp != NULL) { |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 15032 | xmlXPathReleaseObject(ctxt, tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15033 | stack++; |
| 15034 | } |
| 15035 | } while (tmp != NULL); |
| 15036 | if ((stack != 0) && (res != NULL)) { |
| 15037 | xmlGenericError(xmlGenericErrorContext, |
| 15038 | "xmlXPathEvalExpression: %d object left on the stack\n", |
| 15039 | stack); |
| 15040 | } |
| 15041 | xmlXPathFreeParserContext(pctxt); |
| 15042 | return(res); |
| 15043 | } |
| 15044 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15045 | /************************************************************************ |
| 15046 | * * |
| 15047 | * Extra functions not pertaining to the XPath spec * |
| 15048 | * * |
| 15049 | ************************************************************************/ |
| 15050 | /** |
| 15051 | * xmlXPathEscapeUriFunction: |
| 15052 | * @ctxt: the XPath Parser context |
| 15053 | * @nargs: the number of arguments |
| 15054 | * |
| 15055 | * Implement the escape-uri() XPath function |
| 15056 | * string escape-uri(string $str, bool $escape-reserved) |
| 15057 | * |
| 15058 | * This function applies the URI escaping rules defined in section 2 of [RFC |
| 15059 | * 2396] to the string supplied as $uri-part, which typically represents all |
| 15060 | * or part of a URI. The effect of the function is to replace any special |
| 15061 | * character in the string by an escape sequence of the form %xx%yy..., |
| 15062 | * where xxyy... is the hexadecimal representation of the octets used to |
| 15063 | * represent the character in UTF-8. |
| 15064 | * |
| 15065 | * The set of characters that are escaped depends on the setting of the |
| 15066 | * boolean argument $escape-reserved. |
| 15067 | * |
| 15068 | * If $escape-reserved is true, all characters are escaped other than lower |
| 15069 | * case letters a-z, upper case letters A-Z, digits 0-9, and the characters |
| 15070 | * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!" |
| 15071 | * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only |
| 15072 | * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and |
| 15073 | * A-F). |
| 15074 | * |
| 15075 | * If $escape-reserved is false, the behavior differs in that characters |
| 15076 | * referred to in [RFC 2396] as reserved characters are not escaped. These |
| 15077 | * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",". |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15078 | * |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15079 | * [RFC 2396] does not define whether escaped URIs should use lower case or |
| 15080 | * upper case for hexadecimal digits. To ensure that escaped URIs can be |
| 15081 | * compared using string comparison functions, this function must always use |
| 15082 | * the upper-case letters A-F. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15083 | * |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15084 | * Generally, $escape-reserved should be set to true when escaping a string |
| 15085 | * that is to form a single part of a URI, and to false when escaping an |
| 15086 | * entire URI or URI reference. |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15087 | * |
| 15088 | * In the case of non-ascii characters, the string is encoded according to |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15089 | * utf-8 and then converted according to RFC 2396. |
| 15090 | * |
| 15091 | * Examples |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15092 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true()) |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15093 | * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean" |
| 15094 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false()) |
| 15095 | * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean" |
| 15096 | * |
| 15097 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 15098 | static void |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15099 | xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 15100 | xmlXPathObjectPtr str; |
| 15101 | int escape_reserved; |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 15102 | xmlBufPtr target; |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15103 | xmlChar *cptr; |
| 15104 | xmlChar escape[4]; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15105 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15106 | CHECK_ARITY(2); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15107 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15108 | escape_reserved = xmlXPathPopBoolean(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15109 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15110 | CAST_TO_STRING; |
| 15111 | str = valuePop(ctxt); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15112 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 15113 | target = xmlBufCreate(); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15114 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15115 | escape[0] = '%'; |
| 15116 | escape[3] = 0; |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15117 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15118 | if (target) { |
| 15119 | for (cptr = str->stringval; *cptr; cptr++) { |
| 15120 | if ((*cptr >= 'A' && *cptr <= 'Z') || |
| 15121 | (*cptr >= 'a' && *cptr <= 'z') || |
| 15122 | (*cptr >= '0' && *cptr <= '9') || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15123 | *cptr == '-' || *cptr == '_' || *cptr == '.' || |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15124 | *cptr == '!' || *cptr == '~' || *cptr == '*' || |
| 15125 | *cptr == '\''|| *cptr == '(' || *cptr == ')' || |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15126 | (*cptr == '%' && |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15127 | ((cptr[1] >= 'A' && cptr[1] <= 'F') || |
| 15128 | (cptr[1] >= 'a' && cptr[1] <= 'f') || |
| 15129 | (cptr[1] >= '0' && cptr[1] <= '9')) && |
| 15130 | ((cptr[2] >= 'A' && cptr[2] <= 'F') || |
| 15131 | (cptr[2] >= 'a' && cptr[2] <= 'f') || |
| 15132 | (cptr[2] >= '0' && cptr[2] <= '9'))) || |
| 15133 | (!escape_reserved && |
| 15134 | (*cptr == ';' || *cptr == '/' || *cptr == '?' || |
| 15135 | *cptr == ':' || *cptr == '@' || *cptr == '&' || |
| 15136 | *cptr == '=' || *cptr == '+' || *cptr == '$' || |
| 15137 | *cptr == ','))) { |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 15138 | xmlBufAdd(target, cptr, 1); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15139 | } else { |
| 15140 | if ((*cptr >> 4) < 10) |
| 15141 | escape[1] = '0' + (*cptr >> 4); |
| 15142 | else |
| 15143 | escape[1] = 'A' - 10 + (*cptr >> 4); |
| 15144 | if ((*cptr & 0xF) < 10) |
| 15145 | escape[2] = '0' + (*cptr & 0xF); |
| 15146 | else |
| 15147 | escape[2] = 'A' - 10 + (*cptr & 0xF); |
Daniel Veillard | 45490ae | 2008-07-29 09:13:19 +0000 | [diff] [blame] | 15148 | |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 15149 | xmlBufAdd(target, &escape[0], 3); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15150 | } |
| 15151 | } |
| 15152 | } |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 15153 | valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, |
Daniel Veillard | ade10f2 | 2012-07-12 09:43:27 +0800 | [diff] [blame^] | 15154 | xmlBufContent(target))); |
| 15155 | xmlBufFree(target); |
Kasimier T. Buchcik | a724844 | 2006-05-29 16:15:36 +0000 | [diff] [blame] | 15156 | xmlXPathReleaseObject(ctxt->context, str); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15157 | } |
| 15158 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15159 | /** |
| 15160 | * xmlXPathRegisterAllFunctions: |
| 15161 | * @ctxt: the XPath context |
| 15162 | * |
| 15163 | * Registers all default XPath functions in this context |
| 15164 | */ |
| 15165 | void |
| 15166 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 15167 | { |
| 15168 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 15169 | xmlXPathBooleanFunction); |
| 15170 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 15171 | xmlXPathCeilingFunction); |
| 15172 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 15173 | xmlXPathCountFunction); |
| 15174 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 15175 | xmlXPathConcatFunction); |
| 15176 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 15177 | xmlXPathContainsFunction); |
| 15178 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 15179 | xmlXPathIdFunction); |
| 15180 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 15181 | xmlXPathFalseFunction); |
| 15182 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 15183 | xmlXPathFloorFunction); |
| 15184 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 15185 | xmlXPathLastFunction); |
| 15186 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 15187 | xmlXPathLangFunction); |
| 15188 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 15189 | xmlXPathLocalNameFunction); |
| 15190 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 15191 | xmlXPathNotFunction); |
| 15192 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 15193 | xmlXPathNameFunction); |
| 15194 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 15195 | xmlXPathNamespaceURIFunction); |
| 15196 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 15197 | xmlXPathNormalizeFunction); |
| 15198 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 15199 | xmlXPathNumberFunction); |
| 15200 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 15201 | xmlXPathPositionFunction); |
| 15202 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 15203 | xmlXPathRoundFunction); |
| 15204 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 15205 | xmlXPathStringFunction); |
| 15206 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 15207 | xmlXPathStringLengthFunction); |
| 15208 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 15209 | xmlXPathStartsWithFunction); |
| 15210 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 15211 | xmlXPathSubstringFunction); |
| 15212 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 15213 | xmlXPathSubstringBeforeFunction); |
| 15214 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 15215 | xmlXPathSubstringAfterFunction); |
| 15216 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 15217 | xmlXPathSumFunction); |
| 15218 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 15219 | xmlXPathTrueFunction); |
| 15220 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 15221 | xmlXPathTranslateFunction); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 15222 | |
| 15223 | xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri", |
| 15224 | (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions", |
| 15225 | xmlXPathEscapeUriFunction); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15226 | } |
| 15227 | |
| 15228 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 15229 | #define bottom_xpath |
| 15230 | #include "elfgcchack.h" |