2 * xpointer.c : Code to handle XML Pointer
4 * Base implementation was made accordingly to
5 * W3C Candidate Recommendation 7 June 2000
6 * http://www.w3.org/TR/2000/CR-xptr-20000607
8 * Added support for the element() scheme described in:
9 * W3C Proposed Recommendation 13 November 2002
10 * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
12 * See Copyright for the status of this software.
17 /* To avoid EBCDIC trouble when parsing on zOS */
19 #pragma convert("ISO8859-1")
26 * TODO: better handling of error cases, the full expression should
27 * be parsed beforehand instead of a progressive evaluation
28 * TODO: Access into entities references are not supported now ...
29 * need a start to be able to pop out of entities refs since
30 * parent is the entity declaration, not the ref.
34 #include <libxml/xpointer.h>
35 #include <libxml/xmlmemory.h>
36 #include <libxml/parserInternals.h>
37 #include <libxml/uri.h>
38 #include <libxml/xpath.h>
39 #include <libxml/xpathInternals.h>
40 #include <libxml/xmlerror.h>
41 #include <libxml/globals.h>
43 #ifdef LIBXML_XPTR_ENABLED
45 /* Add support of the xmlns() xpointer scheme to initialize the namespaces */
46 #define XPTR_XMLNS_SCHEME
48 /* #define DEBUG_RANGES */
50 #ifdef LIBXML_DEBUG_ENABLED
51 #include <libxml/debugXML.h>
56 xmlGenericError(xmlGenericErrorContext, \
57 "Unimplemented block at %s:%d\n", \
61 xmlGenericError(xmlGenericErrorContext, \
62 "Internal error at %s:%d\n", \
65 /************************************************************************
67 * Some factorized error routines *
69 ************************************************************************/
73 * @extra: extra information
75 * Handle a redefinition of attribute error
78 xmlXPtrErrMemory(const char *extra
)
80 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_XPOINTER
,
81 XML_ERR_NO_MEMORY
, XML_ERR_ERROR
, NULL
, 0, extra
,
83 "Memory allocation failed : %s\n", extra
);
88 * @ctxt: an XPTR evaluation context
89 * @extra: extra information
91 * Handle a redefinition of attribute error
93 static void LIBXML_ATTR_FORMAT(3,0)
94 xmlXPtrErr(xmlXPathParserContextPtr ctxt
, int error
,
95 const char * msg
, const xmlChar
*extra
)
99 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) {
100 __xmlRaiseError(NULL
, NULL
, NULL
,
101 NULL
, NULL
, XML_FROM_XPOINTER
, error
,
102 XML_ERR_ERROR
, NULL
, 0,
103 (const char *) extra
, NULL
, NULL
, 0, 0,
108 /* cleanup current last error */
109 xmlResetError(&ctxt
->context
->lastError
);
111 ctxt
->context
->lastError
.domain
= XML_FROM_XPOINTER
;
112 ctxt
->context
->lastError
.code
= error
;
113 ctxt
->context
->lastError
.level
= XML_ERR_ERROR
;
114 ctxt
->context
->lastError
.str1
= (char *) xmlStrdup(ctxt
->base
);
115 ctxt
->context
->lastError
.int1
= ctxt
->cur
- ctxt
->base
;
116 ctxt
->context
->lastError
.node
= ctxt
->context
->debugNode
;
117 if (ctxt
->context
->error
!= NULL
) {
118 ctxt
->context
->error(ctxt
->context
->userData
,
119 &ctxt
->context
->lastError
);
121 __xmlRaiseError(NULL
, NULL
, NULL
,
122 NULL
, ctxt
->context
->debugNode
, XML_FROM_XPOINTER
,
123 error
, XML_ERR_ERROR
, NULL
, 0,
124 (const char *) extra
, (const char *) ctxt
->base
, NULL
,
125 ctxt
->cur
- ctxt
->base
, 0,
130 /************************************************************************
132 * A few helper functions for child sequences *
134 ************************************************************************/
135 /* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
136 xmlNodePtr
xmlXPtrAdvanceNode(xmlNodePtr cur
, int *level
);
141 * Returns the number of child for an element, -1 in case of error
144 xmlXPtrGetArity(xmlNodePtr cur
) {
146 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
149 for (i
= 0;cur
!= NULL
;cur
= cur
->next
) {
150 if ((cur
->type
== XML_ELEMENT_NODE
) ||
151 (cur
->type
== XML_DOCUMENT_NODE
) ||
152 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
163 * Returns the index of the node in its parent children list, -1
167 xmlXPtrGetIndex(xmlNodePtr cur
) {
169 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
171 for (i
= 1;cur
!= NULL
;cur
= cur
->prev
) {
172 if ((cur
->type
== XML_ELEMENT_NODE
) ||
173 (cur
->type
== XML_DOCUMENT_NODE
) ||
174 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
182 * xmlXPtrGetNthChild:
184 * @no: the child number
186 * Returns the @no'th element child of @cur or NULL
189 xmlXPtrGetNthChild(xmlNodePtr cur
, int no
) {
191 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
194 for (i
= 0;i
<= no
;cur
= cur
->next
) {
197 if ((cur
->type
== XML_ELEMENT_NODE
) ||
198 (cur
->type
== XML_DOCUMENT_NODE
) ||
199 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
208 /************************************************************************
210 * Handling of XPointer specific types *
212 ************************************************************************/
216 * @node1: the first node
217 * @index1: the first index
218 * @node2: the second node
219 * @index2: the second index
221 * Compare two points w.r.t document order
223 * Returns -2 in case of error 1 if first point < second point, 0 if
224 * that's the same point, -1 otherwise
227 xmlXPtrCmpPoints(xmlNodePtr node1
, int index1
, xmlNodePtr node2
, int index2
) {
228 if ((node1
== NULL
) || (node2
== NULL
))
231 * a couple of optimizations which will avoid computations in most cases
233 if (node1
== node2
) {
240 return(xmlXPathCmpNodes(node1
, node2
));
245 * @node: the xmlNodePtr
246 * @indx: the indx within the node
248 * Create a new xmlXPathObjectPtr of type point
250 * Returns the newly created object.
252 static xmlXPathObjectPtr
253 xmlXPtrNewPoint(xmlNodePtr node
, int indx
) {
254 xmlXPathObjectPtr ret
;
261 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
263 xmlXPtrErrMemory("allocating point");
266 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
267 ret
->type
= XPATH_POINT
;
268 ret
->user
= (void *) node
;
274 * xmlXPtrRangeCheckOrder:
275 * @range: an object range
277 * Make sure the points in the range are in the right order
280 xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range
) {
285 if (range
->type
!= XPATH_RANGE
)
287 if (range
->user2
== NULL
)
289 tmp
= xmlXPtrCmpPoints(range
->user
, range
->index
,
290 range
->user2
, range
->index2
);
293 range
->user
= range
->user2
;
296 range
->index
= range
->index2
;
302 * xmlXPtrRangesEqual:
303 * @range1: the first range
304 * @range2: the second range
308 * Returns 1 if equal, 0 otherwise
311 xmlXPtrRangesEqual(xmlXPathObjectPtr range1
, xmlXPathObjectPtr range2
) {
312 if (range1
== range2
)
314 if ((range1
== NULL
) || (range2
== NULL
))
316 if (range1
->type
!= range2
->type
)
318 if (range1
->type
!= XPATH_RANGE
)
320 if (range1
->user
!= range2
->user
)
322 if (range1
->index
!= range2
->index
)
324 if (range1
->user2
!= range2
->user2
)
326 if (range1
->index2
!= range2
->index2
)
332 * xmlXPtrNewRangeInternal:
333 * @start: the starting node
334 * @startindex: the start index
335 * @end: the ending point
336 * @endindex: the ending index
338 * Internal function to create a new xmlXPathObjectPtr of type range
340 * Returns the newly created object.
342 static xmlXPathObjectPtr
343 xmlXPtrNewRangeInternal(xmlNodePtr start
, int startindex
,
344 xmlNodePtr end
, int endindex
) {
345 xmlXPathObjectPtr ret
;
348 * Namespace nodes must be copied (see xmlXPathNodeSetDupNs).
349 * Disallow them for now.
351 if ((start
!= NULL
) && (start
->type
== XML_NAMESPACE_DECL
))
353 if ((end
!= NULL
) && (end
->type
== XML_NAMESPACE_DECL
))
356 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
358 xmlXPtrErrMemory("allocating range");
361 memset(ret
, 0, sizeof(xmlXPathObject
));
362 ret
->type
= XPATH_RANGE
;
364 ret
->index
= startindex
;
366 ret
->index2
= endindex
;
372 * @start: the starting node
373 * @startindex: the start index
374 * @end: the ending point
375 * @endindex: the ending index
377 * Create a new xmlXPathObjectPtr of type range
379 * Returns the newly created object.
382 xmlXPtrNewRange(xmlNodePtr start
, int startindex
,
383 xmlNodePtr end
, int endindex
) {
384 xmlXPathObjectPtr ret
;
395 ret
= xmlXPtrNewRangeInternal(start
, startindex
, end
, endindex
);
396 xmlXPtrRangeCheckOrder(ret
);
401 * xmlXPtrNewRangePoints:
402 * @start: the starting point
403 * @end: the ending point
405 * Create a new xmlXPathObjectPtr of type range using 2 Points
407 * Returns the newly created object.
410 xmlXPtrNewRangePoints(xmlXPathObjectPtr start
, xmlXPathObjectPtr end
) {
411 xmlXPathObjectPtr ret
;
417 if (start
->type
!= XPATH_POINT
)
419 if (end
->type
!= XPATH_POINT
)
422 ret
= xmlXPtrNewRangeInternal(start
->user
, start
->index
, end
->user
,
424 xmlXPtrRangeCheckOrder(ret
);
429 * xmlXPtrNewRangePointNode:
430 * @start: the starting point
431 * @end: the ending node
433 * Create a new xmlXPathObjectPtr of type range from a point to a node
435 * Returns the newly created object.
438 xmlXPtrNewRangePointNode(xmlXPathObjectPtr start
, xmlNodePtr end
) {
439 xmlXPathObjectPtr ret
;
445 if (start
->type
!= XPATH_POINT
)
448 ret
= xmlXPtrNewRangeInternal(start
->user
, start
->index
, end
, -1);
449 xmlXPtrRangeCheckOrder(ret
);
454 * xmlXPtrNewRangeNodePoint:
455 * @start: the starting node
456 * @end: the ending point
458 * Create a new xmlXPathObjectPtr of type range from a node to a point
460 * Returns the newly created object.
463 xmlXPtrNewRangeNodePoint(xmlNodePtr start
, xmlXPathObjectPtr end
) {
464 xmlXPathObjectPtr ret
;
470 if (end
->type
!= XPATH_POINT
)
473 ret
= xmlXPtrNewRangeInternal(start
, -1, end
->user
, end
->index
);
474 xmlXPtrRangeCheckOrder(ret
);
479 * xmlXPtrNewRangeNodes:
480 * @start: the starting node
481 * @end: the ending node
483 * Create a new xmlXPathObjectPtr of type range using 2 nodes
485 * Returns the newly created object.
488 xmlXPtrNewRangeNodes(xmlNodePtr start
, xmlNodePtr end
) {
489 xmlXPathObjectPtr ret
;
496 ret
= xmlXPtrNewRangeInternal(start
, -1, end
, -1);
497 xmlXPtrRangeCheckOrder(ret
);
502 * xmlXPtrNewCollapsedRange:
503 * @start: the starting and ending node
505 * Create a new xmlXPathObjectPtr of type range using a single nodes
507 * Returns the newly created object.
510 xmlXPtrNewCollapsedRange(xmlNodePtr start
) {
511 xmlXPathObjectPtr ret
;
516 ret
= xmlXPtrNewRangeInternal(start
, -1, NULL
, -1);
521 * xmlXPtrNewRangeNodeObject:
522 * @start: the starting node
523 * @end: the ending object
525 * Create a new xmlXPathObjectPtr of type range from a not to an object
527 * Returns the newly created object.
530 xmlXPtrNewRangeNodeObject(xmlNodePtr start
, xmlXPathObjectPtr end
) {
533 xmlXPathObjectPtr ret
;
542 endIndex
= end
->index
;
545 endNode
= end
->user2
;
546 endIndex
= end
->index2
;
552 if ((end
->nodesetval
== NULL
) || (end
->nodesetval
->nodeNr
<= 0))
554 endNode
= end
->nodesetval
->nodeTab
[end
->nodesetval
->nodeNr
- 1];
562 ret
= xmlXPtrNewRangeInternal(start
, -1, endNode
, endIndex
);
563 xmlXPtrRangeCheckOrder(ret
);
567 #define XML_RANGESET_DEFAULT 10
570 * xmlXPtrLocationSetCreate:
571 * @val: an initial xmlXPathObjectPtr, or NULL
573 * Create a new xmlLocationSetPtr of type double and of value @val
575 * Returns the newly created object.
578 xmlXPtrLocationSetCreate(xmlXPathObjectPtr val
) {
579 xmlLocationSetPtr ret
;
581 ret
= (xmlLocationSetPtr
) xmlMalloc(sizeof(xmlLocationSet
));
583 xmlXPtrErrMemory("allocating locationset");
586 memset(ret
, 0 , (size_t) sizeof(xmlLocationSet
));
588 ret
->locTab
= (xmlXPathObjectPtr
*) xmlMalloc(XML_RANGESET_DEFAULT
*
589 sizeof(xmlXPathObjectPtr
));
590 if (ret
->locTab
== NULL
) {
591 xmlXPtrErrMemory("allocating locationset");
595 memset(ret
->locTab
, 0 ,
596 XML_RANGESET_DEFAULT
* (size_t) sizeof(xmlXPathObjectPtr
));
597 ret
->locMax
= XML_RANGESET_DEFAULT
;
598 ret
->locTab
[ret
->locNr
++] = val
;
604 * xmlXPtrLocationSetAdd:
605 * @cur: the initial range set
606 * @val: a new xmlXPathObjectPtr
608 * add a new xmlXPathObjectPtr to an existing LocationSet
609 * If the location already exist in the set @val is freed.
612 xmlXPtrLocationSetAdd(xmlLocationSetPtr cur
, xmlXPathObjectPtr val
) {
615 if ((cur
== NULL
) || (val
== NULL
)) return;
618 * check against doublons
620 for (i
= 0;i
< cur
->locNr
;i
++) {
621 if (xmlXPtrRangesEqual(cur
->locTab
[i
], val
)) {
622 xmlXPathFreeObject(val
);
628 * grow the locTab if needed
630 if (cur
->locMax
== 0) {
631 cur
->locTab
= (xmlXPathObjectPtr
*) xmlMalloc(XML_RANGESET_DEFAULT
*
632 sizeof(xmlXPathObjectPtr
));
633 if (cur
->locTab
== NULL
) {
634 xmlXPtrErrMemory("adding location to set");
637 memset(cur
->locTab
, 0 ,
638 XML_RANGESET_DEFAULT
* (size_t) sizeof(xmlXPathObjectPtr
));
639 cur
->locMax
= XML_RANGESET_DEFAULT
;
640 } else if (cur
->locNr
== cur
->locMax
) {
641 xmlXPathObjectPtr
*temp
;
644 temp
= (xmlXPathObjectPtr
*) xmlRealloc(cur
->locTab
, cur
->locMax
*
645 sizeof(xmlXPathObjectPtr
));
647 xmlXPtrErrMemory("adding location to set");
652 cur
->locTab
[cur
->locNr
++] = val
;
656 * xmlXPtrLocationSetMerge:
657 * @val1: the first LocationSet
658 * @val2: the second LocationSet
660 * Merges two rangesets, all ranges from @val2 are added to @val1
662 * Returns val1 once extended or NULL in case of error.
665 xmlXPtrLocationSetMerge(xmlLocationSetPtr val1
, xmlLocationSetPtr val2
) {
668 if (val1
== NULL
) return(NULL
);
669 if (val2
== NULL
) return(val1
);
672 * !!!!! this can be optimized a lot, knowing that both
673 * val1 and val2 already have unicity of their values.
676 for (i
= 0;i
< val2
->locNr
;i
++)
677 xmlXPtrLocationSetAdd(val1
, val2
->locTab
[i
]);
683 * xmlXPtrLocationSetDel:
684 * @cur: the initial range set
685 * @val: an xmlXPathObjectPtr
687 * Removes an xmlXPathObjectPtr from an existing LocationSet
690 xmlXPtrLocationSetDel(xmlLocationSetPtr cur
, xmlXPathObjectPtr val
) {
693 if (cur
== NULL
) return;
694 if (val
== NULL
) return;
697 * check against doublons
699 for (i
= 0;i
< cur
->locNr
;i
++)
700 if (cur
->locTab
[i
] == val
) break;
702 if (i
>= cur
->locNr
) {
704 xmlGenericError(xmlGenericErrorContext
,
705 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
710 for (;i
< cur
->locNr
;i
++)
711 cur
->locTab
[i
] = cur
->locTab
[i
+ 1];
712 cur
->locTab
[cur
->locNr
] = NULL
;
716 * xmlXPtrLocationSetRemove:
717 * @cur: the initial range set
718 * @val: the index to remove
720 * Removes an entry from an existing LocationSet list.
723 xmlXPtrLocationSetRemove(xmlLocationSetPtr cur
, int val
) {
724 if (cur
== NULL
) return;
725 if (val
>= cur
->locNr
) return;
727 for (;val
< cur
->locNr
;val
++)
728 cur
->locTab
[val
] = cur
->locTab
[val
+ 1];
729 cur
->locTab
[cur
->locNr
] = NULL
;
733 * xmlXPtrFreeLocationSet:
734 * @obj: the xmlLocationSetPtr to free
736 * Free the LocationSet compound (not the actual ranges !).
739 xmlXPtrFreeLocationSet(xmlLocationSetPtr obj
) {
742 if (obj
== NULL
) return;
743 if (obj
->locTab
!= NULL
) {
744 for (i
= 0;i
< obj
->locNr
; i
++) {
745 xmlXPathFreeObject(obj
->locTab
[i
]);
747 xmlFree(obj
->locTab
);
753 * xmlXPtrNewLocationSetNodes:
754 * @start: the start NodePtr value
755 * @end: the end NodePtr value or NULL
757 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
758 * it with the single range made of the two nodes @start and @end
760 * Returns the newly created object.
763 xmlXPtrNewLocationSetNodes(xmlNodePtr start
, xmlNodePtr end
) {
764 xmlXPathObjectPtr ret
;
766 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
768 xmlXPtrErrMemory("allocating locationset");
771 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
772 ret
->type
= XPATH_LOCATIONSET
;
774 ret
->user
= xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start
));
776 ret
->user
= xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start
,end
));
781 * xmlXPtrNewLocationSetNodeSet:
784 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
785 * it with all the nodes from @set
787 * Returns the newly created object.
790 xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set
) {
791 xmlXPathObjectPtr ret
;
793 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
795 xmlXPtrErrMemory("allocating locationset");
798 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
799 ret
->type
= XPATH_LOCATIONSET
;
802 xmlLocationSetPtr newset
;
804 newset
= xmlXPtrLocationSetCreate(NULL
);
808 for (i
= 0;i
< set
->nodeNr
;i
++)
809 xmlXPtrLocationSetAdd(newset
,
810 xmlXPtrNewCollapsedRange(set
->nodeTab
[i
]));
812 ret
->user
= (void *) newset
;
818 * xmlXPtrWrapLocationSet:
819 * @val: the LocationSet value
821 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
823 * Returns the newly created object.
826 xmlXPtrWrapLocationSet(xmlLocationSetPtr val
) {
827 xmlXPathObjectPtr ret
;
829 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
831 xmlXPtrErrMemory("allocating locationset");
834 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
835 ret
->type
= XPATH_LOCATIONSET
;
836 ret
->user
= (void *) val
;
840 /************************************************************************
844 ************************************************************************/
846 static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt
, xmlChar
*name
);
849 * Macros for accessing the content. Those should be used only by the parser,
852 * Dirty macros, i.e. one need to make assumption on the context to use them
854 * CUR_PTR return the current pointer to the xmlChar to be parsed.
855 * CUR returns the current xmlChar value, i.e. a 8 bit value
856 * in ISO-Latin or UTF-8.
857 * This should be used internally by the parser
858 * only to compare to ASCII values otherwise it would break when
859 * running with UTF-8 encoding.
860 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
861 * to compare on ASCII based substring.
862 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
863 * strings within the parser.
864 * CURRENT Returns the current char value, with the full decoding of
865 * UTF-8 if we are using this mode. It returns an int.
866 * NEXT Skip to the next character, this does the proper decoding
867 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
868 * It returns the pointer to the current xmlChar.
871 #define CUR (*ctxt->cur)
872 #define SKIP(val) ctxt->cur += (val)
873 #define NXT(val) ctxt->cur[(val)]
874 #define CUR_PTR ctxt->cur
876 #define SKIP_BLANKS \
877 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
879 #define CURRENT (*ctxt->cur)
880 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
884 * @ctxt: the XPointer Parser context
885 * @index: the child number
887 * Move the current node of the nodeset on the stack to the
888 * given child if found
891 xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt
, int indx
) {
892 xmlNodePtr cur
= NULL
;
893 xmlXPathObjectPtr obj
;
894 xmlNodeSetPtr oldset
;
896 CHECK_TYPE(XPATH_NODESET
);
897 obj
= valuePop(ctxt
);
898 oldset
= obj
->nodesetval
;
899 if ((indx
<= 0) || (oldset
== NULL
) || (oldset
->nodeNr
!= 1)) {
900 xmlXPathFreeObject(obj
);
901 valuePush(ctxt
, xmlXPathNewNodeSet(NULL
));
904 cur
= xmlXPtrGetNthChild(oldset
->nodeTab
[0], indx
);
906 xmlXPathFreeObject(obj
);
907 valuePush(ctxt
, xmlXPathNewNodeSet(NULL
));
910 oldset
->nodeTab
[0] = cur
;
911 valuePush(ctxt
, obj
);
915 * xmlXPtrEvalXPtrPart:
916 * @ctxt: the XPointer Parser context
917 * @name: the preparsed Scheme for the XPtrPart
919 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
920 * | Scheme '(' SchemeSpecificExpr ')'
922 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
924 * SchemeSpecificExpr ::= StringWithBalancedParens
926 * StringWithBalancedParens ::=
927 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
928 * [VC: Parenthesis escaping]
930 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
932 * VC: Parenthesis escaping:
933 * The end of an XPointer part is signaled by the right parenthesis ")"
934 * character that is balanced with the left parenthesis "(" character
935 * that began the part. Any unbalanced parenthesis character inside the
936 * expression, even within literals, must be escaped with a circumflex (^)
937 * character preceding it. If the expression contains any literal
938 * occurrences of the circumflex, each must be escaped with an additional
939 * circumflex (that is, ^^). If the unescaped parentheses in the expression
940 * are not balanced, a syntax error results.
942 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
943 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
945 * TODO: there is no new scheme registration mechanism
949 xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
950 xmlChar
*buffer
, *cur
;
955 name
= xmlXPathParseName(ctxt
);
957 XP_ERROR(XPATH_EXPR_ERROR
);
961 XP_ERROR(XPATH_EXPR_ERROR
);
966 len
= xmlStrlen(ctxt
->cur
);
968 buffer
= (xmlChar
*) xmlMallocAtomic(len
* sizeof (xmlChar
));
969 if (buffer
== NULL
) {
970 xmlXPtrErrMemory("allocating buffer");
983 } else if (CUR
== '(') {
985 } else if (CUR
== '^') {
986 if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
995 if ((level
!= 0) && (CUR
== 0)) {
998 XP_ERROR(XPTR_SYNTAX_ERROR
);
1001 if (xmlStrEqual(name
, (xmlChar
*) "xpointer")) {
1002 const xmlChar
*left
= CUR_PTR
;
1006 * To evaluate an xpointer scheme element (4.3) we need:
1007 * context initialized to the root
1008 * context position initialized to 1
1009 * context size initialized to 1
1011 ctxt
->context
->node
= (xmlNodePtr
)ctxt
->context
->doc
;
1012 ctxt
->context
->proximityPosition
= 1;
1013 ctxt
->context
->contextSize
= 1;
1014 xmlXPathEvalExpr(ctxt
);
1016 } else if (xmlStrEqual(name
, (xmlChar
*) "element")) {
1017 const xmlChar
*left
= CUR_PTR
;
1021 if (buffer
[0] == '/') {
1023 xmlXPtrEvalChildSeq(ctxt
, NULL
);
1025 name2
= xmlXPathParseName(ctxt
);
1026 if (name2
== NULL
) {
1030 XP_ERROR(XPATH_EXPR_ERROR
);
1032 xmlXPtrEvalChildSeq(ctxt
, name2
);
1035 #ifdef XPTR_XMLNS_SCHEME
1036 } else if (xmlStrEqual(name
, (xmlChar
*) "xmlns")) {
1037 const xmlChar
*left
= CUR_PTR
;
1043 prefix
= xmlXPathParseNCName(ctxt
);
1044 if (prefix
== NULL
) {
1047 XP_ERROR(XPTR_SYNTAX_ERROR
);
1054 XP_ERROR(XPTR_SYNTAX_ERROR
);
1058 /* @@ check escaping in the XPointer WD */
1060 value
= xmlParseURI((const char *)ctxt
->cur
);
1061 if (value
== NULL
) {
1065 XP_ERROR(XPTR_SYNTAX_ERROR
);
1067 URI
= xmlSaveUri(value
);
1073 XP_ERROR(XPATH_MEMORY_ERROR
);
1076 xmlXPathRegisterNs(ctxt
->context
, prefix
, URI
);
1080 #endif /* XPTR_XMLNS_SCHEME */
1082 xmlXPtrErr(ctxt
, XML_XPTR_UNKNOWN_SCHEME
,
1083 "unsupported scheme '%s'\n", name
);
1090 * xmlXPtrEvalFullXPtr:
1091 * @ctxt: the XPointer Parser context
1092 * @name: the preparsed Scheme for the first XPtrPart
1094 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1096 * As the specs says:
1098 * When multiple XPtrParts are provided, they must be evaluated in
1099 * left-to-right order. If evaluation of one part fails, the nexti
1100 * is evaluated. The following conditions cause XPointer part failure:
1102 * - An unknown scheme
1103 * - A scheme that does not locate any sub-resource present in the resource
1104 * - A scheme that is not applicable to the media type of the resource
1106 * The XPointer application must consume a failed XPointer part and
1107 * attempt to evaluate the next one, if any. The result of the first
1108 * XPointer part whose evaluation succeeds is taken to be the fragment
1109 * located by the XPointer as a whole. If all the parts fail, the result
1110 * for the XPointer as a whole is a sub-resource error.
1113 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1114 * expressions or other schemes.
1117 xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
1119 name
= xmlXPathParseName(ctxt
);
1121 XP_ERROR(XPATH_EXPR_ERROR
);
1122 while (name
!= NULL
) {
1123 ctxt
->error
= XPATH_EXPRESSION_OK
;
1124 xmlXPtrEvalXPtrPart(ctxt
, name
);
1126 /* in case of syntax error, break here */
1127 if ((ctxt
->error
!= XPATH_EXPRESSION_OK
) &&
1128 (ctxt
->error
!= XML_XPTR_UNKNOWN_SCHEME
))
1132 * If the returned value is a non-empty nodeset
1133 * or location set, return here.
1135 if (ctxt
->value
!= NULL
) {
1136 xmlXPathObjectPtr obj
= ctxt
->value
;
1138 switch (obj
->type
) {
1139 case XPATH_LOCATIONSET
: {
1140 xmlLocationSetPtr loc
= ctxt
->value
->user
;
1141 if ((loc
!= NULL
) && (loc
->locNr
> 0))
1145 case XPATH_NODESET
: {
1146 xmlNodeSetPtr loc
= ctxt
->value
->nodesetval
;
1147 if ((loc
!= NULL
) && (loc
->nodeNr
> 0))
1156 * Evaluating to improper values is equivalent to
1157 * a sub-resource error, clean-up the stack
1160 obj
= valuePop(ctxt
);
1162 xmlXPathFreeObject(obj
);
1164 } while (obj
!= NULL
);
1168 * Is there another XPointer part.
1171 name
= xmlXPathParseName(ctxt
);
1176 * xmlXPtrEvalChildSeq:
1177 * @ctxt: the XPointer Parser context
1178 * @name: a possible ID name of the child sequence
1180 * ChildSeq ::= '/1' ('/' [0-9]*)*
1181 * | Name ('/' [0-9]*)+
1183 * Parse and evaluate a Child Sequence. This routine also handle the
1184 * case of a Bare Name used to get a document ID.
1187 xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
1189 * XPointer don't allow by syntax to address in multirooted trees
1190 * this might prove useful in some cases, warn about it.
1192 if ((name
== NULL
) && (CUR
== '/') && (NXT(1) != '1')) {
1193 xmlXPtrErr(ctxt
, XML_XPTR_CHILDSEQ_START
,
1194 "warning: ChildSeq not starting by /1\n", NULL
);
1198 valuePush(ctxt
, xmlXPathNewString(name
));
1200 xmlXPathIdFunction(ctxt
, 1);
1204 while (CUR
== '/') {
1205 int child
= 0, overflow
= 0;
1208 while ((CUR
>= '0') && (CUR
<= '9')) {
1210 if (child
> INT_MAX
/ 10)
1214 if (child
> INT_MAX
- d
)
1222 xmlXPtrGetChildNo(ctxt
, child
);
1228 * xmlXPtrEvalXPointer:
1229 * @ctxt: the XPointer Parser context
1235 * Parse and evaluate an XPointer
1238 xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt
) {
1239 if (ctxt
->valueTab
== NULL
) {
1240 /* Allocate the value stack */
1241 ctxt
->valueTab
= (xmlXPathObjectPtr
*)
1242 xmlMalloc(10 * sizeof(xmlXPathObjectPtr
));
1243 if (ctxt
->valueTab
== NULL
) {
1244 xmlXPtrErrMemory("allocating evaluation context");
1248 ctxt
->valueMax
= 10;
1250 ctxt
->valueFrame
= 0;
1255 xmlXPtrEvalChildSeq(ctxt
, NULL
);
1259 name
= xmlXPathParseName(ctxt
);
1261 XP_ERROR(XPATH_EXPR_ERROR
);
1263 xmlXPtrEvalFullXPtr(ctxt
, name
);
1264 /* Short evaluation */
1267 /* this handle both Bare Names and Child Sequences */
1268 xmlXPtrEvalChildSeq(ctxt
, name
);
1273 XP_ERROR(XPATH_EXPR_ERROR
);
1277 /************************************************************************
1279 * General routines *
1281 ************************************************************************/
1284 void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1286 void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1288 void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1290 void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1292 void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1294 void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1296 void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1299 * xmlXPtrNewContext:
1300 * @doc: the XML document
1301 * @here: the node that directly contains the XPointer being evaluated or NULL
1302 * @origin: the element from which a user or program initiated traversal of
1303 * the link, or NULL.
1305 * Create a new XPointer context
1307 * Returns the xmlXPathContext just allocated.
1310 xmlXPtrNewContext(xmlDocPtr doc
, xmlNodePtr here
, xmlNodePtr origin
) {
1311 xmlXPathContextPtr ret
;
1313 ret
= xmlXPathNewContext(doc
);
1318 ret
->origin
= origin
;
1320 xmlXPathRegisterFunc(ret
, (xmlChar
*)"range",
1321 xmlXPtrRangeFunction
);
1322 xmlXPathRegisterFunc(ret
, (xmlChar
*)"range-inside",
1323 xmlXPtrRangeInsideFunction
);
1324 xmlXPathRegisterFunc(ret
, (xmlChar
*)"string-range",
1325 xmlXPtrStringRangeFunction
);
1326 xmlXPathRegisterFunc(ret
, (xmlChar
*)"start-point",
1327 xmlXPtrStartPointFunction
);
1328 xmlXPathRegisterFunc(ret
, (xmlChar
*)"end-point",
1329 xmlXPtrEndPointFunction
);
1330 xmlXPathRegisterFunc(ret
, (xmlChar
*)"here",
1331 xmlXPtrHereFunction
);
1332 xmlXPathRegisterFunc(ret
, (xmlChar
*)" origin",
1333 xmlXPtrOriginFunction
);
1340 * @str: the XPointer expression
1341 * @ctx: the XPointer context
1343 * Evaluate the XPath Location Path in the given context.
1345 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
1346 * the caller has to free the object.
1349 xmlXPtrEval(const xmlChar
*str
, xmlXPathContextPtr ctx
) {
1350 xmlXPathParserContextPtr ctxt
;
1351 xmlXPathObjectPtr res
= NULL
, tmp
;
1352 xmlXPathObjectPtr init
= NULL
;
1357 if ((ctx
== NULL
) || (str
== NULL
))
1360 ctxt
= xmlXPathNewParserContext(str
, ctx
);
1364 xmlXPtrEvalXPointer(ctxt
);
1366 if ((ctxt
->value
!= NULL
) &&
1367 (ctxt
->value
->type
!= XPATH_NODESET
) &&
1368 (ctxt
->value
->type
!= XPATH_LOCATIONSET
)) {
1369 xmlXPtrErr(ctxt
, XML_XPTR_EVAL_FAILED
,
1370 "xmlXPtrEval: evaluation failed to return a node set\n",
1373 res
= valuePop(ctxt
);
1377 tmp
= valuePop(ctxt
);
1380 if (tmp
->type
== XPATH_NODESET
) {
1382 * Evaluation may push a root nodeset which is unused
1385 set
= tmp
->nodesetval
;
1386 if ((set
== NULL
) || (set
->nodeNr
!= 1) ||
1387 (set
->nodeTab
[0] != (xmlNodePtr
) ctx
->doc
))
1392 xmlXPathFreeObject(tmp
);
1394 } while (tmp
!= NULL
);
1396 xmlXPtrErr(ctxt
, XML_XPTR_EXTRA_OBJECTS
,
1397 "xmlXPtrEval: object(s) left on the eval stack\n",
1400 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
1401 xmlXPathFreeObject(res
);
1405 xmlXPathFreeParserContext(ctxt
);
1410 * xmlXPtrBuildRangeNodeList:
1411 * @range: a range object
1413 * Build a node list tree copy of the range
1415 * Returns an xmlNodePtr list or NULL.
1416 * the caller has to free the node tree.
1419 xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range
) {
1420 /* pointers to generated nodes */
1421 xmlNodePtr list
= NULL
, last
= NULL
, parent
= NULL
, tmp
;
1422 /* pointers to traversal nodes */
1423 xmlNodePtr start
, cur
, end
;
1428 if (range
->type
!= XPATH_RANGE
)
1430 start
= (xmlNodePtr
) range
->user
;
1432 if ((start
== NULL
) || (start
->type
== XML_NAMESPACE_DECL
))
1436 return(xmlCopyNode(start
, 1));
1437 if (end
->type
== XML_NAMESPACE_DECL
)
1441 index1
= range
->index
;
1442 index2
= range
->index2
;
1443 while (cur
!= NULL
) {
1445 if (cur
->type
== XML_TEXT_NODE
) {
1446 const xmlChar
*content
= cur
->content
;
1449 if (content
== NULL
) {
1450 tmp
= xmlNewTextLen(NULL
, 0);
1453 if ((cur
== start
) && (index1
> 1)) {
1454 content
+= (index1
- 1);
1455 len
-= (index1
- 1);
1460 tmp
= xmlNewTextLen(content
, len
);
1462 /* single sub text node selection */
1465 /* prune and return full set */
1467 xmlAddNextSibling(last
, tmp
);
1469 xmlAddChild(parent
, tmp
);
1472 tmp
= xmlCopyNode(cur
, 0);
1477 xmlAddNextSibling(last
, tmp
);
1479 xmlAddChild(parent
, tmp
);
1485 end
= xmlXPtrGetNthChild(cur
, index2
- 1);
1488 if ((cur
== start
) && (index1
> 1)) {
1489 cur
= xmlXPtrGetNthChild(cur
, index1
- 1);
1492 cur
= cur
->children
;
1495 * Now gather the remaining nodes from cur to end
1497 continue; /* while */
1499 } else if ((cur
== start
) &&
1500 (list
== NULL
) /* looks superfluous but ... */ ) {
1501 if ((cur
->type
== XML_TEXT_NODE
) ||
1502 (cur
->type
== XML_CDATA_SECTION_NODE
)) {
1503 const xmlChar
*content
= cur
->content
;
1505 if (content
== NULL
) {
1506 tmp
= xmlNewTextLen(NULL
, 0);
1509 content
+= (index1
- 1);
1511 tmp
= xmlNewText(content
);
1515 if ((cur
== start
) && (index1
> 1)) {
1516 tmp
= xmlCopyNode(cur
, 0);
1520 cur
= xmlXPtrGetNthChild(cur
, index1
- 1);
1523 * Now gather the remaining nodes from cur to end
1525 continue; /* while */
1527 tmp
= xmlCopyNode(cur
, 1);
1534 switch (cur
->type
) {
1536 case XML_ELEMENT_DECL
:
1537 case XML_ATTRIBUTE_DECL
:
1538 case XML_ENTITY_NODE
:
1539 /* Do not copy DTD information */
1541 case XML_ENTITY_DECL
:
1542 TODO
/* handle crossing entities -> stack needed */
1544 case XML_XINCLUDE_START
:
1545 case XML_XINCLUDE_END
:
1546 /* don't consider it part of the tree content */
1548 case XML_ATTRIBUTE_NODE
:
1549 /* Humm, should not happen ! */
1553 tmp
= xmlCopyNode(cur
, 1);
1557 if ((list
== NULL
) || ((last
== NULL
) && (parent
== NULL
))) {
1562 xmlAddNextSibling(last
, tmp
);
1564 xmlAddChild(parent
, tmp
);
1570 * Skip to next node in document order
1572 if ((list
== NULL
) || ((last
== NULL
) && (parent
== NULL
))) {
1576 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
1582 * xmlXPtrBuildNodeList:
1583 * @obj: the XPointer result from the evaluation.
1585 * Build a node list tree copy of the XPointer result.
1586 * This will drop Attributes and Namespace declarations.
1588 * Returns an xmlNodePtr list or NULL.
1589 * the caller has to free the node tree.
1592 xmlXPtrBuildNodeList(xmlXPathObjectPtr obj
) {
1593 xmlNodePtr list
= NULL
, last
= NULL
;
1598 switch (obj
->type
) {
1599 case XPATH_NODESET
: {
1600 xmlNodeSetPtr set
= obj
->nodesetval
;
1603 for (i
= 0;i
< set
->nodeNr
;i
++) {
1604 if (set
->nodeTab
[i
] == NULL
)
1606 switch (set
->nodeTab
[i
]->type
) {
1608 case XML_CDATA_SECTION_NODE
:
1609 case XML_ELEMENT_NODE
:
1610 case XML_ENTITY_REF_NODE
:
1611 case XML_ENTITY_NODE
:
1613 case XML_COMMENT_NODE
:
1614 case XML_DOCUMENT_NODE
:
1615 case XML_HTML_DOCUMENT_NODE
:
1616 #ifdef LIBXML_DOCB_ENABLED
1617 case XML_DOCB_DOCUMENT_NODE
:
1619 case XML_XINCLUDE_START
:
1620 case XML_XINCLUDE_END
:
1622 case XML_ATTRIBUTE_NODE
:
1623 case XML_NAMESPACE_DECL
:
1624 case XML_DOCUMENT_TYPE_NODE
:
1625 case XML_DOCUMENT_FRAG_NODE
:
1626 case XML_NOTATION_NODE
:
1628 case XML_ELEMENT_DECL
:
1629 case XML_ATTRIBUTE_DECL
:
1630 case XML_ENTITY_DECL
:
1634 list
= last
= xmlCopyNode(set
->nodeTab
[i
], 1);
1636 xmlAddNextSibling(last
, xmlCopyNode(set
->nodeTab
[i
], 1));
1637 if (last
->next
!= NULL
)
1643 case XPATH_LOCATIONSET
: {
1644 xmlLocationSetPtr set
= (xmlLocationSetPtr
) obj
->user
;
1647 for (i
= 0;i
< set
->locNr
;i
++) {
1649 list
= last
= xmlXPtrBuildNodeList(set
->locTab
[i
]);
1651 xmlAddNextSibling(last
,
1652 xmlXPtrBuildNodeList(set
->locTab
[i
]));
1654 while (last
->next
!= NULL
)
1661 return(xmlXPtrBuildRangeNodeList(obj
));
1663 return(xmlCopyNode(obj
->user
, 0));
1670 /************************************************************************
1672 * XPointer functions *
1674 ************************************************************************/
1677 * xmlXPtrNbLocChildren:
1678 * @node: an xmlNodePtr
1680 * Count the number of location children of @node or the length of the
1681 * string value in case of text/PI/Comments nodes
1683 * Returns the number of location children
1686 xmlXPtrNbLocChildren(xmlNodePtr node
) {
1690 switch (node
->type
) {
1691 case XML_HTML_DOCUMENT_NODE
:
1692 case XML_DOCUMENT_NODE
:
1693 case XML_ELEMENT_NODE
:
1694 node
= node
->children
;
1695 while (node
!= NULL
) {
1696 if (node
->type
== XML_ELEMENT_NODE
)
1701 case XML_ATTRIBUTE_NODE
:
1705 case XML_COMMENT_NODE
:
1707 case XML_CDATA_SECTION_NODE
:
1708 case XML_ENTITY_REF_NODE
:
1709 ret
= xmlStrlen(node
->content
);
1718 * xmlXPtrHereFunction:
1719 * @ctxt: the XPointer Parser context
1720 * @nargs: the number of args
1722 * Function implementing here() operation
1723 * as described in 5.4.3
1726 xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1729 if (ctxt
->context
->here
== NULL
)
1730 XP_ERROR(XPTR_SYNTAX_ERROR
);
1732 valuePush(ctxt
, xmlXPtrNewLocationSetNodes(ctxt
->context
->here
, NULL
));
1736 * xmlXPtrOriginFunction:
1737 * @ctxt: the XPointer Parser context
1738 * @nargs: the number of args
1740 * Function implementing origin() operation
1741 * as described in 5.4.3
1744 xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1747 if (ctxt
->context
->origin
== NULL
)
1748 XP_ERROR(XPTR_SYNTAX_ERROR
);
1750 valuePush(ctxt
, xmlXPtrNewLocationSetNodes(ctxt
->context
->origin
, NULL
));
1754 * xmlXPtrStartPointFunction:
1755 * @ctxt: the XPointer Parser context
1756 * @nargs: the number of args
1758 * Function implementing start-point() operation
1759 * as described in 5.4.3
1761 * location-set start-point(location-set)
1763 * For each location x in the argument location-set, start-point adds a
1764 * location of type point to the result location-set. That point represents
1765 * the start point of location x and is determined by the following rules:
1767 * - If x is of type point, the start point is x.
1768 * - If x is of type range, the start point is the start point of x.
1769 * - If x is of type root, element, text, comment, or processing instruction,
1770 * - the container node of the start point is x and the index is 0.
1771 * - If x is of type attribute or namespace, the function must signal a
1777 xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1778 xmlXPathObjectPtr tmp
, obj
, point
;
1779 xmlLocationSetPtr newset
= NULL
;
1780 xmlLocationSetPtr oldset
= NULL
;
1783 if ((ctxt
->value
== NULL
) ||
1784 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
1785 (ctxt
->value
->type
!= XPATH_NODESET
)))
1786 XP_ERROR(XPATH_INVALID_TYPE
)
1788 obj
= valuePop(ctxt
);
1789 if (obj
->type
== XPATH_NODESET
) {
1791 * First convert to a location set
1793 tmp
= xmlXPtrNewLocationSetNodeSet(obj
->nodesetval
);
1794 xmlXPathFreeObject(obj
);
1796 XP_ERROR(XPATH_MEMORY_ERROR
)
1800 newset
= xmlXPtrLocationSetCreate(NULL
);
1801 if (newset
== NULL
) {
1802 xmlXPathFreeObject(obj
);
1803 XP_ERROR(XPATH_MEMORY_ERROR
);
1805 oldset
= (xmlLocationSetPtr
) obj
->user
;
1806 if (oldset
!= NULL
) {
1809 for (i
= 0; i
< oldset
->locNr
; i
++) {
1810 tmp
= oldset
->locTab
[i
];
1814 switch (tmp
->type
) {
1816 point
= xmlXPtrNewPoint(tmp
->user
, tmp
->index
);
1819 xmlNodePtr node
= tmp
->user
;
1821 if ((node
->type
== XML_ATTRIBUTE_NODE
) ||
1822 (node
->type
== XML_NAMESPACE_DECL
)) {
1823 xmlXPathFreeObject(obj
);
1824 xmlXPtrFreeLocationSet(newset
);
1825 XP_ERROR(XPTR_SYNTAX_ERROR
);
1827 point
= xmlXPtrNewPoint(node
, tmp
->index
);
1832 /*** Should we raise an error ?
1833 xmlXPathFreeObject(obj);
1834 xmlXPathFreeObject(newset);
1835 XP_ERROR(XPATH_INVALID_TYPE)
1840 xmlXPtrLocationSetAdd(newset
, point
);
1843 xmlXPathFreeObject(obj
);
1844 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
1848 * xmlXPtrEndPointFunction:
1849 * @ctxt: the XPointer Parser context
1850 * @nargs: the number of args
1852 * Function implementing end-point() operation
1853 * as described in 5.4.3
1854 * ----------------------------
1855 * location-set end-point(location-set)
1857 * For each location x in the argument location-set, end-point adds a
1858 * location of type point to the result location-set. That point represents
1859 * the end point of location x and is determined by the following rules:
1861 * - If x is of type point, the resulting point is x.
1862 * - If x is of type range, the resulting point is the end point of x.
1863 * - If x is of type root or element, the container node of the resulting
1864 * point is x and the index is the number of location children of x.
1865 * - If x is of type text, comment, or processing instruction, the container
1866 * node of the resulting point is x and the index is the length of the
1867 * string-value of x.
1868 * - If x is of type attribute or namespace, the function must signal a
1870 * ----------------------------
1873 xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1874 xmlXPathObjectPtr tmp
, obj
, point
;
1875 xmlLocationSetPtr newset
= NULL
;
1876 xmlLocationSetPtr oldset
= NULL
;
1879 if ((ctxt
->value
== NULL
) ||
1880 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
1881 (ctxt
->value
->type
!= XPATH_NODESET
)))
1882 XP_ERROR(XPATH_INVALID_TYPE
)
1884 obj
= valuePop(ctxt
);
1885 if (obj
->type
== XPATH_NODESET
) {
1887 * First convert to a location set
1889 tmp
= xmlXPtrNewLocationSetNodeSet(obj
->nodesetval
);
1890 xmlXPathFreeObject(obj
);
1892 XP_ERROR(XPATH_MEMORY_ERROR
)
1896 newset
= xmlXPtrLocationSetCreate(NULL
);
1897 if (newset
== NULL
) {
1898 xmlXPathFreeObject(obj
);
1899 XP_ERROR(XPATH_MEMORY_ERROR
);
1901 oldset
= (xmlLocationSetPtr
) obj
->user
;
1902 if (oldset
!= NULL
) {
1905 for (i
= 0; i
< oldset
->locNr
; i
++) {
1906 tmp
= oldset
->locTab
[i
];
1910 switch (tmp
->type
) {
1912 point
= xmlXPtrNewPoint(tmp
->user
, tmp
->index
);
1915 xmlNodePtr node
= tmp
->user2
;
1917 if ((node
->type
== XML_ATTRIBUTE_NODE
) ||
1918 (node
->type
== XML_NAMESPACE_DECL
)) {
1919 xmlXPathFreeObject(obj
);
1920 xmlXPtrFreeLocationSet(newset
);
1921 XP_ERROR(XPTR_SYNTAX_ERROR
);
1923 point
= xmlXPtrNewPoint(node
, tmp
->index2
);
1924 } else if (tmp
->user
== NULL
) {
1925 point
= xmlXPtrNewPoint(node
,
1926 xmlXPtrNbLocChildren(node
));
1931 /*** Should we raise an error ?
1932 xmlXPathFreeObject(obj);
1933 xmlXPathFreeObject(newset);
1934 XP_ERROR(XPATH_INVALID_TYPE)
1939 xmlXPtrLocationSetAdd(newset
, point
);
1942 xmlXPathFreeObject(obj
);
1943 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
1948 * xmlXPtrCoveringRange:
1949 * @ctxt: the XPointer Parser context
1950 * @loc: the location for which the covering range must be computed
1952 * A covering range is a range that wholly encompasses a location
1953 * Section 5.3.3. Covering Ranges for All Location Types
1954 * http://www.w3.org/TR/xptr#N2267
1956 * Returns a new location or NULL in case of error
1958 static xmlXPathObjectPtr
1959 xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt
, xmlXPathObjectPtr loc
) {
1962 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
) ||
1963 (ctxt
->context
->doc
== NULL
))
1965 switch (loc
->type
) {
1967 return(xmlXPtrNewRange(loc
->user
, loc
->index
,
1968 loc
->user
, loc
->index
));
1970 if (loc
->user2
!= NULL
) {
1971 return(xmlXPtrNewRange(loc
->user
, loc
->index
,
1972 loc
->user2
, loc
->index2
));
1974 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
1975 if (node
== (xmlNodePtr
) ctxt
->context
->doc
) {
1976 return(xmlXPtrNewRange(node
, 0, node
,
1977 xmlXPtrGetArity(node
)));
1979 switch (node
->type
) {
1980 case XML_ATTRIBUTE_NODE
:
1981 /* !!! our model is slightly different than XPath */
1982 return(xmlXPtrNewRange(node
, 0, node
,
1983 xmlXPtrGetArity(node
)));
1984 case XML_ELEMENT_NODE
:
1986 case XML_CDATA_SECTION_NODE
:
1987 case XML_ENTITY_REF_NODE
:
1989 case XML_COMMENT_NODE
:
1990 case XML_DOCUMENT_NODE
:
1991 case XML_NOTATION_NODE
:
1992 case XML_HTML_DOCUMENT_NODE
: {
1993 int indx
= xmlXPtrGetIndex(node
);
1995 node
= node
->parent
;
1996 return(xmlXPtrNewRange(node
, indx
- 1,
2005 TODO
/* missed one case ??? */
2011 * xmlXPtrRangeFunction:
2012 * @ctxt: the XPointer Parser context
2013 * @nargs: the number of args
2015 * Function implementing the range() function 5.4.3
2016 * location-set range(location-set )
2018 * The range function returns ranges covering the locations in
2019 * the argument location-set. For each location x in the argument
2020 * location-set, a range location representing the covering range of
2021 * x is added to the result location-set.
2024 xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2026 xmlXPathObjectPtr set
;
2027 xmlLocationSetPtr oldset
;
2028 xmlLocationSetPtr newset
;
2031 if ((ctxt
->value
== NULL
) ||
2032 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
2033 (ctxt
->value
->type
!= XPATH_NODESET
)))
2034 XP_ERROR(XPATH_INVALID_TYPE
)
2036 set
= valuePop(ctxt
);
2037 if (set
->type
== XPATH_NODESET
) {
2038 xmlXPathObjectPtr tmp
;
2041 * First convert to a location set
2043 tmp
= xmlXPtrNewLocationSetNodeSet(set
->nodesetval
);
2044 xmlXPathFreeObject(set
);
2046 XP_ERROR(XPATH_MEMORY_ERROR
)
2049 oldset
= (xmlLocationSetPtr
) set
->user
;
2052 * The loop is to compute the covering range for each item and add it
2054 newset
= xmlXPtrLocationSetCreate(NULL
);
2055 if (newset
== NULL
) {
2056 xmlXPathFreeObject(set
);
2057 XP_ERROR(XPATH_MEMORY_ERROR
);
2059 if (oldset
!= NULL
) {
2060 for (i
= 0;i
< oldset
->locNr
;i
++) {
2061 xmlXPtrLocationSetAdd(newset
,
2062 xmlXPtrCoveringRange(ctxt
, oldset
->locTab
[i
]));
2067 * Save the new value and cleanup
2069 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2070 xmlXPathFreeObject(set
);
2074 * xmlXPtrInsideRange:
2075 * @ctxt: the XPointer Parser context
2076 * @loc: the location for which the inside range must be computed
2078 * A inside range is a range described in the range-inside() description
2080 * Returns a new location or NULL in case of error
2082 static xmlXPathObjectPtr
2083 xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt
, xmlXPathObjectPtr loc
) {
2086 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
) ||
2087 (ctxt
->context
->doc
== NULL
))
2089 switch (loc
->type
) {
2091 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
2092 switch (node
->type
) {
2094 case XML_COMMENT_NODE
:
2096 case XML_CDATA_SECTION_NODE
: {
2097 if (node
->content
== NULL
) {
2098 return(xmlXPtrNewRange(node
, 0, node
, 0));
2100 return(xmlXPtrNewRange(node
, 0, node
,
2101 xmlStrlen(node
->content
)));
2104 case XML_ATTRIBUTE_NODE
:
2105 case XML_ELEMENT_NODE
:
2106 case XML_ENTITY_REF_NODE
:
2107 case XML_DOCUMENT_NODE
:
2108 case XML_NOTATION_NODE
:
2109 case XML_HTML_DOCUMENT_NODE
: {
2110 return(xmlXPtrNewRange(node
, 0, node
,
2111 xmlXPtrGetArity(node
)));
2119 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
2120 if (loc
->user2
!= NULL
) {
2121 return(xmlXPtrNewRange(node
, loc
->index
,
2122 loc
->user2
, loc
->index2
));
2124 switch (node
->type
) {
2126 case XML_COMMENT_NODE
:
2128 case XML_CDATA_SECTION_NODE
: {
2129 if (node
->content
== NULL
) {
2130 return(xmlXPtrNewRange(node
, 0, node
, 0));
2132 return(xmlXPtrNewRange(node
, 0, node
,
2133 xmlStrlen(node
->content
)));
2136 case XML_ATTRIBUTE_NODE
:
2137 case XML_ELEMENT_NODE
:
2138 case XML_ENTITY_REF_NODE
:
2139 case XML_DOCUMENT_NODE
:
2140 case XML_NOTATION_NODE
:
2141 case XML_HTML_DOCUMENT_NODE
: {
2142 return(xmlXPtrNewRange(node
, 0, node
,
2143 xmlXPtrGetArity(node
)));
2152 TODO
/* missed one case ??? */
2158 * xmlXPtrRangeInsideFunction:
2159 * @ctxt: the XPointer Parser context
2160 * @nargs: the number of args
2162 * Function implementing the range-inside() function 5.4.3
2163 * location-set range-inside(location-set )
2165 * The range-inside function returns ranges covering the contents of
2166 * the locations in the argument location-set. For each location x in
2167 * the argument location-set, a range location is added to the result
2168 * location-set. If x is a range location, then x is added to the
2169 * result location-set. If x is not a range location, then x is used
2170 * as the container location of the start and end points of the range
2171 * location to be added; the index of the start point of the range is
2172 * zero; if the end point is a character point then its index is the
2173 * length of the string-value of x, and otherwise is the number of
2174 * location children of x.
2178 xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2180 xmlXPathObjectPtr set
;
2181 xmlLocationSetPtr oldset
;
2182 xmlLocationSetPtr newset
;
2185 if ((ctxt
->value
== NULL
) ||
2186 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
2187 (ctxt
->value
->type
!= XPATH_NODESET
)))
2188 XP_ERROR(XPATH_INVALID_TYPE
)
2190 set
= valuePop(ctxt
);
2191 if (set
->type
== XPATH_NODESET
) {
2192 xmlXPathObjectPtr tmp
;
2195 * First convert to a location set
2197 tmp
= xmlXPtrNewLocationSetNodeSet(set
->nodesetval
);
2198 xmlXPathFreeObject(set
);
2200 XP_ERROR(XPATH_MEMORY_ERROR
)
2205 * The loop is to compute the covering range for each item and add it
2207 newset
= xmlXPtrLocationSetCreate(NULL
);
2208 if (newset
== NULL
) {
2209 xmlXPathFreeObject(set
);
2210 XP_ERROR(XPATH_MEMORY_ERROR
);
2212 oldset
= (xmlLocationSetPtr
) set
->user
;
2213 if (oldset
!= NULL
) {
2214 for (i
= 0;i
< oldset
->locNr
;i
++) {
2215 xmlXPtrLocationSetAdd(newset
,
2216 xmlXPtrInsideRange(ctxt
, oldset
->locTab
[i
]));
2221 * Save the new value and cleanup
2223 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2224 xmlXPathFreeObject(set
);
2228 * xmlXPtrRangeToFunction:
2229 * @ctxt: the XPointer Parser context
2230 * @nargs: the number of args
2232 * Implement the range-to() XPointer function
2234 * Obsolete. range-to is not a real function but a special type of location
2235 * step which is handled in xpath.c.
2238 xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt
,
2239 int nargs ATTRIBUTE_UNUSED
) {
2240 XP_ERROR(XPATH_EXPR_ERROR
);
2244 * xmlXPtrAdvanceNode:
2246 * @level: incremented/decremented to show level in tree
2248 * Advance to the next element or text node in document order
2249 * TODO: add a stack for entering/exiting entities
2251 * Returns -1 in case of failure, 0 otherwise
2254 xmlXPtrAdvanceNode(xmlNodePtr cur
, int *level
) {
2256 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
2258 if (cur
->children
!= NULL
) {
2259 cur
= cur
->children
;
2264 skip
: /* This label should only be needed if something is wrong! */
2265 if (cur
->next
!= NULL
) {
2273 if (cur
== NULL
) return(NULL
);
2274 if (cur
->next
!= NULL
) {
2278 } while (cur
!= NULL
);
2281 if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2282 (cur
->type
!= XML_TEXT_NODE
) &&
2283 (cur
->type
!= XML_DOCUMENT_NODE
) &&
2284 (cur
->type
!= XML_HTML_DOCUMENT_NODE
) &&
2285 (cur
->type
!= XML_CDATA_SECTION_NODE
)) {
2286 if (cur
->type
== XML_ENTITY_REF_NODE
) { /* Shouldn't happen */
2296 * xmlXPtrAdvanceChar:
2299 * @bytes: the number of bytes
2301 * Advance a point of the associated number of bytes (not UTF8 chars)
2303 * Returns -1 in case of failure, 0 otherwise
2306 xmlXPtrAdvanceChar(xmlNodePtr
*node
, int *indx
, int bytes
) {
2311 if ((node
== NULL
) || (indx
== NULL
))
2314 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
2318 while (bytes
>= 0) {
2320 * First position to the beginning of the first text node
2321 * corresponding to this point
2323 while ((cur
!= NULL
) &&
2324 ((cur
->type
== XML_ELEMENT_NODE
) ||
2325 (cur
->type
== XML_DOCUMENT_NODE
) ||
2326 (cur
->type
== XML_HTML_DOCUMENT_NODE
))) {
2328 cur
= xmlXPtrGetNthChild(cur
, pos
);
2331 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2343 * if there is no move needed return the current value.
2345 if (pos
== 0) pos
= 1;
2352 * We should have a text (or cdata) node ...
2355 if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2356 (cur
->content
!= NULL
)) {
2357 len
= xmlStrlen(cur
->content
);
2360 /* Strange, the indx in the text node is greater than it's len */
2364 if (pos
+ bytes
>= len
) {
2365 bytes
-= (len
- pos
);
2366 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2368 } else if (pos
+ bytes
< len
) {
2379 * xmlXPtrMatchString:
2380 * @string: the string to search
2381 * @start: the start textnode
2382 * @startindex: the start index
2383 * @end: the end textnode IN/OUT
2384 * @endindex: the end index IN/OUT
2386 * Check whether the document contains @string at the position
2387 * (@start, @startindex) and limited by the (@end, @endindex) point
2389 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2390 * (@start, @startindex) will indicate the position of the beginning
2391 * of the range and (@end, @endindex) will indicate the end
2395 xmlXPtrMatchString(const xmlChar
*string
, xmlNodePtr start
, int startindex
,
2396 xmlNodePtr
*end
, int *endindex
) {
2398 int pos
; /* 0 based */
2399 int len
; /* in bytes */
2400 int stringlen
; /* in bytes */
2405 if ((start
== NULL
) || (start
->type
== XML_NAMESPACE_DECL
))
2407 if ((end
== NULL
) || (*end
== NULL
) ||
2408 ((*end
)->type
== XML_NAMESPACE_DECL
) || (endindex
== NULL
))
2411 pos
= startindex
- 1;
2412 stringlen
= xmlStrlen(string
);
2414 while (stringlen
> 0) {
2415 if ((cur
== *end
) && (pos
+ stringlen
> *endindex
))
2418 if ((cur
->type
!= XML_ELEMENT_NODE
) && (cur
->content
!= NULL
)) {
2419 len
= xmlStrlen(cur
->content
);
2420 if (len
>= pos
+ stringlen
) {
2421 match
= (!xmlStrncmp(&cur
->content
[pos
], string
, stringlen
));
2424 xmlGenericError(xmlGenericErrorContext
,
2425 "found range %d bytes at index %d of ->",
2426 stringlen
, pos
+ 1);
2427 xmlDebugDumpString(stdout
, cur
->content
);
2428 xmlGenericError(xmlGenericErrorContext
, "\n");
2431 *endindex
= pos
+ stringlen
;
2437 int sub
= len
- pos
;
2438 match
= (!xmlStrncmp(&cur
->content
[pos
], string
, sub
));
2441 xmlGenericError(xmlGenericErrorContext
,
2442 "found subrange %d bytes at index %d of ->",
2444 xmlDebugDumpString(stdout
, cur
->content
);
2445 xmlGenericError(xmlGenericErrorContext
, "\n");
2447 string
= &string
[sub
];
2454 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2463 * xmlXPtrSearchString:
2464 * @string: the string to search
2465 * @start: the start textnode IN/OUT
2466 * @startindex: the start index IN/OUT
2467 * @end: the end textnode
2468 * @endindex: the end index
2470 * Search the next occurrence of @string within the document content
2471 * until the (@end, @endindex) point is reached
2473 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2474 * (@start, @startindex) will indicate the position of the beginning
2475 * of the range and (@end, @endindex) will indicate the end
2479 xmlXPtrSearchString(const xmlChar
*string
, xmlNodePtr
*start
, int *startindex
,
2480 xmlNodePtr
*end
, int *endindex
) {
2483 int pos
; /* 0 based */
2484 int len
; /* in bytes */
2489 if ((start
== NULL
) || (*start
== NULL
) ||
2490 ((*start
)->type
== XML_NAMESPACE_DECL
) || (startindex
== NULL
))
2492 if ((end
== NULL
) || (endindex
== NULL
))
2495 pos
= *startindex
- 1;
2498 while (cur
!= NULL
) {
2499 if ((cur
->type
!= XML_ELEMENT_NODE
) && (cur
->content
!= NULL
)) {
2500 len
= xmlStrlen(cur
->content
);
2501 while (pos
<= len
) {
2503 str
= xmlStrchr(&cur
->content
[pos
], first
);
2505 pos
= (str
- (xmlChar
*)(cur
->content
));
2507 xmlGenericError(xmlGenericErrorContext
,
2508 "found '%c' at index %d of ->",
2510 xmlDebugDumpString(stdout
, cur
->content
);
2511 xmlGenericError(xmlGenericErrorContext
, "\n");
2513 if (xmlXPtrMatchString(string
, cur
, pos
+ 1,
2516 *startindex
= pos
+ 1;
2525 * An empty string is considered to match before each
2526 * character of the string-value and after the final
2530 xmlGenericError(xmlGenericErrorContext
,
2531 "found '' at index %d of ->",
2533 xmlDebugDumpString(stdout
, cur
->content
);
2534 xmlGenericError(xmlGenericErrorContext
, "\n");
2537 *startindex
= pos
+ 1;
2539 *endindex
= pos
+ 1;
2544 if ((cur
== *end
) && (pos
>= *endindex
))
2546 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2555 * xmlXPtrGetLastChar:
2559 * Computes the point coordinates of the last char of this point
2561 * Returns -1 in case of failure, 0 otherwise
2564 xmlXPtrGetLastChar(xmlNodePtr
*node
, int *indx
) {
2568 if ((node
== NULL
) || (*node
== NULL
) ||
2569 ((*node
)->type
== XML_NAMESPACE_DECL
) || (indx
== NULL
))
2574 if ((cur
->type
== XML_ELEMENT_NODE
) ||
2575 (cur
->type
== XML_DOCUMENT_NODE
) ||
2576 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
2578 cur
= xmlXPtrGetNthChild(cur
, pos
);
2581 while (cur
!= NULL
) {
2582 if (cur
->last
!= NULL
)
2584 else if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2585 (cur
->content
!= NULL
)) {
2586 len
= xmlStrlen(cur
->content
);
2600 * xmlXPtrGetStartPoint:
2602 * @node: the resulting node
2603 * @indx: the resulting index
2605 * read the object and return the start point coordinates.
2607 * Returns -1 in case of failure, 0 otherwise
2610 xmlXPtrGetStartPoint(xmlXPathObjectPtr obj
, xmlNodePtr
*node
, int *indx
) {
2611 if ((obj
== NULL
) || (node
== NULL
) || (indx
== NULL
))
2614 switch (obj
->type
) {
2617 if (obj
->index
<= 0)
2624 if (obj
->index
<= 0)
2636 * xmlXPtrGetEndPoint:
2638 * @node: the resulting node
2639 * @indx: the resulting indx
2641 * read the object and return the end point coordinates.
2643 * Returns -1 in case of failure, 0 otherwise
2646 xmlXPtrGetEndPoint(xmlXPathObjectPtr obj
, xmlNodePtr
*node
, int *indx
) {
2647 if ((obj
== NULL
) || (node
== NULL
) || (indx
== NULL
))
2650 switch (obj
->type
) {
2653 if (obj
->index
<= 0)
2660 if (obj
->index
<= 0)
2672 * xmlXPtrStringRangeFunction:
2673 * @ctxt: the XPointer Parser context
2674 * @nargs: the number of args
2676 * Function implementing the string-range() function
2677 * range as described in 5.4.2
2679 * ------------------------------
2680 * [Definition: For each location in the location-set argument,
2681 * string-range returns a set of string ranges, a set of substrings in a
2682 * string. Specifically, the string-value of the location is searched for
2683 * substrings that match the string argument, and the resulting location-set
2684 * will contain a range location for each non-overlapping match.]
2685 * An empty string is considered to match before each character of the
2686 * string-value and after the final character. Whitespace in a string
2687 * is matched literally, with no normalization except that provided by
2688 * XML for line ends. The third argument gives the position of the first
2689 * character to be in the resulting range, relative to the start of the
2690 * match. The default value is 1, which makes the range start immediately
2691 * before the first character of the matched string. The fourth argument
2692 * gives the number of characters in the range; the default is that the
2693 * range extends to the end of the matched string.
2695 * Element boundaries, as well as entire embedded nodes such as processing
2696 * instructions and comments, are ignored as defined in [XPath].
2698 * If the string in the second argument is not found in the string-value
2699 * of the location, or if a value in the third or fourth argument indicates
2700 * a string that is beyond the beginning or end of the document, the
2703 * The points of the range-locations in the returned location-set will
2704 * all be character points.
2705 * ------------------------------
2708 xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2709 int i
, startindex
, endindex
= 0, fendindex
;
2710 xmlNodePtr start
, end
= 0, fend
;
2711 xmlXPathObjectPtr set
= NULL
;
2712 xmlLocationSetPtr oldset
;
2713 xmlLocationSetPtr newset
= NULL
;
2714 xmlXPathObjectPtr string
= NULL
;
2715 xmlXPathObjectPtr position
= NULL
;
2716 xmlXPathObjectPtr number
= NULL
;
2717 int found
, pos
= 0, num
= 0;
2720 * Grab the arguments
2722 if ((nargs
< 2) || (nargs
> 4))
2723 XP_ERROR(XPATH_INVALID_ARITY
);
2726 if ((ctxt
->value
== NULL
) || (ctxt
->value
->type
!= XPATH_NUMBER
)) {
2727 xmlXPathErr(ctxt
, XPATH_INVALID_TYPE
);
2730 number
= valuePop(ctxt
);
2732 num
= (int) number
->floatval
;
2735 if ((ctxt
->value
== NULL
) || (ctxt
->value
->type
!= XPATH_NUMBER
)) {
2736 xmlXPathErr(ctxt
, XPATH_INVALID_TYPE
);
2739 position
= valuePop(ctxt
);
2740 if (position
!= NULL
)
2741 pos
= (int) position
->floatval
;
2743 if ((ctxt
->value
== NULL
) || (ctxt
->value
->type
!= XPATH_STRING
)) {
2744 xmlXPathErr(ctxt
, XPATH_INVALID_TYPE
);
2747 string
= valuePop(ctxt
);
2748 if ((ctxt
->value
== NULL
) ||
2749 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
2750 (ctxt
->value
->type
!= XPATH_NODESET
))) {
2751 xmlXPathErr(ctxt
, XPATH_INVALID_TYPE
);
2754 set
= valuePop(ctxt
);
2755 newset
= xmlXPtrLocationSetCreate(NULL
);
2756 if (newset
== NULL
) {
2757 xmlXPathErr(ctxt
, XPATH_MEMORY_ERROR
);
2760 if (set
->nodesetval
== NULL
) {
2763 if (set
->type
== XPATH_NODESET
) {
2764 xmlXPathObjectPtr tmp
;
2767 * First convert to a location set
2769 tmp
= xmlXPtrNewLocationSetNodeSet(set
->nodesetval
);
2770 xmlXPathFreeObject(set
);
2772 xmlXPathErr(ctxt
, XPATH_MEMORY_ERROR
);
2777 oldset
= (xmlLocationSetPtr
) set
->user
;
2780 * The loop is to search for each element in the location set
2781 * the list of location set corresponding to that search
2783 for (i
= 0;i
< oldset
->locNr
;i
++) {
2785 xmlXPathDebugDumpObject(stdout
, oldset
->locTab
[i
], 0);
2788 xmlXPtrGetStartPoint(oldset
->locTab
[i
], &start
, &startindex
);
2789 xmlXPtrGetEndPoint(oldset
->locTab
[i
], &end
, &endindex
);
2790 xmlXPtrAdvanceChar(&start
, &startindex
, 0);
2791 xmlXPtrGetLastChar(&end
, &endindex
);
2794 xmlGenericError(xmlGenericErrorContext
,
2795 "from index %d of ->", startindex
);
2796 xmlDebugDumpString(stdout
, start
->content
);
2797 xmlGenericError(xmlGenericErrorContext
, "\n");
2798 xmlGenericError(xmlGenericErrorContext
,
2799 "to index %d of ->", endindex
);
2800 xmlDebugDumpString(stdout
, end
->content
);
2801 xmlGenericError(xmlGenericErrorContext
, "\n");
2805 fendindex
= endindex
;
2806 found
= xmlXPtrSearchString(string
->stringval
, &start
, &startindex
,
2809 if (position
== NULL
) {
2810 xmlXPtrLocationSetAdd(newset
,
2811 xmlXPtrNewRange(start
, startindex
, fend
, fendindex
));
2812 } else if (xmlXPtrAdvanceChar(&start
, &startindex
,
2814 if ((number
!= NULL
) && (num
> 0)) {
2818 rindx
= startindex
- 1;
2819 if (xmlXPtrAdvanceChar(&rend
, &rindx
,
2821 xmlXPtrLocationSetAdd(newset
,
2822 xmlXPtrNewRange(start
, startindex
,
2825 } else if ((number
!= NULL
) && (num
<= 0)) {
2826 xmlXPtrLocationSetAdd(newset
,
2827 xmlXPtrNewRange(start
, startindex
,
2828 start
, startindex
));
2830 xmlXPtrLocationSetAdd(newset
,
2831 xmlXPtrNewRange(start
, startindex
,
2836 startindex
= fendindex
;
2837 if (string
->stringval
[0] == 0)
2840 } while (found
== 1);
2844 * Save the new value and cleanup
2848 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2849 xmlXPathFreeObject(set
);
2850 xmlXPathFreeObject(string
);
2851 if (position
) xmlXPathFreeObject(position
);
2852 if (number
) xmlXPathFreeObject(number
);
2856 * xmlXPtrEvalRangePredicate:
2857 * @ctxt: the XPointer Parser context
2859 * [8] Predicate ::= '[' PredicateExpr ']'
2860 * [9] PredicateExpr ::= Expr
2862 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2863 * a Location Set instead of a node set
2866 xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt
) {
2868 xmlXPathObjectPtr res
;
2869 xmlXPathObjectPtr obj
, tmp
;
2870 xmlLocationSetPtr newset
= NULL
;
2871 xmlLocationSetPtr oldset
;
2874 if (ctxt
== NULL
) return;
2878 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);
2884 * Extract the old set, and then evaluate the result of the
2885 * expression for all the element in the set. use it to grow
2888 CHECK_TYPE(XPATH_LOCATIONSET
);
2889 obj
= valuePop(ctxt
);
2891 ctxt
->context
->node
= NULL
;
2893 if ((oldset
== NULL
) || (oldset
->locNr
== 0)) {
2894 ctxt
->context
->contextSize
= 0;
2895 ctxt
->context
->proximityPosition
= 0;
2896 xmlXPathEvalExpr(ctxt
);
2897 res
= valuePop(ctxt
);
2899 xmlXPathFreeObject(res
);
2900 valuePush(ctxt
, obj
);
2904 * Save the expression pointer since we will have to evaluate
2905 * it multiple times. Initialize the new set.
2908 newset
= xmlXPtrLocationSetCreate(NULL
);
2910 for (i
= 0; i
< oldset
->locNr
; i
++) {
2914 * Run the evaluation with a node list made of a single item
2917 ctxt
->context
->node
= oldset
->locTab
[i
]->user
;
2918 tmp
= xmlXPathNewNodeSet(ctxt
->context
->node
);
2919 valuePush(ctxt
, tmp
);
2920 ctxt
->context
->contextSize
= oldset
->locNr
;
2921 ctxt
->context
->proximityPosition
= i
+ 1;
2923 xmlXPathEvalExpr(ctxt
);
2927 * The result of the evaluation need to be tested to
2928 * decided whether the filter succeeded or not
2930 res
= valuePop(ctxt
);
2931 if (xmlXPathEvaluatePredicateResult(ctxt
, res
)) {
2932 xmlXPtrLocationSetAdd(newset
,
2933 xmlXPathObjectCopy(oldset
->locTab
[i
]));
2940 xmlXPathFreeObject(res
);
2941 if (ctxt
->value
== tmp
) {
2942 res
= valuePop(ctxt
);
2943 xmlXPathFreeObject(res
);
2946 ctxt
->context
->node
= NULL
;
2950 * The result is used as the new evaluation set.
2952 xmlXPathFreeObject(obj
);
2953 ctxt
->context
->node
= NULL
;
2954 ctxt
->context
->contextSize
= -1;
2955 ctxt
->context
->proximityPosition
= -1;
2956 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2959 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);
2966 #define bottom_xpointer
2967 #include "elfgcchack.h"