Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * pattern.c: Implemetation of selectors for nodes |
| 3 | * |
| 4 | * Reference: |
| 5 | * http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/ |
| 6 | * to some extent |
| 7 | * http://www.w3.org/TR/1999/REC-xml-19991116 |
| 8 | * |
| 9 | * See Copyright for the status of this software. |
| 10 | * |
| 11 | * daniel@veillard.com |
| 12 | */ |
| 13 | |
Daniel Veillard | f9d1691 | 2005-01-30 22:36:30 +0000 | [diff] [blame] | 14 | /* |
| 15 | * TODO: |
| 16 | * - compilation flags to check for specific syntaxes |
| 17 | * using flags of xmlPatterncompile() |
| 18 | * - making clear how pattern starting with / or . need to be handled, |
| 19 | * currently push(NULL, NULL) means a reset of the streaming context |
| 20 | * and indicating we are on / (the document node), probably need |
| 21 | * something similar for . |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 22 | * - get rid of the "compile" starting with lowercase |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 23 | * - DONE (2006-05-16): get rid of the Strdup/Strndup in case of dictionary |
Daniel Veillard | f9d1691 | 2005-01-30 22:36:30 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 26 | #define IN_LIBXML |
| 27 | #include "libxml.h" |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <libxml/xmlmemory.h> |
| 31 | #include <libxml/tree.h> |
| 32 | #include <libxml/hash.h> |
| 33 | #include <libxml/dict.h> |
| 34 | #include <libxml/xmlerror.h> |
| 35 | #include <libxml/parserInternals.h> |
| 36 | #include <libxml/pattern.h> |
| 37 | |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 38 | #ifdef LIBXML_PATTERN_ENABLED |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 39 | |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 40 | /* #define DEBUG_STREAMING */ |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 41 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 42 | #define ERROR(a, b, c, d) |
| 43 | #define ERROR5(a, b, c, d, e) |
| 44 | |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 45 | #define XML_STREAM_STEP_DESC 1 |
| 46 | #define XML_STREAM_STEP_FINAL 2 |
| 47 | #define XML_STREAM_STEP_ROOT 4 |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 48 | #define XML_STREAM_STEP_ATTR 8 |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 49 | #define XML_STREAM_STEP_NODE 16 |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 50 | #define XML_STREAM_STEP_IN_SET 32 |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 51 | |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 52 | /* |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 53 | * NOTE: Those private flags (XML_STREAM_xxx) are used |
| 54 | * in _xmlStreamCtxt->flag. They extend the public |
| 55 | * xmlPatternFlags, so be carefull not to interfere with the |
| 56 | * reserved values for xmlPatternFlags. |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 57 | */ |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 58 | #define XML_STREAM_FINAL_IS_ANY_NODE 1<<14 |
| 59 | #define XML_STREAM_FROM_ROOT 1<<15 |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 60 | #define XML_STREAM_DESC 1<<16 |
| 61 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 62 | /* |
| 63 | * XML_STREAM_ANY_NODE is used for comparison against |
| 64 | * xmlElementType enums, to indicate a node of any type. |
| 65 | */ |
| 66 | #define XML_STREAM_ANY_NODE 100 |
| 67 | |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 68 | #define XML_PATTERN_NOTPATTERN (XML_PATTERN_XPATH | \ |
| 69 | XML_PATTERN_XSSEL | \ |
| 70 | XML_PATTERN_XSFIELD) |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 71 | |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 72 | #define XML_STREAM_XS_IDC(c) ((c)->flags & \ |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 73 | (XML_PATTERN_XSSEL | XML_PATTERN_XSFIELD)) |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 74 | |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 75 | #define XML_STREAM_XS_IDC_SEL(c) ((c)->flags & XML_PATTERN_XSSEL) |
| 76 | |
| 77 | #define XML_STREAM_XS_IDC_FIELD(c) ((c)->flags & XML_PATTERN_XSFIELD) |
| 78 | |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 79 | #define XML_PAT_COPY_NSNAME(c, r, nsname) \ |
| 80 | if ((c)->comp->dict) \ |
| 81 | r = (xmlChar *) xmlDictLookup((c)->comp->dict, BAD_CAST nsname, -1); \ |
| 82 | else r = xmlStrdup(BAD_CAST nsname); |
| 83 | |
| 84 | #define XML_PAT_FREE_STRING(c, r) if ((c)->comp->dict == NULL) xmlFree(r); |
| 85 | |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 86 | typedef struct _xmlStreamStep xmlStreamStep; |
| 87 | typedef xmlStreamStep *xmlStreamStepPtr; |
| 88 | struct _xmlStreamStep { |
| 89 | int flags; /* properties of that step */ |
| 90 | const xmlChar *name; /* first string value if NULL accept all */ |
| 91 | const xmlChar *ns; /* second string value */ |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 92 | int nodeType; /* type of node */ |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | typedef struct _xmlStreamComp xmlStreamComp; |
| 96 | typedef xmlStreamComp *xmlStreamCompPtr; |
| 97 | struct _xmlStreamComp { |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 98 | xmlDict *dict; /* the dictionary if any */ |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 99 | int nbStep; /* number of steps in the automata */ |
| 100 | int maxStep; /* allocated number of steps */ |
| 101 | xmlStreamStepPtr steps; /* the array of steps */ |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 102 | int flags; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | struct _xmlStreamCtxt { |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 106 | struct _xmlStreamCtxt *next;/* link to next sub pattern if | */ |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 107 | xmlStreamCompPtr comp; /* the compiled stream */ |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 108 | int nbState; /* number of states in the automata */ |
| 109 | int maxState; /* allocated number of states */ |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 110 | int level; /* how deep are we ? */ |
| 111 | int *states; /* the array of step indexes */ |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 112 | int flags; /* validation options */ |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 113 | int blockLevel; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static void xmlFreeStreamComp(xmlStreamCompPtr comp); |
| 117 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 118 | /* |
| 119 | * Types are private: |
| 120 | */ |
| 121 | |
| 122 | typedef enum { |
| 123 | XML_OP_END=0, |
| 124 | XML_OP_ROOT, |
| 125 | XML_OP_ELEM, |
| 126 | XML_OP_CHILD, |
| 127 | XML_OP_ATTR, |
| 128 | XML_OP_PARENT, |
| 129 | XML_OP_ANCESTOR, |
| 130 | XML_OP_NS, |
| 131 | XML_OP_ALL |
| 132 | } xmlPatOp; |
| 133 | |
| 134 | |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 135 | typedef struct _xmlStepState xmlStepState; |
| 136 | typedef xmlStepState *xmlStepStatePtr; |
| 137 | struct _xmlStepState { |
| 138 | int step; |
| 139 | xmlNodePtr node; |
| 140 | }; |
| 141 | |
| 142 | typedef struct _xmlStepStates xmlStepStates; |
| 143 | typedef xmlStepStates *xmlStepStatesPtr; |
| 144 | struct _xmlStepStates { |
| 145 | int nbstates; |
| 146 | int maxstates; |
| 147 | xmlStepStatePtr states; |
| 148 | }; |
| 149 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 150 | typedef struct _xmlStepOp xmlStepOp; |
| 151 | typedef xmlStepOp *xmlStepOpPtr; |
| 152 | struct _xmlStepOp { |
| 153 | xmlPatOp op; |
| 154 | const xmlChar *value; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 155 | const xmlChar *value2; /* The namespace name */ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 158 | #define PAT_FROM_ROOT (1<<8) |
| 159 | #define PAT_FROM_CUR (1<<9) |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 160 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 161 | struct _xmlPattern { |
| 162 | void *data; /* the associated template */ |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 163 | xmlDictPtr dict; /* the optional dictionary */ |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 164 | struct _xmlPattern *next; /* next pattern if | is used */ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 165 | const xmlChar *pattern; /* the pattern */ |
Daniel Veillard | f5812c3 | 2005-09-03 13:43:20 +0000 | [diff] [blame] | 166 | int flags; /* flags */ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 167 | int nbStep; |
| 168 | int maxStep; |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 169 | xmlStepOpPtr steps; /* ops for computation */ |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 170 | xmlStreamCompPtr stream; /* the streaming data if any */ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | typedef struct _xmlPatParserContext xmlPatParserContext; |
| 174 | typedef xmlPatParserContext *xmlPatParserContextPtr; |
| 175 | struct _xmlPatParserContext { |
| 176 | const xmlChar *cur; /* the current char being parsed */ |
| 177 | const xmlChar *base; /* the full expression */ |
| 178 | int error; /* error code */ |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 179 | xmlDictPtr dict; /* the dictionary if any */ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 180 | xmlPatternPtr comp; /* the result */ |
| 181 | xmlNodePtr elem; /* the current node if any */ |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 182 | const xmlChar **namespaces; /* the namespaces definitions */ |
| 183 | int nb_namespaces; /* the number of namespaces */ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | /************************************************************************ |
| 187 | * * |
| 188 | * Type functions * |
| 189 | * * |
| 190 | ************************************************************************/ |
| 191 | |
| 192 | /** |
| 193 | * xmlNewPattern: |
| 194 | * |
| 195 | * Create a new XSLT Pattern |
| 196 | * |
| 197 | * Returns the newly allocated xmlPatternPtr or NULL in case of error |
| 198 | */ |
| 199 | static xmlPatternPtr |
| 200 | xmlNewPattern(void) { |
| 201 | xmlPatternPtr cur; |
| 202 | |
| 203 | cur = (xmlPatternPtr) xmlMalloc(sizeof(xmlPattern)); |
| 204 | if (cur == NULL) { |
| 205 | ERROR(NULL, NULL, NULL, |
| 206 | "xmlNewPattern : malloc failed\n"); |
| 207 | return(NULL); |
| 208 | } |
| 209 | memset(cur, 0, sizeof(xmlPattern)); |
| 210 | cur->maxStep = 10; |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 211 | cur->steps = (xmlStepOpPtr) xmlMalloc(cur->maxStep * sizeof(xmlStepOp)); |
| 212 | if (cur->steps == NULL) { |
| 213 | xmlFree(cur); |
| 214 | ERROR(NULL, NULL, NULL, |
| 215 | "xmlNewPattern : malloc failed\n"); |
| 216 | return(NULL); |
| 217 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 218 | return(cur); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * xmlFreePattern: |
| 223 | * @comp: an XSLT comp |
| 224 | * |
| 225 | * Free up the memory allocated by @comp |
| 226 | */ |
| 227 | void |
| 228 | xmlFreePattern(xmlPatternPtr comp) { |
| 229 | xmlStepOpPtr op; |
| 230 | int i; |
| 231 | |
| 232 | if (comp == NULL) |
| 233 | return; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 234 | if (comp->next != NULL) |
| 235 | xmlFreePattern(comp->next); |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 236 | if (comp->stream != NULL) |
| 237 | xmlFreeStreamComp(comp->stream); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 238 | if (comp->pattern != NULL) |
| 239 | xmlFree((xmlChar *)comp->pattern); |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 240 | if (comp->steps != NULL) { |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 241 | if (comp->dict == NULL) { |
| 242 | for (i = 0;i < comp->nbStep;i++) { |
| 243 | op = &comp->steps[i]; |
| 244 | if (op->value != NULL) |
| 245 | xmlFree((xmlChar *) op->value); |
| 246 | if (op->value2 != NULL) |
| 247 | xmlFree((xmlChar *) op->value2); |
| 248 | } |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 249 | } |
| 250 | xmlFree(comp->steps); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 251 | } |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 252 | if (comp->dict != NULL) |
| 253 | xmlDictFree(comp->dict); |
| 254 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 255 | memset(comp, -1, sizeof(xmlPattern)); |
| 256 | xmlFree(comp); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * xmlFreePatternList: |
| 261 | * @comp: an XSLT comp list |
| 262 | * |
| 263 | * Free up the memory allocated by all the elements of @comp |
| 264 | */ |
| 265 | void |
| 266 | xmlFreePatternList(xmlPatternPtr comp) { |
| 267 | xmlPatternPtr cur; |
| 268 | |
| 269 | while (comp != NULL) { |
| 270 | cur = comp; |
| 271 | comp = comp->next; |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 272 | cur->next = NULL; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 273 | xmlFreePattern(cur); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * xmlNewPatParserContext: |
| 279 | * @pattern: the pattern context |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 280 | * @dict: the inherited dictionary or NULL |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 281 | * @namespaces: the prefix definitions, array of [URI, prefix] terminated |
| 282 | * with [NULL, NULL] or NULL if no namespace is used |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 283 | * |
| 284 | * Create a new XML pattern parser context |
| 285 | * |
| 286 | * Returns the newly allocated xmlPatParserContextPtr or NULL in case of error |
| 287 | */ |
| 288 | static xmlPatParserContextPtr |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 289 | xmlNewPatParserContext(const xmlChar *pattern, xmlDictPtr dict, |
| 290 | const xmlChar **namespaces) { |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 291 | xmlPatParserContextPtr cur; |
| 292 | |
| 293 | if (pattern == NULL) |
| 294 | return(NULL); |
| 295 | |
| 296 | cur = (xmlPatParserContextPtr) xmlMalloc(sizeof(xmlPatParserContext)); |
| 297 | if (cur == NULL) { |
| 298 | ERROR(NULL, NULL, NULL, |
| 299 | "xmlNewPatParserContext : malloc failed\n"); |
| 300 | return(NULL); |
| 301 | } |
| 302 | memset(cur, 0, sizeof(xmlPatParserContext)); |
| 303 | cur->dict = dict; |
| 304 | cur->cur = pattern; |
| 305 | cur->base = pattern; |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 306 | if (namespaces != NULL) { |
| 307 | int i; |
Nico Weber | cedf84d | 2012-03-05 16:36:59 +0800 | [diff] [blame] | 308 | for (i = 0;namespaces[2 * i] != NULL;i++) |
| 309 | ; |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 310 | cur->nb_namespaces = i; |
| 311 | } else { |
| 312 | cur->nb_namespaces = 0; |
| 313 | } |
| 314 | cur->namespaces = namespaces; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 315 | return(cur); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * xmlFreePatParserContext: |
| 320 | * @ctxt: an XSLT parser context |
| 321 | * |
| 322 | * Free up the memory allocated by @ctxt |
| 323 | */ |
| 324 | static void |
| 325 | xmlFreePatParserContext(xmlPatParserContextPtr ctxt) { |
| 326 | if (ctxt == NULL) |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 327 | return; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 328 | memset(ctxt, -1, sizeof(xmlPatParserContext)); |
| 329 | xmlFree(ctxt); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * xmlPatternAdd: |
| 334 | * @comp: the compiled match expression |
| 335 | * @op: an op |
| 336 | * @value: the first value |
| 337 | * @value2: the second value |
| 338 | * |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 339 | * Add a step to an XSLT Compiled Match |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 340 | * |
| 341 | * Returns -1 in case of failure, 0 otherwise. |
| 342 | */ |
| 343 | static int |
| 344 | xmlPatternAdd(xmlPatParserContextPtr ctxt ATTRIBUTE_UNUSED, |
| 345 | xmlPatternPtr comp, |
| 346 | xmlPatOp op, xmlChar * value, xmlChar * value2) |
| 347 | { |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 348 | if (comp->nbStep >= comp->maxStep) { |
| 349 | xmlStepOpPtr temp; |
| 350 | temp = (xmlStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 * |
| 351 | sizeof(xmlStepOp)); |
| 352 | if (temp == NULL) { |
| 353 | ERROR(ctxt, NULL, NULL, |
| 354 | "xmlPatternAdd: realloc failed\n"); |
| 355 | return (-1); |
| 356 | } |
| 357 | comp->steps = temp; |
| 358 | comp->maxStep *= 2; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 359 | } |
| 360 | comp->steps[comp->nbStep].op = op; |
| 361 | comp->steps[comp->nbStep].value = value; |
| 362 | comp->steps[comp->nbStep].value2 = value2; |
| 363 | comp->nbStep++; |
| 364 | return (0); |
| 365 | } |
| 366 | |
| 367 | #if 0 |
| 368 | /** |
| 369 | * xsltSwapTopPattern: |
| 370 | * @comp: the compiled match expression |
| 371 | * |
| 372 | * reverse the two top steps. |
| 373 | */ |
| 374 | static void |
| 375 | xsltSwapTopPattern(xmlPatternPtr comp) { |
| 376 | int i; |
| 377 | int j = comp->nbStep - 1; |
| 378 | |
| 379 | if (j > 0) { |
| 380 | register const xmlChar *tmp; |
| 381 | register xmlPatOp op; |
| 382 | i = j - 1; |
| 383 | tmp = comp->steps[i].value; |
| 384 | comp->steps[i].value = comp->steps[j].value; |
| 385 | comp->steps[j].value = tmp; |
| 386 | tmp = comp->steps[i].value2; |
| 387 | comp->steps[i].value2 = comp->steps[j].value2; |
| 388 | comp->steps[j].value2 = tmp; |
| 389 | op = comp->steps[i].op; |
| 390 | comp->steps[i].op = comp->steps[j].op; |
| 391 | comp->steps[j].op = op; |
| 392 | } |
| 393 | } |
| 394 | #endif |
| 395 | |
| 396 | /** |
| 397 | * xmlReversePattern: |
| 398 | * @comp: the compiled match expression |
| 399 | * |
| 400 | * reverse all the stack of expressions |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 401 | * |
| 402 | * returns 0 in case of success and -1 in case of error. |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 403 | */ |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 404 | static int |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 405 | xmlReversePattern(xmlPatternPtr comp) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 406 | int i, j; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 407 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 408 | /* |
| 409 | * remove the leading // for //a or .//a |
| 410 | */ |
| 411 | if ((comp->nbStep > 0) && (comp->steps[0].op == XML_OP_ANCESTOR)) { |
| 412 | for (i = 0, j = 1;j < comp->nbStep;i++,j++) { |
| 413 | comp->steps[i].value = comp->steps[j].value; |
| 414 | comp->steps[i].value2 = comp->steps[j].value2; |
| 415 | comp->steps[i].op = comp->steps[j].op; |
| 416 | } |
| 417 | comp->nbStep--; |
| 418 | } |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 419 | if (comp->nbStep >= comp->maxStep) { |
| 420 | xmlStepOpPtr temp; |
| 421 | temp = (xmlStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 * |
| 422 | sizeof(xmlStepOp)); |
| 423 | if (temp == NULL) { |
| 424 | ERROR(ctxt, NULL, NULL, |
| 425 | "xmlReversePattern: realloc failed\n"); |
| 426 | return (-1); |
| 427 | } |
| 428 | comp->steps = temp; |
| 429 | comp->maxStep *= 2; |
| 430 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 431 | i = 0; |
| 432 | j = comp->nbStep - 1; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 433 | while (j > i) { |
| 434 | register const xmlChar *tmp; |
| 435 | register xmlPatOp op; |
| 436 | tmp = comp->steps[i].value; |
| 437 | comp->steps[i].value = comp->steps[j].value; |
| 438 | comp->steps[j].value = tmp; |
| 439 | tmp = comp->steps[i].value2; |
| 440 | comp->steps[i].value2 = comp->steps[j].value2; |
| 441 | comp->steps[j].value2 = tmp; |
| 442 | op = comp->steps[i].op; |
| 443 | comp->steps[i].op = comp->steps[j].op; |
| 444 | comp->steps[j].op = op; |
| 445 | j--; |
| 446 | i++; |
| 447 | } |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 448 | comp->steps[comp->nbStep].value = NULL; |
| 449 | comp->steps[comp->nbStep].value2 = NULL; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 450 | comp->steps[comp->nbStep++].op = XML_OP_END; |
Daniel Veillard | c7c9fb1 | 2005-01-12 21:04:15 +0000 | [diff] [blame] | 451 | return(0); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /************************************************************************ |
| 455 | * * |
| 456 | * The interpreter for the precompiled patterns * |
| 457 | * * |
| 458 | ************************************************************************/ |
| 459 | |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 460 | static int |
| 461 | xmlPatPushState(xmlStepStates *states, int step, xmlNodePtr node) { |
| 462 | if ((states->states == NULL) || (states->maxstates <= 0)) { |
| 463 | states->maxstates = 4; |
| 464 | states->nbstates = 0; |
| 465 | states->states = xmlMalloc(4 * sizeof(xmlStepState)); |
| 466 | } |
| 467 | else if (states->maxstates <= states->nbstates) { |
| 468 | xmlStepState *tmp; |
| 469 | |
| 470 | tmp = (xmlStepStatePtr) xmlRealloc(states->states, |
| 471 | 2 * states->maxstates * sizeof(xmlStepState)); |
| 472 | if (tmp == NULL) |
| 473 | return(-1); |
| 474 | states->states = tmp; |
| 475 | states->maxstates *= 2; |
| 476 | } |
| 477 | states->states[states->nbstates].step = step; |
| 478 | states->states[states->nbstates++].node = node; |
| 479 | #if 0 |
| 480 | fprintf(stderr, "Push: %d, %s\n", step, node->name); |
| 481 | #endif |
| 482 | return(0); |
| 483 | } |
| 484 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 485 | /** |
| 486 | * xmlPatMatch: |
| 487 | * @comp: the precompiled pattern |
| 488 | * @node: a node |
| 489 | * |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 490 | * Test whether the node matches the pattern |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 491 | * |
| 492 | * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure |
| 493 | */ |
| 494 | static int |
| 495 | xmlPatMatch(xmlPatternPtr comp, xmlNodePtr node) { |
| 496 | int i; |
| 497 | xmlStepOpPtr step; |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 498 | xmlStepStates states = {0, 0, NULL}; /* // may require backtrack */ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 499 | |
| 500 | if ((comp == NULL) || (node == NULL)) return(-1); |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 501 | i = 0; |
| 502 | restart: |
| 503 | for (;i < comp->nbStep;i++) { |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 504 | step = &comp->steps[i]; |
| 505 | switch (step->op) { |
| 506 | case XML_OP_END: |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 507 | goto found; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 508 | case XML_OP_ROOT: |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 509 | if (node->type == XML_NAMESPACE_DECL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 510 | goto rollback; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 511 | node = node->parent; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 512 | if ((node->type == XML_DOCUMENT_NODE) || |
| 513 | #ifdef LIBXML_DOCB_ENABLED |
| 514 | (node->type == XML_DOCB_DOCUMENT_NODE) || |
| 515 | #endif |
| 516 | (node->type == XML_HTML_DOCUMENT_NODE)) |
| 517 | continue; |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 518 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 519 | case XML_OP_ELEM: |
| 520 | if (node->type != XML_ELEMENT_NODE) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 521 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 522 | if (step->value == NULL) |
| 523 | continue; |
| 524 | if (step->value[0] != node->name[0]) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 525 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 526 | if (!xmlStrEqual(step->value, node->name)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 527 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 528 | |
| 529 | /* Namespace test */ |
| 530 | if (node->ns == NULL) { |
| 531 | if (step->value2 != NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 532 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 533 | } else if (node->ns->href != NULL) { |
| 534 | if (step->value2 == NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 535 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 536 | if (!xmlStrEqual(step->value2, node->ns->href)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 537 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 538 | } |
| 539 | continue; |
| 540 | case XML_OP_CHILD: { |
| 541 | xmlNodePtr lst; |
| 542 | |
| 543 | if ((node->type != XML_ELEMENT_NODE) && |
| 544 | (node->type != XML_DOCUMENT_NODE) && |
| 545 | #ifdef LIBXML_DOCB_ENABLED |
| 546 | (node->type != XML_DOCB_DOCUMENT_NODE) && |
| 547 | #endif |
| 548 | (node->type != XML_HTML_DOCUMENT_NODE)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 549 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 550 | |
| 551 | lst = node->children; |
| 552 | |
| 553 | if (step->value != NULL) { |
| 554 | while (lst != NULL) { |
| 555 | if ((lst->type == XML_ELEMENT_NODE) && |
| 556 | (step->value[0] == lst->name[0]) && |
| 557 | (xmlStrEqual(step->value, lst->name))) |
| 558 | break; |
| 559 | lst = lst->next; |
| 560 | } |
| 561 | if (lst != NULL) |
| 562 | continue; |
| 563 | } |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 564 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 565 | } |
| 566 | case XML_OP_ATTR: |
| 567 | if (node->type != XML_ATTRIBUTE_NODE) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 568 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 569 | if (step->value != NULL) { |
| 570 | if (step->value[0] != node->name[0]) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 571 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 572 | if (!xmlStrEqual(step->value, node->name)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 573 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 574 | } |
| 575 | /* Namespace test */ |
| 576 | if (node->ns == NULL) { |
| 577 | if (step->value2 != NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 578 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 579 | } else if (step->value2 != NULL) { |
| 580 | if (!xmlStrEqual(step->value2, node->ns->href)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 581 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 582 | } |
| 583 | continue; |
| 584 | case XML_OP_PARENT: |
| 585 | if ((node->type == XML_DOCUMENT_NODE) || |
| 586 | (node->type == XML_HTML_DOCUMENT_NODE) || |
| 587 | #ifdef LIBXML_DOCB_ENABLED |
| 588 | (node->type == XML_DOCB_DOCUMENT_NODE) || |
| 589 | #endif |
| 590 | (node->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 591 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 592 | node = node->parent; |
| 593 | if (node == NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 594 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 595 | if (step->value == NULL) |
| 596 | continue; |
| 597 | if (step->value[0] != node->name[0]) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 598 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 599 | if (!xmlStrEqual(step->value, node->name)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 600 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 601 | /* Namespace test */ |
| 602 | if (node->ns == NULL) { |
| 603 | if (step->value2 != NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 604 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 605 | } else if (node->ns->href != NULL) { |
| 606 | if (step->value2 == NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 607 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 608 | if (!xmlStrEqual(step->value2, node->ns->href)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 609 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 610 | } |
| 611 | continue; |
| 612 | case XML_OP_ANCESTOR: |
| 613 | /* TODO: implement coalescing of ANCESTOR/NODE ops */ |
| 614 | if (step->value == NULL) { |
| 615 | i++; |
| 616 | step = &comp->steps[i]; |
| 617 | if (step->op == XML_OP_ROOT) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 618 | goto found; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 619 | if (step->op != XML_OP_ELEM) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 620 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 621 | if (step->value == NULL) |
| 622 | return(-1); |
| 623 | } |
| 624 | if (node == NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 625 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 626 | if ((node->type == XML_DOCUMENT_NODE) || |
| 627 | (node->type == XML_HTML_DOCUMENT_NODE) || |
| 628 | #ifdef LIBXML_DOCB_ENABLED |
| 629 | (node->type == XML_DOCB_DOCUMENT_NODE) || |
| 630 | #endif |
| 631 | (node->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 632 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 633 | node = node->parent; |
| 634 | while (node != NULL) { |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 635 | if ((node->type == XML_ELEMENT_NODE) && |
| 636 | (step->value[0] == node->name[0]) && |
| 637 | (xmlStrEqual(step->value, node->name))) { |
| 638 | /* Namespace test */ |
| 639 | if (node->ns == NULL) { |
| 640 | if (step->value2 == NULL) |
| 641 | break; |
| 642 | } else if (node->ns->href != NULL) { |
| 643 | if ((step->value2 != NULL) && |
| 644 | (xmlStrEqual(step->value2, node->ns->href))) |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | node = node->parent; |
| 649 | } |
| 650 | if (node == NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 651 | goto rollback; |
| 652 | /* |
| 653 | * prepare a potential rollback from here |
| 654 | * for ancestors of that node. |
| 655 | */ |
| 656 | if (step->op == XML_OP_ANCESTOR) |
| 657 | xmlPatPushState(&states, i, node); |
| 658 | else |
| 659 | xmlPatPushState(&states, i - 1, node); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 660 | continue; |
| 661 | case XML_OP_NS: |
| 662 | if (node->type != XML_ELEMENT_NODE) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 663 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 664 | if (node->ns == NULL) { |
| 665 | if (step->value != NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 666 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 667 | } else if (node->ns->href != NULL) { |
| 668 | if (step->value == NULL) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 669 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 670 | if (!xmlStrEqual(step->value, node->ns->href)) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 671 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 672 | } |
| 673 | break; |
| 674 | case XML_OP_ALL: |
| 675 | if (node->type != XML_ELEMENT_NODE) |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 676 | goto rollback; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 677 | break; |
| 678 | } |
| 679 | } |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 680 | found: |
| 681 | if (states.states != NULL) { |
| 682 | /* Free the rollback states */ |
| 683 | xmlFree(states.states); |
| 684 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 685 | return(1); |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 686 | rollback: |
| 687 | /* got an error try to rollback */ |
| 688 | if (states.states == NULL) |
| 689 | return(0); |
| 690 | if (states.nbstates <= 0) { |
| 691 | xmlFree(states.states); |
| 692 | return(0); |
| 693 | } |
| 694 | states.nbstates--; |
| 695 | i = states.states[states.nbstates].step; |
| 696 | node = states.states[states.nbstates].node; |
| 697 | #if 0 |
| 698 | fprintf(stderr, "Pop: %d, %s\n", i, node->name); |
| 699 | #endif |
| 700 | goto restart; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /************************************************************************ |
| 704 | * * |
| 705 | * Dedicated parser for templates * |
| 706 | * * |
| 707 | ************************************************************************/ |
| 708 | |
| 709 | #define TODO \ |
| 710 | xmlGenericError(xmlGenericErrorContext, \ |
| 711 | "Unimplemented block at %s:%d\n", \ |
| 712 | __FILE__, __LINE__); |
| 713 | #define CUR (*ctxt->cur) |
| 714 | #define SKIP(val) ctxt->cur += (val) |
| 715 | #define NXT(val) ctxt->cur[(val)] |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 716 | #define PEEKPREV(val) ctxt->cur[-(val)] |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 717 | #define CUR_PTR ctxt->cur |
| 718 | |
| 719 | #define SKIP_BLANKS \ |
Daniel Veillard | 427174f | 2003-12-10 10:42:59 +0000 | [diff] [blame] | 720 | while (IS_BLANK_CH(CUR)) NEXT |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 721 | |
| 722 | #define CURRENT (*ctxt->cur) |
| 723 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 724 | |
| 725 | |
| 726 | #define PUSH(op, val, val2) \ |
| 727 | if (xmlPatternAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error; |
| 728 | |
| 729 | #define XSLT_ERROR(X) \ |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 730 | { xsltError(ctxt, __FILE__, __LINE__, X); \ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 731 | ctxt->error = (X); return; } |
| 732 | |
| 733 | #define XSLT_ERROR0(X) \ |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 734 | { xsltError(ctxt, __FILE__, __LINE__, X); \ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 735 | ctxt->error = (X); return(0); } |
| 736 | |
| 737 | #if 0 |
| 738 | /** |
| 739 | * xmlPatScanLiteral: |
| 740 | * @ctxt: the XPath Parser context |
| 741 | * |
| 742 | * Parse an XPath Litteral: |
| 743 | * |
| 744 | * [29] Literal ::= '"' [^"]* '"' |
| 745 | * | "'" [^']* "'" |
| 746 | * |
| 747 | * Returns the Literal parsed or NULL |
| 748 | */ |
| 749 | |
| 750 | static xmlChar * |
| 751 | xmlPatScanLiteral(xmlPatParserContextPtr ctxt) { |
| 752 | const xmlChar *q, *cur; |
| 753 | xmlChar *ret = NULL; |
| 754 | int val, len; |
| 755 | |
| 756 | SKIP_BLANKS; |
| 757 | if (CUR == '"') { |
| 758 | NEXT; |
| 759 | cur = q = CUR_PTR; |
| 760 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 761 | while ((IS_CHAR(val)) && (val != '"')) { |
| 762 | cur += len; |
| 763 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 764 | } |
| 765 | if (!IS_CHAR(val)) { |
| 766 | ctxt->error = 1; |
| 767 | return(NULL); |
| 768 | } else { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 769 | if (ctxt->dict) |
| 770 | ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q); |
| 771 | else |
| 772 | ret = xmlStrndup(q, cur - q); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 773 | } |
| 774 | cur += len; |
| 775 | CUR_PTR = cur; |
| 776 | } else if (CUR == '\'') { |
| 777 | NEXT; |
| 778 | cur = q = CUR_PTR; |
| 779 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 780 | while ((IS_CHAR(val)) && (val != '\'')) { |
| 781 | cur += len; |
| 782 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 783 | } |
| 784 | if (!IS_CHAR(val)) { |
| 785 | ctxt->error = 1; |
| 786 | return(NULL); |
| 787 | } else { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 788 | if (ctxt->dict) |
| 789 | ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q); |
| 790 | else |
| 791 | ret = xmlStrndup(q, cur - q); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 792 | } |
| 793 | cur += len; |
| 794 | CUR_PTR = cur; |
| 795 | } else { |
| 796 | /* XP_ERROR(XPATH_START_LITERAL_ERROR); */ |
| 797 | ctxt->error = 1; |
| 798 | return(NULL); |
| 799 | } |
| 800 | return(ret); |
| 801 | } |
| 802 | #endif |
| 803 | |
| 804 | /** |
| 805 | * xmlPatScanName: |
| 806 | * @ctxt: the XPath Parser context |
| 807 | * |
| 808 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 809 | * CombiningChar | Extender |
| 810 | * |
| 811 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 812 | * |
| 813 | * [6] Names ::= Name (S Name)* |
| 814 | * |
| 815 | * Returns the Name parsed or NULL |
| 816 | */ |
| 817 | |
| 818 | static xmlChar * |
| 819 | xmlPatScanName(xmlPatParserContextPtr ctxt) { |
| 820 | const xmlChar *q, *cur; |
| 821 | xmlChar *ret = NULL; |
| 822 | int val, len; |
| 823 | |
| 824 | SKIP_BLANKS; |
| 825 | |
| 826 | cur = q = CUR_PTR; |
| 827 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 828 | if (!IS_LETTER(val) && (val != '_') && (val != ':')) |
| 829 | return(NULL); |
| 830 | |
| 831 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 832 | (val == '.') || (val == '-') || |
| 833 | (val == '_') || |
| 834 | (IS_COMBINING(val)) || |
| 835 | (IS_EXTENDER(val))) { |
| 836 | cur += len; |
| 837 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 838 | } |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 839 | if (ctxt->dict) |
| 840 | ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q); |
| 841 | else |
| 842 | ret = xmlStrndup(q, cur - q); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 843 | CUR_PTR = cur; |
| 844 | return(ret); |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * xmlPatScanNCName: |
| 849 | * @ctxt: the XPath Parser context |
| 850 | * |
| 851 | * Parses a non qualified name |
| 852 | * |
| 853 | * Returns the Name parsed or NULL |
| 854 | */ |
| 855 | |
| 856 | static xmlChar * |
| 857 | xmlPatScanNCName(xmlPatParserContextPtr ctxt) { |
| 858 | const xmlChar *q, *cur; |
| 859 | xmlChar *ret = NULL; |
| 860 | int val, len; |
| 861 | |
| 862 | SKIP_BLANKS; |
| 863 | |
| 864 | cur = q = CUR_PTR; |
| 865 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 866 | if (!IS_LETTER(val) && (val != '_')) |
| 867 | return(NULL); |
| 868 | |
| 869 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 870 | (val == '.') || (val == '-') || |
| 871 | (val == '_') || |
| 872 | (IS_COMBINING(val)) || |
| 873 | (IS_EXTENDER(val))) { |
| 874 | cur += len; |
| 875 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 876 | } |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 877 | if (ctxt->dict) |
| 878 | ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q); |
| 879 | else |
| 880 | ret = xmlStrndup(q, cur - q); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 881 | CUR_PTR = cur; |
| 882 | return(ret); |
| 883 | } |
| 884 | |
| 885 | #if 0 |
| 886 | /** |
| 887 | * xmlPatScanQName: |
| 888 | * @ctxt: the XPath Parser context |
| 889 | * @prefix: the place to store the prefix |
| 890 | * |
| 891 | * Parse a qualified name |
| 892 | * |
| 893 | * Returns the Name parsed or NULL |
| 894 | */ |
| 895 | |
| 896 | static xmlChar * |
| 897 | xmlPatScanQName(xmlPatParserContextPtr ctxt, xmlChar **prefix) { |
| 898 | xmlChar *ret = NULL; |
| 899 | |
| 900 | *prefix = NULL; |
| 901 | ret = xmlPatScanNCName(ctxt); |
| 902 | if (CUR == ':') { |
| 903 | *prefix = ret; |
| 904 | NEXT; |
| 905 | ret = xmlPatScanNCName(ctxt); |
| 906 | } |
| 907 | return(ret); |
| 908 | } |
| 909 | #endif |
| 910 | |
| 911 | /** |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 912 | * xmlCompileAttributeTest: |
| 913 | * @ctxt: the compilation context |
| 914 | * |
| 915 | * Compile an attribute test. |
| 916 | */ |
| 917 | static void |
| 918 | xmlCompileAttributeTest(xmlPatParserContextPtr ctxt) { |
| 919 | xmlChar *token = NULL; |
| 920 | xmlChar *name = NULL; |
| 921 | xmlChar *URL = NULL; |
| 922 | |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 923 | SKIP_BLANKS; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 924 | name = xmlPatScanNCName(ctxt); |
| 925 | if (name == NULL) { |
| 926 | if (CUR == '*') { |
| 927 | PUSH(XML_OP_ATTR, NULL, NULL); |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 928 | NEXT; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 929 | } else { |
| 930 | ERROR(NULL, NULL, NULL, |
| 931 | "xmlCompileAttributeTest : Name expected\n"); |
| 932 | ctxt->error = 1; |
| 933 | } |
| 934 | return; |
| 935 | } |
| 936 | if (CUR == ':') { |
| 937 | int i; |
| 938 | xmlChar *prefix = name; |
| 939 | |
| 940 | NEXT; |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 941 | |
| 942 | if (IS_BLANK_CH(CUR)) { |
| 943 | ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL); |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 944 | XML_PAT_FREE_STRING(ctxt, prefix); |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 945 | ctxt->error = 1; |
| 946 | goto error; |
| 947 | } |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 948 | /* |
| 949 | * This is a namespace match |
| 950 | */ |
| 951 | token = xmlPatScanName(ctxt); |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 952 | if ((prefix[0] == 'x') && |
| 953 | (prefix[1] == 'm') && |
| 954 | (prefix[2] == 'l') && |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 955 | (prefix[3] == 0)) |
| 956 | { |
| 957 | XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE); |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 958 | } else { |
| 959 | for (i = 0;i < ctxt->nb_namespaces;i++) { |
| 960 | if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 961 | XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i]) |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 962 | break; |
| 963 | } |
| 964 | } |
| 965 | if (i >= ctxt->nb_namespaces) { |
| 966 | ERROR5(NULL, NULL, NULL, |
| 967 | "xmlCompileAttributeTest : no namespace bound to prefix %s\n", |
| 968 | prefix); |
| 969 | ctxt->error = 1; |
| 970 | goto error; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 973 | XML_PAT_FREE_STRING(ctxt, prefix); |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 974 | if (token == NULL) { |
| 975 | if (CUR == '*') { |
| 976 | NEXT; |
| 977 | PUSH(XML_OP_ATTR, NULL, URL); |
| 978 | } else { |
| 979 | ERROR(NULL, NULL, NULL, |
| 980 | "xmlCompileAttributeTest : Name expected\n"); |
| 981 | ctxt->error = 1; |
| 982 | goto error; |
| 983 | } |
| 984 | } else { |
| 985 | PUSH(XML_OP_ATTR, token, URL); |
| 986 | } |
| 987 | } else { |
| 988 | PUSH(XML_OP_ATTR, name, NULL); |
| 989 | } |
| 990 | return; |
| 991 | error: |
| 992 | if (URL != NULL) |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 993 | XML_PAT_FREE_STRING(ctxt, URL) |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 994 | if (token != NULL) |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 995 | XML_PAT_FREE_STRING(ctxt, token); |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 998 | /** |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 999 | * xmlCompileStepPattern: |
| 1000 | * @ctxt: the compilation context |
| 1001 | * |
| 1002 | * Compile the Step Pattern and generates a precompiled |
| 1003 | * form suitable for fast matching. |
| 1004 | * |
| 1005 | * [3] Step ::= '.' | NameTest |
| 1006 | * [4] NameTest ::= QName | '*' | NCName ':' '*' |
| 1007 | */ |
| 1008 | |
| 1009 | static void |
| 1010 | xmlCompileStepPattern(xmlPatParserContextPtr ctxt) { |
| 1011 | xmlChar *token = NULL; |
| 1012 | xmlChar *name = NULL; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1013 | xmlChar *URL = NULL; |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1014 | int hasBlanks = 0; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1015 | |
| 1016 | SKIP_BLANKS; |
| 1017 | if (CUR == '.') { |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1018 | /* |
| 1019 | * Context node. |
| 1020 | */ |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1021 | NEXT; |
| 1022 | PUSH(XML_OP_ELEM, NULL, NULL); |
| 1023 | return; |
| 1024 | } |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1025 | if (CUR == '@') { |
| 1026 | /* |
| 1027 | * Attribute test. |
| 1028 | */ |
| 1029 | if (XML_STREAM_XS_IDC_SEL(ctxt->comp)) { |
| 1030 | ERROR5(NULL, NULL, NULL, |
| 1031 | "Unexpected attribute axis in '%s'.\n", ctxt->base); |
| 1032 | ctxt->error = 1; |
| 1033 | return; |
| 1034 | } |
| 1035 | NEXT; |
| 1036 | xmlCompileAttributeTest(ctxt); |
| 1037 | if (ctxt->error != 0) |
| 1038 | goto error; |
| 1039 | return; |
| 1040 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1041 | name = xmlPatScanNCName(ctxt); |
| 1042 | if (name == NULL) { |
| 1043 | if (CUR == '*') { |
| 1044 | NEXT; |
| 1045 | PUSH(XML_OP_ALL, NULL, NULL); |
| 1046 | return; |
| 1047 | } else { |
| 1048 | ERROR(NULL, NULL, NULL, |
| 1049 | "xmlCompileStepPattern : Name expected\n"); |
| 1050 | ctxt->error = 1; |
| 1051 | return; |
| 1052 | } |
| 1053 | } |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1054 | if (IS_BLANK_CH(CUR)) { |
| 1055 | hasBlanks = 1; |
| 1056 | SKIP_BLANKS; |
| 1057 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1058 | if (CUR == ':') { |
| 1059 | NEXT; |
| 1060 | if (CUR != ':') { |
| 1061 | xmlChar *prefix = name; |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1062 | int i; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1063 | |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1064 | if (hasBlanks || IS_BLANK_CH(CUR)) { |
| 1065 | ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL); |
| 1066 | ctxt->error = 1; |
| 1067 | goto error; |
| 1068 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1069 | /* |
| 1070 | * This is a namespace match |
| 1071 | */ |
| 1072 | token = xmlPatScanName(ctxt); |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 1073 | if ((prefix[0] == 'x') && |
| 1074 | (prefix[1] == 'm') && |
| 1075 | (prefix[2] == 'l') && |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1076 | (prefix[3] == 0)) |
| 1077 | { |
| 1078 | XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE) |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 1079 | } else { |
| 1080 | for (i = 0;i < ctxt->nb_namespaces;i++) { |
| 1081 | if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1082 | XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i]) |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 1083 | break; |
| 1084 | } |
Daniel Veillard | d4301ab | 2005-02-03 22:24:10 +0000 | [diff] [blame] | 1085 | } |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 1086 | if (i >= ctxt->nb_namespaces) { |
| 1087 | ERROR5(NULL, NULL, NULL, |
| 1088 | "xmlCompileStepPattern : no namespace bound to prefix %s\n", |
| 1089 | prefix); |
| 1090 | ctxt->error = 1; |
| 1091 | goto error; |
| 1092 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1093 | } |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1094 | XML_PAT_FREE_STRING(ctxt, prefix); |
Rob Richards | 3108ba9 | 2007-12-06 10:08:52 +0000 | [diff] [blame] | 1095 | name = NULL; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1096 | if (token == NULL) { |
| 1097 | if (CUR == '*') { |
| 1098 | NEXT; |
| 1099 | PUSH(XML_OP_NS, URL, NULL); |
| 1100 | } else { |
| 1101 | ERROR(NULL, NULL, NULL, |
| 1102 | "xmlCompileStepPattern : Name expected\n"); |
| 1103 | ctxt->error = 1; |
| 1104 | goto error; |
| 1105 | } |
| 1106 | } else { |
| 1107 | PUSH(XML_OP_ELEM, token, URL); |
| 1108 | } |
| 1109 | } else { |
| 1110 | NEXT; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1111 | if (xmlStrEqual(name, (const xmlChar *) "child")) { |
| 1112 | XML_PAT_FREE_STRING(ctxt, name); |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1113 | name = xmlPatScanName(ctxt); |
| 1114 | if (name == NULL) { |
| 1115 | if (CUR == '*') { |
| 1116 | NEXT; |
| 1117 | PUSH(XML_OP_ALL, NULL, NULL); |
| 1118 | return; |
| 1119 | } else { |
| 1120 | ERROR(NULL, NULL, NULL, |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1121 | "xmlCompileStepPattern : QName expected\n"); |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1122 | ctxt->error = 1; |
| 1123 | goto error; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1126 | if (CUR == ':') { |
| 1127 | xmlChar *prefix = name; |
| 1128 | int i; |
| 1129 | |
| 1130 | NEXT; |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1131 | if (IS_BLANK_CH(CUR)) { |
| 1132 | ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL); |
| 1133 | ctxt->error = 1; |
| 1134 | goto error; |
| 1135 | } |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1136 | /* |
| 1137 | * This is a namespace match |
| 1138 | */ |
| 1139 | token = xmlPatScanName(ctxt); |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 1140 | if ((prefix[0] == 'x') && |
| 1141 | (prefix[1] == 'm') && |
| 1142 | (prefix[2] == 'l') && |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1143 | (prefix[3] == 0)) |
| 1144 | { |
| 1145 | XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE) |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 1146 | } else { |
| 1147 | for (i = 0;i < ctxt->nb_namespaces;i++) { |
| 1148 | if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1149 | XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i]) |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 1150 | break; |
| 1151 | } |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1152 | } |
Kasimier T. Buchcik | 627e9a9 | 2005-07-22 22:37:35 +0000 | [diff] [blame] | 1153 | if (i >= ctxt->nb_namespaces) { |
| 1154 | ERROR5(NULL, NULL, NULL, |
| 1155 | "xmlCompileStepPattern : no namespace bound " |
| 1156 | "to prefix %s\n", prefix); |
| 1157 | ctxt->error = 1; |
| 1158 | goto error; |
| 1159 | } |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1160 | } |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1161 | XML_PAT_FREE_STRING(ctxt, prefix); |
Rob Richards | 3108ba9 | 2007-12-06 10:08:52 +0000 | [diff] [blame] | 1162 | name = NULL; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1163 | if (token == NULL) { |
| 1164 | if (CUR == '*') { |
| 1165 | NEXT; |
| 1166 | PUSH(XML_OP_NS, URL, NULL); |
| 1167 | } else { |
| 1168 | ERROR(NULL, NULL, NULL, |
| 1169 | "xmlCompileStepPattern : Name expected\n"); |
| 1170 | ctxt->error = 1; |
| 1171 | goto error; |
| 1172 | } |
| 1173 | } else { |
| 1174 | PUSH(XML_OP_CHILD, token, URL); |
| 1175 | } |
| 1176 | } else |
| 1177 | PUSH(XML_OP_CHILD, name, NULL); |
| 1178 | return; |
| 1179 | } else if (xmlStrEqual(name, (const xmlChar *) "attribute")) { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1180 | XML_PAT_FREE_STRING(ctxt, name) |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1181 | name = NULL; |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1182 | if (XML_STREAM_XS_IDC_SEL(ctxt->comp)) { |
| 1183 | ERROR5(NULL, NULL, NULL, |
| 1184 | "Unexpected attribute axis in '%s'.\n", ctxt->base); |
| 1185 | ctxt->error = 1; |
| 1186 | goto error; |
| 1187 | } |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1188 | xmlCompileAttributeTest(ctxt); |
| 1189 | if (ctxt->error != 0) |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1190 | goto error; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1191 | return; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1192 | } else { |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1193 | ERROR5(NULL, NULL, NULL, |
| 1194 | "The 'element' or 'attribute' axis is expected.\n", NULL); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1195 | ctxt->error = 1; |
| 1196 | goto error; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1197 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } else if (CUR == '*') { |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 1200 | if (name != NULL) { |
| 1201 | ctxt->error = 1; |
| 1202 | goto error; |
| 1203 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1204 | NEXT; |
| 1205 | PUSH(XML_OP_ALL, token, NULL); |
| 1206 | } else { |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1207 | PUSH(XML_OP_ELEM, name, NULL); |
| 1208 | } |
| 1209 | return; |
| 1210 | error: |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1211 | if (URL != NULL) |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1212 | XML_PAT_FREE_STRING(ctxt, URL) |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1213 | if (token != NULL) |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1214 | XML_PAT_FREE_STRING(ctxt, token) |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1215 | if (name != NULL) |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1216 | XML_PAT_FREE_STRING(ctxt, name) |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * xmlCompilePathPattern: |
| 1221 | * @ctxt: the compilation context |
| 1222 | * |
| 1223 | * Compile the Path Pattern and generates a precompiled |
| 1224 | * form suitable for fast matching. |
| 1225 | * |
| 1226 | * [5] Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest ) |
| 1227 | */ |
| 1228 | static void |
| 1229 | xmlCompilePathPattern(xmlPatParserContextPtr ctxt) { |
| 1230 | SKIP_BLANKS; |
William M. Brack | 537f117 | 2005-06-14 22:02:59 +0000 | [diff] [blame] | 1231 | if (CUR == '/') { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 1232 | ctxt->comp->flags |= PAT_FROM_ROOT; |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 1233 | } else if ((CUR == '.') || (ctxt->comp->flags & XML_PATTERN_NOTPATTERN)) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 1234 | ctxt->comp->flags |= PAT_FROM_CUR; |
| 1235 | } |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 1236 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1237 | if ((CUR == '/') && (NXT(1) == '/')) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 1238 | PUSH(XML_OP_ANCESTOR, NULL, NULL); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1239 | NEXT; |
| 1240 | NEXT; |
| 1241 | } else if ((CUR == '.') && (NXT(1) == '/') && (NXT(2) == '/')) { |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 1242 | PUSH(XML_OP_ANCESTOR, NULL, NULL); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1243 | NEXT; |
| 1244 | NEXT; |
| 1245 | NEXT; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1246 | /* Check for incompleteness. */ |
| 1247 | SKIP_BLANKS; |
| 1248 | if (CUR == 0) { |
| 1249 | ERROR5(NULL, NULL, NULL, |
| 1250 | "Incomplete expression '%s'.\n", ctxt->base); |
| 1251 | ctxt->error = 1; |
| 1252 | goto error; |
| 1253 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1254 | } |
| 1255 | if (CUR == '@') { |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1256 | NEXT; |
| 1257 | xmlCompileAttributeTest(ctxt); |
| 1258 | SKIP_BLANKS; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1259 | /* TODO: check for incompleteness */ |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 1260 | if (CUR != 0) { |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1261 | xmlCompileStepPattern(ctxt); |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1262 | if (ctxt->error != 0) |
| 1263 | goto error; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1264 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1265 | } else { |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1266 | if (CUR == '/') { |
| 1267 | PUSH(XML_OP_ROOT, NULL, NULL); |
| 1268 | NEXT; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1269 | /* Check for incompleteness. */ |
| 1270 | SKIP_BLANKS; |
| 1271 | if (CUR == 0) { |
| 1272 | ERROR5(NULL, NULL, NULL, |
| 1273 | "Incomplete expression '%s'.\n", ctxt->base); |
| 1274 | ctxt->error = 1; |
| 1275 | goto error; |
| 1276 | } |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1277 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1278 | xmlCompileStepPattern(ctxt); |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1279 | if (ctxt->error != 0) |
| 1280 | goto error; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1281 | SKIP_BLANKS; |
| 1282 | while (CUR == '/') { |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 1283 | if (NXT(1) == '/') { |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1284 | PUSH(XML_OP_ANCESTOR, NULL, NULL); |
| 1285 | NEXT; |
| 1286 | NEXT; |
| 1287 | SKIP_BLANKS; |
| 1288 | xmlCompileStepPattern(ctxt); |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1289 | if (ctxt->error != 0) |
| 1290 | goto error; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1291 | } else { |
| 1292 | PUSH(XML_OP_PARENT, NULL, NULL); |
| 1293 | NEXT; |
| 1294 | SKIP_BLANKS; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1295 | if (CUR == 0) { |
| 1296 | ERROR5(NULL, NULL, NULL, |
| 1297 | "Incomplete expression '%s'.\n", ctxt->base); |
| 1298 | ctxt->error = 1; |
| 1299 | goto error; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1300 | } |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1301 | xmlCompileStepPattern(ctxt); |
| 1302 | if (ctxt->error != 0) |
| 1303 | goto error; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 1307 | if (CUR != 0) { |
| 1308 | ERROR5(NULL, NULL, NULL, |
| 1309 | "Failed to compile pattern %s\n", ctxt->base); |
| 1310 | ctxt->error = 1; |
| 1311 | } |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 1312 | error: |
| 1313 | return; |
| 1314 | } |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1315 | |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1316 | /** |
| 1317 | * xmlCompileIDCXPathPath: |
| 1318 | * @ctxt: the compilation context |
| 1319 | * |
| 1320 | * Compile the Path Pattern and generates a precompiled |
| 1321 | * form suitable for fast matching. |
| 1322 | * |
| 1323 | * [5] Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest ) |
| 1324 | */ |
| 1325 | static void |
| 1326 | xmlCompileIDCXPathPath(xmlPatParserContextPtr ctxt) { |
| 1327 | SKIP_BLANKS; |
| 1328 | if (CUR == '/') { |
| 1329 | ERROR5(NULL, NULL, NULL, |
| 1330 | "Unexpected selection of the document root in '%s'.\n", |
| 1331 | ctxt->base); |
| 1332 | goto error; |
| 1333 | } |
| 1334 | ctxt->comp->flags |= PAT_FROM_CUR; |
| 1335 | |
| 1336 | if (CUR == '.') { |
| 1337 | /* "." - "self::node()" */ |
| 1338 | NEXT; |
| 1339 | SKIP_BLANKS; |
| 1340 | if (CUR == 0) { |
| 1341 | /* |
| 1342 | * Selection of the context node. |
| 1343 | */ |
| 1344 | PUSH(XML_OP_ELEM, NULL, NULL); |
| 1345 | return; |
| 1346 | } |
| 1347 | if (CUR != '/') { |
| 1348 | /* TODO: A more meaningful error message. */ |
| 1349 | ERROR5(NULL, NULL, NULL, |
| 1350 | "Unexpected token after '.' in '%s'.\n", ctxt->base); |
| 1351 | goto error; |
| 1352 | } |
| 1353 | /* "./" - "self::node()/" */ |
| 1354 | NEXT; |
| 1355 | SKIP_BLANKS; |
| 1356 | if (CUR == '/') { |
| 1357 | if (IS_BLANK_CH(PEEKPREV(1))) { |
| 1358 | /* |
| 1359 | * Disallow "./ /" |
| 1360 | */ |
| 1361 | ERROR5(NULL, NULL, NULL, |
| 1362 | "Unexpected '/' token in '%s'.\n", ctxt->base); |
| 1363 | goto error; |
| 1364 | } |
| 1365 | /* ".//" - "self:node()/descendant-or-self::node()/" */ |
| 1366 | PUSH(XML_OP_ANCESTOR, NULL, NULL); |
| 1367 | NEXT; |
| 1368 | SKIP_BLANKS; |
| 1369 | } |
| 1370 | if (CUR == 0) |
| 1371 | goto error_unfinished; |
| 1372 | } |
| 1373 | /* |
| 1374 | * Process steps. |
| 1375 | */ |
| 1376 | do { |
| 1377 | xmlCompileStepPattern(ctxt); |
| 1378 | if (ctxt->error != 0) |
| 1379 | goto error; |
| 1380 | SKIP_BLANKS; |
| 1381 | if (CUR != '/') |
| 1382 | break; |
| 1383 | PUSH(XML_OP_PARENT, NULL, NULL); |
| 1384 | NEXT; |
| 1385 | SKIP_BLANKS; |
| 1386 | if (CUR == '/') { |
| 1387 | /* |
| 1388 | * Disallow subsequent '//'. |
| 1389 | */ |
| 1390 | ERROR5(NULL, NULL, NULL, |
| 1391 | "Unexpected subsequent '//' in '%s'.\n", |
| 1392 | ctxt->base); |
| 1393 | goto error; |
| 1394 | } |
| 1395 | if (CUR == 0) |
| 1396 | goto error_unfinished; |
| 1397 | |
| 1398 | } while (CUR != 0); |
| 1399 | |
| 1400 | if (CUR != 0) { |
| 1401 | ERROR5(NULL, NULL, NULL, |
| 1402 | "Failed to compile expression '%s'.\n", ctxt->base); |
| 1403 | ctxt->error = 1; |
| 1404 | } |
| 1405 | return; |
| 1406 | error: |
| 1407 | ctxt->error = 1; |
| 1408 | return; |
| 1409 | |
| 1410 | error_unfinished: |
| 1411 | ctxt->error = 1; |
| 1412 | ERROR5(NULL, NULL, NULL, |
| 1413 | "Unfinished expression '%s'.\n", ctxt->base); |
| 1414 | return; |
| 1415 | } |
| 1416 | |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1417 | /************************************************************************ |
| 1418 | * * |
| 1419 | * The streaming code * |
| 1420 | * * |
| 1421 | ************************************************************************/ |
| 1422 | |
| 1423 | #ifdef DEBUG_STREAMING |
| 1424 | static void |
| 1425 | xmlDebugStreamComp(xmlStreamCompPtr stream) { |
| 1426 | int i; |
| 1427 | |
| 1428 | if (stream == NULL) { |
| 1429 | printf("Stream: NULL\n"); |
| 1430 | return; |
| 1431 | } |
| 1432 | printf("Stream: %d steps\n", stream->nbStep); |
| 1433 | for (i = 0;i < stream->nbStep;i++) { |
| 1434 | if (stream->steps[i].ns != NULL) { |
| 1435 | printf("{%s}", stream->steps[i].ns); |
| 1436 | } |
| 1437 | if (stream->steps[i].name == NULL) { |
| 1438 | printf("* "); |
| 1439 | } else { |
| 1440 | printf("%s ", stream->steps[i].name); |
| 1441 | } |
| 1442 | if (stream->steps[i].flags & XML_STREAM_STEP_ROOT) |
| 1443 | printf("root "); |
| 1444 | if (stream->steps[i].flags & XML_STREAM_STEP_DESC) |
| 1445 | printf("// "); |
| 1446 | if (stream->steps[i].flags & XML_STREAM_STEP_FINAL) |
| 1447 | printf("final "); |
| 1448 | printf("\n"); |
| 1449 | } |
| 1450 | } |
| 1451 | static void |
| 1452 | xmlDebugStreamCtxt(xmlStreamCtxtPtr ctxt, int match) { |
| 1453 | int i; |
| 1454 | |
| 1455 | if (ctxt == NULL) { |
| 1456 | printf("Stream: NULL\n"); |
| 1457 | return; |
| 1458 | } |
| 1459 | printf("Stream: level %d, %d states: ", ctxt->level, ctxt->nbState); |
| 1460 | if (match) |
| 1461 | printf("matches\n"); |
| 1462 | else |
| 1463 | printf("\n"); |
| 1464 | for (i = 0;i < ctxt->nbState;i++) { |
| 1465 | if (ctxt->states[2 * i] < 0) |
| 1466 | printf(" %d: free\n", i); |
| 1467 | else { |
| 1468 | printf(" %d: step %d, level %d", i, ctxt->states[2 * i], |
| 1469 | ctxt->states[(2 * i) + 1]); |
| 1470 | if (ctxt->comp->steps[ctxt->states[2 * i]].flags & |
| 1471 | XML_STREAM_STEP_DESC) |
| 1472 | printf(" //\n"); |
| 1473 | else |
| 1474 | printf("\n"); |
| 1475 | } |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | #endif |
| 1479 | /** |
| 1480 | * xmlNewStreamComp: |
| 1481 | * @size: the number of expected steps |
| 1482 | * |
| 1483 | * build a new compiled pattern for streaming |
| 1484 | * |
| 1485 | * Returns the new structure or NULL in case of error. |
| 1486 | */ |
| 1487 | static xmlStreamCompPtr |
| 1488 | xmlNewStreamComp(int size) { |
| 1489 | xmlStreamCompPtr cur; |
| 1490 | |
| 1491 | if (size < 4) |
| 1492 | size = 4; |
| 1493 | |
| 1494 | cur = (xmlStreamCompPtr) xmlMalloc(sizeof(xmlStreamComp)); |
| 1495 | if (cur == NULL) { |
| 1496 | ERROR(NULL, NULL, NULL, |
| 1497 | "xmlNewStreamComp: malloc failed\n"); |
| 1498 | return(NULL); |
| 1499 | } |
| 1500 | memset(cur, 0, sizeof(xmlStreamComp)); |
| 1501 | cur->steps = (xmlStreamStepPtr) xmlMalloc(size * sizeof(xmlStreamStep)); |
| 1502 | if (cur->steps == NULL) { |
| 1503 | xmlFree(cur); |
| 1504 | ERROR(NULL, NULL, NULL, |
| 1505 | "xmlNewStreamComp: malloc failed\n"); |
| 1506 | return(NULL); |
| 1507 | } |
| 1508 | cur->nbStep = 0; |
| 1509 | cur->maxStep = size; |
| 1510 | return(cur); |
| 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * xmlFreeStreamComp: |
| 1515 | * @comp: the compiled pattern for streaming |
| 1516 | * |
| 1517 | * Free the compiled pattern for streaming |
| 1518 | */ |
| 1519 | static void |
| 1520 | xmlFreeStreamComp(xmlStreamCompPtr comp) { |
| 1521 | if (comp != NULL) { |
| 1522 | if (comp->steps != NULL) |
| 1523 | xmlFree(comp->steps); |
| 1524 | if (comp->dict != NULL) |
| 1525 | xmlDictFree(comp->dict); |
| 1526 | xmlFree(comp); |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | /** |
| 1531 | * xmlStreamCompAddStep: |
| 1532 | * @comp: the compiled pattern for streaming |
| 1533 | * @name: the first string, the name, or NULL for * |
| 1534 | * @ns: the second step, the namespace name |
| 1535 | * @flags: the flags for that step |
| 1536 | * |
| 1537 | * Add a new step to the compiled pattern |
| 1538 | * |
| 1539 | * Returns -1 in case of error or the step index if successful |
| 1540 | */ |
| 1541 | static int |
| 1542 | xmlStreamCompAddStep(xmlStreamCompPtr comp, const xmlChar *name, |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1543 | const xmlChar *ns, int nodeType, int flags) { |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1544 | xmlStreamStepPtr cur; |
| 1545 | |
| 1546 | if (comp->nbStep >= comp->maxStep) { |
| 1547 | cur = (xmlStreamStepPtr) xmlRealloc(comp->steps, |
| 1548 | comp->maxStep * 2 * sizeof(xmlStreamStep)); |
| 1549 | if (cur == NULL) { |
| 1550 | ERROR(NULL, NULL, NULL, |
| 1551 | "xmlNewStreamComp: malloc failed\n"); |
| 1552 | return(-1); |
| 1553 | } |
| 1554 | comp->steps = cur; |
| 1555 | comp->maxStep *= 2; |
| 1556 | } |
| 1557 | cur = &comp->steps[comp->nbStep++]; |
| 1558 | cur->flags = flags; |
| 1559 | cur->name = name; |
| 1560 | cur->ns = ns; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1561 | cur->nodeType = nodeType; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1562 | return(comp->nbStep - 1); |
| 1563 | } |
| 1564 | |
| 1565 | /** |
| 1566 | * xmlStreamCompile: |
| 1567 | * @comp: the precompiled pattern |
| 1568 | * |
| 1569 | * Tries to stream compile a pattern |
| 1570 | * |
| 1571 | * Returns -1 in case of failure and 0 in case of success. |
| 1572 | */ |
| 1573 | static int |
| 1574 | xmlStreamCompile(xmlPatternPtr comp) { |
| 1575 | xmlStreamCompPtr stream; |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1576 | int i, s = 0, root = 0, flags = 0, prevs = -1; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1577 | xmlStepOp step; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1578 | |
| 1579 | if ((comp == NULL) || (comp->steps == NULL)) |
| 1580 | return(-1); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 1581 | /* |
| 1582 | * special case for . |
| 1583 | */ |
| 1584 | if ((comp->nbStep == 1) && |
| 1585 | (comp->steps[0].op == XML_OP_ELEM) && |
| 1586 | (comp->steps[0].value == NULL) && |
| 1587 | (comp->steps[0].value2 == NULL)) { |
| 1588 | stream = xmlNewStreamComp(0); |
| 1589 | if (stream == NULL) |
| 1590 | return(-1); |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1591 | /* Note that the stream will have no steps in this case. */ |
| 1592 | stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE; |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 1593 | comp->stream = stream; |
| 1594 | return(0); |
| 1595 | } |
| 1596 | |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1597 | stream = xmlNewStreamComp((comp->nbStep / 2) + 1); |
| 1598 | if (stream == NULL) |
| 1599 | return(-1); |
| 1600 | if (comp->dict != NULL) { |
| 1601 | stream->dict = comp->dict; |
| 1602 | xmlDictReference(stream->dict); |
| 1603 | } |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 1604 | |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1605 | i = 0; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1606 | if (comp->flags & PAT_FROM_ROOT) |
| 1607 | stream->flags |= XML_STREAM_FROM_ROOT; |
| 1608 | |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 1609 | for (;i < comp->nbStep;i++) { |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1610 | step = comp->steps[i]; |
| 1611 | switch (step.op) { |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1612 | case XML_OP_END: |
| 1613 | break; |
| 1614 | case XML_OP_ROOT: |
| 1615 | if (i != 0) |
| 1616 | goto error; |
| 1617 | root = 1; |
| 1618 | break; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1619 | case XML_OP_NS: |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1620 | s = xmlStreamCompAddStep(stream, NULL, step.value, |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1621 | XML_ELEMENT_NODE, flags); |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1622 | if (s < 0) |
| 1623 | goto error; |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1624 | prevs = s; |
| 1625 | flags = 0; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1626 | break; |
| 1627 | case XML_OP_ATTR: |
| 1628 | flags |= XML_STREAM_STEP_ATTR; |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1629 | prevs = -1; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1630 | s = xmlStreamCompAddStep(stream, |
| 1631 | step.value, step.value2, XML_ATTRIBUTE_NODE, flags); |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1632 | flags = 0; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1633 | if (s < 0) |
| 1634 | goto error; |
| 1635 | break; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1636 | case XML_OP_ELEM: |
| 1637 | if ((step.value == NULL) && (step.value2 == NULL)) { |
| 1638 | /* |
| 1639 | * We have a "." or "self::node()" here. |
| 1640 | * Eliminate redundant self::node() tests like in "/./." |
| 1641 | * or "//./" |
| 1642 | * The only case we won't eliminate is "//.", i.e. if |
| 1643 | * self::node() is the last node test and we had |
| 1644 | * continuation somewhere beforehand. |
| 1645 | */ |
| 1646 | if ((comp->nbStep == i + 1) && |
| 1647 | (flags & XML_STREAM_STEP_DESC)) { |
| 1648 | /* |
| 1649 | * Mark the special case where the expression resolves |
| 1650 | * to any type of node. |
| 1651 | */ |
| 1652 | if (comp->nbStep == i + 1) { |
| 1653 | stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE; |
| 1654 | } |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1655 | flags |= XML_STREAM_STEP_NODE; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1656 | s = xmlStreamCompAddStep(stream, NULL, NULL, |
| 1657 | XML_STREAM_ANY_NODE, flags); |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1658 | if (s < 0) |
| 1659 | goto error; |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1660 | flags = 0; |
| 1661 | /* |
| 1662 | * If there was a previous step, mark it to be added to |
| 1663 | * the result node-set; this is needed since only |
| 1664 | * the last step will be marked as "final" and only |
| 1665 | * "final" nodes are added to the resulting set. |
| 1666 | */ |
| 1667 | if (prevs != -1) { |
| 1668 | stream->steps[prevs].flags |= XML_STREAM_STEP_IN_SET; |
| 1669 | prevs = -1; |
| 1670 | } |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1671 | break; |
| 1672 | |
| 1673 | } else { |
| 1674 | /* Just skip this one. */ |
| 1675 | continue; |
| 1676 | } |
| 1677 | } |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1678 | /* An element node. */ |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1679 | s = xmlStreamCompAddStep(stream, step.value, step.value2, |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1680 | XML_ELEMENT_NODE, flags); |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1681 | if (s < 0) |
| 1682 | goto error; |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1683 | prevs = s; |
| 1684 | flags = 0; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1685 | break; |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 1686 | case XML_OP_CHILD: |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1687 | /* An element node child. */ |
| 1688 | s = xmlStreamCompAddStep(stream, step.value, step.value2, |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1689 | XML_ELEMENT_NODE, flags); |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1690 | if (s < 0) |
| 1691 | goto error; |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1692 | prevs = s; |
| 1693 | flags = 0; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1694 | break; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1695 | case XML_OP_ALL: |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1696 | s = xmlStreamCompAddStep(stream, NULL, NULL, |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1697 | XML_ELEMENT_NODE, flags); |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1698 | if (s < 0) |
| 1699 | goto error; |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1700 | prevs = s; |
| 1701 | flags = 0; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1702 | break; |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 1703 | case XML_OP_PARENT: |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | case XML_OP_ANCESTOR: |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1706 | /* Skip redundant continuations. */ |
| 1707 | if (flags & XML_STREAM_STEP_DESC) |
| 1708 | break; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1709 | flags |= XML_STREAM_STEP_DESC; |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1710 | /* |
| 1711 | * Mark the expression as having "//". |
| 1712 | */ |
| 1713 | if ((stream->flags & XML_STREAM_DESC) == 0) |
| 1714 | stream->flags |= XML_STREAM_DESC; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1715 | break; |
| 1716 | } |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1717 | } |
| 1718 | if ((! root) && (comp->flags & XML_PATTERN_NOTPATTERN) == 0) { |
| 1719 | /* |
| 1720 | * If this should behave like a real pattern, we will mark |
| 1721 | * the first step as having "//", to be reentrant on every |
| 1722 | * tree level. |
| 1723 | */ |
| 1724 | if ((stream->flags & XML_STREAM_DESC) == 0) |
| 1725 | stream->flags |= XML_STREAM_DESC; |
| 1726 | |
| 1727 | if (stream->nbStep > 0) { |
| 1728 | if ((stream->steps[0].flags & XML_STREAM_STEP_DESC) == 0) |
| 1729 | stream->steps[0].flags |= XML_STREAM_STEP_DESC; |
| 1730 | } |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1731 | } |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1732 | if (stream->nbStep <= s) |
| 1733 | goto error; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1734 | stream->steps[s].flags |= XML_STREAM_STEP_FINAL; |
| 1735 | if (root) |
| 1736 | stream->steps[0].flags |= XML_STREAM_STEP_ROOT; |
| 1737 | #ifdef DEBUG_STREAMING |
| 1738 | xmlDebugStreamComp(stream); |
| 1739 | #endif |
| 1740 | comp->stream = stream; |
| 1741 | return(0); |
| 1742 | error: |
| 1743 | xmlFreeStreamComp(stream); |
| 1744 | return(0); |
| 1745 | } |
| 1746 | |
| 1747 | /** |
| 1748 | * xmlNewStreamCtxt: |
| 1749 | * @size: the number of expected states |
| 1750 | * |
| 1751 | * build a new stream context |
| 1752 | * |
| 1753 | * Returns the new structure or NULL in case of error. |
| 1754 | */ |
| 1755 | static xmlStreamCtxtPtr |
| 1756 | xmlNewStreamCtxt(xmlStreamCompPtr stream) { |
| 1757 | xmlStreamCtxtPtr cur; |
| 1758 | |
| 1759 | cur = (xmlStreamCtxtPtr) xmlMalloc(sizeof(xmlStreamCtxt)); |
| 1760 | if (cur == NULL) { |
| 1761 | ERROR(NULL, NULL, NULL, |
| 1762 | "xmlNewStreamCtxt: malloc failed\n"); |
| 1763 | return(NULL); |
| 1764 | } |
| 1765 | memset(cur, 0, sizeof(xmlStreamCtxt)); |
| 1766 | cur->states = (int *) xmlMalloc(4 * 2 * sizeof(int)); |
| 1767 | if (cur->states == NULL) { |
| 1768 | xmlFree(cur); |
| 1769 | ERROR(NULL, NULL, NULL, |
| 1770 | "xmlNewStreamCtxt: malloc failed\n"); |
| 1771 | return(NULL); |
| 1772 | } |
| 1773 | cur->nbState = 0; |
| 1774 | cur->maxState = 4; |
| 1775 | cur->level = 0; |
| 1776 | cur->comp = stream; |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1777 | cur->blockLevel = -1; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1778 | return(cur); |
| 1779 | } |
| 1780 | |
| 1781 | /** |
| 1782 | * xmlFreeStreamCtxt: |
| 1783 | * @stream: the stream context |
| 1784 | * |
| 1785 | * Free the stream context |
| 1786 | */ |
| 1787 | void |
| 1788 | xmlFreeStreamCtxt(xmlStreamCtxtPtr stream) { |
Daniel Veillard | 2b2e02d | 2005-02-05 23:20:22 +0000 | [diff] [blame] | 1789 | xmlStreamCtxtPtr next; |
| 1790 | |
| 1791 | while (stream != NULL) { |
| 1792 | next = stream->next; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1793 | if (stream->states != NULL) |
| 1794 | xmlFree(stream->states); |
| 1795 | xmlFree(stream); |
Daniel Veillard | 2b2e02d | 2005-02-05 23:20:22 +0000 | [diff] [blame] | 1796 | stream = next; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | /** |
| 1801 | * xmlStreamCtxtAddState: |
| 1802 | * @comp: the stream context |
| 1803 | * @idx: the step index for that streaming state |
| 1804 | * |
| 1805 | * Add a new state to the stream context |
| 1806 | * |
| 1807 | * Returns -1 in case of error or the state index if successful |
| 1808 | */ |
| 1809 | static int |
| 1810 | xmlStreamCtxtAddState(xmlStreamCtxtPtr comp, int idx, int level) { |
| 1811 | int i; |
| 1812 | for (i = 0;i < comp->nbState;i++) { |
| 1813 | if (comp->states[2 * i] < 0) { |
| 1814 | comp->states[2 * i] = idx; |
| 1815 | comp->states[2 * i + 1] = level; |
| 1816 | return(i); |
| 1817 | } |
| 1818 | } |
| 1819 | if (comp->nbState >= comp->maxState) { |
| 1820 | int *cur; |
| 1821 | |
| 1822 | cur = (int *) xmlRealloc(comp->states, |
| 1823 | comp->maxState * 4 * sizeof(int)); |
| 1824 | if (cur == NULL) { |
| 1825 | ERROR(NULL, NULL, NULL, |
| 1826 | "xmlNewStreamCtxt: malloc failed\n"); |
| 1827 | return(-1); |
| 1828 | } |
| 1829 | comp->states = cur; |
| 1830 | comp->maxState *= 2; |
| 1831 | } |
| 1832 | comp->states[2 * comp->nbState] = idx; |
| 1833 | comp->states[2 * comp->nbState++ + 1] = level; |
| 1834 | return(comp->nbState - 1); |
| 1835 | } |
| 1836 | |
| 1837 | /** |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1838 | * xmlStreamPushInternal: |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1839 | * @stream: the stream context |
| 1840 | * @name: the current name |
| 1841 | * @ns: the namespace name |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1842 | * @nodeType: the type of the node |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1843 | * |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 1844 | * Push new data onto the stream. NOTE: if the call xmlPatterncompile() |
| 1845 | * indicated a dictionary, then strings for name and ns will be expected |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1846 | * to come from the dictionary. |
| 1847 | * Both @name and @ns being NULL means the / i.e. the root of the document. |
| 1848 | * This can also act as a reset. |
| 1849 | * |
| 1850 | * Returns: -1 in case of error, 1 if the current state in the stream is a |
| 1851 | * match and 0 otherwise. |
| 1852 | */ |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1853 | static int |
| 1854 | xmlStreamPushInternal(xmlStreamCtxtPtr stream, |
| 1855 | const xmlChar *name, const xmlChar *ns, |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1856 | int nodeType) { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1857 | int ret = 0, err = 0, final = 0, tmp, i, m, match, stepNr, desc; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1858 | xmlStreamCompPtr comp; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1859 | xmlStreamStep step; |
Daniel Veillard | 2b2e02d | 2005-02-05 23:20:22 +0000 | [diff] [blame] | 1860 | #ifdef DEBUG_STREAMING |
| 1861 | xmlStreamCtxtPtr orig = stream; |
| 1862 | #endif |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1863 | |
| 1864 | if ((stream == NULL) || (stream->nbState < 0)) |
| 1865 | return(-1); |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1866 | |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 1867 | while (stream != NULL) { |
| 1868 | comp = stream->comp; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1869 | |
| 1870 | if ((nodeType == XML_ELEMENT_NODE) && |
| 1871 | (name == NULL) && (ns == NULL)) { |
| 1872 | /* We have a document node here (or a reset). */ |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 1873 | stream->nbState = 0; |
| 1874 | stream->level = 0; |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1875 | stream->blockLevel = -1; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1876 | if (comp->flags & XML_STREAM_FROM_ROOT) { |
| 1877 | if (comp->nbStep == 0) { |
| 1878 | /* TODO: We have a "/." here? */ |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 1879 | ret = 1; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1880 | } else { |
| 1881 | if ((comp->nbStep == 1) && |
| 1882 | (comp->steps[0].nodeType == XML_STREAM_ANY_NODE) && |
| 1883 | (comp->steps[0].flags & XML_STREAM_STEP_DESC)) |
| 1884 | { |
| 1885 | /* |
| 1886 | * In the case of "//." the document node will match |
| 1887 | * as well. |
| 1888 | */ |
| 1889 | ret = 1; |
| 1890 | } else if (comp->steps[0].flags & XML_STREAM_STEP_ROOT) { |
| 1891 | /* TODO: Do we need this ? */ |
| 1892 | tmp = xmlStreamCtxtAddState(stream, 0, 0); |
| 1893 | if (tmp < 0) |
| 1894 | err++; |
| 1895 | } |
| 1896 | } |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 1897 | } |
Daniel Veillard | 2b2e02d | 2005-02-05 23:20:22 +0000 | [diff] [blame] | 1898 | stream = stream->next; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 1899 | continue; /* while */ |
| 1900 | } |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1901 | |
| 1902 | /* |
| 1903 | * Fast check for ".". |
| 1904 | */ |
| 1905 | if (comp->nbStep == 0) { |
Kasimier T. Buchcik | 2267856 | 2005-05-09 16:01:05 +0000 | [diff] [blame] | 1906 | /* |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 1907 | * / and . are handled at the XPath node set creation |
| 1908 | * level by checking min depth |
| 1909 | */ |
| 1910 | if (stream->flags & XML_PATTERN_XPATH) { |
| 1911 | stream = stream->next; |
| 1912 | continue; /* while */ |
| 1913 | } |
| 1914 | /* |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 1915 | * For non-pattern like evaluation like XML Schema IDCs |
| 1916 | * or traditional XPath expressions, this will match if |
| 1917 | * we are at the first level only, otherwise on every level. |
Kasimier T. Buchcik | 2267856 | 2005-05-09 16:01:05 +0000 | [diff] [blame] | 1918 | */ |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1919 | if ((nodeType != XML_ATTRIBUTE_NODE) && |
Kasimier T. Buchcik | 2267856 | 2005-05-09 16:01:05 +0000 | [diff] [blame] | 1920 | (((stream->flags & XML_PATTERN_NOTPATTERN) == 0) || |
| 1921 | (stream->level == 0))) { |
| 1922 | ret = 1; |
| 1923 | } |
| 1924 | stream->level++; |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1925 | goto stream_next; |
| 1926 | } |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1927 | if (stream->blockLevel != -1) { |
| 1928 | /* |
| 1929 | * Skip blocked expressions. |
| 1930 | */ |
| 1931 | stream->level++; |
| 1932 | goto stream_next; |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 1933 | } |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 1934 | |
| 1935 | if ((nodeType != XML_ELEMENT_NODE) && |
| 1936 | (nodeType != XML_ATTRIBUTE_NODE) && |
| 1937 | ((comp->flags & XML_STREAM_FINAL_IS_ANY_NODE) == 0)) { |
| 1938 | /* |
| 1939 | * No need to process nodes of other types if we don't |
| 1940 | * resolve to those types. |
| 1941 | * TODO: Do we need to block the context here? |
| 1942 | */ |
| 1943 | stream->level++; |
| 1944 | goto stream_next; |
| 1945 | } |
| 1946 | |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 1947 | /* |
| 1948 | * Check evolution of existing states |
| 1949 | */ |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1950 | i = 0; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 1951 | m = stream->nbState; |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1952 | while (i < m) { |
| 1953 | if ((comp->flags & XML_STREAM_DESC) == 0) { |
| 1954 | /* |
| 1955 | * If there is no "//", then only the last |
| 1956 | * added state is of interest. |
| 1957 | */ |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1958 | stepNr = stream->states[2 * (stream->nbState -1)]; |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1959 | /* |
| 1960 | * TODO: Security check, should not happen, remove it. |
| 1961 | */ |
| 1962 | if (stream->states[(2 * (stream->nbState -1)) + 1] < |
| 1963 | stream->level) { |
| 1964 | return (-1); |
| 1965 | } |
| 1966 | desc = 0; |
| 1967 | /* loop-stopper */ |
| 1968 | i = m; |
| 1969 | } else { |
| 1970 | /* |
| 1971 | * If there are "//", then we need to process every "//" |
| 1972 | * occuring in the states, plus any other state for this |
| 1973 | * level. |
| 1974 | */ |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1975 | stepNr = stream->states[2 * i]; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 1976 | |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1977 | /* TODO: should not happen anymore: dead states */ |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1978 | if (stepNr < 0) |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1979 | goto next_state; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 1980 | |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1981 | tmp = stream->states[(2 * i) + 1]; |
| 1982 | |
| 1983 | /* skip new states just added */ |
| 1984 | if (tmp > stream->level) |
| 1985 | goto next_state; |
| 1986 | |
| 1987 | /* skip states at ancestor levels, except if "//" */ |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1988 | desc = comp->steps[stepNr].flags & XML_STREAM_STEP_DESC; |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 1989 | if ((tmp < stream->level) && (!desc)) |
| 1990 | goto next_state; |
| 1991 | } |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 1992 | /* |
| 1993 | * Check for correct node-type. |
| 1994 | */ |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 1995 | step = comp->steps[stepNr]; |
| 1996 | if (step.nodeType != nodeType) { |
| 1997 | if (step.nodeType == XML_ATTRIBUTE_NODE) { |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 1998 | /* |
| 1999 | * Block this expression for deeper evaluation. |
| 2000 | */ |
| 2001 | if ((comp->flags & XML_STREAM_DESC) == 0) |
| 2002 | stream->blockLevel = stream->level +1; |
Kasimier T. Buchcik | 2782027 | 2005-10-14 14:33:48 +0000 | [diff] [blame] | 2003 | goto next_state; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2004 | } else if (step.nodeType != XML_STREAM_ANY_NODE) |
Kasimier T. Buchcik | 2782027 | 2005-10-14 14:33:48 +0000 | [diff] [blame] | 2005 | goto next_state; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2006 | } |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2007 | /* |
| 2008 | * Compare local/namespace-name. |
| 2009 | */ |
| 2010 | match = 0; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2011 | if (step.nodeType == XML_STREAM_ANY_NODE) { |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2012 | match = 1; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2013 | } else if (step.name == NULL) { |
| 2014 | if (step.ns == NULL) { |
| 2015 | /* |
| 2016 | * This lets through all elements/attributes. |
| 2017 | */ |
| 2018 | match = 1; |
| 2019 | } else if (ns != NULL) |
| 2020 | match = xmlStrEqual(step.ns, ns); |
| 2021 | } else if (((step.ns != NULL) == (ns != NULL)) && |
| 2022 | (name != NULL) && |
| 2023 | (step.name[0] == name[0]) && |
| 2024 | xmlStrEqual(step.name, name) && |
| 2025 | ((step.ns == ns) || xmlStrEqual(step.ns, ns))) |
| 2026 | { |
| 2027 | match = 1; |
| 2028 | } |
| 2029 | #if 0 |
| 2030 | /* |
| 2031 | * TODO: Pointer comparison won't work, since not guaranteed that the given |
| 2032 | * values are in the same dict; especially if it's the namespace name, |
| 2033 | * normally coming from ns->href. We need a namespace dict mechanism ! |
| 2034 | */ |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2035 | } else if (comp->dict) { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2036 | if (step.name == NULL) { |
| 2037 | if (step.ns == NULL) |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2038 | match = 1; |
| 2039 | else |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2040 | match = (step.ns == ns); |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2041 | } else { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2042 | match = ((step.name == name) && (step.ns == ns)); |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2043 | } |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2044 | #endif /* if 0 ------------------------------------------------------- */ |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2045 | if (match) { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2046 | final = step.flags & XML_STREAM_STEP_FINAL; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2047 | if (desc) { |
| 2048 | if (final) { |
| 2049 | ret = 1; |
| 2050 | } else { |
| 2051 | /* descending match create a new state */ |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2052 | xmlStreamCtxtAddState(stream, stepNr + 1, |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2053 | stream->level + 1); |
| 2054 | } |
| 2055 | } else { |
| 2056 | if (final) { |
| 2057 | ret = 1; |
| 2058 | } else { |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2059 | xmlStreamCtxtAddState(stream, stepNr + 1, |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2060 | stream->level + 1); |
| 2061 | } |
| 2062 | } |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2063 | if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) { |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 2064 | /* |
| 2065 | * Check if we have a special case like "foo/bar//.", where |
| 2066 | * "foo" is selected as well. |
| 2067 | */ |
| 2068 | ret = 1; |
| 2069 | } |
| 2070 | } |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2071 | if (((comp->flags & XML_STREAM_DESC) == 0) && |
| 2072 | ((! match) || final)) { |
| 2073 | /* |
| 2074 | * Mark this expression as blocked for any evaluation at |
| 2075 | * deeper levels. Note that this includes "/foo" |
| 2076 | * expressions if the *pattern* behaviour is used. |
| 2077 | */ |
| 2078 | stream->blockLevel = stream->level +1; |
| 2079 | } |
| 2080 | next_state: |
| 2081 | i++; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2082 | } |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2083 | |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2084 | stream->level++; |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2085 | |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2086 | /* |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2087 | * Re/enter the expression. |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2088 | * Don't reenter if it's an absolute expression like "/foo", |
| 2089 | * except "//foo". |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2090 | */ |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2091 | step = comp->steps[0]; |
| 2092 | if (step.flags & XML_STREAM_STEP_ROOT) |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2093 | goto stream_next; |
| 2094 | |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2095 | desc = step.flags & XML_STREAM_STEP_DESC; |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2096 | if (stream->flags & XML_PATTERN_NOTPATTERN) { |
| 2097 | /* |
| 2098 | * Re/enter the expression if it is a "descendant" one, |
| 2099 | * or if we are at the 1st level of evaluation. |
| 2100 | */ |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 2101 | |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2102 | if (stream->level == 1) { |
| 2103 | if (XML_STREAM_XS_IDC(stream)) { |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 2104 | /* |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2105 | * XS-IDC: The missing "self::node()" will always |
| 2106 | * match the first given node. |
| 2107 | */ |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 2108 | goto stream_next; |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2109 | } else |
| 2110 | goto compare; |
| 2111 | } |
| 2112 | /* |
| 2113 | * A "//" is always reentrant. |
| 2114 | */ |
| 2115 | if (desc) |
| 2116 | goto compare; |
| 2117 | |
| 2118 | /* |
| 2119 | * XS-IDC: Process the 2nd level, since the missing |
| 2120 | * "self::node()" is responsible for the 2nd level being |
| 2121 | * the real start level. |
| 2122 | */ |
| 2123 | if ((stream->level == 2) && XML_STREAM_XS_IDC(stream)) |
| 2124 | goto compare; |
| 2125 | |
| 2126 | goto stream_next; |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2127 | } |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2128 | |
| 2129 | compare: |
| 2130 | /* |
| 2131 | * Check expected node-type. |
| 2132 | */ |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2133 | if (step.nodeType != nodeType) { |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2134 | if (nodeType == XML_ATTRIBUTE_NODE) |
Kasimier T. Buchcik | 2782027 | 2005-10-14 14:33:48 +0000 | [diff] [blame] | 2135 | goto stream_next; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2136 | else if (step.nodeType != XML_STREAM_ANY_NODE) |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2137 | goto stream_next; |
Kasimier T. Buchcik | 2782027 | 2005-10-14 14:33:48 +0000 | [diff] [blame] | 2138 | } |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2139 | /* |
| 2140 | * Compare local/namespace-name. |
| 2141 | */ |
| 2142 | match = 0; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2143 | if (step.nodeType == XML_STREAM_ANY_NODE) { |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2144 | match = 1; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2145 | } else if (step.name == NULL) { |
| 2146 | if (step.ns == NULL) { |
| 2147 | /* |
| 2148 | * This lets through all elements/attributes. |
| 2149 | */ |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2150 | match = 1; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2151 | } else if (ns != NULL) |
| 2152 | match = xmlStrEqual(step.ns, ns); |
| 2153 | } else if (((step.ns != NULL) == (ns != NULL)) && |
| 2154 | (name != NULL) && |
| 2155 | (step.name[0] == name[0]) && |
| 2156 | xmlStrEqual(step.name, name) && |
| 2157 | ((step.ns == ns) || xmlStrEqual(step.ns, ns))) |
| 2158 | { |
| 2159 | match = 1; |
| 2160 | } |
| 2161 | final = step.flags & XML_STREAM_STEP_FINAL; |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2162 | if (match) { |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2163 | if (final) |
| 2164 | ret = 1; |
| 2165 | else |
| 2166 | xmlStreamCtxtAddState(stream, 1, stream->level); |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2167 | if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) { |
Kasimier T. Buchcik | bb80f54 | 2006-01-05 14:44:45 +0000 | [diff] [blame] | 2168 | /* |
| 2169 | * Check if we have a special case like "foo//.", where |
| 2170 | * "foo" is selected as well. |
| 2171 | */ |
| 2172 | ret = 1; |
| 2173 | } |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2174 | } |
| 2175 | if (((comp->flags & XML_STREAM_DESC) == 0) && |
| 2176 | ((! match) || final)) { |
| 2177 | /* |
| 2178 | * Mark this expression as blocked for any evaluation at |
| 2179 | * deeper levels. |
| 2180 | */ |
| 2181 | stream->blockLevel = stream->level; |
| 2182 | } |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 2183 | |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2184 | stream_next: |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2185 | stream = stream->next; |
| 2186 | } /* while stream != NULL */ |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2187 | |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2188 | if (err > 0) |
| 2189 | ret = -1; |
Daniel Veillard | 2b2e02d | 2005-02-05 23:20:22 +0000 | [diff] [blame] | 2190 | #ifdef DEBUG_STREAMING |
| 2191 | xmlDebugStreamCtxt(orig, ret); |
| 2192 | #endif |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2193 | return(ret); |
| 2194 | } |
| 2195 | |
| 2196 | /** |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2197 | * xmlStreamPush: |
| 2198 | * @stream: the stream context |
| 2199 | * @name: the current name |
| 2200 | * @ns: the namespace name |
| 2201 | * |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 2202 | * Push new data onto the stream. NOTE: if the call xmlPatterncompile() |
| 2203 | * indicated a dictionary, then strings for name and ns will be expected |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2204 | * to come from the dictionary. |
| 2205 | * Both @name and @ns being NULL means the / i.e. the root of the document. |
| 2206 | * This can also act as a reset. |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2207 | * Otherwise the function will act as if it has been given an element-node. |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2208 | * |
| 2209 | * Returns: -1 in case of error, 1 if the current state in the stream is a |
| 2210 | * match and 0 otherwise. |
| 2211 | */ |
| 2212 | int |
| 2213 | xmlStreamPush(xmlStreamCtxtPtr stream, |
| 2214 | const xmlChar *name, const xmlChar *ns) { |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2215 | return (xmlStreamPushInternal(stream, name, ns, (int) XML_ELEMENT_NODE)); |
| 2216 | } |
| 2217 | |
| 2218 | /** |
Daniel Veillard | 6795260 | 2006-01-05 15:29:44 +0000 | [diff] [blame] | 2219 | * xmlStreamPushNode: |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2220 | * @stream: the stream context |
| 2221 | * @name: the current name |
| 2222 | * @ns: the namespace name |
| 2223 | * @nodeType: the type of the node being pushed |
| 2224 | * |
| 2225 | * Push new data onto the stream. NOTE: if the call xmlPatterncompile() |
| 2226 | * indicated a dictionary, then strings for name and ns will be expected |
| 2227 | * to come from the dictionary. |
| 2228 | * Both @name and @ns being NULL means the / i.e. the root of the document. |
| 2229 | * This can also act as a reset. |
| 2230 | * Different from xmlStreamPush() this function can be fed with nodes of type: |
| 2231 | * element-, attribute-, text-, cdata-section-, comment- and |
| 2232 | * processing-instruction-node. |
| 2233 | * |
| 2234 | * Returns: -1 in case of error, 1 if the current state in the stream is a |
| 2235 | * match and 0 otherwise. |
| 2236 | */ |
| 2237 | int |
| 2238 | xmlStreamPushNode(xmlStreamCtxtPtr stream, |
| 2239 | const xmlChar *name, const xmlChar *ns, |
| 2240 | int nodeType) |
| 2241 | { |
| 2242 | return (xmlStreamPushInternal(stream, name, ns, |
| 2243 | nodeType)); |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | /** |
| 2247 | * xmlStreamPushAttr: |
| 2248 | * @stream: the stream context |
| 2249 | * @name: the current name |
| 2250 | * @ns: the namespace name |
| 2251 | * |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 2252 | * Push new attribute data onto the stream. NOTE: if the call xmlPatterncompile() |
| 2253 | * indicated a dictionary, then strings for name and ns will be expected |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2254 | * to come from the dictionary. |
| 2255 | * Both @name and @ns being NULL means the / i.e. the root of the document. |
| 2256 | * This can also act as a reset. |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2257 | * Otherwise the function will act as if it has been given an attribute-node. |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2258 | * |
| 2259 | * Returns: -1 in case of error, 1 if the current state in the stream is a |
| 2260 | * match and 0 otherwise. |
| 2261 | */ |
| 2262 | int |
| 2263 | xmlStreamPushAttr(xmlStreamCtxtPtr stream, |
| 2264 | const xmlChar *name, const xmlChar *ns) { |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2265 | return (xmlStreamPushInternal(stream, name, ns, (int) XML_ATTRIBUTE_NODE)); |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | /** |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2269 | * xmlStreamPop: |
| 2270 | * @stream: the stream context |
| 2271 | * |
| 2272 | * push one level from the stream. |
| 2273 | * |
| 2274 | * Returns: -1 in case of error, 0 otherwise. |
| 2275 | */ |
| 2276 | int |
| 2277 | xmlStreamPop(xmlStreamCtxtPtr stream) { |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2278 | int i, lev; |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 2279 | |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2280 | if (stream == NULL) |
| 2281 | return(-1); |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2282 | while (stream != NULL) { |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2283 | /* |
| 2284 | * Reset block-level. |
| 2285 | */ |
| 2286 | if (stream->blockLevel == stream->level) |
| 2287 | stream->blockLevel = -1; |
| 2288 | |
William M. Brack | f847700 | 2008-07-17 05:29:16 +0000 | [diff] [blame] | 2289 | /* |
| 2290 | * stream->level can be zero when XML_FINAL_IS_ANY_NODE is set |
| 2291 | * (see the thread at |
| 2292 | * http://mail.gnome.org/archives/xslt/2008-July/msg00027.html) |
| 2293 | */ |
| 2294 | if (stream->level) |
| 2295 | stream->level--; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2296 | /* |
| 2297 | * Check evolution of existing states |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2298 | */ |
| 2299 | for (i = stream->nbState -1; i >= 0; i--) { |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2300 | /* discard obsoleted states */ |
Kasimier T. Buchcik | 9ca11bf | 2005-06-14 19:24:47 +0000 | [diff] [blame] | 2301 | lev = stream->states[(2 * i) + 1]; |
| 2302 | if (lev > stream->level) |
| 2303 | stream->nbState--; |
| 2304 | if (lev <= stream->level) |
| 2305 | break; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2306 | } |
| 2307 | stream = stream->next; |
Daniel Veillard | 9740d1d | 2005-02-01 16:21:43 +0000 | [diff] [blame] | 2308 | } |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2309 | return(0); |
| 2310 | } |
| 2311 | |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2312 | /** |
| 2313 | * xmlStreamWantsAnyNode: |
Daniel Veillard | 6795260 | 2006-01-05 15:29:44 +0000 | [diff] [blame] | 2314 | * @streamCtxt: the stream context |
Kasimier T. Buchcik | 9725871 | 2006-01-05 12:30:43 +0000 | [diff] [blame] | 2315 | * |
| 2316 | * Query if the streaming pattern additionally needs to be fed with |
| 2317 | * text-, cdata-section-, comment- and processing-instruction-nodes. |
| 2318 | * If the result is 0 then only element-nodes and attribute-nodes |
| 2319 | * need to be pushed. |
| 2320 | * |
| 2321 | * Returns: 1 in case of need of nodes of the above described types, |
| 2322 | * 0 otherwise. -1 on API errors. |
| 2323 | */ |
| 2324 | int |
| 2325 | xmlStreamWantsAnyNode(xmlStreamCtxtPtr streamCtxt) |
| 2326 | { |
| 2327 | if (streamCtxt == NULL) |
| 2328 | return(-1); |
| 2329 | while (streamCtxt != NULL) { |
| 2330 | if (streamCtxt->comp->flags & XML_STREAM_FINAL_IS_ANY_NODE) |
| 2331 | return(1); |
| 2332 | streamCtxt = streamCtxt->next; |
| 2333 | } |
| 2334 | return(0); |
| 2335 | } |
| 2336 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2337 | /************************************************************************ |
| 2338 | * * |
| 2339 | * The public interfaces * |
| 2340 | * * |
| 2341 | ************************************************************************/ |
| 2342 | |
| 2343 | /** |
| 2344 | * xmlPatterncompile: |
| 2345 | * @pattern: the pattern to compile |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 2346 | * @dict: an optional dictionary for interned strings |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 2347 | * @flags: compilation flags, see xmlPatternFlags |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 2348 | * @namespaces: the prefix definitions, array of [URI, prefix] or NULL |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2349 | * |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 2350 | * Compile a pattern. |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2351 | * |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 2352 | * Returns the compiled form of the pattern or NULL in case of error |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2353 | */ |
| 2354 | xmlPatternPtr |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 2355 | xmlPatterncompile(const xmlChar *pattern, xmlDict *dict, int flags, |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 2356 | const xmlChar **namespaces) { |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2357 | xmlPatternPtr ret = NULL, cur; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2358 | xmlPatParserContextPtr ctxt = NULL; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2359 | const xmlChar *or, *start; |
| 2360 | xmlChar *tmp = NULL; |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 2361 | int type = 0; |
| 2362 | int streamable = 1; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2363 | |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2364 | if (pattern == NULL) |
| 2365 | return(NULL); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2366 | |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2367 | start = pattern; |
Daniel Veillard | 2b2e02d | 2005-02-05 23:20:22 +0000 | [diff] [blame] | 2368 | or = start; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2369 | while (*or != 0) { |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2370 | tmp = NULL; |
| 2371 | while ((*or != 0) && (*or != '|')) or++; |
| 2372 | if (*or == 0) |
| 2373 | ctxt = xmlNewPatParserContext(start, dict, namespaces); |
| 2374 | else { |
| 2375 | tmp = xmlStrndup(start, or - start); |
| 2376 | if (tmp != NULL) { |
| 2377 | ctxt = xmlNewPatParserContext(tmp, dict, namespaces); |
| 2378 | } |
| 2379 | or++; |
| 2380 | } |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2381 | if (ctxt == NULL) goto error; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2382 | cur = xmlNewPattern(); |
| 2383 | if (cur == NULL) goto error; |
Kasimier T. Buchcik | 6ed2eb4 | 2006-05-16 15:13:37 +0000 | [diff] [blame] | 2384 | /* |
| 2385 | * Assign string dict. |
| 2386 | */ |
| 2387 | if (dict) { |
| 2388 | cur->dict = dict; |
| 2389 | xmlDictReference(dict); |
| 2390 | } |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2391 | if (ret == NULL) |
| 2392 | ret = cur; |
| 2393 | else { |
| 2394 | cur->next = ret->next; |
| 2395 | ret->next = cur; |
| 2396 | } |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 2397 | cur->flags = flags; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2398 | ctxt->comp = cur; |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2399 | |
Kasimier T. Buchcik | 940ab0c | 2005-10-19 17:00:53 +0000 | [diff] [blame] | 2400 | if (XML_STREAM_XS_IDC(cur)) |
| 2401 | xmlCompileIDCXPathPath(ctxt); |
| 2402 | else |
| 2403 | xmlCompilePathPattern(ctxt); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 2404 | if (ctxt->error != 0) |
| 2405 | goto error; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2406 | xmlFreePatParserContext(ctxt); |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 2407 | ctxt = NULL; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2408 | |
| 2409 | |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 2410 | if (streamable) { |
| 2411 | if (type == 0) { |
| 2412 | type = cur->flags & (PAT_FROM_ROOT | PAT_FROM_CUR); |
| 2413 | } else if (type == PAT_FROM_ROOT) { |
| 2414 | if (cur->flags & PAT_FROM_CUR) |
| 2415 | streamable = 0; |
| 2416 | } else if (type == PAT_FROM_CUR) { |
| 2417 | if (cur->flags & PAT_FROM_ROOT) |
| 2418 | streamable = 0; |
| 2419 | } |
| 2420 | } |
| 2421 | if (streamable) |
| 2422 | xmlStreamCompile(cur); |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2423 | if (xmlReversePattern(cur) < 0) |
| 2424 | goto error; |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 2425 | if (tmp != NULL) { |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2426 | xmlFree(tmp); |
William M. Brack | ea152c0 | 2005-06-09 18:12:28 +0000 | [diff] [blame] | 2427 | tmp = NULL; |
| 2428 | } |
Daniel Veillard | 2b2e02d | 2005-02-05 23:20:22 +0000 | [diff] [blame] | 2429 | start = or; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2430 | } |
Daniel Veillard | fa1f77f | 2005-02-21 10:44:36 +0000 | [diff] [blame] | 2431 | if (streamable == 0) { |
| 2432 | cur = ret; |
| 2433 | while (cur != NULL) { |
| 2434 | if (cur->stream != NULL) { |
| 2435 | xmlFreeStreamComp(cur->stream); |
| 2436 | cur->stream = NULL; |
| 2437 | } |
| 2438 | cur = cur->next; |
| 2439 | } |
| 2440 | } |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 2441 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2442 | return(ret); |
| 2443 | error: |
| 2444 | if (ctxt != NULL) xmlFreePatParserContext(ctxt); |
| 2445 | if (ret != NULL) xmlFreePattern(ret); |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2446 | if (tmp != NULL) xmlFree(tmp); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2447 | return(NULL); |
| 2448 | } |
| 2449 | |
| 2450 | /** |
| 2451 | * xmlPatternMatch: |
| 2452 | * @comp: the precompiled pattern |
| 2453 | * @node: a node |
| 2454 | * |
William M. Brack | fbb619f | 2005-06-06 13:49:18 +0000 | [diff] [blame] | 2455 | * Test whether the node matches the pattern |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2456 | * |
| 2457 | * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure |
| 2458 | */ |
| 2459 | int |
| 2460 | xmlPatternMatch(xmlPatternPtr comp, xmlNodePtr node) |
| 2461 | { |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2462 | int ret = 0; |
| 2463 | |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2464 | if ((comp == NULL) || (node == NULL)) |
| 2465 | return(-1); |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2466 | |
| 2467 | while (comp != NULL) { |
| 2468 | ret = xmlPatMatch(comp, node); |
| 2469 | if (ret != 0) |
| 2470 | return(ret); |
| 2471 | comp = comp->next; |
| 2472 | } |
| 2473 | return(ret); |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2476 | /** |
| 2477 | * xmlPatternGetStreamCtxt: |
| 2478 | * @comp: the precompiled pattern |
| 2479 | * |
| 2480 | * Get a streaming context for that pattern |
| 2481 | * Use xmlFreeStreamCtxt to free the context. |
| 2482 | * |
| 2483 | * Returns a pointer to the context or NULL in case of failure |
| 2484 | */ |
| 2485 | xmlStreamCtxtPtr |
| 2486 | xmlPatternGetStreamCtxt(xmlPatternPtr comp) |
| 2487 | { |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2488 | xmlStreamCtxtPtr ret = NULL, cur; |
| 2489 | |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2490 | if ((comp == NULL) || (comp->stream == NULL)) |
| 2491 | return(NULL); |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2492 | |
| 2493 | while (comp != NULL) { |
| 2494 | if (comp->stream == NULL) |
| 2495 | goto failed; |
| 2496 | cur = xmlNewStreamCtxt(comp->stream); |
| 2497 | if (cur == NULL) |
| 2498 | goto failed; |
| 2499 | if (ret == NULL) |
| 2500 | ret = cur; |
| 2501 | else { |
| 2502 | cur->next = ret->next; |
| 2503 | ret->next = cur; |
| 2504 | } |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 2505 | cur->flags = comp->flags; |
Daniel Veillard | f1f08cf | 2005-02-05 16:35:04 +0000 | [diff] [blame] | 2506 | comp = comp->next; |
| 2507 | } |
| 2508 | return(ret); |
| 2509 | failed: |
| 2510 | xmlFreeStreamCtxt(ret); |
| 2511 | return(NULL); |
Daniel Veillard | 2fc6df9 | 2005-01-30 18:42:55 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 2514 | /** |
| 2515 | * xmlPatternStreamable: |
| 2516 | * @comp: the precompiled pattern |
| 2517 | * |
| 2518 | * Check if the pattern is streamable i.e. xmlPatternGetStreamCtxt() |
| 2519 | * should work. |
| 2520 | * |
| 2521 | * Returns 1 if streamable, 0 if not and -1 in case of error. |
| 2522 | */ |
| 2523 | int |
| 2524 | xmlPatternStreamable(xmlPatternPtr comp) { |
| 2525 | if (comp == NULL) |
| 2526 | return(-1); |
| 2527 | while (comp != NULL) { |
| 2528 | if (comp->stream == NULL) |
| 2529 | return(0); |
| 2530 | comp = comp->next; |
| 2531 | } |
| 2532 | return(1); |
| 2533 | } |
| 2534 | |
| 2535 | /** |
| 2536 | * xmlPatternMaxDepth: |
| 2537 | * @comp: the precompiled pattern |
| 2538 | * |
| 2539 | * Check the maximum depth reachable by a pattern |
| 2540 | * |
| 2541 | * Returns -2 if no limit (using //), otherwise the depth, |
| 2542 | * and -1 in case of error |
| 2543 | */ |
| 2544 | int |
| 2545 | xmlPatternMaxDepth(xmlPatternPtr comp) { |
| 2546 | int ret = 0, i; |
| 2547 | if (comp == NULL) |
| 2548 | return(-1); |
| 2549 | while (comp != NULL) { |
| 2550 | if (comp->stream == NULL) |
| 2551 | return(-1); |
| 2552 | for (i = 0;i < comp->stream->nbStep;i++) |
| 2553 | if (comp->stream->steps[i].flags & XML_STREAM_STEP_DESC) |
| 2554 | return(-2); |
| 2555 | if (comp->stream->nbStep > ret) |
| 2556 | ret = comp->stream->nbStep; |
| 2557 | comp = comp->next; |
| 2558 | } |
| 2559 | return(ret); |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 2560 | } |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 2561 | |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 2562 | /** |
| 2563 | * xmlPatternMinDepth: |
| 2564 | * @comp: the precompiled pattern |
| 2565 | * |
| 2566 | * Check the minimum depth reachable by a pattern, 0 mean the / or . are |
| 2567 | * part of the set. |
| 2568 | * |
| 2569 | * Returns -1 in case of error otherwise the depth, |
| 2570 | * |
| 2571 | */ |
| 2572 | int |
| 2573 | xmlPatternMinDepth(xmlPatternPtr comp) { |
| 2574 | int ret = 12345678; |
| 2575 | if (comp == NULL) |
| 2576 | return(-1); |
| 2577 | while (comp != NULL) { |
| 2578 | if (comp->stream == NULL) |
| 2579 | return(-1); |
| 2580 | if (comp->stream->nbStep < ret) |
| 2581 | ret = comp->stream->nbStep; |
| 2582 | if (ret == 0) |
| 2583 | return(0); |
| 2584 | comp = comp->next; |
| 2585 | } |
| 2586 | return(ret); |
Daniel Veillard | 56de87e | 2005-02-16 00:22:29 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | /** |
| 2590 | * xmlPatternFromRoot: |
| 2591 | * @comp: the precompiled pattern |
| 2592 | * |
| 2593 | * Check if the pattern must be looked at from the root. |
| 2594 | * |
| 2595 | * Returns 1 if true, 0 if false and -1 in case of error |
| 2596 | */ |
| 2597 | int |
| 2598 | xmlPatternFromRoot(xmlPatternPtr comp) { |
| 2599 | if (comp == NULL) |
| 2600 | return(-1); |
| 2601 | while (comp != NULL) { |
| 2602 | if (comp->stream == NULL) |
| 2603 | return(-1); |
| 2604 | if (comp->flags & PAT_FROM_ROOT) |
| 2605 | return(1); |
| 2606 | comp = comp->next; |
| 2607 | } |
| 2608 | return(0); |
| 2609 | |
| 2610 | } |
Kasimier T. Buchcik | 2a0fdd9 | 2005-02-17 21:34:45 +0000 | [diff] [blame] | 2611 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 2612 | #define bottom_pattern |
| 2613 | #include "elfgcchack.h" |
Daniel Veillard | b3de70c | 2003-12-02 22:32:15 +0000 | [diff] [blame] | 2614 | #endif /* LIBXML_PATTERN_ENABLED */ |