VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.31/xpointer.c@ 52789

最後變更 在這個檔案從52789是 42884,由 vboxsync 提交於 12 年 前

libxml: patch from upstream

  • 屬性 svn:eol-style 設為 native
檔案大小: 75.3 KB
 
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 } else if (CUR == '(') {
1011 level++;
1012 } else if (CUR == '^') {
1013 if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
1014 NEXT;
1015 }
1016 }
1017 *cur++ = CUR;
1018 NEXT;
1019 }
1020 *cur = 0;
1021
1022 if ((level != 0) && (CUR == 0)) {
1023 xmlFree(buffer);
1024 XP_ERROR(XPTR_SYNTAX_ERROR);
1025 }
1026
1027 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
1028 const xmlChar *left = CUR_PTR;
1029
1030 CUR_PTR = buffer;
1031 /*
1032 * To evaluate an xpointer scheme element (4.3) we need:
1033 * context initialized to the root
1034 * context position initalized to 1
1035 * context size initialized to 1
1036 */
1037 ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
1038 ctxt->context->proximityPosition = 1;
1039 ctxt->context->contextSize = 1;
1040 xmlXPathEvalExpr(ctxt);
1041 CUR_PTR=left;
1042 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
1043 const xmlChar *left = CUR_PTR;
1044 xmlChar *name2;
1045
1046 CUR_PTR = buffer;
1047 if (buffer[0] == '/') {
1048 xmlXPathRoot(ctxt);
1049 xmlXPtrEvalChildSeq(ctxt, NULL);
1050 } else {
1051 name2 = xmlXPathParseName(ctxt);
1052 if (name2 == NULL) {
1053 CUR_PTR = left;
1054 xmlFree(buffer);
1055 XP_ERROR(XPATH_EXPR_ERROR);
1056 }
1057 xmlXPtrEvalChildSeq(ctxt, name2);
1058 }
1059 CUR_PTR = left;
1060#ifdef XPTR_XMLNS_SCHEME
1061 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1062 const xmlChar *left = CUR_PTR;
1063 xmlChar *prefix;
1064 xmlChar *URI;
1065 xmlURIPtr value;
1066
1067 CUR_PTR = buffer;
1068 prefix = xmlXPathParseNCName(ctxt);
1069 if (prefix == NULL) {
1070 xmlFree(buffer);
1071 xmlFree(name);
1072 XP_ERROR(XPTR_SYNTAX_ERROR);
1073 }
1074 SKIP_BLANKS;
1075 if (CUR != '=') {
1076 xmlFree(prefix);
1077 xmlFree(buffer);
1078 xmlFree(name);
1079 XP_ERROR(XPTR_SYNTAX_ERROR);
1080 }
1081 NEXT;
1082 SKIP_BLANKS;
1083 /* @@ check escaping in the XPointer WD */
1084
1085 value = xmlParseURI((const char *)ctxt->cur);
1086 if (value == NULL) {
1087 xmlFree(prefix);
1088 xmlFree(buffer);
1089 xmlFree(name);
1090 XP_ERROR(XPTR_SYNTAX_ERROR);
1091 }
1092 URI = xmlSaveUri(value);
1093 xmlFreeURI(value);
1094 if (URI == NULL) {
1095 xmlFree(prefix);
1096 xmlFree(buffer);
1097 xmlFree(name);
1098 XP_ERROR(XPATH_MEMORY_ERROR);
1099 }
1100
1101 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1102 CUR_PTR = left;
1103 xmlFree(URI);
1104 xmlFree(prefix);
1105#endif /* XPTR_XMLNS_SCHEME */
1106 } else {
1107 xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
1108 "unsupported scheme '%s'\n", name);
1109 }
1110 xmlFree(buffer);
1111 xmlFree(name);
1112}
1113
1114/**
1115 * xmlXPtrEvalFullXPtr:
1116 * @ctxt: the XPointer Parser context
1117 * @name: the preparsed Scheme for the first XPtrPart
1118 *
1119 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1120 *
1121 * As the specs says:
1122 * -----------
1123 * When multiple XPtrParts are provided, they must be evaluated in
1124 * left-to-right order. If evaluation of one part fails, the nexti
1125 * is evaluated. The following conditions cause XPointer part failure:
1126 *
1127 * - An unknown scheme
1128 * - A scheme that does not locate any sub-resource present in the resource
1129 * - A scheme that is not applicable to the media type of the resource
1130 *
1131 * The XPointer application must consume a failed XPointer part and
1132 * attempt to evaluate the next one, if any. The result of the first
1133 * XPointer part whose evaluation succeeds is taken to be the fragment
1134 * located by the XPointer as a whole. If all the parts fail, the result
1135 * for the XPointer as a whole is a sub-resource error.
1136 * -----------
1137 *
1138 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1139 * expressions or other schemes.
1140 */
1141static void
1142xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1143 if (name == NULL)
1144 name = xmlXPathParseName(ctxt);
1145 if (name == NULL)
1146 XP_ERROR(XPATH_EXPR_ERROR);
1147 while (name != NULL) {
1148 xmlXPtrEvalXPtrPart(ctxt, name);
1149
1150 /* in case of syntax error, break here */
1151 if (ctxt->error != XPATH_EXPRESSION_OK)
1152 return;
1153
1154 /*
1155 * If the returned value is a non-empty nodeset
1156 * or location set, return here.
1157 */
1158 if (ctxt->value != NULL) {
1159 xmlXPathObjectPtr obj = ctxt->value;
1160
1161 switch (obj->type) {
1162 case XPATH_LOCATIONSET: {
1163 xmlLocationSetPtr loc = ctxt->value->user;
1164 if ((loc != NULL) && (loc->locNr > 0))
1165 return;
1166 break;
1167 }
1168 case XPATH_NODESET: {
1169 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1170 if ((loc != NULL) && (loc->nodeNr > 0))
1171 return;
1172 break;
1173 }
1174 default:
1175 break;
1176 }
1177
1178 /*
1179 * Evaluating to improper values is equivalent to
1180 * a sub-resource error, clean-up the stack
1181 */
1182 do {
1183 obj = valuePop(ctxt);
1184 if (obj != NULL) {
1185 xmlXPathFreeObject(obj);
1186 }
1187 } while (obj != NULL);
1188 }
1189
1190 /*
1191 * Is there another XPointer part.
1192 */
1193 SKIP_BLANKS;
1194 name = xmlXPathParseName(ctxt);
1195 }
1196}
1197
1198/**
1199 * xmlXPtrEvalChildSeq:
1200 * @ctxt: the XPointer Parser context
1201 * @name: a possible ID name of the child sequence
1202 *
1203 * ChildSeq ::= '/1' ('/' [0-9]*)*
1204 * | Name ('/' [0-9]*)+
1205 *
1206 * Parse and evaluate a Child Sequence. This routine also handle the
1207 * case of a Bare Name used to get a document ID.
1208 */
1209static void
1210xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1211 /*
1212 * XPointer don't allow by syntax to address in mutirooted trees
1213 * this might prove useful in some cases, warn about it.
1214 */
1215 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
1216 xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
1217 "warning: ChildSeq not starting by /1\n", NULL);
1218 }
1219
1220 if (name != NULL) {
1221 valuePush(ctxt, xmlXPathNewString(name));
1222 xmlFree(name);
1223 xmlXPathIdFunction(ctxt, 1);
1224 CHECK_ERROR;
1225 }
1226
1227 while (CUR == '/') {
1228 int child = 0;
1229 NEXT;
1230
1231 while ((CUR >= '0') && (CUR <= '9')) {
1232 child = child * 10 + (CUR - '0');
1233 NEXT;
1234 }
1235 xmlXPtrGetChildNo(ctxt, child);
1236 }
1237}
1238
1239
1240/**
1241 * xmlXPtrEvalXPointer:
1242 * @ctxt: the XPointer Parser context
1243 *
1244 * XPointer ::= Name
1245 * | ChildSeq
1246 * | FullXPtr
1247 *
1248 * Parse and evaluate an XPointer
1249 */
1250static void
1251xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
1252 if (ctxt->valueTab == NULL) {
1253 /* Allocate the value stack */
1254 ctxt->valueTab = (xmlXPathObjectPtr *)
1255 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1256 if (ctxt->valueTab == NULL) {
1257 xmlXPtrErrMemory("allocating evaluation context");
1258 return;
1259 }
1260 ctxt->valueNr = 0;
1261 ctxt->valueMax = 10;
1262 ctxt->value = NULL;
1263 ctxt->valueFrame = 0;
1264 }
1265 SKIP_BLANKS;
1266 if (CUR == '/') {
1267 xmlXPathRoot(ctxt);
1268 xmlXPtrEvalChildSeq(ctxt, NULL);
1269 } else {
1270 xmlChar *name;
1271
1272 name = xmlXPathParseName(ctxt);
1273 if (name == NULL)
1274 XP_ERROR(XPATH_EXPR_ERROR);
1275 if (CUR == '(') {
1276 xmlXPtrEvalFullXPtr(ctxt, name);
1277 /* Short evaluation */
1278 return;
1279 } else {
1280 /* this handle both Bare Names and Child Sequences */
1281 xmlXPtrEvalChildSeq(ctxt, name);
1282 }
1283 }
1284 SKIP_BLANKS;
1285 if (CUR != 0)
1286 XP_ERROR(XPATH_EXPR_ERROR);
1287}
1288
1289
1290/************************************************************************
1291 * *
1292 * General routines *
1293 * *
1294 ************************************************************************/
1295
1296void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1297void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1298void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1299void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1300void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1301void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1302void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1303
1304/**
1305 * xmlXPtrNewContext:
1306 * @doc: the XML document
1307 * @here: the node that directly contains the XPointer being evaluated or NULL
1308 * @origin: the element from which a user or program initiated traversal of
1309 * the link, or NULL.
1310 *
1311 * Create a new XPointer context
1312 *
1313 * Returns the xmlXPathContext just allocated.
1314 */
1315xmlXPathContextPtr
1316xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1317 xmlXPathContextPtr ret;
1318
1319 ret = xmlXPathNewContext(doc);
1320 if (ret == NULL)
1321 return(ret);
1322 ret->xptr = 1;
1323 ret->here = here;
1324 ret->origin = origin;
1325
1326 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1327 xmlXPtrRangeToFunction);
1328 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1329 xmlXPtrRangeFunction);
1330 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1331 xmlXPtrRangeInsideFunction);
1332 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1333 xmlXPtrStringRangeFunction);
1334 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1335 xmlXPtrStartPointFunction);
1336 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1337 xmlXPtrEndPointFunction);
1338 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1339 xmlXPtrHereFunction);
1340 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1341 xmlXPtrOriginFunction);
1342
1343 return(ret);
1344}
1345
1346/**
1347 * xmlXPtrEval:
1348 * @str: the XPointer expression
1349 * @ctx: the XPointer context
1350 *
1351 * Evaluate the XPath Location Path in the given context.
1352 *
1353 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
1354 * the caller has to free the object.
1355 */
1356xmlXPathObjectPtr
1357xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1358 xmlXPathParserContextPtr ctxt;
1359 xmlXPathObjectPtr res = NULL, tmp;
1360 xmlXPathObjectPtr init = NULL;
1361 int stack = 0;
1362
1363 xmlXPathInit();
1364
1365 if ((ctx == NULL) || (str == NULL))
1366 return(NULL);
1367
1368 ctxt = xmlXPathNewParserContext(str, ctx);
1369 ctxt->xptr = 1;
1370 xmlXPtrEvalXPointer(ctxt);
1371
1372 if ((ctxt->value != NULL) &&
1373 (ctxt->value->type != XPATH_NODESET) &&
1374 (ctxt->value->type != XPATH_LOCATIONSET)) {
1375 xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
1376 "xmlXPtrEval: evaluation failed to return a node set\n",
1377 NULL);
1378 } else {
1379 res = valuePop(ctxt);
1380 }
1381
1382 do {
1383 tmp = valuePop(ctxt);
1384 if (tmp != NULL) {
1385 if (tmp != init) {
1386 if (tmp->type == XPATH_NODESET) {
1387 /*
1388 * Evaluation may push a root nodeset which is unused
1389 */
1390 xmlNodeSetPtr set;
1391 set = tmp->nodesetval;
1392 if ((set->nodeNr != 1) ||
1393 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1394 stack++;
1395 } else
1396 stack++;
1397 }
1398 xmlXPathFreeObject(tmp);
1399 }
1400 } while (tmp != NULL);
1401 if (stack != 0) {
1402 xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
1403 "xmlXPtrEval: object(s) left on the eval stack\n",
1404 NULL);
1405 }
1406 if (ctxt->error != XPATH_EXPRESSION_OK) {
1407 xmlXPathFreeObject(res);
1408 res = NULL;
1409 }
1410
1411 xmlXPathFreeParserContext(ctxt);
1412 return(res);
1413}
1414
1415/**
1416 * xmlXPtrBuildRangeNodeList:
1417 * @range: a range object
1418 *
1419 * Build a node list tree copy of the range
1420 *
1421 * Returns an xmlNodePtr list or NULL.
1422 * the caller has to free the node tree.
1423 */
1424static xmlNodePtr
1425xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1426 /* pointers to generated nodes */
1427 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1428 /* pointers to traversal nodes */
1429 xmlNodePtr start, cur, end;
1430 int index1, index2;
1431
1432 if (range == NULL)
1433 return(NULL);
1434 if (range->type != XPATH_RANGE)
1435 return(NULL);
1436 start = (xmlNodePtr) range->user;
1437
1438 if (start == NULL)
1439 return(NULL);
1440 end = range->user2;
1441 if (end == NULL)
1442 return(xmlCopyNode(start, 1));
1443
1444 cur = start;
1445 index1 = range->index;
1446 index2 = range->index2;
1447 while (cur != NULL) {
1448 if (cur == end) {
1449 if (cur->type == XML_TEXT_NODE) {
1450 const xmlChar *content = cur->content;
1451 int len;
1452
1453 if (content == NULL) {
1454 tmp = xmlNewTextLen(NULL, 0);
1455 } else {
1456 len = index2;
1457 if ((cur == start) && (index1 > 1)) {
1458 content += (index1 - 1);
1459 len -= (index1 - 1);
1460 index1 = 0;
1461 } else {
1462 len = index2;
1463 }
1464 tmp = xmlNewTextLen(content, len);
1465 }
1466 /* single sub text node selection */
1467 if (list == NULL)
1468 return(tmp);
1469 /* prune and return full set */
1470 if (last != NULL)
1471 xmlAddNextSibling(last, tmp);
1472 else
1473 xmlAddChild(parent, tmp);
1474 return(list);
1475 } else {
1476 tmp = xmlCopyNode(cur, 0);
1477 if (list == NULL)
1478 list = tmp;
1479 else {
1480 if (last != NULL)
1481 xmlAddNextSibling(last, tmp);
1482 else
1483 xmlAddChild(parent, tmp);
1484 }
1485 last = NULL;
1486 parent = tmp;
1487
1488 if (index2 > 1) {
1489 end = xmlXPtrGetNthChild(cur, index2 - 1);
1490 index2 = 0;
1491 }
1492 if ((cur == start) && (index1 > 1)) {
1493 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1494 index1 = 0;
1495 } else {
1496 cur = cur->children;
1497 }
1498 /*
1499 * Now gather the remaining nodes from cur to end
1500 */
1501 continue; /* while */
1502 }
1503 } else if ((cur == start) &&
1504 (list == NULL) /* looks superfluous but ... */ ) {
1505 if ((cur->type == XML_TEXT_NODE) ||
1506 (cur->type == XML_CDATA_SECTION_NODE)) {
1507 const xmlChar *content = cur->content;
1508
1509 if (content == NULL) {
1510 tmp = xmlNewTextLen(NULL, 0);
1511 } else {
1512 if (index1 > 1) {
1513 content += (index1 - 1);
1514 }
1515 tmp = xmlNewText(content);
1516 }
1517 last = list = tmp;
1518 } else {
1519 if ((cur == start) && (index1 > 1)) {
1520 tmp = xmlCopyNode(cur, 0);
1521 list = tmp;
1522 parent = tmp;
1523 last = NULL;
1524 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1525 index1 = 0;
1526 /*
1527 * Now gather the remaining nodes from cur to end
1528 */
1529 continue; /* while */
1530 }
1531 tmp = xmlCopyNode(cur, 1);
1532 list = tmp;
1533 parent = NULL;
1534 last = tmp;
1535 }
1536 } else {
1537 tmp = NULL;
1538 switch (cur->type) {
1539 case XML_DTD_NODE:
1540 case XML_ELEMENT_DECL:
1541 case XML_ATTRIBUTE_DECL:
1542 case XML_ENTITY_NODE:
1543 /* Do not copy DTD informations */
1544 break;
1545 case XML_ENTITY_DECL:
1546 TODO /* handle crossing entities -> stack needed */
1547 break;
1548 case XML_XINCLUDE_START:
1549 case XML_XINCLUDE_END:
1550 /* don't consider it part of the tree content */
1551 break;
1552 case XML_ATTRIBUTE_NODE:
1553 /* Humm, should not happen ! */
1554 STRANGE
1555 break;
1556 default:
1557 tmp = xmlCopyNode(cur, 1);
1558 break;
1559 }
1560 if (tmp != NULL) {
1561 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1562 STRANGE
1563 return(NULL);
1564 }
1565 if (last != NULL)
1566 xmlAddNextSibling(last, tmp);
1567 else {
1568 xmlAddChild(parent, tmp);
1569 last = tmp;
1570 }
1571 }
1572 }
1573 /*
1574 * Skip to next node in document order
1575 */
1576 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1577 STRANGE
1578 return(NULL);
1579 }
1580 cur = xmlXPtrAdvanceNode(cur, NULL);
1581 }
1582 return(list);
1583}
1584
1585/**
1586 * xmlXPtrBuildNodeList:
1587 * @obj: the XPointer result from the evaluation.
1588 *
1589 * Build a node list tree copy of the XPointer result.
1590 * This will drop Attributes and Namespace declarations.
1591 *
1592 * Returns an xmlNodePtr list or NULL.
1593 * the caller has to free the node tree.
1594 */
1595xmlNodePtr
1596xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1597 xmlNodePtr list = NULL, last = NULL;
1598 int i;
1599
1600 if (obj == NULL)
1601 return(NULL);
1602 switch (obj->type) {
1603 case XPATH_NODESET: {
1604 xmlNodeSetPtr set = obj->nodesetval;
1605 if (set == NULL)
1606 return(NULL);
1607 for (i = 0;i < set->nodeNr;i++) {
1608 if (set->nodeTab[i] == NULL)
1609 continue;
1610 switch (set->nodeTab[i]->type) {
1611 case XML_TEXT_NODE:
1612 case XML_CDATA_SECTION_NODE:
1613 case XML_ELEMENT_NODE:
1614 case XML_ENTITY_REF_NODE:
1615 case XML_ENTITY_NODE:
1616 case XML_PI_NODE:
1617 case XML_COMMENT_NODE:
1618 case XML_DOCUMENT_NODE:
1619 case XML_HTML_DOCUMENT_NODE:
1620#ifdef LIBXML_DOCB_ENABLED
1621 case XML_DOCB_DOCUMENT_NODE:
1622#endif
1623 case XML_XINCLUDE_START:
1624 case XML_XINCLUDE_END:
1625 break;
1626 case XML_ATTRIBUTE_NODE:
1627 case XML_NAMESPACE_DECL:
1628 case XML_DOCUMENT_TYPE_NODE:
1629 case XML_DOCUMENT_FRAG_NODE:
1630 case XML_NOTATION_NODE:
1631 case XML_DTD_NODE:
1632 case XML_ELEMENT_DECL:
1633 case XML_ATTRIBUTE_DECL:
1634 case XML_ENTITY_DECL:
1635 continue; /* for */
1636 }
1637 if (last == NULL)
1638 list = last = xmlCopyNode(set->nodeTab[i], 1);
1639 else {
1640 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1641 if (last->next != NULL)
1642 last = last->next;
1643 }
1644 }
1645 break;
1646 }
1647 case XPATH_LOCATIONSET: {
1648 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1649 if (set == NULL)
1650 return(NULL);
1651 for (i = 0;i < set->locNr;i++) {
1652 if (last == NULL)
1653 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1654 else
1655 xmlAddNextSibling(last,
1656 xmlXPtrBuildNodeList(set->locTab[i]));
1657 if (last != NULL) {
1658 while (last->next != NULL)
1659 last = last->next;
1660 }
1661 }
1662 break;
1663 }
1664 case XPATH_RANGE:
1665 return(xmlXPtrBuildRangeNodeList(obj));
1666 case XPATH_POINT:
1667 return(xmlCopyNode(obj->user, 0));
1668 default:
1669 break;
1670 }
1671 return(list);
1672}
1673
1674/************************************************************************
1675 * *
1676 * XPointer functions *
1677 * *
1678 ************************************************************************/
1679
1680/**
1681 * xmlXPtrNbLocChildren:
1682 * @node: an xmlNodePtr
1683 *
1684 * Count the number of location children of @node or the length of the
1685 * string value in case of text/PI/Comments nodes
1686 *
1687 * Returns the number of location children
1688 */
1689static int
1690xmlXPtrNbLocChildren(xmlNodePtr node) {
1691 int ret = 0;
1692 if (node == NULL)
1693 return(-1);
1694 switch (node->type) {
1695 case XML_HTML_DOCUMENT_NODE:
1696 case XML_DOCUMENT_NODE:
1697 case XML_ELEMENT_NODE:
1698 node = node->children;
1699 while (node != NULL) {
1700 if (node->type == XML_ELEMENT_NODE)
1701 ret++;
1702 node = node->next;
1703 }
1704 break;
1705 case XML_ATTRIBUTE_NODE:
1706 return(-1);
1707
1708 case XML_PI_NODE:
1709 case XML_COMMENT_NODE:
1710 case XML_TEXT_NODE:
1711 case XML_CDATA_SECTION_NODE:
1712 case XML_ENTITY_REF_NODE:
1713 ret = xmlStrlen(node->content);
1714 break;
1715 default:
1716 return(-1);
1717 }
1718 return(ret);
1719}
1720
1721/**
1722 * xmlXPtrHereFunction:
1723 * @ctxt: the XPointer Parser context
1724 * @nargs: the number of args
1725 *
1726 * Function implementing here() operation
1727 * as described in 5.4.3
1728 */
1729void
1730xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1731 CHECK_ARITY(0);
1732
1733 if (ctxt->context->here == NULL)
1734 XP_ERROR(XPTR_SYNTAX_ERROR);
1735
1736 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1737}
1738
1739/**
1740 * xmlXPtrOriginFunction:
1741 * @ctxt: the XPointer Parser context
1742 * @nargs: the number of args
1743 *
1744 * Function implementing origin() operation
1745 * as described in 5.4.3
1746 */
1747void
1748xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1749 CHECK_ARITY(0);
1750
1751 if (ctxt->context->origin == NULL)
1752 XP_ERROR(XPTR_SYNTAX_ERROR);
1753
1754 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1755}
1756
1757/**
1758 * xmlXPtrStartPointFunction:
1759 * @ctxt: the XPointer Parser context
1760 * @nargs: the number of args
1761 *
1762 * Function implementing start-point() operation
1763 * as described in 5.4.3
1764 * ----------------
1765 * location-set start-point(location-set)
1766 *
1767 * For each location x in the argument location-set, start-point adds a
1768 * location of type point to the result location-set. That point represents
1769 * the start point of location x and is determined by the following rules:
1770 *
1771 * - If x is of type point, the start point is x.
1772 * - If x is of type range, the start point is the start point of x.
1773 * - If x is of type root, element, text, comment, or processing instruction,
1774 * - the container node of the start point is x and the index is 0.
1775 * - If x is of type attribute or namespace, the function must signal a
1776 * syntax error.
1777 * ----------------
1778 *
1779 */
1780void
1781xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1782 xmlXPathObjectPtr tmp, obj, point;
1783 xmlLocationSetPtr newset = NULL;
1784 xmlLocationSetPtr oldset = NULL;
1785
1786 CHECK_ARITY(1);
1787 if ((ctxt->value == NULL) ||
1788 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1789 (ctxt->value->type != XPATH_NODESET)))
1790 XP_ERROR(XPATH_INVALID_TYPE)
1791
1792 obj = valuePop(ctxt);
1793 if (obj->type == XPATH_NODESET) {
1794 /*
1795 * First convert to a location set
1796 */
1797 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1798 xmlXPathFreeObject(obj);
1799 obj = tmp;
1800 }
1801
1802 newset = xmlXPtrLocationSetCreate(NULL);
1803 if (newset == NULL) {
1804 xmlXPathFreeObject(obj);
1805 XP_ERROR(XPATH_MEMORY_ERROR);
1806 }
1807 oldset = (xmlLocationSetPtr) obj->user;
1808 if (oldset != NULL) {
1809 int i;
1810
1811 for (i = 0; i < oldset->locNr; i++) {
1812 tmp = oldset->locTab[i];
1813 if (tmp == NULL)
1814 continue;
1815 point = NULL;
1816 switch (tmp->type) {
1817 case XPATH_POINT:
1818 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1819 break;
1820 case XPATH_RANGE: {
1821 xmlNodePtr node = tmp->user;
1822 if (node != NULL) {
1823 if (node->type == XML_ATTRIBUTE_NODE) {
1824 /* TODO: Namespace Nodes ??? */
1825 xmlXPathFreeObject(obj);
1826 xmlXPtrFreeLocationSet(newset);
1827 XP_ERROR(XPTR_SYNTAX_ERROR);
1828 }
1829 point = xmlXPtrNewPoint(node, tmp->index);
1830 }
1831 break;
1832 }
1833 default:
1834 /*** Should we raise an error ?
1835 xmlXPathFreeObject(obj);
1836 xmlXPathFreeObject(newset);
1837 XP_ERROR(XPATH_INVALID_TYPE)
1838 ***/
1839 break;
1840 }
1841 if (point != NULL)
1842 xmlXPtrLocationSetAdd(newset, point);
1843 }
1844 }
1845 xmlXPathFreeObject(obj);
1846 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1847}
1848
1849/**
1850 * xmlXPtrEndPointFunction:
1851 * @ctxt: the XPointer Parser context
1852 * @nargs: the number of args
1853 *
1854 * Function implementing end-point() operation
1855 * as described in 5.4.3
1856 * ----------------------------
1857 * location-set end-point(location-set)
1858 *
1859 * For each location x in the argument location-set, end-point adds a
1860 * location of type point to the result location-set. That point represents
1861 * the end point of location x and is determined by the following rules:
1862 *
1863 * - If x is of type point, the resulting point is x.
1864 * - If x is of type range, the resulting point is the end point of x.
1865 * - If x is of type root or element, the container node of the resulting
1866 * point is x and the index is the number of location children of x.
1867 * - If x is of type text, comment, or processing instruction, the container
1868 * node of the resulting point is x and the index is the length of the
1869 * string-value of x.
1870 * - If x is of type attribute or namespace, the function must signal a
1871 * syntax error.
1872 * ----------------------------
1873 */
1874void
1875xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1876 xmlXPathObjectPtr tmp, obj, point;
1877 xmlLocationSetPtr newset = NULL;
1878 xmlLocationSetPtr oldset = NULL;
1879
1880 CHECK_ARITY(1);
1881 if ((ctxt->value == NULL) ||
1882 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1883 (ctxt->value->type != XPATH_NODESET)))
1884 XP_ERROR(XPATH_INVALID_TYPE)
1885
1886 obj = valuePop(ctxt);
1887 if (obj->type == XPATH_NODESET) {
1888 /*
1889 * First convert to a location set
1890 */
1891 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1892 xmlXPathFreeObject(obj);
1893 obj = tmp;
1894 }
1895
1896 newset = xmlXPtrLocationSetCreate(NULL);
1897 oldset = (xmlLocationSetPtr) obj->user;
1898 if (oldset != NULL) {
1899 int i;
1900
1901 for (i = 0; i < oldset->locNr; i++) {
1902 tmp = oldset->locTab[i];
1903 if (tmp == NULL)
1904 continue;
1905 point = NULL;
1906 switch (tmp->type) {
1907 case XPATH_POINT:
1908 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1909 break;
1910 case XPATH_RANGE: {
1911 xmlNodePtr node = tmp->user2;
1912 if (node != NULL) {
1913 if (node->type == XML_ATTRIBUTE_NODE) {
1914 /* TODO: Namespace Nodes ??? */
1915 xmlXPathFreeObject(obj);
1916 xmlXPtrFreeLocationSet(newset);
1917 XP_ERROR(XPTR_SYNTAX_ERROR);
1918 }
1919 point = xmlXPtrNewPoint(node, tmp->index2);
1920 } else if (tmp->user == NULL) {
1921 point = xmlXPtrNewPoint(node,
1922 xmlXPtrNbLocChildren(node));
1923 }
1924 break;
1925 }
1926 default:
1927 /*** Should we raise an error ?
1928 xmlXPathFreeObject(obj);
1929 xmlXPathFreeObject(newset);
1930 XP_ERROR(XPATH_INVALID_TYPE)
1931 ***/
1932 break;
1933 }
1934 if (point != NULL)
1935 xmlXPtrLocationSetAdd(newset, point);
1936 }
1937 }
1938 xmlXPathFreeObject(obj);
1939 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1940}
1941
1942
1943/**
1944 * xmlXPtrCoveringRange:
1945 * @ctxt: the XPointer Parser context
1946 * @loc: the location for which the covering range must be computed
1947 *
1948 * A covering range is a range that wholly encompasses a location
1949 * Section 5.3.3. Covering Ranges for All Location Types
1950 * http://www.w3.org/TR/xptr#N2267
1951 *
1952 * Returns a new location or NULL in case of error
1953 */
1954static xmlXPathObjectPtr
1955xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1956 if (loc == NULL)
1957 return(NULL);
1958 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1959 (ctxt->context->doc == NULL))
1960 return(NULL);
1961 switch (loc->type) {
1962 case XPATH_POINT:
1963 return(xmlXPtrNewRange(loc->user, loc->index,
1964 loc->user, loc->index));
1965 case XPATH_RANGE:
1966 if (loc->user2 != NULL) {
1967 return(xmlXPtrNewRange(loc->user, loc->index,
1968 loc->user2, loc->index2));
1969 } else {
1970 xmlNodePtr node = (xmlNodePtr) loc->user;
1971 if (node == (xmlNodePtr) ctxt->context->doc) {
1972 return(xmlXPtrNewRange(node, 0, node,
1973 xmlXPtrGetArity(node)));
1974 } else {
1975 switch (node->type) {
1976 case XML_ATTRIBUTE_NODE:
1977 /* !!! our model is slightly different than XPath */
1978 return(xmlXPtrNewRange(node, 0, node,
1979 xmlXPtrGetArity(node)));
1980 case XML_ELEMENT_NODE:
1981 case XML_TEXT_NODE:
1982 case XML_CDATA_SECTION_NODE:
1983 case XML_ENTITY_REF_NODE:
1984 case XML_PI_NODE:
1985 case XML_COMMENT_NODE:
1986 case XML_DOCUMENT_NODE:
1987 case XML_NOTATION_NODE:
1988 case XML_HTML_DOCUMENT_NODE: {
1989 int indx = xmlXPtrGetIndex(node);
1990
1991 node = node->parent;
1992 return(xmlXPtrNewRange(node, indx - 1,
1993 node, indx + 1));
1994 }
1995 default:
1996 return(NULL);
1997 }
1998 }
1999 }
2000 default:
2001 TODO /* missed one case ??? */
2002 }
2003 return(NULL);
2004}
2005
2006/**
2007 * xmlXPtrRangeFunction:
2008 * @ctxt: the XPointer Parser context
2009 * @nargs: the number of args
2010 *
2011 * Function implementing the range() function 5.4.3
2012 * location-set range(location-set )
2013 *
2014 * The range function returns ranges covering the locations in
2015 * the argument location-set. For each location x in the argument
2016 * location-set, a range location representing the covering range of
2017 * x is added to the result location-set.
2018 */
2019void
2020xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2021 int i;
2022 xmlXPathObjectPtr set;
2023 xmlLocationSetPtr oldset;
2024 xmlLocationSetPtr newset;
2025
2026 CHECK_ARITY(1);
2027 if ((ctxt->value == NULL) ||
2028 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2029 (ctxt->value->type != XPATH_NODESET)))
2030 XP_ERROR(XPATH_INVALID_TYPE)
2031
2032 set = valuePop(ctxt);
2033 if (set->type == XPATH_NODESET) {
2034 xmlXPathObjectPtr tmp;
2035
2036 /*
2037 * First convert to a location set
2038 */
2039 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2040 xmlXPathFreeObject(set);
2041 set = tmp;
2042 }
2043 oldset = (xmlLocationSetPtr) set->user;
2044
2045 /*
2046 * The loop is to compute the covering range for each item and add it
2047 */
2048 newset = xmlXPtrLocationSetCreate(NULL);
2049 for (i = 0;i < oldset->locNr;i++) {
2050 xmlXPtrLocationSetAdd(newset,
2051 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2052 }
2053
2054 /*
2055 * Save the new value and cleanup
2056 */
2057 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2058 xmlXPathFreeObject(set);
2059}
2060
2061/**
2062 * xmlXPtrInsideRange:
2063 * @ctxt: the XPointer Parser context
2064 * @loc: the location for which the inside range must be computed
2065 *
2066 * A inside range is a range described in the range-inside() description
2067 *
2068 * Returns a new location or NULL in case of error
2069 */
2070static xmlXPathObjectPtr
2071xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2072 if (loc == NULL)
2073 return(NULL);
2074 if ((ctxt == NULL) || (ctxt->context == NULL) ||
2075 (ctxt->context->doc == NULL))
2076 return(NULL);
2077 switch (loc->type) {
2078 case XPATH_POINT: {
2079 xmlNodePtr node = (xmlNodePtr) loc->user;
2080 switch (node->type) {
2081 case XML_PI_NODE:
2082 case XML_COMMENT_NODE:
2083 case XML_TEXT_NODE:
2084 case XML_CDATA_SECTION_NODE: {
2085 if (node->content == NULL) {
2086 return(xmlXPtrNewRange(node, 0, node, 0));
2087 } else {
2088 return(xmlXPtrNewRange(node, 0, node,
2089 xmlStrlen(node->content)));
2090 }
2091 }
2092 case XML_ATTRIBUTE_NODE:
2093 case XML_ELEMENT_NODE:
2094 case XML_ENTITY_REF_NODE:
2095 case XML_DOCUMENT_NODE:
2096 case XML_NOTATION_NODE:
2097 case XML_HTML_DOCUMENT_NODE: {
2098 return(xmlXPtrNewRange(node, 0, node,
2099 xmlXPtrGetArity(node)));
2100 }
2101 default:
2102 break;
2103 }
2104 return(NULL);
2105 }
2106 case XPATH_RANGE: {
2107 xmlNodePtr node = (xmlNodePtr) loc->user;
2108 if (loc->user2 != NULL) {
2109 return(xmlXPtrNewRange(node, loc->index,
2110 loc->user2, loc->index2));
2111 } else {
2112 switch (node->type) {
2113 case XML_PI_NODE:
2114 case XML_COMMENT_NODE:
2115 case XML_TEXT_NODE:
2116 case XML_CDATA_SECTION_NODE: {
2117 if (node->content == NULL) {
2118 return(xmlXPtrNewRange(node, 0, node, 0));
2119 } else {
2120 return(xmlXPtrNewRange(node, 0, node,
2121 xmlStrlen(node->content)));
2122 }
2123 }
2124 case XML_ATTRIBUTE_NODE:
2125 case XML_ELEMENT_NODE:
2126 case XML_ENTITY_REF_NODE:
2127 case XML_DOCUMENT_NODE:
2128 case XML_NOTATION_NODE:
2129 case XML_HTML_DOCUMENT_NODE: {
2130 return(xmlXPtrNewRange(node, 0, node,
2131 xmlXPtrGetArity(node)));
2132 }
2133 default:
2134 break;
2135 }
2136 return(NULL);
2137 }
2138 }
2139 default:
2140 TODO /* missed one case ??? */
2141 }
2142 return(NULL);
2143}
2144
2145/**
2146 * xmlXPtrRangeInsideFunction:
2147 * @ctxt: the XPointer Parser context
2148 * @nargs: the number of args
2149 *
2150 * Function implementing the range-inside() function 5.4.3
2151 * location-set range-inside(location-set )
2152 *
2153 * The range-inside function returns ranges covering the contents of
2154 * the locations in the argument location-set. For each location x in
2155 * the argument location-set, a range location is added to the result
2156 * location-set. If x is a range location, then x is added to the
2157 * result location-set. If x is not a range location, then x is used
2158 * as the container location of the start and end points of the range
2159 * location to be added; the index of the start point of the range is
2160 * zero; if the end point is a character point then its index is the
2161 * length of the string-value of x, and otherwise is the number of
2162 * location children of x.
2163 *
2164 */
2165void
2166xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2167 int i;
2168 xmlXPathObjectPtr set;
2169 xmlLocationSetPtr oldset;
2170 xmlLocationSetPtr newset;
2171
2172 CHECK_ARITY(1);
2173 if ((ctxt->value == NULL) ||
2174 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2175 (ctxt->value->type != XPATH_NODESET)))
2176 XP_ERROR(XPATH_INVALID_TYPE)
2177
2178 set = valuePop(ctxt);
2179 if (set->type == XPATH_NODESET) {
2180 xmlXPathObjectPtr tmp;
2181
2182 /*
2183 * First convert to a location set
2184 */
2185 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2186 xmlXPathFreeObject(set);
2187 set = tmp;
2188 }
2189 oldset = (xmlLocationSetPtr) set->user;
2190
2191 /*
2192 * The loop is to compute the covering range for each item and add it
2193 */
2194 newset = xmlXPtrLocationSetCreate(NULL);
2195 for (i = 0;i < oldset->locNr;i++) {
2196 xmlXPtrLocationSetAdd(newset,
2197 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2198 }
2199
2200 /*
2201 * Save the new value and cleanup
2202 */
2203 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2204 xmlXPathFreeObject(set);
2205}
2206
2207/**
2208 * xmlXPtrRangeToFunction:
2209 * @ctxt: the XPointer Parser context
2210 * @nargs: the number of args
2211 *
2212 * Implement the range-to() XPointer function
2213 */
2214void
2215xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2216 xmlXPathObjectPtr range;
2217 const xmlChar *cur;
2218 xmlXPathObjectPtr res, obj;
2219 xmlXPathObjectPtr tmp;
2220 xmlLocationSetPtr newset = NULL;
2221 xmlNodeSetPtr oldset;
2222 int i;
2223
2224 if (ctxt == NULL) return;
2225 CHECK_ARITY(1);
2226 /*
2227 * Save the expression pointer since we will have to evaluate
2228 * it multiple times. Initialize the new set.
2229 */
2230 CHECK_TYPE(XPATH_NODESET);
2231 obj = valuePop(ctxt);
2232 oldset = obj->nodesetval;
2233 ctxt->context->node = NULL;
2234
2235 cur = ctxt->cur;
2236 newset = xmlXPtrLocationSetCreate(NULL);
2237
2238 for (i = 0; i < oldset->nodeNr; i++) {
2239 ctxt->cur = cur;
2240
2241 /*
2242 * Run the evaluation with a node list made of a single item
2243 * in the nodeset.
2244 */
2245 ctxt->context->node = oldset->nodeTab[i];
2246 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2247 valuePush(ctxt, tmp);
2248
2249 xmlXPathEvalExpr(ctxt);
2250 CHECK_ERROR;
2251
2252 /*
2253 * The result of the evaluation need to be tested to
2254 * decided whether the filter succeeded or not
2255 */
2256 res = valuePop(ctxt);
2257 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2258 if (range != NULL) {
2259 xmlXPtrLocationSetAdd(newset, range);
2260 }
2261
2262 /*
2263 * Cleanup
2264 */
2265 if (res != NULL)
2266 xmlXPathFreeObject(res);
2267 if (ctxt->value == tmp) {
2268 res = valuePop(ctxt);
2269 xmlXPathFreeObject(res);
2270 }
2271
2272 ctxt->context->node = NULL;
2273 }
2274
2275 /*
2276 * The result is used as the new evaluation set.
2277 */
2278 xmlXPathFreeObject(obj);
2279 ctxt->context->node = NULL;
2280 ctxt->context->contextSize = -1;
2281 ctxt->context->proximityPosition = -1;
2282 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2283}
2284
2285/**
2286 * xmlXPtrAdvanceNode:
2287 * @cur: the node
2288 * @level: incremented/decremented to show level in tree
2289 *
2290 * Advance to the next element or text node in document order
2291 * TODO: add a stack for entering/exiting entities
2292 *
2293 * Returns -1 in case of failure, 0 otherwise
2294 */
2295xmlNodePtr
2296xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
2297next:
2298 if (cur == NULL)
2299 return(NULL);
2300 if (cur->children != NULL) {
2301 cur = cur->children ;
2302 if (level != NULL)
2303 (*level)++;
2304 goto found;
2305 }
2306skip: /* This label should only be needed if something is wrong! */
2307 if (cur->next != NULL) {
2308 cur = cur->next;
2309 goto found;
2310 }
2311 do {
2312 cur = cur->parent;
2313 if (level != NULL)
2314 (*level)--;
2315 if (cur == NULL) return(NULL);
2316 if (cur->next != NULL) {
2317 cur = cur->next;
2318 goto found;
2319 }
2320 } while (cur != NULL);
2321
2322found:
2323 if ((cur->type != XML_ELEMENT_NODE) &&
2324 (cur->type != XML_TEXT_NODE) &&
2325 (cur->type != XML_DOCUMENT_NODE) &&
2326 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2327 (cur->type != XML_CDATA_SECTION_NODE)) {
2328 if (cur->type == XML_ENTITY_REF_NODE) { /* Shouldn't happen */
2329 TODO
2330 goto skip;
2331 }
2332 goto next;
2333 }
2334 return(cur);
2335}
2336
2337/**
2338 * xmlXPtrAdvanceChar:
2339 * @node: the node
2340 * @indx: the indx
2341 * @bytes: the number of bytes
2342 *
2343 * Advance a point of the associated number of bytes (not UTF8 chars)
2344 *
2345 * Returns -1 in case of failure, 0 otherwise
2346 */
2347static int
2348xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
2349 xmlNodePtr cur;
2350 int pos;
2351 int len;
2352
2353 if ((node == NULL) || (indx == NULL))
2354 return(-1);
2355 cur = *node;
2356 if (cur == NULL)
2357 return(-1);
2358 pos = *indx;
2359
2360 while (bytes >= 0) {
2361 /*
2362 * First position to the beginning of the first text node
2363 * corresponding to this point
2364 */
2365 while ((cur != NULL) &&
2366 ((cur->type == XML_ELEMENT_NODE) ||
2367 (cur->type == XML_DOCUMENT_NODE) ||
2368 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2369 if (pos > 0) {
2370 cur = xmlXPtrGetNthChild(cur, pos);
2371 pos = 0;
2372 } else {
2373 cur = xmlXPtrAdvanceNode(cur, NULL);
2374 pos = 0;
2375 }
2376 }
2377
2378 if (cur == NULL) {
2379 *node = NULL;
2380 *indx = 0;
2381 return(-1);
2382 }
2383
2384 /*
2385 * if there is no move needed return the current value.
2386 */
2387 if (pos == 0) pos = 1;
2388 if (bytes == 0) {
2389 *node = cur;
2390 *indx = pos;
2391 return(0);
2392 }
2393 /*
2394 * We should have a text (or cdata) node ...
2395 */
2396 len = 0;
2397 if ((cur->type != XML_ELEMENT_NODE) &&
2398 (cur->content != NULL)) {
2399 len = xmlStrlen(cur->content);
2400 }
2401 if (pos > len) {
2402 /* Strange, the indx in the text node is greater than it's len */
2403 STRANGE
2404 pos = len;
2405 }
2406 if (pos + bytes >= len) {
2407 bytes -= (len - pos);
2408 cur = xmlXPtrAdvanceNode(cur, NULL);
2409 pos = 0;
2410 } else if (pos + bytes < len) {
2411 pos += bytes;
2412 *node = cur;
2413 *indx = pos;
2414 return(0);
2415 }
2416 }
2417 return(-1);
2418}
2419
2420/**
2421 * xmlXPtrMatchString:
2422 * @string: the string to search
2423 * @start: the start textnode
2424 * @startindex: the start index
2425 * @end: the end textnode IN/OUT
2426 * @endindex: the end index IN/OUT
2427 *
2428 * Check whether the document contains @string at the position
2429 * (@start, @startindex) and limited by the (@end, @endindex) point
2430 *
2431 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2432 * (@start, @startindex) will indicate the position of the beginning
2433 * of the range and (@end, @endindex) will indicate the end
2434 * of the range
2435 */
2436static int
2437xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2438 xmlNodePtr *end, int *endindex) {
2439 xmlNodePtr cur;
2440 int pos; /* 0 based */
2441 int len; /* in bytes */
2442 int stringlen; /* in bytes */
2443 int match;
2444
2445 if (string == NULL)
2446 return(-1);
2447 if (start == NULL)
2448 return(-1);
2449 if ((end == NULL) || (endindex == NULL))
2450 return(-1);
2451 cur = start;
2452 if (cur == NULL)
2453 return(-1);
2454 pos = startindex - 1;
2455 stringlen = xmlStrlen(string);
2456
2457 while (stringlen > 0) {
2458 if ((cur == *end) && (pos + stringlen > *endindex))
2459 return(0);
2460
2461 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
2462 len = xmlStrlen(cur->content);
2463 if (len >= pos + stringlen) {
2464 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2465 if (match) {
2466#ifdef DEBUG_RANGES
2467 xmlGenericError(xmlGenericErrorContext,
2468 "found range %d bytes at index %d of ->",
2469 stringlen, pos + 1);
2470 xmlDebugDumpString(stdout, cur->content);
2471 xmlGenericError(xmlGenericErrorContext, "\n");
2472#endif
2473 *end = cur;
2474 *endindex = pos + stringlen;
2475 return(1);
2476 } else {
2477 return(0);
2478 }
2479 } else {
2480 int sub = len - pos;
2481 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2482 if (match) {
2483#ifdef DEBUG_RANGES
2484 xmlGenericError(xmlGenericErrorContext,
2485 "found subrange %d bytes at index %d of ->",
2486 sub, pos + 1);
2487 xmlDebugDumpString(stdout, cur->content);
2488 xmlGenericError(xmlGenericErrorContext, "\n");
2489#endif
2490 string = &string[sub];
2491 stringlen -= sub;
2492 } else {
2493 return(0);
2494 }
2495 }
2496 }
2497 cur = xmlXPtrAdvanceNode(cur, NULL);
2498 if (cur == NULL)
2499 return(0);
2500 pos = 0;
2501 }
2502 return(1);
2503}
2504
2505/**
2506 * xmlXPtrSearchString:
2507 * @string: the string to search
2508 * @start: the start textnode IN/OUT
2509 * @startindex: the start index IN/OUT
2510 * @end: the end textnode
2511 * @endindex: the end index
2512 *
2513 * Search the next occurrence of @string within the document content
2514 * until the (@end, @endindex) point is reached
2515 *
2516 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2517 * (@start, @startindex) will indicate the position of the beginning
2518 * of the range and (@end, @endindex) will indicate the end
2519 * of the range
2520 */
2521static int
2522xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2523 xmlNodePtr *end, int *endindex) {
2524 xmlNodePtr cur;
2525 const xmlChar *str;
2526 int pos; /* 0 based */
2527 int len; /* in bytes */
2528 xmlChar first;
2529
2530 if (string == NULL)
2531 return(-1);
2532 if ((start == NULL) || (startindex == NULL))
2533 return(-1);
2534 if ((end == NULL) || (endindex == NULL))
2535 return(-1);
2536 cur = *start;
2537 if (cur == NULL)
2538 return(-1);
2539 pos = *startindex - 1;
2540 first = string[0];
2541
2542 while (cur != NULL) {
2543 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
2544 len = xmlStrlen(cur->content);
2545 while (pos <= len) {
2546 if (first != 0) {
2547 str = xmlStrchr(&cur->content[pos], first);
2548 if (str != NULL) {
2549 pos = (str - (xmlChar *)(cur->content));
2550#ifdef DEBUG_RANGES
2551 xmlGenericError(xmlGenericErrorContext,
2552 "found '%c' at index %d of ->",
2553 first, pos + 1);
2554 xmlDebugDumpString(stdout, cur->content);
2555 xmlGenericError(xmlGenericErrorContext, "\n");
2556#endif
2557 if (xmlXPtrMatchString(string, cur, pos + 1,
2558 end, endindex)) {
2559 *start = cur;
2560 *startindex = pos + 1;
2561 return(1);
2562 }
2563 pos++;
2564 } else {
2565 pos = len + 1;
2566 }
2567 } else {
2568 /*
2569 * An empty string is considered to match before each
2570 * character of the string-value and after the final
2571 * character.
2572 */
2573#ifdef DEBUG_RANGES
2574 xmlGenericError(xmlGenericErrorContext,
2575 "found '' at index %d of ->",
2576 pos + 1);
2577 xmlDebugDumpString(stdout, cur->content);
2578 xmlGenericError(xmlGenericErrorContext, "\n");
2579#endif
2580 *start = cur;
2581 *startindex = pos + 1;
2582 *end = cur;
2583 *endindex = pos + 1;
2584 return(1);
2585 }
2586 }
2587 }
2588 if ((cur == *end) && (pos >= *endindex))
2589 return(0);
2590 cur = xmlXPtrAdvanceNode(cur, NULL);
2591 if (cur == NULL)
2592 return(0);
2593 pos = 1;
2594 }
2595 return(0);
2596}
2597
2598/**
2599 * xmlXPtrGetLastChar:
2600 * @node: the node
2601 * @index: the index
2602 *
2603 * Computes the point coordinates of the last char of this point
2604 *
2605 * Returns -1 in case of failure, 0 otherwise
2606 */
2607static int
2608xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
2609 xmlNodePtr cur;
2610 int pos, len = 0;
2611
2612 if ((node == NULL) || (indx == NULL))
2613 return(-1);
2614 cur = *node;
2615 pos = *indx;
2616
2617 if (cur == NULL)
2618 return(-1);
2619
2620 if ((cur->type == XML_ELEMENT_NODE) ||
2621 (cur->type == XML_DOCUMENT_NODE) ||
2622 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2623 if (pos > 0) {
2624 cur = xmlXPtrGetNthChild(cur, pos);
2625 pos = 0;
2626 }
2627 }
2628 while (cur != NULL) {
2629 if (cur->last != NULL)
2630 cur = cur->last;
2631 else if ((cur->type != XML_ELEMENT_NODE) &&
2632 (cur->content != NULL)) {
2633 len = xmlStrlen(cur->content);
2634 break;
2635 } else {
2636 return(-1);
2637 }
2638 }
2639 if (cur == NULL)
2640 return(-1);
2641 *node = cur;
2642 *indx = len;
2643 return(0);
2644}
2645
2646/**
2647 * xmlXPtrGetStartPoint:
2648 * @obj: an range
2649 * @node: the resulting node
2650 * @indx: the resulting index
2651 *
2652 * read the object and return the start point coordinates.
2653 *
2654 * Returns -1 in case of failure, 0 otherwise
2655 */
2656static int
2657xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2658 if ((obj == NULL) || (node == NULL) || (indx == NULL))
2659 return(-1);
2660
2661 switch (obj->type) {
2662 case XPATH_POINT:
2663 *node = obj->user;
2664 if (obj->index <= 0)
2665 *indx = 0;
2666 else
2667 *indx = obj->index;
2668 return(0);
2669 case XPATH_RANGE:
2670 *node = obj->user;
2671 if (obj->index <= 0)
2672 *indx = 0;
2673 else
2674 *indx = obj->index;
2675 return(0);
2676 default:
2677 break;
2678 }
2679 return(-1);
2680}
2681
2682/**
2683 * xmlXPtrGetEndPoint:
2684 * @obj: an range
2685 * @node: the resulting node
2686 * @indx: the resulting indx
2687 *
2688 * read the object and return the end point coordinates.
2689 *
2690 * Returns -1 in case of failure, 0 otherwise
2691 */
2692static int
2693xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2694 if ((obj == NULL) || (node == NULL) || (indx == NULL))
2695 return(-1);
2696
2697 switch (obj->type) {
2698 case XPATH_POINT:
2699 *node = obj->user;
2700 if (obj->index <= 0)
2701 *indx = 0;
2702 else
2703 *indx = obj->index;
2704 return(0);
2705 case XPATH_RANGE:
2706 *node = obj->user;
2707 if (obj->index <= 0)
2708 *indx = 0;
2709 else
2710 *indx = obj->index;
2711 return(0);
2712 default:
2713 break;
2714 }
2715 return(-1);
2716}
2717
2718/**
2719 * xmlXPtrStringRangeFunction:
2720 * @ctxt: the XPointer Parser context
2721 * @nargs: the number of args
2722 *
2723 * Function implementing the string-range() function
2724 * range as described in 5.4.2
2725 *
2726 * ------------------------------
2727 * [Definition: For each location in the location-set argument,
2728 * string-range returns a set of string ranges, a set of substrings in a
2729 * string. Specifically, the string-value of the location is searched for
2730 * substrings that match the string argument, and the resulting location-set
2731 * will contain a range location for each non-overlapping match.]
2732 * An empty string is considered to match before each character of the
2733 * string-value and after the final character. Whitespace in a string
2734 * is matched literally, with no normalization except that provided by
2735 * XML for line ends. The third argument gives the position of the first
2736 * character to be in the resulting range, relative to the start of the
2737 * match. The default value is 1, which makes the range start immediately
2738 * before the first character of the matched string. The fourth argument
2739 * gives the number of characters in the range; the default is that the
2740 * range extends to the end of the matched string.
2741 *
2742 * Element boundaries, as well as entire embedded nodes such as processing
2743 * instructions and comments, are ignored as defined in [XPath].
2744 *
2745 * If the string in the second argument is not found in the string-value
2746 * of the location, or if a value in the third or fourth argument indicates
2747 * a string that is beyond the beginning or end of the document, the
2748 * expression fails.
2749 *
2750 * The points of the range-locations in the returned location-set will
2751 * all be character points.
2752 * ------------------------------
2753 */
2754void
2755xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2756 int i, startindex, endindex = 0, fendindex;
2757 xmlNodePtr start, end = 0, fend;
2758 xmlXPathObjectPtr set;
2759 xmlLocationSetPtr oldset;
2760 xmlLocationSetPtr newset;
2761 xmlXPathObjectPtr string;
2762 xmlXPathObjectPtr position = NULL;
2763 xmlXPathObjectPtr number = NULL;
2764 int found, pos = 0, num = 0;
2765
2766 /*
2767 * Grab the arguments
2768 */
2769 if ((nargs < 2) || (nargs > 4))
2770 XP_ERROR(XPATH_INVALID_ARITY);
2771
2772 if (nargs >= 4) {
2773 CHECK_TYPE(XPATH_NUMBER);
2774 number = valuePop(ctxt);
2775 if (number != NULL)
2776 num = (int) number->floatval;
2777 }
2778 if (nargs >= 3) {
2779 CHECK_TYPE(XPATH_NUMBER);
2780 position = valuePop(ctxt);
2781 if (position != NULL)
2782 pos = (int) position->floatval;
2783 }
2784 CHECK_TYPE(XPATH_STRING);
2785 string = valuePop(ctxt);
2786 if ((ctxt->value == NULL) ||
2787 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2788 (ctxt->value->type != XPATH_NODESET)))
2789 XP_ERROR(XPATH_INVALID_TYPE)
2790
2791 set = valuePop(ctxt);
2792 newset = xmlXPtrLocationSetCreate(NULL);
2793 if (set->nodesetval == NULL) {
2794 goto error;
2795 }
2796 if (set->type == XPATH_NODESET) {
2797 xmlXPathObjectPtr tmp;
2798
2799 /*
2800 * First convert to a location set
2801 */
2802 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2803 xmlXPathFreeObject(set);
2804 set = tmp;
2805 }
2806 oldset = (xmlLocationSetPtr) set->user;
2807
2808 /*
2809 * The loop is to search for each element in the location set
2810 * the list of location set corresponding to that search
2811 */
2812 for (i = 0;i < oldset->locNr;i++) {
2813#ifdef DEBUG_RANGES
2814 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2815#endif
2816
2817 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2818 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2819 xmlXPtrAdvanceChar(&start, &startindex, 0);
2820 xmlXPtrGetLastChar(&end, &endindex);
2821
2822#ifdef DEBUG_RANGES
2823 xmlGenericError(xmlGenericErrorContext,
2824 "from index %d of ->", startindex);
2825 xmlDebugDumpString(stdout, start->content);
2826 xmlGenericError(xmlGenericErrorContext, "\n");
2827 xmlGenericError(xmlGenericErrorContext,
2828 "to index %d of ->", endindex);
2829 xmlDebugDumpString(stdout, end->content);
2830 xmlGenericError(xmlGenericErrorContext, "\n");
2831#endif
2832 do {
2833 fend = end;
2834 fendindex = endindex;
2835 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2836 &fend, &fendindex);
2837 if (found == 1) {
2838 if (position == NULL) {
2839 xmlXPtrLocationSetAdd(newset,
2840 xmlXPtrNewRange(start, startindex, fend, fendindex));
2841 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2842 pos - 1) == 0) {
2843 if ((number != NULL) && (num > 0)) {
2844 int rindx;
2845 xmlNodePtr rend;
2846 rend = start;
2847 rindx = startindex - 1;
2848 if (xmlXPtrAdvanceChar(&rend, &rindx,
2849 num) == 0) {
2850 xmlXPtrLocationSetAdd(newset,
2851 xmlXPtrNewRange(start, startindex,
2852 rend, rindx));
2853 }
2854 } else if ((number != NULL) && (num <= 0)) {
2855 xmlXPtrLocationSetAdd(newset,
2856 xmlXPtrNewRange(start, startindex,
2857 start, startindex));
2858 } else {
2859 xmlXPtrLocationSetAdd(newset,
2860 xmlXPtrNewRange(start, startindex,
2861 fend, fendindex));
2862 }
2863 }
2864 start = fend;
2865 startindex = fendindex;
2866 if (string->stringval[0] == 0)
2867 startindex++;
2868 }
2869 } while (found == 1);
2870 }
2871
2872 /*
2873 * Save the new value and cleanup
2874 */
2875error:
2876 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2877 xmlXPathFreeObject(set);
2878 xmlXPathFreeObject(string);
2879 if (position) xmlXPathFreeObject(position);
2880 if (number) xmlXPathFreeObject(number);
2881}
2882
2883/**
2884 * xmlXPtrEvalRangePredicate:
2885 * @ctxt: the XPointer Parser context
2886 *
2887 * [8] Predicate ::= '[' PredicateExpr ']'
2888 * [9] PredicateExpr ::= Expr
2889 *
2890 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2891 * a Location Set instead of a node set
2892 */
2893void
2894xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2895 const xmlChar *cur;
2896 xmlXPathObjectPtr res;
2897 xmlXPathObjectPtr obj, tmp;
2898 xmlLocationSetPtr newset = NULL;
2899 xmlLocationSetPtr oldset;
2900 int i;
2901
2902 if (ctxt == NULL) return;
2903
2904 SKIP_BLANKS;
2905 if (CUR != '[') {
2906 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2907 }
2908 NEXT;
2909 SKIP_BLANKS;
2910
2911 /*
2912 * Extract the old set, and then evaluate the result of the
2913 * expression for all the element in the set. use it to grow
2914 * up a new set.
2915 */
2916 CHECK_TYPE(XPATH_LOCATIONSET);
2917 obj = valuePop(ctxt);
2918 oldset = obj->user;
2919 ctxt->context->node = NULL;
2920
2921 if ((oldset == NULL) || (oldset->locNr == 0)) {
2922 ctxt->context->contextSize = 0;
2923 ctxt->context->proximityPosition = 0;
2924 xmlXPathEvalExpr(ctxt);
2925 res = valuePop(ctxt);
2926 if (res != NULL)
2927 xmlXPathFreeObject(res);
2928 valuePush(ctxt, obj);
2929 CHECK_ERROR;
2930 } else {
2931 /*
2932 * Save the expression pointer since we will have to evaluate
2933 * it multiple times. Initialize the new set.
2934 */
2935 cur = ctxt->cur;
2936 newset = xmlXPtrLocationSetCreate(NULL);
2937
2938 for (i = 0; i < oldset->locNr; i++) {
2939 ctxt->cur = cur;
2940
2941 /*
2942 * Run the evaluation with a node list made of a single item
2943 * in the nodeset.
2944 */
2945 ctxt->context->node = oldset->locTab[i]->user;
2946 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2947 valuePush(ctxt, tmp);
2948 ctxt->context->contextSize = oldset->locNr;
2949 ctxt->context->proximityPosition = i + 1;
2950
2951 xmlXPathEvalExpr(ctxt);
2952 CHECK_ERROR;
2953
2954 /*
2955 * The result of the evaluation need to be tested to
2956 * decided whether the filter succeeded or not
2957 */
2958 res = valuePop(ctxt);
2959 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2960 xmlXPtrLocationSetAdd(newset,
2961 xmlXPathObjectCopy(oldset->locTab[i]));
2962 }
2963
2964 /*
2965 * Cleanup
2966 */
2967 if (res != NULL)
2968 xmlXPathFreeObject(res);
2969 if (ctxt->value == tmp) {
2970 res = valuePop(ctxt);
2971 xmlXPathFreeObject(res);
2972 }
2973
2974 ctxt->context->node = NULL;
2975 }
2976
2977 /*
2978 * The result is used as the new evaluation set.
2979 */
2980 xmlXPathFreeObject(obj);
2981 ctxt->context->node = NULL;
2982 ctxt->context->contextSize = -1;
2983 ctxt->context->proximityPosition = -1;
2984 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2985 }
2986 if (CUR != ']') {
2987 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2988 }
2989
2990 NEXT;
2991 SKIP_BLANKS;
2992}
2993
2994#define bottom_xpointer
2995#include "elfgcchack.h"
2996#endif
2997
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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