blob: ead8c1742edadaf5a50862349e1e9293f5edbf86 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xpointer.c : Code to handle XML Pointer
3 *
4 * World Wide Web Consortium Working Draft 03-March-1998
5 * http://www.w3.org/TR/2000/CR-xptr-20000607
6 *
7 * See Copyright for the status of this software.
8 *
9 * Daniel.Veillard@w3.org
10 */
11
12#ifdef WIN32
13#include "win32config.h"
14#else
15#include "config.h"
16#endif
17
18/**
19 * TODO: better handling of error cases, the full expression should
20 * be parsed beforehand instead of a progressive evaluation
21 * TODO: Access into entities references are not supported now ...
22 * need a start to be able to pop out of entities refs since
23 * parent is the endity declaration, not the ref.
24 */
25
26#include <stdio.h>
27#include <string.h>
28#include <libxml/xpointer.h>
29#include <libxml/xmlmemory.h>
30#include <libxml/parserInternals.h>
31#include <libxml/uri.h>
32#include <libxml/xpath.h>
33#include <libxml/xpathInternals.h>
34#ifdef LIBXML_DEBUG_ENABLED
35#include <libxml/debugXML.h>
36#endif
37#include <libxml/xmlerror.h>
38
39#ifdef LIBXML_XPTR_ENABLED
40
41/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
42#define XPTR_XMLNS_SCHEME
43
44/* #define DEBUG_RANGES */
45
46#define TODO \
47 xmlGenericError(xmlGenericErrorContext, \
48 "Unimplemented block at %s:%d\n", \
49 __FILE__, __LINE__);
50
51#define STRANGE \
52 xmlGenericError(xmlGenericErrorContext, \
53 "Internal error at %s:%d\n", \
54 __FILE__, __LINE__);
55
56/************************************************************************
57 * *
58 * A few helper functions for child sequences *
59 * *
60 ************************************************************************/
61
62xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur);
63/**
64 * xmlXPtrGetArity:
65 * @cur: the node
66 *
67 * Returns the number of child for an element, -1 in case of error
68 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +000069static int
Owen Taylor3473f882001-02-23 17:55:21 +000070xmlXPtrGetArity(xmlNodePtr cur) {
71 int i;
72 if (cur == NULL)
73 return(-1);
74 cur = cur->children;
75 for (i = 0;cur != NULL;cur = cur->next) {
76 if ((cur->type == XML_ELEMENT_NODE) ||
77 (cur->type == XML_DOCUMENT_NODE) ||
78 (cur->type == XML_HTML_DOCUMENT_NODE)) {
79 i++;
80 }
81 }
82 return(i);
83}
84
85/**
86 * xmlXPtrGetIndex:
87 * @cur: the node
88 *
89 * Returns the index of the node in its parent children list, -1
90 * in case of error
91 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +000092static int
Owen Taylor3473f882001-02-23 17:55:21 +000093xmlXPtrGetIndex(xmlNodePtr cur) {
94 int i;
95 if (cur == NULL)
96 return(-1);
97 for (i = 1;cur != NULL;cur = cur->prev) {
98 if ((cur->type == XML_ELEMENT_NODE) ||
99 (cur->type == XML_DOCUMENT_NODE) ||
100 (cur->type == XML_HTML_DOCUMENT_NODE)) {
101 i++;
102 }
103 }
104 return(i);
105}
106
107/**
108 * xmlXPtrGetNthChild:
109 * @cur: the node
110 * @no: the child number
111 *
112 * Returns the @no'th element child of @cur or NULL
113 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000114static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000115xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
116 int i;
117 if (cur == NULL)
118 return(cur);
119 cur = cur->children;
120 for (i = 0;i <= no;cur = cur->next) {
121 if (cur == NULL)
122 return(cur);
123 if ((cur->type == XML_ELEMENT_NODE) ||
124 (cur->type == XML_DOCUMENT_NODE) ||
125 (cur->type == XML_HTML_DOCUMENT_NODE)) {
126 i++;
127 if (i == no)
128 break;
129 }
130 }
131 return(cur);
132}
133
134/************************************************************************
135 * *
136 * Handling of XPointer specific types *
137 * *
138 ************************************************************************/
139
140/**
141 * xmlXPtrCmpPoints:
142 * @node1: the first node
143 * @index1: the first index
144 * @node2: the second node
145 * @index2: the second index
146 *
147 * Compare two points w.r.t document order
148 *
149 * Returns -2 in case of error 1 if first point < second point, 0 if
150 * that's the same point, -1 otherwise
151 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000152static int
Owen Taylor3473f882001-02-23 17:55:21 +0000153xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
154 if ((node1 == NULL) || (node2 == NULL))
155 return(-2);
156 /*
157 * a couple of optimizations which will avoid computations in most cases
158 */
159 if (node1 == node2) {
160 if (index1 < index2)
161 return(1);
162 if (index1 > index2)
163 return(-1);
164 return(0);
165 }
166 return(xmlXPathCmpNodes(node1, node2));
167}
168
169/**
170 * xmlXPtrNewPoint:
171 * @node: the xmlNodePtr
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000172 * @indx: the indx within the node
Owen Taylor3473f882001-02-23 17:55:21 +0000173 *
174 * Create a new xmlXPathObjectPtr of type point
175 *
176 * Returns the newly created object.
177 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000178static xmlXPathObjectPtr
179xmlXPtrNewPoint(xmlNodePtr node, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000180 xmlXPathObjectPtr ret;
181
182 if (node == NULL)
183 return(NULL);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000184 if (indx < 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000185 return(NULL);
186
187 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
188 if (ret == NULL) {
189 xmlGenericError(xmlGenericErrorContext,
190 "xmlXPtrNewPoint: out of memory\n");
191 return(NULL);
192 }
193 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
194 ret->type = XPATH_POINT;
195 ret->user = (void *) node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000196 ret->index = indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000197 return(ret);
198}
199
200/**
201 * xmlXPtrRangeCheckOrder:
202 * @range: an object range
203 *
204 * Make sure the points in the range are in the right order
205 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000206static void
Owen Taylor3473f882001-02-23 17:55:21 +0000207xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
208 int tmp;
209 xmlNodePtr tmp2;
210 if (range == NULL)
211 return;
212 if (range->type != XPATH_RANGE)
213 return;
214 if (range->user2 == NULL)
215 return;
216 tmp = xmlXPtrCmpPoints(range->user, range->index,
217 range->user2, range->index2);
218 if (tmp == -1) {
219 tmp2 = range->user;
220 range->user = range->user2;
221 range->user2 = tmp2;
222 tmp = range->index;
223 range->index = range->index2;
224 range->index2 = tmp;
225 }
226}
227
228/**
229 * xmlXPtrRangesEqual:
230 * @range1: the first range
231 * @range2: the second range
232 *
233 * Compare two ranges
234 *
235 * Return 1 if equal, 0 otherwise
236 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000237static int
Owen Taylor3473f882001-02-23 17:55:21 +0000238xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
239 if (range1 == range2)
240 return(1);
241 if ((range1 == NULL) || (range2 == NULL))
242 return(0);
243 if (range1->type != range2->type)
244 return(0);
245 if (range1->type != XPATH_RANGE)
246 return(0);
247 if (range1->user != range2->user)
248 return(0);
249 if (range1->index != range2->index)
250 return(0);
251 if (range1->user2 != range2->user2)
252 return(0);
253 if (range1->index2 != range2->index2)
254 return(0);
255 return(1);
256}
257
258/**
259 * xmlXPtrNewRange:
260 * @start: the starting node
261 * @startindex: the start index
262 * @end: the ending point
263 * @endindex: the ending index
264 *
265 * Create a new xmlXPathObjectPtr of type range
266 *
267 * Returns the newly created object.
268 */
269xmlXPathObjectPtr
270xmlXPtrNewRange(xmlNodePtr start, int startindex,
271 xmlNodePtr end, int endindex) {
272 xmlXPathObjectPtr ret;
273
274 if (start == NULL)
275 return(NULL);
276 if (end == NULL)
277 return(NULL);
278 if (startindex < 0)
279 return(NULL);
280 if (endindex < 0)
281 return(NULL);
282
283 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
284 if (ret == NULL) {
285 xmlGenericError(xmlGenericErrorContext,
286 "xmlXPtrNewRangePoints: out of memory\n");
287 return(NULL);
288 }
289 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
290 ret->type = XPATH_RANGE;
291 ret->user = start;
292 ret->index = startindex;
293 ret->user2 = end;
294 ret->index2 = endindex;
295 xmlXPtrRangeCheckOrder(ret);
296 return(ret);
297}
298
299/**
300 * xmlXPtrNewRangePoints:
301 * @start: the starting point
302 * @end: the ending point
303 *
304 * Create a new xmlXPathObjectPtr of type range using 2 Points
305 *
306 * Returns the newly created object.
307 */
308xmlXPathObjectPtr
309xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
310 xmlXPathObjectPtr ret;
311
312 if (start == NULL)
313 return(NULL);
314 if (end == NULL)
315 return(NULL);
316 if (start->type != XPATH_POINT)
317 return(NULL);
318 if (end->type != XPATH_POINT)
319 return(NULL);
320
321 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
322 if (ret == NULL) {
323 xmlGenericError(xmlGenericErrorContext,
324 "xmlXPtrNewRangePoints: out of memory\n");
325 return(NULL);
326 }
327 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
328 ret->type = XPATH_RANGE;
329 ret->user = start->user;
330 ret->index = start->index;
331 ret->user2 = end->user;
332 ret->index2 = end->index;
333 xmlXPtrRangeCheckOrder(ret);
334 return(ret);
335}
336
337/**
338 * xmlXPtrNewRangePointNode:
339 * @start: the starting point
340 * @end: the ending node
341 *
342 * Create a new xmlXPathObjectPtr of type range from a point to a node
343 *
344 * Returns the newly created object.
345 */
346xmlXPathObjectPtr
347xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
348 xmlXPathObjectPtr ret;
349
350 if (start == NULL)
351 return(NULL);
352 if (end == NULL)
353 return(NULL);
354 if (start->type != XPATH_POINT)
355 return(NULL);
356
357 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
358 if (ret == NULL) {
359 xmlGenericError(xmlGenericErrorContext,
360 "xmlXPtrNewRangePointNode: out of memory\n");
361 return(NULL);
362 }
363 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
364 ret->type = XPATH_RANGE;
365 ret->user = start->user;
366 ret->index = start->index;
367 ret->user2 = end;
368 ret->index2 = -1;
369 xmlXPtrRangeCheckOrder(ret);
370 return(ret);
371}
372
373/**
374 * xmlXPtrNewRangeNodePoint:
375 * @start: the starting node
376 * @end: the ending point
377 *
378 * Create a new xmlXPathObjectPtr of type range from a node to a point
379 *
380 * Returns the newly created object.
381 */
382xmlXPathObjectPtr
383xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
384 xmlXPathObjectPtr ret;
385
386 if (start == NULL)
387 return(NULL);
388 if (end == NULL)
389 return(NULL);
390 if (start->type != XPATH_POINT)
391 return(NULL);
392 if (end->type != XPATH_POINT)
393 return(NULL);
394
395 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
396 if (ret == NULL) {
397 xmlGenericError(xmlGenericErrorContext,
398 "xmlXPtrNewRangeNodePoint: out of memory\n");
399 return(NULL);
400 }
401 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
402 ret->type = XPATH_RANGE;
403 ret->user = start;
404 ret->index = -1;
405 ret->user2 = end->user;
406 ret->index2 = end->index;
407 xmlXPtrRangeCheckOrder(ret);
408 return(ret);
409}
410
411/**
412 * xmlXPtrNewRangeNodes:
413 * @start: the starting node
414 * @end: the ending node
415 *
416 * Create a new xmlXPathObjectPtr of type range using 2 nodes
417 *
418 * Returns the newly created object.
419 */
420xmlXPathObjectPtr
421xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
422 xmlXPathObjectPtr ret;
423
424 if (start == NULL)
425 return(NULL);
426 if (end == NULL)
427 return(NULL);
428
429 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
430 if (ret == NULL) {
431 xmlGenericError(xmlGenericErrorContext,
432 "xmlXPtrNewRangeNodes: out of memory\n");
433 return(NULL);
434 }
435 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
436 ret->type = XPATH_RANGE;
437 ret->user = start;
438 ret->index = -1;
439 ret->user2 = end;
440 ret->index2 = -1;
441 xmlXPtrRangeCheckOrder(ret);
442 return(ret);
443}
444
445/**
446 * xmlXPtrNewCollapsedRange:
447 * @start: the starting and ending node
448 *
449 * Create a new xmlXPathObjectPtr of type range using a single nodes
450 *
451 * Returns the newly created object.
452 */
453xmlXPathObjectPtr
454xmlXPtrNewCollapsedRange(xmlNodePtr start) {
455 xmlXPathObjectPtr ret;
456
457 if (start == NULL)
458 return(NULL);
459
460 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
461 if (ret == NULL) {
462 xmlGenericError(xmlGenericErrorContext,
463 "xmlXPtrNewRangeNodes: out of memory\n");
464 return(NULL);
465 }
466 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
467 ret->type = XPATH_RANGE;
468 ret->user = start;
469 ret->index = -1;
470 ret->user2 = NULL;
471 ret->index2 = -1;
472 return(ret);
473}
474
475/**
476 * xmlXPtrNewRangeNodeObject:
477 * @start: the starting node
478 * @end: the ending object
479 *
480 * Create a new xmlXPathObjectPtr of type range from a not to an object
481 *
482 * Returns the newly created object.
483 */
484xmlXPathObjectPtr
485xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
486 xmlXPathObjectPtr ret;
487
488 if (start == NULL)
489 return(NULL);
490 if (end == NULL)
491 return(NULL);
492 switch (end->type) {
493 case XPATH_POINT:
494 break;
495 case XPATH_NODESET:
496 /*
497 * Empty set ...
498 */
499 if (end->nodesetval->nodeNr <= 0)
500 return(NULL);
501 break;
502 default:
503 TODO
504 return(NULL);
505 }
506
507 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
508 if (ret == NULL) {
509 xmlGenericError(xmlGenericErrorContext,
510 "xmlXPtrNewRangeNodeObject: out of memory\n");
511 return(NULL);
512 }
513 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
514 ret->type = XPATH_RANGE;
515 ret->user = start;
516 ret->index = -1;
517 switch (end->type) {
518 case XPATH_POINT:
519 ret->user2 = end->user;
520 ret->index2 = end->index;
521 case XPATH_NODESET: {
522 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
523 ret->index2 = -1;
524 break;
525 }
526 default:
527 STRANGE
528 return(NULL);
529 }
530 xmlXPtrRangeCheckOrder(ret);
531 return(ret);
532}
533
534#define XML_RANGESET_DEFAULT 10
535
536/**
537 * xmlXPtrLocationSetCreate:
538 * @val: an initial xmlXPathObjectPtr, or NULL
539 *
540 * Create a new xmlLocationSetPtr of type double and of value @val
541 *
542 * Returns the newly created object.
543 */
544xmlLocationSetPtr
545xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
546 xmlLocationSetPtr ret;
547
548 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
549 if (ret == NULL) {
550 xmlGenericError(xmlGenericErrorContext,
551 "xmlXPtrLocationSetCreate: out of memory\n");
552 return(NULL);
553 }
554 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
555 if (val != NULL) {
556 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
557 sizeof(xmlXPathObjectPtr));
558 if (ret->locTab == NULL) {
559 xmlGenericError(xmlGenericErrorContext,
560 "xmlXPtrLocationSetCreate: out of memory\n");
561 return(NULL);
562 }
563 memset(ret->locTab, 0 ,
564 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
565 ret->locMax = XML_RANGESET_DEFAULT;
566 ret->locTab[ret->locNr++] = val;
567 }
568 return(ret);
569}
570
571/**
572 * xmlXPtrLocationSetAdd:
573 * @cur: the initial range set
574 * @val: a new xmlXPathObjectPtr
575 *
576 * add a new xmlXPathObjectPtr ot an existing LocationSet
577 * If the location already exist in the set @val is freed.
578 */
579void
580xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
581 int i;
582
583 if (val == NULL) return;
584
585 /*
586 * check against doublons
587 */
588 for (i = 0;i < cur->locNr;i++) {
589 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
590 xmlXPathFreeObject(val);
591 return;
592 }
593 }
594
595 /*
596 * grow the locTab if needed
597 */
598 if (cur->locMax == 0) {
599 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
600 sizeof(xmlXPathObjectPtr));
601 if (cur->locTab == NULL) {
602 xmlGenericError(xmlGenericErrorContext,
603 "xmlXPtrLocationSetAdd: out of memory\n");
604 return;
605 }
606 memset(cur->locTab, 0 ,
607 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
608 cur->locMax = XML_RANGESET_DEFAULT;
609 } else if (cur->locNr == cur->locMax) {
610 xmlXPathObjectPtr *temp;
611
612 cur->locMax *= 2;
613 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
614 sizeof(xmlXPathObjectPtr));
615 if (temp == NULL) {
616 xmlGenericError(xmlGenericErrorContext,
617 "xmlXPtrLocationSetAdd: out of memory\n");
618 return;
619 }
620 cur->locTab = temp;
621 }
622 cur->locTab[cur->locNr++] = val;
623}
624
625/**
626 * xmlXPtrLocationSetMerge:
627 * @val1: the first LocationSet
628 * @val2: the second LocationSet
629 *
630 * Merges two rangesets, all ranges from @val2 are added to @val1
631 *
632 * Returns val1 once extended or NULL in case of error.
633 */
634xmlLocationSetPtr
635xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
636 int i;
637
638 if (val1 == NULL) return(NULL);
639 if (val2 == NULL) return(val1);
640
641 /*
642 * !!!!! this can be optimized a lot, knowing that both
643 * val1 and val2 already have unicity of their values.
644 */
645
646 for (i = 0;i < val2->locNr;i++)
647 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
648
649 return(val1);
650}
651
652/**
653 * xmlXPtrLocationSetDel:
654 * @cur: the initial range set
655 * @val: an xmlXPathObjectPtr
656 *
657 * Removes an xmlXPathObjectPtr from an existing LocationSet
658 */
659void
660xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
661 int i;
662
663 if (cur == NULL) return;
664 if (val == NULL) return;
665
666 /*
667 * check against doublons
668 */
669 for (i = 0;i < cur->locNr;i++)
670 if (cur->locTab[i] == val) break;
671
672 if (i >= cur->locNr) {
673#ifdef DEBUG
674 xmlGenericError(xmlGenericErrorContext,
675 "xmlXPtrLocationSetDel: Range %s wasn't found in RangeList\n",
676 val->name);
677#endif
678 return;
679 }
680 cur->locNr--;
681 for (;i < cur->locNr;i++)
682 cur->locTab[i] = cur->locTab[i + 1];
683 cur->locTab[cur->locNr] = NULL;
684}
685
686/**
687 * xmlXPtrLocationSetRemove:
688 * @cur: the initial range set
689 * @val: the index to remove
690 *
691 * Removes an entry from an existing LocationSet list.
692 */
693void
694xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
695 if (cur == NULL) return;
696 if (val >= cur->locNr) return;
697 cur->locNr--;
698 for (;val < cur->locNr;val++)
699 cur->locTab[val] = cur->locTab[val + 1];
700 cur->locTab[cur->locNr] = NULL;
701}
702
703/**
704 * xmlXPtrFreeLocationSet:
705 * @obj: the xmlLocationSetPtr to free
706 *
707 * Free the LocationSet compound (not the actual ranges !).
708 */
709void
710xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
711 int i;
712
713 if (obj == NULL) return;
714 if (obj->locTab != NULL) {
715 for (i = 0;i < obj->locNr; i++) {
716 xmlXPathFreeObject(obj->locTab[i]);
717 }
Owen Taylor3473f882001-02-23 17:55:21 +0000718 xmlFree(obj->locTab);
719 }
Owen Taylor3473f882001-02-23 17:55:21 +0000720 xmlFree(obj);
721}
722
723/**
724 * xmlXPtrNewLocationSetNodes:
725 * @start: the start NodePtr value
726 * @end: the end NodePtr value or NULL
727 *
728 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
729 * it with the single range made of the two nodes @start and @end
730 *
731 * Returns the newly created object.
732 */
733xmlXPathObjectPtr
734xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
735 xmlXPathObjectPtr ret;
736
737 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
738 if (ret == NULL) {
739 xmlGenericError(xmlGenericErrorContext,
740 "xmlXPtrNewLocationSetNodes: out of memory\n");
741 return(NULL);
742 }
743 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
744 ret->type = XPATH_LOCATIONSET;
745 if (end == NULL)
746 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
747 else
748 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
749 return(ret);
750}
751
752/**
753 * xmlXPtrNewLocationSetNodeSet:
754 * @set: a node set
755 *
756 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
757 * it with all the nodes from @set
758 *
759 * Returns the newly created object.
760 */
761xmlXPathObjectPtr
762xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
763 xmlXPathObjectPtr ret;
764
765 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
766 if (ret == NULL) {
767 xmlGenericError(xmlGenericErrorContext,
768 "xmlXPtrNewLocationSetNodes: out of memory\n");
769 return(NULL);
770 }
771 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
772 ret->type = XPATH_LOCATIONSET;
773 if (set != NULL) {
774 int i;
775 xmlLocationSetPtr newset;
776
777 newset = xmlXPtrLocationSetCreate(NULL);
778 if (newset == NULL)
779 return(ret);
780
781 for (i = 0;i < set->nodeNr;i++)
782 xmlXPtrLocationSetAdd(newset,
783 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
784
785 ret->user = (void *) newset;
786 }
787 return(ret);
788}
789
790/**
791 * xmlXPtrWrapLocationSet:
792 * @val: the LocationSet value
793 *
794 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
795 *
796 * Returns the newly created object.
797 */
798xmlXPathObjectPtr
799xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
800 xmlXPathObjectPtr ret;
801
802 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
803 if (ret == NULL) {
804 xmlGenericError(xmlGenericErrorContext,
805 "xmlXPtrWrapLocationSet: out of memory\n");
806 return(NULL);
807 }
808 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
809 ret->type = XPATH_LOCATIONSET;
810 ret->user = (void *) val;
811 return(ret);
812}
813
814/************************************************************************
815 * *
816 * The parser *
817 * *
818 ************************************************************************/
819
820/*
821 * Macros for accessing the content. Those should be used only by the parser,
822 * and not exported.
823 *
824 * Dirty macros, i.e. one need to make assumption on the context to use them
825 *
826 * CUR_PTR return the current pointer to the xmlChar to be parsed.
827 * CUR returns the current xmlChar value, i.e. a 8 bit value
828 * in ISO-Latin or UTF-8.
829 * This should be used internally by the parser
830 * only to compare to ASCII values otherwise it would break when
831 * running with UTF-8 encoding.
832 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
833 * to compare on ASCII based substring.
834 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
835 * strings within the parser.
836 * CURRENT Returns the current char value, with the full decoding of
837 * UTF-8 if we are using this mode. It returns an int.
838 * NEXT Skip to the next character, this does the proper decoding
839 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
840 * It returns the pointer to the current xmlChar.
841 */
842
843#define CUR (*ctxt->cur)
844#define SKIP(val) ctxt->cur += (val)
845#define NXT(val) ctxt->cur[(val)]
846#define CUR_PTR ctxt->cur
847
848#define SKIP_BLANKS \
849 while (IS_BLANK(*(ctxt->cur))) NEXT
850
851#define CURRENT (*ctxt->cur)
852#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
853
854/*
855 * xmlXPtrGetChildNo:
856 * @ctxt: the XPointer Parser context
857 * @index: the child number
858 *
859 * Move the current node of the nodeset on the stack to the
860 * given child if found
861 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000862static void
863xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000864 xmlNodePtr cur = NULL;
865 xmlXPathObjectPtr obj;
866 xmlNodeSetPtr oldset;
867
868 CHECK_TYPE(XPATH_NODESET);
869 obj = valuePop(ctxt);
870 oldset = obj->nodesetval;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000871 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000872 xmlXPathFreeObject(obj);
873 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
874 return;
875 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000876 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
Owen Taylor3473f882001-02-23 17:55:21 +0000877 if (cur == NULL) {
878 xmlXPathFreeObject(obj);
879 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
880 return;
881 }
882 oldset->nodeTab[0] = cur;
883 valuePush(ctxt, obj);
884}
885
886/**
887 * xmlXPtrEvalXPtrPart:
888 * @ctxt: the XPointer Parser context
889 * @name: the preparsed Scheme for the XPtrPart
890 *
891 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
892 * | Scheme '(' SchemeSpecificExpr ')'
893 *
894 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
895 *
896 * SchemeSpecificExpr ::= StringWithBalancedParens
897 *
898 * StringWithBalancedParens ::=
899 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
900 * [VC: Parenthesis escaping]
901 *
902 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
903 *
904 * VC: Parenthesis escaping:
905 * The end of an XPointer part is signaled by the right parenthesis ")"
906 * character that is balanced with the left parenthesis "(" character
907 * that began the part. Any unbalanced parenthesis character inside the
908 * expression, even within literals, must be escaped with a circumflex (^)
909 * character preceding it. If the expression contains any literal
910 * occurrences of the circumflex, each must be escaped with an additional
911 * circumflex (that is, ^^). If the unescaped parentheses in the expression
912 * are not balanced, a syntax error results.
913 *
914 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
915 * string and if the scheme is 'xpointer' it will call the XPath interprter.
916 *
917 * TODO: there is no new scheme registration mechanism
918 */
919
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000920static void
Owen Taylor3473f882001-02-23 17:55:21 +0000921xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
922 xmlChar *buffer, *cur;
923 int len;
924 int level;
925
926 if (name == NULL)
927 name = xmlXPathParseName(ctxt);
928 if (name == NULL)
929 XP_ERROR(XPATH_EXPR_ERROR);
930
931 if (CUR != '(')
932 XP_ERROR(XPATH_EXPR_ERROR);
933 NEXT;
934 level = 1;
935
936 len = xmlStrlen(ctxt->cur);
937 len++;
938 buffer = (xmlChar *) xmlMalloc(len * sizeof (xmlChar));
939 if (buffer == NULL) {
940 xmlGenericError(xmlGenericErrorContext,
941 "xmlXPtrEvalXPtrPart: out of memory\n");
942 return;
943 }
944
945 cur = buffer;
946 while (CUR != 0) {
947 if (CUR == ')') {
948 level--;
949 if (level == 0) {
950 NEXT;
951 break;
952 }
953 *cur++ = CUR;
954 } else if (CUR == '(') {
955 level++;
956 *cur++ = CUR;
957 } else if (CUR == '^') {
958 NEXT;
959 if ((CUR == ')') || (CUR == '(') || (CUR == '^')) {
960 *cur++ = CUR;
961 } else {
962 *cur++ = '^';
963 *cur++ = CUR;
964 }
965 } else {
966 *cur++ = CUR;
967 }
968 NEXT;
969 }
970 *cur = 0;
971
972 if ((level != 0) && (CUR == 0)) {
973 xmlFree(buffer);
974 XP_ERROR(XPTR_SYNTAX_ERROR);
975 }
976
977 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
978 const xmlChar *left = CUR_PTR;
979
980 CUR_PTR = buffer;
Owen Taylor3473f882001-02-23 17:55:21 +0000981 xmlXPathEvalExpr(ctxt);
982 CUR_PTR=left;
983#ifdef XPTR_XMLNS_SCHEME
984 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
985 const xmlChar *left = CUR_PTR;
986 xmlChar *prefix;
987 xmlChar *URI;
988 xmlURIPtr value;
989
990 CUR_PTR = buffer;
991 prefix = xmlXPathParseNCName(ctxt);
992 if (prefix == NULL) {
993 xmlFree(buffer);
994 xmlFree(name);
995 XP_ERROR(XPTR_SYNTAX_ERROR);
996 }
997 SKIP_BLANKS;
998 if (CUR != '=') {
999 xmlFree(prefix);
1000 xmlFree(buffer);
1001 xmlFree(name);
1002 XP_ERROR(XPTR_SYNTAX_ERROR);
1003 }
1004 NEXT;
1005 SKIP_BLANKS;
1006 /* @@ check escaping in the XPointer WD */
1007
1008 value = xmlParseURI((const char *)ctxt->cur);
1009 if (value == NULL) {
1010 xmlFree(prefix);
1011 xmlFree(buffer);
1012 xmlFree(name);
1013 XP_ERROR(XPTR_SYNTAX_ERROR);
1014 }
1015 URI = xmlSaveUri(value);
1016 xmlFreeURI(value);
1017 if (URI == NULL) {
1018 xmlFree(prefix);
1019 xmlFree(buffer);
1020 xmlFree(name);
1021 XP_ERROR(XPATH_MEMORY_ERROR);
1022 }
1023
1024 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1025 CUR_PTR = left;
1026#endif /* XPTR_XMLNS_SCHEME */
1027 } else {
1028 xmlGenericError(xmlGenericErrorContext,
1029 "unsupported scheme '%s'\n", name);
1030 }
1031 xmlFree(buffer);
1032 xmlFree(name);
1033}
1034
1035/**
1036 * xmlXPtrEvalFullXPtr:
1037 * @ctxt: the XPointer Parser context
1038 * @name: the preparsed Scheme for the first XPtrPart
1039 *
1040 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1041 *
1042 * As the specs says:
1043 * -----------
1044 * When multiple XPtrParts are provided, they must be evaluated in
1045 * left-to-right order. If evaluation of one part fails, the nexti
1046 * is evaluated. The following conditions cause XPointer part failure:
1047 *
1048 * - An unknown scheme
1049 * - A scheme that does not locate any sub-resource present in the resource
1050 * - A scheme that is not applicable to the media type of the resource
1051 *
1052 * The XPointer application must consume a failed XPointer part and
1053 * attempt to evaluate the next one, if any. The result of the first
1054 * XPointer part whose evaluation succeeds is taken to be the fragment
1055 * located by the XPointer as a whole. If all the parts fail, the result
1056 * for the XPointer as a whole is a sub-resource error.
1057 * -----------
1058 *
1059 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1060 * expressions or other shemes.
1061 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001062static void
Owen Taylor3473f882001-02-23 17:55:21 +00001063xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1064 if (name == NULL)
1065 name = xmlXPathParseName(ctxt);
1066 if (name == NULL)
1067 XP_ERROR(XPATH_EXPR_ERROR);
1068 while (name != NULL) {
1069 xmlXPtrEvalXPtrPart(ctxt, name);
1070
1071 /* in case of syntax error, break here */
1072 if (ctxt->error != XPATH_EXPRESSION_OK)
1073 return;
1074
1075 /*
1076 * If the returned value is a non-empty nodeset
1077 * or location set, return here.
1078 */
1079 if (ctxt->value != NULL) {
1080 xmlXPathObjectPtr obj = ctxt->value;
1081
1082 switch (obj->type) {
1083 case XPATH_LOCATIONSET: {
1084 xmlLocationSetPtr loc = ctxt->value->user;
1085 if ((loc != NULL) && (loc->locNr > 0))
1086 return;
1087 break;
1088 }
1089 case XPATH_NODESET: {
1090 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1091 if ((loc != NULL) && (loc->nodeNr > 0))
1092 return;
1093 break;
1094 }
1095 default:
1096 break;
1097 }
1098
1099 /*
1100 * Evaluating to improper values is equivalent to
1101 * a sub-resource error, clean-up the stack
1102 */
1103 do {
1104 obj = valuePop(ctxt);
1105 if (obj != NULL) {
1106 xmlXPathFreeObject(obj);
1107 }
1108 } while (obj != NULL);
1109 }
1110
1111 /*
1112 * Is there another XPoointer part.
1113 */
1114 SKIP_BLANKS;
1115 name = xmlXPathParseName(ctxt);
1116 }
1117}
1118
1119/**
1120 * xmlXPtrEvalChildSeq:
1121 * @ctxt: the XPointer Parser context
1122 * @name: a possible ID name of the child sequence
1123 *
1124 * ChildSeq ::= '/1' ('/' [0-9]*)*
1125 * | Name ('/' [0-9]*)+
1126 *
1127 * Parse and evaluate a Child Sequence. This routine also handle the
1128 * case of a Bare Name used to get a document ID.
1129 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001130static void
Owen Taylor3473f882001-02-23 17:55:21 +00001131xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1132 /*
1133 * XPointer don't allow by syntax to adress in mutirooted trees
1134 * this might prove useful in some cases, warn about it.
1135 */
1136 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
1137 xmlGenericError(xmlGenericErrorContext,
1138 "warning: ChildSeq not starting by /1\n");
1139 }
1140
1141 if (name != NULL) {
1142 valuePush(ctxt, xmlXPathNewString(name));
1143 xmlFree(name);
1144 xmlXPathIdFunction(ctxt, 1);
1145 CHECK_ERROR;
1146 }
1147
1148 while (CUR == '/') {
1149 int child = 0;
1150 NEXT;
1151
1152 while ((CUR >= '0') && (CUR <= '9')) {
1153 child = child * 10 + (CUR - '0');
1154 NEXT;
1155 }
1156 xmlXPtrGetChildNo(ctxt, child);
1157 }
1158}
1159
1160
1161/**
1162 * xmlXPtrEvalXPointer:
1163 * @ctxt: the XPointer Parser context
1164 *
1165 * XPointer ::= Name
1166 * | ChildSeq
1167 * | FullXPtr
1168 *
1169 * Parse and evaluate an XPointer
1170 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001171static void
Owen Taylor3473f882001-02-23 17:55:21 +00001172xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001173 if (ctxt->valueTab == NULL) {
1174 /* Allocate the value stack */
1175 ctxt->valueTab = (xmlXPathObjectPtr *)
1176 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1177 if (ctxt->valueTab == NULL) {
1178 xmlFree(ctxt);
1179 xmlGenericError(xmlGenericErrorContext,
1180 "xmlXPathRunEval: out of memory\n");
1181 return;
1182 }
1183 ctxt->valueNr = 0;
1184 ctxt->valueMax = 10;
1185 ctxt->value = NULL;
1186 }
Owen Taylor3473f882001-02-23 17:55:21 +00001187 SKIP_BLANKS;
1188 if (CUR == '/') {
1189 xmlXPathRoot(ctxt);
1190 xmlXPtrEvalChildSeq(ctxt, NULL);
1191 } else {
1192 xmlChar *name;
1193
1194 name = xmlXPathParseName(ctxt);
1195 if (name == NULL)
1196 XP_ERROR(XPATH_EXPR_ERROR);
1197 if (CUR == '(') {
1198 xmlXPtrEvalFullXPtr(ctxt, name);
1199 /* Short evaluation */
1200 return;
1201 } else {
1202 /* this handle both Bare Names and Child Sequences */
1203 xmlXPtrEvalChildSeq(ctxt, name);
1204 }
1205 }
1206 SKIP_BLANKS;
1207 if (CUR != 0)
1208 XP_ERROR(XPATH_EXPR_ERROR);
1209}
1210
1211
1212/************************************************************************
1213 * *
1214 * General routines *
1215 * *
1216 ************************************************************************/
1217
1218void xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs);
1219void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1220void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1221void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1222void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1223void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1224void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1225void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1226
1227/**
1228 * xmlXPtrNewContext:
1229 * @doc: the XML document
1230 * @here: the node that directly contains the XPointer being evaluated or NULL
1231 * @origin: the element from which a user or program initiated traversal of
1232 * the link, or NULL.
1233 *
1234 * Create a new XPointer context
1235 *
1236 * Returns the xmlXPathContext just allocated.
1237 */
1238xmlXPathContextPtr
1239xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1240 xmlXPathContextPtr ret;
1241
1242 ret = xmlXPathNewContext(doc);
1243 if (ret == NULL)
1244 return(ret);
1245 ret->xptr = 1;
1246 ret->here = here;
1247 ret->origin = origin;
1248
1249 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1250 xmlXPtrRangeToFunction);
1251 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1252 xmlXPtrRangeFunction);
1253 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1254 xmlXPtrRangeInsideFunction);
1255 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1256 xmlXPtrStringRangeFunction);
1257 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1258 xmlXPtrStartPointFunction);
1259 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1260 xmlXPtrEndPointFunction);
1261 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1262 xmlXPtrHereFunction);
1263 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1264 xmlXPtrOriginFunction);
1265
1266 return(ret);
1267}
1268
1269/**
1270 * xmlXPtrEval:
1271 * @str: the XPointer expression
1272 * @ctx: the XPointer context
1273 *
1274 * Evaluate the XPath Location Path in the given context.
1275 *
1276 * Returns the xmlXPathObjectPtr resulting from the eveluation or NULL.
1277 * the caller has to free the object.
1278 */
1279xmlXPathObjectPtr
1280xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1281 xmlXPathParserContextPtr ctxt;
1282 xmlXPathObjectPtr res = NULL, tmp;
1283 xmlXPathObjectPtr init = NULL;
1284 int stack = 0;
1285
1286 xmlXPathInit();
1287
1288 if ((ctx == NULL) || (str == NULL))
1289 return(NULL);
1290
1291 ctxt = xmlXPathNewParserContext(str, ctx);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001292 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001293 xmlXPtrEvalXPointer(ctxt);
1294
1295 if ((ctxt->value != NULL) &&
1296 (ctxt->value->type != XPATH_NODESET) &&
1297 (ctxt->value->type != XPATH_LOCATIONSET)) {
1298 xmlGenericError(xmlGenericErrorContext,
1299 "xmlXPtrEval: evaluation failed to return a node set\n");
1300 } else {
1301 res = valuePop(ctxt);
1302 }
1303
1304 do {
1305 tmp = valuePop(ctxt);
1306 if (tmp != NULL) {
1307 if (tmp != init) {
1308 if (tmp->type == XPATH_NODESET) {
1309 /*
1310 * Evaluation may push a root nodeset which is unused
1311 */
1312 xmlNodeSetPtr set;
1313 set = tmp->nodesetval;
1314 if ((set->nodeNr != 1) ||
1315 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1316 stack++;
1317 } else
1318 stack++;
1319 }
1320 xmlXPathFreeObject(tmp);
1321 }
1322 } while (tmp != NULL);
1323 if (stack != 0) {
1324 xmlGenericError(xmlGenericErrorContext,
1325 "xmlXPtrEval: %d object left on the stack\n",
1326 stack);
1327 }
1328 if (ctxt->error != XPATH_EXPRESSION_OK) {
1329 xmlXPathFreeObject(res);
1330 res = NULL;
1331 }
1332
1333 xmlXPathFreeParserContext(ctxt);
1334 return(res);
1335}
1336
1337/**
1338 * xmlXPtrBuildRangeNodeList:
1339 * @range: a range object
1340 *
1341 * Build a node list tree copy of the range
1342 *
1343 * Returns an xmlNodePtr list or NULL.
1344 * the caller has to free the node tree.
1345 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001346static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001347xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1348 /* pointers to generated nodes */
1349 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1350 /* pointers to traversal nodes */
1351 xmlNodePtr start, cur, end;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001352 int index1, index2;
Owen Taylor3473f882001-02-23 17:55:21 +00001353
1354 if (range == NULL)
1355 return(NULL);
1356 if (range->type != XPATH_RANGE)
1357 return(NULL);
1358 start = (xmlNodePtr) range->user;
1359
1360 if (start == NULL)
1361 return(NULL);
1362 end = range->user2;
1363 if (end == NULL)
1364 return(xmlCopyNode(start, 1));
1365
1366 cur = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001367 index1 = range->index;
Owen Taylor3473f882001-02-23 17:55:21 +00001368 index2 = range->index2;
1369 while (cur != NULL) {
1370 if (cur == end) {
1371 if (cur->type == XML_TEXT_NODE) {
1372 const xmlChar *content = cur->content;
1373 int len;
1374
1375 if (content == NULL) {
1376 tmp = xmlNewTextLen(NULL, 0);
1377 } else {
1378 len = index2;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001379 if ((cur == start) && (index1 > 1)) {
1380 content += (index1 - 1);
1381 len -= (index1 - 1);
1382 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001383 } else {
1384 len = index2;
1385 }
1386 tmp = xmlNewTextLen(content, len);
1387 }
1388 /* single sub text node selection */
1389 if (list == NULL)
1390 return(tmp);
1391 /* prune and return full set */
1392 if (last != NULL)
1393 xmlAddNextSibling(last, tmp);
1394 else
1395 xmlAddChild(parent, tmp);
1396 return(list);
1397 } else {
1398 tmp = xmlCopyNode(cur, 0);
1399 if (list == NULL)
1400 list = tmp;
1401 else {
1402 if (last != NULL)
1403 xmlAddNextSibling(last, tmp);
1404 else
1405 xmlAddChild(parent, tmp);
1406 }
1407 last = NULL;
1408 parent = tmp;
1409
1410 if (index2 > 1) {
1411 end = xmlXPtrGetNthChild(cur, index2 - 1);
1412 index2 = 0;
1413 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001414 if ((cur == start) && (index1 > 1)) {
1415 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1416 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001417 } else {
1418 cur = cur->children;
1419 }
1420 /*
1421 * Now gather the remaining nodes from cur to end
1422 */
1423 continue; /* while */
1424 }
1425 } else if ((cur == start) &&
1426 (list == NULL) /* looks superfluous but ... */ ) {
1427 if (cur->type == XML_TEXT_NODE) {
1428 const xmlChar *content = cur->content;
1429
1430 if (content == NULL) {
1431 tmp = xmlNewTextLen(NULL, 0);
1432 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001433 if (index1 > 1) {
1434 content += (index1 - 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001435 }
1436 tmp = xmlNewText(content);
1437 }
1438 last = list = tmp;
1439 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001440 if ((cur == start) && (index1 > 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001441 tmp = xmlCopyNode(cur, 0);
1442 list = tmp;
1443 parent = tmp;
1444 last = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001445 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1446 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001447 /*
1448 * Now gather the remaining nodes from cur to end
1449 */
1450 continue; /* while */
1451 }
1452 tmp = xmlCopyNode(cur, 1);
1453 list = tmp;
1454 parent = NULL;
1455 last = tmp;
1456 }
1457 } else {
1458 tmp = NULL;
1459 switch (cur->type) {
1460 case XML_DTD_NODE:
1461 case XML_ELEMENT_DECL:
1462 case XML_ATTRIBUTE_DECL:
1463 case XML_ENTITY_NODE:
1464 /* Do not copy DTD informations */
1465 break;
1466 case XML_ENTITY_DECL:
1467 TODO /* handle csossing entities -> stack needed */
1468 break;
1469 case XML_XINCLUDE_START:
1470 case XML_XINCLUDE_END:
1471 /* don't consider it part of the tree content */
1472 break;
1473 case XML_ATTRIBUTE_NODE:
1474 /* Humm, should not happen ! */
1475 STRANGE
1476 break;
1477 default:
1478 tmp = xmlCopyNode(cur, 1);
1479 break;
1480 }
1481 if (tmp != NULL) {
1482 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1483 STRANGE
1484 return(NULL);
1485 }
1486 if (last != NULL)
1487 xmlAddNextSibling(last, tmp);
1488 else {
1489 xmlAddChild(parent, tmp);
1490 last = tmp;
1491 }
1492 }
1493 }
1494 /*
1495 * Skip to next node in document order
1496 */
1497 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1498 STRANGE
1499 return(NULL);
1500 }
1501 cur = xmlXPtrAdvanceNode(cur);
1502 }
1503 return(list);
1504}
1505
1506/**
1507 * xmlXPtrBuildNodeList:
1508 * @obj: the XPointer result from the evaluation.
1509 *
1510 * Build a node list tree copy of the XPointer result.
1511 *
1512 * Returns an xmlNodePtr list or NULL.
1513 * the caller has to free the node tree.
1514 */
1515xmlNodePtr
1516xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1517 xmlNodePtr list = NULL, last = NULL;
1518 int i;
1519
1520 if (obj == NULL)
1521 return(NULL);
1522 switch (obj->type) {
1523 case XPATH_NODESET: {
1524 xmlNodeSetPtr set = obj->nodesetval;
1525 if (set == NULL)
1526 return(NULL);
1527 for (i = 0;i < set->nodeNr;i++) {
1528 if (last == NULL)
1529 list = last = xmlCopyNode(set->nodeTab[i], 1);
1530 else {
1531 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1532 if (last->next != NULL)
1533 last = last->next;
1534 }
1535 }
1536 break;
1537 }
1538 case XPATH_LOCATIONSET: {
1539 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1540 if (set == NULL)
1541 return(NULL);
1542 for (i = 0;i < set->locNr;i++) {
1543 if (last == NULL)
1544 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1545 else
1546 xmlAddNextSibling(last,
1547 xmlXPtrBuildNodeList(set->locTab[i]));
1548 if (last != NULL) {
1549 while (last->next != NULL)
1550 last = last->next;
1551 }
1552 }
1553 break;
1554 }
1555 case XPATH_RANGE:
1556 return(xmlXPtrBuildRangeNodeList(obj));
1557 case XPATH_POINT:
1558 return(xmlCopyNode(obj->user, 0));
1559 default:
1560 break;
1561 }
1562 return(list);
1563}
1564
1565/************************************************************************
1566 * *
1567 * XPointer functions *
1568 * *
1569 ************************************************************************/
1570
1571/**
1572 * xmlXPtrNbLocChildren:
1573 * @node: an xmlNodePtr
1574 *
1575 * Count the number of location children of @node or the lenght of the
1576 * string value in case of text/PI/Comments nodes
1577 *
1578 * Returns the number of location children
1579 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001580static int
Owen Taylor3473f882001-02-23 17:55:21 +00001581xmlXPtrNbLocChildren(xmlNodePtr node) {
1582 int ret = 0;
1583 if (node == NULL)
1584 return(-1);
1585 switch (node->type) {
1586 case XML_HTML_DOCUMENT_NODE:
1587 case XML_DOCUMENT_NODE:
1588 case XML_ELEMENT_NODE:
1589 node = node->children;
1590 while (node != NULL) {
1591 if (node->type == XML_ELEMENT_NODE)
1592 ret++;
1593 node = node->next;
1594 }
1595 break;
1596 case XML_ATTRIBUTE_NODE:
1597 return(-1);
1598
1599 case XML_PI_NODE:
1600 case XML_COMMENT_NODE:
1601 case XML_TEXT_NODE:
1602 case XML_CDATA_SECTION_NODE:
1603 case XML_ENTITY_REF_NODE:
1604#ifndef XML_USE_BUFFER_CONTENT
1605 ret = xmlStrlen(node->content);
1606#else
1607 ret = xmlBufferLength(node->content);
1608#endif
1609 break;
1610 default:
1611 return(-1);
1612 }
1613 return(ret);
1614}
1615
1616/**
1617 * xmlXPtrHereFunction:
1618 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001619 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001620 *
1621 * Function implementing here() operation
1622 * as described in 5.4.3
1623 */
1624void
1625xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001626 CHECK_ARITY(0);
1627
Owen Taylor3473f882001-02-23 17:55:21 +00001628 if (ctxt->context->here == NULL)
1629 XP_ERROR(XPTR_SYNTAX_ERROR);
1630
1631 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1632}
1633
1634/**
1635 * xmlXPtrOriginFunction:
1636 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001637 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001638 *
1639 * Function implementing origin() operation
1640 * as described in 5.4.3
1641 */
1642void
1643xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001644 CHECK_ARITY(0);
1645
Owen Taylor3473f882001-02-23 17:55:21 +00001646 if (ctxt->context->origin == NULL)
1647 XP_ERROR(XPTR_SYNTAX_ERROR);
1648
1649 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1650}
1651
1652/**
1653 * xmlXPtrStartPointFunction:
1654 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001655 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001656 *
1657 * Function implementing start-point() operation
1658 * as described in 5.4.3
1659 * ----------------
1660 * location-set start-point(location-set)
1661 *
1662 * For each location x in the argument location-set, start-point adds a
1663 * location of type point to the result location-set. That point represents
1664 * the start point of location x and is determined by the following rules:
1665 *
1666 * - If x is of type point, the start point is x.
1667 * - If x is of type range, the start point is the start point of x.
1668 * - If x is of type root, element, text, comment, or processing instruction,
1669 * - the container node of the start point is x and the index is 0.
1670 * - If x is of type attribute or namespace, the function must signal a
1671 * syntax error.
1672 * ----------------
1673 *
1674 */
1675void
1676xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1677 xmlXPathObjectPtr tmp, obj, point;
1678 xmlLocationSetPtr newset = NULL;
1679 xmlLocationSetPtr oldset = NULL;
1680
1681 CHECK_ARITY(1);
1682 if ((ctxt->value == NULL) ||
1683 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1684 (ctxt->value->type != XPATH_NODESET)))
1685 XP_ERROR(XPATH_INVALID_TYPE)
1686
1687 obj = valuePop(ctxt);
1688 if (obj->type == XPATH_NODESET) {
1689 /*
1690 * First convert to a location set
1691 */
1692 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1693 xmlXPathFreeObject(obj);
1694 obj = tmp;
1695 }
1696
1697 newset = xmlXPtrLocationSetCreate(NULL);
1698 if (newset == NULL) {
1699 xmlXPathFreeObject(obj);
1700 XP_ERROR(XPATH_MEMORY_ERROR);
1701 }
1702 oldset = (xmlLocationSetPtr) obj->user;
1703 if (oldset != NULL) {
1704 int i;
1705
1706 for (i = 0; i < oldset->locNr; i++) {
1707 tmp = oldset->locTab[i];
1708 if (tmp == NULL)
1709 continue;
1710 point = NULL;
1711 switch (tmp->type) {
1712 case XPATH_POINT:
1713 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1714 break;
1715 case XPATH_RANGE: {
1716 xmlNodePtr node = tmp->user;
1717 if (node != NULL) {
1718 if (node->type == XML_ATTRIBUTE_NODE) {
1719 /* TODO: Namespace Nodes ??? */
1720 xmlXPathFreeObject(obj);
1721 xmlXPtrFreeLocationSet(newset);
1722 XP_ERROR(XPTR_SYNTAX_ERROR);
1723 }
1724 point = xmlXPtrNewPoint(node, tmp->index);
1725 }
1726 break;
1727 }
1728 default:
1729 /*** Should we raise an error ?
1730 xmlXPathFreeObject(obj);
1731 xmlXPathFreeObject(newset);
1732 XP_ERROR(XPATH_INVALID_TYPE)
1733 ***/
1734 break;
1735 }
1736 if (point != NULL)
1737 xmlXPtrLocationSetAdd(newset, point);
1738 }
1739 }
1740 xmlXPathFreeObject(obj);
1741 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1742}
1743
1744/**
1745 * xmlXPtrEndPointFunction:
1746 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001747 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001748 *
1749 * Function implementing end-point() operation
1750 * as described in 5.4.3
1751 * ----------------------------
1752 * location-set end-point(location-set)
1753 *
1754 * For each location x in the argument location-set, end-point adds a
1755 * location of type point to the result location-set. That point representsi
1756 * the end point of location x and is determined by the following rules:
1757 *
1758 * - If x is of type point, the resulting point is x.
1759 * - If x is of type range, the resulting point is the end point of x.
1760 * - If x is of type root or element, the container node of the resulting
1761 * point is x and the index is the number of location children of x.
1762 * - If x is of type text, comment, or processing instruction, the container
1763 * node of the resulting point is x and the index is the length of thei
1764 * string-value of x.
1765 * - If x is of type attribute or namespace, the function must signal a
1766 * syntax error.
1767 * ----------------------------
1768 */
1769void
1770xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1771 xmlXPathObjectPtr tmp, obj, point;
1772 xmlLocationSetPtr newset = NULL;
1773 xmlLocationSetPtr oldset = NULL;
1774
1775 CHECK_ARITY(1);
1776 if ((ctxt->value == NULL) ||
1777 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1778 (ctxt->value->type != XPATH_NODESET)))
1779 XP_ERROR(XPATH_INVALID_TYPE)
1780
1781 obj = valuePop(ctxt);
1782 if (obj->type == XPATH_NODESET) {
1783 /*
1784 * First convert to a location set
1785 */
1786 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1787 xmlXPathFreeObject(obj);
1788 obj = tmp;
1789 }
1790
1791 newset = xmlXPtrLocationSetCreate(NULL);
1792 oldset = (xmlLocationSetPtr) obj->user;
1793 if (oldset != NULL) {
1794 int i;
1795
1796 for (i = 0; i < oldset->locNr; i++) {
1797 tmp = oldset->locTab[i];
1798 if (tmp == NULL)
1799 continue;
1800 point = NULL;
1801 switch (tmp->type) {
1802 case XPATH_POINT:
1803 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1804 break;
1805 case XPATH_RANGE: {
1806 xmlNodePtr node = tmp->user2;
1807 if (node != NULL) {
1808 if (node->type == XML_ATTRIBUTE_NODE) {
1809 /* TODO: Namespace Nodes ??? */
1810 xmlXPathFreeObject(obj);
1811 xmlXPtrFreeLocationSet(newset);
1812 XP_ERROR(XPTR_SYNTAX_ERROR);
1813 }
1814 point = xmlXPtrNewPoint(node, tmp->index2);
1815 } else if (tmp->user == NULL) {
1816 point = xmlXPtrNewPoint(node,
1817 xmlXPtrNbLocChildren(node));
1818 }
1819 break;
1820 }
1821 default:
1822 /*** Should we raise an error ?
1823 xmlXPathFreeObject(obj);
1824 xmlXPathFreeObject(newset);
1825 XP_ERROR(XPATH_INVALID_TYPE)
1826 ***/
1827 break;
1828 }
1829 if (point != NULL)
1830 xmlXPtrLocationSetAdd(newset, point);
1831 }
1832 }
1833 xmlXPathFreeObject(obj);
1834 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1835}
1836
1837
1838/**
1839 * xmlXPtrCoveringRange:
1840 * @ctxt: the XPointer Parser context
1841 * @loc: the location for which the covering range must be computed
1842 *
1843 * A covering range is a range that wholly encompasses a location
1844 * Section 5.3.3. Covering Ranges for All Location Types
1845 * http://www.w3.org/TR/xptr#N2267
1846 *
1847 * Returns a new location or NULL in case of error
1848 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001849static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001850xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1851 if (loc == NULL)
1852 return(NULL);
1853 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1854 (ctxt->context->doc == NULL))
1855 return(NULL);
1856 switch (loc->type) {
1857 case XPATH_POINT:
1858 return(xmlXPtrNewRange(loc->user, loc->index,
1859 loc->user, loc->index));
1860 case XPATH_RANGE:
1861 if (loc->user2 != NULL) {
1862 return(xmlXPtrNewRange(loc->user, loc->index,
1863 loc->user2, loc->index2));
1864 } else {
1865 xmlNodePtr node = (xmlNodePtr) loc->user;
1866 if (node == (xmlNodePtr) ctxt->context->doc) {
1867 return(xmlXPtrNewRange(node, 0, node,
1868 xmlXPtrGetArity(node)));
1869 } else {
1870 switch (node->type) {
1871 case XML_ATTRIBUTE_NODE:
1872 /* !!! our model is slightly different than XPath */
1873 return(xmlXPtrNewRange(node, 0, node,
1874 xmlXPtrGetArity(node)));
1875 case XML_ELEMENT_NODE:
1876 case XML_TEXT_NODE:
1877 case XML_CDATA_SECTION_NODE:
1878 case XML_ENTITY_REF_NODE:
1879 case XML_PI_NODE:
1880 case XML_COMMENT_NODE:
1881 case XML_DOCUMENT_NODE:
1882 case XML_NOTATION_NODE:
1883 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001884 int indx = xmlXPtrGetIndex(node);
Owen Taylor3473f882001-02-23 17:55:21 +00001885
1886 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001887 return(xmlXPtrNewRange(node, indx - 1,
1888 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00001889 }
1890 default:
1891 return(NULL);
1892 }
1893 }
1894 }
1895 default:
1896 TODO /* missed one case ??? */
1897 }
1898 return(NULL);
1899}
1900
1901/**
1902 * xmlXPtrRangeFunction:
1903 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001904 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001905 *
1906 * Function implementing the range() function 5.4.3
1907 * location-set range(location-set )
1908 *
1909 * The range function returns ranges covering the locations in
1910 * the argument location-set. For each location x in the argument
1911 * location-set, a range location representing the covering range of
1912 * x is added to the result location-set.
1913 */
1914void
1915xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1916 int i;
1917 xmlXPathObjectPtr set;
1918 xmlLocationSetPtr oldset;
1919 xmlLocationSetPtr newset;
1920
1921 CHECK_ARITY(1);
1922 if ((ctxt->value == NULL) ||
1923 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1924 (ctxt->value->type != XPATH_NODESET)))
1925 XP_ERROR(XPATH_INVALID_TYPE)
1926
1927 set = valuePop(ctxt);
1928 if (set->type == XPATH_NODESET) {
1929 xmlXPathObjectPtr tmp;
1930
1931 /*
1932 * First convert to a location set
1933 */
1934 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1935 xmlXPathFreeObject(set);
1936 set = tmp;
1937 }
1938 oldset = (xmlLocationSetPtr) set->user;
1939
1940 /*
1941 * The loop is to compute the covering range for each item and add it
1942 */
1943 newset = xmlXPtrLocationSetCreate(NULL);
1944 for (i = 0;i < oldset->locNr;i++) {
1945 xmlXPtrLocationSetAdd(newset,
1946 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
1947 }
1948
1949 /*
1950 * Save the new value and cleanup
1951 */
1952 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1953 xmlXPathFreeObject(set);
1954}
1955
1956/**
1957 * xmlXPtrInsideRange:
1958 * @ctxt: the XPointer Parser context
1959 * @loc: the location for which the inside range must be computed
1960 *
1961 * A inside range is a range described in the range-inside() description
1962 *
1963 * Returns a new location or NULL in case of error
1964 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001965static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001966xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1967 if (loc == NULL)
1968 return(NULL);
1969 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1970 (ctxt->context->doc == NULL))
1971 return(NULL);
1972 switch (loc->type) {
1973 case XPATH_POINT: {
1974 xmlNodePtr node = (xmlNodePtr) loc->user;
1975 switch (node->type) {
1976 case XML_PI_NODE:
1977 case XML_COMMENT_NODE:
1978 case XML_TEXT_NODE:
1979 case XML_CDATA_SECTION_NODE: {
1980 if (node->content == NULL) {
1981 return(xmlXPtrNewRange(node, 0, node, 0));
1982 } else {
1983#ifndef XML_USE_BUFFER_CONTENT
1984 return(xmlXPtrNewRange(node, 0, node,
1985 xmlStrlen(node->content)));
1986#else
1987 return(xmlXPtrNewRange(node, 0, node,
1988 xmlBufferLength(node->content)));
1989#endif
1990 }
1991 }
1992 case XML_ATTRIBUTE_NODE:
1993 case XML_ELEMENT_NODE:
1994 case XML_ENTITY_REF_NODE:
1995 case XML_DOCUMENT_NODE:
1996 case XML_NOTATION_NODE:
1997 case XML_HTML_DOCUMENT_NODE: {
1998 return(xmlXPtrNewRange(node, 0, node,
1999 xmlXPtrGetArity(node)));
2000 }
2001 default:
2002 return(NULL);
2003 }
2004 return(NULL);
2005 }
2006 case XPATH_RANGE: {
2007 xmlNodePtr node = (xmlNodePtr) loc->user;
2008 if (loc->user2 != NULL) {
2009 return(xmlXPtrNewRange(node, loc->index,
2010 loc->user2, loc->index2));
2011 } else {
2012 switch (node->type) {
2013 case XML_PI_NODE:
2014 case XML_COMMENT_NODE:
2015 case XML_TEXT_NODE:
2016 case XML_CDATA_SECTION_NODE: {
2017 if (node->content == NULL) {
2018 return(xmlXPtrNewRange(node, 0, node, 0));
2019 } else {
2020#ifndef XML_USE_BUFFER_CONTENT
2021 return(xmlXPtrNewRange(node, 0, node,
2022 xmlStrlen(node->content)));
2023#else
2024 return(xmlXPtrNewRange(node, 0, node,
2025 xmlBufferLength(node->content)));
2026#endif
2027 }
2028 }
2029 case XML_ATTRIBUTE_NODE:
2030 case XML_ELEMENT_NODE:
2031 case XML_ENTITY_REF_NODE:
2032 case XML_DOCUMENT_NODE:
2033 case XML_NOTATION_NODE:
2034 case XML_HTML_DOCUMENT_NODE: {
2035 return(xmlXPtrNewRange(node, 0, node,
2036 xmlXPtrGetArity(node)));
2037 }
2038 default:
2039 return(NULL);
2040 }
2041 return(NULL);
2042 }
2043 }
2044 default:
2045 TODO /* missed one case ??? */
2046 }
2047 return(NULL);
2048}
2049
2050/**
2051 * xmlXPtrRangeInsideFunction:
2052 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002053 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002054 *
2055 * Function implementing the range-inside() function 5.4.3
2056 * location-set range-inside(location-set )
2057 *
2058 * The range-inside function returns ranges covering the contents of
2059 * the locations in the argument location-set. For each location x in
2060 * the argument location-set, a range location is added to the result
2061 * location-set. If x is a range location, then x is added to the
2062 * result location-set. If x is not a range location, then x is used
2063 * as the container location of the start and end points of the range
2064 * location to be added; the index of the start point of the range is
2065 * zero; if the end point is a character point then its index is the
2066 * length of the string-value of x, and otherwise is the number of
2067 * location children of x.
2068 *
2069 */
2070void
2071xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2072 int i;
2073 xmlXPathObjectPtr set;
2074 xmlLocationSetPtr oldset;
2075 xmlLocationSetPtr newset;
2076
2077 CHECK_ARITY(1);
2078 if ((ctxt->value == NULL) ||
2079 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2080 (ctxt->value->type != XPATH_NODESET)))
2081 XP_ERROR(XPATH_INVALID_TYPE)
2082
2083 set = valuePop(ctxt);
2084 if (set->type == XPATH_NODESET) {
2085 xmlXPathObjectPtr tmp;
2086
2087 /*
2088 * First convert to a location set
2089 */
2090 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2091 xmlXPathFreeObject(set);
2092 set = tmp;
2093 }
2094 oldset = (xmlLocationSetPtr) set->user;
2095
2096 /*
2097 * The loop is to compute the covering range for each item and add it
2098 */
2099 newset = xmlXPtrLocationSetCreate(NULL);
2100 for (i = 0;i < oldset->locNr;i++) {
2101 xmlXPtrLocationSetAdd(newset,
2102 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2103 }
2104
2105 /*
2106 * Save the new value and cleanup
2107 */
2108 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2109 xmlXPathFreeObject(set);
2110}
2111
2112/**
2113 * xmlXPtrRangeToFunction:
2114 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002115 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002116 *
2117 * Implement the range-to() XPointer function
2118 */
2119void
2120xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2121 xmlXPathObjectPtr range;
2122 const xmlChar *cur;
2123 xmlXPathObjectPtr res, obj;
2124 xmlXPathObjectPtr tmp;
2125 xmlLocationSetPtr newset = NULL;
2126 xmlNodeSetPtr oldset;
2127 int i;
2128
2129 CHECK_ARITY(1);
2130 /*
2131 * Save the expression pointer since we will have to evaluate
2132 * it multiple times. Initialize the new set.
2133 */
2134 CHECK_TYPE(XPATH_NODESET);
2135 obj = valuePop(ctxt);
2136 oldset = obj->nodesetval;
2137 ctxt->context->node = NULL;
2138
2139 cur = ctxt->cur;
2140 newset = xmlXPtrLocationSetCreate(NULL);
2141
2142 for (i = 0; i < oldset->nodeNr; i++) {
2143 ctxt->cur = cur;
2144
2145 /*
2146 * Run the evaluation with a node list made of a single item
2147 * in the nodeset.
2148 */
2149 ctxt->context->node = oldset->nodeTab[i];
2150 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2151 valuePush(ctxt, tmp);
2152
2153 xmlXPathEvalExpr(ctxt);
2154 CHECK_ERROR;
2155
2156 /*
2157 * The result of the evaluation need to be tested to
2158 * decided whether the filter succeeded or not
2159 */
2160 res = valuePop(ctxt);
2161 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2162 if (range != NULL) {
2163 xmlXPtrLocationSetAdd(newset, range);
2164 }
2165
2166 /*
2167 * Cleanup
2168 */
2169 if (res != NULL)
2170 xmlXPathFreeObject(res);
2171 if (ctxt->value == tmp) {
2172 res = valuePop(ctxt);
2173 xmlXPathFreeObject(res);
2174 }
2175
2176 ctxt->context->node = NULL;
2177 }
2178
2179 /*
2180 * The result is used as the new evaluation set.
2181 */
2182 xmlXPathFreeObject(obj);
2183 ctxt->context->node = NULL;
2184 ctxt->context->contextSize = -1;
2185 ctxt->context->proximityPosition = -1;
2186 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2187}
2188
2189/**
2190 * xmlXPtrAdvanceNode:
2191 * @cur: the node
2192 *
2193 * Advance to the next element or text node in document order
2194 * TODO: add a stack for entering/exiting entities
2195 *
2196 * Returns -1 in case of failure, 0 otherwise
2197 */
2198xmlNodePtr
2199xmlXPtrAdvanceNode(xmlNodePtr cur) {
2200next:
2201 if (cur == NULL)
2202 return(NULL);
2203 if (cur->children != NULL) {
2204 cur = cur->children ;
2205 goto found;
2206 }
2207 if (cur->next != NULL) {
2208 cur = cur->next;
2209 goto found;
2210 }
2211 do {
2212 cur = cur->parent;
2213 if (cur == NULL) return(NULL);
2214 if (cur->next != NULL) {
2215 cur = cur->next;
2216 goto found;
2217 }
2218 } while (cur != NULL);
2219
2220found:
2221 if ((cur->type != XML_ELEMENT_NODE) &&
2222 (cur->type != XML_TEXT_NODE) &&
2223 (cur->type != XML_DOCUMENT_NODE) &&
2224 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2225 (cur->type != XML_CDATA_SECTION_NODE))
2226 goto next;
2227 if (cur->type == XML_ENTITY_REF_NODE) {
2228 TODO
2229 }
2230 return(cur);
2231}
2232
2233/**
2234 * xmlXPtrAdvanceChar:
2235 * @node: the node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002236 * @indx: the indx
Owen Taylor3473f882001-02-23 17:55:21 +00002237 * @bytes: the number of bytes
2238 *
2239 * Advance a point of the associated number of bytes (not UTF8 chars)
2240 *
2241 * Returns -1 in case of failure, 0 otherwise
2242 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002243static int
2244xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Owen Taylor3473f882001-02-23 17:55:21 +00002245 xmlNodePtr cur;
2246 int pos;
2247 int len;
2248
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002249 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002250 return(-1);
2251 cur = *node;
2252 if (cur == NULL)
2253 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002254 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002255
2256 while (bytes >= 0) {
2257 /*
2258 * First position to the beginning of the first text node
2259 * corresponding to this point
2260 */
2261 while ((cur != NULL) &&
2262 ((cur->type == XML_ELEMENT_NODE) ||
2263 (cur->type == XML_DOCUMENT_NODE) ||
2264 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2265 if (pos > 0) {
2266 cur = xmlXPtrGetNthChild(cur, pos);
2267 pos = 0;
2268 } else {
2269 cur = xmlXPtrAdvanceNode(cur);
2270 pos = 0;
2271 }
2272 }
2273
2274 if (cur == NULL) {
2275 *node = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002276 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002277 return(-1);
2278 }
2279
2280 /*
2281 * if there is no move needed return the current value.
2282 */
2283 if (pos == 0) pos = 1;
2284 if (bytes == 0) {
2285 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002286 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002287 return(0);
2288 }
2289 /*
2290 * We should have a text (or cdata) node ...
2291 */
2292 len = 0;
2293 if (cur->content != NULL) {
2294#ifndef XML_USE_BUFFER_CONTENT
2295 len = xmlStrlen(cur->content);
2296#else
2297 len = xmlBufferLength(cur->content);
2298#endif
2299 }
2300 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002301 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002302 STRANGE
2303 pos = len;
2304 }
2305 if (pos + bytes >= len) {
2306 bytes -= (len - pos);
2307 cur = xmlXPtrAdvanceNode(cur);
2308 cur = 0;
2309 } else if (pos + bytes < len) {
2310 pos += bytes;
2311 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002312 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002313 return(0);
2314 }
2315 }
2316 return(-1);
2317}
2318
2319/**
2320 * xmlXPtrMatchString:
2321 * @string: the string to search
2322 * @start: the start textnode
2323 * @startindex: the start index
2324 * @end: the end textnode IN/OUT
2325 * @endindex: the end index IN/OUT
2326 *
2327 * Check whether the document contains @string at the position
2328 * (@start, @startindex) and limited by the (@end, @endindex) point
2329 *
2330 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2331 * (@start, @startindex) will indicate the position of the beginning
2332 * of the range and (@end, @endindex) will endicate the end
2333 * of the range
2334 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002335static int
Owen Taylor3473f882001-02-23 17:55:21 +00002336xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2337 xmlNodePtr *end, int *endindex) {
2338 xmlNodePtr cur;
2339 int pos; /* 0 based */
2340 int len; /* in bytes */
2341 int stringlen; /* in bytes */
2342 int match;
2343
2344 if (string == NULL)
2345 return(-1);
2346 if (start == NULL)
2347 return(-1);
2348 if ((end == NULL) || (endindex == NULL))
2349 return(-1);
2350 cur = start;
2351 if (cur == NULL)
2352 return(-1);
2353 pos = startindex - 1;
2354 stringlen = xmlStrlen(string);
2355
2356 while (stringlen > 0) {
2357 if ((cur == *end) && (pos + stringlen > *endindex))
2358 return(0);
2359 if (cur->content != NULL) {
2360#ifndef XML_USE_BUFFER_CONTENT
2361 len = xmlStrlen(cur->content);
2362#else
2363 len = xmlBufferLength(cur->content);
2364#endif
2365 if (len >= pos + stringlen) {
2366#ifndef XML_USE_BUFFER_CONTENT
2367 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2368#else
2369 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2370 string, stringlen));
2371#endif
2372 if (match) {
2373#ifdef DEBUG_RANGES
2374 xmlGenericError(xmlGenericErrorContext,
2375 "found range %d bytes at index %d of ->",
2376 stringlen, pos + 1);
2377 xmlDebugDumpString(stdout, cur->content);
2378 xmlGenericError(xmlGenericErrorContext, "\n");
2379#endif
2380 *end = cur;
2381 *endindex = pos + stringlen;
2382 return(1);
2383 } else {
2384 return(0);
2385 }
2386 } else {
2387 int sub = len - pos;
2388#ifndef XML_USE_BUFFER_CONTENT
2389 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2390#else
2391 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2392 string, sub));
2393#endif
2394 if (match) {
2395#ifdef DEBUG_RANGES
2396 xmlGenericError(xmlGenericErrorContext,
2397 "found subrange %d bytes at index %d of ->",
2398 sub, pos + 1);
2399 xmlDebugDumpString(stdout, cur->content);
2400 xmlGenericError(xmlGenericErrorContext, "\n");
2401#endif
2402 string = &string[sub];
2403 stringlen -= sub;
2404 } else {
2405 return(0);
2406 }
2407 }
2408 }
2409 cur = xmlXPtrAdvanceNode(cur);
2410 if (cur == NULL)
2411 return(0);
2412 pos = 0;
2413 }
2414 return(1);
2415}
2416
2417/**
2418 * xmlXPtrSearchString:
2419 * @string: the string to search
2420 * @start: the start textnode IN/OUT
2421 * @startindex: the start index IN/OUT
2422 * @end: the end textnode
2423 * @endindex: the end index
2424 *
2425 * Search the next occurence of @string within the document content
2426 * until the (@end, @endindex) point is reached
2427 *
2428 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2429 * (@start, @startindex) will indicate the position of the beginning
2430 * of the range and (@end, @endindex) will endicate the end
2431 * of the range
2432 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002433static int
Owen Taylor3473f882001-02-23 17:55:21 +00002434xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2435 xmlNodePtr *end, int *endindex) {
2436 xmlNodePtr cur;
2437 const xmlChar *str;
2438 int pos; /* 0 based */
2439 int len; /* in bytes */
2440 int stringlen; /* in bytes */
2441 xmlChar first;
2442
2443 if (string == NULL)
2444 return(-1);
2445 if ((start == NULL) || (startindex == NULL))
2446 return(-1);
2447 if ((end == NULL) || (endindex == NULL))
2448 return(-1);
2449 cur = *start;
2450 if (cur == NULL)
2451 return(-1);
2452 pos = *startindex - 1;
2453 first = string[0];
2454 stringlen = xmlStrlen(string);
2455
2456 while (cur != NULL) {
2457 if (cur->content != NULL) {
2458#ifndef XML_USE_BUFFER_CONTENT
2459 len = xmlStrlen(cur->content);
2460#else
2461 len = xmlBufferLength(cur->content);
2462#endif
2463 while (pos <= len) {
2464 if (first != 0) {
2465#ifndef XML_USE_BUFFER_CONTENT
2466 str = xmlStrchr(&cur->content[pos], first);
2467#else
2468 str = xmlStrchr(&xmlBufferContent(cur->content)[pos],
2469 first);
2470#endif
2471 if (str != NULL) {
2472 pos = (str - (xmlChar *)(cur->content));
2473#ifdef DEBUG_RANGES
2474 xmlGenericError(xmlGenericErrorContext,
2475 "found '%c' at index %d of ->",
2476 first, pos + 1);
2477 xmlDebugDumpString(stdout, cur->content);
2478 xmlGenericError(xmlGenericErrorContext, "\n");
2479#endif
2480 if (xmlXPtrMatchString(string, cur, pos + 1,
2481 end, endindex)) {
2482 *start = cur;
2483 *startindex = pos + 1;
2484 return(1);
2485 }
2486 pos++;
2487 } else {
2488 pos = len + 1;
2489 }
2490 } else {
2491 /*
2492 * An empty string is considered to match before each
2493 * character of the string-value and after the final
2494 * character.
2495 */
2496#ifdef DEBUG_RANGES
2497 xmlGenericError(xmlGenericErrorContext,
2498 "found '' at index %d of ->",
2499 pos + 1);
2500 xmlDebugDumpString(stdout, cur->content);
2501 xmlGenericError(xmlGenericErrorContext, "\n");
2502#endif
2503 *start = cur;
2504 *startindex = pos + 1;
2505 *end = cur;
2506 *endindex = pos + 1;
2507 return(1);
2508 }
2509 }
2510 }
2511 if ((cur == *end) && (pos >= *endindex))
2512 return(0);
2513 cur = xmlXPtrAdvanceNode(cur);
2514 if (cur == NULL)
2515 return(0);
2516 pos = 1;
2517 }
2518 return(0);
2519}
2520
2521/**
2522 * xmlXPtrGetLastChar:
2523 * @node: the node
2524 * @index: the index
2525 *
2526 * Computes the point coordinates of the last char of this point
2527 *
2528 * Returns -1 in case of failure, 0 otherwise
2529 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002530static int
2531xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002532 xmlNodePtr cur;
2533 int pos, len = 0;
2534
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002535 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002536 return(-1);
2537 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002538 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002539
2540 if (cur == NULL)
2541 return(-1);
2542
2543 if ((cur->type == XML_ELEMENT_NODE) ||
2544 (cur->type == XML_DOCUMENT_NODE) ||
2545 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2546 if (pos > 0) {
2547 cur = xmlXPtrGetNthChild(cur, pos);
2548 pos = 0;
2549 }
2550 }
2551 while (cur != NULL) {
2552 if (cur->last != NULL)
2553 cur = cur->last;
2554 else if (cur->content != NULL) {
2555#ifndef XML_USE_BUFFER_CONTENT
2556 len = xmlStrlen(cur->content);
2557#else
2558 len = xmlBufferLength(cur->content);
2559#endif
2560 break;
2561 } else {
2562 return(-1);
2563 }
2564 }
2565 if (cur == NULL)
2566 return(-1);
2567 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002568 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002569 return(0);
2570}
2571
2572/**
2573 * xmlXPtrGetStartPoint:
2574 * @obj: an range
2575 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002576 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002577 *
2578 * read the object and return the start point coordinates.
2579 *
2580 * Returns -1 in case of failure, 0 otherwise
2581 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002582static int
2583xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2584 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002585 return(-1);
2586
2587 switch (obj->type) {
2588 case XPATH_POINT:
2589 *node = obj->user;
2590 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002591 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002592 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002593 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002594 return(0);
2595 case XPATH_RANGE:
2596 *node = obj->user;
2597 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002598 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002599 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002600 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002601 return(0);
2602 default:
2603 return(-1);
2604 }
2605 return(-1);
2606}
2607
2608/**
2609 * xmlXPtrGetEndPoint:
2610 * @obj: an range
2611 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002612 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002613 *
2614 * read the object and return the end point coordinates.
2615 *
2616 * Returns -1 in case of failure, 0 otherwise
2617 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002618static int
2619xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2620 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002621 return(-1);
2622
2623 switch (obj->type) {
2624 case XPATH_POINT:
2625 *node = obj->user;
2626 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002627 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002628 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002629 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002630 return(0);
2631 case XPATH_RANGE:
2632 *node = obj->user;
2633 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002634 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002635 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002636 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002637 return(0);
2638 default:
2639 return(-1);
2640 }
2641 return(-1);
2642}
2643
2644/**
2645 * xmlXPtrStringRangeFunction:
2646 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002647 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002648 *
2649 * Function implementing the string-range() function
2650 * range as described in 5.4.2
2651 *
2652 * ------------------------------
2653 * [Definition: For each location in the location-set argument,
2654 * string-range returns a set of string ranges, a set of substrings in a
2655 * string. Specifically, the string-value of the location is searched for
2656 * substrings that match the string argument, and the resulting location-set
2657 * will contain a range location for each non-overlapping match.]
2658 * An empty string is considered to match before each character of the
2659 * string-value and after the final character. Whitespace in a string
2660 * is matched literally, with no normalization except that provided by
2661 * XML for line ends. The third argument gives the position of the first
2662 * character to be in the resulting range, relative to the start of the
2663 * match. The default value is 1, which makes the range start immediately
2664 * before the first character of the matched string. The fourth argument
2665 * gives the number of characters in the range; the default is that the
2666 * range extends to the end of the matched string.
2667 *
2668 * Element boundaries, as well as entire embedded nodes such as processing
2669 * instructions and comments, are ignored as defined in [XPath].
2670 *
2671 * If the string in the second argument is not found in the string-value
2672 * of the location, or if a value in the third or fourth argument indicates
2673 * a string that is beyond the beginning or end of the document, the
2674 * expression fails.
2675 *
2676 * The points of the range-locations in the returned location-set will
2677 * all be character points.
2678 * ------------------------------
2679 */
2680void
2681xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2682 int i, startindex, endindex, fendindex;
2683 xmlNodePtr start, end, fend;
2684 xmlXPathObjectPtr set;
2685 xmlLocationSetPtr oldset;
2686 xmlLocationSetPtr newset;
2687 xmlXPathObjectPtr string;
2688 xmlXPathObjectPtr position = NULL;
2689 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002690 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002691
2692 /*
2693 * Grab the arguments
2694 */
2695 if ((nargs < 2) || (nargs > 4))
2696 XP_ERROR(XPATH_INVALID_ARITY);
2697
2698 if (nargs >= 4) {
2699 CHECK_TYPE(XPATH_NUMBER);
2700 number = valuePop(ctxt);
2701 if (number != NULL)
2702 num = number->floatval;
2703 }
2704 if (nargs >= 3) {
2705 CHECK_TYPE(XPATH_NUMBER);
2706 position = valuePop(ctxt);
2707 if (position != NULL)
2708 pos = position->floatval;
2709 }
2710 CHECK_TYPE(XPATH_STRING);
2711 string = valuePop(ctxt);
2712 if ((ctxt->value == NULL) ||
2713 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2714 (ctxt->value->type != XPATH_NODESET)))
2715 XP_ERROR(XPATH_INVALID_TYPE)
2716
2717 set = valuePop(ctxt);
2718 if (set->type == XPATH_NODESET) {
2719 xmlXPathObjectPtr tmp;
2720
2721 /*
2722 * First convert to a location set
2723 */
2724 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2725 xmlXPathFreeObject(set);
2726 set = tmp;
2727 }
2728 oldset = (xmlLocationSetPtr) set->user;
2729
2730 /*
2731 * The loop is to search for each element in the location set
2732 * the list of location set corresponding to that search
2733 */
2734 newset = xmlXPtrLocationSetCreate(NULL);
2735 for (i = 0;i < oldset->locNr;i++) {
2736#ifdef DEBUG_RANGES
2737 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2738#endif
2739
2740 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2741 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2742 xmlXPtrAdvanceChar(&start, &startindex, 0);
2743 xmlXPtrGetLastChar(&end, &endindex);
2744
2745#ifdef DEBUG_RANGES
2746 xmlGenericError(xmlGenericErrorContext,
2747 "from index %d of ->", startindex);
2748 xmlDebugDumpString(stdout, start->content);
2749 xmlGenericError(xmlGenericErrorContext, "\n");
2750 xmlGenericError(xmlGenericErrorContext,
2751 "to index %d of ->", endindex);
2752 xmlDebugDumpString(stdout, end->content);
2753 xmlGenericError(xmlGenericErrorContext, "\n");
2754#endif
2755 do {
2756 fend = end;
2757 fendindex = endindex;
2758 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2759 &fend, &fendindex);
2760 if (found == 1) {
2761 if (position == NULL) {
2762 xmlXPtrLocationSetAdd(newset,
2763 xmlXPtrNewRange(start, startindex, fend, fendindex));
2764 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2765 pos - 1) == 0) {
2766 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002767 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002768 xmlNodePtr rend;
2769 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002770 rindx = startindex - 1;
2771 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002772 num) == 0) {
2773 xmlXPtrLocationSetAdd(newset,
2774 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002775 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002776 }
2777 } else if ((number != NULL) && (num <= 0)) {
2778 xmlXPtrLocationSetAdd(newset,
2779 xmlXPtrNewRange(start, startindex,
2780 start, startindex));
2781 } else {
2782 xmlXPtrLocationSetAdd(newset,
2783 xmlXPtrNewRange(start, startindex,
2784 fend, fendindex));
2785 }
2786 }
2787 start = fend;
2788 startindex = fendindex;
2789 if (string->stringval[0] == 0)
2790 startindex++;
2791 }
2792 } while (found == 1);
2793 }
2794
2795 /*
2796 * Save the new value and cleanup
2797 */
2798 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2799 xmlXPathFreeObject(set);
2800 xmlXPathFreeObject(string);
2801 if (position) xmlXPathFreeObject(position);
2802 if (number) xmlXPathFreeObject(number);
2803}
2804
2805/**
2806 * xmlXPtrEvalRangePredicate:
2807 * @ctxt: the XPointer Parser context
2808 *
2809 * [8] Predicate ::= '[' PredicateExpr ']'
2810 * [9] PredicateExpr ::= Expr
2811 *
2812 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2813 * a Location Set instead of a node set
2814 */
2815void
2816xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2817 const xmlChar *cur;
2818 xmlXPathObjectPtr res;
2819 xmlXPathObjectPtr obj, tmp;
2820 xmlLocationSetPtr newset = NULL;
2821 xmlLocationSetPtr oldset;
2822 int i;
2823
2824 SKIP_BLANKS;
2825 if (CUR != '[') {
2826 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2827 }
2828 NEXT;
2829 SKIP_BLANKS;
2830
2831 /*
2832 * Extract the old set, and then evaluate the result of the
2833 * expression for all the element in the set. use it to grow
2834 * up a new set.
2835 */
2836 CHECK_TYPE(XPATH_LOCATIONSET);
2837 obj = valuePop(ctxt);
2838 oldset = obj->user;
2839 ctxt->context->node = NULL;
2840
2841 if ((oldset == NULL) || (oldset->locNr == 0)) {
2842 ctxt->context->contextSize = 0;
2843 ctxt->context->proximityPosition = 0;
2844 xmlXPathEvalExpr(ctxt);
2845 res = valuePop(ctxt);
2846 if (res != NULL)
2847 xmlXPathFreeObject(res);
2848 valuePush(ctxt, obj);
2849 CHECK_ERROR;
2850 } else {
2851 /*
2852 * Save the expression pointer since we will have to evaluate
2853 * it multiple times. Initialize the new set.
2854 */
2855 cur = ctxt->cur;
2856 newset = xmlXPtrLocationSetCreate(NULL);
2857
2858 for (i = 0; i < oldset->locNr; i++) {
2859 ctxt->cur = cur;
2860
2861 /*
2862 * Run the evaluation with a node list made of a single item
2863 * in the nodeset.
2864 */
2865 ctxt->context->node = oldset->locTab[i]->user;
2866 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2867 valuePush(ctxt, tmp);
2868 ctxt->context->contextSize = oldset->locNr;
2869 ctxt->context->proximityPosition = i + 1;
2870
2871 xmlXPathEvalExpr(ctxt);
2872 CHECK_ERROR;
2873
2874 /*
2875 * The result of the evaluation need to be tested to
2876 * decided whether the filter succeeded or not
2877 */
2878 res = valuePop(ctxt);
2879 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2880 xmlXPtrLocationSetAdd(newset,
2881 xmlXPathObjectCopy(oldset->locTab[i]));
2882 }
2883
2884 /*
2885 * Cleanup
2886 */
2887 if (res != NULL)
2888 xmlXPathFreeObject(res);
2889 if (ctxt->value == tmp) {
2890 res = valuePop(ctxt);
2891 xmlXPathFreeObject(res);
2892 }
2893
2894 ctxt->context->node = NULL;
2895 }
2896
2897 /*
2898 * The result is used as the new evaluation set.
2899 */
2900 xmlXPathFreeObject(obj);
2901 ctxt->context->node = NULL;
2902 ctxt->context->contextSize = -1;
2903 ctxt->context->proximityPosition = -1;
2904 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2905 }
2906 if (CUR != ']') {
2907 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2908 }
2909
2910 NEXT;
2911 SKIP_BLANKS;
2912}
2913
2914#else
2915#endif
2916