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.
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.
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 */
40 #ifdef LIBXML_DEBUG_ENABLED
41 #include <libxml/debugXML.h>
46 xmlGenericError(xmlGenericErrorContext, \
47 "Unimplemented block at %s:%d\n", \
51 xmlGenericError(xmlGenericErrorContext, \
52 "Internal error at %s:%d\n", \
55 /************************************************************************
57 * A few helper functions for child sequences *
59 ************************************************************************/
61 xmlNodePtr
xmlXPtrAdvanceNode(xmlNodePtr cur
);
66 * Returns the number of child for an element, -1 in case of error
69 xmlXPtrGetArity(xmlNodePtr cur
) {
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
)) {
88 * Returns the index of the node in its parent children list, -1
92 xmlXPtrGetIndex(xmlNodePtr cur
) {
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
)) {
107 * xmlXPtrGetNthChild:
109 * @no: the child number
111 * Returns the @no'th element child of @cur or NULL
114 xmlXPtrGetNthChild(xmlNodePtr cur
, int no
) {
119 for (i
= 0;i
<= no
;cur
= cur
->next
) {
122 if ((cur
->type
== XML_ELEMENT_NODE
) ||
123 (cur
->type
== XML_DOCUMENT_NODE
) ||
124 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
133 /************************************************************************
135 * Handling of XPointer specific types *
137 ************************************************************************/
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
152 xmlXPtrCmpPoints(xmlNodePtr node1
, int index1
, xmlNodePtr node2
, int index2
) {
153 if ((node1
== NULL
) || (node2
== NULL
))
156 * a couple of optimizations which will avoid computations in most cases
158 if (node1
== node2
) {
165 return(xmlXPathCmpNodes(node1
, node2
));
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
;
186 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
188 xmlGenericError(xmlGenericErrorContext
,
189 "xmlXPtrNewPoint: out of memory\n");
192 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
193 ret
->type
= XPATH_POINT
;
194 ret
->user
= (void *) node
;
200 * xmlXPtrRangeCheckOrder:
201 * @range: an object range
203 * Make sure the points in the range are in the right order
206 xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range
) {
211 if (range
->type
!= XPATH_RANGE
)
213 if (range
->user2
== NULL
)
215 tmp
= xmlXPtrCmpPoints(range
->user
, range
->index
,
216 range
->user2
, range
->index2
);
219 range
->user
= range
->user2
;
222 range
->index
= range
->index2
;
228 * xmlXPtrRangesEqual:
229 * @range1: the first range
230 * @range2: the second range
234 * Returns 1 if equal, 0 otherwise
237 xmlXPtrRangesEqual(xmlXPathObjectPtr range1
, xmlXPathObjectPtr range2
) {
238 if (range1
== range2
)
240 if ((range1
== NULL
) || (range2
== NULL
))
242 if (range1
->type
!= range2
->type
)
244 if (range1
->type
!= XPATH_RANGE
)
246 if (range1
->user
!= range2
->user
)
248 if (range1
->index
!= range2
->index
)
250 if (range1
->user2
!= range2
->user2
)
252 if (range1
->index2
!= range2
->index2
)
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.
269 xmlXPtrNewRange(xmlNodePtr start
, int startindex
,
270 xmlNodePtr end
, int endindex
) {
271 xmlXPathObjectPtr ret
;
282 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
284 xmlGenericError(xmlGenericErrorContext
,
285 "xmlXPtrNewRange: out of memory\n");
288 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
289 ret
->type
= XPATH_RANGE
;
291 ret
->index
= startindex
;
293 ret
->index2
= endindex
;
294 xmlXPtrRangeCheckOrder(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.
308 xmlXPtrNewRangePoints(xmlXPathObjectPtr start
, xmlXPathObjectPtr end
) {
309 xmlXPathObjectPtr ret
;
315 if (start
->type
!= XPATH_POINT
)
317 if (end
->type
!= XPATH_POINT
)
320 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
322 xmlGenericError(xmlGenericErrorContext
,
323 "xmlXPtrNewRangePoints: out of memory\n");
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
);
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.
346 xmlXPtrNewRangePointNode(xmlXPathObjectPtr start
, xmlNodePtr end
) {
347 xmlXPathObjectPtr ret
;
353 if (start
->type
!= XPATH_POINT
)
356 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
358 xmlGenericError(xmlGenericErrorContext
,
359 "xmlXPtrNewRangePointNode: out of memory\n");
362 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
363 ret
->type
= XPATH_RANGE
;
364 ret
->user
= start
->user
;
365 ret
->index
= start
->index
;
368 xmlXPtrRangeCheckOrder(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.
382 xmlXPtrNewRangeNodePoint(xmlNodePtr start
, xmlXPathObjectPtr end
) {
383 xmlXPathObjectPtr ret
;
389 if (start
->type
!= XPATH_POINT
)
391 if (end
->type
!= XPATH_POINT
)
394 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
396 xmlGenericError(xmlGenericErrorContext
,
397 "xmlXPtrNewRangeNodePoint: out of memory\n");
400 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
401 ret
->type
= XPATH_RANGE
;
404 ret
->user2
= end
->user
;
405 ret
->index2
= end
->index
;
406 xmlXPtrRangeCheckOrder(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.
420 xmlXPtrNewRangeNodes(xmlNodePtr start
, xmlNodePtr end
) {
421 xmlXPathObjectPtr ret
;
428 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
430 xmlGenericError(xmlGenericErrorContext
,
431 "xmlXPtrNewRangeNodes: out of memory\n");
434 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
435 ret
->type
= XPATH_RANGE
;
440 xmlXPtrRangeCheckOrder(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.
453 xmlXPtrNewCollapsedRange(xmlNodePtr start
) {
454 xmlXPathObjectPtr ret
;
459 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
461 xmlGenericError(xmlGenericErrorContext
,
462 "xmlXPtrNewCollapsedRange: out of memory\n");
465 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
466 ret
->type
= XPATH_RANGE
;
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.
484 xmlXPtrNewRangeNodeObject(xmlNodePtr start
, xmlXPathObjectPtr end
) {
485 xmlXPathObjectPtr ret
;
498 if (end
->nodesetval
->nodeNr
<= 0)
506 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
508 xmlGenericError(xmlGenericErrorContext
,
509 "xmlXPtrNewRangeNodeObject: out of memory\n");
512 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
513 ret
->type
= XPATH_RANGE
;
518 ret
->user2
= end
->user
;
519 ret
->index2
= end
->index
;
520 case XPATH_NODESET
: {
521 ret
->user2
= end
->nodesetval
->nodeTab
[end
->nodesetval
->nodeNr
- 1];
529 xmlXPtrRangeCheckOrder(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.
544 xmlXPtrLocationSetCreate(xmlXPathObjectPtr val
) {
545 xmlLocationSetPtr ret
;
547 ret
= (xmlLocationSetPtr
) xmlMalloc(sizeof(xmlLocationSet
));
549 xmlGenericError(xmlGenericErrorContext
,
550 "xmlXPtrLocationSetCreate: out of memory\n");
553 memset(ret
, 0 , (size_t) sizeof(xmlLocationSet
));
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");
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
;
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.
579 xmlXPtrLocationSetAdd(xmlLocationSetPtr cur
, xmlXPathObjectPtr val
) {
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
);
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");
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
;
612 temp
= (xmlXPathObjectPtr
*) xmlRealloc(cur
->locTab
, cur
->locMax
*
613 sizeof(xmlXPathObjectPtr
));
615 xmlGenericError(xmlGenericErrorContext
,
616 "xmlXPtrLocationSetAdd: out of memory\n");
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.
634 xmlXPtrLocationSetMerge(xmlLocationSetPtr val1
, xmlLocationSetPtr val2
) {
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
]);
652 * xmlXPtrLocationSetDel:
653 * @cur: the initial range set
654 * @val: an xmlXPathObjectPtr
656 * Removes an xmlXPathObjectPtr from an existing LocationSet
659 xmlXPtrLocationSetDel(xmlLocationSetPtr cur
, xmlXPathObjectPtr val
) {
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
) {
673 xmlGenericError(xmlGenericErrorContext
,
674 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
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.
692 xmlXPtrLocationSetRemove(xmlLocationSetPtr cur
, int val
) {
693 if (cur
== NULL
) return;
694 if (val
>= cur
->locNr
) return;
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 !).
708 xmlXPtrFreeLocationSet(xmlLocationSetPtr obj
) {
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
);
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.
732 xmlXPtrNewLocationSetNodes(xmlNodePtr start
, xmlNodePtr end
) {
733 xmlXPathObjectPtr ret
;
735 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
737 xmlGenericError(xmlGenericErrorContext
,
738 "xmlXPtrNewLocationSetNodes: out of memory\n");
741 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
742 ret
->type
= XPATH_LOCATIONSET
;
744 ret
->user
= xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start
));
746 ret
->user
= xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start
,end
));
751 * xmlXPtrNewLocationSetNodeSet:
754 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
755 * it with all the nodes from @set
757 * Returns the newly created object.
760 xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set
) {
761 xmlXPathObjectPtr ret
;
763 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
765 xmlGenericError(xmlGenericErrorContext
,
766 "xmlXPtrNewLocationSetNodeSet: out of memory\n");
769 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
770 ret
->type
= XPATH_LOCATIONSET
;
773 xmlLocationSetPtr newset
;
775 newset
= xmlXPtrLocationSetCreate(NULL
);
779 for (i
= 0;i
< set
->nodeNr
;i
++)
780 xmlXPtrLocationSetAdd(newset
,
781 xmlXPtrNewCollapsedRange(set
->nodeTab
[i
]));
783 ret
->user
= (void *) newset
;
789 * xmlXPtrWrapLocationSet:
790 * @val: the LocationSet value
792 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
794 * Returns the newly created object.
797 xmlXPtrWrapLocationSet(xmlLocationSetPtr val
) {
798 xmlXPathObjectPtr ret
;
800 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
802 xmlGenericError(xmlGenericErrorContext
,
803 "xmlXPtrWrapLocationSet: out of memory\n");
806 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
807 ret
->type
= XPATH_LOCATIONSET
;
808 ret
->user
= (void *) val
;
812 /************************************************************************
816 ************************************************************************/
819 * Macros for accessing the content. Those should be used only by the parser,
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)
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
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
));
874 cur
= xmlXPtrGetNthChild(oldset
->nodeTab
[0], indx
);
876 xmlXPathFreeObject(obj
);
877 valuePush(ctxt
, xmlXPathNewNodeSet(NULL
));
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
919 xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
920 xmlChar
*buffer
, *cur
;
925 name
= xmlXPathParseName(ctxt
);
927 XP_ERROR(XPATH_EXPR_ERROR
);
930 XP_ERROR(XPATH_EXPR_ERROR
);
934 len
= xmlStrlen(ctxt
->cur
);
936 buffer
= (xmlChar
*) xmlMalloc(len
* sizeof (xmlChar
));
937 if (buffer
== NULL
) {
938 xmlGenericError(xmlGenericErrorContext
,
939 "xmlXPtrEvalXPtrPart: out of memory\n");
952 } else if (CUR
== '(') {
955 } else if (CUR
== '^') {
957 if ((CUR
== ')') || (CUR
== '(') || (CUR
== '^')) {
970 if ((level
!= 0) && (CUR
== 0)) {
972 XP_ERROR(XPTR_SYNTAX_ERROR
);
975 if (xmlStrEqual(name
, (xmlChar
*) "xpointer")) {
976 const xmlChar
*left
= CUR_PTR
;
979 xmlXPathEvalExpr(ctxt
);
981 #ifdef XPTR_XMLNS_SCHEME
982 } else if (xmlStrEqual(name
, (xmlChar
*) "xmlns")) {
983 const xmlChar
*left
= CUR_PTR
;
989 prefix
= xmlXPathParseNCName(ctxt
);
990 if (prefix
== NULL
) {
993 XP_ERROR(XPTR_SYNTAX_ERROR
);
1000 XP_ERROR(XPTR_SYNTAX_ERROR
);
1004 /* @@ check escaping in the XPointer WD */
1006 value
= xmlParseURI((const char *)ctxt
->cur
);
1007 if (value
== NULL
) {
1011 XP_ERROR(XPTR_SYNTAX_ERROR
);
1013 URI
= xmlSaveUri(value
);
1019 XP_ERROR(XPATH_MEMORY_ERROR
);
1022 xmlXPathRegisterNs(ctxt
->context
, prefix
, URI
);
1026 #endif /* XPTR_XMLNS_SCHEME */
1028 xmlGenericError(xmlGenericErrorContext
,
1029 "unsupported scheme '%s'\n", 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:
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.
1059 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1060 * expressions or other schemes.
1063 xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
1065 name
= xmlXPathParseName(ctxt
);
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
)
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))
1089 case XPATH_NODESET
: {
1090 xmlNodeSetPtr loc
= ctxt
->value
->nodesetval
;
1091 if ((loc
!= NULL
) && (loc
->nodeNr
> 0))
1100 * Evaluating to improper values is equivalent to
1101 * a sub-resource error, clean-up the stack
1104 obj
= valuePop(ctxt
);
1106 xmlXPathFreeObject(obj
);
1108 } while (obj
!= NULL
);
1112 * Is there another XPointer part.
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.
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");
1142 valuePush(ctxt
, xmlXPathNewString(name
));
1144 xmlXPathIdFunction(ctxt
, 1);
1148 while (CUR
== '/') {
1152 while ((CUR
>= '0') && (CUR
<= '9')) {
1153 child
= child
* 10 + (CUR
- '0');
1156 xmlXPtrGetChildNo(ctxt
, child
);
1162 * xmlXPtrEvalXPointer:
1163 * @ctxt: the XPointer Parser context
1169 * Parse and evaluate an XPointer
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
) {
1179 xmlGenericError(xmlGenericErrorContext
,
1180 "xmlXPathEvalXPointer: out of memory\n");
1184 ctxt
->valueMax
= 10;
1190 xmlXPtrEvalChildSeq(ctxt
, NULL
);
1194 name
= xmlXPathParseName(ctxt
);
1196 XP_ERROR(XPATH_EXPR_ERROR
);
1198 xmlXPtrEvalFullXPtr(ctxt
, name
);
1199 /* Short evaluation */
1202 /* this handle both Bare Names and Child Sequences */
1203 xmlXPtrEvalChildSeq(ctxt
, name
);
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.
1238 xmlXPtrNewContext(xmlDocPtr doc
, xmlNodePtr here
, xmlNodePtr origin
) {
1239 xmlXPathContextPtr ret
;
1241 ret
= xmlXPathNewContext(doc
);
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
);
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.
1279 xmlXPtrEval(const xmlChar
*str
, xmlXPathContextPtr ctx
) {
1280 xmlXPathParserContextPtr ctxt
;
1281 xmlXPathObjectPtr res
= NULL
, tmp
;
1282 xmlXPathObjectPtr init
= NULL
;
1287 if ((ctx
== NULL
) || (str
== NULL
))
1290 ctxt
= xmlXPathNewParserContext(str
, ctx
);
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");
1300 res
= valuePop(ctxt
);
1304 tmp
= valuePop(ctxt
);
1307 if (tmp
->type
== XPATH_NODESET
) {
1309 * Evaluation may push a root nodeset which is unused
1312 set
= tmp
->nodesetval
;
1313 if ((set
->nodeNr
!= 1) ||
1314 (set
->nodeTab
[0] != (xmlNodePtr
) ctx
->doc
))
1319 xmlXPathFreeObject(tmp
);
1321 } while (tmp
!= NULL
);
1323 xmlGenericError(xmlGenericErrorContext
,
1324 "xmlXPtrEval: %d object left on the stack\n",
1327 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
1328 xmlXPathFreeObject(res
);
1332 xmlXPathFreeParserContext(ctxt
);
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.
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
;
1355 if (range
->type
!= XPATH_RANGE
)
1357 start
= (xmlNodePtr
) range
->user
;
1363 return(xmlCopyNode(start
, 1));
1366 index1
= range
->index
;
1367 index2
= range
->index2
;
1368 while (cur
!= NULL
) {
1370 if (cur
->type
== XML_TEXT_NODE
) {
1371 const xmlChar
*content
= cur
->content
;
1374 if (content
== NULL
) {
1375 tmp
= xmlNewTextLen(NULL
, 0);
1378 if ((cur
== start
) && (index1
> 1)) {
1379 content
+= (index1
- 1);
1380 len
-= (index1
- 1);
1385 tmp
= xmlNewTextLen(content
, len
);
1387 /* single sub text node selection */
1390 /* prune and return full set */
1392 xmlAddNextSibling(last
, tmp
);
1394 xmlAddChild(parent
, tmp
);
1397 tmp
= xmlCopyNode(cur
, 0);
1402 xmlAddNextSibling(last
, tmp
);
1404 xmlAddChild(parent
, tmp
);
1410 end
= xmlXPtrGetNthChild(cur
, index2
- 1);
1413 if ((cur
== start
) && (index1
> 1)) {
1414 cur
= xmlXPtrGetNthChild(cur
, index1
- 1);
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);
1434 content
+= (index1
- 1);
1436 tmp
= xmlNewText(content
);
1440 if ((cur
== start
) && (index1
> 1)) {
1441 tmp
= xmlCopyNode(cur
, 0);
1445 cur
= xmlXPtrGetNthChild(cur
, index1
- 1);
1448 * Now gather the remaining nodes from cur to end
1450 continue; /* while */
1452 tmp
= xmlCopyNode(cur
, 1);
1459 switch (cur
->type
) {
1461 case XML_ELEMENT_DECL
:
1462 case XML_ATTRIBUTE_DECL
:
1463 case XML_ENTITY_NODE
:
1464 /* Do not copy DTD informations */
1466 case XML_ENTITY_DECL
:
1467 TODO
/* handle crossing entities -> stack needed */
1469 case XML_XINCLUDE_START
:
1470 case XML_XINCLUDE_END
:
1471 /* don't consider it part of the tree content */
1473 case XML_ATTRIBUTE_NODE
:
1474 /* Humm, should not happen ! */
1478 tmp
= xmlCopyNode(cur
, 1);
1482 if ((list
== NULL
) || ((last
== NULL
) && (parent
== NULL
))) {
1487 xmlAddNextSibling(last
, tmp
);
1489 xmlAddChild(parent
, tmp
);
1495 * Skip to next node in document order
1497 if ((list
== NULL
) || ((last
== NULL
) && (parent
== NULL
))) {
1501 cur
= xmlXPtrAdvanceNode(cur
);
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.
1517 xmlXPtrBuildNodeList(xmlXPathObjectPtr obj
) {
1518 xmlNodePtr list
= NULL
, last
= NULL
;
1523 switch (obj
->type
) {
1524 case XPATH_NODESET
: {
1525 xmlNodeSetPtr set
= obj
->nodesetval
;
1528 for (i
= 0;i
< set
->nodeNr
;i
++) {
1529 if (set
->nodeTab
[i
] == NULL
)
1531 switch (set
->nodeTab
[i
]->type
) {
1533 case XML_CDATA_SECTION_NODE
:
1534 case XML_ELEMENT_NODE
:
1535 case XML_ENTITY_REF_NODE
:
1536 case XML_ENTITY_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
:
1544 case XML_XINCLUDE_START
:
1545 case XML_XINCLUDE_END
:
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
:
1553 case XML_ELEMENT_DECL
:
1554 case XML_ATTRIBUTE_DECL
:
1555 case XML_ENTITY_DECL
:
1559 list
= last
= xmlCopyNode(set
->nodeTab
[i
], 1);
1561 xmlAddNextSibling(last
, xmlCopyNode(set
->nodeTab
[i
], 1));
1562 if (last
->next
!= NULL
)
1568 case XPATH_LOCATIONSET
: {
1569 xmlLocationSetPtr set
= (xmlLocationSetPtr
) obj
->user
;
1572 for (i
= 0;i
< set
->locNr
;i
++) {
1574 list
= last
= xmlXPtrBuildNodeList(set
->locTab
[i
]);
1576 xmlAddNextSibling(last
,
1577 xmlXPtrBuildNodeList(set
->locTab
[i
]));
1579 while (last
->next
!= NULL
)
1586 return(xmlXPtrBuildRangeNodeList(obj
));
1588 return(xmlCopyNode(obj
->user
, 0));
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
1611 xmlXPtrNbLocChildren(xmlNodePtr node
) {
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
)
1626 case XML_ATTRIBUTE_NODE
:
1630 case XML_COMMENT_NODE
:
1632 case XML_CDATA_SECTION_NODE
:
1633 case XML_ENTITY_REF_NODE
:
1634 ret
= xmlStrlen(node
->content
);
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
1651 xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
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
1669 xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
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
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
1702 xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1703 xmlXPathObjectPtr tmp
, obj
, point
;
1704 xmlLocationSetPtr newset
= NULL
;
1705 xmlLocationSetPtr oldset
= NULL
;
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
);
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
) {
1732 for (i
= 0; i
< oldset
->locNr
; i
++) {
1733 tmp
= oldset
->locTab
[i
];
1737 switch (tmp
->type
) {
1739 point
= xmlXPtrNewPoint(tmp
->user
, tmp
->index
);
1742 xmlNodePtr node
= tmp
->user
;
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
);
1755 /*** Should we raise an error ?
1756 xmlXPathFreeObject(obj);
1757 xmlXPathFreeObject(newset);
1758 XP_ERROR(XPATH_INVALID_TYPE)
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
1793 * ----------------------------
1796 xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1797 xmlXPathObjectPtr tmp
, obj
, point
;
1798 xmlLocationSetPtr newset
= NULL
;
1799 xmlLocationSetPtr oldset
= NULL
;
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
);
1817 newset
= xmlXPtrLocationSetCreate(NULL
);
1818 oldset
= (xmlLocationSetPtr
) obj
->user
;
1819 if (oldset
!= NULL
) {
1822 for (i
= 0; i
< oldset
->locNr
; i
++) {
1823 tmp
= oldset
->locTab
[i
];
1827 switch (tmp
->type
) {
1829 point
= xmlXPtrNewPoint(tmp
->user
, tmp
->index
);
1832 xmlNodePtr node
= tmp
->user2
;
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
));
1848 /*** Should we raise an error ?
1849 xmlXPathFreeObject(obj);
1850 xmlXPathFreeObject(newset);
1851 XP_ERROR(XPATH_INVALID_TYPE)
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
) {
1879 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
) ||
1880 (ctxt
->context
->doc
== NULL
))
1882 switch (loc
->type
) {
1884 return(xmlXPtrNewRange(loc
->user
, loc
->index
,
1885 loc
->user
, loc
->index
));
1887 if (loc
->user2
!= NULL
) {
1888 return(xmlXPtrNewRange(loc
->user
, loc
->index
,
1889 loc
->user2
, loc
->index2
));
1891 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
1892 if (node
== (xmlNodePtr
) ctxt
->context
->doc
) {
1893 return(xmlXPtrNewRange(node
, 0, node
,
1894 xmlXPtrGetArity(node
)));
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
:
1903 case XML_CDATA_SECTION_NODE
:
1904 case XML_ENTITY_REF_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,
1922 TODO
/* missed one case ??? */
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.
1941 xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1943 xmlXPathObjectPtr set
;
1944 xmlLocationSetPtr oldset
;
1945 xmlLocationSetPtr newset
;
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
);
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
) {
1995 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
) ||
1996 (ctxt
->context
->doc
== NULL
))
1998 switch (loc
->type
) {
2000 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
2001 switch (node
->type
) {
2003 case XML_COMMENT_NODE
:
2005 case XML_CDATA_SECTION_NODE
: {
2006 if (node
->content
== NULL
) {
2007 return(xmlXPtrNewRange(node
, 0, node
, 0));
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
)));
2028 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
2029 if (loc
->user2
!= NULL
) {
2030 return(xmlXPtrNewRange(node
, loc
->index
,
2031 loc
->user2
, loc
->index2
));
2033 switch (node
->type
) {
2035 case XML_COMMENT_NODE
:
2037 case XML_CDATA_SECTION_NODE
: {
2038 if (node
->content
== NULL
) {
2039 return(xmlXPtrNewRange(node
, 0, node
, 0));
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
)));
2061 TODO
/* missed one case ??? */
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.
2087 xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2089 xmlXPathObjectPtr set
;
2090 xmlLocationSetPtr oldset
;
2091 xmlLocationSetPtr newset
;
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
);
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
2136 xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2137 xmlXPathObjectPtr range
;
2139 xmlXPathObjectPtr res
, obj
;
2140 xmlXPathObjectPtr tmp
;
2141 xmlLocationSetPtr newset
= NULL
;
2142 xmlNodeSetPtr oldset
;
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
;
2156 newset
= xmlXPtrLocationSetCreate(NULL
);
2158 for (i
= 0; i
< oldset
->nodeNr
; i
++) {
2162 * Run the evaluation with a node list made of a single item
2165 ctxt
->context
->node
= oldset
->nodeTab
[i
];
2166 tmp
= xmlXPathNewNodeSet(ctxt
->context
->node
);
2167 valuePush(ctxt
, tmp
);
2169 xmlXPathEvalExpr(ctxt
);
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
);
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:
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
2215 xmlXPtrAdvanceNode(xmlNodePtr cur
) {
2219 if (cur
->children
!= NULL
) {
2220 cur
= cur
->children
;
2223 if (cur
->next
!= NULL
) {
2229 if (cur
== NULL
) return(NULL
);
2230 if (cur
->next
!= NULL
) {
2234 } while (cur
!= NULL
);
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
))
2243 if (cur
->type
== XML_ENTITY_REF_NODE
) {
2250 * xmlXPtrAdvanceChar:
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
2260 xmlXPtrAdvanceChar(xmlNodePtr
*node
, int *indx
, int bytes
) {
2265 if ((node
== NULL
) || (indx
== NULL
))
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
))) {
2282 cur
= xmlXPtrGetNthChild(cur
, pos
);
2285 cur
= xmlXPtrAdvanceNode(cur
);
2297 * if there is no move needed return the current value.
2299 if (pos
== 0) pos
= 1;
2306 * We should have a text (or cdata) node ...
2309 if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2310 (cur
->content
!= NULL
)) {
2311 len
= xmlStrlen(cur
->content
);
2314 /* Strange, the indx in the text node is greater than it's len */
2318 if (pos
+ bytes
>= len
) {
2319 bytes
-= (len
- pos
);
2320 cur
= xmlXPtrAdvanceNode(cur
);
2322 } else if (pos
+ bytes
< len
) {
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
2349 xmlXPtrMatchString(const xmlChar
*string
, xmlNodePtr start
, int startindex
,
2350 xmlNodePtr
*end
, int *endindex
) {
2352 int pos
; /* 0 based */
2353 int len
; /* in bytes */
2354 int stringlen
; /* in bytes */
2361 if ((end
== NULL
) || (endindex
== NULL
))
2366 pos
= startindex
- 1;
2367 stringlen
= xmlStrlen(string
);
2369 while (stringlen
> 0) {
2370 if ((cur
== *end
) && (pos
+ stringlen
> *endindex
))
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
));
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");
2386 *endindex
= pos
+ stringlen
;
2392 int sub
= len
- pos
;
2393 match
= (!xmlStrncmp(&cur
->content
[pos
], string
, sub
));
2396 xmlGenericError(xmlGenericErrorContext
,
2397 "found subrange %d bytes at index %d of ->",
2399 xmlDebugDumpString(stdout
, cur
->content
);
2400 xmlGenericError(xmlGenericErrorContext
, "\n");
2402 string
= &string
[sub
];
2409 cur
= xmlXPtrAdvanceNode(cur
);
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
2434 xmlXPtrSearchString(const xmlChar
*string
, xmlNodePtr
*start
, int *startindex
,
2435 xmlNodePtr
*end
, int *endindex
) {
2438 int pos
; /* 0 based */
2439 int len
; /* in bytes */
2444 if ((start
== NULL
) || (startindex
== NULL
))
2446 if ((end
== NULL
) || (endindex
== NULL
))
2451 pos
= *startindex
- 1;
2454 while (cur
!= NULL
) {
2455 if ((cur
->type
!= XML_ELEMENT_NODE
) && (cur
->content
!= NULL
)) {
2456 len
= xmlStrlen(cur
->content
);
2457 while (pos
<= len
) {
2459 str
= xmlStrchr(&cur
->content
[pos
], first
);
2461 pos
= (str
- (xmlChar
*)(cur
->content
));
2463 xmlGenericError(xmlGenericErrorContext
,
2464 "found '%c' at index %d of ->",
2466 xmlDebugDumpString(stdout
, cur
->content
);
2467 xmlGenericError(xmlGenericErrorContext
, "\n");
2469 if (xmlXPtrMatchString(string
, cur
, pos
+ 1,
2472 *startindex
= pos
+ 1;
2481 * An empty string is considered to match before each
2482 * character of the string-value and after the final
2486 xmlGenericError(xmlGenericErrorContext
,
2487 "found '' at index %d of ->",
2489 xmlDebugDumpString(stdout
, cur
->content
);
2490 xmlGenericError(xmlGenericErrorContext
, "\n");
2493 *startindex
= pos
+ 1;
2495 *endindex
= pos
+ 1;
2500 if ((cur
== *end
) && (pos
>= *endindex
))
2502 cur
= xmlXPtrAdvanceNode(cur
);
2511 * xmlXPtrGetLastChar:
2515 * Computes the point coordinates of the last char of this point
2517 * Returns -1 in case of failure, 0 otherwise
2520 xmlXPtrGetLastChar(xmlNodePtr
*node
, int *indx
) {
2524 if ((node
== NULL
) || (indx
== NULL
))
2532 if ((cur
->type
== XML_ELEMENT_NODE
) ||
2533 (cur
->type
== XML_DOCUMENT_NODE
) ||
2534 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
2536 cur
= xmlXPtrGetNthChild(cur
, pos
);
2540 while (cur
!= NULL
) {
2541 if (cur
->last
!= NULL
)
2543 else if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2544 (cur
->content
!= NULL
)) {
2545 len
= xmlStrlen(cur
->content
);
2559 * xmlXPtrGetStartPoint:
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
2569 xmlXPtrGetStartPoint(xmlXPathObjectPtr obj
, xmlNodePtr
*node
, int *indx
) {
2570 if ((obj
== NULL
) || (node
== NULL
) || (indx
== NULL
))
2573 switch (obj
->type
) {
2576 if (obj
->index
<= 0)
2583 if (obj
->index
<= 0)
2595 * xmlXPtrGetEndPoint:
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
2605 xmlXPtrGetEndPoint(xmlXPathObjectPtr obj
, xmlNodePtr
*node
, int *indx
) {
2606 if ((obj
== NULL
) || (node
== NULL
) || (indx
== NULL
))
2609 switch (obj
->type
) {
2612 if (obj
->index
<= 0)
2619 if (obj
->index
<= 0)
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
2662 * The points of the range-locations in the returned location-set will
2663 * all be character points.
2664 * ------------------------------
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
);
2685 CHECK_TYPE(XPATH_NUMBER
);
2686 number
= valuePop(ctxt
);
2688 num
= (int) number
->floatval
;
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
);
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
++) {
2723 xmlXPathDebugDumpObject(stdout
, oldset
->locTab
[i
], 0);
2726 xmlXPtrGetStartPoint(oldset
->locTab
[i
], &start
, &startindex
);
2727 xmlXPtrGetEndPoint(oldset
->locTab
[i
], &end
, &endindex
);
2728 xmlXPtrAdvanceChar(&start
, &startindex
, 0);
2729 xmlXPtrGetLastChar(&end
, &endindex
);
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");
2743 fendindex
= endindex
;
2744 found
= xmlXPtrSearchString(string
->stringval
, &start
, &startindex
,
2747 if (position
== NULL
) {
2748 xmlXPtrLocationSetAdd(newset
,
2749 xmlXPtrNewRange(start
, startindex
, fend
, fendindex
));
2750 } else if (xmlXPtrAdvanceChar(&start
, &startindex
,
2752 if ((number
!= NULL
) && (num
> 0)) {
2756 rindx
= startindex
- 1;
2757 if (xmlXPtrAdvanceChar(&rend
, &rindx
,
2759 xmlXPtrLocationSetAdd(newset
,
2760 xmlXPtrNewRange(start
, startindex
,
2763 } else if ((number
!= NULL
) && (num
<= 0)) {
2764 xmlXPtrLocationSetAdd(newset
,
2765 xmlXPtrNewRange(start
, startindex
,
2766 start
, startindex
));
2768 xmlXPtrLocationSetAdd(newset
,
2769 xmlXPtrNewRange(start
, startindex
,
2774 startindex
= fendindex
;
2775 if (string
->stringval
[0] == 0)
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
2802 xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt
) {
2804 xmlXPathObjectPtr res
;
2805 xmlXPathObjectPtr obj
, tmp
;
2806 xmlLocationSetPtr newset
= NULL
;
2807 xmlLocationSetPtr oldset
;
2812 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);
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
2822 CHECK_TYPE(XPATH_LOCATIONSET
);
2823 obj
= valuePop(ctxt
);
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
);
2833 xmlXPathFreeObject(res
);
2834 valuePush(ctxt
, obj
);
2838 * Save the expression pointer since we will have to evaluate
2839 * it multiple times. Initialize the new set.
2842 newset
= xmlXPtrLocationSetCreate(NULL
);
2844 for (i
= 0; i
< oldset
->locNr
; i
++) {
2848 * Run the evaluation with a node list made of a single item
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
);
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
]));
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
));
2893 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);