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