Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * xpointer.c : Code to handle XML Pointer |
3 | | * |
4 | | * Base implementation was made accordingly to |
5 | | * W3C Candidate Recommendation 7 June 2000 |
6 | | * http://www.w3.org/TR/2000/CR-xptr-20000607 |
7 | | * |
8 | | * Added support for the element() scheme described in: |
9 | | * W3C Proposed Recommendation 13 November 2002 |
10 | | * http://www.w3.org/TR/2002/PR-xptr-element-20021113/ |
11 | | * |
12 | | * See Copyright for the status of this software. |
13 | | * |
14 | | * Author: Daniel Veillard |
15 | | */ |
16 | | |
17 | | /* To avoid EBCDIC trouble when parsing on zOS */ |
18 | | #if defined(__MVS__) |
19 | | #pragma convert("ISO8859-1") |
20 | | #endif |
21 | | |
22 | | #define IN_LIBXML |
23 | | #include "libxml.h" |
24 | | |
25 | | /* |
26 | | * TODO: better handling of error cases, the full expression should |
27 | | * be parsed beforehand instead of a progressive evaluation |
28 | | * TODO: Access into entities references are not supported now ... |
29 | | * need a start to be able to pop out of entities refs since |
30 | | * parent is the entity declaration, not the ref. |
31 | | */ |
32 | | |
33 | | #include <string.h> |
34 | | #include <libxml/xpointer.h> |
35 | | #include <libxml/xmlmemory.h> |
36 | | #include <libxml/parserInternals.h> |
37 | | #include <libxml/uri.h> |
38 | | #include <libxml/xpath.h> |
39 | | #include <libxml/xpathInternals.h> |
40 | | #include <libxml/xmlerror.h> |
41 | | |
42 | | #ifdef LIBXML_XPTR_ENABLED |
43 | | |
44 | | /* Add support of the xmlns() xpointer scheme to initialize the namespaces */ |
45 | | #define XPTR_XMLNS_SCHEME |
46 | | |
47 | | #include "private/error.h" |
48 | | #include "private/xpath.h" |
49 | | |
50 | | /************************************************************************ |
51 | | * * |
52 | | * Some factorized error routines * |
53 | | * * |
54 | | ************************************************************************/ |
55 | | |
56 | | /** |
57 | | * Handle an XPointer error |
58 | | * |
59 | | * @param ctxt an XPTR evaluation context |
60 | | * @param code error code |
61 | | * @param msg error message |
62 | | * @param extra extra information |
63 | | */ |
64 | | static void LIBXML_ATTR_FORMAT(3,0) |
65 | | xmlXPtrErr(xmlXPathParserContextPtr ctxt, int code, |
66 | | const char * msg, const xmlChar *extra) |
67 | 0 | { |
68 | 0 | xmlStructuredErrorFunc serror = NULL; |
69 | 0 | void *data = NULL; |
70 | 0 | xmlNodePtr node = NULL; |
71 | 0 | int res; |
72 | |
|
73 | 0 | if (ctxt == NULL) |
74 | 0 | return; |
75 | | /* Only report the first error */ |
76 | 0 | if (ctxt->error != 0) |
77 | 0 | return; |
78 | | |
79 | 0 | ctxt->error = code; |
80 | |
|
81 | 0 | if (ctxt->context != NULL) { |
82 | 0 | xmlErrorPtr err = &ctxt->context->lastError; |
83 | | |
84 | | /* cleanup current last error */ |
85 | 0 | xmlResetError(err); |
86 | |
|
87 | 0 | err->domain = XML_FROM_XPOINTER; |
88 | 0 | err->code = code; |
89 | 0 | err->level = XML_ERR_ERROR; |
90 | 0 | err->str1 = (char *) xmlStrdup(ctxt->base); |
91 | 0 | if (err->str1 == NULL) { |
92 | 0 | xmlXPathPErrMemory(ctxt); |
93 | 0 | return; |
94 | 0 | } |
95 | 0 | err->int1 = ctxt->cur - ctxt->base; |
96 | 0 | err->node = ctxt->context->debugNode; |
97 | |
|
98 | 0 | serror = ctxt->context->error; |
99 | 0 | data = ctxt->context->userData; |
100 | 0 | node = ctxt->context->debugNode; |
101 | 0 | } |
102 | | |
103 | 0 | res = xmlRaiseError(serror, NULL, data, NULL, node, |
104 | 0 | XML_FROM_XPOINTER, code, XML_ERR_ERROR, NULL, 0, |
105 | 0 | (const char *) extra, (const char *) ctxt->base, |
106 | 0 | NULL, ctxt->cur - ctxt->base, 0, |
107 | 0 | msg, extra); |
108 | 0 | if (res < 0) |
109 | 0 | xmlXPathPErrMemory(ctxt); |
110 | 0 | } |
111 | | |
112 | | /************************************************************************ |
113 | | * * |
114 | | * A few helper functions for child sequences * |
115 | | * * |
116 | | ************************************************************************/ |
117 | | |
118 | | /** |
119 | | * @param cur the node |
120 | | * @param no the child number |
121 | | * @returns the `no`'th element child of `cur` or NULL |
122 | | */ |
123 | | static xmlNodePtr |
124 | 0 | xmlXPtrGetNthChild(xmlNodePtr cur, int no) { |
125 | 0 | int i; |
126 | 0 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) |
127 | 0 | return(cur); |
128 | 0 | cur = cur->children; |
129 | 0 | for (i = 0;i <= no;cur = cur->next) { |
130 | 0 | if (cur == NULL) |
131 | 0 | return(cur); |
132 | 0 | if ((cur->type == XML_ELEMENT_NODE) || |
133 | 0 | (cur->type == XML_DOCUMENT_NODE) || |
134 | 0 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
135 | 0 | i++; |
136 | 0 | if (i == no) |
137 | 0 | break; |
138 | 0 | } |
139 | 0 | } |
140 | 0 | return(cur); |
141 | 0 | } |
142 | | |
143 | | /************************************************************************ |
144 | | * * |
145 | | * The parser * |
146 | | * * |
147 | | ************************************************************************/ |
148 | | |
149 | | static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name); |
150 | | |
151 | | /* |
152 | | * Macros for accessing the content. Those should be used only by the parser, |
153 | | * and not exported. |
154 | | * |
155 | | * Dirty macros, i.e. one need to make assumption on the context to use them |
156 | | * |
157 | | * CUR returns the current xmlChar value, i.e. a 8 bit value |
158 | | * in ISO-Latin or UTF-8. |
159 | | * This should be used internally by the parser |
160 | | * only to compare to ASCII values otherwise it would break when |
161 | | * running with UTF-8 encoding. |
162 | | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
163 | | * to compare on ASCII based substring. |
164 | | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
165 | | * strings within the parser. |
166 | | * CURRENT Returns the current char value, with the full decoding of |
167 | | * UTF-8 if we are using this mode. It returns an int. |
168 | | * NEXT Skip to the next character, this does the proper decoding |
169 | | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
170 | | * It returns the pointer to the current xmlChar. |
171 | | */ |
172 | | |
173 | 0 | #define CUR (*ctxt->cur) |
174 | | #define SKIP(val) ctxt->cur += (val) |
175 | 0 | #define NXT(val) ctxt->cur[(val)] |
176 | | |
177 | | #define SKIP_BLANKS \ |
178 | 0 | while (IS_BLANK_CH(*(ctxt->cur))) NEXT |
179 | | |
180 | | #define CURRENT (*ctxt->cur) |
181 | 0 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
182 | | |
183 | | /* |
184 | | * @param ctxt the XPointer Parser context |
185 | | * @param index the child number |
186 | | * |
187 | | * Move the current node of the nodeset on the stack to the |
188 | | * given child if found |
189 | | */ |
190 | | static void |
191 | 0 | xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) { |
192 | 0 | xmlNodePtr cur = NULL; |
193 | 0 | xmlXPathObjectPtr obj; |
194 | 0 | xmlNodeSetPtr oldset; |
195 | |
|
196 | 0 | CHECK_TYPE(XPATH_NODESET); |
197 | 0 | obj = xmlXPathValuePop(ctxt); |
198 | 0 | oldset = obj->nodesetval; |
199 | 0 | if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) { |
200 | 0 | xmlXPathFreeObject(obj); |
201 | 0 | xmlXPathValuePush(ctxt, xmlXPathNewNodeSet(NULL)); |
202 | 0 | return; |
203 | 0 | } |
204 | 0 | cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx); |
205 | 0 | if (cur == NULL) { |
206 | 0 | xmlXPathFreeObject(obj); |
207 | 0 | xmlXPathValuePush(ctxt, xmlXPathNewNodeSet(NULL)); |
208 | 0 | return; |
209 | 0 | } |
210 | 0 | oldset->nodeTab[0] = cur; |
211 | 0 | xmlXPathValuePush(ctxt, obj); |
212 | 0 | } |
213 | | |
214 | | /** |
215 | | * XPtrPart ::= 'xpointer' '(' XPtrExpr ')' |
216 | | * | Scheme '(' SchemeSpecificExpr ')' |
217 | | * |
218 | | * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes] |
219 | | * |
220 | | * SchemeSpecificExpr ::= StringWithBalancedParens |
221 | | * |
222 | | * StringWithBalancedParens ::= |
223 | | * [^()]* ('(' StringWithBalancedParens ')' [^()]*)* |
224 | | * [VC: Parenthesis escaping] |
225 | | * |
226 | | * XPtrExpr ::= Expr [VC: Parenthesis escaping] |
227 | | * |
228 | | * VC: Parenthesis escaping: |
229 | | * The end of an XPointer part is signaled by the right parenthesis ")" |
230 | | * character that is balanced with the left parenthesis "(" character |
231 | | * that began the part. Any unbalanced parenthesis character inside the |
232 | | * expression, even within literals, must be escaped with a circumflex (^) |
233 | | * character preceding it. If the expression contains any literal |
234 | | * occurrences of the circumflex, each must be escaped with an additional |
235 | | * circumflex (that is, ^^). If the unescaped parentheses in the expression |
236 | | * are not balanced, a syntax error results. |
237 | | * |
238 | | * Parse and evaluate an XPtrPart. Basically it generates the unescaped |
239 | | * string and if the scheme is 'xpointer' it will call the XPath interpreter. |
240 | | * |
241 | | * TODO: there is no new scheme registration mechanism |
242 | | * |
243 | | * @param ctxt the XPointer Parser context |
244 | | * @param name the preparsed Scheme for the XPtrPart |
245 | | */ |
246 | | |
247 | | static void |
248 | 0 | xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) { |
249 | 0 | xmlChar *buffer, *cur; |
250 | 0 | int len; |
251 | 0 | int level; |
252 | |
|
253 | 0 | if (name == NULL) |
254 | 0 | name = xmlXPathParseName(ctxt); |
255 | 0 | if (name == NULL) |
256 | 0 | XP_ERROR(XPATH_EXPR_ERROR); |
257 | |
|
258 | 0 | if (CUR != '(') { |
259 | 0 | xmlFree(name); |
260 | 0 | XP_ERROR(XPATH_EXPR_ERROR); |
261 | 0 | } |
262 | 0 | NEXT; |
263 | 0 | level = 1; |
264 | |
|
265 | 0 | len = xmlStrlen(ctxt->cur); |
266 | 0 | len++; |
267 | 0 | buffer = xmlMalloc(len); |
268 | 0 | if (buffer == NULL) { |
269 | 0 | xmlXPathPErrMemory(ctxt); |
270 | 0 | xmlFree(name); |
271 | 0 | return; |
272 | 0 | } |
273 | | |
274 | 0 | cur = buffer; |
275 | 0 | while (CUR != 0) { |
276 | 0 | if (CUR == ')') { |
277 | 0 | level--; |
278 | 0 | if (level == 0) { |
279 | 0 | NEXT; |
280 | 0 | break; |
281 | 0 | } |
282 | 0 | } else if (CUR == '(') { |
283 | 0 | level++; |
284 | 0 | } else if (CUR == '^') { |
285 | 0 | if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) { |
286 | 0 | NEXT; |
287 | 0 | } |
288 | 0 | } |
289 | 0 | *cur++ = CUR; |
290 | 0 | NEXT; |
291 | 0 | } |
292 | 0 | *cur = 0; |
293 | |
|
294 | 0 | if ((level != 0) && (CUR == 0)) { |
295 | 0 | xmlFree(name); |
296 | 0 | xmlFree(buffer); |
297 | 0 | XP_ERROR(XPTR_SYNTAX_ERROR); |
298 | 0 | } |
299 | | |
300 | 0 | if (xmlStrEqual(name, (xmlChar *) "xpointer") || |
301 | 0 | xmlStrEqual(name, (xmlChar *) "xpath1")) { |
302 | 0 | const xmlChar *oldBase = ctxt->base; |
303 | 0 | const xmlChar *oldCur = ctxt->cur; |
304 | |
|
305 | 0 | ctxt->cur = ctxt->base = buffer; |
306 | | /* |
307 | | * To evaluate an xpointer scheme element (4.3) we need: |
308 | | * context initialized to the root |
309 | | * context position initialized to 1 |
310 | | * context size initialized to 1 |
311 | | */ |
312 | 0 | ctxt->context->node = (xmlNodePtr)ctxt->context->doc; |
313 | 0 | ctxt->context->proximityPosition = 1; |
314 | 0 | ctxt->context->contextSize = 1; |
315 | 0 | xmlXPathEvalExpr(ctxt); |
316 | 0 | ctxt->base = oldBase; |
317 | 0 | ctxt->cur = oldCur; |
318 | 0 | } else if (xmlStrEqual(name, (xmlChar *) "element")) { |
319 | 0 | const xmlChar *oldBase = ctxt->base; |
320 | 0 | const xmlChar *oldCur = ctxt->cur; |
321 | 0 | xmlChar *name2; |
322 | |
|
323 | 0 | ctxt->cur = ctxt->base = buffer; |
324 | 0 | if (buffer[0] == '/') { |
325 | 0 | xmlXPathRoot(ctxt); |
326 | 0 | xmlXPtrEvalChildSeq(ctxt, NULL); |
327 | 0 | } else { |
328 | 0 | name2 = xmlXPathParseName(ctxt); |
329 | 0 | if (name2 == NULL) { |
330 | 0 | ctxt->base = oldBase; |
331 | 0 | ctxt->cur = oldCur; |
332 | 0 | xmlFree(buffer); |
333 | 0 | xmlFree(name); |
334 | 0 | XP_ERROR(XPATH_EXPR_ERROR); |
335 | 0 | } |
336 | 0 | xmlXPtrEvalChildSeq(ctxt, name2); |
337 | 0 | } |
338 | 0 | ctxt->base = oldBase; |
339 | 0 | ctxt->cur = oldCur; |
340 | 0 | #ifdef XPTR_XMLNS_SCHEME |
341 | 0 | } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) { |
342 | 0 | const xmlChar *oldBase = ctxt->base; |
343 | 0 | const xmlChar *oldCur = ctxt->cur; |
344 | 0 | xmlChar *prefix; |
345 | |
|
346 | 0 | ctxt->cur = ctxt->base = buffer; |
347 | 0 | prefix = xmlXPathParseNCName(ctxt); |
348 | 0 | if (prefix == NULL) { |
349 | 0 | ctxt->base = oldBase; |
350 | 0 | ctxt->cur = oldCur; |
351 | 0 | xmlFree(buffer); |
352 | 0 | xmlFree(name); |
353 | 0 | XP_ERROR(XPTR_SYNTAX_ERROR); |
354 | 0 | } |
355 | 0 | SKIP_BLANKS; |
356 | 0 | if (CUR != '=') { |
357 | 0 | ctxt->base = oldBase; |
358 | 0 | ctxt->cur = oldCur; |
359 | 0 | xmlFree(prefix); |
360 | 0 | xmlFree(buffer); |
361 | 0 | xmlFree(name); |
362 | 0 | XP_ERROR(XPTR_SYNTAX_ERROR); |
363 | 0 | } |
364 | 0 | NEXT; |
365 | 0 | SKIP_BLANKS; |
366 | |
|
367 | 0 | if (xmlXPathRegisterNs(ctxt->context, prefix, ctxt->cur) < 0) |
368 | 0 | xmlXPathPErrMemory(ctxt); |
369 | 0 | ctxt->base = oldBase; |
370 | 0 | ctxt->cur = oldCur; |
371 | 0 | xmlFree(prefix); |
372 | 0 | #endif /* XPTR_XMLNS_SCHEME */ |
373 | 0 | } else { |
374 | 0 | xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME, |
375 | 0 | "unsupported scheme '%s'\n", name); |
376 | 0 | } |
377 | 0 | xmlFree(buffer); |
378 | 0 | xmlFree(name); |
379 | 0 | } |
380 | | |
381 | | /** |
382 | | * FullXPtr ::= XPtrPart (S? XPtrPart)* |
383 | | * |
384 | | * As the specs says: |
385 | | * ----------- |
386 | | * When multiple XPtrParts are provided, they must be evaluated in |
387 | | * left-to-right order. If evaluation of one part fails, the nexti |
388 | | * is evaluated. The following conditions cause XPointer part failure: |
389 | | * |
390 | | * - An unknown scheme |
391 | | * - A scheme that does not locate any sub-resource present in the resource |
392 | | * - A scheme that is not applicable to the media type of the resource |
393 | | * |
394 | | * The XPointer application must consume a failed XPointer part and |
395 | | * attempt to evaluate the next one, if any. The result of the first |
396 | | * XPointer part whose evaluation succeeds is taken to be the fragment |
397 | | * located by the XPointer as a whole. If all the parts fail, the result |
398 | | * for the XPointer as a whole is a sub-resource error. |
399 | | * ----------- |
400 | | * |
401 | | * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based |
402 | | * expressions or other schemes. |
403 | | * |
404 | | * @param ctxt the XPointer Parser context |
405 | | * @param name the preparsed Scheme for the first XPtrPart |
406 | | */ |
407 | | static void |
408 | 0 | xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) { |
409 | 0 | if (name == NULL) |
410 | 0 | name = xmlXPathParseName(ctxt); |
411 | 0 | if (name == NULL) |
412 | 0 | XP_ERROR(XPATH_EXPR_ERROR); |
413 | 0 | while (name != NULL) { |
414 | 0 | ctxt->error = XPATH_EXPRESSION_OK; |
415 | 0 | xmlXPtrEvalXPtrPart(ctxt, name); |
416 | | |
417 | | /* in case of syntax error, break here */ |
418 | 0 | if ((ctxt->error != XPATH_EXPRESSION_OK) && |
419 | 0 | (ctxt->error != XML_XPTR_UNKNOWN_SCHEME)) |
420 | 0 | return; |
421 | | |
422 | | /* |
423 | | * If the returned value is a non-empty nodeset |
424 | | * or location set, return here. |
425 | | */ |
426 | 0 | if (ctxt->value != NULL) { |
427 | 0 | xmlXPathObjectPtr obj = ctxt->value; |
428 | |
|
429 | 0 | switch (obj->type) { |
430 | 0 | case XPATH_NODESET: { |
431 | 0 | xmlNodeSetPtr loc = ctxt->value->nodesetval; |
432 | 0 | if ((loc != NULL) && (loc->nodeNr > 0)) |
433 | 0 | return; |
434 | 0 | break; |
435 | 0 | } |
436 | 0 | default: |
437 | 0 | break; |
438 | 0 | } |
439 | | |
440 | | /* |
441 | | * Evaluating to improper values is equivalent to |
442 | | * a sub-resource error, clean-up the stack |
443 | | */ |
444 | 0 | do { |
445 | 0 | obj = xmlXPathValuePop(ctxt); |
446 | 0 | if (obj != NULL) { |
447 | 0 | xmlXPathFreeObject(obj); |
448 | 0 | } |
449 | 0 | } while (obj != NULL); |
450 | 0 | } |
451 | | |
452 | | /* |
453 | | * Is there another XPointer part. |
454 | | */ |
455 | 0 | SKIP_BLANKS; |
456 | 0 | name = xmlXPathParseName(ctxt); |
457 | 0 | } |
458 | 0 | } |
459 | | |
460 | | /** |
461 | | * ChildSeq ::= '/1' ('/' [0-9]*)* |
462 | | * | Name ('/' [0-9]*)+ |
463 | | * |
464 | | * Parse and evaluate a Child Sequence. This routine also handle the |
465 | | * case of a Bare Name used to get a document ID. |
466 | | * |
467 | | * @param ctxt the XPointer Parser context |
468 | | * @param name a possible ID name of the child sequence |
469 | | */ |
470 | | static void |
471 | 0 | xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) { |
472 | | /* |
473 | | * XPointer don't allow by syntax to address in multirooted trees |
474 | | * this might prove useful in some cases, warn about it. |
475 | | */ |
476 | 0 | if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) { |
477 | 0 | xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START, |
478 | 0 | "warning: ChildSeq not starting by /1\n", NULL); |
479 | 0 | } |
480 | |
|
481 | 0 | if (name != NULL) { |
482 | 0 | xmlXPathValuePush(ctxt, xmlXPathNewString(name)); |
483 | 0 | xmlFree(name); |
484 | 0 | xmlXPathIdFunction(ctxt, 1); |
485 | 0 | CHECK_ERROR; |
486 | 0 | } |
487 | | |
488 | 0 | while (CUR == '/') { |
489 | 0 | int child = 0, overflow = 0; |
490 | 0 | NEXT; |
491 | |
|
492 | 0 | while ((CUR >= '0') && (CUR <= '9')) { |
493 | 0 | int d = CUR - '0'; |
494 | 0 | if (child > INT_MAX / 10) |
495 | 0 | overflow = 1; |
496 | 0 | else |
497 | 0 | child *= 10; |
498 | 0 | if (child > INT_MAX - d) |
499 | 0 | overflow = 1; |
500 | 0 | else |
501 | 0 | child += d; |
502 | 0 | NEXT; |
503 | 0 | } |
504 | 0 | if (overflow) |
505 | 0 | child = 0; |
506 | 0 | xmlXPtrGetChildNo(ctxt, child); |
507 | 0 | } |
508 | 0 | } |
509 | | |
510 | | |
511 | | /** |
512 | | * XPointer ::= Name |
513 | | * | ChildSeq |
514 | | * | FullXPtr |
515 | | * |
516 | | * Parse and evaluate an XPointer |
517 | | * |
518 | | * @param ctxt the XPointer Parser context |
519 | | */ |
520 | | static void |
521 | 0 | xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) { |
522 | 0 | if (ctxt->valueTab == NULL) { |
523 | | /* Allocate the value stack */ |
524 | 0 | ctxt->valueTab = (xmlXPathObjectPtr *) |
525 | 0 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
526 | 0 | if (ctxt->valueTab == NULL) { |
527 | 0 | xmlXPathPErrMemory(ctxt); |
528 | 0 | return; |
529 | 0 | } |
530 | 0 | ctxt->valueNr = 0; |
531 | 0 | ctxt->valueMax = 10; |
532 | 0 | ctxt->value = NULL; |
533 | 0 | } |
534 | 0 | SKIP_BLANKS; |
535 | 0 | if (CUR == '/') { |
536 | 0 | xmlXPathRoot(ctxt); |
537 | 0 | xmlXPtrEvalChildSeq(ctxt, NULL); |
538 | 0 | } else { |
539 | 0 | xmlChar *name; |
540 | |
|
541 | 0 | name = xmlXPathParseName(ctxt); |
542 | 0 | if (name == NULL) |
543 | 0 | XP_ERROR(XPATH_EXPR_ERROR); |
544 | 0 | if (CUR == '(') { |
545 | 0 | xmlXPtrEvalFullXPtr(ctxt, name); |
546 | | /* Short evaluation */ |
547 | 0 | return; |
548 | 0 | } else { |
549 | | /* this handle both Bare Names and Child Sequences */ |
550 | 0 | xmlXPtrEvalChildSeq(ctxt, name); |
551 | 0 | } |
552 | 0 | } |
553 | 0 | SKIP_BLANKS; |
554 | 0 | if (CUR != 0) |
555 | 0 | XP_ERROR(XPATH_EXPR_ERROR); |
556 | 0 | } |
557 | | |
558 | | |
559 | | /************************************************************************ |
560 | | * * |
561 | | * General routines * |
562 | | * * |
563 | | ************************************************************************/ |
564 | | |
565 | | /** |
566 | | * Create a new XPointer context |
567 | | * |
568 | | * @deprecated Same as xmlXPathNewContext. |
569 | | * |
570 | | * @param doc the XML document |
571 | | * @param here unused |
572 | | * @param origin unused |
573 | | * @returns the xmlXPathContext just allocated. |
574 | | */ |
575 | | xmlXPathContext * |
576 | 0 | xmlXPtrNewContext(xmlDoc *doc, xmlNode *here, xmlNode *origin) { |
577 | 0 | xmlXPathContextPtr ret; |
578 | 0 | (void) here; |
579 | 0 | (void) origin; |
580 | |
|
581 | 0 | ret = xmlXPathNewContext(doc); |
582 | 0 | if (ret == NULL) |
583 | 0 | return(ret); |
584 | | |
585 | 0 | return(ret); |
586 | 0 | } |
587 | | |
588 | | /** |
589 | | * Evaluate an XPointer expression. |
590 | | * |
591 | | * This function can only return nodesets. The caller has to |
592 | | * free the object. |
593 | | * |
594 | | * @param str an XPointer expression |
595 | | * @param ctx an XPath context |
596 | | * @returns the xmlXPathObject resulting from the evaluation or NULL |
597 | | * in case of error. |
598 | | */ |
599 | | xmlXPathObject * |
600 | 0 | xmlXPtrEval(const xmlChar *str, xmlXPathContext *ctx) { |
601 | 0 | xmlXPathParserContextPtr ctxt; |
602 | 0 | xmlXPathObjectPtr res = NULL, tmp; |
603 | 0 | xmlXPathObjectPtr init = NULL; |
604 | 0 | int stack = 0; |
605 | |
|
606 | 0 | xmlInitParser(); |
607 | |
|
608 | 0 | if ((ctx == NULL) || (str == NULL)) |
609 | 0 | return(NULL); |
610 | | |
611 | 0 | xmlResetError(&ctx->lastError); |
612 | |
|
613 | 0 | ctxt = xmlXPathNewParserContext(str, ctx); |
614 | 0 | if (ctxt == NULL) { |
615 | 0 | xmlXPathErrMemory(ctx); |
616 | 0 | return(NULL); |
617 | 0 | } |
618 | 0 | xmlXPtrEvalXPointer(ctxt); |
619 | 0 | if (ctx->lastError.code != XML_ERR_OK) |
620 | 0 | goto error; |
621 | | |
622 | 0 | if ((ctxt->value != NULL) && |
623 | 0 | (ctxt->value->type != XPATH_NODESET)) { |
624 | 0 | xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED, |
625 | 0 | "xmlXPtrEval: evaluation failed to return a node set\n", |
626 | 0 | NULL); |
627 | 0 | } else { |
628 | 0 | res = xmlXPathValuePop(ctxt); |
629 | 0 | } |
630 | |
|
631 | 0 | do { |
632 | 0 | tmp = xmlXPathValuePop(ctxt); |
633 | 0 | if (tmp != NULL) { |
634 | 0 | if (tmp != init) { |
635 | 0 | if (tmp->type == XPATH_NODESET) { |
636 | | /* |
637 | | * Evaluation may push a root nodeset which is unused |
638 | | */ |
639 | 0 | xmlNodeSetPtr set; |
640 | 0 | set = tmp->nodesetval; |
641 | 0 | if ((set == NULL) || (set->nodeNr != 1) || |
642 | 0 | (set->nodeTab[0] != (xmlNodePtr) ctx->doc)) |
643 | 0 | stack++; |
644 | 0 | } else |
645 | 0 | stack++; |
646 | 0 | } |
647 | 0 | xmlXPathFreeObject(tmp); |
648 | 0 | } |
649 | 0 | } while (tmp != NULL); |
650 | 0 | if (stack != 0) { |
651 | 0 | xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS, |
652 | 0 | "xmlXPtrEval: object(s) left on the eval stack\n", |
653 | 0 | NULL); |
654 | 0 | } |
655 | 0 | if (ctx->lastError.code != XML_ERR_OK) { |
656 | 0 | xmlXPathFreeObject(res); |
657 | 0 | res = NULL; |
658 | 0 | } |
659 | |
|
660 | 0 | error: |
661 | 0 | xmlXPathFreeParserContext(ctxt); |
662 | 0 | return(res); |
663 | 0 | } |
664 | | |
665 | | #endif |
666 | | |