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