blob: 2bc29cbe1460d3f19a04145e435e1d7785128874 [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 ... */ ) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001424 if ((cur->type == XML_TEXT_NODE) ||
1425 (cur->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001426 const xmlChar *content = cur->content;
1427
1428 if (content == NULL) {
1429 tmp = xmlNewTextLen(NULL, 0);
1430 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001431 if (index1 > 1) {
1432 content += (index1 - 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001433 }
1434 tmp = xmlNewText(content);
1435 }
1436 last = list = tmp;
1437 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001438 if ((cur == start) && (index1 > 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001439 tmp = xmlCopyNode(cur, 0);
1440 list = tmp;
1441 parent = tmp;
1442 last = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001443 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1444 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001445 /*
1446 * Now gather the remaining nodes from cur to end
1447 */
1448 continue; /* while */
1449 }
1450 tmp = xmlCopyNode(cur, 1);
1451 list = tmp;
1452 parent = NULL;
1453 last = tmp;
1454 }
1455 } else {
1456 tmp = NULL;
1457 switch (cur->type) {
1458 case XML_DTD_NODE:
1459 case XML_ELEMENT_DECL:
1460 case XML_ATTRIBUTE_DECL:
1461 case XML_ENTITY_NODE:
1462 /* Do not copy DTD informations */
1463 break;
1464 case XML_ENTITY_DECL:
1465 TODO /* handle csossing entities -> stack needed */
1466 break;
1467 case XML_XINCLUDE_START:
1468 case XML_XINCLUDE_END:
1469 /* don't consider it part of the tree content */
1470 break;
1471 case XML_ATTRIBUTE_NODE:
1472 /* Humm, should not happen ! */
1473 STRANGE
1474 break;
1475 default:
1476 tmp = xmlCopyNode(cur, 1);
1477 break;
1478 }
1479 if (tmp != NULL) {
1480 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1481 STRANGE
1482 return(NULL);
1483 }
1484 if (last != NULL)
1485 xmlAddNextSibling(last, tmp);
1486 else {
1487 xmlAddChild(parent, tmp);
1488 last = tmp;
1489 }
1490 }
1491 }
1492 /*
1493 * Skip to next node in document order
1494 */
1495 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1496 STRANGE
1497 return(NULL);
1498 }
1499 cur = xmlXPtrAdvanceNode(cur);
1500 }
1501 return(list);
1502}
1503
1504/**
1505 * xmlXPtrBuildNodeList:
1506 * @obj: the XPointer result from the evaluation.
1507 *
1508 * Build a node list tree copy of the XPointer result.
Daniel Veillard39196eb2001-06-19 18:09:42 +00001509 * This will drop Attributes and Namespace declarations.
Owen Taylor3473f882001-02-23 17:55:21 +00001510 *
1511 * Returns an xmlNodePtr list or NULL.
1512 * the caller has to free the node tree.
1513 */
1514xmlNodePtr
1515xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1516 xmlNodePtr list = NULL, last = NULL;
1517 int i;
1518
1519 if (obj == NULL)
1520 return(NULL);
1521 switch (obj->type) {
1522 case XPATH_NODESET: {
1523 xmlNodeSetPtr set = obj->nodesetval;
1524 if (set == NULL)
1525 return(NULL);
1526 for (i = 0;i < set->nodeNr;i++) {
Daniel Veillard39196eb2001-06-19 18:09:42 +00001527 if (set->nodeTab[i] == NULL)
1528 continue;
1529 switch (set->nodeTab[i]->type) {
1530 case XML_TEXT_NODE:
1531 case XML_CDATA_SECTION_NODE:
1532 case XML_ELEMENT_NODE:
1533 case XML_ENTITY_REF_NODE:
1534 case XML_ENTITY_NODE:
1535 case XML_PI_NODE:
1536 case XML_COMMENT_NODE:
1537 case XML_DOCUMENT_NODE:
1538 case XML_HTML_DOCUMENT_NODE:
1539#ifdef LIBXML_DOCB_ENABLED
1540 case XML_DOCB_DOCUMENT_NODE:
1541#endif
1542 case XML_XINCLUDE_START:
1543 case XML_XINCLUDE_END:
1544 break;
1545 case XML_ATTRIBUTE_NODE:
1546 case XML_NAMESPACE_DECL:
1547 case XML_DOCUMENT_TYPE_NODE:
1548 case XML_DOCUMENT_FRAG_NODE:
1549 case XML_NOTATION_NODE:
1550 case XML_DTD_NODE:
1551 case XML_ELEMENT_DECL:
1552 case XML_ATTRIBUTE_DECL:
1553 case XML_ENTITY_DECL:
1554 continue; /* for */
1555 }
Owen Taylor3473f882001-02-23 17:55:21 +00001556 if (last == NULL)
1557 list = last = xmlCopyNode(set->nodeTab[i], 1);
1558 else {
1559 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1560 if (last->next != NULL)
1561 last = last->next;
1562 }
1563 }
1564 break;
1565 }
1566 case XPATH_LOCATIONSET: {
1567 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1568 if (set == NULL)
1569 return(NULL);
1570 for (i = 0;i < set->locNr;i++) {
1571 if (last == NULL)
1572 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1573 else
1574 xmlAddNextSibling(last,
1575 xmlXPtrBuildNodeList(set->locTab[i]));
1576 if (last != NULL) {
1577 while (last->next != NULL)
1578 last = last->next;
1579 }
1580 }
1581 break;
1582 }
1583 case XPATH_RANGE:
1584 return(xmlXPtrBuildRangeNodeList(obj));
1585 case XPATH_POINT:
1586 return(xmlCopyNode(obj->user, 0));
1587 default:
1588 break;
1589 }
1590 return(list);
1591}
1592
1593/************************************************************************
1594 * *
1595 * XPointer functions *
1596 * *
1597 ************************************************************************/
1598
1599/**
1600 * xmlXPtrNbLocChildren:
1601 * @node: an xmlNodePtr
1602 *
1603 * Count the number of location children of @node or the lenght of the
1604 * string value in case of text/PI/Comments nodes
1605 *
1606 * Returns the number of location children
1607 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001608static int
Owen Taylor3473f882001-02-23 17:55:21 +00001609xmlXPtrNbLocChildren(xmlNodePtr node) {
1610 int ret = 0;
1611 if (node == NULL)
1612 return(-1);
1613 switch (node->type) {
1614 case XML_HTML_DOCUMENT_NODE:
1615 case XML_DOCUMENT_NODE:
1616 case XML_ELEMENT_NODE:
1617 node = node->children;
1618 while (node != NULL) {
1619 if (node->type == XML_ELEMENT_NODE)
1620 ret++;
1621 node = node->next;
1622 }
1623 break;
1624 case XML_ATTRIBUTE_NODE:
1625 return(-1);
1626
1627 case XML_PI_NODE:
1628 case XML_COMMENT_NODE:
1629 case XML_TEXT_NODE:
1630 case XML_CDATA_SECTION_NODE:
1631 case XML_ENTITY_REF_NODE:
1632#ifndef XML_USE_BUFFER_CONTENT
1633 ret = xmlStrlen(node->content);
1634#else
1635 ret = xmlBufferLength(node->content);
1636#endif
1637 break;
1638 default:
1639 return(-1);
1640 }
1641 return(ret);
1642}
1643
1644/**
1645 * xmlXPtrHereFunction:
1646 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001647 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001648 *
1649 * Function implementing here() operation
1650 * as described in 5.4.3
1651 */
1652void
1653xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001654 CHECK_ARITY(0);
1655
Owen Taylor3473f882001-02-23 17:55:21 +00001656 if (ctxt->context->here == NULL)
1657 XP_ERROR(XPTR_SYNTAX_ERROR);
1658
1659 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1660}
1661
1662/**
1663 * xmlXPtrOriginFunction:
1664 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001665 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001666 *
1667 * Function implementing origin() operation
1668 * as described in 5.4.3
1669 */
1670void
1671xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001672 CHECK_ARITY(0);
1673
Owen Taylor3473f882001-02-23 17:55:21 +00001674 if (ctxt->context->origin == NULL)
1675 XP_ERROR(XPTR_SYNTAX_ERROR);
1676
1677 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1678}
1679
1680/**
1681 * xmlXPtrStartPointFunction:
1682 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001683 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001684 *
1685 * Function implementing start-point() operation
1686 * as described in 5.4.3
1687 * ----------------
1688 * location-set start-point(location-set)
1689 *
1690 * For each location x in the argument location-set, start-point adds a
1691 * location of type point to the result location-set. That point represents
1692 * the start point of location x and is determined by the following rules:
1693 *
1694 * - If x is of type point, the start point is x.
1695 * - If x is of type range, the start point is the start point of x.
1696 * - If x is of type root, element, text, comment, or processing instruction,
1697 * - the container node of the start point is x and the index is 0.
1698 * - If x is of type attribute or namespace, the function must signal a
1699 * syntax error.
1700 * ----------------
1701 *
1702 */
1703void
1704xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1705 xmlXPathObjectPtr tmp, obj, point;
1706 xmlLocationSetPtr newset = NULL;
1707 xmlLocationSetPtr oldset = NULL;
1708
1709 CHECK_ARITY(1);
1710 if ((ctxt->value == NULL) ||
1711 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1712 (ctxt->value->type != XPATH_NODESET)))
1713 XP_ERROR(XPATH_INVALID_TYPE)
1714
1715 obj = valuePop(ctxt);
1716 if (obj->type == XPATH_NODESET) {
1717 /*
1718 * First convert to a location set
1719 */
1720 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1721 xmlXPathFreeObject(obj);
1722 obj = tmp;
1723 }
1724
1725 newset = xmlXPtrLocationSetCreate(NULL);
1726 if (newset == NULL) {
1727 xmlXPathFreeObject(obj);
1728 XP_ERROR(XPATH_MEMORY_ERROR);
1729 }
1730 oldset = (xmlLocationSetPtr) obj->user;
1731 if (oldset != NULL) {
1732 int i;
1733
1734 for (i = 0; i < oldset->locNr; i++) {
1735 tmp = oldset->locTab[i];
1736 if (tmp == NULL)
1737 continue;
1738 point = NULL;
1739 switch (tmp->type) {
1740 case XPATH_POINT:
1741 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1742 break;
1743 case XPATH_RANGE: {
1744 xmlNodePtr node = tmp->user;
1745 if (node != NULL) {
1746 if (node->type == XML_ATTRIBUTE_NODE) {
1747 /* TODO: Namespace Nodes ??? */
1748 xmlXPathFreeObject(obj);
1749 xmlXPtrFreeLocationSet(newset);
1750 XP_ERROR(XPTR_SYNTAX_ERROR);
1751 }
1752 point = xmlXPtrNewPoint(node, tmp->index);
1753 }
1754 break;
1755 }
1756 default:
1757 /*** Should we raise an error ?
1758 xmlXPathFreeObject(obj);
1759 xmlXPathFreeObject(newset);
1760 XP_ERROR(XPATH_INVALID_TYPE)
1761 ***/
1762 break;
1763 }
1764 if (point != NULL)
1765 xmlXPtrLocationSetAdd(newset, point);
1766 }
1767 }
1768 xmlXPathFreeObject(obj);
1769 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1770}
1771
1772/**
1773 * xmlXPtrEndPointFunction:
1774 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001775 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001776 *
1777 * Function implementing end-point() operation
1778 * as described in 5.4.3
1779 * ----------------------------
1780 * location-set end-point(location-set)
1781 *
1782 * For each location x in the argument location-set, end-point adds a
1783 * location of type point to the result location-set. That point representsi
1784 * the end point of location x and is determined by the following rules:
1785 *
1786 * - If x is of type point, the resulting point is x.
1787 * - If x is of type range, the resulting point is the end point of x.
1788 * - If x is of type root or element, the container node of the resulting
1789 * point is x and the index is the number of location children of x.
1790 * - If x is of type text, comment, or processing instruction, the container
1791 * node of the resulting point is x and the index is the length of thei
1792 * string-value of x.
1793 * - If x is of type attribute or namespace, the function must signal a
1794 * syntax error.
1795 * ----------------------------
1796 */
1797void
1798xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1799 xmlXPathObjectPtr tmp, obj, point;
1800 xmlLocationSetPtr newset = NULL;
1801 xmlLocationSetPtr oldset = NULL;
1802
1803 CHECK_ARITY(1);
1804 if ((ctxt->value == NULL) ||
1805 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1806 (ctxt->value->type != XPATH_NODESET)))
1807 XP_ERROR(XPATH_INVALID_TYPE)
1808
1809 obj = valuePop(ctxt);
1810 if (obj->type == XPATH_NODESET) {
1811 /*
1812 * First convert to a location set
1813 */
1814 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1815 xmlXPathFreeObject(obj);
1816 obj = tmp;
1817 }
1818
1819 newset = xmlXPtrLocationSetCreate(NULL);
1820 oldset = (xmlLocationSetPtr) obj->user;
1821 if (oldset != NULL) {
1822 int i;
1823
1824 for (i = 0; i < oldset->locNr; i++) {
1825 tmp = oldset->locTab[i];
1826 if (tmp == NULL)
1827 continue;
1828 point = NULL;
1829 switch (tmp->type) {
1830 case XPATH_POINT:
1831 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1832 break;
1833 case XPATH_RANGE: {
1834 xmlNodePtr node = tmp->user2;
1835 if (node != NULL) {
1836 if (node->type == XML_ATTRIBUTE_NODE) {
1837 /* TODO: Namespace Nodes ??? */
1838 xmlXPathFreeObject(obj);
1839 xmlXPtrFreeLocationSet(newset);
1840 XP_ERROR(XPTR_SYNTAX_ERROR);
1841 }
1842 point = xmlXPtrNewPoint(node, tmp->index2);
1843 } else if (tmp->user == NULL) {
1844 point = xmlXPtrNewPoint(node,
1845 xmlXPtrNbLocChildren(node));
1846 }
1847 break;
1848 }
1849 default:
1850 /*** Should we raise an error ?
1851 xmlXPathFreeObject(obj);
1852 xmlXPathFreeObject(newset);
1853 XP_ERROR(XPATH_INVALID_TYPE)
1854 ***/
1855 break;
1856 }
1857 if (point != NULL)
1858 xmlXPtrLocationSetAdd(newset, point);
1859 }
1860 }
1861 xmlXPathFreeObject(obj);
1862 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1863}
1864
1865
1866/**
1867 * xmlXPtrCoveringRange:
1868 * @ctxt: the XPointer Parser context
1869 * @loc: the location for which the covering range must be computed
1870 *
1871 * A covering range is a range that wholly encompasses a location
1872 * Section 5.3.3. Covering Ranges for All Location Types
1873 * http://www.w3.org/TR/xptr#N2267
1874 *
1875 * Returns a new location or NULL in case of error
1876 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001877static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001878xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1879 if (loc == NULL)
1880 return(NULL);
1881 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1882 (ctxt->context->doc == NULL))
1883 return(NULL);
1884 switch (loc->type) {
1885 case XPATH_POINT:
1886 return(xmlXPtrNewRange(loc->user, loc->index,
1887 loc->user, loc->index));
1888 case XPATH_RANGE:
1889 if (loc->user2 != NULL) {
1890 return(xmlXPtrNewRange(loc->user, loc->index,
1891 loc->user2, loc->index2));
1892 } else {
1893 xmlNodePtr node = (xmlNodePtr) loc->user;
1894 if (node == (xmlNodePtr) ctxt->context->doc) {
1895 return(xmlXPtrNewRange(node, 0, node,
1896 xmlXPtrGetArity(node)));
1897 } else {
1898 switch (node->type) {
1899 case XML_ATTRIBUTE_NODE:
1900 /* !!! our model is slightly different than XPath */
1901 return(xmlXPtrNewRange(node, 0, node,
1902 xmlXPtrGetArity(node)));
1903 case XML_ELEMENT_NODE:
1904 case XML_TEXT_NODE:
1905 case XML_CDATA_SECTION_NODE:
1906 case XML_ENTITY_REF_NODE:
1907 case XML_PI_NODE:
1908 case XML_COMMENT_NODE:
1909 case XML_DOCUMENT_NODE:
1910 case XML_NOTATION_NODE:
1911 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001912 int indx = xmlXPtrGetIndex(node);
Owen Taylor3473f882001-02-23 17:55:21 +00001913
1914 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001915 return(xmlXPtrNewRange(node, indx - 1,
1916 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00001917 }
1918 default:
1919 return(NULL);
1920 }
1921 }
1922 }
1923 default:
1924 TODO /* missed one case ??? */
1925 }
1926 return(NULL);
1927}
1928
1929/**
1930 * xmlXPtrRangeFunction:
1931 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001932 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001933 *
1934 * Function implementing the range() function 5.4.3
1935 * location-set range(location-set )
1936 *
1937 * The range function returns ranges covering the locations in
1938 * the argument location-set. For each location x in the argument
1939 * location-set, a range location representing the covering range of
1940 * x is added to the result location-set.
1941 */
1942void
1943xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1944 int i;
1945 xmlXPathObjectPtr set;
1946 xmlLocationSetPtr oldset;
1947 xmlLocationSetPtr newset;
1948
1949 CHECK_ARITY(1);
1950 if ((ctxt->value == NULL) ||
1951 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1952 (ctxt->value->type != XPATH_NODESET)))
1953 XP_ERROR(XPATH_INVALID_TYPE)
1954
1955 set = valuePop(ctxt);
1956 if (set->type == XPATH_NODESET) {
1957 xmlXPathObjectPtr tmp;
1958
1959 /*
1960 * First convert to a location set
1961 */
1962 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1963 xmlXPathFreeObject(set);
1964 set = tmp;
1965 }
1966 oldset = (xmlLocationSetPtr) set->user;
1967
1968 /*
1969 * The loop is to compute the covering range for each item and add it
1970 */
1971 newset = xmlXPtrLocationSetCreate(NULL);
1972 for (i = 0;i < oldset->locNr;i++) {
1973 xmlXPtrLocationSetAdd(newset,
1974 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
1975 }
1976
1977 /*
1978 * Save the new value and cleanup
1979 */
1980 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1981 xmlXPathFreeObject(set);
1982}
1983
1984/**
1985 * xmlXPtrInsideRange:
1986 * @ctxt: the XPointer Parser context
1987 * @loc: the location for which the inside range must be computed
1988 *
1989 * A inside range is a range described in the range-inside() description
1990 *
1991 * Returns a new location or NULL in case of error
1992 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001993static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001994xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1995 if (loc == NULL)
1996 return(NULL);
1997 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1998 (ctxt->context->doc == NULL))
1999 return(NULL);
2000 switch (loc->type) {
2001 case XPATH_POINT: {
2002 xmlNodePtr node = (xmlNodePtr) loc->user;
2003 switch (node->type) {
2004 case XML_PI_NODE:
2005 case XML_COMMENT_NODE:
2006 case XML_TEXT_NODE:
2007 case XML_CDATA_SECTION_NODE: {
2008 if (node->content == NULL) {
2009 return(xmlXPtrNewRange(node, 0, node, 0));
2010 } else {
2011#ifndef XML_USE_BUFFER_CONTENT
2012 return(xmlXPtrNewRange(node, 0, node,
2013 xmlStrlen(node->content)));
2014#else
2015 return(xmlXPtrNewRange(node, 0, node,
2016 xmlBufferLength(node->content)));
2017#endif
2018 }
2019 }
2020 case XML_ATTRIBUTE_NODE:
2021 case XML_ELEMENT_NODE:
2022 case XML_ENTITY_REF_NODE:
2023 case XML_DOCUMENT_NODE:
2024 case XML_NOTATION_NODE:
2025 case XML_HTML_DOCUMENT_NODE: {
2026 return(xmlXPtrNewRange(node, 0, node,
2027 xmlXPtrGetArity(node)));
2028 }
2029 default:
2030 return(NULL);
2031 }
2032 return(NULL);
2033 }
2034 case XPATH_RANGE: {
2035 xmlNodePtr node = (xmlNodePtr) loc->user;
2036 if (loc->user2 != NULL) {
2037 return(xmlXPtrNewRange(node, loc->index,
2038 loc->user2, loc->index2));
2039 } else {
2040 switch (node->type) {
2041 case XML_PI_NODE:
2042 case XML_COMMENT_NODE:
2043 case XML_TEXT_NODE:
2044 case XML_CDATA_SECTION_NODE: {
2045 if (node->content == NULL) {
2046 return(xmlXPtrNewRange(node, 0, node, 0));
2047 } else {
2048#ifndef XML_USE_BUFFER_CONTENT
2049 return(xmlXPtrNewRange(node, 0, node,
2050 xmlStrlen(node->content)));
2051#else
2052 return(xmlXPtrNewRange(node, 0, node,
2053 xmlBufferLength(node->content)));
2054#endif
2055 }
2056 }
2057 case XML_ATTRIBUTE_NODE:
2058 case XML_ELEMENT_NODE:
2059 case XML_ENTITY_REF_NODE:
2060 case XML_DOCUMENT_NODE:
2061 case XML_NOTATION_NODE:
2062 case XML_HTML_DOCUMENT_NODE: {
2063 return(xmlXPtrNewRange(node, 0, node,
2064 xmlXPtrGetArity(node)));
2065 }
2066 default:
2067 return(NULL);
2068 }
2069 return(NULL);
2070 }
2071 }
2072 default:
2073 TODO /* missed one case ??? */
2074 }
2075 return(NULL);
2076}
2077
2078/**
2079 * xmlXPtrRangeInsideFunction:
2080 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002081 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002082 *
2083 * Function implementing the range-inside() function 5.4.3
2084 * location-set range-inside(location-set )
2085 *
2086 * The range-inside function returns ranges covering the contents of
2087 * the locations in the argument location-set. For each location x in
2088 * the argument location-set, a range location is added to the result
2089 * location-set. If x is a range location, then x is added to the
2090 * result location-set. If x is not a range location, then x is used
2091 * as the container location of the start and end points of the range
2092 * location to be added; the index of the start point of the range is
2093 * zero; if the end point is a character point then its index is the
2094 * length of the string-value of x, and otherwise is the number of
2095 * location children of x.
2096 *
2097 */
2098void
2099xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2100 int i;
2101 xmlXPathObjectPtr set;
2102 xmlLocationSetPtr oldset;
2103 xmlLocationSetPtr newset;
2104
2105 CHECK_ARITY(1);
2106 if ((ctxt->value == NULL) ||
2107 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2108 (ctxt->value->type != XPATH_NODESET)))
2109 XP_ERROR(XPATH_INVALID_TYPE)
2110
2111 set = valuePop(ctxt);
2112 if (set->type == XPATH_NODESET) {
2113 xmlXPathObjectPtr tmp;
2114
2115 /*
2116 * First convert to a location set
2117 */
2118 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2119 xmlXPathFreeObject(set);
2120 set = tmp;
2121 }
2122 oldset = (xmlLocationSetPtr) set->user;
2123
2124 /*
2125 * The loop is to compute the covering range for each item and add it
2126 */
2127 newset = xmlXPtrLocationSetCreate(NULL);
2128 for (i = 0;i < oldset->locNr;i++) {
2129 xmlXPtrLocationSetAdd(newset,
2130 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2131 }
2132
2133 /*
2134 * Save the new value and cleanup
2135 */
2136 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2137 xmlXPathFreeObject(set);
2138}
2139
2140/**
2141 * xmlXPtrRangeToFunction:
2142 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002143 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002144 *
2145 * Implement the range-to() XPointer function
2146 */
2147void
2148xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2149 xmlXPathObjectPtr range;
2150 const xmlChar *cur;
2151 xmlXPathObjectPtr res, obj;
2152 xmlXPathObjectPtr tmp;
2153 xmlLocationSetPtr newset = NULL;
2154 xmlNodeSetPtr oldset;
2155 int i;
2156
2157 CHECK_ARITY(1);
2158 /*
2159 * Save the expression pointer since we will have to evaluate
2160 * it multiple times. Initialize the new set.
2161 */
2162 CHECK_TYPE(XPATH_NODESET);
2163 obj = valuePop(ctxt);
2164 oldset = obj->nodesetval;
2165 ctxt->context->node = NULL;
2166
2167 cur = ctxt->cur;
2168 newset = xmlXPtrLocationSetCreate(NULL);
2169
2170 for (i = 0; i < oldset->nodeNr; i++) {
2171 ctxt->cur = cur;
2172
2173 /*
2174 * Run the evaluation with a node list made of a single item
2175 * in the nodeset.
2176 */
2177 ctxt->context->node = oldset->nodeTab[i];
2178 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2179 valuePush(ctxt, tmp);
2180
2181 xmlXPathEvalExpr(ctxt);
2182 CHECK_ERROR;
2183
2184 /*
2185 * The result of the evaluation need to be tested to
2186 * decided whether the filter succeeded or not
2187 */
2188 res = valuePop(ctxt);
2189 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2190 if (range != NULL) {
2191 xmlXPtrLocationSetAdd(newset, range);
2192 }
2193
2194 /*
2195 * Cleanup
2196 */
2197 if (res != NULL)
2198 xmlXPathFreeObject(res);
2199 if (ctxt->value == tmp) {
2200 res = valuePop(ctxt);
2201 xmlXPathFreeObject(res);
2202 }
2203
2204 ctxt->context->node = NULL;
2205 }
2206
2207 /*
2208 * The result is used as the new evaluation set.
2209 */
2210 xmlXPathFreeObject(obj);
2211 ctxt->context->node = NULL;
2212 ctxt->context->contextSize = -1;
2213 ctxt->context->proximityPosition = -1;
2214 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2215}
2216
2217/**
2218 * xmlXPtrAdvanceNode:
2219 * @cur: the node
2220 *
2221 * Advance to the next element or text node in document order
2222 * TODO: add a stack for entering/exiting entities
2223 *
2224 * Returns -1 in case of failure, 0 otherwise
2225 */
2226xmlNodePtr
2227xmlXPtrAdvanceNode(xmlNodePtr cur) {
2228next:
2229 if (cur == NULL)
2230 return(NULL);
2231 if (cur->children != NULL) {
2232 cur = cur->children ;
2233 goto found;
2234 }
2235 if (cur->next != NULL) {
2236 cur = cur->next;
2237 goto found;
2238 }
2239 do {
2240 cur = cur->parent;
2241 if (cur == NULL) return(NULL);
2242 if (cur->next != NULL) {
2243 cur = cur->next;
2244 goto found;
2245 }
2246 } while (cur != NULL);
2247
2248found:
2249 if ((cur->type != XML_ELEMENT_NODE) &&
2250 (cur->type != XML_TEXT_NODE) &&
2251 (cur->type != XML_DOCUMENT_NODE) &&
2252 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2253 (cur->type != XML_CDATA_SECTION_NODE))
2254 goto next;
2255 if (cur->type == XML_ENTITY_REF_NODE) {
2256 TODO
2257 }
2258 return(cur);
2259}
2260
2261/**
2262 * xmlXPtrAdvanceChar:
2263 * @node: the node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002264 * @indx: the indx
Owen Taylor3473f882001-02-23 17:55:21 +00002265 * @bytes: the number of bytes
2266 *
2267 * Advance a point of the associated number of bytes (not UTF8 chars)
2268 *
2269 * Returns -1 in case of failure, 0 otherwise
2270 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002271static int
2272xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Owen Taylor3473f882001-02-23 17:55:21 +00002273 xmlNodePtr cur;
2274 int pos;
2275 int len;
2276
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002277 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002278 return(-1);
2279 cur = *node;
2280 if (cur == NULL)
2281 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002282 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002283
2284 while (bytes >= 0) {
2285 /*
2286 * First position to the beginning of the first text node
2287 * corresponding to this point
2288 */
2289 while ((cur != NULL) &&
2290 ((cur->type == XML_ELEMENT_NODE) ||
2291 (cur->type == XML_DOCUMENT_NODE) ||
2292 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2293 if (pos > 0) {
2294 cur = xmlXPtrGetNthChild(cur, pos);
2295 pos = 0;
2296 } else {
2297 cur = xmlXPtrAdvanceNode(cur);
2298 pos = 0;
2299 }
2300 }
2301
2302 if (cur == NULL) {
2303 *node = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002304 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002305 return(-1);
2306 }
2307
2308 /*
2309 * if there is no move needed return the current value.
2310 */
2311 if (pos == 0) pos = 1;
2312 if (bytes == 0) {
2313 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002314 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002315 return(0);
2316 }
2317 /*
2318 * We should have a text (or cdata) node ...
2319 */
2320 len = 0;
Daniel Veillard7db37732001-07-12 01:20:08 +00002321 if ((cur->type != XML_ELEMENT_NODE) &&
2322 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002323#ifndef XML_USE_BUFFER_CONTENT
2324 len = xmlStrlen(cur->content);
2325#else
2326 len = xmlBufferLength(cur->content);
2327#endif
2328 }
2329 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002330 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002331 STRANGE
2332 pos = len;
2333 }
2334 if (pos + bytes >= len) {
2335 bytes -= (len - pos);
2336 cur = xmlXPtrAdvanceNode(cur);
2337 cur = 0;
2338 } else if (pos + bytes < len) {
2339 pos += bytes;
2340 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002341 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002342 return(0);
2343 }
2344 }
2345 return(-1);
2346}
2347
2348/**
2349 * xmlXPtrMatchString:
2350 * @string: the string to search
2351 * @start: the start textnode
2352 * @startindex: the start index
2353 * @end: the end textnode IN/OUT
2354 * @endindex: the end index IN/OUT
2355 *
2356 * Check whether the document contains @string at the position
2357 * (@start, @startindex) and limited by the (@end, @endindex) point
2358 *
2359 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2360 * (@start, @startindex) will indicate the position of the beginning
2361 * of the range and (@end, @endindex) will endicate the end
2362 * of the range
2363 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002364static int
Owen Taylor3473f882001-02-23 17:55:21 +00002365xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2366 xmlNodePtr *end, int *endindex) {
2367 xmlNodePtr cur;
2368 int pos; /* 0 based */
2369 int len; /* in bytes */
2370 int stringlen; /* in bytes */
2371 int match;
2372
2373 if (string == NULL)
2374 return(-1);
2375 if (start == NULL)
2376 return(-1);
2377 if ((end == NULL) || (endindex == NULL))
2378 return(-1);
2379 cur = start;
2380 if (cur == NULL)
2381 return(-1);
2382 pos = startindex - 1;
2383 stringlen = xmlStrlen(string);
2384
2385 while (stringlen > 0) {
2386 if ((cur == *end) && (pos + stringlen > *endindex))
2387 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002388
2389 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002390#ifndef XML_USE_BUFFER_CONTENT
2391 len = xmlStrlen(cur->content);
2392#else
2393 len = xmlBufferLength(cur->content);
2394#endif
2395 if (len >= pos + stringlen) {
2396#ifndef XML_USE_BUFFER_CONTENT
2397 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2398#else
2399 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2400 string, stringlen));
2401#endif
2402 if (match) {
2403#ifdef DEBUG_RANGES
2404 xmlGenericError(xmlGenericErrorContext,
2405 "found range %d bytes at index %d of ->",
2406 stringlen, pos + 1);
2407 xmlDebugDumpString(stdout, cur->content);
2408 xmlGenericError(xmlGenericErrorContext, "\n");
2409#endif
2410 *end = cur;
2411 *endindex = pos + stringlen;
2412 return(1);
2413 } else {
2414 return(0);
2415 }
2416 } else {
2417 int sub = len - pos;
2418#ifndef XML_USE_BUFFER_CONTENT
2419 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2420#else
2421 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2422 string, sub));
2423#endif
2424 if (match) {
2425#ifdef DEBUG_RANGES
2426 xmlGenericError(xmlGenericErrorContext,
2427 "found subrange %d bytes at index %d of ->",
2428 sub, pos + 1);
2429 xmlDebugDumpString(stdout, cur->content);
2430 xmlGenericError(xmlGenericErrorContext, "\n");
2431#endif
2432 string = &string[sub];
2433 stringlen -= sub;
2434 } else {
2435 return(0);
2436 }
2437 }
2438 }
2439 cur = xmlXPtrAdvanceNode(cur);
2440 if (cur == NULL)
2441 return(0);
2442 pos = 0;
2443 }
2444 return(1);
2445}
2446
2447/**
2448 * xmlXPtrSearchString:
2449 * @string: the string to search
2450 * @start: the start textnode IN/OUT
2451 * @startindex: the start index IN/OUT
2452 * @end: the end textnode
2453 * @endindex: the end index
2454 *
2455 * Search the next occurence of @string within the document content
2456 * until the (@end, @endindex) point is reached
2457 *
2458 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2459 * (@start, @startindex) will indicate the position of the beginning
2460 * of the range and (@end, @endindex) will endicate the end
2461 * of the range
2462 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002463static int
Owen Taylor3473f882001-02-23 17:55:21 +00002464xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2465 xmlNodePtr *end, int *endindex) {
2466 xmlNodePtr cur;
2467 const xmlChar *str;
2468 int pos; /* 0 based */
2469 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002470 xmlChar first;
2471
2472 if (string == NULL)
2473 return(-1);
2474 if ((start == NULL) || (startindex == NULL))
2475 return(-1);
2476 if ((end == NULL) || (endindex == NULL))
2477 return(-1);
2478 cur = *start;
2479 if (cur == NULL)
2480 return(-1);
2481 pos = *startindex - 1;
2482 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002483
2484 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002485 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002486#ifndef XML_USE_BUFFER_CONTENT
2487 len = xmlStrlen(cur->content);
2488#else
2489 len = xmlBufferLength(cur->content);
2490#endif
2491 while (pos <= len) {
2492 if (first != 0) {
2493#ifndef XML_USE_BUFFER_CONTENT
2494 str = xmlStrchr(&cur->content[pos], first);
2495#else
2496 str = xmlStrchr(&xmlBufferContent(cur->content)[pos],
2497 first);
2498#endif
2499 if (str != NULL) {
2500 pos = (str - (xmlChar *)(cur->content));
2501#ifdef DEBUG_RANGES
2502 xmlGenericError(xmlGenericErrorContext,
2503 "found '%c' at index %d of ->",
2504 first, pos + 1);
2505 xmlDebugDumpString(stdout, cur->content);
2506 xmlGenericError(xmlGenericErrorContext, "\n");
2507#endif
2508 if (xmlXPtrMatchString(string, cur, pos + 1,
2509 end, endindex)) {
2510 *start = cur;
2511 *startindex = pos + 1;
2512 return(1);
2513 }
2514 pos++;
2515 } else {
2516 pos = len + 1;
2517 }
2518 } else {
2519 /*
2520 * An empty string is considered to match before each
2521 * character of the string-value and after the final
2522 * character.
2523 */
2524#ifdef DEBUG_RANGES
2525 xmlGenericError(xmlGenericErrorContext,
2526 "found '' at index %d of ->",
2527 pos + 1);
2528 xmlDebugDumpString(stdout, cur->content);
2529 xmlGenericError(xmlGenericErrorContext, "\n");
2530#endif
2531 *start = cur;
2532 *startindex = pos + 1;
2533 *end = cur;
2534 *endindex = pos + 1;
2535 return(1);
2536 }
2537 }
2538 }
2539 if ((cur == *end) && (pos >= *endindex))
2540 return(0);
2541 cur = xmlXPtrAdvanceNode(cur);
2542 if (cur == NULL)
2543 return(0);
2544 pos = 1;
2545 }
2546 return(0);
2547}
2548
2549/**
2550 * xmlXPtrGetLastChar:
2551 * @node: the node
2552 * @index: the index
2553 *
2554 * Computes the point coordinates of the last char of this point
2555 *
2556 * Returns -1 in case of failure, 0 otherwise
2557 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002558static int
2559xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002560 xmlNodePtr cur;
2561 int pos, len = 0;
2562
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002563 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002564 return(-1);
2565 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002566 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002567
2568 if (cur == NULL)
2569 return(-1);
2570
2571 if ((cur->type == XML_ELEMENT_NODE) ||
2572 (cur->type == XML_DOCUMENT_NODE) ||
2573 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2574 if (pos > 0) {
2575 cur = xmlXPtrGetNthChild(cur, pos);
2576 pos = 0;
2577 }
2578 }
2579 while (cur != NULL) {
2580 if (cur->last != NULL)
2581 cur = cur->last;
Daniel Veillard7db37732001-07-12 01:20:08 +00002582 else if ((cur->type != XML_ELEMENT_NODE) &&
2583 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002584#ifndef XML_USE_BUFFER_CONTENT
2585 len = xmlStrlen(cur->content);
2586#else
2587 len = xmlBufferLength(cur->content);
2588#endif
2589 break;
2590 } else {
2591 return(-1);
2592 }
2593 }
2594 if (cur == NULL)
2595 return(-1);
2596 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002597 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002598 return(0);
2599}
2600
2601/**
2602 * xmlXPtrGetStartPoint:
2603 * @obj: an range
2604 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002605 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002606 *
2607 * read the object and return the start point coordinates.
2608 *
2609 * Returns -1 in case of failure, 0 otherwise
2610 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002611static int
2612xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2613 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002614 return(-1);
2615
2616 switch (obj->type) {
2617 case XPATH_POINT:
2618 *node = obj->user;
2619 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002620 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002621 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002622 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002623 return(0);
2624 case XPATH_RANGE:
2625 *node = obj->user;
2626 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002627 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002628 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002629 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002630 return(0);
2631 default:
2632 return(-1);
2633 }
2634 return(-1);
2635}
2636
2637/**
2638 * xmlXPtrGetEndPoint:
2639 * @obj: an range
2640 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002641 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002642 *
2643 * read the object and return the end point coordinates.
2644 *
2645 * Returns -1 in case of failure, 0 otherwise
2646 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002647static int
2648xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2649 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002650 return(-1);
2651
2652 switch (obj->type) {
2653 case XPATH_POINT:
2654 *node = obj->user;
2655 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002656 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002657 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002658 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002659 return(0);
2660 case XPATH_RANGE:
2661 *node = obj->user;
2662 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002663 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002664 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002665 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002666 return(0);
2667 default:
2668 return(-1);
2669 }
2670 return(-1);
2671}
2672
2673/**
2674 * xmlXPtrStringRangeFunction:
2675 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002676 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002677 *
2678 * Function implementing the string-range() function
2679 * range as described in 5.4.2
2680 *
2681 * ------------------------------
2682 * [Definition: For each location in the location-set argument,
2683 * string-range returns a set of string ranges, a set of substrings in a
2684 * string. Specifically, the string-value of the location is searched for
2685 * substrings that match the string argument, and the resulting location-set
2686 * will contain a range location for each non-overlapping match.]
2687 * An empty string is considered to match before each character of the
2688 * string-value and after the final character. Whitespace in a string
2689 * is matched literally, with no normalization except that provided by
2690 * XML for line ends. The third argument gives the position of the first
2691 * character to be in the resulting range, relative to the start of the
2692 * match. The default value is 1, which makes the range start immediately
2693 * before the first character of the matched string. The fourth argument
2694 * gives the number of characters in the range; the default is that the
2695 * range extends to the end of the matched string.
2696 *
2697 * Element boundaries, as well as entire embedded nodes such as processing
2698 * instructions and comments, are ignored as defined in [XPath].
2699 *
2700 * If the string in the second argument is not found in the string-value
2701 * of the location, or if a value in the third or fourth argument indicates
2702 * a string that is beyond the beginning or end of the document, the
2703 * expression fails.
2704 *
2705 * The points of the range-locations in the returned location-set will
2706 * all be character points.
2707 * ------------------------------
2708 */
2709void
2710xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2711 int i, startindex, endindex, fendindex;
2712 xmlNodePtr start, end, fend;
2713 xmlXPathObjectPtr set;
2714 xmlLocationSetPtr oldset;
2715 xmlLocationSetPtr newset;
2716 xmlXPathObjectPtr string;
2717 xmlXPathObjectPtr position = NULL;
2718 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002719 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002720
2721 /*
2722 * Grab the arguments
2723 */
2724 if ((nargs < 2) || (nargs > 4))
2725 XP_ERROR(XPATH_INVALID_ARITY);
2726
2727 if (nargs >= 4) {
2728 CHECK_TYPE(XPATH_NUMBER);
2729 number = valuePop(ctxt);
2730 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002731 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002732 }
2733 if (nargs >= 3) {
2734 CHECK_TYPE(XPATH_NUMBER);
2735 position = valuePop(ctxt);
2736 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002737 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002738 }
2739 CHECK_TYPE(XPATH_STRING);
2740 string = valuePop(ctxt);
2741 if ((ctxt->value == NULL) ||
2742 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2743 (ctxt->value->type != XPATH_NODESET)))
2744 XP_ERROR(XPATH_INVALID_TYPE)
2745
2746 set = valuePop(ctxt);
2747 if (set->type == XPATH_NODESET) {
2748 xmlXPathObjectPtr tmp;
2749
2750 /*
2751 * First convert to a location set
2752 */
2753 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2754 xmlXPathFreeObject(set);
2755 set = tmp;
2756 }
2757 oldset = (xmlLocationSetPtr) set->user;
2758
2759 /*
2760 * The loop is to search for each element in the location set
2761 * the list of location set corresponding to that search
2762 */
2763 newset = xmlXPtrLocationSetCreate(NULL);
2764 for (i = 0;i < oldset->locNr;i++) {
2765#ifdef DEBUG_RANGES
2766 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2767#endif
2768
2769 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2770 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2771 xmlXPtrAdvanceChar(&start, &startindex, 0);
2772 xmlXPtrGetLastChar(&end, &endindex);
2773
2774#ifdef DEBUG_RANGES
2775 xmlGenericError(xmlGenericErrorContext,
2776 "from index %d of ->", startindex);
2777 xmlDebugDumpString(stdout, start->content);
2778 xmlGenericError(xmlGenericErrorContext, "\n");
2779 xmlGenericError(xmlGenericErrorContext,
2780 "to index %d of ->", endindex);
2781 xmlDebugDumpString(stdout, end->content);
2782 xmlGenericError(xmlGenericErrorContext, "\n");
2783#endif
2784 do {
2785 fend = end;
2786 fendindex = endindex;
2787 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2788 &fend, &fendindex);
2789 if (found == 1) {
2790 if (position == NULL) {
2791 xmlXPtrLocationSetAdd(newset,
2792 xmlXPtrNewRange(start, startindex, fend, fendindex));
2793 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2794 pos - 1) == 0) {
2795 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002796 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002797 xmlNodePtr rend;
2798 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002799 rindx = startindex - 1;
2800 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002801 num) == 0) {
2802 xmlXPtrLocationSetAdd(newset,
2803 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002804 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002805 }
2806 } else if ((number != NULL) && (num <= 0)) {
2807 xmlXPtrLocationSetAdd(newset,
2808 xmlXPtrNewRange(start, startindex,
2809 start, startindex));
2810 } else {
2811 xmlXPtrLocationSetAdd(newset,
2812 xmlXPtrNewRange(start, startindex,
2813 fend, fendindex));
2814 }
2815 }
2816 start = fend;
2817 startindex = fendindex;
2818 if (string->stringval[0] == 0)
2819 startindex++;
2820 }
2821 } while (found == 1);
2822 }
2823
2824 /*
2825 * Save the new value and cleanup
2826 */
2827 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2828 xmlXPathFreeObject(set);
2829 xmlXPathFreeObject(string);
2830 if (position) xmlXPathFreeObject(position);
2831 if (number) xmlXPathFreeObject(number);
2832}
2833
2834/**
2835 * xmlXPtrEvalRangePredicate:
2836 * @ctxt: the XPointer Parser context
2837 *
2838 * [8] Predicate ::= '[' PredicateExpr ']'
2839 * [9] PredicateExpr ::= Expr
2840 *
2841 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2842 * a Location Set instead of a node set
2843 */
2844void
2845xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2846 const xmlChar *cur;
2847 xmlXPathObjectPtr res;
2848 xmlXPathObjectPtr obj, tmp;
2849 xmlLocationSetPtr newset = NULL;
2850 xmlLocationSetPtr oldset;
2851 int i;
2852
2853 SKIP_BLANKS;
2854 if (CUR != '[') {
2855 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2856 }
2857 NEXT;
2858 SKIP_BLANKS;
2859
2860 /*
2861 * Extract the old set, and then evaluate the result of the
2862 * expression for all the element in the set. use it to grow
2863 * up a new set.
2864 */
2865 CHECK_TYPE(XPATH_LOCATIONSET);
2866 obj = valuePop(ctxt);
2867 oldset = obj->user;
2868 ctxt->context->node = NULL;
2869
2870 if ((oldset == NULL) || (oldset->locNr == 0)) {
2871 ctxt->context->contextSize = 0;
2872 ctxt->context->proximityPosition = 0;
2873 xmlXPathEvalExpr(ctxt);
2874 res = valuePop(ctxt);
2875 if (res != NULL)
2876 xmlXPathFreeObject(res);
2877 valuePush(ctxt, obj);
2878 CHECK_ERROR;
2879 } else {
2880 /*
2881 * Save the expression pointer since we will have to evaluate
2882 * it multiple times. Initialize the new set.
2883 */
2884 cur = ctxt->cur;
2885 newset = xmlXPtrLocationSetCreate(NULL);
2886
2887 for (i = 0; i < oldset->locNr; i++) {
2888 ctxt->cur = cur;
2889
2890 /*
2891 * Run the evaluation with a node list made of a single item
2892 * in the nodeset.
2893 */
2894 ctxt->context->node = oldset->locTab[i]->user;
2895 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2896 valuePush(ctxt, tmp);
2897 ctxt->context->contextSize = oldset->locNr;
2898 ctxt->context->proximityPosition = i + 1;
2899
2900 xmlXPathEvalExpr(ctxt);
2901 CHECK_ERROR;
2902
2903 /*
2904 * The result of the evaluation need to be tested to
2905 * decided whether the filter succeeded or not
2906 */
2907 res = valuePop(ctxt);
2908 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2909 xmlXPtrLocationSetAdd(newset,
2910 xmlXPathObjectCopy(oldset->locTab[i]));
2911 }
2912
2913 /*
2914 * Cleanup
2915 */
2916 if (res != NULL)
2917 xmlXPathFreeObject(res);
2918 if (ctxt->value == tmp) {
2919 res = valuePop(ctxt);
2920 xmlXPathFreeObject(res);
2921 }
2922
2923 ctxt->context->node = NULL;
2924 }
2925
2926 /*
2927 * The result is used as the new evaluation set.
2928 */
2929 xmlXPathFreeObject(obj);
2930 ctxt->context->node = NULL;
2931 ctxt->context->contextSize = -1;
2932 ctxt->context->proximityPosition = -1;
2933 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2934 }
2935 if (CUR != ']') {
2936 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2937 }
2938
2939 NEXT;
2940 SKIP_BLANKS;
2941}
2942
2943#else
2944#endif
2945