msscript.ocx: Fake success in FreezeEvents.
[wine.git] / libs / xml2 / xinclude.c
blob09c1eef4bdd9e196a36a61b31209a849c6689425
1 /*
2 * xinclude.c : Code to implement XInclude processing
4 * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
5 * http://www.w3.org/TR/2003/WD-xinclude-20031110
7 * See Copyright for the status of this software.
9 * daniel@veillard.com
12 #define IN_LIBXML
13 #include "libxml.h"
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/xpath.h>
21 #include <libxml/xpointer.h>
22 #include <libxml/parserInternals.h>
23 #include <libxml/xmlerror.h>
24 #include <libxml/encoding.h>
25 #include <libxml/globals.h>
27 #ifdef LIBXML_XINCLUDE_ENABLED
28 #include <libxml/xinclude.h>
30 #include "private/buf.h"
31 #include "private/error.h"
32 #include "private/tree.h"
33 #include "private/xinclude.h"
35 #define XINCLUDE_MAX_DEPTH 40
37 /* #define DEBUG_XINCLUDE */
38 #ifdef DEBUG_XINCLUDE
39 #ifdef LIBXML_DEBUG_ENABLED
40 #include <libxml/debugXML.h>
41 #endif
42 #endif
44 /************************************************************************
45 * *
46 * XInclude context handling *
47 * *
48 ************************************************************************/
51 * An XInclude context
53 typedef xmlChar *xmlURL;
55 typedef struct _xmlXIncludeRef xmlXIncludeRef;
56 typedef xmlXIncludeRef *xmlXIncludeRefPtr;
57 struct _xmlXIncludeRef {
58 xmlChar *URI; /* the fully resolved resource URL */
59 xmlChar *fragment; /* the fragment in the URI */
60 xmlNodePtr elem; /* the xi:include element */
61 xmlNodePtr inc; /* the included copy */
62 int xml; /* xml or txt */
63 int fallback; /* fallback was loaded */
64 int emptyFb; /* flag to show fallback empty */
65 int expanding; /* flag to detect inclusion loops */
66 int replace; /* should the node be replaced? */
69 typedef struct _xmlXIncludeDoc xmlXIncludeDoc;
70 typedef xmlXIncludeDoc *xmlXIncludeDocPtr;
71 struct _xmlXIncludeDoc {
72 xmlDocPtr doc; /* the parsed document */
73 xmlChar *url; /* the URL */
74 int expanding; /* flag to detect inclusion loops */
77 typedef struct _xmlXIncludeTxt xmlXIncludeTxt;
78 typedef xmlXIncludeTxt *xmlXIncludeTxtPtr;
79 struct _xmlXIncludeTxt {
80 xmlChar *text; /* text string */
81 xmlChar *url; /* the URL */
84 struct _xmlXIncludeCtxt {
85 xmlDocPtr doc; /* the source document */
86 int incNr; /* number of includes */
87 int incMax; /* size of includes tab */
88 xmlXIncludeRefPtr *incTab; /* array of included references */
90 int txtNr; /* number of unparsed documents */
91 int txtMax; /* size of unparsed documents tab */
92 xmlXIncludeTxt *txtTab; /* array of unparsed documents */
94 int urlNr; /* number of documents stacked */
95 int urlMax; /* size of document stack */
96 xmlXIncludeDoc *urlTab; /* document stack */
98 int nbErrors; /* the number of errors detected */
99 int fatalErr; /* abort processing */
100 int legacy; /* using XINCLUDE_OLD_NS */
101 int parseFlags; /* the flags used for parsing XML documents */
102 xmlChar * base; /* the current xml:base */
104 void *_private; /* application data */
106 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
107 unsigned long incTotal; /* total number of processed inclusions */
108 #endif
109 int depth; /* recursion depth */
110 int isStream; /* streaming mode */
113 static xmlXIncludeRefPtr
114 xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node);
116 static int
117 xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref);
119 static int
120 xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlNodePtr tree);
123 /************************************************************************
125 * XInclude error handler *
127 ************************************************************************/
130 * xmlXIncludeErrMemory:
131 * @extra: extra information
133 * Handle an out of memory condition
135 static void
136 xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
137 const char *extra)
139 if (ctxt != NULL)
140 ctxt->nbErrors++;
141 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
142 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
143 extra, NULL, NULL, 0, 0,
144 "Memory allocation failed : %s\n", extra);
148 * xmlXIncludeErr:
149 * @ctxt: the XInclude context
150 * @node: the context node
151 * @msg: the error message
152 * @extra: extra information
154 * Handle an XInclude error
156 static void LIBXML_ATTR_FORMAT(4,0)
157 xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
158 const char *msg, const xmlChar *extra)
160 if (ctxt != NULL)
161 ctxt->nbErrors++;
162 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
163 error, XML_ERR_ERROR, NULL, 0,
164 (const char *) extra, NULL, NULL, 0, 0,
165 msg, (const char *) extra);
168 #if 0
170 * xmlXIncludeWarn:
171 * @ctxt: the XInclude context
172 * @node: the context node
173 * @msg: the error message
174 * @extra: extra information
176 * Emit an XInclude warning.
178 static void LIBXML_ATTR_FORMAT(4,0)
179 xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
180 const char *msg, const xmlChar *extra)
182 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
183 error, XML_ERR_WARNING, NULL, 0,
184 (const char *) extra, NULL, NULL, 0, 0,
185 msg, (const char *) extra);
187 #endif
190 * xmlXIncludeGetProp:
191 * @ctxt: the XInclude context
192 * @cur: the node
193 * @name: the attribute name
195 * Get an XInclude attribute
197 * Returns the value (to be freed) or NULL if not found
199 static xmlChar *
200 xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
201 const xmlChar *name) {
202 xmlChar *ret;
204 ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
205 if (ret != NULL)
206 return(ret);
207 if (ctxt->legacy != 0) {
208 ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
209 if (ret != NULL)
210 return(ret);
212 ret = xmlGetProp(cur, name);
213 return(ret);
216 * xmlXIncludeFreeRef:
217 * @ref: the XInclude reference
219 * Free an XInclude reference
221 static void
222 xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
223 if (ref == NULL)
224 return;
225 #ifdef DEBUG_XINCLUDE
226 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
227 #endif
228 if (ref->URI != NULL)
229 xmlFree(ref->URI);
230 if (ref->fragment != NULL)
231 xmlFree(ref->fragment);
232 xmlFree(ref);
236 * xmlXIncludeNewRef:
237 * @ctxt: the XInclude context
238 * @URI: the resource URI
239 * @elem: the xi:include element
241 * Creates a new reference within an XInclude context
243 * Returns the new set
245 static xmlXIncludeRefPtr
246 xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
247 xmlNodePtr elem) {
248 xmlXIncludeRefPtr ret;
250 #ifdef DEBUG_XINCLUDE
251 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
252 #endif
253 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
254 if (ret == NULL) {
255 xmlXIncludeErrMemory(ctxt, elem, "growing XInclude context");
256 return(NULL);
258 memset(ret, 0, sizeof(xmlXIncludeRef));
259 if (URI == NULL)
260 ret->URI = NULL;
261 else
262 ret->URI = xmlStrdup(URI);
263 ret->fragment = NULL;
264 ret->elem = elem;
265 ret->xml = 0;
266 ret->inc = NULL;
267 if (ctxt->incMax == 0) {
268 ctxt->incMax = 4;
269 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
270 sizeof(ctxt->incTab[0]));
271 if (ctxt->incTab == NULL) {
272 xmlXIncludeErrMemory(ctxt, elem, "growing XInclude context");
273 xmlXIncludeFreeRef(ret);
274 return(NULL);
277 if (ctxt->incNr >= ctxt->incMax) {
278 xmlXIncludeRefPtr *tmp;
279 size_t newSize = ctxt->incMax * 2;
281 tmp = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
282 newSize * sizeof(ctxt->incTab[0]));
283 if (tmp == NULL) {
284 xmlXIncludeErrMemory(ctxt, elem, "growing XInclude context");
285 xmlXIncludeFreeRef(ret);
286 return(NULL);
288 ctxt->incTab = tmp;
289 ctxt->incMax *= 2;
291 ctxt->incTab[ctxt->incNr++] = ret;
292 return(ret);
296 * xmlXIncludeNewContext:
297 * @doc: an XML Document
299 * Creates a new XInclude context
301 * Returns the new set
303 xmlXIncludeCtxtPtr
304 xmlXIncludeNewContext(xmlDocPtr doc) {
305 xmlXIncludeCtxtPtr ret;
307 #ifdef DEBUG_XINCLUDE
308 xmlGenericError(xmlGenericErrorContext, "New context\n");
309 #endif
310 if (doc == NULL)
311 return(NULL);
312 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
313 if (ret == NULL) {
314 xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
315 "creating XInclude context");
316 return(NULL);
318 memset(ret, 0, sizeof(xmlXIncludeCtxt));
319 ret->doc = doc;
320 ret->incNr = 0;
321 ret->incMax = 0;
322 ret->incTab = NULL;
323 ret->nbErrors = 0;
324 return(ret);
328 * xmlXIncludeFreeContext:
329 * @ctxt: the XInclude context
331 * Free an XInclude context
333 void
334 xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
335 int i;
337 #ifdef DEBUG_XINCLUDE
338 xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
339 #endif
340 if (ctxt == NULL)
341 return;
342 if (ctxt->urlTab != NULL) {
343 for (i = 0; i < ctxt->urlNr; i++) {
344 xmlFreeDoc(ctxt->urlTab[i].doc);
345 xmlFree(ctxt->urlTab[i].url);
347 xmlFree(ctxt->urlTab);
349 for (i = 0;i < ctxt->incNr;i++) {
350 if (ctxt->incTab[i] != NULL)
351 xmlXIncludeFreeRef(ctxt->incTab[i]);
353 if (ctxt->incTab != NULL)
354 xmlFree(ctxt->incTab);
355 if (ctxt->txtTab != NULL) {
356 for (i = 0;i < ctxt->txtNr;i++) {
357 xmlFree(ctxt->txtTab[i].text);
358 xmlFree(ctxt->txtTab[i].url);
360 xmlFree(ctxt->txtTab);
362 if (ctxt->base != NULL) {
363 xmlFree(ctxt->base);
365 xmlFree(ctxt);
369 * xmlXIncludeParseFile:
370 * @ctxt: the XInclude context
371 * @URL: the URL or file path
373 * parse a document for XInclude
375 static xmlDocPtr
376 xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
377 xmlDocPtr ret;
378 xmlParserCtxtPtr pctxt;
379 xmlParserInputPtr inputStream;
381 xmlInitParser();
383 pctxt = xmlNewParserCtxt();
384 if (pctxt == NULL) {
385 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
386 return(NULL);
390 * pass in the application data to the parser context.
392 pctxt->_private = ctxt->_private;
395 * try to ensure that new documents included are actually
396 * built with the same dictionary as the including document.
398 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
399 if (pctxt->dict != NULL)
400 xmlDictFree(pctxt->dict);
401 pctxt->dict = ctxt->doc->dict;
402 xmlDictReference(pctxt->dict);
405 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
407 /* Don't read from stdin. */
408 if ((URL != NULL) && (strcmp(URL, "-") == 0))
409 URL = "./-";
411 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
412 if (inputStream == NULL) {
413 xmlFreeParserCtxt(pctxt);
414 return(NULL);
417 inputPush(pctxt, inputStream);
419 if (pctxt->directory == NULL)
420 pctxt->directory = xmlParserGetDirectory(URL);
422 pctxt->loadsubset |= XML_DETECT_IDS;
424 xmlParseDocument(pctxt);
426 if (pctxt->wellFormed) {
427 ret = pctxt->myDoc;
429 else {
430 ret = NULL;
431 if (pctxt->myDoc != NULL)
432 xmlFreeDoc(pctxt->myDoc);
433 pctxt->myDoc = NULL;
435 xmlFreeParserCtxt(pctxt);
437 return(ret);
441 * xmlXIncludeAddNode:
442 * @ctxt: the XInclude context
443 * @cur: the new node
445 * Add a new node to process to an XInclude context
447 static xmlXIncludeRefPtr
448 xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
449 xmlXIncludeRefPtr ref;
450 xmlURIPtr uri;
451 xmlChar *URL;
452 xmlChar *fragment = NULL;
453 xmlChar *href;
454 xmlChar *parse;
455 xmlChar *base;
456 xmlChar *URI;
457 int xml = 1;
458 int local = 0;
461 if (ctxt == NULL)
462 return(NULL);
463 if (cur == NULL)
464 return(NULL);
466 #ifdef DEBUG_XINCLUDE
467 xmlGenericError(xmlGenericErrorContext, "Add node\n");
468 #endif
470 * read the attributes
472 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
473 if (href == NULL) {
474 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
475 if (href == NULL)
476 return(NULL);
478 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
479 if (parse != NULL) {
480 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
481 xml = 1;
482 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
483 xml = 0;
484 else {
485 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
486 "invalid value %s for 'parse'\n", parse);
487 if (href != NULL)
488 xmlFree(href);
489 if (parse != NULL)
490 xmlFree(parse);
491 return(NULL);
496 * compute the URI
498 base = xmlNodeGetBase(ctxt->doc, cur);
499 if (base == NULL) {
500 URI = xmlBuildURI(href, ctxt->doc->URL);
501 } else {
502 URI = xmlBuildURI(href, base);
504 if (URI == NULL) {
505 xmlChar *escbase;
506 xmlChar *eschref;
508 * Some escaping may be needed
510 escbase = xmlURIEscape(base);
511 eschref = xmlURIEscape(href);
512 URI = xmlBuildURI(eschref, escbase);
513 if (escbase != NULL)
514 xmlFree(escbase);
515 if (eschref != NULL)
516 xmlFree(eschref);
518 if (parse != NULL)
519 xmlFree(parse);
520 if (href != NULL)
521 xmlFree(href);
522 if (base != NULL)
523 xmlFree(base);
524 if (URI == NULL) {
525 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
526 "failed build URL\n", NULL);
527 return(NULL);
529 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
532 * Check the URL and remove any fragment identifier
534 uri = xmlParseURI((const char *)URI);
535 if (uri == NULL) {
536 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
537 "invalid value URI %s\n", URI);
538 if (fragment != NULL)
539 xmlFree(fragment);
540 xmlFree(URI);
541 return(NULL);
544 if (uri->fragment != NULL) {
545 if (ctxt->legacy != 0) {
546 if (fragment == NULL) {
547 fragment = (xmlChar *) uri->fragment;
548 } else {
549 xmlFree(uri->fragment);
551 } else {
552 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
553 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
554 URI);
555 if (fragment != NULL)
556 xmlFree(fragment);
557 xmlFreeURI(uri);
558 xmlFree(URI);
559 return(NULL);
561 uri->fragment = NULL;
563 URL = xmlSaveUri(uri);
564 xmlFreeURI(uri);
565 if (URL == NULL) {
566 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
567 "invalid value URI %s\n", URI);
568 if (fragment != NULL)
569 xmlFree(fragment);
570 xmlFree(URI);
571 return(NULL);
573 xmlFree(URI);
575 if (xmlStrEqual(URL, ctxt->doc->URL))
576 local = 1;
579 * If local and xml then we need a fragment
581 if ((local == 1) && (xml == 1) &&
582 ((fragment == NULL) || (fragment[0] == 0))) {
583 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
584 "detected a local recursion with no xpointer in %s\n",
585 URL);
586 xmlFree(URL);
587 xmlFree(fragment);
588 return(NULL);
591 ref = xmlXIncludeNewRef(ctxt, URL, cur);
592 xmlFree(URL);
593 if (ref == NULL) {
594 xmlFree(fragment);
595 return(NULL);
597 ref->fragment = fragment;
598 ref->xml = xml;
599 return(ref);
603 * xmlXIncludeRecurseDoc:
604 * @ctxt: the XInclude context
605 * @doc: the new document
606 * @url: the associated URL
608 * The XInclude recursive nature is handled at this point.
610 static void
611 xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
612 const xmlURL url ATTRIBUTE_UNUSED) {
613 xmlDocPtr oldDoc;
614 xmlXIncludeRefPtr *oldIncTab;
615 int oldIncMax, oldIncNr, oldIsStream;
616 int i;
618 oldDoc = ctxt->doc;
619 oldIncMax = ctxt->incMax;
620 oldIncNr = ctxt->incNr;
621 oldIncTab = ctxt->incTab;
622 oldIsStream = ctxt->isStream;
623 ctxt->doc = doc;
624 ctxt->incMax = 0;
625 ctxt->incNr = 0;
626 ctxt->incTab = NULL;
627 ctxt->isStream = 0;
629 xmlXIncludeDoProcess(ctxt, xmlDocGetRootElement(doc));
631 if (ctxt->incTab != NULL) {
632 for (i = 0; i < ctxt->incNr; i++)
633 xmlXIncludeFreeRef(ctxt->incTab[i]);
634 xmlFree(ctxt->incTab);
637 ctxt->doc = oldDoc;
638 ctxt->incMax = oldIncMax;
639 ctxt->incNr = oldIncNr;
640 ctxt->incTab = oldIncTab;
641 ctxt->isStream = oldIsStream;
644 /************************************************************************
646 * Node copy with specific semantic *
648 ************************************************************************/
651 * xmlXIncludeCopyNode:
652 * @ctxt: the XInclude context
653 * @elem: the element
654 * @copyChildren: copy children instead of node if true
656 * Make a copy of the node while expanding nested XIncludes.
658 * Returns a node list, not a single node.
660 static xmlNodePtr
661 xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr elem,
662 int copyChildren) {
663 xmlNodePtr result = NULL;
664 xmlNodePtr insertParent = NULL;
665 xmlNodePtr insertLast = NULL;
666 xmlNodePtr cur;
668 if (copyChildren) {
669 cur = elem->children;
670 if (cur == NULL)
671 return(NULL);
672 } else {
673 cur = elem;
676 while (1) {
677 xmlNodePtr copy = NULL;
678 int recurse = 0;
680 if ((cur->type == XML_DOCUMENT_NODE) ||
681 (cur->type == XML_DTD_NODE)) {
683 } else if ((cur->type == XML_ELEMENT_NODE) &&
684 (cur->ns != NULL) &&
685 (xmlStrEqual(cur->name, XINCLUDE_NODE)) &&
686 ((xmlStrEqual(cur->ns->href, XINCLUDE_NS)) ||
687 (xmlStrEqual(cur->ns->href, XINCLUDE_OLD_NS)))) {
688 xmlXIncludeRefPtr ref = xmlXIncludeExpandNode(ctxt, cur);
690 if (ref == NULL)
691 goto error;
693 * TODO: Insert XML_XINCLUDE_START and XML_XINCLUDE_END nodes
695 if (ref->inc != NULL) {
696 copy = xmlStaticCopyNodeList(ref->inc, ctxt->doc,
697 insertParent);
698 if (copy == NULL)
699 goto error;
701 } else {
702 copy = xmlStaticCopyNode(cur, ctxt->doc, insertParent, 2);
703 if (copy == NULL)
704 goto error;
706 recurse = (cur->type != XML_ENTITY_REF_NODE) &&
707 (cur->children != NULL);
710 if (copy != NULL) {
711 if (result == NULL)
712 result = copy;
713 if (insertLast != NULL) {
714 insertLast->next = copy;
715 copy->prev = insertLast;
716 } else if (insertParent != NULL) {
717 insertParent->children = copy;
719 insertLast = copy;
720 while (insertLast->next != NULL) {
721 insertLast = insertLast->next;
725 if (recurse) {
726 cur = cur->children;
727 insertParent = insertLast;
728 insertLast = NULL;
729 continue;
732 if (cur == elem)
733 return(result);
735 while (cur->next == NULL) {
736 cur = cur->parent;
737 if (cur == elem)
738 return(result);
739 insertParent->last = insertLast;
740 insertLast = insertParent;
741 insertParent = insertParent->parent;
744 cur = cur->next;
747 error:
748 xmlFreeNodeList(result);
749 return(NULL);
752 #ifdef LIBXML_XPTR_LOCS_ENABLED
754 * xmlXIncludeGetNthChild:
755 * @cur: the node
756 * @no: the child number
758 * Returns the @n'th element child of @cur or NULL
760 static xmlNodePtr
761 xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
762 int i;
763 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
764 return(NULL);
765 cur = cur->children;
766 for (i = 0;i <= no;cur = cur->next) {
767 if (cur == NULL)
768 return(cur);
769 if ((cur->type == XML_ELEMENT_NODE) ||
770 (cur->type == XML_DOCUMENT_NODE) ||
771 (cur->type == XML_HTML_DOCUMENT_NODE)) {
772 i++;
773 if (i == no)
774 break;
777 return(cur);
780 xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
782 * xmlXIncludeCopyRange:
783 * @ctxt: the XInclude context
784 * @obj: the XPointer result from the evaluation.
786 * Build a node list tree copy of the XPointer result.
788 * Returns an xmlNodePtr list or NULL.
789 * The caller has to free the node tree.
791 static xmlNodePtr
792 xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlXPathObjectPtr range) {
793 /* pointers to generated nodes */
794 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
795 xmlNodePtr tmp, tmp2;
796 /* pointers to traversal nodes */
797 xmlNodePtr start, cur, end;
798 int index1, index2;
799 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
801 if ((ctxt == NULL) || (range == NULL))
802 return(NULL);
803 if (range->type != XPATH_RANGE)
804 return(NULL);
805 start = (xmlNodePtr) range->user;
807 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
808 return(NULL);
809 end = range->user2;
810 if (end == NULL)
811 return(xmlDocCopyNode(start, ctxt->doc, 1));
812 if (end->type == XML_NAMESPACE_DECL)
813 return(NULL);
815 cur = start;
816 index1 = range->index;
817 index2 = range->index2;
819 * level is depth of the current node under consideration
820 * list is the pointer to the root of the output tree
821 * listParent is a pointer to the parent of output tree (within
822 the included file) in case we need to add another level
823 * last is a pointer to the last node added to the output tree
824 * lastLevel is the depth of last (relative to the root)
826 while (cur != NULL) {
828 * Check if our output tree needs a parent
830 if (level < 0) {
831 while (level < 0) {
832 /* copy must include namespaces and properties */
833 tmp2 = xmlDocCopyNode(listParent, ctxt->doc, 2);
834 xmlAddChild(tmp2, list);
835 list = tmp2;
836 listParent = listParent->parent;
837 level++;
839 last = list;
840 lastLevel = 0;
843 * Check whether we need to change our insertion point
845 while (level < lastLevel) {
846 last = last->parent;
847 lastLevel --;
849 if (cur == end) { /* Are we at the end of the range? */
850 if (cur->type == XML_TEXT_NODE) {
851 const xmlChar *content = cur->content;
852 int len;
854 if (content == NULL) {
855 tmp = xmlNewDocTextLen(ctxt->doc, NULL, 0);
856 } else {
857 len = index2;
858 if ((cur == start) && (index1 > 1)) {
859 content += (index1 - 1);
860 len -= (index1 - 1);
861 } else {
862 len = index2;
864 tmp = xmlNewDocTextLen(ctxt->doc, content, len);
866 /* single sub text node selection */
867 if (list == NULL)
868 return(tmp);
869 /* prune and return full set */
870 if (level == lastLevel)
871 xmlAddNextSibling(last, tmp);
872 else
873 xmlAddChild(last, tmp);
874 return(list);
875 } else { /* ending node not a text node */
876 endLevel = level; /* remember the level of the end node */
877 endFlag = 1;
878 /* last node - need to take care of properties + namespaces */
879 tmp = xmlDocCopyNode(cur, ctxt->doc, 2);
880 if (list == NULL) {
881 list = tmp;
882 listParent = cur->parent;
883 last = tmp;
884 } else {
885 if (level == lastLevel)
886 last = xmlAddNextSibling(last, tmp);
887 else {
888 last = xmlAddChild(last, tmp);
889 lastLevel = level;
893 if (index2 > 1) {
894 end = xmlXIncludeGetNthChild(cur, index2 - 1);
895 index2 = 0;
897 if ((cur == start) && (index1 > 1)) {
898 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
899 index1 = 0;
900 } else {
901 cur = cur->children;
903 level++; /* increment level to show change */
905 * Now gather the remaining nodes from cur to end
907 continue; /* while */
909 } else if (cur == start) { /* Not at the end, are we at start? */
910 if ((cur->type == XML_TEXT_NODE) ||
911 (cur->type == XML_CDATA_SECTION_NODE)) {
912 const xmlChar *content = cur->content;
914 if (content == NULL) {
915 tmp = xmlNewDocTextLen(ctxt->doc, NULL, 0);
916 } else {
917 if (index1 > 1) {
918 content += (index1 - 1);
919 index1 = 0;
921 tmp = xmlNewDocText(ctxt->doc, content);
923 last = list = tmp;
924 listParent = cur->parent;
925 } else { /* Not text node */
927 * start of the range - need to take care of
928 * properties and namespaces
930 tmp = xmlDocCopyNode(cur, ctxt->doc, 2);
931 list = last = tmp;
932 listParent = cur->parent;
933 if (index1 > 1) { /* Do we need to position? */
934 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
935 level = lastLevel = 1;
936 index1 = 0;
938 * Now gather the remaining nodes from cur to end
940 continue; /* while */
943 } else {
944 tmp = NULL;
945 switch (cur->type) {
946 case XML_DTD_NODE:
947 case XML_ELEMENT_DECL:
948 case XML_ATTRIBUTE_DECL:
949 case XML_ENTITY_NODE:
950 /* Do not copy DTD information */
951 break;
952 case XML_ENTITY_DECL:
953 /* handle crossing entities -> stack needed */
954 break;
955 case XML_XINCLUDE_START:
956 case XML_XINCLUDE_END:
957 /* don't consider it part of the tree content */
958 break;
959 case XML_ATTRIBUTE_NODE:
960 /* Humm, should not happen ! */
961 break;
962 default:
964 * Middle of the range - need to take care of
965 * properties and namespaces
967 tmp = xmlDocCopyNode(cur, ctxt->doc, 2);
968 break;
970 if (tmp != NULL) {
971 if (level == lastLevel)
972 last = xmlAddNextSibling(last, tmp);
973 else {
974 last = xmlAddChild(last, tmp);
975 lastLevel = level;
980 * Skip to next node in document order
982 cur = xmlXPtrAdvanceNode(cur, &level);
983 if (endFlag && (level >= endLevel))
984 break;
986 return(list);
988 #endif /* LIBXML_XPTR_LOCS_ENABLED */
991 * xmlXIncludeCopyXPointer:
992 * @ctxt: the XInclude context
993 * @obj: the XPointer result from the evaluation.
995 * Build a node list tree copy of the XPointer result.
996 * This will drop Attributes and Namespace declarations.
998 * Returns an xmlNodePtr list or NULL.
999 * the caller has to free the node tree.
1001 static xmlNodePtr
1002 xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlXPathObjectPtr obj) {
1003 xmlNodePtr list = NULL, last = NULL, copy;
1004 int i;
1006 if ((ctxt == NULL) || (obj == NULL))
1007 return(NULL);
1008 switch (obj->type) {
1009 case XPATH_NODESET: {
1010 xmlNodeSetPtr set = obj->nodesetval;
1011 if (set == NULL)
1012 return(NULL);
1013 for (i = 0;i < set->nodeNr;i++) {
1014 xmlNodePtr node;
1016 if (set->nodeTab[i] == NULL)
1017 continue;
1018 switch (set->nodeTab[i]->type) {
1019 case XML_DOCUMENT_NODE:
1020 case XML_HTML_DOCUMENT_NODE:
1021 node = xmlDocGetRootElement(
1022 (xmlDocPtr) set->nodeTab[i]);
1023 if (node == NULL) {
1024 xmlXIncludeErr(ctxt, set->nodeTab[i],
1025 XML_ERR_INTERNAL_ERROR,
1026 "document without root\n", NULL);
1027 continue;
1029 break;
1030 case XML_TEXT_NODE:
1031 case XML_CDATA_SECTION_NODE:
1032 case XML_ELEMENT_NODE:
1033 case XML_PI_NODE:
1034 case XML_COMMENT_NODE:
1035 node = set->nodeTab[i];
1036 break;
1037 default:
1038 xmlXIncludeErr(ctxt, set->nodeTab[i],
1039 XML_XINCLUDE_XPTR_RESULT,
1040 "invalid node type in XPtr result\n",
1041 NULL);
1042 continue; /* for */
1045 * OPTIMIZE TODO: External documents should already be
1046 * expanded, so xmlDocCopyNode should work as well.
1047 * xmlXIncludeCopyNode is only required for the initial
1048 * document.
1050 copy = xmlXIncludeCopyNode(ctxt, node, 0);
1051 if (copy == NULL) {
1052 xmlFreeNodeList(list);
1053 return(NULL);
1055 if (last == NULL) {
1056 list = copy;
1057 } else {
1058 while (last->next != NULL)
1059 last = last->next;
1060 copy->prev = last;
1061 last->next = copy;
1063 last = copy;
1065 break;
1067 #ifdef LIBXML_XPTR_LOCS_ENABLED
1068 case XPATH_LOCATIONSET: {
1069 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1070 if (set == NULL)
1071 return(NULL);
1072 for (i = 0;i < set->locNr;i++) {
1073 if (last == NULL)
1074 list = last = xmlXIncludeCopyXPointer(ctxt,
1075 set->locTab[i]);
1076 else
1077 xmlAddNextSibling(last,
1078 xmlXIncludeCopyXPointer(ctxt, set->locTab[i]));
1079 if (last != NULL) {
1080 while (last->next != NULL)
1081 last = last->next;
1084 break;
1086 case XPATH_RANGE:
1087 return(xmlXIncludeCopyRange(ctxt, obj));
1088 case XPATH_POINT:
1089 /* points are ignored in XInclude */
1090 break;
1091 #endif
1092 default:
1093 break;
1095 return(list);
1097 /************************************************************************
1099 * XInclude I/O handling *
1101 ************************************************************************/
1103 typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1104 typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1105 struct _xmlXIncludeMergeData {
1106 xmlDocPtr doc;
1107 xmlXIncludeCtxtPtr ctxt;
1111 * xmlXIncludeMergeOneEntity:
1112 * @ent: the entity
1113 * @doc: the including doc
1114 * @name: the entity name
1116 * Implements the merge of one entity
1118 static void
1119 xmlXIncludeMergeEntity(void *payload, void *vdata,
1120 const xmlChar *name ATTRIBUTE_UNUSED) {
1121 xmlEntityPtr ent = (xmlEntityPtr) payload;
1122 xmlXIncludeMergeDataPtr data = (xmlXIncludeMergeDataPtr) vdata;
1123 xmlEntityPtr ret, prev;
1124 xmlDocPtr doc;
1125 xmlXIncludeCtxtPtr ctxt;
1127 if ((ent == NULL) || (data == NULL))
1128 return;
1129 ctxt = data->ctxt;
1130 doc = data->doc;
1131 if ((ctxt == NULL) || (doc == NULL))
1132 return;
1133 switch (ent->etype) {
1134 case XML_INTERNAL_PARAMETER_ENTITY:
1135 case XML_EXTERNAL_PARAMETER_ENTITY:
1136 case XML_INTERNAL_PREDEFINED_ENTITY:
1137 return;
1138 case XML_INTERNAL_GENERAL_ENTITY:
1139 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1140 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1141 break;
1143 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1144 ent->SystemID, ent->content);
1145 if (ret != NULL) {
1146 if (ent->URI != NULL)
1147 ret->URI = xmlStrdup(ent->URI);
1148 } else {
1149 prev = xmlGetDocEntity(doc, ent->name);
1150 if (prev != NULL) {
1151 if (ent->etype != prev->etype)
1152 goto error;
1154 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1155 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1156 goto error;
1157 } else if ((ent->ExternalID != NULL) &&
1158 (prev->ExternalID != NULL)) {
1159 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1160 goto error;
1161 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1162 if (!xmlStrEqual(ent->content, prev->content))
1163 goto error;
1164 } else {
1165 goto error;
1170 return;
1171 error:
1172 switch (ent->etype) {
1173 case XML_INTERNAL_PARAMETER_ENTITY:
1174 case XML_EXTERNAL_PARAMETER_ENTITY:
1175 case XML_INTERNAL_PREDEFINED_ENTITY:
1176 case XML_INTERNAL_GENERAL_ENTITY:
1177 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1178 return;
1179 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1180 break;
1182 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1183 "mismatch in redefinition of entity %s\n",
1184 ent->name);
1188 * xmlXIncludeMergeEntities:
1189 * @ctxt: an XInclude context
1190 * @doc: the including doc
1191 * @from: the included doc
1193 * Implements the entity merge
1195 * Returns 0 if merge succeeded, -1 if some processing failed
1197 static int
1198 xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1199 xmlDocPtr from) {
1200 xmlNodePtr cur;
1201 xmlDtdPtr target, source;
1203 if (ctxt == NULL)
1204 return(-1);
1206 if ((from == NULL) || (from->intSubset == NULL))
1207 return(0);
1209 target = doc->intSubset;
1210 if (target == NULL) {
1211 cur = xmlDocGetRootElement(doc);
1212 if (cur == NULL)
1213 return(-1);
1214 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1215 if (target == NULL)
1216 return(-1);
1219 source = from->intSubset;
1220 if ((source != NULL) && (source->entities != NULL)) {
1221 xmlXIncludeMergeData data;
1223 data.ctxt = ctxt;
1224 data.doc = doc;
1226 xmlHashScan((xmlHashTablePtr) source->entities,
1227 xmlXIncludeMergeEntity, &data);
1229 source = from->extSubset;
1230 if ((source != NULL) && (source->entities != NULL)) {
1231 xmlXIncludeMergeData data;
1233 data.ctxt = ctxt;
1234 data.doc = doc;
1237 * don't duplicate existing stuff when external subsets are the same
1239 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1240 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1241 xmlHashScan((xmlHashTablePtr) source->entities,
1242 xmlXIncludeMergeEntity, &data);
1245 return(0);
1249 * xmlXIncludeLoadDoc:
1250 * @ctxt: the XInclude context
1251 * @url: the associated URL
1252 * @ref: an XMLXincludeRefPtr
1254 * Load the document, and store the result in the XInclude context
1256 * Returns 0 in case of success, -1 in case of failure
1258 static int
1259 xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url,
1260 xmlXIncludeRefPtr ref) {
1261 xmlXIncludeDocPtr cache;
1262 xmlDocPtr doc;
1263 xmlURIPtr uri;
1264 xmlChar *URL = NULL;
1265 xmlChar *fragment = NULL;
1266 int i = 0;
1267 int ret = -1;
1268 int cacheNr;
1269 #ifdef LIBXML_XPTR_ENABLED
1270 int saveFlags;
1271 #endif
1273 #ifdef DEBUG_XINCLUDE
1274 xmlGenericError(xmlGenericErrorContext, "Loading doc %s\n", url);
1275 #endif
1277 * Check the URL and remove any fragment identifier
1279 uri = xmlParseURI((const char *)url);
1280 if (uri == NULL) {
1281 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1282 "invalid value URI %s\n", url);
1283 goto error;
1285 if (uri->fragment != NULL) {
1286 fragment = (xmlChar *) uri->fragment;
1287 uri->fragment = NULL;
1289 if (ref->fragment != NULL) {
1290 if (fragment != NULL) xmlFree(fragment);
1291 fragment = xmlStrdup(ref->fragment);
1293 URL = xmlSaveUri(uri);
1294 xmlFreeURI(uri);
1295 if (URL == NULL) {
1296 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1297 "invalid value URI %s\n", url);
1298 goto error;
1302 * Handling of references to the local document are done
1303 * directly through ctxt->doc.
1305 if ((URL[0] == 0) || (URL[0] == '#') ||
1306 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
1307 doc = ctxt->doc;
1308 goto loaded;
1312 * Prevent reloading the document twice.
1314 for (i = 0; i < ctxt->urlNr; i++) {
1315 if (xmlStrEqual(URL, ctxt->urlTab[i].url)) {
1316 #ifdef DEBUG_XINCLUDE
1317 printf("Already loaded %s\n", URL);
1318 #endif
1319 if (ctxt->urlTab[i].expanding) {
1320 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_RECURSION,
1321 "inclusion loop detected\n", NULL);
1322 goto error;
1324 doc = ctxt->urlTab[i].doc;
1325 if (doc == NULL)
1326 goto error;
1327 goto loaded;
1332 * Load it.
1334 #ifdef DEBUG_XINCLUDE
1335 printf("loading %s\n", URL);
1336 #endif
1337 #ifdef LIBXML_XPTR_ENABLED
1339 * If this is an XPointer evaluation, we want to assure that
1340 * all entities have been resolved prior to processing the
1341 * referenced document
1343 saveFlags = ctxt->parseFlags;
1344 if (fragment != NULL) { /* if this is an XPointer eval */
1345 ctxt->parseFlags |= XML_PARSE_NOENT;
1347 #endif
1349 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
1350 #ifdef LIBXML_XPTR_ENABLED
1351 ctxt->parseFlags = saveFlags;
1352 #endif
1354 /* Also cache NULL docs */
1355 if (ctxt->urlNr >= ctxt->urlMax) {
1356 xmlXIncludeDoc *tmp;
1357 size_t newSize = ctxt->urlMax ? ctxt->urlMax * 2 : 8;
1359 tmp = xmlRealloc(ctxt->urlTab, sizeof(xmlXIncludeDoc) * newSize);
1360 if (tmp == NULL) {
1361 xmlXIncludeErrMemory(ctxt, ref->elem,
1362 "growing XInclude URL table");
1363 xmlFreeDoc(doc);
1364 goto error;
1366 ctxt->urlMax = newSize;
1367 ctxt->urlTab = tmp;
1369 cacheNr = ctxt->urlNr++;
1370 cache = &ctxt->urlTab[cacheNr];
1371 cache->doc = doc;
1372 cache->url = xmlStrdup(URL);
1373 cache->expanding = 0;
1375 if (doc == NULL)
1376 goto error;
1378 * It's possible that the requested URL has been mapped to a
1379 * completely different location (e.g. through a catalog entry).
1380 * To check for this, we compare the URL with that of the doc
1381 * and change it if they disagree (bug 146988).
1383 if (!xmlStrEqual(URL, doc->URL)) {
1384 xmlFree(URL);
1385 URL = xmlStrdup(doc->URL);
1389 * Make sure we have all entities fixed up
1391 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
1394 * We don't need the DTD anymore, free up space
1395 if (doc->intSubset != NULL) {
1396 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1397 xmlFreeNode((xmlNodePtr) doc->intSubset);
1398 doc->intSubset = NULL;
1400 if (doc->extSubset != NULL) {
1401 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1402 xmlFreeNode((xmlNodePtr) doc->extSubset);
1403 doc->extSubset = NULL;
1406 cache->expanding = 1;
1407 xmlXIncludeRecurseDoc(ctxt, doc, URL);
1408 /* urlTab might be reallocated. */
1409 cache = &ctxt->urlTab[cacheNr];
1410 cache->expanding = 0;
1412 loaded:
1413 if (fragment == NULL) {
1415 * Add the top children list as the replacement copy.
1417 ref->inc = xmlDocCopyNode(xmlDocGetRootElement(doc), ctxt->doc, 1);
1419 #ifdef LIBXML_XPTR_ENABLED
1420 else {
1422 * Computes the XPointer expression and make a copy used
1423 * as the replacement copy.
1425 xmlXPathObjectPtr xptr;
1426 xmlXPathContextPtr xptrctxt;
1427 xmlNodeSetPtr set;
1429 if (ctxt->isStream && doc == ctxt->doc) {
1430 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
1431 "XPointer expressions not allowed in streaming"
1432 " mode\n", NULL);
1433 goto error;
1436 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1437 if (xptrctxt == NULL) {
1438 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
1439 "could not create XPointer context\n", NULL);
1440 goto error;
1442 xptr = xmlXPtrEval(fragment, xptrctxt);
1443 if (xptr == NULL) {
1444 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
1445 "XPointer evaluation failed: #%s\n",
1446 fragment);
1447 xmlXPathFreeContext(xptrctxt);
1448 goto error;
1450 switch (xptr->type) {
1451 case XPATH_UNDEFINED:
1452 case XPATH_BOOLEAN:
1453 case XPATH_NUMBER:
1454 case XPATH_STRING:
1455 #ifdef LIBXML_XPTR_LOCS_ENABLED
1456 case XPATH_POINT:
1457 #endif
1458 case XPATH_USERS:
1459 case XPATH_XSLT_TREE:
1460 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_RESULT,
1461 "XPointer is not a range: #%s\n",
1462 fragment);
1463 xmlXPathFreeObject(xptr);
1464 xmlXPathFreeContext(xptrctxt);
1465 goto error;
1466 case XPATH_NODESET:
1467 if ((xptr->nodesetval == NULL) ||
1468 (xptr->nodesetval->nodeNr <= 0)) {
1469 xmlXPathFreeObject(xptr);
1470 xmlXPathFreeContext(xptrctxt);
1471 goto error;
1474 #ifdef LIBXML_XPTR_LOCS_ENABLED
1475 case XPATH_RANGE:
1476 case XPATH_LOCATIONSET:
1477 break;
1478 #endif
1480 set = xptr->nodesetval;
1481 if (set != NULL) {
1482 for (i = 0;i < set->nodeNr;i++) {
1483 if (set->nodeTab[i] == NULL)
1484 continue;
1485 switch (set->nodeTab[i]->type) {
1486 case XML_ELEMENT_NODE:
1487 case XML_TEXT_NODE:
1488 case XML_CDATA_SECTION_NODE:
1489 case XML_ENTITY_REF_NODE:
1490 case XML_ENTITY_NODE:
1491 case XML_PI_NODE:
1492 case XML_COMMENT_NODE:
1493 case XML_DOCUMENT_NODE:
1494 case XML_HTML_DOCUMENT_NODE:
1495 continue;
1497 case XML_ATTRIBUTE_NODE:
1498 xmlXIncludeErr(ctxt, ref->elem,
1499 XML_XINCLUDE_XPTR_RESULT,
1500 "XPointer selects an attribute: #%s\n",
1501 fragment);
1502 set->nodeTab[i] = NULL;
1503 continue;
1504 case XML_NAMESPACE_DECL:
1505 xmlXIncludeErr(ctxt, ref->elem,
1506 XML_XINCLUDE_XPTR_RESULT,
1507 "XPointer selects a namespace: #%s\n",
1508 fragment);
1509 set->nodeTab[i] = NULL;
1510 continue;
1511 case XML_DOCUMENT_TYPE_NODE:
1512 case XML_DOCUMENT_FRAG_NODE:
1513 case XML_NOTATION_NODE:
1514 case XML_DTD_NODE:
1515 case XML_ELEMENT_DECL:
1516 case XML_ATTRIBUTE_DECL:
1517 case XML_ENTITY_DECL:
1518 case XML_XINCLUDE_START:
1519 case XML_XINCLUDE_END:
1520 xmlXIncludeErr(ctxt, ref->elem,
1521 XML_XINCLUDE_XPTR_RESULT,
1522 "XPointer selects unexpected nodes: #%s\n",
1523 fragment);
1524 set->nodeTab[i] = NULL;
1525 set->nodeTab[i] = NULL;
1526 continue; /* for */
1530 ref->inc = xmlXIncludeCopyXPointer(ctxt, xptr);
1531 xmlXPathFreeObject(xptr);
1532 xmlXPathFreeContext(xptrctxt);
1534 #endif
1537 * Do the xml:base fixup if needed
1539 if ((doc != NULL) && (URL != NULL) &&
1540 (!(ctxt->parseFlags & XML_PARSE_NOBASEFIX)) &&
1541 (!(doc->parseFlags & XML_PARSE_NOBASEFIX))) {
1542 xmlNodePtr node;
1543 xmlChar *base;
1544 xmlChar *curBase;
1547 * The base is only adjusted if "necessary", i.e. if the xinclude node
1548 * has a base specified, or the URL is relative
1550 base = xmlGetNsProp(ref->elem, BAD_CAST "base", XML_XML_NAMESPACE);
1551 if (base == NULL) {
1553 * No xml:base on the xinclude node, so we check whether the
1554 * URI base is different than (relative to) the context base
1556 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1557 if (curBase == NULL) { /* Error return */
1558 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1559 "trying to build relative URI from %s\n", URL);
1560 } else {
1561 /* If the URI doesn't contain a slash, it's not relative */
1562 if (!xmlStrchr(curBase, '/'))
1563 xmlFree(curBase);
1564 else
1565 base = curBase;
1568 if (base != NULL) { /* Adjustment may be needed */
1569 node = ref->inc;
1570 while (node != NULL) {
1571 /* Only work on element nodes */
1572 if (node->type == XML_ELEMENT_NODE) {
1573 curBase = xmlNodeGetBase(node->doc, node);
1574 /* If no current base, set it */
1575 if (curBase == NULL) {
1576 xmlNodeSetBase(node, base);
1577 } else {
1579 * If the current base is the same as the
1580 * URL of the document, then reset it to be
1581 * the specified xml:base or the relative URI
1583 if (xmlStrEqual(curBase, node->doc->URL)) {
1584 xmlNodeSetBase(node, base);
1585 } else {
1587 * If the element already has an xml:base
1588 * set, then relativise it if necessary
1590 xmlChar *xmlBase;
1591 xmlBase = xmlGetNsProp(node,
1592 BAD_CAST "base",
1593 XML_XML_NAMESPACE);
1594 if (xmlBase != NULL) {
1595 xmlChar *relBase;
1596 relBase = xmlBuildURI(xmlBase, base);
1597 if (relBase == NULL) { /* error */
1598 xmlXIncludeErr(ctxt,
1599 ref->elem,
1600 XML_XINCLUDE_HREF_URI,
1601 "trying to rebuild base from %s\n",
1602 xmlBase);
1603 } else {
1604 xmlNodeSetBase(node, relBase);
1605 xmlFree(relBase);
1607 xmlFree(xmlBase);
1610 xmlFree(curBase);
1613 node = node->next;
1615 xmlFree(base);
1618 ret = 0;
1620 error:
1621 xmlFree(URL);
1622 xmlFree(fragment);
1623 return(ret);
1627 * xmlXIncludeLoadTxt:
1628 * @ctxt: the XInclude context
1629 * @url: the associated URL
1630 * @ref: an XMLXincludeRefPtr
1632 * Load the content, and store the result in the XInclude context
1634 * Returns 0 in case of success, -1 in case of failure
1636 static int
1637 xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url,
1638 xmlXIncludeRefPtr ref) {
1639 xmlParserInputBufferPtr buf;
1640 xmlNodePtr node = NULL;
1641 xmlURIPtr uri = NULL;
1642 xmlChar *URL = NULL;
1643 int i;
1644 int ret = -1;
1645 xmlChar *encoding = NULL;
1646 xmlCharEncoding enc = (xmlCharEncoding) 0;
1647 xmlParserCtxtPtr pctxt = NULL;
1648 xmlParserInputPtr inputStream = NULL;
1649 int len;
1650 const xmlChar *content;
1653 /* Don't read from stdin. */
1654 if (xmlStrcmp(url, BAD_CAST "-") == 0)
1655 url = BAD_CAST "./-";
1658 * Check the URL and remove any fragment identifier
1660 uri = xmlParseURI((const char *)url);
1661 if (uri == NULL) {
1662 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1663 "invalid value URI %s\n", url);
1664 goto error;
1666 if (uri->fragment != NULL) {
1667 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_TEXT_FRAGMENT,
1668 "fragment identifier forbidden for text: %s\n",
1669 (const xmlChar *) uri->fragment);
1670 goto error;
1672 URL = xmlSaveUri(uri);
1673 if (URL == NULL) {
1674 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1675 "invalid value URI %s\n", url);
1676 goto error;
1680 * Handling of references to the local document are done
1681 * directly through ctxt->doc.
1683 if (URL[0] == 0) {
1684 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_TEXT_DOCUMENT,
1685 "text serialization of document not available\n", NULL);
1686 goto error;
1690 * Prevent reloading the document twice.
1692 for (i = 0; i < ctxt->txtNr; i++) {
1693 if (xmlStrEqual(URL, ctxt->txtTab[i].url)) {
1694 node = xmlNewDocText(ctxt->doc, ctxt->txtTab[i].text);
1695 goto loaded;
1700 * Try to get the encoding if available
1702 if (ref->elem != NULL) {
1703 encoding = xmlGetProp(ref->elem, XINCLUDE_PARSE_ENCODING);
1705 if (encoding != NULL) {
1707 * TODO: we should not have to remap to the xmlCharEncoding
1708 * predefined set, a better interface than
1709 * xmlParserInputBufferCreateFilename should allow any
1710 * encoding supported by iconv
1712 enc = xmlParseCharEncoding((const char *) encoding);
1713 if (enc == XML_CHAR_ENCODING_ERROR) {
1714 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_UNKNOWN_ENCODING,
1715 "encoding %s not supported\n", encoding);
1716 goto error;
1721 * Load it.
1723 pctxt = xmlNewParserCtxt();
1724 inputStream = xmlLoadExternalEntity((const char*)URL, NULL, pctxt);
1725 if(inputStream == NULL)
1726 goto error;
1727 buf = inputStream->buf;
1728 if (buf == NULL)
1729 goto error;
1730 if (buf->encoder)
1731 xmlCharEncCloseFunc(buf->encoder);
1732 buf->encoder = xmlGetCharEncodingHandler(enc);
1733 node = xmlNewDocText(ctxt->doc, NULL);
1734 if (node == NULL) {
1735 xmlXIncludeErrMemory(ctxt, ref->elem, NULL);
1736 goto error;
1740 * Scan all chars from the resource and add the to the node
1742 while (xmlParserInputBufferRead(buf, 4096) > 0)
1745 content = xmlBufContent(buf->buffer);
1746 len = xmlBufLength(buf->buffer);
1747 for (i = 0; i < len;) {
1748 int cur;
1749 int l;
1751 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1752 if (!IS_CHAR(cur)) {
1753 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_INVALID_CHAR,
1754 "%s contains invalid char\n", URL);
1755 goto error;
1758 i += l;
1761 xmlNodeAddContentLen(node, content, len);
1763 if (ctxt->txtNr >= ctxt->txtMax) {
1764 xmlXIncludeTxt *tmp;
1765 size_t newSize = ctxt->txtMax ? ctxt->txtMax * 2 : 8;
1767 tmp = xmlRealloc(ctxt->txtTab, sizeof(xmlXIncludeTxt) * newSize);
1768 if (tmp == NULL) {
1769 xmlXIncludeErrMemory(ctxt, ref->elem,
1770 "growing XInclude text table");
1771 goto error;
1773 ctxt->txtMax = newSize;
1774 ctxt->txtTab = tmp;
1776 ctxt->txtTab[ctxt->txtNr].text = xmlStrdup(node->content);
1777 ctxt->txtTab[ctxt->txtNr].url = xmlStrdup(URL);
1778 ctxt->txtNr++;
1780 loaded:
1782 * Add the element as the replacement copy.
1784 ref->inc = node;
1785 node = NULL;
1786 ret = 0;
1788 error:
1789 xmlFreeNode(node);
1790 xmlFreeInputStream(inputStream);
1791 xmlFreeParserCtxt(pctxt);
1792 xmlFree(encoding);
1793 xmlFreeURI(uri);
1794 xmlFree(URL);
1795 return(ret);
1799 * xmlXIncludeLoadFallback:
1800 * @ctxt: the XInclude context
1801 * @fallback: the fallback node
1802 * @ref: an XMLXincludeRefPtr
1804 * Load the content of the fallback node, and store the result
1805 * in the XInclude context
1807 * Returns 0 in case of success, -1 in case of failure
1809 static int
1810 xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback,
1811 xmlXIncludeRefPtr ref) {
1812 int ret = 0;
1813 int oldNbErrors;
1815 if ((fallback == NULL) || (fallback->type == XML_NAMESPACE_DECL) ||
1816 (ctxt == NULL))
1817 return(-1);
1818 if (fallback->children != NULL) {
1820 * It's possible that the fallback also has 'includes'
1821 * (Bug 129969), so we re-process the fallback just in case
1823 oldNbErrors = ctxt->nbErrors;
1824 ref->inc = xmlXIncludeCopyNode(ctxt, fallback, 1);
1825 if (ctxt->nbErrors > oldNbErrors)
1826 ret = -1;
1827 else if (ref->inc == NULL)
1828 ref->emptyFb = 1;
1829 } else {
1830 ref->inc = NULL;
1831 ref->emptyFb = 1; /* flag empty callback */
1833 ref->fallback = 1;
1834 return(ret);
1837 /************************************************************************
1839 * XInclude Processing *
1841 ************************************************************************/
1844 * xmlXIncludeExpandNode:
1845 * @ctxt: an XInclude context
1846 * @node: an XInclude node
1848 * If the XInclude node wasn't processed yet, create a new RefPtr,
1849 * add it to ctxt->incTab and load the included items.
1851 * Returns the new or existing xmlXIncludeRefPtr, or NULL in case of error.
1853 static xmlXIncludeRefPtr
1854 xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1855 xmlXIncludeRefPtr ref;
1856 int i;
1858 if (ctxt->fatalErr)
1859 return(NULL);
1860 if (ctxt->depth >= XINCLUDE_MAX_DEPTH) {
1861 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_RECURSION,
1862 "maximum recursion depth exceeded\n", NULL);
1863 ctxt->fatalErr = 1;
1864 return(NULL);
1867 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1869 * The XInclude engine offers no protection against exponential
1870 * expansion attacks similar to "billion laughs". Avoid timeouts by
1871 * limiting the total number of replacements when fuzzing.
1873 if (ctxt->incTotal >= 20)
1874 return(NULL);
1875 ctxt->incTotal++;
1876 #endif
1878 for (i = 0; i < ctxt->incNr; i++) {
1879 if (ctxt->incTab[i]->elem == node) {
1880 if (ctxt->incTab[i]->expanding) {
1881 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_RECURSION,
1882 "inclusion loop detected\n", NULL);
1883 return(NULL);
1885 return(ctxt->incTab[i]);
1889 ref = xmlXIncludeAddNode(ctxt, node);
1890 if (ref == NULL)
1891 return(NULL);
1892 ref->expanding = 1;
1893 ctxt->depth++;
1894 xmlXIncludeLoadNode(ctxt, ref);
1895 ctxt->depth--;
1896 ref->expanding = 0;
1898 return(ref);
1902 * xmlXIncludeLoadNode:
1903 * @ctxt: an XInclude context
1904 * @ref: an xmlXIncludeRefPtr
1906 * Find and load the infoset replacement for the given node.
1908 * Returns 0 if substitution succeeded, -1 if some processing failed
1910 static int
1911 xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
1912 xmlNodePtr cur;
1913 xmlChar *href;
1914 xmlChar *parse;
1915 xmlChar *base;
1916 xmlChar *oldBase;
1917 xmlChar *URI;
1918 int xml = 1; /* default Issue 64 */
1919 int ret;
1921 if ((ctxt == NULL) || (ref == NULL))
1922 return(-1);
1923 cur = ref->elem;
1924 if (cur == NULL)
1925 return(-1);
1928 * read the attributes
1930 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
1931 if (href == NULL) {
1932 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
1933 if (href == NULL)
1934 return(-1);
1936 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
1937 if (parse != NULL) {
1938 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1939 xml = 1;
1940 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1941 xml = 0;
1942 else {
1943 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
1944 "invalid value %s for 'parse'\n", parse);
1945 if (href != NULL)
1946 xmlFree(href);
1947 if (parse != NULL)
1948 xmlFree(parse);
1949 return(-1);
1954 * compute the URI
1956 base = xmlNodeGetBase(ctxt->doc, cur);
1957 if (base == NULL) {
1958 URI = xmlBuildURI(href, ctxt->doc->URL);
1959 } else {
1960 URI = xmlBuildURI(href, base);
1962 if (URI == NULL) {
1963 xmlChar *escbase;
1964 xmlChar *eschref;
1966 * Some escaping may be needed
1968 escbase = xmlURIEscape(base);
1969 eschref = xmlURIEscape(href);
1970 URI = xmlBuildURI(eschref, escbase);
1971 if (escbase != NULL)
1972 xmlFree(escbase);
1973 if (eschref != NULL)
1974 xmlFree(eschref);
1976 if (URI == NULL) {
1977 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
1978 "failed build URL\n", NULL);
1979 if (parse != NULL)
1980 xmlFree(parse);
1981 if (href != NULL)
1982 xmlFree(href);
1983 if (base != NULL)
1984 xmlFree(base);
1985 return(-1);
1987 #ifdef DEBUG_XINCLUDE
1988 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
1989 xml ? "xml": "text");
1990 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
1991 #endif
1994 * Save the base for this include (saving the current one)
1996 oldBase = ctxt->base;
1997 ctxt->base = base;
1999 if (xml) {
2000 ret = xmlXIncludeLoadDoc(ctxt, URI, ref);
2001 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2002 } else {
2003 ret = xmlXIncludeLoadTxt(ctxt, URI, ref);
2007 * Restore the original base before checking for fallback
2009 ctxt->base = oldBase;
2011 if (ret < 0) {
2012 xmlNodePtr children;
2015 * Time to try a fallback if available
2017 #ifdef DEBUG_XINCLUDE
2018 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2019 #endif
2020 children = cur->children;
2021 while (children != NULL) {
2022 if ((children->type == XML_ELEMENT_NODE) &&
2023 (children->ns != NULL) &&
2024 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
2025 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2026 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
2027 ret = xmlXIncludeLoadFallback(ctxt, children, ref);
2028 break;
2030 children = children->next;
2033 if (ret < 0) {
2034 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_NO_FALLBACK,
2035 "could not load %s, and no fallback was found\n",
2036 URI);
2040 * Cleanup
2042 if (URI != NULL)
2043 xmlFree(URI);
2044 if (parse != NULL)
2045 xmlFree(parse);
2046 if (href != NULL)
2047 xmlFree(href);
2048 if (base != NULL)
2049 xmlFree(base);
2050 return(0);
2054 * xmlXIncludeIncludeNode:
2055 * @ctxt: an XInclude context
2056 * @ref: an xmlXIncludeRefPtr
2058 * Implement the infoset replacement for the given node
2060 * Returns 0 if substitution succeeded, -1 if some processing failed
2062 static int
2063 xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
2064 xmlNodePtr cur, end, list, tmp;
2066 if ((ctxt == NULL) || (ref == NULL))
2067 return(-1);
2068 cur = ref->elem;
2069 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
2070 return(-1);
2072 list = ref->inc;
2073 ref->inc = NULL;
2074 ref->emptyFb = 0;
2077 * Check against the risk of generating a multi-rooted document
2079 if ((cur->parent != NULL) &&
2080 (cur->parent->type != XML_ELEMENT_NODE)) {
2081 int nb_elem = 0;
2083 tmp = list;
2084 while (tmp != NULL) {
2085 if (tmp->type == XML_ELEMENT_NODE)
2086 nb_elem++;
2087 tmp = tmp->next;
2089 if (nb_elem > 1) {
2090 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_MULTIPLE_ROOT,
2091 "XInclude error: would result in multiple root nodes\n",
2092 NULL);
2093 xmlFreeNodeList(list);
2094 return(-1);
2098 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2100 * Add the list of nodes
2102 while (list != NULL) {
2103 end = list;
2104 list = list->next;
2106 xmlAddPrevSibling(cur, end);
2109 * FIXME: xmlUnlinkNode doesn't coalesce text nodes.
2111 xmlUnlinkNode(cur);
2112 xmlFreeNode(cur);
2113 } else {
2114 xmlNodePtr child, next;
2117 * Change the current node as an XInclude start one, and add an
2118 * XInclude end one
2120 if (ref->fallback)
2121 xmlUnsetProp(cur, BAD_CAST "href");
2122 cur->type = XML_XINCLUDE_START;
2123 /* Remove fallback children */
2124 for (child = cur->children; child != NULL; child = next) {
2125 next = child->next;
2126 xmlUnlinkNode(child);
2127 xmlFreeNode(child);
2129 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
2130 if (end == NULL) {
2131 xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_BUILD_FAILED,
2132 "failed to build node\n", NULL);
2133 xmlFreeNodeList(list);
2134 return(-1);
2136 end->type = XML_XINCLUDE_END;
2137 xmlAddNextSibling(cur, end);
2140 * Add the list of nodes
2142 while (list != NULL) {
2143 cur = list;
2144 list = list->next;
2146 xmlAddPrevSibling(end, cur);
2151 return(0);
2155 * xmlXIncludeTestNode:
2156 * @ctxt: the XInclude processing context
2157 * @node: an XInclude node
2159 * test if the node is an XInclude node
2161 * Returns 1 true, 0 otherwise
2163 static int
2164 xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2165 if (node == NULL)
2166 return(0);
2167 if (node->type != XML_ELEMENT_NODE)
2168 return(0);
2169 if (node->ns == NULL)
2170 return(0);
2171 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2172 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2173 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2174 if (ctxt->legacy == 0) {
2175 #if 0 /* wait for the XML Core Working Group to get something stable ! */
2176 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2177 "Deprecated XInclude namespace found, use %s",
2178 XINCLUDE_NS);
2179 #endif
2180 ctxt->legacy = 1;
2183 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2184 xmlNodePtr child = node->children;
2185 int nb_fallback = 0;
2187 while (child != NULL) {
2188 if ((child->type == XML_ELEMENT_NODE) &&
2189 (child->ns != NULL) &&
2190 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2191 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
2192 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
2193 xmlXIncludeErr(ctxt, node,
2194 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2195 "%s has an 'include' child\n",
2196 XINCLUDE_NODE);
2197 return(0);
2199 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2200 nb_fallback++;
2203 child = child->next;
2205 if (nb_fallback > 1) {
2206 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2207 "%s has multiple fallback children\n",
2208 XINCLUDE_NODE);
2209 return(0);
2211 return(1);
2213 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2214 if ((node->parent == NULL) ||
2215 (node->parent->type != XML_ELEMENT_NODE) ||
2216 (node->parent->ns == NULL) ||
2217 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2218 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
2219 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
2220 xmlXIncludeErr(ctxt, node,
2221 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2222 "%s is not the child of an 'include'\n",
2223 XINCLUDE_FALLBACK);
2227 return(0);
2231 * xmlXIncludeDoProcess:
2232 * @ctxt: the XInclude processing context
2233 * @tree: the top of the tree to process
2235 * Implement the XInclude substitution on the XML document @doc
2237 * Returns 0 if no substitution were done, -1 if some processing failed
2238 * or the number of substitutions done.
2240 static int
2241 xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlNodePtr tree) {
2242 xmlXIncludeRefPtr ref;
2243 xmlNodePtr cur;
2244 int ret = 0;
2245 int i, start;
2247 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
2248 return(-1);
2249 if (ctxt == NULL)
2250 return(-1);
2253 * First phase: lookup the elements in the document
2255 start = ctxt->incNr;
2256 cur = tree;
2257 do {
2258 /* TODO: need to work on entities -> stack */
2259 if (xmlXIncludeTestNode(ctxt, cur) == 1) {
2260 ref = xmlXIncludeExpandNode(ctxt, cur);
2262 * Mark direct includes.
2264 if (ref != NULL)
2265 ref->replace = 1;
2266 } else if ((cur->children != NULL) &&
2267 ((cur->type == XML_DOCUMENT_NODE) ||
2268 (cur->type == XML_ELEMENT_NODE))) {
2269 cur = cur->children;
2270 continue;
2272 do {
2273 if (cur == tree)
2274 break;
2275 if (cur->next != NULL) {
2276 cur = cur->next;
2277 break;
2279 cur = cur->parent;
2280 } while (cur != NULL);
2281 } while ((cur != NULL) && (cur != tree));
2284 * Second phase: extend the original document infoset.
2286 for (i = start; i < ctxt->incNr; i++) {
2287 if (ctxt->incTab[i]->replace != 0) {
2288 if ((ctxt->incTab[i]->inc != NULL) ||
2289 (ctxt->incTab[i]->emptyFb != 0)) { /* (empty fallback) */
2290 xmlXIncludeIncludeNode(ctxt, ctxt->incTab[i]);
2292 ctxt->incTab[i]->replace = 0;
2293 } else {
2295 * Ignore includes which were added indirectly, for example
2296 * inside xi:fallback elements.
2298 if (ctxt->incTab[i]->inc != NULL) {
2299 xmlFreeNodeList(ctxt->incTab[i]->inc);
2300 ctxt->incTab[i]->inc = NULL;
2303 ret++;
2306 if (ctxt->isStream) {
2308 * incTab references nodes which will eventually be deleted in
2309 * streaming mode. The table is only required for XPointer
2310 * expressions which aren't allowed in streaming mode.
2312 for (i = 0;i < ctxt->incNr;i++) {
2313 xmlXIncludeFreeRef(ctxt->incTab[i]);
2315 ctxt->incNr = 0;
2318 return(ret);
2322 * xmlXIncludeSetFlags:
2323 * @ctxt: an XInclude processing context
2324 * @flags: a set of xmlParserOption used for parsing XML includes
2326 * Set the flags used for further processing of XML resources.
2328 * Returns 0 in case of success and -1 in case of error.
2331 xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2332 if (ctxt == NULL)
2333 return(-1);
2334 ctxt->parseFlags = flags;
2335 return(0);
2339 * xmlXIncludeSetStreamingMode:
2340 * @ctxt: an XInclude processing context
2341 * @mode: whether streaming mode should be enabled
2343 * In streaming mode, XPointer expressions aren't allowed.
2345 * Returns 0 in case of success and -1 in case of error.
2348 xmlXIncludeSetStreamingMode(xmlXIncludeCtxtPtr ctxt, int mode) {
2349 if (ctxt == NULL)
2350 return(-1);
2351 ctxt->isStream = !!mode;
2352 return(0);
2356 * xmlXIncludeProcessTreeFlagsData:
2357 * @tree: an XML node
2358 * @flags: a set of xmlParserOption used for parsing XML includes
2359 * @data: application data that will be passed to the parser context
2360 * in the _private field of the parser context(s)
2362 * Implement the XInclude substitution on the XML node @tree
2364 * Returns 0 if no substitution were done, -1 if some processing failed
2365 * or the number of substitutions done.
2369 xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
2370 xmlXIncludeCtxtPtr ctxt;
2371 int ret = 0;
2373 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2374 (tree->doc == NULL))
2375 return(-1);
2377 ctxt = xmlXIncludeNewContext(tree->doc);
2378 if (ctxt == NULL)
2379 return(-1);
2380 ctxt->_private = data;
2381 ctxt->base = xmlStrdup((xmlChar *)tree->doc->URL);
2382 xmlXIncludeSetFlags(ctxt, flags);
2383 ret = xmlXIncludeDoProcess(ctxt, tree);
2384 if ((ret >= 0) && (ctxt->nbErrors > 0))
2385 ret = -1;
2387 xmlXIncludeFreeContext(ctxt);
2388 return(ret);
2392 * xmlXIncludeProcessFlagsData:
2393 * @doc: an XML document
2394 * @flags: a set of xmlParserOption used for parsing XML includes
2395 * @data: application data that will be passed to the parser context
2396 * in the _private field of the parser context(s)
2398 * Implement the XInclude substitution on the XML document @doc
2400 * Returns 0 if no substitution were done, -1 if some processing failed
2401 * or the number of substitutions done.
2404 xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
2405 xmlNodePtr tree;
2407 if (doc == NULL)
2408 return(-1);
2409 tree = xmlDocGetRootElement(doc);
2410 if (tree == NULL)
2411 return(-1);
2412 return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
2416 * xmlXIncludeProcessFlags:
2417 * @doc: an XML document
2418 * @flags: a set of xmlParserOption used for parsing XML includes
2420 * Implement the XInclude substitution on the XML document @doc
2422 * Returns 0 if no substitution were done, -1 if some processing failed
2423 * or the number of substitutions done.
2426 xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2427 return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2431 * xmlXIncludeProcess:
2432 * @doc: an XML document
2434 * Implement the XInclude substitution on the XML document @doc
2436 * Returns 0 if no substitution were done, -1 if some processing failed
2437 * or the number of substitutions done.
2440 xmlXIncludeProcess(xmlDocPtr doc) {
2441 return(xmlXIncludeProcessFlags(doc, 0));
2445 * xmlXIncludeProcessTreeFlags:
2446 * @tree: a node in an XML document
2447 * @flags: a set of xmlParserOption used for parsing XML includes
2449 * Implement the XInclude substitution for the given subtree
2451 * Returns 0 if no substitution were done, -1 if some processing failed
2452 * or the number of substitutions done.
2455 xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2456 xmlXIncludeCtxtPtr ctxt;
2457 int ret = 0;
2459 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2460 (tree->doc == NULL))
2461 return(-1);
2462 ctxt = xmlXIncludeNewContext(tree->doc);
2463 if (ctxt == NULL)
2464 return(-1);
2465 ctxt->base = xmlNodeGetBase(tree->doc, tree);
2466 xmlXIncludeSetFlags(ctxt, flags);
2467 ret = xmlXIncludeDoProcess(ctxt, tree);
2468 if ((ret >= 0) && (ctxt->nbErrors > 0))
2469 ret = -1;
2471 xmlXIncludeFreeContext(ctxt);
2472 return(ret);
2476 * xmlXIncludeProcessTree:
2477 * @tree: a node in an XML document
2479 * Implement the XInclude substitution for the given subtree
2481 * Returns 0 if no substitution were done, -1 if some processing failed
2482 * or the number of substitutions done.
2485 xmlXIncludeProcessTree(xmlNodePtr tree) {
2486 return(xmlXIncludeProcessTreeFlags(tree, 0));
2490 * xmlXIncludeProcessNode:
2491 * @ctxt: an existing XInclude context
2492 * @node: a node in an XML document
2494 * Implement the XInclude substitution for the given subtree reusing
2495 * the information and data coming from the given context.
2497 * Returns 0 if no substitution were done, -1 if some processing failed
2498 * or the number of substitutions done.
2501 xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2502 int ret = 0;
2504 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
2505 (node->doc == NULL) || (ctxt == NULL))
2506 return(-1);
2507 ret = xmlXIncludeDoProcess(ctxt, node);
2508 if ((ret >= 0) && (ctxt->nbErrors > 0))
2509 ret = -1;
2510 return(ret);
2513 #else /* !LIBXML_XINCLUDE_ENABLED */
2514 #endif