VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.31/xinclude.c@ 53409

Last change on this file since 53409 was 39915, checked in by vboxsync, 13 years ago

libxml-2.6.31 unmodified

  • Property svn:eol-style set to native
File size: 65.2 KB
Line 
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 * [email protected]
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 private user data
677 */
678 newctxt->_private = ctxt->_private;
679 /*
680 * Copy the existing document set
681 */
682 newctxt->incMax = ctxt->incMax;
683 newctxt->incNr = ctxt->incNr;
684 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
685 sizeof(newctxt->incTab[0]));
686 if (newctxt->incTab == NULL) {
687 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
688 xmlFree(newctxt);
689 return;
690 }
691 /*
692 * copy the urlTab
693 */
694 newctxt->urlMax = ctxt->urlMax;
695 newctxt->urlNr = ctxt->urlNr;
696 newctxt->urlTab = ctxt->urlTab;
697
698 /*
699 * Inherit the existing base
700 */
701 newctxt->base = xmlStrdup(ctxt->base);
702
703 /*
704 * Inherit the documents already in use by other includes
705 */
706 newctxt->incBase = ctxt->incNr;
707 for (i = 0;i < ctxt->incNr;i++) {
708 newctxt->incTab[i] = ctxt->incTab[i];
709 newctxt->incTab[i]->count++; /* prevent the recursion from
710 freeing it */
711 }
712 /*
713 * The new context should also inherit the Parse Flags
714 * (bug 132597)
715 */
716 newctxt->parseFlags = ctxt->parseFlags;
717 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
718 for (i = 0;i < ctxt->incNr;i++) {
719 newctxt->incTab[i]->count--;
720 newctxt->incTab[i] = NULL;
721 }
722
723 /* urlTab may have been reallocated */
724 ctxt->urlTab = newctxt->urlTab;
725 ctxt->urlMax = newctxt->urlMax;
726
727 newctxt->urlMax = 0;
728 newctxt->urlNr = 0;
729 newctxt->urlTab = NULL;
730
731 xmlXIncludeFreeContext(newctxt);
732 }
733#ifdef DEBUG_XINCLUDE
734 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
735#endif
736}
737
738/**
739 * xmlXIncludeAddTxt:
740 * @ctxt: the XInclude context
741 * @txt: the new text node
742 * @url: the associated URL
743 *
744 * Add a new txtument to the list
745 */
746static void
747xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
748#ifdef DEBUG_XINCLUDE
749 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
750#endif
751 if (ctxt->txtMax == 0) {
752 ctxt->txtMax = 4;
753 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
754 sizeof(ctxt->txtTab[0]));
755 if (ctxt->txtTab == NULL) {
756 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
757 return;
758 }
759 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
760 sizeof(ctxt->txturlTab[0]));
761 if (ctxt->txturlTab == NULL) {
762 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
763 return;
764 }
765 }
766 if (ctxt->txtNr >= ctxt->txtMax) {
767 ctxt->txtMax *= 2;
768 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
769 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
770 if (ctxt->txtTab == NULL) {
771 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
772 return;
773 }
774 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
775 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
776 if (ctxt->txturlTab == NULL) {
777 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
778 return;
779 }
780 }
781 ctxt->txtTab[ctxt->txtNr] = txt;
782 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
783 ctxt->txtNr++;
784}
785
786/************************************************************************
787 * *
788 * Node copy with specific semantic *
789 * *
790 ************************************************************************/
791
792/**
793 * xmlXIncludeCopyNode:
794 * @ctxt: the XInclude context
795 * @target: the document target
796 * @source: the document source
797 * @elem: the element
798 *
799 * Make a copy of the node while preserving the XInclude semantic
800 * of the Infoset copy
801 */
802static xmlNodePtr
803xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
804 xmlDocPtr source, xmlNodePtr elem) {
805 xmlNodePtr result = NULL;
806
807 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
808 (elem == NULL))
809 return(NULL);
810 if (elem->type == XML_DTD_NODE)
811 return(NULL);
812 result = xmlDocCopyNode(elem, target, 1);
813 return(result);
814}
815
816/**
817 * xmlXIncludeCopyNodeList:
818 * @ctxt: the XInclude context
819 * @target: the document target
820 * @source: the document source
821 * @elem: the element list
822 *
823 * Make a copy of the node list while preserving the XInclude semantic
824 * of the Infoset copy
825 */
826static xmlNodePtr
827xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
828 xmlDocPtr source, xmlNodePtr elem) {
829 xmlNodePtr cur, res, result = NULL, last = NULL;
830
831 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
832 (elem == NULL))
833 return(NULL);
834 cur = elem;
835 while (cur != NULL) {
836 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
837 if (res != NULL) {
838 if (result == NULL) {
839 result = last = res;
840 } else {
841 last->next = res;
842 res->prev = last;
843 last = res;
844 }
845 }
846 cur = cur->next;
847 }
848 return(result);
849}
850
851/**
852 * xmlXIncludeGetNthChild:
853 * @cur: the node
854 * @no: the child number
855 *
856 * Returns the @n'th element child of @cur or NULL
857 */
858static xmlNodePtr
859xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
860 int i;
861 if (cur == NULL)
862 return(cur);
863 cur = cur->children;
864 for (i = 0;i <= no;cur = cur->next) {
865 if (cur == NULL)
866 return(cur);
867 if ((cur->type == XML_ELEMENT_NODE) ||
868 (cur->type == XML_DOCUMENT_NODE) ||
869 (cur->type == XML_HTML_DOCUMENT_NODE)) {
870 i++;
871 if (i == no)
872 break;
873 }
874 }
875 return(cur);
876}
877
878xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
879/**
880 * xmlXIncludeCopyRange:
881 * @ctxt: the XInclude context
882 * @target: the document target
883 * @source: the document source
884 * @obj: the XPointer result from the evaluation.
885 *
886 * Build a node list tree copy of the XPointer result.
887 *
888 * Returns an xmlNodePtr list or NULL.
889 * The caller has to free the node tree.
890 */
891static xmlNodePtr
892xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
893 xmlDocPtr source, xmlXPathObjectPtr range) {
894 /* pointers to generated nodes */
895 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
896 xmlNodePtr tmp, tmp2;
897 /* pointers to traversal nodes */
898 xmlNodePtr start, cur, end;
899 int index1, index2;
900 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
901
902 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
903 (range == NULL))
904 return(NULL);
905 if (range->type != XPATH_RANGE)
906 return(NULL);
907 start = (xmlNodePtr) range->user;
908
909 if (start == NULL)
910 return(NULL);
911 end = range->user2;
912 if (end == NULL)
913 return(xmlDocCopyNode(start, target, 1));
914
915 cur = start;
916 index1 = range->index;
917 index2 = range->index2;
918 /*
919 * level is depth of the current node under consideration
920 * list is the pointer to the root of the output tree
921 * listParent is a pointer to the parent of output tree (within
922 the included file) in case we need to add another level
923 * last is a pointer to the last node added to the output tree
924 * lastLevel is the depth of last (relative to the root)
925 */
926 while (cur != NULL) {
927 /*
928 * Check if our output tree needs a parent
929 */
930 if (level < 0) {
931 while (level < 0) {
932 /* copy must include namespaces and properties */
933 tmp2 = xmlDocCopyNode(listParent, target, 2);
934 xmlAddChild(tmp2, list);
935 list = tmp2;
936 listParent = listParent->parent;
937 level++;
938 }
939 last = list;
940 lastLevel = 0;
941 }
942 /*
943 * Check whether we need to change our insertion point
944 */
945 while (level < lastLevel) {
946 last = last->parent;
947 lastLevel --;
948 }
949 if (cur == end) { /* Are we at the end of the range? */
950 if (cur->type == XML_TEXT_NODE) {
951 const xmlChar *content = cur->content;
952 int len;
953
954 if (content == NULL) {
955 tmp = xmlNewTextLen(NULL, 0);
956 } else {
957 len = index2;
958 if ((cur == start) && (index1 > 1)) {
959 content += (index1 - 1);
960 len -= (index1 - 1);
961 index1 = 0;
962 } else {
963 len = index2;
964 }
965 tmp = xmlNewTextLen(content, len);
966 }
967 /* single sub text node selection */
968 if (list == NULL)
969 return(tmp);
970 /* prune and return full set */
971 if (level == lastLevel)
972 xmlAddNextSibling(last, tmp);
973 else
974 xmlAddChild(last, tmp);
975 return(list);
976 } else { /* ending node not a text node */
977 endLevel = level; /* remember the level of the end node */
978 endFlag = 1;
979 /* last node - need to take care of properties + namespaces */
980 tmp = xmlDocCopyNode(cur, target, 2);
981 if (list == NULL) {
982 list = tmp;
983 listParent = cur->parent;
984 } else {
985 if (level == lastLevel)
986 xmlAddNextSibling(last, tmp);
987 else {
988 xmlAddChild(last, tmp);
989 lastLevel = level;
990 }
991 }
992 last = tmp;
993
994 if (index2 > 1) {
995 end = xmlXIncludeGetNthChild(cur, index2 - 1);
996 index2 = 0;
997 }
998 if ((cur == start) && (index1 > 1)) {
999 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1000 index1 = 0;
1001 } else {
1002 cur = cur->children;
1003 }
1004 level++; /* increment level to show change */
1005 /*
1006 * Now gather the remaining nodes from cur to end
1007 */
1008 continue; /* while */
1009 }
1010 } else if (cur == start) { /* Not at the end, are we at start? */
1011 if ((cur->type == XML_TEXT_NODE) ||
1012 (cur->type == XML_CDATA_SECTION_NODE)) {
1013 const xmlChar *content = cur->content;
1014
1015 if (content == NULL) {
1016 tmp = xmlNewTextLen(NULL, 0);
1017 } else {
1018 if (index1 > 1) {
1019 content += (index1 - 1);
1020 index1 = 0;
1021 }
1022 tmp = xmlNewText(content);
1023 }
1024 last = list = tmp;
1025 listParent = cur->parent;
1026 } else { /* Not text node */
1027 /*
1028 * start of the range - need to take care of
1029 * properties and namespaces
1030 */
1031 tmp = xmlDocCopyNode(cur, target, 2);
1032 list = last = tmp;
1033 listParent = cur->parent;
1034 if (index1 > 1) { /* Do we need to position? */
1035 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1036 level = lastLevel = 1;
1037 index1 = 0;
1038 /*
1039 * Now gather the remaining nodes from cur to end
1040 */
1041 continue; /* while */
1042 }
1043 }
1044 } else {
1045 tmp = NULL;
1046 switch (cur->type) {
1047 case XML_DTD_NODE:
1048 case XML_ELEMENT_DECL:
1049 case XML_ATTRIBUTE_DECL:
1050 case XML_ENTITY_NODE:
1051 /* Do not copy DTD informations */
1052 break;
1053 case XML_ENTITY_DECL:
1054 /* handle crossing entities -> stack needed */
1055 break;
1056 case XML_XINCLUDE_START:
1057 case XML_XINCLUDE_END:
1058 /* don't consider it part of the tree content */
1059 break;
1060 case XML_ATTRIBUTE_NODE:
1061 /* Humm, should not happen ! */
1062 break;
1063 default:
1064 /*
1065 * Middle of the range - need to take care of
1066 * properties and namespaces
1067 */
1068 tmp = xmlDocCopyNode(cur, target, 2);
1069 break;
1070 }
1071 if (tmp != NULL) {
1072 if (level == lastLevel)
1073 xmlAddNextSibling(last, tmp);
1074 else {
1075 xmlAddChild(last, tmp);
1076 lastLevel = level;
1077 }
1078 last = tmp;
1079 }
1080 }
1081 /*
1082 * Skip to next node in document order
1083 */
1084 cur = xmlXPtrAdvanceNode(cur, &level);
1085 if (endFlag && (level >= endLevel))
1086 break;
1087 }
1088 return(list);
1089}
1090
1091/**
1092 * xmlXIncludeBuildNodeList:
1093 * @ctxt: the XInclude context
1094 * @target: the document target
1095 * @source: the document source
1096 * @obj: the XPointer result from the evaluation.
1097 *
1098 * Build a node list tree copy of the XPointer result.
1099 * This will drop Attributes and Namespace declarations.
1100 *
1101 * Returns an xmlNodePtr list or NULL.
1102 * the caller has to free the node tree.
1103 */
1104static xmlNodePtr
1105xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1106 xmlDocPtr source, xmlXPathObjectPtr obj) {
1107 xmlNodePtr list = NULL, last = NULL;
1108 int i;
1109
1110 if (source == NULL)
1111 source = ctxt->doc;
1112 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1113 (obj == NULL))
1114 return(NULL);
1115 switch (obj->type) {
1116 case XPATH_NODESET: {
1117 xmlNodeSetPtr set = obj->nodesetval;
1118 if (set == NULL)
1119 return(NULL);
1120 for (i = 0;i < set->nodeNr;i++) {
1121 if (set->nodeTab[i] == NULL)
1122 continue;
1123 switch (set->nodeTab[i]->type) {
1124 case XML_TEXT_NODE:
1125 case XML_CDATA_SECTION_NODE:
1126 case XML_ELEMENT_NODE:
1127 case XML_ENTITY_REF_NODE:
1128 case XML_ENTITY_NODE:
1129 case XML_PI_NODE:
1130 case XML_COMMENT_NODE:
1131 case XML_DOCUMENT_NODE:
1132 case XML_HTML_DOCUMENT_NODE:
1133#ifdef LIBXML_DOCB_ENABLED
1134 case XML_DOCB_DOCUMENT_NODE:
1135#endif
1136 case XML_XINCLUDE_END:
1137 break;
1138 case XML_XINCLUDE_START: {
1139 xmlNodePtr tmp, cur = set->nodeTab[i];
1140
1141 cur = cur->next;
1142 while (cur != NULL) {
1143 switch(cur->type) {
1144 case XML_TEXT_NODE:
1145 case XML_CDATA_SECTION_NODE:
1146 case XML_ELEMENT_NODE:
1147 case XML_ENTITY_REF_NODE:
1148 case XML_ENTITY_NODE:
1149 case XML_PI_NODE:
1150 case XML_COMMENT_NODE:
1151 tmp = xmlXIncludeCopyNode(ctxt, target,
1152 source, cur);
1153 if (last == NULL) {
1154 list = last = tmp;
1155 } else {
1156 xmlAddNextSibling(last, tmp);
1157 last = tmp;
1158 }
1159 cur = cur->next;
1160 continue;
1161 default:
1162 break;
1163 }
1164 break;
1165 }
1166 continue;
1167 }
1168 case XML_ATTRIBUTE_NODE:
1169 case XML_NAMESPACE_DECL:
1170 case XML_DOCUMENT_TYPE_NODE:
1171 case XML_DOCUMENT_FRAG_NODE:
1172 case XML_NOTATION_NODE:
1173 case XML_DTD_NODE:
1174 case XML_ELEMENT_DECL:
1175 case XML_ATTRIBUTE_DECL:
1176 case XML_ENTITY_DECL:
1177 continue; /* for */
1178 }
1179 if (last == NULL)
1180 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1181 set->nodeTab[i]);
1182 else {
1183 xmlAddNextSibling(last,
1184 xmlXIncludeCopyNode(ctxt, target, source,
1185 set->nodeTab[i]));
1186 if (last->next != NULL)
1187 last = last->next;
1188 }
1189 }
1190 break;
1191 }
1192 case XPATH_LOCATIONSET: {
1193 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1194 if (set == NULL)
1195 return(NULL);
1196 for (i = 0;i < set->locNr;i++) {
1197 if (last == NULL)
1198 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1199 set->locTab[i]);
1200 else
1201 xmlAddNextSibling(last,
1202 xmlXIncludeCopyXPointer(ctxt, target, source,
1203 set->locTab[i]));
1204 if (last != NULL) {
1205 while (last->next != NULL)
1206 last = last->next;
1207 }
1208 }
1209 break;
1210 }
1211#ifdef LIBXML_XPTR_ENABLED
1212 case XPATH_RANGE:
1213 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
1214#endif
1215 case XPATH_POINT:
1216 /* points are ignored in XInclude */
1217 break;
1218 default:
1219 break;
1220 }
1221 return(list);
1222}
1223/************************************************************************
1224 * *
1225 * XInclude I/O handling *
1226 * *
1227 ************************************************************************/
1228
1229typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1230typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1231struct _xmlXIncludeMergeData {
1232 xmlDocPtr doc;
1233 xmlXIncludeCtxtPtr ctxt;
1234};
1235
1236/**
1237 * xmlXIncludeMergeOneEntity:
1238 * @ent: the entity
1239 * @doc: the including doc
1240 * @nr: the entity name
1241 *
1242 * Inplements the merge of one entity
1243 */
1244static void
1245xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
1246 xmlChar *name ATTRIBUTE_UNUSED) {
1247 xmlEntityPtr ret, prev;
1248 xmlDocPtr doc;
1249 xmlXIncludeCtxtPtr ctxt;
1250
1251 if ((ent == NULL) || (data == NULL))
1252 return;
1253 ctxt = data->ctxt;
1254 doc = data->doc;
1255 if ((ctxt == NULL) || (doc == NULL))
1256 return;
1257 switch (ent->etype) {
1258 case XML_INTERNAL_PARAMETER_ENTITY:
1259 case XML_EXTERNAL_PARAMETER_ENTITY:
1260 case XML_INTERNAL_PREDEFINED_ENTITY:
1261 return;
1262 case XML_INTERNAL_GENERAL_ENTITY:
1263 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1264 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1265 break;
1266 }
1267 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1268 ent->SystemID, ent->content);
1269 if (ret != NULL) {
1270 if (ent->URI != NULL)
1271 ret->URI = xmlStrdup(ent->URI);
1272 } else {
1273 prev = xmlGetDocEntity(doc, ent->name);
1274 if (prev != NULL) {
1275 if (ent->etype != prev->etype)
1276 goto error;
1277
1278 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1279 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1280 goto error;
1281 } else if ((ent->ExternalID != NULL) &&
1282 (prev->ExternalID != NULL)) {
1283 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1284 goto error;
1285 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1286 if (!xmlStrEqual(ent->content, prev->content))
1287 goto error;
1288 } else {
1289 goto error;
1290 }
1291
1292 }
1293 }
1294 return;
1295error:
1296 switch (ent->etype) {
1297 case XML_INTERNAL_PARAMETER_ENTITY:
1298 case XML_EXTERNAL_PARAMETER_ENTITY:
1299 case XML_INTERNAL_PREDEFINED_ENTITY:
1300 case XML_INTERNAL_GENERAL_ENTITY:
1301 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1302 return;
1303 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1304 break;
1305 }
1306 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1307 "mismatch in redefinition of entity %s\n",
1308 ent->name);
1309}
1310
1311/**
1312 * xmlXIncludeMergeEntities:
1313 * @ctxt: an XInclude context
1314 * @doc: the including doc
1315 * @from: the included doc
1316 *
1317 * Inplements the entity merge
1318 *
1319 * Returns 0 if merge succeeded, -1 if some processing failed
1320 */
1321static int
1322xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1323 xmlDocPtr from) {
1324 xmlNodePtr cur;
1325 xmlDtdPtr target, source;
1326
1327 if (ctxt == NULL)
1328 return(-1);
1329
1330 if ((from == NULL) || (from->intSubset == NULL))
1331 return(0);
1332
1333 target = doc->intSubset;
1334 if (target == NULL) {
1335 cur = xmlDocGetRootElement(doc);
1336 if (cur == NULL)
1337 return(-1);
1338 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1339 if (target == NULL)
1340 return(-1);
1341 }
1342
1343 source = from->intSubset;
1344 if ((source != NULL) && (source->entities != NULL)) {
1345 xmlXIncludeMergeData data;
1346
1347 data.ctxt = ctxt;
1348 data.doc = doc;
1349
1350 xmlHashScan((xmlHashTablePtr) source->entities,
1351 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
1352 }
1353 source = from->extSubset;
1354 if ((source != NULL) && (source->entities != NULL)) {
1355 xmlXIncludeMergeData data;
1356
1357 data.ctxt = ctxt;
1358 data.doc = doc;
1359
1360 /*
1361 * don't duplicate existing stuff when external subsets are the same
1362 */
1363 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1364 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1365 xmlHashScan((xmlHashTablePtr) source->entities,
1366 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
1367 }
1368 }
1369 return(0);
1370}
1371
1372/**
1373 * xmlXIncludeLoadDoc:
1374 * @ctxt: the XInclude context
1375 * @url: the associated URL
1376 * @nr: the xinclude node number
1377 *
1378 * Load the document, and store the result in the XInclude context
1379 *
1380 * Returns 0 in case of success, -1 in case of failure
1381 */
1382static int
1383xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1384 xmlDocPtr doc;
1385 xmlURIPtr uri;
1386 xmlChar *URL;
1387 xmlChar *fragment = NULL;
1388 int i = 0;
1389#ifdef LIBXML_XPTR_ENABLED
1390 int saveFlags;
1391#endif
1392
1393#ifdef DEBUG_XINCLUDE
1394 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1395#endif
1396 /*
1397 * Check the URL and remove any fragment identifier
1398 */
1399 uri = xmlParseURI((const char *)url);
1400 if (uri == NULL) {
1401 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1402 XML_XINCLUDE_HREF_URI,
1403 "invalid value URI %s\n", url);
1404 return(-1);
1405 }
1406 if (uri->fragment != NULL) {
1407 fragment = (xmlChar *) uri->fragment;
1408 uri->fragment = NULL;
1409 }
1410 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1411 (ctxt->incTab[nr]->fragment != NULL)) {
1412 if (fragment != NULL) xmlFree(fragment);
1413 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1414 }
1415 URL = xmlSaveUri(uri);
1416 xmlFreeURI(uri);
1417 if (URL == NULL) {
1418 if (ctxt->incTab != NULL)
1419 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1420 XML_XINCLUDE_HREF_URI,
1421 "invalid value URI %s\n", url);
1422 else
1423 xmlXIncludeErr(ctxt, NULL,
1424 XML_XINCLUDE_HREF_URI,
1425 "invalid value URI %s\n", url);
1426 if (fragment != NULL)
1427 xmlFree(fragment);
1428 return(-1);
1429 }
1430
1431 /*
1432 * Handling of references to the local document are done
1433 * directly through ctxt->doc.
1434 */
1435 if ((URL[0] == 0) || (URL[0] == '#') ||
1436 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
1437 doc = NULL;
1438 goto loaded;
1439 }
1440
1441 /*
1442 * Prevent reloading twice the document.
1443 */
1444 for (i = 0; i < ctxt->incNr; i++) {
1445 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1446 (ctxt->incTab[i]->doc != NULL)) {
1447 doc = ctxt->incTab[i]->doc;
1448#ifdef DEBUG_XINCLUDE
1449 printf("Already loaded %s\n", URL);
1450#endif
1451 goto loaded;
1452 }
1453 }
1454
1455 /*
1456 * Load it.
1457 */
1458#ifdef DEBUG_XINCLUDE
1459 printf("loading %s\n", URL);
1460#endif
1461#ifdef LIBXML_XPTR_ENABLED
1462 /*
1463 * If this is an XPointer evaluation, we want to assure that
1464 * all entities have been resolved prior to processing the
1465 * referenced document
1466 */
1467 saveFlags = ctxt->parseFlags;
1468 if (fragment != NULL) { /* if this is an XPointer eval */
1469 ctxt->parseFlags |= XML_PARSE_NOENT;
1470 }
1471#endif
1472
1473 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
1474#ifdef LIBXML_XPTR_ENABLED
1475 ctxt->parseFlags = saveFlags;
1476#endif
1477 if (doc == NULL) {
1478 xmlFree(URL);
1479 if (fragment != NULL)
1480 xmlFree(fragment);
1481 return(-1);
1482 }
1483 ctxt->incTab[nr]->doc = doc;
1484 /*
1485 * It's possible that the requested URL has been mapped to a
1486 * completely different location (e.g. through a catalog entry).
1487 * To check for this, we compare the URL with that of the doc
1488 * and change it if they disagree (bug 146988).
1489 */
1490 if (!xmlStrEqual(URL, doc->URL)) {
1491 xmlFree(URL);
1492 URL = xmlStrdup(doc->URL);
1493 }
1494 for (i = nr + 1; i < ctxt->incNr; i++) {
1495 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1496 ctxt->incTab[nr]->count++;
1497#ifdef DEBUG_XINCLUDE
1498 printf("Increasing %s count since reused\n", URL);
1499#endif
1500 break;
1501 }
1502 }
1503
1504 /*
1505 * Make sure we have all entities fixed up
1506 */
1507 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
1508
1509 /*
1510 * We don't need the DTD anymore, free up space
1511 if (doc->intSubset != NULL) {
1512 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1513 xmlFreeNode((xmlNodePtr) doc->intSubset);
1514 doc->intSubset = NULL;
1515 }
1516 if (doc->extSubset != NULL) {
1517 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1518 xmlFreeNode((xmlNodePtr) doc->extSubset);
1519 doc->extSubset = NULL;
1520 }
1521 */
1522 xmlXIncludeRecurseDoc(ctxt, doc, URL);
1523
1524loaded:
1525 if (fragment == NULL) {
1526 /*
1527 * Add the top children list as the replacement copy.
1528 */
1529 if (doc == NULL)
1530 {
1531 /* Hopefully a DTD declaration won't be copied from
1532 * the same document */
1533 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
1534 } else {
1535 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
1536 doc, doc->children);
1537 }
1538 }
1539#ifdef LIBXML_XPTR_ENABLED
1540 else {
1541 /*
1542 * Computes the XPointer expression and make a copy used
1543 * as the replacement copy.
1544 */
1545 xmlXPathObjectPtr xptr;
1546 xmlXPathContextPtr xptrctxt;
1547 xmlNodeSetPtr set;
1548
1549 if (doc == NULL) {
1550 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1551 NULL);
1552 } else {
1553 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1554 }
1555 if (xptrctxt == NULL) {
1556 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1557 XML_XINCLUDE_XPTR_FAILED,
1558 "could not create XPointer context\n", NULL);
1559 xmlFree(URL);
1560 xmlFree(fragment);
1561 return(-1);
1562 }
1563 xptr = xmlXPtrEval(fragment, xptrctxt);
1564 if (xptr == NULL) {
1565 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1566 XML_XINCLUDE_XPTR_FAILED,
1567 "XPointer evaluation failed: #%s\n",
1568 fragment);
1569 xmlXPathFreeContext(xptrctxt);
1570 xmlFree(URL);
1571 xmlFree(fragment);
1572 return(-1);
1573 }
1574 switch (xptr->type) {
1575 case XPATH_UNDEFINED:
1576 case XPATH_BOOLEAN:
1577 case XPATH_NUMBER:
1578 case XPATH_STRING:
1579 case XPATH_POINT:
1580 case XPATH_USERS:
1581 case XPATH_XSLT_TREE:
1582 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1583 XML_XINCLUDE_XPTR_RESULT,
1584 "XPointer is not a range: #%s\n",
1585 fragment);
1586 xmlXPathFreeContext(xptrctxt);
1587 xmlFree(URL);
1588 xmlFree(fragment);
1589 return(-1);
1590 case XPATH_NODESET:
1591 if ((xptr->nodesetval == NULL) ||
1592 (xptr->nodesetval->nodeNr <= 0)) {
1593 xmlXPathFreeContext(xptrctxt);
1594 xmlFree(URL);
1595 xmlFree(fragment);
1596 return(-1);
1597 }
1598
1599 case XPATH_RANGE:
1600 case XPATH_LOCATIONSET:
1601 break;
1602 }
1603 set = xptr->nodesetval;
1604 if (set != NULL) {
1605 for (i = 0;i < set->nodeNr;i++) {
1606 if (set->nodeTab[i] == NULL)
1607 continue;
1608 switch (set->nodeTab[i]->type) {
1609 case XML_ELEMENT_NODE:
1610 case XML_TEXT_NODE:
1611 case XML_CDATA_SECTION_NODE:
1612 case XML_ENTITY_REF_NODE:
1613 case XML_ENTITY_NODE:
1614 case XML_PI_NODE:
1615 case XML_COMMENT_NODE:
1616 case XML_DOCUMENT_NODE:
1617 case XML_HTML_DOCUMENT_NODE:
1618#ifdef LIBXML_DOCB_ENABLED
1619 case XML_DOCB_DOCUMENT_NODE:
1620#endif
1621 continue;
1622
1623 case XML_ATTRIBUTE_NODE:
1624 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1625 XML_XINCLUDE_XPTR_RESULT,
1626 "XPointer selects an attribute: #%s\n",
1627 fragment);
1628 set->nodeTab[i] = NULL;
1629 continue;
1630 case XML_NAMESPACE_DECL:
1631 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1632 XML_XINCLUDE_XPTR_RESULT,
1633 "XPointer selects a namespace: #%s\n",
1634 fragment);
1635 set->nodeTab[i] = NULL;
1636 continue;
1637 case XML_DOCUMENT_TYPE_NODE:
1638 case XML_DOCUMENT_FRAG_NODE:
1639 case XML_NOTATION_NODE:
1640 case XML_DTD_NODE:
1641 case XML_ELEMENT_DECL:
1642 case XML_ATTRIBUTE_DECL:
1643 case XML_ENTITY_DECL:
1644 case XML_XINCLUDE_START:
1645 case XML_XINCLUDE_END:
1646 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1647 XML_XINCLUDE_XPTR_RESULT,
1648 "XPointer selects unexpected nodes: #%s\n",
1649 fragment);
1650 set->nodeTab[i] = NULL;
1651 set->nodeTab[i] = NULL;
1652 continue; /* for */
1653 }
1654 }
1655 }
1656 if (doc == NULL) {
1657 ctxt->incTab[nr]->xptr = xptr;
1658 ctxt->incTab[nr]->inc = NULL;
1659 } else {
1660 ctxt->incTab[nr]->inc =
1661 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1662 xmlXPathFreeObject(xptr);
1663 }
1664 xmlXPathFreeContext(xptrctxt);
1665 xmlFree(fragment);
1666 }
1667#endif
1668
1669 /*
1670 * Do the xml:base fixup if needed
1671 */
1672 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1673 xmlNodePtr node;
1674 xmlChar *base;
1675 xmlChar *curBase;
1676
1677 /*
1678 * The base is only adjusted if "necessary", i.e. if the xinclude node
1679 * has a base specified, or the URL is relative
1680 */
1681 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1682 XML_XML_NAMESPACE);
1683 if (base == NULL) {
1684 /*
1685 * No xml:base on the xinclude node, so we check whether the
1686 * URI base is different than (relative to) the context base
1687 */
1688 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1689 if (curBase == NULL) { /* Error return */
1690 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1691 XML_XINCLUDE_HREF_URI,
1692 "trying to build relative URI from %s\n", URL);
1693 } else {
1694 /* If the URI doesn't contain a slash, it's not relative */
1695 if (!xmlStrchr(curBase, (xmlChar) '/'))
1696 xmlFree(curBase);
1697 else
1698 base = curBase;
1699 }
1700 }
1701 if (base != NULL) { /* Adjustment may be needed */
1702 node = ctxt->incTab[nr]->inc;
1703 while (node != NULL) {
1704 /* Only work on element nodes */
1705 if (node->type == XML_ELEMENT_NODE) {
1706 curBase = xmlNodeGetBase(node->doc, node);
1707 /* If no current base, set it */
1708 if (curBase == NULL) {
1709 xmlNodeSetBase(node, base);
1710 } else {
1711 /*
1712 * If the current base is the same as the
1713 * URL of the document, then reset it to be
1714 * the specified xml:base or the relative URI
1715 */
1716 if (xmlStrEqual(curBase, node->doc->URL)) {
1717 xmlNodeSetBase(node, base);
1718 } else {
1719 /*
1720 * If the element already has an xml:base
1721 * set, then relativise it if necessary
1722 */
1723 xmlChar *xmlBase;
1724 xmlBase = xmlGetNsProp(node,
1725 BAD_CAST "base",
1726 XML_XML_NAMESPACE);
1727 if (xmlBase != NULL) {
1728 xmlChar *relBase;
1729 relBase = xmlBuildURI(xmlBase, base);
1730 if (relBase == NULL) { /* error */
1731 xmlXIncludeErr(ctxt,
1732 ctxt->incTab[nr]->ref,
1733 XML_XINCLUDE_HREF_URI,
1734 "trying to rebuild base from %s\n",
1735 xmlBase);
1736 } else {
1737 xmlNodeSetBase(node, relBase);
1738 xmlFree(relBase);
1739 }
1740 xmlFree(xmlBase);
1741 }
1742 }
1743 xmlFree(curBase);
1744 }
1745 }
1746 node = node->next;
1747 }
1748 xmlFree(base);
1749 }
1750 }
1751 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1752 (ctxt->incTab[nr]->count <= 1)) {
1753#ifdef DEBUG_XINCLUDE
1754 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1755#endif
1756 xmlFreeDoc(ctxt->incTab[nr]->doc);
1757 ctxt->incTab[nr]->doc = NULL;
1758 }
1759 xmlFree(URL);
1760 return(0);
1761}
1762
1763/**
1764 * xmlXIncludeLoadTxt:
1765 * @ctxt: the XInclude context
1766 * @url: the associated URL
1767 * @nr: the xinclude node number
1768 *
1769 * Load the content, and store the result in the XInclude context
1770 *
1771 * Returns 0 in case of success, -1 in case of failure
1772 */
1773static int
1774xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1775 xmlParserInputBufferPtr buf;
1776 xmlNodePtr node;
1777 xmlURIPtr uri;
1778 xmlChar *URL;
1779 int i;
1780 xmlChar *encoding = NULL;
1781 xmlCharEncoding enc = (xmlCharEncoding) 0;
1782
1783 /*
1784 * Check the URL and remove any fragment identifier
1785 */
1786 uri = xmlParseURI((const char *)url);
1787 if (uri == NULL) {
1788 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1789 "invalid value URI %s\n", url);
1790 return(-1);
1791 }
1792 if (uri->fragment != NULL) {
1793 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1794 "fragment identifier forbidden for text: %s\n",
1795 (const xmlChar *) uri->fragment);
1796 xmlFreeURI(uri);
1797 return(-1);
1798 }
1799 URL = xmlSaveUri(uri);
1800 xmlFreeURI(uri);
1801 if (URL == NULL) {
1802 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1803 "invalid value URI %s\n", url);
1804 return(-1);
1805 }
1806
1807 /*
1808 * Handling of references to the local document are done
1809 * directly through ctxt->doc.
1810 */
1811 if (URL[0] == 0) {
1812 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1813 XML_XINCLUDE_TEXT_DOCUMENT,
1814 "text serialization of document not available\n", NULL);
1815 xmlFree(URL);
1816 return(-1);
1817 }
1818
1819 /*
1820 * Prevent reloading twice the document.
1821 */
1822 for (i = 0; i < ctxt->txtNr; i++) {
1823 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1824 node = xmlCopyNode(ctxt->txtTab[i], 1);
1825 goto loaded;
1826 }
1827 }
1828 /*
1829 * Try to get the encoding if available
1830 */
1831 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1832 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1833 }
1834 if (encoding != NULL) {
1835 /*
1836 * TODO: we should not have to remap to the xmlCharEncoding
1837 * predefined set, a better interface than
1838 * xmlParserInputBufferCreateFilename should allow any
1839 * encoding supported by iconv
1840 */
1841 enc = xmlParseCharEncoding((const char *) encoding);
1842 if (enc == XML_CHAR_ENCODING_ERROR) {
1843 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1844 XML_XINCLUDE_UNKNOWN_ENCODING,
1845 "encoding %s not supported\n", encoding);
1846 xmlFree(encoding);
1847 xmlFree(URL);
1848 return(-1);
1849 }
1850 xmlFree(encoding);
1851 }
1852
1853 /*
1854 * Load it.
1855 */
1856 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
1857 if (buf == NULL) {
1858 xmlFree(URL);
1859 return(-1);
1860 }
1861 node = xmlNewText(NULL);
1862
1863 /*
1864 * Scan all chars from the resource and add the to the node
1865 */
1866 while (xmlParserInputBufferRead(buf, 128) > 0) {
1867 int len;
1868 const xmlChar *content;
1869
1870 content = xmlBufferContent(buf->buffer);
1871 len = xmlBufferLength(buf->buffer);
1872 for (i = 0;i < len;) {
1873 int cur;
1874 int l;
1875
1876 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1877 if (!IS_CHAR(cur)) {
1878 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1879 XML_XINCLUDE_INVALID_CHAR,
1880 "%s contains invalid char\n", URL);
1881 xmlFreeParserInputBuffer(buf);
1882 xmlFree(URL);
1883 return(-1);
1884 } else {
1885 xmlNodeAddContentLen(node, &content[i], l);
1886 }
1887 i += l;
1888 }
1889 xmlBufferShrink(buf->buffer, len);
1890 }
1891 xmlFreeParserInputBuffer(buf);
1892 xmlXIncludeAddTxt(ctxt, node, URL);
1893
1894loaded:
1895 /*
1896 * Add the element as the replacement copy.
1897 */
1898 ctxt->incTab[nr]->inc = node;
1899 xmlFree(URL);
1900 return(0);
1901}
1902
1903/**
1904 * xmlXIncludeLoadFallback:
1905 * @ctxt: the XInclude context
1906 * @fallback: the fallback node
1907 * @nr: the xinclude node number
1908 *
1909 * Load the content of the fallback node, and store the result
1910 * in the XInclude context
1911 *
1912 * Returns 0 in case of success, -1 in case of failure
1913 */
1914static int
1915xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
1916 xmlXIncludeCtxtPtr newctxt;
1917 int ret = 0;
1918
1919 if ((fallback == NULL) || (ctxt == NULL))
1920 return(-1);
1921 if (fallback->children != NULL) {
1922 /*
1923 * It's possible that the fallback also has 'includes'
1924 * (Bug 129969), so we re-process the fallback just in case
1925 */
1926 newctxt = xmlXIncludeNewContext(ctxt->doc);
1927 if (newctxt == NULL)
1928 return (-1);
1929 newctxt->_private = ctxt->_private;
1930 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
1931 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1932 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
1933 if (ctxt->nbErrors > 0)
1934 ret = -1;
1935 else if (ret > 0)
1936 ret = 0; /* xmlXIncludeDoProcess can return +ve number */
1937 xmlXIncludeFreeContext(newctxt);
1938
1939 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1940 fallback->children);
1941 } else {
1942 ctxt->incTab[nr]->inc = NULL;
1943 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
1944 }
1945 return(ret);
1946}
1947
1948/************************************************************************
1949 * *
1950 * XInclude Processing *
1951 * *
1952 ************************************************************************/
1953
1954/**
1955 * xmlXIncludePreProcessNode:
1956 * @ctxt: an XInclude context
1957 * @node: an XInclude node
1958 *
1959 * Implement the XInclude preprocessing, currently just adding the element
1960 * for further processing.
1961 *
1962 * Returns the result list or NULL in case of error
1963 */
1964static xmlNodePtr
1965xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1966 xmlXIncludeAddNode(ctxt, node);
1967 return(NULL);
1968}
1969
1970/**
1971 * xmlXIncludeLoadNode:
1972 * @ctxt: an XInclude context
1973 * @nr: the node number
1974 *
1975 * Find and load the infoset replacement for the given node.
1976 *
1977 * Returns 0 if substitution succeeded, -1 if some processing failed
1978 */
1979static int
1980xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1981 xmlNodePtr cur;
1982 xmlChar *href;
1983 xmlChar *parse;
1984 xmlChar *base;
1985 xmlChar *oldBase;
1986 xmlChar *URI;
1987 int xml = 1; /* default Issue 64 */
1988 int ret;
1989
1990 if (ctxt == NULL)
1991 return(-1);
1992 if ((nr < 0) || (nr >= ctxt->incNr))
1993 return(-1);
1994 cur = ctxt->incTab[nr]->ref;
1995 if (cur == NULL)
1996 return(-1);
1997
1998 /*
1999 * read the attributes
2000 */
2001 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
2002 if (href == NULL) {
2003 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
2004 if (href == NULL)
2005 return(-1);
2006 }
2007 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
2008 if (parse != NULL) {
2009 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
2010 xml = 1;
2011 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2012 xml = 0;
2013 else {
2014 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2015 XML_XINCLUDE_PARSE_VALUE,
2016 "invalid value %s for 'parse'\n", parse);
2017 if (href != NULL)
2018 xmlFree(href);
2019 if (parse != NULL)
2020 xmlFree(parse);
2021 return(-1);
2022 }
2023 }
2024
2025 /*
2026 * compute the URI
2027 */
2028 base = xmlNodeGetBase(ctxt->doc, cur);
2029 if (base == NULL) {
2030 URI = xmlBuildURI(href, ctxt->doc->URL);
2031 } else {
2032 URI = xmlBuildURI(href, base);
2033 }
2034 if (URI == NULL) {
2035 xmlChar *escbase;
2036 xmlChar *eschref;
2037 /*
2038 * Some escaping may be needed
2039 */
2040 escbase = xmlURIEscape(base);
2041 eschref = xmlURIEscape(href);
2042 URI = xmlBuildURI(eschref, escbase);
2043 if (escbase != NULL)
2044 xmlFree(escbase);
2045 if (eschref != NULL)
2046 xmlFree(eschref);
2047 }
2048 if (URI == NULL) {
2049 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2050 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
2051 if (parse != NULL)
2052 xmlFree(parse);
2053 if (href != NULL)
2054 xmlFree(href);
2055 if (base != NULL)
2056 xmlFree(base);
2057 return(-1);
2058 }
2059#ifdef DEBUG_XINCLUDE
2060 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2061 xml ? "xml": "text");
2062 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2063#endif
2064
2065 /*
2066 * Save the base for this include (saving the current one)
2067 */
2068 oldBase = ctxt->base;
2069 ctxt->base = base;
2070
2071 if (xml) {
2072 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
2073 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2074 } else {
2075 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2076 }
2077
2078 /*
2079 * Restore the original base before checking for fallback
2080 */
2081 ctxt->base = oldBase;
2082
2083 if (ret < 0) {
2084 xmlNodePtr children;
2085
2086 /*
2087 * Time to try a fallback if availble
2088 */
2089#ifdef DEBUG_XINCLUDE
2090 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2091#endif
2092 children = cur->children;
2093 while (children != NULL) {
2094 if ((children->type == XML_ELEMENT_NODE) &&
2095 (children->ns != NULL) &&
2096 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
2097 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2098 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
2099 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
2100 if (ret == 0)
2101 break;
2102 }
2103 children = children->next;
2104 }
2105 }
2106 if (ret < 0) {
2107 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2108 XML_XINCLUDE_NO_FALLBACK,
2109 "could not load %s, and no fallback was found\n",
2110 URI);
2111 }
2112
2113 /*
2114 * Cleanup
2115 */
2116 if (URI != NULL)
2117 xmlFree(URI);
2118 if (parse != NULL)
2119 xmlFree(parse);
2120 if (href != NULL)
2121 xmlFree(href);
2122 if (base != NULL)
2123 xmlFree(base);
2124 return(0);
2125}
2126
2127/**
2128 * xmlXIncludeIncludeNode:
2129 * @ctxt: an XInclude context
2130 * @nr: the node number
2131 *
2132 * Inplement the infoset replacement for the given node
2133 *
2134 * Returns 0 if substitution succeeded, -1 if some processing failed
2135 */
2136static int
2137xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
2138 xmlNodePtr cur, end, list, tmp;
2139
2140 if (ctxt == NULL)
2141 return(-1);
2142 if ((nr < 0) || (nr >= ctxt->incNr))
2143 return(-1);
2144 cur = ctxt->incTab[nr]->ref;
2145 if (cur == NULL)
2146 return(-1);
2147
2148 /*
2149 * If we stored an XPointer a late computation may be needed
2150 */
2151 if ((ctxt->incTab[nr]->inc == NULL) &&
2152 (ctxt->incTab[nr]->xptr != NULL)) {
2153 ctxt->incTab[nr]->inc =
2154 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
2155 ctxt->incTab[nr]->xptr);
2156 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2157 ctxt->incTab[nr]->xptr = NULL;
2158 }
2159 list = ctxt->incTab[nr]->inc;
2160 ctxt->incTab[nr]->inc = NULL;
2161
2162 /*
2163 * Check against the risk of generating a multi-rooted document
2164 */
2165 if ((cur->parent != NULL) &&
2166 (cur->parent->type != XML_ELEMENT_NODE)) {
2167 int nb_elem = 0;
2168
2169 tmp = list;
2170 while (tmp != NULL) {
2171 if (tmp->type == XML_ELEMENT_NODE)
2172 nb_elem++;
2173 tmp = tmp->next;
2174 }
2175 if (nb_elem > 1) {
2176 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2177 XML_XINCLUDE_MULTIPLE_ROOT,
2178 "XInclude error: would result in multiple root nodes\n",
2179 NULL);
2180 return(-1);
2181 }
2182 }
2183
2184 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2185 /*
2186 * Add the list of nodes
2187 */
2188 while (list != NULL) {
2189 end = list;
2190 list = list->next;
2191
2192 xmlAddPrevSibling(cur, end);
2193 }
2194 xmlUnlinkNode(cur);
2195 xmlFreeNode(cur);
2196 } else {
2197 /*
2198 * Change the current node as an XInclude start one, and add an
2199 * XInclude end one
2200 */
2201 cur->type = XML_XINCLUDE_START;
2202 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
2203 if (end == NULL) {
2204 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2205 XML_XINCLUDE_BUILD_FAILED,
2206 "failed to build node\n", NULL);
2207 return(-1);
2208 }
2209 end->type = XML_XINCLUDE_END;
2210 xmlAddNextSibling(cur, end);
2211
2212 /*
2213 * Add the list of nodes
2214 */
2215 while (list != NULL) {
2216 cur = list;
2217 list = list->next;
2218
2219 xmlAddPrevSibling(end, cur);
2220 }
2221 }
2222
2223
2224 return(0);
2225}
2226
2227/**
2228 * xmlXIncludeTestNode:
2229 * @ctxt: the XInclude processing context
2230 * @node: an XInclude node
2231 *
2232 * test if the node is an XInclude node
2233 *
2234 * Returns 1 true, 0 otherwise
2235 */
2236static int
2237xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2238 if (node == NULL)
2239 return(0);
2240 if (node->type != XML_ELEMENT_NODE)
2241 return(0);
2242 if (node->ns == NULL)
2243 return(0);
2244 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2245 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2246 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2247 if (ctxt->legacy == 0) {
2248#if 0 /* wait for the XML Core Working Group to get something stable ! */
2249 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2250 "Deprecated XInclude namespace found, use %s",
2251 XINCLUDE_NS);
2252#endif
2253 ctxt->legacy = 1;
2254 }
2255 }
2256 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2257 xmlNodePtr child = node->children;
2258 int nb_fallback = 0;
2259
2260 while (child != NULL) {
2261 if ((child->type == XML_ELEMENT_NODE) &&
2262 (child->ns != NULL) &&
2263 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2264 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
2265 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
2266 xmlXIncludeErr(ctxt, node,
2267 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2268 "%s has an 'include' child\n",
2269 XINCLUDE_NODE);
2270 return(0);
2271 }
2272 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2273 nb_fallback++;
2274 }
2275 }
2276 child = child->next;
2277 }
2278 if (nb_fallback > 1) {
2279 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2280 "%s has multiple fallback children\n",
2281 XINCLUDE_NODE);
2282 return(0);
2283 }
2284 return(1);
2285 }
2286 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2287 if ((node->parent == NULL) ||
2288 (node->parent->type != XML_ELEMENT_NODE) ||
2289 (node->parent->ns == NULL) ||
2290 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2291 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
2292 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
2293 xmlXIncludeErr(ctxt, node,
2294 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2295 "%s is not the child of an 'include'\n",
2296 XINCLUDE_FALLBACK);
2297 }
2298 }
2299 }
2300 return(0);
2301}
2302
2303/**
2304 * xmlXIncludeDoProcess:
2305 * @ctxt: the XInclude processing context
2306 * @doc: an XML document
2307 * @tree: the top of the tree to process
2308 *
2309 * Implement the XInclude substitution on the XML document @doc
2310 *
2311 * Returns 0 if no substitution were done, -1 if some processing failed
2312 * or the number of substitutions done.
2313 */
2314static int
2315xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
2316 xmlNodePtr cur;
2317 int ret = 0;
2318 int i, start;
2319
2320 if ((doc == NULL) || (tree == NULL))
2321 return(-1);
2322 if (ctxt == NULL)
2323 return(-1);
2324
2325 if (doc->URL != NULL) {
2326 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2327 if (ret < 0)
2328 return(-1);
2329 }
2330 start = ctxt->incNr;
2331
2332 /*
2333 * First phase: lookup the elements in the document
2334 */
2335 cur = tree;
2336 if (xmlXIncludeTestNode(ctxt, cur) == 1)
2337 xmlXIncludePreProcessNode(ctxt, cur);
2338 while ((cur != NULL) && (cur != tree->parent)) {
2339 /* TODO: need to work on entities -> stack */
2340 if ((cur->children != NULL) &&
2341 (cur->children->type != XML_ENTITY_DECL) &&
2342 (cur->children->type != XML_XINCLUDE_START) &&
2343 (cur->children->type != XML_XINCLUDE_END)) {
2344 cur = cur->children;
2345 if (xmlXIncludeTestNode(ctxt, cur))
2346 xmlXIncludePreProcessNode(ctxt, cur);
2347 } else if (cur->next != NULL) {
2348 cur = cur->next;
2349 if (xmlXIncludeTestNode(ctxt, cur))
2350 xmlXIncludePreProcessNode(ctxt, cur);
2351 } else {
2352 if (cur == tree)
2353 break;
2354 do {
2355 cur = cur->parent;
2356 if ((cur == NULL) || (cur == tree->parent))
2357 break; /* do */
2358 if (cur->next != NULL) {
2359 cur = cur->next;
2360 if (xmlXIncludeTestNode(ctxt, cur))
2361 xmlXIncludePreProcessNode(ctxt, cur);
2362 break; /* do */
2363 }
2364 } while (cur != NULL);
2365 }
2366 }
2367
2368 /*
2369 * Second Phase : collect the infosets fragments
2370 */
2371 for (i = start;i < ctxt->incNr; i++) {
2372 xmlXIncludeLoadNode(ctxt, i);
2373 ret++;
2374 }
2375
2376 /*
2377 * Third phase: extend the original document infoset.
2378 *
2379 * Originally we bypassed the inclusion if there were any errors
2380 * encountered on any of the XIncludes. A bug was raised (bug
2381 * 132588) requesting that we output the XIncludes without error,
2382 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2383 * give some other problems in the future, but for now it seems to
2384 * work ok.
2385 *
2386 */
2387 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
2388 if ((ctxt->incTab[i]->inc != NULL) ||
2389 (ctxt->incTab[i]->xptr != NULL) ||
2390 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
2391 xmlXIncludeIncludeNode(ctxt, i);
2392 }
2393
2394 if (doc->URL != NULL)
2395 xmlXIncludeURLPop(ctxt);
2396 return(ret);
2397}
2398
2399/**
2400 * xmlXIncludeSetFlags:
2401 * @ctxt: an XInclude processing context
2402 * @flags: a set of xmlParserOption used for parsing XML includes
2403 *
2404 * Set the flags used for further processing of XML resources.
2405 *
2406 * Returns 0 in case of success and -1 in case of error.
2407 */
2408int
2409xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2410 if (ctxt == NULL)
2411 return(-1);
2412 ctxt->parseFlags = flags;
2413 return(0);
2414}
2415
2416/**
2417 * xmlXIncludeProcessFlagsData:
2418 * @doc: an XML document
2419 * @flags: a set of xmlParserOption used for parsing XML includes
2420 * @data: application data that will be passed to the parser context
2421 * in the _private field of the parser context(s)
2422 *
2423 * Implement the XInclude substitution on the XML document @doc
2424 *
2425 * Returns 0 if no substitution were done, -1 if some processing failed
2426 * or the number of substitutions done.
2427 */
2428int
2429xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
2430 xmlXIncludeCtxtPtr ctxt;
2431 xmlNodePtr tree;
2432 int ret = 0;
2433
2434 if (doc == NULL)
2435 return(-1);
2436 tree = xmlDocGetRootElement(doc);
2437 if (tree == NULL)
2438 return(-1);
2439 ctxt = xmlXIncludeNewContext(doc);
2440 if (ctxt == NULL)
2441 return(-1);
2442 ctxt->_private = data;
2443 ctxt->base = xmlStrdup((xmlChar *)doc->URL);
2444 xmlXIncludeSetFlags(ctxt, flags);
2445 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2446 if ((ret >= 0) && (ctxt->nbErrors > 0))
2447 ret = -1;
2448
2449 xmlXIncludeFreeContext(ctxt);
2450 return(ret);
2451}
2452
2453/**
2454 * xmlXIncludeProcessFlags:
2455 * @doc: an XML document
2456 * @flags: a set of xmlParserOption used for parsing XML includes
2457 *
2458 * Implement the XInclude substitution on the XML document @doc
2459 *
2460 * Returns 0 if no substitution were done, -1 if some processing failed
2461 * or the number of substitutions done.
2462 */
2463int
2464xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2465 return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2466}
2467
2468/**
2469 * xmlXIncludeProcess:
2470 * @doc: an XML document
2471 *
2472 * Implement the XInclude substitution on the XML document @doc
2473 *
2474 * Returns 0 if no substitution were done, -1 if some processing failed
2475 * or the number of substitutions done.
2476 */
2477int
2478xmlXIncludeProcess(xmlDocPtr doc) {
2479 return(xmlXIncludeProcessFlags(doc, 0));
2480}
2481
2482/**
2483 * xmlXIncludeProcessTreeFlags:
2484 * @tree: a node in an XML document
2485 * @flags: a set of xmlParserOption used for parsing XML includes
2486 *
2487 * Implement the XInclude substitution for the given subtree
2488 *
2489 * Returns 0 if no substitution were done, -1 if some processing failed
2490 * or the number of substitutions done.
2491 */
2492int
2493xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2494 xmlXIncludeCtxtPtr ctxt;
2495 int ret = 0;
2496
2497 if ((tree == NULL) || (tree->doc == NULL))
2498 return(-1);
2499 ctxt = xmlXIncludeNewContext(tree->doc);
2500 if (ctxt == NULL)
2501 return(-1);
2502 ctxt->base = xmlNodeGetBase(tree->doc, tree);
2503 xmlXIncludeSetFlags(ctxt, flags);
2504 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2505 if ((ret >= 0) && (ctxt->nbErrors > 0))
2506 ret = -1;
2507
2508 xmlXIncludeFreeContext(ctxt);
2509 return(ret);
2510}
2511
2512/**
2513 * xmlXIncludeProcessTree:
2514 * @tree: a node in an XML document
2515 *
2516 * Implement the XInclude substitution for the given subtree
2517 *
2518 * Returns 0 if no substitution were done, -1 if some processing failed
2519 * or the number of substitutions done.
2520 */
2521int
2522xmlXIncludeProcessTree(xmlNodePtr tree) {
2523 return(xmlXIncludeProcessTreeFlags(tree, 0));
2524}
2525
2526/**
2527 * xmlXIncludeProcessNode:
2528 * @ctxt: an existing XInclude context
2529 * @node: a node in an XML document
2530 *
2531 * Implement the XInclude substitution for the given subtree reusing
2532 * the informations and data coming from the given context.
2533 *
2534 * Returns 0 if no substitution were done, -1 if some processing failed
2535 * or the number of substitutions done.
2536 */
2537int
2538xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2539 int ret = 0;
2540
2541 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2542 return(-1);
2543 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2544 if ((ret >= 0) && (ctxt->nbErrors > 0))
2545 ret = -1;
2546 return(ret);
2547}
2548
2549#else /* !LIBXML_XINCLUDE_ENABLED */
2550#endif
2551#define bottom_xinclude
2552#include "elfgcchack.h"
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette