blob: 8ffb975f1fc80a149ac70245e7af8b6031dd307e [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:
Owen Taylor3473f882001-02-23 17:55:21 +00001631 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001632 break;
1633 default:
1634 return(-1);
1635 }
1636 return(ret);
1637}
1638
1639/**
1640 * xmlXPtrHereFunction:
1641 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001642 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001643 *
1644 * Function implementing here() operation
1645 * as described in 5.4.3
1646 */
1647void
1648xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001649 CHECK_ARITY(0);
1650
Owen Taylor3473f882001-02-23 17:55:21 +00001651 if (ctxt->context->here == NULL)
1652 XP_ERROR(XPTR_SYNTAX_ERROR);
1653
1654 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1655}
1656
1657/**
1658 * xmlXPtrOriginFunction:
1659 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001660 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001661 *
1662 * Function implementing origin() operation
1663 * as described in 5.4.3
1664 */
1665void
1666xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001667 CHECK_ARITY(0);
1668
Owen Taylor3473f882001-02-23 17:55:21 +00001669 if (ctxt->context->origin == NULL)
1670 XP_ERROR(XPTR_SYNTAX_ERROR);
1671
1672 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1673}
1674
1675/**
1676 * xmlXPtrStartPointFunction:
1677 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001678 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001679 *
1680 * Function implementing start-point() operation
1681 * as described in 5.4.3
1682 * ----------------
1683 * location-set start-point(location-set)
1684 *
1685 * For each location x in the argument location-set, start-point adds a
1686 * location of type point to the result location-set. That point represents
1687 * the start point of location x and is determined by the following rules:
1688 *
1689 * - If x is of type point, the start point is x.
1690 * - If x is of type range, the start point is the start point of x.
1691 * - If x is of type root, element, text, comment, or processing instruction,
1692 * - the container node of the start point is x and the index is 0.
1693 * - If x is of type attribute or namespace, the function must signal a
1694 * syntax error.
1695 * ----------------
1696 *
1697 */
1698void
1699xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1700 xmlXPathObjectPtr tmp, obj, point;
1701 xmlLocationSetPtr newset = NULL;
1702 xmlLocationSetPtr oldset = NULL;
1703
1704 CHECK_ARITY(1);
1705 if ((ctxt->value == NULL) ||
1706 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1707 (ctxt->value->type != XPATH_NODESET)))
1708 XP_ERROR(XPATH_INVALID_TYPE)
1709
1710 obj = valuePop(ctxt);
1711 if (obj->type == XPATH_NODESET) {
1712 /*
1713 * First convert to a location set
1714 */
1715 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1716 xmlXPathFreeObject(obj);
1717 obj = tmp;
1718 }
1719
1720 newset = xmlXPtrLocationSetCreate(NULL);
1721 if (newset == NULL) {
1722 xmlXPathFreeObject(obj);
1723 XP_ERROR(XPATH_MEMORY_ERROR);
1724 }
1725 oldset = (xmlLocationSetPtr) obj->user;
1726 if (oldset != NULL) {
1727 int i;
1728
1729 for (i = 0; i < oldset->locNr; i++) {
1730 tmp = oldset->locTab[i];
1731 if (tmp == NULL)
1732 continue;
1733 point = NULL;
1734 switch (tmp->type) {
1735 case XPATH_POINT:
1736 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1737 break;
1738 case XPATH_RANGE: {
1739 xmlNodePtr node = tmp->user;
1740 if (node != NULL) {
1741 if (node->type == XML_ATTRIBUTE_NODE) {
1742 /* TODO: Namespace Nodes ??? */
1743 xmlXPathFreeObject(obj);
1744 xmlXPtrFreeLocationSet(newset);
1745 XP_ERROR(XPTR_SYNTAX_ERROR);
1746 }
1747 point = xmlXPtrNewPoint(node, tmp->index);
1748 }
1749 break;
1750 }
1751 default:
1752 /*** Should we raise an error ?
1753 xmlXPathFreeObject(obj);
1754 xmlXPathFreeObject(newset);
1755 XP_ERROR(XPATH_INVALID_TYPE)
1756 ***/
1757 break;
1758 }
1759 if (point != NULL)
1760 xmlXPtrLocationSetAdd(newset, point);
1761 }
1762 }
1763 xmlXPathFreeObject(obj);
1764 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1765}
1766
1767/**
1768 * xmlXPtrEndPointFunction:
1769 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001770 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001771 *
1772 * Function implementing end-point() operation
1773 * as described in 5.4.3
1774 * ----------------------------
1775 * location-set end-point(location-set)
1776 *
1777 * For each location x in the argument location-set, end-point adds a
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001778 * location of type point to the result location-set. That point represents
Owen Taylor3473f882001-02-23 17:55:21 +00001779 * the end point of location x and is determined by the following rules:
1780 *
1781 * - If x is of type point, the resulting point is x.
1782 * - If x is of type range, the resulting point is the end point of x.
1783 * - If x is of type root or element, the container node of the resulting
1784 * point is x and the index is the number of location children of x.
1785 * - If x is of type text, comment, or processing instruction, the container
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001786 * node of the resulting point is x and the index is the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001787 * string-value of x.
1788 * - If x is of type attribute or namespace, the function must signal a
1789 * syntax error.
1790 * ----------------------------
1791 */
1792void
1793xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1794 xmlXPathObjectPtr tmp, obj, point;
1795 xmlLocationSetPtr newset = NULL;
1796 xmlLocationSetPtr oldset = NULL;
1797
1798 CHECK_ARITY(1);
1799 if ((ctxt->value == NULL) ||
1800 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1801 (ctxt->value->type != XPATH_NODESET)))
1802 XP_ERROR(XPATH_INVALID_TYPE)
1803
1804 obj = valuePop(ctxt);
1805 if (obj->type == XPATH_NODESET) {
1806 /*
1807 * First convert to a location set
1808 */
1809 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1810 xmlXPathFreeObject(obj);
1811 obj = tmp;
1812 }
1813
1814 newset = xmlXPtrLocationSetCreate(NULL);
1815 oldset = (xmlLocationSetPtr) obj->user;
1816 if (oldset != NULL) {
1817 int i;
1818
1819 for (i = 0; i < oldset->locNr; i++) {
1820 tmp = oldset->locTab[i];
1821 if (tmp == NULL)
1822 continue;
1823 point = NULL;
1824 switch (tmp->type) {
1825 case XPATH_POINT:
1826 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1827 break;
1828 case XPATH_RANGE: {
1829 xmlNodePtr node = tmp->user2;
1830 if (node != NULL) {
1831 if (node->type == XML_ATTRIBUTE_NODE) {
1832 /* TODO: Namespace Nodes ??? */
1833 xmlXPathFreeObject(obj);
1834 xmlXPtrFreeLocationSet(newset);
1835 XP_ERROR(XPTR_SYNTAX_ERROR);
1836 }
1837 point = xmlXPtrNewPoint(node, tmp->index2);
1838 } else if (tmp->user == NULL) {
1839 point = xmlXPtrNewPoint(node,
1840 xmlXPtrNbLocChildren(node));
1841 }
1842 break;
1843 }
1844 default:
1845 /*** Should we raise an error ?
1846 xmlXPathFreeObject(obj);
1847 xmlXPathFreeObject(newset);
1848 XP_ERROR(XPATH_INVALID_TYPE)
1849 ***/
1850 break;
1851 }
1852 if (point != NULL)
1853 xmlXPtrLocationSetAdd(newset, point);
1854 }
1855 }
1856 xmlXPathFreeObject(obj);
1857 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1858}
1859
1860
1861/**
1862 * xmlXPtrCoveringRange:
1863 * @ctxt: the XPointer Parser context
1864 * @loc: the location for which the covering range must be computed
1865 *
1866 * A covering range is a range that wholly encompasses a location
1867 * Section 5.3.3. Covering Ranges for All Location Types
1868 * http://www.w3.org/TR/xptr#N2267
1869 *
1870 * Returns a new location or NULL in case of error
1871 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001872static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001873xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1874 if (loc == NULL)
1875 return(NULL);
1876 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1877 (ctxt->context->doc == NULL))
1878 return(NULL);
1879 switch (loc->type) {
1880 case XPATH_POINT:
1881 return(xmlXPtrNewRange(loc->user, loc->index,
1882 loc->user, loc->index));
1883 case XPATH_RANGE:
1884 if (loc->user2 != NULL) {
1885 return(xmlXPtrNewRange(loc->user, loc->index,
1886 loc->user2, loc->index2));
1887 } else {
1888 xmlNodePtr node = (xmlNodePtr) loc->user;
1889 if (node == (xmlNodePtr) ctxt->context->doc) {
1890 return(xmlXPtrNewRange(node, 0, node,
1891 xmlXPtrGetArity(node)));
1892 } else {
1893 switch (node->type) {
1894 case XML_ATTRIBUTE_NODE:
1895 /* !!! our model is slightly different than XPath */
1896 return(xmlXPtrNewRange(node, 0, node,
1897 xmlXPtrGetArity(node)));
1898 case XML_ELEMENT_NODE:
1899 case XML_TEXT_NODE:
1900 case XML_CDATA_SECTION_NODE:
1901 case XML_ENTITY_REF_NODE:
1902 case XML_PI_NODE:
1903 case XML_COMMENT_NODE:
1904 case XML_DOCUMENT_NODE:
1905 case XML_NOTATION_NODE:
1906 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001907 int indx = xmlXPtrGetIndex(node);
Owen Taylor3473f882001-02-23 17:55:21 +00001908
1909 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001910 return(xmlXPtrNewRange(node, indx - 1,
1911 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00001912 }
1913 default:
1914 return(NULL);
1915 }
1916 }
1917 }
1918 default:
1919 TODO /* missed one case ??? */
1920 }
1921 return(NULL);
1922}
1923
1924/**
1925 * xmlXPtrRangeFunction:
1926 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001927 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001928 *
1929 * Function implementing the range() function 5.4.3
1930 * location-set range(location-set )
1931 *
1932 * The range function returns ranges covering the locations in
1933 * the argument location-set. For each location x in the argument
1934 * location-set, a range location representing the covering range of
1935 * x is added to the result location-set.
1936 */
1937void
1938xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1939 int i;
1940 xmlXPathObjectPtr set;
1941 xmlLocationSetPtr oldset;
1942 xmlLocationSetPtr newset;
1943
1944 CHECK_ARITY(1);
1945 if ((ctxt->value == NULL) ||
1946 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1947 (ctxt->value->type != XPATH_NODESET)))
1948 XP_ERROR(XPATH_INVALID_TYPE)
1949
1950 set = valuePop(ctxt);
1951 if (set->type == XPATH_NODESET) {
1952 xmlXPathObjectPtr tmp;
1953
1954 /*
1955 * First convert to a location set
1956 */
1957 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1958 xmlXPathFreeObject(set);
1959 set = tmp;
1960 }
1961 oldset = (xmlLocationSetPtr) set->user;
1962
1963 /*
1964 * The loop is to compute the covering range for each item and add it
1965 */
1966 newset = xmlXPtrLocationSetCreate(NULL);
1967 for (i = 0;i < oldset->locNr;i++) {
1968 xmlXPtrLocationSetAdd(newset,
1969 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
1970 }
1971
1972 /*
1973 * Save the new value and cleanup
1974 */
1975 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1976 xmlXPathFreeObject(set);
1977}
1978
1979/**
1980 * xmlXPtrInsideRange:
1981 * @ctxt: the XPointer Parser context
1982 * @loc: the location for which the inside range must be computed
1983 *
1984 * A inside range is a range described in the range-inside() description
1985 *
1986 * Returns a new location or NULL in case of error
1987 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001988static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001989xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1990 if (loc == NULL)
1991 return(NULL);
1992 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1993 (ctxt->context->doc == NULL))
1994 return(NULL);
1995 switch (loc->type) {
1996 case XPATH_POINT: {
1997 xmlNodePtr node = (xmlNodePtr) loc->user;
1998 switch (node->type) {
1999 case XML_PI_NODE:
2000 case XML_COMMENT_NODE:
2001 case XML_TEXT_NODE:
2002 case XML_CDATA_SECTION_NODE: {
2003 if (node->content == NULL) {
2004 return(xmlXPtrNewRange(node, 0, node, 0));
2005 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002006 return(xmlXPtrNewRange(node, 0, node,
2007 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002008 }
2009 }
2010 case XML_ATTRIBUTE_NODE:
2011 case XML_ELEMENT_NODE:
2012 case XML_ENTITY_REF_NODE:
2013 case XML_DOCUMENT_NODE:
2014 case XML_NOTATION_NODE:
2015 case XML_HTML_DOCUMENT_NODE: {
2016 return(xmlXPtrNewRange(node, 0, node,
2017 xmlXPtrGetArity(node)));
2018 }
2019 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002020 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002021 }
2022 return(NULL);
2023 }
2024 case XPATH_RANGE: {
2025 xmlNodePtr node = (xmlNodePtr) loc->user;
2026 if (loc->user2 != NULL) {
2027 return(xmlXPtrNewRange(node, loc->index,
2028 loc->user2, loc->index2));
2029 } else {
2030 switch (node->type) {
2031 case XML_PI_NODE:
2032 case XML_COMMENT_NODE:
2033 case XML_TEXT_NODE:
2034 case XML_CDATA_SECTION_NODE: {
2035 if (node->content == NULL) {
2036 return(xmlXPtrNewRange(node, 0, node, 0));
2037 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002038 return(xmlXPtrNewRange(node, 0, node,
2039 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002040 }
2041 }
2042 case XML_ATTRIBUTE_NODE:
2043 case XML_ELEMENT_NODE:
2044 case XML_ENTITY_REF_NODE:
2045 case XML_DOCUMENT_NODE:
2046 case XML_NOTATION_NODE:
2047 case XML_HTML_DOCUMENT_NODE: {
2048 return(xmlXPtrNewRange(node, 0, node,
2049 xmlXPtrGetArity(node)));
2050 }
2051 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002052 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002053 }
2054 return(NULL);
2055 }
2056 }
2057 default:
2058 TODO /* missed one case ??? */
2059 }
2060 return(NULL);
2061}
2062
2063/**
2064 * xmlXPtrRangeInsideFunction:
2065 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002066 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002067 *
2068 * Function implementing the range-inside() function 5.4.3
2069 * location-set range-inside(location-set )
2070 *
2071 * The range-inside function returns ranges covering the contents of
2072 * the locations in the argument location-set. For each location x in
2073 * the argument location-set, a range location is added to the result
2074 * location-set. If x is a range location, then x is added to the
2075 * result location-set. If x is not a range location, then x is used
2076 * as the container location of the start and end points of the range
2077 * location to be added; the index of the start point of the range is
2078 * zero; if the end point is a character point then its index is the
2079 * length of the string-value of x, and otherwise is the number of
2080 * location children of x.
2081 *
2082 */
2083void
2084xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2085 int i;
2086 xmlXPathObjectPtr set;
2087 xmlLocationSetPtr oldset;
2088 xmlLocationSetPtr newset;
2089
2090 CHECK_ARITY(1);
2091 if ((ctxt->value == NULL) ||
2092 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2093 (ctxt->value->type != XPATH_NODESET)))
2094 XP_ERROR(XPATH_INVALID_TYPE)
2095
2096 set = valuePop(ctxt);
2097 if (set->type == XPATH_NODESET) {
2098 xmlXPathObjectPtr tmp;
2099
2100 /*
2101 * First convert to a location set
2102 */
2103 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2104 xmlXPathFreeObject(set);
2105 set = tmp;
2106 }
2107 oldset = (xmlLocationSetPtr) set->user;
2108
2109 /*
2110 * The loop is to compute the covering range for each item and add it
2111 */
2112 newset = xmlXPtrLocationSetCreate(NULL);
2113 for (i = 0;i < oldset->locNr;i++) {
2114 xmlXPtrLocationSetAdd(newset,
2115 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2116 }
2117
2118 /*
2119 * Save the new value and cleanup
2120 */
2121 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2122 xmlXPathFreeObject(set);
2123}
2124
2125/**
2126 * xmlXPtrRangeToFunction:
2127 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002128 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002129 *
2130 * Implement the range-to() XPointer function
2131 */
2132void
2133xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2134 xmlXPathObjectPtr range;
2135 const xmlChar *cur;
2136 xmlXPathObjectPtr res, obj;
2137 xmlXPathObjectPtr tmp;
2138 xmlLocationSetPtr newset = NULL;
2139 xmlNodeSetPtr oldset;
2140 int i;
2141
2142 CHECK_ARITY(1);
2143 /*
2144 * Save the expression pointer since we will have to evaluate
2145 * it multiple times. Initialize the new set.
2146 */
2147 CHECK_TYPE(XPATH_NODESET);
2148 obj = valuePop(ctxt);
2149 oldset = obj->nodesetval;
2150 ctxt->context->node = NULL;
2151
2152 cur = ctxt->cur;
2153 newset = xmlXPtrLocationSetCreate(NULL);
2154
2155 for (i = 0; i < oldset->nodeNr; i++) {
2156 ctxt->cur = cur;
2157
2158 /*
2159 * Run the evaluation with a node list made of a single item
2160 * in the nodeset.
2161 */
2162 ctxt->context->node = oldset->nodeTab[i];
2163 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2164 valuePush(ctxt, tmp);
2165
2166 xmlXPathEvalExpr(ctxt);
2167 CHECK_ERROR;
2168
2169 /*
2170 * The result of the evaluation need to be tested to
2171 * decided whether the filter succeeded or not
2172 */
2173 res = valuePop(ctxt);
2174 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2175 if (range != NULL) {
2176 xmlXPtrLocationSetAdd(newset, range);
2177 }
2178
2179 /*
2180 * Cleanup
2181 */
2182 if (res != NULL)
2183 xmlXPathFreeObject(res);
2184 if (ctxt->value == tmp) {
2185 res = valuePop(ctxt);
2186 xmlXPathFreeObject(res);
2187 }
2188
2189 ctxt->context->node = NULL;
2190 }
2191
2192 /*
2193 * The result is used as the new evaluation set.
2194 */
2195 xmlXPathFreeObject(obj);
2196 ctxt->context->node = NULL;
2197 ctxt->context->contextSize = -1;
2198 ctxt->context->proximityPosition = -1;
2199 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2200}
2201
2202/**
2203 * xmlXPtrAdvanceNode:
2204 * @cur: the node
2205 *
2206 * Advance to the next element or text node in document order
2207 * TODO: add a stack for entering/exiting entities
2208 *
2209 * Returns -1 in case of failure, 0 otherwise
2210 */
2211xmlNodePtr
2212xmlXPtrAdvanceNode(xmlNodePtr cur) {
2213next:
2214 if (cur == NULL)
2215 return(NULL);
2216 if (cur->children != NULL) {
2217 cur = cur->children ;
2218 goto found;
2219 }
2220 if (cur->next != NULL) {
2221 cur = cur->next;
2222 goto found;
2223 }
2224 do {
2225 cur = cur->parent;
2226 if (cur == NULL) return(NULL);
2227 if (cur->next != NULL) {
2228 cur = cur->next;
2229 goto found;
2230 }
2231 } while (cur != NULL);
2232
2233found:
2234 if ((cur->type != XML_ELEMENT_NODE) &&
2235 (cur->type != XML_TEXT_NODE) &&
2236 (cur->type != XML_DOCUMENT_NODE) &&
2237 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2238 (cur->type != XML_CDATA_SECTION_NODE))
2239 goto next;
2240 if (cur->type == XML_ENTITY_REF_NODE) {
2241 TODO
2242 }
2243 return(cur);
2244}
2245
2246/**
2247 * xmlXPtrAdvanceChar:
2248 * @node: the node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002249 * @indx: the indx
Owen Taylor3473f882001-02-23 17:55:21 +00002250 * @bytes: the number of bytes
2251 *
2252 * Advance a point of the associated number of bytes (not UTF8 chars)
2253 *
2254 * Returns -1 in case of failure, 0 otherwise
2255 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002256static int
2257xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Owen Taylor3473f882001-02-23 17:55:21 +00002258 xmlNodePtr cur;
2259 int pos;
2260 int len;
2261
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002262 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002263 return(-1);
2264 cur = *node;
2265 if (cur == NULL)
2266 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002267 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002268
2269 while (bytes >= 0) {
2270 /*
2271 * First position to the beginning of the first text node
2272 * corresponding to this point
2273 */
2274 while ((cur != NULL) &&
2275 ((cur->type == XML_ELEMENT_NODE) ||
2276 (cur->type == XML_DOCUMENT_NODE) ||
2277 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2278 if (pos > 0) {
2279 cur = xmlXPtrGetNthChild(cur, pos);
2280 pos = 0;
2281 } else {
2282 cur = xmlXPtrAdvanceNode(cur);
2283 pos = 0;
2284 }
2285 }
2286
2287 if (cur == NULL) {
2288 *node = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002289 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002290 return(-1);
2291 }
2292
2293 /*
2294 * if there is no move needed return the current value.
2295 */
2296 if (pos == 0) pos = 1;
2297 if (bytes == 0) {
2298 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002299 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002300 return(0);
2301 }
2302 /*
2303 * We should have a text (or cdata) node ...
2304 */
2305 len = 0;
Daniel Veillard7db37732001-07-12 01:20:08 +00002306 if ((cur->type != XML_ELEMENT_NODE) &&
2307 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002308 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002309 }
2310 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002311 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002312 STRANGE
2313 pos = len;
2314 }
2315 if (pos + bytes >= len) {
2316 bytes -= (len - pos);
2317 cur = xmlXPtrAdvanceNode(cur);
2318 cur = 0;
2319 } else if (pos + bytes < len) {
2320 pos += bytes;
2321 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002322 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002323 return(0);
2324 }
2325 }
2326 return(-1);
2327}
2328
2329/**
2330 * xmlXPtrMatchString:
2331 * @string: the string to search
2332 * @start: the start textnode
2333 * @startindex: the start index
2334 * @end: the end textnode IN/OUT
2335 * @endindex: the end index IN/OUT
2336 *
2337 * Check whether the document contains @string at the position
2338 * (@start, @startindex) and limited by the (@end, @endindex) point
2339 *
2340 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2341 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002342 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002343 * of the range
2344 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002345static int
Owen Taylor3473f882001-02-23 17:55:21 +00002346xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2347 xmlNodePtr *end, int *endindex) {
2348 xmlNodePtr cur;
2349 int pos; /* 0 based */
2350 int len; /* in bytes */
2351 int stringlen; /* in bytes */
2352 int match;
2353
2354 if (string == NULL)
2355 return(-1);
2356 if (start == NULL)
2357 return(-1);
2358 if ((end == NULL) || (endindex == NULL))
2359 return(-1);
2360 cur = start;
2361 if (cur == NULL)
2362 return(-1);
2363 pos = startindex - 1;
2364 stringlen = xmlStrlen(string);
2365
2366 while (stringlen > 0) {
2367 if ((cur == *end) && (pos + stringlen > *endindex))
2368 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002369
2370 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002371 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002372 if (len >= pos + stringlen) {
Owen Taylor3473f882001-02-23 17:55:21 +00002373 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
Owen Taylor3473f882001-02-23 17:55:21 +00002374 if (match) {
2375#ifdef DEBUG_RANGES
2376 xmlGenericError(xmlGenericErrorContext,
2377 "found range %d bytes at index %d of ->",
2378 stringlen, pos + 1);
2379 xmlDebugDumpString(stdout, cur->content);
2380 xmlGenericError(xmlGenericErrorContext, "\n");
2381#endif
2382 *end = cur;
2383 *endindex = pos + stringlen;
2384 return(1);
2385 } else {
2386 return(0);
2387 }
2388 } else {
2389 int sub = len - pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002390 match = (!xmlStrncmp(&cur->content[pos], string, sub));
Owen Taylor3473f882001-02-23 17:55:21 +00002391 if (match) {
2392#ifdef DEBUG_RANGES
2393 xmlGenericError(xmlGenericErrorContext,
2394 "found subrange %d bytes at index %d of ->",
2395 sub, pos + 1);
2396 xmlDebugDumpString(stdout, cur->content);
2397 xmlGenericError(xmlGenericErrorContext, "\n");
2398#endif
2399 string = &string[sub];
2400 stringlen -= sub;
2401 } else {
2402 return(0);
2403 }
2404 }
2405 }
2406 cur = xmlXPtrAdvanceNode(cur);
2407 if (cur == NULL)
2408 return(0);
2409 pos = 0;
2410 }
2411 return(1);
2412}
2413
2414/**
2415 * xmlXPtrSearchString:
2416 * @string: the string to search
2417 * @start: the start textnode IN/OUT
2418 * @startindex: the start index IN/OUT
2419 * @end: the end textnode
2420 * @endindex: the end index
2421 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002422 * Search the next occurrence of @string within the document content
Owen Taylor3473f882001-02-23 17:55:21 +00002423 * until the (@end, @endindex) point is reached
2424 *
2425 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2426 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002427 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002428 * of the range
2429 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002430static int
Owen Taylor3473f882001-02-23 17:55:21 +00002431xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2432 xmlNodePtr *end, int *endindex) {
2433 xmlNodePtr cur;
2434 const xmlChar *str;
2435 int pos; /* 0 based */
2436 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002437 xmlChar first;
2438
2439 if (string == NULL)
2440 return(-1);
2441 if ((start == NULL) || (startindex == NULL))
2442 return(-1);
2443 if ((end == NULL) || (endindex == NULL))
2444 return(-1);
2445 cur = *start;
2446 if (cur == NULL)
2447 return(-1);
2448 pos = *startindex - 1;
2449 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002450
2451 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002452 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002453 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002454 while (pos <= len) {
2455 if (first != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00002456 str = xmlStrchr(&cur->content[pos], first);
Owen Taylor3473f882001-02-23 17:55:21 +00002457 if (str != NULL) {
2458 pos = (str - (xmlChar *)(cur->content));
2459#ifdef DEBUG_RANGES
2460 xmlGenericError(xmlGenericErrorContext,
2461 "found '%c' at index %d of ->",
2462 first, pos + 1);
2463 xmlDebugDumpString(stdout, cur->content);
2464 xmlGenericError(xmlGenericErrorContext, "\n");
2465#endif
2466 if (xmlXPtrMatchString(string, cur, pos + 1,
2467 end, endindex)) {
2468 *start = cur;
2469 *startindex = pos + 1;
2470 return(1);
2471 }
2472 pos++;
2473 } else {
2474 pos = len + 1;
2475 }
2476 } else {
2477 /*
2478 * An empty string is considered to match before each
2479 * character of the string-value and after the final
2480 * character.
2481 */
2482#ifdef DEBUG_RANGES
2483 xmlGenericError(xmlGenericErrorContext,
2484 "found '' at index %d of ->",
2485 pos + 1);
2486 xmlDebugDumpString(stdout, cur->content);
2487 xmlGenericError(xmlGenericErrorContext, "\n");
2488#endif
2489 *start = cur;
2490 *startindex = pos + 1;
2491 *end = cur;
2492 *endindex = pos + 1;
2493 return(1);
2494 }
2495 }
2496 }
2497 if ((cur == *end) && (pos >= *endindex))
2498 return(0);
2499 cur = xmlXPtrAdvanceNode(cur);
2500 if (cur == NULL)
2501 return(0);
2502 pos = 1;
2503 }
2504 return(0);
2505}
2506
2507/**
2508 * xmlXPtrGetLastChar:
2509 * @node: the node
2510 * @index: the index
2511 *
2512 * Computes the point coordinates of the last char of this point
2513 *
2514 * Returns -1 in case of failure, 0 otherwise
2515 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002516static int
2517xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002518 xmlNodePtr cur;
2519 int pos, len = 0;
2520
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002521 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002522 return(-1);
2523 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002524 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002525
2526 if (cur == NULL)
2527 return(-1);
2528
2529 if ((cur->type == XML_ELEMENT_NODE) ||
2530 (cur->type == XML_DOCUMENT_NODE) ||
2531 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2532 if (pos > 0) {
2533 cur = xmlXPtrGetNthChild(cur, pos);
2534 pos = 0;
2535 }
2536 }
2537 while (cur != NULL) {
2538 if (cur->last != NULL)
2539 cur = cur->last;
Daniel Veillard7db37732001-07-12 01:20:08 +00002540 else if ((cur->type != XML_ELEMENT_NODE) &&
2541 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002542 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002543 break;
2544 } else {
2545 return(-1);
2546 }
2547 }
2548 if (cur == NULL)
2549 return(-1);
2550 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002551 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002552 return(0);
2553}
2554
2555/**
2556 * xmlXPtrGetStartPoint:
2557 * @obj: an range
2558 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002559 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002560 *
2561 * read the object and return the start point coordinates.
2562 *
2563 * Returns -1 in case of failure, 0 otherwise
2564 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002565static int
2566xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2567 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002568 return(-1);
2569
2570 switch (obj->type) {
2571 case XPATH_POINT:
2572 *node = obj->user;
2573 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002574 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002575 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002576 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002577 return(0);
2578 case XPATH_RANGE:
2579 *node = obj->user;
2580 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002581 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002582 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002583 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002584 return(0);
2585 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002586 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002587 }
2588 return(-1);
2589}
2590
2591/**
2592 * xmlXPtrGetEndPoint:
2593 * @obj: an range
2594 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002595 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002596 *
2597 * read the object and return the end point coordinates.
2598 *
2599 * Returns -1 in case of failure, 0 otherwise
2600 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002601static int
2602xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2603 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002604 return(-1);
2605
2606 switch (obj->type) {
2607 case XPATH_POINT:
2608 *node = obj->user;
2609 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002610 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002611 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002612 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002613 return(0);
2614 case XPATH_RANGE:
2615 *node = obj->user;
2616 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002617 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002618 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002619 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002620 return(0);
2621 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002622 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002623 }
2624 return(-1);
2625}
2626
2627/**
2628 * xmlXPtrStringRangeFunction:
2629 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002630 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002631 *
2632 * Function implementing the string-range() function
2633 * range as described in 5.4.2
2634 *
2635 * ------------------------------
2636 * [Definition: For each location in the location-set argument,
2637 * string-range returns a set of string ranges, a set of substrings in a
2638 * string. Specifically, the string-value of the location is searched for
2639 * substrings that match the string argument, and the resulting location-set
2640 * will contain a range location for each non-overlapping match.]
2641 * An empty string is considered to match before each character of the
2642 * string-value and after the final character. Whitespace in a string
2643 * is matched literally, with no normalization except that provided by
2644 * XML for line ends. The third argument gives the position of the first
2645 * character to be in the resulting range, relative to the start of the
2646 * match. The default value is 1, which makes the range start immediately
2647 * before the first character of the matched string. The fourth argument
2648 * gives the number of characters in the range; the default is that the
2649 * range extends to the end of the matched string.
2650 *
2651 * Element boundaries, as well as entire embedded nodes such as processing
2652 * instructions and comments, are ignored as defined in [XPath].
2653 *
2654 * If the string in the second argument is not found in the string-value
2655 * of the location, or if a value in the third or fourth argument indicates
2656 * a string that is beyond the beginning or end of the document, the
2657 * expression fails.
2658 *
2659 * The points of the range-locations in the returned location-set will
2660 * all be character points.
2661 * ------------------------------
2662 */
2663void
2664xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2665 int i, startindex, endindex, fendindex;
2666 xmlNodePtr start, end, fend;
2667 xmlXPathObjectPtr set;
2668 xmlLocationSetPtr oldset;
2669 xmlLocationSetPtr newset;
2670 xmlXPathObjectPtr string;
2671 xmlXPathObjectPtr position = NULL;
2672 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002673 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002674
2675 /*
2676 * Grab the arguments
2677 */
2678 if ((nargs < 2) || (nargs > 4))
2679 XP_ERROR(XPATH_INVALID_ARITY);
2680
2681 if (nargs >= 4) {
2682 CHECK_TYPE(XPATH_NUMBER);
2683 number = valuePop(ctxt);
2684 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002685 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002686 }
2687 if (nargs >= 3) {
2688 CHECK_TYPE(XPATH_NUMBER);
2689 position = valuePop(ctxt);
2690 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002691 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002692 }
2693 CHECK_TYPE(XPATH_STRING);
2694 string = valuePop(ctxt);
2695 if ((ctxt->value == NULL) ||
2696 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2697 (ctxt->value->type != XPATH_NODESET)))
2698 XP_ERROR(XPATH_INVALID_TYPE)
2699
2700 set = valuePop(ctxt);
2701 if (set->type == XPATH_NODESET) {
2702 xmlXPathObjectPtr tmp;
2703
2704 /*
2705 * First convert to a location set
2706 */
2707 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2708 xmlXPathFreeObject(set);
2709 set = tmp;
2710 }
2711 oldset = (xmlLocationSetPtr) set->user;
2712
2713 /*
2714 * The loop is to search for each element in the location set
2715 * the list of location set corresponding to that search
2716 */
2717 newset = xmlXPtrLocationSetCreate(NULL);
2718 for (i = 0;i < oldset->locNr;i++) {
2719#ifdef DEBUG_RANGES
2720 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2721#endif
2722
2723 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2724 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2725 xmlXPtrAdvanceChar(&start, &startindex, 0);
2726 xmlXPtrGetLastChar(&end, &endindex);
2727
2728#ifdef DEBUG_RANGES
2729 xmlGenericError(xmlGenericErrorContext,
2730 "from index %d of ->", startindex);
2731 xmlDebugDumpString(stdout, start->content);
2732 xmlGenericError(xmlGenericErrorContext, "\n");
2733 xmlGenericError(xmlGenericErrorContext,
2734 "to index %d of ->", endindex);
2735 xmlDebugDumpString(stdout, end->content);
2736 xmlGenericError(xmlGenericErrorContext, "\n");
2737#endif
2738 do {
2739 fend = end;
2740 fendindex = endindex;
2741 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2742 &fend, &fendindex);
2743 if (found == 1) {
2744 if (position == NULL) {
2745 xmlXPtrLocationSetAdd(newset,
2746 xmlXPtrNewRange(start, startindex, fend, fendindex));
2747 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2748 pos - 1) == 0) {
2749 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002750 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002751 xmlNodePtr rend;
2752 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002753 rindx = startindex - 1;
2754 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002755 num) == 0) {
2756 xmlXPtrLocationSetAdd(newset,
2757 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002758 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002759 }
2760 } else if ((number != NULL) && (num <= 0)) {
2761 xmlXPtrLocationSetAdd(newset,
2762 xmlXPtrNewRange(start, startindex,
2763 start, startindex));
2764 } else {
2765 xmlXPtrLocationSetAdd(newset,
2766 xmlXPtrNewRange(start, startindex,
2767 fend, fendindex));
2768 }
2769 }
2770 start = fend;
2771 startindex = fendindex;
2772 if (string->stringval[0] == 0)
2773 startindex++;
2774 }
2775 } while (found == 1);
2776 }
2777
2778 /*
2779 * Save the new value and cleanup
2780 */
2781 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2782 xmlXPathFreeObject(set);
2783 xmlXPathFreeObject(string);
2784 if (position) xmlXPathFreeObject(position);
2785 if (number) xmlXPathFreeObject(number);
2786}
2787
2788/**
2789 * xmlXPtrEvalRangePredicate:
2790 * @ctxt: the XPointer Parser context
2791 *
2792 * [8] Predicate ::= '[' PredicateExpr ']'
2793 * [9] PredicateExpr ::= Expr
2794 *
2795 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2796 * a Location Set instead of a node set
2797 */
2798void
2799xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2800 const xmlChar *cur;
2801 xmlXPathObjectPtr res;
2802 xmlXPathObjectPtr obj, tmp;
2803 xmlLocationSetPtr newset = NULL;
2804 xmlLocationSetPtr oldset;
2805 int i;
2806
2807 SKIP_BLANKS;
2808 if (CUR != '[') {
2809 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2810 }
2811 NEXT;
2812 SKIP_BLANKS;
2813
2814 /*
2815 * Extract the old set, and then evaluate the result of the
2816 * expression for all the element in the set. use it to grow
2817 * up a new set.
2818 */
2819 CHECK_TYPE(XPATH_LOCATIONSET);
2820 obj = valuePop(ctxt);
2821 oldset = obj->user;
2822 ctxt->context->node = NULL;
2823
2824 if ((oldset == NULL) || (oldset->locNr == 0)) {
2825 ctxt->context->contextSize = 0;
2826 ctxt->context->proximityPosition = 0;
2827 xmlXPathEvalExpr(ctxt);
2828 res = valuePop(ctxt);
2829 if (res != NULL)
2830 xmlXPathFreeObject(res);
2831 valuePush(ctxt, obj);
2832 CHECK_ERROR;
2833 } else {
2834 /*
2835 * Save the expression pointer since we will have to evaluate
2836 * it multiple times. Initialize the new set.
2837 */
2838 cur = ctxt->cur;
2839 newset = xmlXPtrLocationSetCreate(NULL);
2840
2841 for (i = 0; i < oldset->locNr; i++) {
2842 ctxt->cur = cur;
2843
2844 /*
2845 * Run the evaluation with a node list made of a single item
2846 * in the nodeset.
2847 */
2848 ctxt->context->node = oldset->locTab[i]->user;
2849 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2850 valuePush(ctxt, tmp);
2851 ctxt->context->contextSize = oldset->locNr;
2852 ctxt->context->proximityPosition = i + 1;
2853
2854 xmlXPathEvalExpr(ctxt);
2855 CHECK_ERROR;
2856
2857 /*
2858 * The result of the evaluation need to be tested to
2859 * decided whether the filter succeeded or not
2860 */
2861 res = valuePop(ctxt);
2862 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2863 xmlXPtrLocationSetAdd(newset,
2864 xmlXPathObjectCopy(oldset->locTab[i]));
2865 }
2866
2867 /*
2868 * Cleanup
2869 */
2870 if (res != NULL)
2871 xmlXPathFreeObject(res);
2872 if (ctxt->value == tmp) {
2873 res = valuePop(ctxt);
2874 xmlXPathFreeObject(res);
2875 }
2876
2877 ctxt->context->node = NULL;
2878 }
2879
2880 /*
2881 * The result is used as the new evaluation set.
2882 */
2883 xmlXPathFreeObject(obj);
2884 ctxt->context->node = NULL;
2885 ctxt->context->contextSize = -1;
2886 ctxt->context->proximityPosition = -1;
2887 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2888 }
2889 if (CUR != ']') {
2890 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2891 }
2892
2893 NEXT;
2894 SKIP_BLANKS;
2895}
2896
2897#else
2898#endif
2899