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