blob: 2b96fd3d3d71bbb82b7d901eee701fb6e7f72bf3 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xpointer.c : Code to handle XML Pointer
3 *
Daniel Veillard9a237c92003-02-13 15:52:58 +00004 * Base implementation was made accordingly to
5 * W3C Candidate Recommendation 7 June 2000
Owen Taylor3473f882001-02-23 17:55:21 +00006 * http://www.w3.org/TR/2000/CR-xptr-20000607
7 *
Daniel Veillard9a237c92003-02-13 15:52:58 +00008 * 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 *
Owen Taylor3473f882001-02-23 17:55:21 +000012 * See Copyright for the status of this software.
13 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000014 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000015 */
16
Daniel Veillard34ce8be2002-03-18 19:37:11 +000017#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000018#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000019
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000020/*
Owen Taylor3473f882001-02-23 17:55:21 +000021 * 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
Owen Taylor3473f882001-02-23 17:55:21 +000028#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>
Owen Taylor3473f882001-02-23 17:55:21 +000035#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000036#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000037
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 */
Daniel Veillard017b1082001-06-21 11:20:21 +000044#ifdef DEBUG_RANGES
45#ifdef LIBXML_DEBUG_ENABLED
46#include <libxml/debugXML.h>
47#endif
48#endif
Owen Taylor3473f882001-02-23 17:55:21 +000049
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 * *
Daniel Veillardfcf719c2003-10-10 11:42:17 +000062 * 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{
Daniel Veillard659e71e2003-10-10 14:10:40 +000075 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_XPOINTER,
Daniel Veillardfcf719c2003-10-10 11:42:17 +000076 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)) {
Daniel Veillard659e71e2003-10-10 14:10:40 +000095 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardfcf719c2003-10-10 11:42:17 +000096 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 {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000112 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000113 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 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000123 * A few helper functions for child sequences *
124 * *
125 ************************************************************************/
126
127xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur);
128/**
129 * xmlXPtrGetArity:
130 * @cur: the node
131 *
132 * Returns the number of child for an element, -1 in case of error
133 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000134static int
Owen Taylor3473f882001-02-23 17:55:21 +0000135xmlXPtrGetArity(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 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000157static int
Owen Taylor3473f882001-02-23 17:55:21 +0000158xmlXPtrGetIndex(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 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000179static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000180xmlXPtrGetNthChild(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 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000217static int
Owen Taylor3473f882001-02-23 17:55:21 +0000218xmlXPtrCmpPoints(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
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000237 * @indx: the indx within the node
Owen Taylor3473f882001-02-23 17:55:21 +0000238 *
239 * Create a new xmlXPathObjectPtr of type point
240 *
241 * Returns the newly created object.
242 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000243static xmlXPathObjectPtr
244xmlXPtrNewPoint(xmlNodePtr node, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000245 xmlXPathObjectPtr ret;
246
247 if (node == NULL)
248 return(NULL);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000249 if (indx < 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000250 return(NULL);
251
252 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
253 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000254 xmlXPtrErrMemory("allocating point");
Owen Taylor3473f882001-02-23 17:55:21 +0000255 return(NULL);
256 }
257 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
258 ret->type = XPATH_POINT;
259 ret->user = (void *) node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000260 ret->index = indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000261 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 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000270static void
Owen Taylor3473f882001-02-23 17:55:21 +0000271xmlXPtrRangeCheckOrder(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 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000299 * Returns 1 if equal, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000300 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000301static int
Owen Taylor3473f882001-02-23 17:55:21 +0000302xmlXPtrRangesEqual(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) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000349 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000350 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) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000386 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000387 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) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000421 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000422 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) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000458 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000459 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) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000491 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000492 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) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000521 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000522 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 break;
553 case XPATH_NODESET:
554 /*
555 * Empty set ...
556 */
557 if (end->nodesetval->nodeNr <= 0)
558 return(NULL);
559 break;
560 default:
561 TODO
562 return(NULL);
563 }
564
565 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
566 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000567 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000568 return(NULL);
569 }
570 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
571 ret->type = XPATH_RANGE;
572 ret->user = start;
573 ret->index = -1;
574 switch (end->type) {
575 case XPATH_POINT:
576 ret->user2 = end->user;
577 ret->index2 = end->index;
578 case XPATH_NODESET: {
579 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
580 ret->index2 = -1;
581 break;
582 }
583 default:
584 STRANGE
585 return(NULL);
586 }
587 xmlXPtrRangeCheckOrder(ret);
588 return(ret);
589}
590
591#define XML_RANGESET_DEFAULT 10
592
593/**
594 * xmlXPtrLocationSetCreate:
595 * @val: an initial xmlXPathObjectPtr, or NULL
596 *
597 * Create a new xmlLocationSetPtr of type double and of value @val
598 *
599 * Returns the newly created object.
600 */
601xmlLocationSetPtr
602xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
603 xmlLocationSetPtr ret;
604
605 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
606 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000607 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000608 return(NULL);
609 }
610 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
611 if (val != NULL) {
612 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
613 sizeof(xmlXPathObjectPtr));
614 if (ret->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000615 xmlXPtrErrMemory("allocating locationset");
616 xmlFree(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000617 return(NULL);
618 }
619 memset(ret->locTab, 0 ,
620 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
621 ret->locMax = XML_RANGESET_DEFAULT;
622 ret->locTab[ret->locNr++] = val;
623 }
624 return(ret);
625}
626
627/**
628 * xmlXPtrLocationSetAdd:
629 * @cur: the initial range set
630 * @val: a new xmlXPathObjectPtr
631 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000632 * add a new xmlXPathObjectPtr to an existing LocationSet
Owen Taylor3473f882001-02-23 17:55:21 +0000633 * If the location already exist in the set @val is freed.
634 */
635void
636xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
637 int i;
638
639 if (val == NULL) return;
640
641 /*
642 * check against doublons
643 */
644 for (i = 0;i < cur->locNr;i++) {
645 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
646 xmlXPathFreeObject(val);
647 return;
648 }
649 }
650
651 /*
652 * grow the locTab if needed
653 */
654 if (cur->locMax == 0) {
655 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
656 sizeof(xmlXPathObjectPtr));
657 if (cur->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000658 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000659 return;
660 }
661 memset(cur->locTab, 0 ,
662 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
663 cur->locMax = XML_RANGESET_DEFAULT;
664 } else if (cur->locNr == cur->locMax) {
665 xmlXPathObjectPtr *temp;
666
667 cur->locMax *= 2;
668 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
669 sizeof(xmlXPathObjectPtr));
670 if (temp == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000671 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000672 return;
673 }
674 cur->locTab = temp;
675 }
676 cur->locTab[cur->locNr++] = val;
677}
678
679/**
680 * xmlXPtrLocationSetMerge:
681 * @val1: the first LocationSet
682 * @val2: the second LocationSet
683 *
684 * Merges two rangesets, all ranges from @val2 are added to @val1
685 *
686 * Returns val1 once extended or NULL in case of error.
687 */
688xmlLocationSetPtr
689xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
690 int i;
691
692 if (val1 == NULL) return(NULL);
693 if (val2 == NULL) return(val1);
694
695 /*
696 * !!!!! this can be optimized a lot, knowing that both
697 * val1 and val2 already have unicity of their values.
698 */
699
700 for (i = 0;i < val2->locNr;i++)
701 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
702
703 return(val1);
704}
705
706/**
707 * xmlXPtrLocationSetDel:
708 * @cur: the initial range set
709 * @val: an xmlXPathObjectPtr
710 *
711 * Removes an xmlXPathObjectPtr from an existing LocationSet
712 */
713void
714xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
715 int i;
716
717 if (cur == NULL) return;
718 if (val == NULL) return;
719
720 /*
721 * check against doublons
722 */
723 for (i = 0;i < cur->locNr;i++)
724 if (cur->locTab[i] == val) break;
725
726 if (i >= cur->locNr) {
727#ifdef DEBUG
728 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard913d6e02001-11-28 14:53:53 +0000729 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000730#endif
731 return;
732 }
733 cur->locNr--;
734 for (;i < cur->locNr;i++)
735 cur->locTab[i] = cur->locTab[i + 1];
736 cur->locTab[cur->locNr] = NULL;
737}
738
739/**
740 * xmlXPtrLocationSetRemove:
741 * @cur: the initial range set
742 * @val: the index to remove
743 *
744 * Removes an entry from an existing LocationSet list.
745 */
746void
747xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
748 if (cur == NULL) return;
749 if (val >= cur->locNr) return;
750 cur->locNr--;
751 for (;val < cur->locNr;val++)
752 cur->locTab[val] = cur->locTab[val + 1];
753 cur->locTab[cur->locNr] = NULL;
754}
755
756/**
757 * xmlXPtrFreeLocationSet:
758 * @obj: the xmlLocationSetPtr to free
759 *
760 * Free the LocationSet compound (not the actual ranges !).
761 */
762void
763xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
764 int i;
765
766 if (obj == NULL) return;
767 if (obj->locTab != NULL) {
768 for (i = 0;i < obj->locNr; i++) {
769 xmlXPathFreeObject(obj->locTab[i]);
770 }
Owen Taylor3473f882001-02-23 17:55:21 +0000771 xmlFree(obj->locTab);
772 }
Owen Taylor3473f882001-02-23 17:55:21 +0000773 xmlFree(obj);
774}
775
776/**
777 * xmlXPtrNewLocationSetNodes:
778 * @start: the start NodePtr value
779 * @end: the end NodePtr value or NULL
780 *
781 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
782 * it with the single range made of the two nodes @start and @end
783 *
784 * Returns the newly created object.
785 */
786xmlXPathObjectPtr
787xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
788 xmlXPathObjectPtr ret;
789
790 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
791 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000792 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000793 return(NULL);
794 }
795 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
796 ret->type = XPATH_LOCATIONSET;
797 if (end == NULL)
798 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
799 else
800 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
801 return(ret);
802}
803
804/**
805 * xmlXPtrNewLocationSetNodeSet:
806 * @set: a node set
807 *
808 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
809 * it with all the nodes from @set
810 *
811 * Returns the newly created object.
812 */
813xmlXPathObjectPtr
814xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
815 xmlXPathObjectPtr ret;
816
817 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
818 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000819 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000820 return(NULL);
821 }
822 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
823 ret->type = XPATH_LOCATIONSET;
824 if (set != NULL) {
825 int i;
826 xmlLocationSetPtr newset;
827
828 newset = xmlXPtrLocationSetCreate(NULL);
829 if (newset == NULL)
830 return(ret);
831
832 for (i = 0;i < set->nodeNr;i++)
833 xmlXPtrLocationSetAdd(newset,
834 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
835
836 ret->user = (void *) newset;
837 }
838 return(ret);
839}
840
841/**
842 * xmlXPtrWrapLocationSet:
843 * @val: the LocationSet value
844 *
845 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
846 *
847 * Returns the newly created object.
848 */
849xmlXPathObjectPtr
850xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
851 xmlXPathObjectPtr ret;
852
853 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
854 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000855 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000856 return(NULL);
857 }
858 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
859 ret->type = XPATH_LOCATIONSET;
860 ret->user = (void *) val;
861 return(ret);
862}
863
864/************************************************************************
865 * *
866 * The parser *
867 * *
868 ************************************************************************/
869
Daniel Veillard9a237c92003-02-13 15:52:58 +0000870static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
871
Owen Taylor3473f882001-02-23 17:55:21 +0000872/*
873 * Macros for accessing the content. Those should be used only by the parser,
874 * and not exported.
875 *
876 * Dirty macros, i.e. one need to make assumption on the context to use them
877 *
878 * CUR_PTR return the current pointer to the xmlChar to be parsed.
879 * CUR returns the current xmlChar value, i.e. a 8 bit value
880 * in ISO-Latin or UTF-8.
881 * This should be used internally by the parser
882 * only to compare to ASCII values otherwise it would break when
883 * running with UTF-8 encoding.
884 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
885 * to compare on ASCII based substring.
886 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
887 * strings within the parser.
888 * CURRENT Returns the current char value, with the full decoding of
889 * UTF-8 if we are using this mode. It returns an int.
890 * NEXT Skip to the next character, this does the proper decoding
891 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
892 * It returns the pointer to the current xmlChar.
893 */
894
895#define CUR (*ctxt->cur)
896#define SKIP(val) ctxt->cur += (val)
897#define NXT(val) ctxt->cur[(val)]
898#define CUR_PTR ctxt->cur
899
900#define SKIP_BLANKS \
William M. Brack272693c2003-11-14 16:20:34 +0000901 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Owen Taylor3473f882001-02-23 17:55:21 +0000902
903#define CURRENT (*ctxt->cur)
904#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
905
906/*
907 * xmlXPtrGetChildNo:
908 * @ctxt: the XPointer Parser context
909 * @index: the child number
910 *
911 * Move the current node of the nodeset on the stack to the
912 * given child if found
913 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000914static void
915xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000916 xmlNodePtr cur = NULL;
917 xmlXPathObjectPtr obj;
918 xmlNodeSetPtr oldset;
919
920 CHECK_TYPE(XPATH_NODESET);
921 obj = valuePop(ctxt);
922 oldset = obj->nodesetval;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000923 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000924 xmlXPathFreeObject(obj);
925 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
926 return;
927 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000928 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
Owen Taylor3473f882001-02-23 17:55:21 +0000929 if (cur == NULL) {
930 xmlXPathFreeObject(obj);
931 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
932 return;
933 }
934 oldset->nodeTab[0] = cur;
935 valuePush(ctxt, obj);
936}
937
938/**
939 * xmlXPtrEvalXPtrPart:
940 * @ctxt: the XPointer Parser context
941 * @name: the preparsed Scheme for the XPtrPart
942 *
943 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
944 * | Scheme '(' SchemeSpecificExpr ')'
945 *
946 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
947 *
948 * SchemeSpecificExpr ::= StringWithBalancedParens
949 *
950 * StringWithBalancedParens ::=
951 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
952 * [VC: Parenthesis escaping]
953 *
954 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
955 *
956 * VC: Parenthesis escaping:
957 * The end of an XPointer part is signaled by the right parenthesis ")"
958 * character that is balanced with the left parenthesis "(" character
959 * that began the part. Any unbalanced parenthesis character inside the
960 * expression, even within literals, must be escaped with a circumflex (^)
961 * character preceding it. If the expression contains any literal
962 * occurrences of the circumflex, each must be escaped with an additional
963 * circumflex (that is, ^^). If the unescaped parentheses in the expression
964 * are not balanced, a syntax error results.
965 *
966 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000967 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
Owen Taylor3473f882001-02-23 17:55:21 +0000968 *
969 * TODO: there is no new scheme registration mechanism
970 */
971
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000972static void
Owen Taylor3473f882001-02-23 17:55:21 +0000973xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
974 xmlChar *buffer, *cur;
975 int len;
976 int level;
977
978 if (name == NULL)
979 name = xmlXPathParseName(ctxt);
980 if (name == NULL)
981 XP_ERROR(XPATH_EXPR_ERROR);
982
983 if (CUR != '(')
984 XP_ERROR(XPATH_EXPR_ERROR);
985 NEXT;
986 level = 1;
987
988 len = xmlStrlen(ctxt->cur);
989 len++;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000990 buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +0000991 if (buffer == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000992 xmlXPtrErrMemory("allocating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +0000993 return;
994 }
995
996 cur = buffer;
997 while (CUR != 0) {
998 if (CUR == ')') {
999 level--;
1000 if (level == 0) {
1001 NEXT;
1002 break;
1003 }
1004 *cur++ = CUR;
1005 } else if (CUR == '(') {
1006 level++;
1007 *cur++ = CUR;
1008 } else if (CUR == '^') {
1009 NEXT;
1010 if ((CUR == ')') || (CUR == '(') || (CUR == '^')) {
1011 *cur++ = CUR;
1012 } else {
1013 *cur++ = '^';
1014 *cur++ = CUR;
1015 }
1016 } else {
1017 *cur++ = CUR;
1018 }
1019 NEXT;
1020 }
1021 *cur = 0;
1022
1023 if ((level != 0) && (CUR == 0)) {
1024 xmlFree(buffer);
1025 XP_ERROR(XPTR_SYNTAX_ERROR);
1026 }
1027
1028 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
1029 const xmlChar *left = CUR_PTR;
1030
1031 CUR_PTR = buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001032 xmlXPathEvalExpr(ctxt);
1033 CUR_PTR=left;
Daniel Veillard9a237c92003-02-13 15:52:58 +00001034 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
1035 const xmlChar *left = CUR_PTR;
1036 xmlChar *name2;
1037
1038 CUR_PTR = buffer;
1039 if (buffer[0] == '/') {
1040 xmlXPathRoot(ctxt);
1041 xmlXPtrEvalChildSeq(ctxt, NULL);
1042 } else {
1043 name2 = xmlXPathParseName(ctxt);
1044 if (name2 == NULL) {
1045 CUR_PTR = left;
1046 xmlFree(buffer);
1047 XP_ERROR(XPATH_EXPR_ERROR);
1048 }
1049 xmlXPtrEvalChildSeq(ctxt, name2);
1050 }
1051 CUR_PTR = left;
Owen Taylor3473f882001-02-23 17:55:21 +00001052#ifdef XPTR_XMLNS_SCHEME
1053 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1054 const xmlChar *left = CUR_PTR;
1055 xmlChar *prefix;
1056 xmlChar *URI;
1057 xmlURIPtr value;
1058
1059 CUR_PTR = buffer;
1060 prefix = xmlXPathParseNCName(ctxt);
1061 if (prefix == NULL) {
1062 xmlFree(buffer);
1063 xmlFree(name);
1064 XP_ERROR(XPTR_SYNTAX_ERROR);
1065 }
1066 SKIP_BLANKS;
1067 if (CUR != '=') {
1068 xmlFree(prefix);
1069 xmlFree(buffer);
1070 xmlFree(name);
1071 XP_ERROR(XPTR_SYNTAX_ERROR);
1072 }
1073 NEXT;
1074 SKIP_BLANKS;
1075 /* @@ check escaping in the XPointer WD */
1076
1077 value = xmlParseURI((const char *)ctxt->cur);
1078 if (value == NULL) {
1079 xmlFree(prefix);
1080 xmlFree(buffer);
1081 xmlFree(name);
1082 XP_ERROR(XPTR_SYNTAX_ERROR);
1083 }
1084 URI = xmlSaveUri(value);
1085 xmlFreeURI(value);
1086 if (URI == NULL) {
1087 xmlFree(prefix);
1088 xmlFree(buffer);
1089 xmlFree(name);
1090 XP_ERROR(XPATH_MEMORY_ERROR);
1091 }
1092
1093 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1094 CUR_PTR = left;
Daniel Veillard56f21f22002-11-06 15:49:46 +00001095 xmlFree(URI);
1096 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001097#endif /* XPTR_XMLNS_SCHEME */
1098 } else {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001099 xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
1100 "unsupported scheme '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001101 }
1102 xmlFree(buffer);
1103 xmlFree(name);
1104}
1105
1106/**
1107 * xmlXPtrEvalFullXPtr:
1108 * @ctxt: the XPointer Parser context
1109 * @name: the preparsed Scheme for the first XPtrPart
1110 *
1111 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1112 *
1113 * As the specs says:
1114 * -----------
1115 * When multiple XPtrParts are provided, they must be evaluated in
1116 * left-to-right order. If evaluation of one part fails, the nexti
1117 * is evaluated. The following conditions cause XPointer part failure:
1118 *
1119 * - An unknown scheme
1120 * - A scheme that does not locate any sub-resource present in the resource
1121 * - A scheme that is not applicable to the media type of the resource
1122 *
1123 * The XPointer application must consume a failed XPointer part and
1124 * attempt to evaluate the next one, if any. The result of the first
1125 * XPointer part whose evaluation succeeds is taken to be the fragment
1126 * located by the XPointer as a whole. If all the parts fail, the result
1127 * for the XPointer as a whole is a sub-resource error.
1128 * -----------
1129 *
1130 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001131 * expressions or other schemes.
Owen Taylor3473f882001-02-23 17:55:21 +00001132 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001133static void
Owen Taylor3473f882001-02-23 17:55:21 +00001134xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1135 if (name == NULL)
1136 name = xmlXPathParseName(ctxt);
1137 if (name == NULL)
1138 XP_ERROR(XPATH_EXPR_ERROR);
1139 while (name != NULL) {
1140 xmlXPtrEvalXPtrPart(ctxt, name);
1141
1142 /* in case of syntax error, break here */
1143 if (ctxt->error != XPATH_EXPRESSION_OK)
1144 return;
1145
1146 /*
1147 * If the returned value is a non-empty nodeset
1148 * or location set, return here.
1149 */
1150 if (ctxt->value != NULL) {
1151 xmlXPathObjectPtr obj = ctxt->value;
1152
1153 switch (obj->type) {
1154 case XPATH_LOCATIONSET: {
1155 xmlLocationSetPtr loc = ctxt->value->user;
1156 if ((loc != NULL) && (loc->locNr > 0))
1157 return;
1158 break;
1159 }
1160 case XPATH_NODESET: {
1161 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1162 if ((loc != NULL) && (loc->nodeNr > 0))
1163 return;
1164 break;
1165 }
1166 default:
1167 break;
1168 }
1169
1170 /*
1171 * Evaluating to improper values is equivalent to
1172 * a sub-resource error, clean-up the stack
1173 */
1174 do {
1175 obj = valuePop(ctxt);
1176 if (obj != NULL) {
1177 xmlXPathFreeObject(obj);
1178 }
1179 } while (obj != NULL);
1180 }
1181
1182 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001183 * Is there another XPointer part.
Owen Taylor3473f882001-02-23 17:55:21 +00001184 */
1185 SKIP_BLANKS;
1186 name = xmlXPathParseName(ctxt);
1187 }
1188}
1189
1190/**
1191 * xmlXPtrEvalChildSeq:
1192 * @ctxt: the XPointer Parser context
1193 * @name: a possible ID name of the child sequence
1194 *
1195 * ChildSeq ::= '/1' ('/' [0-9]*)*
1196 * | Name ('/' [0-9]*)+
1197 *
1198 * Parse and evaluate a Child Sequence. This routine also handle the
1199 * case of a Bare Name used to get a document ID.
1200 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001201static void
Owen Taylor3473f882001-02-23 17:55:21 +00001202xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1203 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001204 * XPointer don't allow by syntax to address in mutirooted trees
Owen Taylor3473f882001-02-23 17:55:21 +00001205 * this might prove useful in some cases, warn about it.
1206 */
1207 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001208 xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
1209 "warning: ChildSeq not starting by /1\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001210 }
1211
1212 if (name != NULL) {
1213 valuePush(ctxt, xmlXPathNewString(name));
1214 xmlFree(name);
1215 xmlXPathIdFunction(ctxt, 1);
1216 CHECK_ERROR;
1217 }
1218
1219 while (CUR == '/') {
1220 int child = 0;
1221 NEXT;
1222
1223 while ((CUR >= '0') && (CUR <= '9')) {
1224 child = child * 10 + (CUR - '0');
1225 NEXT;
1226 }
1227 xmlXPtrGetChildNo(ctxt, child);
1228 }
1229}
1230
1231
1232/**
1233 * xmlXPtrEvalXPointer:
1234 * @ctxt: the XPointer Parser context
1235 *
1236 * XPointer ::= Name
1237 * | ChildSeq
1238 * | FullXPtr
1239 *
1240 * Parse and evaluate an XPointer
1241 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001242static void
Owen Taylor3473f882001-02-23 17:55:21 +00001243xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001244 if (ctxt->valueTab == NULL) {
1245 /* Allocate the value stack */
1246 ctxt->valueTab = (xmlXPathObjectPtr *)
1247 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1248 if (ctxt->valueTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001249 xmlXPtrErrMemory("allocating evaluation context");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001250 return;
1251 }
1252 ctxt->valueNr = 0;
1253 ctxt->valueMax = 10;
1254 ctxt->value = NULL;
1255 }
Owen Taylor3473f882001-02-23 17:55:21 +00001256 SKIP_BLANKS;
1257 if (CUR == '/') {
1258 xmlXPathRoot(ctxt);
1259 xmlXPtrEvalChildSeq(ctxt, NULL);
1260 } else {
1261 xmlChar *name;
1262
1263 name = xmlXPathParseName(ctxt);
1264 if (name == NULL)
1265 XP_ERROR(XPATH_EXPR_ERROR);
1266 if (CUR == '(') {
1267 xmlXPtrEvalFullXPtr(ctxt, name);
1268 /* Short evaluation */
1269 return;
1270 } else {
1271 /* this handle both Bare Names and Child Sequences */
1272 xmlXPtrEvalChildSeq(ctxt, name);
1273 }
1274 }
1275 SKIP_BLANKS;
1276 if (CUR != 0)
1277 XP_ERROR(XPATH_EXPR_ERROR);
1278}
1279
1280
1281/************************************************************************
1282 * *
1283 * General routines *
1284 * *
1285 ************************************************************************/
1286
Owen Taylor3473f882001-02-23 17:55:21 +00001287void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1288void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1289void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1290void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1291void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1292void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1293void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1294
1295/**
1296 * xmlXPtrNewContext:
1297 * @doc: the XML document
1298 * @here: the node that directly contains the XPointer being evaluated or NULL
1299 * @origin: the element from which a user or program initiated traversal of
1300 * the link, or NULL.
1301 *
1302 * Create a new XPointer context
1303 *
1304 * Returns the xmlXPathContext just allocated.
1305 */
1306xmlXPathContextPtr
1307xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1308 xmlXPathContextPtr ret;
1309
1310 ret = xmlXPathNewContext(doc);
1311 if (ret == NULL)
1312 return(ret);
1313 ret->xptr = 1;
1314 ret->here = here;
1315 ret->origin = origin;
1316
1317 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1318 xmlXPtrRangeToFunction);
1319 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1320 xmlXPtrRangeFunction);
1321 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1322 xmlXPtrRangeInsideFunction);
1323 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1324 xmlXPtrStringRangeFunction);
1325 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1326 xmlXPtrStartPointFunction);
1327 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1328 xmlXPtrEndPointFunction);
1329 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1330 xmlXPtrHereFunction);
1331 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1332 xmlXPtrOriginFunction);
1333
1334 return(ret);
1335}
1336
1337/**
1338 * xmlXPtrEval:
1339 * @str: the XPointer expression
1340 * @ctx: the XPointer context
1341 *
1342 * Evaluate the XPath Location Path in the given context.
1343 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001344 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +00001345 * the caller has to free the object.
1346 */
1347xmlXPathObjectPtr
1348xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1349 xmlXPathParserContextPtr ctxt;
1350 xmlXPathObjectPtr res = NULL, tmp;
1351 xmlXPathObjectPtr init = NULL;
1352 int stack = 0;
1353
1354 xmlXPathInit();
1355
1356 if ((ctx == NULL) || (str == NULL))
1357 return(NULL);
1358
1359 ctxt = xmlXPathNewParserContext(str, ctx);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001360 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001361 xmlXPtrEvalXPointer(ctxt);
1362
1363 if ((ctxt->value != NULL) &&
1364 (ctxt->value->type != XPATH_NODESET) &&
1365 (ctxt->value->type != XPATH_LOCATIONSET)) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001366 xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
1367 "xmlXPtrEval: evaluation failed to return a node set\n",
1368 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001369 } else {
1370 res = valuePop(ctxt);
1371 }
1372
1373 do {
1374 tmp = valuePop(ctxt);
1375 if (tmp != NULL) {
1376 if (tmp != init) {
1377 if (tmp->type == XPATH_NODESET) {
1378 /*
1379 * Evaluation may push a root nodeset which is unused
1380 */
1381 xmlNodeSetPtr set;
1382 set = tmp->nodesetval;
1383 if ((set->nodeNr != 1) ||
1384 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1385 stack++;
1386 } else
1387 stack++;
1388 }
1389 xmlXPathFreeObject(tmp);
1390 }
1391 } while (tmp != NULL);
1392 if (stack != 0) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001393 xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
1394 "xmlXPtrEval: object(s) left on the eval stack\n",
1395 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001396 }
1397 if (ctxt->error != XPATH_EXPRESSION_OK) {
1398 xmlXPathFreeObject(res);
1399 res = NULL;
1400 }
1401
1402 xmlXPathFreeParserContext(ctxt);
1403 return(res);
1404}
1405
1406/**
1407 * xmlXPtrBuildRangeNodeList:
1408 * @range: a range object
1409 *
1410 * Build a node list tree copy of the range
1411 *
1412 * Returns an xmlNodePtr list or NULL.
1413 * the caller has to free the node tree.
1414 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001415static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001416xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1417 /* pointers to generated nodes */
1418 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1419 /* pointers to traversal nodes */
1420 xmlNodePtr start, cur, end;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001421 int index1, index2;
Owen Taylor3473f882001-02-23 17:55:21 +00001422
1423 if (range == NULL)
1424 return(NULL);
1425 if (range->type != XPATH_RANGE)
1426 return(NULL);
1427 start = (xmlNodePtr) range->user;
1428
1429 if (start == NULL)
1430 return(NULL);
1431 end = range->user2;
1432 if (end == NULL)
1433 return(xmlCopyNode(start, 1));
1434
1435 cur = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001436 index1 = range->index;
Owen Taylor3473f882001-02-23 17:55:21 +00001437 index2 = range->index2;
1438 while (cur != NULL) {
1439 if (cur == end) {
1440 if (cur->type == XML_TEXT_NODE) {
1441 const xmlChar *content = cur->content;
1442 int len;
1443
1444 if (content == NULL) {
1445 tmp = xmlNewTextLen(NULL, 0);
1446 } else {
1447 len = index2;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001448 if ((cur == start) && (index1 > 1)) {
1449 content += (index1 - 1);
1450 len -= (index1 - 1);
1451 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001452 } else {
1453 len = index2;
1454 }
1455 tmp = xmlNewTextLen(content, len);
1456 }
1457 /* single sub text node selection */
1458 if (list == NULL)
1459 return(tmp);
1460 /* prune and return full set */
1461 if (last != NULL)
1462 xmlAddNextSibling(last, tmp);
1463 else
1464 xmlAddChild(parent, tmp);
1465 return(list);
1466 } else {
1467 tmp = xmlCopyNode(cur, 0);
1468 if (list == NULL)
1469 list = tmp;
1470 else {
1471 if (last != NULL)
1472 xmlAddNextSibling(last, tmp);
1473 else
1474 xmlAddChild(parent, tmp);
1475 }
1476 last = NULL;
1477 parent = tmp;
1478
1479 if (index2 > 1) {
1480 end = xmlXPtrGetNthChild(cur, index2 - 1);
1481 index2 = 0;
1482 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001483 if ((cur == start) && (index1 > 1)) {
1484 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1485 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001486 } else {
1487 cur = cur->children;
1488 }
1489 /*
1490 * Now gather the remaining nodes from cur to end
1491 */
1492 continue; /* while */
1493 }
1494 } else if ((cur == start) &&
1495 (list == NULL) /* looks superfluous but ... */ ) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001496 if ((cur->type == XML_TEXT_NODE) ||
1497 (cur->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001498 const xmlChar *content = cur->content;
1499
1500 if (content == NULL) {
1501 tmp = xmlNewTextLen(NULL, 0);
1502 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001503 if (index1 > 1) {
1504 content += (index1 - 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001505 }
1506 tmp = xmlNewText(content);
1507 }
1508 last = list = tmp;
1509 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001510 if ((cur == start) && (index1 > 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001511 tmp = xmlCopyNode(cur, 0);
1512 list = tmp;
1513 parent = tmp;
1514 last = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001515 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1516 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001517 /*
1518 * Now gather the remaining nodes from cur to end
1519 */
1520 continue; /* while */
1521 }
1522 tmp = xmlCopyNode(cur, 1);
1523 list = tmp;
1524 parent = NULL;
1525 last = tmp;
1526 }
1527 } else {
1528 tmp = NULL;
1529 switch (cur->type) {
1530 case XML_DTD_NODE:
1531 case XML_ELEMENT_DECL:
1532 case XML_ATTRIBUTE_DECL:
1533 case XML_ENTITY_NODE:
1534 /* Do not copy DTD informations */
1535 break;
1536 case XML_ENTITY_DECL:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001537 TODO /* handle crossing entities -> stack needed */
Owen Taylor3473f882001-02-23 17:55:21 +00001538 break;
1539 case XML_XINCLUDE_START:
1540 case XML_XINCLUDE_END:
1541 /* don't consider it part of the tree content */
1542 break;
1543 case XML_ATTRIBUTE_NODE:
1544 /* Humm, should not happen ! */
1545 STRANGE
1546 break;
1547 default:
1548 tmp = xmlCopyNode(cur, 1);
1549 break;
1550 }
1551 if (tmp != NULL) {
1552 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1553 STRANGE
1554 return(NULL);
1555 }
1556 if (last != NULL)
1557 xmlAddNextSibling(last, tmp);
1558 else {
1559 xmlAddChild(parent, tmp);
1560 last = tmp;
1561 }
1562 }
1563 }
1564 /*
1565 * Skip to next node in document order
1566 */
1567 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1568 STRANGE
1569 return(NULL);
1570 }
1571 cur = xmlXPtrAdvanceNode(cur);
1572 }
1573 return(list);
1574}
1575
1576/**
1577 * xmlXPtrBuildNodeList:
1578 * @obj: the XPointer result from the evaluation.
1579 *
1580 * Build a node list tree copy of the XPointer result.
Daniel Veillard39196eb2001-06-19 18:09:42 +00001581 * This will drop Attributes and Namespace declarations.
Owen Taylor3473f882001-02-23 17:55:21 +00001582 *
1583 * Returns an xmlNodePtr list or NULL.
1584 * the caller has to free the node tree.
1585 */
1586xmlNodePtr
1587xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1588 xmlNodePtr list = NULL, last = NULL;
1589 int i;
1590
1591 if (obj == NULL)
1592 return(NULL);
1593 switch (obj->type) {
1594 case XPATH_NODESET: {
1595 xmlNodeSetPtr set = obj->nodesetval;
1596 if (set == NULL)
1597 return(NULL);
1598 for (i = 0;i < set->nodeNr;i++) {
Daniel Veillard39196eb2001-06-19 18:09:42 +00001599 if (set->nodeTab[i] == NULL)
1600 continue;
1601 switch (set->nodeTab[i]->type) {
1602 case XML_TEXT_NODE:
1603 case XML_CDATA_SECTION_NODE:
1604 case XML_ELEMENT_NODE:
1605 case XML_ENTITY_REF_NODE:
1606 case XML_ENTITY_NODE:
1607 case XML_PI_NODE:
1608 case XML_COMMENT_NODE:
1609 case XML_DOCUMENT_NODE:
1610 case XML_HTML_DOCUMENT_NODE:
1611#ifdef LIBXML_DOCB_ENABLED
1612 case XML_DOCB_DOCUMENT_NODE:
1613#endif
1614 case XML_XINCLUDE_START:
1615 case XML_XINCLUDE_END:
1616 break;
1617 case XML_ATTRIBUTE_NODE:
1618 case XML_NAMESPACE_DECL:
1619 case XML_DOCUMENT_TYPE_NODE:
1620 case XML_DOCUMENT_FRAG_NODE:
1621 case XML_NOTATION_NODE:
1622 case XML_DTD_NODE:
1623 case XML_ELEMENT_DECL:
1624 case XML_ATTRIBUTE_DECL:
1625 case XML_ENTITY_DECL:
1626 continue; /* for */
1627 }
Owen Taylor3473f882001-02-23 17:55:21 +00001628 if (last == NULL)
1629 list = last = xmlCopyNode(set->nodeTab[i], 1);
1630 else {
1631 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1632 if (last->next != NULL)
1633 last = last->next;
1634 }
1635 }
1636 break;
1637 }
1638 case XPATH_LOCATIONSET: {
1639 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1640 if (set == NULL)
1641 return(NULL);
1642 for (i = 0;i < set->locNr;i++) {
1643 if (last == NULL)
1644 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1645 else
1646 xmlAddNextSibling(last,
1647 xmlXPtrBuildNodeList(set->locTab[i]));
1648 if (last != NULL) {
1649 while (last->next != NULL)
1650 last = last->next;
1651 }
1652 }
1653 break;
1654 }
1655 case XPATH_RANGE:
1656 return(xmlXPtrBuildRangeNodeList(obj));
1657 case XPATH_POINT:
1658 return(xmlCopyNode(obj->user, 0));
1659 default:
1660 break;
1661 }
1662 return(list);
1663}
1664
1665/************************************************************************
1666 * *
1667 * XPointer functions *
1668 * *
1669 ************************************************************************/
1670
1671/**
1672 * xmlXPtrNbLocChildren:
1673 * @node: an xmlNodePtr
1674 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001675 * Count the number of location children of @node or the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001676 * string value in case of text/PI/Comments nodes
1677 *
1678 * Returns the number of location children
1679 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001680static int
Owen Taylor3473f882001-02-23 17:55:21 +00001681xmlXPtrNbLocChildren(xmlNodePtr node) {
1682 int ret = 0;
1683 if (node == NULL)
1684 return(-1);
1685 switch (node->type) {
1686 case XML_HTML_DOCUMENT_NODE:
1687 case XML_DOCUMENT_NODE:
1688 case XML_ELEMENT_NODE:
1689 node = node->children;
1690 while (node != NULL) {
1691 if (node->type == XML_ELEMENT_NODE)
1692 ret++;
1693 node = node->next;
1694 }
1695 break;
1696 case XML_ATTRIBUTE_NODE:
1697 return(-1);
1698
1699 case XML_PI_NODE:
1700 case XML_COMMENT_NODE:
1701 case XML_TEXT_NODE:
1702 case XML_CDATA_SECTION_NODE:
1703 case XML_ENTITY_REF_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001704 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001705 break;
1706 default:
1707 return(-1);
1708 }
1709 return(ret);
1710}
1711
1712/**
1713 * xmlXPtrHereFunction:
1714 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001715 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001716 *
1717 * Function implementing here() operation
1718 * as described in 5.4.3
1719 */
1720void
1721xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001722 CHECK_ARITY(0);
1723
Owen Taylor3473f882001-02-23 17:55:21 +00001724 if (ctxt->context->here == NULL)
1725 XP_ERROR(XPTR_SYNTAX_ERROR);
1726
1727 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1728}
1729
1730/**
1731 * xmlXPtrOriginFunction:
1732 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001733 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001734 *
1735 * Function implementing origin() operation
1736 * as described in 5.4.3
1737 */
1738void
1739xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001740 CHECK_ARITY(0);
1741
Owen Taylor3473f882001-02-23 17:55:21 +00001742 if (ctxt->context->origin == NULL)
1743 XP_ERROR(XPTR_SYNTAX_ERROR);
1744
1745 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1746}
1747
1748/**
1749 * xmlXPtrStartPointFunction:
1750 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001751 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001752 *
1753 * Function implementing start-point() operation
1754 * as described in 5.4.3
1755 * ----------------
1756 * location-set start-point(location-set)
1757 *
1758 * For each location x in the argument location-set, start-point adds a
1759 * location of type point to the result location-set. That point represents
1760 * the start point of location x and is determined by the following rules:
1761 *
1762 * - If x is of type point, the start point is x.
1763 * - If x is of type range, the start point is the start point of x.
1764 * - If x is of type root, element, text, comment, or processing instruction,
1765 * - the container node of the start point is x and the index is 0.
1766 * - If x is of type attribute or namespace, the function must signal a
1767 * syntax error.
1768 * ----------------
1769 *
1770 */
1771void
1772xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1773 xmlXPathObjectPtr tmp, obj, point;
1774 xmlLocationSetPtr newset = NULL;
1775 xmlLocationSetPtr oldset = NULL;
1776
1777 CHECK_ARITY(1);
1778 if ((ctxt->value == NULL) ||
1779 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1780 (ctxt->value->type != XPATH_NODESET)))
1781 XP_ERROR(XPATH_INVALID_TYPE)
1782
1783 obj = valuePop(ctxt);
1784 if (obj->type == XPATH_NODESET) {
1785 /*
1786 * First convert to a location set
1787 */
1788 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1789 xmlXPathFreeObject(obj);
1790 obj = tmp;
1791 }
1792
1793 newset = xmlXPtrLocationSetCreate(NULL);
1794 if (newset == NULL) {
1795 xmlXPathFreeObject(obj);
1796 XP_ERROR(XPATH_MEMORY_ERROR);
1797 }
1798 oldset = (xmlLocationSetPtr) obj->user;
1799 if (oldset != NULL) {
1800 int i;
1801
1802 for (i = 0; i < oldset->locNr; i++) {
1803 tmp = oldset->locTab[i];
1804 if (tmp == NULL)
1805 continue;
1806 point = NULL;
1807 switch (tmp->type) {
1808 case XPATH_POINT:
1809 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1810 break;
1811 case XPATH_RANGE: {
1812 xmlNodePtr node = tmp->user;
1813 if (node != NULL) {
1814 if (node->type == XML_ATTRIBUTE_NODE) {
1815 /* TODO: Namespace Nodes ??? */
1816 xmlXPathFreeObject(obj);
1817 xmlXPtrFreeLocationSet(newset);
1818 XP_ERROR(XPTR_SYNTAX_ERROR);
1819 }
1820 point = xmlXPtrNewPoint(node, tmp->index);
1821 }
1822 break;
1823 }
1824 default:
1825 /*** Should we raise an error ?
1826 xmlXPathFreeObject(obj);
1827 xmlXPathFreeObject(newset);
1828 XP_ERROR(XPATH_INVALID_TYPE)
1829 ***/
1830 break;
1831 }
1832 if (point != NULL)
1833 xmlXPtrLocationSetAdd(newset, point);
1834 }
1835 }
1836 xmlXPathFreeObject(obj);
1837 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1838}
1839
1840/**
1841 * xmlXPtrEndPointFunction:
1842 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001843 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001844 *
1845 * Function implementing end-point() operation
1846 * as described in 5.4.3
1847 * ----------------------------
1848 * location-set end-point(location-set)
1849 *
1850 * For each location x in the argument location-set, end-point adds a
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001851 * location of type point to the result location-set. That point represents
Owen Taylor3473f882001-02-23 17:55:21 +00001852 * the end point of location x and is determined by the following rules:
1853 *
1854 * - If x is of type point, the resulting point is x.
1855 * - If x is of type range, the resulting point is the end point of x.
1856 * - If x is of type root or element, the container node of the resulting
1857 * point is x and the index is the number of location children of x.
1858 * - If x is of type text, comment, or processing instruction, the container
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001859 * node of the resulting point is x and the index is the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001860 * string-value of x.
1861 * - If x is of type attribute or namespace, the function must signal a
1862 * syntax error.
1863 * ----------------------------
1864 */
1865void
1866xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1867 xmlXPathObjectPtr tmp, obj, point;
1868 xmlLocationSetPtr newset = NULL;
1869 xmlLocationSetPtr oldset = NULL;
1870
1871 CHECK_ARITY(1);
1872 if ((ctxt->value == NULL) ||
1873 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1874 (ctxt->value->type != XPATH_NODESET)))
1875 XP_ERROR(XPATH_INVALID_TYPE)
1876
1877 obj = valuePop(ctxt);
1878 if (obj->type == XPATH_NODESET) {
1879 /*
1880 * First convert to a location set
1881 */
1882 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1883 xmlXPathFreeObject(obj);
1884 obj = tmp;
1885 }
1886
1887 newset = xmlXPtrLocationSetCreate(NULL);
1888 oldset = (xmlLocationSetPtr) obj->user;
1889 if (oldset != NULL) {
1890 int i;
1891
1892 for (i = 0; i < oldset->locNr; i++) {
1893 tmp = oldset->locTab[i];
1894 if (tmp == NULL)
1895 continue;
1896 point = NULL;
1897 switch (tmp->type) {
1898 case XPATH_POINT:
1899 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1900 break;
1901 case XPATH_RANGE: {
1902 xmlNodePtr node = tmp->user2;
1903 if (node != NULL) {
1904 if (node->type == XML_ATTRIBUTE_NODE) {
1905 /* TODO: Namespace Nodes ??? */
1906 xmlXPathFreeObject(obj);
1907 xmlXPtrFreeLocationSet(newset);
1908 XP_ERROR(XPTR_SYNTAX_ERROR);
1909 }
1910 point = xmlXPtrNewPoint(node, tmp->index2);
1911 } else if (tmp->user == NULL) {
1912 point = xmlXPtrNewPoint(node,
1913 xmlXPtrNbLocChildren(node));
1914 }
1915 break;
1916 }
1917 default:
1918 /*** Should we raise an error ?
1919 xmlXPathFreeObject(obj);
1920 xmlXPathFreeObject(newset);
1921 XP_ERROR(XPATH_INVALID_TYPE)
1922 ***/
1923 break;
1924 }
1925 if (point != NULL)
1926 xmlXPtrLocationSetAdd(newset, point);
1927 }
1928 }
1929 xmlXPathFreeObject(obj);
1930 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1931}
1932
1933
1934/**
1935 * xmlXPtrCoveringRange:
1936 * @ctxt: the XPointer Parser context
1937 * @loc: the location for which the covering range must be computed
1938 *
1939 * A covering range is a range that wholly encompasses a location
1940 * Section 5.3.3. Covering Ranges for All Location Types
1941 * http://www.w3.org/TR/xptr#N2267
1942 *
1943 * Returns a new location or NULL in case of error
1944 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001945static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001946xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1947 if (loc == NULL)
1948 return(NULL);
1949 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1950 (ctxt->context->doc == NULL))
1951 return(NULL);
1952 switch (loc->type) {
1953 case XPATH_POINT:
1954 return(xmlXPtrNewRange(loc->user, loc->index,
1955 loc->user, loc->index));
1956 case XPATH_RANGE:
1957 if (loc->user2 != NULL) {
1958 return(xmlXPtrNewRange(loc->user, loc->index,
1959 loc->user2, loc->index2));
1960 } else {
1961 xmlNodePtr node = (xmlNodePtr) loc->user;
1962 if (node == (xmlNodePtr) ctxt->context->doc) {
1963 return(xmlXPtrNewRange(node, 0, node,
1964 xmlXPtrGetArity(node)));
1965 } else {
1966 switch (node->type) {
1967 case XML_ATTRIBUTE_NODE:
1968 /* !!! our model is slightly different than XPath */
1969 return(xmlXPtrNewRange(node, 0, node,
1970 xmlXPtrGetArity(node)));
1971 case XML_ELEMENT_NODE:
1972 case XML_TEXT_NODE:
1973 case XML_CDATA_SECTION_NODE:
1974 case XML_ENTITY_REF_NODE:
1975 case XML_PI_NODE:
1976 case XML_COMMENT_NODE:
1977 case XML_DOCUMENT_NODE:
1978 case XML_NOTATION_NODE:
1979 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001980 int indx = xmlXPtrGetIndex(node);
Owen Taylor3473f882001-02-23 17:55:21 +00001981
1982 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001983 return(xmlXPtrNewRange(node, indx - 1,
1984 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00001985 }
1986 default:
1987 return(NULL);
1988 }
1989 }
1990 }
1991 default:
1992 TODO /* missed one case ??? */
1993 }
1994 return(NULL);
1995}
1996
1997/**
1998 * xmlXPtrRangeFunction:
1999 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002000 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002001 *
2002 * Function implementing the range() function 5.4.3
2003 * location-set range(location-set )
2004 *
2005 * The range function returns ranges covering the locations in
2006 * the argument location-set. For each location x in the argument
2007 * location-set, a range location representing the covering range of
2008 * x is added to the result location-set.
2009 */
2010void
2011xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2012 int i;
2013 xmlXPathObjectPtr set;
2014 xmlLocationSetPtr oldset;
2015 xmlLocationSetPtr newset;
2016
2017 CHECK_ARITY(1);
2018 if ((ctxt->value == NULL) ||
2019 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2020 (ctxt->value->type != XPATH_NODESET)))
2021 XP_ERROR(XPATH_INVALID_TYPE)
2022
2023 set = valuePop(ctxt);
2024 if (set->type == XPATH_NODESET) {
2025 xmlXPathObjectPtr tmp;
2026
2027 /*
2028 * First convert to a location set
2029 */
2030 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2031 xmlXPathFreeObject(set);
2032 set = tmp;
2033 }
2034 oldset = (xmlLocationSetPtr) set->user;
2035
2036 /*
2037 * The loop is to compute the covering range for each item and add it
2038 */
2039 newset = xmlXPtrLocationSetCreate(NULL);
2040 for (i = 0;i < oldset->locNr;i++) {
2041 xmlXPtrLocationSetAdd(newset,
2042 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2043 }
2044
2045 /*
2046 * Save the new value and cleanup
2047 */
2048 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2049 xmlXPathFreeObject(set);
2050}
2051
2052/**
2053 * xmlXPtrInsideRange:
2054 * @ctxt: the XPointer Parser context
2055 * @loc: the location for which the inside range must be computed
2056 *
2057 * A inside range is a range described in the range-inside() description
2058 *
2059 * Returns a new location or NULL in case of error
2060 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002061static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002062xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2063 if (loc == NULL)
2064 return(NULL);
2065 if ((ctxt == NULL) || (ctxt->context == NULL) ||
2066 (ctxt->context->doc == NULL))
2067 return(NULL);
2068 switch (loc->type) {
2069 case XPATH_POINT: {
2070 xmlNodePtr node = (xmlNodePtr) loc->user;
2071 switch (node->type) {
2072 case XML_PI_NODE:
2073 case XML_COMMENT_NODE:
2074 case XML_TEXT_NODE:
2075 case XML_CDATA_SECTION_NODE: {
2076 if (node->content == NULL) {
2077 return(xmlXPtrNewRange(node, 0, node, 0));
2078 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002079 return(xmlXPtrNewRange(node, 0, node,
2080 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002081 }
2082 }
2083 case XML_ATTRIBUTE_NODE:
2084 case XML_ELEMENT_NODE:
2085 case XML_ENTITY_REF_NODE:
2086 case XML_DOCUMENT_NODE:
2087 case XML_NOTATION_NODE:
2088 case XML_HTML_DOCUMENT_NODE: {
2089 return(xmlXPtrNewRange(node, 0, node,
2090 xmlXPtrGetArity(node)));
2091 }
2092 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002093 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002094 }
2095 return(NULL);
2096 }
2097 case XPATH_RANGE: {
2098 xmlNodePtr node = (xmlNodePtr) loc->user;
2099 if (loc->user2 != NULL) {
2100 return(xmlXPtrNewRange(node, loc->index,
2101 loc->user2, loc->index2));
2102 } else {
2103 switch (node->type) {
2104 case XML_PI_NODE:
2105 case XML_COMMENT_NODE:
2106 case XML_TEXT_NODE:
2107 case XML_CDATA_SECTION_NODE: {
2108 if (node->content == NULL) {
2109 return(xmlXPtrNewRange(node, 0, node, 0));
2110 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002111 return(xmlXPtrNewRange(node, 0, node,
2112 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002113 }
2114 }
2115 case XML_ATTRIBUTE_NODE:
2116 case XML_ELEMENT_NODE:
2117 case XML_ENTITY_REF_NODE:
2118 case XML_DOCUMENT_NODE:
2119 case XML_NOTATION_NODE:
2120 case XML_HTML_DOCUMENT_NODE: {
2121 return(xmlXPtrNewRange(node, 0, node,
2122 xmlXPtrGetArity(node)));
2123 }
2124 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002125 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002126 }
2127 return(NULL);
2128 }
2129 }
2130 default:
2131 TODO /* missed one case ??? */
2132 }
2133 return(NULL);
2134}
2135
2136/**
2137 * xmlXPtrRangeInsideFunction:
2138 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002139 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002140 *
2141 * Function implementing the range-inside() function 5.4.3
2142 * location-set range-inside(location-set )
2143 *
2144 * The range-inside function returns ranges covering the contents of
2145 * the locations in the argument location-set. For each location x in
2146 * the argument location-set, a range location is added to the result
2147 * location-set. If x is a range location, then x is added to the
2148 * result location-set. If x is not a range location, then x is used
2149 * as the container location of the start and end points of the range
2150 * location to be added; the index of the start point of the range is
2151 * zero; if the end point is a character point then its index is the
2152 * length of the string-value of x, and otherwise is the number of
2153 * location children of x.
2154 *
2155 */
2156void
2157xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2158 int i;
2159 xmlXPathObjectPtr set;
2160 xmlLocationSetPtr oldset;
2161 xmlLocationSetPtr newset;
2162
2163 CHECK_ARITY(1);
2164 if ((ctxt->value == NULL) ||
2165 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2166 (ctxt->value->type != XPATH_NODESET)))
2167 XP_ERROR(XPATH_INVALID_TYPE)
2168
2169 set = valuePop(ctxt);
2170 if (set->type == XPATH_NODESET) {
2171 xmlXPathObjectPtr tmp;
2172
2173 /*
2174 * First convert to a location set
2175 */
2176 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2177 xmlXPathFreeObject(set);
2178 set = tmp;
2179 }
2180 oldset = (xmlLocationSetPtr) set->user;
2181
2182 /*
2183 * The loop is to compute the covering range for each item and add it
2184 */
2185 newset = xmlXPtrLocationSetCreate(NULL);
2186 for (i = 0;i < oldset->locNr;i++) {
2187 xmlXPtrLocationSetAdd(newset,
2188 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2189 }
2190
2191 /*
2192 * Save the new value and cleanup
2193 */
2194 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2195 xmlXPathFreeObject(set);
2196}
2197
2198/**
2199 * xmlXPtrRangeToFunction:
2200 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002201 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002202 *
2203 * Implement the range-to() XPointer function
2204 */
2205void
2206xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2207 xmlXPathObjectPtr range;
2208 const xmlChar *cur;
2209 xmlXPathObjectPtr res, obj;
2210 xmlXPathObjectPtr tmp;
2211 xmlLocationSetPtr newset = NULL;
2212 xmlNodeSetPtr oldset;
2213 int i;
2214
2215 CHECK_ARITY(1);
2216 /*
2217 * Save the expression pointer since we will have to evaluate
2218 * it multiple times. Initialize the new set.
2219 */
2220 CHECK_TYPE(XPATH_NODESET);
2221 obj = valuePop(ctxt);
2222 oldset = obj->nodesetval;
2223 ctxt->context->node = NULL;
2224
2225 cur = ctxt->cur;
2226 newset = xmlXPtrLocationSetCreate(NULL);
2227
2228 for (i = 0; i < oldset->nodeNr; i++) {
2229 ctxt->cur = cur;
2230
2231 /*
2232 * Run the evaluation with a node list made of a single item
2233 * in the nodeset.
2234 */
2235 ctxt->context->node = oldset->nodeTab[i];
2236 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2237 valuePush(ctxt, tmp);
2238
2239 xmlXPathEvalExpr(ctxt);
2240 CHECK_ERROR;
2241
2242 /*
2243 * The result of the evaluation need to be tested to
2244 * decided whether the filter succeeded or not
2245 */
2246 res = valuePop(ctxt);
2247 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2248 if (range != NULL) {
2249 xmlXPtrLocationSetAdd(newset, range);
2250 }
2251
2252 /*
2253 * Cleanup
2254 */
2255 if (res != NULL)
2256 xmlXPathFreeObject(res);
2257 if (ctxt->value == tmp) {
2258 res = valuePop(ctxt);
2259 xmlXPathFreeObject(res);
2260 }
2261
2262 ctxt->context->node = NULL;
2263 }
2264
2265 /*
2266 * The result is used as the new evaluation set.
2267 */
2268 xmlXPathFreeObject(obj);
2269 ctxt->context->node = NULL;
2270 ctxt->context->contextSize = -1;
2271 ctxt->context->proximityPosition = -1;
2272 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2273}
2274
2275/**
2276 * xmlXPtrAdvanceNode:
2277 * @cur: the node
2278 *
2279 * Advance to the next element or text node in document order
2280 * TODO: add a stack for entering/exiting entities
2281 *
2282 * Returns -1 in case of failure, 0 otherwise
2283 */
2284xmlNodePtr
2285xmlXPtrAdvanceNode(xmlNodePtr cur) {
2286next:
2287 if (cur == NULL)
2288 return(NULL);
2289 if (cur->children != NULL) {
2290 cur = cur->children ;
2291 goto found;
2292 }
2293 if (cur->next != NULL) {
2294 cur = cur->next;
2295 goto found;
2296 }
2297 do {
2298 cur = cur->parent;
2299 if (cur == NULL) return(NULL);
2300 if (cur->next != NULL) {
2301 cur = cur->next;
2302 goto found;
2303 }
2304 } while (cur != NULL);
2305
2306found:
2307 if ((cur->type != XML_ELEMENT_NODE) &&
2308 (cur->type != XML_TEXT_NODE) &&
2309 (cur->type != XML_DOCUMENT_NODE) &&
2310 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2311 (cur->type != XML_CDATA_SECTION_NODE))
2312 goto next;
2313 if (cur->type == XML_ENTITY_REF_NODE) {
2314 TODO
2315 }
2316 return(cur);
2317}
2318
2319/**
2320 * xmlXPtrAdvanceChar:
2321 * @node: the node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002322 * @indx: the indx
Owen Taylor3473f882001-02-23 17:55:21 +00002323 * @bytes: the number of bytes
2324 *
2325 * Advance a point of the associated number of bytes (not UTF8 chars)
2326 *
2327 * Returns -1 in case of failure, 0 otherwise
2328 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002329static int
2330xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Owen Taylor3473f882001-02-23 17:55:21 +00002331 xmlNodePtr cur;
2332 int pos;
2333 int len;
2334
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002335 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002336 return(-1);
2337 cur = *node;
2338 if (cur == NULL)
2339 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002340 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002341
2342 while (bytes >= 0) {
2343 /*
2344 * First position to the beginning of the first text node
2345 * corresponding to this point
2346 */
2347 while ((cur != NULL) &&
2348 ((cur->type == XML_ELEMENT_NODE) ||
2349 (cur->type == XML_DOCUMENT_NODE) ||
2350 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2351 if (pos > 0) {
2352 cur = xmlXPtrGetNthChild(cur, pos);
2353 pos = 0;
2354 } else {
2355 cur = xmlXPtrAdvanceNode(cur);
2356 pos = 0;
2357 }
2358 }
2359
2360 if (cur == NULL) {
2361 *node = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002362 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002363 return(-1);
2364 }
2365
2366 /*
2367 * if there is no move needed return the current value.
2368 */
2369 if (pos == 0) pos = 1;
2370 if (bytes == 0) {
2371 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002372 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002373 return(0);
2374 }
2375 /*
2376 * We should have a text (or cdata) node ...
2377 */
2378 len = 0;
Daniel Veillard7db37732001-07-12 01:20:08 +00002379 if ((cur->type != XML_ELEMENT_NODE) &&
2380 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002381 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002382 }
2383 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002384 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002385 STRANGE
2386 pos = len;
2387 }
2388 if (pos + bytes >= len) {
2389 bytes -= (len - pos);
2390 cur = xmlXPtrAdvanceNode(cur);
2391 cur = 0;
2392 } else if (pos + bytes < len) {
2393 pos += bytes;
2394 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002395 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002396 return(0);
2397 }
2398 }
2399 return(-1);
2400}
2401
2402/**
2403 * xmlXPtrMatchString:
2404 * @string: the string to search
2405 * @start: the start textnode
2406 * @startindex: the start index
2407 * @end: the end textnode IN/OUT
2408 * @endindex: the end index IN/OUT
2409 *
2410 * Check whether the document contains @string at the position
2411 * (@start, @startindex) and limited by the (@end, @endindex) point
2412 *
2413 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2414 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002415 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002416 * of the range
2417 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002418static int
Owen Taylor3473f882001-02-23 17:55:21 +00002419xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2420 xmlNodePtr *end, int *endindex) {
2421 xmlNodePtr cur;
2422 int pos; /* 0 based */
2423 int len; /* in bytes */
2424 int stringlen; /* in bytes */
2425 int match;
2426
2427 if (string == NULL)
2428 return(-1);
2429 if (start == NULL)
2430 return(-1);
2431 if ((end == NULL) || (endindex == NULL))
2432 return(-1);
2433 cur = start;
2434 if (cur == NULL)
2435 return(-1);
2436 pos = startindex - 1;
2437 stringlen = xmlStrlen(string);
2438
2439 while (stringlen > 0) {
2440 if ((cur == *end) && (pos + stringlen > *endindex))
2441 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002442
2443 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002444 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002445 if (len >= pos + stringlen) {
Owen Taylor3473f882001-02-23 17:55:21 +00002446 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
Owen Taylor3473f882001-02-23 17:55:21 +00002447 if (match) {
2448#ifdef DEBUG_RANGES
2449 xmlGenericError(xmlGenericErrorContext,
2450 "found range %d bytes at index %d of ->",
2451 stringlen, pos + 1);
2452 xmlDebugDumpString(stdout, cur->content);
2453 xmlGenericError(xmlGenericErrorContext, "\n");
2454#endif
2455 *end = cur;
2456 *endindex = pos + stringlen;
2457 return(1);
2458 } else {
2459 return(0);
2460 }
2461 } else {
2462 int sub = len - pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002463 match = (!xmlStrncmp(&cur->content[pos], string, sub));
Owen Taylor3473f882001-02-23 17:55:21 +00002464 if (match) {
2465#ifdef DEBUG_RANGES
2466 xmlGenericError(xmlGenericErrorContext,
2467 "found subrange %d bytes at index %d of ->",
2468 sub, pos + 1);
2469 xmlDebugDumpString(stdout, cur->content);
2470 xmlGenericError(xmlGenericErrorContext, "\n");
2471#endif
2472 string = &string[sub];
2473 stringlen -= sub;
2474 } else {
2475 return(0);
2476 }
2477 }
2478 }
2479 cur = xmlXPtrAdvanceNode(cur);
2480 if (cur == NULL)
2481 return(0);
2482 pos = 0;
2483 }
2484 return(1);
2485}
2486
2487/**
2488 * xmlXPtrSearchString:
2489 * @string: the string to search
2490 * @start: the start textnode IN/OUT
2491 * @startindex: the start index IN/OUT
2492 * @end: the end textnode
2493 * @endindex: the end index
2494 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002495 * Search the next occurrence of @string within the document content
Owen Taylor3473f882001-02-23 17:55:21 +00002496 * until the (@end, @endindex) point is reached
2497 *
2498 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2499 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002500 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002501 * of the range
2502 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002503static int
Owen Taylor3473f882001-02-23 17:55:21 +00002504xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2505 xmlNodePtr *end, int *endindex) {
2506 xmlNodePtr cur;
2507 const xmlChar *str;
2508 int pos; /* 0 based */
2509 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002510 xmlChar first;
2511
2512 if (string == NULL)
2513 return(-1);
2514 if ((start == NULL) || (startindex == NULL))
2515 return(-1);
2516 if ((end == NULL) || (endindex == NULL))
2517 return(-1);
2518 cur = *start;
2519 if (cur == NULL)
2520 return(-1);
2521 pos = *startindex - 1;
2522 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002523
2524 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002525 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002526 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002527 while (pos <= len) {
2528 if (first != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00002529 str = xmlStrchr(&cur->content[pos], first);
Owen Taylor3473f882001-02-23 17:55:21 +00002530 if (str != NULL) {
2531 pos = (str - (xmlChar *)(cur->content));
2532#ifdef DEBUG_RANGES
2533 xmlGenericError(xmlGenericErrorContext,
2534 "found '%c' at index %d of ->",
2535 first, pos + 1);
2536 xmlDebugDumpString(stdout, cur->content);
2537 xmlGenericError(xmlGenericErrorContext, "\n");
2538#endif
2539 if (xmlXPtrMatchString(string, cur, pos + 1,
2540 end, endindex)) {
2541 *start = cur;
2542 *startindex = pos + 1;
2543 return(1);
2544 }
2545 pos++;
2546 } else {
2547 pos = len + 1;
2548 }
2549 } else {
2550 /*
2551 * An empty string is considered to match before each
2552 * character of the string-value and after the final
2553 * character.
2554 */
2555#ifdef DEBUG_RANGES
2556 xmlGenericError(xmlGenericErrorContext,
2557 "found '' at index %d of ->",
2558 pos + 1);
2559 xmlDebugDumpString(stdout, cur->content);
2560 xmlGenericError(xmlGenericErrorContext, "\n");
2561#endif
2562 *start = cur;
2563 *startindex = pos + 1;
2564 *end = cur;
2565 *endindex = pos + 1;
2566 return(1);
2567 }
2568 }
2569 }
2570 if ((cur == *end) && (pos >= *endindex))
2571 return(0);
2572 cur = xmlXPtrAdvanceNode(cur);
2573 if (cur == NULL)
2574 return(0);
2575 pos = 1;
2576 }
2577 return(0);
2578}
2579
2580/**
2581 * xmlXPtrGetLastChar:
2582 * @node: the node
2583 * @index: the index
2584 *
2585 * Computes the point coordinates of the last char of this point
2586 *
2587 * Returns -1 in case of failure, 0 otherwise
2588 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002589static int
2590xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002591 xmlNodePtr cur;
2592 int pos, len = 0;
2593
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002594 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002595 return(-1);
2596 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002597 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002598
2599 if (cur == NULL)
2600 return(-1);
2601
2602 if ((cur->type == XML_ELEMENT_NODE) ||
2603 (cur->type == XML_DOCUMENT_NODE) ||
2604 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2605 if (pos > 0) {
2606 cur = xmlXPtrGetNthChild(cur, pos);
2607 pos = 0;
2608 }
2609 }
2610 while (cur != NULL) {
2611 if (cur->last != NULL)
2612 cur = cur->last;
Daniel Veillard7db37732001-07-12 01:20:08 +00002613 else if ((cur->type != XML_ELEMENT_NODE) &&
2614 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002615 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002616 break;
2617 } else {
2618 return(-1);
2619 }
2620 }
2621 if (cur == NULL)
2622 return(-1);
2623 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002624 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002625 return(0);
2626}
2627
2628/**
2629 * xmlXPtrGetStartPoint:
2630 * @obj: an range
2631 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002632 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002633 *
2634 * read the object and return the start point coordinates.
2635 *
2636 * Returns -1 in case of failure, 0 otherwise
2637 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002638static int
2639xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2640 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002641 return(-1);
2642
2643 switch (obj->type) {
2644 case XPATH_POINT:
2645 *node = obj->user;
2646 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002647 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002648 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002649 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002650 return(0);
2651 case XPATH_RANGE:
2652 *node = obj->user;
2653 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002654 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002655 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002656 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002657 return(0);
2658 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002659 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002660 }
2661 return(-1);
2662}
2663
2664/**
2665 * xmlXPtrGetEndPoint:
2666 * @obj: an range
2667 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002668 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002669 *
2670 * read the object and return the end point coordinates.
2671 *
2672 * Returns -1 in case of failure, 0 otherwise
2673 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002674static int
2675xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2676 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002677 return(-1);
2678
2679 switch (obj->type) {
2680 case XPATH_POINT:
2681 *node = obj->user;
2682 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002683 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002684 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002685 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002686 return(0);
2687 case XPATH_RANGE:
2688 *node = obj->user;
2689 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002690 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002691 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002692 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002693 return(0);
2694 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002695 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002696 }
2697 return(-1);
2698}
2699
2700/**
2701 * xmlXPtrStringRangeFunction:
2702 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002703 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002704 *
2705 * Function implementing the string-range() function
2706 * range as described in 5.4.2
2707 *
2708 * ------------------------------
2709 * [Definition: For each location in the location-set argument,
2710 * string-range returns a set of string ranges, a set of substrings in a
2711 * string. Specifically, the string-value of the location is searched for
2712 * substrings that match the string argument, and the resulting location-set
2713 * will contain a range location for each non-overlapping match.]
2714 * An empty string is considered to match before each character of the
2715 * string-value and after the final character. Whitespace in a string
2716 * is matched literally, with no normalization except that provided by
2717 * XML for line ends. The third argument gives the position of the first
2718 * character to be in the resulting range, relative to the start of the
2719 * match. The default value is 1, which makes the range start immediately
2720 * before the first character of the matched string. The fourth argument
2721 * gives the number of characters in the range; the default is that the
2722 * range extends to the end of the matched string.
2723 *
2724 * Element boundaries, as well as entire embedded nodes such as processing
2725 * instructions and comments, are ignored as defined in [XPath].
2726 *
2727 * If the string in the second argument is not found in the string-value
2728 * of the location, or if a value in the third or fourth argument indicates
2729 * a string that is beyond the beginning or end of the document, the
2730 * expression fails.
2731 *
2732 * The points of the range-locations in the returned location-set will
2733 * all be character points.
2734 * ------------------------------
2735 */
2736void
2737xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2738 int i, startindex, endindex, fendindex;
2739 xmlNodePtr start, end, fend;
2740 xmlXPathObjectPtr set;
2741 xmlLocationSetPtr oldset;
2742 xmlLocationSetPtr newset;
2743 xmlXPathObjectPtr string;
2744 xmlXPathObjectPtr position = NULL;
2745 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002746 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002747
2748 /*
2749 * Grab the arguments
2750 */
2751 if ((nargs < 2) || (nargs > 4))
2752 XP_ERROR(XPATH_INVALID_ARITY);
2753
2754 if (nargs >= 4) {
2755 CHECK_TYPE(XPATH_NUMBER);
2756 number = valuePop(ctxt);
2757 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002758 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002759 }
2760 if (nargs >= 3) {
2761 CHECK_TYPE(XPATH_NUMBER);
2762 position = valuePop(ctxt);
2763 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002764 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002765 }
2766 CHECK_TYPE(XPATH_STRING);
2767 string = valuePop(ctxt);
2768 if ((ctxt->value == NULL) ||
2769 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2770 (ctxt->value->type != XPATH_NODESET)))
2771 XP_ERROR(XPATH_INVALID_TYPE)
2772
2773 set = valuePop(ctxt);
2774 if (set->type == XPATH_NODESET) {
2775 xmlXPathObjectPtr tmp;
2776
2777 /*
2778 * First convert to a location set
2779 */
2780 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2781 xmlXPathFreeObject(set);
2782 set = tmp;
2783 }
2784 oldset = (xmlLocationSetPtr) set->user;
2785
2786 /*
2787 * The loop is to search for each element in the location set
2788 * the list of location set corresponding to that search
2789 */
2790 newset = xmlXPtrLocationSetCreate(NULL);
2791 for (i = 0;i < oldset->locNr;i++) {
2792#ifdef DEBUG_RANGES
2793 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2794#endif
2795
2796 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2797 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2798 xmlXPtrAdvanceChar(&start, &startindex, 0);
2799 xmlXPtrGetLastChar(&end, &endindex);
2800
2801#ifdef DEBUG_RANGES
2802 xmlGenericError(xmlGenericErrorContext,
2803 "from index %d of ->", startindex);
2804 xmlDebugDumpString(stdout, start->content);
2805 xmlGenericError(xmlGenericErrorContext, "\n");
2806 xmlGenericError(xmlGenericErrorContext,
2807 "to index %d of ->", endindex);
2808 xmlDebugDumpString(stdout, end->content);
2809 xmlGenericError(xmlGenericErrorContext, "\n");
2810#endif
2811 do {
2812 fend = end;
2813 fendindex = endindex;
2814 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2815 &fend, &fendindex);
2816 if (found == 1) {
2817 if (position == NULL) {
2818 xmlXPtrLocationSetAdd(newset,
2819 xmlXPtrNewRange(start, startindex, fend, fendindex));
2820 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2821 pos - 1) == 0) {
2822 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002823 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002824 xmlNodePtr rend;
2825 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002826 rindx = startindex - 1;
2827 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002828 num) == 0) {
2829 xmlXPtrLocationSetAdd(newset,
2830 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002831 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002832 }
2833 } else if ((number != NULL) && (num <= 0)) {
2834 xmlXPtrLocationSetAdd(newset,
2835 xmlXPtrNewRange(start, startindex,
2836 start, startindex));
2837 } else {
2838 xmlXPtrLocationSetAdd(newset,
2839 xmlXPtrNewRange(start, startindex,
2840 fend, fendindex));
2841 }
2842 }
2843 start = fend;
2844 startindex = fendindex;
2845 if (string->stringval[0] == 0)
2846 startindex++;
2847 }
2848 } while (found == 1);
2849 }
2850
2851 /*
2852 * Save the new value and cleanup
2853 */
2854 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2855 xmlXPathFreeObject(set);
2856 xmlXPathFreeObject(string);
2857 if (position) xmlXPathFreeObject(position);
2858 if (number) xmlXPathFreeObject(number);
2859}
2860
2861/**
2862 * xmlXPtrEvalRangePredicate:
2863 * @ctxt: the XPointer Parser context
2864 *
2865 * [8] Predicate ::= '[' PredicateExpr ']'
2866 * [9] PredicateExpr ::= Expr
2867 *
2868 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2869 * a Location Set instead of a node set
2870 */
2871void
2872xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2873 const xmlChar *cur;
2874 xmlXPathObjectPtr res;
2875 xmlXPathObjectPtr obj, tmp;
2876 xmlLocationSetPtr newset = NULL;
2877 xmlLocationSetPtr oldset;
2878 int i;
2879
2880 SKIP_BLANKS;
2881 if (CUR != '[') {
2882 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2883 }
2884 NEXT;
2885 SKIP_BLANKS;
2886
2887 /*
2888 * Extract the old set, and then evaluate the result of the
2889 * expression for all the element in the set. use it to grow
2890 * up a new set.
2891 */
2892 CHECK_TYPE(XPATH_LOCATIONSET);
2893 obj = valuePop(ctxt);
2894 oldset = obj->user;
2895 ctxt->context->node = NULL;
2896
2897 if ((oldset == NULL) || (oldset->locNr == 0)) {
2898 ctxt->context->contextSize = 0;
2899 ctxt->context->proximityPosition = 0;
2900 xmlXPathEvalExpr(ctxt);
2901 res = valuePop(ctxt);
2902 if (res != NULL)
2903 xmlXPathFreeObject(res);
2904 valuePush(ctxt, obj);
2905 CHECK_ERROR;
2906 } else {
2907 /*
2908 * Save the expression pointer since we will have to evaluate
2909 * it multiple times. Initialize the new set.
2910 */
2911 cur = ctxt->cur;
2912 newset = xmlXPtrLocationSetCreate(NULL);
2913
2914 for (i = 0; i < oldset->locNr; i++) {
2915 ctxt->cur = cur;
2916
2917 /*
2918 * Run the evaluation with a node list made of a single item
2919 * in the nodeset.
2920 */
2921 ctxt->context->node = oldset->locTab[i]->user;
2922 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2923 valuePush(ctxt, tmp);
2924 ctxt->context->contextSize = oldset->locNr;
2925 ctxt->context->proximityPosition = i + 1;
2926
2927 xmlXPathEvalExpr(ctxt);
2928 CHECK_ERROR;
2929
2930 /*
2931 * The result of the evaluation need to be tested to
2932 * decided whether the filter succeeded or not
2933 */
2934 res = valuePop(ctxt);
2935 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2936 xmlXPtrLocationSetAdd(newset,
2937 xmlXPathObjectCopy(oldset->locTab[i]));
2938 }
2939
2940 /*
2941 * Cleanup
2942 */
2943 if (res != NULL)
2944 xmlXPathFreeObject(res);
2945 if (ctxt->value == tmp) {
2946 res = valuePop(ctxt);
2947 xmlXPathFreeObject(res);
2948 }
2949
2950 ctxt->context->node = NULL;
2951 }
2952
2953 /*
2954 * The result is used as the new evaluation set.
2955 */
2956 xmlXPathFreeObject(obj);
2957 ctxt->context->node = NULL;
2958 ctxt->context->contextSize = -1;
2959 ctxt->context->proximityPosition = -1;
2960 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2961 }
2962 if (CUR != ']') {
2963 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2964 }
2965
2966 NEXT;
2967 SKIP_BLANKS;
2968}
2969
2970#else
2971#endif
2972