blob: 37c13a6a008fab5834b38b67a027087d26e8cae8 [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;
Owen Taylor3473f882001-02-23 17:55:21 +0000984 xmlXPathEvalExpr(ctxt);
985 CUR_PTR=left;
986#ifdef XPTR_XMLNS_SCHEME
987 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
988 const xmlChar *left = CUR_PTR;
989 xmlChar *prefix;
990 xmlChar *URI;
991 xmlURIPtr value;
992
993 CUR_PTR = buffer;
994 prefix = xmlXPathParseNCName(ctxt);
995 if (prefix == NULL) {
996 xmlFree(buffer);
997 xmlFree(name);
998 XP_ERROR(XPTR_SYNTAX_ERROR);
999 }
1000 SKIP_BLANKS;
1001 if (CUR != '=') {
1002 xmlFree(prefix);
1003 xmlFree(buffer);
1004 xmlFree(name);
1005 XP_ERROR(XPTR_SYNTAX_ERROR);
1006 }
1007 NEXT;
1008 SKIP_BLANKS;
1009 /* @@ check escaping in the XPointer WD */
1010
1011 value = xmlParseURI((const char *)ctxt->cur);
1012 if (value == NULL) {
1013 xmlFree(prefix);
1014 xmlFree(buffer);
1015 xmlFree(name);
1016 XP_ERROR(XPTR_SYNTAX_ERROR);
1017 }
1018 URI = xmlSaveUri(value);
1019 xmlFreeURI(value);
1020 if (URI == NULL) {
1021 xmlFree(prefix);
1022 xmlFree(buffer);
1023 xmlFree(name);
1024 XP_ERROR(XPATH_MEMORY_ERROR);
1025 }
1026
1027 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1028 CUR_PTR = left;
1029#endif /* XPTR_XMLNS_SCHEME */
1030 } else {
1031 xmlGenericError(xmlGenericErrorContext,
1032 "unsupported scheme '%s'\n", name);
1033 }
1034 xmlFree(buffer);
1035 xmlFree(name);
1036}
1037
1038/**
1039 * xmlXPtrEvalFullXPtr:
1040 * @ctxt: the XPointer Parser context
1041 * @name: the preparsed Scheme for the first XPtrPart
1042 *
1043 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1044 *
1045 * As the specs says:
1046 * -----------
1047 * When multiple XPtrParts are provided, they must be evaluated in
1048 * left-to-right order. If evaluation of one part fails, the nexti
1049 * is evaluated. The following conditions cause XPointer part failure:
1050 *
1051 * - An unknown scheme
1052 * - A scheme that does not locate any sub-resource present in the resource
1053 * - A scheme that is not applicable to the media type of the resource
1054 *
1055 * The XPointer application must consume a failed XPointer part and
1056 * attempt to evaluate the next one, if any. The result of the first
1057 * XPointer part whose evaluation succeeds is taken to be the fragment
1058 * located by the XPointer as a whole. If all the parts fail, the result
1059 * for the XPointer as a whole is a sub-resource error.
1060 * -----------
1061 *
1062 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1063 * expressions or other shemes.
1064 */
1065void
1066xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1067 if (name == NULL)
1068 name = xmlXPathParseName(ctxt);
1069 if (name == NULL)
1070 XP_ERROR(XPATH_EXPR_ERROR);
1071 while (name != NULL) {
1072 xmlXPtrEvalXPtrPart(ctxt, name);
1073
1074 /* in case of syntax error, break here */
1075 if (ctxt->error != XPATH_EXPRESSION_OK)
1076 return;
1077
1078 /*
1079 * If the returned value is a non-empty nodeset
1080 * or location set, return here.
1081 */
1082 if (ctxt->value != NULL) {
1083 xmlXPathObjectPtr obj = ctxt->value;
1084
1085 switch (obj->type) {
1086 case XPATH_LOCATIONSET: {
1087 xmlLocationSetPtr loc = ctxt->value->user;
1088 if ((loc != NULL) && (loc->locNr > 0))
1089 return;
1090 break;
1091 }
1092 case XPATH_NODESET: {
1093 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1094 if ((loc != NULL) && (loc->nodeNr > 0))
1095 return;
1096 break;
1097 }
1098 default:
1099 break;
1100 }
1101
1102 /*
1103 * Evaluating to improper values is equivalent to
1104 * a sub-resource error, clean-up the stack
1105 */
1106 do {
1107 obj = valuePop(ctxt);
1108 if (obj != NULL) {
1109 xmlXPathFreeObject(obj);
1110 }
1111 } while (obj != NULL);
1112 }
1113
1114 /*
1115 * Is there another XPoointer part.
1116 */
1117 SKIP_BLANKS;
1118 name = xmlXPathParseName(ctxt);
1119 }
1120}
1121
1122/**
1123 * xmlXPtrEvalChildSeq:
1124 * @ctxt: the XPointer Parser context
1125 * @name: a possible ID name of the child sequence
1126 *
1127 * ChildSeq ::= '/1' ('/' [0-9]*)*
1128 * | Name ('/' [0-9]*)+
1129 *
1130 * Parse and evaluate a Child Sequence. This routine also handle the
1131 * case of a Bare Name used to get a document ID.
1132 */
1133void
1134xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1135 /*
1136 * XPointer don't allow by syntax to adress in mutirooted trees
1137 * this might prove useful in some cases, warn about it.
1138 */
1139 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
1140 xmlGenericError(xmlGenericErrorContext,
1141 "warning: ChildSeq not starting by /1\n");
1142 }
1143
1144 if (name != NULL) {
1145 valuePush(ctxt, xmlXPathNewString(name));
1146 xmlFree(name);
1147 xmlXPathIdFunction(ctxt, 1);
1148 CHECK_ERROR;
1149 }
1150
1151 while (CUR == '/') {
1152 int child = 0;
1153 NEXT;
1154
1155 while ((CUR >= '0') && (CUR <= '9')) {
1156 child = child * 10 + (CUR - '0');
1157 NEXT;
1158 }
1159 xmlXPtrGetChildNo(ctxt, child);
1160 }
1161}
1162
1163
1164/**
1165 * xmlXPtrEvalXPointer:
1166 * @ctxt: the XPointer Parser context
1167 *
1168 * XPointer ::= Name
1169 * | ChildSeq
1170 * | FullXPtr
1171 *
1172 * Parse and evaluate an XPointer
1173 */
1174void
1175xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001176 if (ctxt->valueTab == NULL) {
1177 /* Allocate the value stack */
1178 ctxt->valueTab = (xmlXPathObjectPtr *)
1179 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1180 if (ctxt->valueTab == NULL) {
1181 xmlFree(ctxt);
1182 xmlGenericError(xmlGenericErrorContext,
1183 "xmlXPathRunEval: out of memory\n");
1184 return;
1185 }
1186 ctxt->valueNr = 0;
1187 ctxt->valueMax = 10;
1188 ctxt->value = NULL;
1189 }
Owen Taylor3473f882001-02-23 17:55:21 +00001190 SKIP_BLANKS;
1191 if (CUR == '/') {
1192 xmlXPathRoot(ctxt);
1193 xmlXPtrEvalChildSeq(ctxt, NULL);
1194 } else {
1195 xmlChar *name;
1196
1197 name = xmlXPathParseName(ctxt);
1198 if (name == NULL)
1199 XP_ERROR(XPATH_EXPR_ERROR);
1200 if (CUR == '(') {
1201 xmlXPtrEvalFullXPtr(ctxt, name);
1202 /* Short evaluation */
1203 return;
1204 } else {
1205 /* this handle both Bare Names and Child Sequences */
1206 xmlXPtrEvalChildSeq(ctxt, name);
1207 }
1208 }
1209 SKIP_BLANKS;
1210 if (CUR != 0)
1211 XP_ERROR(XPATH_EXPR_ERROR);
1212}
1213
1214
1215/************************************************************************
1216 * *
1217 * General routines *
1218 * *
1219 ************************************************************************/
1220
1221void xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs);
1222void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1223void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1224void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1225void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1226void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1227void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1228void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1229
1230/**
1231 * xmlXPtrNewContext:
1232 * @doc: the XML document
1233 * @here: the node that directly contains the XPointer being evaluated or NULL
1234 * @origin: the element from which a user or program initiated traversal of
1235 * the link, or NULL.
1236 *
1237 * Create a new XPointer context
1238 *
1239 * Returns the xmlXPathContext just allocated.
1240 */
1241xmlXPathContextPtr
1242xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1243 xmlXPathContextPtr ret;
1244
1245 ret = xmlXPathNewContext(doc);
1246 if (ret == NULL)
1247 return(ret);
1248 ret->xptr = 1;
1249 ret->here = here;
1250 ret->origin = origin;
1251
1252 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1253 xmlXPtrRangeToFunction);
1254 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1255 xmlXPtrRangeFunction);
1256 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1257 xmlXPtrRangeInsideFunction);
1258 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1259 xmlXPtrStringRangeFunction);
1260 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1261 xmlXPtrStartPointFunction);
1262 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1263 xmlXPtrEndPointFunction);
1264 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1265 xmlXPtrHereFunction);
1266 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1267 xmlXPtrOriginFunction);
1268
1269 return(ret);
1270}
1271
1272/**
1273 * xmlXPtrEval:
1274 * @str: the XPointer expression
1275 * @ctx: the XPointer context
1276 *
1277 * Evaluate the XPath Location Path in the given context.
1278 *
1279 * Returns the xmlXPathObjectPtr resulting from the eveluation or NULL.
1280 * the caller has to free the object.
1281 */
1282xmlXPathObjectPtr
1283xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1284 xmlXPathParserContextPtr ctxt;
1285 xmlXPathObjectPtr res = NULL, tmp;
1286 xmlXPathObjectPtr init = NULL;
1287 int stack = 0;
1288
1289 xmlXPathInit();
1290
1291 if ((ctx == NULL) || (str == NULL))
1292 return(NULL);
1293
1294 ctxt = xmlXPathNewParserContext(str, ctx);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001295 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001296 xmlXPtrEvalXPointer(ctxt);
1297
1298 if ((ctxt->value != NULL) &&
1299 (ctxt->value->type != XPATH_NODESET) &&
1300 (ctxt->value->type != XPATH_LOCATIONSET)) {
1301 xmlGenericError(xmlGenericErrorContext,
1302 "xmlXPtrEval: evaluation failed to return a node set\n");
1303 } else {
1304 res = valuePop(ctxt);
1305 }
1306
1307 do {
1308 tmp = valuePop(ctxt);
1309 if (tmp != NULL) {
1310 if (tmp != init) {
1311 if (tmp->type == XPATH_NODESET) {
1312 /*
1313 * Evaluation may push a root nodeset which is unused
1314 */
1315 xmlNodeSetPtr set;
1316 set = tmp->nodesetval;
1317 if ((set->nodeNr != 1) ||
1318 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1319 stack++;
1320 } else
1321 stack++;
1322 }
1323 xmlXPathFreeObject(tmp);
1324 }
1325 } while (tmp != NULL);
1326 if (stack != 0) {
1327 xmlGenericError(xmlGenericErrorContext,
1328 "xmlXPtrEval: %d object left on the stack\n",
1329 stack);
1330 }
1331 if (ctxt->error != XPATH_EXPRESSION_OK) {
1332 xmlXPathFreeObject(res);
1333 res = NULL;
1334 }
1335
1336 xmlXPathFreeParserContext(ctxt);
1337 return(res);
1338}
1339
1340/**
1341 * xmlXPtrBuildRangeNodeList:
1342 * @range: a range object
1343 *
1344 * Build a node list tree copy of the range
1345 *
1346 * Returns an xmlNodePtr list or NULL.
1347 * the caller has to free the node tree.
1348 */
1349xmlNodePtr
1350xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1351 /* pointers to generated nodes */
1352 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1353 /* pointers to traversal nodes */
1354 xmlNodePtr start, cur, end;
1355 int index, index2;
1356
1357 if (range == NULL)
1358 return(NULL);
1359 if (range->type != XPATH_RANGE)
1360 return(NULL);
1361 start = (xmlNodePtr) range->user;
1362
1363 if (start == NULL)
1364 return(NULL);
1365 end = range->user2;
1366 if (end == NULL)
1367 return(xmlCopyNode(start, 1));
1368
1369 cur = start;
1370 index = range->index;
1371 index2 = range->index2;
1372 while (cur != NULL) {
1373 if (cur == end) {
1374 if (cur->type == XML_TEXT_NODE) {
1375 const xmlChar *content = cur->content;
1376 int len;
1377
1378 if (content == NULL) {
1379 tmp = xmlNewTextLen(NULL, 0);
1380 } else {
1381 len = index2;
1382 if ((cur == start) && (index > 1)) {
1383 content += (index - 1);
1384 len -= (index - 1);
1385 index = 0;
1386 } else {
1387 len = index2;
1388 }
1389 tmp = xmlNewTextLen(content, len);
1390 }
1391 /* single sub text node selection */
1392 if (list == NULL)
1393 return(tmp);
1394 /* prune and return full set */
1395 if (last != NULL)
1396 xmlAddNextSibling(last, tmp);
1397 else
1398 xmlAddChild(parent, tmp);
1399 return(list);
1400 } else {
1401 tmp = xmlCopyNode(cur, 0);
1402 if (list == NULL)
1403 list = tmp;
1404 else {
1405 if (last != NULL)
1406 xmlAddNextSibling(last, tmp);
1407 else
1408 xmlAddChild(parent, tmp);
1409 }
1410 last = NULL;
1411 parent = tmp;
1412
1413 if (index2 > 1) {
1414 end = xmlXPtrGetNthChild(cur, index2 - 1);
1415 index2 = 0;
1416 }
1417 if ((cur == start) && (index > 1)) {
1418 cur = xmlXPtrGetNthChild(cur, index - 1);
1419 index = 0;
1420 } else {
1421 cur = cur->children;
1422 }
1423 /*
1424 * Now gather the remaining nodes from cur to end
1425 */
1426 continue; /* while */
1427 }
1428 } else if ((cur == start) &&
1429 (list == NULL) /* looks superfluous but ... */ ) {
1430 if (cur->type == XML_TEXT_NODE) {
1431 const xmlChar *content = cur->content;
1432
1433 if (content == NULL) {
1434 tmp = xmlNewTextLen(NULL, 0);
1435 } else {
1436 if (index > 1) {
1437 content += (index - 1);
1438 }
1439 tmp = xmlNewText(content);
1440 }
1441 last = list = tmp;
1442 } else {
1443 if ((cur == start) && (index > 1)) {
1444 tmp = xmlCopyNode(cur, 0);
1445 list = tmp;
1446 parent = tmp;
1447 last = NULL;
1448 cur = xmlXPtrGetNthChild(cur, index - 1);
1449 index = 0;
1450 /*
1451 * Now gather the remaining nodes from cur to end
1452 */
1453 continue; /* while */
1454 }
1455 tmp = xmlCopyNode(cur, 1);
1456 list = tmp;
1457 parent = NULL;
1458 last = tmp;
1459 }
1460 } else {
1461 tmp = NULL;
1462 switch (cur->type) {
1463 case XML_DTD_NODE:
1464 case XML_ELEMENT_DECL:
1465 case XML_ATTRIBUTE_DECL:
1466 case XML_ENTITY_NODE:
1467 /* Do not copy DTD informations */
1468 break;
1469 case XML_ENTITY_DECL:
1470 TODO /* handle csossing entities -> stack needed */
1471 break;
1472 case XML_XINCLUDE_START:
1473 case XML_XINCLUDE_END:
1474 /* don't consider it part of the tree content */
1475 break;
1476 case XML_ATTRIBUTE_NODE:
1477 /* Humm, should not happen ! */
1478 STRANGE
1479 break;
1480 default:
1481 tmp = xmlCopyNode(cur, 1);
1482 break;
1483 }
1484 if (tmp != NULL) {
1485 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1486 STRANGE
1487 return(NULL);
1488 }
1489 if (last != NULL)
1490 xmlAddNextSibling(last, tmp);
1491 else {
1492 xmlAddChild(parent, tmp);
1493 last = tmp;
1494 }
1495 }
1496 }
1497 /*
1498 * Skip to next node in document order
1499 */
1500 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1501 STRANGE
1502 return(NULL);
1503 }
1504 cur = xmlXPtrAdvanceNode(cur);
1505 }
1506 return(list);
1507}
1508
1509/**
1510 * xmlXPtrBuildNodeList:
1511 * @obj: the XPointer result from the evaluation.
1512 *
1513 * Build a node list tree copy of the XPointer result.
1514 *
1515 * Returns an xmlNodePtr list or NULL.
1516 * the caller has to free the node tree.
1517 */
1518xmlNodePtr
1519xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1520 xmlNodePtr list = NULL, last = NULL;
1521 int i;
1522
1523 if (obj == NULL)
1524 return(NULL);
1525 switch (obj->type) {
1526 case XPATH_NODESET: {
1527 xmlNodeSetPtr set = obj->nodesetval;
1528 if (set == NULL)
1529 return(NULL);
1530 for (i = 0;i < set->nodeNr;i++) {
1531 if (last == NULL)
1532 list = last = xmlCopyNode(set->nodeTab[i], 1);
1533 else {
1534 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1535 if (last->next != NULL)
1536 last = last->next;
1537 }
1538 }
1539 break;
1540 }
1541 case XPATH_LOCATIONSET: {
1542 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1543 if (set == NULL)
1544 return(NULL);
1545 for (i = 0;i < set->locNr;i++) {
1546 if (last == NULL)
1547 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1548 else
1549 xmlAddNextSibling(last,
1550 xmlXPtrBuildNodeList(set->locTab[i]));
1551 if (last != NULL) {
1552 while (last->next != NULL)
1553 last = last->next;
1554 }
1555 }
1556 break;
1557 }
1558 case XPATH_RANGE:
1559 return(xmlXPtrBuildRangeNodeList(obj));
1560 case XPATH_POINT:
1561 return(xmlCopyNode(obj->user, 0));
1562 default:
1563 break;
1564 }
1565 return(list);
1566}
1567
1568/************************************************************************
1569 * *
1570 * XPointer functions *
1571 * *
1572 ************************************************************************/
1573
1574/**
1575 * xmlXPtrNbLocChildren:
1576 * @node: an xmlNodePtr
1577 *
1578 * Count the number of location children of @node or the lenght of the
1579 * string value in case of text/PI/Comments nodes
1580 *
1581 * Returns the number of location children
1582 */
1583int
1584xmlXPtrNbLocChildren(xmlNodePtr node) {
1585 int ret = 0;
1586 if (node == NULL)
1587 return(-1);
1588 switch (node->type) {
1589 case XML_HTML_DOCUMENT_NODE:
1590 case XML_DOCUMENT_NODE:
1591 case XML_ELEMENT_NODE:
1592 node = node->children;
1593 while (node != NULL) {
1594 if (node->type == XML_ELEMENT_NODE)
1595 ret++;
1596 node = node->next;
1597 }
1598 break;
1599 case XML_ATTRIBUTE_NODE:
1600 return(-1);
1601
1602 case XML_PI_NODE:
1603 case XML_COMMENT_NODE:
1604 case XML_TEXT_NODE:
1605 case XML_CDATA_SECTION_NODE:
1606 case XML_ENTITY_REF_NODE:
1607#ifndef XML_USE_BUFFER_CONTENT
1608 ret = xmlStrlen(node->content);
1609#else
1610 ret = xmlBufferLength(node->content);
1611#endif
1612 break;
1613 default:
1614 return(-1);
1615 }
1616 return(ret);
1617}
1618
1619/**
1620 * xmlXPtrHereFunction:
1621 * @ctxt: the XPointer Parser context
1622 *
1623 * Function implementing here() operation
1624 * as described in 5.4.3
1625 */
1626void
1627xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1628 if (ctxt->context->here == NULL)
1629 XP_ERROR(XPTR_SYNTAX_ERROR);
1630
1631 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1632}
1633
1634/**
1635 * xmlXPtrOriginFunction:
1636 * @ctxt: the XPointer Parser context
1637 *
1638 * Function implementing origin() operation
1639 * as described in 5.4.3
1640 */
1641void
1642xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1643 if (ctxt->context->origin == NULL)
1644 XP_ERROR(XPTR_SYNTAX_ERROR);
1645
1646 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1647}
1648
1649/**
1650 * xmlXPtrStartPointFunction:
1651 * @ctxt: the XPointer Parser context
1652 *
1653 * Function implementing start-point() operation
1654 * as described in 5.4.3
1655 * ----------------
1656 * location-set start-point(location-set)
1657 *
1658 * For each location x in the argument location-set, start-point adds a
1659 * location of type point to the result location-set. That point represents
1660 * the start point of location x and is determined by the following rules:
1661 *
1662 * - If x is of type point, the start point is x.
1663 * - If x is of type range, the start point is the start point of x.
1664 * - If x is of type root, element, text, comment, or processing instruction,
1665 * - the container node of the start point is x and the index is 0.
1666 * - If x is of type attribute or namespace, the function must signal a
1667 * syntax error.
1668 * ----------------
1669 *
1670 */
1671void
1672xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1673 xmlXPathObjectPtr tmp, obj, point;
1674 xmlLocationSetPtr newset = NULL;
1675 xmlLocationSetPtr oldset = NULL;
1676
1677 CHECK_ARITY(1);
1678 if ((ctxt->value == NULL) ||
1679 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1680 (ctxt->value->type != XPATH_NODESET)))
1681 XP_ERROR(XPATH_INVALID_TYPE)
1682
1683 obj = valuePop(ctxt);
1684 if (obj->type == XPATH_NODESET) {
1685 /*
1686 * First convert to a location set
1687 */
1688 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1689 xmlXPathFreeObject(obj);
1690 obj = tmp;
1691 }
1692
1693 newset = xmlXPtrLocationSetCreate(NULL);
1694 if (newset == NULL) {
1695 xmlXPathFreeObject(obj);
1696 XP_ERROR(XPATH_MEMORY_ERROR);
1697 }
1698 oldset = (xmlLocationSetPtr) obj->user;
1699 if (oldset != NULL) {
1700 int i;
1701
1702 for (i = 0; i < oldset->locNr; i++) {
1703 tmp = oldset->locTab[i];
1704 if (tmp == NULL)
1705 continue;
1706 point = NULL;
1707 switch (tmp->type) {
1708 case XPATH_POINT:
1709 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1710 break;
1711 case XPATH_RANGE: {
1712 xmlNodePtr node = tmp->user;
1713 if (node != NULL) {
1714 if (node->type == XML_ATTRIBUTE_NODE) {
1715 /* TODO: Namespace Nodes ??? */
1716 xmlXPathFreeObject(obj);
1717 xmlXPtrFreeLocationSet(newset);
1718 XP_ERROR(XPTR_SYNTAX_ERROR);
1719 }
1720 point = xmlXPtrNewPoint(node, tmp->index);
1721 }
1722 break;
1723 }
1724 default:
1725 /*** Should we raise an error ?
1726 xmlXPathFreeObject(obj);
1727 xmlXPathFreeObject(newset);
1728 XP_ERROR(XPATH_INVALID_TYPE)
1729 ***/
1730 break;
1731 }
1732 if (point != NULL)
1733 xmlXPtrLocationSetAdd(newset, point);
1734 }
1735 }
1736 xmlXPathFreeObject(obj);
1737 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1738}
1739
1740/**
1741 * xmlXPtrEndPointFunction:
1742 * @ctxt: the XPointer Parser context
1743 *
1744 * Function implementing end-point() operation
1745 * as described in 5.4.3
1746 * ----------------------------
1747 * location-set end-point(location-set)
1748 *
1749 * For each location x in the argument location-set, end-point adds a
1750 * location of type point to the result location-set. That point representsi
1751 * the end point of location x and is determined by the following rules:
1752 *
1753 * - If x is of type point, the resulting point is x.
1754 * - If x is of type range, the resulting point is the end point of x.
1755 * - If x is of type root or element, the container node of the resulting
1756 * point is x and the index is the number of location children of x.
1757 * - If x is of type text, comment, or processing instruction, the container
1758 * node of the resulting point is x and the index is the length of thei
1759 * string-value of x.
1760 * - If x is of type attribute or namespace, the function must signal a
1761 * syntax error.
1762 * ----------------------------
1763 */
1764void
1765xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1766 xmlXPathObjectPtr tmp, obj, point;
1767 xmlLocationSetPtr newset = NULL;
1768 xmlLocationSetPtr oldset = NULL;
1769
1770 CHECK_ARITY(1);
1771 if ((ctxt->value == NULL) ||
1772 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1773 (ctxt->value->type != XPATH_NODESET)))
1774 XP_ERROR(XPATH_INVALID_TYPE)
1775
1776 obj = valuePop(ctxt);
1777 if (obj->type == XPATH_NODESET) {
1778 /*
1779 * First convert to a location set
1780 */
1781 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1782 xmlXPathFreeObject(obj);
1783 obj = tmp;
1784 }
1785
1786 newset = xmlXPtrLocationSetCreate(NULL);
1787 oldset = (xmlLocationSetPtr) obj->user;
1788 if (oldset != NULL) {
1789 int i;
1790
1791 for (i = 0; i < oldset->locNr; i++) {
1792 tmp = oldset->locTab[i];
1793 if (tmp == NULL)
1794 continue;
1795 point = NULL;
1796 switch (tmp->type) {
1797 case XPATH_POINT:
1798 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1799 break;
1800 case XPATH_RANGE: {
1801 xmlNodePtr node = tmp->user2;
1802 if (node != NULL) {
1803 if (node->type == XML_ATTRIBUTE_NODE) {
1804 /* TODO: Namespace Nodes ??? */
1805 xmlXPathFreeObject(obj);
1806 xmlXPtrFreeLocationSet(newset);
1807 XP_ERROR(XPTR_SYNTAX_ERROR);
1808 }
1809 point = xmlXPtrNewPoint(node, tmp->index2);
1810 } else if (tmp->user == NULL) {
1811 point = xmlXPtrNewPoint(node,
1812 xmlXPtrNbLocChildren(node));
1813 }
1814 break;
1815 }
1816 default:
1817 /*** Should we raise an error ?
1818 xmlXPathFreeObject(obj);
1819 xmlXPathFreeObject(newset);
1820 XP_ERROR(XPATH_INVALID_TYPE)
1821 ***/
1822 break;
1823 }
1824 if (point != NULL)
1825 xmlXPtrLocationSetAdd(newset, point);
1826 }
1827 }
1828 xmlXPathFreeObject(obj);
1829 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1830}
1831
1832
1833/**
1834 * xmlXPtrCoveringRange:
1835 * @ctxt: the XPointer Parser context
1836 * @loc: the location for which the covering range must be computed
1837 *
1838 * A covering range is a range that wholly encompasses a location
1839 * Section 5.3.3. Covering Ranges for All Location Types
1840 * http://www.w3.org/TR/xptr#N2267
1841 *
1842 * Returns a new location or NULL in case of error
1843 */
1844xmlXPathObjectPtr
1845xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1846 if (loc == NULL)
1847 return(NULL);
1848 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1849 (ctxt->context->doc == NULL))
1850 return(NULL);
1851 switch (loc->type) {
1852 case XPATH_POINT:
1853 return(xmlXPtrNewRange(loc->user, loc->index,
1854 loc->user, loc->index));
1855 case XPATH_RANGE:
1856 if (loc->user2 != NULL) {
1857 return(xmlXPtrNewRange(loc->user, loc->index,
1858 loc->user2, loc->index2));
1859 } else {
1860 xmlNodePtr node = (xmlNodePtr) loc->user;
1861 if (node == (xmlNodePtr) ctxt->context->doc) {
1862 return(xmlXPtrNewRange(node, 0, node,
1863 xmlXPtrGetArity(node)));
1864 } else {
1865 switch (node->type) {
1866 case XML_ATTRIBUTE_NODE:
1867 /* !!! our model is slightly different than XPath */
1868 return(xmlXPtrNewRange(node, 0, node,
1869 xmlXPtrGetArity(node)));
1870 case XML_ELEMENT_NODE:
1871 case XML_TEXT_NODE:
1872 case XML_CDATA_SECTION_NODE:
1873 case XML_ENTITY_REF_NODE:
1874 case XML_PI_NODE:
1875 case XML_COMMENT_NODE:
1876 case XML_DOCUMENT_NODE:
1877 case XML_NOTATION_NODE:
1878 case XML_HTML_DOCUMENT_NODE: {
1879 int index = xmlXPtrGetIndex(node);
1880
1881 node = node->parent;
1882 return(xmlXPtrNewRange(node, index - 1,
1883 node, index + 1));
1884 }
1885 default:
1886 return(NULL);
1887 }
1888 }
1889 }
1890 default:
1891 TODO /* missed one case ??? */
1892 }
1893 return(NULL);
1894}
1895
1896/**
1897 * xmlXPtrRangeFunction:
1898 * @ctxt: the XPointer Parser context
1899 *
1900 * Function implementing the range() function 5.4.3
1901 * location-set range(location-set )
1902 *
1903 * The range function returns ranges covering the locations in
1904 * the argument location-set. For each location x in the argument
1905 * location-set, a range location representing the covering range of
1906 * x is added to the result location-set.
1907 */
1908void
1909xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1910 int i;
1911 xmlXPathObjectPtr set;
1912 xmlLocationSetPtr oldset;
1913 xmlLocationSetPtr newset;
1914
1915 CHECK_ARITY(1);
1916 if ((ctxt->value == NULL) ||
1917 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1918 (ctxt->value->type != XPATH_NODESET)))
1919 XP_ERROR(XPATH_INVALID_TYPE)
1920
1921 set = valuePop(ctxt);
1922 if (set->type == XPATH_NODESET) {
1923 xmlXPathObjectPtr tmp;
1924
1925 /*
1926 * First convert to a location set
1927 */
1928 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1929 xmlXPathFreeObject(set);
1930 set = tmp;
1931 }
1932 oldset = (xmlLocationSetPtr) set->user;
1933
1934 /*
1935 * The loop is to compute the covering range for each item and add it
1936 */
1937 newset = xmlXPtrLocationSetCreate(NULL);
1938 for (i = 0;i < oldset->locNr;i++) {
1939 xmlXPtrLocationSetAdd(newset,
1940 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
1941 }
1942
1943 /*
1944 * Save the new value and cleanup
1945 */
1946 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1947 xmlXPathFreeObject(set);
1948}
1949
1950/**
1951 * xmlXPtrInsideRange:
1952 * @ctxt: the XPointer Parser context
1953 * @loc: the location for which the inside range must be computed
1954 *
1955 * A inside range is a range described in the range-inside() description
1956 *
1957 * Returns a new location or NULL in case of error
1958 */
1959xmlXPathObjectPtr
1960xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1961 if (loc == NULL)
1962 return(NULL);
1963 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1964 (ctxt->context->doc == NULL))
1965 return(NULL);
1966 switch (loc->type) {
1967 case XPATH_POINT: {
1968 xmlNodePtr node = (xmlNodePtr) loc->user;
1969 switch (node->type) {
1970 case XML_PI_NODE:
1971 case XML_COMMENT_NODE:
1972 case XML_TEXT_NODE:
1973 case XML_CDATA_SECTION_NODE: {
1974 if (node->content == NULL) {
1975 return(xmlXPtrNewRange(node, 0, node, 0));
1976 } else {
1977#ifndef XML_USE_BUFFER_CONTENT
1978 return(xmlXPtrNewRange(node, 0, node,
1979 xmlStrlen(node->content)));
1980#else
1981 return(xmlXPtrNewRange(node, 0, node,
1982 xmlBufferLength(node->content)));
1983#endif
1984 }
1985 }
1986 case XML_ATTRIBUTE_NODE:
1987 case XML_ELEMENT_NODE:
1988 case XML_ENTITY_REF_NODE:
1989 case XML_DOCUMENT_NODE:
1990 case XML_NOTATION_NODE:
1991 case XML_HTML_DOCUMENT_NODE: {
1992 return(xmlXPtrNewRange(node, 0, node,
1993 xmlXPtrGetArity(node)));
1994 }
1995 default:
1996 return(NULL);
1997 }
1998 return(NULL);
1999 }
2000 case XPATH_RANGE: {
2001 xmlNodePtr node = (xmlNodePtr) loc->user;
2002 if (loc->user2 != NULL) {
2003 return(xmlXPtrNewRange(node, loc->index,
2004 loc->user2, loc->index2));
2005 } else {
2006 switch (node->type) {
2007 case XML_PI_NODE:
2008 case XML_COMMENT_NODE:
2009 case XML_TEXT_NODE:
2010 case XML_CDATA_SECTION_NODE: {
2011 if (node->content == NULL) {
2012 return(xmlXPtrNewRange(node, 0, node, 0));
2013 } else {
2014#ifndef XML_USE_BUFFER_CONTENT
2015 return(xmlXPtrNewRange(node, 0, node,
2016 xmlStrlen(node->content)));
2017#else
2018 return(xmlXPtrNewRange(node, 0, node,
2019 xmlBufferLength(node->content)));
2020#endif
2021 }
2022 }
2023 case XML_ATTRIBUTE_NODE:
2024 case XML_ELEMENT_NODE:
2025 case XML_ENTITY_REF_NODE:
2026 case XML_DOCUMENT_NODE:
2027 case XML_NOTATION_NODE:
2028 case XML_HTML_DOCUMENT_NODE: {
2029 return(xmlXPtrNewRange(node, 0, node,
2030 xmlXPtrGetArity(node)));
2031 }
2032 default:
2033 return(NULL);
2034 }
2035 return(NULL);
2036 }
2037 }
2038 default:
2039 TODO /* missed one case ??? */
2040 }
2041 return(NULL);
2042}
2043
2044/**
2045 * xmlXPtrRangeInsideFunction:
2046 * @ctxt: the XPointer Parser context
2047 *
2048 * Function implementing the range-inside() function 5.4.3
2049 * location-set range-inside(location-set )
2050 *
2051 * The range-inside function returns ranges covering the contents of
2052 * the locations in the argument location-set. For each location x in
2053 * the argument location-set, a range location is added to the result
2054 * location-set. If x is a range location, then x is added to the
2055 * result location-set. If x is not a range location, then x is used
2056 * as the container location of the start and end points of the range
2057 * location to be added; the index of the start point of the range is
2058 * zero; if the end point is a character point then its index is the
2059 * length of the string-value of x, and otherwise is the number of
2060 * location children of x.
2061 *
2062 */
2063void
2064xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2065 int i;
2066 xmlXPathObjectPtr set;
2067 xmlLocationSetPtr oldset;
2068 xmlLocationSetPtr newset;
2069
2070 CHECK_ARITY(1);
2071 if ((ctxt->value == NULL) ||
2072 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2073 (ctxt->value->type != XPATH_NODESET)))
2074 XP_ERROR(XPATH_INVALID_TYPE)
2075
2076 set = valuePop(ctxt);
2077 if (set->type == XPATH_NODESET) {
2078 xmlXPathObjectPtr tmp;
2079
2080 /*
2081 * First convert to a location set
2082 */
2083 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2084 xmlXPathFreeObject(set);
2085 set = tmp;
2086 }
2087 oldset = (xmlLocationSetPtr) set->user;
2088
2089 /*
2090 * The loop is to compute the covering range for each item and add it
2091 */
2092 newset = xmlXPtrLocationSetCreate(NULL);
2093 for (i = 0;i < oldset->locNr;i++) {
2094 xmlXPtrLocationSetAdd(newset,
2095 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2096 }
2097
2098 /*
2099 * Save the new value and cleanup
2100 */
2101 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2102 xmlXPathFreeObject(set);
2103}
2104
2105/**
2106 * xmlXPtrRangeToFunction:
2107 * @ctxt: the XPointer Parser context
2108 *
2109 * Implement the range-to() XPointer function
2110 */
2111void
2112xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2113 xmlXPathObjectPtr range;
2114 const xmlChar *cur;
2115 xmlXPathObjectPtr res, obj;
2116 xmlXPathObjectPtr tmp;
2117 xmlLocationSetPtr newset = NULL;
2118 xmlNodeSetPtr oldset;
2119 int i;
2120
2121 CHECK_ARITY(1);
2122 /*
2123 * Save the expression pointer since we will have to evaluate
2124 * it multiple times. Initialize the new set.
2125 */
2126 CHECK_TYPE(XPATH_NODESET);
2127 obj = valuePop(ctxt);
2128 oldset = obj->nodesetval;
2129 ctxt->context->node = NULL;
2130
2131 cur = ctxt->cur;
2132 newset = xmlXPtrLocationSetCreate(NULL);
2133
2134 for (i = 0; i < oldset->nodeNr; i++) {
2135 ctxt->cur = cur;
2136
2137 /*
2138 * Run the evaluation with a node list made of a single item
2139 * in the nodeset.
2140 */
2141 ctxt->context->node = oldset->nodeTab[i];
2142 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2143 valuePush(ctxt, tmp);
2144
2145 xmlXPathEvalExpr(ctxt);
2146 CHECK_ERROR;
2147
2148 /*
2149 * The result of the evaluation need to be tested to
2150 * decided whether the filter succeeded or not
2151 */
2152 res = valuePop(ctxt);
2153 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2154 if (range != NULL) {
2155 xmlXPtrLocationSetAdd(newset, range);
2156 }
2157
2158 /*
2159 * Cleanup
2160 */
2161 if (res != NULL)
2162 xmlXPathFreeObject(res);
2163 if (ctxt->value == tmp) {
2164 res = valuePop(ctxt);
2165 xmlXPathFreeObject(res);
2166 }
2167
2168 ctxt->context->node = NULL;
2169 }
2170
2171 /*
2172 * The result is used as the new evaluation set.
2173 */
2174 xmlXPathFreeObject(obj);
2175 ctxt->context->node = NULL;
2176 ctxt->context->contextSize = -1;
2177 ctxt->context->proximityPosition = -1;
2178 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2179}
2180
2181/**
2182 * xmlXPtrAdvanceNode:
2183 * @cur: the node
2184 *
2185 * Advance to the next element or text node in document order
2186 * TODO: add a stack for entering/exiting entities
2187 *
2188 * Returns -1 in case of failure, 0 otherwise
2189 */
2190xmlNodePtr
2191xmlXPtrAdvanceNode(xmlNodePtr cur) {
2192next:
2193 if (cur == NULL)
2194 return(NULL);
2195 if (cur->children != NULL) {
2196 cur = cur->children ;
2197 goto found;
2198 }
2199 if (cur->next != NULL) {
2200 cur = cur->next;
2201 goto found;
2202 }
2203 do {
2204 cur = cur->parent;
2205 if (cur == NULL) return(NULL);
2206 if (cur->next != NULL) {
2207 cur = cur->next;
2208 goto found;
2209 }
2210 } while (cur != NULL);
2211
2212found:
2213 if ((cur->type != XML_ELEMENT_NODE) &&
2214 (cur->type != XML_TEXT_NODE) &&
2215 (cur->type != XML_DOCUMENT_NODE) &&
2216 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2217 (cur->type != XML_CDATA_SECTION_NODE))
2218 goto next;
2219 if (cur->type == XML_ENTITY_REF_NODE) {
2220 TODO
2221 }
2222 return(cur);
2223}
2224
2225/**
2226 * xmlXPtrAdvanceChar:
2227 * @node: the node
2228 * @index: the index
2229 * @bytes: the number of bytes
2230 *
2231 * Advance a point of the associated number of bytes (not UTF8 chars)
2232 *
2233 * Returns -1 in case of failure, 0 otherwise
2234 */
2235int
2236xmlXPtrAdvanceChar(xmlNodePtr *node, int *index, int bytes) {
2237 xmlNodePtr cur;
2238 int pos;
2239 int len;
2240
2241 if ((node == NULL) || (index == NULL))
2242 return(-1);
2243 cur = *node;
2244 if (cur == NULL)
2245 return(-1);
2246 pos = *index;
2247
2248 while (bytes >= 0) {
2249 /*
2250 * First position to the beginning of the first text node
2251 * corresponding to this point
2252 */
2253 while ((cur != NULL) &&
2254 ((cur->type == XML_ELEMENT_NODE) ||
2255 (cur->type == XML_DOCUMENT_NODE) ||
2256 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2257 if (pos > 0) {
2258 cur = xmlXPtrGetNthChild(cur, pos);
2259 pos = 0;
2260 } else {
2261 cur = xmlXPtrAdvanceNode(cur);
2262 pos = 0;
2263 }
2264 }
2265
2266 if (cur == NULL) {
2267 *node = NULL;
2268 *index = 0;
2269 return(-1);
2270 }
2271
2272 /*
2273 * if there is no move needed return the current value.
2274 */
2275 if (pos == 0) pos = 1;
2276 if (bytes == 0) {
2277 *node = cur;
2278 *index = pos;
2279 return(0);
2280 }
2281 /*
2282 * We should have a text (or cdata) node ...
2283 */
2284 len = 0;
2285 if (cur->content != NULL) {
2286#ifndef XML_USE_BUFFER_CONTENT
2287 len = xmlStrlen(cur->content);
2288#else
2289 len = xmlBufferLength(cur->content);
2290#endif
2291 }
2292 if (pos > len) {
2293 /* Strange, the index in the text node is greater than it's len */
2294 STRANGE
2295 pos = len;
2296 }
2297 if (pos + bytes >= len) {
2298 bytes -= (len - pos);
2299 cur = xmlXPtrAdvanceNode(cur);
2300 cur = 0;
2301 } else if (pos + bytes < len) {
2302 pos += bytes;
2303 *node = cur;
2304 *index = pos;
2305 return(0);
2306 }
2307 }
2308 return(-1);
2309}
2310
2311/**
2312 * xmlXPtrMatchString:
2313 * @string: the string to search
2314 * @start: the start textnode
2315 * @startindex: the start index
2316 * @end: the end textnode IN/OUT
2317 * @endindex: the end index IN/OUT
2318 *
2319 * Check whether the document contains @string at the position
2320 * (@start, @startindex) and limited by the (@end, @endindex) point
2321 *
2322 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2323 * (@start, @startindex) will indicate the position of the beginning
2324 * of the range and (@end, @endindex) will endicate the end
2325 * of the range
2326 */
2327int
2328xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2329 xmlNodePtr *end, int *endindex) {
2330 xmlNodePtr cur;
2331 int pos; /* 0 based */
2332 int len; /* in bytes */
2333 int stringlen; /* in bytes */
2334 int match;
2335
2336 if (string == NULL)
2337 return(-1);
2338 if (start == NULL)
2339 return(-1);
2340 if ((end == NULL) || (endindex == NULL))
2341 return(-1);
2342 cur = start;
2343 if (cur == NULL)
2344 return(-1);
2345 pos = startindex - 1;
2346 stringlen = xmlStrlen(string);
2347
2348 while (stringlen > 0) {
2349 if ((cur == *end) && (pos + stringlen > *endindex))
2350 return(0);
2351 if (cur->content != NULL) {
2352#ifndef XML_USE_BUFFER_CONTENT
2353 len = xmlStrlen(cur->content);
2354#else
2355 len = xmlBufferLength(cur->content);
2356#endif
2357 if (len >= pos + stringlen) {
2358#ifndef XML_USE_BUFFER_CONTENT
2359 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2360#else
2361 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2362 string, stringlen));
2363#endif
2364 if (match) {
2365#ifdef DEBUG_RANGES
2366 xmlGenericError(xmlGenericErrorContext,
2367 "found range %d bytes at index %d of ->",
2368 stringlen, pos + 1);
2369 xmlDebugDumpString(stdout, cur->content);
2370 xmlGenericError(xmlGenericErrorContext, "\n");
2371#endif
2372 *end = cur;
2373 *endindex = pos + stringlen;
2374 return(1);
2375 } else {
2376 return(0);
2377 }
2378 } else {
2379 int sub = len - pos;
2380#ifndef XML_USE_BUFFER_CONTENT
2381 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2382#else
2383 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2384 string, sub));
2385#endif
2386 if (match) {
2387#ifdef DEBUG_RANGES
2388 xmlGenericError(xmlGenericErrorContext,
2389 "found subrange %d bytes at index %d of ->",
2390 sub, pos + 1);
2391 xmlDebugDumpString(stdout, cur->content);
2392 xmlGenericError(xmlGenericErrorContext, "\n");
2393#endif
2394 string = &string[sub];
2395 stringlen -= sub;
2396 } else {
2397 return(0);
2398 }
2399 }
2400 }
2401 cur = xmlXPtrAdvanceNode(cur);
2402 if (cur == NULL)
2403 return(0);
2404 pos = 0;
2405 }
2406 return(1);
2407}
2408
2409/**
2410 * xmlXPtrSearchString:
2411 * @string: the string to search
2412 * @start: the start textnode IN/OUT
2413 * @startindex: the start index IN/OUT
2414 * @end: the end textnode
2415 * @endindex: the end index
2416 *
2417 * Search the next occurence of @string within the document content
2418 * until the (@end, @endindex) point is reached
2419 *
2420 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2421 * (@start, @startindex) will indicate the position of the beginning
2422 * of the range and (@end, @endindex) will endicate the end
2423 * of the range
2424 */
2425int
2426xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2427 xmlNodePtr *end, int *endindex) {
2428 xmlNodePtr cur;
2429 const xmlChar *str;
2430 int pos; /* 0 based */
2431 int len; /* in bytes */
2432 int stringlen; /* in bytes */
2433 xmlChar first;
2434
2435 if (string == NULL)
2436 return(-1);
2437 if ((start == NULL) || (startindex == NULL))
2438 return(-1);
2439 if ((end == NULL) || (endindex == NULL))
2440 return(-1);
2441 cur = *start;
2442 if (cur == NULL)
2443 return(-1);
2444 pos = *startindex - 1;
2445 first = string[0];
2446 stringlen = xmlStrlen(string);
2447
2448 while (cur != NULL) {
2449 if (cur->content != NULL) {
2450#ifndef XML_USE_BUFFER_CONTENT
2451 len = xmlStrlen(cur->content);
2452#else
2453 len = xmlBufferLength(cur->content);
2454#endif
2455 while (pos <= len) {
2456 if (first != 0) {
2457#ifndef XML_USE_BUFFER_CONTENT
2458 str = xmlStrchr(&cur->content[pos], first);
2459#else
2460 str = xmlStrchr(&xmlBufferContent(cur->content)[pos],
2461 first);
2462#endif
2463 if (str != NULL) {
2464 pos = (str - (xmlChar *)(cur->content));
2465#ifdef DEBUG_RANGES
2466 xmlGenericError(xmlGenericErrorContext,
2467 "found '%c' at index %d of ->",
2468 first, pos + 1);
2469 xmlDebugDumpString(stdout, cur->content);
2470 xmlGenericError(xmlGenericErrorContext, "\n");
2471#endif
2472 if (xmlXPtrMatchString(string, cur, pos + 1,
2473 end, endindex)) {
2474 *start = cur;
2475 *startindex = pos + 1;
2476 return(1);
2477 }
2478 pos++;
2479 } else {
2480 pos = len + 1;
2481 }
2482 } else {
2483 /*
2484 * An empty string is considered to match before each
2485 * character of the string-value and after the final
2486 * character.
2487 */
2488#ifdef DEBUG_RANGES
2489 xmlGenericError(xmlGenericErrorContext,
2490 "found '' at index %d of ->",
2491 pos + 1);
2492 xmlDebugDumpString(stdout, cur->content);
2493 xmlGenericError(xmlGenericErrorContext, "\n");
2494#endif
2495 *start = cur;
2496 *startindex = pos + 1;
2497 *end = cur;
2498 *endindex = pos + 1;
2499 return(1);
2500 }
2501 }
2502 }
2503 if ((cur == *end) && (pos >= *endindex))
2504 return(0);
2505 cur = xmlXPtrAdvanceNode(cur);
2506 if (cur == NULL)
2507 return(0);
2508 pos = 1;
2509 }
2510 return(0);
2511}
2512
2513/**
2514 * xmlXPtrGetLastChar:
2515 * @node: the node
2516 * @index: the index
2517 *
2518 * Computes the point coordinates of the last char of this point
2519 *
2520 * Returns -1 in case of failure, 0 otherwise
2521 */
2522int
2523xmlXPtrGetLastChar(xmlNodePtr *node, int *index) {
2524 xmlNodePtr cur;
2525 int pos, len = 0;
2526
2527 if ((node == NULL) || (index == NULL))
2528 return(-1);
2529 cur = *node;
2530 pos = *index;
2531
2532 if (cur == NULL)
2533 return(-1);
2534
2535 if ((cur->type == XML_ELEMENT_NODE) ||
2536 (cur->type == XML_DOCUMENT_NODE) ||
2537 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2538 if (pos > 0) {
2539 cur = xmlXPtrGetNthChild(cur, pos);
2540 pos = 0;
2541 }
2542 }
2543 while (cur != NULL) {
2544 if (cur->last != NULL)
2545 cur = cur->last;
2546 else if (cur->content != NULL) {
2547#ifndef XML_USE_BUFFER_CONTENT
2548 len = xmlStrlen(cur->content);
2549#else
2550 len = xmlBufferLength(cur->content);
2551#endif
2552 break;
2553 } else {
2554 return(-1);
2555 }
2556 }
2557 if (cur == NULL)
2558 return(-1);
2559 *node = cur;
2560 *index = len;
2561 return(0);
2562}
2563
2564/**
2565 * xmlXPtrGetStartPoint:
2566 * @obj: an range
2567 * @node: the resulting node
2568 * @index: the resulting index
2569 *
2570 * read the object and return the start point coordinates.
2571 *
2572 * Returns -1 in case of failure, 0 otherwise
2573 */
2574int
2575xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *index) {
2576 if ((obj == NULL) || (node == NULL) || (index == NULL))
2577 return(-1);
2578
2579 switch (obj->type) {
2580 case XPATH_POINT:
2581 *node = obj->user;
2582 if (obj->index <= 0)
2583 *index = 0;
2584 else
2585 *index = obj->index;
2586 return(0);
2587 case XPATH_RANGE:
2588 *node = obj->user;
2589 if (obj->index <= 0)
2590 *index = 0;
2591 else
2592 *index = obj->index;
2593 return(0);
2594 default:
2595 return(-1);
2596 }
2597 return(-1);
2598}
2599
2600/**
2601 * xmlXPtrGetEndPoint:
2602 * @obj: an range
2603 * @node: the resulting node
2604 * @index: the resulting index
2605 *
2606 * read the object and return the end point coordinates.
2607 *
2608 * Returns -1 in case of failure, 0 otherwise
2609 */
2610int
2611xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *index) {
2612 if ((obj == NULL) || (node == NULL) || (index == NULL))
2613 return(-1);
2614
2615 switch (obj->type) {
2616 case XPATH_POINT:
2617 *node = obj->user;
2618 if (obj->index <= 0)
2619 *index = 0;
2620 else
2621 *index = obj->index;
2622 return(0);
2623 case XPATH_RANGE:
2624 *node = obj->user;
2625 if (obj->index <= 0)
2626 *index = 0;
2627 else
2628 *index = obj->index;
2629 return(0);
2630 default:
2631 return(-1);
2632 }
2633 return(-1);
2634}
2635
2636/**
2637 * xmlXPtrStringRangeFunction:
2638 * @ctxt: the XPointer Parser context
2639 *
2640 * Function implementing the string-range() function
2641 * range as described in 5.4.2
2642 *
2643 * ------------------------------
2644 * [Definition: For each location in the location-set argument,
2645 * string-range returns a set of string ranges, a set of substrings in a
2646 * string. Specifically, the string-value of the location is searched for
2647 * substrings that match the string argument, and the resulting location-set
2648 * will contain a range location for each non-overlapping match.]
2649 * An empty string is considered to match before each character of the
2650 * string-value and after the final character. Whitespace in a string
2651 * is matched literally, with no normalization except that provided by
2652 * XML for line ends. The third argument gives the position of the first
2653 * character to be in the resulting range, relative to the start of the
2654 * match. The default value is 1, which makes the range start immediately
2655 * before the first character of the matched string. The fourth argument
2656 * gives the number of characters in the range; the default is that the
2657 * range extends to the end of the matched string.
2658 *
2659 * Element boundaries, as well as entire embedded nodes such as processing
2660 * instructions and comments, are ignored as defined in [XPath].
2661 *
2662 * If the string in the second argument is not found in the string-value
2663 * of the location, or if a value in the third or fourth argument indicates
2664 * a string that is beyond the beginning or end of the document, the
2665 * expression fails.
2666 *
2667 * The points of the range-locations in the returned location-set will
2668 * all be character points.
2669 * ------------------------------
2670 */
2671void
2672xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2673 int i, startindex, endindex, fendindex;
2674 xmlNodePtr start, end, fend;
2675 xmlXPathObjectPtr set;
2676 xmlLocationSetPtr oldset;
2677 xmlLocationSetPtr newset;
2678 xmlXPathObjectPtr string;
2679 xmlXPathObjectPtr position = NULL;
2680 xmlXPathObjectPtr number = NULL;
2681 int found, pos, num;
2682
2683 /*
2684 * Grab the arguments
2685 */
2686 if ((nargs < 2) || (nargs > 4))
2687 XP_ERROR(XPATH_INVALID_ARITY);
2688
2689 if (nargs >= 4) {
2690 CHECK_TYPE(XPATH_NUMBER);
2691 number = valuePop(ctxt);
2692 if (number != NULL)
2693 num = number->floatval;
2694 }
2695 if (nargs >= 3) {
2696 CHECK_TYPE(XPATH_NUMBER);
2697 position = valuePop(ctxt);
2698 if (position != NULL)
2699 pos = position->floatval;
2700 }
2701 CHECK_TYPE(XPATH_STRING);
2702 string = valuePop(ctxt);
2703 if ((ctxt->value == NULL) ||
2704 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2705 (ctxt->value->type != XPATH_NODESET)))
2706 XP_ERROR(XPATH_INVALID_TYPE)
2707
2708 set = valuePop(ctxt);
2709 if (set->type == XPATH_NODESET) {
2710 xmlXPathObjectPtr tmp;
2711
2712 /*
2713 * First convert to a location set
2714 */
2715 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2716 xmlXPathFreeObject(set);
2717 set = tmp;
2718 }
2719 oldset = (xmlLocationSetPtr) set->user;
2720
2721 /*
2722 * The loop is to search for each element in the location set
2723 * the list of location set corresponding to that search
2724 */
2725 newset = xmlXPtrLocationSetCreate(NULL);
2726 for (i = 0;i < oldset->locNr;i++) {
2727#ifdef DEBUG_RANGES
2728 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2729#endif
2730
2731 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2732 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2733 xmlXPtrAdvanceChar(&start, &startindex, 0);
2734 xmlXPtrGetLastChar(&end, &endindex);
2735
2736#ifdef DEBUG_RANGES
2737 xmlGenericError(xmlGenericErrorContext,
2738 "from index %d of ->", startindex);
2739 xmlDebugDumpString(stdout, start->content);
2740 xmlGenericError(xmlGenericErrorContext, "\n");
2741 xmlGenericError(xmlGenericErrorContext,
2742 "to index %d of ->", endindex);
2743 xmlDebugDumpString(stdout, end->content);
2744 xmlGenericError(xmlGenericErrorContext, "\n");
2745#endif
2746 do {
2747 fend = end;
2748 fendindex = endindex;
2749 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2750 &fend, &fendindex);
2751 if (found == 1) {
2752 if (position == NULL) {
2753 xmlXPtrLocationSetAdd(newset,
2754 xmlXPtrNewRange(start, startindex, fend, fendindex));
2755 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2756 pos - 1) == 0) {
2757 if ((number != NULL) && (num > 0)) {
2758 int rindex;
2759 xmlNodePtr rend;
2760 rend = start;
2761 rindex = startindex - 1;
2762 if (xmlXPtrAdvanceChar(&rend, &rindex,
2763 num) == 0) {
2764 xmlXPtrLocationSetAdd(newset,
2765 xmlXPtrNewRange(start, startindex,
2766 rend, rindex));
2767 }
2768 } else if ((number != NULL) && (num <= 0)) {
2769 xmlXPtrLocationSetAdd(newset,
2770 xmlXPtrNewRange(start, startindex,
2771 start, startindex));
2772 } else {
2773 xmlXPtrLocationSetAdd(newset,
2774 xmlXPtrNewRange(start, startindex,
2775 fend, fendindex));
2776 }
2777 }
2778 start = fend;
2779 startindex = fendindex;
2780 if (string->stringval[0] == 0)
2781 startindex++;
2782 }
2783 } while (found == 1);
2784 }
2785
2786 /*
2787 * Save the new value and cleanup
2788 */
2789 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2790 xmlXPathFreeObject(set);
2791 xmlXPathFreeObject(string);
2792 if (position) xmlXPathFreeObject(position);
2793 if (number) xmlXPathFreeObject(number);
2794}
2795
2796/**
2797 * xmlXPtrEvalRangePredicate:
2798 * @ctxt: the XPointer Parser context
2799 *
2800 * [8] Predicate ::= '[' PredicateExpr ']'
2801 * [9] PredicateExpr ::= Expr
2802 *
2803 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2804 * a Location Set instead of a node set
2805 */
2806void
2807xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2808 const xmlChar *cur;
2809 xmlXPathObjectPtr res;
2810 xmlXPathObjectPtr obj, tmp;
2811 xmlLocationSetPtr newset = NULL;
2812 xmlLocationSetPtr oldset;
2813 int i;
2814
2815 SKIP_BLANKS;
2816 if (CUR != '[') {
2817 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2818 }
2819 NEXT;
2820 SKIP_BLANKS;
2821
2822 /*
2823 * Extract the old set, and then evaluate the result of the
2824 * expression for all the element in the set. use it to grow
2825 * up a new set.
2826 */
2827 CHECK_TYPE(XPATH_LOCATIONSET);
2828 obj = valuePop(ctxt);
2829 oldset = obj->user;
2830 ctxt->context->node = NULL;
2831
2832 if ((oldset == NULL) || (oldset->locNr == 0)) {
2833 ctxt->context->contextSize = 0;
2834 ctxt->context->proximityPosition = 0;
2835 xmlXPathEvalExpr(ctxt);
2836 res = valuePop(ctxt);
2837 if (res != NULL)
2838 xmlXPathFreeObject(res);
2839 valuePush(ctxt, obj);
2840 CHECK_ERROR;
2841 } else {
2842 /*
2843 * Save the expression pointer since we will have to evaluate
2844 * it multiple times. Initialize the new set.
2845 */
2846 cur = ctxt->cur;
2847 newset = xmlXPtrLocationSetCreate(NULL);
2848
2849 for (i = 0; i < oldset->locNr; i++) {
2850 ctxt->cur = cur;
2851
2852 /*
2853 * Run the evaluation with a node list made of a single item
2854 * in the nodeset.
2855 */
2856 ctxt->context->node = oldset->locTab[i]->user;
2857 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2858 valuePush(ctxt, tmp);
2859 ctxt->context->contextSize = oldset->locNr;
2860 ctxt->context->proximityPosition = i + 1;
2861
2862 xmlXPathEvalExpr(ctxt);
2863 CHECK_ERROR;
2864
2865 /*
2866 * The result of the evaluation need to be tested to
2867 * decided whether the filter succeeded or not
2868 */
2869 res = valuePop(ctxt);
2870 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2871 xmlXPtrLocationSetAdd(newset,
2872 xmlXPathObjectCopy(oldset->locTab[i]));
2873 }
2874
2875 /*
2876 * Cleanup
2877 */
2878 if (res != NULL)
2879 xmlXPathFreeObject(res);
2880 if (ctxt->value == tmp) {
2881 res = valuePop(ctxt);
2882 xmlXPathFreeObject(res);
2883 }
2884
2885 ctxt->context->node = NULL;
2886 }
2887
2888 /*
2889 * The result is used as the new evaluation set.
2890 */
2891 xmlXPathFreeObject(obj);
2892 ctxt->context->node = NULL;
2893 ctxt->context->contextSize = -1;
2894 ctxt->context->proximityPosition = -1;
2895 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2896 }
2897 if (CUR != ']') {
2898 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2899 }
2900
2901 NEXT;
2902 SKIP_BLANKS;
2903}
2904
2905#else
2906#endif
2907