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