blob: 9380d15b2a48827ca2208830e882b3f065c367c0 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xpointer.c : Code to handle XML Pointer
3 *
4 * World Wide Web Consortium Working Draft 03-March-1998
5 * http://www.w3.org/TR/2000/CR-xptr-20000607
6 *
7 * See Copyright for the status of this software.
8 *
9 * Daniel.Veillard@w3.org
10 */
11
12#ifdef WIN32
13#include "win32config.h"
14#else
15#include "config.h"
16#endif
17
18/**
19 * TODO: better handling of error cases, the full expression should
20 * be parsed beforehand instead of a progressive evaluation
21 * TODO: Access into entities references are not supported now ...
22 * need a start to be able to pop out of entities refs since
23 * parent is the endity declaration, not the ref.
24 */
25
26#include <stdio.h>
27#include <string.h>
28#include <libxml/xpointer.h>
29#include <libxml/xmlmemory.h>
30#include <libxml/parserInternals.h>
31#include <libxml/uri.h>
32#include <libxml/xpath.h>
33#include <libxml/xpathInternals.h>
34#ifdef LIBXML_DEBUG_ENABLED
35#include <libxml/debugXML.h>
36#endif
37#include <libxml/xmlerror.h>
38
39#ifdef LIBXML_XPTR_ENABLED
40
41/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
42#define XPTR_XMLNS_SCHEME
43
44/* #define DEBUG_RANGES */
45
46#define TODO \
47 xmlGenericError(xmlGenericErrorContext, \
48 "Unimplemented block at %s:%d\n", \
49 __FILE__, __LINE__);
50
51#define STRANGE \
52 xmlGenericError(xmlGenericErrorContext, \
53 "Internal error at %s:%d\n", \
54 __FILE__, __LINE__);
55
56/************************************************************************
57 * *
58 * A few helper functions for child sequences *
59 * *
60 ************************************************************************/
61
62xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur);
63/**
64 * xmlXPtrGetArity:
65 * @cur: the node
66 *
67 * Returns the number of child for an element, -1 in case of error
68 */
69int
70xmlXPtrGetArity(xmlNodePtr cur) {
71 int i;
72 if (cur == NULL)
73 return(-1);
74 cur = cur->children;
75 for (i = 0;cur != NULL;cur = cur->next) {
76 if ((cur->type == XML_ELEMENT_NODE) ||
77 (cur->type == XML_DOCUMENT_NODE) ||
78 (cur->type == XML_HTML_DOCUMENT_NODE)) {
79 i++;
80 }
81 }
82 return(i);
83}
84
85/**
86 * xmlXPtrGetIndex:
87 * @cur: the node
88 *
89 * Returns the index of the node in its parent children list, -1
90 * in case of error
91 */
92int
93xmlXPtrGetIndex(xmlNodePtr cur) {
94 int i;
95 if (cur == NULL)
96 return(-1);
97 for (i = 1;cur != NULL;cur = cur->prev) {
98 if ((cur->type == XML_ELEMENT_NODE) ||
99 (cur->type == XML_DOCUMENT_NODE) ||
100 (cur->type == XML_HTML_DOCUMENT_NODE)) {
101 i++;
102 }
103 }
104 return(i);
105}
106
107/**
108 * xmlXPtrGetNthChild:
109 * @cur: the node
110 * @no: the child number
111 *
112 * Returns the @no'th element child of @cur or NULL
113 */
114xmlNodePtr
115xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
116 int i;
117 if (cur == NULL)
118 return(cur);
119 cur = cur->children;
120 for (i = 0;i <= no;cur = cur->next) {
121 if (cur == NULL)
122 return(cur);
123 if ((cur->type == XML_ELEMENT_NODE) ||
124 (cur->type == XML_DOCUMENT_NODE) ||
125 (cur->type == XML_HTML_DOCUMENT_NODE)) {
126 i++;
127 if (i == no)
128 break;
129 }
130 }
131 return(cur);
132}
133
134/************************************************************************
135 * *
136 * Handling of XPointer specific types *
137 * *
138 ************************************************************************/
139
140/**
141 * xmlXPtrCmpPoints:
142 * @node1: the first node
143 * @index1: the first index
144 * @node2: the second node
145 * @index2: the second index
146 *
147 * Compare two points w.r.t document order
148 *
149 * Returns -2 in case of error 1 if first point < second point, 0 if
150 * that's the same point, -1 otherwise
151 */
152int
153xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
154 if ((node1 == NULL) || (node2 == NULL))
155 return(-2);
156 /*
157 * a couple of optimizations which will avoid computations in most cases
158 */
159 if (node1 == node2) {
160 if (index1 < index2)
161 return(1);
162 if (index1 > index2)
163 return(-1);
164 return(0);
165 }
166 return(xmlXPathCmpNodes(node1, node2));
167}
168
169/**
170 * xmlXPtrNewPoint:
171 * @node: the xmlNodePtr
172 * @index: the index within the node
173 *
174 * Create a new xmlXPathObjectPtr of type point
175 *
176 * Returns the newly created object.
177 */
178xmlXPathObjectPtr
179xmlXPtrNewPoint(xmlNodePtr node, int index) {
180 xmlXPathObjectPtr ret;
181
182 if (node == NULL)
183 return(NULL);
184 if (index < 0)
185 return(NULL);
186
187 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
188 if (ret == NULL) {
189 xmlGenericError(xmlGenericErrorContext,
190 "xmlXPtrNewPoint: out of memory\n");
191 return(NULL);
192 }
193 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
194 ret->type = XPATH_POINT;
195 ret->user = (void *) node;
196 ret->index = index;
197 return(ret);
198}
199
200/**
201 * xmlXPtrRangeCheckOrder:
202 * @range: an object range
203 *
204 * Make sure the points in the range are in the right order
205 */
206void
207xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
208 int tmp;
209 xmlNodePtr tmp2;
210 if (range == NULL)
211 return;
212 if (range->type != XPATH_RANGE)
213 return;
214 if (range->user2 == NULL)
215 return;
216 tmp = xmlXPtrCmpPoints(range->user, range->index,
217 range->user2, range->index2);
218 if (tmp == -1) {
219 tmp2 = range->user;
220 range->user = range->user2;
221 range->user2 = tmp2;
222 tmp = range->index;
223 range->index = range->index2;
224 range->index2 = tmp;
225 }
226}
227
228/**
229 * xmlXPtrRangesEqual:
230 * @range1: the first range
231 * @range2: the second range
232 *
233 * Compare two ranges
234 *
235 * Return 1 if equal, 0 otherwise
236 */
237int
238xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
239 if (range1 == range2)
240 return(1);
241 if ((range1 == NULL) || (range2 == NULL))
242 return(0);
243 if (range1->type != range2->type)
244 return(0);
245 if (range1->type != XPATH_RANGE)
246 return(0);
247 if (range1->user != range2->user)
248 return(0);
249 if (range1->index != range2->index)
250 return(0);
251 if (range1->user2 != range2->user2)
252 return(0);
253 if (range1->index2 != range2->index2)
254 return(0);
255 return(1);
256}
257
258/**
259 * xmlXPtrNewRange:
260 * @start: the starting node
261 * @startindex: the start index
262 * @end: the ending point
263 * @endindex: the ending index
264 *
265 * Create a new xmlXPathObjectPtr of type range
266 *
267 * Returns the newly created object.
268 */
269xmlXPathObjectPtr
270xmlXPtrNewRange(xmlNodePtr start, int startindex,
271 xmlNodePtr end, int endindex) {
272 xmlXPathObjectPtr ret;
273
274 if (start == NULL)
275 return(NULL);
276 if (end == NULL)
277 return(NULL);
278 if (startindex < 0)
279 return(NULL);
280 if (endindex < 0)
281 return(NULL);
282
283 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
284 if (ret == NULL) {
285 xmlGenericError(xmlGenericErrorContext,
286 "xmlXPtrNewRangePoints: out of memory\n");
287 return(NULL);
288 }
289 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
290 ret->type = XPATH_RANGE;
291 ret->user = start;
292 ret->index = startindex;
293 ret->user2 = end;
294 ret->index2 = endindex;
295 xmlXPtrRangeCheckOrder(ret);
296 return(ret);
297}
298
299/**
300 * xmlXPtrNewRangePoints:
301 * @start: the starting point
302 * @end: the ending point
303 *
304 * Create a new xmlXPathObjectPtr of type range using 2 Points
305 *
306 * Returns the newly created object.
307 */
308xmlXPathObjectPtr
309xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
310 xmlXPathObjectPtr ret;
311
312 if (start == NULL)
313 return(NULL);
314 if (end == NULL)
315 return(NULL);
316 if (start->type != XPATH_POINT)
317 return(NULL);
318 if (end->type != XPATH_POINT)
319 return(NULL);
320
321 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
322 if (ret == NULL) {
323 xmlGenericError(xmlGenericErrorContext,
324 "xmlXPtrNewRangePoints: out of memory\n");
325 return(NULL);
326 }
327 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
328 ret->type = XPATH_RANGE;
329 ret->user = start->user;
330 ret->index = start->index;
331 ret->user2 = end->user;
332 ret->index2 = end->index;
333 xmlXPtrRangeCheckOrder(ret);
334 return(ret);
335}
336
337/**
338 * xmlXPtrNewRangePointNode:
339 * @start: the starting point
340 * @end: the ending node
341 *
342 * Create a new xmlXPathObjectPtr of type range from a point to a node
343 *
344 * Returns the newly created object.
345 */
346xmlXPathObjectPtr
347xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
348 xmlXPathObjectPtr ret;
349
350 if (start == NULL)
351 return(NULL);
352 if (end == NULL)
353 return(NULL);
354 if (start->type != XPATH_POINT)
355 return(NULL);
356
357 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
358 if (ret == NULL) {
359 xmlGenericError(xmlGenericErrorContext,
360 "xmlXPtrNewRangePointNode: out of memory\n");
361 return(NULL);
362 }
363 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
364 ret->type = XPATH_RANGE;
365 ret->user = start->user;
366 ret->index = start->index;
367 ret->user2 = end;
368 ret->index2 = -1;
369 xmlXPtrRangeCheckOrder(ret);
370 return(ret);
371}
372
373/**
374 * xmlXPtrNewRangeNodePoint:
375 * @start: the starting node
376 * @end: the ending point
377 *
378 * Create a new xmlXPathObjectPtr of type range from a node to a point
379 *
380 * Returns the newly created object.
381 */
382xmlXPathObjectPtr
383xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
384 xmlXPathObjectPtr ret;
385
386 if (start == NULL)
387 return(NULL);
388 if (end == NULL)
389 return(NULL);
390 if (start->type != XPATH_POINT)
391 return(NULL);
392 if (end->type != XPATH_POINT)
393 return(NULL);
394
395 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
396 if (ret == NULL) {
397 xmlGenericError(xmlGenericErrorContext,
398 "xmlXPtrNewRangeNodePoint: out of memory\n");
399 return(NULL);
400 }
401 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
402 ret->type = XPATH_RANGE;
403 ret->user = start;
404 ret->index = -1;
405 ret->user2 = end->user;
406 ret->index2 = end->index;
407 xmlXPtrRangeCheckOrder(ret);
408 return(ret);
409}
410
411/**
412 * xmlXPtrNewRangeNodes:
413 * @start: the starting node
414 * @end: the ending node
415 *
416 * Create a new xmlXPathObjectPtr of type range using 2 nodes
417 *
418 * Returns the newly created object.
419 */
420xmlXPathObjectPtr
421xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
422 xmlXPathObjectPtr ret;
423
424 if (start == NULL)
425 return(NULL);
426 if (end == NULL)
427 return(NULL);
428
429 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
430 if (ret == NULL) {
431 xmlGenericError(xmlGenericErrorContext,
432 "xmlXPtrNewRangeNodes: out of memory\n");
433 return(NULL);
434 }
435 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
436 ret->type = XPATH_RANGE;
437 ret->user = start;
438 ret->index = -1;
439 ret->user2 = end;
440 ret->index2 = -1;
441 xmlXPtrRangeCheckOrder(ret);
442 return(ret);
443}
444
445/**
446 * xmlXPtrNewCollapsedRange:
447 * @start: the starting and ending node
448 *
449 * Create a new xmlXPathObjectPtr of type range using a single nodes
450 *
451 * Returns the newly created object.
452 */
453xmlXPathObjectPtr
454xmlXPtrNewCollapsedRange(xmlNodePtr start) {
455 xmlXPathObjectPtr ret;
456
457 if (start == NULL)
458 return(NULL);
459
460 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
461 if (ret == NULL) {
462 xmlGenericError(xmlGenericErrorContext,
463 "xmlXPtrNewRangeNodes: out of memory\n");
464 return(NULL);
465 }
466 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
467 ret->type = XPATH_RANGE;
468 ret->user = start;
469 ret->index = -1;
470 ret->user2 = NULL;
471 ret->index2 = -1;
472 return(ret);
473}
474
475/**
476 * xmlXPtrNewRangeNodeObject:
477 * @start: the starting node
478 * @end: the ending object
479 *
480 * Create a new xmlXPathObjectPtr of type range from a not to an object
481 *
482 * Returns the newly created object.
483 */
484xmlXPathObjectPtr
485xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
486 xmlXPathObjectPtr ret;
487
488 if (start == NULL)
489 return(NULL);
490 if (end == NULL)
491 return(NULL);
492 switch (end->type) {
493 case XPATH_POINT:
494 break;
495 case XPATH_NODESET:
496 /*
497 * Empty set ...
498 */
499 if (end->nodesetval->nodeNr <= 0)
500 return(NULL);
501 break;
502 default:
503 TODO
504 return(NULL);
505 }
506
507 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
508 if (ret == NULL) {
509 xmlGenericError(xmlGenericErrorContext,
510 "xmlXPtrNewRangeNodeObject: out of memory\n");
511 return(NULL);
512 }
513 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
514 ret->type = XPATH_RANGE;
515 ret->user = start;
516 ret->index = -1;
517 switch (end->type) {
518 case XPATH_POINT:
519 ret->user2 = end->user;
520 ret->index2 = end->index;
521 case XPATH_NODESET: {
522 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
523 ret->index2 = -1;
524 break;
525 }
526 default:
527 STRANGE
528 return(NULL);
529 }
530 xmlXPtrRangeCheckOrder(ret);
531 return(ret);
532}
533
534#define XML_RANGESET_DEFAULT 10
535
536/**
537 * xmlXPtrLocationSetCreate:
538 * @val: an initial xmlXPathObjectPtr, or NULL
539 *
540 * Create a new xmlLocationSetPtr of type double and of value @val
541 *
542 * Returns the newly created object.
543 */
544xmlLocationSetPtr
545xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
546 xmlLocationSetPtr ret;
547
548 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
549 if (ret == NULL) {
550 xmlGenericError(xmlGenericErrorContext,
551 "xmlXPtrLocationSetCreate: out of memory\n");
552 return(NULL);
553 }
554 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
555 if (val != NULL) {
556 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
557 sizeof(xmlXPathObjectPtr));
558 if (ret->locTab == NULL) {
559 xmlGenericError(xmlGenericErrorContext,
560 "xmlXPtrLocationSetCreate: out of memory\n");
561 return(NULL);
562 }
563 memset(ret->locTab, 0 ,
564 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
565 ret->locMax = XML_RANGESET_DEFAULT;
566 ret->locTab[ret->locNr++] = val;
567 }
568 return(ret);
569}
570
571/**
572 * xmlXPtrLocationSetAdd:
573 * @cur: the initial range set
574 * @val: a new xmlXPathObjectPtr
575 *
576 * add a new xmlXPathObjectPtr ot an existing LocationSet
577 * If the location already exist in the set @val is freed.
578 */
579void
580xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
581 int i;
582
583 if (val == NULL) return;
584
585 /*
586 * check against doublons
587 */
588 for (i = 0;i < cur->locNr;i++) {
589 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
590 xmlXPathFreeObject(val);
591 return;
592 }
593 }
594
595 /*
596 * grow the locTab if needed
597 */
598 if (cur->locMax == 0) {
599 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
600 sizeof(xmlXPathObjectPtr));
601 if (cur->locTab == NULL) {
602 xmlGenericError(xmlGenericErrorContext,
603 "xmlXPtrLocationSetAdd: out of memory\n");
604 return;
605 }
606 memset(cur->locTab, 0 ,
607 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
608 cur->locMax = XML_RANGESET_DEFAULT;
609 } else if (cur->locNr == cur->locMax) {
610 xmlXPathObjectPtr *temp;
611
612 cur->locMax *= 2;
613 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
614 sizeof(xmlXPathObjectPtr));
615 if (temp == NULL) {
616 xmlGenericError(xmlGenericErrorContext,
617 "xmlXPtrLocationSetAdd: out of memory\n");
618 return;
619 }
620 cur->locTab = temp;
621 }
622 cur->locTab[cur->locNr++] = val;
623}
624
625/**
626 * xmlXPtrLocationSetMerge:
627 * @val1: the first LocationSet
628 * @val2: the second LocationSet
629 *
630 * Merges two rangesets, all ranges from @val2 are added to @val1
631 *
632 * Returns val1 once extended or NULL in case of error.
633 */
634xmlLocationSetPtr
635xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
636 int i;
637
638 if (val1 == NULL) return(NULL);
639 if (val2 == NULL) return(val1);
640
641 /*
642 * !!!!! this can be optimized a lot, knowing that both
643 * val1 and val2 already have unicity of their values.
644 */
645
646 for (i = 0;i < val2->locNr;i++)
647 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
648
649 return(val1);
650}
651
652/**
653 * xmlXPtrLocationSetDel:
654 * @cur: the initial range set
655 * @val: an xmlXPathObjectPtr
656 *
657 * Removes an xmlXPathObjectPtr from an existing LocationSet
658 */
659void
660xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
661 int i;
662
663 if (cur == NULL) return;
664 if (val == NULL) return;
665
666 /*
667 * check against doublons
668 */
669 for (i = 0;i < cur->locNr;i++)
670 if (cur->locTab[i] == val) break;
671
672 if (i >= cur->locNr) {
673#ifdef DEBUG
674 xmlGenericError(xmlGenericErrorContext,
675 "xmlXPtrLocationSetDel: Range %s wasn't found in RangeList\n",
676 val->name);
677#endif
678 return;
679 }
680 cur->locNr--;
681 for (;i < cur->locNr;i++)
682 cur->locTab[i] = cur->locTab[i + 1];
683 cur->locTab[cur->locNr] = NULL;
684}
685
686/**
687 * xmlXPtrLocationSetRemove:
688 * @cur: the initial range set
689 * @val: the index to remove
690 *
691 * Removes an entry from an existing LocationSet list.
692 */
693void
694xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
695 if (cur == NULL) return;
696 if (val >= cur->locNr) return;
697 cur->locNr--;
698 for (;val < cur->locNr;val++)
699 cur->locTab[val] = cur->locTab[val + 1];
700 cur->locTab[cur->locNr] = NULL;
701}
702
703/**
704 * xmlXPtrFreeLocationSet:
705 * @obj: the xmlLocationSetPtr to free
706 *
707 * Free the LocationSet compound (not the actual ranges !).
708 */
709void
710xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
711 int i;
712
713 if (obj == NULL) return;
714 if (obj->locTab != NULL) {
715 for (i = 0;i < obj->locNr; i++) {
716 xmlXPathFreeObject(obj->locTab[i]);
717 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000718 MEM_CLEANUP(obj->locTab,
Owen Taylor3473f882001-02-23 17:55:21 +0000719 (size_t) sizeof(xmlXPathObjectPtr) * obj->locMax);
Owen Taylor3473f882001-02-23 17:55:21 +0000720 xmlFree(obj->locTab);
721 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000722 MEM_CLEANUP(obj, (size_t) sizeof(xmlLocationSet));
Owen Taylor3473f882001-02-23 17:55:21 +0000723 xmlFree(obj);
724}
725
726/**
727 * xmlXPtrNewLocationSetNodes:
728 * @start: the start NodePtr value
729 * @end: the end NodePtr value or NULL
730 *
731 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
732 * it with the single range made of the two nodes @start and @end
733 *
734 * Returns the newly created object.
735 */
736xmlXPathObjectPtr
737xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
738 xmlXPathObjectPtr ret;
739
740 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
741 if (ret == NULL) {
742 xmlGenericError(xmlGenericErrorContext,
743 "xmlXPtrNewLocationSetNodes: out of memory\n");
744 return(NULL);
745 }
746 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
747 ret->type = XPATH_LOCATIONSET;
748 if (end == NULL)
749 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
750 else
751 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
752 return(ret);
753}
754
755/**
756 * xmlXPtrNewLocationSetNodeSet:
757 * @set: a node set
758 *
759 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
760 * it with all the nodes from @set
761 *
762 * Returns the newly created object.
763 */
764xmlXPathObjectPtr
765xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
766 xmlXPathObjectPtr ret;
767
768 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
769 if (ret == NULL) {
770 xmlGenericError(xmlGenericErrorContext,
771 "xmlXPtrNewLocationSetNodes: out of memory\n");
772 return(NULL);
773 }
774 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
775 ret->type = XPATH_LOCATIONSET;
776 if (set != NULL) {
777 int i;
778 xmlLocationSetPtr newset;
779
780 newset = xmlXPtrLocationSetCreate(NULL);
781 if (newset == NULL)
782 return(ret);
783
784 for (i = 0;i < set->nodeNr;i++)
785 xmlXPtrLocationSetAdd(newset,
786 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
787
788 ret->user = (void *) newset;
789 }
790 return(ret);
791}
792
793/**
794 * xmlXPtrWrapLocationSet:
795 * @val: the LocationSet value
796 *
797 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
798 *
799 * Returns the newly created object.
800 */
801xmlXPathObjectPtr
802xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
803 xmlXPathObjectPtr ret;
804
805 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
806 if (ret == NULL) {
807 xmlGenericError(xmlGenericErrorContext,
808 "xmlXPtrWrapLocationSet: out of memory\n");
809 return(NULL);
810 }
811 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
812 ret->type = XPATH_LOCATIONSET;
813 ret->user = (void *) val;
814 return(ret);
815}
816
817/************************************************************************
818 * *
819 * The parser *
820 * *
821 ************************************************************************/
822
823/*
824 * Macros for accessing the content. Those should be used only by the parser,
825 * and not exported.
826 *
827 * Dirty macros, i.e. one need to make assumption on the context to use them
828 *
829 * CUR_PTR return the current pointer to the xmlChar to be parsed.
830 * CUR returns the current xmlChar value, i.e. a 8 bit value
831 * in ISO-Latin or UTF-8.
832 * This should be used internally by the parser
833 * only to compare to ASCII values otherwise it would break when
834 * running with UTF-8 encoding.
835 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
836 * to compare on ASCII based substring.
837 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
838 * strings within the parser.
839 * CURRENT Returns the current char value, with the full decoding of
840 * UTF-8 if we are using this mode. It returns an int.
841 * NEXT Skip to the next character, this does the proper decoding
842 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
843 * It returns the pointer to the current xmlChar.
844 */
845
846#define CUR (*ctxt->cur)
847#define SKIP(val) ctxt->cur += (val)
848#define NXT(val) ctxt->cur[(val)]
849#define CUR_PTR ctxt->cur
850
851#define SKIP_BLANKS \
852 while (IS_BLANK(*(ctxt->cur))) NEXT
853
854#define CURRENT (*ctxt->cur)
855#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
856
857/*
858 * xmlXPtrGetChildNo:
859 * @ctxt: the XPointer Parser context
860 * @index: the child number
861 *
862 * Move the current node of the nodeset on the stack to the
863 * given child if found
864 */
865void
866xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int index) {
867 xmlNodePtr cur = NULL;
868 xmlXPathObjectPtr obj;
869 xmlNodeSetPtr oldset;
870
871 CHECK_TYPE(XPATH_NODESET);
872 obj = valuePop(ctxt);
873 oldset = obj->nodesetval;
874 if ((index <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
875 xmlXPathFreeObject(obj);
876 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
877 return;
878 }
879 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], index);
880 if (cur == NULL) {
881 xmlXPathFreeObject(obj);
882 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
883 return;
884 }
885 oldset->nodeTab[0] = cur;
886 valuePush(ctxt, obj);
887}
888
889/**
890 * xmlXPtrEvalXPtrPart:
891 * @ctxt: the XPointer Parser context
892 * @name: the preparsed Scheme for the XPtrPart
893 *
894 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
895 * | Scheme '(' SchemeSpecificExpr ')'
896 *
897 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
898 *
899 * SchemeSpecificExpr ::= StringWithBalancedParens
900 *
901 * StringWithBalancedParens ::=
902 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
903 * [VC: Parenthesis escaping]
904 *
905 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
906 *
907 * VC: Parenthesis escaping:
908 * The end of an XPointer part is signaled by the right parenthesis ")"
909 * character that is balanced with the left parenthesis "(" character
910 * that began the part. Any unbalanced parenthesis character inside the
911 * expression, even within literals, must be escaped with a circumflex (^)
912 * character preceding it. If the expression contains any literal
913 * occurrences of the circumflex, each must be escaped with an additional
914 * circumflex (that is, ^^). If the unescaped parentheses in the expression
915 * are not balanced, a syntax error results.
916 *
917 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
918 * string and if the scheme is 'xpointer' it will call the XPath interprter.
919 *
920 * TODO: there is no new scheme registration mechanism
921 */
922
923void
924xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
925 xmlChar *buffer, *cur;
926 int len;
927 int level;
928
929 if (name == NULL)
930 name = xmlXPathParseName(ctxt);
931 if (name == NULL)
932 XP_ERROR(XPATH_EXPR_ERROR);
933
934 if (CUR != '(')
935 XP_ERROR(XPATH_EXPR_ERROR);
936 NEXT;
937 level = 1;
938
939 len = xmlStrlen(ctxt->cur);
940 len++;
941 buffer = (xmlChar *) xmlMalloc(len * sizeof (xmlChar));
942 if (buffer == NULL) {
943 xmlGenericError(xmlGenericErrorContext,
944 "xmlXPtrEvalXPtrPart: out of memory\n");
945 return;
946 }
947
948 cur = buffer;
949 while (CUR != 0) {
950 if (CUR == ')') {
951 level--;
952 if (level == 0) {
953 NEXT;
954 break;
955 }
956 *cur++ = CUR;
957 } else if (CUR == '(') {
958 level++;
959 *cur++ = CUR;
960 } else if (CUR == '^') {
961 NEXT;
962 if ((CUR == ')') || (CUR == '(') || (CUR == '^')) {
963 *cur++ = CUR;
964 } else {
965 *cur++ = '^';
966 *cur++ = CUR;
967 }
968 } else {
969 *cur++ = CUR;
970 }
971 NEXT;
972 }
973 *cur = 0;
974
975 if ((level != 0) && (CUR == 0)) {
976 xmlFree(buffer);
977 XP_ERROR(XPTR_SYNTAX_ERROR);
978 }
979
980 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
981 const xmlChar *left = CUR_PTR;
982
983 CUR_PTR = buffer;
984 xmlXPathRoot(ctxt);
985 xmlXPathEvalExpr(ctxt);
986 CUR_PTR=left;
987#ifdef XPTR_XMLNS_SCHEME
988 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
989 const xmlChar *left = CUR_PTR;
990 xmlChar *prefix;
991 xmlChar *URI;
992 xmlURIPtr value;
993
994 CUR_PTR = buffer;
995 prefix = xmlXPathParseNCName(ctxt);
996 if (prefix == NULL) {
997 xmlFree(buffer);
998 xmlFree(name);
999 XP_ERROR(XPTR_SYNTAX_ERROR);
1000 }
1001 SKIP_BLANKS;
1002 if (CUR != '=') {
1003 xmlFree(prefix);
1004 xmlFree(buffer);
1005 xmlFree(name);
1006 XP_ERROR(XPTR_SYNTAX_ERROR);
1007 }
1008 NEXT;
1009 SKIP_BLANKS;
1010 /* @@ check escaping in the XPointer WD */
1011
1012 value = xmlParseURI((const char *)ctxt->cur);
1013 if (value == NULL) {
1014 xmlFree(prefix);
1015 xmlFree(buffer);
1016 xmlFree(name);
1017 XP_ERROR(XPTR_SYNTAX_ERROR);
1018 }
1019 URI = xmlSaveUri(value);
1020 xmlFreeURI(value);
1021 if (URI == NULL) {
1022 xmlFree(prefix);
1023 xmlFree(buffer);
1024 xmlFree(name);
1025 XP_ERROR(XPATH_MEMORY_ERROR);
1026 }
1027
1028 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1029 CUR_PTR = left;
1030#endif /* XPTR_XMLNS_SCHEME */
1031 } else {
1032 xmlGenericError(xmlGenericErrorContext,
1033 "unsupported scheme '%s'\n", name);
1034 }
1035 xmlFree(buffer);
1036 xmlFree(name);
1037}
1038
1039/**
1040 * xmlXPtrEvalFullXPtr:
1041 * @ctxt: the XPointer Parser context
1042 * @name: the preparsed Scheme for the first XPtrPart
1043 *
1044 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1045 *
1046 * As the specs says:
1047 * -----------
1048 * When multiple XPtrParts are provided, they must be evaluated in
1049 * left-to-right order. If evaluation of one part fails, the nexti
1050 * is evaluated. The following conditions cause XPointer part failure:
1051 *
1052 * - An unknown scheme
1053 * - A scheme that does not locate any sub-resource present in the resource
1054 * - A scheme that is not applicable to the media type of the resource
1055 *
1056 * The XPointer application must consume a failed XPointer part and
1057 * attempt to evaluate the next one, if any. The result of the first
1058 * XPointer part whose evaluation succeeds is taken to be the fragment
1059 * located by the XPointer as a whole. If all the parts fail, the result
1060 * for the XPointer as a whole is a sub-resource error.
1061 * -----------
1062 *
1063 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1064 * expressions or other shemes.
1065 */
1066void
1067xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1068 if (name == NULL)
1069 name = xmlXPathParseName(ctxt);
1070 if (name == NULL)
1071 XP_ERROR(XPATH_EXPR_ERROR);
1072 while (name != NULL) {
1073 xmlXPtrEvalXPtrPart(ctxt, name);
1074
1075 /* in case of syntax error, break here */
1076 if (ctxt->error != XPATH_EXPRESSION_OK)
1077 return;
1078
1079 /*
1080 * If the returned value is a non-empty nodeset
1081 * or location set, return here.
1082 */
1083 if (ctxt->value != NULL) {
1084 xmlXPathObjectPtr obj = ctxt->value;
1085
1086 switch (obj->type) {
1087 case XPATH_LOCATIONSET: {
1088 xmlLocationSetPtr loc = ctxt->value->user;
1089 if ((loc != NULL) && (loc->locNr > 0))
1090 return;
1091 break;
1092 }
1093 case XPATH_NODESET: {
1094 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1095 if ((loc != NULL) && (loc->nodeNr > 0))
1096 return;
1097 break;
1098 }
1099 default:
1100 break;
1101 }
1102
1103 /*
1104 * Evaluating to improper values is equivalent to
1105 * a sub-resource error, clean-up the stack
1106 */
1107 do {
1108 obj = valuePop(ctxt);
1109 if (obj != NULL) {
1110 xmlXPathFreeObject(obj);
1111 }
1112 } while (obj != NULL);
1113 }
1114
1115 /*
1116 * Is there another XPoointer part.
1117 */
1118 SKIP_BLANKS;
1119 name = xmlXPathParseName(ctxt);
1120 }
1121}
1122
1123/**
1124 * xmlXPtrEvalChildSeq:
1125 * @ctxt: the XPointer Parser context
1126 * @name: a possible ID name of the child sequence
1127 *
1128 * ChildSeq ::= '/1' ('/' [0-9]*)*
1129 * | Name ('/' [0-9]*)+
1130 *
1131 * Parse and evaluate a Child Sequence. This routine also handle the
1132 * case of a Bare Name used to get a document ID.
1133 */
1134void
1135xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1136 /*
1137 * XPointer don't allow by syntax to adress in mutirooted trees
1138 * this might prove useful in some cases, warn about it.
1139 */
1140 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
1141 xmlGenericError(xmlGenericErrorContext,
1142 "warning: ChildSeq not starting by /1\n");
1143 }
1144
1145 if (name != NULL) {
1146 valuePush(ctxt, xmlXPathNewString(name));
1147 xmlFree(name);
1148 xmlXPathIdFunction(ctxt, 1);
1149 CHECK_ERROR;
1150 }
1151
1152 while (CUR == '/') {
1153 int child = 0;
1154 NEXT;
1155
1156 while ((CUR >= '0') && (CUR <= '9')) {
1157 child = child * 10 + (CUR - '0');
1158 NEXT;
1159 }
1160 xmlXPtrGetChildNo(ctxt, child);
1161 }
1162}
1163
1164
1165/**
1166 * xmlXPtrEvalXPointer:
1167 * @ctxt: the XPointer Parser context
1168 *
1169 * XPointer ::= Name
1170 * | ChildSeq
1171 * | FullXPtr
1172 *
1173 * Parse and evaluate an XPointer
1174 */
1175void
1176xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
1177 SKIP_BLANKS;
1178 if (CUR == '/') {
1179 xmlXPathRoot(ctxt);
1180 xmlXPtrEvalChildSeq(ctxt, NULL);
1181 } else {
1182 xmlChar *name;
1183
1184 name = xmlXPathParseName(ctxt);
1185 if (name == NULL)
1186 XP_ERROR(XPATH_EXPR_ERROR);
1187 if (CUR == '(') {
1188 xmlXPtrEvalFullXPtr(ctxt, name);
1189 /* Short evaluation */
1190 return;
1191 } else {
1192 /* this handle both Bare Names and Child Sequences */
1193 xmlXPtrEvalChildSeq(ctxt, name);
1194 }
1195 }
1196 SKIP_BLANKS;
1197 if (CUR != 0)
1198 XP_ERROR(XPATH_EXPR_ERROR);
1199}
1200
1201
1202/************************************************************************
1203 * *
1204 * General routines *
1205 * *
1206 ************************************************************************/
1207
1208void xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs);
1209void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1210void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1211void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1212void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1213void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1214void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1215void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1216
1217/**
1218 * xmlXPtrNewContext:
1219 * @doc: the XML document
1220 * @here: the node that directly contains the XPointer being evaluated or NULL
1221 * @origin: the element from which a user or program initiated traversal of
1222 * the link, or NULL.
1223 *
1224 * Create a new XPointer context
1225 *
1226 * Returns the xmlXPathContext just allocated.
1227 */
1228xmlXPathContextPtr
1229xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1230 xmlXPathContextPtr ret;
1231
1232 ret = xmlXPathNewContext(doc);
1233 if (ret == NULL)
1234 return(ret);
1235 ret->xptr = 1;
1236 ret->here = here;
1237 ret->origin = origin;
1238
1239 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1240 xmlXPtrRangeToFunction);
1241 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1242 xmlXPtrRangeFunction);
1243 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1244 xmlXPtrRangeInsideFunction);
1245 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1246 xmlXPtrStringRangeFunction);
1247 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1248 xmlXPtrStartPointFunction);
1249 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1250 xmlXPtrEndPointFunction);
1251 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1252 xmlXPtrHereFunction);
1253 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1254 xmlXPtrOriginFunction);
1255
1256 return(ret);
1257}
1258
1259/**
1260 * xmlXPtrEval:
1261 * @str: the XPointer expression
1262 * @ctx: the XPointer context
1263 *
1264 * Evaluate the XPath Location Path in the given context.
1265 *
1266 * Returns the xmlXPathObjectPtr resulting from the eveluation or NULL.
1267 * the caller has to free the object.
1268 */
1269xmlXPathObjectPtr
1270xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1271 xmlXPathParserContextPtr ctxt;
1272 xmlXPathObjectPtr res = NULL, tmp;
1273 xmlXPathObjectPtr init = NULL;
1274 int stack = 0;
1275
1276 xmlXPathInit();
1277
1278 if ((ctx == NULL) || (str == NULL))
1279 return(NULL);
1280
1281 ctxt = xmlXPathNewParserContext(str, ctx);
1282 /* TAG:9999
1283 if (ctx->node != NULL) {
1284 init = xmlXPathNewNodeSet(ctx->node);
1285 valuePush(ctxt, init);
1286 }
1287 */
1288 xmlXPtrEvalXPointer(ctxt);
1289
1290 if ((ctxt->value != NULL) &&
1291 (ctxt->value->type != XPATH_NODESET) &&
1292 (ctxt->value->type != XPATH_LOCATIONSET)) {
1293 xmlGenericError(xmlGenericErrorContext,
1294 "xmlXPtrEval: evaluation failed to return a node set\n");
1295 } else {
1296 res = valuePop(ctxt);
1297 }
1298
1299 do {
1300 tmp = valuePop(ctxt);
1301 if (tmp != NULL) {
1302 if (tmp != init) {
1303 if (tmp->type == XPATH_NODESET) {
1304 /*
1305 * Evaluation may push a root nodeset which is unused
1306 */
1307 xmlNodeSetPtr set;
1308 set = tmp->nodesetval;
1309 if ((set->nodeNr != 1) ||
1310 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1311 stack++;
1312 } else
1313 stack++;
1314 }
1315 xmlXPathFreeObject(tmp);
1316 }
1317 } while (tmp != NULL);
1318 if (stack != 0) {
1319 xmlGenericError(xmlGenericErrorContext,
1320 "xmlXPtrEval: %d object left on the stack\n",
1321 stack);
1322 }
1323 if (ctxt->error != XPATH_EXPRESSION_OK) {
1324 xmlXPathFreeObject(res);
1325 res = NULL;
1326 }
1327
1328 xmlXPathFreeParserContext(ctxt);
1329 return(res);
1330}
1331
1332/**
1333 * xmlXPtrBuildRangeNodeList:
1334 * @range: a range object
1335 *
1336 * Build a node list tree copy of the range
1337 *
1338 * Returns an xmlNodePtr list or NULL.
1339 * the caller has to free the node tree.
1340 */
1341xmlNodePtr
1342xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1343 /* pointers to generated nodes */
1344 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1345 /* pointers to traversal nodes */
1346 xmlNodePtr start, cur, end;
1347 int index, index2;
1348
1349 if (range == NULL)
1350 return(NULL);
1351 if (range->type != XPATH_RANGE)
1352 return(NULL);
1353 start = (xmlNodePtr) range->user;
1354
1355 if (start == NULL)
1356 return(NULL);
1357 end = range->user2;
1358 if (end == NULL)
1359 return(xmlCopyNode(start, 1));
1360
1361 cur = start;
1362 index = range->index;
1363 index2 = range->index2;
1364 while (cur != NULL) {
1365 if (cur == end) {
1366 if (cur->type == XML_TEXT_NODE) {
1367 const xmlChar *content = cur->content;
1368 int len;
1369
1370 if (content == NULL) {
1371 tmp = xmlNewTextLen(NULL, 0);
1372 } else {
1373 len = index2;
1374 if ((cur == start) && (index > 1)) {
1375 content += (index - 1);
1376 len -= (index - 1);
1377 index = 0;
1378 } else {
1379 len = index2;
1380 }
1381 tmp = xmlNewTextLen(content, len);
1382 }
1383 /* single sub text node selection */
1384 if (list == NULL)
1385 return(tmp);
1386 /* prune and return full set */
1387 if (last != NULL)
1388 xmlAddNextSibling(last, tmp);
1389 else
1390 xmlAddChild(parent, tmp);
1391 return(list);
1392 } else {
1393 tmp = xmlCopyNode(cur, 0);
1394 if (list == NULL)
1395 list = tmp;
1396 else {
1397 if (last != NULL)
1398 xmlAddNextSibling(last, tmp);
1399 else
1400 xmlAddChild(parent, tmp);
1401 }
1402 last = NULL;
1403 parent = tmp;
1404
1405 if (index2 > 1) {
1406 end = xmlXPtrGetNthChild(cur, index2 - 1);
1407 index2 = 0;
1408 }
1409 if ((cur == start) && (index > 1)) {
1410 cur = xmlXPtrGetNthChild(cur, index - 1);
1411 index = 0;
1412 } else {
1413 cur = cur->children;
1414 }
1415 /*
1416 * Now gather the remaining nodes from cur to end
1417 */
1418 continue; /* while */
1419 }
1420 } else if ((cur == start) &&
1421 (list == NULL) /* looks superfluous but ... */ ) {
1422 if (cur->type == XML_TEXT_NODE) {
1423 const xmlChar *content = cur->content;
1424
1425 if (content == NULL) {
1426 tmp = xmlNewTextLen(NULL, 0);
1427 } else {
1428 if (index > 1) {
1429 content += (index - 1);
1430 }
1431 tmp = xmlNewText(content);
1432 }
1433 last = list = tmp;
1434 } else {
1435 if ((cur == start) && (index > 1)) {
1436 tmp = xmlCopyNode(cur, 0);
1437 list = tmp;
1438 parent = tmp;
1439 last = NULL;
1440 cur = xmlXPtrGetNthChild(cur, index - 1);
1441 index = 0;
1442 /*
1443 * Now gather the remaining nodes from cur to end
1444 */
1445 continue; /* while */
1446 }
1447 tmp = xmlCopyNode(cur, 1);
1448 list = tmp;
1449 parent = NULL;
1450 last = tmp;
1451 }
1452 } else {
1453 tmp = NULL;
1454 switch (cur->type) {
1455 case XML_DTD_NODE:
1456 case XML_ELEMENT_DECL:
1457 case XML_ATTRIBUTE_DECL:
1458 case XML_ENTITY_NODE:
1459 /* Do not copy DTD informations */
1460 break;
1461 case XML_ENTITY_DECL:
1462 TODO /* handle csossing entities -> stack needed */
1463 break;
1464 case XML_XINCLUDE_START:
1465 case XML_XINCLUDE_END:
1466 /* don't consider it part of the tree content */
1467 break;
1468 case XML_ATTRIBUTE_NODE:
1469 /* Humm, should not happen ! */
1470 STRANGE
1471 break;
1472 default:
1473 tmp = xmlCopyNode(cur, 1);
1474 break;
1475 }
1476 if (tmp != NULL) {
1477 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1478 STRANGE
1479 return(NULL);
1480 }
1481 if (last != NULL)
1482 xmlAddNextSibling(last, tmp);
1483 else {
1484 xmlAddChild(parent, tmp);
1485 last = tmp;
1486 }
1487 }
1488 }
1489 /*
1490 * Skip to next node in document order
1491 */
1492 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1493 STRANGE
1494 return(NULL);
1495 }
1496 cur = xmlXPtrAdvanceNode(cur);
1497 }
1498 return(list);
1499}
1500
1501/**
1502 * xmlXPtrBuildNodeList:
1503 * @obj: the XPointer result from the evaluation.
1504 *
1505 * Build a node list tree copy of the XPointer result.
1506 *
1507 * Returns an xmlNodePtr list or NULL.
1508 * the caller has to free the node tree.
1509 */
1510xmlNodePtr
1511xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1512 xmlNodePtr list = NULL, last = NULL;
1513 int i;
1514
1515 if (obj == NULL)
1516 return(NULL);
1517 switch (obj->type) {
1518 case XPATH_NODESET: {
1519 xmlNodeSetPtr set = obj->nodesetval;
1520 if (set == NULL)
1521 return(NULL);
1522 for (i = 0;i < set->nodeNr;i++) {
1523 if (last == NULL)
1524 list = last = xmlCopyNode(set->nodeTab[i], 1);
1525 else {
1526 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1527 if (last->next != NULL)
1528 last = last->next;
1529 }
1530 }
1531 break;
1532 }
1533 case XPATH_LOCATIONSET: {
1534 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1535 if (set == NULL)
1536 return(NULL);
1537 for (i = 0;i < set->locNr;i++) {
1538 if (last == NULL)
1539 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1540 else
1541 xmlAddNextSibling(last,
1542 xmlXPtrBuildNodeList(set->locTab[i]));
1543 if (last != NULL) {
1544 while (last->next != NULL)
1545 last = last->next;
1546 }
1547 }
1548 break;
1549 }
1550 case XPATH_RANGE:
1551 return(xmlXPtrBuildRangeNodeList(obj));
1552 case XPATH_POINT:
1553 return(xmlCopyNode(obj->user, 0));
1554 default:
1555 break;
1556 }
1557 return(list);
1558}
1559
1560/************************************************************************
1561 * *
1562 * XPointer functions *
1563 * *
1564 ************************************************************************/
1565
1566/**
1567 * xmlXPtrNbLocChildren:
1568 * @node: an xmlNodePtr
1569 *
1570 * Count the number of location children of @node or the lenght of the
1571 * string value in case of text/PI/Comments nodes
1572 *
1573 * Returns the number of location children
1574 */
1575int
1576xmlXPtrNbLocChildren(xmlNodePtr node) {
1577 int ret = 0;
1578 if (node == NULL)
1579 return(-1);
1580 switch (node->type) {
1581 case XML_HTML_DOCUMENT_NODE:
1582 case XML_DOCUMENT_NODE:
1583 case XML_ELEMENT_NODE:
1584 node = node->children;
1585 while (node != NULL) {
1586 if (node->type == XML_ELEMENT_NODE)
1587 ret++;
1588 node = node->next;
1589 }
1590 break;
1591 case XML_ATTRIBUTE_NODE:
1592 return(-1);
1593
1594 case XML_PI_NODE:
1595 case XML_COMMENT_NODE:
1596 case XML_TEXT_NODE:
1597 case XML_CDATA_SECTION_NODE:
1598 case XML_ENTITY_REF_NODE:
1599#ifndef XML_USE_BUFFER_CONTENT
1600 ret = xmlStrlen(node->content);
1601#else
1602 ret = xmlBufferLength(node->content);
1603#endif
1604 break;
1605 default:
1606 return(-1);
1607 }
1608 return(ret);
1609}
1610
1611/**
1612 * xmlXPtrHereFunction:
1613 * @ctxt: the XPointer Parser context
1614 *
1615 * Function implementing here() operation
1616 * as described in 5.4.3
1617 */
1618void
1619xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1620 if (ctxt->context->here == NULL)
1621 XP_ERROR(XPTR_SYNTAX_ERROR);
1622
1623 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1624}
1625
1626/**
1627 * xmlXPtrOriginFunction:
1628 * @ctxt: the XPointer Parser context
1629 *
1630 * Function implementing origin() operation
1631 * as described in 5.4.3
1632 */
1633void
1634xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1635 if (ctxt->context->origin == NULL)
1636 XP_ERROR(XPTR_SYNTAX_ERROR);
1637
1638 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1639}
1640
1641/**
1642 * xmlXPtrStartPointFunction:
1643 * @ctxt: the XPointer Parser context
1644 *
1645 * Function implementing start-point() operation
1646 * as described in 5.4.3
1647 * ----------------
1648 * location-set start-point(location-set)
1649 *
1650 * For each location x in the argument location-set, start-point adds a
1651 * location of type point to the result location-set. That point represents
1652 * the start point of location x and is determined by the following rules:
1653 *
1654 * - If x is of type point, the start point is x.
1655 * - If x is of type range, the start point is the start point of x.
1656 * - If x is of type root, element, text, comment, or processing instruction,
1657 * - the container node of the start point is x and the index is 0.
1658 * - If x is of type attribute or namespace, the function must signal a
1659 * syntax error.
1660 * ----------------
1661 *
1662 */
1663void
1664xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1665 xmlXPathObjectPtr tmp, obj, point;
1666 xmlLocationSetPtr newset = NULL;
1667 xmlLocationSetPtr oldset = NULL;
1668
1669 CHECK_ARITY(1);
1670 if ((ctxt->value == NULL) ||
1671 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1672 (ctxt->value->type != XPATH_NODESET)))
1673 XP_ERROR(XPATH_INVALID_TYPE)
1674
1675 obj = valuePop(ctxt);
1676 if (obj->type == XPATH_NODESET) {
1677 /*
1678 * First convert to a location set
1679 */
1680 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1681 xmlXPathFreeObject(obj);
1682 obj = tmp;
1683 }
1684
1685 newset = xmlXPtrLocationSetCreate(NULL);
1686 if (newset == NULL) {
1687 xmlXPathFreeObject(obj);
1688 XP_ERROR(XPATH_MEMORY_ERROR);
1689 }
1690 oldset = (xmlLocationSetPtr) obj->user;
1691 if (oldset != NULL) {
1692 int i;
1693
1694 for (i = 0; i < oldset->locNr; i++) {
1695 tmp = oldset->locTab[i];
1696 if (tmp == NULL)
1697 continue;
1698 point = NULL;
1699 switch (tmp->type) {
1700 case XPATH_POINT:
1701 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1702 break;
1703 case XPATH_RANGE: {
1704 xmlNodePtr node = tmp->user;
1705 if (node != NULL) {
1706 if (node->type == XML_ATTRIBUTE_NODE) {
1707 /* TODO: Namespace Nodes ??? */
1708 xmlXPathFreeObject(obj);
1709 xmlXPtrFreeLocationSet(newset);
1710 XP_ERROR(XPTR_SYNTAX_ERROR);
1711 }
1712 point = xmlXPtrNewPoint(node, tmp->index);
1713 }
1714 break;
1715 }
1716 default:
1717 /*** Should we raise an error ?
1718 xmlXPathFreeObject(obj);
1719 xmlXPathFreeObject(newset);
1720 XP_ERROR(XPATH_INVALID_TYPE)
1721 ***/
1722 break;
1723 }
1724 if (point != NULL)
1725 xmlXPtrLocationSetAdd(newset, point);
1726 }
1727 }
1728 xmlXPathFreeObject(obj);
1729 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1730}
1731
1732/**
1733 * xmlXPtrEndPointFunction:
1734 * @ctxt: the XPointer Parser context
1735 *
1736 * Function implementing end-point() operation
1737 * as described in 5.4.3
1738 * ----------------------------
1739 * location-set end-point(location-set)
1740 *
1741 * For each location x in the argument location-set, end-point adds a
1742 * location of type point to the result location-set. That point representsi
1743 * the end point of location x and is determined by the following rules:
1744 *
1745 * - If x is of type point, the resulting point is x.
1746 * - If x is of type range, the resulting point is the end point of x.
1747 * - If x is of type root or element, the container node of the resulting
1748 * point is x and the index is the number of location children of x.
1749 * - If x is of type text, comment, or processing instruction, the container
1750 * node of the resulting point is x and the index is the length of thei
1751 * string-value of x.
1752 * - If x is of type attribute or namespace, the function must signal a
1753 * syntax error.
1754 * ----------------------------
1755 */
1756void
1757xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1758 xmlXPathObjectPtr tmp, obj, point;
1759 xmlLocationSetPtr newset = NULL;
1760 xmlLocationSetPtr oldset = NULL;
1761
1762 CHECK_ARITY(1);
1763 if ((ctxt->value == NULL) ||
1764 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1765 (ctxt->value->type != XPATH_NODESET)))
1766 XP_ERROR(XPATH_INVALID_TYPE)
1767
1768 obj = valuePop(ctxt);
1769 if (obj->type == XPATH_NODESET) {
1770 /*
1771 * First convert to a location set
1772 */
1773 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1774 xmlXPathFreeObject(obj);
1775 obj = tmp;
1776 }
1777
1778 newset = xmlXPtrLocationSetCreate(NULL);
1779 oldset = (xmlLocationSetPtr) obj->user;
1780 if (oldset != NULL) {
1781 int i;
1782
1783 for (i = 0; i < oldset->locNr; i++) {
1784 tmp = oldset->locTab[i];
1785 if (tmp == NULL)
1786 continue;
1787 point = NULL;
1788 switch (tmp->type) {
1789 case XPATH_POINT:
1790 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1791 break;
1792 case XPATH_RANGE: {
1793 xmlNodePtr node = tmp->user2;
1794 if (node != NULL) {
1795 if (node->type == XML_ATTRIBUTE_NODE) {
1796 /* TODO: Namespace Nodes ??? */
1797 xmlXPathFreeObject(obj);
1798 xmlXPtrFreeLocationSet(newset);
1799 XP_ERROR(XPTR_SYNTAX_ERROR);
1800 }
1801 point = xmlXPtrNewPoint(node, tmp->index2);
1802 } else if (tmp->user == NULL) {
1803 point = xmlXPtrNewPoint(node,
1804 xmlXPtrNbLocChildren(node));
1805 }
1806 break;
1807 }
1808 default:
1809 /*** Should we raise an error ?
1810 xmlXPathFreeObject(obj);
1811 xmlXPathFreeObject(newset);
1812 XP_ERROR(XPATH_INVALID_TYPE)
1813 ***/
1814 break;
1815 }
1816 if (point != NULL)
1817 xmlXPtrLocationSetAdd(newset, point);
1818 }
1819 }
1820 xmlXPathFreeObject(obj);
1821 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1822}
1823
1824
1825/**
1826 * xmlXPtrCoveringRange:
1827 * @ctxt: the XPointer Parser context
1828 * @loc: the location for which the covering range must be computed
1829 *
1830 * A covering range is a range that wholly encompasses a location
1831 * Section 5.3.3. Covering Ranges for All Location Types
1832 * http://www.w3.org/TR/xptr#N2267
1833 *
1834 * Returns a new location or NULL in case of error
1835 */
1836xmlXPathObjectPtr
1837xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1838 if (loc == NULL)
1839 return(NULL);
1840 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1841 (ctxt->context->doc == NULL))
1842 return(NULL);
1843 switch (loc->type) {
1844 case XPATH_POINT:
1845 return(xmlXPtrNewRange(loc->user, loc->index,
1846 loc->user, loc->index));
1847 case XPATH_RANGE:
1848 if (loc->user2 != NULL) {
1849 return(xmlXPtrNewRange(loc->user, loc->index,
1850 loc->user2, loc->index2));
1851 } else {
1852 xmlNodePtr node = (xmlNodePtr) loc->user;
1853 if (node == (xmlNodePtr) ctxt->context->doc) {
1854 return(xmlXPtrNewRange(node, 0, node,
1855 xmlXPtrGetArity(node)));
1856 } else {
1857 switch (node->type) {
1858 case XML_ATTRIBUTE_NODE:
1859 /* !!! our model is slightly different than XPath */
1860 return(xmlXPtrNewRange(node, 0, node,
1861 xmlXPtrGetArity(node)));
1862 case XML_ELEMENT_NODE:
1863 case XML_TEXT_NODE:
1864 case XML_CDATA_SECTION_NODE:
1865 case XML_ENTITY_REF_NODE:
1866 case XML_PI_NODE:
1867 case XML_COMMENT_NODE:
1868 case XML_DOCUMENT_NODE:
1869 case XML_NOTATION_NODE:
1870 case XML_HTML_DOCUMENT_NODE: {
1871 int index = xmlXPtrGetIndex(node);
1872
1873 node = node->parent;
1874 return(xmlXPtrNewRange(node, index - 1,
1875 node, index + 1));
1876 }
1877 default:
1878 return(NULL);
1879 }
1880 }
1881 }
1882 default:
1883 TODO /* missed one case ??? */
1884 }
1885 return(NULL);
1886}
1887
1888/**
1889 * xmlXPtrRangeFunction:
1890 * @ctxt: the XPointer Parser context
1891 *
1892 * Function implementing the range() function 5.4.3
1893 * location-set range(location-set )
1894 *
1895 * The range function returns ranges covering the locations in
1896 * the argument location-set. For each location x in the argument
1897 * location-set, a range location representing the covering range of
1898 * x is added to the result location-set.
1899 */
1900void
1901xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1902 int i;
1903 xmlXPathObjectPtr set;
1904 xmlLocationSetPtr oldset;
1905 xmlLocationSetPtr newset;
1906
1907 CHECK_ARITY(1);
1908 if ((ctxt->value == NULL) ||
1909 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1910 (ctxt->value->type != XPATH_NODESET)))
1911 XP_ERROR(XPATH_INVALID_TYPE)
1912
1913 set = valuePop(ctxt);
1914 if (set->type == XPATH_NODESET) {
1915 xmlXPathObjectPtr tmp;
1916
1917 /*
1918 * First convert to a location set
1919 */
1920 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1921 xmlXPathFreeObject(set);
1922 set = tmp;
1923 }
1924 oldset = (xmlLocationSetPtr) set->user;
1925
1926 /*
1927 * The loop is to compute the covering range for each item and add it
1928 */
1929 newset = xmlXPtrLocationSetCreate(NULL);
1930 for (i = 0;i < oldset->locNr;i++) {
1931 xmlXPtrLocationSetAdd(newset,
1932 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
1933 }
1934
1935 /*
1936 * Save the new value and cleanup
1937 */
1938 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1939 xmlXPathFreeObject(set);
1940}
1941
1942/**
1943 * xmlXPtrInsideRange:
1944 * @ctxt: the XPointer Parser context
1945 * @loc: the location for which the inside range must be computed
1946 *
1947 * A inside range is a range described in the range-inside() description
1948 *
1949 * Returns a new location or NULL in case of error
1950 */
1951xmlXPathObjectPtr
1952xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1953 if (loc == NULL)
1954 return(NULL);
1955 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1956 (ctxt->context->doc == NULL))
1957 return(NULL);
1958 switch (loc->type) {
1959 case XPATH_POINT: {
1960 xmlNodePtr node = (xmlNodePtr) loc->user;
1961 switch (node->type) {
1962 case XML_PI_NODE:
1963 case XML_COMMENT_NODE:
1964 case XML_TEXT_NODE:
1965 case XML_CDATA_SECTION_NODE: {
1966 if (node->content == NULL) {
1967 return(xmlXPtrNewRange(node, 0, node, 0));
1968 } else {
1969#ifndef XML_USE_BUFFER_CONTENT
1970 return(xmlXPtrNewRange(node, 0, node,
1971 xmlStrlen(node->content)));
1972#else
1973 return(xmlXPtrNewRange(node, 0, node,
1974 xmlBufferLength(node->content)));
1975#endif
1976 }
1977 }
1978 case XML_ATTRIBUTE_NODE:
1979 case XML_ELEMENT_NODE:
1980 case XML_ENTITY_REF_NODE:
1981 case XML_DOCUMENT_NODE:
1982 case XML_NOTATION_NODE:
1983 case XML_HTML_DOCUMENT_NODE: {
1984 return(xmlXPtrNewRange(node, 0, node,
1985 xmlXPtrGetArity(node)));
1986 }
1987 default:
1988 return(NULL);
1989 }
1990 return(NULL);
1991 }
1992 case XPATH_RANGE: {
1993 xmlNodePtr node = (xmlNodePtr) loc->user;
1994 if (loc->user2 != NULL) {
1995 return(xmlXPtrNewRange(node, loc->index,
1996 loc->user2, loc->index2));
1997 } else {
1998 switch (node->type) {
1999 case XML_PI_NODE:
2000 case XML_COMMENT_NODE:
2001 case XML_TEXT_NODE:
2002 case XML_CDATA_SECTION_NODE: {
2003 if (node->content == NULL) {
2004 return(xmlXPtrNewRange(node, 0, node, 0));
2005 } else {
2006#ifndef XML_USE_BUFFER_CONTENT
2007 return(xmlXPtrNewRange(node, 0, node,
2008 xmlStrlen(node->content)));
2009#else
2010 return(xmlXPtrNewRange(node, 0, node,
2011 xmlBufferLength(node->content)));
2012#endif
2013 }
2014 }
2015 case XML_ATTRIBUTE_NODE:
2016 case XML_ELEMENT_NODE:
2017 case XML_ENTITY_REF_NODE:
2018 case XML_DOCUMENT_NODE:
2019 case XML_NOTATION_NODE:
2020 case XML_HTML_DOCUMENT_NODE: {
2021 return(xmlXPtrNewRange(node, 0, node,
2022 xmlXPtrGetArity(node)));
2023 }
2024 default:
2025 return(NULL);
2026 }
2027 return(NULL);
2028 }
2029 }
2030 default:
2031 TODO /* missed one case ??? */
2032 }
2033 return(NULL);
2034}
2035
2036/**
2037 * xmlXPtrRangeInsideFunction:
2038 * @ctxt: the XPointer Parser context
2039 *
2040 * Function implementing the range-inside() function 5.4.3
2041 * location-set range-inside(location-set )
2042 *
2043 * The range-inside function returns ranges covering the contents of
2044 * the locations in the argument location-set. For each location x in
2045 * the argument location-set, a range location is added to the result
2046 * location-set. If x is a range location, then x is added to the
2047 * result location-set. If x is not a range location, then x is used
2048 * as the container location of the start and end points of the range
2049 * location to be added; the index of the start point of the range is
2050 * zero; if the end point is a character point then its index is the
2051 * length of the string-value of x, and otherwise is the number of
2052 * location children of x.
2053 *
2054 */
2055void
2056xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2057 int i;
2058 xmlXPathObjectPtr set;
2059 xmlLocationSetPtr oldset;
2060 xmlLocationSetPtr newset;
2061
2062 CHECK_ARITY(1);
2063 if ((ctxt->value == NULL) ||
2064 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2065 (ctxt->value->type != XPATH_NODESET)))
2066 XP_ERROR(XPATH_INVALID_TYPE)
2067
2068 set = valuePop(ctxt);
2069 if (set->type == XPATH_NODESET) {
2070 xmlXPathObjectPtr tmp;
2071
2072 /*
2073 * First convert to a location set
2074 */
2075 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2076 xmlXPathFreeObject(set);
2077 set = tmp;
2078 }
2079 oldset = (xmlLocationSetPtr) set->user;
2080
2081 /*
2082 * The loop is to compute the covering range for each item and add it
2083 */
2084 newset = xmlXPtrLocationSetCreate(NULL);
2085 for (i = 0;i < oldset->locNr;i++) {
2086 xmlXPtrLocationSetAdd(newset,
2087 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2088 }
2089
2090 /*
2091 * Save the new value and cleanup
2092 */
2093 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2094 xmlXPathFreeObject(set);
2095}
2096
2097/**
2098 * xmlXPtrRangeToFunction:
2099 * @ctxt: the XPointer Parser context
2100 *
2101 * Implement the range-to() XPointer function
2102 */
2103void
2104xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2105 xmlXPathObjectPtr range;
2106 const xmlChar *cur;
2107 xmlXPathObjectPtr res, obj;
2108 xmlXPathObjectPtr tmp;
2109 xmlLocationSetPtr newset = NULL;
2110 xmlNodeSetPtr oldset;
2111 int i;
2112
2113 CHECK_ARITY(1);
2114 /*
2115 * Save the expression pointer since we will have to evaluate
2116 * it multiple times. Initialize the new set.
2117 */
2118 CHECK_TYPE(XPATH_NODESET);
2119 obj = valuePop(ctxt);
2120 oldset = obj->nodesetval;
2121 ctxt->context->node = NULL;
2122
2123 cur = ctxt->cur;
2124 newset = xmlXPtrLocationSetCreate(NULL);
2125
2126 for (i = 0; i < oldset->nodeNr; i++) {
2127 ctxt->cur = cur;
2128
2129 /*
2130 * Run the evaluation with a node list made of a single item
2131 * in the nodeset.
2132 */
2133 ctxt->context->node = oldset->nodeTab[i];
2134 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2135 valuePush(ctxt, tmp);
2136
2137 xmlXPathEvalExpr(ctxt);
2138 CHECK_ERROR;
2139
2140 /*
2141 * The result of the evaluation need to be tested to
2142 * decided whether the filter succeeded or not
2143 */
2144 res = valuePop(ctxt);
2145 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2146 if (range != NULL) {
2147 xmlXPtrLocationSetAdd(newset, range);
2148 }
2149
2150 /*
2151 * Cleanup
2152 */
2153 if (res != NULL)
2154 xmlXPathFreeObject(res);
2155 if (ctxt->value == tmp) {
2156 res = valuePop(ctxt);
2157 xmlXPathFreeObject(res);
2158 }
2159
2160 ctxt->context->node = NULL;
2161 }
2162
2163 /*
2164 * The result is used as the new evaluation set.
2165 */
2166 xmlXPathFreeObject(obj);
2167 ctxt->context->node = NULL;
2168 ctxt->context->contextSize = -1;
2169 ctxt->context->proximityPosition = -1;
2170 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2171}
2172
2173/**
2174 * xmlXPtrAdvanceNode:
2175 * @cur: the node
2176 *
2177 * Advance to the next element or text node in document order
2178 * TODO: add a stack for entering/exiting entities
2179 *
2180 * Returns -1 in case of failure, 0 otherwise
2181 */
2182xmlNodePtr
2183xmlXPtrAdvanceNode(xmlNodePtr cur) {
2184next:
2185 if (cur == NULL)
2186 return(NULL);
2187 if (cur->children != NULL) {
2188 cur = cur->children ;
2189 goto found;
2190 }
2191 if (cur->next != NULL) {
2192 cur = cur->next;
2193 goto found;
2194 }
2195 do {
2196 cur = cur->parent;
2197 if (cur == NULL) return(NULL);
2198 if (cur->next != NULL) {
2199 cur = cur->next;
2200 goto found;
2201 }
2202 } while (cur != NULL);
2203
2204found:
2205 if ((cur->type != XML_ELEMENT_NODE) &&
2206 (cur->type != XML_TEXT_NODE) &&
2207 (cur->type != XML_DOCUMENT_NODE) &&
2208 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2209 (cur->type != XML_CDATA_SECTION_NODE))
2210 goto next;
2211 if (cur->type == XML_ENTITY_REF_NODE) {
2212 TODO
2213 }
2214 return(cur);
2215}
2216
2217/**
2218 * xmlXPtrAdvanceChar:
2219 * @node: the node
2220 * @index: the index
2221 * @bytes: the number of bytes
2222 *
2223 * Advance a point of the associated number of bytes (not UTF8 chars)
2224 *
2225 * Returns -1 in case of failure, 0 otherwise
2226 */
2227int
2228xmlXPtrAdvanceChar(xmlNodePtr *node, int *index, int bytes) {
2229 xmlNodePtr cur;
2230 int pos;
2231 int len;
2232
2233 if ((node == NULL) || (index == NULL))
2234 return(-1);
2235 cur = *node;
2236 if (cur == NULL)
2237 return(-1);
2238 pos = *index;
2239
2240 while (bytes >= 0) {
2241 /*
2242 * First position to the beginning of the first text node
2243 * corresponding to this point
2244 */
2245 while ((cur != NULL) &&
2246 ((cur->type == XML_ELEMENT_NODE) ||
2247 (cur->type == XML_DOCUMENT_NODE) ||
2248 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2249 if (pos > 0) {
2250 cur = xmlXPtrGetNthChild(cur, pos);
2251 pos = 0;
2252 } else {
2253 cur = xmlXPtrAdvanceNode(cur);
2254 pos = 0;
2255 }
2256 }
2257
2258 if (cur == NULL) {
2259 *node = NULL;
2260 *index = 0;
2261 return(-1);
2262 }
2263
2264 /*
2265 * if there is no move needed return the current value.
2266 */
2267 if (pos == 0) pos = 1;
2268 if (bytes == 0) {
2269 *node = cur;
2270 *index = pos;
2271 return(0);
2272 }
2273 /*
2274 * We should have a text (or cdata) node ...
2275 */
2276 len = 0;
2277 if (cur->content != NULL) {
2278#ifndef XML_USE_BUFFER_CONTENT
2279 len = xmlStrlen(cur->content);
2280#else
2281 len = xmlBufferLength(cur->content);
2282#endif
2283 }
2284 if (pos > len) {
2285 /* Strange, the index in the text node is greater than it's len */
2286 STRANGE
2287 pos = len;
2288 }
2289 if (pos + bytes >= len) {
2290 bytes -= (len - pos);
2291 cur = xmlXPtrAdvanceNode(cur);
2292 cur = 0;
2293 } else if (pos + bytes < len) {
2294 pos += bytes;
2295 *node = cur;
2296 *index = pos;
2297 return(0);
2298 }
2299 }
2300 return(-1);
2301}
2302
2303/**
2304 * xmlXPtrMatchString:
2305 * @string: the string to search
2306 * @start: the start textnode
2307 * @startindex: the start index
2308 * @end: the end textnode IN/OUT
2309 * @endindex: the end index IN/OUT
2310 *
2311 * Check whether the document contains @string at the position
2312 * (@start, @startindex) and limited by the (@end, @endindex) point
2313 *
2314 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2315 * (@start, @startindex) will indicate the position of the beginning
2316 * of the range and (@end, @endindex) will endicate the end
2317 * of the range
2318 */
2319int
2320xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2321 xmlNodePtr *end, int *endindex) {
2322 xmlNodePtr cur;
2323 int pos; /* 0 based */
2324 int len; /* in bytes */
2325 int stringlen; /* in bytes */
2326 int match;
2327
2328 if (string == NULL)
2329 return(-1);
2330 if (start == NULL)
2331 return(-1);
2332 if ((end == NULL) || (endindex == NULL))
2333 return(-1);
2334 cur = start;
2335 if (cur == NULL)
2336 return(-1);
2337 pos = startindex - 1;
2338 stringlen = xmlStrlen(string);
2339
2340 while (stringlen > 0) {
2341 if ((cur == *end) && (pos + stringlen > *endindex))
2342 return(0);
2343 if (cur->content != NULL) {
2344#ifndef XML_USE_BUFFER_CONTENT
2345 len = xmlStrlen(cur->content);
2346#else
2347 len = xmlBufferLength(cur->content);
2348#endif
2349 if (len >= pos + stringlen) {
2350#ifndef XML_USE_BUFFER_CONTENT
2351 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2352#else
2353 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2354 string, stringlen));
2355#endif
2356 if (match) {
2357#ifdef DEBUG_RANGES
2358 xmlGenericError(xmlGenericErrorContext,
2359 "found range %d bytes at index %d of ->",
2360 stringlen, pos + 1);
2361 xmlDebugDumpString(stdout, cur->content);
2362 xmlGenericError(xmlGenericErrorContext, "\n");
2363#endif
2364 *end = cur;
2365 *endindex = pos + stringlen;
2366 return(1);
2367 } else {
2368 return(0);
2369 }
2370 } else {
2371 int sub = len - pos;
2372#ifndef XML_USE_BUFFER_CONTENT
2373 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2374#else
2375 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2376 string, sub));
2377#endif
2378 if (match) {
2379#ifdef DEBUG_RANGES
2380 xmlGenericError(xmlGenericErrorContext,
2381 "found subrange %d bytes at index %d of ->",
2382 sub, pos + 1);
2383 xmlDebugDumpString(stdout, cur->content);
2384 xmlGenericError(xmlGenericErrorContext, "\n");
2385#endif
2386 string = &string[sub];
2387 stringlen -= sub;
2388 } else {
2389 return(0);
2390 }
2391 }
2392 }
2393 cur = xmlXPtrAdvanceNode(cur);
2394 if (cur == NULL)
2395 return(0);
2396 pos = 0;
2397 }
2398 return(1);
2399}
2400
2401/**
2402 * xmlXPtrSearchString:
2403 * @string: the string to search
2404 * @start: the start textnode IN/OUT
2405 * @startindex: the start index IN/OUT
2406 * @end: the end textnode
2407 * @endindex: the end index
2408 *
2409 * Search the next occurence of @string within the document content
2410 * until the (@end, @endindex) point is reached
2411 *
2412 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2413 * (@start, @startindex) will indicate the position of the beginning
2414 * of the range and (@end, @endindex) will endicate the end
2415 * of the range
2416 */
2417int
2418xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2419 xmlNodePtr *end, int *endindex) {
2420 xmlNodePtr cur;
2421 const xmlChar *str;
2422 int pos; /* 0 based */
2423 int len; /* in bytes */
2424 int stringlen; /* in bytes */
2425 xmlChar first;
2426
2427 if (string == NULL)
2428 return(-1);
2429 if ((start == NULL) || (startindex == NULL))
2430 return(-1);
2431 if ((end == NULL) || (endindex == NULL))
2432 return(-1);
2433 cur = *start;
2434 if (cur == NULL)
2435 return(-1);
2436 pos = *startindex - 1;
2437 first = string[0];
2438 stringlen = xmlStrlen(string);
2439
2440 while (cur != NULL) {
2441 if (cur->content != NULL) {
2442#ifndef XML_USE_BUFFER_CONTENT
2443 len = xmlStrlen(cur->content);
2444#else
2445 len = xmlBufferLength(cur->content);
2446#endif
2447 while (pos <= len) {
2448 if (first != 0) {
2449#ifndef XML_USE_BUFFER_CONTENT
2450 str = xmlStrchr(&cur->content[pos], first);
2451#else
2452 str = xmlStrchr(&xmlBufferContent(cur->content)[pos],
2453 first);
2454#endif
2455 if (str != NULL) {
2456 pos = (str - (xmlChar *)(cur->content));
2457#ifdef DEBUG_RANGES
2458 xmlGenericError(xmlGenericErrorContext,
2459 "found '%c' at index %d of ->",
2460 first, pos + 1);
2461 xmlDebugDumpString(stdout, cur->content);
2462 xmlGenericError(xmlGenericErrorContext, "\n");
2463#endif
2464 if (xmlXPtrMatchString(string, cur, pos + 1,
2465 end, endindex)) {
2466 *start = cur;
2467 *startindex = pos + 1;
2468 return(1);
2469 }
2470 pos++;
2471 } else {
2472 pos = len + 1;
2473 }
2474 } else {
2475 /*
2476 * An empty string is considered to match before each
2477 * character of the string-value and after the final
2478 * character.
2479 */
2480#ifdef DEBUG_RANGES
2481 xmlGenericError(xmlGenericErrorContext,
2482 "found '' at index %d of ->",
2483 pos + 1);
2484 xmlDebugDumpString(stdout, cur->content);
2485 xmlGenericError(xmlGenericErrorContext, "\n");
2486#endif
2487 *start = cur;
2488 *startindex = pos + 1;
2489 *end = cur;
2490 *endindex = pos + 1;
2491 return(1);
2492 }
2493 }
2494 }
2495 if ((cur == *end) && (pos >= *endindex))
2496 return(0);
2497 cur = xmlXPtrAdvanceNode(cur);
2498 if (cur == NULL)
2499 return(0);
2500 pos = 1;
2501 }
2502 return(0);
2503}
2504
2505/**
2506 * xmlXPtrGetLastChar:
2507 * @node: the node
2508 * @index: the index
2509 *
2510 * Computes the point coordinates of the last char of this point
2511 *
2512 * Returns -1 in case of failure, 0 otherwise
2513 */
2514int
2515xmlXPtrGetLastChar(xmlNodePtr *node, int *index) {
2516 xmlNodePtr cur;
2517 int pos, len = 0;
2518
2519 if ((node == NULL) || (index == NULL))
2520 return(-1);
2521 cur = *node;
2522 pos = *index;
2523
2524 if (cur == NULL)
2525 return(-1);
2526
2527 if ((cur->type == XML_ELEMENT_NODE) ||
2528 (cur->type == XML_DOCUMENT_NODE) ||
2529 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2530 if (pos > 0) {
2531 cur = xmlXPtrGetNthChild(cur, pos);
2532 pos = 0;
2533 }
2534 }
2535 while (cur != NULL) {
2536 if (cur->last != NULL)
2537 cur = cur->last;
2538 else if (cur->content != NULL) {
2539#ifndef XML_USE_BUFFER_CONTENT
2540 len = xmlStrlen(cur->content);
2541#else
2542 len = xmlBufferLength(cur->content);
2543#endif
2544 break;
2545 } else {
2546 return(-1);
2547 }
2548 }
2549 if (cur == NULL)
2550 return(-1);
2551 *node = cur;
2552 *index = len;
2553 return(0);
2554}
2555
2556/**
2557 * xmlXPtrGetStartPoint:
2558 * @obj: an range
2559 * @node: the resulting node
2560 * @index: the resulting index
2561 *
2562 * read the object and return the start point coordinates.
2563 *
2564 * Returns -1 in case of failure, 0 otherwise
2565 */
2566int
2567xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *index) {
2568 if ((obj == NULL) || (node == NULL) || (index == NULL))
2569 return(-1);
2570
2571 switch (obj->type) {
2572 case XPATH_POINT:
2573 *node = obj->user;
2574 if (obj->index <= 0)
2575 *index = 0;
2576 else
2577 *index = obj->index;
2578 return(0);
2579 case XPATH_RANGE:
2580 *node = obj->user;
2581 if (obj->index <= 0)
2582 *index = 0;
2583 else
2584 *index = obj->index;
2585 return(0);
2586 default:
2587 return(-1);
2588 }
2589 return(-1);
2590}
2591
2592/**
2593 * xmlXPtrGetEndPoint:
2594 * @obj: an range
2595 * @node: the resulting node
2596 * @index: the resulting index
2597 *
2598 * read the object and return the end point coordinates.
2599 *
2600 * Returns -1 in case of failure, 0 otherwise
2601 */
2602int
2603xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *index) {
2604 if ((obj == NULL) || (node == NULL) || (index == NULL))
2605 return(-1);
2606
2607 switch (obj->type) {
2608 case XPATH_POINT:
2609 *node = obj->user;
2610 if (obj->index <= 0)
2611 *index = 0;
2612 else
2613 *index = obj->index;
2614 return(0);
2615 case XPATH_RANGE:
2616 *node = obj->user;
2617 if (obj->index <= 0)
2618 *index = 0;
2619 else
2620 *index = obj->index;
2621 return(0);
2622 default:
2623 return(-1);
2624 }
2625 return(-1);
2626}
2627
2628/**
2629 * xmlXPtrStringRangeFunction:
2630 * @ctxt: the XPointer Parser context
2631 *
2632 * Function implementing the string-range() function
2633 * range as described in 5.4.2
2634 *
2635 * ------------------------------
2636 * [Definition: For each location in the location-set argument,
2637 * string-range returns a set of string ranges, a set of substrings in a
2638 * string. Specifically, the string-value of the location is searched for
2639 * substrings that match the string argument, and the resulting location-set
2640 * will contain a range location for each non-overlapping match.]
2641 * An empty string is considered to match before each character of the
2642 * string-value and after the final character. Whitespace in a string
2643 * is matched literally, with no normalization except that provided by
2644 * XML for line ends. The third argument gives the position of the first
2645 * character to be in the resulting range, relative to the start of the
2646 * match. The default value is 1, which makes the range start immediately
2647 * before the first character of the matched string. The fourth argument
2648 * gives the number of characters in the range; the default is that the
2649 * range extends to the end of the matched string.
2650 *
2651 * Element boundaries, as well as entire embedded nodes such as processing
2652 * instructions and comments, are ignored as defined in [XPath].
2653 *
2654 * If the string in the second argument is not found in the string-value
2655 * of the location, or if a value in the third or fourth argument indicates
2656 * a string that is beyond the beginning or end of the document, the
2657 * expression fails.
2658 *
2659 * The points of the range-locations in the returned location-set will
2660 * all be character points.
2661 * ------------------------------
2662 */
2663void
2664xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2665 int i, startindex, endindex, fendindex;
2666 xmlNodePtr start, end, fend;
2667 xmlXPathObjectPtr set;
2668 xmlLocationSetPtr oldset;
2669 xmlLocationSetPtr newset;
2670 xmlXPathObjectPtr string;
2671 xmlXPathObjectPtr position = NULL;
2672 xmlXPathObjectPtr number = NULL;
2673 int found, pos, num;
2674
2675 /*
2676 * Grab the arguments
2677 */
2678 if ((nargs < 2) || (nargs > 4))
2679 XP_ERROR(XPATH_INVALID_ARITY);
2680
2681 if (nargs >= 4) {
2682 CHECK_TYPE(XPATH_NUMBER);
2683 number = valuePop(ctxt);
2684 if (number != NULL)
2685 num = number->floatval;
2686 }
2687 if (nargs >= 3) {
2688 CHECK_TYPE(XPATH_NUMBER);
2689 position = valuePop(ctxt);
2690 if (position != NULL)
2691 pos = position->floatval;
2692 }
2693 CHECK_TYPE(XPATH_STRING);
2694 string = valuePop(ctxt);
2695 if ((ctxt->value == NULL) ||
2696 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2697 (ctxt->value->type != XPATH_NODESET)))
2698 XP_ERROR(XPATH_INVALID_TYPE)
2699
2700 set = valuePop(ctxt);
2701 if (set->type == XPATH_NODESET) {
2702 xmlXPathObjectPtr tmp;
2703
2704 /*
2705 * First convert to a location set
2706 */
2707 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2708 xmlXPathFreeObject(set);
2709 set = tmp;
2710 }
2711 oldset = (xmlLocationSetPtr) set->user;
2712
2713 /*
2714 * The loop is to search for each element in the location set
2715 * the list of location set corresponding to that search
2716 */
2717 newset = xmlXPtrLocationSetCreate(NULL);
2718 for (i = 0;i < oldset->locNr;i++) {
2719#ifdef DEBUG_RANGES
2720 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2721#endif
2722
2723 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2724 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2725 xmlXPtrAdvanceChar(&start, &startindex, 0);
2726 xmlXPtrGetLastChar(&end, &endindex);
2727
2728#ifdef DEBUG_RANGES
2729 xmlGenericError(xmlGenericErrorContext,
2730 "from index %d of ->", startindex);
2731 xmlDebugDumpString(stdout, start->content);
2732 xmlGenericError(xmlGenericErrorContext, "\n");
2733 xmlGenericError(xmlGenericErrorContext,
2734 "to index %d of ->", endindex);
2735 xmlDebugDumpString(stdout, end->content);
2736 xmlGenericError(xmlGenericErrorContext, "\n");
2737#endif
2738 do {
2739 fend = end;
2740 fendindex = endindex;
2741 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2742 &fend, &fendindex);
2743 if (found == 1) {
2744 if (position == NULL) {
2745 xmlXPtrLocationSetAdd(newset,
2746 xmlXPtrNewRange(start, startindex, fend, fendindex));
2747 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2748 pos - 1) == 0) {
2749 if ((number != NULL) && (num > 0)) {
2750 int rindex;
2751 xmlNodePtr rend;
2752 rend = start;
2753 rindex = startindex - 1;
2754 if (xmlXPtrAdvanceChar(&rend, &rindex,
2755 num) == 0) {
2756 xmlXPtrLocationSetAdd(newset,
2757 xmlXPtrNewRange(start, startindex,
2758 rend, rindex));
2759 }
2760 } else if ((number != NULL) && (num <= 0)) {
2761 xmlXPtrLocationSetAdd(newset,
2762 xmlXPtrNewRange(start, startindex,
2763 start, startindex));
2764 } else {
2765 xmlXPtrLocationSetAdd(newset,
2766 xmlXPtrNewRange(start, startindex,
2767 fend, fendindex));
2768 }
2769 }
2770 start = fend;
2771 startindex = fendindex;
2772 if (string->stringval[0] == 0)
2773 startindex++;
2774 }
2775 } while (found == 1);
2776 }
2777
2778 /*
2779 * Save the new value and cleanup
2780 */
2781 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2782 xmlXPathFreeObject(set);
2783 xmlXPathFreeObject(string);
2784 if (position) xmlXPathFreeObject(position);
2785 if (number) xmlXPathFreeObject(number);
2786}
2787
2788/**
2789 * xmlXPtrEvalRangePredicate:
2790 * @ctxt: the XPointer Parser context
2791 *
2792 * [8] Predicate ::= '[' PredicateExpr ']'
2793 * [9] PredicateExpr ::= Expr
2794 *
2795 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2796 * a Location Set instead of a node set
2797 */
2798void
2799xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2800 const xmlChar *cur;
2801 xmlXPathObjectPtr res;
2802 xmlXPathObjectPtr obj, tmp;
2803 xmlLocationSetPtr newset = NULL;
2804 xmlLocationSetPtr oldset;
2805 int i;
2806
2807 SKIP_BLANKS;
2808 if (CUR != '[') {
2809 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2810 }
2811 NEXT;
2812 SKIP_BLANKS;
2813
2814 /*
2815 * Extract the old set, and then evaluate the result of the
2816 * expression for all the element in the set. use it to grow
2817 * up a new set.
2818 */
2819 CHECK_TYPE(XPATH_LOCATIONSET);
2820 obj = valuePop(ctxt);
2821 oldset = obj->user;
2822 ctxt->context->node = NULL;
2823
2824 if ((oldset == NULL) || (oldset->locNr == 0)) {
2825 ctxt->context->contextSize = 0;
2826 ctxt->context->proximityPosition = 0;
2827 xmlXPathEvalExpr(ctxt);
2828 res = valuePop(ctxt);
2829 if (res != NULL)
2830 xmlXPathFreeObject(res);
2831 valuePush(ctxt, obj);
2832 CHECK_ERROR;
2833 } else {
2834 /*
2835 * Save the expression pointer since we will have to evaluate
2836 * it multiple times. Initialize the new set.
2837 */
2838 cur = ctxt->cur;
2839 newset = xmlXPtrLocationSetCreate(NULL);
2840
2841 for (i = 0; i < oldset->locNr; i++) {
2842 ctxt->cur = cur;
2843
2844 /*
2845 * Run the evaluation with a node list made of a single item
2846 * in the nodeset.
2847 */
2848 ctxt->context->node = oldset->locTab[i]->user;
2849 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2850 valuePush(ctxt, tmp);
2851 ctxt->context->contextSize = oldset->locNr;
2852 ctxt->context->proximityPosition = i + 1;
2853
2854 xmlXPathEvalExpr(ctxt);
2855 CHECK_ERROR;
2856
2857 /*
2858 * The result of the evaluation need to be tested to
2859 * decided whether the filter succeeded or not
2860 */
2861 res = valuePop(ctxt);
2862 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2863 xmlXPtrLocationSetAdd(newset,
2864 xmlXPathObjectCopy(oldset->locTab[i]));
2865 }
2866
2867 /*
2868 * Cleanup
2869 */
2870 if (res != NULL)
2871 xmlXPathFreeObject(res);
2872 if (ctxt->value == tmp) {
2873 res = valuePop(ctxt);
2874 xmlXPathFreeObject(res);
2875 }
2876
2877 ctxt->context->node = NULL;
2878 }
2879
2880 /*
2881 * The result is used as the new evaluation set.
2882 */
2883 xmlXPathFreeObject(obj);
2884 ctxt->context->node = NULL;
2885 ctxt->context->contextSize = -1;
2886 ctxt->context->proximityPosition = -1;
2887 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2888 }
2889 if (CUR != ']') {
2890 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2891 }
2892
2893 NEXT;
2894 SKIP_BLANKS;
2895}
2896
2897#else
2898#endif
2899