blob: de0982491e0dabc3d7b58cd3c7e6355782a55e8c [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xpointer.c : Code to handle XML Pointer
3 *
Daniel Veillard9a237c92003-02-13 15:52:58 +00004 * Base implementation was made accordingly to
5 * W3C Candidate Recommendation 7 June 2000
Owen Taylor3473f882001-02-23 17:55:21 +00006 * http://www.w3.org/TR/2000/CR-xptr-20000607
7 *
Daniel Veillard9a237c92003-02-13 15:52:58 +00008 * 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 *
Owen Taylor3473f882001-02-23 17:55:21 +000012 * See Copyright for the status of this software.
13 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000014 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000015 */
16
Daniel Veillard34ce8be2002-03-18 19:37:11 +000017#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000018#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000019
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000020/*
Owen Taylor3473f882001-02-23 17:55:21 +000021 * TODO: better handling of error cases, the full expression should
22 * be parsed beforehand instead of a progressive evaluation
23 * TODO: Access into entities references are not supported now ...
24 * need a start to be able to pop out of entities refs since
25 * parent is the endity declaration, not the ref.
26 */
27
Owen Taylor3473f882001-02-23 17:55:21 +000028#include <string.h>
29#include <libxml/xpointer.h>
30#include <libxml/xmlmemory.h>
31#include <libxml/parserInternals.h>
32#include <libxml/uri.h>
33#include <libxml/xpath.h>
34#include <libxml/xpathInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000035#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000036#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000037
38#ifdef LIBXML_XPTR_ENABLED
39
40/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
41#define XPTR_XMLNS_SCHEME
42
43/* #define DEBUG_RANGES */
Daniel Veillard017b1082001-06-21 11:20:21 +000044#ifdef DEBUG_RANGES
45#ifdef LIBXML_DEBUG_ENABLED
46#include <libxml/debugXML.h>
47#endif
48#endif
Owen Taylor3473f882001-02-23 17:55:21 +000049
50#define TODO \
51 xmlGenericError(xmlGenericErrorContext, \
52 "Unimplemented block at %s:%d\n", \
53 __FILE__, __LINE__);
54
55#define STRANGE \
56 xmlGenericError(xmlGenericErrorContext, \
57 "Internal error at %s:%d\n", \
58 __FILE__, __LINE__);
59
60/************************************************************************
61 * *
Daniel Veillardfcf719c2003-10-10 11:42:17 +000062 * Some factorized error routines *
63 * *
64 ************************************************************************/
65
66/**
67 * xmlXPtrErrMemory:
68 * @extra: extra informations
69 *
70 * Handle a redefinition of attribute error
71 */
72static void
73xmlXPtrErrMemory(const char *extra)
74{
Daniel Veillard659e71e2003-10-10 14:10:40 +000075 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_XPOINTER,
Daniel Veillardfcf719c2003-10-10 11:42:17 +000076 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra,
77 NULL, NULL, 0, 0,
78 "Memory allocation failed : %s\n", extra);
79}
80
81/**
82 * xmlXPtrErr:
83 * @ctxt: an XPTR evaluation context
84 * @extra: extra informations
85 *
86 * Handle a redefinition of attribute error
87 */
88static void
89xmlXPtrErr(xmlXPathParserContextPtr ctxt, int error,
90 const char * msg, const xmlChar *extra)
91{
92 if (ctxt != NULL)
93 ctxt->error = error;
94 if ((ctxt == NULL) || (ctxt->context == NULL)) {
Daniel Veillard659e71e2003-10-10 14:10:40 +000095 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardfcf719c2003-10-10 11:42:17 +000096 NULL, NULL, XML_FROM_XPOINTER, error,
97 XML_ERR_ERROR, NULL, 0,
98 (const char *) extra, NULL, NULL, 0, 0,
99 msg, extra);
100 return;
101 }
102 ctxt->context->lastError.domain = XML_FROM_XPOINTER;
103 ctxt->context->lastError.code = error;
104 ctxt->context->lastError.level = XML_ERR_ERROR;
105 ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
106 ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
107 ctxt->context->lastError.node = ctxt->context->debugNode;
108 if (ctxt->context->error != NULL) {
109 ctxt->context->error(ctxt->context->userData,
110 &ctxt->context->lastError);
111 } else {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000112 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000113 NULL, ctxt->context->debugNode, XML_FROM_XPOINTER,
114 error, XML_ERR_ERROR, NULL, 0,
115 (const char *) extra, (const char *) ctxt->base, NULL,
116 ctxt->cur - ctxt->base, 0,
117 msg, extra);
118 }
119}
120
121/************************************************************************
122 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000123 * A few helper functions for child sequences *
124 * *
125 ************************************************************************/
William M. Brackf7eb7942003-12-31 07:59:17 +0000126/* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
127xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
Owen Taylor3473f882001-02-23 17:55:21 +0000128/**
129 * xmlXPtrGetArity:
130 * @cur: the node
131 *
132 * Returns the number of child for an element, -1 in case of error
133 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000134static int
Owen Taylor3473f882001-02-23 17:55:21 +0000135xmlXPtrGetArity(xmlNodePtr cur) {
136 int i;
137 if (cur == NULL)
138 return(-1);
139 cur = cur->children;
140 for (i = 0;cur != NULL;cur = cur->next) {
141 if ((cur->type == XML_ELEMENT_NODE) ||
142 (cur->type == XML_DOCUMENT_NODE) ||
143 (cur->type == XML_HTML_DOCUMENT_NODE)) {
144 i++;
145 }
146 }
147 return(i);
148}
149
150/**
151 * xmlXPtrGetIndex:
152 * @cur: the node
153 *
154 * Returns the index of the node in its parent children list, -1
155 * in case of error
156 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000157static int
Owen Taylor3473f882001-02-23 17:55:21 +0000158xmlXPtrGetIndex(xmlNodePtr cur) {
159 int i;
160 if (cur == NULL)
161 return(-1);
162 for (i = 1;cur != NULL;cur = cur->prev) {
163 if ((cur->type == XML_ELEMENT_NODE) ||
164 (cur->type == XML_DOCUMENT_NODE) ||
165 (cur->type == XML_HTML_DOCUMENT_NODE)) {
166 i++;
167 }
168 }
169 return(i);
170}
171
172/**
173 * xmlXPtrGetNthChild:
174 * @cur: the node
175 * @no: the child number
176 *
177 * Returns the @no'th element child of @cur or NULL
178 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000179static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000180xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
181 int i;
182 if (cur == NULL)
183 return(cur);
184 cur = cur->children;
185 for (i = 0;i <= no;cur = cur->next) {
186 if (cur == NULL)
187 return(cur);
188 if ((cur->type == XML_ELEMENT_NODE) ||
189 (cur->type == XML_DOCUMENT_NODE) ||
190 (cur->type == XML_HTML_DOCUMENT_NODE)) {
191 i++;
192 if (i == no)
193 break;
194 }
195 }
196 return(cur);
197}
198
199/************************************************************************
200 * *
201 * Handling of XPointer specific types *
202 * *
203 ************************************************************************/
204
205/**
206 * xmlXPtrCmpPoints:
207 * @node1: the first node
208 * @index1: the first index
209 * @node2: the second node
210 * @index2: the second index
211 *
212 * Compare two points w.r.t document order
213 *
214 * Returns -2 in case of error 1 if first point < second point, 0 if
215 * that's the same point, -1 otherwise
216 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000217static int
Owen Taylor3473f882001-02-23 17:55:21 +0000218xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
219 if ((node1 == NULL) || (node2 == NULL))
220 return(-2);
221 /*
222 * a couple of optimizations which will avoid computations in most cases
223 */
224 if (node1 == node2) {
225 if (index1 < index2)
226 return(1);
227 if (index1 > index2)
228 return(-1);
229 return(0);
230 }
231 return(xmlXPathCmpNodes(node1, node2));
232}
233
234/**
235 * xmlXPtrNewPoint:
236 * @node: the xmlNodePtr
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000237 * @indx: the indx within the node
Owen Taylor3473f882001-02-23 17:55:21 +0000238 *
239 * Create a new xmlXPathObjectPtr of type point
240 *
241 * Returns the newly created object.
242 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000243static xmlXPathObjectPtr
244xmlXPtrNewPoint(xmlNodePtr node, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000245 xmlXPathObjectPtr ret;
246
247 if (node == NULL)
248 return(NULL);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000249 if (indx < 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000250 return(NULL);
251
252 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
253 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000254 xmlXPtrErrMemory("allocating point");
Owen Taylor3473f882001-02-23 17:55:21 +0000255 return(NULL);
256 }
257 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
258 ret->type = XPATH_POINT;
259 ret->user = (void *) node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000260 ret->index = indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000261 return(ret);
262}
263
264/**
265 * xmlXPtrRangeCheckOrder:
266 * @range: an object range
267 *
268 * Make sure the points in the range are in the right order
269 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000270static void
Owen Taylor3473f882001-02-23 17:55:21 +0000271xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
272 int tmp;
273 xmlNodePtr tmp2;
274 if (range == NULL)
275 return;
276 if (range->type != XPATH_RANGE)
277 return;
278 if (range->user2 == NULL)
279 return;
280 tmp = xmlXPtrCmpPoints(range->user, range->index,
281 range->user2, range->index2);
282 if (tmp == -1) {
283 tmp2 = range->user;
284 range->user = range->user2;
285 range->user2 = tmp2;
286 tmp = range->index;
287 range->index = range->index2;
288 range->index2 = tmp;
289 }
290}
291
292/**
293 * xmlXPtrRangesEqual:
294 * @range1: the first range
295 * @range2: the second range
296 *
297 * Compare two ranges
298 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000299 * Returns 1 if equal, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000300 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000301static int
Owen Taylor3473f882001-02-23 17:55:21 +0000302xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
303 if (range1 == range2)
304 return(1);
305 if ((range1 == NULL) || (range2 == NULL))
306 return(0);
307 if (range1->type != range2->type)
308 return(0);
309 if (range1->type != XPATH_RANGE)
310 return(0);
311 if (range1->user != range2->user)
312 return(0);
313 if (range1->index != range2->index)
314 return(0);
315 if (range1->user2 != range2->user2)
316 return(0);
317 if (range1->index2 != range2->index2)
318 return(0);
319 return(1);
320}
321
322/**
323 * xmlXPtrNewRange:
324 * @start: the starting node
325 * @startindex: the start index
326 * @end: the ending point
327 * @endindex: the ending index
328 *
329 * Create a new xmlXPathObjectPtr of type range
330 *
331 * Returns the newly created object.
332 */
333xmlXPathObjectPtr
334xmlXPtrNewRange(xmlNodePtr start, int startindex,
335 xmlNodePtr end, int endindex) {
336 xmlXPathObjectPtr ret;
337
338 if (start == NULL)
339 return(NULL);
340 if (end == NULL)
341 return(NULL);
342 if (startindex < 0)
343 return(NULL);
344 if (endindex < 0)
345 return(NULL);
346
347 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
348 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000349 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000350 return(NULL);
351 }
352 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
353 ret->type = XPATH_RANGE;
354 ret->user = start;
355 ret->index = startindex;
356 ret->user2 = end;
357 ret->index2 = endindex;
358 xmlXPtrRangeCheckOrder(ret);
359 return(ret);
360}
361
362/**
363 * xmlXPtrNewRangePoints:
364 * @start: the starting point
365 * @end: the ending point
366 *
367 * Create a new xmlXPathObjectPtr of type range using 2 Points
368 *
369 * Returns the newly created object.
370 */
371xmlXPathObjectPtr
372xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
373 xmlXPathObjectPtr ret;
374
375 if (start == NULL)
376 return(NULL);
377 if (end == NULL)
378 return(NULL);
379 if (start->type != XPATH_POINT)
380 return(NULL);
381 if (end->type != XPATH_POINT)
382 return(NULL);
383
384 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
385 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000386 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000387 return(NULL);
388 }
389 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
390 ret->type = XPATH_RANGE;
391 ret->user = start->user;
392 ret->index = start->index;
393 ret->user2 = end->user;
394 ret->index2 = end->index;
395 xmlXPtrRangeCheckOrder(ret);
396 return(ret);
397}
398
399/**
400 * xmlXPtrNewRangePointNode:
401 * @start: the starting point
402 * @end: the ending node
403 *
404 * Create a new xmlXPathObjectPtr of type range from a point to a node
405 *
406 * Returns the newly created object.
407 */
408xmlXPathObjectPtr
409xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
410 xmlXPathObjectPtr ret;
411
412 if (start == NULL)
413 return(NULL);
414 if (end == NULL)
415 return(NULL);
416 if (start->type != XPATH_POINT)
417 return(NULL);
418
419 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
420 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000421 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000422 return(NULL);
423 }
424 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
425 ret->type = XPATH_RANGE;
426 ret->user = start->user;
427 ret->index = start->index;
428 ret->user2 = end;
429 ret->index2 = -1;
430 xmlXPtrRangeCheckOrder(ret);
431 return(ret);
432}
433
434/**
435 * xmlXPtrNewRangeNodePoint:
436 * @start: the starting node
437 * @end: the ending point
438 *
439 * Create a new xmlXPathObjectPtr of type range from a node to a point
440 *
441 * Returns the newly created object.
442 */
443xmlXPathObjectPtr
444xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
445 xmlXPathObjectPtr ret;
446
447 if (start == NULL)
448 return(NULL);
449 if (end == NULL)
450 return(NULL);
451 if (start->type != XPATH_POINT)
452 return(NULL);
453 if (end->type != XPATH_POINT)
454 return(NULL);
455
456 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
457 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000458 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000459 return(NULL);
460 }
461 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
462 ret->type = XPATH_RANGE;
463 ret->user = start;
464 ret->index = -1;
465 ret->user2 = end->user;
466 ret->index2 = end->index;
467 xmlXPtrRangeCheckOrder(ret);
468 return(ret);
469}
470
471/**
472 * xmlXPtrNewRangeNodes:
473 * @start: the starting node
474 * @end: the ending node
475 *
476 * Create a new xmlXPathObjectPtr of type range using 2 nodes
477 *
478 * Returns the newly created object.
479 */
480xmlXPathObjectPtr
481xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
482 xmlXPathObjectPtr ret;
483
484 if (start == NULL)
485 return(NULL);
486 if (end == NULL)
487 return(NULL);
488
489 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
490 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000491 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000492 return(NULL);
493 }
494 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
495 ret->type = XPATH_RANGE;
496 ret->user = start;
497 ret->index = -1;
498 ret->user2 = end;
499 ret->index2 = -1;
500 xmlXPtrRangeCheckOrder(ret);
501 return(ret);
502}
503
504/**
505 * xmlXPtrNewCollapsedRange:
506 * @start: the starting and ending node
507 *
508 * Create a new xmlXPathObjectPtr of type range using a single nodes
509 *
510 * Returns the newly created object.
511 */
512xmlXPathObjectPtr
513xmlXPtrNewCollapsedRange(xmlNodePtr start) {
514 xmlXPathObjectPtr ret;
515
516 if (start == NULL)
517 return(NULL);
518
519 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
520 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000521 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000522 return(NULL);
523 }
524 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
525 ret->type = XPATH_RANGE;
526 ret->user = start;
527 ret->index = -1;
528 ret->user2 = NULL;
529 ret->index2 = -1;
530 return(ret);
531}
532
533/**
534 * xmlXPtrNewRangeNodeObject:
535 * @start: the starting node
536 * @end: the ending object
537 *
538 * Create a new xmlXPathObjectPtr of type range from a not to an object
539 *
540 * Returns the newly created object.
541 */
542xmlXPathObjectPtr
543xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
544 xmlXPathObjectPtr ret;
545
546 if (start == NULL)
547 return(NULL);
548 if (end == NULL)
549 return(NULL);
550 switch (end->type) {
551 case XPATH_POINT:
William M. Brack72ee48d2003-12-30 08:30:19 +0000552 case XPATH_RANGE:
Owen Taylor3473f882001-02-23 17:55:21 +0000553 break;
554 case XPATH_NODESET:
555 /*
556 * Empty set ...
557 */
558 if (end->nodesetval->nodeNr <= 0)
559 return(NULL);
560 break;
561 default:
562 TODO
563 return(NULL);
564 }
565
566 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
567 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000568 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000569 return(NULL);
570 }
571 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
572 ret->type = XPATH_RANGE;
573 ret->user = start;
574 ret->index = -1;
575 switch (end->type) {
576 case XPATH_POINT:
577 ret->user2 = end->user;
578 ret->index2 = end->index;
William M. Brack72ee48d2003-12-30 08:30:19 +0000579 break;
580 case XPATH_RANGE:
581 ret->user2 = end->user2;
582 ret->index2 = end->index2;
583 break;
Owen Taylor3473f882001-02-23 17:55:21 +0000584 case XPATH_NODESET: {
585 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
586 ret->index2 = -1;
587 break;
588 }
589 default:
590 STRANGE
591 return(NULL);
592 }
593 xmlXPtrRangeCheckOrder(ret);
594 return(ret);
595}
596
597#define XML_RANGESET_DEFAULT 10
598
599/**
600 * xmlXPtrLocationSetCreate:
601 * @val: an initial xmlXPathObjectPtr, or NULL
602 *
603 * Create a new xmlLocationSetPtr of type double and of value @val
604 *
605 * Returns the newly created object.
606 */
607xmlLocationSetPtr
608xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
609 xmlLocationSetPtr ret;
610
611 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
612 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000613 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000614 return(NULL);
615 }
616 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
617 if (val != NULL) {
618 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
619 sizeof(xmlXPathObjectPtr));
620 if (ret->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000621 xmlXPtrErrMemory("allocating locationset");
622 xmlFree(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000623 return(NULL);
624 }
625 memset(ret->locTab, 0 ,
626 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
627 ret->locMax = XML_RANGESET_DEFAULT;
628 ret->locTab[ret->locNr++] = val;
629 }
630 return(ret);
631}
632
633/**
634 * xmlXPtrLocationSetAdd:
635 * @cur: the initial range set
636 * @val: a new xmlXPathObjectPtr
637 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000638 * add a new xmlXPathObjectPtr to an existing LocationSet
Owen Taylor3473f882001-02-23 17:55:21 +0000639 * If the location already exist in the set @val is freed.
640 */
641void
642xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
643 int i;
644
645 if (val == NULL) return;
646
647 /*
648 * check against doublons
649 */
650 for (i = 0;i < cur->locNr;i++) {
651 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
652 xmlXPathFreeObject(val);
653 return;
654 }
655 }
656
657 /*
658 * grow the locTab if needed
659 */
660 if (cur->locMax == 0) {
661 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
662 sizeof(xmlXPathObjectPtr));
663 if (cur->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000664 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000665 return;
666 }
667 memset(cur->locTab, 0 ,
668 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
669 cur->locMax = XML_RANGESET_DEFAULT;
670 } else if (cur->locNr == cur->locMax) {
671 xmlXPathObjectPtr *temp;
672
673 cur->locMax *= 2;
674 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
675 sizeof(xmlXPathObjectPtr));
676 if (temp == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000677 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000678 return;
679 }
680 cur->locTab = temp;
681 }
682 cur->locTab[cur->locNr++] = val;
683}
684
685/**
686 * xmlXPtrLocationSetMerge:
687 * @val1: the first LocationSet
688 * @val2: the second LocationSet
689 *
690 * Merges two rangesets, all ranges from @val2 are added to @val1
691 *
692 * Returns val1 once extended or NULL in case of error.
693 */
694xmlLocationSetPtr
695xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
696 int i;
697
698 if (val1 == NULL) return(NULL);
699 if (val2 == NULL) return(val1);
700
701 /*
702 * !!!!! this can be optimized a lot, knowing that both
703 * val1 and val2 already have unicity of their values.
704 */
705
706 for (i = 0;i < val2->locNr;i++)
707 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
708
709 return(val1);
710}
711
712/**
713 * xmlXPtrLocationSetDel:
714 * @cur: the initial range set
715 * @val: an xmlXPathObjectPtr
716 *
717 * Removes an xmlXPathObjectPtr from an existing LocationSet
718 */
719void
720xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
721 int i;
722
723 if (cur == NULL) return;
724 if (val == NULL) return;
725
726 /*
727 * check against doublons
728 */
729 for (i = 0;i < cur->locNr;i++)
730 if (cur->locTab[i] == val) break;
731
732 if (i >= cur->locNr) {
733#ifdef DEBUG
734 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard913d6e02001-11-28 14:53:53 +0000735 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000736#endif
737 return;
738 }
739 cur->locNr--;
740 for (;i < cur->locNr;i++)
741 cur->locTab[i] = cur->locTab[i + 1];
742 cur->locTab[cur->locNr] = NULL;
743}
744
745/**
746 * xmlXPtrLocationSetRemove:
747 * @cur: the initial range set
748 * @val: the index to remove
749 *
750 * Removes an entry from an existing LocationSet list.
751 */
752void
753xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
754 if (cur == NULL) return;
755 if (val >= cur->locNr) return;
756 cur->locNr--;
757 for (;val < cur->locNr;val++)
758 cur->locTab[val] = cur->locTab[val + 1];
759 cur->locTab[cur->locNr] = NULL;
760}
761
762/**
763 * xmlXPtrFreeLocationSet:
764 * @obj: the xmlLocationSetPtr to free
765 *
766 * Free the LocationSet compound (not the actual ranges !).
767 */
768void
769xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
770 int i;
771
772 if (obj == NULL) return;
773 if (obj->locTab != NULL) {
774 for (i = 0;i < obj->locNr; i++) {
775 xmlXPathFreeObject(obj->locTab[i]);
776 }
Owen Taylor3473f882001-02-23 17:55:21 +0000777 xmlFree(obj->locTab);
778 }
Owen Taylor3473f882001-02-23 17:55:21 +0000779 xmlFree(obj);
780}
781
782/**
783 * xmlXPtrNewLocationSetNodes:
784 * @start: the start NodePtr value
785 * @end: the end NodePtr value or NULL
786 *
787 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
788 * it with the single range made of the two nodes @start and @end
789 *
790 * Returns the newly created object.
791 */
792xmlXPathObjectPtr
793xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
794 xmlXPathObjectPtr ret;
795
796 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
797 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000798 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000799 return(NULL);
800 }
801 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
802 ret->type = XPATH_LOCATIONSET;
803 if (end == NULL)
804 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
805 else
806 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
807 return(ret);
808}
809
810/**
811 * xmlXPtrNewLocationSetNodeSet:
812 * @set: a node set
813 *
814 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
815 * it with all the nodes from @set
816 *
817 * Returns the newly created object.
818 */
819xmlXPathObjectPtr
820xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
821 xmlXPathObjectPtr ret;
822
823 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
824 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000825 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000826 return(NULL);
827 }
828 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
829 ret->type = XPATH_LOCATIONSET;
830 if (set != NULL) {
831 int i;
832 xmlLocationSetPtr newset;
833
834 newset = xmlXPtrLocationSetCreate(NULL);
835 if (newset == NULL)
836 return(ret);
837
838 for (i = 0;i < set->nodeNr;i++)
839 xmlXPtrLocationSetAdd(newset,
840 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
841
842 ret->user = (void *) newset;
843 }
844 return(ret);
845}
846
847/**
848 * xmlXPtrWrapLocationSet:
849 * @val: the LocationSet value
850 *
851 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
852 *
853 * Returns the newly created object.
854 */
855xmlXPathObjectPtr
856xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
857 xmlXPathObjectPtr ret;
858
859 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
860 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000861 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000862 return(NULL);
863 }
864 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
865 ret->type = XPATH_LOCATIONSET;
866 ret->user = (void *) val;
867 return(ret);
868}
869
870/************************************************************************
871 * *
872 * The parser *
873 * *
874 ************************************************************************/
875
Daniel Veillard9a237c92003-02-13 15:52:58 +0000876static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
877
Owen Taylor3473f882001-02-23 17:55:21 +0000878/*
879 * Macros for accessing the content. Those should be used only by the parser,
880 * and not exported.
881 *
882 * Dirty macros, i.e. one need to make assumption on the context to use them
883 *
884 * CUR_PTR return the current pointer to the xmlChar to be parsed.
885 * CUR returns the current xmlChar value, i.e. a 8 bit value
886 * in ISO-Latin or UTF-8.
887 * This should be used internally by the parser
888 * only to compare to ASCII values otherwise it would break when
889 * running with UTF-8 encoding.
890 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
891 * to compare on ASCII based substring.
892 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
893 * strings within the parser.
894 * CURRENT Returns the current char value, with the full decoding of
895 * UTF-8 if we are using this mode. It returns an int.
896 * NEXT Skip to the next character, this does the proper decoding
897 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
898 * It returns the pointer to the current xmlChar.
899 */
900
901#define CUR (*ctxt->cur)
902#define SKIP(val) ctxt->cur += (val)
903#define NXT(val) ctxt->cur[(val)]
904#define CUR_PTR ctxt->cur
905
906#define SKIP_BLANKS \
William M. Brack272693c2003-11-14 16:20:34 +0000907 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Owen Taylor3473f882001-02-23 17:55:21 +0000908
909#define CURRENT (*ctxt->cur)
910#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
911
912/*
913 * xmlXPtrGetChildNo:
914 * @ctxt: the XPointer Parser context
915 * @index: the child number
916 *
917 * Move the current node of the nodeset on the stack to the
918 * given child if found
919 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000920static void
921xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000922 xmlNodePtr cur = NULL;
923 xmlXPathObjectPtr obj;
924 xmlNodeSetPtr oldset;
925
926 CHECK_TYPE(XPATH_NODESET);
927 obj = valuePop(ctxt);
928 oldset = obj->nodesetval;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000929 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000930 xmlXPathFreeObject(obj);
931 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
932 return;
933 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000934 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
Owen Taylor3473f882001-02-23 17:55:21 +0000935 if (cur == NULL) {
936 xmlXPathFreeObject(obj);
937 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
938 return;
939 }
940 oldset->nodeTab[0] = cur;
941 valuePush(ctxt, obj);
942}
943
944/**
945 * xmlXPtrEvalXPtrPart:
946 * @ctxt: the XPointer Parser context
947 * @name: the preparsed Scheme for the XPtrPart
948 *
949 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
950 * | Scheme '(' SchemeSpecificExpr ')'
951 *
952 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
953 *
954 * SchemeSpecificExpr ::= StringWithBalancedParens
955 *
956 * StringWithBalancedParens ::=
957 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
958 * [VC: Parenthesis escaping]
959 *
960 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
961 *
962 * VC: Parenthesis escaping:
963 * The end of an XPointer part is signaled by the right parenthesis ")"
964 * character that is balanced with the left parenthesis "(" character
965 * that began the part. Any unbalanced parenthesis character inside the
966 * expression, even within literals, must be escaped with a circumflex (^)
967 * character preceding it. If the expression contains any literal
968 * occurrences of the circumflex, each must be escaped with an additional
969 * circumflex (that is, ^^). If the unescaped parentheses in the expression
970 * are not balanced, a syntax error results.
971 *
972 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000973 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
Owen Taylor3473f882001-02-23 17:55:21 +0000974 *
975 * TODO: there is no new scheme registration mechanism
976 */
977
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000978static void
Owen Taylor3473f882001-02-23 17:55:21 +0000979xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
980 xmlChar *buffer, *cur;
981 int len;
982 int level;
983
984 if (name == NULL)
985 name = xmlXPathParseName(ctxt);
986 if (name == NULL)
987 XP_ERROR(XPATH_EXPR_ERROR);
988
989 if (CUR != '(')
990 XP_ERROR(XPATH_EXPR_ERROR);
991 NEXT;
992 level = 1;
993
994 len = xmlStrlen(ctxt->cur);
995 len++;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000996 buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +0000997 if (buffer == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000998 xmlXPtrErrMemory("allocating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +0000999 return;
1000 }
1001
1002 cur = buffer;
1003 while (CUR != 0) {
1004 if (CUR == ')') {
1005 level--;
1006 if (level == 0) {
1007 NEXT;
1008 break;
1009 }
1010 *cur++ = CUR;
1011 } else if (CUR == '(') {
1012 level++;
1013 *cur++ = CUR;
1014 } else if (CUR == '^') {
1015 NEXT;
1016 if ((CUR == ')') || (CUR == '(') || (CUR == '^')) {
1017 *cur++ = CUR;
1018 } else {
1019 *cur++ = '^';
1020 *cur++ = CUR;
1021 }
1022 } else {
1023 *cur++ = CUR;
1024 }
1025 NEXT;
1026 }
1027 *cur = 0;
1028
1029 if ((level != 0) && (CUR == 0)) {
1030 xmlFree(buffer);
1031 XP_ERROR(XPTR_SYNTAX_ERROR);
1032 }
1033
1034 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
1035 const xmlChar *left = CUR_PTR;
1036
1037 CUR_PTR = buffer;
William M. Brack72ee48d2003-12-30 08:30:19 +00001038 /*
1039 * To evaluate an xpointer scheme element (4.3) we need:
1040 * context initialized to the root
1041 * context position initalized to 1
1042 * context size initialized to 1
1043 */
1044 ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
1045 ctxt->context->proximityPosition = 1;
1046 ctxt->context->contextSize = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001047 xmlXPathEvalExpr(ctxt);
1048 CUR_PTR=left;
Daniel Veillard9a237c92003-02-13 15:52:58 +00001049 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
1050 const xmlChar *left = CUR_PTR;
1051 xmlChar *name2;
1052
1053 CUR_PTR = buffer;
1054 if (buffer[0] == '/') {
1055 xmlXPathRoot(ctxt);
1056 xmlXPtrEvalChildSeq(ctxt, NULL);
1057 } else {
1058 name2 = xmlXPathParseName(ctxt);
1059 if (name2 == NULL) {
1060 CUR_PTR = left;
1061 xmlFree(buffer);
1062 XP_ERROR(XPATH_EXPR_ERROR);
1063 }
1064 xmlXPtrEvalChildSeq(ctxt, name2);
1065 }
1066 CUR_PTR = left;
Owen Taylor3473f882001-02-23 17:55:21 +00001067#ifdef XPTR_XMLNS_SCHEME
1068 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1069 const xmlChar *left = CUR_PTR;
1070 xmlChar *prefix;
1071 xmlChar *URI;
1072 xmlURIPtr value;
1073
1074 CUR_PTR = buffer;
1075 prefix = xmlXPathParseNCName(ctxt);
1076 if (prefix == NULL) {
1077 xmlFree(buffer);
1078 xmlFree(name);
1079 XP_ERROR(XPTR_SYNTAX_ERROR);
1080 }
1081 SKIP_BLANKS;
1082 if (CUR != '=') {
1083 xmlFree(prefix);
1084 xmlFree(buffer);
1085 xmlFree(name);
1086 XP_ERROR(XPTR_SYNTAX_ERROR);
1087 }
1088 NEXT;
1089 SKIP_BLANKS;
1090 /* @@ check escaping in the XPointer WD */
1091
1092 value = xmlParseURI((const char *)ctxt->cur);
1093 if (value == NULL) {
1094 xmlFree(prefix);
1095 xmlFree(buffer);
1096 xmlFree(name);
1097 XP_ERROR(XPTR_SYNTAX_ERROR);
1098 }
1099 URI = xmlSaveUri(value);
1100 xmlFreeURI(value);
1101 if (URI == NULL) {
1102 xmlFree(prefix);
1103 xmlFree(buffer);
1104 xmlFree(name);
1105 XP_ERROR(XPATH_MEMORY_ERROR);
1106 }
1107
1108 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1109 CUR_PTR = left;
Daniel Veillard56f21f22002-11-06 15:49:46 +00001110 xmlFree(URI);
1111 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001112#endif /* XPTR_XMLNS_SCHEME */
1113 } else {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001114 xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
1115 "unsupported scheme '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001116 }
1117 xmlFree(buffer);
1118 xmlFree(name);
1119}
1120
1121/**
1122 * xmlXPtrEvalFullXPtr:
1123 * @ctxt: the XPointer Parser context
1124 * @name: the preparsed Scheme for the first XPtrPart
1125 *
1126 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1127 *
1128 * As the specs says:
1129 * -----------
1130 * When multiple XPtrParts are provided, they must be evaluated in
1131 * left-to-right order. If evaluation of one part fails, the nexti
1132 * is evaluated. The following conditions cause XPointer part failure:
1133 *
1134 * - An unknown scheme
1135 * - A scheme that does not locate any sub-resource present in the resource
1136 * - A scheme that is not applicable to the media type of the resource
1137 *
1138 * The XPointer application must consume a failed XPointer part and
1139 * attempt to evaluate the next one, if any. The result of the first
1140 * XPointer part whose evaluation succeeds is taken to be the fragment
1141 * located by the XPointer as a whole. If all the parts fail, the result
1142 * for the XPointer as a whole is a sub-resource error.
1143 * -----------
1144 *
1145 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001146 * expressions or other schemes.
Owen Taylor3473f882001-02-23 17:55:21 +00001147 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001148static void
Owen Taylor3473f882001-02-23 17:55:21 +00001149xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1150 if (name == NULL)
1151 name = xmlXPathParseName(ctxt);
1152 if (name == NULL)
1153 XP_ERROR(XPATH_EXPR_ERROR);
1154 while (name != NULL) {
1155 xmlXPtrEvalXPtrPart(ctxt, name);
1156
1157 /* in case of syntax error, break here */
1158 if (ctxt->error != XPATH_EXPRESSION_OK)
1159 return;
1160
1161 /*
1162 * If the returned value is a non-empty nodeset
1163 * or location set, return here.
1164 */
1165 if (ctxt->value != NULL) {
1166 xmlXPathObjectPtr obj = ctxt->value;
1167
1168 switch (obj->type) {
1169 case XPATH_LOCATIONSET: {
1170 xmlLocationSetPtr loc = ctxt->value->user;
1171 if ((loc != NULL) && (loc->locNr > 0))
1172 return;
1173 break;
1174 }
1175 case XPATH_NODESET: {
1176 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1177 if ((loc != NULL) && (loc->nodeNr > 0))
1178 return;
1179 break;
1180 }
1181 default:
1182 break;
1183 }
1184
1185 /*
1186 * Evaluating to improper values is equivalent to
1187 * a sub-resource error, clean-up the stack
1188 */
1189 do {
1190 obj = valuePop(ctxt);
1191 if (obj != NULL) {
1192 xmlXPathFreeObject(obj);
1193 }
1194 } while (obj != NULL);
1195 }
1196
1197 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001198 * Is there another XPointer part.
Owen Taylor3473f882001-02-23 17:55:21 +00001199 */
1200 SKIP_BLANKS;
1201 name = xmlXPathParseName(ctxt);
1202 }
1203}
1204
1205/**
1206 * xmlXPtrEvalChildSeq:
1207 * @ctxt: the XPointer Parser context
1208 * @name: a possible ID name of the child sequence
1209 *
1210 * ChildSeq ::= '/1' ('/' [0-9]*)*
1211 * | Name ('/' [0-9]*)+
1212 *
1213 * Parse and evaluate a Child Sequence. This routine also handle the
1214 * case of a Bare Name used to get a document ID.
1215 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001216static void
Owen Taylor3473f882001-02-23 17:55:21 +00001217xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1218 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001219 * XPointer don't allow by syntax to address in mutirooted trees
Owen Taylor3473f882001-02-23 17:55:21 +00001220 * this might prove useful in some cases, warn about it.
1221 */
1222 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001223 xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
1224 "warning: ChildSeq not starting by /1\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001225 }
1226
1227 if (name != NULL) {
1228 valuePush(ctxt, xmlXPathNewString(name));
1229 xmlFree(name);
1230 xmlXPathIdFunction(ctxt, 1);
1231 CHECK_ERROR;
1232 }
1233
1234 while (CUR == '/') {
1235 int child = 0;
1236 NEXT;
1237
1238 while ((CUR >= '0') && (CUR <= '9')) {
1239 child = child * 10 + (CUR - '0');
1240 NEXT;
1241 }
1242 xmlXPtrGetChildNo(ctxt, child);
1243 }
1244}
1245
1246
1247/**
1248 * xmlXPtrEvalXPointer:
1249 * @ctxt: the XPointer Parser context
1250 *
1251 * XPointer ::= Name
1252 * | ChildSeq
1253 * | FullXPtr
1254 *
1255 * Parse and evaluate an XPointer
1256 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001257static void
Owen Taylor3473f882001-02-23 17:55:21 +00001258xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001259 if (ctxt->valueTab == NULL) {
1260 /* Allocate the value stack */
1261 ctxt->valueTab = (xmlXPathObjectPtr *)
1262 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1263 if (ctxt->valueTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001264 xmlXPtrErrMemory("allocating evaluation context");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001265 return;
1266 }
1267 ctxt->valueNr = 0;
1268 ctxt->valueMax = 10;
1269 ctxt->value = NULL;
1270 }
Owen Taylor3473f882001-02-23 17:55:21 +00001271 SKIP_BLANKS;
1272 if (CUR == '/') {
1273 xmlXPathRoot(ctxt);
1274 xmlXPtrEvalChildSeq(ctxt, NULL);
1275 } else {
1276 xmlChar *name;
1277
1278 name = xmlXPathParseName(ctxt);
1279 if (name == NULL)
1280 XP_ERROR(XPATH_EXPR_ERROR);
1281 if (CUR == '(') {
1282 xmlXPtrEvalFullXPtr(ctxt, name);
1283 /* Short evaluation */
1284 return;
1285 } else {
1286 /* this handle both Bare Names and Child Sequences */
1287 xmlXPtrEvalChildSeq(ctxt, name);
1288 }
1289 }
1290 SKIP_BLANKS;
1291 if (CUR != 0)
1292 XP_ERROR(XPATH_EXPR_ERROR);
1293}
1294
1295
1296/************************************************************************
1297 * *
1298 * General routines *
1299 * *
1300 ************************************************************************/
1301
Owen Taylor3473f882001-02-23 17:55:21 +00001302void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1303void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1304void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1305void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1306void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1307void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1308void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1309
1310/**
1311 * xmlXPtrNewContext:
1312 * @doc: the XML document
1313 * @here: the node that directly contains the XPointer being evaluated or NULL
1314 * @origin: the element from which a user or program initiated traversal of
1315 * the link, or NULL.
1316 *
1317 * Create a new XPointer context
1318 *
1319 * Returns the xmlXPathContext just allocated.
1320 */
1321xmlXPathContextPtr
1322xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1323 xmlXPathContextPtr ret;
1324
1325 ret = xmlXPathNewContext(doc);
1326 if (ret == NULL)
1327 return(ret);
1328 ret->xptr = 1;
1329 ret->here = here;
1330 ret->origin = origin;
1331
1332 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1333 xmlXPtrRangeToFunction);
1334 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1335 xmlXPtrRangeFunction);
1336 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1337 xmlXPtrRangeInsideFunction);
1338 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1339 xmlXPtrStringRangeFunction);
1340 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1341 xmlXPtrStartPointFunction);
1342 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1343 xmlXPtrEndPointFunction);
1344 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1345 xmlXPtrHereFunction);
1346 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1347 xmlXPtrOriginFunction);
1348
1349 return(ret);
1350}
1351
1352/**
1353 * xmlXPtrEval:
1354 * @str: the XPointer expression
1355 * @ctx: the XPointer context
1356 *
1357 * Evaluate the XPath Location Path in the given context.
1358 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001359 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +00001360 * the caller has to free the object.
1361 */
1362xmlXPathObjectPtr
1363xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1364 xmlXPathParserContextPtr ctxt;
1365 xmlXPathObjectPtr res = NULL, tmp;
1366 xmlXPathObjectPtr init = NULL;
1367 int stack = 0;
1368
1369 xmlXPathInit();
1370
1371 if ((ctx == NULL) || (str == NULL))
1372 return(NULL);
1373
1374 ctxt = xmlXPathNewParserContext(str, ctx);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001375 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001376 xmlXPtrEvalXPointer(ctxt);
1377
1378 if ((ctxt->value != NULL) &&
1379 (ctxt->value->type != XPATH_NODESET) &&
1380 (ctxt->value->type != XPATH_LOCATIONSET)) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001381 xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
1382 "xmlXPtrEval: evaluation failed to return a node set\n",
1383 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001384 } else {
1385 res = valuePop(ctxt);
1386 }
1387
1388 do {
1389 tmp = valuePop(ctxt);
1390 if (tmp != NULL) {
1391 if (tmp != init) {
1392 if (tmp->type == XPATH_NODESET) {
1393 /*
1394 * Evaluation may push a root nodeset which is unused
1395 */
1396 xmlNodeSetPtr set;
1397 set = tmp->nodesetval;
1398 if ((set->nodeNr != 1) ||
1399 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1400 stack++;
1401 } else
1402 stack++;
1403 }
1404 xmlXPathFreeObject(tmp);
1405 }
1406 } while (tmp != NULL);
1407 if (stack != 0) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001408 xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
1409 "xmlXPtrEval: object(s) left on the eval stack\n",
1410 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001411 }
1412 if (ctxt->error != XPATH_EXPRESSION_OK) {
1413 xmlXPathFreeObject(res);
1414 res = NULL;
1415 }
1416
1417 xmlXPathFreeParserContext(ctxt);
1418 return(res);
1419}
1420
1421/**
1422 * xmlXPtrBuildRangeNodeList:
1423 * @range: a range object
1424 *
1425 * Build a node list tree copy of the range
1426 *
1427 * Returns an xmlNodePtr list or NULL.
1428 * the caller has to free the node tree.
1429 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001430static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001431xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1432 /* pointers to generated nodes */
1433 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1434 /* pointers to traversal nodes */
1435 xmlNodePtr start, cur, end;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001436 int index1, index2;
Owen Taylor3473f882001-02-23 17:55:21 +00001437
1438 if (range == NULL)
1439 return(NULL);
1440 if (range->type != XPATH_RANGE)
1441 return(NULL);
1442 start = (xmlNodePtr) range->user;
1443
1444 if (start == NULL)
1445 return(NULL);
1446 end = range->user2;
1447 if (end == NULL)
1448 return(xmlCopyNode(start, 1));
1449
1450 cur = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001451 index1 = range->index;
Owen Taylor3473f882001-02-23 17:55:21 +00001452 index2 = range->index2;
1453 while (cur != NULL) {
1454 if (cur == end) {
1455 if (cur->type == XML_TEXT_NODE) {
1456 const xmlChar *content = cur->content;
1457 int len;
1458
1459 if (content == NULL) {
1460 tmp = xmlNewTextLen(NULL, 0);
1461 } else {
1462 len = index2;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001463 if ((cur == start) && (index1 > 1)) {
1464 content += (index1 - 1);
1465 len -= (index1 - 1);
1466 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001467 } else {
1468 len = index2;
1469 }
1470 tmp = xmlNewTextLen(content, len);
1471 }
1472 /* single sub text node selection */
1473 if (list == NULL)
1474 return(tmp);
1475 /* prune and return full set */
1476 if (last != NULL)
1477 xmlAddNextSibling(last, tmp);
1478 else
1479 xmlAddChild(parent, tmp);
1480 return(list);
1481 } else {
1482 tmp = xmlCopyNode(cur, 0);
1483 if (list == NULL)
1484 list = tmp;
1485 else {
1486 if (last != NULL)
1487 xmlAddNextSibling(last, tmp);
1488 else
1489 xmlAddChild(parent, tmp);
1490 }
1491 last = NULL;
1492 parent = tmp;
1493
1494 if (index2 > 1) {
1495 end = xmlXPtrGetNthChild(cur, index2 - 1);
1496 index2 = 0;
1497 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001498 if ((cur == start) && (index1 > 1)) {
1499 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1500 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001501 } else {
1502 cur = cur->children;
1503 }
1504 /*
1505 * Now gather the remaining nodes from cur to end
1506 */
1507 continue; /* while */
1508 }
1509 } else if ((cur == start) &&
1510 (list == NULL) /* looks superfluous but ... */ ) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001511 if ((cur->type == XML_TEXT_NODE) ||
1512 (cur->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001513 const xmlChar *content = cur->content;
1514
1515 if (content == NULL) {
1516 tmp = xmlNewTextLen(NULL, 0);
1517 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001518 if (index1 > 1) {
1519 content += (index1 - 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001520 }
1521 tmp = xmlNewText(content);
1522 }
1523 last = list = tmp;
1524 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001525 if ((cur == start) && (index1 > 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001526 tmp = xmlCopyNode(cur, 0);
1527 list = tmp;
1528 parent = tmp;
1529 last = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001530 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1531 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001532 /*
1533 * Now gather the remaining nodes from cur to end
1534 */
1535 continue; /* while */
1536 }
1537 tmp = xmlCopyNode(cur, 1);
1538 list = tmp;
1539 parent = NULL;
1540 last = tmp;
1541 }
1542 } else {
1543 tmp = NULL;
1544 switch (cur->type) {
1545 case XML_DTD_NODE:
1546 case XML_ELEMENT_DECL:
1547 case XML_ATTRIBUTE_DECL:
1548 case XML_ENTITY_NODE:
1549 /* Do not copy DTD informations */
1550 break;
1551 case XML_ENTITY_DECL:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001552 TODO /* handle crossing entities -> stack needed */
Owen Taylor3473f882001-02-23 17:55:21 +00001553 break;
1554 case XML_XINCLUDE_START:
1555 case XML_XINCLUDE_END:
1556 /* don't consider it part of the tree content */
1557 break;
1558 case XML_ATTRIBUTE_NODE:
1559 /* Humm, should not happen ! */
1560 STRANGE
1561 break;
1562 default:
1563 tmp = xmlCopyNode(cur, 1);
1564 break;
1565 }
1566 if (tmp != NULL) {
1567 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1568 STRANGE
1569 return(NULL);
1570 }
1571 if (last != NULL)
1572 xmlAddNextSibling(last, tmp);
1573 else {
1574 xmlAddChild(parent, tmp);
1575 last = tmp;
1576 }
1577 }
1578 }
1579 /*
1580 * Skip to next node in document order
1581 */
1582 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1583 STRANGE
1584 return(NULL);
1585 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001586 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001587 }
1588 return(list);
1589}
1590
1591/**
1592 * xmlXPtrBuildNodeList:
1593 * @obj: the XPointer result from the evaluation.
1594 *
1595 * Build a node list tree copy of the XPointer result.
Daniel Veillard39196eb2001-06-19 18:09:42 +00001596 * This will drop Attributes and Namespace declarations.
Owen Taylor3473f882001-02-23 17:55:21 +00001597 *
1598 * Returns an xmlNodePtr list or NULL.
1599 * the caller has to free the node tree.
1600 */
1601xmlNodePtr
1602xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1603 xmlNodePtr list = NULL, last = NULL;
1604 int i;
1605
1606 if (obj == NULL)
1607 return(NULL);
1608 switch (obj->type) {
1609 case XPATH_NODESET: {
1610 xmlNodeSetPtr set = obj->nodesetval;
1611 if (set == NULL)
1612 return(NULL);
1613 for (i = 0;i < set->nodeNr;i++) {
Daniel Veillard39196eb2001-06-19 18:09:42 +00001614 if (set->nodeTab[i] == NULL)
1615 continue;
1616 switch (set->nodeTab[i]->type) {
1617 case XML_TEXT_NODE:
1618 case XML_CDATA_SECTION_NODE:
1619 case XML_ELEMENT_NODE:
1620 case XML_ENTITY_REF_NODE:
1621 case XML_ENTITY_NODE:
1622 case XML_PI_NODE:
1623 case XML_COMMENT_NODE:
1624 case XML_DOCUMENT_NODE:
1625 case XML_HTML_DOCUMENT_NODE:
1626#ifdef LIBXML_DOCB_ENABLED
1627 case XML_DOCB_DOCUMENT_NODE:
1628#endif
1629 case XML_XINCLUDE_START:
1630 case XML_XINCLUDE_END:
1631 break;
1632 case XML_ATTRIBUTE_NODE:
1633 case XML_NAMESPACE_DECL:
1634 case XML_DOCUMENT_TYPE_NODE:
1635 case XML_DOCUMENT_FRAG_NODE:
1636 case XML_NOTATION_NODE:
1637 case XML_DTD_NODE:
1638 case XML_ELEMENT_DECL:
1639 case XML_ATTRIBUTE_DECL:
1640 case XML_ENTITY_DECL:
1641 continue; /* for */
1642 }
Owen Taylor3473f882001-02-23 17:55:21 +00001643 if (last == NULL)
1644 list = last = xmlCopyNode(set->nodeTab[i], 1);
1645 else {
1646 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1647 if (last->next != NULL)
1648 last = last->next;
1649 }
1650 }
1651 break;
1652 }
1653 case XPATH_LOCATIONSET: {
1654 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1655 if (set == NULL)
1656 return(NULL);
1657 for (i = 0;i < set->locNr;i++) {
1658 if (last == NULL)
1659 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1660 else
1661 xmlAddNextSibling(last,
1662 xmlXPtrBuildNodeList(set->locTab[i]));
1663 if (last != NULL) {
1664 while (last->next != NULL)
1665 last = last->next;
1666 }
1667 }
1668 break;
1669 }
1670 case XPATH_RANGE:
1671 return(xmlXPtrBuildRangeNodeList(obj));
1672 case XPATH_POINT:
1673 return(xmlCopyNode(obj->user, 0));
1674 default:
1675 break;
1676 }
1677 return(list);
1678}
1679
1680/************************************************************************
1681 * *
1682 * XPointer functions *
1683 * *
1684 ************************************************************************/
1685
1686/**
1687 * xmlXPtrNbLocChildren:
1688 * @node: an xmlNodePtr
1689 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001690 * Count the number of location children of @node or the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001691 * string value in case of text/PI/Comments nodes
1692 *
1693 * Returns the number of location children
1694 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001695static int
Owen Taylor3473f882001-02-23 17:55:21 +00001696xmlXPtrNbLocChildren(xmlNodePtr node) {
1697 int ret = 0;
1698 if (node == NULL)
1699 return(-1);
1700 switch (node->type) {
1701 case XML_HTML_DOCUMENT_NODE:
1702 case XML_DOCUMENT_NODE:
1703 case XML_ELEMENT_NODE:
1704 node = node->children;
1705 while (node != NULL) {
1706 if (node->type == XML_ELEMENT_NODE)
1707 ret++;
1708 node = node->next;
1709 }
1710 break;
1711 case XML_ATTRIBUTE_NODE:
1712 return(-1);
1713
1714 case XML_PI_NODE:
1715 case XML_COMMENT_NODE:
1716 case XML_TEXT_NODE:
1717 case XML_CDATA_SECTION_NODE:
1718 case XML_ENTITY_REF_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001719 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001720 break;
1721 default:
1722 return(-1);
1723 }
1724 return(ret);
1725}
1726
1727/**
1728 * xmlXPtrHereFunction:
1729 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001730 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001731 *
1732 * Function implementing here() operation
1733 * as described in 5.4.3
1734 */
1735void
1736xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001737 CHECK_ARITY(0);
1738
Owen Taylor3473f882001-02-23 17:55:21 +00001739 if (ctxt->context->here == NULL)
1740 XP_ERROR(XPTR_SYNTAX_ERROR);
1741
1742 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1743}
1744
1745/**
1746 * xmlXPtrOriginFunction:
1747 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001748 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001749 *
1750 * Function implementing origin() operation
1751 * as described in 5.4.3
1752 */
1753void
1754xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001755 CHECK_ARITY(0);
1756
Owen Taylor3473f882001-02-23 17:55:21 +00001757 if (ctxt->context->origin == NULL)
1758 XP_ERROR(XPTR_SYNTAX_ERROR);
1759
1760 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1761}
1762
1763/**
1764 * xmlXPtrStartPointFunction:
1765 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001766 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001767 *
1768 * Function implementing start-point() operation
1769 * as described in 5.4.3
1770 * ----------------
1771 * location-set start-point(location-set)
1772 *
1773 * For each location x in the argument location-set, start-point adds a
1774 * location of type point to the result location-set. That point represents
1775 * the start point of location x and is determined by the following rules:
1776 *
1777 * - If x is of type point, the start point is x.
1778 * - If x is of type range, the start point is the start point of x.
1779 * - If x is of type root, element, text, comment, or processing instruction,
1780 * - the container node of the start point is x and the index is 0.
1781 * - If x is of type attribute or namespace, the function must signal a
1782 * syntax error.
1783 * ----------------
1784 *
1785 */
1786void
1787xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1788 xmlXPathObjectPtr tmp, obj, point;
1789 xmlLocationSetPtr newset = NULL;
1790 xmlLocationSetPtr oldset = NULL;
1791
1792 CHECK_ARITY(1);
1793 if ((ctxt->value == NULL) ||
1794 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1795 (ctxt->value->type != XPATH_NODESET)))
1796 XP_ERROR(XPATH_INVALID_TYPE)
1797
1798 obj = valuePop(ctxt);
1799 if (obj->type == XPATH_NODESET) {
1800 /*
1801 * First convert to a location set
1802 */
1803 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1804 xmlXPathFreeObject(obj);
1805 obj = tmp;
1806 }
1807
1808 newset = xmlXPtrLocationSetCreate(NULL);
1809 if (newset == NULL) {
1810 xmlXPathFreeObject(obj);
1811 XP_ERROR(XPATH_MEMORY_ERROR);
1812 }
1813 oldset = (xmlLocationSetPtr) obj->user;
1814 if (oldset != NULL) {
1815 int i;
1816
1817 for (i = 0; i < oldset->locNr; i++) {
1818 tmp = oldset->locTab[i];
1819 if (tmp == NULL)
1820 continue;
1821 point = NULL;
1822 switch (tmp->type) {
1823 case XPATH_POINT:
1824 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1825 break;
1826 case XPATH_RANGE: {
1827 xmlNodePtr node = tmp->user;
1828 if (node != NULL) {
1829 if (node->type == XML_ATTRIBUTE_NODE) {
1830 /* TODO: Namespace Nodes ??? */
1831 xmlXPathFreeObject(obj);
1832 xmlXPtrFreeLocationSet(newset);
1833 XP_ERROR(XPTR_SYNTAX_ERROR);
1834 }
1835 point = xmlXPtrNewPoint(node, tmp->index);
1836 }
1837 break;
1838 }
1839 default:
1840 /*** Should we raise an error ?
1841 xmlXPathFreeObject(obj);
1842 xmlXPathFreeObject(newset);
1843 XP_ERROR(XPATH_INVALID_TYPE)
1844 ***/
1845 break;
1846 }
1847 if (point != NULL)
1848 xmlXPtrLocationSetAdd(newset, point);
1849 }
1850 }
1851 xmlXPathFreeObject(obj);
1852 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1853}
1854
1855/**
1856 * xmlXPtrEndPointFunction:
1857 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001858 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001859 *
1860 * Function implementing end-point() operation
1861 * as described in 5.4.3
1862 * ----------------------------
1863 * location-set end-point(location-set)
1864 *
1865 * For each location x in the argument location-set, end-point adds a
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001866 * location of type point to the result location-set. That point represents
Owen Taylor3473f882001-02-23 17:55:21 +00001867 * the end point of location x and is determined by the following rules:
1868 *
1869 * - If x is of type point, the resulting point is x.
1870 * - If x is of type range, the resulting point is the end point of x.
1871 * - If x is of type root or element, the container node of the resulting
1872 * point is x and the index is the number of location children of x.
1873 * - If x is of type text, comment, or processing instruction, the container
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001874 * node of the resulting point is x and the index is the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001875 * string-value of x.
1876 * - If x is of type attribute or namespace, the function must signal a
1877 * syntax error.
1878 * ----------------------------
1879 */
1880void
1881xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1882 xmlXPathObjectPtr tmp, obj, point;
1883 xmlLocationSetPtr newset = NULL;
1884 xmlLocationSetPtr oldset = NULL;
1885
1886 CHECK_ARITY(1);
1887 if ((ctxt->value == NULL) ||
1888 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1889 (ctxt->value->type != XPATH_NODESET)))
1890 XP_ERROR(XPATH_INVALID_TYPE)
1891
1892 obj = valuePop(ctxt);
1893 if (obj->type == XPATH_NODESET) {
1894 /*
1895 * First convert to a location set
1896 */
1897 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1898 xmlXPathFreeObject(obj);
1899 obj = tmp;
1900 }
1901
1902 newset = xmlXPtrLocationSetCreate(NULL);
1903 oldset = (xmlLocationSetPtr) obj->user;
1904 if (oldset != NULL) {
1905 int i;
1906
1907 for (i = 0; i < oldset->locNr; i++) {
1908 tmp = oldset->locTab[i];
1909 if (tmp == NULL)
1910 continue;
1911 point = NULL;
1912 switch (tmp->type) {
1913 case XPATH_POINT:
1914 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1915 break;
1916 case XPATH_RANGE: {
1917 xmlNodePtr node = tmp->user2;
1918 if (node != NULL) {
1919 if (node->type == XML_ATTRIBUTE_NODE) {
1920 /* TODO: Namespace Nodes ??? */
1921 xmlXPathFreeObject(obj);
1922 xmlXPtrFreeLocationSet(newset);
1923 XP_ERROR(XPTR_SYNTAX_ERROR);
1924 }
1925 point = xmlXPtrNewPoint(node, tmp->index2);
1926 } else if (tmp->user == NULL) {
1927 point = xmlXPtrNewPoint(node,
1928 xmlXPtrNbLocChildren(node));
1929 }
1930 break;
1931 }
1932 default:
1933 /*** Should we raise an error ?
1934 xmlXPathFreeObject(obj);
1935 xmlXPathFreeObject(newset);
1936 XP_ERROR(XPATH_INVALID_TYPE)
1937 ***/
1938 break;
1939 }
1940 if (point != NULL)
1941 xmlXPtrLocationSetAdd(newset, point);
1942 }
1943 }
1944 xmlXPathFreeObject(obj);
1945 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1946}
1947
1948
1949/**
1950 * xmlXPtrCoveringRange:
1951 * @ctxt: the XPointer Parser context
1952 * @loc: the location for which the covering range must be computed
1953 *
1954 * A covering range is a range that wholly encompasses a location
1955 * Section 5.3.3. Covering Ranges for All Location Types
1956 * http://www.w3.org/TR/xptr#N2267
1957 *
1958 * Returns a new location or NULL in case of error
1959 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001960static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001961xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1962 if (loc == NULL)
1963 return(NULL);
1964 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1965 (ctxt->context->doc == NULL))
1966 return(NULL);
1967 switch (loc->type) {
1968 case XPATH_POINT:
1969 return(xmlXPtrNewRange(loc->user, loc->index,
1970 loc->user, loc->index));
1971 case XPATH_RANGE:
1972 if (loc->user2 != NULL) {
1973 return(xmlXPtrNewRange(loc->user, loc->index,
1974 loc->user2, loc->index2));
1975 } else {
1976 xmlNodePtr node = (xmlNodePtr) loc->user;
1977 if (node == (xmlNodePtr) ctxt->context->doc) {
1978 return(xmlXPtrNewRange(node, 0, node,
1979 xmlXPtrGetArity(node)));
1980 } else {
1981 switch (node->type) {
1982 case XML_ATTRIBUTE_NODE:
1983 /* !!! our model is slightly different than XPath */
1984 return(xmlXPtrNewRange(node, 0, node,
1985 xmlXPtrGetArity(node)));
1986 case XML_ELEMENT_NODE:
1987 case XML_TEXT_NODE:
1988 case XML_CDATA_SECTION_NODE:
1989 case XML_ENTITY_REF_NODE:
1990 case XML_PI_NODE:
1991 case XML_COMMENT_NODE:
1992 case XML_DOCUMENT_NODE:
1993 case XML_NOTATION_NODE:
1994 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001995 int indx = xmlXPtrGetIndex(node);
Owen Taylor3473f882001-02-23 17:55:21 +00001996
1997 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001998 return(xmlXPtrNewRange(node, indx - 1,
1999 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00002000 }
2001 default:
2002 return(NULL);
2003 }
2004 }
2005 }
2006 default:
2007 TODO /* missed one case ??? */
2008 }
2009 return(NULL);
2010}
2011
2012/**
2013 * xmlXPtrRangeFunction:
2014 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002015 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002016 *
2017 * Function implementing the range() function 5.4.3
2018 * location-set range(location-set )
2019 *
2020 * The range function returns ranges covering the locations in
2021 * the argument location-set. For each location x in the argument
2022 * location-set, a range location representing the covering range of
2023 * x is added to the result location-set.
2024 */
2025void
2026xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2027 int i;
2028 xmlXPathObjectPtr set;
2029 xmlLocationSetPtr oldset;
2030 xmlLocationSetPtr newset;
2031
2032 CHECK_ARITY(1);
2033 if ((ctxt->value == NULL) ||
2034 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2035 (ctxt->value->type != XPATH_NODESET)))
2036 XP_ERROR(XPATH_INVALID_TYPE)
2037
2038 set = valuePop(ctxt);
2039 if (set->type == XPATH_NODESET) {
2040 xmlXPathObjectPtr tmp;
2041
2042 /*
2043 * First convert to a location set
2044 */
2045 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2046 xmlXPathFreeObject(set);
2047 set = tmp;
2048 }
2049 oldset = (xmlLocationSetPtr) set->user;
2050
2051 /*
2052 * The loop is to compute the covering range for each item and add it
2053 */
2054 newset = xmlXPtrLocationSetCreate(NULL);
2055 for (i = 0;i < oldset->locNr;i++) {
2056 xmlXPtrLocationSetAdd(newset,
2057 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2058 }
2059
2060 /*
2061 * Save the new value and cleanup
2062 */
2063 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2064 xmlXPathFreeObject(set);
2065}
2066
2067/**
2068 * xmlXPtrInsideRange:
2069 * @ctxt: the XPointer Parser context
2070 * @loc: the location for which the inside range must be computed
2071 *
2072 * A inside range is a range described in the range-inside() description
2073 *
2074 * Returns a new location or NULL in case of error
2075 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002076static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002077xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2078 if (loc == NULL)
2079 return(NULL);
2080 if ((ctxt == NULL) || (ctxt->context == NULL) ||
2081 (ctxt->context->doc == NULL))
2082 return(NULL);
2083 switch (loc->type) {
2084 case XPATH_POINT: {
2085 xmlNodePtr node = (xmlNodePtr) loc->user;
2086 switch (node->type) {
2087 case XML_PI_NODE:
2088 case XML_COMMENT_NODE:
2089 case XML_TEXT_NODE:
2090 case XML_CDATA_SECTION_NODE: {
2091 if (node->content == NULL) {
2092 return(xmlXPtrNewRange(node, 0, node, 0));
2093 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002094 return(xmlXPtrNewRange(node, 0, node,
2095 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002096 }
2097 }
2098 case XML_ATTRIBUTE_NODE:
2099 case XML_ELEMENT_NODE:
2100 case XML_ENTITY_REF_NODE:
2101 case XML_DOCUMENT_NODE:
2102 case XML_NOTATION_NODE:
2103 case XML_HTML_DOCUMENT_NODE: {
2104 return(xmlXPtrNewRange(node, 0, node,
2105 xmlXPtrGetArity(node)));
2106 }
2107 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002108 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002109 }
2110 return(NULL);
2111 }
2112 case XPATH_RANGE: {
2113 xmlNodePtr node = (xmlNodePtr) loc->user;
2114 if (loc->user2 != NULL) {
2115 return(xmlXPtrNewRange(node, loc->index,
2116 loc->user2, loc->index2));
2117 } else {
2118 switch (node->type) {
2119 case XML_PI_NODE:
2120 case XML_COMMENT_NODE:
2121 case XML_TEXT_NODE:
2122 case XML_CDATA_SECTION_NODE: {
2123 if (node->content == NULL) {
2124 return(xmlXPtrNewRange(node, 0, node, 0));
2125 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002126 return(xmlXPtrNewRange(node, 0, node,
2127 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002128 }
2129 }
2130 case XML_ATTRIBUTE_NODE:
2131 case XML_ELEMENT_NODE:
2132 case XML_ENTITY_REF_NODE:
2133 case XML_DOCUMENT_NODE:
2134 case XML_NOTATION_NODE:
2135 case XML_HTML_DOCUMENT_NODE: {
2136 return(xmlXPtrNewRange(node, 0, node,
2137 xmlXPtrGetArity(node)));
2138 }
2139 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002140 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002141 }
2142 return(NULL);
2143 }
2144 }
2145 default:
2146 TODO /* missed one case ??? */
2147 }
2148 return(NULL);
2149}
2150
2151/**
2152 * xmlXPtrRangeInsideFunction:
2153 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002154 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002155 *
2156 * Function implementing the range-inside() function 5.4.3
2157 * location-set range-inside(location-set )
2158 *
2159 * The range-inside function returns ranges covering the contents of
2160 * the locations in the argument location-set. For each location x in
2161 * the argument location-set, a range location is added to the result
2162 * location-set. If x is a range location, then x is added to the
2163 * result location-set. If x is not a range location, then x is used
2164 * as the container location of the start and end points of the range
2165 * location to be added; the index of the start point of the range is
2166 * zero; if the end point is a character point then its index is the
2167 * length of the string-value of x, and otherwise is the number of
2168 * location children of x.
2169 *
2170 */
2171void
2172xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2173 int i;
2174 xmlXPathObjectPtr set;
2175 xmlLocationSetPtr oldset;
2176 xmlLocationSetPtr newset;
2177
2178 CHECK_ARITY(1);
2179 if ((ctxt->value == NULL) ||
2180 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2181 (ctxt->value->type != XPATH_NODESET)))
2182 XP_ERROR(XPATH_INVALID_TYPE)
2183
2184 set = valuePop(ctxt);
2185 if (set->type == XPATH_NODESET) {
2186 xmlXPathObjectPtr tmp;
2187
2188 /*
2189 * First convert to a location set
2190 */
2191 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2192 xmlXPathFreeObject(set);
2193 set = tmp;
2194 }
2195 oldset = (xmlLocationSetPtr) set->user;
2196
2197 /*
2198 * The loop is to compute the covering range for each item and add it
2199 */
2200 newset = xmlXPtrLocationSetCreate(NULL);
2201 for (i = 0;i < oldset->locNr;i++) {
2202 xmlXPtrLocationSetAdd(newset,
2203 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2204 }
2205
2206 /*
2207 * Save the new value and cleanup
2208 */
2209 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2210 xmlXPathFreeObject(set);
2211}
2212
2213/**
2214 * xmlXPtrRangeToFunction:
2215 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002216 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002217 *
2218 * Implement the range-to() XPointer function
2219 */
2220void
2221xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2222 xmlXPathObjectPtr range;
2223 const xmlChar *cur;
2224 xmlXPathObjectPtr res, obj;
2225 xmlXPathObjectPtr tmp;
2226 xmlLocationSetPtr newset = NULL;
2227 xmlNodeSetPtr oldset;
2228 int i;
2229
2230 CHECK_ARITY(1);
2231 /*
2232 * Save the expression pointer since we will have to evaluate
2233 * it multiple times. Initialize the new set.
2234 */
2235 CHECK_TYPE(XPATH_NODESET);
2236 obj = valuePop(ctxt);
2237 oldset = obj->nodesetval;
2238 ctxt->context->node = NULL;
2239
2240 cur = ctxt->cur;
2241 newset = xmlXPtrLocationSetCreate(NULL);
2242
2243 for (i = 0; i < oldset->nodeNr; i++) {
2244 ctxt->cur = cur;
2245
2246 /*
2247 * Run the evaluation with a node list made of a single item
2248 * in the nodeset.
2249 */
2250 ctxt->context->node = oldset->nodeTab[i];
2251 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2252 valuePush(ctxt, tmp);
2253
2254 xmlXPathEvalExpr(ctxt);
2255 CHECK_ERROR;
2256
2257 /*
2258 * The result of the evaluation need to be tested to
2259 * decided whether the filter succeeded or not
2260 */
2261 res = valuePop(ctxt);
2262 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2263 if (range != NULL) {
2264 xmlXPtrLocationSetAdd(newset, range);
2265 }
2266
2267 /*
2268 * Cleanup
2269 */
2270 if (res != NULL)
2271 xmlXPathFreeObject(res);
2272 if (ctxt->value == tmp) {
2273 res = valuePop(ctxt);
2274 xmlXPathFreeObject(res);
2275 }
2276
2277 ctxt->context->node = NULL;
2278 }
2279
2280 /*
2281 * The result is used as the new evaluation set.
2282 */
2283 xmlXPathFreeObject(obj);
2284 ctxt->context->node = NULL;
2285 ctxt->context->contextSize = -1;
2286 ctxt->context->proximityPosition = -1;
2287 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2288}
2289
2290/**
2291 * xmlXPtrAdvanceNode:
2292 * @cur: the node
William M. Brackce4fc562004-01-22 02:47:18 +00002293 * @level: incremented/decremented to show level in tree
Owen Taylor3473f882001-02-23 17:55:21 +00002294 *
2295 * Advance to the next element or text node in document order
2296 * TODO: add a stack for entering/exiting entities
2297 *
2298 * Returns -1 in case of failure, 0 otherwise
2299 */
2300xmlNodePtr
William M. Brackf7eb7942003-12-31 07:59:17 +00002301xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
Owen Taylor3473f882001-02-23 17:55:21 +00002302next:
2303 if (cur == NULL)
2304 return(NULL);
2305 if (cur->children != NULL) {
2306 cur = cur->children ;
William M. Brackf7eb7942003-12-31 07:59:17 +00002307 if (level != NULL)
2308 (*level)++;
Owen Taylor3473f882001-02-23 17:55:21 +00002309 goto found;
2310 }
2311 if (cur->next != NULL) {
2312 cur = cur->next;
2313 goto found;
2314 }
2315 do {
2316 cur = cur->parent;
William M. Brackf7eb7942003-12-31 07:59:17 +00002317 if (level != NULL)
2318 (*level)--;
Owen Taylor3473f882001-02-23 17:55:21 +00002319 if (cur == NULL) return(NULL);
2320 if (cur->next != NULL) {
2321 cur = cur->next;
2322 goto found;
2323 }
2324 } while (cur != NULL);
2325
2326found:
2327 if ((cur->type != XML_ELEMENT_NODE) &&
2328 (cur->type != XML_TEXT_NODE) &&
2329 (cur->type != XML_DOCUMENT_NODE) &&
2330 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2331 (cur->type != XML_CDATA_SECTION_NODE))
2332 goto next;
2333 if (cur->type == XML_ENTITY_REF_NODE) {
2334 TODO
2335 }
2336 return(cur);
2337}
2338
2339/**
2340 * xmlXPtrAdvanceChar:
2341 * @node: the node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002342 * @indx: the indx
Owen Taylor3473f882001-02-23 17:55:21 +00002343 * @bytes: the number of bytes
2344 *
2345 * Advance a point of the associated number of bytes (not UTF8 chars)
2346 *
2347 * Returns -1 in case of failure, 0 otherwise
2348 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002349static int
2350xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Owen Taylor3473f882001-02-23 17:55:21 +00002351 xmlNodePtr cur;
2352 int pos;
2353 int len;
2354
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002355 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002356 return(-1);
2357 cur = *node;
2358 if (cur == NULL)
2359 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002360 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002361
2362 while (bytes >= 0) {
2363 /*
2364 * First position to the beginning of the first text node
2365 * corresponding to this point
2366 */
2367 while ((cur != NULL) &&
2368 ((cur->type == XML_ELEMENT_NODE) ||
2369 (cur->type == XML_DOCUMENT_NODE) ||
2370 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2371 if (pos > 0) {
2372 cur = xmlXPtrGetNthChild(cur, pos);
2373 pos = 0;
2374 } else {
William M. Brackf7eb7942003-12-31 07:59:17 +00002375 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002376 pos = 0;
2377 }
2378 }
2379
2380 if (cur == NULL) {
2381 *node = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002382 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002383 return(-1);
2384 }
2385
2386 /*
2387 * if there is no move needed return the current value.
2388 */
2389 if (pos == 0) pos = 1;
2390 if (bytes == 0) {
2391 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002392 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002393 return(0);
2394 }
2395 /*
2396 * We should have a text (or cdata) node ...
2397 */
2398 len = 0;
Daniel Veillard7db37732001-07-12 01:20:08 +00002399 if ((cur->type != XML_ELEMENT_NODE) &&
2400 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002401 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002402 }
2403 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002404 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002405 STRANGE
2406 pos = len;
2407 }
2408 if (pos + bytes >= len) {
2409 bytes -= (len - pos);
William M. Brackf7eb7942003-12-31 07:59:17 +00002410 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002411 cur = 0;
2412 } else if (pos + bytes < len) {
2413 pos += bytes;
2414 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002415 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002416 return(0);
2417 }
2418 }
2419 return(-1);
2420}
2421
2422/**
2423 * xmlXPtrMatchString:
2424 * @string: the string to search
2425 * @start: the start textnode
2426 * @startindex: the start index
2427 * @end: the end textnode IN/OUT
2428 * @endindex: the end index IN/OUT
2429 *
2430 * Check whether the document contains @string at the position
2431 * (@start, @startindex) and limited by the (@end, @endindex) point
2432 *
2433 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2434 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002435 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002436 * of the range
2437 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002438static int
Owen Taylor3473f882001-02-23 17:55:21 +00002439xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2440 xmlNodePtr *end, int *endindex) {
2441 xmlNodePtr cur;
2442 int pos; /* 0 based */
2443 int len; /* in bytes */
2444 int stringlen; /* in bytes */
2445 int match;
2446
2447 if (string == NULL)
2448 return(-1);
2449 if (start == NULL)
2450 return(-1);
2451 if ((end == NULL) || (endindex == NULL))
2452 return(-1);
2453 cur = start;
2454 if (cur == NULL)
2455 return(-1);
2456 pos = startindex - 1;
2457 stringlen = xmlStrlen(string);
2458
2459 while (stringlen > 0) {
2460 if ((cur == *end) && (pos + stringlen > *endindex))
2461 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002462
2463 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002464 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002465 if (len >= pos + stringlen) {
Owen Taylor3473f882001-02-23 17:55:21 +00002466 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
Owen Taylor3473f882001-02-23 17:55:21 +00002467 if (match) {
2468#ifdef DEBUG_RANGES
2469 xmlGenericError(xmlGenericErrorContext,
2470 "found range %d bytes at index %d of ->",
2471 stringlen, pos + 1);
2472 xmlDebugDumpString(stdout, cur->content);
2473 xmlGenericError(xmlGenericErrorContext, "\n");
2474#endif
2475 *end = cur;
2476 *endindex = pos + stringlen;
2477 return(1);
2478 } else {
2479 return(0);
2480 }
2481 } else {
2482 int sub = len - pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002483 match = (!xmlStrncmp(&cur->content[pos], string, sub));
Owen Taylor3473f882001-02-23 17:55:21 +00002484 if (match) {
2485#ifdef DEBUG_RANGES
2486 xmlGenericError(xmlGenericErrorContext,
2487 "found subrange %d bytes at index %d of ->",
2488 sub, pos + 1);
2489 xmlDebugDumpString(stdout, cur->content);
2490 xmlGenericError(xmlGenericErrorContext, "\n");
2491#endif
2492 string = &string[sub];
2493 stringlen -= sub;
2494 } else {
2495 return(0);
2496 }
2497 }
2498 }
William M. Brackf7eb7942003-12-31 07:59:17 +00002499 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002500 if (cur == NULL)
2501 return(0);
2502 pos = 0;
2503 }
2504 return(1);
2505}
2506
2507/**
2508 * xmlXPtrSearchString:
2509 * @string: the string to search
2510 * @start: the start textnode IN/OUT
2511 * @startindex: the start index IN/OUT
2512 * @end: the end textnode
2513 * @endindex: the end index
2514 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002515 * Search the next occurrence of @string within the document content
Owen Taylor3473f882001-02-23 17:55:21 +00002516 * until the (@end, @endindex) point is reached
2517 *
2518 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2519 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002520 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002521 * of the range
2522 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002523static int
Owen Taylor3473f882001-02-23 17:55:21 +00002524xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2525 xmlNodePtr *end, int *endindex) {
2526 xmlNodePtr cur;
2527 const xmlChar *str;
2528 int pos; /* 0 based */
2529 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002530 xmlChar first;
2531
2532 if (string == NULL)
2533 return(-1);
2534 if ((start == NULL) || (startindex == NULL))
2535 return(-1);
2536 if ((end == NULL) || (endindex == NULL))
2537 return(-1);
2538 cur = *start;
2539 if (cur == NULL)
2540 return(-1);
2541 pos = *startindex - 1;
2542 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002543
2544 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002545 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002546 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002547 while (pos <= len) {
2548 if (first != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00002549 str = xmlStrchr(&cur->content[pos], first);
Owen Taylor3473f882001-02-23 17:55:21 +00002550 if (str != NULL) {
2551 pos = (str - (xmlChar *)(cur->content));
2552#ifdef DEBUG_RANGES
2553 xmlGenericError(xmlGenericErrorContext,
2554 "found '%c' at index %d of ->",
2555 first, pos + 1);
2556 xmlDebugDumpString(stdout, cur->content);
2557 xmlGenericError(xmlGenericErrorContext, "\n");
2558#endif
2559 if (xmlXPtrMatchString(string, cur, pos + 1,
2560 end, endindex)) {
2561 *start = cur;
2562 *startindex = pos + 1;
2563 return(1);
2564 }
2565 pos++;
2566 } else {
2567 pos = len + 1;
2568 }
2569 } else {
2570 /*
2571 * An empty string is considered to match before each
2572 * character of the string-value and after the final
2573 * character.
2574 */
2575#ifdef DEBUG_RANGES
2576 xmlGenericError(xmlGenericErrorContext,
2577 "found '' at index %d of ->",
2578 pos + 1);
2579 xmlDebugDumpString(stdout, cur->content);
2580 xmlGenericError(xmlGenericErrorContext, "\n");
2581#endif
2582 *start = cur;
2583 *startindex = pos + 1;
2584 *end = cur;
2585 *endindex = pos + 1;
2586 return(1);
2587 }
2588 }
2589 }
2590 if ((cur == *end) && (pos >= *endindex))
2591 return(0);
William M. Brackf7eb7942003-12-31 07:59:17 +00002592 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002593 if (cur == NULL)
2594 return(0);
2595 pos = 1;
2596 }
2597 return(0);
2598}
2599
2600/**
2601 * xmlXPtrGetLastChar:
2602 * @node: the node
2603 * @index: the index
2604 *
2605 * Computes the point coordinates of the last char of this point
2606 *
2607 * Returns -1 in case of failure, 0 otherwise
2608 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002609static int
2610xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002611 xmlNodePtr cur;
2612 int pos, len = 0;
2613
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002614 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002615 return(-1);
2616 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002617 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002618
2619 if (cur == NULL)
2620 return(-1);
2621
2622 if ((cur->type == XML_ELEMENT_NODE) ||
2623 (cur->type == XML_DOCUMENT_NODE) ||
2624 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2625 if (pos > 0) {
2626 cur = xmlXPtrGetNthChild(cur, pos);
2627 pos = 0;
2628 }
2629 }
2630 while (cur != NULL) {
2631 if (cur->last != NULL)
2632 cur = cur->last;
Daniel Veillard7db37732001-07-12 01:20:08 +00002633 else if ((cur->type != XML_ELEMENT_NODE) &&
2634 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002635 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002636 break;
2637 } else {
2638 return(-1);
2639 }
2640 }
2641 if (cur == NULL)
2642 return(-1);
2643 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002644 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002645 return(0);
2646}
2647
2648/**
2649 * xmlXPtrGetStartPoint:
2650 * @obj: an range
2651 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002652 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002653 *
2654 * read the object and return the start point coordinates.
2655 *
2656 * Returns -1 in case of failure, 0 otherwise
2657 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002658static int
2659xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2660 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002661 return(-1);
2662
2663 switch (obj->type) {
2664 case XPATH_POINT:
2665 *node = obj->user;
2666 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002667 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002668 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002669 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002670 return(0);
2671 case XPATH_RANGE:
2672 *node = obj->user;
2673 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002674 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002675 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002676 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002677 return(0);
2678 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002679 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002680 }
2681 return(-1);
2682}
2683
2684/**
2685 * xmlXPtrGetEndPoint:
2686 * @obj: an range
2687 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002688 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002689 *
2690 * read the object and return the end point coordinates.
2691 *
2692 * Returns -1 in case of failure, 0 otherwise
2693 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002694static int
2695xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2696 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002697 return(-1);
2698
2699 switch (obj->type) {
2700 case XPATH_POINT:
2701 *node = obj->user;
2702 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002703 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002704 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002705 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002706 return(0);
2707 case XPATH_RANGE:
2708 *node = obj->user;
2709 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002710 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002711 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002712 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002713 return(0);
2714 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002715 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002716 }
2717 return(-1);
2718}
2719
2720/**
2721 * xmlXPtrStringRangeFunction:
2722 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002723 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002724 *
2725 * Function implementing the string-range() function
2726 * range as described in 5.4.2
2727 *
2728 * ------------------------------
2729 * [Definition: For each location in the location-set argument,
2730 * string-range returns a set of string ranges, a set of substrings in a
2731 * string. Specifically, the string-value of the location is searched for
2732 * substrings that match the string argument, and the resulting location-set
2733 * will contain a range location for each non-overlapping match.]
2734 * An empty string is considered to match before each character of the
2735 * string-value and after the final character. Whitespace in a string
2736 * is matched literally, with no normalization except that provided by
2737 * XML for line ends. The third argument gives the position of the first
2738 * character to be in the resulting range, relative to the start of the
2739 * match. The default value is 1, which makes the range start immediately
2740 * before the first character of the matched string. The fourth argument
2741 * gives the number of characters in the range; the default is that the
2742 * range extends to the end of the matched string.
2743 *
2744 * Element boundaries, as well as entire embedded nodes such as processing
2745 * instructions and comments, are ignored as defined in [XPath].
2746 *
2747 * If the string in the second argument is not found in the string-value
2748 * of the location, or if a value in the third or fourth argument indicates
2749 * a string that is beyond the beginning or end of the document, the
2750 * expression fails.
2751 *
2752 * The points of the range-locations in the returned location-set will
2753 * all be character points.
2754 * ------------------------------
2755 */
2756void
2757xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2758 int i, startindex, endindex, fendindex;
2759 xmlNodePtr start, end, fend;
2760 xmlXPathObjectPtr set;
2761 xmlLocationSetPtr oldset;
2762 xmlLocationSetPtr newset;
2763 xmlXPathObjectPtr string;
2764 xmlXPathObjectPtr position = NULL;
2765 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002766 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002767
2768 /*
2769 * Grab the arguments
2770 */
2771 if ((nargs < 2) || (nargs > 4))
2772 XP_ERROR(XPATH_INVALID_ARITY);
2773
2774 if (nargs >= 4) {
2775 CHECK_TYPE(XPATH_NUMBER);
2776 number = valuePop(ctxt);
2777 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002778 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002779 }
2780 if (nargs >= 3) {
2781 CHECK_TYPE(XPATH_NUMBER);
2782 position = valuePop(ctxt);
2783 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002784 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002785 }
2786 CHECK_TYPE(XPATH_STRING);
2787 string = valuePop(ctxt);
2788 if ((ctxt->value == NULL) ||
2789 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2790 (ctxt->value->type != XPATH_NODESET)))
2791 XP_ERROR(XPATH_INVALID_TYPE)
2792
2793 set = valuePop(ctxt);
William M. Brack08171912003-12-29 02:52:11 +00002794 newset = xmlXPtrLocationSetCreate(NULL);
2795 if (set->nodesetval == NULL) {
2796 goto error;
2797 }
Owen Taylor3473f882001-02-23 17:55:21 +00002798 if (set->type == XPATH_NODESET) {
2799 xmlXPathObjectPtr tmp;
2800
2801 /*
2802 * First convert to a location set
2803 */
2804 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2805 xmlXPathFreeObject(set);
2806 set = tmp;
2807 }
2808 oldset = (xmlLocationSetPtr) set->user;
2809
2810 /*
2811 * The loop is to search for each element in the location set
2812 * the list of location set corresponding to that search
2813 */
Owen Taylor3473f882001-02-23 17:55:21 +00002814 for (i = 0;i < oldset->locNr;i++) {
2815#ifdef DEBUG_RANGES
2816 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2817#endif
2818
2819 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2820 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2821 xmlXPtrAdvanceChar(&start, &startindex, 0);
2822 xmlXPtrGetLastChar(&end, &endindex);
2823
2824#ifdef DEBUG_RANGES
2825 xmlGenericError(xmlGenericErrorContext,
2826 "from index %d of ->", startindex);
2827 xmlDebugDumpString(stdout, start->content);
2828 xmlGenericError(xmlGenericErrorContext, "\n");
2829 xmlGenericError(xmlGenericErrorContext,
2830 "to index %d of ->", endindex);
2831 xmlDebugDumpString(stdout, end->content);
2832 xmlGenericError(xmlGenericErrorContext, "\n");
2833#endif
2834 do {
2835 fend = end;
2836 fendindex = endindex;
2837 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2838 &fend, &fendindex);
2839 if (found == 1) {
2840 if (position == NULL) {
2841 xmlXPtrLocationSetAdd(newset,
2842 xmlXPtrNewRange(start, startindex, fend, fendindex));
2843 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2844 pos - 1) == 0) {
2845 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002846 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002847 xmlNodePtr rend;
2848 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002849 rindx = startindex - 1;
2850 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002851 num) == 0) {
2852 xmlXPtrLocationSetAdd(newset,
2853 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002854 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002855 }
2856 } else if ((number != NULL) && (num <= 0)) {
2857 xmlXPtrLocationSetAdd(newset,
2858 xmlXPtrNewRange(start, startindex,
2859 start, startindex));
2860 } else {
2861 xmlXPtrLocationSetAdd(newset,
2862 xmlXPtrNewRange(start, startindex,
2863 fend, fendindex));
2864 }
2865 }
2866 start = fend;
2867 startindex = fendindex;
2868 if (string->stringval[0] == 0)
2869 startindex++;
2870 }
2871 } while (found == 1);
2872 }
2873
2874 /*
2875 * Save the new value and cleanup
2876 */
William M. Brack08171912003-12-29 02:52:11 +00002877error:
Owen Taylor3473f882001-02-23 17:55:21 +00002878 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2879 xmlXPathFreeObject(set);
2880 xmlXPathFreeObject(string);
2881 if (position) xmlXPathFreeObject(position);
2882 if (number) xmlXPathFreeObject(number);
2883}
2884
2885/**
2886 * xmlXPtrEvalRangePredicate:
2887 * @ctxt: the XPointer Parser context
2888 *
2889 * [8] Predicate ::= '[' PredicateExpr ']'
2890 * [9] PredicateExpr ::= Expr
2891 *
2892 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2893 * a Location Set instead of a node set
2894 */
2895void
2896xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2897 const xmlChar *cur;
2898 xmlXPathObjectPtr res;
2899 xmlXPathObjectPtr obj, tmp;
2900 xmlLocationSetPtr newset = NULL;
2901 xmlLocationSetPtr oldset;
2902 int i;
2903
2904 SKIP_BLANKS;
2905 if (CUR != '[') {
2906 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2907 }
2908 NEXT;
2909 SKIP_BLANKS;
2910
2911 /*
2912 * Extract the old set, and then evaluate the result of the
2913 * expression for all the element in the set. use it to grow
2914 * up a new set.
2915 */
2916 CHECK_TYPE(XPATH_LOCATIONSET);
2917 obj = valuePop(ctxt);
2918 oldset = obj->user;
2919 ctxt->context->node = NULL;
2920
2921 if ((oldset == NULL) || (oldset->locNr == 0)) {
2922 ctxt->context->contextSize = 0;
2923 ctxt->context->proximityPosition = 0;
2924 xmlXPathEvalExpr(ctxt);
2925 res = valuePop(ctxt);
2926 if (res != NULL)
2927 xmlXPathFreeObject(res);
2928 valuePush(ctxt, obj);
2929 CHECK_ERROR;
2930 } else {
2931 /*
2932 * Save the expression pointer since we will have to evaluate
2933 * it multiple times. Initialize the new set.
2934 */
2935 cur = ctxt->cur;
2936 newset = xmlXPtrLocationSetCreate(NULL);
2937
2938 for (i = 0; i < oldset->locNr; i++) {
2939 ctxt->cur = cur;
2940
2941 /*
2942 * Run the evaluation with a node list made of a single item
2943 * in the nodeset.
2944 */
2945 ctxt->context->node = oldset->locTab[i]->user;
2946 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2947 valuePush(ctxt, tmp);
2948 ctxt->context->contextSize = oldset->locNr;
2949 ctxt->context->proximityPosition = i + 1;
2950
2951 xmlXPathEvalExpr(ctxt);
2952 CHECK_ERROR;
2953
2954 /*
2955 * The result of the evaluation need to be tested to
2956 * decided whether the filter succeeded or not
2957 */
2958 res = valuePop(ctxt);
2959 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2960 xmlXPtrLocationSetAdd(newset,
2961 xmlXPathObjectCopy(oldset->locTab[i]));
2962 }
2963
2964 /*
2965 * Cleanup
2966 */
2967 if (res != NULL)
2968 xmlXPathFreeObject(res);
2969 if (ctxt->value == tmp) {
2970 res = valuePop(ctxt);
2971 xmlXPathFreeObject(res);
2972 }
2973
2974 ctxt->context->node = NULL;
2975 }
2976
2977 /*
2978 * The result is used as the new evaluation set.
2979 */
2980 xmlXPathFreeObject(obj);
2981 ctxt->context->node = NULL;
2982 ctxt->context->contextSize = -1;
2983 ctxt->context->proximityPosition = -1;
2984 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2985 }
2986 if (CUR != ']') {
2987 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2988 }
2989
2990 NEXT;
2991 SKIP_BLANKS;
2992}
2993
2994#else
2995#endif
2996