blob: 7674942afaf495e593e75236f480184b3785d759 [file] [log] [blame]
Daniel Veillarde8eac3d2000-10-11 08:55:02 +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
Daniel Veillardc2df4cd2000-10-12 23:15:24 +000021 * 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 * TODO: some functions are still missing !
Daniel Veillarde8eac3d2000-10-11 08:55:02 +000025 */
26
27#include <stdio.h>
28#include <libxml/xpointer.h>
29#include <libxml/xmlmemory.h>
30#include <libxml/parserInternals.h>
31#include <libxml/xpath.h>
Daniel Veillardc2df4cd2000-10-12 23:15:24 +000032#ifdef LIBXML_DEBUG_ENABLED
33#include <libxml/debugXML.h>
34#endif
Daniel Veillarde8eac3d2000-10-11 08:55:02 +000035
36#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc2df4cd2000-10-12 23:15:24 +000037
38/* #define DEBUG_RANGES */
39
40
Daniel Veillarde8eac3d2000-10-11 08:55:02 +000041extern FILE *xmlXPathDebug;
42
43#define TODO \
44 fprintf(xmlXPathDebug, "Unimplemented block at %s:%d\n", \
45 __FILE__, __LINE__);
46
47#define STRANGE \
48 fprintf(xmlXPathDebug, "Internal error at %s:%d\n", \
49 __FILE__, __LINE__);
50
51/************************************************************************
52 * *
Daniel Veillard1baf4122000-10-15 20:38:39 +000053 * A few helper functions for child sequences *
54 * *
55 ************************************************************************/
56
57/**
58 * xmlXPtrGetArity:
59 * @cur: the node
60 *
61 * Returns the number of child for an element, -1 in case of error
62 */
63int
64xmlXPtrGetArity(xmlNodePtr cur) {
65 int i;
66 if (cur == NULL)
67 return(-1);
68 cur = cur->children;
69 for (i = 0;cur != NULL;cur = cur->next) {
70 if ((cur->type == XML_ELEMENT_NODE) ||
71 (cur->type == XML_DOCUMENT_NODE) ||
72 (cur->type == XML_HTML_DOCUMENT_NODE)) {
73 i++;
74 }
75 }
76 return(i);
77}
78
79/**
80 * xmlXPtrGetIndex:
81 * @cur: the node
82 *
83 * Returns the index of the node in its parent children list, -1
84 * in case of error
85 */
86int
87xmlXPtrGetIndex(xmlNodePtr cur) {
88 int i;
89 if (cur == NULL)
90 return(-1);
91 for (i = 1;cur != NULL;cur = cur->prev) {
92 if ((cur->type == XML_ELEMENT_NODE) ||
93 (cur->type == XML_DOCUMENT_NODE) ||
94 (cur->type == XML_HTML_DOCUMENT_NODE)) {
95 i++;
96 }
97 }
98 return(i);
99}
100
101/**
102 * xmlXPtrGetNthChild:
103 * @cur: the node
104 * @no: the child number
105 *
106 * Returns the @no'th element child of @cur or NULL
107 */
108xmlNodePtr
109xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
110 int i;
111 if (cur == NULL)
112 return(cur);
113 cur = cur->children;
114 for (i = 0;i <= no;cur = cur->next) {
115 if (cur == NULL)
116 return(cur);
117 if ((cur->type == XML_ELEMENT_NODE) ||
118 (cur->type == XML_DOCUMENT_NODE) ||
119 (cur->type == XML_HTML_DOCUMENT_NODE)) {
120 i++;
121 if (i == no)
122 break;
123 }
124 }
125 return(cur);
126}
127
128/************************************************************************
129 * *
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000130 * Handling of XPointer specific types *
131 * *
132 ************************************************************************/
133
134/**
Daniel Veillardff9c3302000-10-13 16:38:25 +0000135 * xmlXPtrCmpPoints:
136 * @node1: the first node
137 * @index1: the first index
138 * @node2: the second node
139 * @index2: the second index
140 *
141 * Compare two points w.r.t document order
142 *
143 * Returns -2 in case of error 1 if first point < second point, 0 if
144 * that's the same point, -1 otherwise
145 */
146int
147xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
148 int depth1, depth2;
149 xmlNodePtr cur, root;
150
151 if ((node1 == NULL) || (node2 == NULL))
152 return(-2);
153 /*
154 * a couple of optimizations which will avoid computations in most cases
155 */
156 if (node1 == node2) {
157 if (index1 < index2)
158 return(1);
159 if (index1 > index2)
160 return(-1);
161 return(0);
162 }
163 if (node1 == node2->prev)
164 return(1);
165 if (node1 == node2->next)
166 return(-1);
167
168 /*
169 * compute depth to root
170 */
171 for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) {
172 if (cur == node1)
173 return(1);
174 depth2++;
175 }
176 root = cur;
177 for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) {
178 if (cur == node2)
179 return(-1);
180 depth1++;
181 }
182 /*
183 * Distinct document (or distinct entities :-( ) case.
184 */
185 if (root != cur) {
186 return(-2);
187 }
188 /*
189 * get the nearest common ancestor.
190 */
191 while (depth1 > depth2) {
192 depth1--;
193 node1 = node1->parent;
194 }
195 while (depth2 > depth1) {
196 depth2--;
197 node2 = node2->parent;
198 }
199 while (node1->parent != node2->parent) {
200 node1 = node1->parent;
201 node2 = node2->parent;
202 /* should not happen but just in case ... */
203 if ((node1 == NULL) || (node2 == NULL))
204 return(-2);
205 }
206 /*
207 * Find who's first.
208 */
209 if (node1 == node2->next)
210 return(-1);
211 for (cur = node1->next;cur != NULL;cur = cur->next)
212 if (cur == node2)
213 return(1);
214 return(-1); /* assume there is no sibling list corruption */
215}
216
217/**
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000218 * xmlXPtrNewPoint:
219 * @node: the xmlNodePtr
220 * @index: the index within the node
221 *
222 * Create a new xmlXPathObjectPtr of type point
223 *
224 * Returns the newly created object.
225 */
226xmlXPathObjectPtr
227xmlXPtrNewPoint(xmlNodePtr node, int index) {
228 xmlXPathObjectPtr ret;
229
230 if (node == NULL)
231 return(NULL);
232 if (index < 0)
233 return(NULL);
234
235 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
236 if (ret == NULL) {
237 fprintf(xmlXPathDebug, "xmlXPtrNewPoint: out of memory\n");
238 return(NULL);
239 }
240 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
241 ret->type = XPATH_POINT;
242 ret->user = (void *) node;
243 ret->index = index;
244 return(ret);
245}
246
247/**
Daniel Veillardff9c3302000-10-13 16:38:25 +0000248 * xmlXPtrRangeCheckOrder:
249 * @range: an object range
250 *
251 * Make sure the points in the range are in the right order
252 */
253void
254xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
255 int tmp;
256 xmlNodePtr tmp2;
257 if (range == NULL)
258 return;
259 if (range->type != XPATH_RANGE)
260 return;
261 if (range->user2 == NULL)
262 return;
263 tmp = xmlXPtrCmpPoints(range->user, range->index,
264 range->user2, range->index2);
265 if (tmp == -1) {
266 tmp2 = range->user;
267 range->user = range->user2;
268 range->user2 = tmp2;
269 tmp = range->index;
270 range->index = range->index2;
271 range->index2 = tmp;
272 }
273}
274
275/**
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000276 * xmlXPtrNewRange:
277 * @start: the starting node
278 * @startindex: the start index
279 * @end: the ending point
280 * @endindex: the ending index
281 *
282 * Create a new xmlXPathObjectPtr of type range
283 *
284 * Returns the newly created object.
285 */
286xmlXPathObjectPtr
287xmlXPtrNewRange(xmlNodePtr start, int startindex,
288 xmlNodePtr end, int endindex) {
289 xmlXPathObjectPtr ret;
290
291 if (start == NULL)
292 return(NULL);
293 if (end == NULL)
294 return(NULL);
295 if (startindex < 0)
296 return(NULL);
297 if (endindex < 0)
298 return(NULL);
299
300 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
301 if (ret == NULL) {
302 fprintf(xmlXPathDebug, "xmlXPtrNewRangePoints: out of memory\n");
303 return(NULL);
304 }
305 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
306 ret->type = XPATH_RANGE;
307 ret->user = start;
308 ret->index = startindex;
309 ret->user2 = end;
310 ret->index2 = endindex;
Daniel Veillardff9c3302000-10-13 16:38:25 +0000311 xmlXPtrRangeCheckOrder(ret);
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000312 return(ret);
313}
314
315/**
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000316 * xmlXPtrNewRangePoints:
317 * @start: the starting point
318 * @end: the ending point
319 *
320 * Create a new xmlXPathObjectPtr of type range using 2 Points
321 *
322 * Returns the newly created object.
323 */
324xmlXPathObjectPtr
325xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
326 xmlXPathObjectPtr ret;
327
328 if (start == NULL)
329 return(NULL);
330 if (end == NULL)
331 return(NULL);
332 if (start->type != XPATH_POINT)
333 return(NULL);
334 if (end->type != XPATH_POINT)
335 return(NULL);
336
337 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
338 if (ret == NULL) {
339 fprintf(xmlXPathDebug, "xmlXPtrNewRangePoints: out of memory\n");
340 return(NULL);
341 }
342 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
343 ret->type = XPATH_RANGE;
344 ret->user = start->user;
345 ret->index = start->index;
346 ret->user2 = end->user;
347 ret->index2 = end->index;
Daniel Veillardff9c3302000-10-13 16:38:25 +0000348 xmlXPtrRangeCheckOrder(ret);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000349 return(ret);
350}
351
352/**
353 * xmlXPtrNewRangePointNode:
354 * @start: the starting point
355 * @end: the ending node
356 *
357 * Create a new xmlXPathObjectPtr of type range from a point to a node
358 *
359 * Returns the newly created object.
360 */
361xmlXPathObjectPtr
362xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
363 xmlXPathObjectPtr ret;
364
365 if (start == NULL)
366 return(NULL);
367 if (end == NULL)
368 return(NULL);
369 if (start->type != XPATH_POINT)
370 return(NULL);
371
372 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
373 if (ret == NULL) {
374 fprintf(xmlXPathDebug, "xmlXPtrNewRangePointNode: out of memory\n");
375 return(NULL);
376 }
377 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
378 ret->type = XPATH_RANGE;
379 ret->user = start->user;
380 ret->index = start->index;
381 ret->user2 = end;
382 ret->index2 = -1;
Daniel Veillardff9c3302000-10-13 16:38:25 +0000383 xmlXPtrRangeCheckOrder(ret);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000384 return(ret);
385}
386
387/**
388 * xmlXPtrNewRangeNodePoint:
389 * @start: the starting node
390 * @end: the ending point
391 *
392 * Create a new xmlXPathObjectPtr of type range from a node to a point
393 *
394 * Returns the newly created object.
395 */
396xmlXPathObjectPtr
397xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
398 xmlXPathObjectPtr ret;
399
400 if (start == NULL)
401 return(NULL);
402 if (end == NULL)
403 return(NULL);
404 if (start->type != XPATH_POINT)
405 return(NULL);
406 if (end->type != XPATH_POINT)
407 return(NULL);
408
409 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
410 if (ret == NULL) {
411 fprintf(xmlXPathDebug, "xmlXPtrNewRangeNodePoint: out of memory\n");
412 return(NULL);
413 }
414 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
415 ret->type = XPATH_RANGE;
416 ret->user = start;
417 ret->index = -1;
418 ret->user2 = end->user;
419 ret->index2 = end->index;
Daniel Veillardff9c3302000-10-13 16:38:25 +0000420 xmlXPtrRangeCheckOrder(ret);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000421 return(ret);
422}
423
424/**
425 * xmlXPtrNewRangeNodes:
426 * @start: the starting node
427 * @end: the ending node
428 *
429 * Create a new xmlXPathObjectPtr of type range using 2 nodes
430 *
431 * Returns the newly created object.
432 */
433xmlXPathObjectPtr
434xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
435 xmlXPathObjectPtr ret;
436
437 if (start == NULL)
438 return(NULL);
439 if (end == NULL)
440 return(NULL);
441
442 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
443 if (ret == NULL) {
444 fprintf(xmlXPathDebug, "xmlXPtrNewRangeNodes: out of memory\n");
445 return(NULL);
446 }
447 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
448 ret->type = XPATH_RANGE;
449 ret->user = start;
450 ret->index = -1;
451 ret->user2 = end;
452 ret->index2 = -1;
Daniel Veillardff9c3302000-10-13 16:38:25 +0000453 xmlXPtrRangeCheckOrder(ret);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000454 return(ret);
455}
456
457/**
458 * xmlXPtrNewCollapsedRange:
459 * @start: the starting and ending node
460 *
461 * Create a new xmlXPathObjectPtr of type range using a single nodes
462 *
463 * Returns the newly created object.
464 */
465xmlXPathObjectPtr
466xmlXPtrNewCollapsedRange(xmlNodePtr start) {
467 xmlXPathObjectPtr ret;
468
469 if (start == NULL)
470 return(NULL);
471
472 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
473 if (ret == NULL) {
474 fprintf(xmlXPathDebug, "xmlXPtrNewRangeNodes: out of memory\n");
475 return(NULL);
476 }
477 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
478 ret->type = XPATH_RANGE;
479 ret->user = start;
480 ret->index = -1;
481 ret->user2 = NULL;
482 ret->index2 = -1;
483 return(ret);
484}
485
486/**
487 * xmlXPtrNewRangeNodeObject:
488 * @start: the starting node
489 * @end: the ending object
490 *
491 * Create a new xmlXPathObjectPtr of type range from a not to an object
492 *
493 * Returns the newly created object.
494 */
495xmlXPathObjectPtr
496xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
497 xmlXPathObjectPtr ret;
498
499 if (start == NULL)
500 return(NULL);
501 if (end == NULL)
502 return(NULL);
503 switch (end->type) {
504 case XPATH_POINT:
505 break;
506 case XPATH_NODESET:
507 /*
508 * Empty set ...
509 */
510 if (end->nodesetval->nodeNr <= 0)
511 return(NULL);
512 break;
513 default:
514 TODO
515 return(NULL);
516 }
517
518 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
519 if (ret == NULL) {
520 fprintf(xmlXPathDebug, "xmlXPtrNewRangeNodeObject: out of memory\n");
521 return(NULL);
522 }
523 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
524 ret->type = XPATH_RANGE;
525 ret->user = start;
526 ret->index = -1;
527 switch (end->type) {
528 case XPATH_POINT:
529 ret->user2 = end->user;
530 ret->index2 = end->index;
531 case XPATH_NODESET: {
532 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
533 ret->index2 = -1;
534 break;
535 }
536 default:
537 STRANGE
538 return(NULL);
539 }
Daniel Veillardff9c3302000-10-13 16:38:25 +0000540 xmlXPtrRangeCheckOrder(ret);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000541 return(ret);
542}
543
544#define XML_RANGESET_DEFAULT 10
545
546/**
547 * xmlXPtrLocationSetCreate:
548 * @val: an initial xmlXPathObjectPtr, or NULL
549 *
550 * Create a new xmlLocationSetPtr of type double and of value @val
551 *
552 * Returns the newly created object.
553 */
554xmlLocationSetPtr
555xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
556 xmlLocationSetPtr ret;
557
558 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
559 if (ret == NULL) {
560 fprintf(xmlXPathDebug, "xmlXPtrLocationSetCreate: out of memory\n");
561 return(NULL);
562 }
563 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
564 if (val != NULL) {
565 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
566 sizeof(xmlXPathObjectPtr));
567 if (ret->locTab == NULL) {
568 fprintf(xmlXPathDebug, "xmlXPtrLocationSetCreate: out of memory\n");
569 return(NULL);
570 }
571 memset(ret->locTab, 0 ,
572 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
573 ret->locMax = XML_RANGESET_DEFAULT;
574 ret->locTab[ret->locNr++] = val;
575 }
576 return(ret);
577}
578
579/**
580 * xmlXPtrLocationSetAdd:
581 * @cur: the initial range set
582 * @val: a new xmlXPathObjectPtr
583 *
584 * add a new xmlXPathObjectPtr ot an existing LocationSet
585 */
586void
587xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
588 int i;
589
590 if (val == NULL) return;
591
592 /*
593 * check against doublons
594 */
595 for (i = 0;i < cur->locNr;i++)
596 if (cur->locTab[i] == val) return;
597
598 /*
599 * grow the locTab if needed
600 */
601 if (cur->locMax == 0) {
602 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
603 sizeof(xmlXPathObjectPtr));
604 if (cur->locTab == NULL) {
605 fprintf(xmlXPathDebug, "xmlXPtrLocationSetAdd: out of memory\n");
606 return;
607 }
608 memset(cur->locTab, 0 ,
609 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
610 cur->locMax = XML_RANGESET_DEFAULT;
611 } else if (cur->locNr == cur->locMax) {
612 xmlXPathObjectPtr *temp;
613
614 cur->locMax *= 2;
615 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
616 sizeof(xmlXPathObjectPtr));
617 if (temp == NULL) {
618 fprintf(xmlXPathDebug, "xmlXPtrLocationSetAdd: out of memory\n");
619 return;
620 }
621 cur->locTab = temp;
622 }
623 cur->locTab[cur->locNr++] = val;
624}
625
626/**
627 * xmlXPtrLocationSetMerge:
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000628 * @val1: the first LocationSet
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000629 * @val2: the second LocationSet
630 *
631 * Merges two rangesets, all ranges from @val2 are added to @val1
632 *
633 * Returns val1 once extended or NULL in case of error.
634 */
635xmlLocationSetPtr
636xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
637 int i;
638
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000639 if (val1 == NULL) return(NULL);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000640 if (val2 == NULL) return(val1);
641
642 /*
643 * !!!!! this can be optimized a lot, knowing that both
644 * val1 and val2 already have unicity of their values.
645 */
646
647 for (i = 0;i < val2->locNr;i++)
648 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
649
650 return(val1);
651}
652
653/**
654 * xmlXPtrLocationSetDel:
655 * @cur: the initial range set
656 * @val: an xmlXPathObjectPtr
657 *
658 * Removes an xmlXPathObjectPtr from an existing LocationSet
659 */
660void
661xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
662 int i;
663
664 if (cur == NULL) return;
665 if (val == NULL) return;
666
667 /*
668 * check against doublons
669 */
670 for (i = 0;i < cur->locNr;i++)
671 if (cur->locTab[i] == val) break;
672
673 if (i >= cur->locNr) {
674#ifdef DEBUG
675 fprintf(xmlXPathDebug,
676 "xmlXPtrLocationSetDel: Range %s wasn't found in RangeList\n",
677 val->name);
678#endif
679 return;
680 }
681 cur->locNr--;
682 for (;i < cur->locNr;i++)
683 cur->locTab[i] = cur->locTab[i + 1];
684 cur->locTab[cur->locNr] = NULL;
685}
686
687/**
688 * xmlXPtrLocationSetRemove:
689 * @cur: the initial range set
690 * @val: the index to remove
691 *
692 * Removes an entry from an existing LocationSet list.
693 */
694void
695xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
696 if (cur == NULL) return;
697 if (val >= cur->locNr) return;
698 cur->locNr--;
699 for (;val < cur->locNr;val++)
700 cur->locTab[val] = cur->locTab[val + 1];
701 cur->locTab[cur->locNr] = NULL;
702}
703
704/**
705 * xmlXPtrFreeLocationSet:
706 * @obj: the xmlLocationSetPtr to free
707 *
708 * Free the LocationSet compound (not the actual ranges !).
709 */
710void
711xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
712 int i;
713
714 if (obj == NULL) return;
715 if (obj->locTab != NULL) {
716 for (i = 0;i < obj->locNr; i++) {
717 xmlXPathFreeObject(obj->locTab[i]);
718 }
719#ifdef DEBUG
720 memset(obj->locTab, 0xB ,
721 (size_t) sizeof(xmlXPathObjectPtr) * obj->locMax);
722#endif
723 xmlFree(obj->locTab);
724 }
725#ifdef DEBUG
726 memset(obj, 0xB , (size_t) sizeof(xmlLocationSet));
727#endif
728 xmlFree(obj);
729}
730
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000731/**
732 * xmlXPtrNewLocationSetNodes:
733 * @start: the start NodePtr value
734 * @end: the end NodePtr value or NULL
735 *
736 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
737 * it with the single range made of the two nodes @start and @end
738 *
739 * Returns the newly created object.
740 */
741xmlXPathObjectPtr
742xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
743 xmlXPathObjectPtr ret;
744
745 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
746 if (ret == NULL) {
747 fprintf(xmlXPathDebug, "xmlXPtrNewLocationSetNodes: out of memory\n");
748 return(NULL);
749 }
750 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
751 ret->type = XPATH_LOCATIONSET;
752 if (end == NULL)
753 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
754 else
755 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
756 return(ret);
757}
758
759/**
760 * xmlXPtrNewLocationSetNodeSet:
761 * @set: a node set
762 *
763 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
764 * it with all the nodes from @set
765 *
766 * Returns the newly created object.
767 */
768xmlXPathObjectPtr
769xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
770 xmlXPathObjectPtr ret;
771
772 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
773 if (ret == NULL) {
774 fprintf(xmlXPathDebug, "xmlXPtrNewLocationSetNodes: out of memory\n");
775 return(NULL);
776 }
777 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
778 ret->type = XPATH_LOCATIONSET;
779 if (set != NULL) {
780 int i;
781 xmlLocationSetPtr newset;
782
783 newset = xmlXPtrLocationSetCreate(NULL);
784 if (newset == NULL)
785 return(ret);
786
787 for (i = 0;i < set->nodeNr;i++)
788 xmlXPtrLocationSetAdd(newset,
789 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
790
791 ret->user = (void *) newset;
792 }
793 return(ret);
794}
795
796/**
797 * xmlXPtrWrapLocationSet:
798 * @val: the LocationSet value
799 *
800 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
801 *
802 * Returns the newly created object.
803 */
804xmlXPathObjectPtr
805xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
806 xmlXPathObjectPtr ret;
807
808 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
809 if (ret == NULL) {
810 fprintf(xmlXPathDebug, "xmlXPtrWrapLocationSet: out of memory\n");
811 return(NULL);
812 }
813 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
814 ret->type = XPATH_LOCATIONSET;
815 ret->user = (void *) val;
816 return(ret);
817}
818
819/************************************************************************
820 * *
821 * The parser *
822 * *
823 ************************************************************************/
824
825/*
826 * Macros for accessing the content. Those should be used only by the parser,
827 * and not exported.
828 *
829 * Dirty macros, i.e. one need to make assumption on the context to use them
830 *
831 * CUR_PTR return the current pointer to the xmlChar to be parsed.
832 * CUR returns the current xmlChar value, i.e. a 8 bit value
833 * in ISO-Latin or UTF-8.
834 * This should be used internally by the parser
835 * only to compare to ASCII values otherwise it would break when
836 * running with UTF-8 encoding.
837 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
838 * to compare on ASCII based substring.
839 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
840 * strings within the parser.
841 * CURRENT Returns the current char value, with the full decoding of
842 * UTF-8 if we are using this mode. It returns an int.
843 * NEXT Skip to the next character, this does the proper decoding
844 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
845 * It returns the pointer to the current xmlChar.
846 */
847
848#define CUR (*ctxt->cur)
849#define SKIP(val) ctxt->cur += (val)
850#define NXT(val) ctxt->cur[(val)]
851#define CUR_PTR ctxt->cur
852
853#define SKIP_BLANKS \
854 while (IS_BLANK(*(ctxt->cur))) NEXT
855
856#define CURRENT (*ctxt->cur)
857#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
858
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000859/*
860 * xmlXPtrGetChildNo:
861 * @ctxt: the XPointer Parser context
862 * @index: the child number
863 *
864 * Move the current node of the nodeset on the stack to the
865 * given child if found
866 */
867void
868xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int index) {
869 xmlNodePtr cur = NULL;
870 xmlXPathObjectPtr obj;
871 xmlNodeSetPtr oldset;
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000872
873 CHECK_TYPE(XPATH_NODESET);
874 obj = valuePop(ctxt);
875 oldset = obj->nodesetval;
876 if ((index <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
877 xmlXPathFreeObject(obj);
878 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
879 return;
880 }
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000881 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], index);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000882 if (cur == NULL) {
883 xmlXPathFreeObject(obj);
884 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
885 return;
886 }
887 oldset->nodeTab[0] = cur;
888 valuePush(ctxt, obj);
889}
890
891/**
892 * xmlXPtrEvalXPtrPart:
893 * @ctxt: the XPointer Parser context
894 * @name: the preparsed Scheme for the XPtrPart
895 *
896 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
897 * | Scheme '(' SchemeSpecificExpr ')'
898 *
899 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
900 *
901 * SchemeSpecificExpr ::= StringWithBalancedParens
902 *
903 * StringWithBalancedParens ::=
904 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
905 * [VC: Parenthesis escaping]
906 *
907 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
908 *
909 * VC: Parenthesis escaping:
910 * The end of an XPointer part is signaled by the right parenthesis ")"
911 * character that is balanced with the left parenthesis "(" character
912 * that began the part. Any unbalanced parenthesis character inside the
913 * expression, even within literals, must be escaped with a circumflex (^)
914 * character preceding it. If the expression contains any literal
915 * occurrences of the circumflex, each must be escaped with an additional
916 * circumflex (that is, ^^). If the unescaped parentheses in the expression
917 * are not balanced, a syntax error results.
918 *
919 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
920 * string and if the scheme is 'xpointer' it will call the XPath interprter.
921 *
922 * TODO: there is no new scheme registration mechanism
923 */
924
925void
926xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
927 xmlChar *buffer, *cur;
928 int len;
929 int level;
930
931 if (name == NULL)
Daniel Veillard2d38f042000-10-11 10:54:10 +0000932 name = xmlXPathParseName(ctxt);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000933 if (name == NULL)
934 XP_ERROR(XPATH_EXPR_ERROR);
935
936 if (CUR != '(')
937 XP_ERROR(XPATH_EXPR_ERROR);
938 NEXT;
939 level = 1;
940
941 len = xmlStrlen(ctxt->cur);
942 len++;
943 buffer = (xmlChar *) xmlMalloc(len * sizeof (xmlChar));
944 if (buffer == NULL) {
945 fprintf(xmlXPathDebug, "xmlXPtrEvalXPtrPart: out of memory\n");
946 return;
947 }
948
949 cur = buffer;
950 while (CUR != '0') {
951 if (CUR == ')') {
952 level--;
953 if (level == 0) {
954 NEXT;
955 break;
956 }
957 *cur++ = CUR;
958 } else if (CUR == '(') {
959 level++;
960 *cur++ = CUR;
961 } else if (CUR == '^') {
962 NEXT;
963 if ((CUR == ')') || (CUR == '(') || (CUR == '^')) {
964 *cur++ = CUR;
965 } else {
966 *cur++ = '^';
967 *cur++ = CUR;
968 }
969 } else {
970 *cur++ = CUR;
971 }
972 NEXT;
973 }
974 *cur = 0;
975
976 if ((level != 0) && (CUR == 0)) {
977 xmlFree(buffer);
978 XP_ERROR(XPTR_SYNTAX_ERROR);
979 }
980
981 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
982 const xmlChar *left = CUR_PTR;
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000983
984 CUR_PTR = buffer;
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000985 xmlXPathRoot(ctxt);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +0000986 xmlXPathEvalExpr(ctxt);
987 CUR_PTR=left;
988 } else {
989 fprintf(xmlXPathDebug, "unsupported scheme '%s'\n", name);
990 }
991 xmlFree(buffer);
992 xmlFree(name);
993}
994
995/**
996 * xmlXPtrEvalFullXPtr:
997 * @ctxt: the XPointer Parser context
998 * @name: the preparsed Scheme for the first XPtrPart
999 *
1000 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1001 *
1002 * As the specs says:
1003 * -----------
1004 * When multiple XPtrParts are provided, they must be evaluated in
1005 * left-to-right order. If evaluation of one part fails, the nexti
1006 * is evaluated. The following conditions cause XPointer part failure:
1007 *
1008 * - An unknown scheme
1009 * - A scheme that does not locate any sub-resource present in the resource
1010 * - A scheme that is not applicable to the media type of the resource
1011 *
1012 * The XPointer application must consume a failed XPointer part and
1013 * attempt to evaluate the next one, if any. The result of the first
1014 * XPointer part whose evaluation succeeds is taken to be the fragment
1015 * located by the XPointer as a whole. If all the parts fail, the result
1016 * for the XPointer as a whole is a sub-resource error.
1017 * -----------
1018 *
1019 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1020 * expressions or other shemes.
1021 */
1022void
1023xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1024 if (name == NULL)
Daniel Veillard2d38f042000-10-11 10:54:10 +00001025 name = xmlXPathParseName(ctxt);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001026 if (name == NULL)
1027 XP_ERROR(XPATH_EXPR_ERROR);
1028 while (name != NULL) {
1029 xmlXPtrEvalXPtrPart(ctxt, name);
1030
1031 /* in case of syntax error, break here */
1032 if (ctxt->error != XPATH_EXPRESSION_OK)
1033 return;
1034
1035 /*
1036 * If the returned value is a non-empty nodeset
1037 * or location set, return here.
1038 */
1039 if (ctxt->value != NULL) {
1040 xmlXPathObjectPtr obj = ctxt->value;
1041
1042 switch (obj->type) {
1043 case XPATH_LOCATIONSET: {
1044 xmlLocationSetPtr loc = ctxt->value->user;
1045 if ((loc != NULL) && (loc->locNr > 0))
1046 return;
1047 break;
1048 }
1049 case XPATH_NODESET: {
1050 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1051 if ((loc != NULL) && (loc->nodeNr > 0))
1052 return;
1053 break;
1054 }
1055 default:
1056 break;
1057 }
1058
1059 /*
1060 * Evaluating to improper values is equivalent to
1061 * a sub-resource error, clean-up the stack
1062 */
1063 do {
1064 obj = valuePop(ctxt);
1065 if (obj != NULL) {
1066 xmlXPathFreeObject(obj);
1067 }
1068 } while (obj != NULL);
1069 }
1070
1071 /*
1072 * Is there another XPoointer part.
1073 */
1074 SKIP_BLANKS;
Daniel Veillard2d38f042000-10-11 10:54:10 +00001075 name = xmlXPathParseName(ctxt);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001076 }
1077}
1078
1079/**
1080 * xmlXPtrEvalChildSeq:
1081 * @ctxt: the XPointer Parser context
1082 * @name: a possible ID name of the child sequence
1083 *
1084 * ChildSeq ::= '/1' ('/' [0-9]*)*
1085 * | Name ('/' [0-9]*)+
1086 *
1087 * Parse and evaluate a Child Sequence. This routine also handle the
1088 * case of a Bare Name used to get a document ID.
1089 */
1090void
1091xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1092 /*
1093 * XPointer don't allow by syntax to adress in mutirooted trees
1094 * this might prove useful in some cases, warn about it.
1095 */
1096 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
1097 fprintf(xmlXPathDebug, "warning: ChildSeq not starting by /1\n");
1098 }
1099
1100 if (name != NULL) {
1101 valuePush(ctxt, xmlXPathNewString(name));
1102 xmlFree(name);
1103 xmlXPathIdFunction(ctxt, 1);
1104 CHECK_ERROR;
1105 }
1106
1107 while (CUR == '/') {
1108 int child = 0;
1109 NEXT;
1110
1111 while ((CUR >= '0') && (CUR <= '9')) {
1112 child = child * 10 + (CUR - '0');
1113 NEXT;
1114 }
1115 xmlXPtrGetChildNo(ctxt, child);
1116 }
1117}
1118
1119
1120/**
1121 * xmlXPtrEvalXPointer:
1122 * @ctxt: the XPointer Parser context
1123 *
1124 * XPointer ::= Name
1125 * | ChildSeq
1126 * | FullXPtr
1127 *
1128 * Parse and evaluate an XPointer
1129 */
1130void
1131xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
1132 SKIP_BLANKS;
1133 if (CUR == '/') {
1134 xmlXPathRoot(ctxt);
1135 xmlXPtrEvalChildSeq(ctxt, NULL);
1136 } else {
1137 xmlChar *name;
1138
Daniel Veillard2d38f042000-10-11 10:54:10 +00001139 name = xmlXPathParseName(ctxt);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001140 if (name == NULL)
1141 XP_ERROR(XPATH_EXPR_ERROR);
1142 if (CUR == '(') {
1143 xmlXPtrEvalFullXPtr(ctxt, name);
1144 /* Short evaluation */
1145 return;
1146 } else {
1147 /* this handle both Bare Names and Child Sequences */
1148 xmlXPtrEvalChildSeq(ctxt, name);
1149 }
1150 }
1151 SKIP_BLANKS;
1152 if (CUR != 0)
1153 XP_ERROR(XPATH_EXPR_ERROR);
1154}
1155
1156
1157/************************************************************************
1158 * *
1159 * General routines *
1160 * *
1161 ************************************************************************/
1162
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001163void xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs);
1164void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1165void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1166void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1167void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1168void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard1baf4122000-10-15 20:38:39 +00001169void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1170void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001171
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001172/**
1173 * xmlXPtrNewContext:
1174 * @doc: the XML document
1175 * @here: the node that directly contains the XPointer being evaluated or NULL
1176 * @origin: the element from which a user or program initiated traversal of
1177 * the link, or NULL.
1178 *
1179 * Create a new XPointer context
1180 *
1181 * Returns the xmlXPathContext just allocated.
1182 */
1183xmlXPathContextPtr
1184xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1185 xmlXPathContextPtr ret;
1186
1187 ret = xmlXPathNewContext(doc);
1188 if (ret == NULL)
1189 return(ret);
1190 ret->xptr = 1;
1191 ret->here = here;
1192 ret->origin = origin;
1193
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001194 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1195 xmlXPtrRangeToFunction);
Daniel Veillard1baf4122000-10-15 20:38:39 +00001196 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1197 xmlXPtrRangeFunction);
1198 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1199 xmlXPtrRangeInsideFunction);
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001200 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1201 xmlXPtrStringRangeFunction);
1202 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1203 xmlXPtrStartPointFunction);
1204 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1205 xmlXPtrEndPointFunction);
1206 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1207 xmlXPtrHereFunction);
1208 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1209 xmlXPtrOriginFunction);
1210
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001211 return(ret);
1212}
1213
1214/**
1215 * xmlXPtrEval:
1216 * @str: the XPointer expression
1217 * @ctx: the XPointer context
1218 *
1219 * Evaluate the XPath Location Path in the given context.
1220 *
1221 * Returns the xmlXPathObjectPtr resulting from the eveluation or NULL.
1222 * the caller has to free the object.
1223 */
1224xmlXPathObjectPtr
1225xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1226 xmlXPathParserContextPtr ctxt;
1227 xmlXPathObjectPtr res = NULL, tmp;
1228 xmlXPathObjectPtr init = NULL;
1229 int stack = 0;
1230
1231 xmlXPathInit();
1232
1233 if ((ctx == NULL) || (str == NULL))
1234 return(NULL);
1235
1236 if (xmlXPathDebug == NULL)
1237 xmlXPathDebug = stderr;
1238 ctxt = xmlXPathNewParserContext(str, ctx);
1239 if (ctx->node != NULL) {
1240 init = xmlXPathNewNodeSet(ctx->node);
1241 valuePush(ctxt, init);
1242 }
1243 xmlXPtrEvalXPointer(ctxt);
1244
1245 if ((ctxt->value != NULL) &&
1246 (ctxt->value->type != XPATH_NODESET) &&
1247 (ctxt->value->type != XPATH_LOCATIONSET)) {
1248 fprintf(xmlXPathDebug,
1249 "xmlXPtrEval: evaluation failed to return a node set\n");
1250 } else {
1251 res = valuePop(ctxt);
1252 }
1253
1254 do {
1255 tmp = valuePop(ctxt);
1256 if (tmp != NULL) {
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001257 if (tmp != init) {
1258 if (tmp->type == XPATH_NODESET) {
1259 /*
1260 * Evaluation may push a root nodeset which is unused
1261 */
1262 xmlNodeSetPtr set;
1263 set = tmp->nodesetval;
1264 if ((set->nodeNr != 1) ||
1265 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1266 stack++;
1267 } else
1268 stack++;
1269 }
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001270 xmlXPathFreeObject(tmp);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001271 }
1272 } while (tmp != NULL);
1273 if (stack != 0) {
1274 fprintf(xmlXPathDebug, "xmlXPtrEval: %d object left on the stack\n",
1275 stack);
1276 }
1277 if (ctxt->error != XPATH_EXPRESSION_OK) {
1278 xmlXPathFreeObject(res);
1279 res = NULL;
1280 }
1281
1282 xmlXPathFreeParserContext(ctxt);
1283 return(res);
1284}
1285
1286
1287/************************************************************************
1288 * *
1289 * XPointer functions *
1290 * *
1291 ************************************************************************/
1292
1293/**
1294 * xmlXPtrNbLocChildren:
1295 * @node: an xmlNodePtr
1296 *
1297 * Count the number of location children of @node or the lenght of the
1298 * string value in case of text/PI/Comments nodes
1299 *
1300 * Returns the number of location children
1301 */
1302int
1303xmlXPtrNbLocChildren(xmlNodePtr node) {
1304 int ret = 0;
1305 if (node == NULL)
1306 return(-1);
1307 switch (node->type) {
1308 case XML_HTML_DOCUMENT_NODE:
1309 case XML_DOCUMENT_NODE:
1310 case XML_ELEMENT_NODE:
1311 node = node->children;
1312 while (node != NULL) {
1313 if (node->type == XML_ELEMENT_NODE)
1314 ret++;
1315 node = node->next;
1316 }
1317 break;
1318 case XML_ATTRIBUTE_NODE:
1319 return(-1);
1320
1321 case XML_PI_NODE:
1322 case XML_COMMENT_NODE:
1323 case XML_TEXT_NODE:
1324 case XML_CDATA_SECTION_NODE:
1325 case XML_ENTITY_REF_NODE:
1326#ifndef XML_USE_BUFFER_CONTENT
1327 ret = xmlStrlen(node->content);
1328#else
1329 ret = xmlBufferLength(node->content);
1330#endif
1331 break;
1332 default:
1333 return(-1);
1334 }
1335 return(ret);
1336}
1337
1338/**
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001339 * xmlXPtrHereFunction:
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001340 * @ctxt: the XPointer Parser context
1341 *
1342 * Function implementing here() operation
1343 * as described in 5.4.3
1344 */
1345void
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001346xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001347 if (ctxt->context->here == NULL)
1348 XP_ERROR(XPTR_SYNTAX_ERROR);
1349
1350 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1351}
1352
1353/**
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001354 * xmlXPtrOriginFunction:
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001355 * @ctxt: the XPointer Parser context
1356 *
1357 * Function implementing origin() operation
1358 * as described in 5.4.3
1359 */
1360void
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001361xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001362 if (ctxt->context->origin == NULL)
1363 XP_ERROR(XPTR_SYNTAX_ERROR);
1364
1365 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1366}
1367
1368/**
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001369 * xmlXPtrStartPointFunction:
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001370 * @ctxt: the XPointer Parser context
1371 *
1372 * Function implementing start-point() operation
1373 * as described in 5.4.3
1374 * ----------------
1375 * location-set start-point(location-set)
1376 *
1377 * For each location x in the argument location-set, start-point adds a
1378 * location of type point to the result location-set. That point represents
1379 * the start point of location x and is determined by the following rules:
1380 *
1381 * - If x is of type point, the start point is x.
1382 * - If x is of type range, the start point is the start point of x.
1383 * - If x is of type root, element, text, comment, or processing instruction,
1384 * - the container node of the start point is x and the index is 0.
1385 * - If x is of type attribute or namespace, the function must signal a
1386 * syntax error.
1387 * ----------------
1388 *
1389 */
1390void
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001391xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001392 xmlXPathObjectPtr tmp, obj, point;
1393 xmlLocationSetPtr newset = NULL;
1394 xmlLocationSetPtr oldset = NULL;
1395
1396 CHECK_ARITY(1);
1397 if ((ctxt->value == NULL) ||
1398 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1399 (ctxt->value->type != XPATH_NODESET)))
1400 XP_ERROR(XPATH_INVALID_TYPE)
1401
1402 obj = valuePop(ctxt);
1403 if (obj->type == XPATH_NODESET) {
1404 /*
1405 * First convert to a location set
1406 */
1407 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1408 xmlXPathFreeObject(obj);
1409 obj = tmp;
1410 }
1411
1412 newset = xmlXPtrLocationSetCreate(NULL);
1413 oldset = (xmlLocationSetPtr) obj->user;
1414 if (oldset != NULL) {
1415 int i;
1416
1417 for (i = 0; i < oldset->locNr; i++) {
1418 tmp = oldset->locTab[i];
1419 if (tmp == NULL)
1420 continue;
1421 point = NULL;
1422 switch (tmp->type) {
1423 case XPATH_POINT:
1424 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1425 break;
1426 case XPATH_RANGE: {
1427 xmlNodePtr node = tmp->user;
1428 if (node != NULL) {
1429 if (node->type == XML_ATTRIBUTE_NODE) {
1430 /* TODO: Namespace Nodes ??? */
1431 xmlXPathFreeObject(obj);
1432 xmlXPtrFreeLocationSet(newset);
1433 XP_ERROR(XPTR_SYNTAX_ERROR);
1434 }
1435 point = xmlXPtrNewPoint(node, tmp->index);
1436 }
1437 if (tmp->user2 == NULL) {
1438 point = xmlXPtrNewPoint(node, 0);
1439 } else
1440 point = xmlXPtrNewPoint(node, tmp->index);
1441 break;
1442 }
1443 default:
1444 /*** Should we raise an error ?
1445 xmlXPathFreeObject(obj);
1446 xmlXPathFreeObject(newset);
1447 XP_ERROR(XPATH_INVALID_TYPE)
1448 ***/
1449 break;
1450 }
1451 if (point != NULL)
1452 xmlXPtrLocationSetAdd(newset, point);
1453 }
1454 }
1455 xmlXPathFreeObject(obj);
1456}
1457
1458/**
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001459 * xmlXPtrEndPointFunction:
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001460 * @ctxt: the XPointer Parser context
1461 *
1462 * Function implementing end-point() operation
1463 * as described in 5.4.3
1464 * ----------------------------
1465 * location-set end-point(location-set)
1466 *
1467 * For each location x in the argument location-set, end-point adds a
1468 * location of type point to the result location-set. That point representsi
1469 * the end point of location x and is determined by the following rules:
1470 *
1471 * - If x is of type point, the resulting point is x.
1472 * - If x is of type range, the resulting point is the end point of x.
1473 * - If x is of type root or element, the container node of the resulting
1474 * point is x and the index is the number of location children of x.
1475 * - If x is of type text, comment, or processing instruction, the container
1476 * node of the resulting point is x and the index is the length of thei
1477 * string-value of x.
1478 * - If x is of type attribute or namespace, the function must signal a
1479 * syntax error.
1480 * ----------------------------
1481 */
1482void
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001483xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001484 xmlXPathObjectPtr tmp, obj, point;
1485 xmlLocationSetPtr newset = NULL;
1486 xmlLocationSetPtr oldset = NULL;
1487
1488 CHECK_ARITY(1);
1489 if ((ctxt->value == NULL) ||
1490 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1491 (ctxt->value->type != XPATH_NODESET)))
1492 XP_ERROR(XPATH_INVALID_TYPE)
1493
1494 obj = valuePop(ctxt);
1495 if (obj->type == XPATH_NODESET) {
1496 /*
1497 * First convert to a location set
1498 */
1499 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1500 xmlXPathFreeObject(obj);
1501 obj = tmp;
1502 }
1503
1504 newset = xmlXPtrLocationSetCreate(NULL);
1505 oldset = (xmlLocationSetPtr) obj->user;
1506 if (oldset != NULL) {
1507 int i;
1508
1509 for (i = 0; i < oldset->locNr; i++) {
1510 tmp = oldset->locTab[i];
1511 if (tmp == NULL)
1512 continue;
1513 point = NULL;
1514 switch (tmp->type) {
1515 case XPATH_POINT:
1516 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1517 break;
1518 case XPATH_RANGE: {
1519 xmlNodePtr node = tmp->user;
1520 if (node != NULL) {
1521 if (node->type == XML_ATTRIBUTE_NODE) {
1522 /* TODO: Namespace Nodes ??? */
1523 xmlXPathFreeObject(obj);
1524 xmlXPtrFreeLocationSet(newset);
1525 XP_ERROR(XPTR_SYNTAX_ERROR);
1526 }
1527 point = xmlXPtrNewPoint(node, tmp->index);
1528 }
1529 if (tmp->user2 == NULL) {
1530 point = xmlXPtrNewPoint(node,
1531 xmlXPtrNbLocChildren(node));
1532 } else
1533 point = xmlXPtrNewPoint(node, tmp->index);
1534 break;
1535 }
1536 default:
1537 /*** Should we raise an error ?
1538 xmlXPathFreeObject(obj);
1539 xmlXPathFreeObject(newset);
1540 XP_ERROR(XPATH_INVALID_TYPE)
1541 ***/
1542 break;
1543 }
1544 if (point != NULL)
1545 xmlXPtrLocationSetAdd(newset, point);
1546 }
1547 }
1548 xmlXPathFreeObject(obj);
1549}
1550
Daniel Veillard1baf4122000-10-15 20:38:39 +00001551
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001552/**
1553 * xmlXPtrCoveringRange:
1554 * @ctxt: the XPointer Parser context
Daniel Veillard1baf4122000-10-15 20:38:39 +00001555 * @loc: the location for which the covering range must be computed
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001556 *
Daniel Veillard1baf4122000-10-15 20:38:39 +00001557 * A covering range is a range that wholly encompasses a location
1558 * Section 5.3.3. Covering Ranges for All Location Types
1559 * http://www.w3.org/TR/xptr#N2267
1560 *
1561 * Returns a new location or NULL in case of error
1562 */
1563xmlXPathObjectPtr
1564xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1565 if (loc == NULL)
1566 return(NULL);
1567 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1568 (ctxt->context->doc == NULL))
1569 return(NULL);
1570 switch (loc->type) {
1571 case XPATH_POINT:
1572 return(xmlXPtrNewRange(loc->user, loc->index,
1573 loc->user, loc->index));
1574 case XPATH_RANGE:
1575 if (loc->user2 != NULL) {
1576 return(xmlXPtrNewRange(loc->user, loc->index,
1577 loc->user2, loc->index2));
1578 } else {
1579 xmlNodePtr node = (xmlNodePtr) loc->user;
1580 if (node == (xmlNodePtr) ctxt->context->doc) {
1581 return(xmlXPtrNewRange(node, 0, node,
1582 xmlXPtrGetArity(node)));
1583 } else {
1584 switch (node->type) {
1585 case XML_ATTRIBUTE_NODE:
1586 /* !!! our model is slightly different than XPath */
1587 return(xmlXPtrNewRange(node, 0, node,
1588 xmlXPtrGetArity(node)));
1589 case XML_ELEMENT_NODE:
1590 case XML_TEXT_NODE:
1591 case XML_CDATA_SECTION_NODE:
1592 case XML_ENTITY_REF_NODE:
1593 case XML_PI_NODE:
1594 case XML_COMMENT_NODE:
1595 case XML_DOCUMENT_NODE:
1596 case XML_NOTATION_NODE:
1597 case XML_HTML_DOCUMENT_NODE: {
1598 int index = xmlXPtrGetIndex(node);
1599
1600 node = node->parent;
1601 return(xmlXPtrNewRange(node, index - 1,
1602 node, index + 1));
1603 }
1604 default:
1605 return(NULL);
1606 }
1607 }
1608 }
1609 default:
1610 TODO /* missed one case ??? */
1611 }
1612 return(NULL);
1613}
1614
1615/**
1616 * xmlXPtrRangeFunction:
1617 * @ctxt: the XPointer Parser context
1618 *
1619 * Function implementing the range() function 5.4.3
1620 * location-set range(location-set )
1621 *
1622 * The range function returns ranges covering the locations in
1623 * the argument location-set. For each location x in the argument
1624 * location-set, a range location representing the covering range of
1625 * x is added to the result location-set.
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001626 */
1627void
Daniel Veillard1baf4122000-10-15 20:38:39 +00001628xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1629 int i;
1630 xmlXPathObjectPtr set;
1631 xmlLocationSetPtr oldset;
1632 xmlLocationSetPtr newset;
1633
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001634 CHECK_ARITY(1);
Daniel Veillard1baf4122000-10-15 20:38:39 +00001635 if ((ctxt->value == NULL) ||
1636 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1637 (ctxt->value->type != XPATH_NODESET)))
1638 XP_ERROR(XPATH_INVALID_TYPE)
1639
1640 set = valuePop(ctxt);
1641 if (set->type == XPATH_NODESET) {
1642 xmlXPathObjectPtr tmp;
1643
1644 /*
1645 * First convert to a location set
1646 */
1647 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1648 xmlXPathFreeObject(set);
1649 set = tmp;
1650 }
1651 oldset = (xmlLocationSetPtr) set->user;
1652
1653 /*
1654 * The loop is to compute the covering range for each item and add it
1655 */
1656 newset = xmlXPtrLocationSetCreate(NULL);
1657 for (i = 0;i < oldset->locNr;i++) {
1658 xmlXPtrLocationSetAdd(newset,
1659 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
1660 }
1661
1662 /*
1663 * Save the new value and cleanup
1664 */
1665 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1666 xmlXPathFreeObject(set);
1667}
1668
1669/**
1670 * xmlXPtrInsideRange:
1671 * @ctxt: the XPointer Parser context
1672 * @loc: the location for which the inside range must be computed
1673 *
1674 * A inside range is a range described in the range-inside() description
1675 *
1676 * Returns a new location or NULL in case of error
1677 */
1678xmlXPathObjectPtr
1679xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1680 if (loc == NULL)
1681 return(NULL);
1682 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1683 (ctxt->context->doc == NULL))
1684 return(NULL);
1685 switch (loc->type) {
1686 case XPATH_POINT: {
1687 xmlNodePtr node = (xmlNodePtr) loc->user;
1688 switch (node->type) {
1689 case XML_PI_NODE:
1690 case XML_COMMENT_NODE:
1691 case XML_TEXT_NODE:
1692 case XML_CDATA_SECTION_NODE: {
1693 if (node->content == NULL) {
1694 return(xmlXPtrNewRange(node, 0, node, 0));
1695 } else {
1696 return(xmlXPtrNewRange(node, 0, node,
1697 xmlStrlen(node->content)));
1698 }
1699 }
1700 case XML_ATTRIBUTE_NODE:
1701 case XML_ELEMENT_NODE:
1702 case XML_ENTITY_REF_NODE:
1703 case XML_DOCUMENT_NODE:
1704 case XML_NOTATION_NODE:
1705 case XML_HTML_DOCUMENT_NODE: {
1706 return(xmlXPtrNewRange(node, 0, node,
1707 xmlXPtrGetArity(node)));
1708 }
1709 default:
1710 return(NULL);
1711 }
1712 return(NULL);
1713 }
1714 case XPATH_RANGE: {
1715 xmlNodePtr node = (xmlNodePtr) loc->user;
1716 if (loc->user2 != NULL) {
1717 return(xmlXPtrNewRange(node, loc->index,
1718 loc->user2, loc->index2));
1719 } else {
1720 switch (node->type) {
1721 case XML_PI_NODE:
1722 case XML_COMMENT_NODE:
1723 case XML_TEXT_NODE:
1724 case XML_CDATA_SECTION_NODE: {
1725 if (node->content == NULL) {
1726 return(xmlXPtrNewRange(node, 0, node, 0));
1727 } else {
1728 return(xmlXPtrNewRange(node, 0, node,
1729 xmlStrlen(node->content)));
1730 }
1731 }
1732 case XML_ATTRIBUTE_NODE:
1733 case XML_ELEMENT_NODE:
1734 case XML_ENTITY_REF_NODE:
1735 case XML_DOCUMENT_NODE:
1736 case XML_NOTATION_NODE:
1737 case XML_HTML_DOCUMENT_NODE: {
1738 return(xmlXPtrNewRange(node, 0, node,
1739 xmlXPtrGetArity(node)));
1740 }
1741 default:
1742 return(NULL);
1743 }
1744 return(NULL);
1745 }
1746 }
1747 default:
1748 TODO /* missed one case ??? */
1749 }
1750 return(NULL);
1751}
1752
1753/**
1754 * xmlXPtrRangeInsideFunction:
1755 * @ctxt: the XPointer Parser context
1756 *
1757 * Function implementing the range-inside() function 5.4.3
1758 * location-set range-inside(location-set )
1759 *
1760 * The range-inside function returns ranges covering the contents of
1761 * the locations in the argument location-set. For each location x in
1762 * the argument location-set, a range location is added to the result
1763 * location-set. If x is a range location, then x is added to the
1764 * result location-set. If x is not a range location, then x is used
1765 * as the container location of the start and end points of the range
1766 * location to be added; the index of the start point of the range is
1767 * zero; if the end point is a character point then its index is the
1768 * length of the string-value of x, and otherwise is the number of
1769 * location children of x.
1770 *
1771 */
1772void
1773xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1774 int i;
1775 xmlXPathObjectPtr set;
1776 xmlLocationSetPtr oldset;
1777 xmlLocationSetPtr newset;
1778
1779 CHECK_ARITY(1);
1780 if ((ctxt->value == NULL) ||
1781 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1782 (ctxt->value->type != XPATH_NODESET)))
1783 XP_ERROR(XPATH_INVALID_TYPE)
1784
1785 set = valuePop(ctxt);
1786 if (set->type == XPATH_NODESET) {
1787 xmlXPathObjectPtr tmp;
1788
1789 /*
1790 * First convert to a location set
1791 */
1792 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1793 xmlXPathFreeObject(set);
1794 set = tmp;
1795 }
1796 oldset = (xmlLocationSetPtr) set->user;
1797
1798 /*
1799 * The loop is to compute the covering range for each item and add it
1800 */
1801 newset = xmlXPtrLocationSetCreate(NULL);
1802 for (i = 0;i < oldset->locNr;i++) {
1803 xmlXPtrLocationSetAdd(newset,
1804 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
1805 }
1806
1807 /*
1808 * Save the new value and cleanup
1809 */
1810 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1811 xmlXPathFreeObject(set);
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00001812}
1813
1814/**
1815 * xmlXPtrRangeToFunction:
1816 * @ctxt: the XPointer Parser context
1817 *
1818 * Implement the range-to() XPointer function
1819 */
1820void
1821xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1822 xmlXPathObjectPtr range;
1823 const xmlChar *cur;
1824 xmlXPathObjectPtr res, obj;
1825 xmlXPathObjectPtr tmp;
1826 xmlLocationSetPtr newset = NULL;
1827 xmlNodeSetPtr oldset;
1828 int i;
1829
1830 CHECK_ARITY(1);
1831 /*
1832 * Save the expression pointer since we will have to evaluate
1833 * it multiple times. Initialize the new set.
1834 */
1835 CHECK_TYPE(XPATH_NODESET);
1836 obj = valuePop(ctxt);
1837 oldset = obj->nodesetval;
1838 ctxt->context->node = NULL;
1839
1840 cur = ctxt->cur;
1841 newset = xmlXPtrLocationSetCreate(NULL);
1842
1843 for (i = 0; i < oldset->nodeNr; i++) {
1844 ctxt->cur = cur;
1845
1846 /*
1847 * Run the evaluation with a node list made of a single item
1848 * in the nodeset.
1849 */
1850 ctxt->context->node = oldset->nodeTab[i];
1851 tmp = xmlXPathNewNodeSet(ctxt->context->node);
1852 valuePush(ctxt, tmp);
1853
1854 xmlXPathEvalExpr(ctxt);
1855 CHECK_ERROR;
1856
1857 /*
1858 * The result of the evaluation need to be tested to
1859 * decided whether the filter succeeded or not
1860 */
1861 res = valuePop(ctxt);
1862 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
1863 if (range != NULL) {
1864 xmlXPtrLocationSetAdd(newset, range);
1865 }
1866
1867 /*
1868 * Cleanup
1869 */
1870 if (res != NULL)
1871 xmlXPathFreeObject(res);
1872 if (ctxt->value == tmp) {
1873 res = valuePop(ctxt);
1874 xmlXPathFreeObject(res);
1875 }
1876
1877 ctxt->context->node = NULL;
1878 }
1879
1880 /*
1881 * The result is used as the new evaluation set.
1882 */
1883 xmlXPathFreeObject(obj);
1884 ctxt->context->node = NULL;
1885 ctxt->context->contextSize = -1;
1886 ctxt->context->proximityPosition = -1;
1887 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1888}
1889
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00001890/**
1891 * xmlXPtrAdvanceNode:
1892 * @cur: the node
1893 *
1894 * Advance to the next element or text node in document order
1895 * TODO: add a stack for entering/exiting entities
1896 *
1897 * Returns -1 in case of failure, 0 otherwise
1898 */
1899xmlNodePtr
1900xmlXPtrAdvanceNode(xmlNodePtr cur) {
1901next:
1902 if (cur == NULL)
1903 return(NULL);
1904 if (cur->children != NULL) {
1905 cur = cur->children ;
1906 goto found;
1907 }
1908 if (cur->next != NULL) {
1909 cur = cur->next;
1910 goto found;
1911 }
1912 do {
1913 cur = cur->parent;
1914 if (cur == NULL) return(NULL);
1915 if (cur->next != NULL) {
1916 cur = cur->next;
1917 goto found;
1918 }
1919 } while (cur != NULL);
1920
1921found:
1922 if ((cur->type != XML_ELEMENT_NODE) &&
1923 (cur->type != XML_TEXT_NODE) &&
1924 (cur->type != XML_DOCUMENT_NODE) &&
1925 (cur->type != XML_HTML_DOCUMENT_NODE) &&
1926 (cur->type != XML_CDATA_SECTION_NODE))
1927 goto next;
1928 if (cur->type == XML_ENTITY_REF_NODE) {
1929 TODO
1930 }
1931 return(cur);
1932}
1933
1934/**
1935 * xmlXPtrAdvanceChar:
1936 * @node: the node
1937 * @index: the index
1938 * @bytes: the number of bytes
1939 *
1940 * Advance a point of the associated number of bytes (not UTF8 chars)
1941 *
1942 * Returns -1 in case of failure, 0 otherwise
1943 */
1944int
1945xmlXPtrAdvanceChar(xmlNodePtr *node, int *index, int bytes) {
1946 xmlNodePtr cur;
1947 int pos;
1948 int len;
1949
1950 if ((node == NULL) || (index == NULL))
1951 return(-1);
1952 cur = *node;
1953 if (cur == NULL)
1954 return(-1);
1955 pos = *index;
1956
1957 while (bytes >= 0) {
1958 /*
1959 * First position to the beginning of the first text node
1960 * corresponding to this point
1961 */
1962 while ((cur != NULL) &&
1963 ((cur->type == XML_ELEMENT_NODE) ||
1964 (cur->type == XML_DOCUMENT_NODE) ||
1965 (cur->type == XML_HTML_DOCUMENT_NODE))) {
1966 if (pos > 0) {
1967 cur = xmlXPtrGetNthChild(cur, pos);
1968 pos = 0;
1969 } else {
1970 cur = xmlXPtrAdvanceNode(cur);
1971 pos = 0;
1972 }
1973 }
1974 if (cur == NULL) {
1975 *node = NULL;
1976 *index = 0;
1977 return(-1);
1978 }
1979
1980 /*
1981 * if there is no move needed return the current value.
1982 */
1983 if (pos == 0) pos = 1;
1984 if (bytes == 0) {
1985 *node = cur;
1986 *index = pos;
1987 return(0);
1988 }
1989 /*
1990 * We should have a text (or cdata) node ...
1991 */
1992 len = 0;
1993 if (cur->content != NULL) {
1994 len = xmlStrlen(cur->content);
1995 }
1996 if (pos > len) {
1997 /* Strange, the index in the text node is greater than it's len */
1998 STRANGE
1999 pos = len;
2000 }
2001 if (pos + bytes >= len) {
2002 bytes -= (len - pos);
2003 cur = xmlXPtrAdvanceNode(cur);
2004 cur = 0;
2005 }
2006 }
2007 return(-1);
2008}
2009
2010/**
2011 * xmlXPtrMatchString:
2012 * @string: the string to search
2013 * @start: the start textnode
2014 * @startindex: the start index
2015 * @end: the end textnode IN/OUT
2016 * @endindex: the end index IN/OUT
2017 *
2018 * Check whether the document contains @string at the position
2019 * (@start, @startindex) and limited by the (@end, @endindex) point
2020 *
2021 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2022 * (@start, @startindex) will indicate the position of the beginning
2023 * of the range and (@end, @endindex) will endicate the end
2024 * of the range
2025 */
2026int
2027xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2028 xmlNodePtr *end, int *endindex) {
2029 xmlNodePtr cur;
2030 int pos; /* 0 based */
2031 int len; /* in bytes */
2032 int stringlen; /* in bytes */
2033 int match;
2034
2035 if (string == NULL)
2036 return(-1);
2037 if (start == NULL)
2038 return(-1);
2039 if ((end == NULL) || (endindex == NULL))
2040 return(-1);
2041 cur = start;
2042 if (cur == NULL)
2043 return(-1);
2044 pos = startindex - 1;
2045 stringlen = xmlStrlen(string);
2046
2047 while (stringlen > 0) {
2048 if ((cur == *end) && (pos + stringlen > *endindex))
2049 return(0);
2050 if (cur->content != NULL) {
2051 len = xmlStrlen(cur->content);
2052 if (len >= pos + stringlen) {
2053 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2054 if (match) {
2055#ifdef DEBUG_RANGES
2056 fprintf(stdout, "found range %d bytes at index %d of ->",
2057 stringlen, pos + 1);
2058 xmlDebugDumpString(stdout, cur->content);
2059 fprintf(stdout, "\n");
2060#endif
2061 *end = cur;
2062 *endindex = pos + stringlen;
2063 return(1);
2064 } else {
2065 return(0);
2066 }
2067 } else {
2068 int sub = len - pos;
2069 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2070 if (match) {
2071#ifdef DEBUG_RANGES
2072 fprintf(stdout, "found subrange %d bytes at index %d of ->",
2073 sub, pos + 1);
2074 xmlDebugDumpString(stdout, cur->content);
2075 fprintf(stdout, "\n");
2076#endif
2077 string = &string[sub];
2078 stringlen -= sub;
2079 } else {
2080 return(0);
2081 }
2082 }
2083 }
2084 cur = xmlXPtrAdvanceNode(cur);
2085 if (cur == NULL)
2086 return(0);
2087 pos = 0;
2088 }
2089 return(1);
2090}
2091
2092/**
2093 * xmlXPtrSearchString:
2094 * @string: the string to search
2095 * @start: the start textnode IN/OUT
2096 * @startindex: the start index IN/OUT
2097 * @end: the end textnode
2098 * @endindex: the end index
2099 *
2100 * Search the next occurence of @string within the document content
2101 * until the (@end, @endindex) point is reached
2102 *
2103 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2104 * (@start, @startindex) will indicate the position of the beginning
2105 * of the range and (@end, @endindex) will endicate the end
2106 * of the range
2107 */
2108int
2109xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2110 xmlNodePtr *end, int *endindex) {
2111 xmlNodePtr cur;
2112 const xmlChar *str;
2113 int pos; /* 0 based */
2114 int len; /* in bytes */
2115 int stringlen; /* in bytes */
2116 xmlChar first;
2117
2118 if (string == NULL)
2119 return(-1);
2120 if ((start == NULL) || (startindex == NULL))
2121 return(-1);
2122 if ((end == NULL) || (endindex == NULL))
2123 return(-1);
2124 cur = *start;
2125 if (cur == NULL)
2126 return(-1);
2127 pos = *startindex - 1;
2128 first = string[0];
2129 stringlen = xmlStrlen(string);
2130
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00002131 while (cur != NULL) {
2132 if (cur->content != NULL) {
2133 len = xmlStrlen(cur->content);
2134 while (pos <= len) {
Daniel Veillardff9c3302000-10-13 16:38:25 +00002135 if (first != 0) {
2136 str = xmlStrchr(&cur->content[pos], first);
2137 if (str != NULL) {
2138 pos = (str - cur->content);
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00002139#ifdef DEBUG_RANGES
Daniel Veillardff9c3302000-10-13 16:38:25 +00002140 fprintf(stdout, "found '%c' at index %d of ->",
2141 first, pos + 1);
2142 xmlDebugDumpString(stdout, cur->content);
2143 fprintf(stdout, "\n");
2144#endif
2145 if (xmlXPtrMatchString(string, cur, pos + 1,
2146 end, endindex)) {
2147 *start = cur;
2148 *startindex = pos + 1;
2149 return(1);
2150 }
2151 pos++;
2152 } else {
2153 pos = len + 1;
2154 }
2155 } else {
2156 /*
2157 * An empty string is considered to match before each
2158 * character of the string-value and after the final
2159 * character.
2160 */
2161#ifdef DEBUG_RANGES
2162 fprintf(stdout, "found '' at index %d of ->",
2163 pos + 1);
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00002164 xmlDebugDumpString(stdout, cur->content);
2165 fprintf(stdout, "\n");
2166#endif
Daniel Veillardff9c3302000-10-13 16:38:25 +00002167 *start = cur;
2168 *startindex = pos + 1;
2169 *end = cur;
2170 *endindex = pos + 1;
2171 return(1);
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00002172 }
2173 }
2174 }
2175 if ((cur == *end) && (pos >= *endindex))
2176 return(0);
2177 cur = xmlXPtrAdvanceNode(cur);
2178 if (cur == NULL)
2179 return(0);
2180 pos = 1;
2181 }
2182 return(0);
2183}
2184
2185/**
2186 * xmlXPtrGetLastChar:
2187 * @node: the node
2188 * @index: the index
2189 *
2190 * Computes the point coordinates of the last char of this point
2191 *
2192 * Returns -1 in case of failure, 0 otherwise
2193 */
2194int
2195xmlXPtrGetLastChar(xmlNodePtr *node, int *index) {
2196 xmlNodePtr cur;
2197 int pos, len = 0;
2198
2199 if ((node == NULL) || (index == NULL))
2200 return(-1);
2201 cur = *node;
2202 pos = *index;
2203
2204 if (cur == NULL)
2205 return(-1);
2206
2207 if ((cur->type == XML_ELEMENT_NODE) ||
2208 (cur->type == XML_DOCUMENT_NODE) ||
2209 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2210 if (pos > 0) {
2211 cur = xmlXPtrGetNthChild(cur, pos);
2212 pos = 0;
2213 }
2214 }
2215 while (cur != NULL) {
2216 if (cur->last != NULL)
2217 cur = cur->last;
2218 else if (cur->content != NULL) {
2219 len = xmlStrlen(cur->content);
2220 break;
2221 }
2222 }
2223 if (cur == NULL)
2224 return(-1);
2225 *node = cur;
2226 *index = len;
2227 return(0);
2228}
2229
2230/**
2231 * xmlXPtrGetStartPoint:
2232 * @obj: an range
2233 * @node: the resulting node
2234 * @index: the resulting index
2235 *
2236 * read the object and return the start point coordinates.
2237 *
2238 * Returns -1 in case of failure, 0 otherwise
2239 */
2240int
2241xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *index) {
2242 if ((obj == NULL) || (node == NULL) || (index == NULL))
2243 return(-1);
2244
2245 switch (obj->type) {
2246 case XPATH_POINT:
2247 *node = obj->user;
2248 if (obj->index <= 0)
2249 *index = 0;
2250 else
2251 *index = obj->index;
2252 return(0);
2253 case XPATH_RANGE:
2254 *node = obj->user;
2255 if (obj->index <= 0)
2256 *index = 0;
2257 else
2258 *index = obj->index;
2259 return(0);
2260 default:
2261 return(-1);
2262 }
2263 return(-1);
2264}
2265
2266/**
2267 * xmlXPtrGetEndPoint:
2268 * @obj: an range
2269 * @node: the resulting node
2270 * @index: the resulting index
2271 *
2272 * read the object and return the end point coordinates.
2273 *
2274 * Returns -1 in case of failure, 0 otherwise
2275 */
2276int
2277xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *index) {
2278 if ((obj == NULL) || (node == NULL) || (index == NULL))
2279 return(-1);
2280
2281 switch (obj->type) {
2282 case XPATH_POINT:
2283 *node = obj->user;
2284 if (obj->index <= 0)
2285 *index = 0;
2286 else
2287 *index = obj->index;
2288 return(0);
2289 case XPATH_RANGE:
2290 *node = obj->user;
2291 if (obj->index <= 0)
2292 *index = 0;
2293 else
2294 *index = obj->index;
2295 return(0);
2296 default:
2297 return(-1);
2298 }
2299 return(-1);
2300}
2301
2302/**
2303 * xmlXPtrStringRangeFunction:
2304 * @ctxt: the XPointer Parser context
2305 *
2306 * Function implementing the string-range() function
2307 * range as described in 5.4.2
2308 *
2309 * ------------------------------
2310 * [Definition: For each location in the location-set argument,
2311 * string-range returns a set of string ranges, a set of substrings in a
2312 * string. Specifically, the string-value of the location is searched for
2313 * substrings that match the string argument, and the resulting location-set
2314 * will contain a range location for each non-overlapping match.]
2315 * An empty string is considered to match before each character of the
2316 * string-value and after the final character. Whitespace in a string
2317 * is matched literally, with no normalization except that provided by
2318 * XML for line ends. The third argument gives the position of the first
2319 * character to be in the resulting range, relative to the start of the
2320 * match. The default value is 1, which makes the range start immediately
2321 * before the first character of the matched string. The fourth argument
2322 * gives the number of characters in the range; the default is that the
2323 * range extends to the end of the matched string.
2324 *
2325 * Element boundaries, as well as entire embedded nodes such as processing
2326 * instructions and comments, are ignored as defined in [XPath].
2327 *
2328 * If the string in the second argument is not found in the string-value
2329 * of the location, or if a value in the third or fourth argument indicates
2330 * a string that is beyond the beginning or end of the document, the
2331 * expression fails.
2332 *
2333 * The points of the range-locations in the returned location-set will
2334 * all be character points.
2335 * ------------------------------
2336 */
2337void
2338xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2339 int i, startindex, endindex, fendindex;
2340 xmlNodePtr start, end, fend;
2341 xmlXPathObjectPtr set;
2342 xmlLocationSetPtr oldset;
2343 xmlLocationSetPtr newset;
2344 xmlXPathObjectPtr string;
2345 xmlXPathObjectPtr position = NULL;
2346 xmlXPathObjectPtr number = NULL;
2347 int found;
2348
2349 /*
2350 * Grab the arguments
2351 */
2352 if ((nargs < 2) || (nargs > 4))
2353 XP_ERROR(XPATH_INVALID_ARITY);
2354
2355 if (nargs >= 4) {
2356 CHECK_TYPE(XPATH_NUMBER);
2357 number = valuePop(ctxt);
2358 }
2359 if (nargs >= 3) {
2360 CHECK_TYPE(XPATH_NUMBER);
2361 position = valuePop(ctxt);
2362 }
2363 CHECK_TYPE(XPATH_STRING);
2364 string = valuePop(ctxt);
2365 if ((ctxt->value == NULL) ||
2366 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2367 (ctxt->value->type != XPATH_NODESET)))
2368 XP_ERROR(XPATH_INVALID_TYPE)
2369
2370 set = valuePop(ctxt);
2371 if (set->type == XPATH_NODESET) {
2372 xmlXPathObjectPtr tmp;
2373
2374 /*
2375 * First convert to a location set
2376 */
2377 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2378 xmlXPathFreeObject(set);
2379 set = tmp;
2380 }
2381 oldset = (xmlLocationSetPtr) set->user;
2382
2383 /*
2384 * The loop is to search for each element in the location set
2385 * the list of location set corresponding to that search
2386 */
2387 newset = xmlXPtrLocationSetCreate(NULL);
2388 for (i = 0;i < oldset->locNr;i++) {
2389#ifdef DEBUG_RANGES
2390 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2391#endif
2392
2393 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2394 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2395 xmlXPtrAdvanceChar(&start, &startindex, 0);
2396 xmlXPtrGetLastChar(&end, &endindex);
2397
2398#ifdef DEBUG_RANGES
2399 fprintf(stdout, "from index %d of ->", startindex);
2400 xmlDebugDumpString(stdout, start->content);
2401 fprintf(stdout, "\n");
2402 fprintf(stdout, "to index %d of ->", endindex);
2403 xmlDebugDumpString(stdout, end->content);
2404 fprintf(stdout, "\n");
2405#endif
2406 do {
2407 fend = end;
2408 fendindex = endindex;
2409 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2410 &fend, &fendindex);
2411 if (found == 1) {
2412 xmlXPtrLocationSetAdd(newset,
2413 xmlXPtrNewRange(start, startindex, fend, fendindex));
2414 start = fend;
2415 startindex = fendindex;
Daniel Veillardff9c3302000-10-13 16:38:25 +00002416 if (string->stringval[0] == 0)
2417 startindex++;
Daniel Veillardc2df4cd2000-10-12 23:15:24 +00002418 }
2419 } while (found == 1);
2420 }
2421
2422 /*
2423 * Save the new value and cleanup
2424 */
2425 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2426 xmlXPathFreeObject(set);
2427 xmlXPathFreeObject(string);
2428 if (position) xmlXPathFreeObject(position);
2429 if (number) xmlXPathFreeObject(number);
2430}
2431
Daniel Veillarde8eac3d2000-10-11 08:55:02 +00002432#else
2433#endif
2434