blob: 21670a9b8506bbaa68c2641ba166bca52248ff89 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xinclude.c : Code to implement XInclude processing
3 *
Daniel Veillarde74d2e12003-12-09 11:35:37 +00004 * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
5 * http://www.w3.org/TR/2003/WD-xinclude-20031110
Owen Taylor3473f882001-02-23 17:55:21 +00006 *
7 * See Copyright for the status of this software.
8 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00009 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000010 */
11
Daniel Veillard34ce8be2002-03-18 19:37:11 +000012#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000013#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000014
Owen Taylor3473f882001-02-23 17:55:21 +000015#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>
Owen Taylor3473f882001-02-23 17:55:21 +000022#include <libxml/xmlerror.h>
Daniel Veillardd076a202002-11-20 13:28:31 +000023#include <libxml/encoding.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000024#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000025
26#ifdef LIBXML_XINCLUDE_ENABLED
27#include <libxml/xinclude.h>
28
Owen Taylor3473f882001-02-23 17:55:21 +000029
Daniel Veillardf4b4f982003-02-13 11:02:08 +000030#define XINCLUDE_MAX_DEPTH 40
31
Daniel Veillard98485322003-08-14 15:44:40 +000032/* #define DEBUG_XINCLUDE */
Daniel Veillard017b1082001-06-21 11:20:21 +000033#ifdef DEBUG_XINCLUDE
34#ifdef LIBXML_DEBUG_ENABLED
35#include <libxml/debugXML.h>
36#endif
37#endif
Owen Taylor3473f882001-02-23 17:55:21 +000038
39/************************************************************************
40 * *
William M. Brack72ee48d2003-12-30 08:30:19 +000041 * XInclude context handling *
Owen Taylor3473f882001-02-23 17:55:21 +000042 * *
43 ************************************************************************/
44
45/*
46 * An XInclude context
47 */
Daniel Veillardedac3c92001-02-26 01:36:19 +000048typedef xmlChar *xmlURL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +000049
50typedef struct _xmlXIncludeRef xmlXIncludeRef;
51typedef xmlXIncludeRef *xmlXIncludeRefPtr;
52struct _xmlXIncludeRef {
William M. Brack72ee48d2003-12-30 08:30:19 +000053 xmlChar *URI; /* the fully resolved resource URL */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000054 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 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +000060 xmlXPathObjectPtr xptr; /* the xpointer if needed */
William M. Brack95af5942004-02-08 04:12:49 +000061 int emptyFb; /* flag to show fallback empty */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000062};
63
Owen Taylor3473f882001-02-23 17:55:21 +000064struct _xmlXIncludeCtxt {
65 xmlDocPtr doc; /* the source document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000066 int incBase; /* the first include for this document */
Owen Taylor3473f882001-02-23 17:55:21 +000067 int incNr; /* number of includes */
68 int incMax; /* size of includes tab */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000069 xmlXIncludeRefPtr *incTab; /* array of included references */
70
Owen Taylor3473f882001-02-23 17:55:21 +000071 int txtNr; /* number of unparsed documents */
72 int txtMax; /* size of unparsed documents tab */
73 xmlNodePtr *txtTab; /* array of unparsed text nodes */
William M. Brack72ee48d2003-12-30 08:30:19 +000074 xmlURL *txturlTab; /* array of unparsed text URLs */
Daniel Veillardd581b7e2003-02-11 18:03:05 +000075
Daniel Veillardf4b4f982003-02-13 11:02:08 +000076 xmlChar * url; /* the current URL processed */
William M. Brack72ee48d2003-12-30 08:30:19 +000077 int urlNr; /* number of URLs stacked */
78 int urlMax; /* size of URL stack */
79 xmlChar * *urlTab; /* URL stack */
Daniel Veillardf4b4f982003-02-13 11:02:08 +000080
Daniel Veillardd581b7e2003-02-11 18:03:05 +000081 int nbErrors; /* the number of errors detected */
Daniel Veillardb5fa0202003-12-08 17:41:29 +000082 int legacy; /* using XINCLUDE_OLD_NS */
Daniel Veillarde74d2e12003-12-09 11:35:37 +000083 int parseFlags; /* the flags used for parsing XML documents */
William M. Brackf7789b12004-06-07 08:57:27 +000084 xmlChar * base; /* the current xml:base */
Owen Taylor3473f882001-02-23 17:55:21 +000085};
86
Daniel Veillardd16df9f2001-05-23 13:44:21 +000087static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +000088xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree);
Owen Taylor3473f882001-02-23 17:55:21 +000089
Daniel Veillard98485322003-08-14 15:44:40 +000090
Daniel Veillardcd6ff282003-10-08 22:38:13 +000091/************************************************************************
92 * *
Daniel Veillard69d2c172003-10-09 11:46:07 +000093 * XInclude error handler *
Daniel Veillardcd6ff282003-10-08 22:38:13 +000094 * *
95 ************************************************************************/
96
Daniel Veillard98485322003-08-14 15:44:40 +000097/**
Daniel Veillardcd6ff282003-10-08 22:38:13 +000098 * xmlXIncludeErrMemory:
William M. Brack72ee48d2003-12-30 08:30:19 +000099 * @extra: extra information
Daniel Veillard98485322003-08-14 15:44:40 +0000100 *
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000101 * Handle an out of memory condition
Daniel Veillard98485322003-08-14 15:44:40 +0000102 */
103static void
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000104xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
105 const char *extra)
Daniel Veillard98485322003-08-14 15:44:40 +0000106{
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000107 if (ctxt != NULL)
108 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000109 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000110 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
111 extra, NULL, NULL, 0, 0,
112 "Memory allocation failed : %s\n", extra);
113}
Daniel Veillard98485322003-08-14 15:44:40 +0000114
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000115/**
116 * xmlXIncludeErr:
117 * @ctxt: the XInclude context
118 * @node: the context node
119 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000120 * @extra: extra information
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000121 *
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000122 * Handle an XInclude error
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000123 */
124static void
125xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
126 const char *msg, const xmlChar *extra)
127{
128 if (ctxt != NULL)
129 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000130 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000131 error, XML_ERR_ERROR, NULL, 0,
132 (const char *) extra, NULL, NULL, 0, 0,
133 msg, (const char *) extra);
Daniel Veillard98485322003-08-14 15:44:40 +0000134}
135
Daniel Veillardf54cd532004-02-25 11:52:31 +0000136#if 0
Owen Taylor3473f882001-02-23 17:55:21 +0000137/**
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000138 * xmlXIncludeWarn:
139 * @ctxt: the XInclude context
140 * @node: the context node
141 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000142 * @extra: extra information
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000143 *
144 * Emit an XInclude warning.
145 */
146static void
147xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
148 const char *msg, const xmlChar *extra)
149{
150 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
151 error, XML_ERR_WARNING, NULL, 0,
152 (const char *) extra, NULL, NULL, 0, 0,
153 msg, (const char *) extra);
154}
Daniel Veillardf54cd532004-02-25 11:52:31 +0000155#endif
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000156
157/**
158 * xmlXIncludeGetProp:
159 * @ctxt: the XInclude context
160 * @cur: the node
161 * @name: the attribute name
162 *
163 * Get an XInclude attribute
164 *
165 * Returns the value (to be freed) or NULL if not found
166 */
167static xmlChar *
168xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
169 const xmlChar *name) {
170 xmlChar *ret;
171
172 ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
173 if (ret != NULL)
174 return(ret);
175 if (ctxt->legacy != 0) {
176 ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
177 if (ret != NULL)
178 return(ret);
179 }
180 ret = xmlGetProp(cur, name);
181 return(ret);
182}
183/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000184 * xmlXIncludeFreeRef:
185 * @ref: the XInclude reference
186 *
187 * Free an XInclude reference
188 */
189static void
190xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
191 if (ref == NULL)
192 return;
193#ifdef DEBUG_XINCLUDE
194 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
195#endif
196 if (ref->doc != NULL) {
197#ifdef DEBUG_XINCLUDE
198 xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
199#endif
200 xmlFreeDoc(ref->doc);
201 }
202 if (ref->URI != NULL)
203 xmlFree(ref->URI);
204 if (ref->fragment != NULL)
205 xmlFree(ref->fragment);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000206 if (ref->xptr != NULL)
207 xmlXPathFreeObject(ref->xptr);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000208 xmlFree(ref);
209}
210
211/**
212 * xmlXIncludeNewRef:
213 * @ctxt: the XInclude context
214 * @URI: the resource URI
215 *
216 * Creates a new reference within an XInclude context
217 *
218 * Returns the new set
219 */
220static xmlXIncludeRefPtr
221xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
222 xmlNodePtr ref) {
223 xmlXIncludeRefPtr ret;
224
225#ifdef DEBUG_XINCLUDE
226 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
227#endif
228 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000229 if (ret == NULL) {
230 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000231 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000232 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000233 memset(ret, 0, sizeof(xmlXIncludeRef));
234 if (URI == NULL)
235 ret->URI = NULL;
236 else
237 ret->URI = xmlStrdup(URI);
238 ret->fragment = NULL;
239 ret->ref = ref;
Daniel Veillard24505b02005-07-28 23:49:35 +0000240 ret->doc = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000241 ret->count = 0;
242 ret->xml = 0;
243 ret->inc = NULL;
244 if (ctxt->incMax == 0) {
245 ctxt->incMax = 4;
246 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
247 sizeof(ctxt->incTab[0]));
248 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000249 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000250 xmlXIncludeFreeRef(ret);
251 return(NULL);
252 }
253 }
254 if (ctxt->incNr >= ctxt->incMax) {
255 ctxt->incMax *= 2;
256 ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
257 ctxt->incMax * sizeof(ctxt->incTab[0]));
258 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000259 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000260 xmlXIncludeFreeRef(ret);
261 return(NULL);
262 }
263 }
264 ctxt->incTab[ctxt->incNr++] = ret;
265 return(ret);
266}
267
268/**
Owen Taylor3473f882001-02-23 17:55:21 +0000269 * xmlXIncludeNewContext:
270 * @doc: an XML Document
271 *
272 * Creates a new XInclude context
273 *
274 * Returns the new set
275 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000276xmlXIncludeCtxtPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000277xmlXIncludeNewContext(xmlDocPtr doc) {
278 xmlXIncludeCtxtPtr ret;
279
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000280#ifdef DEBUG_XINCLUDE
281 xmlGenericError(xmlGenericErrorContext, "New context\n");
282#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000283 if (doc == NULL)
284 return(NULL);
285 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000286 if (ret == NULL) {
287 xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
288 "creating XInclude context");
Owen Taylor3473f882001-02-23 17:55:21 +0000289 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000290 }
Owen Taylor3473f882001-02-23 17:55:21 +0000291 memset(ret, 0, sizeof(xmlXIncludeCtxt));
292 ret->doc = doc;
293 ret->incNr = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000294 ret->incBase = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000295 ret->incMax = 0;
296 ret->incTab = NULL;
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000297 ret->nbErrors = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000298 return(ret);
299}
300
301/**
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000302 * xmlXIncludeURLPush:
303 * @ctxt: the parser context
304 * @value: the url
305 *
306 * Pushes a new url on top of the url stack
307 *
308 * Returns -1 in case of error, the index in the stack otherwise
309 */
310static int
311xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
312 const xmlChar *value)
313{
314 if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000315 xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION,
316 "detected a recursion in %s\n", value);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000317 return(-1);
318 }
319 if (ctxt->urlTab == NULL) {
320 ctxt->urlMax = 4;
321 ctxt->urlNr = 0;
322 ctxt->urlTab = (xmlChar * *) xmlMalloc(
323 ctxt->urlMax * sizeof(ctxt->urlTab[0]));
324 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000325 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000326 return (-1);
327 }
328 }
329 if (ctxt->urlNr >= ctxt->urlMax) {
330 ctxt->urlMax *= 2;
331 ctxt->urlTab =
332 (xmlChar * *) xmlRealloc(ctxt->urlTab,
333 ctxt->urlMax *
334 sizeof(ctxt->urlTab[0]));
335 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000336 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000337 return (-1);
338 }
339 }
340 ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
341 return (ctxt->urlNr++);
342}
343
344/**
345 * xmlXIncludeURLPop:
346 * @ctxt: the parser context
347 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000348 * Pops the top URL from the URL stack
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000349 */
350static void
351xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
352{
353 xmlChar * ret;
354
355 if (ctxt->urlNr <= 0)
356 return;
357 ctxt->urlNr--;
358 if (ctxt->urlNr > 0)
359 ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
360 else
361 ctxt->url = NULL;
362 ret = ctxt->urlTab[ctxt->urlNr];
Daniel Veillard24505b02005-07-28 23:49:35 +0000363 ctxt->urlTab[ctxt->urlNr] = NULL;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000364 if (ret != NULL)
365 xmlFree(ret);
366}
367
368/**
Owen Taylor3473f882001-02-23 17:55:21 +0000369 * xmlXIncludeFreeContext:
370 * @ctxt: the XInclude context
371 *
372 * Free an XInclude context
373 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000374void
Owen Taylor3473f882001-02-23 17:55:21 +0000375xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
376 int i;
377
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000378#ifdef DEBUG_XINCLUDE
379 xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
380#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000381 if (ctxt == NULL)
382 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000383 while (ctxt->urlNr > 0)
384 xmlXIncludeURLPop(ctxt);
385 if (ctxt->urlTab != NULL)
386 xmlFree(ctxt->urlTab);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000387 for (i = 0;i < ctxt->incNr;i++) {
388 if (ctxt->incTab[i] != NULL)
389 xmlXIncludeFreeRef(ctxt->incTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +0000390 }
Daniel Veillard11ce4002006-03-10 00:36:23 +0000391 if (ctxt->txturlTab != NULL) {
392 for (i = 0;i < ctxt->txtNr;i++) {
393 if (ctxt->txturlTab[i] != NULL)
394 xmlFree(ctxt->txturlTab[i]);
395 }
Owen Taylor3473f882001-02-23 17:55:21 +0000396 }
397 if (ctxt->incTab != NULL)
398 xmlFree(ctxt->incTab);
Owen Taylor3473f882001-02-23 17:55:21 +0000399 if (ctxt->txtTab != NULL)
400 xmlFree(ctxt->txtTab);
401 if (ctxt->txturlTab != NULL)
402 xmlFree(ctxt->txturlTab);
Daniel Veillardce244ad2004-11-05 10:03:46 +0000403 if (ctxt->base != NULL) {
404 xmlFree(ctxt->base);
405 }
Owen Taylor3473f882001-02-23 17:55:21 +0000406 xmlFree(ctxt);
407}
408
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000409/**
Daniel Veillard98485322003-08-14 15:44:40 +0000410 * xmlXIncludeParseFile:
411 * @ctxt: the XInclude context
412 * @URL: the URL or file path
413 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000414 * parse a document for XInclude
Daniel Veillard98485322003-08-14 15:44:40 +0000415 */
416static xmlDocPtr
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000417xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
Daniel Veillard98485322003-08-14 15:44:40 +0000418 xmlDocPtr ret;
419 xmlParserCtxtPtr pctxt;
420 char *directory = NULL;
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000421 xmlParserInputPtr inputStream;
Daniel Veillard98485322003-08-14 15:44:40 +0000422
423 xmlInitParser();
424
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000425 pctxt = xmlNewParserCtxt();
Daniel Veillard98485322003-08-14 15:44:40 +0000426 if (pctxt == NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000427 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
Daniel Veillard98485322003-08-14 15:44:40 +0000428 return(NULL);
429 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000430 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000431 * try to ensure that new documents included are actually
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000432 * built with the same dictionary as the including document.
433 */
434 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL) &&
435 (pctxt->dict != NULL)) {
436 xmlDictFree(pctxt->dict);
437 pctxt->dict = ctxt->doc->dict;
438 xmlDictReference(pctxt->dict);
439 }
440
441 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
442
443 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
444 if (inputStream == NULL) {
445 xmlFreeParserCtxt(pctxt);
446 return(NULL);
447 }
448
449 inputPush(pctxt, inputStream);
Daniel Veillard98485322003-08-14 15:44:40 +0000450
451 if ((pctxt->directory == NULL) && (directory == NULL))
452 directory = xmlParserGetDirectory(URL);
453 if ((pctxt->directory == NULL) && (directory != NULL))
454 pctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
455
William M. Bracka22da292005-02-12 01:08:22 +0000456 pctxt->loadsubset |= XML_DETECT_IDS;
Daniel Veillard98485322003-08-14 15:44:40 +0000457
458 xmlParseDocument(pctxt);
459
William M. Brack1ff42132003-12-31 14:05:15 +0000460 if (pctxt->wellFormed) {
Daniel Veillard98485322003-08-14 15:44:40 +0000461 ret = pctxt->myDoc;
William M. Brack1ff42132003-12-31 14:05:15 +0000462 }
Daniel Veillard98485322003-08-14 15:44:40 +0000463 else {
464 ret = NULL;
Daniel Veillard500a1de2004-03-22 15:22:58 +0000465 if (pctxt->myDoc != NULL)
Daniel Veillard4773df22004-01-23 13:15:13 +0000466 xmlFreeDoc(pctxt->myDoc);
Daniel Veillard98485322003-08-14 15:44:40 +0000467 pctxt->myDoc = NULL;
468 }
469 xmlFreeParserCtxt(pctxt);
470
471 return(ret);
472}
473
474/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000475 * xmlXIncludeAddNode:
476 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000477 * @cur: the new node
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000478 *
479 * Add a new node to process to an XInclude context
480 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000481static int
482xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
483 xmlXIncludeRefPtr ref;
484 xmlURIPtr uri;
485 xmlChar *URL;
486 xmlChar *fragment = NULL;
487 xmlChar *href;
488 xmlChar *parse;
489 xmlChar *base;
490 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000491 int xml = 1, i; /* default Issue 64 */
492 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000493
494
495 if (ctxt == NULL)
496 return(-1);
497 if (cur == NULL)
498 return(-1);
499
500#ifdef DEBUG_XINCLUDE
501 xmlGenericError(xmlGenericErrorContext, "Add node\n");
502#endif
503 /*
504 * read the attributes
505 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000506 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000507 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000508 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
509 if (href == NULL)
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000510 return(-1);
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000511 local = 1;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000512 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000513 if (href[0] == '#')
514 local = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000515 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000516 if (parse != NULL) {
517 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
518 xml = 1;
519 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
520 xml = 0;
521 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000522 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
523 "invalid value %s for 'parse'\n", parse);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000524 if (href != NULL)
525 xmlFree(href);
526 if (parse != NULL)
527 xmlFree(parse);
528 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000529 }
530 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000531
532 /*
533 * compute the URI
534 */
535 base = xmlNodeGetBase(ctxt->doc, cur);
536 if (base == NULL) {
537 URI = xmlBuildURI(href, ctxt->doc->URL);
538 } else {
539 URI = xmlBuildURI(href, base);
540 }
541 if (URI == NULL) {
542 xmlChar *escbase;
543 xmlChar *eschref;
544 /*
545 * Some escaping may be needed
546 */
547 escbase = xmlURIEscape(base);
548 eschref = xmlURIEscape(href);
549 URI = xmlBuildURI(eschref, escbase);
550 if (escbase != NULL)
551 xmlFree(escbase);
552 if (eschref != NULL)
553 xmlFree(eschref);
554 }
555 if (parse != NULL)
556 xmlFree(parse);
557 if (href != NULL)
558 xmlFree(href);
559 if (base != NULL)
560 xmlFree(base);
561 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000562 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
563 "failed build URL\n", NULL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000564 return(-1);
565 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000566 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000567
568 /*
569 * Check the URL and remove any fragment identifier
570 */
571 uri = xmlParseURI((const char *)URI);
572 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000573 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
574 "invalid value URI %s\n", URI);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000575 if (fragment != NULL)
576 xmlFree(fragment);
577 xmlFree(URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000578 return(-1);
579 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000580
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000581 if (uri->fragment != NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000582 if (ctxt->legacy != 0) {
583 if (fragment == NULL) {
584 fragment = (xmlChar *) uri->fragment;
585 } else {
586 xmlFree(uri->fragment);
587 }
588 } else {
589 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
590 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
591 URI);
592 if (fragment != NULL)
593 xmlFree(fragment);
594 xmlFreeURI(uri);
595 xmlFree(URI);
596 return(-1);
597 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000598 uri->fragment = NULL;
599 }
600 URL = xmlSaveUri(uri);
601 xmlFreeURI(uri);
602 xmlFree(URI);
603 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000604 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
605 "invalid value URI %s\n", URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000606 if (fragment != NULL)
607 xmlFree(fragment);
608 return(-1);
609 }
610
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000611 /*
612 * Check the URL against the stack for recursions
613 */
614 if (!local) {
615 for (i = 0;i < ctxt->urlNr;i++) {
616 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000617 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
618 "detected a recursion in %s\n", URL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000619 return(-1);
620 }
621 }
622 }
623
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000624 ref = xmlXIncludeNewRef(ctxt, URL, cur);
625 if (ref == NULL) {
626 return(-1);
627 }
628 ref->fragment = fragment;
629 ref->doc = NULL;
630 ref->xml = xml;
631 ref->count = 1;
632 xmlFree(URL);
633 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000634}
635
636/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000637 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000638 * @ctxt: the XInclude context
639 * @doc: the new document
640 * @url: the associated URL
641 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000642 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000643 */
644static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000645xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000646 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000647 xmlXIncludeCtxtPtr newctxt;
648 int i;
649
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000650 /*
651 * Avoid recursion in already substitued resources
652 for (i = 0;i < ctxt->urlNr;i++) {
653 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
654 return;
655 }
656 */
657
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000658#ifdef DEBUG_XINCLUDE
659 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
660#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000661 /*
662 * Handle recursion here.
663 */
664
665 newctxt = xmlXIncludeNewContext(doc);
666 if (newctxt != NULL) {
667 /*
668 * Copy the existing document set
669 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000670 newctxt->incMax = ctxt->incMax;
671 newctxt->incNr = ctxt->incNr;
672 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
673 sizeof(newctxt->incTab[0]));
674 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000675 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000676 xmlFree(newctxt);
677 return;
678 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000679 /*
680 * copy the urlTab
681 */
682 newctxt->urlMax = ctxt->urlMax;
683 newctxt->urlNr = ctxt->urlNr;
684 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000685
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000686 /*
William M. Brackf7789b12004-06-07 08:57:27 +0000687 * Inherit the existing base
688 */
Daniel Veillardce244ad2004-11-05 10:03:46 +0000689 newctxt->base = xmlStrdup(ctxt->base);
William M. Brackf7789b12004-06-07 08:57:27 +0000690
691 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000692 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000693 */
694 newctxt->incBase = ctxt->incNr;
695 for (i = 0;i < ctxt->incNr;i++) {
696 newctxt->incTab[i] = ctxt->incTab[i];
697 newctxt->incTab[i]->count++; /* prevent the recursion from
698 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000699 }
William M. Bracka11e4832004-03-07 11:03:43 +0000700 /*
701 * The new context should also inherit the Parse Flags
702 * (bug 132597)
703 */
704 newctxt->parseFlags = ctxt->parseFlags;
Daniel Veillard8edf1c52003-07-22 20:52:14 +0000705 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000706 for (i = 0;i < ctxt->incNr;i++) {
707 newctxt->incTab[i]->count--;
708 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000709 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000710
711 /* urlTab may have been reallocated */
712 ctxt->urlTab = newctxt->urlTab;
713 ctxt->urlMax = newctxt->urlMax;
714
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000715 newctxt->urlMax = 0;
716 newctxt->urlNr = 0;
717 newctxt->urlTab = NULL;
718
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000719 xmlXIncludeFreeContext(newctxt);
720 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000721#ifdef DEBUG_XINCLUDE
722 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
723#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000724}
725
726/**
727 * xmlXIncludeAddTxt:
728 * @ctxt: the XInclude context
729 * @txt: the new text node
730 * @url: the associated URL
731 *
732 * Add a new txtument to the list
733 */
734static void
735xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000736#ifdef DEBUG_XINCLUDE
737 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
738#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000739 if (ctxt->txtMax == 0) {
740 ctxt->txtMax = 4;
741 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
742 sizeof(ctxt->txtTab[0]));
743 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000744 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000745 return;
746 }
747 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
748 sizeof(ctxt->txturlTab[0]));
749 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000750 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000751 return;
752 }
753 }
754 if (ctxt->txtNr >= ctxt->txtMax) {
755 ctxt->txtMax *= 2;
756 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
757 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
758 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000759 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000760 return;
761 }
762 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000763 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000764 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000765 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000766 return;
767 }
768 }
769 ctxt->txtTab[ctxt->txtNr] = txt;
770 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
771 ctxt->txtNr++;
772}
773
Owen Taylor3473f882001-02-23 17:55:21 +0000774/************************************************************************
775 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000776 * Node copy with specific semantic *
777 * *
778 ************************************************************************/
779
780/**
781 * xmlXIncludeCopyNode:
782 * @ctxt: the XInclude context
783 * @target: the document target
784 * @source: the document source
785 * @elem: the element
786 *
787 * Make a copy of the node while preserving the XInclude semantic
788 * of the Infoset copy
789 */
790static xmlNodePtr
791xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
792 xmlDocPtr source, xmlNodePtr elem) {
793 xmlNodePtr result = NULL;
794
795 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
796 (elem == NULL))
797 return(NULL);
798 if (elem->type == XML_DTD_NODE)
799 return(NULL);
800 result = xmlDocCopyNode(elem, target, 1);
801 return(result);
802}
803
804/**
805 * xmlXIncludeCopyNodeList:
806 * @ctxt: the XInclude context
807 * @target: the document target
808 * @source: the document source
809 * @elem: the element list
810 *
811 * Make a copy of the node list while preserving the XInclude semantic
812 * of the Infoset copy
813 */
814static xmlNodePtr
815xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
816 xmlDocPtr source, xmlNodePtr elem) {
817 xmlNodePtr cur, res, result = NULL, last = NULL;
818
819 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
820 (elem == NULL))
821 return(NULL);
822 cur = elem;
823 while (cur != NULL) {
824 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
825 if (res != NULL) {
826 if (result == NULL) {
827 result = last = res;
828 } else {
829 last->next = res;
830 res->prev = last;
831 last = res;
832 }
833 }
834 cur = cur->next;
835 }
836 return(result);
837}
838
839/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000840 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000841 * @cur: the node
842 * @no: the child number
843 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000844 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000845 */
846static xmlNodePtr
847xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
848 int i;
849 if (cur == NULL)
850 return(cur);
851 cur = cur->children;
852 for (i = 0;i <= no;cur = cur->next) {
853 if (cur == NULL)
854 return(cur);
855 if ((cur->type == XML_ELEMENT_NODE) ||
856 (cur->type == XML_DOCUMENT_NODE) ||
857 (cur->type == XML_HTML_DOCUMENT_NODE)) {
858 i++;
859 if (i == no)
860 break;
861 }
862 }
863 return(cur);
864}
865
William M. Brackf7eb7942003-12-31 07:59:17 +0000866xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000867/**
868 * xmlXIncludeCopyRange:
869 * @ctxt: the XInclude context
870 * @target: the document target
871 * @source: the document source
872 * @obj: the XPointer result from the evaluation.
873 *
874 * Build a node list tree copy of the XPointer result.
875 *
876 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000877 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000878 */
879static xmlNodePtr
880xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
881 xmlDocPtr source, xmlXPathObjectPtr range) {
882 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000883 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
884 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000885 /* pointers to traversal nodes */
886 xmlNodePtr start, cur, end;
887 int index1, index2;
William M. Brack6bdacd72004-02-07 08:53:23 +0000888 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000889
890 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
891 (range == NULL))
892 return(NULL);
893 if (range->type != XPATH_RANGE)
894 return(NULL);
895 start = (xmlNodePtr) range->user;
896
897 if (start == NULL)
898 return(NULL);
899 end = range->user2;
900 if (end == NULL)
901 return(xmlDocCopyNode(start, target, 1));
902
903 cur = start;
904 index1 = range->index;
905 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000906 /*
907 * level is depth of the current node under consideration
908 * list is the pointer to the root of the output tree
909 * listParent is a pointer to the parent of output tree (within
910 the included file) in case we need to add another level
911 * last is a pointer to the last node added to the output tree
912 * lastLevel is the depth of last (relative to the root)
913 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000914 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000915 /*
916 * Check if our output tree needs a parent
917 */
918 if (level < 0) {
919 while (level < 0) {
William M. Brack57e9e912004-03-09 16:19:02 +0000920 /* copy must include namespaces and properties */
921 tmp2 = xmlDocCopyNode(listParent, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000922 xmlAddChild(tmp2, list);
923 list = tmp2;
924 listParent = listParent->parent;
925 level++;
926 }
927 last = list;
928 lastLevel = 0;
929 }
930 /*
931 * Check whether we need to change our insertion point
932 */
933 while (level < lastLevel) {
934 last = last->parent;
935 lastLevel --;
936 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000937 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000938 if (cur->type == XML_TEXT_NODE) {
939 const xmlChar *content = cur->content;
940 int len;
941
942 if (content == NULL) {
943 tmp = xmlNewTextLen(NULL, 0);
944 } else {
945 len = index2;
946 if ((cur == start) && (index1 > 1)) {
947 content += (index1 - 1);
948 len -= (index1 - 1);
949 index1 = 0;
950 } else {
951 len = index2;
952 }
953 tmp = xmlNewTextLen(content, len);
954 }
955 /* single sub text node selection */
956 if (list == NULL)
957 return(tmp);
958 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +0000959 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000960 xmlAddNextSibling(last, tmp);
961 else
William M. Brackf7eb7942003-12-31 07:59:17 +0000962 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000963 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +0000964 } else { /* ending node not a text node */
William M. Brack6bdacd72004-02-07 08:53:23 +0000965 endLevel = level; /* remember the level of the end node */
966 endFlag = 1;
William M. Brack57e9e912004-03-09 16:19:02 +0000967 /* last node - need to take care of properties + namespaces */
968 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000969 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000970 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000971 listParent = cur->parent;
972 } else {
973 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000974 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +0000975 else {
976 xmlAddChild(last, tmp);
977 lastLevel = level;
978 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000979 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000980 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000981
982 if (index2 > 1) {
983 end = xmlXIncludeGetNthChild(cur, index2 - 1);
984 index2 = 0;
985 }
986 if ((cur == start) && (index1 > 1)) {
987 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
988 index1 = 0;
William M. Brack6bdacd72004-02-07 08:53:23 +0000989 } else {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000990 cur = cur->children;
991 }
William M. Brack6bdacd72004-02-07 08:53:23 +0000992 level++; /* increment level to show change */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000993 /*
994 * Now gather the remaining nodes from cur to end
995 */
William M. Brack6bdacd72004-02-07 08:53:23 +0000996 continue; /* while */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000997 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000998 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000999 if ((cur->type == XML_TEXT_NODE) ||
1000 (cur->type == XML_CDATA_SECTION_NODE)) {
1001 const xmlChar *content = cur->content;
1002
1003 if (content == NULL) {
1004 tmp = xmlNewTextLen(NULL, 0);
1005 } else {
1006 if (index1 > 1) {
1007 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +00001008 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001009 }
1010 tmp = xmlNewText(content);
1011 }
1012 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001013 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001014 } else { /* Not text node */
William M. Brack57e9e912004-03-09 16:19:02 +00001015 /*
1016 * start of the range - need to take care of
1017 * properties and namespaces
1018 */
1019 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +00001020 list = last = tmp;
1021 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001022 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001023 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001024 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001025 index1 = 0;
1026 /*
1027 * Now gather the remaining nodes from cur to end
1028 */
1029 continue; /* while */
1030 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001031 }
1032 } else {
1033 tmp = NULL;
1034 switch (cur->type) {
1035 case XML_DTD_NODE:
1036 case XML_ELEMENT_DECL:
1037 case XML_ATTRIBUTE_DECL:
1038 case XML_ENTITY_NODE:
1039 /* Do not copy DTD informations */
1040 break;
1041 case XML_ENTITY_DECL:
1042 /* handle crossing entities -> stack needed */
1043 break;
1044 case XML_XINCLUDE_START:
1045 case XML_XINCLUDE_END:
1046 /* don't consider it part of the tree content */
1047 break;
1048 case XML_ATTRIBUTE_NODE:
1049 /* Humm, should not happen ! */
1050 break;
1051 default:
William M. Brack57e9e912004-03-09 16:19:02 +00001052 /*
1053 * Middle of the range - need to take care of
1054 * properties and namespaces
1055 */
1056 tmp = xmlDocCopyNode(cur, target, 2);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001057 break;
1058 }
1059 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001060 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001061 xmlAddNextSibling(last, tmp);
1062 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001063 xmlAddChild(last, tmp);
1064 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001065 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001066 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001067 }
1068 }
1069 /*
1070 * Skip to next node in document order
1071 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001072 cur = xmlXPtrAdvanceNode(cur, &level);
William M. Brack6bdacd72004-02-07 08:53:23 +00001073 if (endFlag && (level >= endLevel))
1074 break;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001075 }
1076 return(list);
1077}
1078
1079/**
1080 * xmlXIncludeBuildNodeList:
1081 * @ctxt: the XInclude context
1082 * @target: the document target
1083 * @source: the document source
1084 * @obj: the XPointer result from the evaluation.
1085 *
1086 * Build a node list tree copy of the XPointer result.
1087 * This will drop Attributes and Namespace declarations.
1088 *
1089 * Returns an xmlNodePtr list or NULL.
1090 * the caller has to free the node tree.
1091 */
1092static xmlNodePtr
1093xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1094 xmlDocPtr source, xmlXPathObjectPtr obj) {
1095 xmlNodePtr list = NULL, last = NULL;
1096 int i;
1097
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001098 if (source == NULL)
1099 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001100 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1101 (obj == NULL))
1102 return(NULL);
1103 switch (obj->type) {
1104 case XPATH_NODESET: {
1105 xmlNodeSetPtr set = obj->nodesetval;
1106 if (set == NULL)
1107 return(NULL);
1108 for (i = 0;i < set->nodeNr;i++) {
1109 if (set->nodeTab[i] == NULL)
1110 continue;
1111 switch (set->nodeTab[i]->type) {
1112 case XML_TEXT_NODE:
1113 case XML_CDATA_SECTION_NODE:
1114 case XML_ELEMENT_NODE:
1115 case XML_ENTITY_REF_NODE:
1116 case XML_ENTITY_NODE:
1117 case XML_PI_NODE:
1118 case XML_COMMENT_NODE:
1119 case XML_DOCUMENT_NODE:
1120 case XML_HTML_DOCUMENT_NODE:
1121#ifdef LIBXML_DOCB_ENABLED
1122 case XML_DOCB_DOCUMENT_NODE:
1123#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001124 case XML_XINCLUDE_END:
1125 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001126 case XML_XINCLUDE_START: {
1127 xmlNodePtr tmp, cur = set->nodeTab[i];
1128
1129 cur = cur->next;
1130 while (cur != NULL) {
1131 switch(cur->type) {
1132 case XML_TEXT_NODE:
1133 case XML_CDATA_SECTION_NODE:
1134 case XML_ELEMENT_NODE:
1135 case XML_ENTITY_REF_NODE:
1136 case XML_ENTITY_NODE:
1137 case XML_PI_NODE:
1138 case XML_COMMENT_NODE:
1139 tmp = xmlXIncludeCopyNode(ctxt, target,
1140 source, cur);
1141 if (last == NULL) {
1142 list = last = tmp;
1143 } else {
1144 xmlAddNextSibling(last, tmp);
1145 last = tmp;
1146 }
1147 cur = cur->next;
1148 continue;
1149 default:
1150 break;
1151 }
1152 break;
1153 }
1154 continue;
1155 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001156 case XML_ATTRIBUTE_NODE:
1157 case XML_NAMESPACE_DECL:
1158 case XML_DOCUMENT_TYPE_NODE:
1159 case XML_DOCUMENT_FRAG_NODE:
1160 case XML_NOTATION_NODE:
1161 case XML_DTD_NODE:
1162 case XML_ELEMENT_DECL:
1163 case XML_ATTRIBUTE_DECL:
1164 case XML_ENTITY_DECL:
1165 continue; /* for */
1166 }
1167 if (last == NULL)
1168 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1169 set->nodeTab[i]);
1170 else {
1171 xmlAddNextSibling(last,
1172 xmlXIncludeCopyNode(ctxt, target, source,
1173 set->nodeTab[i]));
1174 if (last->next != NULL)
1175 last = last->next;
1176 }
1177 }
1178 break;
1179 }
1180 case XPATH_LOCATIONSET: {
1181 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1182 if (set == NULL)
1183 return(NULL);
1184 for (i = 0;i < set->locNr;i++) {
1185 if (last == NULL)
1186 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1187 set->locTab[i]);
1188 else
1189 xmlAddNextSibling(last,
1190 xmlXIncludeCopyXPointer(ctxt, target, source,
1191 set->locTab[i]));
1192 if (last != NULL) {
1193 while (last->next != NULL)
1194 last = last->next;
1195 }
1196 }
1197 break;
1198 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001199#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001200 case XPATH_RANGE:
1201 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001202#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001203 case XPATH_POINT:
1204 /* points are ignored in XInclude */
1205 break;
1206 default:
1207 break;
1208 }
1209 return(list);
1210}
1211/************************************************************************
1212 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001213 * XInclude I/O handling *
1214 * *
1215 ************************************************************************/
1216
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001217typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1218typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1219struct _xmlXIncludeMergeData {
1220 xmlDocPtr doc;
1221 xmlXIncludeCtxtPtr ctxt;
1222};
1223
Owen Taylor3473f882001-02-23 17:55:21 +00001224/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001225 * xmlXIncludeMergeOneEntity:
1226 * @ent: the entity
1227 * @doc: the including doc
1228 * @nr: the entity name
1229 *
1230 * Inplements the merge of one entity
1231 */
1232static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001233xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001234 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001235 xmlEntityPtr ret, prev;
1236 xmlDocPtr doc;
1237 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001238
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001239 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001240 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001241 ctxt = data->ctxt;
1242 doc = data->doc;
1243 if ((ctxt == NULL) || (doc == NULL))
1244 return;
1245 switch (ent->etype) {
1246 case XML_INTERNAL_PARAMETER_ENTITY:
1247 case XML_EXTERNAL_PARAMETER_ENTITY:
1248 case XML_INTERNAL_PREDEFINED_ENTITY:
1249 return;
1250 case XML_INTERNAL_GENERAL_ENTITY:
1251 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1252 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1253 break;
1254 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001255 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1256 ent->SystemID, ent->content);
1257 if (ret != NULL) {
1258 if (ent->URI != NULL)
1259 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001260 } else {
1261 prev = xmlGetDocEntity(doc, ent->name);
1262 if (prev != NULL) {
1263 if (ent->etype != prev->etype)
1264 goto error;
1265
1266 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1267 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1268 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001269 } else if ((ent->ExternalID != NULL) &&
1270 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001271 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1272 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001273 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1274 if (!xmlStrEqual(ent->content, prev->content))
1275 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001276 } else {
1277 goto error;
1278 }
1279
1280 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001281 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001282 return;
1283error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001284 switch (ent->etype) {
1285 case XML_INTERNAL_PARAMETER_ENTITY:
1286 case XML_EXTERNAL_PARAMETER_ENTITY:
1287 case XML_INTERNAL_PREDEFINED_ENTITY:
1288 case XML_INTERNAL_GENERAL_ENTITY:
1289 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1290 return;
1291 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1292 break;
1293 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001294 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1295 "mismatch in redefinition of entity %s\n",
1296 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001297}
1298
1299/**
1300 * xmlXIncludeMergeEntities:
1301 * @ctxt: an XInclude context
1302 * @doc: the including doc
1303 * @from: the included doc
1304 *
1305 * Inplements the entity merge
1306 *
1307 * Returns 0 if merge succeeded, -1 if some processing failed
1308 */
1309static int
1310xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1311 xmlDocPtr from) {
1312 xmlNodePtr cur;
1313 xmlDtdPtr target, source;
1314
1315 if (ctxt == NULL)
1316 return(-1);
1317
1318 if ((from == NULL) || (from->intSubset == NULL))
1319 return(0);
1320
1321 target = doc->intSubset;
1322 if (target == NULL) {
1323 cur = xmlDocGetRootElement(doc);
1324 if (cur == NULL)
1325 return(-1);
1326 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1327 if (target == NULL)
1328 return(-1);
1329 }
1330
1331 source = from->intSubset;
1332 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001333 xmlXIncludeMergeData data;
1334
1335 data.ctxt = ctxt;
1336 data.doc = doc;
1337
Daniel Veillard4287c572003-02-04 22:48:53 +00001338 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001339 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001340 }
1341 source = from->extSubset;
1342 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001343 xmlXIncludeMergeData data;
1344
1345 data.ctxt = ctxt;
1346 data.doc = doc;
1347
Daniel Veillard4287c572003-02-04 22:48:53 +00001348 /*
1349 * don't duplicate existing stuff when external subsets are the same
1350 */
1351 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1352 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1353 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001354 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001355 }
1356 }
1357 return(0);
1358}
1359
1360/**
Owen Taylor3473f882001-02-23 17:55:21 +00001361 * xmlXIncludeLoadDoc:
1362 * @ctxt: the XInclude context
1363 * @url: the associated URL
1364 * @nr: the xinclude node number
1365 *
1366 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001367 *
1368 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001369 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001370static int
Owen Taylor3473f882001-02-23 17:55:21 +00001371xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1372 xmlDocPtr doc;
1373 xmlURIPtr uri;
1374 xmlChar *URL;
1375 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001376 int i = 0;
William M. Brack4d59e222004-03-08 14:42:31 +00001377#ifdef LIBXML_XPTR_ENABLED
1378 int saveFlags;
1379#endif
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001380
1381#ifdef DEBUG_XINCLUDE
1382 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1383#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001384 /*
1385 * Check the URL and remove any fragment identifier
1386 */
1387 uri = xmlParseURI((const char *)url);
1388 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001389 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1390 XML_XINCLUDE_HREF_URI,
1391 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001392 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001393 }
1394 if (uri->fragment != NULL) {
1395 fragment = (xmlChar *) uri->fragment;
1396 uri->fragment = NULL;
1397 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001398 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1399 (ctxt->incTab[nr]->fragment != NULL)) {
1400 if (fragment != NULL) xmlFree(fragment);
1401 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1402 }
Owen Taylor3473f882001-02-23 17:55:21 +00001403 URL = xmlSaveUri(uri);
1404 xmlFreeURI(uri);
1405 if (URL == NULL) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00001406 if (ctxt->incTab != NULL)
1407 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1408 XML_XINCLUDE_HREF_URI,
1409 "invalid value URI %s\n", url);
1410 else
1411 xmlXIncludeErr(ctxt, NULL,
1412 XML_XINCLUDE_HREF_URI,
1413 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001414 if (fragment != NULL)
1415 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001416 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001417 }
1418
1419 /*
1420 * Handling of references to the local document are done
1421 * directly through ctxt->doc.
1422 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001423 if ((URL[0] == 0) || (URL[0] == '#') ||
1424 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001425 doc = NULL;
1426 goto loaded;
1427 }
1428
1429 /*
1430 * Prevent reloading twice the document.
1431 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001432 for (i = 0; i < ctxt->incNr; i++) {
1433 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1434 (ctxt->incTab[i]->doc != NULL)) {
1435 doc = ctxt->incTab[i]->doc;
1436#ifdef DEBUG_XINCLUDE
1437 printf("Already loaded %s\n", URL);
1438#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001439 goto loaded;
1440 }
1441 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001442
Owen Taylor3473f882001-02-23 17:55:21 +00001443 /*
1444 * Load it.
1445 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001446#ifdef DEBUG_XINCLUDE
1447 printf("loading %s\n", URL);
1448#endif
William M. Brack4d59e222004-03-08 14:42:31 +00001449#ifdef LIBXML_XPTR_ENABLED
1450 /*
1451 * If this is an XPointer evaluation, we want to assure that
1452 * all entities have been resolved prior to processing the
1453 * referenced document
1454 */
1455 saveFlags = ctxt->parseFlags;
1456 if (fragment != NULL) { /* if this is an XPointer eval */
1457 ctxt->parseFlags |= XML_PARSE_NOENT;
1458 }
1459#endif
1460
Daniel Veillard98485322003-08-14 15:44:40 +00001461 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
William M. Brack4d59e222004-03-08 14:42:31 +00001462#ifdef LIBXML_XPTR_ENABLED
1463 ctxt->parseFlags = saveFlags;
1464#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001465 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001466 xmlFree(URL);
1467 if (fragment != NULL)
1468 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001469 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001470 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001471 ctxt->incTab[nr]->doc = doc;
William M. Brackb85c9202004-07-26 00:20:13 +00001472 /*
1473 * It's possible that the requested URL has been mapped to a
1474 * completely different location (e.g. through a catalog entry).
1475 * To check for this, we compare the URL with that of the doc
1476 * and change it if they disagree (bug 146988).
1477 */
1478 if (!xmlStrEqual(URL, doc->URL)) {
1479 xmlFree(URL);
1480 URL = xmlStrdup(doc->URL);
1481 }
Daniel Veillard98485322003-08-14 15:44:40 +00001482 for (i = nr + 1; i < ctxt->incNr; i++) {
1483 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1484 ctxt->incTab[nr]->count++;
1485#ifdef DEBUG_XINCLUDE
1486 printf("Increasing %s count since reused\n", URL);
1487#endif
1488 break;
1489 }
1490 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001491
1492 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001493 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001494 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001495 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001496
1497 /*
1498 * We don't need the DTD anymore, free up space
1499 if (doc->intSubset != NULL) {
1500 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1501 xmlFreeNode((xmlNodePtr) doc->intSubset);
1502 doc->intSubset = NULL;
1503 }
1504 if (doc->extSubset != NULL) {
1505 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1506 xmlFreeNode((xmlNodePtr) doc->extSubset);
1507 doc->extSubset = NULL;
1508 }
1509 */
1510 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001511
1512loaded:
1513 if (fragment == NULL) {
1514 /*
1515 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001516 */
1517 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001518 {
1519 /* Hopefully a DTD declaration won't be copied from
1520 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001521 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001522 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001523 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001524 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001525 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001526 }
1527#ifdef LIBXML_XPTR_ENABLED
1528 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001529 /*
1530 * Computes the XPointer expression and make a copy used
1531 * as the replacement copy.
1532 */
1533 xmlXPathObjectPtr xptr;
1534 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001535 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001536
1537 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001538 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1539 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001540 } else {
1541 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1542 }
1543 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001544 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1545 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001546 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001547 xmlFree(URL);
1548 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001549 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001550 }
1551 xptr = xmlXPtrEval(fragment, xptrctxt);
1552 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001553 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1554 XML_XINCLUDE_XPTR_FAILED,
1555 "XPointer evaluation failed: #%s\n",
1556 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001557 xmlXPathFreeContext(xptrctxt);
1558 xmlFree(URL);
1559 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001560 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001561 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001562 switch (xptr->type) {
1563 case XPATH_UNDEFINED:
1564 case XPATH_BOOLEAN:
1565 case XPATH_NUMBER:
1566 case XPATH_STRING:
1567 case XPATH_POINT:
1568 case XPATH_USERS:
1569 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001570 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1571 XML_XINCLUDE_XPTR_RESULT,
1572 "XPointer is not a range: #%s\n",
1573 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001574 xmlXPathFreeContext(xptrctxt);
1575 xmlFree(URL);
1576 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001577 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001578 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001579 if ((xptr->nodesetval == NULL) ||
1580 (xptr->nodesetval->nodeNr <= 0)) {
1581 xmlXPathFreeContext(xptrctxt);
1582 xmlFree(URL);
1583 xmlFree(fragment);
1584 return(-1);
1585 }
William M. Brackabf598b2004-06-08 02:01:28 +00001586
Daniel Veillard39196eb2001-06-19 18:09:42 +00001587 case XPATH_RANGE:
1588 case XPATH_LOCATIONSET:
1589 break;
1590 }
1591 set = xptr->nodesetval;
1592 if (set != NULL) {
1593 for (i = 0;i < set->nodeNr;i++) {
1594 if (set->nodeTab[i] == NULL)
1595 continue;
1596 switch (set->nodeTab[i]->type) {
1597 case XML_TEXT_NODE:
1598 case XML_CDATA_SECTION_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001599 case XML_ENTITY_REF_NODE:
1600 case XML_ENTITY_NODE:
1601 case XML_PI_NODE:
1602 case XML_COMMENT_NODE:
1603 case XML_DOCUMENT_NODE:
1604 case XML_HTML_DOCUMENT_NODE:
1605#ifdef LIBXML_DOCB_ENABLED
1606 case XML_DOCB_DOCUMENT_NODE:
1607#endif
1608 continue;
William M. Brackabf598b2004-06-08 02:01:28 +00001609 case XML_ELEMENT_NODE: {
1610 xmlChar *nodeBase;
1611 xmlNodePtr el = set->nodeTab[i];
1612
1613 nodeBase = xmlNodeGetBase(el->doc, el);
1614 if (nodeBase != NULL) {
1615 if (!xmlStrEqual(nodeBase, el->doc->URL))
1616 xmlNodeSetBase(el, nodeBase);
1617 xmlFree(nodeBase);
1618 }
1619 continue;
1620 }
1621
Daniel Veillard39196eb2001-06-19 18:09:42 +00001622 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001623 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1624 XML_XINCLUDE_XPTR_RESULT,
1625 "XPointer selects an attribute: #%s\n",
1626 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001627 set->nodeTab[i] = NULL;
1628 continue;
1629 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001630 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1631 XML_XINCLUDE_XPTR_RESULT,
1632 "XPointer selects a namespace: #%s\n",
1633 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001634 set->nodeTab[i] = NULL;
1635 continue;
1636 case XML_DOCUMENT_TYPE_NODE:
1637 case XML_DOCUMENT_FRAG_NODE:
1638 case XML_NOTATION_NODE:
1639 case XML_DTD_NODE:
1640 case XML_ELEMENT_DECL:
1641 case XML_ATTRIBUTE_DECL:
1642 case XML_ENTITY_DECL:
1643 case XML_XINCLUDE_START:
1644 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001645 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1646 XML_XINCLUDE_XPTR_RESULT,
1647 "XPointer selects unexpected nodes: #%s\n",
1648 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001649 set->nodeTab[i] = NULL;
1650 set->nodeTab[i] = NULL;
1651 continue; /* for */
1652 }
1653 }
1654 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001655 if (doc == NULL) {
1656 ctxt->incTab[nr]->xptr = xptr;
1657 ctxt->incTab[nr]->inc = NULL;
1658 } else {
1659 ctxt->incTab[nr]->inc =
1660 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1661 xmlXPathFreeObject(xptr);
1662 }
Owen Taylor3473f882001-02-23 17:55:21 +00001663 xmlXPathFreeContext(xptrctxt);
1664 xmlFree(fragment);
1665 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001666#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001667
1668 /*
1669 * Do the xml:base fixup if needed
1670 */
1671 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1672 xmlNodePtr node;
William M. Brack0b13a092005-06-01 03:37:59 +00001673 xmlChar *base;
William M. Brackabf598b2004-06-08 02:01:28 +00001674 xmlChar *curBase;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001675
William M. Brackf7789b12004-06-07 08:57:27 +00001676 /*
William M. Brack0b13a092005-06-01 03:37:59 +00001677 * The base is only adjusted if "necessary", i.e. if the xinclude node
1678 * has a base specified, or the URL is relative
William M. Brackf7789b12004-06-07 08:57:27 +00001679 */
William M. Brack0b13a092005-06-01 03:37:59 +00001680 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1681 XML_XML_NAMESPACE);
1682 if (base == NULL) {
1683 /*
1684 * No xml:base on the xinclude node, so we check whether the
1685 * URI base is different than (relative to) the context base
1686 */
1687 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1688 if (curBase == NULL) { /* Error return */
1689 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
William M. Brackf7789b12004-06-07 08:57:27 +00001690 XML_XINCLUDE_HREF_URI,
1691 "trying to build relative URI from %s\n", URL);
William M. Brack0b13a092005-06-01 03:37:59 +00001692 } else {
1693 /* If the URI doesn't contain a slash, it's not relative */
1694 if (!xmlStrchr(curBase, (xmlChar) '/'))
1695 xmlFree(curBase);
1696 else
1697 base = curBase;
William M. Brackf7789b12004-06-07 08:57:27 +00001698 }
William M. Brack0b13a092005-06-01 03:37:59 +00001699 }
1700 if (base != NULL) { /* Adjustment may be needed */
1701 node = ctxt->incTab[nr]->inc;
1702 while (node != NULL) {
1703 /* Only work on element nodes */
1704 if (node->type == XML_ELEMENT_NODE) {
1705 curBase = xmlNodeGetBase(node->doc, node);
1706 /* If no current base, set it */
1707 if (curBase == NULL) {
1708 xmlNodeSetBase(node, base);
1709 } else {
1710 /*
1711 * If the current base is the same as the
1712 * URL of the document, then reset it to be
1713 * the specified xml:base or the relative URI
1714 */
1715 if (xmlStrEqual(curBase, node->doc->URL)) {
1716 xmlNodeSetBase(node, base);
1717 } else {
1718 /*
1719 * If the element already has an xml:base
1720 * set, then relativise it if necessary
1721 */
1722 xmlChar *xmlBase;
1723 xmlBase = xmlGetNsProp(node,
1724 BAD_CAST "base",
1725 XML_XML_NAMESPACE);
1726 if (xmlBase != NULL) {
1727 xmlChar *relBase;
1728 relBase = xmlBuildURI(xmlBase, base);
1729 if (relBase == NULL) { /* error */
1730 xmlXIncludeErr(ctxt,
1731 ctxt->incTab[nr]->ref,
1732 XML_XINCLUDE_HREF_URI,
1733 "trying to rebuild base from %s\n",
1734 xmlBase);
1735 } else {
1736 xmlNodeSetBase(node, relBase);
1737 xmlFree(relBase);
1738 }
1739 xmlFree(xmlBase);
1740 }
1741 }
1742 xmlFree(curBase);
1743 }
1744 }
1745 node = node->next;
1746 }
1747 xmlFree(base);
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001748 }
1749 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001750 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1751 (ctxt->incTab[nr]->count <= 1)) {
1752#ifdef DEBUG_XINCLUDE
1753 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1754#endif
1755 xmlFreeDoc(ctxt->incTab[nr]->doc);
1756 ctxt->incTab[nr]->doc = NULL;
1757 }
Owen Taylor3473f882001-02-23 17:55:21 +00001758 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001759 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001760}
1761
1762/**
1763 * xmlXIncludeLoadTxt:
1764 * @ctxt: the XInclude context
1765 * @url: the associated URL
1766 * @nr: the xinclude node number
1767 *
1768 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001769 *
1770 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001771 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001772static int
Owen Taylor3473f882001-02-23 17:55:21 +00001773xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1774 xmlParserInputBufferPtr buf;
1775 xmlNodePtr node;
1776 xmlURIPtr uri;
1777 xmlChar *URL;
1778 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001779 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001780 xmlCharEncoding enc = (xmlCharEncoding) 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001781
Owen Taylor3473f882001-02-23 17:55:21 +00001782 /*
1783 * Check the URL and remove any fragment identifier
1784 */
1785 uri = xmlParseURI((const char *)url);
1786 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001787 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1788 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001789 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001790 }
1791 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001792 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1793 "fragment identifier forbidden for text: %s\n",
1794 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001795 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001796 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001797 }
1798 URL = xmlSaveUri(uri);
1799 xmlFreeURI(uri);
1800 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001801 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1802 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001803 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001804 }
1805
1806 /*
1807 * Handling of references to the local document are done
1808 * directly through ctxt->doc.
1809 */
1810 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001811 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1812 XML_XINCLUDE_TEXT_DOCUMENT,
1813 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001814 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001815 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001816 }
1817
1818 /*
1819 * Prevent reloading twice the document.
1820 */
1821 for (i = 0; i < ctxt->txtNr; i++) {
1822 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1823 node = xmlCopyNode(ctxt->txtTab[i], 1);
1824 goto loaded;
1825 }
1826 }
1827 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001828 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001829 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001830 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1831 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1832 }
1833 if (encoding != NULL) {
1834 /*
1835 * TODO: we should not have to remap to the xmlCharEncoding
1836 * predefined set, a better interface than
1837 * xmlParserInputBufferCreateFilename should allow any
1838 * encoding supported by iconv
1839 */
1840 enc = xmlParseCharEncoding((const char *) encoding);
1841 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001842 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1843 XML_XINCLUDE_UNKNOWN_ENCODING,
1844 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001845 xmlFree(encoding);
1846 xmlFree(URL);
1847 return(-1);
1848 }
1849 xmlFree(encoding);
1850 }
1851
1852 /*
1853 * Load it.
1854 */
1855 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001856 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001857 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001858 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001859 }
1860 node = xmlNewText(NULL);
1861
1862 /*
1863 * Scan all chars from the resource and add the to the node
1864 */
1865 while (xmlParserInputBufferRead(buf, 128) > 0) {
1866 int len;
1867 const xmlChar *content;
1868
1869 content = xmlBufferContent(buf->buffer);
1870 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001871 for (i = 0;i < len;) {
1872 int cur;
1873 int l;
1874
1875 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1876 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001877 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1878 XML_XINCLUDE_INVALID_CHAR,
1879 "%s contains invalid char\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001880 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001881 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001882 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001883 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001884 }
1885 xmlBufferShrink(buf->buffer, len);
1886 }
1887 xmlFreeParserInputBuffer(buf);
1888 xmlXIncludeAddTxt(ctxt, node, URL);
1889
1890loaded:
1891 /*
1892 * Add the element as the replacement copy.
1893 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001894 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001895 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001896 return(0);
1897}
1898
1899/**
1900 * xmlXIncludeLoadFallback:
1901 * @ctxt: the XInclude context
1902 * @fallback: the fallback node
1903 * @nr: the xinclude node number
1904 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001905 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001906 * in the XInclude context
1907 *
1908 * Returns 0 in case of success, -1 in case of failure
1909 */
1910static int
1911xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001912 xmlXIncludeCtxtPtr newctxt;
1913 int ret = 0;
1914
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001915 if ((fallback == NULL) || (ctxt == NULL))
1916 return(-1);
William M. Brackef245fd2004-02-06 09:33:59 +00001917 if (fallback->children != NULL) {
1918 /*
1919 * It's possible that the fallback also has 'includes'
1920 * (Bug 129969), so we re-process the fallback just in case
1921 */
1922 newctxt = xmlXIncludeNewContext(ctxt->doc);
1923 if (newctxt == NULL)
1924 return (-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00001925 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
William M. Brackef245fd2004-02-06 09:33:59 +00001926 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1927 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
William M. Brack87640d52004-04-17 14:58:15 +00001928 if (ctxt->nbErrors > 0)
William M. Brackef245fd2004-02-06 09:33:59 +00001929 ret = -1;
William M. Brack87640d52004-04-17 14:58:15 +00001930 else if (ret > 0)
1931 ret = 0; /* xmlXIncludeDoProcess can return +ve number */
William M. Brackef245fd2004-02-06 09:33:59 +00001932 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001933
Daniel Veillard03a53c32004-10-26 16:06:51 +00001934 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1935 fallback->children);
William M. Brackef245fd2004-02-06 09:33:59 +00001936 } else {
1937 ctxt->incTab[nr]->inc = NULL;
William M. Brack95af5942004-02-08 04:12:49 +00001938 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
William M. Brackef245fd2004-02-06 09:33:59 +00001939 }
William M. Brackaae10522004-01-02 14:59:41 +00001940 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001941}
1942
1943/************************************************************************
1944 * *
1945 * XInclude Processing *
1946 * *
1947 ************************************************************************/
1948
1949/**
1950 * xmlXIncludePreProcessNode:
1951 * @ctxt: an XInclude context
1952 * @node: an XInclude node
1953 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001954 * Implement the XInclude preprocessing, currently just adding the element
1955 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001956 *
1957 * Returns the result list or NULL in case of error
1958 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001959static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001960xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1961 xmlXIncludeAddNode(ctxt, node);
Daniel Veillard24505b02005-07-28 23:49:35 +00001962 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001963}
1964
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001965/**
Owen Taylor3473f882001-02-23 17:55:21 +00001966 * xmlXIncludeLoadNode:
1967 * @ctxt: an XInclude context
1968 * @nr: the node number
1969 *
1970 * Find and load the infoset replacement for the given node.
1971 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001972 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001973 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001974static int
Owen Taylor3473f882001-02-23 17:55:21 +00001975xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1976 xmlNodePtr cur;
1977 xmlChar *href;
1978 xmlChar *parse;
1979 xmlChar *base;
William M. Brackf7789b12004-06-07 08:57:27 +00001980 xmlChar *oldBase;
Owen Taylor3473f882001-02-23 17:55:21 +00001981 xmlChar *URI;
1982 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001983 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001984
1985 if (ctxt == NULL)
1986 return(-1);
1987 if ((nr < 0) || (nr >= ctxt->incNr))
1988 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001989 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001990 if (cur == NULL)
1991 return(-1);
1992
Owen Taylor3473f882001-02-23 17:55:21 +00001993 /*
1994 * read the attributes
1995 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001996 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00001997 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001998 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
1999 if (href == NULL)
2000 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002001 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002002 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00002003 if (parse != NULL) {
2004 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
2005 xml = 1;
2006 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2007 xml = 0;
2008 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002009 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2010 XML_XINCLUDE_PARSE_VALUE,
2011 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00002012 if (href != NULL)
2013 xmlFree(href);
2014 if (parse != NULL)
2015 xmlFree(parse);
2016 return(-1);
2017 }
2018 }
2019
2020 /*
2021 * compute the URI
2022 */
2023 base = xmlNodeGetBase(ctxt->doc, cur);
2024 if (base == NULL) {
2025 URI = xmlBuildURI(href, ctxt->doc->URL);
2026 } else {
2027 URI = xmlBuildURI(href, base);
2028 }
2029 if (URI == NULL) {
2030 xmlChar *escbase;
2031 xmlChar *eschref;
2032 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002033 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00002034 */
2035 escbase = xmlURIEscape(base);
2036 eschref = xmlURIEscape(href);
2037 URI = xmlBuildURI(eschref, escbase);
2038 if (escbase != NULL)
2039 xmlFree(escbase);
2040 if (eschref != NULL)
2041 xmlFree(eschref);
2042 }
2043 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002044 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2045 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002046 if (parse != NULL)
2047 xmlFree(parse);
2048 if (href != NULL)
2049 xmlFree(href);
2050 if (base != NULL)
2051 xmlFree(base);
2052 return(-1);
2053 }
2054#ifdef DEBUG_XINCLUDE
2055 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2056 xml ? "xml": "text");
2057 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2058#endif
2059
2060 /*
William M. Brackf7789b12004-06-07 08:57:27 +00002061 * Save the base for this include (saving the current one)
Owen Taylor3473f882001-02-23 17:55:21 +00002062 */
William M. Brackf7789b12004-06-07 08:57:27 +00002063 oldBase = ctxt->base;
2064 ctxt->base = base;
2065
Owen Taylor3473f882001-02-23 17:55:21 +00002066 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002067 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00002068 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2069 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002070 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2071 }
William M. Brackf7789b12004-06-07 08:57:27 +00002072
2073 /*
2074 * Restore the original base before checking for fallback
2075 */
2076 ctxt->base = oldBase;
2077
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002078 if (ret < 0) {
2079 xmlNodePtr children;
2080
2081 /*
2082 * Time to try a fallback if availble
2083 */
2084#ifdef DEBUG_XINCLUDE
2085 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2086#endif
2087 children = cur->children;
2088 while (children != NULL) {
2089 if ((children->type == XML_ELEMENT_NODE) &&
2090 (children->ns != NULL) &&
2091 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002092 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2093 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002094 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00002095 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002096 break;
2097 }
2098 children = children->next;
2099 }
2100 }
2101 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002102 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2103 XML_XINCLUDE_NO_FALLBACK,
2104 "could not load %s, and no fallback was found\n",
2105 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002106 }
2107
2108 /*
2109 * Cleanup
2110 */
2111 if (URI != NULL)
2112 xmlFree(URI);
2113 if (parse != NULL)
2114 xmlFree(parse);
2115 if (href != NULL)
2116 xmlFree(href);
2117 if (base != NULL)
2118 xmlFree(base);
2119 return(0);
2120}
2121
2122/**
2123 * xmlXIncludeIncludeNode:
2124 * @ctxt: an XInclude context
2125 * @nr: the node number
2126 *
2127 * Inplement the infoset replacement for the given node
2128 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002129 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002130 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002131static int
Owen Taylor3473f882001-02-23 17:55:21 +00002132xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002133 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002134
2135 if (ctxt == NULL)
2136 return(-1);
2137 if ((nr < 0) || (nr >= ctxt->incNr))
2138 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002139 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00002140 if (cur == NULL)
2141 return(-1);
2142
2143 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002144 * If we stored an XPointer a late computation may be needed
2145 */
2146 if ((ctxt->incTab[nr]->inc == NULL) &&
2147 (ctxt->incTab[nr]->xptr != NULL)) {
2148 ctxt->incTab[nr]->inc =
2149 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
2150 ctxt->incTab[nr]->xptr);
2151 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2152 ctxt->incTab[nr]->xptr = NULL;
2153 }
2154 list = ctxt->incTab[nr]->inc;
2155 ctxt->incTab[nr]->inc = NULL;
2156
2157 /*
2158 * Check against the risk of generating a multi-rooted document
2159 */
2160 if ((cur->parent != NULL) &&
2161 (cur->parent->type != XML_ELEMENT_NODE)) {
2162 int nb_elem = 0;
2163
2164 tmp = list;
2165 while (tmp != NULL) {
2166 if (tmp->type == XML_ELEMENT_NODE)
2167 nb_elem++;
2168 tmp = tmp->next;
2169 }
2170 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002171 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2172 XML_XINCLUDE_MULTIPLE_ROOT,
2173 "XInclude error: would result in multiple root nodes\n",
2174 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002175 return(-1);
2176 }
2177 }
2178
Daniel Veillardc14c3892004-08-16 12:34:50 +00002179 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2180 /*
2181 * Add the list of nodes
2182 */
2183 while (list != NULL) {
2184 end = list;
2185 list = list->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002186
Daniel Veillardc14c3892004-08-16 12:34:50 +00002187 xmlAddPrevSibling(cur, end);
2188 }
2189 xmlUnlinkNode(cur);
2190 xmlFreeNode(cur);
2191 } else {
2192 /*
2193 * Change the current node as an XInclude start one, and add an
2194 * XInclude end one
2195 */
2196 cur->type = XML_XINCLUDE_START;
Daniel Veillard03a53c32004-10-26 16:06:51 +00002197 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
Daniel Veillardc14c3892004-08-16 12:34:50 +00002198 if (end == NULL) {
2199 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2200 XML_XINCLUDE_BUILD_FAILED,
2201 "failed to build node\n", NULL);
2202 return(-1);
2203 }
2204 end->type = XML_XINCLUDE_END;
2205 xmlAddNextSibling(cur, end);
Owen Taylor3473f882001-02-23 17:55:21 +00002206
Daniel Veillardc14c3892004-08-16 12:34:50 +00002207 /*
2208 * Add the list of nodes
2209 */
2210 while (list != NULL) {
2211 cur = list;
2212 list = list->next;
2213
2214 xmlAddPrevSibling(end, cur);
2215 }
Owen Taylor3473f882001-02-23 17:55:21 +00002216 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002217
2218
Owen Taylor3473f882001-02-23 17:55:21 +00002219 return(0);
2220}
2221
2222/**
2223 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002224 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002225 * @node: an XInclude node
2226 *
2227 * test if the node is an XInclude node
2228 *
2229 * Returns 1 true, 0 otherwise
2230 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002231static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002232xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002233 if (node == NULL)
2234 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002235 if (node->type != XML_ELEMENT_NODE)
2236 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002237 if (node->ns == NULL)
2238 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002239 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2240 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2241 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2242 if (ctxt->legacy == 0) {
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002243#if 0 /* wait for the XML Core Working Group to get something stable ! */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002244 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2245 "Deprecated XInclude namespace found, use %s",
2246 XINCLUDE_NS);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002247#endif
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002248 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002249 }
2250 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002251 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2252 xmlNodePtr child = node->children;
2253 int nb_fallback = 0;
2254
2255 while (child != NULL) {
2256 if ((child->type == XML_ELEMENT_NODE) &&
2257 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002258 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2259 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002260 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002261 xmlXIncludeErr(ctxt, node,
2262 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2263 "%s has an 'include' child\n",
2264 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002265 return(0);
2266 }
2267 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2268 nb_fallback++;
2269 }
2270 }
2271 child = child->next;
2272 }
2273 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002274 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2275 "%s has multiple fallback children\n",
2276 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002277 return(0);
2278 }
2279 return(1);
2280 }
2281 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2282 if ((node->parent == NULL) ||
2283 (node->parent->type != XML_ELEMENT_NODE) ||
2284 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002285 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2286 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002287 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002288 xmlXIncludeErr(ctxt, node,
2289 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2290 "%s is not the child of an 'include'\n",
2291 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002292 }
2293 }
2294 }
Owen Taylor3473f882001-02-23 17:55:21 +00002295 return(0);
2296}
2297
2298/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002299 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002300 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002301 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002302 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002303 *
2304 * Implement the XInclude substitution on the XML document @doc
2305 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002306 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002307 * or the number of substitutions done.
2308 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002309static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002310xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002311 xmlNodePtr cur;
2312 int ret = 0;
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002313 int i, start;
Owen Taylor3473f882001-02-23 17:55:21 +00002314
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002315 if ((doc == NULL) || (tree == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002316 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002317 if (ctxt == NULL)
2318 return(-1);
2319
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002320 if (doc->URL != NULL) {
2321 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2322 if (ret < 0)
2323 return(-1);
2324 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00002325 start = ctxt->incNr;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002326
Owen Taylor3473f882001-02-23 17:55:21 +00002327 /*
2328 * First phase: lookup the elements in the document
2329 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002330 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002331 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002332 xmlXIncludePreProcessNode(ctxt, cur);
William M. Brack7b0e2762004-05-12 09:33:23 +00002333 while ((cur != NULL) && (cur != tree->parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002334 /* TODO: need to work on entities -> stack */
2335 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002336 (cur->children->type != XML_ENTITY_DECL) &&
2337 (cur->children->type != XML_XINCLUDE_START) &&
2338 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002339 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002340 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002341 xmlXIncludePreProcessNode(ctxt, cur);
2342 } else if (cur->next != NULL) {
2343 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002344 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002345 xmlXIncludePreProcessNode(ctxt, cur);
2346 } else {
William M. Brack5d8d10b2004-04-16 08:11:26 +00002347 if (cur == tree)
2348 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002349 do {
2350 cur = cur->parent;
William M. Brack7b0e2762004-05-12 09:33:23 +00002351 if ((cur == NULL) || (cur == tree->parent))
2352 break; /* do */
Owen Taylor3473f882001-02-23 17:55:21 +00002353 if (cur->next != NULL) {
2354 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002355 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002356 xmlXIncludePreProcessNode(ctxt, cur);
2357 break; /* do */
2358 }
2359 } while (cur != NULL);
2360 }
2361 }
2362
2363 /*
2364 * Second Phase : collect the infosets fragments
2365 */
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002366 for (i = start;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002367 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002368 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002369 }
2370
2371 /*
2372 * Third phase: extend the original document infoset.
William M. Brack6b1a28d2004-02-06 11:24:44 +00002373 *
2374 * Originally we bypassed the inclusion if there were any errors
2375 * encountered on any of the XIncludes. A bug was raised (bug
2376 * 132588) requesting that we output the XIncludes without error,
2377 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2378 * give some other problems in the future, but for now it seems to
2379 * work ok.
2380 *
Owen Taylor3473f882001-02-23 17:55:21 +00002381 */
William M. Brack6b1a28d2004-02-06 11:24:44 +00002382 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
William M. Brack95af5942004-02-08 04:12:49 +00002383 if ((ctxt->incTab[i]->inc != NULL) ||
2384 (ctxt->incTab[i]->xptr != NULL) ||
2385 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002386 xmlXIncludeIncludeNode(ctxt, i);
Owen Taylor3473f882001-02-23 17:55:21 +00002387 }
2388
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002389 if (doc->URL != NULL)
2390 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002391 return(ret);
2392}
2393
2394/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002395 * xmlXIncludeSetFlags:
2396 * @ctxt: an XInclude processing context
2397 * @flags: a set of xmlParserOption used for parsing XML includes
2398 *
2399 * Set the flags used for further processing of XML resources.
2400 *
2401 * Returns 0 in case of success and -1 in case of error.
2402 */
2403int
2404xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2405 if (ctxt == NULL)
2406 return(-1);
2407 ctxt->parseFlags = flags;
2408 return(0);
2409}
2410
2411/**
2412 * xmlXIncludeProcessFlags:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002413 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002414 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002415 *
2416 * Implement the XInclude substitution on the XML document @doc
2417 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002418 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002419 * or the number of substitutions done.
2420 */
2421int
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002422xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002423 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002424 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002425 int ret = 0;
2426
2427 if (doc == NULL)
2428 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002429 tree = xmlDocGetRootElement(doc);
2430 if (tree == NULL)
2431 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002432 ctxt = xmlXIncludeNewContext(doc);
2433 if (ctxt == NULL)
2434 return(-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00002435 ctxt->base = xmlStrdup((xmlChar *)doc->URL);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002436 xmlXIncludeSetFlags(ctxt, flags);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002437 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2438 if ((ret >= 0) && (ctxt->nbErrors > 0))
2439 ret = -1;
2440
2441 xmlXIncludeFreeContext(ctxt);
2442 return(ret);
2443}
2444
2445/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002446 * xmlXIncludeProcess:
2447 * @doc: an XML document
2448 *
2449 * Implement the XInclude substitution on the XML document @doc
2450 *
2451 * Returns 0 if no substitution were done, -1 if some processing failed
2452 * or the number of substitutions done.
2453 */
2454int
2455xmlXIncludeProcess(xmlDocPtr doc) {
2456 return(xmlXIncludeProcessFlags(doc, 0));
2457}
2458
2459/**
2460 * xmlXIncludeProcessTreeFlags:
2461 * @tree: a node in an XML document
2462 * @flags: a set of xmlParserOption used for parsing XML includes
2463 *
2464 * Implement the XInclude substitution for the given subtree
2465 *
2466 * Returns 0 if no substitution were done, -1 if some processing failed
2467 * or the number of substitutions done.
2468 */
2469int
2470xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2471 xmlXIncludeCtxtPtr ctxt;
2472 int ret = 0;
2473
2474 if ((tree == NULL) || (tree->doc == NULL))
2475 return(-1);
2476 ctxt = xmlXIncludeNewContext(tree->doc);
2477 if (ctxt == NULL)
2478 return(-1);
William M. Brackf7789b12004-06-07 08:57:27 +00002479 ctxt->base = xmlNodeGetBase(tree->doc, tree);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002480 xmlXIncludeSetFlags(ctxt, flags);
2481 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2482 if ((ret >= 0) && (ctxt->nbErrors > 0))
2483 ret = -1;
2484
2485 xmlXIncludeFreeContext(ctxt);
2486 return(ret);
2487}
2488
2489/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002490 * xmlXIncludeProcessTree:
2491 * @tree: a node in an XML document
2492 *
2493 * Implement the XInclude substitution for the given subtree
2494 *
2495 * Returns 0 if no substitution were done, -1 if some processing failed
2496 * or the number of substitutions done.
2497 */
2498int
2499xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002500 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002501}
2502
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002503/**
2504 * xmlXIncludeProcessNode:
2505 * @ctxt: an existing XInclude context
2506 * @node: a node in an XML document
2507 *
2508 * Implement the XInclude substitution for the given subtree reusing
2509 * the informations and data coming from the given context.
2510 *
2511 * Returns 0 if no substitution were done, -1 if some processing failed
2512 * or the number of substitutions done.
2513 */
2514int
2515xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2516 int ret = 0;
2517
2518 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2519 return(-1);
2520 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2521 if ((ret >= 0) && (ctxt->nbErrors > 0))
2522 ret = -1;
2523 return(ret);
2524}
2525
Owen Taylor3473f882001-02-23 17:55:21 +00002526#else /* !LIBXML_XINCLUDE_ENABLED */
2527#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002528#define bottom_xinclude
2529#include "elfgcchack.h"