blob: c58bbcfdf9ac82cca6e47670c90f5e84fad0ae77 [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 */
Daniel Veillard681e9042006-09-29 09:16:00 +000085
86 void *_private; /* application data */
Owen Taylor3473f882001-02-23 17:55:21 +000087};
88
Daniel Veillardd16df9f2001-05-23 13:44:21 +000089static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +000090xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree);
Owen Taylor3473f882001-02-23 17:55:21 +000091
Daniel Veillard98485322003-08-14 15:44:40 +000092
Daniel Veillardcd6ff282003-10-08 22:38:13 +000093/************************************************************************
94 * *
Daniel Veillard69d2c172003-10-09 11:46:07 +000095 * XInclude error handler *
Daniel Veillardcd6ff282003-10-08 22:38:13 +000096 * *
97 ************************************************************************/
98
Daniel Veillard98485322003-08-14 15:44:40 +000099/**
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000100 * xmlXIncludeErrMemory:
William M. Brack72ee48d2003-12-30 08:30:19 +0000101 * @extra: extra information
Daniel Veillard98485322003-08-14 15:44:40 +0000102 *
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000103 * Handle an out of memory condition
Daniel Veillard98485322003-08-14 15:44:40 +0000104 */
105static void
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000106xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
107 const char *extra)
Daniel Veillard98485322003-08-14 15:44:40 +0000108{
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000109 if (ctxt != NULL)
110 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000111 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000112 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
113 extra, NULL, NULL, 0, 0,
114 "Memory allocation failed : %s\n", extra);
115}
Daniel Veillard98485322003-08-14 15:44:40 +0000116
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000117/**
118 * xmlXIncludeErr:
119 * @ctxt: the XInclude context
120 * @node: the context node
121 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000122 * @extra: extra information
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000123 *
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000124 * Handle an XInclude error
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000125 */
126static void
127xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
128 const char *msg, const xmlChar *extra)
129{
130 if (ctxt != NULL)
131 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000132 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000133 error, XML_ERR_ERROR, NULL, 0,
134 (const char *) extra, NULL, NULL, 0, 0,
135 msg, (const char *) extra);
Daniel Veillard98485322003-08-14 15:44:40 +0000136}
137
Daniel Veillardf54cd532004-02-25 11:52:31 +0000138#if 0
Owen Taylor3473f882001-02-23 17:55:21 +0000139/**
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000140 * xmlXIncludeWarn:
141 * @ctxt: the XInclude context
142 * @node: the context node
143 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000144 * @extra: extra information
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000145 *
146 * Emit an XInclude warning.
147 */
148static void
149xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
150 const char *msg, const xmlChar *extra)
151{
152 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
153 error, XML_ERR_WARNING, NULL, 0,
154 (const char *) extra, NULL, NULL, 0, 0,
155 msg, (const char *) extra);
156}
Daniel Veillardf54cd532004-02-25 11:52:31 +0000157#endif
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000158
159/**
160 * xmlXIncludeGetProp:
161 * @ctxt: the XInclude context
162 * @cur: the node
163 * @name: the attribute name
164 *
165 * Get an XInclude attribute
166 *
167 * Returns the value (to be freed) or NULL if not found
168 */
169static xmlChar *
170xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
171 const xmlChar *name) {
172 xmlChar *ret;
173
174 ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
175 if (ret != NULL)
176 return(ret);
177 if (ctxt->legacy != 0) {
178 ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
179 if (ret != NULL)
180 return(ret);
181 }
182 ret = xmlGetProp(cur, name);
183 return(ret);
184}
185/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000186 * xmlXIncludeFreeRef:
187 * @ref: the XInclude reference
188 *
189 * Free an XInclude reference
190 */
191static void
192xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
193 if (ref == NULL)
194 return;
195#ifdef DEBUG_XINCLUDE
196 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
197#endif
198 if (ref->doc != NULL) {
199#ifdef DEBUG_XINCLUDE
200 xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
201#endif
202 xmlFreeDoc(ref->doc);
203 }
204 if (ref->URI != NULL)
205 xmlFree(ref->URI);
206 if (ref->fragment != NULL)
207 xmlFree(ref->fragment);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000208 if (ref->xptr != NULL)
209 xmlXPathFreeObject(ref->xptr);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000210 xmlFree(ref);
211}
212
213/**
214 * xmlXIncludeNewRef:
215 * @ctxt: the XInclude context
216 * @URI: the resource URI
217 *
218 * Creates a new reference within an XInclude context
219 *
220 * Returns the new set
221 */
222static xmlXIncludeRefPtr
223xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
224 xmlNodePtr ref) {
225 xmlXIncludeRefPtr ret;
226
227#ifdef DEBUG_XINCLUDE
228 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
229#endif
230 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000231 if (ret == NULL) {
232 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000233 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000234 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000235 memset(ret, 0, sizeof(xmlXIncludeRef));
236 if (URI == NULL)
237 ret->URI = NULL;
238 else
239 ret->URI = xmlStrdup(URI);
240 ret->fragment = NULL;
241 ret->ref = ref;
Daniel Veillard24505b02005-07-28 23:49:35 +0000242 ret->doc = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000243 ret->count = 0;
244 ret->xml = 0;
245 ret->inc = NULL;
246 if (ctxt->incMax == 0) {
247 ctxt->incMax = 4;
248 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
249 sizeof(ctxt->incTab[0]));
250 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000251 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000252 xmlXIncludeFreeRef(ret);
253 return(NULL);
254 }
255 }
256 if (ctxt->incNr >= ctxt->incMax) {
257 ctxt->incMax *= 2;
258 ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
259 ctxt->incMax * sizeof(ctxt->incTab[0]));
260 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000261 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000262 xmlXIncludeFreeRef(ret);
263 return(NULL);
264 }
265 }
266 ctxt->incTab[ctxt->incNr++] = ret;
267 return(ret);
268}
269
270/**
Owen Taylor3473f882001-02-23 17:55:21 +0000271 * xmlXIncludeNewContext:
272 * @doc: an XML Document
273 *
274 * Creates a new XInclude context
275 *
276 * Returns the new set
277 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000278xmlXIncludeCtxtPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000279xmlXIncludeNewContext(xmlDocPtr doc) {
280 xmlXIncludeCtxtPtr ret;
281
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000282#ifdef DEBUG_XINCLUDE
283 xmlGenericError(xmlGenericErrorContext, "New context\n");
284#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000285 if (doc == NULL)
286 return(NULL);
287 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000288 if (ret == NULL) {
289 xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
290 "creating XInclude context");
Owen Taylor3473f882001-02-23 17:55:21 +0000291 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000292 }
Owen Taylor3473f882001-02-23 17:55:21 +0000293 memset(ret, 0, sizeof(xmlXIncludeCtxt));
294 ret->doc = doc;
295 ret->incNr = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000296 ret->incBase = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000297 ret->incMax = 0;
298 ret->incTab = NULL;
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000299 ret->nbErrors = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000300 return(ret);
301}
302
303/**
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000304 * xmlXIncludeURLPush:
305 * @ctxt: the parser context
306 * @value: the url
307 *
308 * Pushes a new url on top of the url stack
309 *
310 * Returns -1 in case of error, the index in the stack otherwise
311 */
312static int
313xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
314 const xmlChar *value)
315{
316 if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000317 xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION,
318 "detected a recursion in %s\n", value);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000319 return(-1);
320 }
321 if (ctxt->urlTab == NULL) {
322 ctxt->urlMax = 4;
323 ctxt->urlNr = 0;
324 ctxt->urlTab = (xmlChar * *) xmlMalloc(
325 ctxt->urlMax * sizeof(ctxt->urlTab[0]));
326 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000327 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000328 return (-1);
329 }
330 }
331 if (ctxt->urlNr >= ctxt->urlMax) {
332 ctxt->urlMax *= 2;
333 ctxt->urlTab =
334 (xmlChar * *) xmlRealloc(ctxt->urlTab,
335 ctxt->urlMax *
336 sizeof(ctxt->urlTab[0]));
337 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000338 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000339 return (-1);
340 }
341 }
342 ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
343 return (ctxt->urlNr++);
344}
345
346/**
347 * xmlXIncludeURLPop:
348 * @ctxt: the parser context
349 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000350 * Pops the top URL from the URL stack
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000351 */
352static void
353xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
354{
355 xmlChar * ret;
356
357 if (ctxt->urlNr <= 0)
358 return;
359 ctxt->urlNr--;
360 if (ctxt->urlNr > 0)
361 ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
362 else
363 ctxt->url = NULL;
364 ret = ctxt->urlTab[ctxt->urlNr];
Daniel Veillard24505b02005-07-28 23:49:35 +0000365 ctxt->urlTab[ctxt->urlNr] = NULL;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000366 if (ret != NULL)
367 xmlFree(ret);
368}
369
370/**
Owen Taylor3473f882001-02-23 17:55:21 +0000371 * xmlXIncludeFreeContext:
372 * @ctxt: the XInclude context
373 *
374 * Free an XInclude context
375 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000376void
Owen Taylor3473f882001-02-23 17:55:21 +0000377xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
378 int i;
379
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000380#ifdef DEBUG_XINCLUDE
381 xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
382#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000383 if (ctxt == NULL)
384 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000385 while (ctxt->urlNr > 0)
386 xmlXIncludeURLPop(ctxt);
387 if (ctxt->urlTab != NULL)
388 xmlFree(ctxt->urlTab);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000389 for (i = 0;i < ctxt->incNr;i++) {
390 if (ctxt->incTab[i] != NULL)
391 xmlXIncludeFreeRef(ctxt->incTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +0000392 }
Daniel Veillard11ce4002006-03-10 00:36:23 +0000393 if (ctxt->txturlTab != NULL) {
394 for (i = 0;i < ctxt->txtNr;i++) {
395 if (ctxt->txturlTab[i] != NULL)
396 xmlFree(ctxt->txturlTab[i]);
397 }
Owen Taylor3473f882001-02-23 17:55:21 +0000398 }
399 if (ctxt->incTab != NULL)
400 xmlFree(ctxt->incTab);
Owen Taylor3473f882001-02-23 17:55:21 +0000401 if (ctxt->txtTab != NULL)
402 xmlFree(ctxt->txtTab);
403 if (ctxt->txturlTab != NULL)
404 xmlFree(ctxt->txturlTab);
Daniel Veillardce244ad2004-11-05 10:03:46 +0000405 if (ctxt->base != NULL) {
406 xmlFree(ctxt->base);
407 }
Owen Taylor3473f882001-02-23 17:55:21 +0000408 xmlFree(ctxt);
409}
410
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000411/**
Daniel Veillard98485322003-08-14 15:44:40 +0000412 * xmlXIncludeParseFile:
413 * @ctxt: the XInclude context
414 * @URL: the URL or file path
415 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000416 * parse a document for XInclude
Daniel Veillard98485322003-08-14 15:44:40 +0000417 */
418static xmlDocPtr
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000419xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
Daniel Veillard98485322003-08-14 15:44:40 +0000420 xmlDocPtr ret;
421 xmlParserCtxtPtr pctxt;
422 char *directory = NULL;
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000423 xmlParserInputPtr inputStream;
Daniel Veillard98485322003-08-14 15:44:40 +0000424
425 xmlInitParser();
426
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000427 pctxt = xmlNewParserCtxt();
Daniel Veillard98485322003-08-14 15:44:40 +0000428 if (pctxt == NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000429 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
Daniel Veillard98485322003-08-14 15:44:40 +0000430 return(NULL);
431 }
Daniel Veillard681e9042006-09-29 09:16:00 +0000432
433 /*
434 * pass in the application data to the parser context.
435 */
436 pctxt->_private = ctxt->_private;
437
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000438 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000439 * try to ensure that new documents included are actually
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000440 * built with the same dictionary as the including document.
441 */
442 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL) &&
443 (pctxt->dict != NULL)) {
444 xmlDictFree(pctxt->dict);
445 pctxt->dict = ctxt->doc->dict;
446 xmlDictReference(pctxt->dict);
447 }
448
449 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
450
451 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
452 if (inputStream == NULL) {
453 xmlFreeParserCtxt(pctxt);
454 return(NULL);
455 }
456
457 inputPush(pctxt, inputStream);
Daniel Veillard98485322003-08-14 15:44:40 +0000458
459 if ((pctxt->directory == NULL) && (directory == NULL))
460 directory = xmlParserGetDirectory(URL);
461 if ((pctxt->directory == NULL) && (directory != NULL))
462 pctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
463
William M. Bracka22da292005-02-12 01:08:22 +0000464 pctxt->loadsubset |= XML_DETECT_IDS;
Daniel Veillard98485322003-08-14 15:44:40 +0000465
466 xmlParseDocument(pctxt);
467
William M. Brack1ff42132003-12-31 14:05:15 +0000468 if (pctxt->wellFormed) {
Daniel Veillard98485322003-08-14 15:44:40 +0000469 ret = pctxt->myDoc;
William M. Brack1ff42132003-12-31 14:05:15 +0000470 }
Daniel Veillard98485322003-08-14 15:44:40 +0000471 else {
472 ret = NULL;
Daniel Veillard500a1de2004-03-22 15:22:58 +0000473 if (pctxt->myDoc != NULL)
Daniel Veillard4773df22004-01-23 13:15:13 +0000474 xmlFreeDoc(pctxt->myDoc);
Daniel Veillard98485322003-08-14 15:44:40 +0000475 pctxt->myDoc = NULL;
476 }
477 xmlFreeParserCtxt(pctxt);
478
479 return(ret);
480}
481
482/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000483 * xmlXIncludeAddNode:
484 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000485 * @cur: the new node
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000486 *
487 * Add a new node to process to an XInclude context
488 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000489static int
490xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
491 xmlXIncludeRefPtr ref;
492 xmlURIPtr uri;
493 xmlChar *URL;
494 xmlChar *fragment = NULL;
495 xmlChar *href;
496 xmlChar *parse;
497 xmlChar *base;
498 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000499 int xml = 1, i; /* default Issue 64 */
500 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000501
502
503 if (ctxt == NULL)
504 return(-1);
505 if (cur == NULL)
506 return(-1);
507
508#ifdef DEBUG_XINCLUDE
509 xmlGenericError(xmlGenericErrorContext, "Add node\n");
510#endif
511 /*
512 * read the attributes
513 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000514 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000515 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000516 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
517 if (href == NULL)
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000518 return(-1);
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000519 local = 1;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000520 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000521 if (href[0] == '#')
522 local = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000523 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000524 if (parse != NULL) {
525 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
526 xml = 1;
527 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
528 xml = 0;
529 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000530 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
531 "invalid value %s for 'parse'\n", parse);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000532 if (href != NULL)
533 xmlFree(href);
534 if (parse != NULL)
535 xmlFree(parse);
536 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000537 }
538 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000539
540 /*
541 * compute the URI
542 */
543 base = xmlNodeGetBase(ctxt->doc, cur);
544 if (base == NULL) {
545 URI = xmlBuildURI(href, ctxt->doc->URL);
546 } else {
547 URI = xmlBuildURI(href, base);
548 }
549 if (URI == NULL) {
550 xmlChar *escbase;
551 xmlChar *eschref;
552 /*
553 * Some escaping may be needed
554 */
555 escbase = xmlURIEscape(base);
556 eschref = xmlURIEscape(href);
557 URI = xmlBuildURI(eschref, escbase);
558 if (escbase != NULL)
559 xmlFree(escbase);
560 if (eschref != NULL)
561 xmlFree(eschref);
562 }
563 if (parse != NULL)
564 xmlFree(parse);
565 if (href != NULL)
566 xmlFree(href);
567 if (base != NULL)
568 xmlFree(base);
569 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000570 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
571 "failed build URL\n", NULL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000572 return(-1);
573 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000574 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000575
576 /*
577 * Check the URL and remove any fragment identifier
578 */
579 uri = xmlParseURI((const char *)URI);
580 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000581 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
582 "invalid value URI %s\n", URI);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000583 if (fragment != NULL)
584 xmlFree(fragment);
585 xmlFree(URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000586 return(-1);
587 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000588
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000589 if (uri->fragment != NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000590 if (ctxt->legacy != 0) {
591 if (fragment == NULL) {
592 fragment = (xmlChar *) uri->fragment;
593 } else {
594 xmlFree(uri->fragment);
595 }
596 } else {
597 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
598 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
599 URI);
600 if (fragment != NULL)
601 xmlFree(fragment);
602 xmlFreeURI(uri);
603 xmlFree(URI);
604 return(-1);
605 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000606 uri->fragment = NULL;
607 }
608 URL = xmlSaveUri(uri);
609 xmlFreeURI(uri);
610 xmlFree(URI);
611 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000612 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
613 "invalid value URI %s\n", URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000614 if (fragment != NULL)
615 xmlFree(fragment);
616 return(-1);
617 }
618
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000619 /*
620 * Check the URL against the stack for recursions
621 */
Daniel Veillardbf630c02006-06-06 08:21:41 +0000622 if ((!local) && (xml == 1)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000623 for (i = 0;i < ctxt->urlNr;i++) {
624 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000625 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
626 "detected a recursion in %s\n", URL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000627 return(-1);
628 }
629 }
630 }
631
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000632 ref = xmlXIncludeNewRef(ctxt, URL, cur);
633 if (ref == NULL) {
634 return(-1);
635 }
636 ref->fragment = fragment;
637 ref->doc = NULL;
638 ref->xml = xml;
639 ref->count = 1;
640 xmlFree(URL);
641 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000642}
643
644/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000645 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000646 * @ctxt: the XInclude context
647 * @doc: the new document
648 * @url: the associated URL
649 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000650 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000651 */
652static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000653xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000654 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000655 xmlXIncludeCtxtPtr newctxt;
656 int i;
657
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000658 /*
659 * Avoid recursion in already substitued resources
660 for (i = 0;i < ctxt->urlNr;i++) {
661 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
662 return;
663 }
664 */
665
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000666#ifdef DEBUG_XINCLUDE
667 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
668#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000669 /*
670 * Handle recursion here.
671 */
672
673 newctxt = xmlXIncludeNewContext(doc);
674 if (newctxt != NULL) {
675 /*
676 * Copy the existing document set
677 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000678 newctxt->incMax = ctxt->incMax;
679 newctxt->incNr = ctxt->incNr;
680 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
681 sizeof(newctxt->incTab[0]));
682 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000683 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000684 xmlFree(newctxt);
685 return;
686 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000687 /*
688 * copy the urlTab
689 */
690 newctxt->urlMax = ctxt->urlMax;
691 newctxt->urlNr = ctxt->urlNr;
692 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000693
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000694 /*
William M. Brackf7789b12004-06-07 08:57:27 +0000695 * Inherit the existing base
696 */
Daniel Veillardce244ad2004-11-05 10:03:46 +0000697 newctxt->base = xmlStrdup(ctxt->base);
William M. Brackf7789b12004-06-07 08:57:27 +0000698
699 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000700 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000701 */
702 newctxt->incBase = ctxt->incNr;
703 for (i = 0;i < ctxt->incNr;i++) {
704 newctxt->incTab[i] = ctxt->incTab[i];
705 newctxt->incTab[i]->count++; /* prevent the recursion from
706 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000707 }
William M. Bracka11e4832004-03-07 11:03:43 +0000708 /*
709 * The new context should also inherit the Parse Flags
710 * (bug 132597)
711 */
712 newctxt->parseFlags = ctxt->parseFlags;
Daniel Veillard8edf1c52003-07-22 20:52:14 +0000713 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000714 for (i = 0;i < ctxt->incNr;i++) {
715 newctxt->incTab[i]->count--;
716 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000717 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000718
719 /* urlTab may have been reallocated */
720 ctxt->urlTab = newctxt->urlTab;
721 ctxt->urlMax = newctxt->urlMax;
722
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000723 newctxt->urlMax = 0;
724 newctxt->urlNr = 0;
725 newctxt->urlTab = NULL;
726
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000727 xmlXIncludeFreeContext(newctxt);
728 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000729#ifdef DEBUG_XINCLUDE
730 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
731#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000732}
733
734/**
735 * xmlXIncludeAddTxt:
736 * @ctxt: the XInclude context
737 * @txt: the new text node
738 * @url: the associated URL
739 *
740 * Add a new txtument to the list
741 */
742static void
743xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000744#ifdef DEBUG_XINCLUDE
745 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
746#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000747 if (ctxt->txtMax == 0) {
748 ctxt->txtMax = 4;
749 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
750 sizeof(ctxt->txtTab[0]));
751 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000752 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000753 return;
754 }
755 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
756 sizeof(ctxt->txturlTab[0]));
757 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000758 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000759 return;
760 }
761 }
762 if (ctxt->txtNr >= ctxt->txtMax) {
763 ctxt->txtMax *= 2;
764 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
765 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
766 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000767 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000768 return;
769 }
770 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000771 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000772 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000773 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000774 return;
775 }
776 }
777 ctxt->txtTab[ctxt->txtNr] = txt;
778 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
779 ctxt->txtNr++;
780}
781
Owen Taylor3473f882001-02-23 17:55:21 +0000782/************************************************************************
783 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000784 * Node copy with specific semantic *
785 * *
786 ************************************************************************/
787
788/**
789 * xmlXIncludeCopyNode:
790 * @ctxt: the XInclude context
791 * @target: the document target
792 * @source: the document source
793 * @elem: the element
794 *
795 * Make a copy of the node while preserving the XInclude semantic
796 * of the Infoset copy
797 */
798static xmlNodePtr
799xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
800 xmlDocPtr source, xmlNodePtr elem) {
801 xmlNodePtr result = NULL;
802
803 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
804 (elem == NULL))
805 return(NULL);
806 if (elem->type == XML_DTD_NODE)
807 return(NULL);
808 result = xmlDocCopyNode(elem, target, 1);
809 return(result);
810}
811
812/**
813 * xmlXIncludeCopyNodeList:
814 * @ctxt: the XInclude context
815 * @target: the document target
816 * @source: the document source
817 * @elem: the element list
818 *
819 * Make a copy of the node list while preserving the XInclude semantic
820 * of the Infoset copy
821 */
822static xmlNodePtr
823xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
824 xmlDocPtr source, xmlNodePtr elem) {
825 xmlNodePtr cur, res, result = NULL, last = NULL;
826
827 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
828 (elem == NULL))
829 return(NULL);
830 cur = elem;
831 while (cur != NULL) {
832 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
833 if (res != NULL) {
834 if (result == NULL) {
835 result = last = res;
836 } else {
837 last->next = res;
838 res->prev = last;
839 last = res;
840 }
841 }
842 cur = cur->next;
843 }
844 return(result);
845}
846
847/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000848 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000849 * @cur: the node
850 * @no: the child number
851 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000852 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000853 */
854static xmlNodePtr
855xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
856 int i;
857 if (cur == NULL)
858 return(cur);
859 cur = cur->children;
860 for (i = 0;i <= no;cur = cur->next) {
861 if (cur == NULL)
862 return(cur);
863 if ((cur->type == XML_ELEMENT_NODE) ||
864 (cur->type == XML_DOCUMENT_NODE) ||
865 (cur->type == XML_HTML_DOCUMENT_NODE)) {
866 i++;
867 if (i == no)
868 break;
869 }
870 }
871 return(cur);
872}
873
William M. Brackf7eb7942003-12-31 07:59:17 +0000874xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000875/**
876 * xmlXIncludeCopyRange:
877 * @ctxt: the XInclude context
878 * @target: the document target
879 * @source: the document source
880 * @obj: the XPointer result from the evaluation.
881 *
882 * Build a node list tree copy of the XPointer result.
883 *
884 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000885 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000886 */
887static xmlNodePtr
888xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
889 xmlDocPtr source, xmlXPathObjectPtr range) {
890 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000891 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
892 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000893 /* pointers to traversal nodes */
894 xmlNodePtr start, cur, end;
895 int index1, index2;
William M. Brack6bdacd72004-02-07 08:53:23 +0000896 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000897
898 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
899 (range == NULL))
900 return(NULL);
901 if (range->type != XPATH_RANGE)
902 return(NULL);
903 start = (xmlNodePtr) range->user;
904
905 if (start == NULL)
906 return(NULL);
907 end = range->user2;
908 if (end == NULL)
909 return(xmlDocCopyNode(start, target, 1));
910
911 cur = start;
912 index1 = range->index;
913 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000914 /*
915 * level is depth of the current node under consideration
916 * list is the pointer to the root of the output tree
917 * listParent is a pointer to the parent of output tree (within
918 the included file) in case we need to add another level
919 * last is a pointer to the last node added to the output tree
920 * lastLevel is the depth of last (relative to the root)
921 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000922 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000923 /*
924 * Check if our output tree needs a parent
925 */
926 if (level < 0) {
927 while (level < 0) {
William M. Brack57e9e912004-03-09 16:19:02 +0000928 /* copy must include namespaces and properties */
929 tmp2 = xmlDocCopyNode(listParent, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000930 xmlAddChild(tmp2, list);
931 list = tmp2;
932 listParent = listParent->parent;
933 level++;
934 }
935 last = list;
936 lastLevel = 0;
937 }
938 /*
939 * Check whether we need to change our insertion point
940 */
941 while (level < lastLevel) {
942 last = last->parent;
943 lastLevel --;
944 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000945 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000946 if (cur->type == XML_TEXT_NODE) {
947 const xmlChar *content = cur->content;
948 int len;
949
950 if (content == NULL) {
951 tmp = xmlNewTextLen(NULL, 0);
952 } else {
953 len = index2;
954 if ((cur == start) && (index1 > 1)) {
955 content += (index1 - 1);
956 len -= (index1 - 1);
957 index1 = 0;
958 } else {
959 len = index2;
960 }
961 tmp = xmlNewTextLen(content, len);
962 }
963 /* single sub text node selection */
964 if (list == NULL)
965 return(tmp);
966 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +0000967 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000968 xmlAddNextSibling(last, tmp);
969 else
William M. Brackf7eb7942003-12-31 07:59:17 +0000970 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000971 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +0000972 } else { /* ending node not a text node */
William M. Brack6bdacd72004-02-07 08:53:23 +0000973 endLevel = level; /* remember the level of the end node */
974 endFlag = 1;
William M. Brack57e9e912004-03-09 16:19:02 +0000975 /* last node - need to take care of properties + namespaces */
976 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000977 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000978 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000979 listParent = cur->parent;
980 } else {
981 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000982 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +0000983 else {
984 xmlAddChild(last, tmp);
985 lastLevel = level;
986 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000987 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000988 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000989
990 if (index2 > 1) {
991 end = xmlXIncludeGetNthChild(cur, index2 - 1);
992 index2 = 0;
993 }
994 if ((cur == start) && (index1 > 1)) {
995 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
996 index1 = 0;
William M. Brack6bdacd72004-02-07 08:53:23 +0000997 } else {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000998 cur = cur->children;
999 }
William M. Brack6bdacd72004-02-07 08:53:23 +00001000 level++; /* increment level to show change */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001001 /*
1002 * Now gather the remaining nodes from cur to end
1003 */
William M. Brack6bdacd72004-02-07 08:53:23 +00001004 continue; /* while */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001005 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001006 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001007 if ((cur->type == XML_TEXT_NODE) ||
1008 (cur->type == XML_CDATA_SECTION_NODE)) {
1009 const xmlChar *content = cur->content;
1010
1011 if (content == NULL) {
1012 tmp = xmlNewTextLen(NULL, 0);
1013 } else {
1014 if (index1 > 1) {
1015 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +00001016 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001017 }
1018 tmp = xmlNewText(content);
1019 }
1020 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001021 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001022 } else { /* Not text node */
William M. Brack57e9e912004-03-09 16:19:02 +00001023 /*
1024 * start of the range - need to take care of
1025 * properties and namespaces
1026 */
1027 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +00001028 list = last = tmp;
1029 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001030 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001031 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001032 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001033 index1 = 0;
1034 /*
1035 * Now gather the remaining nodes from cur to end
1036 */
1037 continue; /* while */
1038 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001039 }
1040 } else {
1041 tmp = NULL;
1042 switch (cur->type) {
1043 case XML_DTD_NODE:
1044 case XML_ELEMENT_DECL:
1045 case XML_ATTRIBUTE_DECL:
1046 case XML_ENTITY_NODE:
1047 /* Do not copy DTD informations */
1048 break;
1049 case XML_ENTITY_DECL:
1050 /* handle crossing entities -> stack needed */
1051 break;
1052 case XML_XINCLUDE_START:
1053 case XML_XINCLUDE_END:
1054 /* don't consider it part of the tree content */
1055 break;
1056 case XML_ATTRIBUTE_NODE:
1057 /* Humm, should not happen ! */
1058 break;
1059 default:
William M. Brack57e9e912004-03-09 16:19:02 +00001060 /*
1061 * Middle of the range - need to take care of
1062 * properties and namespaces
1063 */
1064 tmp = xmlDocCopyNode(cur, target, 2);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001065 break;
1066 }
1067 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001068 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001069 xmlAddNextSibling(last, tmp);
1070 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001071 xmlAddChild(last, tmp);
1072 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001073 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001074 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001075 }
1076 }
1077 /*
1078 * Skip to next node in document order
1079 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001080 cur = xmlXPtrAdvanceNode(cur, &level);
William M. Brack6bdacd72004-02-07 08:53:23 +00001081 if (endFlag && (level >= endLevel))
1082 break;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001083 }
1084 return(list);
1085}
1086
1087/**
1088 * xmlXIncludeBuildNodeList:
1089 * @ctxt: the XInclude context
1090 * @target: the document target
1091 * @source: the document source
1092 * @obj: the XPointer result from the evaluation.
1093 *
1094 * Build a node list tree copy of the XPointer result.
1095 * This will drop Attributes and Namespace declarations.
1096 *
1097 * Returns an xmlNodePtr list or NULL.
1098 * the caller has to free the node tree.
1099 */
1100static xmlNodePtr
1101xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1102 xmlDocPtr source, xmlXPathObjectPtr obj) {
1103 xmlNodePtr list = NULL, last = NULL;
1104 int i;
1105
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001106 if (source == NULL)
1107 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001108 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1109 (obj == NULL))
1110 return(NULL);
1111 switch (obj->type) {
1112 case XPATH_NODESET: {
1113 xmlNodeSetPtr set = obj->nodesetval;
1114 if (set == NULL)
1115 return(NULL);
1116 for (i = 0;i < set->nodeNr;i++) {
1117 if (set->nodeTab[i] == NULL)
1118 continue;
1119 switch (set->nodeTab[i]->type) {
1120 case XML_TEXT_NODE:
1121 case XML_CDATA_SECTION_NODE:
1122 case XML_ELEMENT_NODE:
1123 case XML_ENTITY_REF_NODE:
1124 case XML_ENTITY_NODE:
1125 case XML_PI_NODE:
1126 case XML_COMMENT_NODE:
1127 case XML_DOCUMENT_NODE:
1128 case XML_HTML_DOCUMENT_NODE:
1129#ifdef LIBXML_DOCB_ENABLED
1130 case XML_DOCB_DOCUMENT_NODE:
1131#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001132 case XML_XINCLUDE_END:
1133 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001134 case XML_XINCLUDE_START: {
1135 xmlNodePtr tmp, cur = set->nodeTab[i];
1136
1137 cur = cur->next;
1138 while (cur != NULL) {
1139 switch(cur->type) {
1140 case XML_TEXT_NODE:
1141 case XML_CDATA_SECTION_NODE:
1142 case XML_ELEMENT_NODE:
1143 case XML_ENTITY_REF_NODE:
1144 case XML_ENTITY_NODE:
1145 case XML_PI_NODE:
1146 case XML_COMMENT_NODE:
1147 tmp = xmlXIncludeCopyNode(ctxt, target,
1148 source, cur);
1149 if (last == NULL) {
1150 list = last = tmp;
1151 } else {
1152 xmlAddNextSibling(last, tmp);
1153 last = tmp;
1154 }
1155 cur = cur->next;
1156 continue;
1157 default:
1158 break;
1159 }
1160 break;
1161 }
1162 continue;
1163 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001164 case XML_ATTRIBUTE_NODE:
1165 case XML_NAMESPACE_DECL:
1166 case XML_DOCUMENT_TYPE_NODE:
1167 case XML_DOCUMENT_FRAG_NODE:
1168 case XML_NOTATION_NODE:
1169 case XML_DTD_NODE:
1170 case XML_ELEMENT_DECL:
1171 case XML_ATTRIBUTE_DECL:
1172 case XML_ENTITY_DECL:
1173 continue; /* for */
1174 }
1175 if (last == NULL)
1176 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1177 set->nodeTab[i]);
1178 else {
1179 xmlAddNextSibling(last,
1180 xmlXIncludeCopyNode(ctxt, target, source,
1181 set->nodeTab[i]));
1182 if (last->next != NULL)
1183 last = last->next;
1184 }
1185 }
1186 break;
1187 }
1188 case XPATH_LOCATIONSET: {
1189 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1190 if (set == NULL)
1191 return(NULL);
1192 for (i = 0;i < set->locNr;i++) {
1193 if (last == NULL)
1194 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1195 set->locTab[i]);
1196 else
1197 xmlAddNextSibling(last,
1198 xmlXIncludeCopyXPointer(ctxt, target, source,
1199 set->locTab[i]));
1200 if (last != NULL) {
1201 while (last->next != NULL)
1202 last = last->next;
1203 }
1204 }
1205 break;
1206 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001207#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001208 case XPATH_RANGE:
1209 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001210#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001211 case XPATH_POINT:
1212 /* points are ignored in XInclude */
1213 break;
1214 default:
1215 break;
1216 }
1217 return(list);
1218}
1219/************************************************************************
1220 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001221 * XInclude I/O handling *
1222 * *
1223 ************************************************************************/
1224
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001225typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1226typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1227struct _xmlXIncludeMergeData {
1228 xmlDocPtr doc;
1229 xmlXIncludeCtxtPtr ctxt;
1230};
1231
Owen Taylor3473f882001-02-23 17:55:21 +00001232/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001233 * xmlXIncludeMergeOneEntity:
1234 * @ent: the entity
1235 * @doc: the including doc
1236 * @nr: the entity name
1237 *
1238 * Inplements the merge of one entity
1239 */
1240static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001241xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001242 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001243 xmlEntityPtr ret, prev;
1244 xmlDocPtr doc;
1245 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001246
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001247 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001248 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001249 ctxt = data->ctxt;
1250 doc = data->doc;
1251 if ((ctxt == NULL) || (doc == NULL))
1252 return;
1253 switch (ent->etype) {
1254 case XML_INTERNAL_PARAMETER_ENTITY:
1255 case XML_EXTERNAL_PARAMETER_ENTITY:
1256 case XML_INTERNAL_PREDEFINED_ENTITY:
1257 return;
1258 case XML_INTERNAL_GENERAL_ENTITY:
1259 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1260 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1261 break;
1262 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001263 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1264 ent->SystemID, ent->content);
1265 if (ret != NULL) {
1266 if (ent->URI != NULL)
1267 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001268 } else {
1269 prev = xmlGetDocEntity(doc, ent->name);
1270 if (prev != NULL) {
1271 if (ent->etype != prev->etype)
1272 goto error;
1273
1274 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1275 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1276 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001277 } else if ((ent->ExternalID != NULL) &&
1278 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001279 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1280 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001281 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1282 if (!xmlStrEqual(ent->content, prev->content))
1283 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001284 } else {
1285 goto error;
1286 }
1287
1288 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001289 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001290 return;
1291error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001292 switch (ent->etype) {
1293 case XML_INTERNAL_PARAMETER_ENTITY:
1294 case XML_EXTERNAL_PARAMETER_ENTITY:
1295 case XML_INTERNAL_PREDEFINED_ENTITY:
1296 case XML_INTERNAL_GENERAL_ENTITY:
1297 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1298 return;
1299 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1300 break;
1301 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001302 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1303 "mismatch in redefinition of entity %s\n",
1304 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001305}
1306
1307/**
1308 * xmlXIncludeMergeEntities:
1309 * @ctxt: an XInclude context
1310 * @doc: the including doc
1311 * @from: the included doc
1312 *
1313 * Inplements the entity merge
1314 *
1315 * Returns 0 if merge succeeded, -1 if some processing failed
1316 */
1317static int
1318xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1319 xmlDocPtr from) {
1320 xmlNodePtr cur;
1321 xmlDtdPtr target, source;
1322
1323 if (ctxt == NULL)
1324 return(-1);
1325
1326 if ((from == NULL) || (from->intSubset == NULL))
1327 return(0);
1328
1329 target = doc->intSubset;
1330 if (target == NULL) {
1331 cur = xmlDocGetRootElement(doc);
1332 if (cur == NULL)
1333 return(-1);
1334 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1335 if (target == NULL)
1336 return(-1);
1337 }
1338
1339 source = from->intSubset;
1340 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001341 xmlXIncludeMergeData data;
1342
1343 data.ctxt = ctxt;
1344 data.doc = doc;
1345
Daniel Veillard4287c572003-02-04 22:48:53 +00001346 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001347 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001348 }
1349 source = from->extSubset;
1350 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001351 xmlXIncludeMergeData data;
1352
1353 data.ctxt = ctxt;
1354 data.doc = doc;
1355
Daniel Veillard4287c572003-02-04 22:48:53 +00001356 /*
1357 * don't duplicate existing stuff when external subsets are the same
1358 */
1359 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1360 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1361 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001362 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001363 }
1364 }
1365 return(0);
1366}
1367
1368/**
Owen Taylor3473f882001-02-23 17:55:21 +00001369 * xmlXIncludeLoadDoc:
1370 * @ctxt: the XInclude context
1371 * @url: the associated URL
1372 * @nr: the xinclude node number
1373 *
1374 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001375 *
1376 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001377 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001378static int
Owen Taylor3473f882001-02-23 17:55:21 +00001379xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1380 xmlDocPtr doc;
1381 xmlURIPtr uri;
1382 xmlChar *URL;
1383 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001384 int i = 0;
William M. Brack4d59e222004-03-08 14:42:31 +00001385#ifdef LIBXML_XPTR_ENABLED
1386 int saveFlags;
1387#endif
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001388
1389#ifdef DEBUG_XINCLUDE
1390 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1391#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001392 /*
1393 * Check the URL and remove any fragment identifier
1394 */
1395 uri = xmlParseURI((const char *)url);
1396 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001397 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1398 XML_XINCLUDE_HREF_URI,
1399 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001400 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001401 }
1402 if (uri->fragment != NULL) {
1403 fragment = (xmlChar *) uri->fragment;
1404 uri->fragment = NULL;
1405 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001406 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1407 (ctxt->incTab[nr]->fragment != NULL)) {
1408 if (fragment != NULL) xmlFree(fragment);
1409 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1410 }
Owen Taylor3473f882001-02-23 17:55:21 +00001411 URL = xmlSaveUri(uri);
1412 xmlFreeURI(uri);
1413 if (URL == NULL) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00001414 if (ctxt->incTab != NULL)
1415 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1416 XML_XINCLUDE_HREF_URI,
1417 "invalid value URI %s\n", url);
1418 else
1419 xmlXIncludeErr(ctxt, NULL,
1420 XML_XINCLUDE_HREF_URI,
1421 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001422 if (fragment != NULL)
1423 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001424 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001425 }
1426
1427 /*
1428 * Handling of references to the local document are done
1429 * directly through ctxt->doc.
1430 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001431 if ((URL[0] == 0) || (URL[0] == '#') ||
1432 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001433 doc = NULL;
1434 goto loaded;
1435 }
1436
1437 /*
1438 * Prevent reloading twice the document.
1439 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001440 for (i = 0; i < ctxt->incNr; i++) {
1441 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1442 (ctxt->incTab[i]->doc != NULL)) {
1443 doc = ctxt->incTab[i]->doc;
1444#ifdef DEBUG_XINCLUDE
1445 printf("Already loaded %s\n", URL);
1446#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001447 goto loaded;
1448 }
1449 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001450
Owen Taylor3473f882001-02-23 17:55:21 +00001451 /*
1452 * Load it.
1453 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001454#ifdef DEBUG_XINCLUDE
1455 printf("loading %s\n", URL);
1456#endif
William M. Brack4d59e222004-03-08 14:42:31 +00001457#ifdef LIBXML_XPTR_ENABLED
1458 /*
1459 * If this is an XPointer evaluation, we want to assure that
1460 * all entities have been resolved prior to processing the
1461 * referenced document
1462 */
1463 saveFlags = ctxt->parseFlags;
1464 if (fragment != NULL) { /* if this is an XPointer eval */
1465 ctxt->parseFlags |= XML_PARSE_NOENT;
1466 }
1467#endif
1468
Daniel Veillard98485322003-08-14 15:44:40 +00001469 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
William M. Brack4d59e222004-03-08 14:42:31 +00001470#ifdef LIBXML_XPTR_ENABLED
1471 ctxt->parseFlags = saveFlags;
1472#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001473 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001474 xmlFree(URL);
1475 if (fragment != NULL)
1476 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001477 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001478 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001479 ctxt->incTab[nr]->doc = doc;
William M. Brackb85c9202004-07-26 00:20:13 +00001480 /*
1481 * It's possible that the requested URL has been mapped to a
1482 * completely different location (e.g. through a catalog entry).
1483 * To check for this, we compare the URL with that of the doc
1484 * and change it if they disagree (bug 146988).
1485 */
1486 if (!xmlStrEqual(URL, doc->URL)) {
1487 xmlFree(URL);
1488 URL = xmlStrdup(doc->URL);
1489 }
Daniel Veillard98485322003-08-14 15:44:40 +00001490 for (i = nr + 1; i < ctxt->incNr; i++) {
1491 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1492 ctxt->incTab[nr]->count++;
1493#ifdef DEBUG_XINCLUDE
1494 printf("Increasing %s count since reused\n", URL);
1495#endif
1496 break;
1497 }
1498 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001499
1500 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001501 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001502 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001503 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001504
1505 /*
1506 * We don't need the DTD anymore, free up space
1507 if (doc->intSubset != NULL) {
1508 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1509 xmlFreeNode((xmlNodePtr) doc->intSubset);
1510 doc->intSubset = NULL;
1511 }
1512 if (doc->extSubset != NULL) {
1513 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1514 xmlFreeNode((xmlNodePtr) doc->extSubset);
1515 doc->extSubset = NULL;
1516 }
1517 */
1518 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001519
1520loaded:
1521 if (fragment == NULL) {
1522 /*
1523 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001524 */
1525 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001526 {
1527 /* Hopefully a DTD declaration won't be copied from
1528 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001529 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001530 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001531 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001532 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001533 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001534 }
1535#ifdef LIBXML_XPTR_ENABLED
1536 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001537 /*
1538 * Computes the XPointer expression and make a copy used
1539 * as the replacement copy.
1540 */
1541 xmlXPathObjectPtr xptr;
1542 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001543 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001544
1545 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001546 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1547 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001548 } else {
1549 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1550 }
1551 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001552 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1553 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001554 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001555 xmlFree(URL);
1556 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001557 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001558 }
1559 xptr = xmlXPtrEval(fragment, xptrctxt);
1560 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001561 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1562 XML_XINCLUDE_XPTR_FAILED,
1563 "XPointer evaluation failed: #%s\n",
1564 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001565 xmlXPathFreeContext(xptrctxt);
1566 xmlFree(URL);
1567 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001568 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001569 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001570 switch (xptr->type) {
1571 case XPATH_UNDEFINED:
1572 case XPATH_BOOLEAN:
1573 case XPATH_NUMBER:
1574 case XPATH_STRING:
1575 case XPATH_POINT:
1576 case XPATH_USERS:
1577 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001578 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1579 XML_XINCLUDE_XPTR_RESULT,
1580 "XPointer is not a range: #%s\n",
1581 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001582 xmlXPathFreeContext(xptrctxt);
1583 xmlFree(URL);
1584 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001585 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001586 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001587 if ((xptr->nodesetval == NULL) ||
1588 (xptr->nodesetval->nodeNr <= 0)) {
1589 xmlXPathFreeContext(xptrctxt);
1590 xmlFree(URL);
1591 xmlFree(fragment);
1592 return(-1);
1593 }
William M. Brackabf598b2004-06-08 02:01:28 +00001594
Daniel Veillard39196eb2001-06-19 18:09:42 +00001595 case XPATH_RANGE:
1596 case XPATH_LOCATIONSET:
1597 break;
1598 }
1599 set = xptr->nodesetval;
1600 if (set != NULL) {
1601 for (i = 0;i < set->nodeNr;i++) {
1602 if (set->nodeTab[i] == NULL)
1603 continue;
1604 switch (set->nodeTab[i]->type) {
1605 case XML_TEXT_NODE:
1606 case XML_CDATA_SECTION_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001607 case XML_ENTITY_REF_NODE:
1608 case XML_ENTITY_NODE:
1609 case XML_PI_NODE:
1610 case XML_COMMENT_NODE:
1611 case XML_DOCUMENT_NODE:
1612 case XML_HTML_DOCUMENT_NODE:
1613#ifdef LIBXML_DOCB_ENABLED
1614 case XML_DOCB_DOCUMENT_NODE:
1615#endif
1616 continue;
William M. Brackabf598b2004-06-08 02:01:28 +00001617 case XML_ELEMENT_NODE: {
1618 xmlChar *nodeBase;
1619 xmlNodePtr el = set->nodeTab[i];
1620
1621 nodeBase = xmlNodeGetBase(el->doc, el);
1622 if (nodeBase != NULL) {
1623 if (!xmlStrEqual(nodeBase, el->doc->URL))
1624 xmlNodeSetBase(el, nodeBase);
1625 xmlFree(nodeBase);
1626 }
1627 continue;
1628 }
1629
Daniel Veillard39196eb2001-06-19 18:09:42 +00001630 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001631 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1632 XML_XINCLUDE_XPTR_RESULT,
1633 "XPointer selects an attribute: #%s\n",
1634 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001635 set->nodeTab[i] = NULL;
1636 continue;
1637 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001638 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1639 XML_XINCLUDE_XPTR_RESULT,
1640 "XPointer selects a namespace: #%s\n",
1641 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001642 set->nodeTab[i] = NULL;
1643 continue;
1644 case XML_DOCUMENT_TYPE_NODE:
1645 case XML_DOCUMENT_FRAG_NODE:
1646 case XML_NOTATION_NODE:
1647 case XML_DTD_NODE:
1648 case XML_ELEMENT_DECL:
1649 case XML_ATTRIBUTE_DECL:
1650 case XML_ENTITY_DECL:
1651 case XML_XINCLUDE_START:
1652 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001653 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1654 XML_XINCLUDE_XPTR_RESULT,
1655 "XPointer selects unexpected nodes: #%s\n",
1656 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001657 set->nodeTab[i] = NULL;
1658 set->nodeTab[i] = NULL;
1659 continue; /* for */
1660 }
1661 }
1662 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001663 if (doc == NULL) {
1664 ctxt->incTab[nr]->xptr = xptr;
1665 ctxt->incTab[nr]->inc = NULL;
1666 } else {
1667 ctxt->incTab[nr]->inc =
1668 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1669 xmlXPathFreeObject(xptr);
1670 }
Owen Taylor3473f882001-02-23 17:55:21 +00001671 xmlXPathFreeContext(xptrctxt);
1672 xmlFree(fragment);
1673 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001674#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001675
1676 /*
1677 * Do the xml:base fixup if needed
1678 */
1679 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1680 xmlNodePtr node;
William M. Brack0b13a092005-06-01 03:37:59 +00001681 xmlChar *base;
William M. Brackabf598b2004-06-08 02:01:28 +00001682 xmlChar *curBase;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001683
William M. Brackf7789b12004-06-07 08:57:27 +00001684 /*
William M. Brack0b13a092005-06-01 03:37:59 +00001685 * The base is only adjusted if "necessary", i.e. if the xinclude node
1686 * has a base specified, or the URL is relative
William M. Brackf7789b12004-06-07 08:57:27 +00001687 */
William M. Brack0b13a092005-06-01 03:37:59 +00001688 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1689 XML_XML_NAMESPACE);
1690 if (base == NULL) {
1691 /*
1692 * No xml:base on the xinclude node, so we check whether the
1693 * URI base is different than (relative to) the context base
1694 */
1695 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1696 if (curBase == NULL) { /* Error return */
1697 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
William M. Brackf7789b12004-06-07 08:57:27 +00001698 XML_XINCLUDE_HREF_URI,
1699 "trying to build relative URI from %s\n", URL);
William M. Brack0b13a092005-06-01 03:37:59 +00001700 } else {
1701 /* If the URI doesn't contain a slash, it's not relative */
1702 if (!xmlStrchr(curBase, (xmlChar) '/'))
1703 xmlFree(curBase);
1704 else
1705 base = curBase;
William M. Brackf7789b12004-06-07 08:57:27 +00001706 }
William M. Brack0b13a092005-06-01 03:37:59 +00001707 }
1708 if (base != NULL) { /* Adjustment may be needed */
1709 node = ctxt->incTab[nr]->inc;
1710 while (node != NULL) {
1711 /* Only work on element nodes */
1712 if (node->type == XML_ELEMENT_NODE) {
1713 curBase = xmlNodeGetBase(node->doc, node);
1714 /* If no current base, set it */
1715 if (curBase == NULL) {
1716 xmlNodeSetBase(node, base);
1717 } else {
1718 /*
1719 * If the current base is the same as the
1720 * URL of the document, then reset it to be
1721 * the specified xml:base or the relative URI
1722 */
1723 if (xmlStrEqual(curBase, node->doc->URL)) {
1724 xmlNodeSetBase(node, base);
1725 } else {
1726 /*
1727 * If the element already has an xml:base
1728 * set, then relativise it if necessary
1729 */
1730 xmlChar *xmlBase;
1731 xmlBase = xmlGetNsProp(node,
1732 BAD_CAST "base",
1733 XML_XML_NAMESPACE);
1734 if (xmlBase != NULL) {
1735 xmlChar *relBase;
1736 relBase = xmlBuildURI(xmlBase, base);
1737 if (relBase == NULL) { /* error */
1738 xmlXIncludeErr(ctxt,
1739 ctxt->incTab[nr]->ref,
1740 XML_XINCLUDE_HREF_URI,
1741 "trying to rebuild base from %s\n",
1742 xmlBase);
1743 } else {
1744 xmlNodeSetBase(node, relBase);
1745 xmlFree(relBase);
1746 }
1747 xmlFree(xmlBase);
1748 }
1749 }
1750 xmlFree(curBase);
1751 }
1752 }
1753 node = node->next;
1754 }
1755 xmlFree(base);
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001756 }
1757 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001758 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1759 (ctxt->incTab[nr]->count <= 1)) {
1760#ifdef DEBUG_XINCLUDE
1761 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1762#endif
1763 xmlFreeDoc(ctxt->incTab[nr]->doc);
1764 ctxt->incTab[nr]->doc = NULL;
1765 }
Owen Taylor3473f882001-02-23 17:55:21 +00001766 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001767 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001768}
1769
1770/**
1771 * xmlXIncludeLoadTxt:
1772 * @ctxt: the XInclude context
1773 * @url: the associated URL
1774 * @nr: the xinclude node number
1775 *
1776 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001777 *
1778 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001779 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001780static int
Owen Taylor3473f882001-02-23 17:55:21 +00001781xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1782 xmlParserInputBufferPtr buf;
1783 xmlNodePtr node;
1784 xmlURIPtr uri;
1785 xmlChar *URL;
1786 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001787 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001788 xmlCharEncoding enc = (xmlCharEncoding) 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001789
Owen Taylor3473f882001-02-23 17:55:21 +00001790 /*
1791 * Check the URL and remove any fragment identifier
1792 */
1793 uri = xmlParseURI((const char *)url);
1794 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001795 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1796 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001797 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001798 }
1799 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001800 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1801 "fragment identifier forbidden for text: %s\n",
1802 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001803 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001804 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001805 }
1806 URL = xmlSaveUri(uri);
1807 xmlFreeURI(uri);
1808 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001809 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1810 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001811 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001812 }
1813
1814 /*
1815 * Handling of references to the local document are done
1816 * directly through ctxt->doc.
1817 */
1818 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001819 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1820 XML_XINCLUDE_TEXT_DOCUMENT,
1821 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001822 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001823 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001824 }
1825
1826 /*
1827 * Prevent reloading twice the document.
1828 */
1829 for (i = 0; i < ctxt->txtNr; i++) {
1830 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1831 node = xmlCopyNode(ctxt->txtTab[i], 1);
1832 goto loaded;
1833 }
1834 }
1835 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001836 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001837 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001838 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1839 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1840 }
1841 if (encoding != NULL) {
1842 /*
1843 * TODO: we should not have to remap to the xmlCharEncoding
1844 * predefined set, a better interface than
1845 * xmlParserInputBufferCreateFilename should allow any
1846 * encoding supported by iconv
1847 */
1848 enc = xmlParseCharEncoding((const char *) encoding);
1849 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001850 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1851 XML_XINCLUDE_UNKNOWN_ENCODING,
1852 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001853 xmlFree(encoding);
1854 xmlFree(URL);
1855 return(-1);
1856 }
1857 xmlFree(encoding);
1858 }
1859
1860 /*
1861 * Load it.
1862 */
1863 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001864 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001865 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001866 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001867 }
1868 node = xmlNewText(NULL);
1869
1870 /*
1871 * Scan all chars from the resource and add the to the node
1872 */
1873 while (xmlParserInputBufferRead(buf, 128) > 0) {
1874 int len;
1875 const xmlChar *content;
1876
1877 content = xmlBufferContent(buf->buffer);
1878 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001879 for (i = 0;i < len;) {
1880 int cur;
1881 int l;
1882
1883 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1884 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001885 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1886 XML_XINCLUDE_INVALID_CHAR,
1887 "%s contains invalid char\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001888 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001889 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001890 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001891 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001892 }
1893 xmlBufferShrink(buf->buffer, len);
1894 }
1895 xmlFreeParserInputBuffer(buf);
1896 xmlXIncludeAddTxt(ctxt, node, URL);
1897
1898loaded:
1899 /*
1900 * Add the element as the replacement copy.
1901 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001902 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001903 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001904 return(0);
1905}
1906
1907/**
1908 * xmlXIncludeLoadFallback:
1909 * @ctxt: the XInclude context
1910 * @fallback: the fallback node
1911 * @nr: the xinclude node number
1912 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001913 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001914 * in the XInclude context
1915 *
1916 * Returns 0 in case of success, -1 in case of failure
1917 */
1918static int
1919xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001920 xmlXIncludeCtxtPtr newctxt;
1921 int ret = 0;
1922
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001923 if ((fallback == NULL) || (ctxt == NULL))
1924 return(-1);
William M. Brackef245fd2004-02-06 09:33:59 +00001925 if (fallback->children != NULL) {
1926 /*
1927 * It's possible that the fallback also has 'includes'
1928 * (Bug 129969), so we re-process the fallback just in case
1929 */
1930 newctxt = xmlXIncludeNewContext(ctxt->doc);
1931 if (newctxt == NULL)
1932 return (-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00001933 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
William M. Brackef245fd2004-02-06 09:33:59 +00001934 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1935 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
William M. Brack87640d52004-04-17 14:58:15 +00001936 if (ctxt->nbErrors > 0)
William M. Brackef245fd2004-02-06 09:33:59 +00001937 ret = -1;
William M. Brack87640d52004-04-17 14:58:15 +00001938 else if (ret > 0)
1939 ret = 0; /* xmlXIncludeDoProcess can return +ve number */
William M. Brackef245fd2004-02-06 09:33:59 +00001940 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001941
Daniel Veillard03a53c32004-10-26 16:06:51 +00001942 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1943 fallback->children);
William M. Brackef245fd2004-02-06 09:33:59 +00001944 } else {
1945 ctxt->incTab[nr]->inc = NULL;
William M. Brack95af5942004-02-08 04:12:49 +00001946 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
William M. Brackef245fd2004-02-06 09:33:59 +00001947 }
William M. Brackaae10522004-01-02 14:59:41 +00001948 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001949}
1950
1951/************************************************************************
1952 * *
1953 * XInclude Processing *
1954 * *
1955 ************************************************************************/
1956
1957/**
1958 * xmlXIncludePreProcessNode:
1959 * @ctxt: an XInclude context
1960 * @node: an XInclude node
1961 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001962 * Implement the XInclude preprocessing, currently just adding the element
1963 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001964 *
1965 * Returns the result list or NULL in case of error
1966 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001967static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001968xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1969 xmlXIncludeAddNode(ctxt, node);
Daniel Veillard24505b02005-07-28 23:49:35 +00001970 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001971}
1972
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001973/**
Owen Taylor3473f882001-02-23 17:55:21 +00001974 * xmlXIncludeLoadNode:
1975 * @ctxt: an XInclude context
1976 * @nr: the node number
1977 *
1978 * Find and load the infoset replacement for the given node.
1979 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001980 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001981 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001982static int
Owen Taylor3473f882001-02-23 17:55:21 +00001983xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1984 xmlNodePtr cur;
1985 xmlChar *href;
1986 xmlChar *parse;
1987 xmlChar *base;
William M. Brackf7789b12004-06-07 08:57:27 +00001988 xmlChar *oldBase;
Owen Taylor3473f882001-02-23 17:55:21 +00001989 xmlChar *URI;
1990 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001991 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001992
1993 if (ctxt == NULL)
1994 return(-1);
1995 if ((nr < 0) || (nr >= ctxt->incNr))
1996 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001997 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001998 if (cur == NULL)
1999 return(-1);
2000
Owen Taylor3473f882001-02-23 17:55:21 +00002001 /*
2002 * read the attributes
2003 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002004 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00002005 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00002006 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
2007 if (href == NULL)
2008 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002009 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002010 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00002011 if (parse != NULL) {
2012 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
2013 xml = 1;
2014 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2015 xml = 0;
2016 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002017 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2018 XML_XINCLUDE_PARSE_VALUE,
2019 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00002020 if (href != NULL)
2021 xmlFree(href);
2022 if (parse != NULL)
2023 xmlFree(parse);
2024 return(-1);
2025 }
2026 }
2027
2028 /*
2029 * compute the URI
2030 */
2031 base = xmlNodeGetBase(ctxt->doc, cur);
2032 if (base == NULL) {
2033 URI = xmlBuildURI(href, ctxt->doc->URL);
2034 } else {
2035 URI = xmlBuildURI(href, base);
2036 }
2037 if (URI == NULL) {
2038 xmlChar *escbase;
2039 xmlChar *eschref;
2040 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002041 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00002042 */
2043 escbase = xmlURIEscape(base);
2044 eschref = xmlURIEscape(href);
2045 URI = xmlBuildURI(eschref, escbase);
2046 if (escbase != NULL)
2047 xmlFree(escbase);
2048 if (eschref != NULL)
2049 xmlFree(eschref);
2050 }
2051 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002052 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2053 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002054 if (parse != NULL)
2055 xmlFree(parse);
2056 if (href != NULL)
2057 xmlFree(href);
2058 if (base != NULL)
2059 xmlFree(base);
2060 return(-1);
2061 }
2062#ifdef DEBUG_XINCLUDE
2063 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2064 xml ? "xml": "text");
2065 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2066#endif
2067
2068 /*
William M. Brackf7789b12004-06-07 08:57:27 +00002069 * Save the base for this include (saving the current one)
Owen Taylor3473f882001-02-23 17:55:21 +00002070 */
William M. Brackf7789b12004-06-07 08:57:27 +00002071 oldBase = ctxt->base;
2072 ctxt->base = base;
2073
Owen Taylor3473f882001-02-23 17:55:21 +00002074 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002075 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00002076 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2077 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002078 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2079 }
William M. Brackf7789b12004-06-07 08:57:27 +00002080
2081 /*
2082 * Restore the original base before checking for fallback
2083 */
2084 ctxt->base = oldBase;
2085
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002086 if (ret < 0) {
2087 xmlNodePtr children;
2088
2089 /*
2090 * Time to try a fallback if availble
2091 */
2092#ifdef DEBUG_XINCLUDE
2093 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2094#endif
2095 children = cur->children;
2096 while (children != NULL) {
2097 if ((children->type == XML_ELEMENT_NODE) &&
2098 (children->ns != NULL) &&
2099 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002100 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2101 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002102 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00002103 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002104 break;
2105 }
2106 children = children->next;
2107 }
2108 }
2109 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002110 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2111 XML_XINCLUDE_NO_FALLBACK,
2112 "could not load %s, and no fallback was found\n",
2113 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002114 }
2115
2116 /*
2117 * Cleanup
2118 */
2119 if (URI != NULL)
2120 xmlFree(URI);
2121 if (parse != NULL)
2122 xmlFree(parse);
2123 if (href != NULL)
2124 xmlFree(href);
2125 if (base != NULL)
2126 xmlFree(base);
2127 return(0);
2128}
2129
2130/**
2131 * xmlXIncludeIncludeNode:
2132 * @ctxt: an XInclude context
2133 * @nr: the node number
2134 *
2135 * Inplement the infoset replacement for the given node
2136 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002137 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002138 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002139static int
Owen Taylor3473f882001-02-23 17:55:21 +00002140xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002141 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002142
2143 if (ctxt == NULL)
2144 return(-1);
2145 if ((nr < 0) || (nr >= ctxt->incNr))
2146 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002147 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00002148 if (cur == NULL)
2149 return(-1);
2150
2151 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002152 * If we stored an XPointer a late computation may be needed
2153 */
2154 if ((ctxt->incTab[nr]->inc == NULL) &&
2155 (ctxt->incTab[nr]->xptr != NULL)) {
2156 ctxt->incTab[nr]->inc =
2157 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
2158 ctxt->incTab[nr]->xptr);
2159 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2160 ctxt->incTab[nr]->xptr = NULL;
2161 }
2162 list = ctxt->incTab[nr]->inc;
2163 ctxt->incTab[nr]->inc = NULL;
2164
2165 /*
2166 * Check against the risk of generating a multi-rooted document
2167 */
2168 if ((cur->parent != NULL) &&
2169 (cur->parent->type != XML_ELEMENT_NODE)) {
2170 int nb_elem = 0;
2171
2172 tmp = list;
2173 while (tmp != NULL) {
2174 if (tmp->type == XML_ELEMENT_NODE)
2175 nb_elem++;
2176 tmp = tmp->next;
2177 }
2178 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002179 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2180 XML_XINCLUDE_MULTIPLE_ROOT,
2181 "XInclude error: would result in multiple root nodes\n",
2182 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002183 return(-1);
2184 }
2185 }
2186
Daniel Veillardc14c3892004-08-16 12:34:50 +00002187 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2188 /*
2189 * Add the list of nodes
2190 */
2191 while (list != NULL) {
2192 end = list;
2193 list = list->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002194
Daniel Veillardc14c3892004-08-16 12:34:50 +00002195 xmlAddPrevSibling(cur, end);
2196 }
2197 xmlUnlinkNode(cur);
2198 xmlFreeNode(cur);
2199 } else {
2200 /*
2201 * Change the current node as an XInclude start one, and add an
2202 * XInclude end one
2203 */
2204 cur->type = XML_XINCLUDE_START;
Daniel Veillard03a53c32004-10-26 16:06:51 +00002205 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
Daniel Veillardc14c3892004-08-16 12:34:50 +00002206 if (end == NULL) {
2207 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2208 XML_XINCLUDE_BUILD_FAILED,
2209 "failed to build node\n", NULL);
2210 return(-1);
2211 }
2212 end->type = XML_XINCLUDE_END;
2213 xmlAddNextSibling(cur, end);
Owen Taylor3473f882001-02-23 17:55:21 +00002214
Daniel Veillardc14c3892004-08-16 12:34:50 +00002215 /*
2216 * Add the list of nodes
2217 */
2218 while (list != NULL) {
2219 cur = list;
2220 list = list->next;
2221
2222 xmlAddPrevSibling(end, cur);
2223 }
Owen Taylor3473f882001-02-23 17:55:21 +00002224 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002225
2226
Owen Taylor3473f882001-02-23 17:55:21 +00002227 return(0);
2228}
2229
2230/**
2231 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002232 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002233 * @node: an XInclude node
2234 *
2235 * test if the node is an XInclude node
2236 *
2237 * Returns 1 true, 0 otherwise
2238 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002239static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002240xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002241 if (node == NULL)
2242 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002243 if (node->type != XML_ELEMENT_NODE)
2244 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002245 if (node->ns == NULL)
2246 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002247 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2248 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2249 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2250 if (ctxt->legacy == 0) {
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002251#if 0 /* wait for the XML Core Working Group to get something stable ! */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002252 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2253 "Deprecated XInclude namespace found, use %s",
2254 XINCLUDE_NS);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002255#endif
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002256 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002257 }
2258 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002259 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2260 xmlNodePtr child = node->children;
2261 int nb_fallback = 0;
2262
2263 while (child != NULL) {
2264 if ((child->type == XML_ELEMENT_NODE) &&
2265 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002266 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2267 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002268 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002269 xmlXIncludeErr(ctxt, node,
2270 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2271 "%s has an 'include' child\n",
2272 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002273 return(0);
2274 }
2275 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2276 nb_fallback++;
2277 }
2278 }
2279 child = child->next;
2280 }
2281 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002282 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2283 "%s has multiple fallback children\n",
2284 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002285 return(0);
2286 }
2287 return(1);
2288 }
2289 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2290 if ((node->parent == NULL) ||
2291 (node->parent->type != XML_ELEMENT_NODE) ||
2292 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002293 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2294 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002295 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002296 xmlXIncludeErr(ctxt, node,
2297 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2298 "%s is not the child of an 'include'\n",
2299 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002300 }
2301 }
2302 }
Owen Taylor3473f882001-02-23 17:55:21 +00002303 return(0);
2304}
2305
2306/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002307 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002308 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002309 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002310 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002311 *
2312 * Implement the XInclude substitution on the XML document @doc
2313 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002314 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002315 * or the number of substitutions done.
2316 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002317static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002318xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002319 xmlNodePtr cur;
2320 int ret = 0;
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002321 int i, start;
Owen Taylor3473f882001-02-23 17:55:21 +00002322
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002323 if ((doc == NULL) || (tree == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002324 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002325 if (ctxt == NULL)
2326 return(-1);
2327
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002328 if (doc->URL != NULL) {
2329 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2330 if (ret < 0)
2331 return(-1);
2332 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00002333 start = ctxt->incNr;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002334
Owen Taylor3473f882001-02-23 17:55:21 +00002335 /*
2336 * First phase: lookup the elements in the document
2337 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002338 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002339 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002340 xmlXIncludePreProcessNode(ctxt, cur);
William M. Brack7b0e2762004-05-12 09:33:23 +00002341 while ((cur != NULL) && (cur != tree->parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002342 /* TODO: need to work on entities -> stack */
2343 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002344 (cur->children->type != XML_ENTITY_DECL) &&
2345 (cur->children->type != XML_XINCLUDE_START) &&
2346 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002347 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002348 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002349 xmlXIncludePreProcessNode(ctxt, cur);
2350 } else if (cur->next != NULL) {
2351 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002352 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002353 xmlXIncludePreProcessNode(ctxt, cur);
2354 } else {
William M. Brack5d8d10b2004-04-16 08:11:26 +00002355 if (cur == tree)
2356 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002357 do {
2358 cur = cur->parent;
William M. Brack7b0e2762004-05-12 09:33:23 +00002359 if ((cur == NULL) || (cur == tree->parent))
2360 break; /* do */
Owen Taylor3473f882001-02-23 17:55:21 +00002361 if (cur->next != NULL) {
2362 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002363 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002364 xmlXIncludePreProcessNode(ctxt, cur);
2365 break; /* do */
2366 }
2367 } while (cur != NULL);
2368 }
2369 }
2370
2371 /*
2372 * Second Phase : collect the infosets fragments
2373 */
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002374 for (i = start;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002375 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002376 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002377 }
2378
2379 /*
2380 * Third phase: extend the original document infoset.
William M. Brack6b1a28d2004-02-06 11:24:44 +00002381 *
2382 * Originally we bypassed the inclusion if there were any errors
2383 * encountered on any of the XIncludes. A bug was raised (bug
2384 * 132588) requesting that we output the XIncludes without error,
2385 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2386 * give some other problems in the future, but for now it seems to
2387 * work ok.
2388 *
Owen Taylor3473f882001-02-23 17:55:21 +00002389 */
William M. Brack6b1a28d2004-02-06 11:24:44 +00002390 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
William M. Brack95af5942004-02-08 04:12:49 +00002391 if ((ctxt->incTab[i]->inc != NULL) ||
2392 (ctxt->incTab[i]->xptr != NULL) ||
2393 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002394 xmlXIncludeIncludeNode(ctxt, i);
Owen Taylor3473f882001-02-23 17:55:21 +00002395 }
2396
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002397 if (doc->URL != NULL)
2398 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002399 return(ret);
2400}
2401
2402/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002403 * xmlXIncludeSetFlags:
2404 * @ctxt: an XInclude processing context
2405 * @flags: a set of xmlParserOption used for parsing XML includes
2406 *
2407 * Set the flags used for further processing of XML resources.
2408 *
2409 * Returns 0 in case of success and -1 in case of error.
2410 */
2411int
2412xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2413 if (ctxt == NULL)
2414 return(-1);
2415 ctxt->parseFlags = flags;
2416 return(0);
2417}
2418
2419/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002420 * xmlXIncludeProcessFlagsData:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002421 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002422 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillard681e9042006-09-29 09:16:00 +00002423 * @data: application data that will be passed to the parser context
2424 * in the _private field of the parser context(s)
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002425 *
2426 * Implement the XInclude substitution on the XML document @doc
2427 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002428 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002429 * or the number of substitutions done.
2430 */
2431int
Daniel Veillard681e9042006-09-29 09:16:00 +00002432xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002433 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002434 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002435 int ret = 0;
2436
2437 if (doc == NULL)
2438 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002439 tree = xmlDocGetRootElement(doc);
2440 if (tree == NULL)
2441 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002442 ctxt = xmlXIncludeNewContext(doc);
2443 if (ctxt == NULL)
2444 return(-1);
Daniel Veillard681e9042006-09-29 09:16:00 +00002445 ctxt->_private = data;
Daniel Veillardce244ad2004-11-05 10:03:46 +00002446 ctxt->base = xmlStrdup((xmlChar *)doc->URL);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002447 xmlXIncludeSetFlags(ctxt, flags);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002448 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2449 if ((ret >= 0) && (ctxt->nbErrors > 0))
2450 ret = -1;
2451
2452 xmlXIncludeFreeContext(ctxt);
2453 return(ret);
2454}
2455
2456/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002457 * xmlXIncludeProcessFlags:
2458 * @doc: an XML document
2459 * @flags: a set of xmlParserOption used for parsing XML includes
2460 *
2461 * Implement the XInclude substitution on the XML document @doc
2462 *
2463 * Returns 0 if no substitution were done, -1 if some processing failed
2464 * or the number of substitutions done.
2465 */
2466int
2467xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2468 return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2469}
2470
2471/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002472 * xmlXIncludeProcess:
2473 * @doc: an XML document
2474 *
2475 * Implement the XInclude substitution on the XML document @doc
2476 *
2477 * Returns 0 if no substitution were done, -1 if some processing failed
2478 * or the number of substitutions done.
2479 */
2480int
2481xmlXIncludeProcess(xmlDocPtr doc) {
2482 return(xmlXIncludeProcessFlags(doc, 0));
2483}
2484
2485/**
2486 * xmlXIncludeProcessTreeFlags:
2487 * @tree: a node in an XML document
2488 * @flags: a set of xmlParserOption used for parsing XML includes
2489 *
2490 * Implement the XInclude substitution for the given subtree
2491 *
2492 * Returns 0 if no substitution were done, -1 if some processing failed
2493 * or the number of substitutions done.
2494 */
2495int
2496xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2497 xmlXIncludeCtxtPtr ctxt;
2498 int ret = 0;
2499
2500 if ((tree == NULL) || (tree->doc == NULL))
2501 return(-1);
2502 ctxt = xmlXIncludeNewContext(tree->doc);
2503 if (ctxt == NULL)
2504 return(-1);
William M. Brackf7789b12004-06-07 08:57:27 +00002505 ctxt->base = xmlNodeGetBase(tree->doc, tree);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002506 xmlXIncludeSetFlags(ctxt, flags);
2507 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2508 if ((ret >= 0) && (ctxt->nbErrors > 0))
2509 ret = -1;
2510
2511 xmlXIncludeFreeContext(ctxt);
2512 return(ret);
2513}
2514
2515/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002516 * xmlXIncludeProcessTree:
2517 * @tree: a node in an XML document
2518 *
2519 * Implement the XInclude substitution for the given subtree
2520 *
2521 * Returns 0 if no substitution were done, -1 if some processing failed
2522 * or the number of substitutions done.
2523 */
2524int
2525xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002526 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002527}
2528
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002529/**
2530 * xmlXIncludeProcessNode:
2531 * @ctxt: an existing XInclude context
2532 * @node: a node in an XML document
2533 *
2534 * Implement the XInclude substitution for the given subtree reusing
2535 * the informations and data coming from the given context.
2536 *
2537 * Returns 0 if no substitution were done, -1 if some processing failed
2538 * or the number of substitutions done.
2539 */
2540int
2541xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2542 int ret = 0;
2543
2544 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2545 return(-1);
2546 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2547 if ((ret >= 0) && (ctxt->nbErrors > 0))
2548 ret = -1;
2549 return(ret);
2550}
2551
Owen Taylor3473f882001-02-23 17:55:21 +00002552#else /* !LIBXML_XINCLUDE_ENABLED */
2553#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002554#define bottom_xinclude
2555#include "elfgcchack.h"