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