blob: 34a5489a46ae53ff8f6c8717bdfa4bbb3a565703 [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 /*
Daniel Veillarda6585822006-12-04 09:21:28 +0000676 * Copy the private user data
677 */
678 newctxt->_private = ctxt->_private;
679 /*
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000680 * Copy the existing document set
681 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000682 newctxt->incMax = ctxt->incMax;
683 newctxt->incNr = ctxt->incNr;
684 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
685 sizeof(newctxt->incTab[0]));
686 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000687 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000688 xmlFree(newctxt);
689 return;
690 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000691 /*
692 * copy the urlTab
693 */
694 newctxt->urlMax = ctxt->urlMax;
695 newctxt->urlNr = ctxt->urlNr;
696 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000697
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000698 /*
William M. Brackf7789b12004-06-07 08:57:27 +0000699 * Inherit the existing base
700 */
Daniel Veillardce244ad2004-11-05 10:03:46 +0000701 newctxt->base = xmlStrdup(ctxt->base);
William M. Brackf7789b12004-06-07 08:57:27 +0000702
703 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000704 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000705 */
706 newctxt->incBase = ctxt->incNr;
707 for (i = 0;i < ctxt->incNr;i++) {
708 newctxt->incTab[i] = ctxt->incTab[i];
709 newctxt->incTab[i]->count++; /* prevent the recursion from
710 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000711 }
William M. Bracka11e4832004-03-07 11:03:43 +0000712 /*
713 * The new context should also inherit the Parse Flags
714 * (bug 132597)
715 */
716 newctxt->parseFlags = ctxt->parseFlags;
Daniel Veillard8edf1c52003-07-22 20:52:14 +0000717 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000718 for (i = 0;i < ctxt->incNr;i++) {
719 newctxt->incTab[i]->count--;
720 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000721 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000722
723 /* urlTab may have been reallocated */
724 ctxt->urlTab = newctxt->urlTab;
725 ctxt->urlMax = newctxt->urlMax;
726
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000727 newctxt->urlMax = 0;
728 newctxt->urlNr = 0;
729 newctxt->urlTab = NULL;
730
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000731 xmlXIncludeFreeContext(newctxt);
732 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000733#ifdef DEBUG_XINCLUDE
734 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
735#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000736}
737
738/**
739 * xmlXIncludeAddTxt:
740 * @ctxt: the XInclude context
741 * @txt: the new text node
742 * @url: the associated URL
743 *
744 * Add a new txtument to the list
745 */
746static void
747xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000748#ifdef DEBUG_XINCLUDE
749 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
750#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000751 if (ctxt->txtMax == 0) {
752 ctxt->txtMax = 4;
753 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
754 sizeof(ctxt->txtTab[0]));
755 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000756 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000757 return;
758 }
759 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
760 sizeof(ctxt->txturlTab[0]));
761 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000762 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000763 return;
764 }
765 }
766 if (ctxt->txtNr >= ctxt->txtMax) {
767 ctxt->txtMax *= 2;
768 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
769 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
770 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000771 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000772 return;
773 }
774 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000775 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000776 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000777 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000778 return;
779 }
780 }
781 ctxt->txtTab[ctxt->txtNr] = txt;
782 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
783 ctxt->txtNr++;
784}
785
Owen Taylor3473f882001-02-23 17:55:21 +0000786/************************************************************************
787 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000788 * Node copy with specific semantic *
789 * *
790 ************************************************************************/
791
792/**
793 * xmlXIncludeCopyNode:
794 * @ctxt: the XInclude context
795 * @target: the document target
796 * @source: the document source
797 * @elem: the element
798 *
799 * Make a copy of the node while preserving the XInclude semantic
800 * of the Infoset copy
801 */
802static xmlNodePtr
803xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
804 xmlDocPtr source, xmlNodePtr elem) {
805 xmlNodePtr result = NULL;
806
807 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
808 (elem == NULL))
809 return(NULL);
810 if (elem->type == XML_DTD_NODE)
811 return(NULL);
812 result = xmlDocCopyNode(elem, target, 1);
813 return(result);
814}
815
816/**
817 * xmlXIncludeCopyNodeList:
818 * @ctxt: the XInclude context
819 * @target: the document target
820 * @source: the document source
821 * @elem: the element list
822 *
823 * Make a copy of the node list while preserving the XInclude semantic
824 * of the Infoset copy
825 */
826static xmlNodePtr
827xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
828 xmlDocPtr source, xmlNodePtr elem) {
829 xmlNodePtr cur, res, result = NULL, last = NULL;
830
831 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
832 (elem == NULL))
833 return(NULL);
834 cur = elem;
835 while (cur != NULL) {
836 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
837 if (res != NULL) {
838 if (result == NULL) {
839 result = last = res;
840 } else {
841 last->next = res;
842 res->prev = last;
843 last = res;
844 }
845 }
846 cur = cur->next;
847 }
848 return(result);
849}
850
851/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000852 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000853 * @cur: the node
854 * @no: the child number
855 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000856 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000857 */
858static xmlNodePtr
859xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
860 int i;
861 if (cur == NULL)
862 return(cur);
863 cur = cur->children;
864 for (i = 0;i <= no;cur = cur->next) {
865 if (cur == NULL)
866 return(cur);
867 if ((cur->type == XML_ELEMENT_NODE) ||
868 (cur->type == XML_DOCUMENT_NODE) ||
869 (cur->type == XML_HTML_DOCUMENT_NODE)) {
870 i++;
871 if (i == no)
872 break;
873 }
874 }
875 return(cur);
876}
877
William M. Brackf7eb7942003-12-31 07:59:17 +0000878xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000879/**
880 * xmlXIncludeCopyRange:
881 * @ctxt: the XInclude context
882 * @target: the document target
883 * @source: the document source
884 * @obj: the XPointer result from the evaluation.
885 *
886 * Build a node list tree copy of the XPointer result.
887 *
888 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000889 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000890 */
891static xmlNodePtr
892xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
893 xmlDocPtr source, xmlXPathObjectPtr range) {
894 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000895 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
896 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000897 /* pointers to traversal nodes */
898 xmlNodePtr start, cur, end;
899 int index1, index2;
William M. Brack6bdacd72004-02-07 08:53:23 +0000900 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000901
902 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
903 (range == NULL))
904 return(NULL);
905 if (range->type != XPATH_RANGE)
906 return(NULL);
907 start = (xmlNodePtr) range->user;
908
909 if (start == NULL)
910 return(NULL);
911 end = range->user2;
912 if (end == NULL)
913 return(xmlDocCopyNode(start, target, 1));
914
915 cur = start;
916 index1 = range->index;
917 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000918 /*
919 * level is depth of the current node under consideration
920 * list is the pointer to the root of the output tree
921 * listParent is a pointer to the parent of output tree (within
922 the included file) in case we need to add another level
923 * last is a pointer to the last node added to the output tree
924 * lastLevel is the depth of last (relative to the root)
925 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000926 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000927 /*
928 * Check if our output tree needs a parent
929 */
930 if (level < 0) {
931 while (level < 0) {
William M. Brack57e9e912004-03-09 16:19:02 +0000932 /* copy must include namespaces and properties */
933 tmp2 = xmlDocCopyNode(listParent, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000934 xmlAddChild(tmp2, list);
935 list = tmp2;
936 listParent = listParent->parent;
937 level++;
938 }
939 last = list;
940 lastLevel = 0;
941 }
942 /*
943 * Check whether we need to change our insertion point
944 */
945 while (level < lastLevel) {
946 last = last->parent;
947 lastLevel --;
948 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000949 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000950 if (cur->type == XML_TEXT_NODE) {
951 const xmlChar *content = cur->content;
952 int len;
953
954 if (content == NULL) {
955 tmp = xmlNewTextLen(NULL, 0);
956 } else {
957 len = index2;
958 if ((cur == start) && (index1 > 1)) {
959 content += (index1 - 1);
960 len -= (index1 - 1);
961 index1 = 0;
962 } else {
963 len = index2;
964 }
965 tmp = xmlNewTextLen(content, len);
966 }
967 /* single sub text node selection */
968 if (list == NULL)
969 return(tmp);
970 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +0000971 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000972 xmlAddNextSibling(last, tmp);
973 else
William M. Brackf7eb7942003-12-31 07:59:17 +0000974 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000975 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +0000976 } else { /* ending node not a text node */
William M. Brack6bdacd72004-02-07 08:53:23 +0000977 endLevel = level; /* remember the level of the end node */
978 endFlag = 1;
William M. Brack57e9e912004-03-09 16:19:02 +0000979 /* last node - need to take care of properties + namespaces */
980 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000981 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000982 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000983 listParent = cur->parent;
984 } else {
985 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000986 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +0000987 else {
988 xmlAddChild(last, tmp);
989 lastLevel = level;
990 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000991 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000992 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000993
994 if (index2 > 1) {
995 end = xmlXIncludeGetNthChild(cur, index2 - 1);
996 index2 = 0;
997 }
998 if ((cur == start) && (index1 > 1)) {
999 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1000 index1 = 0;
William M. Brack6bdacd72004-02-07 08:53:23 +00001001 } else {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001002 cur = cur->children;
1003 }
William M. Brack6bdacd72004-02-07 08:53:23 +00001004 level++; /* increment level to show change */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001005 /*
1006 * Now gather the remaining nodes from cur to end
1007 */
William M. Brack6bdacd72004-02-07 08:53:23 +00001008 continue; /* while */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001009 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001010 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001011 if ((cur->type == XML_TEXT_NODE) ||
1012 (cur->type == XML_CDATA_SECTION_NODE)) {
1013 const xmlChar *content = cur->content;
1014
1015 if (content == NULL) {
1016 tmp = xmlNewTextLen(NULL, 0);
1017 } else {
1018 if (index1 > 1) {
1019 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +00001020 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001021 }
1022 tmp = xmlNewText(content);
1023 }
1024 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001025 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001026 } else { /* Not text node */
William M. Brack57e9e912004-03-09 16:19:02 +00001027 /*
1028 * start of the range - need to take care of
1029 * properties and namespaces
1030 */
1031 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +00001032 list = last = tmp;
1033 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001034 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001035 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001036 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001037 index1 = 0;
1038 /*
1039 * Now gather the remaining nodes from cur to end
1040 */
1041 continue; /* while */
1042 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001043 }
1044 } else {
1045 tmp = NULL;
1046 switch (cur->type) {
1047 case XML_DTD_NODE:
1048 case XML_ELEMENT_DECL:
1049 case XML_ATTRIBUTE_DECL:
1050 case XML_ENTITY_NODE:
1051 /* Do not copy DTD informations */
1052 break;
1053 case XML_ENTITY_DECL:
1054 /* handle crossing entities -> stack needed */
1055 break;
1056 case XML_XINCLUDE_START:
1057 case XML_XINCLUDE_END:
1058 /* don't consider it part of the tree content */
1059 break;
1060 case XML_ATTRIBUTE_NODE:
1061 /* Humm, should not happen ! */
1062 break;
1063 default:
William M. Brack57e9e912004-03-09 16:19:02 +00001064 /*
1065 * Middle of the range - need to take care of
1066 * properties and namespaces
1067 */
1068 tmp = xmlDocCopyNode(cur, target, 2);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001069 break;
1070 }
1071 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001072 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001073 xmlAddNextSibling(last, tmp);
1074 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001075 xmlAddChild(last, tmp);
1076 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001077 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001078 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001079 }
1080 }
1081 /*
1082 * Skip to next node in document order
1083 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001084 cur = xmlXPtrAdvanceNode(cur, &level);
William M. Brack6bdacd72004-02-07 08:53:23 +00001085 if (endFlag && (level >= endLevel))
1086 break;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001087 }
1088 return(list);
1089}
1090
1091/**
1092 * xmlXIncludeBuildNodeList:
1093 * @ctxt: the XInclude context
1094 * @target: the document target
1095 * @source: the document source
1096 * @obj: the XPointer result from the evaluation.
1097 *
1098 * Build a node list tree copy of the XPointer result.
1099 * This will drop Attributes and Namespace declarations.
1100 *
1101 * Returns an xmlNodePtr list or NULL.
1102 * the caller has to free the node tree.
1103 */
1104static xmlNodePtr
1105xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1106 xmlDocPtr source, xmlXPathObjectPtr obj) {
1107 xmlNodePtr list = NULL, last = NULL;
1108 int i;
1109
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001110 if (source == NULL)
1111 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001112 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1113 (obj == NULL))
1114 return(NULL);
1115 switch (obj->type) {
1116 case XPATH_NODESET: {
1117 xmlNodeSetPtr set = obj->nodesetval;
1118 if (set == NULL)
1119 return(NULL);
1120 for (i = 0;i < set->nodeNr;i++) {
1121 if (set->nodeTab[i] == NULL)
1122 continue;
1123 switch (set->nodeTab[i]->type) {
1124 case XML_TEXT_NODE:
1125 case XML_CDATA_SECTION_NODE:
1126 case XML_ELEMENT_NODE:
1127 case XML_ENTITY_REF_NODE:
1128 case XML_ENTITY_NODE:
1129 case XML_PI_NODE:
1130 case XML_COMMENT_NODE:
1131 case XML_DOCUMENT_NODE:
1132 case XML_HTML_DOCUMENT_NODE:
1133#ifdef LIBXML_DOCB_ENABLED
1134 case XML_DOCB_DOCUMENT_NODE:
1135#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001136 case XML_XINCLUDE_END:
1137 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001138 case XML_XINCLUDE_START: {
1139 xmlNodePtr tmp, cur = set->nodeTab[i];
1140
1141 cur = cur->next;
1142 while (cur != NULL) {
1143 switch(cur->type) {
1144 case XML_TEXT_NODE:
1145 case XML_CDATA_SECTION_NODE:
1146 case XML_ELEMENT_NODE:
1147 case XML_ENTITY_REF_NODE:
1148 case XML_ENTITY_NODE:
1149 case XML_PI_NODE:
1150 case XML_COMMENT_NODE:
1151 tmp = xmlXIncludeCopyNode(ctxt, target,
1152 source, cur);
1153 if (last == NULL) {
1154 list = last = tmp;
1155 } else {
1156 xmlAddNextSibling(last, tmp);
1157 last = tmp;
1158 }
1159 cur = cur->next;
1160 continue;
1161 default:
1162 break;
1163 }
1164 break;
1165 }
1166 continue;
1167 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001168 case XML_ATTRIBUTE_NODE:
1169 case XML_NAMESPACE_DECL:
1170 case XML_DOCUMENT_TYPE_NODE:
1171 case XML_DOCUMENT_FRAG_NODE:
1172 case XML_NOTATION_NODE:
1173 case XML_DTD_NODE:
1174 case XML_ELEMENT_DECL:
1175 case XML_ATTRIBUTE_DECL:
1176 case XML_ENTITY_DECL:
1177 continue; /* for */
1178 }
1179 if (last == NULL)
1180 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1181 set->nodeTab[i]);
1182 else {
1183 xmlAddNextSibling(last,
1184 xmlXIncludeCopyNode(ctxt, target, source,
1185 set->nodeTab[i]));
1186 if (last->next != NULL)
1187 last = last->next;
1188 }
1189 }
1190 break;
1191 }
1192 case XPATH_LOCATIONSET: {
1193 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1194 if (set == NULL)
1195 return(NULL);
1196 for (i = 0;i < set->locNr;i++) {
1197 if (last == NULL)
1198 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1199 set->locTab[i]);
1200 else
1201 xmlAddNextSibling(last,
1202 xmlXIncludeCopyXPointer(ctxt, target, source,
1203 set->locTab[i]));
1204 if (last != NULL) {
1205 while (last->next != NULL)
1206 last = last->next;
1207 }
1208 }
1209 break;
1210 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001211#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001212 case XPATH_RANGE:
1213 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001214#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001215 case XPATH_POINT:
1216 /* points are ignored in XInclude */
1217 break;
1218 default:
1219 break;
1220 }
1221 return(list);
1222}
1223/************************************************************************
1224 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001225 * XInclude I/O handling *
1226 * *
1227 ************************************************************************/
1228
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001229typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1230typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1231struct _xmlXIncludeMergeData {
1232 xmlDocPtr doc;
1233 xmlXIncludeCtxtPtr ctxt;
1234};
1235
Owen Taylor3473f882001-02-23 17:55:21 +00001236/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001237 * xmlXIncludeMergeOneEntity:
1238 * @ent: the entity
1239 * @doc: the including doc
1240 * @nr: the entity name
1241 *
1242 * Inplements the merge of one entity
1243 */
1244static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001245xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001246 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001247 xmlEntityPtr ret, prev;
1248 xmlDocPtr doc;
1249 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001250
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001251 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001252 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001253 ctxt = data->ctxt;
1254 doc = data->doc;
1255 if ((ctxt == NULL) || (doc == NULL))
1256 return;
1257 switch (ent->etype) {
1258 case XML_INTERNAL_PARAMETER_ENTITY:
1259 case XML_EXTERNAL_PARAMETER_ENTITY:
1260 case XML_INTERNAL_PREDEFINED_ENTITY:
1261 return;
1262 case XML_INTERNAL_GENERAL_ENTITY:
1263 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1264 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1265 break;
1266 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001267 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1268 ent->SystemID, ent->content);
1269 if (ret != NULL) {
1270 if (ent->URI != NULL)
1271 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001272 } else {
1273 prev = xmlGetDocEntity(doc, ent->name);
1274 if (prev != NULL) {
1275 if (ent->etype != prev->etype)
1276 goto error;
1277
1278 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1279 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1280 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001281 } else if ((ent->ExternalID != NULL) &&
1282 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001283 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1284 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001285 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1286 if (!xmlStrEqual(ent->content, prev->content))
1287 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001288 } else {
1289 goto error;
1290 }
1291
1292 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001293 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001294 return;
1295error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001296 switch (ent->etype) {
1297 case XML_INTERNAL_PARAMETER_ENTITY:
1298 case XML_EXTERNAL_PARAMETER_ENTITY:
1299 case XML_INTERNAL_PREDEFINED_ENTITY:
1300 case XML_INTERNAL_GENERAL_ENTITY:
1301 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1302 return;
1303 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1304 break;
1305 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001306 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1307 "mismatch in redefinition of entity %s\n",
1308 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001309}
1310
1311/**
1312 * xmlXIncludeMergeEntities:
1313 * @ctxt: an XInclude context
1314 * @doc: the including doc
1315 * @from: the included doc
1316 *
1317 * Inplements the entity merge
1318 *
1319 * Returns 0 if merge succeeded, -1 if some processing failed
1320 */
1321static int
1322xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1323 xmlDocPtr from) {
1324 xmlNodePtr cur;
1325 xmlDtdPtr target, source;
1326
1327 if (ctxt == NULL)
1328 return(-1);
1329
1330 if ((from == NULL) || (from->intSubset == NULL))
1331 return(0);
1332
1333 target = doc->intSubset;
1334 if (target == NULL) {
1335 cur = xmlDocGetRootElement(doc);
1336 if (cur == NULL)
1337 return(-1);
1338 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1339 if (target == NULL)
1340 return(-1);
1341 }
1342
1343 source = from->intSubset;
1344 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001345 xmlXIncludeMergeData data;
1346
1347 data.ctxt = ctxt;
1348 data.doc = doc;
1349
Daniel Veillard4287c572003-02-04 22:48:53 +00001350 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001351 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001352 }
1353 source = from->extSubset;
1354 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001355 xmlXIncludeMergeData data;
1356
1357 data.ctxt = ctxt;
1358 data.doc = doc;
1359
Daniel Veillard4287c572003-02-04 22:48:53 +00001360 /*
1361 * don't duplicate existing stuff when external subsets are the same
1362 */
1363 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1364 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1365 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001366 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001367 }
1368 }
1369 return(0);
1370}
1371
1372/**
Owen Taylor3473f882001-02-23 17:55:21 +00001373 * xmlXIncludeLoadDoc:
1374 * @ctxt: the XInclude context
1375 * @url: the associated URL
1376 * @nr: the xinclude node number
1377 *
1378 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001379 *
1380 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001381 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001382static int
Owen Taylor3473f882001-02-23 17:55:21 +00001383xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1384 xmlDocPtr doc;
1385 xmlURIPtr uri;
1386 xmlChar *URL;
1387 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001388 int i = 0;
William M. Brack4d59e222004-03-08 14:42:31 +00001389#ifdef LIBXML_XPTR_ENABLED
1390 int saveFlags;
1391#endif
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001392
1393#ifdef DEBUG_XINCLUDE
1394 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1395#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001396 /*
1397 * Check the URL and remove any fragment identifier
1398 */
1399 uri = xmlParseURI((const char *)url);
1400 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001401 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1402 XML_XINCLUDE_HREF_URI,
1403 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001404 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001405 }
1406 if (uri->fragment != NULL) {
1407 fragment = (xmlChar *) uri->fragment;
1408 uri->fragment = NULL;
1409 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001410 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1411 (ctxt->incTab[nr]->fragment != NULL)) {
1412 if (fragment != NULL) xmlFree(fragment);
1413 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1414 }
Owen Taylor3473f882001-02-23 17:55:21 +00001415 URL = xmlSaveUri(uri);
1416 xmlFreeURI(uri);
1417 if (URL == NULL) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00001418 if (ctxt->incTab != NULL)
1419 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1420 XML_XINCLUDE_HREF_URI,
1421 "invalid value URI %s\n", url);
1422 else
1423 xmlXIncludeErr(ctxt, NULL,
1424 XML_XINCLUDE_HREF_URI,
1425 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001426 if (fragment != NULL)
1427 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001428 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001429 }
1430
1431 /*
1432 * Handling of references to the local document are done
1433 * directly through ctxt->doc.
1434 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001435 if ((URL[0] == 0) || (URL[0] == '#') ||
1436 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001437 doc = NULL;
1438 goto loaded;
1439 }
1440
1441 /*
1442 * Prevent reloading twice the document.
1443 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001444 for (i = 0; i < ctxt->incNr; i++) {
1445 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1446 (ctxt->incTab[i]->doc != NULL)) {
1447 doc = ctxt->incTab[i]->doc;
1448#ifdef DEBUG_XINCLUDE
1449 printf("Already loaded %s\n", URL);
1450#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001451 goto loaded;
1452 }
1453 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001454
Owen Taylor3473f882001-02-23 17:55:21 +00001455 /*
1456 * Load it.
1457 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001458#ifdef DEBUG_XINCLUDE
1459 printf("loading %s\n", URL);
1460#endif
William M. Brack4d59e222004-03-08 14:42:31 +00001461#ifdef LIBXML_XPTR_ENABLED
1462 /*
1463 * If this is an XPointer evaluation, we want to assure that
1464 * all entities have been resolved prior to processing the
1465 * referenced document
1466 */
1467 saveFlags = ctxt->parseFlags;
1468 if (fragment != NULL) { /* if this is an XPointer eval */
1469 ctxt->parseFlags |= XML_PARSE_NOENT;
1470 }
1471#endif
1472
Daniel Veillard98485322003-08-14 15:44:40 +00001473 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
William M. Brack4d59e222004-03-08 14:42:31 +00001474#ifdef LIBXML_XPTR_ENABLED
1475 ctxt->parseFlags = saveFlags;
1476#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001477 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001478 xmlFree(URL);
1479 if (fragment != NULL)
1480 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001481 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001482 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001483 ctxt->incTab[nr]->doc = doc;
William M. Brackb85c9202004-07-26 00:20:13 +00001484 /*
1485 * It's possible that the requested URL has been mapped to a
1486 * completely different location (e.g. through a catalog entry).
1487 * To check for this, we compare the URL with that of the doc
1488 * and change it if they disagree (bug 146988).
1489 */
1490 if (!xmlStrEqual(URL, doc->URL)) {
1491 xmlFree(URL);
1492 URL = xmlStrdup(doc->URL);
1493 }
Daniel Veillard98485322003-08-14 15:44:40 +00001494 for (i = nr + 1; i < ctxt->incNr; i++) {
1495 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1496 ctxt->incTab[nr]->count++;
1497#ifdef DEBUG_XINCLUDE
1498 printf("Increasing %s count since reused\n", URL);
1499#endif
1500 break;
1501 }
1502 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001503
1504 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001505 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001506 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001507 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001508
1509 /*
1510 * We don't need the DTD anymore, free up space
1511 if (doc->intSubset != NULL) {
1512 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1513 xmlFreeNode((xmlNodePtr) doc->intSubset);
1514 doc->intSubset = NULL;
1515 }
1516 if (doc->extSubset != NULL) {
1517 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1518 xmlFreeNode((xmlNodePtr) doc->extSubset);
1519 doc->extSubset = NULL;
1520 }
1521 */
1522 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001523
1524loaded:
1525 if (fragment == NULL) {
1526 /*
1527 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001528 */
1529 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001530 {
1531 /* Hopefully a DTD declaration won't be copied from
1532 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001533 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001534 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001535 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001536 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001537 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001538 }
1539#ifdef LIBXML_XPTR_ENABLED
1540 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001541 /*
1542 * Computes the XPointer expression and make a copy used
1543 * as the replacement copy.
1544 */
1545 xmlXPathObjectPtr xptr;
1546 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001547 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001548
1549 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001550 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1551 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001552 } else {
1553 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1554 }
1555 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001556 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1557 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001558 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001559 xmlFree(URL);
1560 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001561 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001562 }
1563 xptr = xmlXPtrEval(fragment, xptrctxt);
1564 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001565 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1566 XML_XINCLUDE_XPTR_FAILED,
1567 "XPointer evaluation failed: #%s\n",
1568 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001569 xmlXPathFreeContext(xptrctxt);
1570 xmlFree(URL);
1571 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001572 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001573 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001574 switch (xptr->type) {
1575 case XPATH_UNDEFINED:
1576 case XPATH_BOOLEAN:
1577 case XPATH_NUMBER:
1578 case XPATH_STRING:
1579 case XPATH_POINT:
1580 case XPATH_USERS:
1581 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001582 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1583 XML_XINCLUDE_XPTR_RESULT,
1584 "XPointer is not a range: #%s\n",
1585 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001586 xmlXPathFreeContext(xptrctxt);
1587 xmlFree(URL);
1588 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001589 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001590 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001591 if ((xptr->nodesetval == NULL) ||
1592 (xptr->nodesetval->nodeNr <= 0)) {
1593 xmlXPathFreeContext(xptrctxt);
1594 xmlFree(URL);
1595 xmlFree(fragment);
1596 return(-1);
1597 }
William M. Brackabf598b2004-06-08 02:01:28 +00001598
Daniel Veillard39196eb2001-06-19 18:09:42 +00001599 case XPATH_RANGE:
1600 case XPATH_LOCATIONSET:
1601 break;
1602 }
1603 set = xptr->nodesetval;
1604 if (set != NULL) {
1605 for (i = 0;i < set->nodeNr;i++) {
1606 if (set->nodeTab[i] == NULL)
1607 continue;
1608 switch (set->nodeTab[i]->type) {
1609 case XML_TEXT_NODE:
1610 case XML_CDATA_SECTION_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001611 case XML_ENTITY_REF_NODE:
1612 case XML_ENTITY_NODE:
1613 case XML_PI_NODE:
1614 case XML_COMMENT_NODE:
1615 case XML_DOCUMENT_NODE:
1616 case XML_HTML_DOCUMENT_NODE:
1617#ifdef LIBXML_DOCB_ENABLED
1618 case XML_DOCB_DOCUMENT_NODE:
1619#endif
1620 continue;
William M. Brackabf598b2004-06-08 02:01:28 +00001621 case XML_ELEMENT_NODE: {
1622 xmlChar *nodeBase;
1623 xmlNodePtr el = set->nodeTab[i];
1624
1625 nodeBase = xmlNodeGetBase(el->doc, el);
1626 if (nodeBase != NULL) {
1627 if (!xmlStrEqual(nodeBase, el->doc->URL))
1628 xmlNodeSetBase(el, nodeBase);
1629 xmlFree(nodeBase);
1630 }
1631 continue;
1632 }
1633
Daniel Veillard39196eb2001-06-19 18:09:42 +00001634 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001635 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1636 XML_XINCLUDE_XPTR_RESULT,
1637 "XPointer selects an attribute: #%s\n",
1638 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001639 set->nodeTab[i] = NULL;
1640 continue;
1641 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001642 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1643 XML_XINCLUDE_XPTR_RESULT,
1644 "XPointer selects a namespace: #%s\n",
1645 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001646 set->nodeTab[i] = NULL;
1647 continue;
1648 case XML_DOCUMENT_TYPE_NODE:
1649 case XML_DOCUMENT_FRAG_NODE:
1650 case XML_NOTATION_NODE:
1651 case XML_DTD_NODE:
1652 case XML_ELEMENT_DECL:
1653 case XML_ATTRIBUTE_DECL:
1654 case XML_ENTITY_DECL:
1655 case XML_XINCLUDE_START:
1656 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001657 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1658 XML_XINCLUDE_XPTR_RESULT,
1659 "XPointer selects unexpected nodes: #%s\n",
1660 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001661 set->nodeTab[i] = NULL;
1662 set->nodeTab[i] = NULL;
1663 continue; /* for */
1664 }
1665 }
1666 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001667 if (doc == NULL) {
1668 ctxt->incTab[nr]->xptr = xptr;
1669 ctxt->incTab[nr]->inc = NULL;
1670 } else {
1671 ctxt->incTab[nr]->inc =
1672 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1673 xmlXPathFreeObject(xptr);
1674 }
Owen Taylor3473f882001-02-23 17:55:21 +00001675 xmlXPathFreeContext(xptrctxt);
1676 xmlFree(fragment);
1677 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001678#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001679
1680 /*
1681 * Do the xml:base fixup if needed
1682 */
1683 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1684 xmlNodePtr node;
William M. Brack0b13a092005-06-01 03:37:59 +00001685 xmlChar *base;
William M. Brackabf598b2004-06-08 02:01:28 +00001686 xmlChar *curBase;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001687
William M. Brackf7789b12004-06-07 08:57:27 +00001688 /*
William M. Brack0b13a092005-06-01 03:37:59 +00001689 * The base is only adjusted if "necessary", i.e. if the xinclude node
1690 * has a base specified, or the URL is relative
William M. Brackf7789b12004-06-07 08:57:27 +00001691 */
William M. Brack0b13a092005-06-01 03:37:59 +00001692 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1693 XML_XML_NAMESPACE);
1694 if (base == NULL) {
1695 /*
1696 * No xml:base on the xinclude node, so we check whether the
1697 * URI base is different than (relative to) the context base
1698 */
1699 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1700 if (curBase == NULL) { /* Error return */
1701 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
William M. Brackf7789b12004-06-07 08:57:27 +00001702 XML_XINCLUDE_HREF_URI,
1703 "trying to build relative URI from %s\n", URL);
William M. Brack0b13a092005-06-01 03:37:59 +00001704 } else {
1705 /* If the URI doesn't contain a slash, it's not relative */
1706 if (!xmlStrchr(curBase, (xmlChar) '/'))
1707 xmlFree(curBase);
1708 else
1709 base = curBase;
William M. Brackf7789b12004-06-07 08:57:27 +00001710 }
William M. Brack0b13a092005-06-01 03:37:59 +00001711 }
1712 if (base != NULL) { /* Adjustment may be needed */
1713 node = ctxt->incTab[nr]->inc;
1714 while (node != NULL) {
1715 /* Only work on element nodes */
1716 if (node->type == XML_ELEMENT_NODE) {
1717 curBase = xmlNodeGetBase(node->doc, node);
1718 /* If no current base, set it */
1719 if (curBase == NULL) {
1720 xmlNodeSetBase(node, base);
1721 } else {
1722 /*
1723 * If the current base is the same as the
1724 * URL of the document, then reset it to be
1725 * the specified xml:base or the relative URI
1726 */
1727 if (xmlStrEqual(curBase, node->doc->URL)) {
1728 xmlNodeSetBase(node, base);
1729 } else {
1730 /*
1731 * If the element already has an xml:base
1732 * set, then relativise it if necessary
1733 */
1734 xmlChar *xmlBase;
1735 xmlBase = xmlGetNsProp(node,
1736 BAD_CAST "base",
1737 XML_XML_NAMESPACE);
1738 if (xmlBase != NULL) {
1739 xmlChar *relBase;
1740 relBase = xmlBuildURI(xmlBase, base);
1741 if (relBase == NULL) { /* error */
1742 xmlXIncludeErr(ctxt,
1743 ctxt->incTab[nr]->ref,
1744 XML_XINCLUDE_HREF_URI,
1745 "trying to rebuild base from %s\n",
1746 xmlBase);
1747 } else {
1748 xmlNodeSetBase(node, relBase);
1749 xmlFree(relBase);
1750 }
1751 xmlFree(xmlBase);
1752 }
1753 }
1754 xmlFree(curBase);
1755 }
1756 }
1757 node = node->next;
1758 }
1759 xmlFree(base);
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001760 }
1761 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001762 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1763 (ctxt->incTab[nr]->count <= 1)) {
1764#ifdef DEBUG_XINCLUDE
1765 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1766#endif
1767 xmlFreeDoc(ctxt->incTab[nr]->doc);
1768 ctxt->incTab[nr]->doc = NULL;
1769 }
Owen Taylor3473f882001-02-23 17:55:21 +00001770 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001771 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001772}
1773
1774/**
1775 * xmlXIncludeLoadTxt:
1776 * @ctxt: the XInclude context
1777 * @url: the associated URL
1778 * @nr: the xinclude node number
1779 *
1780 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001781 *
1782 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001783 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001784static int
Owen Taylor3473f882001-02-23 17:55:21 +00001785xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1786 xmlParserInputBufferPtr buf;
1787 xmlNodePtr node;
1788 xmlURIPtr uri;
1789 xmlChar *URL;
1790 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001791 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001792 xmlCharEncoding enc = (xmlCharEncoding) 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001793
Owen Taylor3473f882001-02-23 17:55:21 +00001794 /*
1795 * Check the URL and remove any fragment identifier
1796 */
1797 uri = xmlParseURI((const char *)url);
1798 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001799 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1800 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001801 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001802 }
1803 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001804 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1805 "fragment identifier forbidden for text: %s\n",
1806 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001807 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001808 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001809 }
1810 URL = xmlSaveUri(uri);
1811 xmlFreeURI(uri);
1812 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001813 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1814 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001815 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001816 }
1817
1818 /*
1819 * Handling of references to the local document are done
1820 * directly through ctxt->doc.
1821 */
1822 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001823 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1824 XML_XINCLUDE_TEXT_DOCUMENT,
1825 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001826 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001827 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001828 }
1829
1830 /*
1831 * Prevent reloading twice the document.
1832 */
1833 for (i = 0; i < ctxt->txtNr; i++) {
1834 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1835 node = xmlCopyNode(ctxt->txtTab[i], 1);
1836 goto loaded;
1837 }
1838 }
1839 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001840 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001841 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001842 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1843 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1844 }
1845 if (encoding != NULL) {
1846 /*
1847 * TODO: we should not have to remap to the xmlCharEncoding
1848 * predefined set, a better interface than
1849 * xmlParserInputBufferCreateFilename should allow any
1850 * encoding supported by iconv
1851 */
1852 enc = xmlParseCharEncoding((const char *) encoding);
1853 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001854 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1855 XML_XINCLUDE_UNKNOWN_ENCODING,
1856 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001857 xmlFree(encoding);
1858 xmlFree(URL);
1859 return(-1);
1860 }
1861 xmlFree(encoding);
1862 }
1863
1864 /*
1865 * Load it.
1866 */
1867 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001868 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001869 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001870 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001871 }
1872 node = xmlNewText(NULL);
1873
1874 /*
1875 * Scan all chars from the resource and add the to the node
1876 */
1877 while (xmlParserInputBufferRead(buf, 128) > 0) {
1878 int len;
1879 const xmlChar *content;
1880
1881 content = xmlBufferContent(buf->buffer);
1882 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001883 for (i = 0;i < len;) {
1884 int cur;
1885 int l;
1886
1887 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1888 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001889 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1890 XML_XINCLUDE_INVALID_CHAR,
1891 "%s contains invalid char\n", URL);
William M. Brack53ce98c2007-02-13 00:37:20 +00001892 xmlFreeParserInputBuffer(buf);
1893 xmlFree(URL);
1894 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001895 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001896 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001897 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001898 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001899 }
1900 xmlBufferShrink(buf->buffer, len);
1901 }
1902 xmlFreeParserInputBuffer(buf);
1903 xmlXIncludeAddTxt(ctxt, node, URL);
1904
1905loaded:
1906 /*
1907 * Add the element as the replacement copy.
1908 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001909 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001910 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001911 return(0);
1912}
1913
1914/**
1915 * xmlXIncludeLoadFallback:
1916 * @ctxt: the XInclude context
1917 * @fallback: the fallback node
1918 * @nr: the xinclude node number
1919 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001920 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001921 * in the XInclude context
1922 *
1923 * Returns 0 in case of success, -1 in case of failure
1924 */
1925static int
1926xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001927 xmlXIncludeCtxtPtr newctxt;
1928 int ret = 0;
1929
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001930 if ((fallback == NULL) || (ctxt == NULL))
1931 return(-1);
William M. Brackef245fd2004-02-06 09:33:59 +00001932 if (fallback->children != NULL) {
1933 /*
1934 * It's possible that the fallback also has 'includes'
1935 * (Bug 129969), so we re-process the fallback just in case
1936 */
1937 newctxt = xmlXIncludeNewContext(ctxt->doc);
1938 if (newctxt == NULL)
1939 return (-1);
Daniel Veillarda6585822006-12-04 09:21:28 +00001940 newctxt->_private = ctxt->_private;
Daniel Veillardce244ad2004-11-05 10:03:46 +00001941 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
William M. Brackef245fd2004-02-06 09:33:59 +00001942 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1943 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
William M. Brack87640d52004-04-17 14:58:15 +00001944 if (ctxt->nbErrors > 0)
William M. Brackef245fd2004-02-06 09:33:59 +00001945 ret = -1;
William M. Brack87640d52004-04-17 14:58:15 +00001946 else if (ret > 0)
1947 ret = 0; /* xmlXIncludeDoProcess can return +ve number */
William M. Brackef245fd2004-02-06 09:33:59 +00001948 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001949
Daniel Veillard03a53c32004-10-26 16:06:51 +00001950 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1951 fallback->children);
William M. Brackef245fd2004-02-06 09:33:59 +00001952 } else {
1953 ctxt->incTab[nr]->inc = NULL;
William M. Brack95af5942004-02-08 04:12:49 +00001954 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
William M. Brackef245fd2004-02-06 09:33:59 +00001955 }
William M. Brackaae10522004-01-02 14:59:41 +00001956 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001957}
1958
1959/************************************************************************
1960 * *
1961 * XInclude Processing *
1962 * *
1963 ************************************************************************/
1964
1965/**
1966 * xmlXIncludePreProcessNode:
1967 * @ctxt: an XInclude context
1968 * @node: an XInclude node
1969 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001970 * Implement the XInclude preprocessing, currently just adding the element
1971 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001972 *
1973 * Returns the result list or NULL in case of error
1974 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001975static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001976xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1977 xmlXIncludeAddNode(ctxt, node);
Daniel Veillard24505b02005-07-28 23:49:35 +00001978 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001979}
1980
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001981/**
Owen Taylor3473f882001-02-23 17:55:21 +00001982 * xmlXIncludeLoadNode:
1983 * @ctxt: an XInclude context
1984 * @nr: the node number
1985 *
1986 * Find and load the infoset replacement for the given node.
1987 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001988 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001989 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001990static int
Owen Taylor3473f882001-02-23 17:55:21 +00001991xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1992 xmlNodePtr cur;
1993 xmlChar *href;
1994 xmlChar *parse;
1995 xmlChar *base;
William M. Brackf7789b12004-06-07 08:57:27 +00001996 xmlChar *oldBase;
Owen Taylor3473f882001-02-23 17:55:21 +00001997 xmlChar *URI;
1998 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001999 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002000
2001 if (ctxt == NULL)
2002 return(-1);
2003 if ((nr < 0) || (nr >= ctxt->incNr))
2004 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002005 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00002006 if (cur == NULL)
2007 return(-1);
2008
Owen Taylor3473f882001-02-23 17:55:21 +00002009 /*
2010 * read the attributes
2011 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002012 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00002013 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00002014 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
2015 if (href == NULL)
2016 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002017 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002018 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00002019 if (parse != NULL) {
2020 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
2021 xml = 1;
2022 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2023 xml = 0;
2024 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002025 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2026 XML_XINCLUDE_PARSE_VALUE,
2027 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00002028 if (href != NULL)
2029 xmlFree(href);
2030 if (parse != NULL)
2031 xmlFree(parse);
2032 return(-1);
2033 }
2034 }
2035
2036 /*
2037 * compute the URI
2038 */
2039 base = xmlNodeGetBase(ctxt->doc, cur);
2040 if (base == NULL) {
2041 URI = xmlBuildURI(href, ctxt->doc->URL);
2042 } else {
2043 URI = xmlBuildURI(href, base);
2044 }
2045 if (URI == NULL) {
2046 xmlChar *escbase;
2047 xmlChar *eschref;
2048 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002049 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00002050 */
2051 escbase = xmlURIEscape(base);
2052 eschref = xmlURIEscape(href);
2053 URI = xmlBuildURI(eschref, escbase);
2054 if (escbase != NULL)
2055 xmlFree(escbase);
2056 if (eschref != NULL)
2057 xmlFree(eschref);
2058 }
2059 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002060 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2061 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002062 if (parse != NULL)
2063 xmlFree(parse);
2064 if (href != NULL)
2065 xmlFree(href);
2066 if (base != NULL)
2067 xmlFree(base);
2068 return(-1);
2069 }
2070#ifdef DEBUG_XINCLUDE
2071 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2072 xml ? "xml": "text");
2073 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2074#endif
2075
2076 /*
William M. Brackf7789b12004-06-07 08:57:27 +00002077 * Save the base for this include (saving the current one)
Owen Taylor3473f882001-02-23 17:55:21 +00002078 */
William M. Brackf7789b12004-06-07 08:57:27 +00002079 oldBase = ctxt->base;
2080 ctxt->base = base;
2081
Owen Taylor3473f882001-02-23 17:55:21 +00002082 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002083 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00002084 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2085 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002086 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2087 }
William M. Brackf7789b12004-06-07 08:57:27 +00002088
2089 /*
2090 * Restore the original base before checking for fallback
2091 */
2092 ctxt->base = oldBase;
2093
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002094 if (ret < 0) {
2095 xmlNodePtr children;
2096
2097 /*
2098 * Time to try a fallback if availble
2099 */
2100#ifdef DEBUG_XINCLUDE
2101 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2102#endif
2103 children = cur->children;
2104 while (children != NULL) {
2105 if ((children->type == XML_ELEMENT_NODE) &&
2106 (children->ns != NULL) &&
2107 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002108 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2109 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002110 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00002111 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002112 break;
2113 }
2114 children = children->next;
2115 }
2116 }
2117 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002118 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2119 XML_XINCLUDE_NO_FALLBACK,
2120 "could not load %s, and no fallback was found\n",
2121 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002122 }
2123
2124 /*
2125 * Cleanup
2126 */
2127 if (URI != NULL)
2128 xmlFree(URI);
2129 if (parse != NULL)
2130 xmlFree(parse);
2131 if (href != NULL)
2132 xmlFree(href);
2133 if (base != NULL)
2134 xmlFree(base);
2135 return(0);
2136}
2137
2138/**
2139 * xmlXIncludeIncludeNode:
2140 * @ctxt: an XInclude context
2141 * @nr: the node number
2142 *
2143 * Inplement the infoset replacement for the given node
2144 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002145 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002146 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002147static int
Owen Taylor3473f882001-02-23 17:55:21 +00002148xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002149 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002150
2151 if (ctxt == NULL)
2152 return(-1);
2153 if ((nr < 0) || (nr >= ctxt->incNr))
2154 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002155 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00002156 if (cur == NULL)
2157 return(-1);
2158
2159 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002160 * If we stored an XPointer a late computation may be needed
2161 */
2162 if ((ctxt->incTab[nr]->inc == NULL) &&
2163 (ctxt->incTab[nr]->xptr != NULL)) {
2164 ctxt->incTab[nr]->inc =
2165 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
2166 ctxt->incTab[nr]->xptr);
2167 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2168 ctxt->incTab[nr]->xptr = NULL;
2169 }
2170 list = ctxt->incTab[nr]->inc;
2171 ctxt->incTab[nr]->inc = NULL;
2172
2173 /*
2174 * Check against the risk of generating a multi-rooted document
2175 */
2176 if ((cur->parent != NULL) &&
2177 (cur->parent->type != XML_ELEMENT_NODE)) {
2178 int nb_elem = 0;
2179
2180 tmp = list;
2181 while (tmp != NULL) {
2182 if (tmp->type == XML_ELEMENT_NODE)
2183 nb_elem++;
2184 tmp = tmp->next;
2185 }
2186 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002187 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2188 XML_XINCLUDE_MULTIPLE_ROOT,
2189 "XInclude error: would result in multiple root nodes\n",
2190 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002191 return(-1);
2192 }
2193 }
2194
Daniel Veillardc14c3892004-08-16 12:34:50 +00002195 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2196 /*
2197 * Add the list of nodes
2198 */
2199 while (list != NULL) {
2200 end = list;
2201 list = list->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002202
Daniel Veillardc14c3892004-08-16 12:34:50 +00002203 xmlAddPrevSibling(cur, end);
2204 }
2205 xmlUnlinkNode(cur);
2206 xmlFreeNode(cur);
2207 } else {
2208 /*
2209 * Change the current node as an XInclude start one, and add an
2210 * XInclude end one
2211 */
2212 cur->type = XML_XINCLUDE_START;
Daniel Veillard03a53c32004-10-26 16:06:51 +00002213 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
Daniel Veillardc14c3892004-08-16 12:34:50 +00002214 if (end == NULL) {
2215 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2216 XML_XINCLUDE_BUILD_FAILED,
2217 "failed to build node\n", NULL);
2218 return(-1);
2219 }
2220 end->type = XML_XINCLUDE_END;
2221 xmlAddNextSibling(cur, end);
Owen Taylor3473f882001-02-23 17:55:21 +00002222
Daniel Veillardc14c3892004-08-16 12:34:50 +00002223 /*
2224 * Add the list of nodes
2225 */
2226 while (list != NULL) {
2227 cur = list;
2228 list = list->next;
2229
2230 xmlAddPrevSibling(end, cur);
2231 }
Owen Taylor3473f882001-02-23 17:55:21 +00002232 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002233
2234
Owen Taylor3473f882001-02-23 17:55:21 +00002235 return(0);
2236}
2237
2238/**
2239 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002240 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002241 * @node: an XInclude node
2242 *
2243 * test if the node is an XInclude node
2244 *
2245 * Returns 1 true, 0 otherwise
2246 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002247static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002248xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002249 if (node == NULL)
2250 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002251 if (node->type != XML_ELEMENT_NODE)
2252 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002253 if (node->ns == NULL)
2254 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002255 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2256 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2257 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2258 if (ctxt->legacy == 0) {
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002259#if 0 /* wait for the XML Core Working Group to get something stable ! */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002260 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2261 "Deprecated XInclude namespace found, use %s",
2262 XINCLUDE_NS);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002263#endif
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002264 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002265 }
2266 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002267 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2268 xmlNodePtr child = node->children;
2269 int nb_fallback = 0;
2270
2271 while (child != NULL) {
2272 if ((child->type == XML_ELEMENT_NODE) &&
2273 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002274 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2275 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002276 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002277 xmlXIncludeErr(ctxt, node,
2278 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2279 "%s has an 'include' child\n",
2280 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002281 return(0);
2282 }
2283 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2284 nb_fallback++;
2285 }
2286 }
2287 child = child->next;
2288 }
2289 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002290 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2291 "%s has multiple fallback children\n",
2292 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002293 return(0);
2294 }
2295 return(1);
2296 }
2297 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2298 if ((node->parent == NULL) ||
2299 (node->parent->type != XML_ELEMENT_NODE) ||
2300 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002301 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2302 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002303 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002304 xmlXIncludeErr(ctxt, node,
2305 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2306 "%s is not the child of an 'include'\n",
2307 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002308 }
2309 }
2310 }
Owen Taylor3473f882001-02-23 17:55:21 +00002311 return(0);
2312}
2313
2314/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002315 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002316 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002317 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002318 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002319 *
2320 * Implement the XInclude substitution on the XML document @doc
2321 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002322 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002323 * or the number of substitutions done.
2324 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002325static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002326xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002327 xmlNodePtr cur;
2328 int ret = 0;
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002329 int i, start;
Owen Taylor3473f882001-02-23 17:55:21 +00002330
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002331 if ((doc == NULL) || (tree == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002332 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002333 if (ctxt == NULL)
2334 return(-1);
2335
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002336 if (doc->URL != NULL) {
2337 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2338 if (ret < 0)
2339 return(-1);
2340 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00002341 start = ctxt->incNr;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002342
Owen Taylor3473f882001-02-23 17:55:21 +00002343 /*
2344 * First phase: lookup the elements in the document
2345 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002346 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002347 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002348 xmlXIncludePreProcessNode(ctxt, cur);
William M. Brack7b0e2762004-05-12 09:33:23 +00002349 while ((cur != NULL) && (cur != tree->parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002350 /* TODO: need to work on entities -> stack */
2351 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002352 (cur->children->type != XML_ENTITY_DECL) &&
2353 (cur->children->type != XML_XINCLUDE_START) &&
2354 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002355 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002356 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002357 xmlXIncludePreProcessNode(ctxt, cur);
2358 } else if (cur->next != NULL) {
2359 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002360 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002361 xmlXIncludePreProcessNode(ctxt, cur);
2362 } else {
William M. Brack5d8d10b2004-04-16 08:11:26 +00002363 if (cur == tree)
2364 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002365 do {
2366 cur = cur->parent;
William M. Brack7b0e2762004-05-12 09:33:23 +00002367 if ((cur == NULL) || (cur == tree->parent))
2368 break; /* do */
Owen Taylor3473f882001-02-23 17:55:21 +00002369 if (cur->next != NULL) {
2370 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002371 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002372 xmlXIncludePreProcessNode(ctxt, cur);
2373 break; /* do */
2374 }
2375 } while (cur != NULL);
2376 }
2377 }
2378
2379 /*
2380 * Second Phase : collect the infosets fragments
2381 */
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002382 for (i = start;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002383 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002384 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002385 }
2386
2387 /*
2388 * Third phase: extend the original document infoset.
William M. Brack6b1a28d2004-02-06 11:24:44 +00002389 *
2390 * Originally we bypassed the inclusion if there were any errors
2391 * encountered on any of the XIncludes. A bug was raised (bug
2392 * 132588) requesting that we output the XIncludes without error,
2393 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2394 * give some other problems in the future, but for now it seems to
2395 * work ok.
2396 *
Owen Taylor3473f882001-02-23 17:55:21 +00002397 */
William M. Brack6b1a28d2004-02-06 11:24:44 +00002398 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
William M. Brack95af5942004-02-08 04:12:49 +00002399 if ((ctxt->incTab[i]->inc != NULL) ||
2400 (ctxt->incTab[i]->xptr != NULL) ||
2401 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002402 xmlXIncludeIncludeNode(ctxt, i);
Owen Taylor3473f882001-02-23 17:55:21 +00002403 }
2404
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002405 if (doc->URL != NULL)
2406 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002407 return(ret);
2408}
2409
2410/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002411 * xmlXIncludeSetFlags:
2412 * @ctxt: an XInclude processing context
2413 * @flags: a set of xmlParserOption used for parsing XML includes
2414 *
2415 * Set the flags used for further processing of XML resources.
2416 *
2417 * Returns 0 in case of success and -1 in case of error.
2418 */
2419int
2420xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2421 if (ctxt == NULL)
2422 return(-1);
2423 ctxt->parseFlags = flags;
2424 return(0);
2425}
2426
2427/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002428 * xmlXIncludeProcessFlagsData:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002429 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002430 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillard681e9042006-09-29 09:16:00 +00002431 * @data: application data that will be passed to the parser context
2432 * in the _private field of the parser context(s)
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002433 *
2434 * Implement the XInclude substitution on the XML document @doc
2435 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002436 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002437 * or the number of substitutions done.
2438 */
2439int
Daniel Veillard681e9042006-09-29 09:16:00 +00002440xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002441 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002442 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002443 int ret = 0;
2444
2445 if (doc == NULL)
2446 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002447 tree = xmlDocGetRootElement(doc);
2448 if (tree == NULL)
2449 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002450 ctxt = xmlXIncludeNewContext(doc);
2451 if (ctxt == NULL)
2452 return(-1);
Daniel Veillard681e9042006-09-29 09:16:00 +00002453 ctxt->_private = data;
Daniel Veillardce244ad2004-11-05 10:03:46 +00002454 ctxt->base = xmlStrdup((xmlChar *)doc->URL);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002455 xmlXIncludeSetFlags(ctxt, flags);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002456 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2457 if ((ret >= 0) && (ctxt->nbErrors > 0))
2458 ret = -1;
2459
2460 xmlXIncludeFreeContext(ctxt);
2461 return(ret);
2462}
2463
2464/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002465 * xmlXIncludeProcessFlags:
2466 * @doc: an XML document
2467 * @flags: a set of xmlParserOption used for parsing XML includes
2468 *
2469 * Implement the XInclude substitution on the XML document @doc
2470 *
2471 * Returns 0 if no substitution were done, -1 if some processing failed
2472 * or the number of substitutions done.
2473 */
2474int
2475xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2476 return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2477}
2478
2479/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002480 * xmlXIncludeProcess:
2481 * @doc: an XML document
2482 *
2483 * Implement the XInclude substitution on the XML document @doc
2484 *
2485 * Returns 0 if no substitution were done, -1 if some processing failed
2486 * or the number of substitutions done.
2487 */
2488int
2489xmlXIncludeProcess(xmlDocPtr doc) {
2490 return(xmlXIncludeProcessFlags(doc, 0));
2491}
2492
2493/**
2494 * xmlXIncludeProcessTreeFlags:
2495 * @tree: a node in an XML document
2496 * @flags: a set of xmlParserOption used for parsing XML includes
2497 *
2498 * Implement the XInclude substitution for the given subtree
2499 *
2500 * Returns 0 if no substitution were done, -1 if some processing failed
2501 * or the number of substitutions done.
2502 */
2503int
2504xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2505 xmlXIncludeCtxtPtr ctxt;
2506 int ret = 0;
2507
2508 if ((tree == NULL) || (tree->doc == NULL))
2509 return(-1);
2510 ctxt = xmlXIncludeNewContext(tree->doc);
2511 if (ctxt == NULL)
2512 return(-1);
William M. Brackf7789b12004-06-07 08:57:27 +00002513 ctxt->base = xmlNodeGetBase(tree->doc, tree);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002514 xmlXIncludeSetFlags(ctxt, flags);
2515 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2516 if ((ret >= 0) && (ctxt->nbErrors > 0))
2517 ret = -1;
2518
2519 xmlXIncludeFreeContext(ctxt);
2520 return(ret);
2521}
2522
2523/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002524 * xmlXIncludeProcessTree:
2525 * @tree: a node in an XML document
2526 *
2527 * Implement the XInclude substitution for the given subtree
2528 *
2529 * Returns 0 if no substitution were done, -1 if some processing failed
2530 * or the number of substitutions done.
2531 */
2532int
2533xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002534 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002535}
2536
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002537/**
2538 * xmlXIncludeProcessNode:
2539 * @ctxt: an existing XInclude context
2540 * @node: a node in an XML document
2541 *
2542 * Implement the XInclude substitution for the given subtree reusing
2543 * the informations and data coming from the given context.
2544 *
2545 * Returns 0 if no substitution were done, -1 if some processing failed
2546 * or the number of substitutions done.
2547 */
2548int
2549xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2550 int ret = 0;
2551
2552 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2553 return(-1);
2554 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2555 if ((ret >= 0) && (ctxt->nbErrors > 0))
2556 ret = -1;
2557 return(ret);
2558}
2559
Owen Taylor3473f882001-02-23 17:55:21 +00002560#else /* !LIBXML_XINCLUDE_ENABLED */
2561#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002562#define bottom_xinclude
2563#include "elfgcchack.h"