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