blob: e86b445cb7b07cc06c209fe7f58985773bfc1779 [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
Daniel Veillardf8e3db02012-09-11 13:26:36 +080010 * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
Daniel Veillard9a237c92003-02-13 15:52:58 +000011 *
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
Daniel Veillardf8e3db02012-09-11 13:26:36 +080050#define TODO \
Owen Taylor3473f882001-02-23 17:55:21 +000051 xmlGenericError(xmlGenericErrorContext, \
52 "Unimplemented block at %s:%d\n", \
53 __FILE__, __LINE__);
54
Daniel Veillardf8e3db02012-09-11 13:26:36 +080055#define STRANGE \
Owen Taylor3473f882001-02-23 17:55:21 +000056 xmlGenericError(xmlGenericErrorContext, \
57 "Internal error at %s:%d\n", \
58 __FILE__, __LINE__);
59
60/************************************************************************
61 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +080062 * Some factorized error routines *
Daniel Veillardfcf719c2003-10-10 11:42:17 +000063 * *
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 */
Xin Li28c53d32017-03-07 00:33:02 +000088static void LIBXML_ATTR_FORMAT(3,0)
Daniel Veillardfcf719c2003-10-10 11:42:17 +000089xmlXPtrErr(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 ************************************************************************/
William M. Brackf7eb7942003-12-31 07:59:17 +0000126/* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
127xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
Owen Taylor3473f882001-02-23 17:55:21 +0000128/**
129 * xmlXPtrGetArity:
130 * @cur: the node
131 *
132 * Returns the number of child for an element, -1 in case of error
133 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000134static int
Owen Taylor3473f882001-02-23 17:55:21 +0000135xmlXPtrGetArity(xmlNodePtr cur) {
136 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800137 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +0000138 return(-1);
139 cur = cur->children;
140 for (i = 0;cur != NULL;cur = cur->next) {
141 if ((cur->type == XML_ELEMENT_NODE) ||
142 (cur->type == XML_DOCUMENT_NODE) ||
143 (cur->type == XML_HTML_DOCUMENT_NODE)) {
144 i++;
145 }
146 }
147 return(i);
148}
149
150/**
151 * xmlXPtrGetIndex:
152 * @cur: the node
153 *
154 * Returns the index of the node in its parent children list, -1
155 * in case of error
156 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000157static int
Owen Taylor3473f882001-02-23 17:55:21 +0000158xmlXPtrGetIndex(xmlNodePtr cur) {
159 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800160 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +0000161 return(-1);
162 for (i = 1;cur != NULL;cur = cur->prev) {
163 if ((cur->type == XML_ELEMENT_NODE) ||
164 (cur->type == XML_DOCUMENT_NODE) ||
165 (cur->type == XML_HTML_DOCUMENT_NODE)) {
166 i++;
167 }
168 }
169 return(i);
170}
171
172/**
173 * xmlXPtrGetNthChild:
174 * @cur: the node
175 * @no: the child number
176 *
177 * Returns the @no'th element child of @cur or NULL
178 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000179static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000180xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
181 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800182 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +0000183 return(cur);
184 cur = cur->children;
185 for (i = 0;i <= no;cur = cur->next) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800186 if (cur == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000187 return(cur);
188 if ((cur->type == XML_ELEMENT_NODE) ||
189 (cur->type == XML_DOCUMENT_NODE) ||
190 (cur->type == XML_HTML_DOCUMENT_NODE)) {
191 i++;
192 if (i == no)
193 break;
194 }
195 }
196 return(cur);
197}
198
199/************************************************************************
200 * *
201 * Handling of XPointer specific types *
202 * *
203 ************************************************************************/
204
205/**
206 * xmlXPtrCmpPoints:
207 * @node1: the first node
208 * @index1: the first index
209 * @node2: the second node
210 * @index2: the second index
211 *
212 * Compare two points w.r.t document order
213 *
214 * Returns -2 in case of error 1 if first point < second point, 0 if
215 * that's the same point, -1 otherwise
216 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000217static int
Owen Taylor3473f882001-02-23 17:55:21 +0000218xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
219 if ((node1 == NULL) || (node2 == NULL))
220 return(-2);
221 /*
222 * a couple of optimizations which will avoid computations in most cases
223 */
224 if (node1 == node2) {
225 if (index1 < index2)
226 return(1);
227 if (index1 > index2)
228 return(-1);
229 return(0);
230 }
231 return(xmlXPathCmpNodes(node1, node2));
232}
233
234/**
235 * xmlXPtrNewPoint:
236 * @node: the xmlNodePtr
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000237 * @indx: the indx within the node
Owen Taylor3473f882001-02-23 17:55:21 +0000238 *
239 * Create a new xmlXPathObjectPtr of type point
240 *
241 * Returns the newly created object.
242 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000243static xmlXPathObjectPtr
244xmlXPtrNewPoint(xmlNodePtr node, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000245 xmlXPathObjectPtr ret;
246
247 if (node == NULL)
248 return(NULL);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000249 if (indx < 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000250 return(NULL);
251
252 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
253 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000254 xmlXPtrErrMemory("allocating point");
Owen Taylor3473f882001-02-23 17:55:21 +0000255 return(NULL);
256 }
257 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
258 ret->type = XPATH_POINT;
259 ret->user = (void *) node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000260 ret->index = indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000261 return(ret);
262}
263
264/**
265 * xmlXPtrRangeCheckOrder:
266 * @range: an object range
267 *
268 * Make sure the points in the range are in the right order
269 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000270static void
Owen Taylor3473f882001-02-23 17:55:21 +0000271xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
272 int tmp;
273 xmlNodePtr tmp2;
274 if (range == NULL)
275 return;
276 if (range->type != XPATH_RANGE)
277 return;
278 if (range->user2 == NULL)
279 return;
280 tmp = xmlXPtrCmpPoints(range->user, range->index,
281 range->user2, range->index2);
282 if (tmp == -1) {
283 tmp2 = range->user;
284 range->user = range->user2;
285 range->user2 = tmp2;
286 tmp = range->index;
287 range->index = range->index2;
288 range->index2 = tmp;
289 }
290}
291
292/**
293 * xmlXPtrRangesEqual:
294 * @range1: the first range
295 * @range2: the second range
296 *
297 * Compare two ranges
298 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000299 * Returns 1 if equal, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000300 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000301static int
Owen Taylor3473f882001-02-23 17:55:21 +0000302xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
303 if (range1 == range2)
304 return(1);
305 if ((range1 == NULL) || (range2 == NULL))
306 return(0);
307 if (range1->type != range2->type)
308 return(0);
309 if (range1->type != XPATH_RANGE)
310 return(0);
311 if (range1->user != range2->user)
312 return(0);
313 if (range1->index != range2->index)
314 return(0);
315 if (range1->user2 != range2->user2)
316 return(0);
317 if (range1->index2 != range2->index2)
318 return(0);
319 return(1);
320}
321
322/**
Brian C. Young48a151c2017-04-03 12:39:04 -0700323 * xmlXPtrNewRangeInternal:
324 * @start: the starting node
325 * @startindex: the start index
326 * @end: the ending point
327 * @endindex: the ending index
328 *
329 * Internal function to create a new xmlXPathObjectPtr of type range
330 *
331 * Returns the newly created object.
332 */
333static xmlXPathObjectPtr
334xmlXPtrNewRangeInternal(xmlNodePtr start, int startindex,
335 xmlNodePtr end, int endindex) {
336 xmlXPathObjectPtr ret;
337
338 /*
339 * Namespace nodes must be copied (see xmlXPathNodeSetDupNs).
340 * Disallow them for now.
341 */
342 if ((start != NULL) && (start->type == XML_NAMESPACE_DECL))
343 return(NULL);
344 if ((end != NULL) && (end->type == XML_NAMESPACE_DECL))
345 return(NULL);
346
347 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
348 if (ret == NULL) {
349 xmlXPtrErrMemory("allocating range");
350 return(NULL);
351 }
352 memset(ret, 0, sizeof(xmlXPathObject));
353 ret->type = XPATH_RANGE;
354 ret->user = start;
355 ret->index = startindex;
356 ret->user2 = end;
357 ret->index2 = endindex;
358 return(ret);
359}
360
361/**
Owen Taylor3473f882001-02-23 17:55:21 +0000362 * xmlXPtrNewRange:
363 * @start: the starting node
364 * @startindex: the start index
365 * @end: the ending point
366 * @endindex: the ending index
367 *
368 * Create a new xmlXPathObjectPtr of type range
369 *
370 * Returns the newly created object.
371 */
372xmlXPathObjectPtr
373xmlXPtrNewRange(xmlNodePtr start, int startindex,
374 xmlNodePtr end, int endindex) {
375 xmlXPathObjectPtr ret;
376
377 if (start == NULL)
378 return(NULL);
379 if (end == NULL)
380 return(NULL);
381 if (startindex < 0)
382 return(NULL);
383 if (endindex < 0)
384 return(NULL);
385
Brian C. Young48a151c2017-04-03 12:39:04 -0700386 ret = xmlXPtrNewRangeInternal(start, startindex, end, endindex);
Owen Taylor3473f882001-02-23 17:55:21 +0000387 xmlXPtrRangeCheckOrder(ret);
388 return(ret);
389}
390
391/**
392 * xmlXPtrNewRangePoints:
393 * @start: the starting point
394 * @end: the ending point
395 *
396 * Create a new xmlXPathObjectPtr of type range using 2 Points
397 *
398 * Returns the newly created object.
399 */
400xmlXPathObjectPtr
401xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
402 xmlXPathObjectPtr ret;
403
404 if (start == NULL)
405 return(NULL);
406 if (end == NULL)
407 return(NULL);
408 if (start->type != XPATH_POINT)
409 return(NULL);
410 if (end->type != XPATH_POINT)
411 return(NULL);
412
Brian C. Young48a151c2017-04-03 12:39:04 -0700413 ret = xmlXPtrNewRangeInternal(start->user, start->index, end->user,
414 end->index);
Owen Taylor3473f882001-02-23 17:55:21 +0000415 xmlXPtrRangeCheckOrder(ret);
416 return(ret);
417}
418
419/**
420 * xmlXPtrNewRangePointNode:
421 * @start: the starting point
422 * @end: the ending node
423 *
424 * Create a new xmlXPathObjectPtr of type range from a point to a node
425 *
426 * Returns the newly created object.
427 */
428xmlXPathObjectPtr
429xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
430 xmlXPathObjectPtr ret;
431
432 if (start == NULL)
433 return(NULL);
434 if (end == NULL)
435 return(NULL);
436 if (start->type != XPATH_POINT)
437 return(NULL);
438
Brian C. Young48a151c2017-04-03 12:39:04 -0700439 ret = xmlXPtrNewRangeInternal(start->user, start->index, end, -1);
Owen Taylor3473f882001-02-23 17:55:21 +0000440 xmlXPtrRangeCheckOrder(ret);
441 return(ret);
442}
443
444/**
445 * xmlXPtrNewRangeNodePoint:
446 * @start: the starting node
447 * @end: the ending point
448 *
449 * Create a new xmlXPathObjectPtr of type range from a node to a point
450 *
451 * Returns the newly created object.
452 */
453xmlXPathObjectPtr
454xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
455 xmlXPathObjectPtr ret;
456
457 if (start == NULL)
458 return(NULL);
459 if (end == NULL)
460 return(NULL);
461 if (start->type != XPATH_POINT)
462 return(NULL);
463 if (end->type != XPATH_POINT)
464 return(NULL);
465
Brian C. Young48a151c2017-04-03 12:39:04 -0700466 ret = xmlXPtrNewRangeInternal(start, -1, end->user, end->index);
Owen Taylor3473f882001-02-23 17:55:21 +0000467 xmlXPtrRangeCheckOrder(ret);
468 return(ret);
469}
470
471/**
472 * xmlXPtrNewRangeNodes:
473 * @start: the starting node
474 * @end: the ending node
475 *
476 * Create a new xmlXPathObjectPtr of type range using 2 nodes
477 *
478 * Returns the newly created object.
479 */
480xmlXPathObjectPtr
481xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
482 xmlXPathObjectPtr ret;
483
484 if (start == NULL)
485 return(NULL);
486 if (end == NULL)
487 return(NULL);
488
Brian C. Young48a151c2017-04-03 12:39:04 -0700489 ret = xmlXPtrNewRangeInternal(start, -1, end, -1);
Owen Taylor3473f882001-02-23 17:55:21 +0000490 xmlXPtrRangeCheckOrder(ret);
491 return(ret);
492}
493
494/**
495 * xmlXPtrNewCollapsedRange:
496 * @start: the starting and ending node
497 *
498 * Create a new xmlXPathObjectPtr of type range using a single nodes
499 *
500 * Returns the newly created object.
501 */
502xmlXPathObjectPtr
503xmlXPtrNewCollapsedRange(xmlNodePtr start) {
504 xmlXPathObjectPtr ret;
505
506 if (start == NULL)
507 return(NULL);
508
Brian C. Young48a151c2017-04-03 12:39:04 -0700509 ret = xmlXPtrNewRangeInternal(start, -1, NULL, -1);
Owen Taylor3473f882001-02-23 17:55:21 +0000510 return(ret);
511}
512
513/**
514 * xmlXPtrNewRangeNodeObject:
515 * @start: the starting node
516 * @end: the ending object
517 *
518 * Create a new xmlXPathObjectPtr of type range from a not to an object
519 *
520 * Returns the newly created object.
521 */
522xmlXPathObjectPtr
523xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
Brian C. Young48a151c2017-04-03 12:39:04 -0700524 xmlNodePtr endNode;
525 int endIndex;
Owen Taylor3473f882001-02-23 17:55:21 +0000526 xmlXPathObjectPtr ret;
527
528 if (start == NULL)
529 return(NULL);
530 if (end == NULL)
531 return(NULL);
532 switch (end->type) {
533 case XPATH_POINT:
Brian C. Young48a151c2017-04-03 12:39:04 -0700534 endNode = end->user;
535 endIndex = end->index;
536 break;
William M. Brack72ee48d2003-12-30 08:30:19 +0000537 case XPATH_RANGE:
Brian C. Young48a151c2017-04-03 12:39:04 -0700538 endNode = end->user2;
539 endIndex = end->index2;
Owen Taylor3473f882001-02-23 17:55:21 +0000540 break;
541 case XPATH_NODESET:
542 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800543 * Empty set ...
Owen Taylor3473f882001-02-23 17:55:21 +0000544 */
545 if (end->nodesetval->nodeNr <= 0)
546 return(NULL);
Brian C. Young48a151c2017-04-03 12:39:04 -0700547 endNode = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
548 endIndex = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000549 break;
550 default:
Daniel Veillard3d97e662004-11-04 10:49:00 +0000551 /* TODO */
Owen Taylor3473f882001-02-23 17:55:21 +0000552 return(NULL);
553 }
554
Brian C. Young48a151c2017-04-03 12:39:04 -0700555 ret = xmlXPtrNewRangeInternal(start, -1, endNode, endIndex);
Owen Taylor3473f882001-02-23 17:55:21 +0000556 xmlXPtrRangeCheckOrder(ret);
557 return(ret);
558}
559
560#define XML_RANGESET_DEFAULT 10
561
562/**
563 * xmlXPtrLocationSetCreate:
564 * @val: an initial xmlXPathObjectPtr, or NULL
565 *
566 * Create a new xmlLocationSetPtr of type double and of value @val
567 *
568 * Returns the newly created object.
569 */
570xmlLocationSetPtr
571xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
572 xmlLocationSetPtr ret;
573
574 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
575 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000576 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000577 return(NULL);
578 }
579 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
580 if (val != NULL) {
581 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
582 sizeof(xmlXPathObjectPtr));
583 if (ret->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000584 xmlXPtrErrMemory("allocating locationset");
585 xmlFree(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000586 return(NULL);
587 }
588 memset(ret->locTab, 0 ,
589 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
590 ret->locMax = XML_RANGESET_DEFAULT;
591 ret->locTab[ret->locNr++] = val;
592 }
593 return(ret);
594}
595
596/**
597 * xmlXPtrLocationSetAdd:
598 * @cur: the initial range set
599 * @val: a new xmlXPathObjectPtr
600 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000601 * add a new xmlXPathObjectPtr to an existing LocationSet
Owen Taylor3473f882001-02-23 17:55:21 +0000602 * If the location already exist in the set @val is freed.
603 */
604void
605xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
606 int i;
607
Daniel Veillardce682bc2004-11-05 17:22:25 +0000608 if ((cur == NULL) || (val == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000609
610 /*
611 * check against doublons
612 */
613 for (i = 0;i < cur->locNr;i++) {
614 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
615 xmlXPathFreeObject(val);
616 return;
617 }
618 }
619
620 /*
621 * grow the locTab if needed
622 */
623 if (cur->locMax == 0) {
624 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
625 sizeof(xmlXPathObjectPtr));
626 if (cur->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000627 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000628 return;
629 }
630 memset(cur->locTab, 0 ,
631 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
632 cur->locMax = XML_RANGESET_DEFAULT;
633 } else if (cur->locNr == cur->locMax) {
634 xmlXPathObjectPtr *temp;
635
636 cur->locMax *= 2;
637 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
638 sizeof(xmlXPathObjectPtr));
639 if (temp == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000640 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000641 return;
642 }
643 cur->locTab = temp;
644 }
645 cur->locTab[cur->locNr++] = val;
646}
647
648/**
649 * xmlXPtrLocationSetMerge:
650 * @val1: the first LocationSet
651 * @val2: the second LocationSet
652 *
653 * Merges two rangesets, all ranges from @val2 are added to @val1
654 *
655 * Returns val1 once extended or NULL in case of error.
656 */
657xmlLocationSetPtr
658xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
659 int i;
660
661 if (val1 == NULL) return(NULL);
662 if (val2 == NULL) return(val1);
663
664 /*
665 * !!!!! this can be optimized a lot, knowing that both
666 * val1 and val2 already have unicity of their values.
667 */
668
669 for (i = 0;i < val2->locNr;i++)
670 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
671
672 return(val1);
673}
674
675/**
676 * xmlXPtrLocationSetDel:
677 * @cur: the initial range set
678 * @val: an xmlXPathObjectPtr
679 *
680 * Removes an xmlXPathObjectPtr from an existing LocationSet
681 */
682void
683xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
684 int i;
685
686 if (cur == NULL) return;
687 if (val == NULL) return;
688
689 /*
690 * check against doublons
691 */
692 for (i = 0;i < cur->locNr;i++)
693 if (cur->locTab[i] == val) break;
694
695 if (i >= cur->locNr) {
696#ifdef DEBUG
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800697 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard913d6e02001-11-28 14:53:53 +0000698 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000699#endif
700 return;
701 }
702 cur->locNr--;
703 for (;i < cur->locNr;i++)
704 cur->locTab[i] = cur->locTab[i + 1];
705 cur->locTab[cur->locNr] = NULL;
706}
707
708/**
709 * xmlXPtrLocationSetRemove:
710 * @cur: the initial range set
711 * @val: the index to remove
712 *
713 * Removes an entry from an existing LocationSet list.
714 */
715void
716xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
717 if (cur == NULL) return;
718 if (val >= cur->locNr) return;
719 cur->locNr--;
720 for (;val < cur->locNr;val++)
721 cur->locTab[val] = cur->locTab[val + 1];
722 cur->locTab[cur->locNr] = NULL;
723}
724
725/**
726 * xmlXPtrFreeLocationSet:
727 * @obj: the xmlLocationSetPtr to free
728 *
729 * Free the LocationSet compound (not the actual ranges !).
730 */
731void
732xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
733 int i;
734
735 if (obj == NULL) return;
736 if (obj->locTab != NULL) {
737 for (i = 0;i < obj->locNr; i++) {
738 xmlXPathFreeObject(obj->locTab[i]);
739 }
Owen Taylor3473f882001-02-23 17:55:21 +0000740 xmlFree(obj->locTab);
741 }
Owen Taylor3473f882001-02-23 17:55:21 +0000742 xmlFree(obj);
743}
744
745/**
746 * xmlXPtrNewLocationSetNodes:
747 * @start: the start NodePtr value
748 * @end: the end NodePtr value or NULL
749 *
750 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
751 * it with the single range made of the two nodes @start and @end
752 *
753 * Returns the newly created object.
754 */
755xmlXPathObjectPtr
756xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
757 xmlXPathObjectPtr ret;
758
759 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
760 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000761 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000762 return(NULL);
763 }
764 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
765 ret->type = XPATH_LOCATIONSET;
766 if (end == NULL)
767 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
768 else
769 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
770 return(ret);
771}
772
773/**
774 * xmlXPtrNewLocationSetNodeSet:
775 * @set: a node set
776 *
777 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
778 * it with all the nodes from @set
779 *
780 * Returns the newly created object.
781 */
782xmlXPathObjectPtr
783xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
784 xmlXPathObjectPtr ret;
785
786 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
787 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000788 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000789 return(NULL);
790 }
791 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
792 ret->type = XPATH_LOCATIONSET;
793 if (set != NULL) {
794 int i;
795 xmlLocationSetPtr newset;
796
797 newset = xmlXPtrLocationSetCreate(NULL);
798 if (newset == NULL)
799 return(ret);
800
801 for (i = 0;i < set->nodeNr;i++)
802 xmlXPtrLocationSetAdd(newset,
803 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
804
805 ret->user = (void *) newset;
806 }
807 return(ret);
808}
809
810/**
811 * xmlXPtrWrapLocationSet:
812 * @val: the LocationSet value
813 *
814 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
815 *
816 * Returns the newly created object.
817 */
818xmlXPathObjectPtr
819xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
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 ret->user = (void *) val;
830 return(ret);
831}
832
833/************************************************************************
834 * *
835 * The parser *
836 * *
837 ************************************************************************/
838
Daniel Veillard9a237c92003-02-13 15:52:58 +0000839static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
840
Owen Taylor3473f882001-02-23 17:55:21 +0000841/*
842 * Macros for accessing the content. Those should be used only by the parser,
843 * and not exported.
844 *
845 * Dirty macros, i.e. one need to make assumption on the context to use them
846 *
847 * CUR_PTR return the current pointer to the xmlChar to be parsed.
848 * CUR returns the current xmlChar value, i.e. a 8 bit value
849 * in ISO-Latin or UTF-8.
850 * This should be used internally by the parser
851 * only to compare to ASCII values otherwise it would break when
852 * running with UTF-8 encoding.
853 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
854 * to compare on ASCII based substring.
855 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
856 * strings within the parser.
857 * CURRENT Returns the current char value, with the full decoding of
858 * UTF-8 if we are using this mode. It returns an int.
859 * NEXT Skip to the next character, this does the proper decoding
860 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
861 * It returns the pointer to the current xmlChar.
862 */
863
864#define CUR (*ctxt->cur)
865#define SKIP(val) ctxt->cur += (val)
866#define NXT(val) ctxt->cur[(val)]
867#define CUR_PTR ctxt->cur
868
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800869#define SKIP_BLANKS \
William M. Brack272693c2003-11-14 16:20:34 +0000870 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Owen Taylor3473f882001-02-23 17:55:21 +0000871
872#define CURRENT (*ctxt->cur)
873#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
874
875/*
876 * xmlXPtrGetChildNo:
877 * @ctxt: the XPointer Parser context
878 * @index: the child number
879 *
880 * Move the current node of the nodeset on the stack to the
881 * given child if found
882 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000883static void
884xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000885 xmlNodePtr cur = NULL;
886 xmlXPathObjectPtr obj;
887 xmlNodeSetPtr oldset;
888
889 CHECK_TYPE(XPATH_NODESET);
890 obj = valuePop(ctxt);
891 oldset = obj->nodesetval;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000892 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000893 xmlXPathFreeObject(obj);
894 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
895 return;
896 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000897 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
Owen Taylor3473f882001-02-23 17:55:21 +0000898 if (cur == NULL) {
899 xmlXPathFreeObject(obj);
900 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
901 return;
902 }
903 oldset->nodeTab[0] = cur;
904 valuePush(ctxt, obj);
905}
906
907/**
908 * xmlXPtrEvalXPtrPart:
909 * @ctxt: the XPointer Parser context
910 * @name: the preparsed Scheme for the XPtrPart
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800911 *
Owen Taylor3473f882001-02-23 17:55:21 +0000912 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
913 * | Scheme '(' SchemeSpecificExpr ')'
914 *
915 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
916 *
917 * SchemeSpecificExpr ::= StringWithBalancedParens
918 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800919 * StringWithBalancedParens ::=
Owen Taylor3473f882001-02-23 17:55:21 +0000920 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
921 * [VC: Parenthesis escaping]
922 *
923 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
924 *
925 * VC: Parenthesis escaping:
926 * The end of an XPointer part is signaled by the right parenthesis ")"
927 * character that is balanced with the left parenthesis "(" character
928 * that began the part. Any unbalanced parenthesis character inside the
929 * expression, even within literals, must be escaped with a circumflex (^)
930 * character preceding it. If the expression contains any literal
931 * occurrences of the circumflex, each must be escaped with an additional
932 * circumflex (that is, ^^). If the unescaped parentheses in the expression
933 * are not balanced, a syntax error results.
934 *
935 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000936 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800937 *
Owen Taylor3473f882001-02-23 17:55:21 +0000938 * TODO: there is no new scheme registration mechanism
939 */
940
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000941static void
Owen Taylor3473f882001-02-23 17:55:21 +0000942xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
943 xmlChar *buffer, *cur;
944 int len;
945 int level;
946
947 if (name == NULL)
948 name = xmlXPathParseName(ctxt);
949 if (name == NULL)
950 XP_ERROR(XPATH_EXPR_ERROR);
951
952 if (CUR != '(')
953 XP_ERROR(XPATH_EXPR_ERROR);
954 NEXT;
955 level = 1;
956
957 len = xmlStrlen(ctxt->cur);
958 len++;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000959 buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +0000960 if (buffer == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000961 xmlXPtrErrMemory("allocating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +0000962 return;
963 }
964
965 cur = buffer;
966 while (CUR != 0) {
967 if (CUR == ')') {
968 level--;
969 if (level == 0) {
970 NEXT;
971 break;
972 }
Owen Taylor3473f882001-02-23 17:55:21 +0000973 } else if (CUR == '(') {
974 level++;
Owen Taylor3473f882001-02-23 17:55:21 +0000975 } else if (CUR == '^') {
Jüri Aedlad8e1fae2012-05-07 15:06:56 +0800976 if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
977 NEXT;
978 }
Owen Taylor3473f882001-02-23 17:55:21 +0000979 }
Jüri Aedlad8e1fae2012-05-07 15:06:56 +0800980 *cur++ = CUR;
Owen Taylor3473f882001-02-23 17:55:21 +0000981 NEXT;
982 }
983 *cur = 0;
984
985 if ((level != 0) && (CUR == 0)) {
986 xmlFree(buffer);
987 XP_ERROR(XPTR_SYNTAX_ERROR);
988 }
989
990 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
991 const xmlChar *left = CUR_PTR;
992
993 CUR_PTR = buffer;
William M. Brack72ee48d2003-12-30 08:30:19 +0000994 /*
995 * To evaluate an xpointer scheme element (4.3) we need:
996 * context initialized to the root
997 * context position initalized to 1
998 * context size initialized to 1
999 */
1000 ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
1001 ctxt->context->proximityPosition = 1;
1002 ctxt->context->contextSize = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001003 xmlXPathEvalExpr(ctxt);
1004 CUR_PTR=left;
Daniel Veillard9a237c92003-02-13 15:52:58 +00001005 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
1006 const xmlChar *left = CUR_PTR;
1007 xmlChar *name2;
1008
1009 CUR_PTR = buffer;
1010 if (buffer[0] == '/') {
1011 xmlXPathRoot(ctxt);
1012 xmlXPtrEvalChildSeq(ctxt, NULL);
1013 } else {
1014 name2 = xmlXPathParseName(ctxt);
1015 if (name2 == NULL) {
1016 CUR_PTR = left;
1017 xmlFree(buffer);
1018 XP_ERROR(XPATH_EXPR_ERROR);
1019 }
1020 xmlXPtrEvalChildSeq(ctxt, name2);
1021 }
1022 CUR_PTR = left;
Owen Taylor3473f882001-02-23 17:55:21 +00001023#ifdef XPTR_XMLNS_SCHEME
1024 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1025 const xmlChar *left = CUR_PTR;
1026 xmlChar *prefix;
1027 xmlChar *URI;
1028 xmlURIPtr value;
1029
1030 CUR_PTR = buffer;
1031 prefix = xmlXPathParseNCName(ctxt);
1032 if (prefix == NULL) {
1033 xmlFree(buffer);
1034 xmlFree(name);
1035 XP_ERROR(XPTR_SYNTAX_ERROR);
1036 }
1037 SKIP_BLANKS;
1038 if (CUR != '=') {
1039 xmlFree(prefix);
1040 xmlFree(buffer);
1041 xmlFree(name);
1042 XP_ERROR(XPTR_SYNTAX_ERROR);
1043 }
1044 NEXT;
1045 SKIP_BLANKS;
1046 /* @@ check escaping in the XPointer WD */
1047
1048 value = xmlParseURI((const char *)ctxt->cur);
1049 if (value == NULL) {
1050 xmlFree(prefix);
1051 xmlFree(buffer);
1052 xmlFree(name);
1053 XP_ERROR(XPTR_SYNTAX_ERROR);
1054 }
1055 URI = xmlSaveUri(value);
1056 xmlFreeURI(value);
1057 if (URI == NULL) {
1058 xmlFree(prefix);
1059 xmlFree(buffer);
1060 xmlFree(name);
1061 XP_ERROR(XPATH_MEMORY_ERROR);
1062 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001063
Owen Taylor3473f882001-02-23 17:55:21 +00001064 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1065 CUR_PTR = left;
Daniel Veillard56f21f22002-11-06 15:49:46 +00001066 xmlFree(URI);
1067 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001068#endif /* XPTR_XMLNS_SCHEME */
1069 } else {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001070 xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
1071 "unsupported scheme '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001072 }
1073 xmlFree(buffer);
1074 xmlFree(name);
1075}
1076
1077/**
1078 * xmlXPtrEvalFullXPtr:
1079 * @ctxt: the XPointer Parser context
1080 * @name: the preparsed Scheme for the first XPtrPart
1081 *
1082 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1083 *
1084 * As the specs says:
1085 * -----------
1086 * When multiple XPtrParts are provided, they must be evaluated in
1087 * left-to-right order. If evaluation of one part fails, the nexti
1088 * is evaluated. The following conditions cause XPointer part failure:
1089 *
1090 * - An unknown scheme
1091 * - A scheme that does not locate any sub-resource present in the resource
1092 * - A scheme that is not applicable to the media type of the resource
1093 *
1094 * The XPointer application must consume a failed XPointer part and
1095 * attempt to evaluate the next one, if any. The result of the first
1096 * XPointer part whose evaluation succeeds is taken to be the fragment
1097 * located by the XPointer as a whole. If all the parts fail, the result
1098 * for the XPointer as a whole is a sub-resource error.
1099 * -----------
1100 *
1101 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001102 * expressions or other schemes.
Owen Taylor3473f882001-02-23 17:55:21 +00001103 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001104static void
Owen Taylor3473f882001-02-23 17:55:21 +00001105xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1106 if (name == NULL)
1107 name = xmlXPathParseName(ctxt);
1108 if (name == NULL)
1109 XP_ERROR(XPATH_EXPR_ERROR);
1110 while (name != NULL) {
Jakub Wilk928d7032009-07-29 12:24:11 +02001111 ctxt->error = XPATH_EXPRESSION_OK;
Owen Taylor3473f882001-02-23 17:55:21 +00001112 xmlXPtrEvalXPtrPart(ctxt, name);
1113
1114 /* in case of syntax error, break here */
Jakub Wilk928d7032009-07-29 12:24:11 +02001115 if ((ctxt->error != XPATH_EXPRESSION_OK) &&
1116 (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
Owen Taylor3473f882001-02-23 17:55:21 +00001117 return;
1118
1119 /*
1120 * If the returned value is a non-empty nodeset
1121 * or location set, return here.
1122 */
1123 if (ctxt->value != NULL) {
1124 xmlXPathObjectPtr obj = ctxt->value;
1125
1126 switch (obj->type) {
1127 case XPATH_LOCATIONSET: {
1128 xmlLocationSetPtr loc = ctxt->value->user;
1129 if ((loc != NULL) && (loc->locNr > 0))
1130 return;
1131 break;
1132 }
1133 case XPATH_NODESET: {
1134 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1135 if ((loc != NULL) && (loc->nodeNr > 0))
1136 return;
1137 break;
1138 }
1139 default:
1140 break;
1141 }
1142
1143 /*
1144 * Evaluating to improper values is equivalent to
1145 * a sub-resource error, clean-up the stack
1146 */
1147 do {
1148 obj = valuePop(ctxt);
1149 if (obj != NULL) {
1150 xmlXPathFreeObject(obj);
1151 }
1152 } while (obj != NULL);
1153 }
1154
1155 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001156 * Is there another XPointer part.
Owen Taylor3473f882001-02-23 17:55:21 +00001157 */
1158 SKIP_BLANKS;
1159 name = xmlXPathParseName(ctxt);
1160 }
1161}
1162
1163/**
1164 * xmlXPtrEvalChildSeq:
1165 * @ctxt: the XPointer Parser context
1166 * @name: a possible ID name of the child sequence
1167 *
1168 * ChildSeq ::= '/1' ('/' [0-9]*)*
1169 * | Name ('/' [0-9]*)+
1170 *
1171 * Parse and evaluate a Child Sequence. This routine also handle the
1172 * case of a Bare Name used to get a document ID.
1173 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001174static void
Owen Taylor3473f882001-02-23 17:55:21 +00001175xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1176 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001177 * XPointer don't allow by syntax to address in mutirooted trees
Owen Taylor3473f882001-02-23 17:55:21 +00001178 * this might prove useful in some cases, warn about it.
1179 */
1180 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001181 xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
1182 "warning: ChildSeq not starting by /1\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001183 }
1184
1185 if (name != NULL) {
1186 valuePush(ctxt, xmlXPathNewString(name));
1187 xmlFree(name);
1188 xmlXPathIdFunction(ctxt, 1);
1189 CHECK_ERROR;
1190 }
1191
1192 while (CUR == '/') {
1193 int child = 0;
1194 NEXT;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001195
Owen Taylor3473f882001-02-23 17:55:21 +00001196 while ((CUR >= '0') && (CUR <= '9')) {
1197 child = child * 10 + (CUR - '0');
1198 NEXT;
1199 }
1200 xmlXPtrGetChildNo(ctxt, child);
1201 }
1202}
1203
1204
1205/**
1206 * xmlXPtrEvalXPointer:
1207 * @ctxt: the XPointer Parser context
1208 *
1209 * XPointer ::= Name
1210 * | ChildSeq
1211 * | FullXPtr
1212 *
1213 * Parse and evaluate an XPointer
1214 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001215static void
Owen Taylor3473f882001-02-23 17:55:21 +00001216xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001217 if (ctxt->valueTab == NULL) {
1218 /* Allocate the value stack */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001219 ctxt->valueTab = (xmlXPathObjectPtr *)
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001220 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1221 if (ctxt->valueTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001222 xmlXPtrErrMemory("allocating evaluation context");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001223 return;
1224 }
1225 ctxt->valueNr = 0;
1226 ctxt->valueMax = 10;
1227 ctxt->value = NULL;
Daniel Veillardf5048b32011-08-18 17:10:13 +08001228 ctxt->valueFrame = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001229 }
Owen Taylor3473f882001-02-23 17:55:21 +00001230 SKIP_BLANKS;
1231 if (CUR == '/') {
1232 xmlXPathRoot(ctxt);
1233 xmlXPtrEvalChildSeq(ctxt, NULL);
1234 } else {
1235 xmlChar *name;
1236
1237 name = xmlXPathParseName(ctxt);
1238 if (name == NULL)
1239 XP_ERROR(XPATH_EXPR_ERROR);
1240 if (CUR == '(') {
1241 xmlXPtrEvalFullXPtr(ctxt, name);
1242 /* Short evaluation */
1243 return;
1244 } else {
1245 /* this handle both Bare Names and Child Sequences */
1246 xmlXPtrEvalChildSeq(ctxt, name);
1247 }
1248 }
1249 SKIP_BLANKS;
1250 if (CUR != 0)
1251 XP_ERROR(XPATH_EXPR_ERROR);
1252}
1253
1254
1255/************************************************************************
1256 * *
1257 * General routines *
1258 * *
1259 ************************************************************************/
1260
Daniel Veillard8ed10722009-08-20 19:17:36 +02001261static
Owen Taylor3473f882001-02-23 17:55:21 +00001262void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001263static
Owen Taylor3473f882001-02-23 17:55:21 +00001264void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001265static
Owen Taylor3473f882001-02-23 17:55:21 +00001266void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001267static
Owen Taylor3473f882001-02-23 17:55:21 +00001268void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001269static
Owen Taylor3473f882001-02-23 17:55:21 +00001270void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001271static
Owen Taylor3473f882001-02-23 17:55:21 +00001272void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001273static
Owen Taylor3473f882001-02-23 17:55:21 +00001274void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1275
1276/**
1277 * xmlXPtrNewContext:
1278 * @doc: the XML document
1279 * @here: the node that directly contains the XPointer being evaluated or NULL
1280 * @origin: the element from which a user or program initiated traversal of
1281 * the link, or NULL.
1282 *
1283 * Create a new XPointer context
1284 *
1285 * Returns the xmlXPathContext just allocated.
1286 */
1287xmlXPathContextPtr
1288xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1289 xmlXPathContextPtr ret;
1290
1291 ret = xmlXPathNewContext(doc);
1292 if (ret == NULL)
1293 return(ret);
1294 ret->xptr = 1;
1295 ret->here = here;
1296 ret->origin = origin;
1297
1298 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1299 xmlXPtrRangeToFunction);
1300 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1301 xmlXPtrRangeFunction);
1302 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1303 xmlXPtrRangeInsideFunction);
1304 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1305 xmlXPtrStringRangeFunction);
1306 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1307 xmlXPtrStartPointFunction);
1308 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1309 xmlXPtrEndPointFunction);
1310 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1311 xmlXPtrHereFunction);
1312 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1313 xmlXPtrOriginFunction);
1314
1315 return(ret);
1316}
1317
1318/**
1319 * xmlXPtrEval:
1320 * @str: the XPointer expression
1321 * @ctx: the XPointer context
1322 *
1323 * Evaluate the XPath Location Path in the given context.
1324 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001325 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +00001326 * the caller has to free the object.
1327 */
1328xmlXPathObjectPtr
1329xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1330 xmlXPathParserContextPtr ctxt;
1331 xmlXPathObjectPtr res = NULL, tmp;
1332 xmlXPathObjectPtr init = NULL;
1333 int stack = 0;
1334
1335 xmlXPathInit();
1336
1337 if ((ctx == NULL) || (str == NULL))
1338 return(NULL);
1339
1340 ctxt = xmlXPathNewParserContext(str, ctx);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001341 if (ctxt == NULL)
1342 return(NULL);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001343 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001344 xmlXPtrEvalXPointer(ctxt);
1345
1346 if ((ctxt->value != NULL) &&
1347 (ctxt->value->type != XPATH_NODESET) &&
1348 (ctxt->value->type != XPATH_LOCATIONSET)) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001349 xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
1350 "xmlXPtrEval: evaluation failed to return a node set\n",
1351 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001352 } else {
1353 res = valuePop(ctxt);
1354 }
1355
1356 do {
1357 tmp = valuePop(ctxt);
1358 if (tmp != NULL) {
1359 if (tmp != init) {
1360 if (tmp->type == XPATH_NODESET) {
1361 /*
1362 * Evaluation may push a root nodeset which is unused
1363 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001364 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001365 set = tmp->nodesetval;
1366 if ((set->nodeNr != 1) ||
1367 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1368 stack++;
1369 } else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001370 stack++;
Owen Taylor3473f882001-02-23 17:55:21 +00001371 }
1372 xmlXPathFreeObject(tmp);
1373 }
1374 } while (tmp != NULL);
1375 if (stack != 0) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001376 xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
1377 "xmlXPtrEval: object(s) left on the eval stack\n",
1378 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001379 }
1380 if (ctxt->error != XPATH_EXPRESSION_OK) {
1381 xmlXPathFreeObject(res);
1382 res = NULL;
1383 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001384
Owen Taylor3473f882001-02-23 17:55:21 +00001385 xmlXPathFreeParserContext(ctxt);
1386 return(res);
1387}
1388
1389/**
1390 * xmlXPtrBuildRangeNodeList:
1391 * @range: a range object
1392 *
1393 * Build a node list tree copy of the range
1394 *
1395 * Returns an xmlNodePtr list or NULL.
1396 * the caller has to free the node tree.
1397 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001398static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001399xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1400 /* pointers to generated nodes */
1401 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1402 /* pointers to traversal nodes */
1403 xmlNodePtr start, cur, end;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001404 int index1, index2;
Owen Taylor3473f882001-02-23 17:55:21 +00001405
1406 if (range == NULL)
1407 return(NULL);
1408 if (range->type != XPATH_RANGE)
1409 return(NULL);
1410 start = (xmlNodePtr) range->user;
1411
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001412 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00001413 return(NULL);
1414 end = range->user2;
1415 if (end == NULL)
1416 return(xmlCopyNode(start, 1));
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001417 if (end->type == XML_NAMESPACE_DECL)
1418 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001419
1420 cur = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001421 index1 = range->index;
Owen Taylor3473f882001-02-23 17:55:21 +00001422 index2 = range->index2;
1423 while (cur != NULL) {
1424 if (cur == end) {
1425 if (cur->type == XML_TEXT_NODE) {
1426 const xmlChar *content = cur->content;
1427 int len;
1428
1429 if (content == NULL) {
1430 tmp = xmlNewTextLen(NULL, 0);
1431 } else {
1432 len = index2;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001433 if ((cur == start) && (index1 > 1)) {
1434 content += (index1 - 1);
1435 len -= (index1 - 1);
1436 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001437 } else {
1438 len = index2;
1439 }
1440 tmp = xmlNewTextLen(content, len);
1441 }
1442 /* single sub text node selection */
1443 if (list == NULL)
1444 return(tmp);
1445 /* prune and return full set */
1446 if (last != NULL)
1447 xmlAddNextSibling(last, tmp);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001448 else
Owen Taylor3473f882001-02-23 17:55:21 +00001449 xmlAddChild(parent, tmp);
1450 return(list);
1451 } else {
1452 tmp = xmlCopyNode(cur, 0);
1453 if (list == NULL)
1454 list = tmp;
1455 else {
1456 if (last != NULL)
1457 xmlAddNextSibling(last, tmp);
1458 else
1459 xmlAddChild(parent, tmp);
1460 }
1461 last = NULL;
1462 parent = tmp;
1463
1464 if (index2 > 1) {
1465 end = xmlXPtrGetNthChild(cur, index2 - 1);
1466 index2 = 0;
1467 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001468 if ((cur == start) && (index1 > 1)) {
1469 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1470 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001471 } else {
1472 cur = cur->children;
1473 }
1474 /*
1475 * Now gather the remaining nodes from cur to end
1476 */
1477 continue; /* while */
1478 }
1479 } else if ((cur == start) &&
1480 (list == NULL) /* looks superfluous but ... */ ) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001481 if ((cur->type == XML_TEXT_NODE) ||
1482 (cur->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001483 const xmlChar *content = cur->content;
1484
1485 if (content == NULL) {
1486 tmp = xmlNewTextLen(NULL, 0);
1487 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001488 if (index1 > 1) {
1489 content += (index1 - 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001490 }
1491 tmp = xmlNewText(content);
1492 }
1493 last = list = tmp;
1494 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001495 if ((cur == start) && (index1 > 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001496 tmp = xmlCopyNode(cur, 0);
1497 list = tmp;
1498 parent = tmp;
1499 last = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001500 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1501 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001502 /*
1503 * Now gather the remaining nodes from cur to end
1504 */
1505 continue; /* while */
1506 }
1507 tmp = xmlCopyNode(cur, 1);
1508 list = tmp;
1509 parent = NULL;
1510 last = tmp;
1511 }
1512 } else {
1513 tmp = NULL;
1514 switch (cur->type) {
1515 case XML_DTD_NODE:
1516 case XML_ELEMENT_DECL:
1517 case XML_ATTRIBUTE_DECL:
1518 case XML_ENTITY_NODE:
1519 /* Do not copy DTD informations */
1520 break;
1521 case XML_ENTITY_DECL:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001522 TODO /* handle crossing entities -> stack needed */
Owen Taylor3473f882001-02-23 17:55:21 +00001523 break;
1524 case XML_XINCLUDE_START:
1525 case XML_XINCLUDE_END:
1526 /* don't consider it part of the tree content */
1527 break;
1528 case XML_ATTRIBUTE_NODE:
1529 /* Humm, should not happen ! */
1530 STRANGE
1531 break;
1532 default:
1533 tmp = xmlCopyNode(cur, 1);
1534 break;
1535 }
1536 if (tmp != NULL) {
1537 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1538 STRANGE
1539 return(NULL);
1540 }
1541 if (last != NULL)
1542 xmlAddNextSibling(last, tmp);
1543 else {
1544 xmlAddChild(parent, tmp);
1545 last = tmp;
1546 }
1547 }
1548 }
1549 /*
1550 * Skip to next node in document order
1551 */
1552 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1553 STRANGE
1554 return(NULL);
1555 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001556 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001557 }
1558 return(list);
1559}
1560
1561/**
1562 * xmlXPtrBuildNodeList:
1563 * @obj: the XPointer result from the evaluation.
1564 *
1565 * Build a node list tree copy of the XPointer result.
Daniel Veillard39196eb2001-06-19 18:09:42 +00001566 * This will drop Attributes and Namespace declarations.
Owen Taylor3473f882001-02-23 17:55:21 +00001567 *
1568 * Returns an xmlNodePtr list or NULL.
1569 * the caller has to free the node tree.
1570 */
1571xmlNodePtr
1572xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1573 xmlNodePtr list = NULL, last = NULL;
1574 int i;
1575
1576 if (obj == NULL)
1577 return(NULL);
1578 switch (obj->type) {
1579 case XPATH_NODESET: {
1580 xmlNodeSetPtr set = obj->nodesetval;
1581 if (set == NULL)
1582 return(NULL);
1583 for (i = 0;i < set->nodeNr;i++) {
Daniel Veillard39196eb2001-06-19 18:09:42 +00001584 if (set->nodeTab[i] == NULL)
1585 continue;
1586 switch (set->nodeTab[i]->type) {
1587 case XML_TEXT_NODE:
1588 case XML_CDATA_SECTION_NODE:
1589 case XML_ELEMENT_NODE:
1590 case XML_ENTITY_REF_NODE:
1591 case XML_ENTITY_NODE:
1592 case XML_PI_NODE:
1593 case XML_COMMENT_NODE:
1594 case XML_DOCUMENT_NODE:
1595 case XML_HTML_DOCUMENT_NODE:
1596#ifdef LIBXML_DOCB_ENABLED
1597 case XML_DOCB_DOCUMENT_NODE:
1598#endif
1599 case XML_XINCLUDE_START:
1600 case XML_XINCLUDE_END:
1601 break;
1602 case XML_ATTRIBUTE_NODE:
1603 case XML_NAMESPACE_DECL:
1604 case XML_DOCUMENT_TYPE_NODE:
1605 case XML_DOCUMENT_FRAG_NODE:
1606 case XML_NOTATION_NODE:
1607 case XML_DTD_NODE:
1608 case XML_ELEMENT_DECL:
1609 case XML_ATTRIBUTE_DECL:
1610 case XML_ENTITY_DECL:
1611 continue; /* for */
1612 }
Owen Taylor3473f882001-02-23 17:55:21 +00001613 if (last == NULL)
1614 list = last = xmlCopyNode(set->nodeTab[i], 1);
1615 else {
1616 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1617 if (last->next != NULL)
1618 last = last->next;
1619 }
1620 }
1621 break;
1622 }
1623 case XPATH_LOCATIONSET: {
1624 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1625 if (set == NULL)
1626 return(NULL);
1627 for (i = 0;i < set->locNr;i++) {
1628 if (last == NULL)
1629 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1630 else
1631 xmlAddNextSibling(last,
1632 xmlXPtrBuildNodeList(set->locTab[i]));
1633 if (last != NULL) {
1634 while (last->next != NULL)
1635 last = last->next;
1636 }
1637 }
1638 break;
1639 }
1640 case XPATH_RANGE:
1641 return(xmlXPtrBuildRangeNodeList(obj));
1642 case XPATH_POINT:
1643 return(xmlCopyNode(obj->user, 0));
1644 default:
1645 break;
1646 }
1647 return(list);
1648}
1649
1650/************************************************************************
1651 * *
1652 * XPointer functions *
1653 * *
1654 ************************************************************************/
1655
1656/**
1657 * xmlXPtrNbLocChildren:
1658 * @node: an xmlNodePtr
1659 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001660 * Count the number of location children of @node or the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001661 * string value in case of text/PI/Comments nodes
1662 *
1663 * Returns the number of location children
1664 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001665static int
Owen Taylor3473f882001-02-23 17:55:21 +00001666xmlXPtrNbLocChildren(xmlNodePtr node) {
1667 int ret = 0;
1668 if (node == NULL)
1669 return(-1);
1670 switch (node->type) {
1671 case XML_HTML_DOCUMENT_NODE:
1672 case XML_DOCUMENT_NODE:
1673 case XML_ELEMENT_NODE:
1674 node = node->children;
1675 while (node != NULL) {
1676 if (node->type == XML_ELEMENT_NODE)
1677 ret++;
1678 node = node->next;
1679 }
1680 break;
1681 case XML_ATTRIBUTE_NODE:
1682 return(-1);
1683
1684 case XML_PI_NODE:
1685 case XML_COMMENT_NODE:
1686 case XML_TEXT_NODE:
1687 case XML_CDATA_SECTION_NODE:
1688 case XML_ENTITY_REF_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001689 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001690 break;
1691 default:
1692 return(-1);
1693 }
1694 return(ret);
1695}
1696
1697/**
1698 * xmlXPtrHereFunction:
1699 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001700 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001701 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001702 * Function implementing here() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001703 * as described in 5.4.3
1704 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001705static void
Owen Taylor3473f882001-02-23 17:55:21 +00001706xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001707 CHECK_ARITY(0);
1708
Owen Taylor3473f882001-02-23 17:55:21 +00001709 if (ctxt->context->here == NULL)
1710 XP_ERROR(XPTR_SYNTAX_ERROR);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001711
Owen Taylor3473f882001-02-23 17:55:21 +00001712 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1713}
1714
1715/**
1716 * xmlXPtrOriginFunction:
1717 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001718 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001719 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001720 * Function implementing origin() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001721 * as described in 5.4.3
1722 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001723static void
Owen Taylor3473f882001-02-23 17:55:21 +00001724xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001725 CHECK_ARITY(0);
1726
Owen Taylor3473f882001-02-23 17:55:21 +00001727 if (ctxt->context->origin == NULL)
1728 XP_ERROR(XPTR_SYNTAX_ERROR);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001729
Owen Taylor3473f882001-02-23 17:55:21 +00001730 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1731}
1732
1733/**
1734 * xmlXPtrStartPointFunction:
1735 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001736 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001737 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001738 * Function implementing start-point() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001739 * as described in 5.4.3
1740 * ----------------
1741 * location-set start-point(location-set)
1742 *
1743 * For each location x in the argument location-set, start-point adds a
1744 * location of type point to the result location-set. That point represents
1745 * the start point of location x and is determined by the following rules:
1746 *
1747 * - If x is of type point, the start point is x.
1748 * - If x is of type range, the start point is the start point of x.
1749 * - If x is of type root, element, text, comment, or processing instruction,
1750 * - the container node of the start point is x and the index is 0.
1751 * - If x is of type attribute or namespace, the function must signal a
1752 * syntax error.
1753 * ----------------
1754 *
1755 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001756static void
Owen Taylor3473f882001-02-23 17:55:21 +00001757xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1758 xmlXPathObjectPtr tmp, obj, point;
1759 xmlLocationSetPtr newset = NULL;
1760 xmlLocationSetPtr oldset = NULL;
1761
1762 CHECK_ARITY(1);
1763 if ((ctxt->value == NULL) ||
1764 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1765 (ctxt->value->type != XPATH_NODESET)))
1766 XP_ERROR(XPATH_INVALID_TYPE)
1767
1768 obj = valuePop(ctxt);
1769 if (obj->type == XPATH_NODESET) {
1770 /*
1771 * First convert to a location set
1772 */
1773 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1774 xmlXPathFreeObject(obj);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001775 if (tmp == NULL)
1776 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00001777 obj = tmp;
1778 }
1779
1780 newset = xmlXPtrLocationSetCreate(NULL);
1781 if (newset == NULL) {
1782 xmlXPathFreeObject(obj);
1783 XP_ERROR(XPATH_MEMORY_ERROR);
1784 }
1785 oldset = (xmlLocationSetPtr) obj->user;
1786 if (oldset != NULL) {
1787 int i;
1788
1789 for (i = 0; i < oldset->locNr; i++) {
1790 tmp = oldset->locTab[i];
1791 if (tmp == NULL)
1792 continue;
1793 point = NULL;
1794 switch (tmp->type) {
1795 case XPATH_POINT:
1796 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1797 break;
1798 case XPATH_RANGE: {
1799 xmlNodePtr node = tmp->user;
1800 if (node != NULL) {
1801 if (node->type == XML_ATTRIBUTE_NODE) {
1802 /* TODO: Namespace Nodes ??? */
1803 xmlXPathFreeObject(obj);
1804 xmlXPtrFreeLocationSet(newset);
1805 XP_ERROR(XPTR_SYNTAX_ERROR);
1806 }
1807 point = xmlXPtrNewPoint(node, tmp->index);
1808 }
1809 break;
1810 }
1811 default:
1812 /*** Should we raise an error ?
1813 xmlXPathFreeObject(obj);
1814 xmlXPathFreeObject(newset);
1815 XP_ERROR(XPATH_INVALID_TYPE)
1816 ***/
1817 break;
1818 }
1819 if (point != NULL)
1820 xmlXPtrLocationSetAdd(newset, point);
1821 }
1822 }
1823 xmlXPathFreeObject(obj);
1824 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1825}
1826
1827/**
1828 * xmlXPtrEndPointFunction:
1829 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001830 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001831 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001832 * Function implementing end-point() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001833 * as described in 5.4.3
1834 * ----------------------------
1835 * location-set end-point(location-set)
1836 *
1837 * For each location x in the argument location-set, end-point adds a
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001838 * location of type point to the result location-set. That point represents
Owen Taylor3473f882001-02-23 17:55:21 +00001839 * the end point of location x and is determined by the following rules:
1840 *
1841 * - If x is of type point, the resulting point is x.
1842 * - If x is of type range, the resulting point is the end point of x.
1843 * - If x is of type root or element, the container node of the resulting
1844 * point is x and the index is the number of location children of x.
1845 * - If x is of type text, comment, or processing instruction, the container
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001846 * node of the resulting point is x and the index is the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001847 * string-value of x.
1848 * - If x is of type attribute or namespace, the function must signal a
1849 * syntax error.
1850 * ----------------------------
1851 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001852static void
Owen Taylor3473f882001-02-23 17:55:21 +00001853xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1854 xmlXPathObjectPtr tmp, obj, point;
1855 xmlLocationSetPtr newset = NULL;
1856 xmlLocationSetPtr oldset = NULL;
1857
1858 CHECK_ARITY(1);
1859 if ((ctxt->value == NULL) ||
1860 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1861 (ctxt->value->type != XPATH_NODESET)))
1862 XP_ERROR(XPATH_INVALID_TYPE)
1863
1864 obj = valuePop(ctxt);
1865 if (obj->type == XPATH_NODESET) {
1866 /*
1867 * First convert to a location set
1868 */
1869 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1870 xmlXPathFreeObject(obj);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001871 if (tmp == NULL)
1872 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00001873 obj = tmp;
1874 }
1875
1876 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001877 if (newset == NULL) {
1878 xmlXPathFreeObject(obj);
1879 XP_ERROR(XPATH_MEMORY_ERROR);
1880 }
Owen Taylor3473f882001-02-23 17:55:21 +00001881 oldset = (xmlLocationSetPtr) obj->user;
1882 if (oldset != NULL) {
1883 int i;
1884
1885 for (i = 0; i < oldset->locNr; i++) {
1886 tmp = oldset->locTab[i];
1887 if (tmp == NULL)
1888 continue;
1889 point = NULL;
1890 switch (tmp->type) {
1891 case XPATH_POINT:
1892 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1893 break;
1894 case XPATH_RANGE: {
1895 xmlNodePtr node = tmp->user2;
1896 if (node != NULL) {
1897 if (node->type == XML_ATTRIBUTE_NODE) {
1898 /* TODO: Namespace Nodes ??? */
1899 xmlXPathFreeObject(obj);
1900 xmlXPtrFreeLocationSet(newset);
1901 XP_ERROR(XPTR_SYNTAX_ERROR);
1902 }
1903 point = xmlXPtrNewPoint(node, tmp->index2);
1904 } else if (tmp->user == NULL) {
1905 point = xmlXPtrNewPoint(node,
1906 xmlXPtrNbLocChildren(node));
1907 }
1908 break;
1909 }
1910 default:
1911 /*** Should we raise an error ?
1912 xmlXPathFreeObject(obj);
1913 xmlXPathFreeObject(newset);
1914 XP_ERROR(XPATH_INVALID_TYPE)
1915 ***/
1916 break;
1917 }
1918 if (point != NULL)
1919 xmlXPtrLocationSetAdd(newset, point);
1920 }
1921 }
1922 xmlXPathFreeObject(obj);
1923 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1924}
1925
1926
1927/**
1928 * xmlXPtrCoveringRange:
1929 * @ctxt: the XPointer Parser context
1930 * @loc: the location for which the covering range must be computed
1931 *
1932 * A covering range is a range that wholly encompasses a location
1933 * Section 5.3.3. Covering Ranges for All Location Types
1934 * http://www.w3.org/TR/xptr#N2267
1935 *
1936 * Returns a new location or NULL in case of error
1937 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001938static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001939xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1940 if (loc == NULL)
1941 return(NULL);
1942 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1943 (ctxt->context->doc == NULL))
1944 return(NULL);
1945 switch (loc->type) {
1946 case XPATH_POINT:
1947 return(xmlXPtrNewRange(loc->user, loc->index,
1948 loc->user, loc->index));
1949 case XPATH_RANGE:
1950 if (loc->user2 != NULL) {
1951 return(xmlXPtrNewRange(loc->user, loc->index,
1952 loc->user2, loc->index2));
1953 } else {
1954 xmlNodePtr node = (xmlNodePtr) loc->user;
1955 if (node == (xmlNodePtr) ctxt->context->doc) {
1956 return(xmlXPtrNewRange(node, 0, node,
1957 xmlXPtrGetArity(node)));
1958 } else {
1959 switch (node->type) {
1960 case XML_ATTRIBUTE_NODE:
1961 /* !!! our model is slightly different than XPath */
1962 return(xmlXPtrNewRange(node, 0, node,
1963 xmlXPtrGetArity(node)));
1964 case XML_ELEMENT_NODE:
1965 case XML_TEXT_NODE:
1966 case XML_CDATA_SECTION_NODE:
1967 case XML_ENTITY_REF_NODE:
1968 case XML_PI_NODE:
1969 case XML_COMMENT_NODE:
1970 case XML_DOCUMENT_NODE:
1971 case XML_NOTATION_NODE:
1972 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001973 int indx = xmlXPtrGetIndex(node);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001974
Owen Taylor3473f882001-02-23 17:55:21 +00001975 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001976 return(xmlXPtrNewRange(node, indx - 1,
1977 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00001978 }
1979 default:
1980 return(NULL);
1981 }
1982 }
1983 }
1984 default:
1985 TODO /* missed one case ??? */
1986 }
1987 return(NULL);
1988}
1989
1990/**
1991 * xmlXPtrRangeFunction:
1992 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001993 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001994 *
1995 * Function implementing the range() function 5.4.3
1996 * location-set range(location-set )
1997 *
1998 * The range function returns ranges covering the locations in
1999 * the argument location-set. For each location x in the argument
2000 * location-set, a range location representing the covering range of
2001 * x is added to the result location-set.
2002 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002003static void
Owen Taylor3473f882001-02-23 17:55:21 +00002004xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2005 int i;
2006 xmlXPathObjectPtr set;
2007 xmlLocationSetPtr oldset;
2008 xmlLocationSetPtr newset;
2009
2010 CHECK_ARITY(1);
2011 if ((ctxt->value == NULL) ||
2012 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2013 (ctxt->value->type != XPATH_NODESET)))
2014 XP_ERROR(XPATH_INVALID_TYPE)
2015
2016 set = valuePop(ctxt);
2017 if (set->type == XPATH_NODESET) {
2018 xmlXPathObjectPtr tmp;
2019
2020 /*
2021 * First convert to a location set
2022 */
2023 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2024 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002025 if (tmp == NULL)
2026 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002027 set = tmp;
2028 }
2029 oldset = (xmlLocationSetPtr) set->user;
2030
2031 /*
2032 * The loop is to compute the covering range for each item and add it
2033 */
2034 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002035 if (newset == NULL) {
2036 xmlXPathFreeObject(set);
2037 XP_ERROR(XPATH_MEMORY_ERROR);
2038 }
Owen Taylor3473f882001-02-23 17:55:21 +00002039 for (i = 0;i < oldset->locNr;i++) {
2040 xmlXPtrLocationSetAdd(newset,
2041 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2042 }
2043
2044 /*
2045 * Save the new value and cleanup
2046 */
2047 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2048 xmlXPathFreeObject(set);
2049}
2050
2051/**
2052 * xmlXPtrInsideRange:
2053 * @ctxt: the XPointer Parser context
2054 * @loc: the location for which the inside range must be computed
2055 *
2056 * A inside range is a range described in the range-inside() description
2057 *
2058 * Returns a new location or NULL in case of error
2059 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002060static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002061xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2062 if (loc == NULL)
2063 return(NULL);
2064 if ((ctxt == NULL) || (ctxt->context == NULL) ||
2065 (ctxt->context->doc == NULL))
2066 return(NULL);
2067 switch (loc->type) {
2068 case XPATH_POINT: {
2069 xmlNodePtr node = (xmlNodePtr) loc->user;
2070 switch (node->type) {
2071 case XML_PI_NODE:
2072 case XML_COMMENT_NODE:
2073 case XML_TEXT_NODE:
2074 case XML_CDATA_SECTION_NODE: {
2075 if (node->content == NULL) {
2076 return(xmlXPtrNewRange(node, 0, node, 0));
2077 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002078 return(xmlXPtrNewRange(node, 0, node,
2079 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002080 }
2081 }
2082 case XML_ATTRIBUTE_NODE:
2083 case XML_ELEMENT_NODE:
2084 case XML_ENTITY_REF_NODE:
2085 case XML_DOCUMENT_NODE:
2086 case XML_NOTATION_NODE:
2087 case XML_HTML_DOCUMENT_NODE: {
2088 return(xmlXPtrNewRange(node, 0, node,
2089 xmlXPtrGetArity(node)));
2090 }
2091 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002092 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002093 }
2094 return(NULL);
2095 }
2096 case XPATH_RANGE: {
2097 xmlNodePtr node = (xmlNodePtr) loc->user;
2098 if (loc->user2 != NULL) {
2099 return(xmlXPtrNewRange(node, loc->index,
2100 loc->user2, loc->index2));
2101 } else {
2102 switch (node->type) {
2103 case XML_PI_NODE:
2104 case XML_COMMENT_NODE:
2105 case XML_TEXT_NODE:
2106 case XML_CDATA_SECTION_NODE: {
2107 if (node->content == NULL) {
2108 return(xmlXPtrNewRange(node, 0, node, 0));
2109 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002110 return(xmlXPtrNewRange(node, 0, node,
2111 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002112 }
2113 }
2114 case XML_ATTRIBUTE_NODE:
2115 case XML_ELEMENT_NODE:
2116 case XML_ENTITY_REF_NODE:
2117 case XML_DOCUMENT_NODE:
2118 case XML_NOTATION_NODE:
2119 case XML_HTML_DOCUMENT_NODE: {
2120 return(xmlXPtrNewRange(node, 0, node,
2121 xmlXPtrGetArity(node)));
2122 }
2123 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002124 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002125 }
2126 return(NULL);
2127 }
2128 }
2129 default:
2130 TODO /* missed one case ??? */
2131 }
2132 return(NULL);
2133}
2134
2135/**
2136 * xmlXPtrRangeInsideFunction:
2137 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002138 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002139 *
2140 * Function implementing the range-inside() function 5.4.3
2141 * location-set range-inside(location-set )
2142 *
2143 * The range-inside function returns ranges covering the contents of
2144 * the locations in the argument location-set. For each location x in
2145 * the argument location-set, a range location is added to the result
2146 * location-set. If x is a range location, then x is added to the
2147 * result location-set. If x is not a range location, then x is used
2148 * as the container location of the start and end points of the range
2149 * location to be added; the index of the start point of the range is
2150 * zero; if the end point is a character point then its index is the
2151 * length of the string-value of x, and otherwise is the number of
2152 * location children of x.
2153 *
2154 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002155static void
Owen Taylor3473f882001-02-23 17:55:21 +00002156xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2157 int i;
2158 xmlXPathObjectPtr set;
2159 xmlLocationSetPtr oldset;
2160 xmlLocationSetPtr newset;
2161
2162 CHECK_ARITY(1);
2163 if ((ctxt->value == NULL) ||
2164 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2165 (ctxt->value->type != XPATH_NODESET)))
2166 XP_ERROR(XPATH_INVALID_TYPE)
2167
2168 set = valuePop(ctxt);
2169 if (set->type == XPATH_NODESET) {
2170 xmlXPathObjectPtr tmp;
2171
2172 /*
2173 * First convert to a location set
2174 */
2175 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2176 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002177 if (tmp == NULL)
2178 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002179 set = tmp;
2180 }
2181 oldset = (xmlLocationSetPtr) set->user;
2182
2183 /*
2184 * The loop is to compute the covering range for each item and add it
2185 */
2186 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002187 if (newset == NULL) {
2188 xmlXPathFreeObject(set);
2189 XP_ERROR(XPATH_MEMORY_ERROR);
2190 }
Owen Taylor3473f882001-02-23 17:55:21 +00002191 for (i = 0;i < oldset->locNr;i++) {
2192 xmlXPtrLocationSetAdd(newset,
2193 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2194 }
2195
2196 /*
2197 * Save the new value and cleanup
2198 */
2199 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2200 xmlXPathFreeObject(set);
2201}
2202
2203/**
2204 * xmlXPtrRangeToFunction:
2205 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002206 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002207 *
2208 * Implement the range-to() XPointer function
2209 */
2210void
2211xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2212 xmlXPathObjectPtr range;
2213 const xmlChar *cur;
2214 xmlXPathObjectPtr res, obj;
2215 xmlXPathObjectPtr tmp;
2216 xmlLocationSetPtr newset = NULL;
2217 xmlNodeSetPtr oldset;
2218 int i;
2219
Daniel Veillardce682bc2004-11-05 17:22:25 +00002220 if (ctxt == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002221 CHECK_ARITY(1);
2222 /*
2223 * Save the expression pointer since we will have to evaluate
2224 * it multiple times. Initialize the new set.
2225 */
2226 CHECK_TYPE(XPATH_NODESET);
2227 obj = valuePop(ctxt);
2228 oldset = obj->nodesetval;
2229 ctxt->context->node = NULL;
2230
2231 cur = ctxt->cur;
2232 newset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002233
Owen Taylor3473f882001-02-23 17:55:21 +00002234 for (i = 0; i < oldset->nodeNr; i++) {
2235 ctxt->cur = cur;
2236
2237 /*
2238 * Run the evaluation with a node list made of a single item
2239 * in the nodeset.
2240 */
2241 ctxt->context->node = oldset->nodeTab[i];
2242 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2243 valuePush(ctxt, tmp);
2244
2245 xmlXPathEvalExpr(ctxt);
2246 CHECK_ERROR;
2247
2248 /*
2249 * The result of the evaluation need to be tested to
2250 * decided whether the filter succeeded or not
2251 */
2252 res = valuePop(ctxt);
2253 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2254 if (range != NULL) {
2255 xmlXPtrLocationSetAdd(newset, range);
2256 }
2257
2258 /*
2259 * Cleanup
2260 */
2261 if (res != NULL)
2262 xmlXPathFreeObject(res);
2263 if (ctxt->value == tmp) {
2264 res = valuePop(ctxt);
2265 xmlXPathFreeObject(res);
2266 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002267
Owen Taylor3473f882001-02-23 17:55:21 +00002268 ctxt->context->node = NULL;
2269 }
2270
2271 /*
2272 * The result is used as the new evaluation set.
2273 */
2274 xmlXPathFreeObject(obj);
2275 ctxt->context->node = NULL;
2276 ctxt->context->contextSize = -1;
2277 ctxt->context->proximityPosition = -1;
2278 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2279}
2280
2281/**
2282 * xmlXPtrAdvanceNode:
2283 * @cur: the node
William M. Brackce4fc562004-01-22 02:47:18 +00002284 * @level: incremented/decremented to show level in tree
Owen Taylor3473f882001-02-23 17:55:21 +00002285 *
2286 * Advance to the next element or text node in document order
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002287 * TODO: add a stack for entering/exiting entities
Owen Taylor3473f882001-02-23 17:55:21 +00002288 *
2289 * Returns -1 in case of failure, 0 otherwise
2290 */
2291xmlNodePtr
William M. Brackf7eb7942003-12-31 07:59:17 +00002292xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
Owen Taylor3473f882001-02-23 17:55:21 +00002293next:
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002294 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002295 return(NULL);
2296 if (cur->children != NULL) {
2297 cur = cur->children ;
William M. Brackf7eb7942003-12-31 07:59:17 +00002298 if (level != NULL)
2299 (*level)++;
Owen Taylor3473f882001-02-23 17:55:21 +00002300 goto found;
2301 }
William M. Brack4d59e222004-03-08 14:42:31 +00002302skip: /* This label should only be needed if something is wrong! */
Owen Taylor3473f882001-02-23 17:55:21 +00002303 if (cur->next != NULL) {
2304 cur = cur->next;
2305 goto found;
2306 }
2307 do {
2308 cur = cur->parent;
William M. Brackf7eb7942003-12-31 07:59:17 +00002309 if (level != NULL)
2310 (*level)--;
Owen Taylor3473f882001-02-23 17:55:21 +00002311 if (cur == NULL) return(NULL);
2312 if (cur->next != NULL) {
2313 cur = cur->next;
2314 goto found;
2315 }
2316 } while (cur != NULL);
2317
2318found:
2319 if ((cur->type != XML_ELEMENT_NODE) &&
2320 (cur->type != XML_TEXT_NODE) &&
2321 (cur->type != XML_DOCUMENT_NODE) &&
2322 (cur->type != XML_HTML_DOCUMENT_NODE) &&
William M. Brack4d59e222004-03-08 14:42:31 +00002323 (cur->type != XML_CDATA_SECTION_NODE)) {
2324 if (cur->type == XML_ENTITY_REF_NODE) { /* Shouldn't happen */
2325 TODO
2326 goto skip;
2327 }
2328 goto next;
2329 }
Owen Taylor3473f882001-02-23 17:55:21 +00002330 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;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002352 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002353 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 {
William M. Brackf7eb7942003-12-31 07:59:17 +00002369 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002370 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 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002390 * We should have a text (or cdata) node ...
Owen Taylor3473f882001-02-23 17:55:21 +00002391 */
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);
William M. Brackf7eb7942003-12-31 07:59:17 +00002404 cur = xmlXPtrAdvanceNode(cur, NULL);
Daniel Veillard24505b02005-07-28 23:49:35 +00002405 pos = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002406 } 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);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002443 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002444 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002445 if ((end == NULL) || (*end == NULL) ||
2446 ((*end)->type == XML_NAMESPACE_DECL) || (endindex == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002447 return(-1);
2448 cur = start;
Owen Taylor3473f882001-02-23 17:55:21 +00002449 pos = startindex - 1;
2450 stringlen = xmlStrlen(string);
2451
2452 while (stringlen > 0) {
2453 if ((cur == *end) && (pos + stringlen > *endindex))
2454 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002455
2456 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002457 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002458 if (len >= pos + stringlen) {
Owen Taylor3473f882001-02-23 17:55:21 +00002459 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
Owen Taylor3473f882001-02-23 17:55:21 +00002460 if (match) {
2461#ifdef DEBUG_RANGES
2462 xmlGenericError(xmlGenericErrorContext,
2463 "found range %d bytes at index %d of ->",
2464 stringlen, pos + 1);
2465 xmlDebugDumpString(stdout, cur->content);
2466 xmlGenericError(xmlGenericErrorContext, "\n");
2467#endif
2468 *end = cur;
2469 *endindex = pos + stringlen;
2470 return(1);
2471 } else {
2472 return(0);
2473 }
2474 } else {
2475 int sub = len - pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002476 match = (!xmlStrncmp(&cur->content[pos], string, sub));
Owen Taylor3473f882001-02-23 17:55:21 +00002477 if (match) {
2478#ifdef DEBUG_RANGES
2479 xmlGenericError(xmlGenericErrorContext,
2480 "found subrange %d bytes at index %d of ->",
2481 sub, pos + 1);
2482 xmlDebugDumpString(stdout, cur->content);
2483 xmlGenericError(xmlGenericErrorContext, "\n");
2484#endif
2485 string = &string[sub];
2486 stringlen -= sub;
2487 } else {
2488 return(0);
2489 }
2490 }
2491 }
William M. Brackf7eb7942003-12-31 07:59:17 +00002492 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002493 if (cur == NULL)
2494 return(0);
2495 pos = 0;
2496 }
2497 return(1);
2498}
2499
2500/**
2501 * xmlXPtrSearchString:
2502 * @string: the string to search
2503 * @start: the start textnode IN/OUT
2504 * @startindex: the start index IN/OUT
2505 * @end: the end textnode
2506 * @endindex: the end index
2507 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002508 * Search the next occurrence of @string within the document content
Owen Taylor3473f882001-02-23 17:55:21 +00002509 * until the (@end, @endindex) point is reached
2510 *
2511 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2512 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002513 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002514 * of the range
2515 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002516static int
Owen Taylor3473f882001-02-23 17:55:21 +00002517xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2518 xmlNodePtr *end, int *endindex) {
2519 xmlNodePtr cur;
2520 const xmlChar *str;
2521 int pos; /* 0 based */
2522 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002523 xmlChar first;
2524
2525 if (string == NULL)
2526 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002527 if ((start == NULL) || (*start == NULL) ||
2528 ((*start)->type == XML_NAMESPACE_DECL) || (startindex == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002529 return(-1);
2530 if ((end == NULL) || (endindex == NULL))
2531 return(-1);
2532 cur = *start;
Owen Taylor3473f882001-02-23 17:55:21 +00002533 pos = *startindex - 1;
2534 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002535
2536 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002537 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002538 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002539 while (pos <= len) {
2540 if (first != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00002541 str = xmlStrchr(&cur->content[pos], first);
Owen Taylor3473f882001-02-23 17:55:21 +00002542 if (str != NULL) {
2543 pos = (str - (xmlChar *)(cur->content));
2544#ifdef DEBUG_RANGES
2545 xmlGenericError(xmlGenericErrorContext,
2546 "found '%c' at index %d of ->",
2547 first, pos + 1);
2548 xmlDebugDumpString(stdout, cur->content);
2549 xmlGenericError(xmlGenericErrorContext, "\n");
2550#endif
2551 if (xmlXPtrMatchString(string, cur, pos + 1,
2552 end, endindex)) {
2553 *start = cur;
2554 *startindex = pos + 1;
2555 return(1);
2556 }
2557 pos++;
2558 } else {
2559 pos = len + 1;
2560 }
2561 } else {
2562 /*
2563 * An empty string is considered to match before each
2564 * character of the string-value and after the final
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002565 * character.
Owen Taylor3473f882001-02-23 17:55:21 +00002566 */
2567#ifdef DEBUG_RANGES
2568 xmlGenericError(xmlGenericErrorContext,
2569 "found '' at index %d of ->",
2570 pos + 1);
2571 xmlDebugDumpString(stdout, cur->content);
2572 xmlGenericError(xmlGenericErrorContext, "\n");
2573#endif
2574 *start = cur;
2575 *startindex = pos + 1;
2576 *end = cur;
2577 *endindex = pos + 1;
2578 return(1);
2579 }
2580 }
2581 }
2582 if ((cur == *end) && (pos >= *endindex))
2583 return(0);
William M. Brackf7eb7942003-12-31 07:59:17 +00002584 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002585 if (cur == NULL)
2586 return(0);
2587 pos = 1;
2588 }
2589 return(0);
2590}
2591
2592/**
2593 * xmlXPtrGetLastChar:
2594 * @node: the node
2595 * @index: the index
2596 *
2597 * Computes the point coordinates of the last char of this point
2598 *
2599 * Returns -1 in case of failure, 0 otherwise
2600 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002601static int
2602xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002603 xmlNodePtr cur;
2604 int pos, len = 0;
2605
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002606 if ((node == NULL) || (*node == NULL) ||
2607 ((*node)->type == XML_NAMESPACE_DECL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002608 return(-1);
2609 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002610 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002611
Owen Taylor3473f882001-02-23 17:55:21 +00002612 if ((cur->type == XML_ELEMENT_NODE) ||
2613 (cur->type == XML_DOCUMENT_NODE) ||
2614 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2615 if (pos > 0) {
2616 cur = xmlXPtrGetNthChild(cur, pos);
Owen Taylor3473f882001-02-23 17:55:21 +00002617 }
2618 }
2619 while (cur != NULL) {
2620 if (cur->last != NULL)
2621 cur = cur->last;
Daniel Veillard7db37732001-07-12 01:20:08 +00002622 else if ((cur->type != XML_ELEMENT_NODE) &&
2623 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002624 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002625 break;
2626 } else {
2627 return(-1);
2628 }
2629 }
2630 if (cur == NULL)
2631 return(-1);
2632 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002633 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002634 return(0);
2635}
2636
2637/**
2638 * xmlXPtrGetStartPoint:
2639 * @obj: an range
2640 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002641 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002642 *
2643 * read the object and return the start point coordinates.
2644 *
2645 * Returns -1 in case of failure, 0 otherwise
2646 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002647static int
2648xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2649 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002650 return(-1);
2651
2652 switch (obj->type) {
2653 case XPATH_POINT:
2654 *node = obj->user;
2655 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002656 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002657 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002658 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002659 return(0);
2660 case XPATH_RANGE:
2661 *node = obj->user;
2662 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002663 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002664 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002665 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002666 return(0);
2667 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002668 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002669 }
2670 return(-1);
2671}
2672
2673/**
2674 * xmlXPtrGetEndPoint:
2675 * @obj: an range
2676 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002677 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002678 *
2679 * read the object and return the end point coordinates.
2680 *
2681 * Returns -1 in case of failure, 0 otherwise
2682 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002683static int
2684xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2685 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002686 return(-1);
2687
2688 switch (obj->type) {
2689 case XPATH_POINT:
2690 *node = obj->user;
2691 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002692 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002693 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002694 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002695 return(0);
2696 case XPATH_RANGE:
2697 *node = obj->user;
2698 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002699 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002700 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002701 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002702 return(0);
2703 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002704 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002705 }
2706 return(-1);
2707}
2708
2709/**
2710 * xmlXPtrStringRangeFunction:
2711 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002712 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002713 *
2714 * Function implementing the string-range() function
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002715 * range as described in 5.4.2
Owen Taylor3473f882001-02-23 17:55:21 +00002716 *
2717 * ------------------------------
2718 * [Definition: For each location in the location-set argument,
2719 * string-range returns a set of string ranges, a set of substrings in a
2720 * string. Specifically, the string-value of the location is searched for
2721 * substrings that match the string argument, and the resulting location-set
2722 * will contain a range location for each non-overlapping match.]
2723 * An empty string is considered to match before each character of the
2724 * string-value and after the final character. Whitespace in a string
2725 * is matched literally, with no normalization except that provided by
2726 * XML for line ends. The third argument gives the position of the first
2727 * character to be in the resulting range, relative to the start of the
2728 * match. The default value is 1, which makes the range start immediately
2729 * before the first character of the matched string. The fourth argument
2730 * gives the number of characters in the range; the default is that the
2731 * range extends to the end of the matched string.
2732 *
2733 * Element boundaries, as well as entire embedded nodes such as processing
2734 * instructions and comments, are ignored as defined in [XPath].
2735 *
2736 * If the string in the second argument is not found in the string-value
2737 * of the location, or if a value in the third or fourth argument indicates
2738 * a string that is beyond the beginning or end of the document, the
2739 * expression fails.
2740 *
2741 * The points of the range-locations in the returned location-set will
2742 * all be character points.
2743 * ------------------------------
2744 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002745static void
Owen Taylor3473f882001-02-23 17:55:21 +00002746xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillardaac7c682006-03-10 13:40:16 +00002747 int i, startindex, endindex = 0, fendindex;
2748 xmlNodePtr start, end = 0, fend;
Owen Taylor3473f882001-02-23 17:55:21 +00002749 xmlXPathObjectPtr set;
2750 xmlLocationSetPtr oldset;
2751 xmlLocationSetPtr newset;
2752 xmlXPathObjectPtr string;
2753 xmlXPathObjectPtr position = NULL;
2754 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002755 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002756
2757 /*
2758 * Grab the arguments
2759 */
2760 if ((nargs < 2) || (nargs > 4))
2761 XP_ERROR(XPATH_INVALID_ARITY);
2762
2763 if (nargs >= 4) {
2764 CHECK_TYPE(XPATH_NUMBER);
2765 number = valuePop(ctxt);
2766 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002767 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002768 }
2769 if (nargs >= 3) {
2770 CHECK_TYPE(XPATH_NUMBER);
2771 position = valuePop(ctxt);
2772 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002773 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002774 }
2775 CHECK_TYPE(XPATH_STRING);
2776 string = valuePop(ctxt);
2777 if ((ctxt->value == NULL) ||
2778 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2779 (ctxt->value->type != XPATH_NODESET)))
2780 XP_ERROR(XPATH_INVALID_TYPE)
2781
2782 set = valuePop(ctxt);
William M. Brack08171912003-12-29 02:52:11 +00002783 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002784 if (newset == NULL) {
2785 xmlXPathFreeObject(set);
2786 XP_ERROR(XPATH_MEMORY_ERROR);
2787 }
William M. Brack08171912003-12-29 02:52:11 +00002788 if (set->nodesetval == NULL) {
2789 goto error;
2790 }
Owen Taylor3473f882001-02-23 17:55:21 +00002791 if (set->type == XPATH_NODESET) {
2792 xmlXPathObjectPtr tmp;
2793
2794 /*
2795 * First convert to a location set
2796 */
2797 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2798 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002799 if (tmp == NULL)
2800 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002801 set = tmp;
2802 }
2803 oldset = (xmlLocationSetPtr) set->user;
2804
2805 /*
2806 * The loop is to search for each element in the location set
2807 * the list of location set corresponding to that search
2808 */
Owen Taylor3473f882001-02-23 17:55:21 +00002809 for (i = 0;i < oldset->locNr;i++) {
2810#ifdef DEBUG_RANGES
2811 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2812#endif
2813
2814 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2815 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2816 xmlXPtrAdvanceChar(&start, &startindex, 0);
2817 xmlXPtrGetLastChar(&end, &endindex);
2818
2819#ifdef DEBUG_RANGES
2820 xmlGenericError(xmlGenericErrorContext,
2821 "from index %d of ->", startindex);
2822 xmlDebugDumpString(stdout, start->content);
2823 xmlGenericError(xmlGenericErrorContext, "\n");
2824 xmlGenericError(xmlGenericErrorContext,
2825 "to index %d of ->", endindex);
2826 xmlDebugDumpString(stdout, end->content);
2827 xmlGenericError(xmlGenericErrorContext, "\n");
2828#endif
2829 do {
2830 fend = end;
2831 fendindex = endindex;
2832 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2833 &fend, &fendindex);
2834 if (found == 1) {
2835 if (position == NULL) {
2836 xmlXPtrLocationSetAdd(newset,
2837 xmlXPtrNewRange(start, startindex, fend, fendindex));
2838 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2839 pos - 1) == 0) {
2840 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002841 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002842 xmlNodePtr rend;
2843 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002844 rindx = startindex - 1;
2845 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002846 num) == 0) {
2847 xmlXPtrLocationSetAdd(newset,
2848 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002849 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002850 }
2851 } else if ((number != NULL) && (num <= 0)) {
2852 xmlXPtrLocationSetAdd(newset,
2853 xmlXPtrNewRange(start, startindex,
2854 start, startindex));
2855 } else {
2856 xmlXPtrLocationSetAdd(newset,
2857 xmlXPtrNewRange(start, startindex,
2858 fend, fendindex));
2859 }
2860 }
2861 start = fend;
2862 startindex = fendindex;
2863 if (string->stringval[0] == 0)
2864 startindex++;
2865 }
2866 } while (found == 1);
2867 }
2868
2869 /*
2870 * Save the new value and cleanup
2871 */
William M. Brack08171912003-12-29 02:52:11 +00002872error:
Owen Taylor3473f882001-02-23 17:55:21 +00002873 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2874 xmlXPathFreeObject(set);
2875 xmlXPathFreeObject(string);
2876 if (position) xmlXPathFreeObject(position);
2877 if (number) xmlXPathFreeObject(number);
2878}
2879
2880/**
2881 * xmlXPtrEvalRangePredicate:
2882 * @ctxt: the XPointer Parser context
2883 *
2884 * [8] Predicate ::= '[' PredicateExpr ']'
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002885 * [9] PredicateExpr ::= Expr
Owen Taylor3473f882001-02-23 17:55:21 +00002886 *
2887 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2888 * a Location Set instead of a node set
2889 */
2890void
2891xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2892 const xmlChar *cur;
2893 xmlXPathObjectPtr res;
2894 xmlXPathObjectPtr obj, tmp;
2895 xmlLocationSetPtr newset = NULL;
2896 xmlLocationSetPtr oldset;
2897 int i;
2898
Daniel Veillardce682bc2004-11-05 17:22:25 +00002899 if (ctxt == NULL) return;
2900
Owen Taylor3473f882001-02-23 17:55:21 +00002901 SKIP_BLANKS;
2902 if (CUR != '[') {
2903 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2904 }
2905 NEXT;
2906 SKIP_BLANKS;
2907
2908 /*
2909 * Extract the old set, and then evaluate the result of the
2910 * expression for all the element in the set. use it to grow
2911 * up a new set.
2912 */
2913 CHECK_TYPE(XPATH_LOCATIONSET);
2914 obj = valuePop(ctxt);
2915 oldset = obj->user;
2916 ctxt->context->node = NULL;
2917
2918 if ((oldset == NULL) || (oldset->locNr == 0)) {
2919 ctxt->context->contextSize = 0;
2920 ctxt->context->proximityPosition = 0;
2921 xmlXPathEvalExpr(ctxt);
2922 res = valuePop(ctxt);
2923 if (res != NULL)
2924 xmlXPathFreeObject(res);
2925 valuePush(ctxt, obj);
2926 CHECK_ERROR;
2927 } else {
2928 /*
2929 * Save the expression pointer since we will have to evaluate
2930 * it multiple times. Initialize the new set.
2931 */
2932 cur = ctxt->cur;
2933 newset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002934
Owen Taylor3473f882001-02-23 17:55:21 +00002935 for (i = 0; i < oldset->locNr; i++) {
2936 ctxt->cur = cur;
2937
2938 /*
2939 * Run the evaluation with a node list made of a single item
2940 * in the nodeset.
2941 */
2942 ctxt->context->node = oldset->locTab[i]->user;
2943 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2944 valuePush(ctxt, tmp);
2945 ctxt->context->contextSize = oldset->locNr;
2946 ctxt->context->proximityPosition = i + 1;
2947
2948 xmlXPathEvalExpr(ctxt);
2949 CHECK_ERROR;
2950
2951 /*
2952 * The result of the evaluation need to be tested to
2953 * decided whether the filter succeeded or not
2954 */
2955 res = valuePop(ctxt);
2956 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2957 xmlXPtrLocationSetAdd(newset,
2958 xmlXPathObjectCopy(oldset->locTab[i]));
2959 }
2960
2961 /*
2962 * Cleanup
2963 */
2964 if (res != NULL)
2965 xmlXPathFreeObject(res);
2966 if (ctxt->value == tmp) {
2967 res = valuePop(ctxt);
2968 xmlXPathFreeObject(res);
2969 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002970
Owen Taylor3473f882001-02-23 17:55:21 +00002971 ctxt->context->node = NULL;
2972 }
2973
2974 /*
2975 * The result is used as the new evaluation set.
2976 */
2977 xmlXPathFreeObject(obj);
2978 ctxt->context->node = NULL;
2979 ctxt->context->contextSize = -1;
2980 ctxt->context->proximityPosition = -1;
2981 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2982 }
2983 if (CUR != ']') {
2984 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2985 }
2986
2987 NEXT;
2988 SKIP_BLANKS;
2989}
2990
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002991#define bottom_xpointer
2992#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00002993#endif
2994