blob: 8bed994bee3cf92b7885f7e4008f25ec215f710c [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>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000030#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000031
32#ifdef LIBXML_XPTR_ENABLED
33
34/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
35#define XPTR_XMLNS_SCHEME
36
37/* #define DEBUG_RANGES */
Daniel Veillard017b1082001-06-21 11:20:21 +000038#ifdef DEBUG_RANGES
39#ifdef LIBXML_DEBUG_ENABLED
40#include <libxml/debugXML.h>
41#endif
42#endif
Owen Taylor3473f882001-02-23 17:55:21 +000043
44#define TODO \
45 xmlGenericError(xmlGenericErrorContext, \
46 "Unimplemented block at %s:%d\n", \
47 __FILE__, __LINE__);
48
49#define STRANGE \
50 xmlGenericError(xmlGenericErrorContext, \
51 "Internal error at %s:%d\n", \
52 __FILE__, __LINE__);
53
54/************************************************************************
55 * *
56 * A few helper functions for child sequences *
57 * *
58 ************************************************************************/
59
60xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur);
61/**
62 * xmlXPtrGetArity:
63 * @cur: the node
64 *
65 * Returns the number of child for an element, -1 in case of error
66 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +000067static int
Owen Taylor3473f882001-02-23 17:55:21 +000068xmlXPtrGetArity(xmlNodePtr cur) {
69 int i;
70 if (cur == NULL)
71 return(-1);
72 cur = cur->children;
73 for (i = 0;cur != NULL;cur = cur->next) {
74 if ((cur->type == XML_ELEMENT_NODE) ||
75 (cur->type == XML_DOCUMENT_NODE) ||
76 (cur->type == XML_HTML_DOCUMENT_NODE)) {
77 i++;
78 }
79 }
80 return(i);
81}
82
83/**
84 * xmlXPtrGetIndex:
85 * @cur: the node
86 *
87 * Returns the index of the node in its parent children list, -1
88 * in case of error
89 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +000090static int
Owen Taylor3473f882001-02-23 17:55:21 +000091xmlXPtrGetIndex(xmlNodePtr cur) {
92 int i;
93 if (cur == NULL)
94 return(-1);
95 for (i = 1;cur != NULL;cur = cur->prev) {
96 if ((cur->type == XML_ELEMENT_NODE) ||
97 (cur->type == XML_DOCUMENT_NODE) ||
98 (cur->type == XML_HTML_DOCUMENT_NODE)) {
99 i++;
100 }
101 }
102 return(i);
103}
104
105/**
106 * xmlXPtrGetNthChild:
107 * @cur: the node
108 * @no: the child number
109 *
110 * Returns the @no'th element child of @cur or NULL
111 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000112static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000113xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
114 int i;
115 if (cur == NULL)
116 return(cur);
117 cur = cur->children;
118 for (i = 0;i <= no;cur = cur->next) {
119 if (cur == NULL)
120 return(cur);
121 if ((cur->type == XML_ELEMENT_NODE) ||
122 (cur->type == XML_DOCUMENT_NODE) ||
123 (cur->type == XML_HTML_DOCUMENT_NODE)) {
124 i++;
125 if (i == no)
126 break;
127 }
128 }
129 return(cur);
130}
131
132/************************************************************************
133 * *
134 * Handling of XPointer specific types *
135 * *
136 ************************************************************************/
137
138/**
139 * xmlXPtrCmpPoints:
140 * @node1: the first node
141 * @index1: the first index
142 * @node2: the second node
143 * @index2: the second index
144 *
145 * Compare two points w.r.t document order
146 *
147 * Returns -2 in case of error 1 if first point < second point, 0 if
148 * that's the same point, -1 otherwise
149 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000150static int
Owen Taylor3473f882001-02-23 17:55:21 +0000151xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
152 if ((node1 == NULL) || (node2 == NULL))
153 return(-2);
154 /*
155 * a couple of optimizations which will avoid computations in most cases
156 */
157 if (node1 == node2) {
158 if (index1 < index2)
159 return(1);
160 if (index1 > index2)
161 return(-1);
162 return(0);
163 }
164 return(xmlXPathCmpNodes(node1, node2));
165}
166
167/**
168 * xmlXPtrNewPoint:
169 * @node: the xmlNodePtr
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000170 * @indx: the indx within the node
Owen Taylor3473f882001-02-23 17:55:21 +0000171 *
172 * Create a new xmlXPathObjectPtr of type point
173 *
174 * Returns the newly created object.
175 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000176static xmlXPathObjectPtr
177xmlXPtrNewPoint(xmlNodePtr node, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000178 xmlXPathObjectPtr ret;
179
180 if (node == NULL)
181 return(NULL);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000182 if (indx < 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000183 return(NULL);
184
185 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
186 if (ret == NULL) {
187 xmlGenericError(xmlGenericErrorContext,
188 "xmlXPtrNewPoint: out of memory\n");
189 return(NULL);
190 }
191 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
192 ret->type = XPATH_POINT;
193 ret->user = (void *) node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000194 ret->index = indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000195 return(ret);
196}
197
198/**
199 * xmlXPtrRangeCheckOrder:
200 * @range: an object range
201 *
202 * Make sure the points in the range are in the right order
203 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000204static void
Owen Taylor3473f882001-02-23 17:55:21 +0000205xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
206 int tmp;
207 xmlNodePtr tmp2;
208 if (range == NULL)
209 return;
210 if (range->type != XPATH_RANGE)
211 return;
212 if (range->user2 == NULL)
213 return;
214 tmp = xmlXPtrCmpPoints(range->user, range->index,
215 range->user2, range->index2);
216 if (tmp == -1) {
217 tmp2 = range->user;
218 range->user = range->user2;
219 range->user2 = tmp2;
220 tmp = range->index;
221 range->index = range->index2;
222 range->index2 = tmp;
223 }
224}
225
226/**
227 * xmlXPtrRangesEqual:
228 * @range1: the first range
229 * @range2: the second range
230 *
231 * Compare two ranges
232 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000233 * Returns 1 if equal, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000234 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000235static int
Owen Taylor3473f882001-02-23 17:55:21 +0000236xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
237 if (range1 == range2)
238 return(1);
239 if ((range1 == NULL) || (range2 == NULL))
240 return(0);
241 if (range1->type != range2->type)
242 return(0);
243 if (range1->type != XPATH_RANGE)
244 return(0);
245 if (range1->user != range2->user)
246 return(0);
247 if (range1->index != range2->index)
248 return(0);
249 if (range1->user2 != range2->user2)
250 return(0);
251 if (range1->index2 != range2->index2)
252 return(0);
253 return(1);
254}
255
256/**
257 * xmlXPtrNewRange:
258 * @start: the starting node
259 * @startindex: the start index
260 * @end: the ending point
261 * @endindex: the ending index
262 *
263 * Create a new xmlXPathObjectPtr of type range
264 *
265 * Returns the newly created object.
266 */
267xmlXPathObjectPtr
268xmlXPtrNewRange(xmlNodePtr start, int startindex,
269 xmlNodePtr end, int endindex) {
270 xmlXPathObjectPtr ret;
271
272 if (start == NULL)
273 return(NULL);
274 if (end == NULL)
275 return(NULL);
276 if (startindex < 0)
277 return(NULL);
278 if (endindex < 0)
279 return(NULL);
280
281 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
282 if (ret == NULL) {
283 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000284 "xmlXPtrNewRange: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000285 return(NULL);
286 }
287 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
288 ret->type = XPATH_RANGE;
289 ret->user = start;
290 ret->index = startindex;
291 ret->user2 = end;
292 ret->index2 = endindex;
293 xmlXPtrRangeCheckOrder(ret);
294 return(ret);
295}
296
297/**
298 * xmlXPtrNewRangePoints:
299 * @start: the starting point
300 * @end: the ending point
301 *
302 * Create a new xmlXPathObjectPtr of type range using 2 Points
303 *
304 * Returns the newly created object.
305 */
306xmlXPathObjectPtr
307xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
308 xmlXPathObjectPtr ret;
309
310 if (start == NULL)
311 return(NULL);
312 if (end == NULL)
313 return(NULL);
314 if (start->type != XPATH_POINT)
315 return(NULL);
316 if (end->type != XPATH_POINT)
317 return(NULL);
318
319 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
320 if (ret == NULL) {
321 xmlGenericError(xmlGenericErrorContext,
322 "xmlXPtrNewRangePoints: out of memory\n");
323 return(NULL);
324 }
325 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
326 ret->type = XPATH_RANGE;
327 ret->user = start->user;
328 ret->index = start->index;
329 ret->user2 = end->user;
330 ret->index2 = end->index;
331 xmlXPtrRangeCheckOrder(ret);
332 return(ret);
333}
334
335/**
336 * xmlXPtrNewRangePointNode:
337 * @start: the starting point
338 * @end: the ending node
339 *
340 * Create a new xmlXPathObjectPtr of type range from a point to a node
341 *
342 * Returns the newly created object.
343 */
344xmlXPathObjectPtr
345xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
346 xmlXPathObjectPtr ret;
347
348 if (start == NULL)
349 return(NULL);
350 if (end == NULL)
351 return(NULL);
352 if (start->type != XPATH_POINT)
353 return(NULL);
354
355 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
356 if (ret == NULL) {
357 xmlGenericError(xmlGenericErrorContext,
358 "xmlXPtrNewRangePointNode: out of memory\n");
359 return(NULL);
360 }
361 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
362 ret->type = XPATH_RANGE;
363 ret->user = start->user;
364 ret->index = start->index;
365 ret->user2 = end;
366 ret->index2 = -1;
367 xmlXPtrRangeCheckOrder(ret);
368 return(ret);
369}
370
371/**
372 * xmlXPtrNewRangeNodePoint:
373 * @start: the starting node
374 * @end: the ending point
375 *
376 * Create a new xmlXPathObjectPtr of type range from a node to a point
377 *
378 * Returns the newly created object.
379 */
380xmlXPathObjectPtr
381xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
382 xmlXPathObjectPtr ret;
383
384 if (start == NULL)
385 return(NULL);
386 if (end == NULL)
387 return(NULL);
388 if (start->type != XPATH_POINT)
389 return(NULL);
390 if (end->type != XPATH_POINT)
391 return(NULL);
392
393 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
394 if (ret == NULL) {
395 xmlGenericError(xmlGenericErrorContext,
396 "xmlXPtrNewRangeNodePoint: out of memory\n");
397 return(NULL);
398 }
399 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
400 ret->type = XPATH_RANGE;
401 ret->user = start;
402 ret->index = -1;
403 ret->user2 = end->user;
404 ret->index2 = end->index;
405 xmlXPtrRangeCheckOrder(ret);
406 return(ret);
407}
408
409/**
410 * xmlXPtrNewRangeNodes:
411 * @start: the starting node
412 * @end: the ending node
413 *
414 * Create a new xmlXPathObjectPtr of type range using 2 nodes
415 *
416 * Returns the newly created object.
417 */
418xmlXPathObjectPtr
419xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
420 xmlXPathObjectPtr ret;
421
422 if (start == NULL)
423 return(NULL);
424 if (end == NULL)
425 return(NULL);
426
427 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
428 if (ret == NULL) {
429 xmlGenericError(xmlGenericErrorContext,
430 "xmlXPtrNewRangeNodes: out of memory\n");
431 return(NULL);
432 }
433 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
434 ret->type = XPATH_RANGE;
435 ret->user = start;
436 ret->index = -1;
437 ret->user2 = end;
438 ret->index2 = -1;
439 xmlXPtrRangeCheckOrder(ret);
440 return(ret);
441}
442
443/**
444 * xmlXPtrNewCollapsedRange:
445 * @start: the starting and ending node
446 *
447 * Create a new xmlXPathObjectPtr of type range using a single nodes
448 *
449 * Returns the newly created object.
450 */
451xmlXPathObjectPtr
452xmlXPtrNewCollapsedRange(xmlNodePtr start) {
453 xmlXPathObjectPtr ret;
454
455 if (start == NULL)
456 return(NULL);
457
458 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
459 if (ret == NULL) {
460 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000461 "xmlXPtrNewCollapsedRange: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000462 return(NULL);
463 }
464 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
465 ret->type = XPATH_RANGE;
466 ret->user = start;
467 ret->index = -1;
468 ret->user2 = NULL;
469 ret->index2 = -1;
470 return(ret);
471}
472
473/**
474 * xmlXPtrNewRangeNodeObject:
475 * @start: the starting node
476 * @end: the ending object
477 *
478 * Create a new xmlXPathObjectPtr of type range from a not to an object
479 *
480 * Returns the newly created object.
481 */
482xmlXPathObjectPtr
483xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
484 xmlXPathObjectPtr ret;
485
486 if (start == NULL)
487 return(NULL);
488 if (end == NULL)
489 return(NULL);
490 switch (end->type) {
491 case XPATH_POINT:
492 break;
493 case XPATH_NODESET:
494 /*
495 * Empty set ...
496 */
497 if (end->nodesetval->nodeNr <= 0)
498 return(NULL);
499 break;
500 default:
501 TODO
502 return(NULL);
503 }
504
505 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
506 if (ret == NULL) {
507 xmlGenericError(xmlGenericErrorContext,
508 "xmlXPtrNewRangeNodeObject: out of memory\n");
509 return(NULL);
510 }
511 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
512 ret->type = XPATH_RANGE;
513 ret->user = start;
514 ret->index = -1;
515 switch (end->type) {
516 case XPATH_POINT:
517 ret->user2 = end->user;
518 ret->index2 = end->index;
519 case XPATH_NODESET: {
520 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
521 ret->index2 = -1;
522 break;
523 }
524 default:
525 STRANGE
526 return(NULL);
527 }
528 xmlXPtrRangeCheckOrder(ret);
529 return(ret);
530}
531
532#define XML_RANGESET_DEFAULT 10
533
534/**
535 * xmlXPtrLocationSetCreate:
536 * @val: an initial xmlXPathObjectPtr, or NULL
537 *
538 * Create a new xmlLocationSetPtr of type double and of value @val
539 *
540 * Returns the newly created object.
541 */
542xmlLocationSetPtr
543xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
544 xmlLocationSetPtr ret;
545
546 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
547 if (ret == NULL) {
548 xmlGenericError(xmlGenericErrorContext,
549 "xmlXPtrLocationSetCreate: out of memory\n");
550 return(NULL);
551 }
552 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
553 if (val != NULL) {
554 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
555 sizeof(xmlXPathObjectPtr));
556 if (ret->locTab == NULL) {
557 xmlGenericError(xmlGenericErrorContext,
558 "xmlXPtrLocationSetCreate: out of memory\n");
559 return(NULL);
560 }
561 memset(ret->locTab, 0 ,
562 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
563 ret->locMax = XML_RANGESET_DEFAULT;
564 ret->locTab[ret->locNr++] = val;
565 }
566 return(ret);
567}
568
569/**
570 * xmlXPtrLocationSetAdd:
571 * @cur: the initial range set
572 * @val: a new xmlXPathObjectPtr
573 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000574 * add a new xmlXPathObjectPtr to an existing LocationSet
Owen Taylor3473f882001-02-23 17:55:21 +0000575 * If the location already exist in the set @val is freed.
576 */
577void
578xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
579 int i;
580
581 if (val == NULL) return;
582
583 /*
584 * check against doublons
585 */
586 for (i = 0;i < cur->locNr;i++) {
587 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
588 xmlXPathFreeObject(val);
589 return;
590 }
591 }
592
593 /*
594 * grow the locTab if needed
595 */
596 if (cur->locMax == 0) {
597 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
598 sizeof(xmlXPathObjectPtr));
599 if (cur->locTab == NULL) {
600 xmlGenericError(xmlGenericErrorContext,
601 "xmlXPtrLocationSetAdd: out of memory\n");
602 return;
603 }
604 memset(cur->locTab, 0 ,
605 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
606 cur->locMax = XML_RANGESET_DEFAULT;
607 } else if (cur->locNr == cur->locMax) {
608 xmlXPathObjectPtr *temp;
609
610 cur->locMax *= 2;
611 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
612 sizeof(xmlXPathObjectPtr));
613 if (temp == NULL) {
614 xmlGenericError(xmlGenericErrorContext,
615 "xmlXPtrLocationSetAdd: out of memory\n");
616 return;
617 }
618 cur->locTab = temp;
619 }
620 cur->locTab[cur->locNr++] = val;
621}
622
623/**
624 * xmlXPtrLocationSetMerge:
625 * @val1: the first LocationSet
626 * @val2: the second LocationSet
627 *
628 * Merges two rangesets, all ranges from @val2 are added to @val1
629 *
630 * Returns val1 once extended or NULL in case of error.
631 */
632xmlLocationSetPtr
633xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
634 int i;
635
636 if (val1 == NULL) return(NULL);
637 if (val2 == NULL) return(val1);
638
639 /*
640 * !!!!! this can be optimized a lot, knowing that both
641 * val1 and val2 already have unicity of their values.
642 */
643
644 for (i = 0;i < val2->locNr;i++)
645 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
646
647 return(val1);
648}
649
650/**
651 * xmlXPtrLocationSetDel:
652 * @cur: the initial range set
653 * @val: an xmlXPathObjectPtr
654 *
655 * Removes an xmlXPathObjectPtr from an existing LocationSet
656 */
657void
658xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
659 int i;
660
661 if (cur == NULL) return;
662 if (val == NULL) return;
663
664 /*
665 * check against doublons
666 */
667 for (i = 0;i < cur->locNr;i++)
668 if (cur->locTab[i] == val) break;
669
670 if (i >= cur->locNr) {
671#ifdef DEBUG
672 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard913d6e02001-11-28 14:53:53 +0000673 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000674#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,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000765 "xmlXPtrNewLocationSetNodeSet: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000766 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
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000912 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
Owen Taylor3473f882001-02-23 17:55:21 +0000913 *
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
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001057 * expressions or other schemes.
Owen Taylor3473f882001-02-23 17:55:21 +00001058 */
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 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001109 * Is there another XPointer part.
Owen Taylor3473f882001-02-23 17:55:21 +00001110 */
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 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001130 * XPointer don't allow by syntax to address in mutirooted trees
Owen Taylor3473f882001-02-23 17:55:21 +00001131 * 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,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001177 "xmlXPathEvalXPointer: out of memory\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001178 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
Owen Taylor3473f882001-02-23 17:55:21 +00001215void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1216void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1217void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1218void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1219void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1220void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1221void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1222
1223/**
1224 * xmlXPtrNewContext:
1225 * @doc: the XML document
1226 * @here: the node that directly contains the XPointer being evaluated or NULL
1227 * @origin: the element from which a user or program initiated traversal of
1228 * the link, or NULL.
1229 *
1230 * Create a new XPointer context
1231 *
1232 * Returns the xmlXPathContext just allocated.
1233 */
1234xmlXPathContextPtr
1235xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1236 xmlXPathContextPtr ret;
1237
1238 ret = xmlXPathNewContext(doc);
1239 if (ret == NULL)
1240 return(ret);
1241 ret->xptr = 1;
1242 ret->here = here;
1243 ret->origin = origin;
1244
1245 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1246 xmlXPtrRangeToFunction);
1247 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1248 xmlXPtrRangeFunction);
1249 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1250 xmlXPtrRangeInsideFunction);
1251 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1252 xmlXPtrStringRangeFunction);
1253 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1254 xmlXPtrStartPointFunction);
1255 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1256 xmlXPtrEndPointFunction);
1257 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1258 xmlXPtrHereFunction);
1259 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1260 xmlXPtrOriginFunction);
1261
1262 return(ret);
1263}
1264
1265/**
1266 * xmlXPtrEval:
1267 * @str: the XPointer expression
1268 * @ctx: the XPointer context
1269 *
1270 * Evaluate the XPath Location Path in the given context.
1271 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001272 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +00001273 * the caller has to free the object.
1274 */
1275xmlXPathObjectPtr
1276xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1277 xmlXPathParserContextPtr ctxt;
1278 xmlXPathObjectPtr res = NULL, tmp;
1279 xmlXPathObjectPtr init = NULL;
1280 int stack = 0;
1281
1282 xmlXPathInit();
1283
1284 if ((ctx == NULL) || (str == NULL))
1285 return(NULL);
1286
1287 ctxt = xmlXPathNewParserContext(str, ctx);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001288 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001289 xmlXPtrEvalXPointer(ctxt);
1290
1291 if ((ctxt->value != NULL) &&
1292 (ctxt->value->type != XPATH_NODESET) &&
1293 (ctxt->value->type != XPATH_LOCATIONSET)) {
1294 xmlGenericError(xmlGenericErrorContext,
1295 "xmlXPtrEval: evaluation failed to return a node set\n");
1296 } else {
1297 res = valuePop(ctxt);
1298 }
1299
1300 do {
1301 tmp = valuePop(ctxt);
1302 if (tmp != NULL) {
1303 if (tmp != init) {
1304 if (tmp->type == XPATH_NODESET) {
1305 /*
1306 * Evaluation may push a root nodeset which is unused
1307 */
1308 xmlNodeSetPtr set;
1309 set = tmp->nodesetval;
1310 if ((set->nodeNr != 1) ||
1311 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1312 stack++;
1313 } else
1314 stack++;
1315 }
1316 xmlXPathFreeObject(tmp);
1317 }
1318 } while (tmp != NULL);
1319 if (stack != 0) {
1320 xmlGenericError(xmlGenericErrorContext,
1321 "xmlXPtrEval: %d object left on the stack\n",
1322 stack);
1323 }
1324 if (ctxt->error != XPATH_EXPRESSION_OK) {
1325 xmlXPathFreeObject(res);
1326 res = NULL;
1327 }
1328
1329 xmlXPathFreeParserContext(ctxt);
1330 return(res);
1331}
1332
1333/**
1334 * xmlXPtrBuildRangeNodeList:
1335 * @range: a range object
1336 *
1337 * Build a node list tree copy of the range
1338 *
1339 * Returns an xmlNodePtr list or NULL.
1340 * the caller has to free the node tree.
1341 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001342static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001343xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1344 /* pointers to generated nodes */
1345 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1346 /* pointers to traversal nodes */
1347 xmlNodePtr start, cur, end;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001348 int index1, index2;
Owen Taylor3473f882001-02-23 17:55:21 +00001349
1350 if (range == NULL)
1351 return(NULL);
1352 if (range->type != XPATH_RANGE)
1353 return(NULL);
1354 start = (xmlNodePtr) range->user;
1355
1356 if (start == NULL)
1357 return(NULL);
1358 end = range->user2;
1359 if (end == NULL)
1360 return(xmlCopyNode(start, 1));
1361
1362 cur = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001363 index1 = range->index;
Owen Taylor3473f882001-02-23 17:55:21 +00001364 index2 = range->index2;
1365 while (cur != NULL) {
1366 if (cur == end) {
1367 if (cur->type == XML_TEXT_NODE) {
1368 const xmlChar *content = cur->content;
1369 int len;
1370
1371 if (content == NULL) {
1372 tmp = xmlNewTextLen(NULL, 0);
1373 } else {
1374 len = index2;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001375 if ((cur == start) && (index1 > 1)) {
1376 content += (index1 - 1);
1377 len -= (index1 - 1);
1378 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001379 } else {
1380 len = index2;
1381 }
1382 tmp = xmlNewTextLen(content, len);
1383 }
1384 /* single sub text node selection */
1385 if (list == NULL)
1386 return(tmp);
1387 /* prune and return full set */
1388 if (last != NULL)
1389 xmlAddNextSibling(last, tmp);
1390 else
1391 xmlAddChild(parent, tmp);
1392 return(list);
1393 } else {
1394 tmp = xmlCopyNode(cur, 0);
1395 if (list == NULL)
1396 list = tmp;
1397 else {
1398 if (last != NULL)
1399 xmlAddNextSibling(last, tmp);
1400 else
1401 xmlAddChild(parent, tmp);
1402 }
1403 last = NULL;
1404 parent = tmp;
1405
1406 if (index2 > 1) {
1407 end = xmlXPtrGetNthChild(cur, index2 - 1);
1408 index2 = 0;
1409 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001410 if ((cur == start) && (index1 > 1)) {
1411 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1412 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001413 } else {
1414 cur = cur->children;
1415 }
1416 /*
1417 * Now gather the remaining nodes from cur to end
1418 */
1419 continue; /* while */
1420 }
1421 } else if ((cur == start) &&
1422 (list == NULL) /* looks superfluous but ... */ ) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001423 if ((cur->type == XML_TEXT_NODE) ||
1424 (cur->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001425 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:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001464 TODO /* handle crossing entities -> stack needed */
Owen Taylor3473f882001-02-23 17:55:21 +00001465 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 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001602 * Count the number of location children of @node or the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001603 * 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
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001782 * location of type point to the result location-set. That point represents
Owen Taylor3473f882001-02-23 17:55:21 +00001783 * 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
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001790 * node of the resulting point is x and the index is the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001791 * 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:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002029 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002030 }
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:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002066 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002067 }
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;
Daniel Veillard7db37732001-07-12 01:20:08 +00002320 if ((cur->type != XML_ELEMENT_NODE) &&
2321 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002322#ifndef XML_USE_BUFFER_CONTENT
2323 len = xmlStrlen(cur->content);
2324#else
2325 len = xmlBufferLength(cur->content);
2326#endif
2327 }
2328 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002329 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002330 STRANGE
2331 pos = len;
2332 }
2333 if (pos + bytes >= len) {
2334 bytes -= (len - pos);
2335 cur = xmlXPtrAdvanceNode(cur);
2336 cur = 0;
2337 } else if (pos + bytes < len) {
2338 pos += bytes;
2339 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002340 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002341 return(0);
2342 }
2343 }
2344 return(-1);
2345}
2346
2347/**
2348 * xmlXPtrMatchString:
2349 * @string: the string to search
2350 * @start: the start textnode
2351 * @startindex: the start index
2352 * @end: the end textnode IN/OUT
2353 * @endindex: the end index IN/OUT
2354 *
2355 * Check whether the document contains @string at the position
2356 * (@start, @startindex) and limited by the (@end, @endindex) point
2357 *
2358 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2359 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002360 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002361 * of the range
2362 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002363static int
Owen Taylor3473f882001-02-23 17:55:21 +00002364xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2365 xmlNodePtr *end, int *endindex) {
2366 xmlNodePtr cur;
2367 int pos; /* 0 based */
2368 int len; /* in bytes */
2369 int stringlen; /* in bytes */
2370 int match;
2371
2372 if (string == NULL)
2373 return(-1);
2374 if (start == NULL)
2375 return(-1);
2376 if ((end == NULL) || (endindex == NULL))
2377 return(-1);
2378 cur = start;
2379 if (cur == NULL)
2380 return(-1);
2381 pos = startindex - 1;
2382 stringlen = xmlStrlen(string);
2383
2384 while (stringlen > 0) {
2385 if ((cur == *end) && (pos + stringlen > *endindex))
2386 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002387
2388 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002389#ifndef XML_USE_BUFFER_CONTENT
2390 len = xmlStrlen(cur->content);
2391#else
2392 len = xmlBufferLength(cur->content);
2393#endif
2394 if (len >= pos + stringlen) {
2395#ifndef XML_USE_BUFFER_CONTENT
2396 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2397#else
2398 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2399 string, stringlen));
2400#endif
2401 if (match) {
2402#ifdef DEBUG_RANGES
2403 xmlGenericError(xmlGenericErrorContext,
2404 "found range %d bytes at index %d of ->",
2405 stringlen, pos + 1);
2406 xmlDebugDumpString(stdout, cur->content);
2407 xmlGenericError(xmlGenericErrorContext, "\n");
2408#endif
2409 *end = cur;
2410 *endindex = pos + stringlen;
2411 return(1);
2412 } else {
2413 return(0);
2414 }
2415 } else {
2416 int sub = len - pos;
2417#ifndef XML_USE_BUFFER_CONTENT
2418 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2419#else
2420 len = (!xmlStrncmp(&xmlBufferContent(cur->content)[pos],
2421 string, sub));
2422#endif
2423 if (match) {
2424#ifdef DEBUG_RANGES
2425 xmlGenericError(xmlGenericErrorContext,
2426 "found subrange %d bytes at index %d of ->",
2427 sub, pos + 1);
2428 xmlDebugDumpString(stdout, cur->content);
2429 xmlGenericError(xmlGenericErrorContext, "\n");
2430#endif
2431 string = &string[sub];
2432 stringlen -= sub;
2433 } else {
2434 return(0);
2435 }
2436 }
2437 }
2438 cur = xmlXPtrAdvanceNode(cur);
2439 if (cur == NULL)
2440 return(0);
2441 pos = 0;
2442 }
2443 return(1);
2444}
2445
2446/**
2447 * xmlXPtrSearchString:
2448 * @string: the string to search
2449 * @start: the start textnode IN/OUT
2450 * @startindex: the start index IN/OUT
2451 * @end: the end textnode
2452 * @endindex: the end index
2453 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002454 * Search the next occurrence of @string within the document content
Owen Taylor3473f882001-02-23 17:55:21 +00002455 * until the (@end, @endindex) point is reached
2456 *
2457 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2458 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002459 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002460 * of the range
2461 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002462static int
Owen Taylor3473f882001-02-23 17:55:21 +00002463xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2464 xmlNodePtr *end, int *endindex) {
2465 xmlNodePtr cur;
2466 const xmlChar *str;
2467 int pos; /* 0 based */
2468 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002469 xmlChar first;
2470
2471 if (string == NULL)
2472 return(-1);
2473 if ((start == NULL) || (startindex == NULL))
2474 return(-1);
2475 if ((end == NULL) || (endindex == NULL))
2476 return(-1);
2477 cur = *start;
2478 if (cur == NULL)
2479 return(-1);
2480 pos = *startindex - 1;
2481 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002482
2483 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002484 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002485#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;
Daniel Veillard7db37732001-07-12 01:20:08 +00002581 else if ((cur->type != XML_ELEMENT_NODE) &&
2582 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002583#ifndef XML_USE_BUFFER_CONTENT
2584 len = xmlStrlen(cur->content);
2585#else
2586 len = xmlBufferLength(cur->content);
2587#endif
2588 break;
2589 } else {
2590 return(-1);
2591 }
2592 }
2593 if (cur == NULL)
2594 return(-1);
2595 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002596 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002597 return(0);
2598}
2599
2600/**
2601 * xmlXPtrGetStartPoint:
2602 * @obj: an range
2603 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002604 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002605 *
2606 * read the object and return the start point coordinates.
2607 *
2608 * Returns -1 in case of failure, 0 otherwise
2609 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002610static int
2611xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2612 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002613 return(-1);
2614
2615 switch (obj->type) {
2616 case XPATH_POINT:
2617 *node = obj->user;
2618 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002619 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002620 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002621 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002622 return(0);
2623 case XPATH_RANGE:
2624 *node = obj->user;
2625 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002626 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002627 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002628 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002629 return(0);
2630 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002631 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002632 }
2633 return(-1);
2634}
2635
2636/**
2637 * xmlXPtrGetEndPoint:
2638 * @obj: an range
2639 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002640 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002641 *
2642 * read the object and return the end point coordinates.
2643 *
2644 * Returns -1 in case of failure, 0 otherwise
2645 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002646static int
2647xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2648 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002649 return(-1);
2650
2651 switch (obj->type) {
2652 case XPATH_POINT:
2653 *node = obj->user;
2654 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002655 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002656 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002657 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002658 return(0);
2659 case XPATH_RANGE:
2660 *node = obj->user;
2661 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002662 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002663 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002664 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002665 return(0);
2666 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002667 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002668 }
2669 return(-1);
2670}
2671
2672/**
2673 * xmlXPtrStringRangeFunction:
2674 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002675 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002676 *
2677 * Function implementing the string-range() function
2678 * range as described in 5.4.2
2679 *
2680 * ------------------------------
2681 * [Definition: For each location in the location-set argument,
2682 * string-range returns a set of string ranges, a set of substrings in a
2683 * string. Specifically, the string-value of the location is searched for
2684 * substrings that match the string argument, and the resulting location-set
2685 * will contain a range location for each non-overlapping match.]
2686 * An empty string is considered to match before each character of the
2687 * string-value and after the final character. Whitespace in a string
2688 * is matched literally, with no normalization except that provided by
2689 * XML for line ends. The third argument gives the position of the first
2690 * character to be in the resulting range, relative to the start of the
2691 * match. The default value is 1, which makes the range start immediately
2692 * before the first character of the matched string. The fourth argument
2693 * gives the number of characters in the range; the default is that the
2694 * range extends to the end of the matched string.
2695 *
2696 * Element boundaries, as well as entire embedded nodes such as processing
2697 * instructions and comments, are ignored as defined in [XPath].
2698 *
2699 * If the string in the second argument is not found in the string-value
2700 * of the location, or if a value in the third or fourth argument indicates
2701 * a string that is beyond the beginning or end of the document, the
2702 * expression fails.
2703 *
2704 * The points of the range-locations in the returned location-set will
2705 * all be character points.
2706 * ------------------------------
2707 */
2708void
2709xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2710 int i, startindex, endindex, fendindex;
2711 xmlNodePtr start, end, fend;
2712 xmlXPathObjectPtr set;
2713 xmlLocationSetPtr oldset;
2714 xmlLocationSetPtr newset;
2715 xmlXPathObjectPtr string;
2716 xmlXPathObjectPtr position = NULL;
2717 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002718 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002719
2720 /*
2721 * Grab the arguments
2722 */
2723 if ((nargs < 2) || (nargs > 4))
2724 XP_ERROR(XPATH_INVALID_ARITY);
2725
2726 if (nargs >= 4) {
2727 CHECK_TYPE(XPATH_NUMBER);
2728 number = valuePop(ctxt);
2729 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002730 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002731 }
2732 if (nargs >= 3) {
2733 CHECK_TYPE(XPATH_NUMBER);
2734 position = valuePop(ctxt);
2735 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002736 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002737 }
2738 CHECK_TYPE(XPATH_STRING);
2739 string = valuePop(ctxt);
2740 if ((ctxt->value == NULL) ||
2741 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2742 (ctxt->value->type != XPATH_NODESET)))
2743 XP_ERROR(XPATH_INVALID_TYPE)
2744
2745 set = valuePop(ctxt);
2746 if (set->type == XPATH_NODESET) {
2747 xmlXPathObjectPtr tmp;
2748
2749 /*
2750 * First convert to a location set
2751 */
2752 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2753 xmlXPathFreeObject(set);
2754 set = tmp;
2755 }
2756 oldset = (xmlLocationSetPtr) set->user;
2757
2758 /*
2759 * The loop is to search for each element in the location set
2760 * the list of location set corresponding to that search
2761 */
2762 newset = xmlXPtrLocationSetCreate(NULL);
2763 for (i = 0;i < oldset->locNr;i++) {
2764#ifdef DEBUG_RANGES
2765 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2766#endif
2767
2768 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2769 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2770 xmlXPtrAdvanceChar(&start, &startindex, 0);
2771 xmlXPtrGetLastChar(&end, &endindex);
2772
2773#ifdef DEBUG_RANGES
2774 xmlGenericError(xmlGenericErrorContext,
2775 "from index %d of ->", startindex);
2776 xmlDebugDumpString(stdout, start->content);
2777 xmlGenericError(xmlGenericErrorContext, "\n");
2778 xmlGenericError(xmlGenericErrorContext,
2779 "to index %d of ->", endindex);
2780 xmlDebugDumpString(stdout, end->content);
2781 xmlGenericError(xmlGenericErrorContext, "\n");
2782#endif
2783 do {
2784 fend = end;
2785 fendindex = endindex;
2786 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2787 &fend, &fendindex);
2788 if (found == 1) {
2789 if (position == NULL) {
2790 xmlXPtrLocationSetAdd(newset,
2791 xmlXPtrNewRange(start, startindex, fend, fendindex));
2792 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2793 pos - 1) == 0) {
2794 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002795 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002796 xmlNodePtr rend;
2797 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002798 rindx = startindex - 1;
2799 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002800 num) == 0) {
2801 xmlXPtrLocationSetAdd(newset,
2802 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002803 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002804 }
2805 } else if ((number != NULL) && (num <= 0)) {
2806 xmlXPtrLocationSetAdd(newset,
2807 xmlXPtrNewRange(start, startindex,
2808 start, startindex));
2809 } else {
2810 xmlXPtrLocationSetAdd(newset,
2811 xmlXPtrNewRange(start, startindex,
2812 fend, fendindex));
2813 }
2814 }
2815 start = fend;
2816 startindex = fendindex;
2817 if (string->stringval[0] == 0)
2818 startindex++;
2819 }
2820 } while (found == 1);
2821 }
2822
2823 /*
2824 * Save the new value and cleanup
2825 */
2826 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2827 xmlXPathFreeObject(set);
2828 xmlXPathFreeObject(string);
2829 if (position) xmlXPathFreeObject(position);
2830 if (number) xmlXPathFreeObject(number);
2831}
2832
2833/**
2834 * xmlXPtrEvalRangePredicate:
2835 * @ctxt: the XPointer Parser context
2836 *
2837 * [8] Predicate ::= '[' PredicateExpr ']'
2838 * [9] PredicateExpr ::= Expr
2839 *
2840 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2841 * a Location Set instead of a node set
2842 */
2843void
2844xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2845 const xmlChar *cur;
2846 xmlXPathObjectPtr res;
2847 xmlXPathObjectPtr obj, tmp;
2848 xmlLocationSetPtr newset = NULL;
2849 xmlLocationSetPtr oldset;
2850 int i;
2851
2852 SKIP_BLANKS;
2853 if (CUR != '[') {
2854 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2855 }
2856 NEXT;
2857 SKIP_BLANKS;
2858
2859 /*
2860 * Extract the old set, and then evaluate the result of the
2861 * expression for all the element in the set. use it to grow
2862 * up a new set.
2863 */
2864 CHECK_TYPE(XPATH_LOCATIONSET);
2865 obj = valuePop(ctxt);
2866 oldset = obj->user;
2867 ctxt->context->node = NULL;
2868
2869 if ((oldset == NULL) || (oldset->locNr == 0)) {
2870 ctxt->context->contextSize = 0;
2871 ctxt->context->proximityPosition = 0;
2872 xmlXPathEvalExpr(ctxt);
2873 res = valuePop(ctxt);
2874 if (res != NULL)
2875 xmlXPathFreeObject(res);
2876 valuePush(ctxt, obj);
2877 CHECK_ERROR;
2878 } else {
2879 /*
2880 * Save the expression pointer since we will have to evaluate
2881 * it multiple times. Initialize the new set.
2882 */
2883 cur = ctxt->cur;
2884 newset = xmlXPtrLocationSetCreate(NULL);
2885
2886 for (i = 0; i < oldset->locNr; i++) {
2887 ctxt->cur = cur;
2888
2889 /*
2890 * Run the evaluation with a node list made of a single item
2891 * in the nodeset.
2892 */
2893 ctxt->context->node = oldset->locTab[i]->user;
2894 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2895 valuePush(ctxt, tmp);
2896 ctxt->context->contextSize = oldset->locNr;
2897 ctxt->context->proximityPosition = i + 1;
2898
2899 xmlXPathEvalExpr(ctxt);
2900 CHECK_ERROR;
2901
2902 /*
2903 * The result of the evaluation need to be tested to
2904 * decided whether the filter succeeded or not
2905 */
2906 res = valuePop(ctxt);
2907 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2908 xmlXPtrLocationSetAdd(newset,
2909 xmlXPathObjectCopy(oldset->locTab[i]));
2910 }
2911
2912 /*
2913 * Cleanup
2914 */
2915 if (res != NULL)
2916 xmlXPathFreeObject(res);
2917 if (ctxt->value == tmp) {
2918 res = valuePop(ctxt);
2919 xmlXPathFreeObject(res);
2920 }
2921
2922 ctxt->context->node = NULL;
2923 }
2924
2925 /*
2926 * The result is used as the new evaluation set.
2927 */
2928 xmlXPathFreeObject(obj);
2929 ctxt->context->node = NULL;
2930 ctxt->context->contextSize = -1;
2931 ctxt->context->proximityPosition = -1;
2932 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2933 }
2934 if (CUR != ']') {
2935 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2936 }
2937
2938 NEXT;
2939 SKIP_BLANKS;
2940}
2941
2942#else
2943#endif
2944