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