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.
21 * TODO: better handling of error cases, the full expression should
22 * be parsed beforehand instead of a progressive evaluation
23 * TODO: Access into entities references are not supported now ...
24 * need a start to be able to pop out of entities refs since
25 * parent is the endity declaration, not the ref.
29 #include <libxml/xpointer.h>
30 #include <libxml/xmlmemory.h>
31 #include <libxml/parserInternals.h>
32 #include <libxml/uri.h>
33 #include <libxml/xpath.h>
34 #include <libxml/xpathInternals.h>
35 #include <libxml/xmlerror.h>
36 #include <libxml/globals.h>
38 #ifdef LIBXML_XPTR_ENABLED
40 /* Add support of the xmlns() xpointer scheme to initialize the namespaces */
41 #define XPTR_XMLNS_SCHEME
43 /* #define DEBUG_RANGES */
45 #ifdef LIBXML_DEBUG_ENABLED
46 #include <libxml/debugXML.h>
51 xmlGenericError(xmlGenericErrorContext, \
52 "Unimplemented block at %s:%d\n", \
56 xmlGenericError(xmlGenericErrorContext, \
57 "Internal error at %s:%d\n", \
60 /************************************************************************
62 * Some factorized error routines *
64 ************************************************************************/
68 * @extra: extra informations
70 * Handle a redefinition of attribute error
73 xmlXPtrErrMemory(const char *extra
)
75 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_XPOINTER
,
76 XML_ERR_NO_MEMORY
, XML_ERR_ERROR
, NULL
, 0, extra
,
78 "Memory allocation failed : %s\n", extra
);
83 * @ctxt: an XPTR evaluation context
84 * @extra: extra informations
86 * Handle a redefinition of attribute error
89 xmlXPtrErr(xmlXPathParserContextPtr ctxt
, int error
,
90 const char * msg
, const xmlChar
*extra
)
94 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) {
95 __xmlRaiseError(NULL
, NULL
, NULL
,
96 NULL
, NULL
, XML_FROM_XPOINTER
, error
,
97 XML_ERR_ERROR
, NULL
, 0,
98 (const char *) extra
, NULL
, NULL
, 0, 0,
102 ctxt
->context
->lastError
.domain
= XML_FROM_XPOINTER
;
103 ctxt
->context
->lastError
.code
= error
;
104 ctxt
->context
->lastError
.level
= XML_ERR_ERROR
;
105 ctxt
->context
->lastError
.str1
= (char *) xmlStrdup(ctxt
->base
);
106 ctxt
->context
->lastError
.int1
= ctxt
->cur
- ctxt
->base
;
107 ctxt
->context
->lastError
.node
= ctxt
->context
->debugNode
;
108 if (ctxt
->context
->error
!= NULL
) {
109 ctxt
->context
->error(ctxt
->context
->userData
,
110 &ctxt
->context
->lastError
);
112 __xmlRaiseError(NULL
, NULL
, NULL
,
113 NULL
, ctxt
->context
->debugNode
, XML_FROM_XPOINTER
,
114 error
, XML_ERR_ERROR
, NULL
, 0,
115 (const char *) extra
, (const char *) ctxt
->base
, NULL
,
116 ctxt
->cur
- ctxt
->base
, 0,
121 /************************************************************************
123 * A few helper functions for child sequences *
125 ************************************************************************/
126 /* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
127 xmlNodePtr
xmlXPtrAdvanceNode(xmlNodePtr cur
, int *level
);
132 * Returns the number of child for an element, -1 in case of error
135 xmlXPtrGetArity(xmlNodePtr cur
) {
137 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
140 for (i
= 0;cur
!= NULL
;cur
= cur
->next
) {
141 if ((cur
->type
== XML_ELEMENT_NODE
) ||
142 (cur
->type
== XML_DOCUMENT_NODE
) ||
143 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
154 * Returns the index of the node in its parent children list, -1
158 xmlXPtrGetIndex(xmlNodePtr cur
) {
160 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
162 for (i
= 1;cur
!= NULL
;cur
= cur
->prev
) {
163 if ((cur
->type
== XML_ELEMENT_NODE
) ||
164 (cur
->type
== XML_DOCUMENT_NODE
) ||
165 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
173 * xmlXPtrGetNthChild:
175 * @no: the child number
177 * Returns the @no'th element child of @cur or NULL
180 xmlXPtrGetNthChild(xmlNodePtr cur
, int no
) {
182 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
185 for (i
= 0;i
<= no
;cur
= cur
->next
) {
188 if ((cur
->type
== XML_ELEMENT_NODE
) ||
189 (cur
->type
== XML_DOCUMENT_NODE
) ||
190 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
199 /************************************************************************
201 * Handling of XPointer specific types *
203 ************************************************************************/
207 * @node1: the first node
208 * @index1: the first index
209 * @node2: the second node
210 * @index2: the second index
212 * Compare two points w.r.t document order
214 * Returns -2 in case of error 1 if first point < second point, 0 if
215 * that's the same point, -1 otherwise
218 xmlXPtrCmpPoints(xmlNodePtr node1
, int index1
, xmlNodePtr node2
, int index2
) {
219 if ((node1
== NULL
) || (node2
== NULL
))
222 * a couple of optimizations which will avoid computations in most cases
224 if (node1
== node2
) {
231 return(xmlXPathCmpNodes(node1
, node2
));
236 * @node: the xmlNodePtr
237 * @indx: the indx within the node
239 * Create a new xmlXPathObjectPtr of type point
241 * Returns the newly created object.
243 static xmlXPathObjectPtr
244 xmlXPtrNewPoint(xmlNodePtr node
, int indx
) {
245 xmlXPathObjectPtr ret
;
252 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
254 xmlXPtrErrMemory("allocating point");
257 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
258 ret
->type
= XPATH_POINT
;
259 ret
->user
= (void *) node
;
265 * xmlXPtrRangeCheckOrder:
266 * @range: an object range
268 * Make sure the points in the range are in the right order
271 xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range
) {
276 if (range
->type
!= XPATH_RANGE
)
278 if (range
->user2
== NULL
)
280 tmp
= xmlXPtrCmpPoints(range
->user
, range
->index
,
281 range
->user2
, range
->index2
);
284 range
->user
= range
->user2
;
287 range
->index
= range
->index2
;
293 * xmlXPtrRangesEqual:
294 * @range1: the first range
295 * @range2: the second range
299 * Returns 1 if equal, 0 otherwise
302 xmlXPtrRangesEqual(xmlXPathObjectPtr range1
, xmlXPathObjectPtr range2
) {
303 if (range1
== range2
)
305 if ((range1
== NULL
) || (range2
== NULL
))
307 if (range1
->type
!= range2
->type
)
309 if (range1
->type
!= XPATH_RANGE
)
311 if (range1
->user
!= range2
->user
)
313 if (range1
->index
!= range2
->index
)
315 if (range1
->user2
!= range2
->user2
)
317 if (range1
->index2
!= range2
->index2
)
324 * @start: the starting node
325 * @startindex: the start index
326 * @end: the ending point
327 * @endindex: the ending index
329 * Create a new xmlXPathObjectPtr of type range
331 * Returns the newly created object.
334 xmlXPtrNewRange(xmlNodePtr start
, int startindex
,
335 xmlNodePtr end
, int endindex
) {
336 xmlXPathObjectPtr ret
;
347 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
349 xmlXPtrErrMemory("allocating range");
352 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
353 ret
->type
= XPATH_RANGE
;
355 ret
->index
= startindex
;
357 ret
->index2
= endindex
;
358 xmlXPtrRangeCheckOrder(ret
);
363 * xmlXPtrNewRangePoints:
364 * @start: the starting point
365 * @end: the ending point
367 * Create a new xmlXPathObjectPtr of type range using 2 Points
369 * Returns the newly created object.
372 xmlXPtrNewRangePoints(xmlXPathObjectPtr start
, xmlXPathObjectPtr end
) {
373 xmlXPathObjectPtr ret
;
379 if (start
->type
!= XPATH_POINT
)
381 if (end
->type
!= XPATH_POINT
)
384 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
386 xmlXPtrErrMemory("allocating range");
389 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
390 ret
->type
= XPATH_RANGE
;
391 ret
->user
= start
->user
;
392 ret
->index
= start
->index
;
393 ret
->user2
= end
->user
;
394 ret
->index2
= end
->index
;
395 xmlXPtrRangeCheckOrder(ret
);
400 * xmlXPtrNewRangePointNode:
401 * @start: the starting point
402 * @end: the ending node
404 * Create a new xmlXPathObjectPtr of type range from a point to a node
406 * Returns the newly created object.
409 xmlXPtrNewRangePointNode(xmlXPathObjectPtr start
, xmlNodePtr end
) {
410 xmlXPathObjectPtr ret
;
416 if (start
->type
!= XPATH_POINT
)
419 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
421 xmlXPtrErrMemory("allocating range");
424 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
425 ret
->type
= XPATH_RANGE
;
426 ret
->user
= start
->user
;
427 ret
->index
= start
->index
;
430 xmlXPtrRangeCheckOrder(ret
);
435 * xmlXPtrNewRangeNodePoint:
436 * @start: the starting node
437 * @end: the ending point
439 * Create a new xmlXPathObjectPtr of type range from a node to a point
441 * Returns the newly created object.
444 xmlXPtrNewRangeNodePoint(xmlNodePtr start
, xmlXPathObjectPtr end
) {
445 xmlXPathObjectPtr ret
;
451 if (start
->type
!= XPATH_POINT
)
453 if (end
->type
!= XPATH_POINT
)
456 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
458 xmlXPtrErrMemory("allocating range");
461 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
462 ret
->type
= XPATH_RANGE
;
465 ret
->user2
= end
->user
;
466 ret
->index2
= end
->index
;
467 xmlXPtrRangeCheckOrder(ret
);
472 * xmlXPtrNewRangeNodes:
473 * @start: the starting node
474 * @end: the ending node
476 * Create a new xmlXPathObjectPtr of type range using 2 nodes
478 * Returns the newly created object.
481 xmlXPtrNewRangeNodes(xmlNodePtr start
, xmlNodePtr end
) {
482 xmlXPathObjectPtr ret
;
489 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
491 xmlXPtrErrMemory("allocating range");
494 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
495 ret
->type
= XPATH_RANGE
;
500 xmlXPtrRangeCheckOrder(ret
);
505 * xmlXPtrNewCollapsedRange:
506 * @start: the starting and ending node
508 * Create a new xmlXPathObjectPtr of type range using a single nodes
510 * Returns the newly created object.
513 xmlXPtrNewCollapsedRange(xmlNodePtr start
) {
514 xmlXPathObjectPtr ret
;
519 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
521 xmlXPtrErrMemory("allocating range");
524 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
525 ret
->type
= XPATH_RANGE
;
534 * xmlXPtrNewRangeNodeObject:
535 * @start: the starting node
536 * @end: the ending object
538 * Create a new xmlXPathObjectPtr of type range from a not to an object
540 * Returns the newly created object.
543 xmlXPtrNewRangeNodeObject(xmlNodePtr start
, xmlXPathObjectPtr end
) {
544 xmlXPathObjectPtr ret
;
558 if (end
->nodesetval
->nodeNr
<= 0)
566 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
568 xmlXPtrErrMemory("allocating range");
571 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
572 ret
->type
= XPATH_RANGE
;
577 ret
->user2
= end
->user
;
578 ret
->index2
= end
->index
;
581 ret
->user2
= end
->user2
;
582 ret
->index2
= end
->index2
;
584 case XPATH_NODESET
: {
585 ret
->user2
= end
->nodesetval
->nodeTab
[end
->nodesetval
->nodeNr
- 1];
593 xmlXPtrRangeCheckOrder(ret
);
597 #define XML_RANGESET_DEFAULT 10
600 * xmlXPtrLocationSetCreate:
601 * @val: an initial xmlXPathObjectPtr, or NULL
603 * Create a new xmlLocationSetPtr of type double and of value @val
605 * Returns the newly created object.
608 xmlXPtrLocationSetCreate(xmlXPathObjectPtr val
) {
609 xmlLocationSetPtr ret
;
611 ret
= (xmlLocationSetPtr
) xmlMalloc(sizeof(xmlLocationSet
));
613 xmlXPtrErrMemory("allocating locationset");
616 memset(ret
, 0 , (size_t) sizeof(xmlLocationSet
));
618 ret
->locTab
= (xmlXPathObjectPtr
*) xmlMalloc(XML_RANGESET_DEFAULT
*
619 sizeof(xmlXPathObjectPtr
));
620 if (ret
->locTab
== NULL
) {
621 xmlXPtrErrMemory("allocating locationset");
625 memset(ret
->locTab
, 0 ,
626 XML_RANGESET_DEFAULT
* (size_t) sizeof(xmlXPathObjectPtr
));
627 ret
->locMax
= XML_RANGESET_DEFAULT
;
628 ret
->locTab
[ret
->locNr
++] = val
;
634 * xmlXPtrLocationSetAdd:
635 * @cur: the initial range set
636 * @val: a new xmlXPathObjectPtr
638 * add a new xmlXPathObjectPtr to an existing LocationSet
639 * If the location already exist in the set @val is freed.
642 xmlXPtrLocationSetAdd(xmlLocationSetPtr cur
, xmlXPathObjectPtr val
) {
645 if ((cur
== NULL
) || (val
== NULL
)) return;
648 * check against doublons
650 for (i
= 0;i
< cur
->locNr
;i
++) {
651 if (xmlXPtrRangesEqual(cur
->locTab
[i
], val
)) {
652 xmlXPathFreeObject(val
);
658 * grow the locTab if needed
660 if (cur
->locMax
== 0) {
661 cur
->locTab
= (xmlXPathObjectPtr
*) xmlMalloc(XML_RANGESET_DEFAULT
*
662 sizeof(xmlXPathObjectPtr
));
663 if (cur
->locTab
== NULL
) {
664 xmlXPtrErrMemory("adding location to set");
667 memset(cur
->locTab
, 0 ,
668 XML_RANGESET_DEFAULT
* (size_t) sizeof(xmlXPathObjectPtr
));
669 cur
->locMax
= XML_RANGESET_DEFAULT
;
670 } else if (cur
->locNr
== cur
->locMax
) {
671 xmlXPathObjectPtr
*temp
;
674 temp
= (xmlXPathObjectPtr
*) xmlRealloc(cur
->locTab
, cur
->locMax
*
675 sizeof(xmlXPathObjectPtr
));
677 xmlXPtrErrMemory("adding location to set");
682 cur
->locTab
[cur
->locNr
++] = val
;
686 * xmlXPtrLocationSetMerge:
687 * @val1: the first LocationSet
688 * @val2: the second LocationSet
690 * Merges two rangesets, all ranges from @val2 are added to @val1
692 * Returns val1 once extended or NULL in case of error.
695 xmlXPtrLocationSetMerge(xmlLocationSetPtr val1
, xmlLocationSetPtr val2
) {
698 if (val1
== NULL
) return(NULL
);
699 if (val2
== NULL
) return(val1
);
702 * !!!!! this can be optimized a lot, knowing that both
703 * val1 and val2 already have unicity of their values.
706 for (i
= 0;i
< val2
->locNr
;i
++)
707 xmlXPtrLocationSetAdd(val1
, val2
->locTab
[i
]);
713 * xmlXPtrLocationSetDel:
714 * @cur: the initial range set
715 * @val: an xmlXPathObjectPtr
717 * Removes an xmlXPathObjectPtr from an existing LocationSet
720 xmlXPtrLocationSetDel(xmlLocationSetPtr cur
, xmlXPathObjectPtr val
) {
723 if (cur
== NULL
) return;
724 if (val
== NULL
) return;
727 * check against doublons
729 for (i
= 0;i
< cur
->locNr
;i
++)
730 if (cur
->locTab
[i
] == val
) break;
732 if (i
>= cur
->locNr
) {
734 xmlGenericError(xmlGenericErrorContext
,
735 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
740 for (;i
< cur
->locNr
;i
++)
741 cur
->locTab
[i
] = cur
->locTab
[i
+ 1];
742 cur
->locTab
[cur
->locNr
] = NULL
;
746 * xmlXPtrLocationSetRemove:
747 * @cur: the initial range set
748 * @val: the index to remove
750 * Removes an entry from an existing LocationSet list.
753 xmlXPtrLocationSetRemove(xmlLocationSetPtr cur
, int val
) {
754 if (cur
== NULL
) return;
755 if (val
>= cur
->locNr
) return;
757 for (;val
< cur
->locNr
;val
++)
758 cur
->locTab
[val
] = cur
->locTab
[val
+ 1];
759 cur
->locTab
[cur
->locNr
] = NULL
;
763 * xmlXPtrFreeLocationSet:
764 * @obj: the xmlLocationSetPtr to free
766 * Free the LocationSet compound (not the actual ranges !).
769 xmlXPtrFreeLocationSet(xmlLocationSetPtr obj
) {
772 if (obj
== NULL
) return;
773 if (obj
->locTab
!= NULL
) {
774 for (i
= 0;i
< obj
->locNr
; i
++) {
775 xmlXPathFreeObject(obj
->locTab
[i
]);
777 xmlFree(obj
->locTab
);
783 * xmlXPtrNewLocationSetNodes:
784 * @start: the start NodePtr value
785 * @end: the end NodePtr value or NULL
787 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
788 * it with the single range made of the two nodes @start and @end
790 * Returns the newly created object.
793 xmlXPtrNewLocationSetNodes(xmlNodePtr start
, xmlNodePtr end
) {
794 xmlXPathObjectPtr ret
;
796 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
798 xmlXPtrErrMemory("allocating locationset");
801 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
802 ret
->type
= XPATH_LOCATIONSET
;
804 ret
->user
= xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start
));
806 ret
->user
= xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start
,end
));
811 * xmlXPtrNewLocationSetNodeSet:
814 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
815 * it with all the nodes from @set
817 * Returns the newly created object.
820 xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set
) {
821 xmlXPathObjectPtr ret
;
823 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
825 xmlXPtrErrMemory("allocating locationset");
828 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
829 ret
->type
= XPATH_LOCATIONSET
;
832 xmlLocationSetPtr newset
;
834 newset
= xmlXPtrLocationSetCreate(NULL
);
838 for (i
= 0;i
< set
->nodeNr
;i
++)
839 xmlXPtrLocationSetAdd(newset
,
840 xmlXPtrNewCollapsedRange(set
->nodeTab
[i
]));
842 ret
->user
= (void *) newset
;
848 * xmlXPtrWrapLocationSet:
849 * @val: the LocationSet value
851 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
853 * Returns the newly created object.
856 xmlXPtrWrapLocationSet(xmlLocationSetPtr val
) {
857 xmlXPathObjectPtr ret
;
859 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
861 xmlXPtrErrMemory("allocating locationset");
864 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
865 ret
->type
= XPATH_LOCATIONSET
;
866 ret
->user
= (void *) val
;
870 /************************************************************************
874 ************************************************************************/
876 static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt
, xmlChar
*name
);
879 * Macros for accessing the content. Those should be used only by the parser,
882 * Dirty macros, i.e. one need to make assumption on the context to use them
884 * CUR_PTR return the current pointer to the xmlChar to be parsed.
885 * CUR returns the current xmlChar value, i.e. a 8 bit value
886 * in ISO-Latin or UTF-8.
887 * This should be used internally by the parser
888 * only to compare to ASCII values otherwise it would break when
889 * running with UTF-8 encoding.
890 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
891 * to compare on ASCII based substring.
892 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
893 * strings within the parser.
894 * CURRENT Returns the current char value, with the full decoding of
895 * UTF-8 if we are using this mode. It returns an int.
896 * NEXT Skip to the next character, this does the proper decoding
897 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
898 * It returns the pointer to the current xmlChar.
901 #define CUR (*ctxt->cur)
902 #define SKIP(val) ctxt->cur += (val)
903 #define NXT(val) ctxt->cur[(val)]
904 #define CUR_PTR ctxt->cur
906 #define SKIP_BLANKS \
907 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
909 #define CURRENT (*ctxt->cur)
910 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
914 * @ctxt: the XPointer Parser context
915 * @index: the child number
917 * Move the current node of the nodeset on the stack to the
918 * given child if found
921 xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt
, int indx
) {
922 xmlNodePtr cur
= NULL
;
923 xmlXPathObjectPtr obj
;
924 xmlNodeSetPtr oldset
;
926 CHECK_TYPE(XPATH_NODESET
);
927 obj
= valuePop(ctxt
);
928 oldset
= obj
->nodesetval
;
929 if ((indx
<= 0) || (oldset
== NULL
) || (oldset
->nodeNr
!= 1)) {
930 xmlXPathFreeObject(obj
);
931 valuePush(ctxt
, xmlXPathNewNodeSet(NULL
));
934 cur
= xmlXPtrGetNthChild(oldset
->nodeTab
[0], indx
);
936 xmlXPathFreeObject(obj
);
937 valuePush(ctxt
, xmlXPathNewNodeSet(NULL
));
940 oldset
->nodeTab
[0] = cur
;
941 valuePush(ctxt
, obj
);
945 * xmlXPtrEvalXPtrPart:
946 * @ctxt: the XPointer Parser context
947 * @name: the preparsed Scheme for the XPtrPart
949 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
950 * | Scheme '(' SchemeSpecificExpr ')'
952 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
954 * SchemeSpecificExpr ::= StringWithBalancedParens
956 * StringWithBalancedParens ::=
957 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
958 * [VC: Parenthesis escaping]
960 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
962 * VC: Parenthesis escaping:
963 * The end of an XPointer part is signaled by the right parenthesis ")"
964 * character that is balanced with the left parenthesis "(" character
965 * that began the part. Any unbalanced parenthesis character inside the
966 * expression, even within literals, must be escaped with a circumflex (^)
967 * character preceding it. If the expression contains any literal
968 * occurrences of the circumflex, each must be escaped with an additional
969 * circumflex (that is, ^^). If the unescaped parentheses in the expression
970 * are not balanced, a syntax error results.
972 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
973 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
975 * TODO: there is no new scheme registration mechanism
979 xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
980 xmlChar
*buffer
, *cur
;
985 name
= xmlXPathParseName(ctxt
);
987 XP_ERROR(XPATH_EXPR_ERROR
);
990 XP_ERROR(XPATH_EXPR_ERROR
);
994 len
= xmlStrlen(ctxt
->cur
);
996 buffer
= (xmlChar
*) xmlMallocAtomic(len
* sizeof (xmlChar
));
997 if (buffer
== NULL
) {
998 xmlXPtrErrMemory("allocating buffer");
1010 } else if (CUR
== '(') {
1012 } else if (CUR
== '^') {
1013 if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
1022 if ((level
!= 0) && (CUR
== 0)) {
1024 XP_ERROR(XPTR_SYNTAX_ERROR
);
1027 if (xmlStrEqual(name
, (xmlChar
*) "xpointer")) {
1028 const xmlChar
*left
= CUR_PTR
;
1032 * To evaluate an xpointer scheme element (4.3) we need:
1033 * context initialized to the root
1034 * context position initalized to 1
1035 * context size initialized to 1
1037 ctxt
->context
->node
= (xmlNodePtr
)ctxt
->context
->doc
;
1038 ctxt
->context
->proximityPosition
= 1;
1039 ctxt
->context
->contextSize
= 1;
1040 xmlXPathEvalExpr(ctxt
);
1042 } else if (xmlStrEqual(name
, (xmlChar
*) "element")) {
1043 const xmlChar
*left
= CUR_PTR
;
1047 if (buffer
[0] == '/') {
1049 xmlXPtrEvalChildSeq(ctxt
, NULL
);
1051 name2
= xmlXPathParseName(ctxt
);
1052 if (name2
== NULL
) {
1055 XP_ERROR(XPATH_EXPR_ERROR
);
1057 xmlXPtrEvalChildSeq(ctxt
, name2
);
1060 #ifdef XPTR_XMLNS_SCHEME
1061 } else if (xmlStrEqual(name
, (xmlChar
*) "xmlns")) {
1062 const xmlChar
*left
= CUR_PTR
;
1068 prefix
= xmlXPathParseNCName(ctxt
);
1069 if (prefix
== NULL
) {
1072 XP_ERROR(XPTR_SYNTAX_ERROR
);
1079 XP_ERROR(XPTR_SYNTAX_ERROR
);
1083 /* @@ check escaping in the XPointer WD */
1085 value
= xmlParseURI((const char *)ctxt
->cur
);
1086 if (value
== NULL
) {
1090 XP_ERROR(XPTR_SYNTAX_ERROR
);
1092 URI
= xmlSaveUri(value
);
1098 XP_ERROR(XPATH_MEMORY_ERROR
);
1101 xmlXPathRegisterNs(ctxt
->context
, prefix
, URI
);
1105 #endif /* XPTR_XMLNS_SCHEME */
1107 xmlXPtrErr(ctxt
, XML_XPTR_UNKNOWN_SCHEME
,
1108 "unsupported scheme '%s'\n", name
);
1115 * xmlXPtrEvalFullXPtr:
1116 * @ctxt: the XPointer Parser context
1117 * @name: the preparsed Scheme for the first XPtrPart
1119 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1121 * As the specs says:
1123 * When multiple XPtrParts are provided, they must be evaluated in
1124 * left-to-right order. If evaluation of one part fails, the nexti
1125 * is evaluated. The following conditions cause XPointer part failure:
1127 * - An unknown scheme
1128 * - A scheme that does not locate any sub-resource present in the resource
1129 * - A scheme that is not applicable to the media type of the resource
1131 * The XPointer application must consume a failed XPointer part and
1132 * attempt to evaluate the next one, if any. The result of the first
1133 * XPointer part whose evaluation succeeds is taken to be the fragment
1134 * located by the XPointer as a whole. If all the parts fail, the result
1135 * for the XPointer as a whole is a sub-resource error.
1138 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1139 * expressions or other schemes.
1142 xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
1144 name
= xmlXPathParseName(ctxt
);
1146 XP_ERROR(XPATH_EXPR_ERROR
);
1147 while (name
!= NULL
) {
1148 ctxt
->error
= XPATH_EXPRESSION_OK
;
1149 xmlXPtrEvalXPtrPart(ctxt
, name
);
1151 /* in case of syntax error, break here */
1152 if ((ctxt
->error
!= XPATH_EXPRESSION_OK
) &&
1153 (ctxt
->error
!= XML_XPTR_UNKNOWN_SCHEME
))
1157 * If the returned value is a non-empty nodeset
1158 * or location set, return here.
1160 if (ctxt
->value
!= NULL
) {
1161 xmlXPathObjectPtr obj
= ctxt
->value
;
1163 switch (obj
->type
) {
1164 case XPATH_LOCATIONSET
: {
1165 xmlLocationSetPtr loc
= ctxt
->value
->user
;
1166 if ((loc
!= NULL
) && (loc
->locNr
> 0))
1170 case XPATH_NODESET
: {
1171 xmlNodeSetPtr loc
= ctxt
->value
->nodesetval
;
1172 if ((loc
!= NULL
) && (loc
->nodeNr
> 0))
1181 * Evaluating to improper values is equivalent to
1182 * a sub-resource error, clean-up the stack
1185 obj
= valuePop(ctxt
);
1187 xmlXPathFreeObject(obj
);
1189 } while (obj
!= NULL
);
1193 * Is there another XPointer part.
1196 name
= xmlXPathParseName(ctxt
);
1201 * xmlXPtrEvalChildSeq:
1202 * @ctxt: the XPointer Parser context
1203 * @name: a possible ID name of the child sequence
1205 * ChildSeq ::= '/1' ('/' [0-9]*)*
1206 * | Name ('/' [0-9]*)+
1208 * Parse and evaluate a Child Sequence. This routine also handle the
1209 * case of a Bare Name used to get a document ID.
1212 xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt
, xmlChar
*name
) {
1214 * XPointer don't allow by syntax to address in mutirooted trees
1215 * this might prove useful in some cases, warn about it.
1217 if ((name
== NULL
) && (CUR
== '/') && (NXT(1) != '1')) {
1218 xmlXPtrErr(ctxt
, XML_XPTR_CHILDSEQ_START
,
1219 "warning: ChildSeq not starting by /1\n", NULL
);
1223 valuePush(ctxt
, xmlXPathNewString(name
));
1225 xmlXPathIdFunction(ctxt
, 1);
1229 while (CUR
== '/') {
1233 while ((CUR
>= '0') && (CUR
<= '9')) {
1234 child
= child
* 10 + (CUR
- '0');
1237 xmlXPtrGetChildNo(ctxt
, child
);
1243 * xmlXPtrEvalXPointer:
1244 * @ctxt: the XPointer Parser context
1250 * Parse and evaluate an XPointer
1253 xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt
) {
1254 if (ctxt
->valueTab
== NULL
) {
1255 /* Allocate the value stack */
1256 ctxt
->valueTab
= (xmlXPathObjectPtr
*)
1257 xmlMalloc(10 * sizeof(xmlXPathObjectPtr
));
1258 if (ctxt
->valueTab
== NULL
) {
1259 xmlXPtrErrMemory("allocating evaluation context");
1263 ctxt
->valueMax
= 10;
1265 ctxt
->valueFrame
= 0;
1270 xmlXPtrEvalChildSeq(ctxt
, NULL
);
1274 name
= xmlXPathParseName(ctxt
);
1276 XP_ERROR(XPATH_EXPR_ERROR
);
1278 xmlXPtrEvalFullXPtr(ctxt
, name
);
1279 /* Short evaluation */
1282 /* this handle both Bare Names and Child Sequences */
1283 xmlXPtrEvalChildSeq(ctxt
, name
);
1288 XP_ERROR(XPATH_EXPR_ERROR
);
1292 /************************************************************************
1294 * General routines *
1296 ************************************************************************/
1299 void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1301 void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1303 void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1305 void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1307 void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1309 void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1311 void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
);
1314 * xmlXPtrNewContext:
1315 * @doc: the XML document
1316 * @here: the node that directly contains the XPointer being evaluated or NULL
1317 * @origin: the element from which a user or program initiated traversal of
1318 * the link, or NULL.
1320 * Create a new XPointer context
1322 * Returns the xmlXPathContext just allocated.
1325 xmlXPtrNewContext(xmlDocPtr doc
, xmlNodePtr here
, xmlNodePtr origin
) {
1326 xmlXPathContextPtr ret
;
1328 ret
= xmlXPathNewContext(doc
);
1333 ret
->origin
= origin
;
1335 xmlXPathRegisterFunc(ret
, (xmlChar
*)"range-to",
1336 xmlXPtrRangeToFunction
);
1337 xmlXPathRegisterFunc(ret
, (xmlChar
*)"range",
1338 xmlXPtrRangeFunction
);
1339 xmlXPathRegisterFunc(ret
, (xmlChar
*)"range-inside",
1340 xmlXPtrRangeInsideFunction
);
1341 xmlXPathRegisterFunc(ret
, (xmlChar
*)"string-range",
1342 xmlXPtrStringRangeFunction
);
1343 xmlXPathRegisterFunc(ret
, (xmlChar
*)"start-point",
1344 xmlXPtrStartPointFunction
);
1345 xmlXPathRegisterFunc(ret
, (xmlChar
*)"end-point",
1346 xmlXPtrEndPointFunction
);
1347 xmlXPathRegisterFunc(ret
, (xmlChar
*)"here",
1348 xmlXPtrHereFunction
);
1349 xmlXPathRegisterFunc(ret
, (xmlChar
*)" origin",
1350 xmlXPtrOriginFunction
);
1357 * @str: the XPointer expression
1358 * @ctx: the XPointer context
1360 * Evaluate the XPath Location Path in the given context.
1362 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
1363 * the caller has to free the object.
1366 xmlXPtrEval(const xmlChar
*str
, xmlXPathContextPtr ctx
) {
1367 xmlXPathParserContextPtr ctxt
;
1368 xmlXPathObjectPtr res
= NULL
, tmp
;
1369 xmlXPathObjectPtr init
= NULL
;
1374 if ((ctx
== NULL
) || (str
== NULL
))
1377 ctxt
= xmlXPathNewParserContext(str
, ctx
);
1379 xmlXPtrEvalXPointer(ctxt
);
1381 if ((ctxt
->value
!= NULL
) &&
1382 (ctxt
->value
->type
!= XPATH_NODESET
) &&
1383 (ctxt
->value
->type
!= XPATH_LOCATIONSET
)) {
1384 xmlXPtrErr(ctxt
, XML_XPTR_EVAL_FAILED
,
1385 "xmlXPtrEval: evaluation failed to return a node set\n",
1388 res
= valuePop(ctxt
);
1392 tmp
= valuePop(ctxt
);
1395 if (tmp
->type
== XPATH_NODESET
) {
1397 * Evaluation may push a root nodeset which is unused
1400 set
= tmp
->nodesetval
;
1401 if ((set
->nodeNr
!= 1) ||
1402 (set
->nodeTab
[0] != (xmlNodePtr
) ctx
->doc
))
1407 xmlXPathFreeObject(tmp
);
1409 } while (tmp
!= NULL
);
1411 xmlXPtrErr(ctxt
, XML_XPTR_EXTRA_OBJECTS
,
1412 "xmlXPtrEval: object(s) left on the eval stack\n",
1415 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
1416 xmlXPathFreeObject(res
);
1420 xmlXPathFreeParserContext(ctxt
);
1425 * xmlXPtrBuildRangeNodeList:
1426 * @range: a range object
1428 * Build a node list tree copy of the range
1430 * Returns an xmlNodePtr list or NULL.
1431 * the caller has to free the node tree.
1434 xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range
) {
1435 /* pointers to generated nodes */
1436 xmlNodePtr list
= NULL
, last
= NULL
, parent
= NULL
, tmp
;
1437 /* pointers to traversal nodes */
1438 xmlNodePtr start
, cur
, end
;
1443 if (range
->type
!= XPATH_RANGE
)
1445 start
= (xmlNodePtr
) range
->user
;
1447 if ((start
== NULL
) || (start
->type
== XML_NAMESPACE_DECL
))
1451 return(xmlCopyNode(start
, 1));
1452 if (end
->type
== XML_NAMESPACE_DECL
)
1456 index1
= range
->index
;
1457 index2
= range
->index2
;
1458 while (cur
!= NULL
) {
1460 if (cur
->type
== XML_TEXT_NODE
) {
1461 const xmlChar
*content
= cur
->content
;
1464 if (content
== NULL
) {
1465 tmp
= xmlNewTextLen(NULL
, 0);
1468 if ((cur
== start
) && (index1
> 1)) {
1469 content
+= (index1
- 1);
1470 len
-= (index1
- 1);
1475 tmp
= xmlNewTextLen(content
, len
);
1477 /* single sub text node selection */
1480 /* prune and return full set */
1482 xmlAddNextSibling(last
, tmp
);
1484 xmlAddChild(parent
, tmp
);
1487 tmp
= xmlCopyNode(cur
, 0);
1492 xmlAddNextSibling(last
, tmp
);
1494 xmlAddChild(parent
, tmp
);
1500 end
= xmlXPtrGetNthChild(cur
, index2
- 1);
1503 if ((cur
== start
) && (index1
> 1)) {
1504 cur
= xmlXPtrGetNthChild(cur
, index1
- 1);
1507 cur
= cur
->children
;
1510 * Now gather the remaining nodes from cur to end
1512 continue; /* while */
1514 } else if ((cur
== start
) &&
1515 (list
== NULL
) /* looks superfluous but ... */ ) {
1516 if ((cur
->type
== XML_TEXT_NODE
) ||
1517 (cur
->type
== XML_CDATA_SECTION_NODE
)) {
1518 const xmlChar
*content
= cur
->content
;
1520 if (content
== NULL
) {
1521 tmp
= xmlNewTextLen(NULL
, 0);
1524 content
+= (index1
- 1);
1526 tmp
= xmlNewText(content
);
1530 if ((cur
== start
) && (index1
> 1)) {
1531 tmp
= xmlCopyNode(cur
, 0);
1535 cur
= xmlXPtrGetNthChild(cur
, index1
- 1);
1538 * Now gather the remaining nodes from cur to end
1540 continue; /* while */
1542 tmp
= xmlCopyNode(cur
, 1);
1549 switch (cur
->type
) {
1551 case XML_ELEMENT_DECL
:
1552 case XML_ATTRIBUTE_DECL
:
1553 case XML_ENTITY_NODE
:
1554 /* Do not copy DTD informations */
1556 case XML_ENTITY_DECL
:
1557 TODO
/* handle crossing entities -> stack needed */
1559 case XML_XINCLUDE_START
:
1560 case XML_XINCLUDE_END
:
1561 /* don't consider it part of the tree content */
1563 case XML_ATTRIBUTE_NODE
:
1564 /* Humm, should not happen ! */
1568 tmp
= xmlCopyNode(cur
, 1);
1572 if ((list
== NULL
) || ((last
== NULL
) && (parent
== NULL
))) {
1577 xmlAddNextSibling(last
, tmp
);
1579 xmlAddChild(parent
, tmp
);
1585 * Skip to next node in document order
1587 if ((list
== NULL
) || ((last
== NULL
) && (parent
== NULL
))) {
1591 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
1597 * xmlXPtrBuildNodeList:
1598 * @obj: the XPointer result from the evaluation.
1600 * Build a node list tree copy of the XPointer result.
1601 * This will drop Attributes and Namespace declarations.
1603 * Returns an xmlNodePtr list or NULL.
1604 * the caller has to free the node tree.
1607 xmlXPtrBuildNodeList(xmlXPathObjectPtr obj
) {
1608 xmlNodePtr list
= NULL
, last
= NULL
;
1613 switch (obj
->type
) {
1614 case XPATH_NODESET
: {
1615 xmlNodeSetPtr set
= obj
->nodesetval
;
1618 for (i
= 0;i
< set
->nodeNr
;i
++) {
1619 if (set
->nodeTab
[i
] == NULL
)
1621 switch (set
->nodeTab
[i
]->type
) {
1623 case XML_CDATA_SECTION_NODE
:
1624 case XML_ELEMENT_NODE
:
1625 case XML_ENTITY_REF_NODE
:
1626 case XML_ENTITY_NODE
:
1628 case XML_COMMENT_NODE
:
1629 case XML_DOCUMENT_NODE
:
1630 case XML_HTML_DOCUMENT_NODE
:
1631 #ifdef LIBXML_DOCB_ENABLED
1632 case XML_DOCB_DOCUMENT_NODE
:
1634 case XML_XINCLUDE_START
:
1635 case XML_XINCLUDE_END
:
1637 case XML_ATTRIBUTE_NODE
:
1638 case XML_NAMESPACE_DECL
:
1639 case XML_DOCUMENT_TYPE_NODE
:
1640 case XML_DOCUMENT_FRAG_NODE
:
1641 case XML_NOTATION_NODE
:
1643 case XML_ELEMENT_DECL
:
1644 case XML_ATTRIBUTE_DECL
:
1645 case XML_ENTITY_DECL
:
1649 list
= last
= xmlCopyNode(set
->nodeTab
[i
], 1);
1651 xmlAddNextSibling(last
, xmlCopyNode(set
->nodeTab
[i
], 1));
1652 if (last
->next
!= NULL
)
1658 case XPATH_LOCATIONSET
: {
1659 xmlLocationSetPtr set
= (xmlLocationSetPtr
) obj
->user
;
1662 for (i
= 0;i
< set
->locNr
;i
++) {
1664 list
= last
= xmlXPtrBuildNodeList(set
->locTab
[i
]);
1666 xmlAddNextSibling(last
,
1667 xmlXPtrBuildNodeList(set
->locTab
[i
]));
1669 while (last
->next
!= NULL
)
1676 return(xmlXPtrBuildRangeNodeList(obj
));
1678 return(xmlCopyNode(obj
->user
, 0));
1685 /************************************************************************
1687 * XPointer functions *
1689 ************************************************************************/
1692 * xmlXPtrNbLocChildren:
1693 * @node: an xmlNodePtr
1695 * Count the number of location children of @node or the length of the
1696 * string value in case of text/PI/Comments nodes
1698 * Returns the number of location children
1701 xmlXPtrNbLocChildren(xmlNodePtr node
) {
1705 switch (node
->type
) {
1706 case XML_HTML_DOCUMENT_NODE
:
1707 case XML_DOCUMENT_NODE
:
1708 case XML_ELEMENT_NODE
:
1709 node
= node
->children
;
1710 while (node
!= NULL
) {
1711 if (node
->type
== XML_ELEMENT_NODE
)
1716 case XML_ATTRIBUTE_NODE
:
1720 case XML_COMMENT_NODE
:
1722 case XML_CDATA_SECTION_NODE
:
1723 case XML_ENTITY_REF_NODE
:
1724 ret
= xmlStrlen(node
->content
);
1733 * xmlXPtrHereFunction:
1734 * @ctxt: the XPointer Parser context
1735 * @nargs: the number of args
1737 * Function implementing here() operation
1738 * as described in 5.4.3
1741 xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1744 if (ctxt
->context
->here
== NULL
)
1745 XP_ERROR(XPTR_SYNTAX_ERROR
);
1747 valuePush(ctxt
, xmlXPtrNewLocationSetNodes(ctxt
->context
->here
, NULL
));
1751 * xmlXPtrOriginFunction:
1752 * @ctxt: the XPointer Parser context
1753 * @nargs: the number of args
1755 * Function implementing origin() operation
1756 * as described in 5.4.3
1759 xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1762 if (ctxt
->context
->origin
== NULL
)
1763 XP_ERROR(XPTR_SYNTAX_ERROR
);
1765 valuePush(ctxt
, xmlXPtrNewLocationSetNodes(ctxt
->context
->origin
, NULL
));
1769 * xmlXPtrStartPointFunction:
1770 * @ctxt: the XPointer Parser context
1771 * @nargs: the number of args
1773 * Function implementing start-point() operation
1774 * as described in 5.4.3
1776 * location-set start-point(location-set)
1778 * For each location x in the argument location-set, start-point adds a
1779 * location of type point to the result location-set. That point represents
1780 * the start point of location x and is determined by the following rules:
1782 * - If x is of type point, the start point is x.
1783 * - If x is of type range, the start point is the start point of x.
1784 * - If x is of type root, element, text, comment, or processing instruction,
1785 * - the container node of the start point is x and the index is 0.
1786 * - If x is of type attribute or namespace, the function must signal a
1792 xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1793 xmlXPathObjectPtr tmp
, obj
, point
;
1794 xmlLocationSetPtr newset
= NULL
;
1795 xmlLocationSetPtr oldset
= NULL
;
1798 if ((ctxt
->value
== NULL
) ||
1799 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
1800 (ctxt
->value
->type
!= XPATH_NODESET
)))
1801 XP_ERROR(XPATH_INVALID_TYPE
)
1803 obj
= valuePop(ctxt
);
1804 if (obj
->type
== XPATH_NODESET
) {
1806 * First convert to a location set
1808 tmp
= xmlXPtrNewLocationSetNodeSet(obj
->nodesetval
);
1809 xmlXPathFreeObject(obj
);
1813 newset
= xmlXPtrLocationSetCreate(NULL
);
1814 if (newset
== NULL
) {
1815 xmlXPathFreeObject(obj
);
1816 XP_ERROR(XPATH_MEMORY_ERROR
);
1818 oldset
= (xmlLocationSetPtr
) obj
->user
;
1819 if (oldset
!= NULL
) {
1822 for (i
= 0; i
< oldset
->locNr
; i
++) {
1823 tmp
= oldset
->locTab
[i
];
1827 switch (tmp
->type
) {
1829 point
= xmlXPtrNewPoint(tmp
->user
, tmp
->index
);
1832 xmlNodePtr node
= tmp
->user
;
1834 if (node
->type
== XML_ATTRIBUTE_NODE
) {
1835 /* TODO: Namespace Nodes ??? */
1836 xmlXPathFreeObject(obj
);
1837 xmlXPtrFreeLocationSet(newset
);
1838 XP_ERROR(XPTR_SYNTAX_ERROR
);
1840 point
= xmlXPtrNewPoint(node
, tmp
->index
);
1845 /*** Should we raise an error ?
1846 xmlXPathFreeObject(obj);
1847 xmlXPathFreeObject(newset);
1848 XP_ERROR(XPATH_INVALID_TYPE)
1853 xmlXPtrLocationSetAdd(newset
, point
);
1856 xmlXPathFreeObject(obj
);
1857 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
1861 * xmlXPtrEndPointFunction:
1862 * @ctxt: the XPointer Parser context
1863 * @nargs: the number of args
1865 * Function implementing end-point() operation
1866 * as described in 5.4.3
1867 * ----------------------------
1868 * location-set end-point(location-set)
1870 * For each location x in the argument location-set, end-point adds a
1871 * location of type point to the result location-set. That point represents
1872 * the end point of location x and is determined by the following rules:
1874 * - If x is of type point, the resulting point is x.
1875 * - If x is of type range, the resulting point is the end point of x.
1876 * - If x is of type root or element, the container node of the resulting
1877 * point is x and the index is the number of location children of x.
1878 * - If x is of type text, comment, or processing instruction, the container
1879 * node of the resulting point is x and the index is the length of the
1880 * string-value of x.
1881 * - If x is of type attribute or namespace, the function must signal a
1883 * ----------------------------
1886 xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
1887 xmlXPathObjectPtr tmp
, obj
, point
;
1888 xmlLocationSetPtr newset
= NULL
;
1889 xmlLocationSetPtr oldset
= NULL
;
1892 if ((ctxt
->value
== NULL
) ||
1893 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
1894 (ctxt
->value
->type
!= XPATH_NODESET
)))
1895 XP_ERROR(XPATH_INVALID_TYPE
)
1897 obj
= valuePop(ctxt
);
1898 if (obj
->type
== XPATH_NODESET
) {
1900 * First convert to a location set
1902 tmp
= xmlXPtrNewLocationSetNodeSet(obj
->nodesetval
);
1903 xmlXPathFreeObject(obj
);
1907 newset
= xmlXPtrLocationSetCreate(NULL
);
1908 oldset
= (xmlLocationSetPtr
) obj
->user
;
1909 if (oldset
!= NULL
) {
1912 for (i
= 0; i
< oldset
->locNr
; i
++) {
1913 tmp
= oldset
->locTab
[i
];
1917 switch (tmp
->type
) {
1919 point
= xmlXPtrNewPoint(tmp
->user
, tmp
->index
);
1922 xmlNodePtr node
= tmp
->user2
;
1924 if (node
->type
== XML_ATTRIBUTE_NODE
) {
1925 /* TODO: Namespace Nodes ??? */
1926 xmlXPathFreeObject(obj
);
1927 xmlXPtrFreeLocationSet(newset
);
1928 XP_ERROR(XPTR_SYNTAX_ERROR
);
1930 point
= xmlXPtrNewPoint(node
, tmp
->index2
);
1931 } else if (tmp
->user
== NULL
) {
1932 point
= xmlXPtrNewPoint(node
,
1933 xmlXPtrNbLocChildren(node
));
1938 /*** Should we raise an error ?
1939 xmlXPathFreeObject(obj);
1940 xmlXPathFreeObject(newset);
1941 XP_ERROR(XPATH_INVALID_TYPE)
1946 xmlXPtrLocationSetAdd(newset
, point
);
1949 xmlXPathFreeObject(obj
);
1950 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
1955 * xmlXPtrCoveringRange:
1956 * @ctxt: the XPointer Parser context
1957 * @loc: the location for which the covering range must be computed
1959 * A covering range is a range that wholly encompasses a location
1960 * Section 5.3.3. Covering Ranges for All Location Types
1961 * http://www.w3.org/TR/xptr#N2267
1963 * Returns a new location or NULL in case of error
1965 static xmlXPathObjectPtr
1966 xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt
, xmlXPathObjectPtr loc
) {
1969 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
) ||
1970 (ctxt
->context
->doc
== NULL
))
1972 switch (loc
->type
) {
1974 return(xmlXPtrNewRange(loc
->user
, loc
->index
,
1975 loc
->user
, loc
->index
));
1977 if (loc
->user2
!= NULL
) {
1978 return(xmlXPtrNewRange(loc
->user
, loc
->index
,
1979 loc
->user2
, loc
->index2
));
1981 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
1982 if (node
== (xmlNodePtr
) ctxt
->context
->doc
) {
1983 return(xmlXPtrNewRange(node
, 0, node
,
1984 xmlXPtrGetArity(node
)));
1986 switch (node
->type
) {
1987 case XML_ATTRIBUTE_NODE
:
1988 /* !!! our model is slightly different than XPath */
1989 return(xmlXPtrNewRange(node
, 0, node
,
1990 xmlXPtrGetArity(node
)));
1991 case XML_ELEMENT_NODE
:
1993 case XML_CDATA_SECTION_NODE
:
1994 case XML_ENTITY_REF_NODE
:
1996 case XML_COMMENT_NODE
:
1997 case XML_DOCUMENT_NODE
:
1998 case XML_NOTATION_NODE
:
1999 case XML_HTML_DOCUMENT_NODE
: {
2000 int indx
= xmlXPtrGetIndex(node
);
2002 node
= node
->parent
;
2003 return(xmlXPtrNewRange(node
, indx
- 1,
2012 TODO
/* missed one case ??? */
2018 * xmlXPtrRangeFunction:
2019 * @ctxt: the XPointer Parser context
2020 * @nargs: the number of args
2022 * Function implementing the range() function 5.4.3
2023 * location-set range(location-set )
2025 * The range function returns ranges covering the locations in
2026 * the argument location-set. For each location x in the argument
2027 * location-set, a range location representing the covering range of
2028 * x is added to the result location-set.
2031 xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2033 xmlXPathObjectPtr set
;
2034 xmlLocationSetPtr oldset
;
2035 xmlLocationSetPtr newset
;
2038 if ((ctxt
->value
== NULL
) ||
2039 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
2040 (ctxt
->value
->type
!= XPATH_NODESET
)))
2041 XP_ERROR(XPATH_INVALID_TYPE
)
2043 set
= valuePop(ctxt
);
2044 if (set
->type
== XPATH_NODESET
) {
2045 xmlXPathObjectPtr tmp
;
2048 * First convert to a location set
2050 tmp
= xmlXPtrNewLocationSetNodeSet(set
->nodesetval
);
2051 xmlXPathFreeObject(set
);
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 for (i
= 0;i
< oldset
->locNr
;i
++) {
2061 xmlXPtrLocationSetAdd(newset
,
2062 xmlXPtrCoveringRange(ctxt
, oldset
->locTab
[i
]));
2066 * Save the new value and cleanup
2068 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2069 xmlXPathFreeObject(set
);
2073 * xmlXPtrInsideRange:
2074 * @ctxt: the XPointer Parser context
2075 * @loc: the location for which the inside range must be computed
2077 * A inside range is a range described in the range-inside() description
2079 * Returns a new location or NULL in case of error
2081 static xmlXPathObjectPtr
2082 xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt
, xmlXPathObjectPtr loc
) {
2085 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
) ||
2086 (ctxt
->context
->doc
== NULL
))
2088 switch (loc
->type
) {
2090 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
2091 switch (node
->type
) {
2093 case XML_COMMENT_NODE
:
2095 case XML_CDATA_SECTION_NODE
: {
2096 if (node
->content
== NULL
) {
2097 return(xmlXPtrNewRange(node
, 0, node
, 0));
2099 return(xmlXPtrNewRange(node
, 0, node
,
2100 xmlStrlen(node
->content
)));
2103 case XML_ATTRIBUTE_NODE
:
2104 case XML_ELEMENT_NODE
:
2105 case XML_ENTITY_REF_NODE
:
2106 case XML_DOCUMENT_NODE
:
2107 case XML_NOTATION_NODE
:
2108 case XML_HTML_DOCUMENT_NODE
: {
2109 return(xmlXPtrNewRange(node
, 0, node
,
2110 xmlXPtrGetArity(node
)));
2118 xmlNodePtr node
= (xmlNodePtr
) loc
->user
;
2119 if (loc
->user2
!= NULL
) {
2120 return(xmlXPtrNewRange(node
, loc
->index
,
2121 loc
->user2
, loc
->index2
));
2123 switch (node
->type
) {
2125 case XML_COMMENT_NODE
:
2127 case XML_CDATA_SECTION_NODE
: {
2128 if (node
->content
== NULL
) {
2129 return(xmlXPtrNewRange(node
, 0, node
, 0));
2131 return(xmlXPtrNewRange(node
, 0, node
,
2132 xmlStrlen(node
->content
)));
2135 case XML_ATTRIBUTE_NODE
:
2136 case XML_ELEMENT_NODE
:
2137 case XML_ENTITY_REF_NODE
:
2138 case XML_DOCUMENT_NODE
:
2139 case XML_NOTATION_NODE
:
2140 case XML_HTML_DOCUMENT_NODE
: {
2141 return(xmlXPtrNewRange(node
, 0, node
,
2142 xmlXPtrGetArity(node
)));
2151 TODO
/* missed one case ??? */
2157 * xmlXPtrRangeInsideFunction:
2158 * @ctxt: the XPointer Parser context
2159 * @nargs: the number of args
2161 * Function implementing the range-inside() function 5.4.3
2162 * location-set range-inside(location-set )
2164 * The range-inside function returns ranges covering the contents of
2165 * the locations in the argument location-set. For each location x in
2166 * the argument location-set, a range location is added to the result
2167 * location-set. If x is a range location, then x is added to the
2168 * result location-set. If x is not a range location, then x is used
2169 * as the container location of the start and end points of the range
2170 * location to be added; the index of the start point of the range is
2171 * zero; if the end point is a character point then its index is the
2172 * length of the string-value of x, and otherwise is the number of
2173 * location children of x.
2177 xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2179 xmlXPathObjectPtr set
;
2180 xmlLocationSetPtr oldset
;
2181 xmlLocationSetPtr newset
;
2184 if ((ctxt
->value
== NULL
) ||
2185 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
2186 (ctxt
->value
->type
!= XPATH_NODESET
)))
2187 XP_ERROR(XPATH_INVALID_TYPE
)
2189 set
= valuePop(ctxt
);
2190 if (set
->type
== XPATH_NODESET
) {
2191 xmlXPathObjectPtr tmp
;
2194 * First convert to a location set
2196 tmp
= xmlXPtrNewLocationSetNodeSet(set
->nodesetval
);
2197 xmlXPathFreeObject(set
);
2200 oldset
= (xmlLocationSetPtr
) set
->user
;
2203 * The loop is to compute the covering range for each item and add it
2205 newset
= xmlXPtrLocationSetCreate(NULL
);
2206 for (i
= 0;i
< oldset
->locNr
;i
++) {
2207 xmlXPtrLocationSetAdd(newset
,
2208 xmlXPtrInsideRange(ctxt
, oldset
->locTab
[i
]));
2212 * Save the new value and cleanup
2214 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2215 xmlXPathFreeObject(set
);
2219 * xmlXPtrRangeToFunction:
2220 * @ctxt: the XPointer Parser context
2221 * @nargs: the number of args
2223 * Implement the range-to() XPointer function
2226 xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2227 xmlXPathObjectPtr range
;
2229 xmlXPathObjectPtr res
, obj
;
2230 xmlXPathObjectPtr tmp
;
2231 xmlLocationSetPtr newset
= NULL
;
2232 xmlNodeSetPtr oldset
;
2235 if (ctxt
== NULL
) return;
2238 * Save the expression pointer since we will have to evaluate
2239 * it multiple times. Initialize the new set.
2241 CHECK_TYPE(XPATH_NODESET
);
2242 obj
= valuePop(ctxt
);
2243 oldset
= obj
->nodesetval
;
2244 ctxt
->context
->node
= NULL
;
2247 newset
= xmlXPtrLocationSetCreate(NULL
);
2249 for (i
= 0; i
< oldset
->nodeNr
; i
++) {
2253 * Run the evaluation with a node list made of a single item
2256 ctxt
->context
->node
= oldset
->nodeTab
[i
];
2257 tmp
= xmlXPathNewNodeSet(ctxt
->context
->node
);
2258 valuePush(ctxt
, tmp
);
2260 xmlXPathEvalExpr(ctxt
);
2264 * The result of the evaluation need to be tested to
2265 * decided whether the filter succeeded or not
2267 res
= valuePop(ctxt
);
2268 range
= xmlXPtrNewRangeNodeObject(oldset
->nodeTab
[i
], res
);
2269 if (range
!= NULL
) {
2270 xmlXPtrLocationSetAdd(newset
, range
);
2277 xmlXPathFreeObject(res
);
2278 if (ctxt
->value
== tmp
) {
2279 res
= valuePop(ctxt
);
2280 xmlXPathFreeObject(res
);
2283 ctxt
->context
->node
= NULL
;
2287 * The result is used as the new evaluation set.
2289 xmlXPathFreeObject(obj
);
2290 ctxt
->context
->node
= NULL
;
2291 ctxt
->context
->contextSize
= -1;
2292 ctxt
->context
->proximityPosition
= -1;
2293 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2297 * xmlXPtrAdvanceNode:
2299 * @level: incremented/decremented to show level in tree
2301 * Advance to the next element or text node in document order
2302 * TODO: add a stack for entering/exiting entities
2304 * Returns -1 in case of failure, 0 otherwise
2307 xmlXPtrAdvanceNode(xmlNodePtr cur
, int *level
) {
2309 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
2311 if (cur
->children
!= NULL
) {
2312 cur
= cur
->children
;
2317 skip
: /* This label should only be needed if something is wrong! */
2318 if (cur
->next
!= NULL
) {
2326 if (cur
== NULL
) return(NULL
);
2327 if (cur
->next
!= NULL
) {
2331 } while (cur
!= NULL
);
2334 if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2335 (cur
->type
!= XML_TEXT_NODE
) &&
2336 (cur
->type
!= XML_DOCUMENT_NODE
) &&
2337 (cur
->type
!= XML_HTML_DOCUMENT_NODE
) &&
2338 (cur
->type
!= XML_CDATA_SECTION_NODE
)) {
2339 if (cur
->type
== XML_ENTITY_REF_NODE
) { /* Shouldn't happen */
2349 * xmlXPtrAdvanceChar:
2352 * @bytes: the number of bytes
2354 * Advance a point of the associated number of bytes (not UTF8 chars)
2356 * Returns -1 in case of failure, 0 otherwise
2359 xmlXPtrAdvanceChar(xmlNodePtr
*node
, int *indx
, int bytes
) {
2364 if ((node
== NULL
) || (indx
== NULL
))
2367 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
2371 while (bytes
>= 0) {
2373 * First position to the beginning of the first text node
2374 * corresponding to this point
2376 while ((cur
!= NULL
) &&
2377 ((cur
->type
== XML_ELEMENT_NODE
) ||
2378 (cur
->type
== XML_DOCUMENT_NODE
) ||
2379 (cur
->type
== XML_HTML_DOCUMENT_NODE
))) {
2381 cur
= xmlXPtrGetNthChild(cur
, pos
);
2384 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2396 * if there is no move needed return the current value.
2398 if (pos
== 0) pos
= 1;
2405 * We should have a text (or cdata) node ...
2408 if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2409 (cur
->content
!= NULL
)) {
2410 len
= xmlStrlen(cur
->content
);
2413 /* Strange, the indx in the text node is greater than it's len */
2417 if (pos
+ bytes
>= len
) {
2418 bytes
-= (len
- pos
);
2419 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2421 } else if (pos
+ bytes
< len
) {
2432 * xmlXPtrMatchString:
2433 * @string: the string to search
2434 * @start: the start textnode
2435 * @startindex: the start index
2436 * @end: the end textnode IN/OUT
2437 * @endindex: the end index IN/OUT
2439 * Check whether the document contains @string at the position
2440 * (@start, @startindex) and limited by the (@end, @endindex) point
2442 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2443 * (@start, @startindex) will indicate the position of the beginning
2444 * of the range and (@end, @endindex) will indicate the end
2448 xmlXPtrMatchString(const xmlChar
*string
, xmlNodePtr start
, int startindex
,
2449 xmlNodePtr
*end
, int *endindex
) {
2451 int pos
; /* 0 based */
2452 int len
; /* in bytes */
2453 int stringlen
; /* in bytes */
2458 if ((start
== NULL
) || (start
->type
== XML_NAMESPACE_DECL
))
2460 if ((end
== NULL
) || (*end
== NULL
) ||
2461 ((*end
)->type
== XML_NAMESPACE_DECL
) || (endindex
== NULL
))
2466 pos
= startindex
- 1;
2467 stringlen
= xmlStrlen(string
);
2469 while (stringlen
> 0) {
2470 if ((cur
== *end
) && (pos
+ stringlen
> *endindex
))
2473 if ((cur
->type
!= XML_ELEMENT_NODE
) && (cur
->content
!= NULL
)) {
2474 len
= xmlStrlen(cur
->content
);
2475 if (len
>= pos
+ stringlen
) {
2476 match
= (!xmlStrncmp(&cur
->content
[pos
], string
, stringlen
));
2479 xmlGenericError(xmlGenericErrorContext
,
2480 "found range %d bytes at index %d of ->",
2481 stringlen
, pos
+ 1);
2482 xmlDebugDumpString(stdout
, cur
->content
);
2483 xmlGenericError(xmlGenericErrorContext
, "\n");
2486 *endindex
= pos
+ stringlen
;
2492 int sub
= len
- pos
;
2493 match
= (!xmlStrncmp(&cur
->content
[pos
], string
, sub
));
2496 xmlGenericError(xmlGenericErrorContext
,
2497 "found subrange %d bytes at index %d of ->",
2499 xmlDebugDumpString(stdout
, cur
->content
);
2500 xmlGenericError(xmlGenericErrorContext
, "\n");
2502 string
= &string
[sub
];
2509 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2518 * xmlXPtrSearchString:
2519 * @string: the string to search
2520 * @start: the start textnode IN/OUT
2521 * @startindex: the start index IN/OUT
2522 * @end: the end textnode
2523 * @endindex: the end index
2525 * Search the next occurrence of @string within the document content
2526 * until the (@end, @endindex) point is reached
2528 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2529 * (@start, @startindex) will indicate the position of the beginning
2530 * of the range and (@end, @endindex) will indicate the end
2534 xmlXPtrSearchString(const xmlChar
*string
, xmlNodePtr
*start
, int *startindex
,
2535 xmlNodePtr
*end
, int *endindex
) {
2538 int pos
; /* 0 based */
2539 int len
; /* in bytes */
2544 if ((start
== NULL
) || (*start
== NULL
) ||
2545 ((*start
)->type
== XML_NAMESPACE_DECL
) || (startindex
== NULL
))
2547 if ((end
== NULL
) || (endindex
== NULL
))
2550 pos
= *startindex
- 1;
2553 while (cur
!= NULL
) {
2554 if ((cur
->type
!= XML_ELEMENT_NODE
) && (cur
->content
!= NULL
)) {
2555 len
= xmlStrlen(cur
->content
);
2556 while (pos
<= len
) {
2558 str
= xmlStrchr(&cur
->content
[pos
], first
);
2560 pos
= (str
- (xmlChar
*)(cur
->content
));
2562 xmlGenericError(xmlGenericErrorContext
,
2563 "found '%c' at index %d of ->",
2565 xmlDebugDumpString(stdout
, cur
->content
);
2566 xmlGenericError(xmlGenericErrorContext
, "\n");
2568 if (xmlXPtrMatchString(string
, cur
, pos
+ 1,
2571 *startindex
= pos
+ 1;
2580 * An empty string is considered to match before each
2581 * character of the string-value and after the final
2585 xmlGenericError(xmlGenericErrorContext
,
2586 "found '' at index %d of ->",
2588 xmlDebugDumpString(stdout
, cur
->content
);
2589 xmlGenericError(xmlGenericErrorContext
, "\n");
2592 *startindex
= pos
+ 1;
2594 *endindex
= pos
+ 1;
2599 if ((cur
== *end
) && (pos
>= *endindex
))
2601 cur
= xmlXPtrAdvanceNode(cur
, NULL
);
2610 * xmlXPtrGetLastChar:
2614 * Computes the point coordinates of the last char of this point
2616 * Returns -1 in case of failure, 0 otherwise
2619 xmlXPtrGetLastChar(xmlNodePtr
*node
, int *indx
) {
2623 if ((node
== NULL
) || (*node
== NULL
) ||
2624 ((*node
)->type
== XML_NAMESPACE_DECL
) || (indx
== NULL
))
2629 if ((cur
->type
== XML_ELEMENT_NODE
) ||
2630 (cur
->type
== XML_DOCUMENT_NODE
) ||
2631 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
2633 cur
= xmlXPtrGetNthChild(cur
, pos
);
2636 while (cur
!= NULL
) {
2637 if (cur
->last
!= NULL
)
2639 else if ((cur
->type
!= XML_ELEMENT_NODE
) &&
2640 (cur
->content
!= NULL
)) {
2641 len
= xmlStrlen(cur
->content
);
2655 * xmlXPtrGetStartPoint:
2657 * @node: the resulting node
2658 * @indx: the resulting index
2660 * read the object and return the start point coordinates.
2662 * Returns -1 in case of failure, 0 otherwise
2665 xmlXPtrGetStartPoint(xmlXPathObjectPtr obj
, xmlNodePtr
*node
, int *indx
) {
2666 if ((obj
== NULL
) || (node
== NULL
) || (indx
== NULL
))
2669 switch (obj
->type
) {
2672 if (obj
->index
<= 0)
2679 if (obj
->index
<= 0)
2691 * xmlXPtrGetEndPoint:
2693 * @node: the resulting node
2694 * @indx: the resulting indx
2696 * read the object and return the end point coordinates.
2698 * Returns -1 in case of failure, 0 otherwise
2701 xmlXPtrGetEndPoint(xmlXPathObjectPtr obj
, xmlNodePtr
*node
, int *indx
) {
2702 if ((obj
== NULL
) || (node
== NULL
) || (indx
== NULL
))
2705 switch (obj
->type
) {
2708 if (obj
->index
<= 0)
2715 if (obj
->index
<= 0)
2727 * xmlXPtrStringRangeFunction:
2728 * @ctxt: the XPointer Parser context
2729 * @nargs: the number of args
2731 * Function implementing the string-range() function
2732 * range as described in 5.4.2
2734 * ------------------------------
2735 * [Definition: For each location in the location-set argument,
2736 * string-range returns a set of string ranges, a set of substrings in a
2737 * string. Specifically, the string-value of the location is searched for
2738 * substrings that match the string argument, and the resulting location-set
2739 * will contain a range location for each non-overlapping match.]
2740 * An empty string is considered to match before each character of the
2741 * string-value and after the final character. Whitespace in a string
2742 * is matched literally, with no normalization except that provided by
2743 * XML for line ends. The third argument gives the position of the first
2744 * character to be in the resulting range, relative to the start of the
2745 * match. The default value is 1, which makes the range start immediately
2746 * before the first character of the matched string. The fourth argument
2747 * gives the number of characters in the range; the default is that the
2748 * range extends to the end of the matched string.
2750 * Element boundaries, as well as entire embedded nodes such as processing
2751 * instructions and comments, are ignored as defined in [XPath].
2753 * If the string in the second argument is not found in the string-value
2754 * of the location, or if a value in the third or fourth argument indicates
2755 * a string that is beyond the beginning or end of the document, the
2758 * The points of the range-locations in the returned location-set will
2759 * all be character points.
2760 * ------------------------------
2763 xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
2764 int i
, startindex
, endindex
= 0, fendindex
;
2765 xmlNodePtr start
, end
= 0, fend
;
2766 xmlXPathObjectPtr set
;
2767 xmlLocationSetPtr oldset
;
2768 xmlLocationSetPtr newset
;
2769 xmlXPathObjectPtr string
;
2770 xmlXPathObjectPtr position
= NULL
;
2771 xmlXPathObjectPtr number
= NULL
;
2772 int found
, pos
= 0, num
= 0;
2775 * Grab the arguments
2777 if ((nargs
< 2) || (nargs
> 4))
2778 XP_ERROR(XPATH_INVALID_ARITY
);
2781 CHECK_TYPE(XPATH_NUMBER
);
2782 number
= valuePop(ctxt
);
2784 num
= (int) number
->floatval
;
2787 CHECK_TYPE(XPATH_NUMBER
);
2788 position
= valuePop(ctxt
);
2789 if (position
!= NULL
)
2790 pos
= (int) position
->floatval
;
2792 CHECK_TYPE(XPATH_STRING
);
2793 string
= valuePop(ctxt
);
2794 if ((ctxt
->value
== NULL
) ||
2795 ((ctxt
->value
->type
!= XPATH_LOCATIONSET
) &&
2796 (ctxt
->value
->type
!= XPATH_NODESET
)))
2797 XP_ERROR(XPATH_INVALID_TYPE
)
2799 set
= valuePop(ctxt
);
2800 newset
= xmlXPtrLocationSetCreate(NULL
);
2801 if (set
->nodesetval
== NULL
) {
2804 if (set
->type
== XPATH_NODESET
) {
2805 xmlXPathObjectPtr tmp
;
2808 * First convert to a location set
2810 tmp
= xmlXPtrNewLocationSetNodeSet(set
->nodesetval
);
2811 xmlXPathFreeObject(set
);
2814 oldset
= (xmlLocationSetPtr
) set
->user
;
2817 * The loop is to search for each element in the location set
2818 * the list of location set corresponding to that search
2820 for (i
= 0;i
< oldset
->locNr
;i
++) {
2822 xmlXPathDebugDumpObject(stdout
, oldset
->locTab
[i
], 0);
2825 xmlXPtrGetStartPoint(oldset
->locTab
[i
], &start
, &startindex
);
2826 xmlXPtrGetEndPoint(oldset
->locTab
[i
], &end
, &endindex
);
2827 xmlXPtrAdvanceChar(&start
, &startindex
, 0);
2828 xmlXPtrGetLastChar(&end
, &endindex
);
2831 xmlGenericError(xmlGenericErrorContext
,
2832 "from index %d of ->", startindex
);
2833 xmlDebugDumpString(stdout
, start
->content
);
2834 xmlGenericError(xmlGenericErrorContext
, "\n");
2835 xmlGenericError(xmlGenericErrorContext
,
2836 "to index %d of ->", endindex
);
2837 xmlDebugDumpString(stdout
, end
->content
);
2838 xmlGenericError(xmlGenericErrorContext
, "\n");
2842 fendindex
= endindex
;
2843 found
= xmlXPtrSearchString(string
->stringval
, &start
, &startindex
,
2846 if (position
== NULL
) {
2847 xmlXPtrLocationSetAdd(newset
,
2848 xmlXPtrNewRange(start
, startindex
, fend
, fendindex
));
2849 } else if (xmlXPtrAdvanceChar(&start
, &startindex
,
2851 if ((number
!= NULL
) && (num
> 0)) {
2855 rindx
= startindex
- 1;
2856 if (xmlXPtrAdvanceChar(&rend
, &rindx
,
2858 xmlXPtrLocationSetAdd(newset
,
2859 xmlXPtrNewRange(start
, startindex
,
2862 } else if ((number
!= NULL
) && (num
<= 0)) {
2863 xmlXPtrLocationSetAdd(newset
,
2864 xmlXPtrNewRange(start
, startindex
,
2865 start
, startindex
));
2867 xmlXPtrLocationSetAdd(newset
,
2868 xmlXPtrNewRange(start
, startindex
,
2873 startindex
= fendindex
;
2874 if (string
->stringval
[0] == 0)
2877 } while (found
== 1);
2881 * Save the new value and cleanup
2884 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2885 xmlXPathFreeObject(set
);
2886 xmlXPathFreeObject(string
);
2887 if (position
) xmlXPathFreeObject(position
);
2888 if (number
) xmlXPathFreeObject(number
);
2892 * xmlXPtrEvalRangePredicate:
2893 * @ctxt: the XPointer Parser context
2895 * [8] Predicate ::= '[' PredicateExpr ']'
2896 * [9] PredicateExpr ::= Expr
2898 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2899 * a Location Set instead of a node set
2902 xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt
) {
2904 xmlXPathObjectPtr res
;
2905 xmlXPathObjectPtr obj
, tmp
;
2906 xmlLocationSetPtr newset
= NULL
;
2907 xmlLocationSetPtr oldset
;
2910 if (ctxt
== NULL
) return;
2914 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);
2920 * Extract the old set, and then evaluate the result of the
2921 * expression for all the element in the set. use it to grow
2924 CHECK_TYPE(XPATH_LOCATIONSET
);
2925 obj
= valuePop(ctxt
);
2927 ctxt
->context
->node
= NULL
;
2929 if ((oldset
== NULL
) || (oldset
->locNr
== 0)) {
2930 ctxt
->context
->contextSize
= 0;
2931 ctxt
->context
->proximityPosition
= 0;
2932 xmlXPathEvalExpr(ctxt
);
2933 res
= valuePop(ctxt
);
2935 xmlXPathFreeObject(res
);
2936 valuePush(ctxt
, obj
);
2940 * Save the expression pointer since we will have to evaluate
2941 * it multiple times. Initialize the new set.
2944 newset
= xmlXPtrLocationSetCreate(NULL
);
2946 for (i
= 0; i
< oldset
->locNr
; i
++) {
2950 * Run the evaluation with a node list made of a single item
2953 ctxt
->context
->node
= oldset
->locTab
[i
]->user
;
2954 tmp
= xmlXPathNewNodeSet(ctxt
->context
->node
);
2955 valuePush(ctxt
, tmp
);
2956 ctxt
->context
->contextSize
= oldset
->locNr
;
2957 ctxt
->context
->proximityPosition
= i
+ 1;
2959 xmlXPathEvalExpr(ctxt
);
2963 * The result of the evaluation need to be tested to
2964 * decided whether the filter succeeded or not
2966 res
= valuePop(ctxt
);
2967 if (xmlXPathEvaluatePredicateResult(ctxt
, res
)) {
2968 xmlXPtrLocationSetAdd(newset
,
2969 xmlXPathObjectCopy(oldset
->locTab
[i
]));
2976 xmlXPathFreeObject(res
);
2977 if (ctxt
->value
== tmp
) {
2978 res
= valuePop(ctxt
);
2979 xmlXPathFreeObject(res
);
2982 ctxt
->context
->node
= NULL
;
2986 * The result is used as the new evaluation set.
2988 xmlXPathFreeObject(obj
);
2989 ctxt
->context
->node
= NULL
;
2990 ctxt
->context
->contextSize
= -1;
2991 ctxt
->context
->proximityPosition
= -1;
2992 valuePush(ctxt
, xmlXPtrWrapLocationSet(newset
));
2995 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);
3002 #define bottom_xpointer
3003 #include "elfgcchack.h"