missing project/build files
[client-tools.git] / src / external / 3rd / library / libxml / xpointer.c
blob4c4cf991c5c0150b6a1f2c8e6d2c904ff21a8c19
1 /*
2 * xpointer.c : Code to handle XML Pointer
4 * World Wide Web Consortium Working Draft 03-March-1998
5 * http://www.w3.org/TR/2000/CR-xptr-20000607
7 * See Copyright for the status of this software.
9 * daniel@veillard.com
12 #define IN_LIBXML
13 #include "libxml.h"
16 * TODO: better handling of error cases, the full expression should
17 * be parsed beforehand instead of a progressive evaluation
18 * TODO: Access into entities references are not supported now ...
19 * need a start to be able to pop out of entities refs since
20 * parent is the endity declaration, not the ref.
23 #include <string.h>
24 #include <libxml/xpointer.h>
25 #include <libxml/xmlmemory.h>
26 #include <libxml/parserInternals.h>
27 #include <libxml/uri.h>
28 #include <libxml/xpath.h>
29 #include <libxml/xpathInternals.h>
30 #include <libxml/xmlerror.h>
31 #include <libxml/globals.h>
33 #ifdef LIBXML_XPTR_ENABLED
35 /* Add support of the xmlns() xpointer scheme to initialize the namespaces */
36 #define XPTR_XMLNS_SCHEME
38 /* #define DEBUG_RANGES */
39 #ifdef DEBUG_RANGES
40 #ifdef LIBXML_DEBUG_ENABLED
41 #include <libxml/debugXML.h>
42 #endif
43 #endif
45 #define TODO \
46 xmlGenericError(xmlGenericErrorContext, \
47 "Unimplemented block at %s:%d\n", \
48 __FILE__, __LINE__);
50 #define STRANGE \
51 xmlGenericError(xmlGenericErrorContext, \
52 "Internal error at %s:%d\n", \
53 __FILE__, __LINE__);
55 /************************************************************************
56 * *
57 * A few helper functions for child sequences *
58 * *
59 ************************************************************************/
61 xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur);
62 /**
63 * xmlXPtrGetArity:
64 * @cur: the node
66 * Returns the number of child for an element, -1 in case of error
68 static int
69 xmlXPtrGetArity(xmlNodePtr cur) {
70 int i;
71 if (cur == NULL)
72 return(-1);
73 cur = cur->children;
74 for (i = 0;cur != NULL;cur = cur->next) {
75 if ((cur->type == XML_ELEMENT_NODE) ||
76 (cur->type == XML_DOCUMENT_NODE) ||
77 (cur->type == XML_HTML_DOCUMENT_NODE)) {
78 i++;
81 return(i);
84 /**
85 * xmlXPtrGetIndex:
86 * @cur: the node
88 * Returns the index of the node in its parent children list, -1
89 * in case of error
91 static int
92 xmlXPtrGetIndex(xmlNodePtr cur) {
93 int i;
94 if (cur == NULL)
95 return(-1);
96 for (i = 1;cur != NULL;cur = cur->prev) {
97 if ((cur->type == XML_ELEMENT_NODE) ||
98 (cur->type == XML_DOCUMENT_NODE) ||
99 (cur->type == XML_HTML_DOCUMENT_NODE)) {
100 i++;
103 return(i);
107 * xmlXPtrGetNthChild:
108 * @cur: the node
109 * @no: the child number
111 * Returns the @no'th element child of @cur or NULL
113 static xmlNodePtr
114 xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
115 int i;
116 if (cur == NULL)
117 return(cur);
118 cur = cur->children;
119 for (i = 0;i <= no;cur = cur->next) {
120 if (cur == NULL)
121 return(cur);
122 if ((cur->type == XML_ELEMENT_NODE) ||
123 (cur->type == XML_DOCUMENT_NODE) ||
124 (cur->type == XML_HTML_DOCUMENT_NODE)) {
125 i++;
126 if (i == no)
127 break;
130 return(cur);
133 /************************************************************************
135 * Handling of XPointer specific types *
137 ************************************************************************/
140 * xmlXPtrCmpPoints:
141 * @node1: the first node
142 * @index1: the first index
143 * @node2: the second node
144 * @index2: the second index
146 * Compare two points w.r.t document order
148 * Returns -2 in case of error 1 if first point < second point, 0 if
149 * that's the same point, -1 otherwise
151 static int
152 xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
153 if ((node1 == NULL) || (node2 == NULL))
154 return(-2);
156 * a couple of optimizations which will avoid computations in most cases
158 if (node1 == node2) {
159 if (index1 < index2)
160 return(1);
161 if (index1 > index2)
162 return(-1);
163 return(0);
165 return(xmlXPathCmpNodes(node1, node2));
169 * xmlXPtrNewPoint:
170 * @node: the xmlNodePtr
171 * @indx: the indx within the node
173 * Create a new xmlXPathObjectPtr of type point
175 * Returns the newly created object.
177 static xmlXPathObjectPtr
178 xmlXPtrNewPoint(xmlNodePtr node, int indx) {
179 xmlXPathObjectPtr ret;
181 if (node == NULL)
182 return(NULL);
183 if (indx < 0)
184 return(NULL);
186 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
187 if (ret == NULL) {
188 xmlGenericError(xmlGenericErrorContext,
189 "xmlXPtrNewPoint: out of memory\n");
190 return(NULL);
192 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
193 ret->type = XPATH_POINT;
194 ret->user = (void *) node;
195 ret->index = indx;
196 return(ret);
200 * xmlXPtrRangeCheckOrder:
201 * @range: an object range
203 * Make sure the points in the range are in the right order
205 static void
206 xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
207 int tmp;
208 xmlNodePtr tmp2;
209 if (range == NULL)
210 return;
211 if (range->type != XPATH_RANGE)
212 return;
213 if (range->user2 == NULL)
214 return;
215 tmp = xmlXPtrCmpPoints(range->user, range->index,
216 range->user2, range->index2);
217 if (tmp == -1) {
218 tmp2 = range->user;
219 range->user = range->user2;
220 range->user2 = tmp2;
221 tmp = range->index;
222 range->index = range->index2;
223 range->index2 = tmp;
228 * xmlXPtrRangesEqual:
229 * @range1: the first range
230 * @range2: the second range
232 * Compare two ranges
234 * Returns 1 if equal, 0 otherwise
236 static int
237 xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
238 if (range1 == range2)
239 return(1);
240 if ((range1 == NULL) || (range2 == NULL))
241 return(0);
242 if (range1->type != range2->type)
243 return(0);
244 if (range1->type != XPATH_RANGE)
245 return(0);
246 if (range1->user != range2->user)
247 return(0);
248 if (range1->index != range2->index)
249 return(0);
250 if (range1->user2 != range2->user2)
251 return(0);
252 if (range1->index2 != range2->index2)
253 return(0);
254 return(1);
258 * xmlXPtrNewRange:
259 * @start: the starting node
260 * @startindex: the start index
261 * @end: the ending point
262 * @endindex: the ending index
264 * Create a new xmlXPathObjectPtr of type range
266 * Returns the newly created object.
268 xmlXPathObjectPtr
269 xmlXPtrNewRange(xmlNodePtr start, int startindex,
270 xmlNodePtr end, int endindex) {
271 xmlXPathObjectPtr ret;
273 if (start == NULL)
274 return(NULL);
275 if (end == NULL)
276 return(NULL);
277 if (startindex < 0)
278 return(NULL);
279 if (endindex < 0)
280 return(NULL);
282 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
283 if (ret == NULL) {
284 xmlGenericError(xmlGenericErrorContext,
285 "xmlXPtrNewRange: out of memory\n");
286 return(NULL);
288 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
289 ret->type = XPATH_RANGE;
290 ret->user = start;
291 ret->index = startindex;
292 ret->user2 = end;
293 ret->index2 = endindex;
294 xmlXPtrRangeCheckOrder(ret);
295 return(ret);
299 * xmlXPtrNewRangePoints:
300 * @start: the starting point
301 * @end: the ending point
303 * Create a new xmlXPathObjectPtr of type range using 2 Points
305 * Returns the newly created object.
307 xmlXPathObjectPtr
308 xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
309 xmlXPathObjectPtr ret;
311 if (start == NULL)
312 return(NULL);
313 if (end == NULL)
314 return(NULL);
315 if (start->type != XPATH_POINT)
316 return(NULL);
317 if (end->type != XPATH_POINT)
318 return(NULL);
320 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
321 if (ret == NULL) {
322 xmlGenericError(xmlGenericErrorContext,
323 "xmlXPtrNewRangePoints: out of memory\n");
324 return(NULL);
326 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
327 ret->type = XPATH_RANGE;
328 ret->user = start->user;
329 ret->index = start->index;
330 ret->user2 = end->user;
331 ret->index2 = end->index;
332 xmlXPtrRangeCheckOrder(ret);
333 return(ret);
337 * xmlXPtrNewRangePointNode:
338 * @start: the starting point
339 * @end: the ending node
341 * Create a new xmlXPathObjectPtr of type range from a point to a node
343 * Returns the newly created object.
345 xmlXPathObjectPtr
346 xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
347 xmlXPathObjectPtr ret;
349 if (start == NULL)
350 return(NULL);
351 if (end == NULL)
352 return(NULL);
353 if (start->type != XPATH_POINT)
354 return(NULL);
356 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
357 if (ret == NULL) {
358 xmlGenericError(xmlGenericErrorContext,
359 "xmlXPtrNewRangePointNode: out of memory\n");
360 return(NULL);
362 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
363 ret->type = XPATH_RANGE;
364 ret->user = start->user;
365 ret->index = start->index;
366 ret->user2 = end;
367 ret->index2 = -1;
368 xmlXPtrRangeCheckOrder(ret);
369 return(ret);
373 * xmlXPtrNewRangeNodePoint:
374 * @start: the starting node
375 * @end: the ending point
377 * Create a new xmlXPathObjectPtr of type range from a node to a point
379 * Returns the newly created object.
381 xmlXPathObjectPtr
382 xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
383 xmlXPathObjectPtr ret;
385 if (start == NULL)
386 return(NULL);
387 if (end == NULL)
388 return(NULL);
389 if (start->type != XPATH_POINT)
390 return(NULL);
391 if (end->type != XPATH_POINT)
392 return(NULL);
394 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
395 if (ret == NULL) {
396 xmlGenericError(xmlGenericErrorContext,
397 "xmlXPtrNewRangeNodePoint: out of memory\n");
398 return(NULL);
400 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
401 ret->type = XPATH_RANGE;
402 ret->user = start;
403 ret->index = -1;
404 ret->user2 = end->user;
405 ret->index2 = end->index;
406 xmlXPtrRangeCheckOrder(ret);
407 return(ret);
411 * xmlXPtrNewRangeNodes:
412 * @start: the starting node
413 * @end: the ending node
415 * Create a new xmlXPathObjectPtr of type range using 2 nodes
417 * Returns the newly created object.
419 xmlXPathObjectPtr
420 xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
421 xmlXPathObjectPtr ret;
423 if (start == NULL)
424 return(NULL);
425 if (end == NULL)
426 return(NULL);
428 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
429 if (ret == NULL) {
430 xmlGenericError(xmlGenericErrorContext,
431 "xmlXPtrNewRangeNodes: out of memory\n");
432 return(NULL);
434 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
435 ret->type = XPATH_RANGE;
436 ret->user = start;
437 ret->index = -1;
438 ret->user2 = end;
439 ret->index2 = -1;
440 xmlXPtrRangeCheckOrder(ret);
441 return(ret);
445 * xmlXPtrNewCollapsedRange:
446 * @start: the starting and ending node
448 * Create a new xmlXPathObjectPtr of type range using a single nodes
450 * Returns the newly created object.
452 xmlXPathObjectPtr
453 xmlXPtrNewCollapsedRange(xmlNodePtr start) {
454 xmlXPathObjectPtr ret;
456 if (start == NULL)
457 return(NULL);
459 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
460 if (ret == NULL) {
461 xmlGenericError(xmlGenericErrorContext,
462 "xmlXPtrNewCollapsedRange: out of memory\n");
463 return(NULL);
465 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
466 ret->type = XPATH_RANGE;
467 ret->user = start;
468 ret->index = -1;
469 ret->user2 = NULL;
470 ret->index2 = -1;
471 return(ret);
475 * xmlXPtrNewRangeNodeObject:
476 * @start: the starting node
477 * @end: the ending object
479 * Create a new xmlXPathObjectPtr of type range from a not to an object
481 * Returns the newly created object.
483 xmlXPathObjectPtr
484 xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
485 xmlXPathObjectPtr ret;
487 if (start == NULL)
488 return(NULL);
489 if (end == NULL)
490 return(NULL);
491 switch (end->type) {
492 case XPATH_POINT:
493 break;
494 case XPATH_NODESET:
496 * Empty set ...
498 if (end->nodesetval->nodeNr <= 0)
499 return(NULL);
500 break;
501 default:
502 TODO
503 return(NULL);
506 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
507 if (ret == NULL) {
508 xmlGenericError(xmlGenericErrorContext,
509 "xmlXPtrNewRangeNodeObject: out of memory\n");
510 return(NULL);
512 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
513 ret->type = XPATH_RANGE;
514 ret->user = start;
515 ret->index = -1;
516 switch (end->type) {
517 case XPATH_POINT:
518 ret->user2 = end->user;
519 ret->index2 = end->index;
520 case XPATH_NODESET: {
521 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
522 ret->index2 = -1;
523 break;
525 default:
526 STRANGE
527 return(NULL);
529 xmlXPtrRangeCheckOrder(ret);
530 return(ret);
533 #define XML_RANGESET_DEFAULT 10
536 * xmlXPtrLocationSetCreate:
537 * @val: an initial xmlXPathObjectPtr, or NULL
539 * Create a new xmlLocationSetPtr of type double and of value @val
541 * Returns the newly created object.
543 xmlLocationSetPtr
544 xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
545 xmlLocationSetPtr ret;
547 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
548 if (ret == NULL) {
549 xmlGenericError(xmlGenericErrorContext,
550 "xmlXPtrLocationSetCreate: out of memory\n");
551 return(NULL);
553 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
554 if (val != NULL) {
555 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
556 sizeof(xmlXPathObjectPtr));
557 if (ret->locTab == NULL) {
558 xmlGenericError(xmlGenericErrorContext,
559 "xmlXPtrLocationSetCreate: out of memory\n");
560 return(NULL);
562 memset(ret->locTab, 0 ,
563 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
564 ret->locMax = XML_RANGESET_DEFAULT;
565 ret->locTab[ret->locNr++] = val;
567 return(ret);
571 * xmlXPtrLocationSetAdd:
572 * @cur: the initial range set
573 * @val: a new xmlXPathObjectPtr
575 * add a new xmlXPathObjectPtr to an existing LocationSet
576 * If the location already exist in the set @val is freed.
578 void
579 xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
580 int i;
582 if (val == NULL) return;
585 * check against doublons
587 for (i = 0;i < cur->locNr;i++) {
588 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
589 xmlXPathFreeObject(val);
590 return;
595 * grow the locTab if needed
597 if (cur->locMax == 0) {
598 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
599 sizeof(xmlXPathObjectPtr));
600 if (cur->locTab == NULL) {
601 xmlGenericError(xmlGenericErrorContext,
602 "xmlXPtrLocationSetAdd: out of memory\n");
603 return;
605 memset(cur->locTab, 0 ,
606 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
607 cur->locMax = XML_RANGESET_DEFAULT;
608 } else if (cur->locNr == cur->locMax) {
609 xmlXPathObjectPtr *temp;
611 cur->locMax *= 2;
612 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
613 sizeof(xmlXPathObjectPtr));
614 if (temp == NULL) {
615 xmlGenericError(xmlGenericErrorContext,
616 "xmlXPtrLocationSetAdd: out of memory\n");
617 return;
619 cur->locTab = temp;
621 cur->locTab[cur->locNr++] = val;
625 * xmlXPtrLocationSetMerge:
626 * @val1: the first LocationSet
627 * @val2: the second LocationSet
629 * Merges two rangesets, all ranges from @val2 are added to @val1
631 * Returns val1 once extended or NULL in case of error.
633 xmlLocationSetPtr
634 xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
635 int i;
637 if (val1 == NULL) return(NULL);
638 if (val2 == NULL) return(val1);
641 * !!!!! this can be optimized a lot, knowing that both
642 * val1 and val2 already have unicity of their values.
645 for (i = 0;i < val2->locNr;i++)
646 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
648 return(val1);
652 * xmlXPtrLocationSetDel:
653 * @cur: the initial range set
654 * @val: an xmlXPathObjectPtr
656 * Removes an xmlXPathObjectPtr from an existing LocationSet
658 void
659 xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
660 int i;
662 if (cur == NULL) return;
663 if (val == NULL) return;
666 * check against doublons
668 for (i = 0;i < cur->locNr;i++)
669 if (cur->locTab[i] == val) break;
671 if (i >= cur->locNr) {
672 #ifdef DEBUG
673 xmlGenericError(xmlGenericErrorContext,
674 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
675 #endif
676 return;
678 cur->locNr--;
679 for (;i < cur->locNr;i++)
680 cur->locTab[i] = cur->locTab[i + 1];
681 cur->locTab[cur->locNr] = NULL;
685 * xmlXPtrLocationSetRemove:
686 * @cur: the initial range set
687 * @val: the index to remove
689 * Removes an entry from an existing LocationSet list.
691 void
692 xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
693 if (cur == NULL) return;
694 if (val >= cur->locNr) return;
695 cur->locNr--;
696 for (;val < cur->locNr;val++)
697 cur->locTab[val] = cur->locTab[val + 1];
698 cur->locTab[cur->locNr] = NULL;
702 * xmlXPtrFreeLocationSet:
703 * @obj: the xmlLocationSetPtr to free
705 * Free the LocationSet compound (not the actual ranges !).
707 void
708 xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
709 int i;
711 if (obj == NULL) return;
712 if (obj->locTab != NULL) {
713 for (i = 0;i < obj->locNr; i++) {
714 xmlXPathFreeObject(obj->locTab[i]);
716 xmlFree(obj->locTab);
718 xmlFree(obj);
722 * xmlXPtrNewLocationSetNodes:
723 * @start: the start NodePtr value
724 * @end: the end NodePtr value or NULL
726 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
727 * it with the single range made of the two nodes @start and @end
729 * Returns the newly created object.
731 xmlXPathObjectPtr
732 xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
733 xmlXPathObjectPtr ret;
735 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
736 if (ret == NULL) {
737 xmlGenericError(xmlGenericErrorContext,
738 "xmlXPtrNewLocationSetNodes: out of memory\n");
739 return(NULL);
741 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
742 ret->type = XPATH_LOCATIONSET;
743 if (end == NULL)
744 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
745 else
746 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
747 return(ret);
751 * xmlXPtrNewLocationSetNodeSet:
752 * @set: a node set
754 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
755 * it with all the nodes from @set
757 * Returns the newly created object.
759 xmlXPathObjectPtr
760 xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
761 xmlXPathObjectPtr ret;
763 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
764 if (ret == NULL) {
765 xmlGenericError(xmlGenericErrorContext,
766 "xmlXPtrNewLocationSetNodeSet: out of memory\n");
767 return(NULL);
769 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
770 ret->type = XPATH_LOCATIONSET;
771 if (set != NULL) {
772 int i;
773 xmlLocationSetPtr newset;
775 newset = xmlXPtrLocationSetCreate(NULL);
776 if (newset == NULL)
777 return(ret);
779 for (i = 0;i < set->nodeNr;i++)
780 xmlXPtrLocationSetAdd(newset,
781 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
783 ret->user = (void *) newset;
785 return(ret);
789 * xmlXPtrWrapLocationSet:
790 * @val: the LocationSet value
792 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
794 * Returns the newly created object.
796 xmlXPathObjectPtr
797 xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
798 xmlXPathObjectPtr ret;
800 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
801 if (ret == NULL) {
802 xmlGenericError(xmlGenericErrorContext,
803 "xmlXPtrWrapLocationSet: out of memory\n");
804 return(NULL);
806 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
807 ret->type = XPATH_LOCATIONSET;
808 ret->user = (void *) val;
809 return(ret);
812 /************************************************************************
814 * The parser *
816 ************************************************************************/
819 * Macros for accessing the content. Those should be used only by the parser,
820 * and not exported.
822 * Dirty macros, i.e. one need to make assumption on the context to use them
824 * CUR_PTR return the current pointer to the xmlChar to be parsed.
825 * CUR returns the current xmlChar value, i.e. a 8 bit value
826 * in ISO-Latin or UTF-8.
827 * This should be used internally by the parser
828 * only to compare to ASCII values otherwise it would break when
829 * running with UTF-8 encoding.
830 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
831 * to compare on ASCII based substring.
832 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
833 * strings within the parser.
834 * CURRENT Returns the current char value, with the full decoding of
835 * UTF-8 if we are using this mode. It returns an int.
836 * NEXT Skip to the next character, this does the proper decoding
837 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
838 * It returns the pointer to the current xmlChar.
841 #define CUR (*ctxt->cur)
842 #define SKIP(val) ctxt->cur += (val)
843 #define NXT(val) ctxt->cur[(val)]
844 #define CUR_PTR ctxt->cur
846 #define SKIP_BLANKS \
847 while (IS_BLANK(*(ctxt->cur))) NEXT
849 #define CURRENT (*ctxt->cur)
850 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
853 * xmlXPtrGetChildNo:
854 * @ctxt: the XPointer Parser context
855 * @index: the child number
857 * Move the current node of the nodeset on the stack to the
858 * given child if found
860 static void
861 xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
862 xmlNodePtr cur = NULL;
863 xmlXPathObjectPtr obj;
864 xmlNodeSetPtr oldset;
866 CHECK_TYPE(XPATH_NODESET);
867 obj = valuePop(ctxt);
868 oldset = obj->nodesetval;
869 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
870 xmlXPathFreeObject(obj);
871 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
872 return;
874 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
875 if (cur == NULL) {
876 xmlXPathFreeObject(obj);
877 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
878 return;
880 oldset->nodeTab[0] = cur;
881 valuePush(ctxt, obj);
885 * xmlXPtrEvalXPtrPart:
886 * @ctxt: the XPointer Parser context
887 * @name: the preparsed Scheme for the XPtrPart
889 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
890 * | Scheme '(' SchemeSpecificExpr ')'
892 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
894 * SchemeSpecificExpr ::= StringWithBalancedParens
896 * StringWithBalancedParens ::=
897 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
898 * [VC: Parenthesis escaping]
900 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
902 * VC: Parenthesis escaping:
903 * The end of an XPointer part is signaled by the right parenthesis ")"
904 * character that is balanced with the left parenthesis "(" character
905 * that began the part. Any unbalanced parenthesis character inside the
906 * expression, even within literals, must be escaped with a circumflex (^)
907 * character preceding it. If the expression contains any literal
908 * occurrences of the circumflex, each must be escaped with an additional
909 * circumflex (that is, ^^). If the unescaped parentheses in the expression
910 * are not balanced, a syntax error results.
912 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
913 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
915 * TODO: there is no new scheme registration mechanism
918 static void
919 xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
920 xmlChar *buffer, *cur;
921 int len;
922 int level;
924 if (name == NULL)
925 name = xmlXPathParseName(ctxt);
926 if (name == NULL)
927 XP_ERROR(XPATH_EXPR_ERROR);
929 if (CUR != '(')
930 XP_ERROR(XPATH_EXPR_ERROR);
931 NEXT;
932 level = 1;
934 len = xmlStrlen(ctxt->cur);
935 len++;
936 buffer = (xmlChar *) xmlMalloc(len * sizeof (xmlChar));
937 if (buffer == NULL) {
938 xmlGenericError(xmlGenericErrorContext,
939 "xmlXPtrEvalXPtrPart: out of memory\n");
940 return;
943 cur = buffer;
944 while (CUR != 0) {
945 if (CUR == ')') {
946 level--;
947 if (level == 0) {
948 NEXT;
949 break;
951 *cur++ = CUR;
952 } else if (CUR == '(') {
953 level++;
954 *cur++ = CUR;
955 } else if (CUR == '^') {
956 NEXT;
957 if ((CUR == ')') || (CUR == '(') || (CUR == '^')) {
958 *cur++ = CUR;
959 } else {
960 *cur++ = '^';
961 *cur++ = CUR;
963 } else {
964 *cur++ = CUR;
966 NEXT;
968 *cur = 0;
970 if ((level != 0) && (CUR == 0)) {
971 xmlFree(buffer);
972 XP_ERROR(XPTR_SYNTAX_ERROR);
975 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
976 const xmlChar *left = CUR_PTR;
978 CUR_PTR = buffer;
979 xmlXPathEvalExpr(ctxt);
980 CUR_PTR=left;
981 #ifdef XPTR_XMLNS_SCHEME
982 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
983 const xmlChar *left = CUR_PTR;
984 xmlChar *prefix;
985 xmlChar *URI;
986 xmlURIPtr value;
988 CUR_PTR = buffer;
989 prefix = xmlXPathParseNCName(ctxt);
990 if (prefix == NULL) {
991 xmlFree(buffer);
992 xmlFree(name);
993 XP_ERROR(XPTR_SYNTAX_ERROR);
995 SKIP_BLANKS;
996 if (CUR != '=') {
997 xmlFree(prefix);
998 xmlFree(buffer);
999 xmlFree(name);
1000 XP_ERROR(XPTR_SYNTAX_ERROR);
1002 NEXT;
1003 SKIP_BLANKS;
1004 /* @@ check escaping in the XPointer WD */
1006 value = xmlParseURI((const char *)ctxt->cur);
1007 if (value == NULL) {
1008 xmlFree(prefix);
1009 xmlFree(buffer);
1010 xmlFree(name);
1011 XP_ERROR(XPTR_SYNTAX_ERROR);
1013 URI = xmlSaveUri(value);
1014 xmlFreeURI(value);
1015 if (URI == NULL) {
1016 xmlFree(prefix);
1017 xmlFree(buffer);
1018 xmlFree(name);
1019 XP_ERROR(XPATH_MEMORY_ERROR);
1022 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1023 CUR_PTR = left;
1024 xmlFree(URI);
1025 xmlFree(prefix);
1026 #endif /* XPTR_XMLNS_SCHEME */
1027 } else {
1028 xmlGenericError(xmlGenericErrorContext,
1029 "unsupported scheme '%s'\n", name);
1031 xmlFree(buffer);
1032 xmlFree(name);
1036 * xmlXPtrEvalFullXPtr:
1037 * @ctxt: the XPointer Parser context
1038 * @name: the preparsed Scheme for the first XPtrPart
1040 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1042 * As the specs says:
1043 * -----------
1044 * When multiple XPtrParts are provided, they must be evaluated in
1045 * left-to-right order. If evaluation of one part fails, the nexti
1046 * is evaluated. The following conditions cause XPointer part failure:
1048 * - An unknown scheme
1049 * - A scheme that does not locate any sub-resource present in the resource
1050 * - A scheme that is not applicable to the media type of the resource
1052 * The XPointer application must consume a failed XPointer part and
1053 * attempt to evaluate the next one, if any. The result of the first
1054 * XPointer part whose evaluation succeeds is taken to be the fragment
1055 * located by the XPointer as a whole. If all the parts fail, the result
1056 * for the XPointer as a whole is a sub-resource error.
1057 * -----------
1059 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1060 * expressions or other schemes.
1062 static void
1063 xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1064 if (name == NULL)
1065 name = xmlXPathParseName(ctxt);
1066 if (name == NULL)
1067 XP_ERROR(XPATH_EXPR_ERROR);
1068 while (name != NULL) {
1069 xmlXPtrEvalXPtrPart(ctxt, name);
1071 /* in case of syntax error, break here */
1072 if (ctxt->error != XPATH_EXPRESSION_OK)
1073 return;
1076 * If the returned value is a non-empty nodeset
1077 * or location set, return here.
1079 if (ctxt->value != NULL) {
1080 xmlXPathObjectPtr obj = ctxt->value;
1082 switch (obj->type) {
1083 case XPATH_LOCATIONSET: {
1084 xmlLocationSetPtr loc = ctxt->value->user;
1085 if ((loc != NULL) && (loc->locNr > 0))
1086 return;
1087 break;
1089 case XPATH_NODESET: {
1090 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1091 if ((loc != NULL) && (loc->nodeNr > 0))
1092 return;
1093 break;
1095 default:
1096 break;
1100 * Evaluating to improper values is equivalent to
1101 * a sub-resource error, clean-up the stack
1103 do {
1104 obj = valuePop(ctxt);
1105 if (obj != NULL) {
1106 xmlXPathFreeObject(obj);
1108 } while (obj != NULL);
1112 * Is there another XPointer part.
1114 SKIP_BLANKS;
1115 name = xmlXPathParseName(ctxt);
1120 * xmlXPtrEvalChildSeq:
1121 * @ctxt: the XPointer Parser context
1122 * @name: a possible ID name of the child sequence
1124 * ChildSeq ::= '/1' ('/' [0-9]*)*
1125 * | Name ('/' [0-9]*)+
1127 * Parse and evaluate a Child Sequence. This routine also handle the
1128 * case of a Bare Name used to get a document ID.
1130 static void
1131 xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1133 * XPointer don't allow by syntax to address in mutirooted trees
1134 * this might prove useful in some cases, warn about it.
1136 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
1137 xmlGenericError(xmlGenericErrorContext,
1138 "warning: ChildSeq not starting by /1\n");
1141 if (name != NULL) {
1142 valuePush(ctxt, xmlXPathNewString(name));
1143 xmlFree(name);
1144 xmlXPathIdFunction(ctxt, 1);
1145 CHECK_ERROR;
1148 while (CUR == '/') {
1149 int child = 0;
1150 NEXT;
1152 while ((CUR >= '0') && (CUR <= '9')) {
1153 child = child * 10 + (CUR - '0');
1154 NEXT;
1156 xmlXPtrGetChildNo(ctxt, child);
1162 * xmlXPtrEvalXPointer:
1163 * @ctxt: the XPointer Parser context
1165 * XPointer ::= Name
1166 * | ChildSeq
1167 * | FullXPtr
1169 * Parse and evaluate an XPointer
1171 static void
1172 xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
1173 if (ctxt->valueTab == NULL) {
1174 /* Allocate the value stack */
1175 ctxt->valueTab = (xmlXPathObjectPtr *)
1176 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1177 if (ctxt->valueTab == NULL) {
1178 xmlFree(ctxt);
1179 xmlGenericError(xmlGenericErrorContext,
1180 "xmlXPathEvalXPointer: out of memory\n");
1181 return;
1183 ctxt->valueNr = 0;
1184 ctxt->valueMax = 10;
1185 ctxt->value = NULL;
1187 SKIP_BLANKS;
1188 if (CUR == '/') {
1189 xmlXPathRoot(ctxt);
1190 xmlXPtrEvalChildSeq(ctxt, NULL);
1191 } else {
1192 xmlChar *name;
1194 name = xmlXPathParseName(ctxt);
1195 if (name == NULL)
1196 XP_ERROR(XPATH_EXPR_ERROR);
1197 if (CUR == '(') {
1198 xmlXPtrEvalFullXPtr(ctxt, name);
1199 /* Short evaluation */
1200 return;
1201 } else {
1202 /* this handle both Bare Names and Child Sequences */
1203 xmlXPtrEvalChildSeq(ctxt, name);
1206 SKIP_BLANKS;
1207 if (CUR != 0)
1208 XP_ERROR(XPATH_EXPR_ERROR);
1212 /************************************************************************
1214 * General routines *
1216 ************************************************************************/
1218 void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1219 void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1220 void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1221 void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1222 void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1223 void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1224 void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1227 * xmlXPtrNewContext:
1228 * @doc: the XML document
1229 * @here: the node that directly contains the XPointer being evaluated or NULL
1230 * @origin: the element from which a user or program initiated traversal of
1231 * the link, or NULL.
1233 * Create a new XPointer context
1235 * Returns the xmlXPathContext just allocated.
1237 xmlXPathContextPtr
1238 xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1239 xmlXPathContextPtr ret;
1241 ret = xmlXPathNewContext(doc);
1242 if (ret == NULL)
1243 return(ret);
1244 ret->xptr = 1;
1245 ret->here = here;
1246 ret->origin = origin;
1248 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1249 xmlXPtrRangeToFunction);
1250 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1251 xmlXPtrRangeFunction);
1252 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1253 xmlXPtrRangeInsideFunction);
1254 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1255 xmlXPtrStringRangeFunction);
1256 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1257 xmlXPtrStartPointFunction);
1258 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1259 xmlXPtrEndPointFunction);
1260 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1261 xmlXPtrHereFunction);
1262 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1263 xmlXPtrOriginFunction);
1265 return(ret);
1269 * xmlXPtrEval:
1270 * @str: the XPointer expression
1271 * @ctx: the XPointer context
1273 * Evaluate the XPath Location Path in the given context.
1275 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
1276 * the caller has to free the object.
1278 xmlXPathObjectPtr
1279 xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1280 xmlXPathParserContextPtr ctxt;
1281 xmlXPathObjectPtr res = NULL, tmp;
1282 xmlXPathObjectPtr init = NULL;
1283 int stack = 0;
1285 xmlXPathInit();
1287 if ((ctx == NULL) || (str == NULL))
1288 return(NULL);
1290 ctxt = xmlXPathNewParserContext(str, ctx);
1291 ctxt->xptr = 1;
1292 xmlXPtrEvalXPointer(ctxt);
1294 if ((ctxt->value != NULL) &&
1295 (ctxt->value->type != XPATH_NODESET) &&
1296 (ctxt->value->type != XPATH_LOCATIONSET)) {
1297 xmlGenericError(xmlGenericErrorContext,
1298 "xmlXPtrEval: evaluation failed to return a node set\n");
1299 } else {
1300 res = valuePop(ctxt);
1303 do {
1304 tmp = valuePop(ctxt);
1305 if (tmp != NULL) {
1306 if (tmp != init) {
1307 if (tmp->type == XPATH_NODESET) {
1309 * Evaluation may push a root nodeset which is unused
1311 xmlNodeSetPtr set;
1312 set = tmp->nodesetval;
1313 if ((set->nodeNr != 1) ||
1314 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1315 stack++;
1316 } else
1317 stack++;
1319 xmlXPathFreeObject(tmp);
1321 } while (tmp != NULL);
1322 if (stack != 0) {
1323 xmlGenericError(xmlGenericErrorContext,
1324 "xmlXPtrEval: %d object left on the stack\n",
1325 stack);
1327 if (ctxt->error != XPATH_EXPRESSION_OK) {
1328 xmlXPathFreeObject(res);
1329 res = NULL;
1332 xmlXPathFreeParserContext(ctxt);
1333 return(res);
1337 * xmlXPtrBuildRangeNodeList:
1338 * @range: a range object
1340 * Build a node list tree copy of the range
1342 * Returns an xmlNodePtr list or NULL.
1343 * the caller has to free the node tree.
1345 static xmlNodePtr
1346 xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1347 /* pointers to generated nodes */
1348 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1349 /* pointers to traversal nodes */
1350 xmlNodePtr start, cur, end;
1351 int index1, index2;
1353 if (range == NULL)
1354 return(NULL);
1355 if (range->type != XPATH_RANGE)
1356 return(NULL);
1357 start = (xmlNodePtr) range->user;
1359 if (start == NULL)
1360 return(NULL);
1361 end = range->user2;
1362 if (end == NULL)
1363 return(xmlCopyNode(start, 1));
1365 cur = start;
1366 index1 = range->index;
1367 index2 = range->index2;
1368 while (cur != NULL) {
1369 if (cur == end) {
1370 if (cur->type == XML_TEXT_NODE) {
1371 const xmlChar *content = cur->content;
1372 int len;
1374 if (content == NULL) {
1375 tmp = xmlNewTextLen(NULL, 0);
1376 } else {
1377 len = index2;
1378 if ((cur == start) && (index1 > 1)) {
1379 content += (index1 - 1);
1380 len -= (index1 - 1);
1381 index1 = 0;
1382 } else {
1383 len = index2;
1385 tmp = xmlNewTextLen(content, len);
1387 /* single sub text node selection */
1388 if (list == NULL)
1389 return(tmp);
1390 /* prune and return full set */
1391 if (last != NULL)
1392 xmlAddNextSibling(last, tmp);
1393 else
1394 xmlAddChild(parent, tmp);
1395 return(list);
1396 } else {
1397 tmp = xmlCopyNode(cur, 0);
1398 if (list == NULL)
1399 list = tmp;
1400 else {
1401 if (last != NULL)
1402 xmlAddNextSibling(last, tmp);
1403 else
1404 xmlAddChild(parent, tmp);
1406 last = NULL;
1407 parent = tmp;
1409 if (index2 > 1) {
1410 end = xmlXPtrGetNthChild(cur, index2 - 1);
1411 index2 = 0;
1413 if ((cur == start) && (index1 > 1)) {
1414 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1415 index1 = 0;
1416 } else {
1417 cur = cur->children;
1420 * Now gather the remaining nodes from cur to end
1422 continue; /* while */
1424 } else if ((cur == start) &&
1425 (list == NULL) /* looks superfluous but ... */ ) {
1426 if ((cur->type == XML_TEXT_NODE) ||
1427 (cur->type == XML_CDATA_SECTION_NODE)) {
1428 const xmlChar *content = cur->content;
1430 if (content == NULL) {
1431 tmp = xmlNewTextLen(NULL, 0);
1432 } else {
1433 if (index1 > 1) {
1434 content += (index1 - 1);
1436 tmp = xmlNewText(content);
1438 last = list = tmp;
1439 } else {
1440 if ((cur == start) && (index1 > 1)) {
1441 tmp = xmlCopyNode(cur, 0);
1442 list = tmp;
1443 parent = tmp;
1444 last = NULL;
1445 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1446 index1 = 0;
1448 * Now gather the remaining nodes from cur to end
1450 continue; /* while */
1452 tmp = xmlCopyNode(cur, 1);
1453 list = tmp;
1454 parent = NULL;
1455 last = tmp;
1457 } else {
1458 tmp = NULL;
1459 switch (cur->type) {
1460 case XML_DTD_NODE:
1461 case XML_ELEMENT_DECL:
1462 case XML_ATTRIBUTE_DECL:
1463 case XML_ENTITY_NODE:
1464 /* Do not copy DTD informations */
1465 break;
1466 case XML_ENTITY_DECL:
1467 TODO /* handle crossing entities -> stack needed */
1468 break;
1469 case XML_XINCLUDE_START:
1470 case XML_XINCLUDE_END:
1471 /* don't consider it part of the tree content */
1472 break;
1473 case XML_ATTRIBUTE_NODE:
1474 /* Humm, should not happen ! */
1475 STRANGE
1476 break;
1477 default:
1478 tmp = xmlCopyNode(cur, 1);
1479 break;
1481 if (tmp != NULL) {
1482 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1483 STRANGE
1484 return(NULL);
1486 if (last != NULL)
1487 xmlAddNextSibling(last, tmp);
1488 else {
1489 xmlAddChild(parent, tmp);
1490 last = tmp;
1495 * Skip to next node in document order
1497 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1498 STRANGE
1499 return(NULL);
1501 cur = xmlXPtrAdvanceNode(cur);
1503 return(list);
1507 * xmlXPtrBuildNodeList:
1508 * @obj: the XPointer result from the evaluation.
1510 * Build a node list tree copy of the XPointer result.
1511 * This will drop Attributes and Namespace declarations.
1513 * Returns an xmlNodePtr list or NULL.
1514 * the caller has to free the node tree.
1516 xmlNodePtr
1517 xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1518 xmlNodePtr list = NULL, last = NULL;
1519 int i;
1521 if (obj == NULL)
1522 return(NULL);
1523 switch (obj->type) {
1524 case XPATH_NODESET: {
1525 xmlNodeSetPtr set = obj->nodesetval;
1526 if (set == NULL)
1527 return(NULL);
1528 for (i = 0;i < set->nodeNr;i++) {
1529 if (set->nodeTab[i] == NULL)
1530 continue;
1531 switch (set->nodeTab[i]->type) {
1532 case XML_TEXT_NODE:
1533 case XML_CDATA_SECTION_NODE:
1534 case XML_ELEMENT_NODE:
1535 case XML_ENTITY_REF_NODE:
1536 case XML_ENTITY_NODE:
1537 case XML_PI_NODE:
1538 case XML_COMMENT_NODE:
1539 case XML_DOCUMENT_NODE:
1540 case XML_HTML_DOCUMENT_NODE:
1541 #ifdef LIBXML_DOCB_ENABLED
1542 case XML_DOCB_DOCUMENT_NODE:
1543 #endif
1544 case XML_XINCLUDE_START:
1545 case XML_XINCLUDE_END:
1546 break;
1547 case XML_ATTRIBUTE_NODE:
1548 case XML_NAMESPACE_DECL:
1549 case XML_DOCUMENT_TYPE_NODE:
1550 case XML_DOCUMENT_FRAG_NODE:
1551 case XML_NOTATION_NODE:
1552 case XML_DTD_NODE:
1553 case XML_ELEMENT_DECL:
1554 case XML_ATTRIBUTE_DECL:
1555 case XML_ENTITY_DECL:
1556 continue; /* for */
1558 if (last == NULL)
1559 list = last = xmlCopyNode(set->nodeTab[i], 1);
1560 else {
1561 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1562 if (last->next != NULL)
1563 last = last->next;
1566 break;
1568 case XPATH_LOCATIONSET: {
1569 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1570 if (set == NULL)
1571 return(NULL);
1572 for (i = 0;i < set->locNr;i++) {
1573 if (last == NULL)
1574 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1575 else
1576 xmlAddNextSibling(last,
1577 xmlXPtrBuildNodeList(set->locTab[i]));
1578 if (last != NULL) {
1579 while (last->next != NULL)
1580 last = last->next;
1583 break;
1585 case XPATH_RANGE:
1586 return(xmlXPtrBuildRangeNodeList(obj));
1587 case XPATH_POINT:
1588 return(xmlCopyNode(obj->user, 0));
1589 default:
1590 break;
1592 return(list);
1595 /************************************************************************
1597 * XPointer functions *
1599 ************************************************************************/
1602 * xmlXPtrNbLocChildren:
1603 * @node: an xmlNodePtr
1605 * Count the number of location children of @node or the length of the
1606 * string value in case of text/PI/Comments nodes
1608 * Returns the number of location children
1610 static int
1611 xmlXPtrNbLocChildren(xmlNodePtr node) {
1612 int ret = 0;
1613 if (node == NULL)
1614 return(-1);
1615 switch (node->type) {
1616 case XML_HTML_DOCUMENT_NODE:
1617 case XML_DOCUMENT_NODE:
1618 case XML_ELEMENT_NODE:
1619 node = node->children;
1620 while (node != NULL) {
1621 if (node->type == XML_ELEMENT_NODE)
1622 ret++;
1623 node = node->next;
1625 break;
1626 case XML_ATTRIBUTE_NODE:
1627 return(-1);
1629 case XML_PI_NODE:
1630 case XML_COMMENT_NODE:
1631 case XML_TEXT_NODE:
1632 case XML_CDATA_SECTION_NODE:
1633 case XML_ENTITY_REF_NODE:
1634 ret = xmlStrlen(node->content);
1635 break;
1636 default:
1637 return(-1);
1639 return(ret);
1643 * xmlXPtrHereFunction:
1644 * @ctxt: the XPointer Parser context
1645 * @nargs: the number of args
1647 * Function implementing here() operation
1648 * as described in 5.4.3
1650 void
1651 xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1652 CHECK_ARITY(0);
1654 if (ctxt->context->here == NULL)
1655 XP_ERROR(XPTR_SYNTAX_ERROR);
1657 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1661 * xmlXPtrOriginFunction:
1662 * @ctxt: the XPointer Parser context
1663 * @nargs: the number of args
1665 * Function implementing origin() operation
1666 * as described in 5.4.3
1668 void
1669 xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1670 CHECK_ARITY(0);
1672 if (ctxt->context->origin == NULL)
1673 XP_ERROR(XPTR_SYNTAX_ERROR);
1675 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1679 * xmlXPtrStartPointFunction:
1680 * @ctxt: the XPointer Parser context
1681 * @nargs: the number of args
1683 * Function implementing start-point() operation
1684 * as described in 5.4.3
1685 * ----------------
1686 * location-set start-point(location-set)
1688 * For each location x in the argument location-set, start-point adds a
1689 * location of type point to the result location-set. That point represents
1690 * the start point of location x and is determined by the following rules:
1692 * - If x is of type point, the start point is x.
1693 * - If x is of type range, the start point is the start point of x.
1694 * - If x is of type root, element, text, comment, or processing instruction,
1695 * - the container node of the start point is x and the index is 0.
1696 * - If x is of type attribute or namespace, the function must signal a
1697 * syntax error.
1698 * ----------------
1701 void
1702 xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1703 xmlXPathObjectPtr tmp, obj, point;
1704 xmlLocationSetPtr newset = NULL;
1705 xmlLocationSetPtr oldset = NULL;
1707 CHECK_ARITY(1);
1708 if ((ctxt->value == NULL) ||
1709 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1710 (ctxt->value->type != XPATH_NODESET)))
1711 XP_ERROR(XPATH_INVALID_TYPE)
1713 obj = valuePop(ctxt);
1714 if (obj->type == XPATH_NODESET) {
1716 * First convert to a location set
1718 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1719 xmlXPathFreeObject(obj);
1720 obj = tmp;
1723 newset = xmlXPtrLocationSetCreate(NULL);
1724 if (newset == NULL) {
1725 xmlXPathFreeObject(obj);
1726 XP_ERROR(XPATH_MEMORY_ERROR);
1728 oldset = (xmlLocationSetPtr) obj->user;
1729 if (oldset != NULL) {
1730 int i;
1732 for (i = 0; i < oldset->locNr; i++) {
1733 tmp = oldset->locTab[i];
1734 if (tmp == NULL)
1735 continue;
1736 point = NULL;
1737 switch (tmp->type) {
1738 case XPATH_POINT:
1739 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1740 break;
1741 case XPATH_RANGE: {
1742 xmlNodePtr node = tmp->user;
1743 if (node != NULL) {
1744 if (node->type == XML_ATTRIBUTE_NODE) {
1745 /* TODO: Namespace Nodes ??? */
1746 xmlXPathFreeObject(obj);
1747 xmlXPtrFreeLocationSet(newset);
1748 XP_ERROR(XPTR_SYNTAX_ERROR);
1750 point = xmlXPtrNewPoint(node, tmp->index);
1752 break;
1754 default:
1755 /*** Should we raise an error ?
1756 xmlXPathFreeObject(obj);
1757 xmlXPathFreeObject(newset);
1758 XP_ERROR(XPATH_INVALID_TYPE)
1759 ***/
1760 break;
1762 if (point != NULL)
1763 xmlXPtrLocationSetAdd(newset, point);
1766 xmlXPathFreeObject(obj);
1767 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1771 * xmlXPtrEndPointFunction:
1772 * @ctxt: the XPointer Parser context
1773 * @nargs: the number of args
1775 * Function implementing end-point() operation
1776 * as described in 5.4.3
1777 * ----------------------------
1778 * location-set end-point(location-set)
1780 * For each location x in the argument location-set, end-point adds a
1781 * location of type point to the result location-set. That point represents
1782 * the end point of location x and is determined by the following rules:
1784 * - If x is of type point, the resulting point is x.
1785 * - If x is of type range, the resulting point is the end point of x.
1786 * - If x is of type root or element, the container node of the resulting
1787 * point is x and the index is the number of location children of x.
1788 * - If x is of type text, comment, or processing instruction, the container
1789 * node of the resulting point is x and the index is the length of the
1790 * string-value of x.
1791 * - If x is of type attribute or namespace, the function must signal a
1792 * syntax error.
1793 * ----------------------------
1795 void
1796 xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1797 xmlXPathObjectPtr tmp, obj, point;
1798 xmlLocationSetPtr newset = NULL;
1799 xmlLocationSetPtr oldset = NULL;
1801 CHECK_ARITY(1);
1802 if ((ctxt->value == NULL) ||
1803 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1804 (ctxt->value->type != XPATH_NODESET)))
1805 XP_ERROR(XPATH_INVALID_TYPE)
1807 obj = valuePop(ctxt);
1808 if (obj->type == XPATH_NODESET) {
1810 * First convert to a location set
1812 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1813 xmlXPathFreeObject(obj);
1814 obj = tmp;
1817 newset = xmlXPtrLocationSetCreate(NULL);
1818 oldset = (xmlLocationSetPtr) obj->user;
1819 if (oldset != NULL) {
1820 int i;
1822 for (i = 0; i < oldset->locNr; i++) {
1823 tmp = oldset->locTab[i];
1824 if (tmp == NULL)
1825 continue;
1826 point = NULL;
1827 switch (tmp->type) {
1828 case XPATH_POINT:
1829 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1830 break;
1831 case XPATH_RANGE: {
1832 xmlNodePtr node = tmp->user2;
1833 if (node != NULL) {
1834 if (node->type == XML_ATTRIBUTE_NODE) {
1835 /* TODO: Namespace Nodes ??? */
1836 xmlXPathFreeObject(obj);
1837 xmlXPtrFreeLocationSet(newset);
1838 XP_ERROR(XPTR_SYNTAX_ERROR);
1840 point = xmlXPtrNewPoint(node, tmp->index2);
1841 } else if (tmp->user == NULL) {
1842 point = xmlXPtrNewPoint(node,
1843 xmlXPtrNbLocChildren(node));
1845 break;
1847 default:
1848 /*** Should we raise an error ?
1849 xmlXPathFreeObject(obj);
1850 xmlXPathFreeObject(newset);
1851 XP_ERROR(XPATH_INVALID_TYPE)
1852 ***/
1853 break;
1855 if (point != NULL)
1856 xmlXPtrLocationSetAdd(newset, point);
1859 xmlXPathFreeObject(obj);
1860 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1865 * xmlXPtrCoveringRange:
1866 * @ctxt: the XPointer Parser context
1867 * @loc: the location for which the covering range must be computed
1869 * A covering range is a range that wholly encompasses a location
1870 * Section 5.3.3. Covering Ranges for All Location Types
1871 * http://www.w3.org/TR/xptr#N2267
1873 * Returns a new location or NULL in case of error
1875 static xmlXPathObjectPtr
1876 xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1877 if (loc == NULL)
1878 return(NULL);
1879 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1880 (ctxt->context->doc == NULL))
1881 return(NULL);
1882 switch (loc->type) {
1883 case XPATH_POINT:
1884 return(xmlXPtrNewRange(loc->user, loc->index,
1885 loc->user, loc->index));
1886 case XPATH_RANGE:
1887 if (loc->user2 != NULL) {
1888 return(xmlXPtrNewRange(loc->user, loc->index,
1889 loc->user2, loc->index2));
1890 } else {
1891 xmlNodePtr node = (xmlNodePtr) loc->user;
1892 if (node == (xmlNodePtr) ctxt->context->doc) {
1893 return(xmlXPtrNewRange(node, 0, node,
1894 xmlXPtrGetArity(node)));
1895 } else {
1896 switch (node->type) {
1897 case XML_ATTRIBUTE_NODE:
1898 /* !!! our model is slightly different than XPath */
1899 return(xmlXPtrNewRange(node, 0, node,
1900 xmlXPtrGetArity(node)));
1901 case XML_ELEMENT_NODE:
1902 case XML_TEXT_NODE:
1903 case XML_CDATA_SECTION_NODE:
1904 case XML_ENTITY_REF_NODE:
1905 case XML_PI_NODE:
1906 case XML_COMMENT_NODE:
1907 case XML_DOCUMENT_NODE:
1908 case XML_NOTATION_NODE:
1909 case XML_HTML_DOCUMENT_NODE: {
1910 int indx = xmlXPtrGetIndex(node);
1912 node = node->parent;
1913 return(xmlXPtrNewRange(node, indx - 1,
1914 node, indx + 1));
1916 default:
1917 return(NULL);
1921 default:
1922 TODO /* missed one case ??? */
1924 return(NULL);
1928 * xmlXPtrRangeFunction:
1929 * @ctxt: the XPointer Parser context
1930 * @nargs: the number of args
1932 * Function implementing the range() function 5.4.3
1933 * location-set range(location-set )
1935 * The range function returns ranges covering the locations in
1936 * the argument location-set. For each location x in the argument
1937 * location-set, a range location representing the covering range of
1938 * x is added to the result location-set.
1940 void
1941 xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1942 int i;
1943 xmlXPathObjectPtr set;
1944 xmlLocationSetPtr oldset;
1945 xmlLocationSetPtr newset;
1947 CHECK_ARITY(1);
1948 if ((ctxt->value == NULL) ||
1949 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1950 (ctxt->value->type != XPATH_NODESET)))
1951 XP_ERROR(XPATH_INVALID_TYPE)
1953 set = valuePop(ctxt);
1954 if (set->type == XPATH_NODESET) {
1955 xmlXPathObjectPtr tmp;
1958 * First convert to a location set
1960 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1961 xmlXPathFreeObject(set);
1962 set = tmp;
1964 oldset = (xmlLocationSetPtr) set->user;
1967 * The loop is to compute the covering range for each item and add it
1969 newset = xmlXPtrLocationSetCreate(NULL);
1970 for (i = 0;i < oldset->locNr;i++) {
1971 xmlXPtrLocationSetAdd(newset,
1972 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
1976 * Save the new value and cleanup
1978 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1979 xmlXPathFreeObject(set);
1983 * xmlXPtrInsideRange:
1984 * @ctxt: the XPointer Parser context
1985 * @loc: the location for which the inside range must be computed
1987 * A inside range is a range described in the range-inside() description
1989 * Returns a new location or NULL in case of error
1991 static xmlXPathObjectPtr
1992 xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1993 if (loc == NULL)
1994 return(NULL);
1995 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1996 (ctxt->context->doc == NULL))
1997 return(NULL);
1998 switch (loc->type) {
1999 case XPATH_POINT: {
2000 xmlNodePtr node = (xmlNodePtr) loc->user;
2001 switch (node->type) {
2002 case XML_PI_NODE:
2003 case XML_COMMENT_NODE:
2004 case XML_TEXT_NODE:
2005 case XML_CDATA_SECTION_NODE: {
2006 if (node->content == NULL) {
2007 return(xmlXPtrNewRange(node, 0, node, 0));
2008 } else {
2009 return(xmlXPtrNewRange(node, 0, node,
2010 xmlStrlen(node->content)));
2013 case XML_ATTRIBUTE_NODE:
2014 case XML_ELEMENT_NODE:
2015 case XML_ENTITY_REF_NODE:
2016 case XML_DOCUMENT_NODE:
2017 case XML_NOTATION_NODE:
2018 case XML_HTML_DOCUMENT_NODE: {
2019 return(xmlXPtrNewRange(node, 0, node,
2020 xmlXPtrGetArity(node)));
2022 default:
2023 break;
2025 return(NULL);
2027 case XPATH_RANGE: {
2028 xmlNodePtr node = (xmlNodePtr) loc->user;
2029 if (loc->user2 != NULL) {
2030 return(xmlXPtrNewRange(node, loc->index,
2031 loc->user2, loc->index2));
2032 } else {
2033 switch (node->type) {
2034 case XML_PI_NODE:
2035 case XML_COMMENT_NODE:
2036 case XML_TEXT_NODE:
2037 case XML_CDATA_SECTION_NODE: {
2038 if (node->content == NULL) {
2039 return(xmlXPtrNewRange(node, 0, node, 0));
2040 } else {
2041 return(xmlXPtrNewRange(node, 0, node,
2042 xmlStrlen(node->content)));
2045 case XML_ATTRIBUTE_NODE:
2046 case XML_ELEMENT_NODE:
2047 case XML_ENTITY_REF_NODE:
2048 case XML_DOCUMENT_NODE:
2049 case XML_NOTATION_NODE:
2050 case XML_HTML_DOCUMENT_NODE: {
2051 return(xmlXPtrNewRange(node, 0, node,
2052 xmlXPtrGetArity(node)));
2054 default:
2055 break;
2057 return(NULL);
2060 default:
2061 TODO /* missed one case ??? */
2063 return(NULL);
2067 * xmlXPtrRangeInsideFunction:
2068 * @ctxt: the XPointer Parser context
2069 * @nargs: the number of args
2071 * Function implementing the range-inside() function 5.4.3
2072 * location-set range-inside(location-set )
2074 * The range-inside function returns ranges covering the contents of
2075 * the locations in the argument location-set. For each location x in
2076 * the argument location-set, a range location is added to the result
2077 * location-set. If x is a range location, then x is added to the
2078 * result location-set. If x is not a range location, then x is used
2079 * as the container location of the start and end points of the range
2080 * location to be added; the index of the start point of the range is
2081 * zero; if the end point is a character point then its index is the
2082 * length of the string-value of x, and otherwise is the number of
2083 * location children of x.
2086 void
2087 xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2088 int i;
2089 xmlXPathObjectPtr set;
2090 xmlLocationSetPtr oldset;
2091 xmlLocationSetPtr newset;
2093 CHECK_ARITY(1);
2094 if ((ctxt->value == NULL) ||
2095 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2096 (ctxt->value->type != XPATH_NODESET)))
2097 XP_ERROR(XPATH_INVALID_TYPE)
2099 set = valuePop(ctxt);
2100 if (set->type == XPATH_NODESET) {
2101 xmlXPathObjectPtr tmp;
2104 * First convert to a location set
2106 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2107 xmlXPathFreeObject(set);
2108 set = tmp;
2110 oldset = (xmlLocationSetPtr) set->user;
2113 * The loop is to compute the covering range for each item and add it
2115 newset = xmlXPtrLocationSetCreate(NULL);
2116 for (i = 0;i < oldset->locNr;i++) {
2117 xmlXPtrLocationSetAdd(newset,
2118 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2122 * Save the new value and cleanup
2124 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2125 xmlXPathFreeObject(set);
2129 * xmlXPtrRangeToFunction:
2130 * @ctxt: the XPointer Parser context
2131 * @nargs: the number of args
2133 * Implement the range-to() XPointer function
2135 void
2136 xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2137 xmlXPathObjectPtr range;
2138 const xmlChar *cur;
2139 xmlXPathObjectPtr res, obj;
2140 xmlXPathObjectPtr tmp;
2141 xmlLocationSetPtr newset = NULL;
2142 xmlNodeSetPtr oldset;
2143 int i;
2145 CHECK_ARITY(1);
2147 * Save the expression pointer since we will have to evaluate
2148 * it multiple times. Initialize the new set.
2150 CHECK_TYPE(XPATH_NODESET);
2151 obj = valuePop(ctxt);
2152 oldset = obj->nodesetval;
2153 ctxt->context->node = NULL;
2155 cur = ctxt->cur;
2156 newset = xmlXPtrLocationSetCreate(NULL);
2158 for (i = 0; i < oldset->nodeNr; i++) {
2159 ctxt->cur = cur;
2162 * Run the evaluation with a node list made of a single item
2163 * in the nodeset.
2165 ctxt->context->node = oldset->nodeTab[i];
2166 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2167 valuePush(ctxt, tmp);
2169 xmlXPathEvalExpr(ctxt);
2170 CHECK_ERROR;
2173 * The result of the evaluation need to be tested to
2174 * decided whether the filter succeeded or not
2176 res = valuePop(ctxt);
2177 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2178 if (range != NULL) {
2179 xmlXPtrLocationSetAdd(newset, range);
2183 * Cleanup
2185 if (res != NULL)
2186 xmlXPathFreeObject(res);
2187 if (ctxt->value == tmp) {
2188 res = valuePop(ctxt);
2189 xmlXPathFreeObject(res);
2192 ctxt->context->node = NULL;
2196 * The result is used as the new evaluation set.
2198 xmlXPathFreeObject(obj);
2199 ctxt->context->node = NULL;
2200 ctxt->context->contextSize = -1;
2201 ctxt->context->proximityPosition = -1;
2202 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2206 * xmlXPtrAdvanceNode:
2207 * @cur: the node
2209 * Advance to the next element or text node in document order
2210 * TODO: add a stack for entering/exiting entities
2212 * Returns -1 in case of failure, 0 otherwise
2214 xmlNodePtr
2215 xmlXPtrAdvanceNode(xmlNodePtr cur) {
2216 next:
2217 if (cur == NULL)
2218 return(NULL);
2219 if (cur->children != NULL) {
2220 cur = cur->children ;
2221 goto found;
2223 if (cur->next != NULL) {
2224 cur = cur->next;
2225 goto found;
2227 do {
2228 cur = cur->parent;
2229 if (cur == NULL) return(NULL);
2230 if (cur->next != NULL) {
2231 cur = cur->next;
2232 goto found;
2234 } while (cur != NULL);
2236 found:
2237 if ((cur->type != XML_ELEMENT_NODE) &&
2238 (cur->type != XML_TEXT_NODE) &&
2239 (cur->type != XML_DOCUMENT_NODE) &&
2240 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2241 (cur->type != XML_CDATA_SECTION_NODE))
2242 goto next;
2243 if (cur->type == XML_ENTITY_REF_NODE) {
2244 TODO
2246 return(cur);
2250 * xmlXPtrAdvanceChar:
2251 * @node: the node
2252 * @indx: the indx
2253 * @bytes: the number of bytes
2255 * Advance a point of the associated number of bytes (not UTF8 chars)
2257 * Returns -1 in case of failure, 0 otherwise
2259 static int
2260 xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
2261 xmlNodePtr cur;
2262 int pos;
2263 int len;
2265 if ((node == NULL) || (indx == NULL))
2266 return(-1);
2267 cur = *node;
2268 if (cur == NULL)
2269 return(-1);
2270 pos = *indx;
2272 while (bytes >= 0) {
2274 * First position to the beginning of the first text node
2275 * corresponding to this point
2277 while ((cur != NULL) &&
2278 ((cur->type == XML_ELEMENT_NODE) ||
2279 (cur->type == XML_DOCUMENT_NODE) ||
2280 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2281 if (pos > 0) {
2282 cur = xmlXPtrGetNthChild(cur, pos);
2283 pos = 0;
2284 } else {
2285 cur = xmlXPtrAdvanceNode(cur);
2286 pos = 0;
2290 if (cur == NULL) {
2291 *node = NULL;
2292 *indx = 0;
2293 return(-1);
2297 * if there is no move needed return the current value.
2299 if (pos == 0) pos = 1;
2300 if (bytes == 0) {
2301 *node = cur;
2302 *indx = pos;
2303 return(0);
2306 * We should have a text (or cdata) node ...
2308 len = 0;
2309 if ((cur->type != XML_ELEMENT_NODE) &&
2310 (cur->content != NULL)) {
2311 len = xmlStrlen(cur->content);
2313 if (pos > len) {
2314 /* Strange, the indx in the text node is greater than it's len */
2315 STRANGE
2316 pos = len;
2318 if (pos + bytes >= len) {
2319 bytes -= (len - pos);
2320 cur = xmlXPtrAdvanceNode(cur);
2321 cur = 0;
2322 } else if (pos + bytes < len) {
2323 pos += bytes;
2324 *node = cur;
2325 *indx = pos;
2326 return(0);
2329 return(-1);
2333 * xmlXPtrMatchString:
2334 * @string: the string to search
2335 * @start: the start textnode
2336 * @startindex: the start index
2337 * @end: the end textnode IN/OUT
2338 * @endindex: the end index IN/OUT
2340 * Check whether the document contains @string at the position
2341 * (@start, @startindex) and limited by the (@end, @endindex) point
2343 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2344 * (@start, @startindex) will indicate the position of the beginning
2345 * of the range and (@end, @endindex) will indicate the end
2346 * of the range
2348 static int
2349 xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2350 xmlNodePtr *end, int *endindex) {
2351 xmlNodePtr cur;
2352 int pos; /* 0 based */
2353 int len; /* in bytes */
2354 int stringlen; /* in bytes */
2355 int match;
2357 if (string == NULL)
2358 return(-1);
2359 if (start == NULL)
2360 return(-1);
2361 if ((end == NULL) || (endindex == NULL))
2362 return(-1);
2363 cur = start;
2364 if (cur == NULL)
2365 return(-1);
2366 pos = startindex - 1;
2367 stringlen = xmlStrlen(string);
2369 while (stringlen > 0) {
2370 if ((cur == *end) && (pos + stringlen > *endindex))
2371 return(0);
2373 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
2374 len = xmlStrlen(cur->content);
2375 if (len >= pos + stringlen) {
2376 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2377 if (match) {
2378 #ifdef DEBUG_RANGES
2379 xmlGenericError(xmlGenericErrorContext,
2380 "found range %d bytes at index %d of ->",
2381 stringlen, pos + 1);
2382 xmlDebugDumpString(stdout, cur->content);
2383 xmlGenericError(xmlGenericErrorContext, "\n");
2384 #endif
2385 *end = cur;
2386 *endindex = pos + stringlen;
2387 return(1);
2388 } else {
2389 return(0);
2391 } else {
2392 int sub = len - pos;
2393 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2394 if (match) {
2395 #ifdef DEBUG_RANGES
2396 xmlGenericError(xmlGenericErrorContext,
2397 "found subrange %d bytes at index %d of ->",
2398 sub, pos + 1);
2399 xmlDebugDumpString(stdout, cur->content);
2400 xmlGenericError(xmlGenericErrorContext, "\n");
2401 #endif
2402 string = &string[sub];
2403 stringlen -= sub;
2404 } else {
2405 return(0);
2409 cur = xmlXPtrAdvanceNode(cur);
2410 if (cur == NULL)
2411 return(0);
2412 pos = 0;
2414 return(1);
2418 * xmlXPtrSearchString:
2419 * @string: the string to search
2420 * @start: the start textnode IN/OUT
2421 * @startindex: the start index IN/OUT
2422 * @end: the end textnode
2423 * @endindex: the end index
2425 * Search the next occurrence of @string within the document content
2426 * until the (@end, @endindex) point is reached
2428 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2429 * (@start, @startindex) will indicate the position of the beginning
2430 * of the range and (@end, @endindex) will indicate the end
2431 * of the range
2433 static int
2434 xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2435 xmlNodePtr *end, int *endindex) {
2436 xmlNodePtr cur;
2437 const xmlChar *str;
2438 int pos; /* 0 based */
2439 int len; /* in bytes */
2440 xmlChar first;
2442 if (string == NULL)
2443 return(-1);
2444 if ((start == NULL) || (startindex == NULL))
2445 return(-1);
2446 if ((end == NULL) || (endindex == NULL))
2447 return(-1);
2448 cur = *start;
2449 if (cur == NULL)
2450 return(-1);
2451 pos = *startindex - 1;
2452 first = string[0];
2454 while (cur != NULL) {
2455 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
2456 len = xmlStrlen(cur->content);
2457 while (pos <= len) {
2458 if (first != 0) {
2459 str = xmlStrchr(&cur->content[pos], first);
2460 if (str != NULL) {
2461 pos = (str - (xmlChar *)(cur->content));
2462 #ifdef DEBUG_RANGES
2463 xmlGenericError(xmlGenericErrorContext,
2464 "found '%c' at index %d of ->",
2465 first, pos + 1);
2466 xmlDebugDumpString(stdout, cur->content);
2467 xmlGenericError(xmlGenericErrorContext, "\n");
2468 #endif
2469 if (xmlXPtrMatchString(string, cur, pos + 1,
2470 end, endindex)) {
2471 *start = cur;
2472 *startindex = pos + 1;
2473 return(1);
2475 pos++;
2476 } else {
2477 pos = len + 1;
2479 } else {
2481 * An empty string is considered to match before each
2482 * character of the string-value and after the final
2483 * character.
2485 #ifdef DEBUG_RANGES
2486 xmlGenericError(xmlGenericErrorContext,
2487 "found '' at index %d of ->",
2488 pos + 1);
2489 xmlDebugDumpString(stdout, cur->content);
2490 xmlGenericError(xmlGenericErrorContext, "\n");
2491 #endif
2492 *start = cur;
2493 *startindex = pos + 1;
2494 *end = cur;
2495 *endindex = pos + 1;
2496 return(1);
2500 if ((cur == *end) && (pos >= *endindex))
2501 return(0);
2502 cur = xmlXPtrAdvanceNode(cur);
2503 if (cur == NULL)
2504 return(0);
2505 pos = 1;
2507 return(0);
2511 * xmlXPtrGetLastChar:
2512 * @node: the node
2513 * @index: the index
2515 * Computes the point coordinates of the last char of this point
2517 * Returns -1 in case of failure, 0 otherwise
2519 static int
2520 xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
2521 xmlNodePtr cur;
2522 int pos, len = 0;
2524 if ((node == NULL) || (indx == NULL))
2525 return(-1);
2526 cur = *node;
2527 pos = *indx;
2529 if (cur == NULL)
2530 return(-1);
2532 if ((cur->type == XML_ELEMENT_NODE) ||
2533 (cur->type == XML_DOCUMENT_NODE) ||
2534 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2535 if (pos > 0) {
2536 cur = xmlXPtrGetNthChild(cur, pos);
2537 pos = 0;
2540 while (cur != NULL) {
2541 if (cur->last != NULL)
2542 cur = cur->last;
2543 else if ((cur->type != XML_ELEMENT_NODE) &&
2544 (cur->content != NULL)) {
2545 len = xmlStrlen(cur->content);
2546 break;
2547 } else {
2548 return(-1);
2551 if (cur == NULL)
2552 return(-1);
2553 *node = cur;
2554 *indx = len;
2555 return(0);
2559 * xmlXPtrGetStartPoint:
2560 * @obj: an range
2561 * @node: the resulting node
2562 * @indx: the resulting index
2564 * read the object and return the start point coordinates.
2566 * Returns -1 in case of failure, 0 otherwise
2568 static int
2569 xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2570 if ((obj == NULL) || (node == NULL) || (indx == NULL))
2571 return(-1);
2573 switch (obj->type) {
2574 case XPATH_POINT:
2575 *node = obj->user;
2576 if (obj->index <= 0)
2577 *indx = 0;
2578 else
2579 *indx = obj->index;
2580 return(0);
2581 case XPATH_RANGE:
2582 *node = obj->user;
2583 if (obj->index <= 0)
2584 *indx = 0;
2585 else
2586 *indx = obj->index;
2587 return(0);
2588 default:
2589 break;
2591 return(-1);
2595 * xmlXPtrGetEndPoint:
2596 * @obj: an range
2597 * @node: the resulting node
2598 * @indx: the resulting indx
2600 * read the object and return the end point coordinates.
2602 * Returns -1 in case of failure, 0 otherwise
2604 static int
2605 xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2606 if ((obj == NULL) || (node == NULL) || (indx == NULL))
2607 return(-1);
2609 switch (obj->type) {
2610 case XPATH_POINT:
2611 *node = obj->user;
2612 if (obj->index <= 0)
2613 *indx = 0;
2614 else
2615 *indx = obj->index;
2616 return(0);
2617 case XPATH_RANGE:
2618 *node = obj->user;
2619 if (obj->index <= 0)
2620 *indx = 0;
2621 else
2622 *indx = obj->index;
2623 return(0);
2624 default:
2625 break;
2627 return(-1);
2631 * xmlXPtrStringRangeFunction:
2632 * @ctxt: the XPointer Parser context
2633 * @nargs: the number of args
2635 * Function implementing the string-range() function
2636 * range as described in 5.4.2
2638 * ------------------------------
2639 * [Definition: For each location in the location-set argument,
2640 * string-range returns a set of string ranges, a set of substrings in a
2641 * string. Specifically, the string-value of the location is searched for
2642 * substrings that match the string argument, and the resulting location-set
2643 * will contain a range location for each non-overlapping match.]
2644 * An empty string is considered to match before each character of the
2645 * string-value and after the final character. Whitespace in a string
2646 * is matched literally, with no normalization except that provided by
2647 * XML for line ends. The third argument gives the position of the first
2648 * character to be in the resulting range, relative to the start of the
2649 * match. The default value is 1, which makes the range start immediately
2650 * before the first character of the matched string. The fourth argument
2651 * gives the number of characters in the range; the default is that the
2652 * range extends to the end of the matched string.
2654 * Element boundaries, as well as entire embedded nodes such as processing
2655 * instructions and comments, are ignored as defined in [XPath].
2657 * If the string in the second argument is not found in the string-value
2658 * of the location, or if a value in the third or fourth argument indicates
2659 * a string that is beyond the beginning or end of the document, the
2660 * expression fails.
2662 * The points of the range-locations in the returned location-set will
2663 * all be character points.
2664 * ------------------------------
2666 void
2667 xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2668 int i, startindex, endindex, fendindex;
2669 xmlNodePtr start, end, fend;
2670 xmlXPathObjectPtr set;
2671 xmlLocationSetPtr oldset;
2672 xmlLocationSetPtr newset;
2673 xmlXPathObjectPtr string;
2674 xmlXPathObjectPtr position = NULL;
2675 xmlXPathObjectPtr number = NULL;
2676 int found, pos = 0, num = 0;
2679 * Grab the arguments
2681 if ((nargs < 2) || (nargs > 4))
2682 XP_ERROR(XPATH_INVALID_ARITY);
2684 if (nargs >= 4) {
2685 CHECK_TYPE(XPATH_NUMBER);
2686 number = valuePop(ctxt);
2687 if (number != NULL)
2688 num = (int) number->floatval;
2690 if (nargs >= 3) {
2691 CHECK_TYPE(XPATH_NUMBER);
2692 position = valuePop(ctxt);
2693 if (position != NULL)
2694 pos = (int) position->floatval;
2696 CHECK_TYPE(XPATH_STRING);
2697 string = valuePop(ctxt);
2698 if ((ctxt->value == NULL) ||
2699 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2700 (ctxt->value->type != XPATH_NODESET)))
2701 XP_ERROR(XPATH_INVALID_TYPE)
2703 set = valuePop(ctxt);
2704 if (set->type == XPATH_NODESET) {
2705 xmlXPathObjectPtr tmp;
2708 * First convert to a location set
2710 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2711 xmlXPathFreeObject(set);
2712 set = tmp;
2714 oldset = (xmlLocationSetPtr) set->user;
2717 * The loop is to search for each element in the location set
2718 * the list of location set corresponding to that search
2720 newset = xmlXPtrLocationSetCreate(NULL);
2721 for (i = 0;i < oldset->locNr;i++) {
2722 #ifdef DEBUG_RANGES
2723 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2724 #endif
2726 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2727 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2728 xmlXPtrAdvanceChar(&start, &startindex, 0);
2729 xmlXPtrGetLastChar(&end, &endindex);
2731 #ifdef DEBUG_RANGES
2732 xmlGenericError(xmlGenericErrorContext,
2733 "from index %d of ->", startindex);
2734 xmlDebugDumpString(stdout, start->content);
2735 xmlGenericError(xmlGenericErrorContext, "\n");
2736 xmlGenericError(xmlGenericErrorContext,
2737 "to index %d of ->", endindex);
2738 xmlDebugDumpString(stdout, end->content);
2739 xmlGenericError(xmlGenericErrorContext, "\n");
2740 #endif
2741 do {
2742 fend = end;
2743 fendindex = endindex;
2744 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2745 &fend, &fendindex);
2746 if (found == 1) {
2747 if (position == NULL) {
2748 xmlXPtrLocationSetAdd(newset,
2749 xmlXPtrNewRange(start, startindex, fend, fendindex));
2750 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2751 pos - 1) == 0) {
2752 if ((number != NULL) && (num > 0)) {
2753 int rindx;
2754 xmlNodePtr rend;
2755 rend = start;
2756 rindx = startindex - 1;
2757 if (xmlXPtrAdvanceChar(&rend, &rindx,
2758 num) == 0) {
2759 xmlXPtrLocationSetAdd(newset,
2760 xmlXPtrNewRange(start, startindex,
2761 rend, rindx));
2763 } else if ((number != NULL) && (num <= 0)) {
2764 xmlXPtrLocationSetAdd(newset,
2765 xmlXPtrNewRange(start, startindex,
2766 start, startindex));
2767 } else {
2768 xmlXPtrLocationSetAdd(newset,
2769 xmlXPtrNewRange(start, startindex,
2770 fend, fendindex));
2773 start = fend;
2774 startindex = fendindex;
2775 if (string->stringval[0] == 0)
2776 startindex++;
2778 } while (found == 1);
2782 * Save the new value and cleanup
2784 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2785 xmlXPathFreeObject(set);
2786 xmlXPathFreeObject(string);
2787 if (position) xmlXPathFreeObject(position);
2788 if (number) xmlXPathFreeObject(number);
2792 * xmlXPtrEvalRangePredicate:
2793 * @ctxt: the XPointer Parser context
2795 * [8] Predicate ::= '[' PredicateExpr ']'
2796 * [9] PredicateExpr ::= Expr
2798 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2799 * a Location Set instead of a node set
2801 void
2802 xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2803 const xmlChar *cur;
2804 xmlXPathObjectPtr res;
2805 xmlXPathObjectPtr obj, tmp;
2806 xmlLocationSetPtr newset = NULL;
2807 xmlLocationSetPtr oldset;
2808 int i;
2810 SKIP_BLANKS;
2811 if (CUR != '[') {
2812 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2814 NEXT;
2815 SKIP_BLANKS;
2818 * Extract the old set, and then evaluate the result of the
2819 * expression for all the element in the set. use it to grow
2820 * up a new set.
2822 CHECK_TYPE(XPATH_LOCATIONSET);
2823 obj = valuePop(ctxt);
2824 oldset = obj->user;
2825 ctxt->context->node = NULL;
2827 if ((oldset == NULL) || (oldset->locNr == 0)) {
2828 ctxt->context->contextSize = 0;
2829 ctxt->context->proximityPosition = 0;
2830 xmlXPathEvalExpr(ctxt);
2831 res = valuePop(ctxt);
2832 if (res != NULL)
2833 xmlXPathFreeObject(res);
2834 valuePush(ctxt, obj);
2835 CHECK_ERROR;
2836 } else {
2838 * Save the expression pointer since we will have to evaluate
2839 * it multiple times. Initialize the new set.
2841 cur = ctxt->cur;
2842 newset = xmlXPtrLocationSetCreate(NULL);
2844 for (i = 0; i < oldset->locNr; i++) {
2845 ctxt->cur = cur;
2848 * Run the evaluation with a node list made of a single item
2849 * in the nodeset.
2851 ctxt->context->node = oldset->locTab[i]->user;
2852 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2853 valuePush(ctxt, tmp);
2854 ctxt->context->contextSize = oldset->locNr;
2855 ctxt->context->proximityPosition = i + 1;
2857 xmlXPathEvalExpr(ctxt);
2858 CHECK_ERROR;
2861 * The result of the evaluation need to be tested to
2862 * decided whether the filter succeeded or not
2864 res = valuePop(ctxt);
2865 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2866 xmlXPtrLocationSetAdd(newset,
2867 xmlXPathObjectCopy(oldset->locTab[i]));
2871 * Cleanup
2873 if (res != NULL)
2874 xmlXPathFreeObject(res);
2875 if (ctxt->value == tmp) {
2876 res = valuePop(ctxt);
2877 xmlXPathFreeObject(res);
2880 ctxt->context->node = NULL;
2884 * The result is used as the new evaluation set.
2886 xmlXPathFreeObject(obj);
2887 ctxt->context->node = NULL;
2888 ctxt->context->contextSize = -1;
2889 ctxt->context->proximityPosition = -1;
2890 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2892 if (CUR != ']') {
2893 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2896 NEXT;
2897 SKIP_BLANKS;
2900 #else
2901 #endif