VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.30/xpointer.c@ 11632

Last change on this file since 11632 was 6076, checked in by vboxsync, 17 years ago

Merged dmik/s2 branch (r25959:26751) to the trunk.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette