1/* 2 * xinclude.c : Code to implement XInclude processing 3 * 4 * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003 5 * http://www.w3.org/TR/2003/WD-xinclude-20031110 6 * 7 * See Copyright for the status of this software. 8 * 9 * daniel@veillard.com 10 */ 11 12#define IN_LIBXML 13#include "libxml.h" 14 15#include <string.h> 16#include <libxml/xmlmemory.h> 17#include <libxml/tree.h> 18#include <libxml/parser.h> 19#include <libxml/uri.h> 20#include <libxml/xpointer.h> 21#include <libxml/parserInternals.h> 22#include <libxml/xmlerror.h> 23#include <libxml/encoding.h> 24#include <libxml/globals.h> 25 26#ifdef LIBXML_XINCLUDE_ENABLED 27#include <libxml/xinclude.h> 28 29 30#define XINCLUDE_MAX_DEPTH 40 31 32/* #define DEBUG_XINCLUDE */ 33#ifdef DEBUG_XINCLUDE 34#ifdef LIBXML_DEBUG_ENABLED 35#include <libxml/debugXML.h> 36#endif 37#endif 38 39/************************************************************************ 40 * * 41 * XInclude context handling * 42 * * 43 ************************************************************************/ 44 45/* 46 * An XInclude context 47 */ 48typedef xmlChar *xmlURL; 49 50typedef struct _xmlXIncludeRef xmlXIncludeRef; 51typedef xmlXIncludeRef *xmlXIncludeRefPtr; 52struct _xmlXIncludeRef { 53 xmlChar *URI; /* the fully resolved resource URL */ 54 xmlChar *fragment; /* the fragment in the URI */ 55 xmlDocPtr doc; /* the parsed document */ 56 xmlNodePtr ref; /* the node making the reference in the source */ 57 xmlNodePtr inc; /* the included copy */ 58 int xml; /* xml or txt */ 59 int count; /* how many refs use that specific doc */ 60 xmlXPathObjectPtr xptr; /* the xpointer if needed */ 61 int emptyFb; /* flag to show fallback empty */ 62}; 63 64struct _xmlXIncludeCtxt { 65 xmlDocPtr doc; /* the source document */ 66 int incBase; /* the first include for this document */ 67 int incNr; /* number of includes */ 68 int incMax; /* size of includes tab */ 69 xmlXIncludeRefPtr *incTab; /* array of included references */ 70 71 int txtNr; /* number of unparsed documents */ 72 int txtMax; /* size of unparsed documents tab */ 73 xmlNodePtr *txtTab; /* array of unparsed text nodes */ 74 xmlURL *txturlTab; /* array of unparsed text URLs */ 75 76 xmlChar * url; /* the current URL processed */ 77 int urlNr; /* number of URLs stacked */ 78 int urlMax; /* size of URL stack */ 79 xmlChar * *urlTab; /* URL stack */ 80 81 int nbErrors; /* the number of errors detected */ 82 int legacy; /* using XINCLUDE_OLD_NS */ 83 int parseFlags; /* the flags used for parsing XML documents */ 84 xmlChar * base; /* the current xml:base */ 85 86 void *_private; /* application data */ 87}; 88 89static int 90xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree); 91 92 93/************************************************************************ 94 * * 95 * XInclude error handler * 96 * * 97 ************************************************************************/ 98 99/** 100 * xmlXIncludeErrMemory: 101 * @extra: extra information 102 * 103 * Handle an out of memory condition 104 */ 105static void 106xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, 107 const char *extra) 108{ 109 if (ctxt != NULL) 110 ctxt->nbErrors++; 111 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE, 112 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, 113 extra, NULL, NULL, 0, 0, 114 "Memory allocation failed : %s\n", extra); 115} 116 117/** 118 * xmlXIncludeErr: 119 * @ctxt: the XInclude context 120 * @node: the context node 121 * @msg: the error message 122 * @extra: extra information 123 * 124 * Handle an XInclude error 125 */ 126static void 127xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error, 128 const char *msg, const xmlChar *extra) 129{ 130 if (ctxt != NULL) 131 ctxt->nbErrors++; 132 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE, 133 error, XML_ERR_ERROR, NULL, 0, 134 (const char *) extra, NULL, NULL, 0, 0, 135 msg, (const char *) extra); 136} 137 138#if 0 139/** 140 * xmlXIncludeWarn: 141 * @ctxt: the XInclude context 142 * @node: the context node 143 * @msg: the error message 144 * @extra: extra information 145 * 146 * Emit an XInclude warning. 147 */ 148static void 149xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error, 150 const char *msg, const xmlChar *extra) 151{ 152 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE, 153 error, XML_ERR_WARNING, NULL, 0, 154 (const char *) extra, NULL, NULL, 0, 0, 155 msg, (const char *) extra); 156} 157#endif 158 159/** 160 * xmlXIncludeGetProp: 161 * @ctxt: the XInclude context 162 * @cur: the node 163 * @name: the attribute name 164 * 165 * Get an XInclude attribute 166 * 167 * Returns the value (to be freed) or NULL if not found 168 */ 169static xmlChar * 170xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur, 171 const xmlChar *name) { 172 xmlChar *ret; 173 174 ret = xmlGetNsProp(cur, XINCLUDE_NS, name); 175 if (ret != NULL) 176 return(ret); 177 if (ctxt->legacy != 0) { 178 ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name); 179 if (ret != NULL) 180 return(ret); 181 } 182 ret = xmlGetProp(cur, name); 183 return(ret); 184} 185/** 186 * xmlXIncludeFreeRef: 187 * @ref: the XInclude reference 188 * 189 * Free an XInclude reference 190 */ 191static void 192xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) { 193 if (ref == NULL) 194 return; 195#ifdef DEBUG_XINCLUDE 196 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n"); 197#endif 198 if (ref->doc != NULL) { 199#ifdef DEBUG_XINCLUDE 200 xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI); 201#endif 202 xmlFreeDoc(ref->doc); 203 } 204 if (ref->URI != NULL) 205 xmlFree(ref->URI); 206 if (ref->fragment != NULL) 207 xmlFree(ref->fragment); 208 if (ref->xptr != NULL) 209 xmlXPathFreeObject(ref->xptr); 210 xmlFree(ref); 211} 212 213/** 214 * xmlXIncludeNewRef: 215 * @ctxt: the XInclude context 216 * @URI: the resource URI 217 * 218 * Creates a new reference within an XInclude context 219 * 220 * Returns the new set 221 */ 222static xmlXIncludeRefPtr 223xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI, 224 xmlNodePtr ref) { 225 xmlXIncludeRefPtr ret; 226 227#ifdef DEBUG_XINCLUDE 228 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI); 229#endif 230 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef)); 231 if (ret == NULL) { 232 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context"); 233 return(NULL); 234 } 235 memset(ret, 0, sizeof(xmlXIncludeRef)); 236 if (URI == NULL) 237 ret->URI = NULL; 238 else 239 ret->URI = xmlStrdup(URI); 240 ret->fragment = NULL; 241 ret->ref = ref; 242 ret->doc = NULL; 243 ret->count = 0; 244 ret->xml = 0; 245 ret->inc = NULL; 246 if (ctxt->incMax == 0) { 247 ctxt->incMax = 4; 248 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax * 249 sizeof(ctxt->incTab[0])); 250 if (ctxt->incTab == NULL) { 251 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context"); 252 xmlXIncludeFreeRef(ret); 253 return(NULL); 254 } 255 } 256 if (ctxt->incNr >= ctxt->incMax) { 257 ctxt->incMax *= 2; 258 ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab, 259 ctxt->incMax * sizeof(ctxt->incTab[0])); 260 if (ctxt->incTab == NULL) { 261 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context"); 262 xmlXIncludeFreeRef(ret); 263 return(NULL); 264 } 265 } 266 ctxt->incTab[ctxt->incNr++] = ret; 267 return(ret); 268} 269 270/** 271 * xmlXIncludeNewContext: 272 * @doc: an XML Document 273 * 274 * Creates a new XInclude context 275 * 276 * Returns the new set 277 */ 278xmlXIncludeCtxtPtr 279xmlXIncludeNewContext(xmlDocPtr doc) { 280 xmlXIncludeCtxtPtr ret; 281 282#ifdef DEBUG_XINCLUDE 283 xmlGenericError(xmlGenericErrorContext, "New context\n"); 284#endif 285 if (doc == NULL) 286 return(NULL); 287 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt)); 288 if (ret == NULL) { 289 xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc, 290 "creating XInclude context"); 291 return(NULL); 292 } 293 memset(ret, 0, sizeof(xmlXIncludeCtxt)); 294 ret->doc = doc; 295 ret->incNr = 0; 296 ret->incBase = 0; 297 ret->incMax = 0; 298 ret->incTab = NULL; 299 ret->nbErrors = 0; 300 return(ret); 301} 302 303/** 304 * xmlXIncludeURLPush: 305 * @ctxt: the parser context 306 * @value: the url 307 * 308 * Pushes a new url on top of the url stack 309 * 310 * Returns -1 in case of error, the index in the stack otherwise 311 */ 312static int 313xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt, 314 const xmlChar *value) 315{ 316 if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) { 317 xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION, 318 "detected a recursion in %s\n", value); 319 return(-1); 320 } 321 if (ctxt->urlTab == NULL) { 322 ctxt->urlMax = 4; 323 ctxt->urlNr = 0; 324 ctxt->urlTab = (xmlChar * *) xmlMalloc( 325 ctxt->urlMax * sizeof(ctxt->urlTab[0])); 326 if (ctxt->urlTab == NULL) { 327 xmlXIncludeErrMemory(ctxt, NULL, "adding URL"); 328 return (-1); 329 } 330 } 331 if (ctxt->urlNr >= ctxt->urlMax) { 332 ctxt->urlMax *= 2; 333 ctxt->urlTab = 334 (xmlChar * *) xmlRealloc(ctxt->urlTab, 335 ctxt->urlMax * 336 sizeof(ctxt->urlTab[0])); 337 if (ctxt->urlTab == NULL) { 338 xmlXIncludeErrMemory(ctxt, NULL, "adding URL"); 339 return (-1); 340 } 341 } 342 ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value); 343 return (ctxt->urlNr++); 344} 345 346/** 347 * xmlXIncludeURLPop: 348 * @ctxt: the parser context 349 * 350 * Pops the top URL from the URL stack 351 */ 352static void 353xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt) 354{ 355 xmlChar * ret; 356 357 if (ctxt->urlNr <= 0) 358 return; 359 ctxt->urlNr--; 360 if (ctxt->urlNr > 0) 361 ctxt->url = ctxt->urlTab[ctxt->urlNr - 1]; 362 else 363 ctxt->url = NULL; 364 ret = ctxt->urlTab[ctxt->urlNr]; 365 ctxt->urlTab[ctxt->urlNr] = NULL; 366 if (ret != NULL) 367 xmlFree(ret); 368} 369 370/** 371 * xmlXIncludeFreeContext: 372 * @ctxt: the XInclude context 373 * 374 * Free an XInclude context 375 */ 376void 377xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) { 378 int i; 379 380#ifdef DEBUG_XINCLUDE 381 xmlGenericError(xmlGenericErrorContext, "Freeing context\n"); 382#endif 383 if (ctxt == NULL) 384 return; 385 while (ctxt->urlNr > 0) 386 xmlXIncludeURLPop(ctxt); 387 if (ctxt->urlTab != NULL) 388 xmlFree(ctxt->urlTab); 389 for (i = 0;i < ctxt->incNr;i++) { 390 if (ctxt->incTab[i] != NULL) 391 xmlXIncludeFreeRef(ctxt->incTab[i]); 392 } 393 if (ctxt->txturlTab != NULL) { 394 for (i = 0;i < ctxt->txtNr;i++) { 395 if (ctxt->txturlTab[i] != NULL) 396 xmlFree(ctxt->txturlTab[i]); 397 } 398 } 399 if (ctxt->incTab != NULL) 400 xmlFree(ctxt->incTab); 401 if (ctxt->txtTab != NULL) 402 xmlFree(ctxt->txtTab); 403 if (ctxt->txturlTab != NULL) 404 xmlFree(ctxt->txturlTab); 405 if (ctxt->base != NULL) { 406 xmlFree(ctxt->base); 407 } 408 xmlFree(ctxt); 409} 410 411/** 412 * xmlXIncludeParseFile: 413 * @ctxt: the XInclude context 414 * @URL: the URL or file path 415 * 416 * parse a document for XInclude 417 */ 418static xmlDocPtr 419xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) { 420 xmlDocPtr ret; 421 xmlParserCtxtPtr pctxt; 422 char *directory = NULL; 423 xmlParserInputPtr inputStream; 424 425 xmlInitParser(); 426 427 pctxt = xmlNewParserCtxt(); 428 if (pctxt == NULL) { 429 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context"); 430 return(NULL); 431 } 432 433 /* 434 * pass in the application data to the parser context. 435 */ 436 pctxt->_private = ctxt->_private; 437 438 /* 439 * try to ensure that new documents included are actually 440 * built with the same dictionary as the including document. 441 */ 442 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL) && 443 (pctxt->dict != NULL)) { 444 xmlDictFree(pctxt->dict); 445 pctxt->dict = ctxt->doc->dict; 446 xmlDictReference(pctxt->dict); 447 } 448 449 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD); 450 451 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt); 452 if (inputStream == NULL) { 453 xmlFreeParserCtxt(pctxt); 454 return(NULL); 455 } 456 457 inputPush(pctxt, inputStream); 458 459 if ((pctxt->directory == NULL) && (directory == NULL)) 460 directory = xmlParserGetDirectory(URL); 461 if ((pctxt->directory == NULL) && (directory != NULL)) 462 pctxt->directory = (char *) xmlStrdup((xmlChar *) directory); 463 464 pctxt->loadsubset |= XML_DETECT_IDS; 465 466 xmlParseDocument(pctxt); 467 468 if (pctxt->wellFormed) { 469 ret = pctxt->myDoc; 470 } 471 else { 472 ret = NULL; 473 if (pctxt->myDoc != NULL) 474 xmlFreeDoc(pctxt->myDoc); 475 pctxt->myDoc = NULL; 476 } 477 xmlFreeParserCtxt(pctxt); 478 479 return(ret); 480} 481 482/** 483 * xmlXIncludeAddNode: 484 * @ctxt: the XInclude context 485 * @cur: the new node 486 * 487 * Add a new node to process to an XInclude context 488 */ 489static int 490xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) { 491 xmlXIncludeRefPtr ref; 492 xmlURIPtr uri; 493 xmlChar *URL; 494 xmlChar *fragment = NULL; 495 xmlChar *href; 496 xmlChar *parse; 497 xmlChar *base; 498 xmlChar *URI; 499 int xml = 1, i; /* default Issue 64 */ 500 int local = 0; 501 502 503 if (ctxt == NULL) 504 return(-1); 505 if (cur == NULL) 506 return(-1); 507 508#ifdef DEBUG_XINCLUDE 509 xmlGenericError(xmlGenericErrorContext, "Add node\n"); 510#endif 511 /* 512 * read the attributes 513 */ 514 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF); 515 if (href == NULL) { 516 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */ 517 if (href == NULL) 518 return(-1); 519 local = 1; 520 } 521 if (href[0] == '#') 522 local = 1; 523 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE); 524 if (parse != NULL) { 525 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML)) 526 xml = 1; 527 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) 528 xml = 0; 529 else { 530 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE, 531 "invalid value %s for 'parse'\n", parse); 532 if (href != NULL) 533 xmlFree(href); 534 if (parse != NULL) 535 xmlFree(parse); 536 return(-1); 537 } 538 } 539 540 /* 541 * compute the URI 542 */ 543 base = xmlNodeGetBase(ctxt->doc, cur); 544 if (base == NULL) { 545 URI = xmlBuildURI(href, ctxt->doc->URL); 546 } else { 547 URI = xmlBuildURI(href, base); 548 } 549 if (URI == NULL) { 550 xmlChar *escbase; 551 xmlChar *eschref; 552 /* 553 * Some escaping may be needed 554 */ 555 escbase = xmlURIEscape(base); 556 eschref = xmlURIEscape(href); 557 URI = xmlBuildURI(eschref, escbase); 558 if (escbase != NULL) 559 xmlFree(escbase); 560 if (eschref != NULL) 561 xmlFree(eschref); 562 } 563 if (parse != NULL) 564 xmlFree(parse); 565 if (href != NULL) 566 xmlFree(href); 567 if (base != NULL) 568 xmlFree(base); 569 if (URI == NULL) { 570 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, 571 "failed build URL\n", NULL); 572 return(-1); 573 } 574 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER); 575 576 /* 577 * Check the URL and remove any fragment identifier 578 */ 579 uri = xmlParseURI((const char *)URI); 580 if (uri == NULL) { 581 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, 582 "invalid value URI %s\n", URI); 583 if (fragment != NULL) 584 xmlFree(fragment); 585 xmlFree(URI); 586 return(-1); 587 } 588 589 if (uri->fragment != NULL) { 590 if (ctxt->legacy != 0) { 591 if (fragment == NULL) { 592 fragment = (xmlChar *) uri->fragment; 593 } else { 594 xmlFree(uri->fragment); 595 } 596 } else { 597 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID, 598 "Invalid fragment identifier in URI %s use the xpointer attribute\n", 599 URI); 600 if (fragment != NULL) 601 xmlFree(fragment); 602 xmlFreeURI(uri); 603 xmlFree(URI); 604 return(-1); 605 } 606 uri->fragment = NULL; 607 } 608 URL = xmlSaveUri(uri); 609 xmlFreeURI(uri); 610 xmlFree(URI); 611 if (URL == NULL) { 612 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, 613 "invalid value URI %s\n", URI); 614 if (fragment != NULL) 615 xmlFree(fragment); 616 return(-1); 617 } 618 619 /* 620 * Check the URL against the stack for recursions 621 */ 622 if ((!local) && (xml == 1)) { 623 for (i = 0;i < ctxt->urlNr;i++) { 624 if (xmlStrEqual(URL, ctxt->urlTab[i])) { 625 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION, 626 "detected a recursion in %s\n", URL); 627 return(-1); 628 } 629 } 630 } 631 632 ref = xmlXIncludeNewRef(ctxt, URL, cur); 633 if (ref == NULL) { 634 return(-1); 635 } 636 ref->fragment = fragment; 637 ref->doc = NULL; 638 ref->xml = xml; 639 ref->count = 1; 640 xmlFree(URL); 641 return(0); 642} 643 644/** 645 * xmlXIncludeRecurseDoc: 646 * @ctxt: the XInclude context 647 * @doc: the new document 648 * @url: the associated URL 649 * 650 * The XInclude recursive nature is handled at this point. 651 */ 652static void 653xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, 654 const xmlURL url ATTRIBUTE_UNUSED) { 655 xmlXIncludeCtxtPtr newctxt; 656 int i; 657 658 /* 659 * Avoid recursion in already substitued resources 660 for (i = 0;i < ctxt->urlNr;i++) { 661 if (xmlStrEqual(doc->URL, ctxt->urlTab[i])) 662 return; 663 } 664 */ 665 666#ifdef DEBUG_XINCLUDE 667 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL); 668#endif 669 /* 670 * Handle recursion here. 671 */ 672 673 newctxt = xmlXIncludeNewContext(doc); 674 if (newctxt != NULL) { 675 /* 676 * Copy the existing document set 677 */ 678 newctxt->incMax = ctxt->incMax; 679 newctxt->incNr = ctxt->incNr; 680 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax * 681 sizeof(newctxt->incTab[0])); 682 if (newctxt->incTab == NULL) { 683 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc"); 684 xmlFree(newctxt); 685 return; 686 } 687 /* 688 * copy the urlTab 689 */ 690 newctxt->urlMax = ctxt->urlMax; 691 newctxt->urlNr = ctxt->urlNr; 692 newctxt->urlTab = ctxt->urlTab; 693 694 /* 695 * Inherit the existing base 696 */ 697 newctxt->base = xmlStrdup(ctxt->base); 698 699 /* 700 * Inherit the documents already in use by other includes 701 */ 702 newctxt->incBase = ctxt->incNr; 703 for (i = 0;i < ctxt->incNr;i++) { 704 newctxt->incTab[i] = ctxt->incTab[i]; 705 newctxt->incTab[i]->count++; /* prevent the recursion from 706 freeing it */ 707 } 708 /* 709 * The new context should also inherit the Parse Flags 710 * (bug 132597) 711 */ 712 newctxt->parseFlags = ctxt->parseFlags; 713 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc)); 714 for (i = 0;i < ctxt->incNr;i++) { 715 newctxt->incTab[i]->count--; 716 newctxt->incTab[i] = NULL; 717 } 718 719 /* urlTab may have been reallocated */ 720 ctxt->urlTab = newctxt->urlTab; 721 ctxt->urlMax = newctxt->urlMax; 722 723 newctxt->urlMax = 0; 724 newctxt->urlNr = 0; 725 newctxt->urlTab = NULL; 726 727 xmlXIncludeFreeContext(newctxt); 728 } 729#ifdef DEBUG_XINCLUDE 730 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url); 731#endif 732} 733 734/** 735 * xmlXIncludeAddTxt: 736 * @ctxt: the XInclude context 737 * @txt: the new text node 738 * @url: the associated URL 739 * 740 * Add a new txtument to the list 741 */ 742static void 743xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) { 744#ifdef DEBUG_XINCLUDE 745 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url); 746#endif 747 if (ctxt->txtMax == 0) { 748 ctxt->txtMax = 4; 749 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax * 750 sizeof(ctxt->txtTab[0])); 751 if (ctxt->txtTab == NULL) { 752 xmlXIncludeErrMemory(ctxt, NULL, "processing text"); 753 return; 754 } 755 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax * 756 sizeof(ctxt->txturlTab[0])); 757 if (ctxt->txturlTab == NULL) { 758 xmlXIncludeErrMemory(ctxt, NULL, "processing text"); 759 return; 760 } 761 } 762 if (ctxt->txtNr >= ctxt->txtMax) { 763 ctxt->txtMax *= 2; 764 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab, 765 ctxt->txtMax * sizeof(ctxt->txtTab[0])); 766 if (ctxt->txtTab == NULL) { 767 xmlXIncludeErrMemory(ctxt, NULL, "processing text"); 768 return; 769 } 770 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab, 771 ctxt->txtMax * sizeof(ctxt->txturlTab[0])); 772 if (ctxt->txturlTab == NULL) { 773 xmlXIncludeErrMemory(ctxt, NULL, "processing text"); 774 return; 775 } 776 } 777 ctxt->txtTab[ctxt->txtNr] = txt; 778 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url); 779 ctxt->txtNr++; 780} 781 782/************************************************************************ 783 * * 784 * Node copy with specific semantic * 785 * * 786 ************************************************************************/ 787 788/** 789 * xmlXIncludeCopyNode: 790 * @ctxt: the XInclude context 791 * @target: the document target 792 * @source: the document source 793 * @elem: the element 794 * 795 * Make a copy of the node while preserving the XInclude semantic 796 * of the Infoset copy 797 */ 798static xmlNodePtr 799xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, 800 xmlDocPtr source, xmlNodePtr elem) { 801 xmlNodePtr result = NULL; 802 803 if ((ctxt == NULL) || (target == NULL) || (source == NULL) || 804 (elem == NULL)) 805 return(NULL); 806 if (elem->type == XML_DTD_NODE) 807 return(NULL); 808 result = xmlDocCopyNode(elem, target, 1); 809 return(result); 810} 811 812/** 813 * xmlXIncludeCopyNodeList: 814 * @ctxt: the XInclude context 815 * @target: the document target 816 * @source: the document source 817 * @elem: the element list 818 * 819 * Make a copy of the node list while preserving the XInclude semantic 820 * of the Infoset copy 821 */ 822static xmlNodePtr 823xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, 824 xmlDocPtr source, xmlNodePtr elem) { 825 xmlNodePtr cur, res, result = NULL, last = NULL; 826 827 if ((ctxt == NULL) || (target == NULL) || (source == NULL) || 828 (elem == NULL)) 829 return(NULL); 830 cur = elem; 831 while (cur != NULL) { 832 res = xmlXIncludeCopyNode(ctxt, target, source, cur); 833 if (res != NULL) { 834 if (result == NULL) { 835 result = last = res; 836 } else { 837 last->next = res; 838 res->prev = last; 839 last = res; 840 } 841 } 842 cur = cur->next; 843 } 844 return(result); 845} 846 847/** 848 * xmlXIncludeGetNthChild: 849 * @cur: the node 850 * @no: the child number 851 * 852 * Returns the @n'th element child of @cur or NULL 853 */ 854static xmlNodePtr 855xmlXIncludeGetNthChild(xmlNodePtr cur, int no) { 856 int i; 857 if (cur == NULL) 858 return(cur); 859 cur = cur->children; 860 for (i = 0;i <= no;cur = cur->next) { 861 if (cur == NULL) 862 return(cur); 863 if ((cur->type == XML_ELEMENT_NODE) || 864 (cur->type == XML_DOCUMENT_NODE) || 865 (cur->type == XML_HTML_DOCUMENT_NODE)) { 866 i++; 867 if (i == no) 868 break; 869 } 870 } 871 return(cur); 872} 873 874xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */ 875/** 876 * xmlXIncludeCopyRange: 877 * @ctxt: the XInclude context 878 * @target: the document target 879 * @source: the document source 880 * @obj: the XPointer result from the evaluation. 881 * 882 * Build a node list tree copy of the XPointer result. 883 * 884 * Returns an xmlNodePtr list or NULL. 885 * The caller has to free the node tree. 886 */ 887static xmlNodePtr 888xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, 889 xmlDocPtr source, xmlXPathObjectPtr range) { 890 /* pointers to generated nodes */ 891 xmlNodePtr list = NULL, last = NULL, listParent = NULL; 892 xmlNodePtr tmp, tmp2; 893 /* pointers to traversal nodes */ 894 xmlNodePtr start, cur, end; 895 int index1, index2; 896 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0; 897 898 if ((ctxt == NULL) || (target == NULL) || (source == NULL) || 899 (range == NULL)) 900 return(NULL); 901 if (range->type != XPATH_RANGE) 902 return(NULL); 903 start = (xmlNodePtr) range->user; 904 905 if (start == NULL) 906 return(NULL); 907 end = range->user2; 908 if (end == NULL) 909 return(xmlDocCopyNode(start, target, 1)); 910 911 cur = start; 912 index1 = range->index; 913 index2 = range->index2; 914 /* 915 * level is depth of the current node under consideration 916 * list is the pointer to the root of the output tree 917 * listParent is a pointer to the parent of output tree (within 918 the included file) in case we need to add another level 919 * last is a pointer to the last node added to the output tree 920 * lastLevel is the depth of last (relative to the root) 921 */ 922 while (cur != NULL) { 923 /* 924 * Check if our output tree needs a parent 925 */ 926 if (level < 0) { 927 while (level < 0) { 928 /* copy must include namespaces and properties */ 929 tmp2 = xmlDocCopyNode(listParent, target, 2); 930 xmlAddChild(tmp2, list); 931 list = tmp2; 932 listParent = listParent->parent; 933 level++; 934 } 935 last = list; 936 lastLevel = 0; 937 } 938 /* 939 * Check whether we need to change our insertion point 940 */ 941 while (level < lastLevel) { 942 last = last->parent; 943 lastLevel --; 944 } 945 if (cur == end) { /* Are we at the end of the range? */ 946 if (cur->type == XML_TEXT_NODE) { 947 const xmlChar *content = cur->content; 948 int len; 949 950 if (content == NULL) { 951 tmp = xmlNewTextLen(NULL, 0); 952 } else { 953 len = index2; 954 if ((cur == start) && (index1 > 1)) { 955 content += (index1 - 1); 956 len -= (index1 - 1); 957 index1 = 0; 958 } else { 959 len = index2; 960 } 961 tmp = xmlNewTextLen(content, len); 962 } 963 /* single sub text node selection */ 964 if (list == NULL) 965 return(tmp); 966 /* prune and return full set */ 967 if (level == lastLevel) 968 xmlAddNextSibling(last, tmp); 969 else 970 xmlAddChild(last, tmp); 971 return(list); 972 } else { /* ending node not a text node */ 973 endLevel = level; /* remember the level of the end node */ 974 endFlag = 1; 975 /* last node - need to take care of properties + namespaces */ 976 tmp = xmlDocCopyNode(cur, target, 2); 977 if (list == NULL) { 978 list = tmp; 979 listParent = cur->parent; 980 } else { 981 if (level == lastLevel) 982 xmlAddNextSibling(last, tmp); 983 else { 984 xmlAddChild(last, tmp); 985 lastLevel = level; 986 } 987 } 988 last = tmp; 989 990 if (index2 > 1) { 991 end = xmlXIncludeGetNthChild(cur, index2 - 1); 992 index2 = 0; 993 } 994 if ((cur == start) && (index1 > 1)) { 995 cur = xmlXIncludeGetNthChild(cur, index1 - 1); 996 index1 = 0; 997 } else { 998 cur = cur->children; 999 } 1000 level++; /* increment level to show change */ 1001 /* 1002 * Now gather the remaining nodes from cur to end 1003 */ 1004 continue; /* while */ 1005 } 1006 } else if (cur == start) { /* Not at the end, are we at start? */ 1007 if ((cur->type == XML_TEXT_NODE) || 1008 (cur->type == XML_CDATA_SECTION_NODE)) { 1009 const xmlChar *content = cur->content; 1010 1011 if (content == NULL) { 1012 tmp = xmlNewTextLen(NULL, 0); 1013 } else { 1014 if (index1 > 1) { 1015 content += (index1 - 1); 1016 index1 = 0; 1017 } 1018 tmp = xmlNewText(content); 1019 } 1020 last = list = tmp; 1021 listParent = cur->parent; 1022 } else { /* Not text node */ 1023 /* 1024 * start of the range - need to take care of 1025 * properties and namespaces 1026 */ 1027 tmp = xmlDocCopyNode(cur, target, 2); 1028 list = last = tmp; 1029 listParent = cur->parent; 1030 if (index1 > 1) { /* Do we need to position? */ 1031 cur = xmlXIncludeGetNthChild(cur, index1 - 1); 1032 level = lastLevel = 1; 1033 index1 = 0; 1034 /* 1035 * Now gather the remaining nodes from cur to end 1036 */ 1037 continue; /* while */ 1038 } 1039 } 1040 } else { 1041 tmp = NULL; 1042 switch (cur->type) { 1043 case XML_DTD_NODE: 1044 case XML_ELEMENT_DECL: 1045 case XML_ATTRIBUTE_DECL: 1046 case XML_ENTITY_NODE: 1047 /* Do not copy DTD informations */ 1048 break; 1049 case XML_ENTITY_DECL: 1050 /* handle crossing entities -> stack needed */ 1051 break; 1052 case XML_XINCLUDE_START: 1053 case XML_XINCLUDE_END: 1054 /* don't consider it part of the tree content */ 1055 break; 1056 case XML_ATTRIBUTE_NODE: 1057 /* Humm, should not happen ! */ 1058 break; 1059 default: 1060 /* 1061 * Middle of the range - need to take care of 1062 * properties and namespaces 1063 */ 1064 tmp = xmlDocCopyNode(cur, target, 2); 1065 break; 1066 } 1067 if (tmp != NULL) { 1068 if (level == lastLevel) 1069 xmlAddNextSibling(last, tmp); 1070 else { 1071 xmlAddChild(last, tmp); 1072 lastLevel = level; 1073 } 1074 last = tmp; 1075 } 1076 } 1077 /* 1078 * Skip to next node in document order 1079 */ 1080 cur = xmlXPtrAdvanceNode(cur, &level); 1081 if (endFlag && (level >= endLevel)) 1082 break; 1083 } 1084 return(list); 1085} 1086 1087/** 1088 * xmlXIncludeBuildNodeList: 1089 * @ctxt: the XInclude context 1090 * @target: the document target 1091 * @source: the document source 1092 * @obj: the XPointer result from the evaluation. 1093 * 1094 * Build a node list tree copy of the XPointer result. 1095 * This will drop Attributes and Namespace declarations. 1096 * 1097 * Returns an xmlNodePtr list or NULL. 1098 * the caller has to free the node tree. 1099 */ 1100static xmlNodePtr 1101xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, 1102 xmlDocPtr source, xmlXPathObjectPtr obj) { 1103 xmlNodePtr list = NULL, last = NULL; 1104 int i; 1105 1106 if (source == NULL) 1107 source = ctxt->doc; 1108 if ((ctxt == NULL) || (target == NULL) || (source == NULL) || 1109 (obj == NULL)) 1110 return(NULL); 1111 switch (obj->type) { 1112 case XPATH_NODESET: { 1113 xmlNodeSetPtr set = obj->nodesetval; 1114 if (set == NULL) 1115 return(NULL); 1116 for (i = 0;i < set->nodeNr;i++) { 1117 if (set->nodeTab[i] == NULL) 1118 continue; 1119 switch (set->nodeTab[i]->type) { 1120 case XML_TEXT_NODE: 1121 case XML_CDATA_SECTION_NODE: 1122 case XML_ELEMENT_NODE: 1123 case XML_ENTITY_REF_NODE: 1124 case XML_ENTITY_NODE: 1125 case XML_PI_NODE: 1126 case XML_COMMENT_NODE: 1127 case XML_DOCUMENT_NODE: 1128 case XML_HTML_DOCUMENT_NODE: 1129#ifdef LIBXML_DOCB_ENABLED 1130 case XML_DOCB_DOCUMENT_NODE: 1131#endif 1132 case XML_XINCLUDE_END: 1133 break; 1134 case XML_XINCLUDE_START: { 1135 xmlNodePtr tmp, cur = set->nodeTab[i]; 1136 1137 cur = cur->next; 1138 while (cur != NULL) { 1139 switch(cur->type) { 1140 case XML_TEXT_NODE: 1141 case XML_CDATA_SECTION_NODE: 1142 case XML_ELEMENT_NODE: 1143 case XML_ENTITY_REF_NODE: 1144 case XML_ENTITY_NODE: 1145 case XML_PI_NODE: 1146 case XML_COMMENT_NODE: 1147 tmp = xmlXIncludeCopyNode(ctxt, target, 1148 source, cur); 1149 if (last == NULL) { 1150 list = last = tmp; 1151 } else { 1152 xmlAddNextSibling(last, tmp); 1153 last = tmp; 1154 } 1155 cur = cur->next; 1156 continue; 1157 default: 1158 break; 1159 } 1160 break; 1161 } 1162 continue; 1163 } 1164 case XML_ATTRIBUTE_NODE: 1165 case XML_NAMESPACE_DECL: 1166 case XML_DOCUMENT_TYPE_NODE: 1167 case XML_DOCUMENT_FRAG_NODE: 1168 case XML_NOTATION_NODE: 1169 case XML_DTD_NODE: 1170 case XML_ELEMENT_DECL: 1171 case XML_ATTRIBUTE_DECL: 1172 case XML_ENTITY_DECL: 1173 continue; /* for */ 1174 } 1175 if (last == NULL) 1176 list = last = xmlXIncludeCopyNode(ctxt, target, source, 1177 set->nodeTab[i]); 1178 else { 1179 xmlAddNextSibling(last, 1180 xmlXIncludeCopyNode(ctxt, target, source, 1181 set->nodeTab[i])); 1182 if (last->next != NULL) 1183 last = last->next; 1184 } 1185 } 1186 break; 1187 } 1188 case XPATH_LOCATIONSET: { 1189 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user; 1190 if (set == NULL) 1191 return(NULL); 1192 for (i = 0;i < set->locNr;i++) { 1193 if (last == NULL) 1194 list = last = xmlXIncludeCopyXPointer(ctxt, target, source, 1195 set->locTab[i]); 1196 else 1197 xmlAddNextSibling(last, 1198 xmlXIncludeCopyXPointer(ctxt, target, source, 1199 set->locTab[i])); 1200 if (last != NULL) { 1201 while (last->next != NULL) 1202 last = last->next; 1203 } 1204 } 1205 break; 1206 } 1207#ifdef LIBXML_XPTR_ENABLED 1208 case XPATH_RANGE: 1209 return(xmlXIncludeCopyRange(ctxt, target, source, obj)); 1210#endif 1211 case XPATH_POINT: 1212 /* points are ignored in XInclude */ 1213 break; 1214 default: 1215 break; 1216 } 1217 return(list); 1218} 1219/************************************************************************ 1220 * * 1221 * XInclude I/O handling * 1222 * * 1223 ************************************************************************/ 1224 1225typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData; 1226typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr; 1227struct _xmlXIncludeMergeData { 1228 xmlDocPtr doc; 1229 xmlXIncludeCtxtPtr ctxt; 1230}; 1231 1232/** 1233 * xmlXIncludeMergeOneEntity: 1234 * @ent: the entity 1235 * @doc: the including doc 1236 * @nr: the entity name 1237 * 1238 * Inplements the merge of one entity 1239 */ 1240static void 1241xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data, 1242 xmlChar *name ATTRIBUTE_UNUSED) { 1243 xmlEntityPtr ret, prev; 1244 xmlDocPtr doc; 1245 xmlXIncludeCtxtPtr ctxt; 1246 1247 if ((ent == NULL) || (data == NULL)) 1248 return; 1249 ctxt = data->ctxt; 1250 doc = data->doc; 1251 if ((ctxt == NULL) || (doc == NULL)) 1252 return; 1253 switch (ent->etype) { 1254 case XML_INTERNAL_PARAMETER_ENTITY: 1255 case XML_EXTERNAL_PARAMETER_ENTITY: 1256 case XML_INTERNAL_PREDEFINED_ENTITY: 1257 return; 1258 case XML_INTERNAL_GENERAL_ENTITY: 1259 case XML_EXTERNAL_GENERAL_PARSED_ENTITY: 1260 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: 1261 break; 1262 } 1263 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID, 1264 ent->SystemID, ent->content); 1265 if (ret != NULL) { 1266 if (ent->URI != NULL) 1267 ret->URI = xmlStrdup(ent->URI); 1268 } else { 1269 prev = xmlGetDocEntity(doc, ent->name); 1270 if (prev != NULL) { 1271 if (ent->etype != prev->etype) 1272 goto error; 1273 1274 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) { 1275 if (!xmlStrEqual(ent->SystemID, prev->SystemID)) 1276 goto error; 1277 } else if ((ent->ExternalID != NULL) && 1278 (prev->ExternalID != NULL)) { 1279 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID)) 1280 goto error; 1281 } else if ((ent->content != NULL) && (prev->content != NULL)) { 1282 if (!xmlStrEqual(ent->content, prev->content)) 1283 goto error; 1284 } else { 1285 goto error; 1286 } 1287 1288 } 1289 } 1290 return; 1291error: 1292 switch (ent->etype) { 1293 case XML_INTERNAL_PARAMETER_ENTITY: 1294 case XML_EXTERNAL_PARAMETER_ENTITY: 1295 case XML_INTERNAL_PREDEFINED_ENTITY: 1296 case XML_INTERNAL_GENERAL_ENTITY: 1297 case XML_EXTERNAL_GENERAL_PARSED_ENTITY: 1298 return; 1299 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: 1300 break; 1301 } 1302 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH, 1303 "mismatch in redefinition of entity %s\n", 1304 ent->name); 1305} 1306 1307/** 1308 * xmlXIncludeMergeEntities: 1309 * @ctxt: an XInclude context 1310 * @doc: the including doc 1311 * @from: the included doc 1312 * 1313 * Inplements the entity merge 1314 * 1315 * Returns 0 if merge succeeded, -1 if some processing failed 1316 */ 1317static int 1318xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, 1319 xmlDocPtr from) { 1320 xmlNodePtr cur; 1321 xmlDtdPtr target, source; 1322 1323 if (ctxt == NULL) 1324 return(-1); 1325 1326 if ((from == NULL) || (from->intSubset == NULL)) 1327 return(0); 1328 1329 target = doc->intSubset; 1330 if (target == NULL) { 1331 cur = xmlDocGetRootElement(doc); 1332 if (cur == NULL) 1333 return(-1); 1334 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL); 1335 if (target == NULL) 1336 return(-1); 1337 } 1338 1339 source = from->intSubset; 1340 if ((source != NULL) && (source->entities != NULL)) { 1341 xmlXIncludeMergeData data; 1342 1343 data.ctxt = ctxt; 1344 data.doc = doc; 1345 1346 xmlHashScan((xmlHashTablePtr) source->entities, 1347 (xmlHashScanner) xmlXIncludeMergeEntity, &data); 1348 } 1349 source = from->extSubset; 1350 if ((source != NULL) && (source->entities != NULL)) { 1351 xmlXIncludeMergeData data; 1352 1353 data.ctxt = ctxt; 1354 data.doc = doc; 1355 1356 /* 1357 * don't duplicate existing stuff when external subsets are the same 1358 */ 1359 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) && 1360 (!xmlStrEqual(target->SystemID, source->SystemID))) { 1361 xmlHashScan((xmlHashTablePtr) source->entities, 1362 (xmlHashScanner) xmlXIncludeMergeEntity, &data); 1363 } 1364 } 1365 return(0); 1366} 1367 1368/** 1369 * xmlXIncludeLoadDoc: 1370 * @ctxt: the XInclude context 1371 * @url: the associated URL 1372 * @nr: the xinclude node number 1373 * 1374 * Load the document, and store the result in the XInclude context 1375 * 1376 * Returns 0 in case of success, -1 in case of failure 1377 */ 1378static int 1379xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { 1380 xmlDocPtr doc; 1381 xmlURIPtr uri; 1382 xmlChar *URL; 1383 xmlChar *fragment = NULL; 1384 int i = 0; 1385#ifdef LIBXML_XPTR_ENABLED 1386 int saveFlags; 1387#endif 1388 1389#ifdef DEBUG_XINCLUDE 1390 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr); 1391#endif 1392 /* 1393 * Check the URL and remove any fragment identifier 1394 */ 1395 uri = xmlParseURI((const char *)url); 1396 if (uri == NULL) { 1397 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1398 XML_XINCLUDE_HREF_URI, 1399 "invalid value URI %s\n", url); 1400 return(-1); 1401 } 1402 if (uri->fragment != NULL) { 1403 fragment = (xmlChar *) uri->fragment; 1404 uri->fragment = NULL; 1405 } 1406 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) && 1407 (ctxt->incTab[nr]->fragment != NULL)) { 1408 if (fragment != NULL) xmlFree(fragment); 1409 fragment = xmlStrdup(ctxt->incTab[nr]->fragment); 1410 } 1411 URL = xmlSaveUri(uri); 1412 xmlFreeURI(uri); 1413 if (URL == NULL) { 1414 if (ctxt->incTab != NULL) 1415 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1416 XML_XINCLUDE_HREF_URI, 1417 "invalid value URI %s\n", url); 1418 else 1419 xmlXIncludeErr(ctxt, NULL, 1420 XML_XINCLUDE_HREF_URI, 1421 "invalid value URI %s\n", url); 1422 if (fragment != NULL) 1423 xmlFree(fragment); 1424 return(-1); 1425 } 1426 1427 /* 1428 * Handling of references to the local document are done 1429 * directly through ctxt->doc. 1430 */ 1431 if ((URL[0] == 0) || (URL[0] == '#') || 1432 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) { 1433 doc = NULL; 1434 goto loaded; 1435 } 1436 1437 /* 1438 * Prevent reloading twice the document. 1439 */ 1440 for (i = 0; i < ctxt->incNr; i++) { 1441 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) && 1442 (ctxt->incTab[i]->doc != NULL)) { 1443 doc = ctxt->incTab[i]->doc; 1444#ifdef DEBUG_XINCLUDE 1445 printf("Already loaded %s\n", URL); 1446#endif 1447 goto loaded; 1448 } 1449 } 1450 1451 /* 1452 * Load it. 1453 */ 1454#ifdef DEBUG_XINCLUDE 1455 printf("loading %s\n", URL); 1456#endif 1457#ifdef LIBXML_XPTR_ENABLED 1458 /* 1459 * If this is an XPointer evaluation, we want to assure that 1460 * all entities have been resolved prior to processing the 1461 * referenced document 1462 */ 1463 saveFlags = ctxt->parseFlags; 1464 if (fragment != NULL) { /* if this is an XPointer eval */ 1465 ctxt->parseFlags |= XML_PARSE_NOENT; 1466 } 1467#endif 1468 1469 doc = xmlXIncludeParseFile(ctxt, (const char *)URL); 1470#ifdef LIBXML_XPTR_ENABLED 1471 ctxt->parseFlags = saveFlags; 1472#endif 1473 if (doc == NULL) { 1474 xmlFree(URL); 1475 if (fragment != NULL) 1476 xmlFree(fragment); 1477 return(-1); 1478 } 1479 ctxt->incTab[nr]->doc = doc; 1480 /* 1481 * It's possible that the requested URL has been mapped to a 1482 * completely different location (e.g. through a catalog entry). 1483 * To check for this, we compare the URL with that of the doc 1484 * and change it if they disagree (bug 146988). 1485 */ 1486 if (!xmlStrEqual(URL, doc->URL)) { 1487 xmlFree(URL); 1488 URL = xmlStrdup(doc->URL); 1489 } 1490 for (i = nr + 1; i < ctxt->incNr; i++) { 1491 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) { 1492 ctxt->incTab[nr]->count++; 1493#ifdef DEBUG_XINCLUDE 1494 printf("Increasing %s count since reused\n", URL); 1495#endif 1496 break; 1497 } 1498 } 1499 1500 /* 1501 * Make sure we have all entities fixed up 1502 */ 1503 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc); 1504 1505 /* 1506 * We don't need the DTD anymore, free up space 1507 if (doc->intSubset != NULL) { 1508 xmlUnlinkNode((xmlNodePtr) doc->intSubset); 1509 xmlFreeNode((xmlNodePtr) doc->intSubset); 1510 doc->intSubset = NULL; 1511 } 1512 if (doc->extSubset != NULL) { 1513 xmlUnlinkNode((xmlNodePtr) doc->extSubset); 1514 xmlFreeNode((xmlNodePtr) doc->extSubset); 1515 doc->extSubset = NULL; 1516 } 1517 */ 1518 xmlXIncludeRecurseDoc(ctxt, doc, URL); 1519 1520loaded: 1521 if (fragment == NULL) { 1522 /* 1523 * Add the top children list as the replacement copy. 1524 */ 1525 if (doc == NULL) 1526 { 1527 /* Hopefully a DTD declaration won't be copied from 1528 * the same document */ 1529 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children); 1530 } else { 1531 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc, 1532 doc, doc->children); 1533 } 1534 } 1535#ifdef LIBXML_XPTR_ENABLED 1536 else { 1537 /* 1538 * Computes the XPointer expression and make a copy used 1539 * as the replacement copy. 1540 */ 1541 xmlXPathObjectPtr xptr; 1542 xmlXPathContextPtr xptrctxt; 1543 xmlNodeSetPtr set; 1544 1545 if (doc == NULL) { 1546 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref, 1547 NULL); 1548 } else { 1549 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL); 1550 } 1551 if (xptrctxt == NULL) { 1552 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1553 XML_XINCLUDE_XPTR_FAILED, 1554 "could not create XPointer context\n", NULL); 1555 xmlFree(URL); 1556 xmlFree(fragment); 1557 return(-1); 1558 } 1559 xptr = xmlXPtrEval(fragment, xptrctxt); 1560 if (xptr == NULL) { 1561 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1562 XML_XINCLUDE_XPTR_FAILED, 1563 "XPointer evaluation failed: #%s\n", 1564 fragment); 1565 xmlXPathFreeContext(xptrctxt); 1566 xmlFree(URL); 1567 xmlFree(fragment); 1568 return(-1); 1569 } 1570 switch (xptr->type) { 1571 case XPATH_UNDEFINED: 1572 case XPATH_BOOLEAN: 1573 case XPATH_NUMBER: 1574 case XPATH_STRING: 1575 case XPATH_POINT: 1576 case XPATH_USERS: 1577 case XPATH_XSLT_TREE: 1578 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1579 XML_XINCLUDE_XPTR_RESULT, 1580 "XPointer is not a range: #%s\n", 1581 fragment); 1582 xmlXPathFreeContext(xptrctxt); 1583 xmlFree(URL); 1584 xmlFree(fragment); 1585 return(-1); 1586 case XPATH_NODESET: 1587 if ((xptr->nodesetval == NULL) || 1588 (xptr->nodesetval->nodeNr <= 0)) { 1589 xmlXPathFreeContext(xptrctxt); 1590 xmlFree(URL); 1591 xmlFree(fragment); 1592 return(-1); 1593 } 1594 1595 case XPATH_RANGE: 1596 case XPATH_LOCATIONSET: 1597 break; 1598 } 1599 set = xptr->nodesetval; 1600 if (set != NULL) { 1601 for (i = 0;i < set->nodeNr;i++) { 1602 if (set->nodeTab[i] == NULL) 1603 continue; 1604 switch (set->nodeTab[i]->type) { 1605 case XML_TEXT_NODE: 1606 case XML_CDATA_SECTION_NODE: 1607 case XML_ENTITY_REF_NODE: 1608 case XML_ENTITY_NODE: 1609 case XML_PI_NODE: 1610 case XML_COMMENT_NODE: 1611 case XML_DOCUMENT_NODE: 1612 case XML_HTML_DOCUMENT_NODE: 1613#ifdef LIBXML_DOCB_ENABLED 1614 case XML_DOCB_DOCUMENT_NODE: 1615#endif 1616 continue; 1617 case XML_ELEMENT_NODE: { 1618 xmlChar *nodeBase; 1619 xmlNodePtr el = set->nodeTab[i]; 1620 1621 nodeBase = xmlNodeGetBase(el->doc, el); 1622 if (nodeBase != NULL) { 1623 if (!xmlStrEqual(nodeBase, el->doc->URL)) 1624 xmlNodeSetBase(el, nodeBase); 1625 xmlFree(nodeBase); 1626 } 1627 continue; 1628 } 1629 1630 case XML_ATTRIBUTE_NODE: 1631 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1632 XML_XINCLUDE_XPTR_RESULT, 1633 "XPointer selects an attribute: #%s\n", 1634 fragment); 1635 set->nodeTab[i] = NULL; 1636 continue; 1637 case XML_NAMESPACE_DECL: 1638 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1639 XML_XINCLUDE_XPTR_RESULT, 1640 "XPointer selects a namespace: #%s\n", 1641 fragment); 1642 set->nodeTab[i] = NULL; 1643 continue; 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 case XML_XINCLUDE_START: 1652 case XML_XINCLUDE_END: 1653 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1654 XML_XINCLUDE_XPTR_RESULT, 1655 "XPointer selects unexpected nodes: #%s\n", 1656 fragment); 1657 set->nodeTab[i] = NULL; 1658 set->nodeTab[i] = NULL; 1659 continue; /* for */ 1660 } 1661 } 1662 } 1663 if (doc == NULL) { 1664 ctxt->incTab[nr]->xptr = xptr; 1665 ctxt->incTab[nr]->inc = NULL; 1666 } else { 1667 ctxt->incTab[nr]->inc = 1668 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr); 1669 xmlXPathFreeObject(xptr); 1670 } 1671 xmlXPathFreeContext(xptrctxt); 1672 xmlFree(fragment); 1673 } 1674#endif 1675 1676 /* 1677 * Do the xml:base fixup if needed 1678 */ 1679 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) { 1680 xmlNodePtr node; 1681 xmlChar *base; 1682 xmlChar *curBase; 1683 1684 /* 1685 * The base is only adjusted if "necessary", i.e. if the xinclude node 1686 * has a base specified, or the URL is relative 1687 */ 1688 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base", 1689 XML_XML_NAMESPACE); 1690 if (base == NULL) { 1691 /* 1692 * No xml:base on the xinclude node, so we check whether the 1693 * URI base is different than (relative to) the context base 1694 */ 1695 curBase = xmlBuildRelativeURI(URL, ctxt->base); 1696 if (curBase == NULL) { /* Error return */ 1697 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1698 XML_XINCLUDE_HREF_URI, 1699 "trying to build relative URI from %s\n", URL); 1700 } else { 1701 /* If the URI doesn't contain a slash, it's not relative */ 1702 if (!xmlStrchr(curBase, (xmlChar) '/')) 1703 xmlFree(curBase); 1704 else 1705 base = curBase; 1706 } 1707 } 1708 if (base != NULL) { /* Adjustment may be needed */ 1709 node = ctxt->incTab[nr]->inc; 1710 while (node != NULL) { 1711 /* Only work on element nodes */ 1712 if (node->type == XML_ELEMENT_NODE) { 1713 curBase = xmlNodeGetBase(node->doc, node); 1714 /* If no current base, set it */ 1715 if (curBase == NULL) { 1716 xmlNodeSetBase(node, base); 1717 } else { 1718 /* 1719 * If the current base is the same as the 1720 * URL of the document, then reset it to be 1721 * the specified xml:base or the relative URI 1722 */ 1723 if (xmlStrEqual(curBase, node->doc->URL)) { 1724 xmlNodeSetBase(node, base); 1725 } else { 1726 /* 1727 * If the element already has an xml:base 1728 * set, then relativise it if necessary 1729 */ 1730 xmlChar *xmlBase; 1731 xmlBase = xmlGetNsProp(node, 1732 BAD_CAST "base", 1733 XML_XML_NAMESPACE); 1734 if (xmlBase != NULL) { 1735 xmlChar *relBase; 1736 relBase = xmlBuildURI(xmlBase, base); 1737 if (relBase == NULL) { /* error */ 1738 xmlXIncludeErr(ctxt, 1739 ctxt->incTab[nr]->ref, 1740 XML_XINCLUDE_HREF_URI, 1741 "trying to rebuild base from %s\n", 1742 xmlBase); 1743 } else { 1744 xmlNodeSetBase(node, relBase); 1745 xmlFree(relBase); 1746 } 1747 xmlFree(xmlBase); 1748 } 1749 } 1750 xmlFree(curBase); 1751 } 1752 } 1753 node = node->next; 1754 } 1755 xmlFree(base); 1756 } 1757 } 1758 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) && 1759 (ctxt->incTab[nr]->count <= 1)) { 1760#ifdef DEBUG_XINCLUDE 1761 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL); 1762#endif 1763 xmlFreeDoc(ctxt->incTab[nr]->doc); 1764 ctxt->incTab[nr]->doc = NULL; 1765 } 1766 xmlFree(URL); 1767 return(0); 1768} 1769 1770/** 1771 * xmlXIncludeLoadTxt: 1772 * @ctxt: the XInclude context 1773 * @url: the associated URL 1774 * @nr: the xinclude node number 1775 * 1776 * Load the content, and store the result in the XInclude context 1777 * 1778 * Returns 0 in case of success, -1 in case of failure 1779 */ 1780static int 1781xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { 1782 xmlParserInputBufferPtr buf; 1783 xmlNodePtr node; 1784 xmlURIPtr uri; 1785 xmlChar *URL; 1786 int i; 1787 xmlChar *encoding = NULL; 1788 xmlCharEncoding enc = (xmlCharEncoding) 0; 1789 1790 /* 1791 * Check the URL and remove any fragment identifier 1792 */ 1793 uri = xmlParseURI((const char *)url); 1794 if (uri == NULL) { 1795 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI, 1796 "invalid value URI %s\n", url); 1797 return(-1); 1798 } 1799 if (uri->fragment != NULL) { 1800 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT, 1801 "fragment identifier forbidden for text: %s\n", 1802 (const xmlChar *) uri->fragment); 1803 xmlFreeURI(uri); 1804 return(-1); 1805 } 1806 URL = xmlSaveUri(uri); 1807 xmlFreeURI(uri); 1808 if (URL == NULL) { 1809 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI, 1810 "invalid value URI %s\n", url); 1811 return(-1); 1812 } 1813 1814 /* 1815 * Handling of references to the local document are done 1816 * directly through ctxt->doc. 1817 */ 1818 if (URL[0] == 0) { 1819 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1820 XML_XINCLUDE_TEXT_DOCUMENT, 1821 "text serialization of document not available\n", NULL); 1822 xmlFree(URL); 1823 return(-1); 1824 } 1825 1826 /* 1827 * Prevent reloading twice the document. 1828 */ 1829 for (i = 0; i < ctxt->txtNr; i++) { 1830 if (xmlStrEqual(URL, ctxt->txturlTab[i])) { 1831 node = xmlCopyNode(ctxt->txtTab[i], 1); 1832 goto loaded; 1833 } 1834 } 1835 /* 1836 * Try to get the encoding if available 1837 */ 1838 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) { 1839 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING); 1840 } 1841 if (encoding != NULL) { 1842 /* 1843 * TODO: we should not have to remap to the xmlCharEncoding 1844 * predefined set, a better interface than 1845 * xmlParserInputBufferCreateFilename should allow any 1846 * encoding supported by iconv 1847 */ 1848 enc = xmlParseCharEncoding((const char *) encoding); 1849 if (enc == XML_CHAR_ENCODING_ERROR) { 1850 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1851 XML_XINCLUDE_UNKNOWN_ENCODING, 1852 "encoding %s not supported\n", encoding); 1853 xmlFree(encoding); 1854 xmlFree(URL); 1855 return(-1); 1856 } 1857 xmlFree(encoding); 1858 } 1859 1860 /* 1861 * Load it. 1862 */ 1863 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc); 1864 if (buf == NULL) { 1865 xmlFree(URL); 1866 return(-1); 1867 } 1868 node = xmlNewText(NULL); 1869 1870 /* 1871 * Scan all chars from the resource and add the to the node 1872 */ 1873 while (xmlParserInputBufferRead(buf, 128) > 0) { 1874 int len; 1875 const xmlChar *content; 1876 1877 content = xmlBufferContent(buf->buffer); 1878 len = xmlBufferLength(buf->buffer); 1879 for (i = 0;i < len;) { 1880 int cur; 1881 int l; 1882 1883 cur = xmlStringCurrentChar(NULL, &content[i], &l); 1884 if (!IS_CHAR(cur)) { 1885 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 1886 XML_XINCLUDE_INVALID_CHAR, 1887 "%s contains invalid char\n", URL); 1888 } else { 1889 xmlNodeAddContentLen(node, &content[i], l); 1890 } 1891 i += l; 1892 } 1893 xmlBufferShrink(buf->buffer, len); 1894 } 1895 xmlFreeParserInputBuffer(buf); 1896 xmlXIncludeAddTxt(ctxt, node, URL); 1897 1898loaded: 1899 /* 1900 * Add the element as the replacement copy. 1901 */ 1902 ctxt->incTab[nr]->inc = node; 1903 xmlFree(URL); 1904 return(0); 1905} 1906 1907/** 1908 * xmlXIncludeLoadFallback: 1909 * @ctxt: the XInclude context 1910 * @fallback: the fallback node 1911 * @nr: the xinclude node number 1912 * 1913 * Load the content of the fallback node, and store the result 1914 * in the XInclude context 1915 * 1916 * Returns 0 in case of success, -1 in case of failure 1917 */ 1918static int 1919xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) { 1920 xmlXIncludeCtxtPtr newctxt; 1921 int ret = 0; 1922 1923 if ((fallback == NULL) || (ctxt == NULL)) 1924 return(-1); 1925 if (fallback->children != NULL) { 1926 /* 1927 * It's possible that the fallback also has 'includes' 1928 * (Bug 129969), so we re-process the fallback just in case 1929 */ 1930 newctxt = xmlXIncludeNewContext(ctxt->doc); 1931 if (newctxt == NULL) 1932 return (-1); 1933 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */ 1934 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags); 1935 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children); 1936 if (ctxt->nbErrors > 0) 1937 ret = -1; 1938 else if (ret > 0) 1939 ret = 0; /* xmlXIncludeDoProcess can return +ve number */ 1940 xmlXIncludeFreeContext(newctxt); 1941 1942 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc, 1943 fallback->children); 1944 } else { 1945 ctxt->incTab[nr]->inc = NULL; 1946 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */ 1947 } 1948 return(ret); 1949} 1950 1951/************************************************************************ 1952 * * 1953 * XInclude Processing * 1954 * * 1955 ************************************************************************/ 1956 1957/** 1958 * xmlXIncludePreProcessNode: 1959 * @ctxt: an XInclude context 1960 * @node: an XInclude node 1961 * 1962 * Implement the XInclude preprocessing, currently just adding the element 1963 * for further processing. 1964 * 1965 * Returns the result list or NULL in case of error 1966 */ 1967static xmlNodePtr 1968xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { 1969 xmlXIncludeAddNode(ctxt, node); 1970 return(NULL); 1971} 1972 1973/** 1974 * xmlXIncludeLoadNode: 1975 * @ctxt: an XInclude context 1976 * @nr: the node number 1977 * 1978 * Find and load the infoset replacement for the given node. 1979 * 1980 * Returns 0 if substitution succeeded, -1 if some processing failed 1981 */ 1982static int 1983xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) { 1984 xmlNodePtr cur; 1985 xmlChar *href; 1986 xmlChar *parse; 1987 xmlChar *base; 1988 xmlChar *oldBase; 1989 xmlChar *URI; 1990 int xml = 1; /* default Issue 64 */ 1991 int ret; 1992 1993 if (ctxt == NULL) 1994 return(-1); 1995 if ((nr < 0) || (nr >= ctxt->incNr)) 1996 return(-1); 1997 cur = ctxt->incTab[nr]->ref; 1998 if (cur == NULL) 1999 return(-1); 2000 2001 /* 2002 * read the attributes 2003 */ 2004 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF); 2005 if (href == NULL) { 2006 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */ 2007 if (href == NULL) 2008 return(-1); 2009 } 2010 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE); 2011 if (parse != NULL) { 2012 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML)) 2013 xml = 1; 2014 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) 2015 xml = 0; 2016 else { 2017 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 2018 XML_XINCLUDE_PARSE_VALUE, 2019 "invalid value %s for 'parse'\n", parse); 2020 if (href != NULL) 2021 xmlFree(href); 2022 if (parse != NULL) 2023 xmlFree(parse); 2024 return(-1); 2025 } 2026 } 2027 2028 /* 2029 * compute the URI 2030 */ 2031 base = xmlNodeGetBase(ctxt->doc, cur); 2032 if (base == NULL) { 2033 URI = xmlBuildURI(href, ctxt->doc->URL); 2034 } else { 2035 URI = xmlBuildURI(href, base); 2036 } 2037 if (URI == NULL) { 2038 xmlChar *escbase; 2039 xmlChar *eschref; 2040 /* 2041 * Some escaping may be needed 2042 */ 2043 escbase = xmlURIEscape(base); 2044 eschref = xmlURIEscape(href); 2045 URI = xmlBuildURI(eschref, escbase); 2046 if (escbase != NULL) 2047 xmlFree(escbase); 2048 if (eschref != NULL) 2049 xmlFree(eschref); 2050 } 2051 if (URI == NULL) { 2052 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 2053 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL); 2054 if (parse != NULL) 2055 xmlFree(parse); 2056 if (href != NULL) 2057 xmlFree(href); 2058 if (base != NULL) 2059 xmlFree(base); 2060 return(-1); 2061 } 2062#ifdef DEBUG_XINCLUDE 2063 xmlGenericError(xmlGenericErrorContext, "parse: %s\n", 2064 xml ? "xml": "text"); 2065 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI); 2066#endif 2067 2068 /* 2069 * Save the base for this include (saving the current one) 2070 */ 2071 oldBase = ctxt->base; 2072 ctxt->base = base; 2073 2074 if (xml) { 2075 ret = xmlXIncludeLoadDoc(ctxt, URI, nr); 2076 /* xmlXIncludeGetFragment(ctxt, cur, URI); */ 2077 } else { 2078 ret = xmlXIncludeLoadTxt(ctxt, URI, nr); 2079 } 2080 2081 /* 2082 * Restore the original base before checking for fallback 2083 */ 2084 ctxt->base = oldBase; 2085 2086 if (ret < 0) { 2087 xmlNodePtr children; 2088 2089 /* 2090 * Time to try a fallback if availble 2091 */ 2092#ifdef DEBUG_XINCLUDE 2093 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n"); 2094#endif 2095 children = cur->children; 2096 while (children != NULL) { 2097 if ((children->type == XML_ELEMENT_NODE) && 2098 (children->ns != NULL) && 2099 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) && 2100 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) || 2101 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) { 2102 ret = xmlXIncludeLoadFallback(ctxt, children, nr); 2103 if (ret == 0) 2104 break; 2105 } 2106 children = children->next; 2107 } 2108 } 2109 if (ret < 0) { 2110 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 2111 XML_XINCLUDE_NO_FALLBACK, 2112 "could not load %s, and no fallback was found\n", 2113 URI); 2114 } 2115 2116 /* 2117 * Cleanup 2118 */ 2119 if (URI != NULL) 2120 xmlFree(URI); 2121 if (parse != NULL) 2122 xmlFree(parse); 2123 if (href != NULL) 2124 xmlFree(href); 2125 if (base != NULL) 2126 xmlFree(base); 2127 return(0); 2128} 2129 2130/** 2131 * xmlXIncludeIncludeNode: 2132 * @ctxt: an XInclude context 2133 * @nr: the node number 2134 * 2135 * Inplement the infoset replacement for the given node 2136 * 2137 * Returns 0 if substitution succeeded, -1 if some processing failed 2138 */ 2139static int 2140xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) { 2141 xmlNodePtr cur, end, list, tmp; 2142 2143 if (ctxt == NULL) 2144 return(-1); 2145 if ((nr < 0) || (nr >= ctxt->incNr)) 2146 return(-1); 2147 cur = ctxt->incTab[nr]->ref; 2148 if (cur == NULL) 2149 return(-1); 2150 2151 /* 2152 * If we stored an XPointer a late computation may be needed 2153 */ 2154 if ((ctxt->incTab[nr]->inc == NULL) && 2155 (ctxt->incTab[nr]->xptr != NULL)) { 2156 ctxt->incTab[nr]->inc = 2157 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc, 2158 ctxt->incTab[nr]->xptr); 2159 xmlXPathFreeObject(ctxt->incTab[nr]->xptr); 2160 ctxt->incTab[nr]->xptr = NULL; 2161 } 2162 list = ctxt->incTab[nr]->inc; 2163 ctxt->incTab[nr]->inc = NULL; 2164 2165 /* 2166 * Check against the risk of generating a multi-rooted document 2167 */ 2168 if ((cur->parent != NULL) && 2169 (cur->parent->type != XML_ELEMENT_NODE)) { 2170 int nb_elem = 0; 2171 2172 tmp = list; 2173 while (tmp != NULL) { 2174 if (tmp->type == XML_ELEMENT_NODE) 2175 nb_elem++; 2176 tmp = tmp->next; 2177 } 2178 if (nb_elem > 1) { 2179 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 2180 XML_XINCLUDE_MULTIPLE_ROOT, 2181 "XInclude error: would result in multiple root nodes\n", 2182 NULL); 2183 return(-1); 2184 } 2185 } 2186 2187 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) { 2188 /* 2189 * Add the list of nodes 2190 */ 2191 while (list != NULL) { 2192 end = list; 2193 list = list->next; 2194 2195 xmlAddPrevSibling(cur, end); 2196 } 2197 xmlUnlinkNode(cur); 2198 xmlFreeNode(cur); 2199 } else { 2200 /* 2201 * Change the current node as an XInclude start one, and add an 2202 * XInclude end one 2203 */ 2204 cur->type = XML_XINCLUDE_START; 2205 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL); 2206 if (end == NULL) { 2207 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, 2208 XML_XINCLUDE_BUILD_FAILED, 2209 "failed to build node\n", NULL); 2210 return(-1); 2211 } 2212 end->type = XML_XINCLUDE_END; 2213 xmlAddNextSibling(cur, end); 2214 2215 /* 2216 * Add the list of nodes 2217 */ 2218 while (list != NULL) { 2219 cur = list; 2220 list = list->next; 2221 2222 xmlAddPrevSibling(end, cur); 2223 } 2224 } 2225 2226 2227 return(0); 2228} 2229 2230/** 2231 * xmlXIncludeTestNode: 2232 * @ctxt: the XInclude processing context 2233 * @node: an XInclude node 2234 * 2235 * test if the node is an XInclude node 2236 * 2237 * Returns 1 true, 0 otherwise 2238 */ 2239static int 2240xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { 2241 if (node == NULL) 2242 return(0); 2243 if (node->type != XML_ELEMENT_NODE) 2244 return(0); 2245 if (node->ns == NULL) 2246 return(0); 2247 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) || 2248 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) { 2249 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) { 2250 if (ctxt->legacy == 0) { 2251#if 0 /* wait for the XML Core Working Group to get something stable ! */ 2252 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS, 2253 "Deprecated XInclude namespace found, use %s", 2254 XINCLUDE_NS); 2255#endif 2256 ctxt->legacy = 1; 2257 } 2258 } 2259 if (xmlStrEqual(node->name, XINCLUDE_NODE)) { 2260 xmlNodePtr child = node->children; 2261 int nb_fallback = 0; 2262 2263 while (child != NULL) { 2264 if ((child->type == XML_ELEMENT_NODE) && 2265 (child->ns != NULL) && 2266 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) || 2267 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) { 2268 if (xmlStrEqual(child->name, XINCLUDE_NODE)) { 2269 xmlXIncludeErr(ctxt, node, 2270 XML_XINCLUDE_INCLUDE_IN_INCLUDE, 2271 "%s has an 'include' child\n", 2272 XINCLUDE_NODE); 2273 return(0); 2274 } 2275 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) { 2276 nb_fallback++; 2277 } 2278 } 2279 child = child->next; 2280 } 2281 if (nb_fallback > 1) { 2282 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE, 2283 "%s has multiple fallback children\n", 2284 XINCLUDE_NODE); 2285 return(0); 2286 } 2287 return(1); 2288 } 2289 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) { 2290 if ((node->parent == NULL) || 2291 (node->parent->type != XML_ELEMENT_NODE) || 2292 (node->parent->ns == NULL) || 2293 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) && 2294 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) || 2295 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) { 2296 xmlXIncludeErr(ctxt, node, 2297 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE, 2298 "%s is not the child of an 'include'\n", 2299 XINCLUDE_FALLBACK); 2300 } 2301 } 2302 } 2303 return(0); 2304} 2305 2306/** 2307 * xmlXIncludeDoProcess: 2308 * @ctxt: the XInclude processing context 2309 * @doc: an XML document 2310 * @tree: the top of the tree to process 2311 * 2312 * Implement the XInclude substitution on the XML document @doc 2313 * 2314 * Returns 0 if no substitution were done, -1 if some processing failed 2315 * or the number of substitutions done. 2316 */ 2317static int 2318xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) { 2319 xmlNodePtr cur; 2320 int ret = 0; 2321 int i, start; 2322 2323 if ((doc == NULL) || (tree == NULL)) 2324 return(-1); 2325 if (ctxt == NULL) 2326 return(-1); 2327 2328 if (doc->URL != NULL) { 2329 ret = xmlXIncludeURLPush(ctxt, doc->URL); 2330 if (ret < 0) 2331 return(-1); 2332 } 2333 start = ctxt->incNr; 2334 2335 /* 2336 * First phase: lookup the elements in the document 2337 */ 2338 cur = tree; 2339 if (xmlXIncludeTestNode(ctxt, cur) == 1) 2340 xmlXIncludePreProcessNode(ctxt, cur); 2341 while ((cur != NULL) && (cur != tree->parent)) { 2342 /* TODO: need to work on entities -> stack */ 2343 if ((cur->children != NULL) && 2344 (cur->children->type != XML_ENTITY_DECL) && 2345 (cur->children->type != XML_XINCLUDE_START) && 2346 (cur->children->type != XML_XINCLUDE_END)) { 2347 cur = cur->children; 2348 if (xmlXIncludeTestNode(ctxt, cur)) 2349 xmlXIncludePreProcessNode(ctxt, cur); 2350 } else if (cur->next != NULL) { 2351 cur = cur->next; 2352 if (xmlXIncludeTestNode(ctxt, cur)) 2353 xmlXIncludePreProcessNode(ctxt, cur); 2354 } else { 2355 if (cur == tree) 2356 break; 2357 do { 2358 cur = cur->parent; 2359 if ((cur == NULL) || (cur == tree->parent)) 2360 break; /* do */ 2361 if (cur->next != NULL) { 2362 cur = cur->next; 2363 if (xmlXIncludeTestNode(ctxt, cur)) 2364 xmlXIncludePreProcessNode(ctxt, cur); 2365 break; /* do */ 2366 } 2367 } while (cur != NULL); 2368 } 2369 } 2370 2371 /* 2372 * Second Phase : collect the infosets fragments 2373 */ 2374 for (i = start;i < ctxt->incNr; i++) { 2375 xmlXIncludeLoadNode(ctxt, i); 2376 ret++; 2377 } 2378 2379 /* 2380 * Third phase: extend the original document infoset. 2381 * 2382 * Originally we bypassed the inclusion if there were any errors 2383 * encountered on any of the XIncludes. A bug was raised (bug 2384 * 132588) requesting that we output the XIncludes without error, 2385 * so the check for inc!=NULL || xptr!=NULL was put in. This may 2386 * give some other problems in the future, but for now it seems to 2387 * work ok. 2388 * 2389 */ 2390 for (i = ctxt->incBase;i < ctxt->incNr; i++) { 2391 if ((ctxt->incTab[i]->inc != NULL) || 2392 (ctxt->incTab[i]->xptr != NULL) || 2393 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */ 2394 xmlXIncludeIncludeNode(ctxt, i); 2395 } 2396 2397 if (doc->URL != NULL) 2398 xmlXIncludeURLPop(ctxt); 2399 return(ret); 2400} 2401 2402/** 2403 * xmlXIncludeSetFlags: 2404 * @ctxt: an XInclude processing context 2405 * @flags: a set of xmlParserOption used for parsing XML includes 2406 * 2407 * Set the flags used for further processing of XML resources. 2408 * 2409 * Returns 0 in case of success and -1 in case of error. 2410 */ 2411int 2412xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) { 2413 if (ctxt == NULL) 2414 return(-1); 2415 ctxt->parseFlags = flags; 2416 return(0); 2417} 2418 2419/** 2420 * xmlXIncludeProcessFlagsData: 2421 * @doc: an XML document 2422 * @flags: a set of xmlParserOption used for parsing XML includes 2423 * @data: application data that will be passed to the parser context 2424 * in the _private field of the parser context(s) 2425 * 2426 * Implement the XInclude substitution on the XML document @doc 2427 * 2428 * Returns 0 if no substitution were done, -1 if some processing failed 2429 * or the number of substitutions done. 2430 */ 2431int 2432xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) { 2433 xmlXIncludeCtxtPtr ctxt; 2434 xmlNodePtr tree; 2435 int ret = 0; 2436 2437 if (doc == NULL) 2438 return(-1); 2439 tree = xmlDocGetRootElement(doc); 2440 if (tree == NULL) 2441 return(-1); 2442 ctxt = xmlXIncludeNewContext(doc); 2443 if (ctxt == NULL) 2444 return(-1); 2445 ctxt->_private = data; 2446 ctxt->base = xmlStrdup((xmlChar *)doc->URL); 2447 xmlXIncludeSetFlags(ctxt, flags); 2448 ret = xmlXIncludeDoProcess(ctxt, doc, tree); 2449 if ((ret >= 0) && (ctxt->nbErrors > 0)) 2450 ret = -1; 2451 2452 xmlXIncludeFreeContext(ctxt); 2453 return(ret); 2454} 2455 2456/** 2457 * xmlXIncludeProcessFlags: 2458 * @doc: an XML document 2459 * @flags: a set of xmlParserOption used for parsing XML includes 2460 * 2461 * Implement the XInclude substitution on the XML document @doc 2462 * 2463 * Returns 0 if no substitution were done, -1 if some processing failed 2464 * or the number of substitutions done. 2465 */ 2466int 2467xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) { 2468 return xmlXIncludeProcessFlagsData(doc, flags, NULL); 2469} 2470 2471/** 2472 * xmlXIncludeProcess: 2473 * @doc: an XML document 2474 * 2475 * Implement the XInclude substitution on the XML document @doc 2476 * 2477 * Returns 0 if no substitution were done, -1 if some processing failed 2478 * or the number of substitutions done. 2479 */ 2480int 2481xmlXIncludeProcess(xmlDocPtr doc) { 2482 return(xmlXIncludeProcessFlags(doc, 0)); 2483} 2484 2485/** 2486 * xmlXIncludeProcessTreeFlags: 2487 * @tree: a node in an XML document 2488 * @flags: a set of xmlParserOption used for parsing XML includes 2489 * 2490 * Implement the XInclude substitution for the given subtree 2491 * 2492 * Returns 0 if no substitution were done, -1 if some processing failed 2493 * or the number of substitutions done. 2494 */ 2495int 2496xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) { 2497 xmlXIncludeCtxtPtr ctxt; 2498 int ret = 0; 2499 2500 if ((tree == NULL) || (tree->doc == NULL)) 2501 return(-1); 2502 ctxt = xmlXIncludeNewContext(tree->doc); 2503 if (ctxt == NULL) 2504 return(-1); 2505 ctxt->base = xmlNodeGetBase(tree->doc, tree); 2506 xmlXIncludeSetFlags(ctxt, flags); 2507 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree); 2508 if ((ret >= 0) && (ctxt->nbErrors > 0)) 2509 ret = -1; 2510 2511 xmlXIncludeFreeContext(ctxt); 2512 return(ret); 2513} 2514 2515/** 2516 * xmlXIncludeProcessTree: 2517 * @tree: a node in an XML document 2518 * 2519 * Implement the XInclude substitution for the given subtree 2520 * 2521 * Returns 0 if no substitution were done, -1 if some processing failed 2522 * or the number of substitutions done. 2523 */ 2524int 2525xmlXIncludeProcessTree(xmlNodePtr tree) { 2526 return(xmlXIncludeProcessTreeFlags(tree, 0)); 2527} 2528 2529/** 2530 * xmlXIncludeProcessNode: 2531 * @ctxt: an existing XInclude context 2532 * @node: a node in an XML document 2533 * 2534 * Implement the XInclude substitution for the given subtree reusing 2535 * the informations and data coming from the given context. 2536 * 2537 * Returns 0 if no substitution were done, -1 if some processing failed 2538 * or the number of substitutions done. 2539 */ 2540int 2541xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { 2542 int ret = 0; 2543 2544 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL)) 2545 return(-1); 2546 ret = xmlXIncludeDoProcess(ctxt, node->doc, node); 2547 if ((ret >= 0) && (ctxt->nbErrors > 0)) 2548 ret = -1; 2549 return(ret); 2550} 2551 2552#else /* !LIBXML_XINCLUDE_ENABLED */ 2553#endif 2554#define bottom_xinclude 2555#include "elfgcchack.h" 2556