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