blob: d74174a318f145e108614770fdda089c9fa9ddaa [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/**
323 * xmlXPtrNewRange:
324 * @start: the starting node
325 * @startindex: the start index
326 * @end: the ending point
327 * @endindex: the ending index
328 *
329 * Create a new xmlXPathObjectPtr of type range
330 *
331 * Returns the newly created object.
332 */
333xmlXPathObjectPtr
334xmlXPtrNewRange(xmlNodePtr start, int startindex,
335 xmlNodePtr end, int endindex) {
336 xmlXPathObjectPtr ret;
337
338 if (start == NULL)
339 return(NULL);
340 if (end == NULL)
341 return(NULL);
342 if (startindex < 0)
343 return(NULL);
344 if (endindex < 0)
345 return(NULL);
346
347 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
348 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000349 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000350 return(NULL);
351 }
352 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
353 ret->type = XPATH_RANGE;
354 ret->user = start;
355 ret->index = startindex;
356 ret->user2 = end;
357 ret->index2 = endindex;
358 xmlXPtrRangeCheckOrder(ret);
359 return(ret);
360}
361
362/**
363 * xmlXPtrNewRangePoints:
364 * @start: the starting point
365 * @end: the ending point
366 *
367 * Create a new xmlXPathObjectPtr of type range using 2 Points
368 *
369 * Returns the newly created object.
370 */
371xmlXPathObjectPtr
372xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
373 xmlXPathObjectPtr ret;
374
375 if (start == NULL)
376 return(NULL);
377 if (end == NULL)
378 return(NULL);
379 if (start->type != XPATH_POINT)
380 return(NULL);
381 if (end->type != XPATH_POINT)
382 return(NULL);
383
384 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
385 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000386 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000387 return(NULL);
388 }
389 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
390 ret->type = XPATH_RANGE;
391 ret->user = start->user;
392 ret->index = start->index;
393 ret->user2 = end->user;
394 ret->index2 = end->index;
395 xmlXPtrRangeCheckOrder(ret);
396 return(ret);
397}
398
399/**
400 * xmlXPtrNewRangePointNode:
401 * @start: the starting point
402 * @end: the ending node
403 *
404 * Create a new xmlXPathObjectPtr of type range from a point to a node
405 *
406 * Returns the newly created object.
407 */
408xmlXPathObjectPtr
409xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
410 xmlXPathObjectPtr ret;
411
412 if (start == NULL)
413 return(NULL);
414 if (end == NULL)
415 return(NULL);
416 if (start->type != XPATH_POINT)
417 return(NULL);
418
419 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
420 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000421 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000422 return(NULL);
423 }
424 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
425 ret->type = XPATH_RANGE;
426 ret->user = start->user;
427 ret->index = start->index;
428 ret->user2 = end;
429 ret->index2 = -1;
430 xmlXPtrRangeCheckOrder(ret);
431 return(ret);
432}
433
434/**
435 * xmlXPtrNewRangeNodePoint:
436 * @start: the starting node
437 * @end: the ending point
438 *
439 * Create a new xmlXPathObjectPtr of type range from a node to a point
440 *
441 * Returns the newly created object.
442 */
443xmlXPathObjectPtr
444xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
445 xmlXPathObjectPtr ret;
446
447 if (start == NULL)
448 return(NULL);
449 if (end == NULL)
450 return(NULL);
451 if (start->type != XPATH_POINT)
452 return(NULL);
453 if (end->type != XPATH_POINT)
454 return(NULL);
455
456 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
457 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000458 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000459 return(NULL);
460 }
461 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
462 ret->type = XPATH_RANGE;
463 ret->user = start;
464 ret->index = -1;
465 ret->user2 = end->user;
466 ret->index2 = end->index;
467 xmlXPtrRangeCheckOrder(ret);
468 return(ret);
469}
470
471/**
472 * xmlXPtrNewRangeNodes:
473 * @start: the starting node
474 * @end: the ending node
475 *
476 * Create a new xmlXPathObjectPtr of type range using 2 nodes
477 *
478 * Returns the newly created object.
479 */
480xmlXPathObjectPtr
481xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
482 xmlXPathObjectPtr ret;
483
484 if (start == NULL)
485 return(NULL);
486 if (end == NULL)
487 return(NULL);
488
489 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
490 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000491 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000492 return(NULL);
493 }
494 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
495 ret->type = XPATH_RANGE;
496 ret->user = start;
497 ret->index = -1;
498 ret->user2 = end;
499 ret->index2 = -1;
500 xmlXPtrRangeCheckOrder(ret);
501 return(ret);
502}
503
504/**
505 * xmlXPtrNewCollapsedRange:
506 * @start: the starting and ending node
507 *
508 * Create a new xmlXPathObjectPtr of type range using a single nodes
509 *
510 * Returns the newly created object.
511 */
512xmlXPathObjectPtr
513xmlXPtrNewCollapsedRange(xmlNodePtr start) {
514 xmlXPathObjectPtr ret;
515
516 if (start == NULL)
517 return(NULL);
518
519 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
520 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000521 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000522 return(NULL);
523 }
524 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
525 ret->type = XPATH_RANGE;
526 ret->user = start;
527 ret->index = -1;
528 ret->user2 = NULL;
529 ret->index2 = -1;
530 return(ret);
531}
532
533/**
534 * xmlXPtrNewRangeNodeObject:
535 * @start: the starting node
536 * @end: the ending object
537 *
538 * Create a new xmlXPathObjectPtr of type range from a not to an object
539 *
540 * Returns the newly created object.
541 */
542xmlXPathObjectPtr
543xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
544 xmlXPathObjectPtr ret;
545
546 if (start == NULL)
547 return(NULL);
548 if (end == NULL)
549 return(NULL);
550 switch (end->type) {
551 case XPATH_POINT:
William M. Brack72ee48d2003-12-30 08:30:19 +0000552 case XPATH_RANGE:
Owen Taylor3473f882001-02-23 17:55:21 +0000553 break;
554 case XPATH_NODESET:
555 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800556 * Empty set ...
Owen Taylor3473f882001-02-23 17:55:21 +0000557 */
558 if (end->nodesetval->nodeNr <= 0)
559 return(NULL);
560 break;
561 default:
Daniel Veillard3d97e662004-11-04 10:49:00 +0000562 /* TODO */
Owen Taylor3473f882001-02-23 17:55:21 +0000563 return(NULL);
564 }
565
566 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
567 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000568 xmlXPtrErrMemory("allocating range");
Owen Taylor3473f882001-02-23 17:55:21 +0000569 return(NULL);
570 }
571 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
572 ret->type = XPATH_RANGE;
573 ret->user = start;
574 ret->index = -1;
575 switch (end->type) {
576 case XPATH_POINT:
577 ret->user2 = end->user;
578 ret->index2 = end->index;
William M. Brack72ee48d2003-12-30 08:30:19 +0000579 break;
580 case XPATH_RANGE:
581 ret->user2 = end->user2;
582 ret->index2 = end->index2;
583 break;
Owen Taylor3473f882001-02-23 17:55:21 +0000584 case XPATH_NODESET: {
585 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
586 ret->index2 = -1;
587 break;
588 }
589 default:
590 STRANGE
591 return(NULL);
592 }
593 xmlXPtrRangeCheckOrder(ret);
594 return(ret);
595}
596
597#define XML_RANGESET_DEFAULT 10
598
599/**
600 * xmlXPtrLocationSetCreate:
601 * @val: an initial xmlXPathObjectPtr, or NULL
602 *
603 * Create a new xmlLocationSetPtr of type double and of value @val
604 *
605 * Returns the newly created object.
606 */
607xmlLocationSetPtr
608xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
609 xmlLocationSetPtr ret;
610
611 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
612 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000613 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000614 return(NULL);
615 }
616 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
617 if (val != NULL) {
618 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
619 sizeof(xmlXPathObjectPtr));
620 if (ret->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000621 xmlXPtrErrMemory("allocating locationset");
622 xmlFree(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000623 return(NULL);
624 }
625 memset(ret->locTab, 0 ,
626 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
627 ret->locMax = XML_RANGESET_DEFAULT;
628 ret->locTab[ret->locNr++] = val;
629 }
630 return(ret);
631}
632
633/**
634 * xmlXPtrLocationSetAdd:
635 * @cur: the initial range set
636 * @val: a new xmlXPathObjectPtr
637 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000638 * add a new xmlXPathObjectPtr to an existing LocationSet
Owen Taylor3473f882001-02-23 17:55:21 +0000639 * If the location already exist in the set @val is freed.
640 */
641void
642xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
643 int i;
644
Daniel Veillardce682bc2004-11-05 17:22:25 +0000645 if ((cur == NULL) || (val == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000646
647 /*
648 * check against doublons
649 */
650 for (i = 0;i < cur->locNr;i++) {
651 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
652 xmlXPathFreeObject(val);
653 return;
654 }
655 }
656
657 /*
658 * grow the locTab if needed
659 */
660 if (cur->locMax == 0) {
661 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
662 sizeof(xmlXPathObjectPtr));
663 if (cur->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000664 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000665 return;
666 }
667 memset(cur->locTab, 0 ,
668 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
669 cur->locMax = XML_RANGESET_DEFAULT;
670 } else if (cur->locNr == cur->locMax) {
671 xmlXPathObjectPtr *temp;
672
673 cur->locMax *= 2;
674 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
675 sizeof(xmlXPathObjectPtr));
676 if (temp == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000677 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000678 return;
679 }
680 cur->locTab = temp;
681 }
682 cur->locTab[cur->locNr++] = val;
683}
684
685/**
686 * xmlXPtrLocationSetMerge:
687 * @val1: the first LocationSet
688 * @val2: the second LocationSet
689 *
690 * Merges two rangesets, all ranges from @val2 are added to @val1
691 *
692 * Returns val1 once extended or NULL in case of error.
693 */
694xmlLocationSetPtr
695xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
696 int i;
697
698 if (val1 == NULL) return(NULL);
699 if (val2 == NULL) return(val1);
700
701 /*
702 * !!!!! this can be optimized a lot, knowing that both
703 * val1 and val2 already have unicity of their values.
704 */
705
706 for (i = 0;i < val2->locNr;i++)
707 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
708
709 return(val1);
710}
711
712/**
713 * xmlXPtrLocationSetDel:
714 * @cur: the initial range set
715 * @val: an xmlXPathObjectPtr
716 *
717 * Removes an xmlXPathObjectPtr from an existing LocationSet
718 */
719void
720xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
721 int i;
722
723 if (cur == NULL) return;
724 if (val == NULL) return;
725
726 /*
727 * check against doublons
728 */
729 for (i = 0;i < cur->locNr;i++)
730 if (cur->locTab[i] == val) break;
731
732 if (i >= cur->locNr) {
733#ifdef DEBUG
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800734 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard913d6e02001-11-28 14:53:53 +0000735 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000736#endif
737 return;
738 }
739 cur->locNr--;
740 for (;i < cur->locNr;i++)
741 cur->locTab[i] = cur->locTab[i + 1];
742 cur->locTab[cur->locNr] = NULL;
743}
744
745/**
746 * xmlXPtrLocationSetRemove:
747 * @cur: the initial range set
748 * @val: the index to remove
749 *
750 * Removes an entry from an existing LocationSet list.
751 */
752void
753xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
754 if (cur == NULL) return;
755 if (val >= cur->locNr) return;
756 cur->locNr--;
757 for (;val < cur->locNr;val++)
758 cur->locTab[val] = cur->locTab[val + 1];
759 cur->locTab[cur->locNr] = NULL;
760}
761
762/**
763 * xmlXPtrFreeLocationSet:
764 * @obj: the xmlLocationSetPtr to free
765 *
766 * Free the LocationSet compound (not the actual ranges !).
767 */
768void
769xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
770 int i;
771
772 if (obj == NULL) return;
773 if (obj->locTab != NULL) {
774 for (i = 0;i < obj->locNr; i++) {
775 xmlXPathFreeObject(obj->locTab[i]);
776 }
Owen Taylor3473f882001-02-23 17:55:21 +0000777 xmlFree(obj->locTab);
778 }
Owen Taylor3473f882001-02-23 17:55:21 +0000779 xmlFree(obj);
780}
781
782/**
783 * xmlXPtrNewLocationSetNodes:
784 * @start: the start NodePtr value
785 * @end: the end NodePtr value or NULL
786 *
787 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
788 * it with the single range made of the two nodes @start and @end
789 *
790 * Returns the newly created object.
791 */
792xmlXPathObjectPtr
793xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
794 xmlXPathObjectPtr ret;
795
796 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
797 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000798 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000799 return(NULL);
800 }
801 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
802 ret->type = XPATH_LOCATIONSET;
803 if (end == NULL)
804 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
805 else
806 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
807 return(ret);
808}
809
810/**
811 * xmlXPtrNewLocationSetNodeSet:
812 * @set: a node set
813 *
814 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
815 * it with all the nodes from @set
816 *
817 * Returns the newly created object.
818 */
819xmlXPathObjectPtr
820xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
821 xmlXPathObjectPtr ret;
822
823 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
824 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000825 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000826 return(NULL);
827 }
828 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
829 ret->type = XPATH_LOCATIONSET;
830 if (set != NULL) {
831 int i;
832 xmlLocationSetPtr newset;
833
834 newset = xmlXPtrLocationSetCreate(NULL);
835 if (newset == NULL)
836 return(ret);
837
838 for (i = 0;i < set->nodeNr;i++)
839 xmlXPtrLocationSetAdd(newset,
840 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
841
842 ret->user = (void *) newset;
843 }
844 return(ret);
845}
846
847/**
848 * xmlXPtrWrapLocationSet:
849 * @val: the LocationSet value
850 *
851 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
852 *
853 * Returns the newly created object.
854 */
855xmlXPathObjectPtr
856xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
857 xmlXPathObjectPtr ret;
858
859 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
860 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000861 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000862 return(NULL);
863 }
864 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
865 ret->type = XPATH_LOCATIONSET;
866 ret->user = (void *) val;
867 return(ret);
868}
869
870/************************************************************************
871 * *
872 * The parser *
873 * *
874 ************************************************************************/
875
Daniel Veillard9a237c92003-02-13 15:52:58 +0000876static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
877
Owen Taylor3473f882001-02-23 17:55:21 +0000878/*
879 * Macros for accessing the content. Those should be used only by the parser,
880 * and not exported.
881 *
882 * Dirty macros, i.e. one need to make assumption on the context to use them
883 *
884 * CUR_PTR return the current pointer to the xmlChar to be parsed.
885 * CUR returns the current xmlChar value, i.e. a 8 bit value
886 * in ISO-Latin or UTF-8.
887 * This should be used internally by the parser
888 * only to compare to ASCII values otherwise it would break when
889 * running with UTF-8 encoding.
890 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
891 * to compare on ASCII based substring.
892 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
893 * strings within the parser.
894 * CURRENT Returns the current char value, with the full decoding of
895 * UTF-8 if we are using this mode. It returns an int.
896 * NEXT Skip to the next character, this does the proper decoding
897 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
898 * It returns the pointer to the current xmlChar.
899 */
900
901#define CUR (*ctxt->cur)
902#define SKIP(val) ctxt->cur += (val)
903#define NXT(val) ctxt->cur[(val)]
904#define CUR_PTR ctxt->cur
905
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800906#define SKIP_BLANKS \
William M. Brack272693c2003-11-14 16:20:34 +0000907 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Owen Taylor3473f882001-02-23 17:55:21 +0000908
909#define CURRENT (*ctxt->cur)
910#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
911
912/*
913 * xmlXPtrGetChildNo:
914 * @ctxt: the XPointer Parser context
915 * @index: the child number
916 *
917 * Move the current node of the nodeset on the stack to the
918 * given child if found
919 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000920static void
921xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000922 xmlNodePtr cur = NULL;
923 xmlXPathObjectPtr obj;
924 xmlNodeSetPtr oldset;
925
926 CHECK_TYPE(XPATH_NODESET);
927 obj = valuePop(ctxt);
928 oldset = obj->nodesetval;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000929 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000930 xmlXPathFreeObject(obj);
931 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
932 return;
933 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000934 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
Owen Taylor3473f882001-02-23 17:55:21 +0000935 if (cur == NULL) {
936 xmlXPathFreeObject(obj);
937 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
938 return;
939 }
940 oldset->nodeTab[0] = cur;
941 valuePush(ctxt, obj);
942}
943
944/**
945 * xmlXPtrEvalXPtrPart:
946 * @ctxt: the XPointer Parser context
947 * @name: the preparsed Scheme for the XPtrPart
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800948 *
Owen Taylor3473f882001-02-23 17:55:21 +0000949 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
950 * | Scheme '(' SchemeSpecificExpr ')'
951 *
952 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
953 *
954 * SchemeSpecificExpr ::= StringWithBalancedParens
955 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800956 * StringWithBalancedParens ::=
Owen Taylor3473f882001-02-23 17:55:21 +0000957 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
958 * [VC: Parenthesis escaping]
959 *
960 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
961 *
962 * VC: Parenthesis escaping:
963 * The end of an XPointer part is signaled by the right parenthesis ")"
964 * character that is balanced with the left parenthesis "(" character
965 * that began the part. Any unbalanced parenthesis character inside the
966 * expression, even within literals, must be escaped with a circumflex (^)
967 * character preceding it. If the expression contains any literal
968 * occurrences of the circumflex, each must be escaped with an additional
969 * circumflex (that is, ^^). If the unescaped parentheses in the expression
970 * are not balanced, a syntax error results.
971 *
972 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000973 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800974 *
Owen Taylor3473f882001-02-23 17:55:21 +0000975 * TODO: there is no new scheme registration mechanism
976 */
977
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000978static void
Owen Taylor3473f882001-02-23 17:55:21 +0000979xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
980 xmlChar *buffer, *cur;
981 int len;
982 int level;
983
984 if (name == NULL)
985 name = xmlXPathParseName(ctxt);
986 if (name == NULL)
987 XP_ERROR(XPATH_EXPR_ERROR);
988
989 if (CUR != '(')
990 XP_ERROR(XPATH_EXPR_ERROR);
991 NEXT;
992 level = 1;
993
994 len = xmlStrlen(ctxt->cur);
995 len++;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000996 buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +0000997 if (buffer == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000998 xmlXPtrErrMemory("allocating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +0000999 return;
1000 }
1001
1002 cur = buffer;
1003 while (CUR != 0) {
1004 if (CUR == ')') {
1005 level--;
1006 if (level == 0) {
1007 NEXT;
1008 break;
1009 }
Owen Taylor3473f882001-02-23 17:55:21 +00001010 } else if (CUR == '(') {
1011 level++;
Owen Taylor3473f882001-02-23 17:55:21 +00001012 } else if (CUR == '^') {
Jüri Aedlad8e1fae2012-05-07 15:06:56 +08001013 if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
1014 NEXT;
1015 }
Owen Taylor3473f882001-02-23 17:55:21 +00001016 }
Jüri Aedlad8e1fae2012-05-07 15:06:56 +08001017 *cur++ = CUR;
Owen Taylor3473f882001-02-23 17:55:21 +00001018 NEXT;
1019 }
1020 *cur = 0;
1021
1022 if ((level != 0) && (CUR == 0)) {
1023 xmlFree(buffer);
1024 XP_ERROR(XPTR_SYNTAX_ERROR);
1025 }
1026
1027 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
1028 const xmlChar *left = CUR_PTR;
1029
1030 CUR_PTR = buffer;
William M. Brack72ee48d2003-12-30 08:30:19 +00001031 /*
1032 * To evaluate an xpointer scheme element (4.3) we need:
1033 * context initialized to the root
1034 * context position initalized to 1
1035 * context size initialized to 1
1036 */
1037 ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
1038 ctxt->context->proximityPosition = 1;
1039 ctxt->context->contextSize = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001040 xmlXPathEvalExpr(ctxt);
1041 CUR_PTR=left;
Daniel Veillard9a237c92003-02-13 15:52:58 +00001042 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
1043 const xmlChar *left = CUR_PTR;
1044 xmlChar *name2;
1045
1046 CUR_PTR = buffer;
1047 if (buffer[0] == '/') {
1048 xmlXPathRoot(ctxt);
1049 xmlXPtrEvalChildSeq(ctxt, NULL);
1050 } else {
1051 name2 = xmlXPathParseName(ctxt);
1052 if (name2 == NULL) {
1053 CUR_PTR = left;
1054 xmlFree(buffer);
1055 XP_ERROR(XPATH_EXPR_ERROR);
1056 }
1057 xmlXPtrEvalChildSeq(ctxt, name2);
1058 }
1059 CUR_PTR = left;
Owen Taylor3473f882001-02-23 17:55:21 +00001060#ifdef XPTR_XMLNS_SCHEME
1061 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1062 const xmlChar *left = CUR_PTR;
1063 xmlChar *prefix;
1064 xmlChar *URI;
1065 xmlURIPtr value;
1066
1067 CUR_PTR = buffer;
1068 prefix = xmlXPathParseNCName(ctxt);
1069 if (prefix == NULL) {
1070 xmlFree(buffer);
1071 xmlFree(name);
1072 XP_ERROR(XPTR_SYNTAX_ERROR);
1073 }
1074 SKIP_BLANKS;
1075 if (CUR != '=') {
1076 xmlFree(prefix);
1077 xmlFree(buffer);
1078 xmlFree(name);
1079 XP_ERROR(XPTR_SYNTAX_ERROR);
1080 }
1081 NEXT;
1082 SKIP_BLANKS;
1083 /* @@ check escaping in the XPointer WD */
1084
1085 value = xmlParseURI((const char *)ctxt->cur);
1086 if (value == NULL) {
1087 xmlFree(prefix);
1088 xmlFree(buffer);
1089 xmlFree(name);
1090 XP_ERROR(XPTR_SYNTAX_ERROR);
1091 }
1092 URI = xmlSaveUri(value);
1093 xmlFreeURI(value);
1094 if (URI == NULL) {
1095 xmlFree(prefix);
1096 xmlFree(buffer);
1097 xmlFree(name);
1098 XP_ERROR(XPATH_MEMORY_ERROR);
1099 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001100
Owen Taylor3473f882001-02-23 17:55:21 +00001101 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1102 CUR_PTR = left;
Daniel Veillard56f21f22002-11-06 15:49:46 +00001103 xmlFree(URI);
1104 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001105#endif /* XPTR_XMLNS_SCHEME */
1106 } else {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001107 xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
1108 "unsupported scheme '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001109 }
1110 xmlFree(buffer);
1111 xmlFree(name);
1112}
1113
1114/**
1115 * xmlXPtrEvalFullXPtr:
1116 * @ctxt: the XPointer Parser context
1117 * @name: the preparsed Scheme for the first XPtrPart
1118 *
1119 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1120 *
1121 * As the specs says:
1122 * -----------
1123 * When multiple XPtrParts are provided, they must be evaluated in
1124 * left-to-right order. If evaluation of one part fails, the nexti
1125 * is evaluated. The following conditions cause XPointer part failure:
1126 *
1127 * - An unknown scheme
1128 * - A scheme that does not locate any sub-resource present in the resource
1129 * - A scheme that is not applicable to the media type of the resource
1130 *
1131 * The XPointer application must consume a failed XPointer part and
1132 * attempt to evaluate the next one, if any. The result of the first
1133 * XPointer part whose evaluation succeeds is taken to be the fragment
1134 * located by the XPointer as a whole. If all the parts fail, the result
1135 * for the XPointer as a whole is a sub-resource error.
1136 * -----------
1137 *
1138 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001139 * expressions or other schemes.
Owen Taylor3473f882001-02-23 17:55:21 +00001140 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001141static void
Owen Taylor3473f882001-02-23 17:55:21 +00001142xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1143 if (name == NULL)
1144 name = xmlXPathParseName(ctxt);
1145 if (name == NULL)
1146 XP_ERROR(XPATH_EXPR_ERROR);
1147 while (name != NULL) {
Jakub Wilk928d7032009-07-29 12:24:11 +02001148 ctxt->error = XPATH_EXPRESSION_OK;
Owen Taylor3473f882001-02-23 17:55:21 +00001149 xmlXPtrEvalXPtrPart(ctxt, name);
1150
1151 /* in case of syntax error, break here */
Jakub Wilk928d7032009-07-29 12:24:11 +02001152 if ((ctxt->error != XPATH_EXPRESSION_OK) &&
1153 (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
Owen Taylor3473f882001-02-23 17:55:21 +00001154 return;
1155
1156 /*
1157 * If the returned value is a non-empty nodeset
1158 * or location set, return here.
1159 */
1160 if (ctxt->value != NULL) {
1161 xmlXPathObjectPtr obj = ctxt->value;
1162
1163 switch (obj->type) {
1164 case XPATH_LOCATIONSET: {
1165 xmlLocationSetPtr loc = ctxt->value->user;
1166 if ((loc != NULL) && (loc->locNr > 0))
1167 return;
1168 break;
1169 }
1170 case XPATH_NODESET: {
1171 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1172 if ((loc != NULL) && (loc->nodeNr > 0))
1173 return;
1174 break;
1175 }
1176 default:
1177 break;
1178 }
1179
1180 /*
1181 * Evaluating to improper values is equivalent to
1182 * a sub-resource error, clean-up the stack
1183 */
1184 do {
1185 obj = valuePop(ctxt);
1186 if (obj != NULL) {
1187 xmlXPathFreeObject(obj);
1188 }
1189 } while (obj != NULL);
1190 }
1191
1192 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001193 * Is there another XPointer part.
Owen Taylor3473f882001-02-23 17:55:21 +00001194 */
1195 SKIP_BLANKS;
1196 name = xmlXPathParseName(ctxt);
1197 }
1198}
1199
1200/**
1201 * xmlXPtrEvalChildSeq:
1202 * @ctxt: the XPointer Parser context
1203 * @name: a possible ID name of the child sequence
1204 *
1205 * ChildSeq ::= '/1' ('/' [0-9]*)*
1206 * | Name ('/' [0-9]*)+
1207 *
1208 * Parse and evaluate a Child Sequence. This routine also handle the
1209 * case of a Bare Name used to get a document ID.
1210 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001211static void
Owen Taylor3473f882001-02-23 17:55:21 +00001212xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1213 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001214 * XPointer don't allow by syntax to address in mutirooted trees
Owen Taylor3473f882001-02-23 17:55:21 +00001215 * this might prove useful in some cases, warn about it.
1216 */
1217 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001218 xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
1219 "warning: ChildSeq not starting by /1\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001220 }
1221
1222 if (name != NULL) {
1223 valuePush(ctxt, xmlXPathNewString(name));
1224 xmlFree(name);
1225 xmlXPathIdFunction(ctxt, 1);
1226 CHECK_ERROR;
1227 }
1228
1229 while (CUR == '/') {
1230 int child = 0;
1231 NEXT;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001232
Owen Taylor3473f882001-02-23 17:55:21 +00001233 while ((CUR >= '0') && (CUR <= '9')) {
1234 child = child * 10 + (CUR - '0');
1235 NEXT;
1236 }
1237 xmlXPtrGetChildNo(ctxt, child);
1238 }
1239}
1240
1241
1242/**
1243 * xmlXPtrEvalXPointer:
1244 * @ctxt: the XPointer Parser context
1245 *
1246 * XPointer ::= Name
1247 * | ChildSeq
1248 * | FullXPtr
1249 *
1250 * Parse and evaluate an XPointer
1251 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001252static void
Owen Taylor3473f882001-02-23 17:55:21 +00001253xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001254 if (ctxt->valueTab == NULL) {
1255 /* Allocate the value stack */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001256 ctxt->valueTab = (xmlXPathObjectPtr *)
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001257 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1258 if (ctxt->valueTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001259 xmlXPtrErrMemory("allocating evaluation context");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001260 return;
1261 }
1262 ctxt->valueNr = 0;
1263 ctxt->valueMax = 10;
1264 ctxt->value = NULL;
Daniel Veillardf5048b32011-08-18 17:10:13 +08001265 ctxt->valueFrame = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001266 }
Owen Taylor3473f882001-02-23 17:55:21 +00001267 SKIP_BLANKS;
1268 if (CUR == '/') {
1269 xmlXPathRoot(ctxt);
1270 xmlXPtrEvalChildSeq(ctxt, NULL);
1271 } else {
1272 xmlChar *name;
1273
1274 name = xmlXPathParseName(ctxt);
1275 if (name == NULL)
1276 XP_ERROR(XPATH_EXPR_ERROR);
1277 if (CUR == '(') {
1278 xmlXPtrEvalFullXPtr(ctxt, name);
1279 /* Short evaluation */
1280 return;
1281 } else {
1282 /* this handle both Bare Names and Child Sequences */
1283 xmlXPtrEvalChildSeq(ctxt, name);
1284 }
1285 }
1286 SKIP_BLANKS;
1287 if (CUR != 0)
1288 XP_ERROR(XPATH_EXPR_ERROR);
1289}
1290
1291
1292/************************************************************************
1293 * *
1294 * General routines *
1295 * *
1296 ************************************************************************/
1297
Daniel Veillard8ed10722009-08-20 19:17:36 +02001298static
Owen Taylor3473f882001-02-23 17:55:21 +00001299void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001300static
Owen Taylor3473f882001-02-23 17:55:21 +00001301void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001302static
Owen Taylor3473f882001-02-23 17:55:21 +00001303void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001304static
Owen Taylor3473f882001-02-23 17:55:21 +00001305void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001306static
Owen Taylor3473f882001-02-23 17:55:21 +00001307void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001308static
Owen Taylor3473f882001-02-23 17:55:21 +00001309void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001310static
Owen Taylor3473f882001-02-23 17:55:21 +00001311void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1312
1313/**
1314 * xmlXPtrNewContext:
1315 * @doc: the XML document
1316 * @here: the node that directly contains the XPointer being evaluated or NULL
1317 * @origin: the element from which a user or program initiated traversal of
1318 * the link, or NULL.
1319 *
1320 * Create a new XPointer context
1321 *
1322 * Returns the xmlXPathContext just allocated.
1323 */
1324xmlXPathContextPtr
1325xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1326 xmlXPathContextPtr ret;
1327
1328 ret = xmlXPathNewContext(doc);
1329 if (ret == NULL)
1330 return(ret);
1331 ret->xptr = 1;
1332 ret->here = here;
1333 ret->origin = origin;
1334
Owen Taylor3473f882001-02-23 17:55:21 +00001335 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1336 xmlXPtrRangeFunction);
1337 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1338 xmlXPtrRangeInsideFunction);
1339 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1340 xmlXPtrStringRangeFunction);
1341 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1342 xmlXPtrStartPointFunction);
1343 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1344 xmlXPtrEndPointFunction);
1345 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1346 xmlXPtrHereFunction);
1347 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1348 xmlXPtrOriginFunction);
1349
1350 return(ret);
1351}
1352
1353/**
1354 * xmlXPtrEval:
1355 * @str: the XPointer expression
1356 * @ctx: the XPointer context
1357 *
1358 * Evaluate the XPath Location Path in the given context.
1359 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001360 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +00001361 * the caller has to free the object.
1362 */
1363xmlXPathObjectPtr
1364xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1365 xmlXPathParserContextPtr ctxt;
1366 xmlXPathObjectPtr res = NULL, tmp;
1367 xmlXPathObjectPtr init = NULL;
1368 int stack = 0;
1369
1370 xmlXPathInit();
1371
1372 if ((ctx == NULL) || (str == NULL))
1373 return(NULL);
1374
1375 ctxt = xmlXPathNewParserContext(str, ctx);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001376 if (ctxt == NULL)
1377 return(NULL);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001378 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001379 xmlXPtrEvalXPointer(ctxt);
1380
1381 if ((ctxt->value != NULL) &&
1382 (ctxt->value->type != XPATH_NODESET) &&
1383 (ctxt->value->type != XPATH_LOCATIONSET)) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001384 xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
1385 "xmlXPtrEval: evaluation failed to return a node set\n",
1386 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001387 } else {
1388 res = valuePop(ctxt);
1389 }
1390
1391 do {
1392 tmp = valuePop(ctxt);
1393 if (tmp != NULL) {
1394 if (tmp != init) {
1395 if (tmp->type == XPATH_NODESET) {
1396 /*
1397 * Evaluation may push a root nodeset which is unused
1398 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001399 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001400 set = tmp->nodesetval;
1401 if ((set->nodeNr != 1) ||
1402 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1403 stack++;
1404 } else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001405 stack++;
Owen Taylor3473f882001-02-23 17:55:21 +00001406 }
1407 xmlXPathFreeObject(tmp);
1408 }
1409 } while (tmp != NULL);
1410 if (stack != 0) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001411 xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
1412 "xmlXPtrEval: object(s) left on the eval stack\n",
1413 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001414 }
1415 if (ctxt->error != XPATH_EXPRESSION_OK) {
1416 xmlXPathFreeObject(res);
1417 res = NULL;
1418 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001419
Owen Taylor3473f882001-02-23 17:55:21 +00001420 xmlXPathFreeParserContext(ctxt);
1421 return(res);
1422}
1423
1424/**
1425 * xmlXPtrBuildRangeNodeList:
1426 * @range: a range object
1427 *
1428 * Build a node list tree copy of the range
1429 *
1430 * Returns an xmlNodePtr list or NULL.
1431 * the caller has to free the node tree.
1432 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001433static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001434xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1435 /* pointers to generated nodes */
1436 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1437 /* pointers to traversal nodes */
1438 xmlNodePtr start, cur, end;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001439 int index1, index2;
Owen Taylor3473f882001-02-23 17:55:21 +00001440
1441 if (range == NULL)
1442 return(NULL);
1443 if (range->type != XPATH_RANGE)
1444 return(NULL);
1445 start = (xmlNodePtr) range->user;
1446
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001447 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00001448 return(NULL);
1449 end = range->user2;
1450 if (end == NULL)
1451 return(xmlCopyNode(start, 1));
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001452 if (end->type == XML_NAMESPACE_DECL)
1453 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001454
1455 cur = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001456 index1 = range->index;
Owen Taylor3473f882001-02-23 17:55:21 +00001457 index2 = range->index2;
1458 while (cur != NULL) {
1459 if (cur == end) {
1460 if (cur->type == XML_TEXT_NODE) {
1461 const xmlChar *content = cur->content;
1462 int len;
1463
1464 if (content == NULL) {
1465 tmp = xmlNewTextLen(NULL, 0);
1466 } else {
1467 len = index2;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001468 if ((cur == start) && (index1 > 1)) {
1469 content += (index1 - 1);
1470 len -= (index1 - 1);
1471 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001472 } else {
1473 len = index2;
1474 }
1475 tmp = xmlNewTextLen(content, len);
1476 }
1477 /* single sub text node selection */
1478 if (list == NULL)
1479 return(tmp);
1480 /* prune and return full set */
1481 if (last != NULL)
1482 xmlAddNextSibling(last, tmp);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001483 else
Owen Taylor3473f882001-02-23 17:55:21 +00001484 xmlAddChild(parent, tmp);
1485 return(list);
1486 } else {
1487 tmp = xmlCopyNode(cur, 0);
1488 if (list == NULL)
1489 list = tmp;
1490 else {
1491 if (last != NULL)
1492 xmlAddNextSibling(last, tmp);
1493 else
1494 xmlAddChild(parent, tmp);
1495 }
1496 last = NULL;
1497 parent = tmp;
1498
1499 if (index2 > 1) {
1500 end = xmlXPtrGetNthChild(cur, index2 - 1);
1501 index2 = 0;
1502 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001503 if ((cur == start) && (index1 > 1)) {
1504 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1505 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001506 } else {
1507 cur = cur->children;
1508 }
1509 /*
1510 * Now gather the remaining nodes from cur to end
1511 */
1512 continue; /* while */
1513 }
1514 } else if ((cur == start) &&
1515 (list == NULL) /* looks superfluous but ... */ ) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001516 if ((cur->type == XML_TEXT_NODE) ||
1517 (cur->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001518 const xmlChar *content = cur->content;
1519
1520 if (content == NULL) {
1521 tmp = xmlNewTextLen(NULL, 0);
1522 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001523 if (index1 > 1) {
1524 content += (index1 - 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001525 }
1526 tmp = xmlNewText(content);
1527 }
1528 last = list = tmp;
1529 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001530 if ((cur == start) && (index1 > 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001531 tmp = xmlCopyNode(cur, 0);
1532 list = tmp;
1533 parent = tmp;
1534 last = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001535 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1536 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001537 /*
1538 * Now gather the remaining nodes from cur to end
1539 */
1540 continue; /* while */
1541 }
1542 tmp = xmlCopyNode(cur, 1);
1543 list = tmp;
1544 parent = NULL;
1545 last = tmp;
1546 }
1547 } else {
1548 tmp = NULL;
1549 switch (cur->type) {
1550 case XML_DTD_NODE:
1551 case XML_ELEMENT_DECL:
1552 case XML_ATTRIBUTE_DECL:
1553 case XML_ENTITY_NODE:
1554 /* Do not copy DTD informations */
1555 break;
1556 case XML_ENTITY_DECL:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001557 TODO /* handle crossing entities -> stack needed */
Owen Taylor3473f882001-02-23 17:55:21 +00001558 break;
1559 case XML_XINCLUDE_START:
1560 case XML_XINCLUDE_END:
1561 /* don't consider it part of the tree content */
1562 break;
1563 case XML_ATTRIBUTE_NODE:
1564 /* Humm, should not happen ! */
1565 STRANGE
1566 break;
1567 default:
1568 tmp = xmlCopyNode(cur, 1);
1569 break;
1570 }
1571 if (tmp != NULL) {
1572 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1573 STRANGE
1574 return(NULL);
1575 }
1576 if (last != NULL)
1577 xmlAddNextSibling(last, tmp);
1578 else {
1579 xmlAddChild(parent, tmp);
1580 last = tmp;
1581 }
1582 }
1583 }
1584 /*
1585 * Skip to next node in document order
1586 */
1587 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1588 STRANGE
1589 return(NULL);
1590 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001591 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001592 }
1593 return(list);
1594}
1595
1596/**
1597 * xmlXPtrBuildNodeList:
1598 * @obj: the XPointer result from the evaluation.
1599 *
1600 * Build a node list tree copy of the XPointer result.
Daniel Veillard39196eb2001-06-19 18:09:42 +00001601 * This will drop Attributes and Namespace declarations.
Owen Taylor3473f882001-02-23 17:55:21 +00001602 *
1603 * Returns an xmlNodePtr list or NULL.
1604 * the caller has to free the node tree.
1605 */
1606xmlNodePtr
1607xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1608 xmlNodePtr list = NULL, last = NULL;
1609 int i;
1610
1611 if (obj == NULL)
1612 return(NULL);
1613 switch (obj->type) {
1614 case XPATH_NODESET: {
1615 xmlNodeSetPtr set = obj->nodesetval;
1616 if (set == NULL)
1617 return(NULL);
1618 for (i = 0;i < set->nodeNr;i++) {
Daniel Veillard39196eb2001-06-19 18:09:42 +00001619 if (set->nodeTab[i] == NULL)
1620 continue;
1621 switch (set->nodeTab[i]->type) {
1622 case XML_TEXT_NODE:
1623 case XML_CDATA_SECTION_NODE:
1624 case XML_ELEMENT_NODE:
1625 case XML_ENTITY_REF_NODE:
1626 case XML_ENTITY_NODE:
1627 case XML_PI_NODE:
1628 case XML_COMMENT_NODE:
1629 case XML_DOCUMENT_NODE:
1630 case XML_HTML_DOCUMENT_NODE:
1631#ifdef LIBXML_DOCB_ENABLED
1632 case XML_DOCB_DOCUMENT_NODE:
1633#endif
1634 case XML_XINCLUDE_START:
1635 case XML_XINCLUDE_END:
1636 break;
1637 case XML_ATTRIBUTE_NODE:
1638 case XML_NAMESPACE_DECL:
1639 case XML_DOCUMENT_TYPE_NODE:
1640 case XML_DOCUMENT_FRAG_NODE:
1641 case XML_NOTATION_NODE:
1642 case XML_DTD_NODE:
1643 case XML_ELEMENT_DECL:
1644 case XML_ATTRIBUTE_DECL:
1645 case XML_ENTITY_DECL:
1646 continue; /* for */
1647 }
Owen Taylor3473f882001-02-23 17:55:21 +00001648 if (last == NULL)
1649 list = last = xmlCopyNode(set->nodeTab[i], 1);
1650 else {
1651 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1652 if (last->next != NULL)
1653 last = last->next;
1654 }
1655 }
1656 break;
1657 }
1658 case XPATH_LOCATIONSET: {
1659 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1660 if (set == NULL)
1661 return(NULL);
1662 for (i = 0;i < set->locNr;i++) {
1663 if (last == NULL)
1664 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1665 else
1666 xmlAddNextSibling(last,
1667 xmlXPtrBuildNodeList(set->locTab[i]));
1668 if (last != NULL) {
1669 while (last->next != NULL)
1670 last = last->next;
1671 }
1672 }
1673 break;
1674 }
1675 case XPATH_RANGE:
1676 return(xmlXPtrBuildRangeNodeList(obj));
1677 case XPATH_POINT:
1678 return(xmlCopyNode(obj->user, 0));
1679 default:
1680 break;
1681 }
1682 return(list);
1683}
1684
1685/************************************************************************
1686 * *
1687 * XPointer functions *
1688 * *
1689 ************************************************************************/
1690
1691/**
1692 * xmlXPtrNbLocChildren:
1693 * @node: an xmlNodePtr
1694 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001695 * Count the number of location children of @node or the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001696 * string value in case of text/PI/Comments nodes
1697 *
1698 * Returns the number of location children
1699 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001700static int
Owen Taylor3473f882001-02-23 17:55:21 +00001701xmlXPtrNbLocChildren(xmlNodePtr node) {
1702 int ret = 0;
1703 if (node == NULL)
1704 return(-1);
1705 switch (node->type) {
1706 case XML_HTML_DOCUMENT_NODE:
1707 case XML_DOCUMENT_NODE:
1708 case XML_ELEMENT_NODE:
1709 node = node->children;
1710 while (node != NULL) {
1711 if (node->type == XML_ELEMENT_NODE)
1712 ret++;
1713 node = node->next;
1714 }
1715 break;
1716 case XML_ATTRIBUTE_NODE:
1717 return(-1);
1718
1719 case XML_PI_NODE:
1720 case XML_COMMENT_NODE:
1721 case XML_TEXT_NODE:
1722 case XML_CDATA_SECTION_NODE:
1723 case XML_ENTITY_REF_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001724 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001725 break;
1726 default:
1727 return(-1);
1728 }
1729 return(ret);
1730}
1731
1732/**
1733 * xmlXPtrHereFunction:
1734 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001735 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001736 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001737 * Function implementing here() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001738 * as described in 5.4.3
1739 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001740static void
Owen Taylor3473f882001-02-23 17:55:21 +00001741xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001742 CHECK_ARITY(0);
1743
Owen Taylor3473f882001-02-23 17:55:21 +00001744 if (ctxt->context->here == NULL)
1745 XP_ERROR(XPTR_SYNTAX_ERROR);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001746
Owen Taylor3473f882001-02-23 17:55:21 +00001747 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1748}
1749
1750/**
1751 * xmlXPtrOriginFunction:
1752 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001753 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001754 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001755 * Function implementing origin() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001756 * as described in 5.4.3
1757 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001758static void
Owen Taylor3473f882001-02-23 17:55:21 +00001759xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001760 CHECK_ARITY(0);
1761
Owen Taylor3473f882001-02-23 17:55:21 +00001762 if (ctxt->context->origin == NULL)
1763 XP_ERROR(XPTR_SYNTAX_ERROR);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001764
Owen Taylor3473f882001-02-23 17:55:21 +00001765 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1766}
1767
1768/**
1769 * xmlXPtrStartPointFunction:
1770 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001771 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001772 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001773 * Function implementing start-point() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001774 * as described in 5.4.3
1775 * ----------------
1776 * location-set start-point(location-set)
1777 *
1778 * For each location x in the argument location-set, start-point adds a
1779 * location of type point to the result location-set. That point represents
1780 * the start point of location x and is determined by the following rules:
1781 *
1782 * - If x is of type point, the start point is x.
1783 * - If x is of type range, the start point is the start point of x.
1784 * - If x is of type root, element, text, comment, or processing instruction,
1785 * - the container node of the start point is x and the index is 0.
1786 * - If x is of type attribute or namespace, the function must signal a
1787 * syntax error.
1788 * ----------------
1789 *
1790 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001791static void
Owen Taylor3473f882001-02-23 17:55:21 +00001792xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1793 xmlXPathObjectPtr tmp, obj, point;
1794 xmlLocationSetPtr newset = NULL;
1795 xmlLocationSetPtr oldset = NULL;
1796
1797 CHECK_ARITY(1);
1798 if ((ctxt->value == NULL) ||
1799 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1800 (ctxt->value->type != XPATH_NODESET)))
1801 XP_ERROR(XPATH_INVALID_TYPE)
1802
1803 obj = valuePop(ctxt);
1804 if (obj->type == XPATH_NODESET) {
1805 /*
1806 * First convert to a location set
1807 */
1808 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1809 xmlXPathFreeObject(obj);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001810 if (tmp == NULL)
1811 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00001812 obj = tmp;
1813 }
1814
1815 newset = xmlXPtrLocationSetCreate(NULL);
1816 if (newset == NULL) {
1817 xmlXPathFreeObject(obj);
1818 XP_ERROR(XPATH_MEMORY_ERROR);
1819 }
1820 oldset = (xmlLocationSetPtr) obj->user;
1821 if (oldset != NULL) {
1822 int i;
1823
1824 for (i = 0; i < oldset->locNr; i++) {
1825 tmp = oldset->locTab[i];
1826 if (tmp == NULL)
1827 continue;
1828 point = NULL;
1829 switch (tmp->type) {
1830 case XPATH_POINT:
1831 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1832 break;
1833 case XPATH_RANGE: {
1834 xmlNodePtr node = tmp->user;
1835 if (node != NULL) {
1836 if (node->type == XML_ATTRIBUTE_NODE) {
1837 /* TODO: Namespace Nodes ??? */
1838 xmlXPathFreeObject(obj);
1839 xmlXPtrFreeLocationSet(newset);
1840 XP_ERROR(XPTR_SYNTAX_ERROR);
1841 }
1842 point = xmlXPtrNewPoint(node, tmp->index);
1843 }
1844 break;
1845 }
1846 default:
1847 /*** Should we raise an error ?
1848 xmlXPathFreeObject(obj);
1849 xmlXPathFreeObject(newset);
1850 XP_ERROR(XPATH_INVALID_TYPE)
1851 ***/
1852 break;
1853 }
1854 if (point != NULL)
1855 xmlXPtrLocationSetAdd(newset, point);
1856 }
1857 }
1858 xmlXPathFreeObject(obj);
1859 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1860}
1861
1862/**
1863 * xmlXPtrEndPointFunction:
1864 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001865 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001866 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001867 * Function implementing end-point() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001868 * as described in 5.4.3
1869 * ----------------------------
1870 * location-set end-point(location-set)
1871 *
1872 * For each location x in the argument location-set, end-point adds a
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001873 * location of type point to the result location-set. That point represents
Owen Taylor3473f882001-02-23 17:55:21 +00001874 * the end point of location x and is determined by the following rules:
1875 *
1876 * - If x is of type point, the resulting point is x.
1877 * - If x is of type range, the resulting point is the end point of x.
1878 * - If x is of type root or element, the container node of the resulting
1879 * point is x and the index is the number of location children of x.
1880 * - If x is of type text, comment, or processing instruction, the container
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001881 * node of the resulting point is x and the index is the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001882 * string-value of x.
1883 * - If x is of type attribute or namespace, the function must signal a
1884 * syntax error.
1885 * ----------------------------
1886 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001887static void
Owen Taylor3473f882001-02-23 17:55:21 +00001888xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1889 xmlXPathObjectPtr tmp, obj, point;
1890 xmlLocationSetPtr newset = NULL;
1891 xmlLocationSetPtr oldset = NULL;
1892
1893 CHECK_ARITY(1);
1894 if ((ctxt->value == NULL) ||
1895 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1896 (ctxt->value->type != XPATH_NODESET)))
1897 XP_ERROR(XPATH_INVALID_TYPE)
1898
1899 obj = valuePop(ctxt);
1900 if (obj->type == XPATH_NODESET) {
1901 /*
1902 * First convert to a location set
1903 */
1904 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1905 xmlXPathFreeObject(obj);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001906 if (tmp == NULL)
1907 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00001908 obj = tmp;
1909 }
1910
1911 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001912 if (newset == NULL) {
1913 xmlXPathFreeObject(obj);
1914 XP_ERROR(XPATH_MEMORY_ERROR);
1915 }
Owen Taylor3473f882001-02-23 17:55:21 +00001916 oldset = (xmlLocationSetPtr) obj->user;
1917 if (oldset != NULL) {
1918 int i;
1919
1920 for (i = 0; i < oldset->locNr; i++) {
1921 tmp = oldset->locTab[i];
1922 if (tmp == NULL)
1923 continue;
1924 point = NULL;
1925 switch (tmp->type) {
1926 case XPATH_POINT:
1927 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1928 break;
1929 case XPATH_RANGE: {
1930 xmlNodePtr node = tmp->user2;
1931 if (node != NULL) {
1932 if (node->type == XML_ATTRIBUTE_NODE) {
1933 /* TODO: Namespace Nodes ??? */
1934 xmlXPathFreeObject(obj);
1935 xmlXPtrFreeLocationSet(newset);
1936 XP_ERROR(XPTR_SYNTAX_ERROR);
1937 }
1938 point = xmlXPtrNewPoint(node, tmp->index2);
1939 } else if (tmp->user == NULL) {
1940 point = xmlXPtrNewPoint(node,
1941 xmlXPtrNbLocChildren(node));
1942 }
1943 break;
1944 }
1945 default:
1946 /*** Should we raise an error ?
1947 xmlXPathFreeObject(obj);
1948 xmlXPathFreeObject(newset);
1949 XP_ERROR(XPATH_INVALID_TYPE)
1950 ***/
1951 break;
1952 }
1953 if (point != NULL)
1954 xmlXPtrLocationSetAdd(newset, point);
1955 }
1956 }
1957 xmlXPathFreeObject(obj);
1958 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1959}
1960
1961
1962/**
1963 * xmlXPtrCoveringRange:
1964 * @ctxt: the XPointer Parser context
1965 * @loc: the location for which the covering range must be computed
1966 *
1967 * A covering range is a range that wholly encompasses a location
1968 * Section 5.3.3. Covering Ranges for All Location Types
1969 * http://www.w3.org/TR/xptr#N2267
1970 *
1971 * Returns a new location or NULL in case of error
1972 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001973static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001974xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1975 if (loc == NULL)
1976 return(NULL);
1977 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1978 (ctxt->context->doc == NULL))
1979 return(NULL);
1980 switch (loc->type) {
1981 case XPATH_POINT:
1982 return(xmlXPtrNewRange(loc->user, loc->index,
1983 loc->user, loc->index));
1984 case XPATH_RANGE:
1985 if (loc->user2 != NULL) {
1986 return(xmlXPtrNewRange(loc->user, loc->index,
1987 loc->user2, loc->index2));
1988 } else {
1989 xmlNodePtr node = (xmlNodePtr) loc->user;
1990 if (node == (xmlNodePtr) ctxt->context->doc) {
1991 return(xmlXPtrNewRange(node, 0, node,
1992 xmlXPtrGetArity(node)));
1993 } else {
1994 switch (node->type) {
1995 case XML_ATTRIBUTE_NODE:
1996 /* !!! our model is slightly different than XPath */
1997 return(xmlXPtrNewRange(node, 0, node,
1998 xmlXPtrGetArity(node)));
1999 case XML_ELEMENT_NODE:
2000 case XML_TEXT_NODE:
2001 case XML_CDATA_SECTION_NODE:
2002 case XML_ENTITY_REF_NODE:
2003 case XML_PI_NODE:
2004 case XML_COMMENT_NODE:
2005 case XML_DOCUMENT_NODE:
2006 case XML_NOTATION_NODE:
2007 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002008 int indx = xmlXPtrGetIndex(node);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002009
Owen Taylor3473f882001-02-23 17:55:21 +00002010 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002011 return(xmlXPtrNewRange(node, indx - 1,
2012 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00002013 }
2014 default:
2015 return(NULL);
2016 }
2017 }
2018 }
2019 default:
2020 TODO /* missed one case ??? */
2021 }
2022 return(NULL);
2023}
2024
2025/**
2026 * xmlXPtrRangeFunction:
2027 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002028 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002029 *
2030 * Function implementing the range() function 5.4.3
2031 * location-set range(location-set )
2032 *
2033 * The range function returns ranges covering the locations in
2034 * the argument location-set. For each location x in the argument
2035 * location-set, a range location representing the covering range of
2036 * x is added to the result location-set.
2037 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002038static void
Owen Taylor3473f882001-02-23 17:55:21 +00002039xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2040 int i;
2041 xmlXPathObjectPtr set;
2042 xmlLocationSetPtr oldset;
2043 xmlLocationSetPtr newset;
2044
2045 CHECK_ARITY(1);
2046 if ((ctxt->value == NULL) ||
2047 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2048 (ctxt->value->type != XPATH_NODESET)))
2049 XP_ERROR(XPATH_INVALID_TYPE)
2050
2051 set = valuePop(ctxt);
2052 if (set->type == XPATH_NODESET) {
2053 xmlXPathObjectPtr tmp;
2054
2055 /*
2056 * First convert to a location set
2057 */
2058 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2059 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002060 if (tmp == NULL)
2061 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002062 set = tmp;
2063 }
2064 oldset = (xmlLocationSetPtr) set->user;
2065
2066 /*
2067 * The loop is to compute the covering range for each item and add it
2068 */
2069 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002070 if (newset == NULL) {
2071 xmlXPathFreeObject(set);
2072 XP_ERROR(XPATH_MEMORY_ERROR);
2073 }
Owen Taylor3473f882001-02-23 17:55:21 +00002074 for (i = 0;i < oldset->locNr;i++) {
2075 xmlXPtrLocationSetAdd(newset,
2076 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2077 }
2078
2079 /*
2080 * Save the new value and cleanup
2081 */
2082 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2083 xmlXPathFreeObject(set);
2084}
2085
2086/**
2087 * xmlXPtrInsideRange:
2088 * @ctxt: the XPointer Parser context
2089 * @loc: the location for which the inside range must be computed
2090 *
2091 * A inside range is a range described in the range-inside() description
2092 *
2093 * Returns a new location or NULL in case of error
2094 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002095static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002096xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2097 if (loc == NULL)
2098 return(NULL);
2099 if ((ctxt == NULL) || (ctxt->context == NULL) ||
2100 (ctxt->context->doc == NULL))
2101 return(NULL);
2102 switch (loc->type) {
2103 case XPATH_POINT: {
2104 xmlNodePtr node = (xmlNodePtr) loc->user;
2105 switch (node->type) {
2106 case XML_PI_NODE:
2107 case XML_COMMENT_NODE:
2108 case XML_TEXT_NODE:
2109 case XML_CDATA_SECTION_NODE: {
2110 if (node->content == NULL) {
2111 return(xmlXPtrNewRange(node, 0, node, 0));
2112 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002113 return(xmlXPtrNewRange(node, 0, node,
2114 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002115 }
2116 }
2117 case XML_ATTRIBUTE_NODE:
2118 case XML_ELEMENT_NODE:
2119 case XML_ENTITY_REF_NODE:
2120 case XML_DOCUMENT_NODE:
2121 case XML_NOTATION_NODE:
2122 case XML_HTML_DOCUMENT_NODE: {
2123 return(xmlXPtrNewRange(node, 0, node,
2124 xmlXPtrGetArity(node)));
2125 }
2126 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002127 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002128 }
2129 return(NULL);
2130 }
2131 case XPATH_RANGE: {
2132 xmlNodePtr node = (xmlNodePtr) loc->user;
2133 if (loc->user2 != NULL) {
2134 return(xmlXPtrNewRange(node, loc->index,
2135 loc->user2, loc->index2));
2136 } else {
2137 switch (node->type) {
2138 case XML_PI_NODE:
2139 case XML_COMMENT_NODE:
2140 case XML_TEXT_NODE:
2141 case XML_CDATA_SECTION_NODE: {
2142 if (node->content == NULL) {
2143 return(xmlXPtrNewRange(node, 0, node, 0));
2144 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002145 return(xmlXPtrNewRange(node, 0, node,
2146 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002147 }
2148 }
2149 case XML_ATTRIBUTE_NODE:
2150 case XML_ELEMENT_NODE:
2151 case XML_ENTITY_REF_NODE:
2152 case XML_DOCUMENT_NODE:
2153 case XML_NOTATION_NODE:
2154 case XML_HTML_DOCUMENT_NODE: {
2155 return(xmlXPtrNewRange(node, 0, node,
2156 xmlXPtrGetArity(node)));
2157 }
2158 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002159 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002160 }
2161 return(NULL);
2162 }
2163 }
2164 default:
2165 TODO /* missed one case ??? */
2166 }
2167 return(NULL);
2168}
2169
2170/**
2171 * xmlXPtrRangeInsideFunction:
2172 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002173 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002174 *
2175 * Function implementing the range-inside() function 5.4.3
2176 * location-set range-inside(location-set )
2177 *
2178 * The range-inside function returns ranges covering the contents of
2179 * the locations in the argument location-set. For each location x in
2180 * the argument location-set, a range location is added to the result
2181 * location-set. If x is a range location, then x is added to the
2182 * result location-set. If x is not a range location, then x is used
2183 * as the container location of the start and end points of the range
2184 * location to be added; the index of the start point of the range is
2185 * zero; if the end point is a character point then its index is the
2186 * length of the string-value of x, and otherwise is the number of
2187 * location children of x.
2188 *
2189 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002190static void
Owen Taylor3473f882001-02-23 17:55:21 +00002191xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2192 int i;
2193 xmlXPathObjectPtr set;
2194 xmlLocationSetPtr oldset;
2195 xmlLocationSetPtr newset;
2196
2197 CHECK_ARITY(1);
2198 if ((ctxt->value == NULL) ||
2199 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2200 (ctxt->value->type != XPATH_NODESET)))
2201 XP_ERROR(XPATH_INVALID_TYPE)
2202
2203 set = valuePop(ctxt);
2204 if (set->type == XPATH_NODESET) {
2205 xmlXPathObjectPtr tmp;
2206
2207 /*
2208 * First convert to a location set
2209 */
2210 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2211 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002212 if (tmp == NULL)
2213 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002214 set = tmp;
2215 }
2216 oldset = (xmlLocationSetPtr) set->user;
2217
2218 /*
2219 * The loop is to compute the covering range for each item and add it
2220 */
2221 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002222 if (newset == NULL) {
2223 xmlXPathFreeObject(set);
2224 XP_ERROR(XPATH_MEMORY_ERROR);
2225 }
Owen Taylor3473f882001-02-23 17:55:21 +00002226 for (i = 0;i < oldset->locNr;i++) {
2227 xmlXPtrLocationSetAdd(newset,
2228 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2229 }
2230
2231 /*
2232 * Save the new value and cleanup
2233 */
2234 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2235 xmlXPathFreeObject(set);
2236}
2237
2238/**
2239 * xmlXPtrRangeToFunction:
2240 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002241 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002242 *
2243 * Implement the range-to() XPointer function
Brian C. Young142c7c62017-04-03 12:46:02 -07002244 *
2245 * Obsolete. range-to is not a real function but a special type of location
2246 * step which is handled in xpath.c.
Owen Taylor3473f882001-02-23 17:55:21 +00002247 */
2248void
Brian C. Young142c7c62017-04-03 12:46:02 -07002249xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt,
2250 int nargs ATTRIBUTE_UNUSED) {
2251 XP_ERROR(XPATH_EXPR_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +00002252}
2253
2254/**
2255 * xmlXPtrAdvanceNode:
2256 * @cur: the node
William M. Brackce4fc562004-01-22 02:47:18 +00002257 * @level: incremented/decremented to show level in tree
Owen Taylor3473f882001-02-23 17:55:21 +00002258 *
2259 * Advance to the next element or text node in document order
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002260 * TODO: add a stack for entering/exiting entities
Owen Taylor3473f882001-02-23 17:55:21 +00002261 *
2262 * Returns -1 in case of failure, 0 otherwise
2263 */
2264xmlNodePtr
William M. Brackf7eb7942003-12-31 07:59:17 +00002265xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
Owen Taylor3473f882001-02-23 17:55:21 +00002266next:
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002267 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002268 return(NULL);
2269 if (cur->children != NULL) {
2270 cur = cur->children ;
William M. Brackf7eb7942003-12-31 07:59:17 +00002271 if (level != NULL)
2272 (*level)++;
Owen Taylor3473f882001-02-23 17:55:21 +00002273 goto found;
2274 }
William M. Brack4d59e222004-03-08 14:42:31 +00002275skip: /* This label should only be needed if something is wrong! */
Owen Taylor3473f882001-02-23 17:55:21 +00002276 if (cur->next != NULL) {
2277 cur = cur->next;
2278 goto found;
2279 }
2280 do {
2281 cur = cur->parent;
William M. Brackf7eb7942003-12-31 07:59:17 +00002282 if (level != NULL)
2283 (*level)--;
Owen Taylor3473f882001-02-23 17:55:21 +00002284 if (cur == NULL) return(NULL);
2285 if (cur->next != NULL) {
2286 cur = cur->next;
2287 goto found;
2288 }
2289 } while (cur != NULL);
2290
2291found:
2292 if ((cur->type != XML_ELEMENT_NODE) &&
2293 (cur->type != XML_TEXT_NODE) &&
2294 (cur->type != XML_DOCUMENT_NODE) &&
2295 (cur->type != XML_HTML_DOCUMENT_NODE) &&
William M. Brack4d59e222004-03-08 14:42:31 +00002296 (cur->type != XML_CDATA_SECTION_NODE)) {
2297 if (cur->type == XML_ENTITY_REF_NODE) { /* Shouldn't happen */
2298 TODO
2299 goto skip;
2300 }
2301 goto next;
2302 }
Owen Taylor3473f882001-02-23 17:55:21 +00002303 return(cur);
2304}
2305
2306/**
2307 * xmlXPtrAdvanceChar:
2308 * @node: the node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002309 * @indx: the indx
Owen Taylor3473f882001-02-23 17:55:21 +00002310 * @bytes: the number of bytes
2311 *
2312 * Advance a point of the associated number of bytes (not UTF8 chars)
2313 *
2314 * Returns -1 in case of failure, 0 otherwise
2315 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002316static int
2317xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Owen Taylor3473f882001-02-23 17:55:21 +00002318 xmlNodePtr cur;
2319 int pos;
2320 int len;
2321
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002322 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002323 return(-1);
2324 cur = *node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002325 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002326 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002327 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002328
2329 while (bytes >= 0) {
2330 /*
2331 * First position to the beginning of the first text node
2332 * corresponding to this point
2333 */
2334 while ((cur != NULL) &&
2335 ((cur->type == XML_ELEMENT_NODE) ||
2336 (cur->type == XML_DOCUMENT_NODE) ||
2337 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2338 if (pos > 0) {
2339 cur = xmlXPtrGetNthChild(cur, pos);
2340 pos = 0;
2341 } else {
William M. Brackf7eb7942003-12-31 07:59:17 +00002342 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002343 pos = 0;
2344 }
2345 }
2346
2347 if (cur == NULL) {
2348 *node = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002349 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002350 return(-1);
2351 }
2352
2353 /*
2354 * if there is no move needed return the current value.
2355 */
2356 if (pos == 0) pos = 1;
2357 if (bytes == 0) {
2358 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002359 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002360 return(0);
2361 }
2362 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002363 * We should have a text (or cdata) node ...
Owen Taylor3473f882001-02-23 17:55:21 +00002364 */
2365 len = 0;
Daniel Veillard7db37732001-07-12 01:20:08 +00002366 if ((cur->type != XML_ELEMENT_NODE) &&
2367 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002368 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002369 }
2370 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002371 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002372 STRANGE
2373 pos = len;
2374 }
2375 if (pos + bytes >= len) {
2376 bytes -= (len - pos);
William M. Brackf7eb7942003-12-31 07:59:17 +00002377 cur = xmlXPtrAdvanceNode(cur, NULL);
Daniel Veillard24505b02005-07-28 23:49:35 +00002378 pos = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002379 } else if (pos + bytes < len) {
2380 pos += bytes;
2381 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002382 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002383 return(0);
2384 }
2385 }
2386 return(-1);
2387}
2388
2389/**
2390 * xmlXPtrMatchString:
2391 * @string: the string to search
2392 * @start: the start textnode
2393 * @startindex: the start index
2394 * @end: the end textnode IN/OUT
2395 * @endindex: the end index IN/OUT
2396 *
2397 * Check whether the document contains @string at the position
2398 * (@start, @startindex) and limited by the (@end, @endindex) point
2399 *
2400 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2401 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002402 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002403 * of the range
2404 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002405static int
Owen Taylor3473f882001-02-23 17:55:21 +00002406xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2407 xmlNodePtr *end, int *endindex) {
2408 xmlNodePtr cur;
2409 int pos; /* 0 based */
2410 int len; /* in bytes */
2411 int stringlen; /* in bytes */
2412 int match;
2413
2414 if (string == NULL)
2415 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002416 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002417 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002418 if ((end == NULL) || (*end == NULL) ||
2419 ((*end)->type == XML_NAMESPACE_DECL) || (endindex == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002420 return(-1);
2421 cur = start;
Owen Taylor3473f882001-02-23 17:55:21 +00002422 pos = startindex - 1;
2423 stringlen = xmlStrlen(string);
2424
2425 while (stringlen > 0) {
2426 if ((cur == *end) && (pos + stringlen > *endindex))
2427 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002428
2429 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002430 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002431 if (len >= pos + stringlen) {
Owen Taylor3473f882001-02-23 17:55:21 +00002432 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
Owen Taylor3473f882001-02-23 17:55:21 +00002433 if (match) {
2434#ifdef DEBUG_RANGES
2435 xmlGenericError(xmlGenericErrorContext,
2436 "found range %d bytes at index %d of ->",
2437 stringlen, pos + 1);
2438 xmlDebugDumpString(stdout, cur->content);
2439 xmlGenericError(xmlGenericErrorContext, "\n");
2440#endif
2441 *end = cur;
2442 *endindex = pos + stringlen;
2443 return(1);
2444 } else {
2445 return(0);
2446 }
2447 } else {
2448 int sub = len - pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002449 match = (!xmlStrncmp(&cur->content[pos], string, sub));
Owen Taylor3473f882001-02-23 17:55:21 +00002450 if (match) {
2451#ifdef DEBUG_RANGES
2452 xmlGenericError(xmlGenericErrorContext,
2453 "found subrange %d bytes at index %d of ->",
2454 sub, pos + 1);
2455 xmlDebugDumpString(stdout, cur->content);
2456 xmlGenericError(xmlGenericErrorContext, "\n");
2457#endif
2458 string = &string[sub];
2459 stringlen -= sub;
2460 } else {
2461 return(0);
2462 }
2463 }
2464 }
William M. Brackf7eb7942003-12-31 07:59:17 +00002465 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002466 if (cur == NULL)
2467 return(0);
2468 pos = 0;
2469 }
2470 return(1);
2471}
2472
2473/**
2474 * xmlXPtrSearchString:
2475 * @string: the string to search
2476 * @start: the start textnode IN/OUT
2477 * @startindex: the start index IN/OUT
2478 * @end: the end textnode
2479 * @endindex: the end index
2480 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002481 * Search the next occurrence of @string within the document content
Owen Taylor3473f882001-02-23 17:55:21 +00002482 * until the (@end, @endindex) point is reached
2483 *
2484 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2485 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002486 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002487 * of the range
2488 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002489static int
Owen Taylor3473f882001-02-23 17:55:21 +00002490xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2491 xmlNodePtr *end, int *endindex) {
2492 xmlNodePtr cur;
2493 const xmlChar *str;
2494 int pos; /* 0 based */
2495 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002496 xmlChar first;
2497
2498 if (string == NULL)
2499 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002500 if ((start == NULL) || (*start == NULL) ||
2501 ((*start)->type == XML_NAMESPACE_DECL) || (startindex == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002502 return(-1);
2503 if ((end == NULL) || (endindex == NULL))
2504 return(-1);
2505 cur = *start;
Owen Taylor3473f882001-02-23 17:55:21 +00002506 pos = *startindex - 1;
2507 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002508
2509 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002510 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002511 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002512 while (pos <= len) {
2513 if (first != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00002514 str = xmlStrchr(&cur->content[pos], first);
Owen Taylor3473f882001-02-23 17:55:21 +00002515 if (str != NULL) {
2516 pos = (str - (xmlChar *)(cur->content));
2517#ifdef DEBUG_RANGES
2518 xmlGenericError(xmlGenericErrorContext,
2519 "found '%c' at index %d of ->",
2520 first, pos + 1);
2521 xmlDebugDumpString(stdout, cur->content);
2522 xmlGenericError(xmlGenericErrorContext, "\n");
2523#endif
2524 if (xmlXPtrMatchString(string, cur, pos + 1,
2525 end, endindex)) {
2526 *start = cur;
2527 *startindex = pos + 1;
2528 return(1);
2529 }
2530 pos++;
2531 } else {
2532 pos = len + 1;
2533 }
2534 } else {
2535 /*
2536 * An empty string is considered to match before each
2537 * character of the string-value and after the final
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002538 * character.
Owen Taylor3473f882001-02-23 17:55:21 +00002539 */
2540#ifdef DEBUG_RANGES
2541 xmlGenericError(xmlGenericErrorContext,
2542 "found '' at index %d of ->",
2543 pos + 1);
2544 xmlDebugDumpString(stdout, cur->content);
2545 xmlGenericError(xmlGenericErrorContext, "\n");
2546#endif
2547 *start = cur;
2548 *startindex = pos + 1;
2549 *end = cur;
2550 *endindex = pos + 1;
2551 return(1);
2552 }
2553 }
2554 }
2555 if ((cur == *end) && (pos >= *endindex))
2556 return(0);
William M. Brackf7eb7942003-12-31 07:59:17 +00002557 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002558 if (cur == NULL)
2559 return(0);
2560 pos = 1;
2561 }
2562 return(0);
2563}
2564
2565/**
2566 * xmlXPtrGetLastChar:
2567 * @node: the node
2568 * @index: the index
2569 *
2570 * Computes the point coordinates of the last char of this point
2571 *
2572 * Returns -1 in case of failure, 0 otherwise
2573 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002574static int
2575xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002576 xmlNodePtr cur;
2577 int pos, len = 0;
2578
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002579 if ((node == NULL) || (*node == NULL) ||
2580 ((*node)->type == XML_NAMESPACE_DECL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002581 return(-1);
2582 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002583 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002584
Owen Taylor3473f882001-02-23 17:55:21 +00002585 if ((cur->type == XML_ELEMENT_NODE) ||
2586 (cur->type == XML_DOCUMENT_NODE) ||
2587 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2588 if (pos > 0) {
2589 cur = xmlXPtrGetNthChild(cur, pos);
Owen Taylor3473f882001-02-23 17:55:21 +00002590 }
2591 }
2592 while (cur != NULL) {
2593 if (cur->last != NULL)
2594 cur = cur->last;
Daniel Veillard7db37732001-07-12 01:20:08 +00002595 else if ((cur->type != XML_ELEMENT_NODE) &&
2596 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002597 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002598 break;
2599 } else {
2600 return(-1);
2601 }
2602 }
2603 if (cur == NULL)
2604 return(-1);
2605 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002606 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002607 return(0);
2608}
2609
2610/**
2611 * xmlXPtrGetStartPoint:
2612 * @obj: an range
2613 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002614 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002615 *
2616 * read the object and return the start point coordinates.
2617 *
2618 * Returns -1 in case of failure, 0 otherwise
2619 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002620static int
2621xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2622 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002623 return(-1);
2624
2625 switch (obj->type) {
2626 case XPATH_POINT:
2627 *node = obj->user;
2628 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002629 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002630 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002631 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002632 return(0);
2633 case XPATH_RANGE:
2634 *node = obj->user;
2635 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002636 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002637 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002638 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002639 return(0);
2640 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002641 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002642 }
2643 return(-1);
2644}
2645
2646/**
2647 * xmlXPtrGetEndPoint:
2648 * @obj: an range
2649 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002650 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002651 *
2652 * read the object and return the end point coordinates.
2653 *
2654 * Returns -1 in case of failure, 0 otherwise
2655 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002656static int
2657xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2658 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002659 return(-1);
2660
2661 switch (obj->type) {
2662 case XPATH_POINT:
2663 *node = obj->user;
2664 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002665 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002666 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002667 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002668 return(0);
2669 case XPATH_RANGE:
2670 *node = obj->user;
2671 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002672 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002673 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002674 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002675 return(0);
2676 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002677 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002678 }
2679 return(-1);
2680}
2681
2682/**
2683 * xmlXPtrStringRangeFunction:
2684 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002685 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002686 *
2687 * Function implementing the string-range() function
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002688 * range as described in 5.4.2
Owen Taylor3473f882001-02-23 17:55:21 +00002689 *
2690 * ------------------------------
2691 * [Definition: For each location in the location-set argument,
2692 * string-range returns a set of string ranges, a set of substrings in a
2693 * string. Specifically, the string-value of the location is searched for
2694 * substrings that match the string argument, and the resulting location-set
2695 * will contain a range location for each non-overlapping match.]
2696 * An empty string is considered to match before each character of the
2697 * string-value and after the final character. Whitespace in a string
2698 * is matched literally, with no normalization except that provided by
2699 * XML for line ends. The third argument gives the position of the first
2700 * character to be in the resulting range, relative to the start of the
2701 * match. The default value is 1, which makes the range start immediately
2702 * before the first character of the matched string. The fourth argument
2703 * gives the number of characters in the range; the default is that the
2704 * range extends to the end of the matched string.
2705 *
2706 * Element boundaries, as well as entire embedded nodes such as processing
2707 * instructions and comments, are ignored as defined in [XPath].
2708 *
2709 * If the string in the second argument is not found in the string-value
2710 * of the location, or if a value in the third or fourth argument indicates
2711 * a string that is beyond the beginning or end of the document, the
2712 * expression fails.
2713 *
2714 * The points of the range-locations in the returned location-set will
2715 * all be character points.
2716 * ------------------------------
2717 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002718static void
Owen Taylor3473f882001-02-23 17:55:21 +00002719xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillardaac7c682006-03-10 13:40:16 +00002720 int i, startindex, endindex = 0, fendindex;
2721 xmlNodePtr start, end = 0, fend;
Owen Taylor3473f882001-02-23 17:55:21 +00002722 xmlXPathObjectPtr set;
2723 xmlLocationSetPtr oldset;
2724 xmlLocationSetPtr newset;
2725 xmlXPathObjectPtr string;
2726 xmlXPathObjectPtr position = NULL;
2727 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002728 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002729
2730 /*
2731 * Grab the arguments
2732 */
2733 if ((nargs < 2) || (nargs > 4))
2734 XP_ERROR(XPATH_INVALID_ARITY);
2735
2736 if (nargs >= 4) {
2737 CHECK_TYPE(XPATH_NUMBER);
2738 number = valuePop(ctxt);
2739 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002740 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002741 }
2742 if (nargs >= 3) {
2743 CHECK_TYPE(XPATH_NUMBER);
2744 position = valuePop(ctxt);
2745 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002746 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002747 }
2748 CHECK_TYPE(XPATH_STRING);
2749 string = valuePop(ctxt);
2750 if ((ctxt->value == NULL) ||
2751 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2752 (ctxt->value->type != XPATH_NODESET)))
2753 XP_ERROR(XPATH_INVALID_TYPE)
2754
2755 set = valuePop(ctxt);
William M. Brack08171912003-12-29 02:52:11 +00002756 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002757 if (newset == NULL) {
2758 xmlXPathFreeObject(set);
2759 XP_ERROR(XPATH_MEMORY_ERROR);
2760 }
William M. Brack08171912003-12-29 02:52:11 +00002761 if (set->nodesetval == NULL) {
2762 goto error;
2763 }
Owen Taylor3473f882001-02-23 17:55:21 +00002764 if (set->type == XPATH_NODESET) {
2765 xmlXPathObjectPtr tmp;
2766
2767 /*
2768 * First convert to a location set
2769 */
2770 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2771 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002772 if (tmp == NULL)
2773 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002774 set = tmp;
2775 }
2776 oldset = (xmlLocationSetPtr) set->user;
2777
2778 /*
2779 * The loop is to search for each element in the location set
2780 * the list of location set corresponding to that search
2781 */
Owen Taylor3473f882001-02-23 17:55:21 +00002782 for (i = 0;i < oldset->locNr;i++) {
2783#ifdef DEBUG_RANGES
2784 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2785#endif
2786
2787 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2788 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2789 xmlXPtrAdvanceChar(&start, &startindex, 0);
2790 xmlXPtrGetLastChar(&end, &endindex);
2791
2792#ifdef DEBUG_RANGES
2793 xmlGenericError(xmlGenericErrorContext,
2794 "from index %d of ->", startindex);
2795 xmlDebugDumpString(stdout, start->content);
2796 xmlGenericError(xmlGenericErrorContext, "\n");
2797 xmlGenericError(xmlGenericErrorContext,
2798 "to index %d of ->", endindex);
2799 xmlDebugDumpString(stdout, end->content);
2800 xmlGenericError(xmlGenericErrorContext, "\n");
2801#endif
2802 do {
2803 fend = end;
2804 fendindex = endindex;
2805 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2806 &fend, &fendindex);
2807 if (found == 1) {
2808 if (position == NULL) {
2809 xmlXPtrLocationSetAdd(newset,
2810 xmlXPtrNewRange(start, startindex, fend, fendindex));
2811 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2812 pos - 1) == 0) {
2813 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002814 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002815 xmlNodePtr rend;
2816 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002817 rindx = startindex - 1;
2818 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002819 num) == 0) {
2820 xmlXPtrLocationSetAdd(newset,
2821 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002822 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002823 }
2824 } else if ((number != NULL) && (num <= 0)) {
2825 xmlXPtrLocationSetAdd(newset,
2826 xmlXPtrNewRange(start, startindex,
2827 start, startindex));
2828 } else {
2829 xmlXPtrLocationSetAdd(newset,
2830 xmlXPtrNewRange(start, startindex,
2831 fend, fendindex));
2832 }
2833 }
2834 start = fend;
2835 startindex = fendindex;
2836 if (string->stringval[0] == 0)
2837 startindex++;
2838 }
2839 } while (found == 1);
2840 }
2841
2842 /*
2843 * Save the new value and cleanup
2844 */
William M. Brack08171912003-12-29 02:52:11 +00002845error:
Owen Taylor3473f882001-02-23 17:55:21 +00002846 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2847 xmlXPathFreeObject(set);
2848 xmlXPathFreeObject(string);
2849 if (position) xmlXPathFreeObject(position);
2850 if (number) xmlXPathFreeObject(number);
2851}
2852
2853/**
2854 * xmlXPtrEvalRangePredicate:
2855 * @ctxt: the XPointer Parser context
2856 *
2857 * [8] Predicate ::= '[' PredicateExpr ']'
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002858 * [9] PredicateExpr ::= Expr
Owen Taylor3473f882001-02-23 17:55:21 +00002859 *
2860 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2861 * a Location Set instead of a node set
2862 */
2863void
2864xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2865 const xmlChar *cur;
2866 xmlXPathObjectPtr res;
2867 xmlXPathObjectPtr obj, tmp;
2868 xmlLocationSetPtr newset = NULL;
2869 xmlLocationSetPtr oldset;
2870 int i;
2871
Daniel Veillardce682bc2004-11-05 17:22:25 +00002872 if (ctxt == NULL) return;
2873
Owen Taylor3473f882001-02-23 17:55:21 +00002874 SKIP_BLANKS;
2875 if (CUR != '[') {
2876 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2877 }
2878 NEXT;
2879 SKIP_BLANKS;
2880
2881 /*
2882 * Extract the old set, and then evaluate the result of the
2883 * expression for all the element in the set. use it to grow
2884 * up a new set.
2885 */
2886 CHECK_TYPE(XPATH_LOCATIONSET);
2887 obj = valuePop(ctxt);
2888 oldset = obj->user;
2889 ctxt->context->node = NULL;
2890
2891 if ((oldset == NULL) || (oldset->locNr == 0)) {
2892 ctxt->context->contextSize = 0;
2893 ctxt->context->proximityPosition = 0;
2894 xmlXPathEvalExpr(ctxt);
2895 res = valuePop(ctxt);
2896 if (res != NULL)
2897 xmlXPathFreeObject(res);
2898 valuePush(ctxt, obj);
2899 CHECK_ERROR;
2900 } else {
2901 /*
2902 * Save the expression pointer since we will have to evaluate
2903 * it multiple times. Initialize the new set.
2904 */
2905 cur = ctxt->cur;
2906 newset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002907
Owen Taylor3473f882001-02-23 17:55:21 +00002908 for (i = 0; i < oldset->locNr; i++) {
2909 ctxt->cur = cur;
2910
2911 /*
2912 * Run the evaluation with a node list made of a single item
2913 * in the nodeset.
2914 */
2915 ctxt->context->node = oldset->locTab[i]->user;
2916 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2917 valuePush(ctxt, tmp);
2918 ctxt->context->contextSize = oldset->locNr;
2919 ctxt->context->proximityPosition = i + 1;
2920
2921 xmlXPathEvalExpr(ctxt);
2922 CHECK_ERROR;
2923
2924 /*
2925 * The result of the evaluation need to be tested to
2926 * decided whether the filter succeeded or not
2927 */
2928 res = valuePop(ctxt);
2929 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2930 xmlXPtrLocationSetAdd(newset,
2931 xmlXPathObjectCopy(oldset->locTab[i]));
2932 }
2933
2934 /*
2935 * Cleanup
2936 */
2937 if (res != NULL)
2938 xmlXPathFreeObject(res);
2939 if (ctxt->value == tmp) {
2940 res = valuePop(ctxt);
2941 xmlXPathFreeObject(res);
2942 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002943
Owen Taylor3473f882001-02-23 17:55:21 +00002944 ctxt->context->node = NULL;
2945 }
2946
2947 /*
2948 * The result is used as the new evaluation set.
2949 */
2950 xmlXPathFreeObject(obj);
2951 ctxt->context->node = NULL;
2952 ctxt->context->contextSize = -1;
2953 ctxt->context->proximityPosition = -1;
2954 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2955 }
2956 if (CUR != ']') {
2957 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2958 }
2959
2960 NEXT;
2961 SKIP_BLANKS;
2962}
2963
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002964#define bottom_xpointer
2965#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00002966#endif
2967