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