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>
55 #include "private/error.h"
58 xmlGenericError(xmlGenericErrorContext, \
59 "Unimplemented block at %s:%d\n", \
63 xmlGenericError(xmlGenericErrorContext, \
64 "Internal error at %s:%d\n", \
67 /************************************************************************
69 * Some factorized error routines *
71 ************************************************************************/
75 * @extra: extra information
77 * Handle a redefinition of attribute error
80 xmlXPtrErrMemory(const char *extra
)
82 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_XPOINTER
,
83 XML_ERR_NO_MEMORY
, XML_ERR_ERROR
, NULL
, 0, extra
,
85 "Memory allocation failed : %s\n", extra
);
90 * @ctxt: an XPTR evaluation context
91 * @extra: extra information
93 * Handle a redefinition of attribute error
95 static void LIBXML_ATTR_FORMAT(3,0)
96 xmlXPtrErr(xmlXPathParserContextPtr ctxt
, int error
,
97 const char * msg
, const xmlChar
*extra
)
101 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) {
102 __xmlRaiseError(NULL
, NULL
, NULL
,
103 NULL
, NULL
, XML_FROM_XPOINTER
, error
,
104 XML_ERR_ERROR
, NULL
, 0,
105 (const char *) extra
, NULL
, NULL
, 0, 0,
110 /* cleanup current last error */
111 xmlResetError(&ctxt
->context
->lastError
);
113 ctxt
->context
->lastError
.domain
= XML_FROM_XPOINTER
;
114 ctxt
->context
->lastError
.code
= error
;
115 ctxt
->context
->lastError
.level
= XML_ERR_ERROR
;
116 ctxt
->context
->lastError
.str1
= (char *) xmlStrdup(ctxt
->base
);
117 ctxt
->context
->lastError
.int1
= ctxt
->cur
- ctxt
->base
;
118 ctxt
->context
->lastError
.node
= ctxt
->context
->debugNode
;
119 if (ctxt
->context
->error
!= NULL
) {
120 ctxt
->context
->error(ctxt
->context
->userData
,
121 &ctxt
->context
->lastError
);
123 __xmlRaiseError(NULL
, NULL
, NULL
,
124 NULL
, ctxt
->context
->debugNode
, XML_FROM_XPOINTER
,
125 error
, XML_ERR_ERROR
, NULL
, 0,
126 (const char *) extra
, (const char *) ctxt
->base
, NULL
,
127 ctxt
->cur
- ctxt
->base
, 0,
132 /************************************************************************
134 * A few helper functions for child sequences *
136 ************************************************************************/
137 #ifdef LIBXML_XPTR_LOCS_ENABLED
138 /* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
139 xmlNodePtr
xmlXPtrAdvanceNode(xmlNodePtr cur
, int *level
);
144 * Returns the number of child for an element, -1 in case of error
147 xmlXPtrGetArity(xmlNodePtr cur
) {
149 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
152 for (i
= 0;cur
!= NULL
;cur
= cur
->next
) {
153 if ((cur
->type
== XML_ELEMENT_NODE
) ||
154 (cur
->type
== XML_DOCUMENT_NODE
) ||
155 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
166 * Returns the index of the node in its parent children list, -1
170 xmlXPtrGetIndex(xmlNodePtr cur
) {
172 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
174 for (i
= 1;cur
!= NULL
;cur
= cur
->prev
) {
175 if ((cur
->type
== XML_ELEMENT_NODE
) ||
176 (cur
->type
== XML_DOCUMENT_NODE
) ||
177 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
183 #endif /* LIBXML_XPTR_LOCS_ENABLED */
186 * xmlXPtrGetNthChild:
188 * @no: the child number
190 * Returns the @no'th element child of @cur or NULL
193 xmlXPtrGetNthChild(xmlNodePtr cur
, int no
) {
195 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
198 for (i
= 0;i
<= no
;cur
= cur
->next
) {
201 if ((cur
->type
== XML_ELEMENT_NODE
) ||
202 (cur
->type
== XML_DOCUMENT_NODE
) ||
203 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
212 #ifdef LIBXML_XPTR_LOCS_ENABLED
213 /************************************************************************
215 * Handling of XPointer specific types *
217 ************************************************************************/
221 * @node1: the first node
222 * @index1: the first index
223 * @node2: the second node
224 * @index2: the second index
226 * Compare two points w.r.t document order
228 * Returns -2 in case of error 1 if first point < second point, 0 if
229 * that's the same point, -1 otherwise
232 xmlXPtrCmpPoints(xmlNodePtr node1
, int index1
, xmlNodePtr node2
, int index2
) {
233 if ((node1
== NULL
) || (node2
== NULL
))
236 * a couple of optimizations which will avoid computations in most cases
238 if (node1
== node2
) {
245 return(xmlXPathCmpNodes(node1
, node2
));
250 * @node: the xmlNodePtr
251 * @indx: the indx within the node
253 * Create a new xmlXPathObjectPtr of type point
255 * Returns the newly created object.
257 static xmlXPathObjectPtr
258 xmlXPtrNewPoint(xmlNodePtr node
, int indx
) {
259 xmlXPathObjectPtr ret
;
266 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
268 xmlXPtrErrMemory("allocating point");
271 memset(ret
, 0 , sizeof(xmlXPathObject
));
272 ret
->type
= XPATH_POINT
;
273 ret
->user
= (void *) node
;
279 * xmlXPtrRangeCheckOrder:
280 * @range: an object range
282 * Make sure the points in the range are in the right order
285 xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range
) {
290 if (range
->type
!= XPATH_RANGE
)
292 if (range
->user2
== NULL
)
294 tmp
= xmlXPtrCmpPoints(range
->user
, range
->index
,
295 range
->user2
, range
->index2
);
298 range
->user
= range
->user2
;
301 range
->index
= range
->index2
;
307 * xmlXPtrRangesEqual:
308 * @range1: the first range
309 * @range2: the second range
313 * Returns 1 if equal, 0 otherwise
316 xmlXPtrRangesEqual(xmlXPathObjectPtr range1
, xmlXPathObjectPtr range2
) {
317 if (range1
== range2
)
319 if ((range1
== NULL
) || (range2
== NULL
))
321 if (range1
->type
!= range2
->type
)
323 if (range1
->type
!= XPATH_RANGE
)
325 if (range1
->user
!= range2
->user
)
327 if (range1
->index
!= range2
->index
)
329 if (range1
->user2
!= range2
->user2
)
331 if (range1
->index2
!= range2
->index2
)
337 * xmlXPtrNewRangeInternal:
338 * @start: the starting node
339 * @startindex: the start index
340 * @end: the ending point
341 * @endindex: the ending index
343 * Internal function to create a new xmlXPathObjectPtr of type range
345 * Returns the newly created object.
347 static xmlXPathObjectPtr
348 xmlXPtrNewRangeInternal(xmlNodePtr start
, int startindex
,
349 xmlNodePtr end
, int endindex
) {
350 xmlXPathObjectPtr ret
;
353 * Namespace nodes must be copied (see xmlXPathNodeSetDupNs).
354 * Disallow them for now.
356 if ((start
!= NULL
) && (start
->type
== XML_NAMESPACE_DECL
))
358 if ((end
!= NULL
) && (end
->type
== XML_NAMESPACE_DECL
))
361 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
363 xmlXPtrErrMemory("allocating range");
366 memset(ret
, 0, sizeof(xmlXPathObject
));
367 ret
->type
= XPATH_RANGE
;
369 ret
->index
= startindex
;
371 ret
->index2
= endindex
;
377 * @start: the starting node
378 * @startindex: the start index
379 * @end: the ending point
380 * @endindex: the ending index
382 * Create a new xmlXPathObjectPtr of type range
384 * Returns the newly created object.
387 xmlXPtrNewRange(xmlNodePtr start
, int startindex
,
388 xmlNodePtr end
, int endindex
) {
389 xmlXPathObjectPtr ret
;
400 ret
= xmlXPtrNewRangeInternal(start
, startindex
, end
, endindex
);
401 xmlXPtrRangeCheckOrder(ret
);
406 * xmlXPtrNewRangePoints:
407 * @start: the starting point
408 * @end: the ending point
410 * Create a new xmlXPathObjectPtr of type range using 2 Points
412 * Returns the newly created object.
415 xmlXPtrNewRangePoints(xmlXPathObjectPtr start
, xmlXPathObjectPtr end
) {
416 xmlXPathObjectPtr ret
;
422 if (start
->type
!= XPATH_POINT
)
424 if (end
->type
!= XPATH_POINT
)
427 ret
= xmlXPtrNewRangeInternal(start
->user
, start
->index
, end
->user
,
429 xmlXPtrRangeCheckOrder(ret
);
434 * xmlXPtrNewRangePointNode:
435 * @start: the starting point
436 * @end: the ending node
438 * Create a new xmlXPathObjectPtr of type range from a point to a node
440 * Returns the newly created object.
443 xmlXPtrNewRangePointNode(xmlXPathObjectPtr start
, xmlNodePtr end
) {
444 xmlXPathObjectPtr ret
;
450 if (start
->type
!= XPATH_POINT
)
453 ret
= xmlXPtrNewRangeInternal(start
->user
, start
->index
, end
, -1);
454 xmlXPtrRangeCheckOrder(ret
);
459 * xmlXPtrNewRangeNodePoint:
460 * @start: the starting node
461 * @end: the ending point
463 * Create a new xmlXPathObjectPtr of type range from a node to a point
465 * Returns the newly created object.
468 xmlXPtrNewRangeNodePoint(xmlNodePtr start
, xmlXPathObjectPtr end
) {
469 xmlXPathObjectPtr ret
;
475 if (end
->type
!= XPATH_POINT
)
478 ret
= xmlXPtrNewRangeInternal(start
, -1, end
->user
, end
->index
);
479 xmlXPtrRangeCheckOrder(ret
);
484 * xmlXPtrNewRangeNodes:
485 * @start: the starting node
486 * @end: the ending node
488 * Create a new xmlXPathObjectPtr of type range using 2 nodes
490 * Returns the newly created object.
493 xmlXPtrNewRangeNodes(xmlNodePtr start
, xmlNodePtr end
) {
494 xmlXPathObjectPtr ret
;
501 ret
= xmlXPtrNewRangeInternal(start
, -1, end
, -1);
502 xmlXPtrRangeCheckOrder(ret
);
507 * xmlXPtrNewCollapsedRange:
508 * @start: the starting and ending node
510 * Create a new xmlXPathObjectPtr of type range using a single nodes
512 * Returns the newly created object.
515 xmlXPtrNewCollapsedRange(xmlNodePtr start
) {
516 xmlXPathObjectPtr ret
;
521 ret
= xmlXPtrNewRangeInternal(start
, -1, NULL
, -1);
526 * xmlXPtrNewRangeNodeObject:
527 * @start: the starting node
528 * @end: the ending object
530 * Create a new xmlXPathObjectPtr of type range from a not to an object
532 * Returns the newly created object.
535 xmlXPtrNewRangeNodeObject(xmlNodePtr start
, xmlXPathObjectPtr end
) {
538 xmlXPathObjectPtr ret
;
547 endIndex
= end
->index
;
550 endNode
= end
->user2
;
551 endIndex
= end
->index2
;
557 if ((end
->nodesetval
== NULL
) || (end
->nodesetval
->nodeNr
<= 0))
559 endNode
= end
->nodesetval
->nodeTab
[end
->nodesetval
->nodeNr
- 1];
567 ret
= xmlXPtrNewRangeInternal(start
, -1, endNode
, endIndex
);
568 xmlXPtrRangeCheckOrder(ret
);
572 #define XML_RANGESET_DEFAULT 10
575 * xmlXPtrLocationSetCreate:
576 * @val: an initial xmlXPathObjectPtr, or NULL
578 * Create a new xmlLocationSetPtr of type double and of value @val
580 * Returns the newly created object.
583 xmlXPtrLocationSetCreate(xmlXPathObjectPtr val
) {
584 xmlLocationSetPtr ret
;
586 ret
= (xmlLocationSetPtr
) xmlMalloc(sizeof(xmlLocationSet
));
588 xmlXPtrErrMemory("allocating locationset");
591 memset(ret
, 0 , sizeof(xmlLocationSet
));
593 ret
->locTab
= (xmlXPathObjectPtr
*) xmlMalloc(XML_RANGESET_DEFAULT
*
594 sizeof(xmlXPathObjectPtr
));
595 if (ret
->locTab
== NULL
) {
596 xmlXPtrErrMemory("allocating locationset");
600 memset(ret
->locTab
, 0 ,
601 XML_RANGESET_DEFAULT
* sizeof(xmlXPathObjectPtr
));
602 ret
->locMax
= XML_RANGESET_DEFAULT
;
603 ret
->locTab
[ret
->locNr
++] = val
;
609 * xmlXPtrLocationSetAdd:
610 * @cur: the initial range set
611 * @val: a new xmlXPathObjectPtr
613 * add a new xmlXPathObjectPtr to an existing LocationSet
614 * If the location already exist in the set @val is freed.
617 xmlXPtrLocationSetAdd(xmlLocationSetPtr cur
, xmlXPathObjectPtr val
) {
620 if ((cur
== NULL
) || (val
== NULL
)) return;
623 * check against doublons
625 for (i
= 0;i
< cur
->locNr
;i
++) {
626 if (xmlXPtrRangesEqual(cur
->locTab
[i
], val
)) {
627 xmlXPathFreeObject(val
);
633 * grow the locTab if needed
635 if (cur
->locMax
== 0) {
636 cur
->locTab
= (xmlXPathObjectPtr
*) xmlMalloc(XML_RANGESET_DEFAULT
*
637 sizeof(xmlXPathObjectPtr
));
638 if (cur
->locTab
== NULL
) {
639 xmlXPtrErrMemory("adding location to set");
642 memset(cur
->locTab
, 0 ,
643 XML_RANGESET_DEFAULT
* sizeof(xmlXPathObjectPtr
));
644 cur
->locMax
= XML_RANGESET_DEFAULT
;
645 } else if (cur
->locNr
== cur
->locMax
) {
646 xmlXPathObjectPtr
*temp
;
649 temp
= (xmlXPathObjectPtr
*) xmlRealloc(cur
->locTab
, cur
->locMax
*
650 sizeof(xmlXPathObjectPtr
));
652 xmlXPtrErrMemory("adding location to set");
657 cur
->locTab
[cur
->locNr
++] = val
;
661 * xmlXPtrLocationSetMerge:
662 * @val1: the first LocationSet
663 * @val2: the second LocationSet
665 * Merges two rangesets, all ranges from @val2 are added to @val1
667 * Returns val1 once extended or NULL in case of error.
670 xmlXPtrLocationSetMerge(xmlLocationSetPtr val1
, xmlLocationSetPtr val2
) {
673 if (val1
== NULL
) return(NULL
);
674 if (val2
== NULL
) return(val1
);
677 * !!!!! this can be optimized a lot, knowing that both
678 * val1 and val2 already have unicity of their values.
681 for (i
= 0;i
< val2
->locNr
;i
++)
682 xmlXPtrLocationSetAdd(val1
, val2
->locTab
[i
]);
688 * xmlXPtrLocationSetDel:
689 * @cur: the initial range set
690 * @val: an xmlXPathObjectPtr
692 * Removes an xmlXPathObjectPtr from an existing LocationSet
695 xmlXPtrLocationSetDel(xmlLocationSetPtr cur
, xmlXPathObjectPtr val
) {
698 if (cur
== NULL
) return;
699 if (val
== NULL
) return;
702 * check against doublons
704 for (i
= 0;i
< cur
->locNr
;i
++)
705 if (cur
->locTab
[i
] == val
) break;
707 if (i
>= cur
->locNr
) {
709 xmlGenericError(xmlGenericErrorContext
,
710 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
715 for (;i
< cur
->locNr
;i
++)
716 cur
->locTab
[i
] = cur
->locTab
[i
+ 1];
717 cur
->locTab
[cur
->locNr
] = NULL
;
721 * xmlXPtrLocationSetRemove:
722 * @cur: the initial range set
723 * @val: the index to remove
725 * Removes an entry from an existing LocationSet list.
728 xmlXPtrLocationSetRemove(xmlLocationSetPtr cur
, int val
) {
729 if (cur
== NULL
) return;
730 if (val
>= cur
->locNr
) return;
732 for (;val
< cur
->locNr
;val
++)
733 cur
->locTab
[val
] = cur
->locTab
[val
+ 1];
734 cur
->locTab
[cur
->locNr
] = NULL
;
738 * xmlXPtrFreeLocationSet:
739 * @obj: the xmlLocationSetPtr to free
741 * Free the LocationSet compound (not the actual ranges !).
744 xmlXPtrFreeLocationSet(xmlLocationSetPtr obj
) {
747 if (obj
== NULL
) return;
748 if (obj
->locTab
!= NULL
) {
749 for (i
= 0;i
< obj
->locNr
; i
++) {
750 xmlXPathFreeObject(obj
->locTab
[i
]);
752 xmlFree(obj
->locTab
);
758 * xmlXPtrNewLocationSetNodes:
759 * @start: the start NodePtr value
760 * @end: the end NodePtr value or NULL
762 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
763 * it with the single range made of the two nodes @start and @end
765 * Returns the newly created object.
768 xmlXPtrNewLocationSetNodes(xmlNodePtr start
, xmlNodePtr end
) {
769 xmlXPathObjectPtr ret
;
771 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
773 xmlXPtrErrMemory("allocating locationset");
776 memset(ret
, 0 , sizeof(xmlXPathObject
));
777 ret
->type
= XPATH_LOCATIONSET
;
779 ret
->user
= xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start
));
781 ret
->user
= xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start
,end
));
786 * xmlXPtrNewLocationSetNodeSet:
789 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
790 * it with all the nodes from @set
792 * Returns the newly created object.
795 xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set
) {
796 xmlXPathObjectPtr ret
;
798 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
800 xmlXPtrErrMemory("allocating locationset");
803 memset(ret
, 0, sizeof(xmlXPathObject
));
804 ret
->type
= XPATH_LOCATIONSET
;
807 xmlLocationSetPtr newset
;
809 newset
= xmlXPtrLocationSetCreate(NULL
);
813 for (i
= 0;i
< set
->nodeNr
;i
++)
814 xmlXPtrLocationSetAdd(newset
,
815 xmlXPtrNewCollapsedRange(set
->nodeTab
[i
]));
817 ret
->user
= (void *) newset
;
823 * xmlXPtrWrapLocationSet:
824 * @val: the LocationSet value
826 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
828 * Returns the newly created object.
831 xmlXPtrWrapLocationSet(xmlLocationSetPtr val
) {
832 xmlXPathObjectPtr ret
;
834 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
836 xmlXPtrErrMemory("allocating locationset");
839 memset(ret
, 0, sizeof(xmlXPathObject
));
840 ret
->type
= XPATH_LOCATIONSET
;
841 ret
->user
= (void *) val
;
844 #endif /* LIBXML_XPTR_LOCS_ENABLED */
846 /************************************************************************
850 ************************************************************************/
852 static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt
, xmlChar
*name
);
855 * Macros for accessing the content. Those should be used only by the parser,
858 * Dirty macros, i.e. one need to make assumption on the context to use them
860 * CUR returns the current xmlChar value, i.e. a 8 bit value
861 * in ISO-Latin or UTF-8.
862 * This should be used internally by the parser
863 * only to compare to ASCII values otherwise it would break when
864 * running with UTF-8 encoding.
865 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
866 * to compare on ASCII based substring.
867 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
868 * strings within the parser.
869 * CURRENT Returns the current char value, with the full decoding of
870 * UTF-8 if we are using this mode. It returns an int.
871 * NEXT Skip to the next character, this does the proper decoding
872 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
873 * It returns the pointer to the current xmlChar.
876 #define CUR (*ctxt->cur)
877 #define SKIP(val) ctxt->cur += (val)
878 #define NXT(val) ctxt->cur[(val)]
880 #define SKIP_BLANKS \
881 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
883 #define CURRENT (*ctxt->cur)
884 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
888 * @ctxt: the XPointer Parser context
889 * @index: the child number
891 * Move the current node of the nodeset on the stack to the
892 * given child if found
895 xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt
, int indx
) {
896 xmlNodePtr cur
= NULL
;
897 xmlXPathObjectPtr obj
;
898 xmlNodeSetPtr oldset
;
900 CHECK_TYPE(XPATH_NODESET
);
901 obj
= valuePop(ctxt
);
902 oldset
= obj
->nodesetval
;
903 if ((indx
<= 0) || (oldset
== NULL
) || (oldset
->nodeNr
!= 1)) {
904 xmlXPathFreeObject(obj
);
905 valuePush(ctxt
, xmlXPathNewNodeSet(NULL
));
908 cur
= xmlXPtrGetNthChild(oldset
->nodeTab
[0], indx
);
910 xmlXPathFreeObject(obj
);
911 valuePush(ctxt
, xmlXPathNewNodeSet(NULL
));
914 oldset
->nodeTab
[0] = cur
;
915 valuePush(ctxt
, obj
);
919 * xmlXPtrEvalXPtrPart:
920 * @ctxt: the XPointer Parser context
921 * @name: the preparsed Scheme for the XPtrPart
923 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
924 * | Scheme '(' SchemeSpecificExpr ')'
926 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
928 * SchemeSpecificExpr ::= StringWithBalancedParens
930 * StringWithBalancedParens ::=
931 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
932 * [VC: Parenthesis escaping]
934 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
936 * VC: Parenthesis escaping:
937 * The end of an XPointer part is signaled by the right parenthesis ")"
938 * character that is balanced with the left parenthesis "(" character
939 * that began the part. Any unbalanced parenthesis character inside the
940 * expression, even within literals, must be escaped with a circumflex (^)
941 * character preceding it. If the expression contains any literal
942 * occurrences of the circumflex, each must be escaped with an additional
943 * circumflex (that is, ^^). If the unescaped parentheses in the expression
944 * are not balanced, a syntax error results.
946 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
947 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
949 * TODO: there is no new scheme registration mechanism
953 xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
954 xmlChar
*buffer
, *cur
;
959 name
= xmlXPathParseName(ctxt
);
961 XP_ERROR(XPATH_EXPR_ERROR
);
965 XP_ERROR(XPATH_EXPR_ERROR
);
970 len
= xmlStrlen(ctxt
->cur
);
972 buffer
= (xmlChar
*) xmlMallocAtomic(len
);
973 if (buffer
== NULL
) {
974 xmlXPtrErrMemory("allocating buffer");
987 } else if (CUR
== '(') {
989 } else if (CUR
== '^') {
990 if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
999 if ((level
!= 0) && (CUR
== 0)) {
1002 XP_ERROR(XPTR_SYNTAX_ERROR
);
1005 if (xmlStrEqual(name
, (xmlChar
*) "xpointer") ||
1006 xmlStrEqual(name
, (xmlChar
*) "xpath1")) {
1007 const xmlChar
*oldBase
= ctxt
->base
;
1008 const xmlChar
*oldCur
= ctxt
->cur
;
1010 ctxt
->cur
= ctxt
->base
= buffer
;
1012 * To evaluate an xpointer scheme element (4.3) we need:
1013 * context initialized to the root
1014 * context position initialized to 1
1015 * context size initialized to 1
1017 ctxt
->context
->node
= (xmlNodePtr
)ctxt
->context
->doc
;
1018 ctxt
->context
->proximityPosition
= 1;
1019 ctxt
->context
->contextSize
= 1;
1020 #ifdef LIBXML_XPTR_LOCS_ENABLED
1021 ctxt
->xptr
= xmlStrEqual(name
, (xmlChar
*) "xpointer");
1023 xmlXPathEvalExpr(ctxt
);
1024 ctxt
->base
= oldBase
;
1026 } else if (xmlStrEqual(name
, (xmlChar
*) "element")) {
1027 const xmlChar
*oldBase
= ctxt
->base
;
1028 const xmlChar
*oldCur
= ctxt
->cur
;
1031 ctxt
->cur
= ctxt
->base
= buffer
;
1032 if (buffer
[0] == '/') {
1034 xmlXPtrEvalChildSeq(ctxt
, NULL
);
1036 name2
= xmlXPathParseName(ctxt
);
1037 if (name2
== NULL
) {
1038 ctxt
->base
= oldBase
;
1042 XP_ERROR(XPATH_EXPR_ERROR
);
1044 xmlXPtrEvalChildSeq(ctxt
, name2
);
1046 ctxt
->base
= oldBase
;
1048 #ifdef XPTR_XMLNS_SCHEME
1049 } else if (xmlStrEqual(name
, (xmlChar
*) "xmlns")) {
1050 const xmlChar
*oldBase
= ctxt
->base
;
1051 const xmlChar
*oldCur
= ctxt
->cur
;
1054 ctxt
->cur
= ctxt
->base
= buffer
;
1055 prefix
= xmlXPathParseNCName(ctxt
);
1056 if (prefix
== NULL
) {
1057 ctxt
->base
= oldBase
;
1061 XP_ERROR(XPTR_SYNTAX_ERROR
);
1065 ctxt
->base
= oldBase
;
1070 XP_ERROR(XPTR_SYNTAX_ERROR
);
1075 xmlXPathRegisterNs(ctxt
->context
, prefix
, ctxt
->cur
);
1076 ctxt
->base
= oldBase
;
1079 #endif /* XPTR_XMLNS_SCHEME */
1081 xmlXPtrErr(ctxt
, XML_XPTR_UNKNOWN_SCHEME
,
1082 "unsupported scheme '%s'\n", name
);
1089 * xmlXPtrEvalFullXPtr:
1090 * @ctxt: the XPointer Parser context
1091 * @name: the preparsed Scheme for the first XPtrPart
1093 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1095 * As the specs says:
1097 * When multiple XPtrParts are provided, they must be evaluated in
1098 * left-to-right order. If evaluation of one part fails, the nexti
1099 * is evaluated. The following conditions cause XPointer part failure:
1101 * - An unknown scheme
1102 * - A scheme that does not locate any sub-resource present in the resource
1103 * - A scheme that is not applicable to the media type of the resource
1105 * The XPointer application must consume a failed XPointer part and
1106 * attempt to evaluate the next one, if any. The result of the first
1107 * XPointer part whose evaluation succeeds is taken to be the fragment
1108 * located by the XPointer as a whole. If all the parts fail, the result
1109 * for the XPointer as a whole is a sub-resource error.
1112 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1113 * expressions or other schemes.
1116 xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
1118 name
= xmlXPathParseName(ctxt
);
1120 XP_ERROR(XPATH_EXPR_ERROR
);
1121 while (name
!= NULL
) {
1122 ctxt
->error
= XPATH_EXPRESSION_OK
;
1123 xmlXPtrEvalXPtrPart(ctxt
, name
);
1125 /* in case of syntax error, break here */
1126 if ((ctxt
->error
!= XPATH_EXPRESSION_OK
) &&
1127 (ctxt
->error
!= XML_XPTR_UNKNOWN_SCHEME
))
1131 * If the returned value is a non-empty nodeset
1132 * or location set, return here.
1134 if (ctxt
->value
!= NULL
) {
1135 xmlXPathObjectPtr obj
= ctxt
->value
;
1137 switch (obj
->type
) {
1138 #ifdef LIBXML_XPTR_LOCS_ENABLED
1139 case XPATH_LOCATIONSET
: {
1140 xmlLocationSetPtr loc
= ctxt
->value
->user
;
1141 if ((loc
!= NULL
) && (loc
->locNr
> 0))
1146 case XPATH_NODESET
: {
1147 xmlNodeSetPtr loc
= ctxt
->value
->nodesetval
;
1148 if ((loc
!= NULL
) && (loc
->nodeNr
> 0))
1157 * Evaluating to improper values is equivalent to
1158 * a sub-resource error, clean-up the stack
1161 obj
= valuePop(ctxt
);
1163 xmlXPathFreeObject(obj
);
1165 } while (obj
!= NULL
);
1169 * Is there another XPointer part.
1172 name
= xmlXPathParseName(ctxt
);
1177 * xmlXPtrEvalChildSeq:
1178 * @ctxt: the XPointer Parser context
1179 * @name: a possible ID name of the child sequence
1181 * ChildSeq ::= '/1' ('/' [0-9]*)*
1182 * | Name ('/' [0-9]*)+
1184 * Parse and evaluate a Child Sequence. This routine also handle the
1185 * case of a Bare Name used to get a document ID.
1188 xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
1190 * XPointer don't allow by syntax to address in multirooted trees
1191 * this might prove useful in some cases, warn about it.
1193 if ((name
== NULL
) && (CUR
== '/') && (NXT(1) != '1')) {
1194 xmlXPtrErr(ctxt
, XML_XPTR_CHILDSEQ_START
,
1195 "warning: ChildSeq not starting by /1\n", NULL
);
1199 valuePush(ctxt
, xmlXPathNewString(name
));
1201 xmlXPathIdFunction(ctxt
, 1);
1205 while (CUR
== '/') {
1206 int child
= 0, overflow
= 0;
1209 while ((CUR
>= '0') && (CUR
<= '9')) {
1211 if (child
> INT_MAX
/ 10)
1215 if (child
> INT_MAX
- d
)
1223 xmlXPtrGetChildNo(ctxt
, child
);
1229 * xmlXPtrEvalXPointer:
1230 * @ctxt: the XPointer Parser context
1236 * Parse and evaluate an XPointer
1239 xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt
) {
1240 if (ctxt
->valueTab
== NULL
) {
1241 /* Allocate the value stack */
1242 ctxt
->valueTab
= (xmlXPathObjectPtr
*)
1243 xmlMalloc(10 * sizeof(xmlXPathObjectPtr
));
1244 if (ctxt
->valueTab
== NULL
) {
1245 xmlXPtrErrMemory("allocating evaluation context");
1249 ctxt
->valueMax
= 10;
1251 ctxt
->valueFrame
= 0;
1256 xmlXPtrEvalChildSeq(ctxt
, NULL
);
1260 name
= xmlXPathParseName(ctxt
);
1262 XP_ERROR(XPATH_EXPR_ERROR
);
1264 xmlXPtrEvalFullXPtr(ctxt
, name
);
1265 /* Short evaluation */
1268 /* this handle both Bare Names and Child Sequences */
1269 xmlXPtrEvalChildSeq(ctxt
, name
);
1274 XP_ERROR(XPATH_EXPR_ERROR
);
1278 /************************************************************************
1280 * General routines *
1282 ************************************************************************/
1284 #ifdef LIBXML_XPTR_LOCS_ENABLED
1286 void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1288 void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1290 void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1292 void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1294 void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1296 void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1298 void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1299 #endif /* LIBXML_XPTR_LOCS_ENABLED */
1302 * xmlXPtrNewContext:
1303 * @doc: the XML document
1304 * @here: the node that directly contains the XPointer being evaluated or NULL
1305 * @origin: the element from which a user or program initiated traversal of
1306 * the link, or NULL.
1308 * Create a new XPointer context
1310 * Returns the xmlXPathContext just allocated.
1313 xmlXPtrNewContext(xmlDocPtr doc
, xmlNodePtr here
, xmlNodePtr origin
) {
1314 xmlXPathContextPtr ret
;
1318 ret
= xmlXPathNewContext(doc
);
1321 #ifdef LIBXML_XPTR_LOCS_ENABLED
1324 ret
->origin
= origin
;
1326 xmlXPathRegisterFunc(ret
, (xmlChar
*)"range",
1327 xmlXPtrRangeFunction
);
1328 xmlXPathRegisterFunc(ret
, (xmlChar
*)"range-inside",
1329 xmlXPtrRangeInsideFunction
);
1330 xmlXPathRegisterFunc(ret
, (xmlChar
*)"string-range",
1331 xmlXPtrStringRangeFunction
);
1332 xmlXPathRegisterFunc(ret
, (xmlChar
*)"start-point",
1333 xmlXPtrStartPointFunction
);
1334 xmlXPathRegisterFunc(ret
, (xmlChar
*)"end-point",
1335 xmlXPtrEndPointFunction
);
1336 xmlXPathRegisterFunc(ret
, (xmlChar
*)"here",
1337 xmlXPtrHereFunction
);
1338 xmlXPathRegisterFunc(ret
, (xmlChar
*)" origin",
1339 xmlXPtrOriginFunction
);
1340 #endif /* LIBXML_XPTR_LOCS_ENABLED */
1347 * @str: the XPointer expression
1348 * @ctx: the XPointer context
1350 * Evaluate the XPath Location Path in the given context.
1352 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
1353 * the caller has to free the object.
1356 xmlXPtrEval(const xmlChar
*str
, xmlXPathContextPtr ctx
) {
1357 xmlXPathParserContextPtr ctxt
;
1358 xmlXPathObjectPtr res
= NULL
, tmp
;
1359 xmlXPathObjectPtr init
= NULL
;
1364 if ((ctx
== NULL
) || (str
== NULL
))
1367 ctxt
= xmlXPathNewParserContext(str
, ctx
);
1370 xmlXPtrEvalXPointer(ctxt
);
1372 if ((ctxt
->value
!= NULL
) &&
1373 #ifdef LIBXML_XPTR_LOCS_ENABLED
1374 (ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
1376 (ctxt
->value
->type
!= XPATH_NODESET
)) {
1377 xmlXPtrErr(ctxt
, XML_XPTR_EVAL_FAILED
,
1378 "xmlXPtrEval: evaluation failed to return a node set\n",
1381 res
= valuePop(ctxt
);
1385 tmp
= valuePop(ctxt
);
1388 if (tmp
->type
== XPATH_NODESET
) {
1390 * Evaluation may push a root nodeset which is unused
1393 set
= tmp
->nodesetval
;
1394 if ((set
== NULL
) || (set
->nodeNr
!= 1) ||
1395 (set
->nodeTab
[0] != (xmlNodePtr
) ctx
->doc
))
1400 xmlXPathFreeObject(tmp
);
1402 } while (tmp
!= NULL
);
1404 xmlXPtrErr(ctxt
, XML_XPTR_EXTRA_OBJECTS
,
1405 "xmlXPtrEval: object(s) left on the eval stack\n",
1408 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
1409 xmlXPathFreeObject(res
);
1413 xmlXPathFreeParserContext(ctxt
);
1417 #ifdef LIBXML_XPTR_LOCS_ENABLED
1419 * xmlXPtrBuildRangeNodeList:
1420 * @range: a range object
1422 * Build a node list tree copy of the range
1424 * Returns an xmlNodePtr list or NULL.
1425 * the caller has to free the node tree.
1428 xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range
) {
1429 /* pointers to generated nodes */
1430 xmlNodePtr list
= NULL
, last
= NULL
, parent
= NULL
, tmp
;
1431 /* pointers to traversal nodes */
1432 xmlNodePtr start
, cur
, end
;
1437 if (range
->type
!= XPATH_RANGE
)
1439 start
= (xmlNodePtr
) range
->user
;
1441 if ((start
== NULL
) || (start
->type
== XML_NAMESPACE_DECL
))
1445 return(xmlCopyNode(start
, 1));
1446 if (end
->type
== XML_NAMESPACE_DECL
)
1450 index1
= range
->index
;
1451 index2
= range
->index2
;
1452 while (cur
!= NULL
) {
1454 if (cur
->type
== XML_TEXT_NODE
) {
1455 const xmlChar
*content
= cur
->content
;
1458 if (content
== NULL
) {
1459 tmp
= xmlNewTextLen(NULL
, 0);
1462 if ((cur
== start
) && (index1
> 1)) {
1463 content
+= (index1
- 1);
1464 len
-= (index1
- 1);
1469 tmp
= xmlNewTextLen(content
, len
);
1471 /* single sub text node selection */
1474 /* prune and return full set */
1476 xmlAddNextSibling(last
, tmp
);
1478 xmlAddChild(parent
, tmp
);
1481 tmp
= xmlCopyNode(cur
, 0);
1487 parent
= xmlAddNextSibling(last
, tmp
);
1489 parent
= xmlAddChild(parent
, tmp
);
1494 end
= xmlXPtrGetNthChild(cur
, index2
- 1);
1497 if ((cur
== start
) && (index1
> 1)) {
1498 cur
= xmlXPtrGetNthChild(cur
, index1
- 1);
1501 cur
= cur
->children
;
1504 * Now gather the remaining nodes from cur to end
1506 continue; /* while */
1508 } else if ((cur
== start
) &&
1509 (list
== NULL
) /* looks superfluous but ... */ ) {
1510 if ((cur
->type
== XML_TEXT_NODE
) ||
1511 (cur
->type
== XML_CDATA_SECTION_NODE
)) {
1512 const xmlChar
*content
= cur
->content
;
1514 if (content
== NULL
) {
1515 tmp
= xmlNewTextLen(NULL
, 0);
1518 content
+= (index1
- 1);
1520 tmp
= xmlNewText(content
);
1524 if ((cur
== start
) && (index1
> 1)) {
1525 tmp
= xmlCopyNode(cur
, 0);
1529 cur
= xmlXPtrGetNthChild(cur
, index1
- 1);
1532 * Now gather the remaining nodes from cur to end
1534 continue; /* while */
1536 tmp
= xmlCopyNode(cur
, 1);
1543 switch (cur
->type
) {
1545 case XML_ELEMENT_DECL
:
1546 case XML_ATTRIBUTE_DECL
:
1547 case XML_ENTITY_NODE
:
1548 /* Do not copy DTD information */
1550 case XML_ENTITY_DECL
:
1551 TODO
/* handle crossing entities -> stack needed */
1553 case XML_XINCLUDE_START
:
1554 case XML_XINCLUDE_END
:
1555 /* don't consider it part of the tree content */
1557 case XML_ATTRIBUTE_NODE
:
1558 /* Humm, should not happen ! */
1562 tmp
= xmlCopyNode(cur
, 1);
1566 if ((list
== NULL
) || ((last
== NULL
) && (parent
== NULL
))) {
1571 xmlAddNextSibling(last
, tmp
);
1573 last
= xmlAddChild(parent
, tmp
);
1578 * Skip to next node in document order
1580 if ((list
== NULL
) || ((last
== NULL
) && (parent
== NULL
))) {
1584 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
1590 * xmlXPtrBuildNodeList:
1591 * @obj: the XPointer result from the evaluation.
1593 * Build a node list tree copy of the XPointer result.
1594 * This will drop Attributes and Namespace declarations.
1596 * Returns an xmlNodePtr list or NULL.
1597 * the caller has to free the node tree.
1600 xmlXPtrBuildNodeList(xmlXPathObjectPtr obj
) {
1601 xmlNodePtr list
= NULL
, last
= NULL
;
1606 switch (obj
->type
) {
1607 case XPATH_NODESET
: {
1608 xmlNodeSetPtr set
= obj
->nodesetval
;
1611 for (i
= 0;i
< set
->nodeNr
;i
++) {
1612 if (set
->nodeTab
[i
] == NULL
)
1614 switch (set
->nodeTab
[i
]->type
) {
1616 case XML_CDATA_SECTION_NODE
:
1617 case XML_ELEMENT_NODE
:
1618 case XML_ENTITY_REF_NODE
:
1619 case XML_ENTITY_NODE
:
1621 case XML_COMMENT_NODE
:
1622 case XML_DOCUMENT_NODE
:
1623 case XML_HTML_DOCUMENT_NODE
:
1624 case XML_XINCLUDE_START
:
1625 case XML_XINCLUDE_END
:
1627 case XML_ATTRIBUTE_NODE
:
1628 case XML_NAMESPACE_DECL
:
1629 case XML_DOCUMENT_TYPE_NODE
:
1630 case XML_DOCUMENT_FRAG_NODE
:
1631 case XML_NOTATION_NODE
:
1633 case XML_ELEMENT_DECL
:
1634 case XML_ATTRIBUTE_DECL
:
1635 case XML_ENTITY_DECL
:
1639 list
= last
= xmlCopyNode(set
->nodeTab
[i
], 1);
1641 xmlAddNextSibling(last
, xmlCopyNode(set
->nodeTab
[i
], 1));
1642 if (last
->next
!= NULL
)
1648 case XPATH_LOCATIONSET
: {
1649 xmlLocationSetPtr set
= (xmlLocationSetPtr
) obj
->user
;
1652 for (i
= 0;i
< set
->locNr
;i
++) {
1654 list
= last
= xmlXPtrBuildNodeList(set
->locTab
[i
]);
1656 xmlAddNextSibling(last
,
1657 xmlXPtrBuildNodeList(set
->locTab
[i
]));
1659 while (last
->next
!= NULL
)
1666 return(xmlXPtrBuildRangeNodeList(obj
));
1668 return(xmlCopyNode(obj
->user
, 0));
1675 /************************************************************************
1677 * XPointer functions *
1679 ************************************************************************/
1682 * xmlXPtrNbLocChildren:
1683 * @node: an xmlNodePtr
1685 * Count the number of location children of @node or the length of the
1686 * string value in case of text/PI/Comments nodes
1688 * Returns the number of location children
1691 xmlXPtrNbLocChildren(xmlNodePtr node
) {
1695 switch (node
->type
) {
1696 case XML_HTML_DOCUMENT_NODE
:
1697 case XML_DOCUMENT_NODE
:
1698 case XML_ELEMENT_NODE
:
1699 node
= node
->children
;
1700 while (node
!= NULL
) {
1701 if (node
->type
== XML_ELEMENT_NODE
)
1706 case XML_ATTRIBUTE_NODE
:
1710 case XML_COMMENT_NODE
:
1712 case XML_CDATA_SECTION_NODE
:
1713 case XML_ENTITY_REF_NODE
:
1714 ret
= xmlStrlen(node
->content
);
1723 * xmlXPtrHereFunction:
1724 * @ctxt: the XPointer Parser context
1725 * @nargs: the number of args
1727 * Function implementing here() operation
1728 * as described in 5.4.3
1731 xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1734 if (ctxt
->context
->here
== NULL
)
1735 XP_ERROR(XPTR_SYNTAX_ERROR
);
1737 valuePush(ctxt
, xmlXPtrNewLocationSetNodes(ctxt
->context
->here
, NULL
));
1741 * xmlXPtrOriginFunction:
1742 * @ctxt: the XPointer Parser context
1743 * @nargs: the number of args
1745 * Function implementing origin() operation
1746 * as described in 5.4.3
1749 xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1752 if (ctxt
->context
->origin
== NULL
)
1753 XP_ERROR(XPTR_SYNTAX_ERROR
);
1755 valuePush(ctxt
, xmlXPtrNewLocationSetNodes(ctxt
->context
->origin
, NULL
));
1759 * xmlXPtrStartPointFunction:
1760 * @ctxt: the XPointer Parser context
1761 * @nargs: the number of args
1763 * Function implementing start-point() operation
1764 * as described in 5.4.3
1766 * location-set start-point(location-set)
1768 * For each location x in the argument location-set, start-point adds a
1769 * location of type point to the result location-set. That point represents
1770 * the start point of location x and is determined by the following rules:
1772 * - If x is of type point, the start point is x.
1773 * - If x is of type range, the start point is the start point of x.
1774 * - If x is of type root, element, text, comment, or processing instruction,
1775 * - the container node of the start point is x and the index is 0.
1776 * - If x is of type attribute or namespace, the function must signal a
1782 xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1783 xmlXPathObjectPtr tmp
, obj
, point
;
1784 xmlLocationSetPtr newset
= NULL
;
1785 xmlLocationSetPtr oldset
= NULL
;
1788 if ((ctxt
->value
== NULL
) ||
1789 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
1790 (ctxt
->value
->type
!= XPATH_NODESET
)))
1791 XP_ERROR(XPATH_INVALID_TYPE
)
1793 obj
= valuePop(ctxt
);
1794 if (obj
->type
== XPATH_NODESET
) {
1796 * First convert to a location set
1798 tmp
= xmlXPtrNewLocationSetNodeSet(obj
->nodesetval
);
1799 xmlXPathFreeObject(obj
);
1801 XP_ERROR(XPATH_MEMORY_ERROR
)
1805 newset
= xmlXPtrLocationSetCreate(NULL
);
1806 if (newset
== NULL
) {
1807 xmlXPathFreeObject(obj
);
1808 XP_ERROR(XPATH_MEMORY_ERROR
);
1810 oldset
= (xmlLocationSetPtr
) obj
->user
;
1811 if (oldset
!= NULL
) {
1814 for (i
= 0; i
< oldset
->locNr
; i
++) {
1815 tmp
= oldset
->locTab
[i
];
1819 switch (tmp
->type
) {
1821 point
= xmlXPtrNewPoint(tmp
->user
, tmp
->index
);
1824 xmlNodePtr node
= tmp
->user
;
1826 if ((node
->type
== XML_ATTRIBUTE_NODE
) ||
1827 (node
->type
== XML_NAMESPACE_DECL
)) {
1828 xmlXPathFreeObject(obj
);
1829 xmlXPtrFreeLocationSet(newset
);
1830 XP_ERROR(XPTR_SYNTAX_ERROR
);
1832 point
= xmlXPtrNewPoint(node
, tmp
->index
);
1837 /*** Should we raise an error ?
1838 xmlXPathFreeObject(obj);
1839 xmlXPathFreeObject(newset);
1840 XP_ERROR(XPATH_INVALID_TYPE)
1845 xmlXPtrLocationSetAdd(newset
, point
);
1848 xmlXPathFreeObject(obj
);
1849 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
1853 * xmlXPtrEndPointFunction:
1854 * @ctxt: the XPointer Parser context
1855 * @nargs: the number of args
1857 * Function implementing end-point() operation
1858 * as described in 5.4.3
1859 * ----------------------------
1860 * location-set end-point(location-set)
1862 * For each location x in the argument location-set, end-point adds a
1863 * location of type point to the result location-set. That point represents
1864 * the end point of location x and is determined by the following rules:
1866 * - If x is of type point, the resulting point is x.
1867 * - If x is of type range, the resulting point is the end point of x.
1868 * - If x is of type root or element, the container node of the resulting
1869 * point is x and the index is the number of location children of x.
1870 * - If x is of type text, comment, or processing instruction, the container
1871 * node of the resulting point is x and the index is the length of the
1872 * string-value of x.
1873 * - If x is of type attribute or namespace, the function must signal a
1875 * ----------------------------
1878 xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1879 xmlXPathObjectPtr tmp
, obj
, point
;
1880 xmlLocationSetPtr newset
= NULL
;
1881 xmlLocationSetPtr oldset
= NULL
;
1884 if ((ctxt
->value
== NULL
) ||
1885 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
1886 (ctxt
->value
->type
!= XPATH_NODESET
)))
1887 XP_ERROR(XPATH_INVALID_TYPE
)
1889 obj
= valuePop(ctxt
);
1890 if (obj
->type
== XPATH_NODESET
) {
1892 * First convert to a location set
1894 tmp
= xmlXPtrNewLocationSetNodeSet(obj
->nodesetval
);
1895 xmlXPathFreeObject(obj
);
1897 XP_ERROR(XPATH_MEMORY_ERROR
)
1901 newset
= xmlXPtrLocationSetCreate(NULL
);
1902 if (newset
== NULL
) {
1903 xmlXPathFreeObject(obj
);
1904 XP_ERROR(XPATH_MEMORY_ERROR
);
1906 oldset
= (xmlLocationSetPtr
) obj
->user
;
1907 if (oldset
!= NULL
) {
1910 for (i
= 0; i
< oldset
->locNr
; i
++) {
1911 tmp
= oldset
->locTab
[i
];
1915 switch (tmp
->type
) {
1917 point
= xmlXPtrNewPoint(tmp
->user
, tmp
->index
);
1920 xmlNodePtr node
= tmp
->user2
;
1922 if ((node
->type
== XML_ATTRIBUTE_NODE
) ||
1923 (node
->type
== XML_NAMESPACE_DECL
)) {
1924 xmlXPathFreeObject(obj
);
1925 xmlXPtrFreeLocationSet(newset
);
1926 XP_ERROR(XPTR_SYNTAX_ERROR
);
1928 point
= xmlXPtrNewPoint(node
, tmp
->index2
);
1929 } else if (tmp
->user
== NULL
) {
1930 point
= xmlXPtrNewPoint(node
,
1931 xmlXPtrNbLocChildren(node
));
1936 /*** Should we raise an error ?
1937 xmlXPathFreeObject(obj);
1938 xmlXPathFreeObject(newset);
1939 XP_ERROR(XPATH_INVALID_TYPE)
1944 xmlXPtrLocationSetAdd(newset
, point
);
1947 xmlXPathFreeObject(obj
);
1948 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
1953 * xmlXPtrCoveringRange:
1954 * @ctxt: the XPointer Parser context
1955 * @loc: the location for which the covering range must be computed
1957 * A covering range is a range that wholly encompasses a location
1958 * Section 5.3.3. Covering Ranges for All Location Types
1959 * http://www.w3.org/TR/xptr#N2267
1961 * Returns a new location or NULL in case of error
1963 static xmlXPathObjectPtr
1964 xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt
, xmlXPathObjectPtr loc
) {
1967 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
) ||
1968 (ctxt
->context
->doc
== NULL
))
1970 switch (loc
->type
) {
1972 return(xmlXPtrNewRange(loc
->user
, loc
->index
,
1973 loc
->user
, loc
->index
));
1975 if (loc
->user2
!= NULL
) {
1976 return(xmlXPtrNewRange(loc
->user
, loc
->index
,
1977 loc
->user2
, loc
->index2
));
1979 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
1980 if (node
== (xmlNodePtr
) ctxt
->context
->doc
) {
1981 return(xmlXPtrNewRange(node
, 0, node
,
1982 xmlXPtrGetArity(node
)));
1984 switch (node
->type
) {
1985 case XML_ATTRIBUTE_NODE
:
1986 /* !!! our model is slightly different than XPath */
1987 return(xmlXPtrNewRange(node
, 0, node
,
1988 xmlXPtrGetArity(node
)));
1989 case XML_ELEMENT_NODE
:
1991 case XML_CDATA_SECTION_NODE
:
1992 case XML_ENTITY_REF_NODE
:
1994 case XML_COMMENT_NODE
:
1995 case XML_DOCUMENT_NODE
:
1996 case XML_NOTATION_NODE
:
1997 case XML_HTML_DOCUMENT_NODE
: {
1998 int indx
= xmlXPtrGetIndex(node
);
2000 node
= node
->parent
;
2001 return(xmlXPtrNewRange(node
, indx
- 1,
2010 TODO
/* missed one case ??? */
2016 * xmlXPtrRangeFunction:
2017 * @ctxt: the XPointer Parser context
2018 * @nargs: the number of args
2020 * Function implementing the range() function 5.4.3
2021 * location-set range(location-set )
2023 * The range function returns ranges covering the locations in
2024 * the argument location-set. For each location x in the argument
2025 * location-set, a range location representing the covering range of
2026 * x is added to the result location-set.
2029 xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2031 xmlXPathObjectPtr set
;
2032 xmlLocationSetPtr oldset
;
2033 xmlLocationSetPtr newset
;
2036 if ((ctxt
->value
== NULL
) ||
2037 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
2038 (ctxt
->value
->type
!= XPATH_NODESET
)))
2039 XP_ERROR(XPATH_INVALID_TYPE
)
2041 set
= valuePop(ctxt
);
2042 if (set
->type
== XPATH_NODESET
) {
2043 xmlXPathObjectPtr tmp
;
2046 * First convert to a location set
2048 tmp
= xmlXPtrNewLocationSetNodeSet(set
->nodesetval
);
2049 xmlXPathFreeObject(set
);
2051 XP_ERROR(XPATH_MEMORY_ERROR
)
2054 oldset
= (xmlLocationSetPtr
) set
->user
;
2057 * The loop is to compute the covering range for each item and add it
2059 newset
= xmlXPtrLocationSetCreate(NULL
);
2060 if (newset
== NULL
) {
2061 xmlXPathFreeObject(set
);
2062 XP_ERROR(XPATH_MEMORY_ERROR
);
2064 if (oldset
!= NULL
) {
2065 for (i
= 0;i
< oldset
->locNr
;i
++) {
2066 xmlXPtrLocationSetAdd(newset
,
2067 xmlXPtrCoveringRange(ctxt
, oldset
->locTab
[i
]));
2072 * Save the new value and cleanup
2074 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2075 xmlXPathFreeObject(set
);
2079 * xmlXPtrInsideRange:
2080 * @ctxt: the XPointer Parser context
2081 * @loc: the location for which the inside range must be computed
2083 * A inside range is a range described in the range-inside() description
2085 * Returns a new location or NULL in case of error
2087 static xmlXPathObjectPtr
2088 xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt
, xmlXPathObjectPtr loc
) {
2091 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
) ||
2092 (ctxt
->context
->doc
== NULL
))
2094 switch (loc
->type
) {
2096 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
2097 switch (node
->type
) {
2099 case XML_COMMENT_NODE
:
2101 case XML_CDATA_SECTION_NODE
: {
2102 if (node
->content
== NULL
) {
2103 return(xmlXPtrNewRange(node
, 0, node
, 0));
2105 return(xmlXPtrNewRange(node
, 0, node
,
2106 xmlStrlen(node
->content
)));
2109 case XML_ATTRIBUTE_NODE
:
2110 case XML_ELEMENT_NODE
:
2111 case XML_ENTITY_REF_NODE
:
2112 case XML_DOCUMENT_NODE
:
2113 case XML_NOTATION_NODE
:
2114 case XML_HTML_DOCUMENT_NODE
: {
2115 return(xmlXPtrNewRange(node
, 0, node
,
2116 xmlXPtrGetArity(node
)));
2124 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
2125 if (loc
->user2
!= NULL
) {
2126 return(xmlXPtrNewRange(node
, loc
->index
,
2127 loc
->user2
, loc
->index2
));
2129 switch (node
->type
) {
2131 case XML_COMMENT_NODE
:
2133 case XML_CDATA_SECTION_NODE
: {
2134 if (node
->content
== NULL
) {
2135 return(xmlXPtrNewRange(node
, 0, node
, 0));
2137 return(xmlXPtrNewRange(node
, 0, node
,
2138 xmlStrlen(node
->content
)));
2141 case XML_ATTRIBUTE_NODE
:
2142 case XML_ELEMENT_NODE
:
2143 case XML_ENTITY_REF_NODE
:
2144 case XML_DOCUMENT_NODE
:
2145 case XML_NOTATION_NODE
:
2146 case XML_HTML_DOCUMENT_NODE
: {
2147 return(xmlXPtrNewRange(node
, 0, node
,
2148 xmlXPtrGetArity(node
)));
2157 TODO
/* missed one case ??? */
2163 * xmlXPtrRangeInsideFunction:
2164 * @ctxt: the XPointer Parser context
2165 * @nargs: the number of args
2167 * Function implementing the range-inside() function 5.4.3
2168 * location-set range-inside(location-set )
2170 * The range-inside function returns ranges covering the contents of
2171 * the locations in the argument location-set. For each location x in
2172 * the argument location-set, a range location is added to the result
2173 * location-set. If x is a range location, then x is added to the
2174 * result location-set. If x is not a range location, then x is used
2175 * as the container location of the start and end points of the range
2176 * location to be added; the index of the start point of the range is
2177 * zero; if the end point is a character point then its index is the
2178 * length of the string-value of x, and otherwise is the number of
2179 * location children of x.
2183 xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2185 xmlXPathObjectPtr set
;
2186 xmlLocationSetPtr oldset
;
2187 xmlLocationSetPtr newset
;
2190 if ((ctxt
->value
== NULL
) ||
2191 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
2192 (ctxt
->value
->type
!= XPATH_NODESET
)))
2193 XP_ERROR(XPATH_INVALID_TYPE
)
2195 set
= valuePop(ctxt
);
2196 if (set
->type
== XPATH_NODESET
) {
2197 xmlXPathObjectPtr tmp
;
2200 * First convert to a location set
2202 tmp
= xmlXPtrNewLocationSetNodeSet(set
->nodesetval
);
2203 xmlXPathFreeObject(set
);
2205 XP_ERROR(XPATH_MEMORY_ERROR
)
2210 * The loop is to compute the covering range for each item and add it
2212 newset
= xmlXPtrLocationSetCreate(NULL
);
2213 if (newset
== NULL
) {
2214 xmlXPathFreeObject(set
);
2215 XP_ERROR(XPATH_MEMORY_ERROR
);
2217 oldset
= (xmlLocationSetPtr
) set
->user
;
2218 if (oldset
!= NULL
) {
2219 for (i
= 0;i
< oldset
->locNr
;i
++) {
2220 xmlXPtrLocationSetAdd(newset
,
2221 xmlXPtrInsideRange(ctxt
, oldset
->locTab
[i
]));
2226 * Save the new value and cleanup
2228 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2229 xmlXPathFreeObject(set
);
2233 * xmlXPtrRangeToFunction:
2234 * @ctxt: the XPointer Parser context
2235 * @nargs: the number of args
2237 * Implement the range-to() XPointer function
2239 * Obsolete. range-to is not a real function but a special type of location
2240 * step which is handled in xpath.c.
2243 xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt
,
2244 int nargs ATTRIBUTE_UNUSED
) {
2245 XP_ERROR(XPATH_EXPR_ERROR
);
2249 * xmlXPtrAdvanceNode:
2251 * @level: incremented/decremented to show level in tree
2253 * Advance to the next element or text node in document order
2254 * TODO: add a stack for entering/exiting entities
2256 * Returns -1 in case of failure, 0 otherwise
2259 xmlXPtrAdvanceNode(xmlNodePtr cur
, int *level
) {
2261 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
2263 if (cur
->children
!= NULL
) {
2264 cur
= cur
->children
;
2269 skip
: /* This label should only be needed if something is wrong! */
2270 if (cur
->next
!= NULL
) {
2278 if (cur
== NULL
) return(NULL
);
2279 if (cur
->next
!= NULL
) {
2283 } while (cur
!= NULL
);
2286 if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2287 (cur
->type
!= XML_TEXT_NODE
) &&
2288 (cur
->type
!= XML_DOCUMENT_NODE
) &&
2289 (cur
->type
!= XML_HTML_DOCUMENT_NODE
) &&
2290 (cur
->type
!= XML_CDATA_SECTION_NODE
)) {
2291 if (cur
->type
== XML_ENTITY_REF_NODE
) { /* Shouldn't happen */
2301 * xmlXPtrAdvanceChar:
2304 * @bytes: the number of bytes
2306 * Advance a point of the associated number of bytes (not UTF8 chars)
2308 * Returns -1 in case of failure, 0 otherwise
2311 xmlXPtrAdvanceChar(xmlNodePtr
*node
, int *indx
, int bytes
) {
2316 if ((node
== NULL
) || (indx
== NULL
))
2319 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
2323 while (bytes
>= 0) {
2325 * First position to the beginning of the first text node
2326 * corresponding to this point
2328 while ((cur
!= NULL
) &&
2329 ((cur
->type
== XML_ELEMENT_NODE
) ||
2330 (cur
->type
== XML_DOCUMENT_NODE
) ||
2331 (cur
->type
== XML_HTML_DOCUMENT_NODE
))) {
2333 cur
= xmlXPtrGetNthChild(cur
, pos
);
2336 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2348 * if there is no move needed return the current value.
2350 if (pos
== 0) pos
= 1;
2357 * We should have a text (or cdata) node ...
2360 if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2361 (cur
->content
!= NULL
)) {
2362 len
= xmlStrlen(cur
->content
);
2365 /* Strange, the indx in the text node is greater than it's len */
2369 if (pos
+ bytes
>= len
) {
2370 bytes
-= (len
- pos
);
2371 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2373 } else if (pos
+ bytes
< len
) {
2384 * xmlXPtrMatchString:
2385 * @string: the string to search
2386 * @start: the start textnode
2387 * @startindex: the start index
2388 * @end: the end textnode IN/OUT
2389 * @endindex: the end index IN/OUT
2391 * Check whether the document contains @string at the position
2392 * (@start, @startindex) and limited by the (@end, @endindex) point
2394 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2395 * (@start, @startindex) will indicate the position of the beginning
2396 * of the range and (@end, @endindex) will indicate the end
2400 xmlXPtrMatchString(const xmlChar
*string
, xmlNodePtr start
, int startindex
,
2401 xmlNodePtr
*end
, int *endindex
) {
2403 int pos
; /* 0 based */
2404 int len
; /* in bytes */
2405 int stringlen
; /* in bytes */
2410 if ((start
== NULL
) || (start
->type
== XML_NAMESPACE_DECL
))
2412 if ((end
== NULL
) || (*end
== NULL
) ||
2413 ((*end
)->type
== XML_NAMESPACE_DECL
) || (endindex
== NULL
))
2416 pos
= startindex
- 1;
2417 stringlen
= xmlStrlen(string
);
2419 while (stringlen
> 0) {
2420 if ((cur
== *end
) && (pos
+ stringlen
> *endindex
))
2423 if ((cur
->type
!= XML_ELEMENT_NODE
) && (cur
->content
!= NULL
)) {
2424 len
= xmlStrlen(cur
->content
);
2425 if (len
>= pos
+ stringlen
) {
2426 match
= (!xmlStrncmp(&cur
->content
[pos
], string
, stringlen
));
2429 xmlGenericError(xmlGenericErrorContext
,
2430 "found range %d bytes at index %d of ->",
2431 stringlen
, pos
+ 1);
2432 xmlDebugDumpString(stdout
, cur
->content
);
2433 xmlGenericError(xmlGenericErrorContext
, "\n");
2436 *endindex
= pos
+ stringlen
;
2442 int sub
= len
- pos
;
2443 match
= (!xmlStrncmp(&cur
->content
[pos
], string
, sub
));
2446 xmlGenericError(xmlGenericErrorContext
,
2447 "found subrange %d bytes at index %d of ->",
2449 xmlDebugDumpString(stdout
, cur
->content
);
2450 xmlGenericError(xmlGenericErrorContext
, "\n");
2452 string
= &string
[sub
];
2459 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2468 * xmlXPtrSearchString:
2469 * @string: the string to search
2470 * @start: the start textnode IN/OUT
2471 * @startindex: the start index IN/OUT
2472 * @end: the end textnode
2473 * @endindex: the end index
2475 * Search the next occurrence of @string within the document content
2476 * until the (@end, @endindex) point is reached
2478 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2479 * (@start, @startindex) will indicate the position of the beginning
2480 * of the range and (@end, @endindex) will indicate the end
2484 xmlXPtrSearchString(const xmlChar
*string
, xmlNodePtr
*start
, int *startindex
,
2485 xmlNodePtr
*end
, int *endindex
) {
2488 int pos
; /* 0 based */
2489 int len
; /* in bytes */
2494 if ((start
== NULL
) || (*start
== NULL
) ||
2495 ((*start
)->type
== XML_NAMESPACE_DECL
) || (startindex
== NULL
))
2497 if ((end
== NULL
) || (endindex
== NULL
))
2500 pos
= *startindex
- 1;
2503 while (cur
!= NULL
) {
2504 if ((cur
->type
!= XML_ELEMENT_NODE
) && (cur
->content
!= NULL
)) {
2505 len
= xmlStrlen(cur
->content
);
2506 while (pos
<= len
) {
2508 str
= xmlStrchr(&cur
->content
[pos
], first
);
2510 pos
= (str
- (xmlChar
*)(cur
->content
));
2512 xmlGenericError(xmlGenericErrorContext
,
2513 "found '%c' at index %d of ->",
2515 xmlDebugDumpString(stdout
, cur
->content
);
2516 xmlGenericError(xmlGenericErrorContext
, "\n");
2518 if (xmlXPtrMatchString(string
, cur
, pos
+ 1,
2521 *startindex
= pos
+ 1;
2530 * An empty string is considered to match before each
2531 * character of the string-value and after the final
2535 xmlGenericError(xmlGenericErrorContext
,
2536 "found '' at index %d of ->",
2538 xmlDebugDumpString(stdout
, cur
->content
);
2539 xmlGenericError(xmlGenericErrorContext
, "\n");
2542 *startindex
= pos
+ 1;
2544 *endindex
= pos
+ 1;
2549 if ((cur
== *end
) && (pos
>= *endindex
))
2551 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2560 * xmlXPtrGetLastChar:
2564 * Computes the point coordinates of the last char of this point
2566 * Returns -1 in case of failure, 0 otherwise
2569 xmlXPtrGetLastChar(xmlNodePtr
*node
, int *indx
) {
2573 if ((node
== NULL
) || (*node
== NULL
) ||
2574 ((*node
)->type
== XML_NAMESPACE_DECL
) || (indx
== NULL
))
2579 if ((cur
->type
== XML_ELEMENT_NODE
) ||
2580 (cur
->type
== XML_DOCUMENT_NODE
) ||
2581 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
2583 cur
= xmlXPtrGetNthChild(cur
, pos
);
2586 while (cur
!= NULL
) {
2587 if (cur
->last
!= NULL
)
2589 else if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2590 (cur
->content
!= NULL
)) {
2591 len
= xmlStrlen(cur
->content
);
2605 * xmlXPtrGetStartPoint:
2607 * @node: the resulting node
2608 * @indx: the resulting index
2610 * read the object and return the start point coordinates.
2612 * Returns -1 in case of failure, 0 otherwise
2615 xmlXPtrGetStartPoint(xmlXPathObjectPtr obj
, xmlNodePtr
*node
, int *indx
) {
2616 if ((obj
== NULL
) || (node
== NULL
) || (indx
== NULL
))
2619 switch (obj
->type
) {
2622 if (obj
->index
<= 0)
2629 if (obj
->index
<= 0)
2641 * xmlXPtrGetEndPoint:
2643 * @node: the resulting node
2644 * @indx: the resulting indx
2646 * read the object and return the end point coordinates.
2648 * Returns -1 in case of failure, 0 otherwise
2651 xmlXPtrGetEndPoint(xmlXPathObjectPtr obj
, xmlNodePtr
*node
, int *indx
) {
2652 if ((obj
== NULL
) || (node
== NULL
) || (indx
== NULL
))
2655 switch (obj
->type
) {
2658 if (obj
->index
<= 0)
2665 if (obj
->index
<= 0)
2677 * xmlXPtrStringRangeFunction:
2678 * @ctxt: the XPointer Parser context
2679 * @nargs: the number of args
2681 * Function implementing the string-range() function
2682 * range as described in 5.4.2
2684 * ------------------------------
2685 * [Definition: For each location in the location-set argument,
2686 * string-range returns a set of string ranges, a set of substrings in a
2687 * string. Specifically, the string-value of the location is searched for
2688 * substrings that match the string argument, and the resulting location-set
2689 * will contain a range location for each non-overlapping match.]
2690 * An empty string is considered to match before each character of the
2691 * string-value and after the final character. Whitespace in a string
2692 * is matched literally, with no normalization except that provided by
2693 * XML for line ends. The third argument gives the position of the first
2694 * character to be in the resulting range, relative to the start of the
2695 * match. The default value is 1, which makes the range start immediately
2696 * before the first character of the matched string. The fourth argument
2697 * gives the number of characters in the range; the default is that the
2698 * range extends to the end of the matched string.
2700 * Element boundaries, as well as entire embedded nodes such as processing
2701 * instructions and comments, are ignored as defined in [XPath].
2703 * If the string in the second argument is not found in the string-value
2704 * of the location, or if a value in the third or fourth argument indicates
2705 * a string that is beyond the beginning or end of the document, the
2708 * The points of the range-locations in the returned location-set will
2709 * all be character points.
2710 * ------------------------------
2713 xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2714 int i
, startindex
, endindex
= 0, fendindex
;
2715 xmlNodePtr start
, end
= 0, fend
;
2716 xmlXPathObjectPtr set
= NULL
;
2717 xmlLocationSetPtr oldset
;
2718 xmlLocationSetPtr newset
= NULL
;
2719 xmlXPathObjectPtr string
= NULL
;
2720 xmlXPathObjectPtr position
= NULL
;
2721 xmlXPathObjectPtr number
= NULL
;
2722 int found
, pos
= 0, num
= 0;
2725 * Grab the arguments
2727 if ((nargs
< 2) || (nargs
> 4))
2728 XP_ERROR(XPATH_INVALID_ARITY
);
2731 if ((ctxt
->value
== NULL
) || (ctxt
->value
->type
!= XPATH_NUMBER
)) {
2732 xmlXPathErr(ctxt
, XPATH_INVALID_TYPE
);
2735 number
= valuePop(ctxt
);
2737 num
= (int) number
->floatval
;
2740 if ((ctxt
->value
== NULL
) || (ctxt
->value
->type
!= XPATH_NUMBER
)) {
2741 xmlXPathErr(ctxt
, XPATH_INVALID_TYPE
);
2744 position
= valuePop(ctxt
);
2745 if (position
!= NULL
)
2746 pos
= (int) position
->floatval
;
2748 if ((ctxt
->value
== NULL
) || (ctxt
->value
->type
!= XPATH_STRING
)) {
2749 xmlXPathErr(ctxt
, XPATH_INVALID_TYPE
);
2752 string
= valuePop(ctxt
);
2753 if ((ctxt
->value
== NULL
) ||
2754 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
2755 (ctxt
->value
->type
!= XPATH_NODESET
))) {
2756 xmlXPathErr(ctxt
, XPATH_INVALID_TYPE
);
2759 set
= valuePop(ctxt
);
2760 newset
= xmlXPtrLocationSetCreate(NULL
);
2761 if (newset
== NULL
) {
2762 xmlXPathErr(ctxt
, XPATH_MEMORY_ERROR
);
2765 if (set
->nodesetval
== NULL
) {
2768 if (set
->type
== XPATH_NODESET
) {
2769 xmlXPathObjectPtr tmp
;
2772 * First convert to a location set
2774 tmp
= xmlXPtrNewLocationSetNodeSet(set
->nodesetval
);
2775 xmlXPathFreeObject(set
);
2778 xmlXPathErr(ctxt
, XPATH_MEMORY_ERROR
);
2783 oldset
= (xmlLocationSetPtr
) set
->user
;
2786 * The loop is to search for each element in the location set
2787 * the list of location set corresponding to that search
2789 for (i
= 0;i
< oldset
->locNr
;i
++) {
2791 xmlXPathDebugDumpObject(stdout
, oldset
->locTab
[i
], 0);
2794 xmlXPtrGetStartPoint(oldset
->locTab
[i
], &start
, &startindex
);
2795 xmlXPtrGetEndPoint(oldset
->locTab
[i
], &end
, &endindex
);
2796 xmlXPtrAdvanceChar(&start
, &startindex
, 0);
2797 xmlXPtrGetLastChar(&end
, &endindex
);
2800 xmlGenericError(xmlGenericErrorContext
,
2801 "from index %d of ->", startindex
);
2802 xmlDebugDumpString(stdout
, start
->content
);
2803 xmlGenericError(xmlGenericErrorContext
, "\n");
2804 xmlGenericError(xmlGenericErrorContext
,
2805 "to index %d of ->", endindex
);
2806 xmlDebugDumpString(stdout
, end
->content
);
2807 xmlGenericError(xmlGenericErrorContext
, "\n");
2811 fendindex
= endindex
;
2812 found
= xmlXPtrSearchString(string
->stringval
, &start
, &startindex
,
2815 if (position
== NULL
) {
2816 xmlXPtrLocationSetAdd(newset
,
2817 xmlXPtrNewRange(start
, startindex
, fend
, fendindex
));
2818 } else if (xmlXPtrAdvanceChar(&start
, &startindex
,
2820 if ((number
!= NULL
) && (num
> 0)) {
2824 rindx
= startindex
- 1;
2825 if (xmlXPtrAdvanceChar(&rend
, &rindx
,
2827 xmlXPtrLocationSetAdd(newset
,
2828 xmlXPtrNewRange(start
, startindex
,
2831 } else if ((number
!= NULL
) && (num
<= 0)) {
2832 xmlXPtrLocationSetAdd(newset
,
2833 xmlXPtrNewRange(start
, startindex
,
2834 start
, startindex
));
2836 xmlXPtrLocationSetAdd(newset
,
2837 xmlXPtrNewRange(start
, startindex
,
2842 startindex
= fendindex
;
2843 if (string
->stringval
[0] == 0)
2846 } while (found
== 1);
2850 * Save the new value and cleanup
2854 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2855 xmlXPathFreeObject(set
);
2856 xmlXPathFreeObject(string
);
2857 if (position
) xmlXPathFreeObject(position
);
2858 if (number
) xmlXPathFreeObject(number
);
2862 * xmlXPtrEvalRangePredicate:
2863 * @ctxt: the XPointer Parser context
2865 * [8] Predicate ::= '[' PredicateExpr ']'
2866 * [9] PredicateExpr ::= Expr
2868 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2869 * a Location Set instead of a node set
2872 xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt
) {
2874 xmlXPathObjectPtr res
;
2875 xmlXPathObjectPtr obj
, tmp
;
2876 xmlLocationSetPtr newset
= NULL
;
2877 xmlLocationSetPtr oldset
;
2880 if (ctxt
== NULL
) return;
2884 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);
2890 * Extract the old set, and then evaluate the result of the
2891 * expression for all the element in the set. use it to grow
2894 CHECK_TYPE(XPATH_LOCATIONSET
);
2895 obj
= valuePop(ctxt
);
2897 ctxt
->context
->node
= NULL
;
2899 if ((oldset
== NULL
) || (oldset
->locNr
== 0)) {
2900 ctxt
->context
->contextSize
= 0;
2901 ctxt
->context
->proximityPosition
= 0;
2902 xmlXPathEvalExpr(ctxt
);
2903 res
= valuePop(ctxt
);
2905 xmlXPathFreeObject(res
);
2906 valuePush(ctxt
, obj
);
2910 * Save the expression pointer since we will have to evaluate
2911 * it multiple times. Initialize the new set.
2914 newset
= xmlXPtrLocationSetCreate(NULL
);
2916 for (i
= 0; i
< oldset
->locNr
; i
++) {
2920 * Run the evaluation with a node list made of a single item
2923 ctxt
->context
->node
= oldset
->locTab
[i
]->user
;
2924 tmp
= xmlXPathNewNodeSet(ctxt
->context
->node
);
2925 valuePush(ctxt
, tmp
);
2926 ctxt
->context
->contextSize
= oldset
->locNr
;
2927 ctxt
->context
->proximityPosition
= i
+ 1;
2929 xmlXPathEvalExpr(ctxt
);
2933 * The result of the evaluation need to be tested to
2934 * decided whether the filter succeeded or not
2936 res
= valuePop(ctxt
);
2937 if (xmlXPathEvaluatePredicateResult(ctxt
, res
)) {
2938 xmlXPtrLocationSetAdd(newset
,
2939 xmlXPathObjectCopy(oldset
->locTab
[i
]));
2946 xmlXPathFreeObject(res
);
2947 if (ctxt
->value
== tmp
) {
2948 res
= valuePop(ctxt
);
2949 xmlXPathFreeObject(res
);
2952 ctxt
->context
->node
= NULL
;
2956 * The result is used as the new evaluation set.
2958 xmlXPathFreeObject(obj
);
2959 ctxt
->context
->node
= NULL
;
2960 ctxt
->context
->contextSize
= -1;
2961 ctxt
->context
->proximityPosition
= -1;
2962 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2965 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);
2971 #endif /* LIBXML_XPTR_LOCS_ENABLED */