blob: 4c4cf991c5c0150b6a1f2c8e6d2c904ff21a8c19 [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;
Daniel Veillard56f21f22002-11-06 15:49:46 +00001024 xmlFree(URI);
1025 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001026#endif /* XPTR_XMLNS_SCHEME */
1027 } else {
1028 xmlGenericError(xmlGenericErrorContext,
1029 "unsupported scheme '%s'\n", name);
1030 }
1031 xmlFree(buffer);
1032 xmlFree(name);
1033}
1034
1035/**
1036 * xmlXPtrEvalFullXPtr:
1037 * @ctxt: the XPointer Parser context
1038 * @name: the preparsed Scheme for the first XPtrPart
1039 *
1040 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1041 *
1042 * As the specs says:
1043 * -----------
1044 * When multiple XPtrParts are provided, they must be evaluated in
1045 * left-to-right order. If evaluation of one part fails, the nexti
1046 * is evaluated. The following conditions cause XPointer part failure:
1047 *
1048 * - An unknown scheme
1049 * - A scheme that does not locate any sub-resource present in the resource
1050 * - A scheme that is not applicable to the media type of the resource
1051 *
1052 * The XPointer application must consume a failed XPointer part and
1053 * attempt to evaluate the next one, if any. The result of the first
1054 * XPointer part whose evaluation succeeds is taken to be the fragment
1055 * located by the XPointer as a whole. If all the parts fail, the result
1056 * for the XPointer as a whole is a sub-resource error.
1057 * -----------
1058 *
1059 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001060 * expressions or other schemes.
Owen Taylor3473f882001-02-23 17:55:21 +00001061 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001062static void
Owen Taylor3473f882001-02-23 17:55:21 +00001063xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1064 if (name == NULL)
1065 name = xmlXPathParseName(ctxt);
1066 if (name == NULL)
1067 XP_ERROR(XPATH_EXPR_ERROR);
1068 while (name != NULL) {
1069 xmlXPtrEvalXPtrPart(ctxt, name);
1070
1071 /* in case of syntax error, break here */
1072 if (ctxt->error != XPATH_EXPRESSION_OK)
1073 return;
1074
1075 /*
1076 * If the returned value is a non-empty nodeset
1077 * or location set, return here.
1078 */
1079 if (ctxt->value != NULL) {
1080 xmlXPathObjectPtr obj = ctxt->value;
1081
1082 switch (obj->type) {
1083 case XPATH_LOCATIONSET: {
1084 xmlLocationSetPtr loc = ctxt->value->user;
1085 if ((loc != NULL) && (loc->locNr > 0))
1086 return;
1087 break;
1088 }
1089 case XPATH_NODESET: {
1090 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1091 if ((loc != NULL) && (loc->nodeNr > 0))
1092 return;
1093 break;
1094 }
1095 default:
1096 break;
1097 }
1098
1099 /*
1100 * Evaluating to improper values is equivalent to
1101 * a sub-resource error, clean-up the stack
1102 */
1103 do {
1104 obj = valuePop(ctxt);
1105 if (obj != NULL) {
1106 xmlXPathFreeObject(obj);
1107 }
1108 } while (obj != NULL);
1109 }
1110
1111 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001112 * Is there another XPointer part.
Owen Taylor3473f882001-02-23 17:55:21 +00001113 */
1114 SKIP_BLANKS;
1115 name = xmlXPathParseName(ctxt);
1116 }
1117}
1118
1119/**
1120 * xmlXPtrEvalChildSeq:
1121 * @ctxt: the XPointer Parser context
1122 * @name: a possible ID name of the child sequence
1123 *
1124 * ChildSeq ::= '/1' ('/' [0-9]*)*
1125 * | Name ('/' [0-9]*)+
1126 *
1127 * Parse and evaluate a Child Sequence. This routine also handle the
1128 * case of a Bare Name used to get a document ID.
1129 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001130static void
Owen Taylor3473f882001-02-23 17:55:21 +00001131xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1132 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001133 * XPointer don't allow by syntax to address in mutirooted trees
Owen Taylor3473f882001-02-23 17:55:21 +00001134 * this might prove useful in some cases, warn about it.
1135 */
1136 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
1137 xmlGenericError(xmlGenericErrorContext,
1138 "warning: ChildSeq not starting by /1\n");
1139 }
1140
1141 if (name != NULL) {
1142 valuePush(ctxt, xmlXPathNewString(name));
1143 xmlFree(name);
1144 xmlXPathIdFunction(ctxt, 1);
1145 CHECK_ERROR;
1146 }
1147
1148 while (CUR == '/') {
1149 int child = 0;
1150 NEXT;
1151
1152 while ((CUR >= '0') && (CUR <= '9')) {
1153 child = child * 10 + (CUR - '0');
1154 NEXT;
1155 }
1156 xmlXPtrGetChildNo(ctxt, child);
1157 }
1158}
1159
1160
1161/**
1162 * xmlXPtrEvalXPointer:
1163 * @ctxt: the XPointer Parser context
1164 *
1165 * XPointer ::= Name
1166 * | ChildSeq
1167 * | FullXPtr
1168 *
1169 * Parse and evaluate an XPointer
1170 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001171static void
Owen Taylor3473f882001-02-23 17:55:21 +00001172xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001173 if (ctxt->valueTab == NULL) {
1174 /* Allocate the value stack */
1175 ctxt->valueTab = (xmlXPathObjectPtr *)
1176 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1177 if (ctxt->valueTab == NULL) {
1178 xmlFree(ctxt);
1179 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001180 "xmlXPathEvalXPointer: out of memory\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001181 return;
1182 }
1183 ctxt->valueNr = 0;
1184 ctxt->valueMax = 10;
1185 ctxt->value = NULL;
1186 }
Owen Taylor3473f882001-02-23 17:55:21 +00001187 SKIP_BLANKS;
1188 if (CUR == '/') {
1189 xmlXPathRoot(ctxt);
1190 xmlXPtrEvalChildSeq(ctxt, NULL);
1191 } else {
1192 xmlChar *name;
1193
1194 name = xmlXPathParseName(ctxt);
1195 if (name == NULL)
1196 XP_ERROR(XPATH_EXPR_ERROR);
1197 if (CUR == '(') {
1198 xmlXPtrEvalFullXPtr(ctxt, name);
1199 /* Short evaluation */
1200 return;
1201 } else {
1202 /* this handle both Bare Names and Child Sequences */
1203 xmlXPtrEvalChildSeq(ctxt, name);
1204 }
1205 }
1206 SKIP_BLANKS;
1207 if (CUR != 0)
1208 XP_ERROR(XPATH_EXPR_ERROR);
1209}
1210
1211
1212/************************************************************************
1213 * *
1214 * General routines *
1215 * *
1216 ************************************************************************/
1217
Owen Taylor3473f882001-02-23 17:55:21 +00001218void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1219void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1220void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1221void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1222void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1223void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1224void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1225
1226/**
1227 * xmlXPtrNewContext:
1228 * @doc: the XML document
1229 * @here: the node that directly contains the XPointer being evaluated or NULL
1230 * @origin: the element from which a user or program initiated traversal of
1231 * the link, or NULL.
1232 *
1233 * Create a new XPointer context
1234 *
1235 * Returns the xmlXPathContext just allocated.
1236 */
1237xmlXPathContextPtr
1238xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1239 xmlXPathContextPtr ret;
1240
1241 ret = xmlXPathNewContext(doc);
1242 if (ret == NULL)
1243 return(ret);
1244 ret->xptr = 1;
1245 ret->here = here;
1246 ret->origin = origin;
1247
1248 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1249 xmlXPtrRangeToFunction);
1250 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1251 xmlXPtrRangeFunction);
1252 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1253 xmlXPtrRangeInsideFunction);
1254 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1255 xmlXPtrStringRangeFunction);
1256 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1257 xmlXPtrStartPointFunction);
1258 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1259 xmlXPtrEndPointFunction);
1260 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1261 xmlXPtrHereFunction);
1262 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1263 xmlXPtrOriginFunction);
1264
1265 return(ret);
1266}
1267
1268/**
1269 * xmlXPtrEval:
1270 * @str: the XPointer expression
1271 * @ctx: the XPointer context
1272 *
1273 * Evaluate the XPath Location Path in the given context.
1274 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001275 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +00001276 * the caller has to free the object.
1277 */
1278xmlXPathObjectPtr
1279xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1280 xmlXPathParserContextPtr ctxt;
1281 xmlXPathObjectPtr res = NULL, tmp;
1282 xmlXPathObjectPtr init = NULL;
1283 int stack = 0;
1284
1285 xmlXPathInit();
1286
1287 if ((ctx == NULL) || (str == NULL))
1288 return(NULL);
1289
1290 ctxt = xmlXPathNewParserContext(str, ctx);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001291 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001292 xmlXPtrEvalXPointer(ctxt);
1293
1294 if ((ctxt->value != NULL) &&
1295 (ctxt->value->type != XPATH_NODESET) &&
1296 (ctxt->value->type != XPATH_LOCATIONSET)) {
1297 xmlGenericError(xmlGenericErrorContext,
1298 "xmlXPtrEval: evaluation failed to return a node set\n");
1299 } else {
1300 res = valuePop(ctxt);
1301 }
1302
1303 do {
1304 tmp = valuePop(ctxt);
1305 if (tmp != NULL) {
1306 if (tmp != init) {
1307 if (tmp->type == XPATH_NODESET) {
1308 /*
1309 * Evaluation may push a root nodeset which is unused
1310 */
1311 xmlNodeSetPtr set;
1312 set = tmp->nodesetval;
1313 if ((set->nodeNr != 1) ||
1314 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1315 stack++;
1316 } else
1317 stack++;
1318 }
1319 xmlXPathFreeObject(tmp);
1320 }
1321 } while (tmp != NULL);
1322 if (stack != 0) {
1323 xmlGenericError(xmlGenericErrorContext,
1324 "xmlXPtrEval: %d object left on the stack\n",
1325 stack);
1326 }
1327 if (ctxt->error != XPATH_EXPRESSION_OK) {
1328 xmlXPathFreeObject(res);
1329 res = NULL;
1330 }
1331
1332 xmlXPathFreeParserContext(ctxt);
1333 return(res);
1334}
1335
1336/**
1337 * xmlXPtrBuildRangeNodeList:
1338 * @range: a range object
1339 *
1340 * Build a node list tree copy of the range
1341 *
1342 * Returns an xmlNodePtr list or NULL.
1343 * the caller has to free the node tree.
1344 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001345static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001346xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1347 /* pointers to generated nodes */
1348 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1349 /* pointers to traversal nodes */
1350 xmlNodePtr start, cur, end;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001351 int index1, index2;
Owen Taylor3473f882001-02-23 17:55:21 +00001352
1353 if (range == NULL)
1354 return(NULL);
1355 if (range->type != XPATH_RANGE)
1356 return(NULL);
1357 start = (xmlNodePtr) range->user;
1358
1359 if (start == NULL)
1360 return(NULL);
1361 end = range->user2;
1362 if (end == NULL)
1363 return(xmlCopyNode(start, 1));
1364
1365 cur = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001366 index1 = range->index;
Owen Taylor3473f882001-02-23 17:55:21 +00001367 index2 = range->index2;
1368 while (cur != NULL) {
1369 if (cur == end) {
1370 if (cur->type == XML_TEXT_NODE) {
1371 const xmlChar *content = cur->content;
1372 int len;
1373
1374 if (content == NULL) {
1375 tmp = xmlNewTextLen(NULL, 0);
1376 } else {
1377 len = index2;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001378 if ((cur == start) && (index1 > 1)) {
1379 content += (index1 - 1);
1380 len -= (index1 - 1);
1381 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001382 } else {
1383 len = index2;
1384 }
1385 tmp = xmlNewTextLen(content, len);
1386 }
1387 /* single sub text node selection */
1388 if (list == NULL)
1389 return(tmp);
1390 /* prune and return full set */
1391 if (last != NULL)
1392 xmlAddNextSibling(last, tmp);
1393 else
1394 xmlAddChild(parent, tmp);
1395 return(list);
1396 } else {
1397 tmp = xmlCopyNode(cur, 0);
1398 if (list == NULL)
1399 list = tmp;
1400 else {
1401 if (last != NULL)
1402 xmlAddNextSibling(last, tmp);
1403 else
1404 xmlAddChild(parent, tmp);
1405 }
1406 last = NULL;
1407 parent = tmp;
1408
1409 if (index2 > 1) {
1410 end = xmlXPtrGetNthChild(cur, index2 - 1);
1411 index2 = 0;
1412 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001413 if ((cur == start) && (index1 > 1)) {
1414 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1415 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001416 } else {
1417 cur = cur->children;
1418 }
1419 /*
1420 * Now gather the remaining nodes from cur to end
1421 */
1422 continue; /* while */
1423 }
1424 } else if ((cur == start) &&
1425 (list == NULL) /* looks superfluous but ... */ ) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001426 if ((cur->type == XML_TEXT_NODE) ||
1427 (cur->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001428 const xmlChar *content = cur->content;
1429
1430 if (content == NULL) {
1431 tmp = xmlNewTextLen(NULL, 0);
1432 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001433 if (index1 > 1) {
1434 content += (index1 - 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001435 }
1436 tmp = xmlNewText(content);
1437 }
1438 last = list = tmp;
1439 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001440 if ((cur == start) && (index1 > 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001441 tmp = xmlCopyNode(cur, 0);
1442 list = tmp;
1443 parent = tmp;
1444 last = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001445 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1446 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001447 /*
1448 * Now gather the remaining nodes from cur to end
1449 */
1450 continue; /* while */
1451 }
1452 tmp = xmlCopyNode(cur, 1);
1453 list = tmp;
1454 parent = NULL;
1455 last = tmp;
1456 }
1457 } else {
1458 tmp = NULL;
1459 switch (cur->type) {
1460 case XML_DTD_NODE:
1461 case XML_ELEMENT_DECL:
1462 case XML_ATTRIBUTE_DECL:
1463 case XML_ENTITY_NODE:
1464 /* Do not copy DTD informations */
1465 break;
1466 case XML_ENTITY_DECL:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001467 TODO /* handle crossing entities -> stack needed */
Owen Taylor3473f882001-02-23 17:55:21 +00001468 break;
1469 case XML_XINCLUDE_START:
1470 case XML_XINCLUDE_END:
1471 /* don't consider it part of the tree content */
1472 break;
1473 case XML_ATTRIBUTE_NODE:
1474 /* Humm, should not happen ! */
1475 STRANGE
1476 break;
1477 default:
1478 tmp = xmlCopyNode(cur, 1);
1479 break;
1480 }
1481 if (tmp != NULL) {
1482 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1483 STRANGE
1484 return(NULL);
1485 }
1486 if (last != NULL)
1487 xmlAddNextSibling(last, tmp);
1488 else {
1489 xmlAddChild(parent, tmp);
1490 last = tmp;
1491 }
1492 }
1493 }
1494 /*
1495 * Skip to next node in document order
1496 */
1497 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1498 STRANGE
1499 return(NULL);
1500 }
1501 cur = xmlXPtrAdvanceNode(cur);
1502 }
1503 return(list);
1504}
1505
1506/**
1507 * xmlXPtrBuildNodeList:
1508 * @obj: the XPointer result from the evaluation.
1509 *
1510 * Build a node list tree copy of the XPointer result.
Daniel Veillard39196eb2001-06-19 18:09:42 +00001511 * This will drop Attributes and Namespace declarations.
Owen Taylor3473f882001-02-23 17:55:21 +00001512 *
1513 * Returns an xmlNodePtr list or NULL.
1514 * the caller has to free the node tree.
1515 */
1516xmlNodePtr
1517xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1518 xmlNodePtr list = NULL, last = NULL;
1519 int i;
1520
1521 if (obj == NULL)
1522 return(NULL);
1523 switch (obj->type) {
1524 case XPATH_NODESET: {
1525 xmlNodeSetPtr set = obj->nodesetval;
1526 if (set == NULL)
1527 return(NULL);
1528 for (i = 0;i < set->nodeNr;i++) {
Daniel Veillard39196eb2001-06-19 18:09:42 +00001529 if (set->nodeTab[i] == NULL)
1530 continue;
1531 switch (set->nodeTab[i]->type) {
1532 case XML_TEXT_NODE:
1533 case XML_CDATA_SECTION_NODE:
1534 case XML_ELEMENT_NODE:
1535 case XML_ENTITY_REF_NODE:
1536 case XML_ENTITY_NODE:
1537 case XML_PI_NODE:
1538 case XML_COMMENT_NODE:
1539 case XML_DOCUMENT_NODE:
1540 case XML_HTML_DOCUMENT_NODE:
1541#ifdef LIBXML_DOCB_ENABLED
1542 case XML_DOCB_DOCUMENT_NODE:
1543#endif
1544 case XML_XINCLUDE_START:
1545 case XML_XINCLUDE_END:
1546 break;
1547 case XML_ATTRIBUTE_NODE:
1548 case XML_NAMESPACE_DECL:
1549 case XML_DOCUMENT_TYPE_NODE:
1550 case XML_DOCUMENT_FRAG_NODE:
1551 case XML_NOTATION_NODE:
1552 case XML_DTD_NODE:
1553 case XML_ELEMENT_DECL:
1554 case XML_ATTRIBUTE_DECL:
1555 case XML_ENTITY_DECL:
1556 continue; /* for */
1557 }
Owen Taylor3473f882001-02-23 17:55:21 +00001558 if (last == NULL)
1559 list = last = xmlCopyNode(set->nodeTab[i], 1);
1560 else {
1561 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1562 if (last->next != NULL)
1563 last = last->next;
1564 }
1565 }
1566 break;
1567 }
1568 case XPATH_LOCATIONSET: {
1569 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1570 if (set == NULL)
1571 return(NULL);
1572 for (i = 0;i < set->locNr;i++) {
1573 if (last == NULL)
1574 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1575 else
1576 xmlAddNextSibling(last,
1577 xmlXPtrBuildNodeList(set->locTab[i]));
1578 if (last != NULL) {
1579 while (last->next != NULL)
1580 last = last->next;
1581 }
1582 }
1583 break;
1584 }
1585 case XPATH_RANGE:
1586 return(xmlXPtrBuildRangeNodeList(obj));
1587 case XPATH_POINT:
1588 return(xmlCopyNode(obj->user, 0));
1589 default:
1590 break;
1591 }
1592 return(list);
1593}
1594
1595/************************************************************************
1596 * *
1597 * XPointer functions *
1598 * *
1599 ************************************************************************/
1600
1601/**
1602 * xmlXPtrNbLocChildren:
1603 * @node: an xmlNodePtr
1604 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001605 * Count the number of location children of @node or the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001606 * string value in case of text/PI/Comments nodes
1607 *
1608 * Returns the number of location children
1609 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001610static int
Owen Taylor3473f882001-02-23 17:55:21 +00001611xmlXPtrNbLocChildren(xmlNodePtr node) {
1612 int ret = 0;
1613 if (node == NULL)
1614 return(-1);
1615 switch (node->type) {
1616 case XML_HTML_DOCUMENT_NODE:
1617 case XML_DOCUMENT_NODE:
1618 case XML_ELEMENT_NODE:
1619 node = node->children;
1620 while (node != NULL) {
1621 if (node->type == XML_ELEMENT_NODE)
1622 ret++;
1623 node = node->next;
1624 }
1625 break;
1626 case XML_ATTRIBUTE_NODE:
1627 return(-1);
1628
1629 case XML_PI_NODE:
1630 case XML_COMMENT_NODE:
1631 case XML_TEXT_NODE:
1632 case XML_CDATA_SECTION_NODE:
1633 case XML_ENTITY_REF_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001634 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001635 break;
1636 default:
1637 return(-1);
1638 }
1639 return(ret);
1640}
1641
1642/**
1643 * xmlXPtrHereFunction:
1644 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001645 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001646 *
1647 * Function implementing here() operation
1648 * as described in 5.4.3
1649 */
1650void
1651xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001652 CHECK_ARITY(0);
1653
Owen Taylor3473f882001-02-23 17:55:21 +00001654 if (ctxt->context->here == NULL)
1655 XP_ERROR(XPTR_SYNTAX_ERROR);
1656
1657 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1658}
1659
1660/**
1661 * xmlXPtrOriginFunction:
1662 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001663 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001664 *
1665 * Function implementing origin() operation
1666 * as described in 5.4.3
1667 */
1668void
1669xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001670 CHECK_ARITY(0);
1671
Owen Taylor3473f882001-02-23 17:55:21 +00001672 if (ctxt->context->origin == NULL)
1673 XP_ERROR(XPTR_SYNTAX_ERROR);
1674
1675 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1676}
1677
1678/**
1679 * xmlXPtrStartPointFunction:
1680 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001681 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001682 *
1683 * Function implementing start-point() operation
1684 * as described in 5.4.3
1685 * ----------------
1686 * location-set start-point(location-set)
1687 *
1688 * For each location x in the argument location-set, start-point adds a
1689 * location of type point to the result location-set. That point represents
1690 * the start point of location x and is determined by the following rules:
1691 *
1692 * - If x is of type point, the start point is x.
1693 * - If x is of type range, the start point is the start point of x.
1694 * - If x is of type root, element, text, comment, or processing instruction,
1695 * - the container node of the start point is x and the index is 0.
1696 * - If x is of type attribute or namespace, the function must signal a
1697 * syntax error.
1698 * ----------------
1699 *
1700 */
1701void
1702xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1703 xmlXPathObjectPtr tmp, obj, point;
1704 xmlLocationSetPtr newset = NULL;
1705 xmlLocationSetPtr oldset = NULL;
1706
1707 CHECK_ARITY(1);
1708 if ((ctxt->value == NULL) ||
1709 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1710 (ctxt->value->type != XPATH_NODESET)))
1711 XP_ERROR(XPATH_INVALID_TYPE)
1712
1713 obj = valuePop(ctxt);
1714 if (obj->type == XPATH_NODESET) {
1715 /*
1716 * First convert to a location set
1717 */
1718 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1719 xmlXPathFreeObject(obj);
1720 obj = tmp;
1721 }
1722
1723 newset = xmlXPtrLocationSetCreate(NULL);
1724 if (newset == NULL) {
1725 xmlXPathFreeObject(obj);
1726 XP_ERROR(XPATH_MEMORY_ERROR);
1727 }
1728 oldset = (xmlLocationSetPtr) obj->user;
1729 if (oldset != NULL) {
1730 int i;
1731
1732 for (i = 0; i < oldset->locNr; i++) {
1733 tmp = oldset->locTab[i];
1734 if (tmp == NULL)
1735 continue;
1736 point = NULL;
1737 switch (tmp->type) {
1738 case XPATH_POINT:
1739 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1740 break;
1741 case XPATH_RANGE: {
1742 xmlNodePtr node = tmp->user;
1743 if (node != NULL) {
1744 if (node->type == XML_ATTRIBUTE_NODE) {
1745 /* TODO: Namespace Nodes ??? */
1746 xmlXPathFreeObject(obj);
1747 xmlXPtrFreeLocationSet(newset);
1748 XP_ERROR(XPTR_SYNTAX_ERROR);
1749 }
1750 point = xmlXPtrNewPoint(node, tmp->index);
1751 }
1752 break;
1753 }
1754 default:
1755 /*** Should we raise an error ?
1756 xmlXPathFreeObject(obj);
1757 xmlXPathFreeObject(newset);
1758 XP_ERROR(XPATH_INVALID_TYPE)
1759 ***/
1760 break;
1761 }
1762 if (point != NULL)
1763 xmlXPtrLocationSetAdd(newset, point);
1764 }
1765 }
1766 xmlXPathFreeObject(obj);
1767 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1768}
1769
1770/**
1771 * xmlXPtrEndPointFunction:
1772 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001773 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001774 *
1775 * Function implementing end-point() operation
1776 * as described in 5.4.3
1777 * ----------------------------
1778 * location-set end-point(location-set)
1779 *
1780 * For each location x in the argument location-set, end-point adds a
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001781 * location of type point to the result location-set. That point represents
Owen Taylor3473f882001-02-23 17:55:21 +00001782 * the end point of location x and is determined by the following rules:
1783 *
1784 * - If x is of type point, the resulting point is x.
1785 * - If x is of type range, the resulting point is the end point of x.
1786 * - If x is of type root or element, the container node of the resulting
1787 * point is x and the index is the number of location children of x.
1788 * - If x is of type text, comment, or processing instruction, the container
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001789 * node of the resulting point is x and the index is the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001790 * string-value of x.
1791 * - If x is of type attribute or namespace, the function must signal a
1792 * syntax error.
1793 * ----------------------------
1794 */
1795void
1796xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1797 xmlXPathObjectPtr tmp, obj, point;
1798 xmlLocationSetPtr newset = NULL;
1799 xmlLocationSetPtr oldset = NULL;
1800
1801 CHECK_ARITY(1);
1802 if ((ctxt->value == NULL) ||
1803 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1804 (ctxt->value->type != XPATH_NODESET)))
1805 XP_ERROR(XPATH_INVALID_TYPE)
1806
1807 obj = valuePop(ctxt);
1808 if (obj->type == XPATH_NODESET) {
1809 /*
1810 * First convert to a location set
1811 */
1812 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1813 xmlXPathFreeObject(obj);
1814 obj = tmp;
1815 }
1816
1817 newset = xmlXPtrLocationSetCreate(NULL);
1818 oldset = (xmlLocationSetPtr) obj->user;
1819 if (oldset != NULL) {
1820 int i;
1821
1822 for (i = 0; i < oldset->locNr; i++) {
1823 tmp = oldset->locTab[i];
1824 if (tmp == NULL)
1825 continue;
1826 point = NULL;
1827 switch (tmp->type) {
1828 case XPATH_POINT:
1829 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1830 break;
1831 case XPATH_RANGE: {
1832 xmlNodePtr node = tmp->user2;
1833 if (node != NULL) {
1834 if (node->type == XML_ATTRIBUTE_NODE) {
1835 /* TODO: Namespace Nodes ??? */
1836 xmlXPathFreeObject(obj);
1837 xmlXPtrFreeLocationSet(newset);
1838 XP_ERROR(XPTR_SYNTAX_ERROR);
1839 }
1840 point = xmlXPtrNewPoint(node, tmp->index2);
1841 } else if (tmp->user == NULL) {
1842 point = xmlXPtrNewPoint(node,
1843 xmlXPtrNbLocChildren(node));
1844 }
1845 break;
1846 }
1847 default:
1848 /*** Should we raise an error ?
1849 xmlXPathFreeObject(obj);
1850 xmlXPathFreeObject(newset);
1851 XP_ERROR(XPATH_INVALID_TYPE)
1852 ***/
1853 break;
1854 }
1855 if (point != NULL)
1856 xmlXPtrLocationSetAdd(newset, point);
1857 }
1858 }
1859 xmlXPathFreeObject(obj);
1860 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1861}
1862
1863
1864/**
1865 * xmlXPtrCoveringRange:
1866 * @ctxt: the XPointer Parser context
1867 * @loc: the location for which the covering range must be computed
1868 *
1869 * A covering range is a range that wholly encompasses a location
1870 * Section 5.3.3. Covering Ranges for All Location Types
1871 * http://www.w3.org/TR/xptr#N2267
1872 *
1873 * Returns a new location or NULL in case of error
1874 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001875static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001876xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1877 if (loc == NULL)
1878 return(NULL);
1879 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1880 (ctxt->context->doc == NULL))
1881 return(NULL);
1882 switch (loc->type) {
1883 case XPATH_POINT:
1884 return(xmlXPtrNewRange(loc->user, loc->index,
1885 loc->user, loc->index));
1886 case XPATH_RANGE:
1887 if (loc->user2 != NULL) {
1888 return(xmlXPtrNewRange(loc->user, loc->index,
1889 loc->user2, loc->index2));
1890 } else {
1891 xmlNodePtr node = (xmlNodePtr) loc->user;
1892 if (node == (xmlNodePtr) ctxt->context->doc) {
1893 return(xmlXPtrNewRange(node, 0, node,
1894 xmlXPtrGetArity(node)));
1895 } else {
1896 switch (node->type) {
1897 case XML_ATTRIBUTE_NODE:
1898 /* !!! our model is slightly different than XPath */
1899 return(xmlXPtrNewRange(node, 0, node,
1900 xmlXPtrGetArity(node)));
1901 case XML_ELEMENT_NODE:
1902 case XML_TEXT_NODE:
1903 case XML_CDATA_SECTION_NODE:
1904 case XML_ENTITY_REF_NODE:
1905 case XML_PI_NODE:
1906 case XML_COMMENT_NODE:
1907 case XML_DOCUMENT_NODE:
1908 case XML_NOTATION_NODE:
1909 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001910 int indx = xmlXPtrGetIndex(node);
Owen Taylor3473f882001-02-23 17:55:21 +00001911
1912 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001913 return(xmlXPtrNewRange(node, indx - 1,
1914 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00001915 }
1916 default:
1917 return(NULL);
1918 }
1919 }
1920 }
1921 default:
1922 TODO /* missed one case ??? */
1923 }
1924 return(NULL);
1925}
1926
1927/**
1928 * xmlXPtrRangeFunction:
1929 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001930 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001931 *
1932 * Function implementing the range() function 5.4.3
1933 * location-set range(location-set )
1934 *
1935 * The range function returns ranges covering the locations in
1936 * the argument location-set. For each location x in the argument
1937 * location-set, a range location representing the covering range of
1938 * x is added to the result location-set.
1939 */
1940void
1941xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1942 int i;
1943 xmlXPathObjectPtr set;
1944 xmlLocationSetPtr oldset;
1945 xmlLocationSetPtr newset;
1946
1947 CHECK_ARITY(1);
1948 if ((ctxt->value == NULL) ||
1949 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1950 (ctxt->value->type != XPATH_NODESET)))
1951 XP_ERROR(XPATH_INVALID_TYPE)
1952
1953 set = valuePop(ctxt);
1954 if (set->type == XPATH_NODESET) {
1955 xmlXPathObjectPtr tmp;
1956
1957 /*
1958 * First convert to a location set
1959 */
1960 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1961 xmlXPathFreeObject(set);
1962 set = tmp;
1963 }
1964 oldset = (xmlLocationSetPtr) set->user;
1965
1966 /*
1967 * The loop is to compute the covering range for each item and add it
1968 */
1969 newset = xmlXPtrLocationSetCreate(NULL);
1970 for (i = 0;i < oldset->locNr;i++) {
1971 xmlXPtrLocationSetAdd(newset,
1972 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
1973 }
1974
1975 /*
1976 * Save the new value and cleanup
1977 */
1978 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1979 xmlXPathFreeObject(set);
1980}
1981
1982/**
1983 * xmlXPtrInsideRange:
1984 * @ctxt: the XPointer Parser context
1985 * @loc: the location for which the inside range must be computed
1986 *
1987 * A inside range is a range described in the range-inside() description
1988 *
1989 * Returns a new location or NULL in case of error
1990 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001991static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001992xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1993 if (loc == NULL)
1994 return(NULL);
1995 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1996 (ctxt->context->doc == NULL))
1997 return(NULL);
1998 switch (loc->type) {
1999 case XPATH_POINT: {
2000 xmlNodePtr node = (xmlNodePtr) loc->user;
2001 switch (node->type) {
2002 case XML_PI_NODE:
2003 case XML_COMMENT_NODE:
2004 case XML_TEXT_NODE:
2005 case XML_CDATA_SECTION_NODE: {
2006 if (node->content == NULL) {
2007 return(xmlXPtrNewRange(node, 0, node, 0));
2008 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002009 return(xmlXPtrNewRange(node, 0, node,
2010 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002011 }
2012 }
2013 case XML_ATTRIBUTE_NODE:
2014 case XML_ELEMENT_NODE:
2015 case XML_ENTITY_REF_NODE:
2016 case XML_DOCUMENT_NODE:
2017 case XML_NOTATION_NODE:
2018 case XML_HTML_DOCUMENT_NODE: {
2019 return(xmlXPtrNewRange(node, 0, node,
2020 xmlXPtrGetArity(node)));
2021 }
2022 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002023 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002024 }
2025 return(NULL);
2026 }
2027 case XPATH_RANGE: {
2028 xmlNodePtr node = (xmlNodePtr) loc->user;
2029 if (loc->user2 != NULL) {
2030 return(xmlXPtrNewRange(node, loc->index,
2031 loc->user2, loc->index2));
2032 } else {
2033 switch (node->type) {
2034 case XML_PI_NODE:
2035 case XML_COMMENT_NODE:
2036 case XML_TEXT_NODE:
2037 case XML_CDATA_SECTION_NODE: {
2038 if (node->content == NULL) {
2039 return(xmlXPtrNewRange(node, 0, node, 0));
2040 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002041 return(xmlXPtrNewRange(node, 0, node,
2042 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002043 }
2044 }
2045 case XML_ATTRIBUTE_NODE:
2046 case XML_ELEMENT_NODE:
2047 case XML_ENTITY_REF_NODE:
2048 case XML_DOCUMENT_NODE:
2049 case XML_NOTATION_NODE:
2050 case XML_HTML_DOCUMENT_NODE: {
2051 return(xmlXPtrNewRange(node, 0, node,
2052 xmlXPtrGetArity(node)));
2053 }
2054 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002055 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002056 }
2057 return(NULL);
2058 }
2059 }
2060 default:
2061 TODO /* missed one case ??? */
2062 }
2063 return(NULL);
2064}
2065
2066/**
2067 * xmlXPtrRangeInsideFunction:
2068 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002069 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002070 *
2071 * Function implementing the range-inside() function 5.4.3
2072 * location-set range-inside(location-set )
2073 *
2074 * The range-inside function returns ranges covering the contents of
2075 * the locations in the argument location-set. For each location x in
2076 * the argument location-set, a range location is added to the result
2077 * location-set. If x is a range location, then x is added to the
2078 * result location-set. If x is not a range location, then x is used
2079 * as the container location of the start and end points of the range
2080 * location to be added; the index of the start point of the range is
2081 * zero; if the end point is a character point then its index is the
2082 * length of the string-value of x, and otherwise is the number of
2083 * location children of x.
2084 *
2085 */
2086void
2087xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2088 int i;
2089 xmlXPathObjectPtr set;
2090 xmlLocationSetPtr oldset;
2091 xmlLocationSetPtr newset;
2092
2093 CHECK_ARITY(1);
2094 if ((ctxt->value == NULL) ||
2095 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2096 (ctxt->value->type != XPATH_NODESET)))
2097 XP_ERROR(XPATH_INVALID_TYPE)
2098
2099 set = valuePop(ctxt);
2100 if (set->type == XPATH_NODESET) {
2101 xmlXPathObjectPtr tmp;
2102
2103 /*
2104 * First convert to a location set
2105 */
2106 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2107 xmlXPathFreeObject(set);
2108 set = tmp;
2109 }
2110 oldset = (xmlLocationSetPtr) set->user;
2111
2112 /*
2113 * The loop is to compute the covering range for each item and add it
2114 */
2115 newset = xmlXPtrLocationSetCreate(NULL);
2116 for (i = 0;i < oldset->locNr;i++) {
2117 xmlXPtrLocationSetAdd(newset,
2118 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2119 }
2120
2121 /*
2122 * Save the new value and cleanup
2123 */
2124 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2125 xmlXPathFreeObject(set);
2126}
2127
2128/**
2129 * xmlXPtrRangeToFunction:
2130 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002131 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002132 *
2133 * Implement the range-to() XPointer function
2134 */
2135void
2136xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2137 xmlXPathObjectPtr range;
2138 const xmlChar *cur;
2139 xmlXPathObjectPtr res, obj;
2140 xmlXPathObjectPtr tmp;
2141 xmlLocationSetPtr newset = NULL;
2142 xmlNodeSetPtr oldset;
2143 int i;
2144
2145 CHECK_ARITY(1);
2146 /*
2147 * Save the expression pointer since we will have to evaluate
2148 * it multiple times. Initialize the new set.
2149 */
2150 CHECK_TYPE(XPATH_NODESET);
2151 obj = valuePop(ctxt);
2152 oldset = obj->nodesetval;
2153 ctxt->context->node = NULL;
2154
2155 cur = ctxt->cur;
2156 newset = xmlXPtrLocationSetCreate(NULL);
2157
2158 for (i = 0; i < oldset->nodeNr; i++) {
2159 ctxt->cur = cur;
2160
2161 /*
2162 * Run the evaluation with a node list made of a single item
2163 * in the nodeset.
2164 */
2165 ctxt->context->node = oldset->nodeTab[i];
2166 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2167 valuePush(ctxt, tmp);
2168
2169 xmlXPathEvalExpr(ctxt);
2170 CHECK_ERROR;
2171
2172 /*
2173 * The result of the evaluation need to be tested to
2174 * decided whether the filter succeeded or not
2175 */
2176 res = valuePop(ctxt);
2177 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2178 if (range != NULL) {
2179 xmlXPtrLocationSetAdd(newset, range);
2180 }
2181
2182 /*
2183 * Cleanup
2184 */
2185 if (res != NULL)
2186 xmlXPathFreeObject(res);
2187 if (ctxt->value == tmp) {
2188 res = valuePop(ctxt);
2189 xmlXPathFreeObject(res);
2190 }
2191
2192 ctxt->context->node = NULL;
2193 }
2194
2195 /*
2196 * The result is used as the new evaluation set.
2197 */
2198 xmlXPathFreeObject(obj);
2199 ctxt->context->node = NULL;
2200 ctxt->context->contextSize = -1;
2201 ctxt->context->proximityPosition = -1;
2202 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2203}
2204
2205/**
2206 * xmlXPtrAdvanceNode:
2207 * @cur: the node
2208 *
2209 * Advance to the next element or text node in document order
2210 * TODO: add a stack for entering/exiting entities
2211 *
2212 * Returns -1 in case of failure, 0 otherwise
2213 */
2214xmlNodePtr
2215xmlXPtrAdvanceNode(xmlNodePtr cur) {
2216next:
2217 if (cur == NULL)
2218 return(NULL);
2219 if (cur->children != NULL) {
2220 cur = cur->children ;
2221 goto found;
2222 }
2223 if (cur->next != NULL) {
2224 cur = cur->next;
2225 goto found;
2226 }
2227 do {
2228 cur = cur->parent;
2229 if (cur == NULL) return(NULL);
2230 if (cur->next != NULL) {
2231 cur = cur->next;
2232 goto found;
2233 }
2234 } while (cur != NULL);
2235
2236found:
2237 if ((cur->type != XML_ELEMENT_NODE) &&
2238 (cur->type != XML_TEXT_NODE) &&
2239 (cur->type != XML_DOCUMENT_NODE) &&
2240 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2241 (cur->type != XML_CDATA_SECTION_NODE))
2242 goto next;
2243 if (cur->type == XML_ENTITY_REF_NODE) {
2244 TODO
2245 }
2246 return(cur);
2247}
2248
2249/**
2250 * xmlXPtrAdvanceChar:
2251 * @node: the node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002252 * @indx: the indx
Owen Taylor3473f882001-02-23 17:55:21 +00002253 * @bytes: the number of bytes
2254 *
2255 * Advance a point of the associated number of bytes (not UTF8 chars)
2256 *
2257 * Returns -1 in case of failure, 0 otherwise
2258 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002259static int
2260xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Owen Taylor3473f882001-02-23 17:55:21 +00002261 xmlNodePtr cur;
2262 int pos;
2263 int len;
2264
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002265 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002266 return(-1);
2267 cur = *node;
2268 if (cur == NULL)
2269 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002270 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002271
2272 while (bytes >= 0) {
2273 /*
2274 * First position to the beginning of the first text node
2275 * corresponding to this point
2276 */
2277 while ((cur != NULL) &&
2278 ((cur->type == XML_ELEMENT_NODE) ||
2279 (cur->type == XML_DOCUMENT_NODE) ||
2280 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2281 if (pos > 0) {
2282 cur = xmlXPtrGetNthChild(cur, pos);
2283 pos = 0;
2284 } else {
2285 cur = xmlXPtrAdvanceNode(cur);
2286 pos = 0;
2287 }
2288 }
2289
2290 if (cur == NULL) {
2291 *node = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002292 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002293 return(-1);
2294 }
2295
2296 /*
2297 * if there is no move needed return the current value.
2298 */
2299 if (pos == 0) pos = 1;
2300 if (bytes == 0) {
2301 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002302 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002303 return(0);
2304 }
2305 /*
2306 * We should have a text (or cdata) node ...
2307 */
2308 len = 0;
Daniel Veillard7db37732001-07-12 01:20:08 +00002309 if ((cur->type != XML_ELEMENT_NODE) &&
2310 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002311 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002312 }
2313 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002314 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002315 STRANGE
2316 pos = len;
2317 }
2318 if (pos + bytes >= len) {
2319 bytes -= (len - pos);
2320 cur = xmlXPtrAdvanceNode(cur);
2321 cur = 0;
2322 } else if (pos + bytes < len) {
2323 pos += bytes;
2324 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002325 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002326 return(0);
2327 }
2328 }
2329 return(-1);
2330}
2331
2332/**
2333 * xmlXPtrMatchString:
2334 * @string: the string to search
2335 * @start: the start textnode
2336 * @startindex: the start index
2337 * @end: the end textnode IN/OUT
2338 * @endindex: the end index IN/OUT
2339 *
2340 * Check whether the document contains @string at the position
2341 * (@start, @startindex) and limited by the (@end, @endindex) point
2342 *
2343 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2344 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002345 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002346 * of the range
2347 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002348static int
Owen Taylor3473f882001-02-23 17:55:21 +00002349xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2350 xmlNodePtr *end, int *endindex) {
2351 xmlNodePtr cur;
2352 int pos; /* 0 based */
2353 int len; /* in bytes */
2354 int stringlen; /* in bytes */
2355 int match;
2356
2357 if (string == NULL)
2358 return(-1);
2359 if (start == NULL)
2360 return(-1);
2361 if ((end == NULL) || (endindex == NULL))
2362 return(-1);
2363 cur = start;
2364 if (cur == NULL)
2365 return(-1);
2366 pos = startindex - 1;
2367 stringlen = xmlStrlen(string);
2368
2369 while (stringlen > 0) {
2370 if ((cur == *end) && (pos + stringlen > *endindex))
2371 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002372
2373 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002374 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002375 if (len >= pos + stringlen) {
Owen Taylor3473f882001-02-23 17:55:21 +00002376 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
Owen Taylor3473f882001-02-23 17:55:21 +00002377 if (match) {
2378#ifdef DEBUG_RANGES
2379 xmlGenericError(xmlGenericErrorContext,
2380 "found range %d bytes at index %d of ->",
2381 stringlen, pos + 1);
2382 xmlDebugDumpString(stdout, cur->content);
2383 xmlGenericError(xmlGenericErrorContext, "\n");
2384#endif
2385 *end = cur;
2386 *endindex = pos + stringlen;
2387 return(1);
2388 } else {
2389 return(0);
2390 }
2391 } else {
2392 int sub = len - pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002393 match = (!xmlStrncmp(&cur->content[pos], string, sub));
Owen Taylor3473f882001-02-23 17:55:21 +00002394 if (match) {
2395#ifdef DEBUG_RANGES
2396 xmlGenericError(xmlGenericErrorContext,
2397 "found subrange %d bytes at index %d of ->",
2398 sub, pos + 1);
2399 xmlDebugDumpString(stdout, cur->content);
2400 xmlGenericError(xmlGenericErrorContext, "\n");
2401#endif
2402 string = &string[sub];
2403 stringlen -= sub;
2404 } else {
2405 return(0);
2406 }
2407 }
2408 }
2409 cur = xmlXPtrAdvanceNode(cur);
2410 if (cur == NULL)
2411 return(0);
2412 pos = 0;
2413 }
2414 return(1);
2415}
2416
2417/**
2418 * xmlXPtrSearchString:
2419 * @string: the string to search
2420 * @start: the start textnode IN/OUT
2421 * @startindex: the start index IN/OUT
2422 * @end: the end textnode
2423 * @endindex: the end index
2424 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002425 * Search the next occurrence of @string within the document content
Owen Taylor3473f882001-02-23 17:55:21 +00002426 * until the (@end, @endindex) point is reached
2427 *
2428 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2429 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002430 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002431 * of the range
2432 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002433static int
Owen Taylor3473f882001-02-23 17:55:21 +00002434xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2435 xmlNodePtr *end, int *endindex) {
2436 xmlNodePtr cur;
2437 const xmlChar *str;
2438 int pos; /* 0 based */
2439 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002440 xmlChar first;
2441
2442 if (string == NULL)
2443 return(-1);
2444 if ((start == NULL) || (startindex == NULL))
2445 return(-1);
2446 if ((end == NULL) || (endindex == NULL))
2447 return(-1);
2448 cur = *start;
2449 if (cur == NULL)
2450 return(-1);
2451 pos = *startindex - 1;
2452 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002453
2454 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002455 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002456 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002457 while (pos <= len) {
2458 if (first != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00002459 str = xmlStrchr(&cur->content[pos], first);
Owen Taylor3473f882001-02-23 17:55:21 +00002460 if (str != NULL) {
2461 pos = (str - (xmlChar *)(cur->content));
2462#ifdef DEBUG_RANGES
2463 xmlGenericError(xmlGenericErrorContext,
2464 "found '%c' at index %d of ->",
2465 first, pos + 1);
2466 xmlDebugDumpString(stdout, cur->content);
2467 xmlGenericError(xmlGenericErrorContext, "\n");
2468#endif
2469 if (xmlXPtrMatchString(string, cur, pos + 1,
2470 end, endindex)) {
2471 *start = cur;
2472 *startindex = pos + 1;
2473 return(1);
2474 }
2475 pos++;
2476 } else {
2477 pos = len + 1;
2478 }
2479 } else {
2480 /*
2481 * An empty string is considered to match before each
2482 * character of the string-value and after the final
2483 * character.
2484 */
2485#ifdef DEBUG_RANGES
2486 xmlGenericError(xmlGenericErrorContext,
2487 "found '' at index %d of ->",
2488 pos + 1);
2489 xmlDebugDumpString(stdout, cur->content);
2490 xmlGenericError(xmlGenericErrorContext, "\n");
2491#endif
2492 *start = cur;
2493 *startindex = pos + 1;
2494 *end = cur;
2495 *endindex = pos + 1;
2496 return(1);
2497 }
2498 }
2499 }
2500 if ((cur == *end) && (pos >= *endindex))
2501 return(0);
2502 cur = xmlXPtrAdvanceNode(cur);
2503 if (cur == NULL)
2504 return(0);
2505 pos = 1;
2506 }
2507 return(0);
2508}
2509
2510/**
2511 * xmlXPtrGetLastChar:
2512 * @node: the node
2513 * @index: the index
2514 *
2515 * Computes the point coordinates of the last char of this point
2516 *
2517 * Returns -1 in case of failure, 0 otherwise
2518 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002519static int
2520xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002521 xmlNodePtr cur;
2522 int pos, len = 0;
2523
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002524 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002525 return(-1);
2526 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002527 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002528
2529 if (cur == NULL)
2530 return(-1);
2531
2532 if ((cur->type == XML_ELEMENT_NODE) ||
2533 (cur->type == XML_DOCUMENT_NODE) ||
2534 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2535 if (pos > 0) {
2536 cur = xmlXPtrGetNthChild(cur, pos);
2537 pos = 0;
2538 }
2539 }
2540 while (cur != NULL) {
2541 if (cur->last != NULL)
2542 cur = cur->last;
Daniel Veillard7db37732001-07-12 01:20:08 +00002543 else if ((cur->type != XML_ELEMENT_NODE) &&
2544 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002545 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002546 break;
2547 } else {
2548 return(-1);
2549 }
2550 }
2551 if (cur == NULL)
2552 return(-1);
2553 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002554 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002555 return(0);
2556}
2557
2558/**
2559 * xmlXPtrGetStartPoint:
2560 * @obj: an range
2561 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002562 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002563 *
2564 * read the object and return the start point coordinates.
2565 *
2566 * Returns -1 in case of failure, 0 otherwise
2567 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002568static int
2569xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2570 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002571 return(-1);
2572
2573 switch (obj->type) {
2574 case XPATH_POINT:
2575 *node = obj->user;
2576 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002577 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002578 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002579 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002580 return(0);
2581 case XPATH_RANGE:
2582 *node = obj->user;
2583 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002584 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002585 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002586 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002587 return(0);
2588 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002589 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002590 }
2591 return(-1);
2592}
2593
2594/**
2595 * xmlXPtrGetEndPoint:
2596 * @obj: an range
2597 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002598 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002599 *
2600 * read the object and return the end point coordinates.
2601 *
2602 * Returns -1 in case of failure, 0 otherwise
2603 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002604static int
2605xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2606 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002607 return(-1);
2608
2609 switch (obj->type) {
2610 case XPATH_POINT:
2611 *node = obj->user;
2612 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002613 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002614 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002615 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002616 return(0);
2617 case XPATH_RANGE:
2618 *node = obj->user;
2619 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002620 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002621 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002622 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002623 return(0);
2624 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002625 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002626 }
2627 return(-1);
2628}
2629
2630/**
2631 * xmlXPtrStringRangeFunction:
2632 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002633 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002634 *
2635 * Function implementing the string-range() function
2636 * range as described in 5.4.2
2637 *
2638 * ------------------------------
2639 * [Definition: For each location in the location-set argument,
2640 * string-range returns a set of string ranges, a set of substrings in a
2641 * string. Specifically, the string-value of the location is searched for
2642 * substrings that match the string argument, and the resulting location-set
2643 * will contain a range location for each non-overlapping match.]
2644 * An empty string is considered to match before each character of the
2645 * string-value and after the final character. Whitespace in a string
2646 * is matched literally, with no normalization except that provided by
2647 * XML for line ends. The third argument gives the position of the first
2648 * character to be in the resulting range, relative to the start of the
2649 * match. The default value is 1, which makes the range start immediately
2650 * before the first character of the matched string. The fourth argument
2651 * gives the number of characters in the range; the default is that the
2652 * range extends to the end of the matched string.
2653 *
2654 * Element boundaries, as well as entire embedded nodes such as processing
2655 * instructions and comments, are ignored as defined in [XPath].
2656 *
2657 * If the string in the second argument is not found in the string-value
2658 * of the location, or if a value in the third or fourth argument indicates
2659 * a string that is beyond the beginning or end of the document, the
2660 * expression fails.
2661 *
2662 * The points of the range-locations in the returned location-set will
2663 * all be character points.
2664 * ------------------------------
2665 */
2666void
2667xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2668 int i, startindex, endindex, fendindex;
2669 xmlNodePtr start, end, fend;
2670 xmlXPathObjectPtr set;
2671 xmlLocationSetPtr oldset;
2672 xmlLocationSetPtr newset;
2673 xmlXPathObjectPtr string;
2674 xmlXPathObjectPtr position = NULL;
2675 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002676 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002677
2678 /*
2679 * Grab the arguments
2680 */
2681 if ((nargs < 2) || (nargs > 4))
2682 XP_ERROR(XPATH_INVALID_ARITY);
2683
2684 if (nargs >= 4) {
2685 CHECK_TYPE(XPATH_NUMBER);
2686 number = valuePop(ctxt);
2687 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002688 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002689 }
2690 if (nargs >= 3) {
2691 CHECK_TYPE(XPATH_NUMBER);
2692 position = valuePop(ctxt);
2693 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002694 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002695 }
2696 CHECK_TYPE(XPATH_STRING);
2697 string = valuePop(ctxt);
2698 if ((ctxt->value == NULL) ||
2699 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2700 (ctxt->value->type != XPATH_NODESET)))
2701 XP_ERROR(XPATH_INVALID_TYPE)
2702
2703 set = valuePop(ctxt);
2704 if (set->type == XPATH_NODESET) {
2705 xmlXPathObjectPtr tmp;
2706
2707 /*
2708 * First convert to a location set
2709 */
2710 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2711 xmlXPathFreeObject(set);
2712 set = tmp;
2713 }
2714 oldset = (xmlLocationSetPtr) set->user;
2715
2716 /*
2717 * The loop is to search for each element in the location set
2718 * the list of location set corresponding to that search
2719 */
2720 newset = xmlXPtrLocationSetCreate(NULL);
2721 for (i = 0;i < oldset->locNr;i++) {
2722#ifdef DEBUG_RANGES
2723 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2724#endif
2725
2726 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2727 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2728 xmlXPtrAdvanceChar(&start, &startindex, 0);
2729 xmlXPtrGetLastChar(&end, &endindex);
2730
2731#ifdef DEBUG_RANGES
2732 xmlGenericError(xmlGenericErrorContext,
2733 "from index %d of ->", startindex);
2734 xmlDebugDumpString(stdout, start->content);
2735 xmlGenericError(xmlGenericErrorContext, "\n");
2736 xmlGenericError(xmlGenericErrorContext,
2737 "to index %d of ->", endindex);
2738 xmlDebugDumpString(stdout, end->content);
2739 xmlGenericError(xmlGenericErrorContext, "\n");
2740#endif
2741 do {
2742 fend = end;
2743 fendindex = endindex;
2744 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2745 &fend, &fendindex);
2746 if (found == 1) {
2747 if (position == NULL) {
2748 xmlXPtrLocationSetAdd(newset,
2749 xmlXPtrNewRange(start, startindex, fend, fendindex));
2750 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2751 pos - 1) == 0) {
2752 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002753 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002754 xmlNodePtr rend;
2755 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002756 rindx = startindex - 1;
2757 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002758 num) == 0) {
2759 xmlXPtrLocationSetAdd(newset,
2760 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002761 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002762 }
2763 } else if ((number != NULL) && (num <= 0)) {
2764 xmlXPtrLocationSetAdd(newset,
2765 xmlXPtrNewRange(start, startindex,
2766 start, startindex));
2767 } else {
2768 xmlXPtrLocationSetAdd(newset,
2769 xmlXPtrNewRange(start, startindex,
2770 fend, fendindex));
2771 }
2772 }
2773 start = fend;
2774 startindex = fendindex;
2775 if (string->stringval[0] == 0)
2776 startindex++;
2777 }
2778 } while (found == 1);
2779 }
2780
2781 /*
2782 * Save the new value and cleanup
2783 */
2784 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2785 xmlXPathFreeObject(set);
2786 xmlXPathFreeObject(string);
2787 if (position) xmlXPathFreeObject(position);
2788 if (number) xmlXPathFreeObject(number);
2789}
2790
2791/**
2792 * xmlXPtrEvalRangePredicate:
2793 * @ctxt: the XPointer Parser context
2794 *
2795 * [8] Predicate ::= '[' PredicateExpr ']'
2796 * [9] PredicateExpr ::= Expr
2797 *
2798 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2799 * a Location Set instead of a node set
2800 */
2801void
2802xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2803 const xmlChar *cur;
2804 xmlXPathObjectPtr res;
2805 xmlXPathObjectPtr obj, tmp;
2806 xmlLocationSetPtr newset = NULL;
2807 xmlLocationSetPtr oldset;
2808 int i;
2809
2810 SKIP_BLANKS;
2811 if (CUR != '[') {
2812 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2813 }
2814 NEXT;
2815 SKIP_BLANKS;
2816
2817 /*
2818 * Extract the old set, and then evaluate the result of the
2819 * expression for all the element in the set. use it to grow
2820 * up a new set.
2821 */
2822 CHECK_TYPE(XPATH_LOCATIONSET);
2823 obj = valuePop(ctxt);
2824 oldset = obj->user;
2825 ctxt->context->node = NULL;
2826
2827 if ((oldset == NULL) || (oldset->locNr == 0)) {
2828 ctxt->context->contextSize = 0;
2829 ctxt->context->proximityPosition = 0;
2830 xmlXPathEvalExpr(ctxt);
2831 res = valuePop(ctxt);
2832 if (res != NULL)
2833 xmlXPathFreeObject(res);
2834 valuePush(ctxt, obj);
2835 CHECK_ERROR;
2836 } else {
2837 /*
2838 * Save the expression pointer since we will have to evaluate
2839 * it multiple times. Initialize the new set.
2840 */
2841 cur = ctxt->cur;
2842 newset = xmlXPtrLocationSetCreate(NULL);
2843
2844 for (i = 0; i < oldset->locNr; i++) {
2845 ctxt->cur = cur;
2846
2847 /*
2848 * Run the evaluation with a node list made of a single item
2849 * in the nodeset.
2850 */
2851 ctxt->context->node = oldset->locTab[i]->user;
2852 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2853 valuePush(ctxt, tmp);
2854 ctxt->context->contextSize = oldset->locNr;
2855 ctxt->context->proximityPosition = i + 1;
2856
2857 xmlXPathEvalExpr(ctxt);
2858 CHECK_ERROR;
2859
2860 /*
2861 * The result of the evaluation need to be tested to
2862 * decided whether the filter succeeded or not
2863 */
2864 res = valuePop(ctxt);
2865 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2866 xmlXPtrLocationSetAdd(newset,
2867 xmlXPathObjectCopy(oldset->locTab[i]));
2868 }
2869
2870 /*
2871 * Cleanup
2872 */
2873 if (res != NULL)
2874 xmlXPathFreeObject(res);
2875 if (ctxt->value == tmp) {
2876 res = valuePop(ctxt);
2877 xmlXPathFreeObject(res);
2878 }
2879
2880 ctxt->context->node = NULL;
2881 }
2882
2883 /*
2884 * The result is used as the new evaluation set.
2885 */
2886 xmlXPathFreeObject(obj);
2887 ctxt->context->node = NULL;
2888 ctxt->context->contextSize = -1;
2889 ctxt->context->proximityPosition = -1;
2890 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2891 }
2892 if (CUR != ']') {
2893 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2894 }
2895
2896 NEXT;
2897 SKIP_BLANKS;
2898}
2899
2900#else
2901#endif
2902