blob: 7394d0c1c091930e6b38b44c9db1c02c671a4c4c [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;
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000422 xmlParserInputPtr inputStream;
Daniel Veillard98485322003-08-14 15:44:40 +0000423
424 xmlInitParser();
425
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000426 pctxt = xmlNewParserCtxt();
Daniel Veillard98485322003-08-14 15:44:40 +0000427 if (pctxt == NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000428 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
Daniel Veillard98485322003-08-14 15:44:40 +0000429 return(NULL);
430 }
Daniel Veillard681e9042006-09-29 09:16:00 +0000431
432 /*
433 * pass in the application data to the parser context.
434 */
435 pctxt->_private = ctxt->_private;
436
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000437 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000438 * try to ensure that new documents included are actually
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000439 * built with the same dictionary as the including document.
440 */
Daniel Veillardcb6f5252009-08-25 19:24:15 +0200441 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
442 if (pctxt->dict != NULL)
443 xmlDictFree(pctxt->dict);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000444 pctxt->dict = ctxt->doc->dict;
445 xmlDictReference(pctxt->dict);
446 }
447
448 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
449
450 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
451 if (inputStream == NULL) {
452 xmlFreeParserCtxt(pctxt);
453 return(NULL);
454 }
455
456 inputPush(pctxt, inputStream);
Daniel Veillard98485322003-08-14 15:44:40 +0000457
Daniel Veillard37d2d162008-03-14 10:54:00 +0000458 if (pctxt->directory == NULL)
459 pctxt->directory = xmlParserGetDirectory(URL);
Daniel Veillard98485322003-08-14 15:44:40 +0000460
William M. Bracka22da292005-02-12 01:08:22 +0000461 pctxt->loadsubset |= XML_DETECT_IDS;
Daniel Veillard98485322003-08-14 15:44:40 +0000462
463 xmlParseDocument(pctxt);
464
William M. Brack1ff42132003-12-31 14:05:15 +0000465 if (pctxt->wellFormed) {
Daniel Veillard98485322003-08-14 15:44:40 +0000466 ret = pctxt->myDoc;
William M. Brack1ff42132003-12-31 14:05:15 +0000467 }
Daniel Veillard98485322003-08-14 15:44:40 +0000468 else {
469 ret = NULL;
Daniel Veillard500a1de2004-03-22 15:22:58 +0000470 if (pctxt->myDoc != NULL)
Daniel Veillard4773df22004-01-23 13:15:13 +0000471 xmlFreeDoc(pctxt->myDoc);
Daniel Veillard98485322003-08-14 15:44:40 +0000472 pctxt->myDoc = NULL;
473 }
474 xmlFreeParserCtxt(pctxt);
475
476 return(ret);
477}
478
479/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000480 * xmlXIncludeAddNode:
481 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000482 * @cur: the new node
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000483 *
484 * Add a new node to process to an XInclude context
485 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000486static int
487xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
488 xmlXIncludeRefPtr ref;
489 xmlURIPtr uri;
490 xmlChar *URL;
491 xmlChar *fragment = NULL;
492 xmlChar *href;
493 xmlChar *parse;
494 xmlChar *base;
495 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000496 int xml = 1, i; /* default Issue 64 */
497 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000498
499
500 if (ctxt == NULL)
501 return(-1);
502 if (cur == NULL)
503 return(-1);
504
505#ifdef DEBUG_XINCLUDE
506 xmlGenericError(xmlGenericErrorContext, "Add node\n");
507#endif
508 /*
509 * read the attributes
510 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000511 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000512 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000513 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
514 if (href == NULL)
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000515 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000516 }
Daniel Veillardb242b082008-02-08 09:56:31 +0000517 if ((href[0] == '#') || (href[0] == 0))
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000518 local = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000519 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000520 if (parse != NULL) {
521 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
522 xml = 1;
523 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
524 xml = 0;
525 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000526 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
527 "invalid value %s for 'parse'\n", parse);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000528 if (href != NULL)
529 xmlFree(href);
530 if (parse != NULL)
531 xmlFree(parse);
532 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000533 }
534 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000535
536 /*
537 * compute the URI
538 */
539 base = xmlNodeGetBase(ctxt->doc, cur);
540 if (base == NULL) {
541 URI = xmlBuildURI(href, ctxt->doc->URL);
542 } else {
543 URI = xmlBuildURI(href, base);
544 }
545 if (URI == NULL) {
546 xmlChar *escbase;
547 xmlChar *eschref;
548 /*
549 * Some escaping may be needed
550 */
551 escbase = xmlURIEscape(base);
552 eschref = xmlURIEscape(href);
553 URI = xmlBuildURI(eschref, escbase);
554 if (escbase != NULL)
555 xmlFree(escbase);
556 if (eschref != NULL)
557 xmlFree(eschref);
558 }
559 if (parse != NULL)
560 xmlFree(parse);
561 if (href != NULL)
562 xmlFree(href);
563 if (base != NULL)
564 xmlFree(base);
565 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000566 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
567 "failed build URL\n", NULL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000568 return(-1);
569 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000570 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000571
572 /*
573 * Check the URL and remove any fragment identifier
574 */
575 uri = xmlParseURI((const char *)URI);
576 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000577 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
578 "invalid value URI %s\n", URI);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000579 if (fragment != NULL)
580 xmlFree(fragment);
581 xmlFree(URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000582 return(-1);
583 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000584
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000585 if (uri->fragment != NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000586 if (ctxt->legacy != 0) {
587 if (fragment == NULL) {
588 fragment = (xmlChar *) uri->fragment;
589 } else {
590 xmlFree(uri->fragment);
591 }
592 } else {
593 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
594 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
595 URI);
596 if (fragment != NULL)
597 xmlFree(fragment);
598 xmlFreeURI(uri);
599 xmlFree(URI);
600 return(-1);
601 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000602 uri->fragment = NULL;
603 }
604 URL = xmlSaveUri(uri);
605 xmlFreeURI(uri);
606 xmlFree(URI);
607 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000608 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
609 "invalid value URI %s\n", URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000610 if (fragment != NULL)
611 xmlFree(fragment);
612 return(-1);
613 }
614
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000615 /*
Daniel Veillardb242b082008-02-08 09:56:31 +0000616 * If local and xml then we need a fragment
617 */
618 if ((local == 1) && (xml == 1) &&
619 ((fragment == NULL) || (fragment[0] == 0))) {
620 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
621 "detected a local recursion with no xpointer in %s\n",
622 URL);
623 if (fragment != NULL)
624 xmlFree(fragment);
625 return(-1);
626 }
627
628 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000629 * Check the URL against the stack for recursions
630 */
Daniel Veillardbf630c02006-06-06 08:21:41 +0000631 if ((!local) && (xml == 1)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000632 for (i = 0;i < ctxt->urlNr;i++) {
633 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000634 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
635 "detected a recursion in %s\n", URL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000636 return(-1);
637 }
638 }
639 }
640
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000641 ref = xmlXIncludeNewRef(ctxt, URL, cur);
642 if (ref == NULL) {
643 return(-1);
644 }
645 ref->fragment = fragment;
646 ref->doc = NULL;
647 ref->xml = xml;
648 ref->count = 1;
649 xmlFree(URL);
650 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000651}
652
653/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000654 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000655 * @ctxt: the XInclude context
656 * @doc: the new document
657 * @url: the associated URL
658 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000659 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000660 */
661static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000662xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000663 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000664 xmlXIncludeCtxtPtr newctxt;
665 int i;
666
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000667 /*
668 * Avoid recursion in already substitued resources
669 for (i = 0;i < ctxt->urlNr;i++) {
670 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
671 return;
672 }
673 */
674
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000675#ifdef DEBUG_XINCLUDE
676 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
677#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000678 /*
679 * Handle recursion here.
680 */
681
682 newctxt = xmlXIncludeNewContext(doc);
683 if (newctxt != NULL) {
684 /*
Daniel Veillarda6585822006-12-04 09:21:28 +0000685 * Copy the private user data
686 */
687 newctxt->_private = ctxt->_private;
688 /*
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000689 * Copy the existing document set
690 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000691 newctxt->incMax = ctxt->incMax;
692 newctxt->incNr = ctxt->incNr;
693 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
694 sizeof(newctxt->incTab[0]));
695 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000696 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000697 xmlFree(newctxt);
698 return;
699 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000700 /*
701 * copy the urlTab
702 */
703 newctxt->urlMax = ctxt->urlMax;
704 newctxt->urlNr = ctxt->urlNr;
705 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000706
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000707 /*
William M. Brackf7789b12004-06-07 08:57:27 +0000708 * Inherit the existing base
709 */
Daniel Veillardce244ad2004-11-05 10:03:46 +0000710 newctxt->base = xmlStrdup(ctxt->base);
William M. Brackf7789b12004-06-07 08:57:27 +0000711
712 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000713 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000714 */
715 newctxt->incBase = ctxt->incNr;
716 for (i = 0;i < ctxt->incNr;i++) {
717 newctxt->incTab[i] = ctxt->incTab[i];
718 newctxt->incTab[i]->count++; /* prevent the recursion from
719 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000720 }
William M. Bracka11e4832004-03-07 11:03:43 +0000721 /*
722 * The new context should also inherit the Parse Flags
723 * (bug 132597)
724 */
725 newctxt->parseFlags = ctxt->parseFlags;
Daniel Veillard8edf1c52003-07-22 20:52:14 +0000726 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000727 for (i = 0;i < ctxt->incNr;i++) {
728 newctxt->incTab[i]->count--;
729 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000730 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000731
732 /* urlTab may have been reallocated */
733 ctxt->urlTab = newctxt->urlTab;
734 ctxt->urlMax = newctxt->urlMax;
735
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000736 newctxt->urlMax = 0;
737 newctxt->urlNr = 0;
738 newctxt->urlTab = NULL;
739
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000740 xmlXIncludeFreeContext(newctxt);
741 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000742#ifdef DEBUG_XINCLUDE
743 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
744#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000745}
746
747/**
748 * xmlXIncludeAddTxt:
749 * @ctxt: the XInclude context
750 * @txt: the new text node
751 * @url: the associated URL
752 *
753 * Add a new txtument to the list
754 */
755static void
756xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000757#ifdef DEBUG_XINCLUDE
758 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
759#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000760 if (ctxt->txtMax == 0) {
761 ctxt->txtMax = 4;
762 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
763 sizeof(ctxt->txtTab[0]));
764 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000765 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000766 return;
767 }
768 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
769 sizeof(ctxt->txturlTab[0]));
770 if (ctxt->txturlTab == 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 }
775 if (ctxt->txtNr >= ctxt->txtMax) {
776 ctxt->txtMax *= 2;
777 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
778 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
779 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000780 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000781 return;
782 }
783 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000784 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000785 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000786 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000787 return;
788 }
789 }
790 ctxt->txtTab[ctxt->txtNr] = txt;
791 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
792 ctxt->txtNr++;
793}
794
Owen Taylor3473f882001-02-23 17:55:21 +0000795/************************************************************************
796 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000797 * Node copy with specific semantic *
798 * *
799 ************************************************************************/
800
Daniel Veillardcb6f5252009-08-25 19:24:15 +0200801static xmlNodePtr
802xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
803 xmlDocPtr source, xmlNodePtr elem);
804
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000805/**
806 * xmlXIncludeCopyNode:
807 * @ctxt: the XInclude context
808 * @target: the document target
809 * @source: the document source
810 * @elem: the element
811 *
812 * Make a copy of the node while preserving the XInclude semantic
813 * of the Infoset copy
814 */
815static xmlNodePtr
816xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
817 xmlDocPtr source, xmlNodePtr elem) {
818 xmlNodePtr result = NULL;
819
820 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
821 (elem == NULL))
822 return(NULL);
823 if (elem->type == XML_DTD_NODE)
824 return(NULL);
Daniel Veillardcb6f5252009-08-25 19:24:15 +0200825 if (elem->type == XML_DOCUMENT_NODE)
826 result = xmlXIncludeCopyNodeList(ctxt, target, source, elem->children);
827 else
828 result = xmlDocCopyNode(elem, target, 1);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000829 return(result);
830}
831
832/**
833 * xmlXIncludeCopyNodeList:
834 * @ctxt: the XInclude context
835 * @target: the document target
836 * @source: the document source
837 * @elem: the element list
838 *
839 * Make a copy of the node list while preserving the XInclude semantic
840 * of the Infoset copy
841 */
842static xmlNodePtr
843xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
844 xmlDocPtr source, xmlNodePtr elem) {
845 xmlNodePtr cur, res, result = NULL, last = NULL;
846
847 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
848 (elem == NULL))
849 return(NULL);
850 cur = elem;
851 while (cur != NULL) {
852 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
853 if (res != NULL) {
854 if (result == NULL) {
855 result = last = res;
856 } else {
857 last->next = res;
858 res->prev = last;
859 last = res;
860 }
861 }
862 cur = cur->next;
863 }
864 return(result);
865}
866
867/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000868 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000869 * @cur: the node
870 * @no: the child number
871 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000872 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000873 */
874static xmlNodePtr
875xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
876 int i;
877 if (cur == NULL)
878 return(cur);
879 cur = cur->children;
880 for (i = 0;i <= no;cur = cur->next) {
881 if (cur == NULL)
882 return(cur);
883 if ((cur->type == XML_ELEMENT_NODE) ||
884 (cur->type == XML_DOCUMENT_NODE) ||
885 (cur->type == XML_HTML_DOCUMENT_NODE)) {
886 i++;
887 if (i == no)
888 break;
889 }
890 }
891 return(cur);
892}
893
William M. Brackf7eb7942003-12-31 07:59:17 +0000894xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000895/**
896 * xmlXIncludeCopyRange:
897 * @ctxt: the XInclude context
898 * @target: the document target
899 * @source: the document source
900 * @obj: the XPointer result from the evaluation.
901 *
902 * Build a node list tree copy of the XPointer result.
903 *
904 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000905 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000906 */
907static xmlNodePtr
908xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
909 xmlDocPtr source, xmlXPathObjectPtr range) {
910 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000911 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
912 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000913 /* pointers to traversal nodes */
914 xmlNodePtr start, cur, end;
915 int index1, index2;
William M. Brack6bdacd72004-02-07 08:53:23 +0000916 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000917
918 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
919 (range == NULL))
920 return(NULL);
921 if (range->type != XPATH_RANGE)
922 return(NULL);
923 start = (xmlNodePtr) range->user;
924
925 if (start == NULL)
926 return(NULL);
927 end = range->user2;
928 if (end == NULL)
929 return(xmlDocCopyNode(start, target, 1));
930
931 cur = start;
932 index1 = range->index;
933 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000934 /*
935 * level is depth of the current node under consideration
936 * list is the pointer to the root of the output tree
937 * listParent is a pointer to the parent of output tree (within
938 the included file) in case we need to add another level
939 * last is a pointer to the last node added to the output tree
940 * lastLevel is the depth of last (relative to the root)
941 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000942 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000943 /*
944 * Check if our output tree needs a parent
945 */
946 if (level < 0) {
947 while (level < 0) {
William M. Brack57e9e912004-03-09 16:19:02 +0000948 /* copy must include namespaces and properties */
949 tmp2 = xmlDocCopyNode(listParent, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000950 xmlAddChild(tmp2, list);
951 list = tmp2;
952 listParent = listParent->parent;
953 level++;
954 }
955 last = list;
956 lastLevel = 0;
957 }
958 /*
959 * Check whether we need to change our insertion point
960 */
961 while (level < lastLevel) {
962 last = last->parent;
963 lastLevel --;
964 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000965 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000966 if (cur->type == XML_TEXT_NODE) {
967 const xmlChar *content = cur->content;
968 int len;
969
970 if (content == NULL) {
971 tmp = xmlNewTextLen(NULL, 0);
972 } else {
973 len = index2;
974 if ((cur == start) && (index1 > 1)) {
975 content += (index1 - 1);
976 len -= (index1 - 1);
977 index1 = 0;
978 } else {
979 len = index2;
980 }
981 tmp = xmlNewTextLen(content, len);
982 }
983 /* single sub text node selection */
984 if (list == NULL)
985 return(tmp);
986 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +0000987 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000988 xmlAddNextSibling(last, tmp);
989 else
William M. Brackf7eb7942003-12-31 07:59:17 +0000990 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000991 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +0000992 } else { /* ending node not a text node */
William M. Brack6bdacd72004-02-07 08:53:23 +0000993 endLevel = level; /* remember the level of the end node */
994 endFlag = 1;
William M. Brack57e9e912004-03-09 16:19:02 +0000995 /* last node - need to take care of properties + namespaces */
996 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000997 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000998 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000999 listParent = cur->parent;
1000 } else {
1001 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001002 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +00001003 else {
1004 xmlAddChild(last, tmp);
1005 lastLevel = level;
1006 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001007 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001008 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001009
1010 if (index2 > 1) {
1011 end = xmlXIncludeGetNthChild(cur, index2 - 1);
1012 index2 = 0;
1013 }
1014 if ((cur == start) && (index1 > 1)) {
1015 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1016 index1 = 0;
William M. Brack6bdacd72004-02-07 08:53:23 +00001017 } else {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001018 cur = cur->children;
1019 }
William M. Brack6bdacd72004-02-07 08:53:23 +00001020 level++; /* increment level to show change */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001021 /*
1022 * Now gather the remaining nodes from cur to end
1023 */
William M. Brack6bdacd72004-02-07 08:53:23 +00001024 continue; /* while */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001025 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001026 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001027 if ((cur->type == XML_TEXT_NODE) ||
1028 (cur->type == XML_CDATA_SECTION_NODE)) {
1029 const xmlChar *content = cur->content;
1030
1031 if (content == NULL) {
1032 tmp = xmlNewTextLen(NULL, 0);
1033 } else {
1034 if (index1 > 1) {
1035 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +00001036 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001037 }
1038 tmp = xmlNewText(content);
1039 }
1040 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001041 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001042 } else { /* Not text node */
William M. Brack57e9e912004-03-09 16:19:02 +00001043 /*
1044 * start of the range - need to take care of
1045 * properties and namespaces
1046 */
1047 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +00001048 list = last = tmp;
1049 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001050 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001051 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001052 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001053 index1 = 0;
1054 /*
1055 * Now gather the remaining nodes from cur to end
1056 */
1057 continue; /* while */
1058 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001059 }
1060 } else {
1061 tmp = NULL;
1062 switch (cur->type) {
1063 case XML_DTD_NODE:
1064 case XML_ELEMENT_DECL:
1065 case XML_ATTRIBUTE_DECL:
1066 case XML_ENTITY_NODE:
1067 /* Do not copy DTD informations */
1068 break;
1069 case XML_ENTITY_DECL:
1070 /* handle crossing entities -> stack needed */
1071 break;
1072 case XML_XINCLUDE_START:
1073 case XML_XINCLUDE_END:
1074 /* don't consider it part of the tree content */
1075 break;
1076 case XML_ATTRIBUTE_NODE:
1077 /* Humm, should not happen ! */
1078 break;
1079 default:
William M. Brack57e9e912004-03-09 16:19:02 +00001080 /*
1081 * Middle of the range - need to take care of
1082 * properties and namespaces
1083 */
1084 tmp = xmlDocCopyNode(cur, target, 2);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001085 break;
1086 }
1087 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001088 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001089 xmlAddNextSibling(last, tmp);
1090 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001091 xmlAddChild(last, tmp);
1092 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001093 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001094 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001095 }
1096 }
1097 /*
1098 * Skip to next node in document order
1099 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001100 cur = xmlXPtrAdvanceNode(cur, &level);
William M. Brack6bdacd72004-02-07 08:53:23 +00001101 if (endFlag && (level >= endLevel))
1102 break;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001103 }
1104 return(list);
1105}
1106
1107/**
1108 * xmlXIncludeBuildNodeList:
1109 * @ctxt: the XInclude context
1110 * @target: the document target
1111 * @source: the document source
1112 * @obj: the XPointer result from the evaluation.
1113 *
1114 * Build a node list tree copy of the XPointer result.
1115 * This will drop Attributes and Namespace declarations.
1116 *
1117 * Returns an xmlNodePtr list or NULL.
1118 * the caller has to free the node tree.
1119 */
1120static xmlNodePtr
1121xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1122 xmlDocPtr source, xmlXPathObjectPtr obj) {
1123 xmlNodePtr list = NULL, last = NULL;
1124 int i;
1125
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001126 if (source == NULL)
1127 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001128 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1129 (obj == NULL))
1130 return(NULL);
1131 switch (obj->type) {
1132 case XPATH_NODESET: {
1133 xmlNodeSetPtr set = obj->nodesetval;
1134 if (set == NULL)
1135 return(NULL);
1136 for (i = 0;i < set->nodeNr;i++) {
1137 if (set->nodeTab[i] == NULL)
1138 continue;
1139 switch (set->nodeTab[i]->type) {
1140 case XML_TEXT_NODE:
1141 case XML_CDATA_SECTION_NODE:
1142 case XML_ELEMENT_NODE:
1143 case XML_ENTITY_REF_NODE:
1144 case XML_ENTITY_NODE:
1145 case XML_PI_NODE:
1146 case XML_COMMENT_NODE:
1147 case XML_DOCUMENT_NODE:
1148 case XML_HTML_DOCUMENT_NODE:
1149#ifdef LIBXML_DOCB_ENABLED
1150 case XML_DOCB_DOCUMENT_NODE:
1151#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001152 case XML_XINCLUDE_END:
1153 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001154 case XML_XINCLUDE_START: {
1155 xmlNodePtr tmp, cur = set->nodeTab[i];
1156
1157 cur = cur->next;
1158 while (cur != NULL) {
1159 switch(cur->type) {
1160 case XML_TEXT_NODE:
1161 case XML_CDATA_SECTION_NODE:
1162 case XML_ELEMENT_NODE:
1163 case XML_ENTITY_REF_NODE:
1164 case XML_ENTITY_NODE:
1165 case XML_PI_NODE:
1166 case XML_COMMENT_NODE:
1167 tmp = xmlXIncludeCopyNode(ctxt, target,
1168 source, cur);
1169 if (last == NULL) {
1170 list = last = tmp;
1171 } else {
1172 xmlAddNextSibling(last, tmp);
1173 last = tmp;
1174 }
1175 cur = cur->next;
1176 continue;
1177 default:
1178 break;
1179 }
1180 break;
1181 }
1182 continue;
1183 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001184 case XML_ATTRIBUTE_NODE:
1185 case XML_NAMESPACE_DECL:
1186 case XML_DOCUMENT_TYPE_NODE:
1187 case XML_DOCUMENT_FRAG_NODE:
1188 case XML_NOTATION_NODE:
1189 case XML_DTD_NODE:
1190 case XML_ELEMENT_DECL:
1191 case XML_ATTRIBUTE_DECL:
1192 case XML_ENTITY_DECL:
1193 continue; /* for */
1194 }
1195 if (last == NULL)
1196 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1197 set->nodeTab[i]);
1198 else {
1199 xmlAddNextSibling(last,
1200 xmlXIncludeCopyNode(ctxt, target, source,
1201 set->nodeTab[i]));
1202 if (last->next != NULL)
1203 last = last->next;
1204 }
1205 }
1206 break;
1207 }
1208 case XPATH_LOCATIONSET: {
1209 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1210 if (set == NULL)
1211 return(NULL);
1212 for (i = 0;i < set->locNr;i++) {
1213 if (last == NULL)
1214 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1215 set->locTab[i]);
1216 else
1217 xmlAddNextSibling(last,
1218 xmlXIncludeCopyXPointer(ctxt, target, source,
1219 set->locTab[i]));
1220 if (last != NULL) {
1221 while (last->next != NULL)
1222 last = last->next;
1223 }
1224 }
1225 break;
1226 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001227#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001228 case XPATH_RANGE:
1229 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001230#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001231 case XPATH_POINT:
1232 /* points are ignored in XInclude */
1233 break;
1234 default:
1235 break;
1236 }
1237 return(list);
1238}
1239/************************************************************************
1240 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001241 * XInclude I/O handling *
1242 * *
1243 ************************************************************************/
1244
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001245typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1246typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1247struct _xmlXIncludeMergeData {
1248 xmlDocPtr doc;
1249 xmlXIncludeCtxtPtr ctxt;
1250};
1251
Owen Taylor3473f882001-02-23 17:55:21 +00001252/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001253 * xmlXIncludeMergeOneEntity:
1254 * @ent: the entity
1255 * @doc: the including doc
1256 * @nr: the entity name
1257 *
1258 * Inplements the merge of one entity
1259 */
1260static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001261xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001262 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001263 xmlEntityPtr ret, prev;
1264 xmlDocPtr doc;
1265 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001266
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001267 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001268 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001269 ctxt = data->ctxt;
1270 doc = data->doc;
1271 if ((ctxt == NULL) || (doc == NULL))
1272 return;
1273 switch (ent->etype) {
1274 case XML_INTERNAL_PARAMETER_ENTITY:
1275 case XML_EXTERNAL_PARAMETER_ENTITY:
1276 case XML_INTERNAL_PREDEFINED_ENTITY:
1277 return;
1278 case XML_INTERNAL_GENERAL_ENTITY:
1279 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1280 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1281 break;
1282 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001283 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1284 ent->SystemID, ent->content);
1285 if (ret != NULL) {
1286 if (ent->URI != NULL)
1287 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001288 } else {
1289 prev = xmlGetDocEntity(doc, ent->name);
1290 if (prev != NULL) {
1291 if (ent->etype != prev->etype)
1292 goto error;
1293
1294 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1295 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1296 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001297 } else if ((ent->ExternalID != NULL) &&
1298 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001299 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1300 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001301 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1302 if (!xmlStrEqual(ent->content, prev->content))
1303 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001304 } else {
1305 goto error;
1306 }
1307
1308 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001309 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001310 return;
1311error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001312 switch (ent->etype) {
1313 case XML_INTERNAL_PARAMETER_ENTITY:
1314 case XML_EXTERNAL_PARAMETER_ENTITY:
1315 case XML_INTERNAL_PREDEFINED_ENTITY:
1316 case XML_INTERNAL_GENERAL_ENTITY:
1317 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1318 return;
1319 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1320 break;
1321 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001322 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1323 "mismatch in redefinition of entity %s\n",
1324 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001325}
1326
1327/**
1328 * xmlXIncludeMergeEntities:
1329 * @ctxt: an XInclude context
1330 * @doc: the including doc
1331 * @from: the included doc
1332 *
1333 * Inplements the entity merge
1334 *
1335 * Returns 0 if merge succeeded, -1 if some processing failed
1336 */
1337static int
1338xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1339 xmlDocPtr from) {
1340 xmlNodePtr cur;
1341 xmlDtdPtr target, source;
1342
1343 if (ctxt == NULL)
1344 return(-1);
1345
1346 if ((from == NULL) || (from->intSubset == NULL))
1347 return(0);
1348
1349 target = doc->intSubset;
1350 if (target == NULL) {
1351 cur = xmlDocGetRootElement(doc);
1352 if (cur == NULL)
1353 return(-1);
1354 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1355 if (target == NULL)
1356 return(-1);
1357 }
1358
1359 source = from->intSubset;
1360 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001361 xmlXIncludeMergeData data;
1362
1363 data.ctxt = ctxt;
1364 data.doc = doc;
1365
Daniel Veillard4287c572003-02-04 22:48:53 +00001366 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001367 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001368 }
1369 source = from->extSubset;
1370 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001371 xmlXIncludeMergeData data;
1372
1373 data.ctxt = ctxt;
1374 data.doc = doc;
1375
Daniel Veillard4287c572003-02-04 22:48:53 +00001376 /*
1377 * don't duplicate existing stuff when external subsets are the same
1378 */
1379 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1380 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1381 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001382 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001383 }
1384 }
1385 return(0);
1386}
1387
1388/**
Owen Taylor3473f882001-02-23 17:55:21 +00001389 * xmlXIncludeLoadDoc:
1390 * @ctxt: the XInclude context
1391 * @url: the associated URL
1392 * @nr: the xinclude node number
1393 *
1394 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001395 *
1396 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001397 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001398static int
Owen Taylor3473f882001-02-23 17:55:21 +00001399xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1400 xmlDocPtr doc;
1401 xmlURIPtr uri;
1402 xmlChar *URL;
1403 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001404 int i = 0;
William M. Brack4d59e222004-03-08 14:42:31 +00001405#ifdef LIBXML_XPTR_ENABLED
1406 int saveFlags;
1407#endif
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001408
1409#ifdef DEBUG_XINCLUDE
1410 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1411#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001412 /*
1413 * Check the URL and remove any fragment identifier
1414 */
1415 uri = xmlParseURI((const char *)url);
1416 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001417 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1418 XML_XINCLUDE_HREF_URI,
1419 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001420 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001421 }
1422 if (uri->fragment != NULL) {
1423 fragment = (xmlChar *) uri->fragment;
1424 uri->fragment = NULL;
1425 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001426 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1427 (ctxt->incTab[nr]->fragment != NULL)) {
1428 if (fragment != NULL) xmlFree(fragment);
1429 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1430 }
Owen Taylor3473f882001-02-23 17:55:21 +00001431 URL = xmlSaveUri(uri);
1432 xmlFreeURI(uri);
1433 if (URL == NULL) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00001434 if (ctxt->incTab != NULL)
1435 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1436 XML_XINCLUDE_HREF_URI,
1437 "invalid value URI %s\n", url);
1438 else
1439 xmlXIncludeErr(ctxt, NULL,
1440 XML_XINCLUDE_HREF_URI,
1441 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001442 if (fragment != NULL)
1443 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001444 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001445 }
1446
1447 /*
1448 * Handling of references to the local document are done
1449 * directly through ctxt->doc.
1450 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001451 if ((URL[0] == 0) || (URL[0] == '#') ||
1452 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001453 doc = NULL;
1454 goto loaded;
1455 }
1456
1457 /*
1458 * Prevent reloading twice the document.
1459 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001460 for (i = 0; i < ctxt->incNr; i++) {
1461 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1462 (ctxt->incTab[i]->doc != NULL)) {
1463 doc = ctxt->incTab[i]->doc;
1464#ifdef DEBUG_XINCLUDE
1465 printf("Already loaded %s\n", URL);
1466#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001467 goto loaded;
1468 }
1469 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001470
Owen Taylor3473f882001-02-23 17:55:21 +00001471 /*
1472 * Load it.
1473 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001474#ifdef DEBUG_XINCLUDE
1475 printf("loading %s\n", URL);
1476#endif
William M. Brack4d59e222004-03-08 14:42:31 +00001477#ifdef LIBXML_XPTR_ENABLED
1478 /*
1479 * If this is an XPointer evaluation, we want to assure that
1480 * all entities have been resolved prior to processing the
1481 * referenced document
1482 */
1483 saveFlags = ctxt->parseFlags;
1484 if (fragment != NULL) { /* if this is an XPointer eval */
1485 ctxt->parseFlags |= XML_PARSE_NOENT;
1486 }
1487#endif
1488
Daniel Veillard98485322003-08-14 15:44:40 +00001489 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
William M. Brack4d59e222004-03-08 14:42:31 +00001490#ifdef LIBXML_XPTR_ENABLED
1491 ctxt->parseFlags = saveFlags;
1492#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001493 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001494 xmlFree(URL);
1495 if (fragment != NULL)
1496 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001497 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001498 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001499 ctxt->incTab[nr]->doc = doc;
William M. Brackb85c9202004-07-26 00:20:13 +00001500 /*
1501 * It's possible that the requested URL has been mapped to a
1502 * completely different location (e.g. through a catalog entry).
1503 * To check for this, we compare the URL with that of the doc
1504 * and change it if they disagree (bug 146988).
1505 */
1506 if (!xmlStrEqual(URL, doc->URL)) {
1507 xmlFree(URL);
1508 URL = xmlStrdup(doc->URL);
1509 }
Daniel Veillard98485322003-08-14 15:44:40 +00001510 for (i = nr + 1; i < ctxt->incNr; i++) {
1511 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1512 ctxt->incTab[nr]->count++;
1513#ifdef DEBUG_XINCLUDE
1514 printf("Increasing %s count since reused\n", URL);
1515#endif
1516 break;
1517 }
1518 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001519
1520 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001521 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001522 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001523 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001524
1525 /*
1526 * We don't need the DTD anymore, free up space
1527 if (doc->intSubset != NULL) {
1528 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1529 xmlFreeNode((xmlNodePtr) doc->intSubset);
1530 doc->intSubset = NULL;
1531 }
1532 if (doc->extSubset != NULL) {
1533 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1534 xmlFreeNode((xmlNodePtr) doc->extSubset);
1535 doc->extSubset = NULL;
1536 }
1537 */
1538 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001539
1540loaded:
1541 if (fragment == NULL) {
1542 /*
1543 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001544 */
1545 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001546 {
1547 /* Hopefully a DTD declaration won't be copied from
1548 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001549 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001550 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001551 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001552 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001553 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001554 }
1555#ifdef LIBXML_XPTR_ENABLED
1556 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001557 /*
1558 * Computes the XPointer expression and make a copy used
1559 * as the replacement copy.
1560 */
1561 xmlXPathObjectPtr xptr;
1562 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001563 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001564
1565 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001566 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1567 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001568 } else {
1569 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1570 }
1571 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001572 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1573 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001574 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001575 xmlFree(URL);
1576 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001577 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001578 }
1579 xptr = xmlXPtrEval(fragment, xptrctxt);
1580 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001581 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1582 XML_XINCLUDE_XPTR_FAILED,
1583 "XPointer evaluation failed: #%s\n",
1584 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001585 xmlXPathFreeContext(xptrctxt);
1586 xmlFree(URL);
1587 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001588 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001589 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001590 switch (xptr->type) {
1591 case XPATH_UNDEFINED:
1592 case XPATH_BOOLEAN:
1593 case XPATH_NUMBER:
1594 case XPATH_STRING:
1595 case XPATH_POINT:
1596 case XPATH_USERS:
1597 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001598 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1599 XML_XINCLUDE_XPTR_RESULT,
1600 "XPointer is not a range: #%s\n",
1601 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001602 xmlXPathFreeContext(xptrctxt);
1603 xmlFree(URL);
1604 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001605 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001606 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001607 if ((xptr->nodesetval == NULL) ||
1608 (xptr->nodesetval->nodeNr <= 0)) {
1609 xmlXPathFreeContext(xptrctxt);
1610 xmlFree(URL);
1611 xmlFree(fragment);
1612 return(-1);
1613 }
William M. Brackabf598b2004-06-08 02:01:28 +00001614
Daniel Veillard39196eb2001-06-19 18:09:42 +00001615 case XPATH_RANGE:
1616 case XPATH_LOCATIONSET:
1617 break;
1618 }
1619 set = xptr->nodesetval;
1620 if (set != NULL) {
1621 for (i = 0;i < set->nodeNr;i++) {
1622 if (set->nodeTab[i] == NULL)
1623 continue;
1624 switch (set->nodeTab[i]->type) {
William M. Brackb0a94e82007-07-18 18:04:55 +00001625 case XML_ELEMENT_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001626 case XML_TEXT_NODE:
1627 case XML_CDATA_SECTION_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001628 case XML_ENTITY_REF_NODE:
1629 case XML_ENTITY_NODE:
1630 case XML_PI_NODE:
1631 case XML_COMMENT_NODE:
1632 case XML_DOCUMENT_NODE:
1633 case XML_HTML_DOCUMENT_NODE:
1634#ifdef LIBXML_DOCB_ENABLED
1635 case XML_DOCB_DOCUMENT_NODE:
1636#endif
1637 continue;
William M. Brackabf598b2004-06-08 02:01:28 +00001638
Daniel Veillard39196eb2001-06-19 18:09:42 +00001639 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001640 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1641 XML_XINCLUDE_XPTR_RESULT,
1642 "XPointer selects an attribute: #%s\n",
1643 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001644 set->nodeTab[i] = NULL;
1645 continue;
1646 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001647 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1648 XML_XINCLUDE_XPTR_RESULT,
1649 "XPointer selects a namespace: #%s\n",
1650 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001651 set->nodeTab[i] = NULL;
1652 continue;
1653 case XML_DOCUMENT_TYPE_NODE:
1654 case XML_DOCUMENT_FRAG_NODE:
1655 case XML_NOTATION_NODE:
1656 case XML_DTD_NODE:
1657 case XML_ELEMENT_DECL:
1658 case XML_ATTRIBUTE_DECL:
1659 case XML_ENTITY_DECL:
1660 case XML_XINCLUDE_START:
1661 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001662 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1663 XML_XINCLUDE_XPTR_RESULT,
1664 "XPointer selects unexpected nodes: #%s\n",
1665 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001666 set->nodeTab[i] = NULL;
1667 set->nodeTab[i] = NULL;
1668 continue; /* for */
1669 }
1670 }
1671 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001672 if (doc == NULL) {
1673 ctxt->incTab[nr]->xptr = xptr;
1674 ctxt->incTab[nr]->inc = NULL;
1675 } else {
1676 ctxt->incTab[nr]->inc =
1677 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1678 xmlXPathFreeObject(xptr);
1679 }
Owen Taylor3473f882001-02-23 17:55:21 +00001680 xmlXPathFreeContext(xptrctxt);
1681 xmlFree(fragment);
1682 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001683#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001684
1685 /*
1686 * Do the xml:base fixup if needed
1687 */
Daniel Veillard54bd29b2008-08-26 07:26:55 +00001688 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/')) &&
1689 (!(ctxt->parseFlags & XML_PARSE_NOBASEFIX)) &&
1690 (!(doc->parseFlags & XML_PARSE_NOBASEFIX))) {
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001691 xmlNodePtr node;
William M. Brack0b13a092005-06-01 03:37:59 +00001692 xmlChar *base;
William M. Brackabf598b2004-06-08 02:01:28 +00001693 xmlChar *curBase;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001694
William M. Brackf7789b12004-06-07 08:57:27 +00001695 /*
William M. Brack0b13a092005-06-01 03:37:59 +00001696 * The base is only adjusted if "necessary", i.e. if the xinclude node
1697 * has a base specified, or the URL is relative
William M. Brackf7789b12004-06-07 08:57:27 +00001698 */
William M. Brack0b13a092005-06-01 03:37:59 +00001699 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1700 XML_XML_NAMESPACE);
1701 if (base == NULL) {
1702 /*
1703 * No xml:base on the xinclude node, so we check whether the
1704 * URI base is different than (relative to) the context base
1705 */
1706 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1707 if (curBase == NULL) { /* Error return */
1708 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
William M. Brackf7789b12004-06-07 08:57:27 +00001709 XML_XINCLUDE_HREF_URI,
1710 "trying to build relative URI from %s\n", URL);
William M. Brack0b13a092005-06-01 03:37:59 +00001711 } else {
1712 /* If the URI doesn't contain a slash, it's not relative */
1713 if (!xmlStrchr(curBase, (xmlChar) '/'))
1714 xmlFree(curBase);
1715 else
1716 base = curBase;
William M. Brackf7789b12004-06-07 08:57:27 +00001717 }
William M. Brack0b13a092005-06-01 03:37:59 +00001718 }
1719 if (base != NULL) { /* Adjustment may be needed */
1720 node = ctxt->incTab[nr]->inc;
1721 while (node != NULL) {
1722 /* Only work on element nodes */
1723 if (node->type == XML_ELEMENT_NODE) {
1724 curBase = xmlNodeGetBase(node->doc, node);
1725 /* If no current base, set it */
1726 if (curBase == NULL) {
1727 xmlNodeSetBase(node, base);
1728 } else {
1729 /*
1730 * If the current base is the same as the
1731 * URL of the document, then reset it to be
1732 * the specified xml:base or the relative URI
1733 */
1734 if (xmlStrEqual(curBase, node->doc->URL)) {
1735 xmlNodeSetBase(node, base);
1736 } else {
1737 /*
1738 * If the element already has an xml:base
1739 * set, then relativise it if necessary
1740 */
1741 xmlChar *xmlBase;
1742 xmlBase = xmlGetNsProp(node,
1743 BAD_CAST "base",
1744 XML_XML_NAMESPACE);
1745 if (xmlBase != NULL) {
1746 xmlChar *relBase;
1747 relBase = xmlBuildURI(xmlBase, base);
1748 if (relBase == NULL) { /* error */
1749 xmlXIncludeErr(ctxt,
1750 ctxt->incTab[nr]->ref,
1751 XML_XINCLUDE_HREF_URI,
1752 "trying to rebuild base from %s\n",
1753 xmlBase);
1754 } else {
1755 xmlNodeSetBase(node, relBase);
1756 xmlFree(relBase);
1757 }
1758 xmlFree(xmlBase);
1759 }
1760 }
1761 xmlFree(curBase);
1762 }
1763 }
1764 node = node->next;
1765 }
1766 xmlFree(base);
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001767 }
1768 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001769 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1770 (ctxt->incTab[nr]->count <= 1)) {
1771#ifdef DEBUG_XINCLUDE
1772 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1773#endif
1774 xmlFreeDoc(ctxt->incTab[nr]->doc);
1775 ctxt->incTab[nr]->doc = NULL;
1776 }
Owen Taylor3473f882001-02-23 17:55:21 +00001777 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001778 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001779}
1780
1781/**
1782 * xmlXIncludeLoadTxt:
1783 * @ctxt: the XInclude context
1784 * @url: the associated URL
1785 * @nr: the xinclude node number
1786 *
1787 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001788 *
1789 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001790 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001791static int
Owen Taylor3473f882001-02-23 17:55:21 +00001792xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1793 xmlParserInputBufferPtr buf;
1794 xmlNodePtr node;
1795 xmlURIPtr uri;
1796 xmlChar *URL;
1797 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001798 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001799 xmlCharEncoding enc = (xmlCharEncoding) 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001800
Owen Taylor3473f882001-02-23 17:55:21 +00001801 /*
1802 * Check the URL and remove any fragment identifier
1803 */
1804 uri = xmlParseURI((const char *)url);
1805 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001806 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1807 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001808 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001809 }
1810 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001811 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1812 "fragment identifier forbidden for text: %s\n",
1813 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001814 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001815 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001816 }
1817 URL = xmlSaveUri(uri);
1818 xmlFreeURI(uri);
1819 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001820 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1821 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001822 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001823 }
1824
1825 /*
1826 * Handling of references to the local document are done
1827 * directly through ctxt->doc.
1828 */
1829 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001830 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1831 XML_XINCLUDE_TEXT_DOCUMENT,
1832 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001833 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001834 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001835 }
1836
1837 /*
1838 * Prevent reloading twice the document.
1839 */
1840 for (i = 0; i < ctxt->txtNr; i++) {
1841 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1842 node = xmlCopyNode(ctxt->txtTab[i], 1);
1843 goto loaded;
1844 }
1845 }
1846 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001847 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001848 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001849 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1850 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1851 }
1852 if (encoding != NULL) {
1853 /*
1854 * TODO: we should not have to remap to the xmlCharEncoding
1855 * predefined set, a better interface than
1856 * xmlParserInputBufferCreateFilename should allow any
1857 * encoding supported by iconv
1858 */
1859 enc = xmlParseCharEncoding((const char *) encoding);
1860 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001861 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1862 XML_XINCLUDE_UNKNOWN_ENCODING,
1863 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001864 xmlFree(encoding);
1865 xmlFree(URL);
1866 return(-1);
1867 }
1868 xmlFree(encoding);
1869 }
1870
1871 /*
1872 * Load it.
1873 */
1874 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001875 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001876 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001877 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001878 }
1879 node = xmlNewText(NULL);
1880
1881 /*
1882 * Scan all chars from the resource and add the to the node
1883 */
1884 while (xmlParserInputBufferRead(buf, 128) > 0) {
1885 int len;
1886 const xmlChar *content;
1887
1888 content = xmlBufferContent(buf->buffer);
1889 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001890 for (i = 0;i < len;) {
1891 int cur;
1892 int l;
1893
1894 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1895 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001896 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1897 XML_XINCLUDE_INVALID_CHAR,
1898 "%s contains invalid char\n", URL);
William M. Brack53ce98c2007-02-13 00:37:20 +00001899 xmlFreeParserInputBuffer(buf);
1900 xmlFree(URL);
1901 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001902 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001903 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001904 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001905 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001906 }
1907 xmlBufferShrink(buf->buffer, len);
1908 }
1909 xmlFreeParserInputBuffer(buf);
1910 xmlXIncludeAddTxt(ctxt, node, URL);
1911
1912loaded:
1913 /*
1914 * Add the element as the replacement copy.
1915 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001916 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001917 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001918 return(0);
1919}
1920
1921/**
1922 * xmlXIncludeLoadFallback:
1923 * @ctxt: the XInclude context
1924 * @fallback: the fallback node
1925 * @nr: the xinclude node number
1926 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001927 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001928 * in the XInclude context
1929 *
1930 * Returns 0 in case of success, -1 in case of failure
1931 */
1932static int
1933xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001934 xmlXIncludeCtxtPtr newctxt;
1935 int ret = 0;
1936
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001937 if ((fallback == NULL) || (ctxt == NULL))
1938 return(-1);
William M. Brackef245fd2004-02-06 09:33:59 +00001939 if (fallback->children != NULL) {
1940 /*
1941 * It's possible that the fallback also has 'includes'
1942 * (Bug 129969), so we re-process the fallback just in case
1943 */
1944 newctxt = xmlXIncludeNewContext(ctxt->doc);
1945 if (newctxt == NULL)
1946 return (-1);
Daniel Veillarda6585822006-12-04 09:21:28 +00001947 newctxt->_private = ctxt->_private;
Daniel Veillardce244ad2004-11-05 10:03:46 +00001948 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
William M. Brackef245fd2004-02-06 09:33:59 +00001949 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1950 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
William M. Brack87640d52004-04-17 14:58:15 +00001951 if (ctxt->nbErrors > 0)
William M. Brackef245fd2004-02-06 09:33:59 +00001952 ret = -1;
William M. Brack87640d52004-04-17 14:58:15 +00001953 else if (ret > 0)
1954 ret = 0; /* xmlXIncludeDoProcess can return +ve number */
William M. Brackef245fd2004-02-06 09:33:59 +00001955 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001956
Daniel Veillard03a53c32004-10-26 16:06:51 +00001957 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1958 fallback->children);
William M. Brackef245fd2004-02-06 09:33:59 +00001959 } else {
1960 ctxt->incTab[nr]->inc = NULL;
William M. Brack95af5942004-02-08 04:12:49 +00001961 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
William M. Brackef245fd2004-02-06 09:33:59 +00001962 }
William M. Brackaae10522004-01-02 14:59:41 +00001963 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001964}
1965
1966/************************************************************************
1967 * *
1968 * XInclude Processing *
1969 * *
1970 ************************************************************************/
1971
1972/**
1973 * xmlXIncludePreProcessNode:
1974 * @ctxt: an XInclude context
1975 * @node: an XInclude node
1976 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001977 * Implement the XInclude preprocessing, currently just adding the element
1978 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001979 *
1980 * Returns the result list or NULL in case of error
1981 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001982static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001983xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1984 xmlXIncludeAddNode(ctxt, node);
Daniel Veillard24505b02005-07-28 23:49:35 +00001985 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001986}
1987
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001988/**
Owen Taylor3473f882001-02-23 17:55:21 +00001989 * xmlXIncludeLoadNode:
1990 * @ctxt: an XInclude context
1991 * @nr: the node number
1992 *
1993 * Find and load the infoset replacement for the given node.
1994 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001995 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001996 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001997static int
Owen Taylor3473f882001-02-23 17:55:21 +00001998xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1999 xmlNodePtr cur;
2000 xmlChar *href;
2001 xmlChar *parse;
2002 xmlChar *base;
William M. Brackf7789b12004-06-07 08:57:27 +00002003 xmlChar *oldBase;
Owen Taylor3473f882001-02-23 17:55:21 +00002004 xmlChar *URI;
2005 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002006 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002007
2008 if (ctxt == NULL)
2009 return(-1);
2010 if ((nr < 0) || (nr >= ctxt->incNr))
2011 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002012 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00002013 if (cur == NULL)
2014 return(-1);
2015
Owen Taylor3473f882001-02-23 17:55:21 +00002016 /*
2017 * read the attributes
2018 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002019 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00002020 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00002021 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
2022 if (href == NULL)
2023 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002024 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002025 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00002026 if (parse != NULL) {
2027 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
2028 xml = 1;
2029 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2030 xml = 0;
2031 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002032 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2033 XML_XINCLUDE_PARSE_VALUE,
2034 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00002035 if (href != NULL)
2036 xmlFree(href);
2037 if (parse != NULL)
2038 xmlFree(parse);
2039 return(-1);
2040 }
2041 }
2042
2043 /*
2044 * compute the URI
2045 */
2046 base = xmlNodeGetBase(ctxt->doc, cur);
2047 if (base == NULL) {
2048 URI = xmlBuildURI(href, ctxt->doc->URL);
2049 } else {
2050 URI = xmlBuildURI(href, base);
2051 }
2052 if (URI == NULL) {
2053 xmlChar *escbase;
2054 xmlChar *eschref;
2055 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002056 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00002057 */
2058 escbase = xmlURIEscape(base);
2059 eschref = xmlURIEscape(href);
2060 URI = xmlBuildURI(eschref, escbase);
2061 if (escbase != NULL)
2062 xmlFree(escbase);
2063 if (eschref != NULL)
2064 xmlFree(eschref);
2065 }
2066 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002067 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2068 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002069 if (parse != NULL)
2070 xmlFree(parse);
2071 if (href != NULL)
2072 xmlFree(href);
2073 if (base != NULL)
2074 xmlFree(base);
2075 return(-1);
2076 }
2077#ifdef DEBUG_XINCLUDE
2078 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2079 xml ? "xml": "text");
2080 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2081#endif
2082
2083 /*
William M. Brackf7789b12004-06-07 08:57:27 +00002084 * Save the base for this include (saving the current one)
Owen Taylor3473f882001-02-23 17:55:21 +00002085 */
William M. Brackf7789b12004-06-07 08:57:27 +00002086 oldBase = ctxt->base;
2087 ctxt->base = base;
2088
Owen Taylor3473f882001-02-23 17:55:21 +00002089 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002090 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00002091 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2092 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002093 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2094 }
William M. Brackf7789b12004-06-07 08:57:27 +00002095
2096 /*
2097 * Restore the original base before checking for fallback
2098 */
2099 ctxt->base = oldBase;
2100
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002101 if (ret < 0) {
2102 xmlNodePtr children;
2103
2104 /*
2105 * Time to try a fallback if availble
2106 */
2107#ifdef DEBUG_XINCLUDE
2108 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2109#endif
2110 children = cur->children;
2111 while (children != NULL) {
2112 if ((children->type == XML_ELEMENT_NODE) &&
2113 (children->ns != NULL) &&
2114 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002115 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2116 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002117 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00002118 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002119 break;
2120 }
2121 children = children->next;
2122 }
2123 }
2124 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002125 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2126 XML_XINCLUDE_NO_FALLBACK,
2127 "could not load %s, and no fallback was found\n",
2128 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002129 }
2130
2131 /*
2132 * Cleanup
2133 */
2134 if (URI != NULL)
2135 xmlFree(URI);
2136 if (parse != NULL)
2137 xmlFree(parse);
2138 if (href != NULL)
2139 xmlFree(href);
2140 if (base != NULL)
2141 xmlFree(base);
2142 return(0);
2143}
2144
2145/**
2146 * xmlXIncludeIncludeNode:
2147 * @ctxt: an XInclude context
2148 * @nr: the node number
2149 *
2150 * Inplement the infoset replacement for the given node
2151 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002152 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002153 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002154static int
Owen Taylor3473f882001-02-23 17:55:21 +00002155xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002156 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002157
2158 if (ctxt == NULL)
2159 return(-1);
2160 if ((nr < 0) || (nr >= ctxt->incNr))
2161 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002162 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00002163 if (cur == NULL)
2164 return(-1);
2165
2166 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002167 * If we stored an XPointer a late computation may be needed
2168 */
2169 if ((ctxt->incTab[nr]->inc == NULL) &&
2170 (ctxt->incTab[nr]->xptr != NULL)) {
2171 ctxt->incTab[nr]->inc =
2172 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
2173 ctxt->incTab[nr]->xptr);
2174 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2175 ctxt->incTab[nr]->xptr = NULL;
2176 }
2177 list = ctxt->incTab[nr]->inc;
2178 ctxt->incTab[nr]->inc = NULL;
2179
2180 /*
2181 * Check against the risk of generating a multi-rooted document
2182 */
2183 if ((cur->parent != NULL) &&
2184 (cur->parent->type != XML_ELEMENT_NODE)) {
2185 int nb_elem = 0;
2186
2187 tmp = list;
2188 while (tmp != NULL) {
2189 if (tmp->type == XML_ELEMENT_NODE)
2190 nb_elem++;
2191 tmp = tmp->next;
2192 }
2193 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002194 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2195 XML_XINCLUDE_MULTIPLE_ROOT,
2196 "XInclude error: would result in multiple root nodes\n",
2197 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002198 return(-1);
2199 }
2200 }
2201
Daniel Veillardc14c3892004-08-16 12:34:50 +00002202 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2203 /*
2204 * Add the list of nodes
2205 */
2206 while (list != NULL) {
2207 end = list;
2208 list = list->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002209
Daniel Veillardc14c3892004-08-16 12:34:50 +00002210 xmlAddPrevSibling(cur, end);
2211 }
2212 xmlUnlinkNode(cur);
2213 xmlFreeNode(cur);
2214 } else {
2215 /*
2216 * Change the current node as an XInclude start one, and add an
2217 * XInclude end one
2218 */
2219 cur->type = XML_XINCLUDE_START;
Daniel Veillard03a53c32004-10-26 16:06:51 +00002220 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
Daniel Veillardc14c3892004-08-16 12:34:50 +00002221 if (end == NULL) {
2222 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2223 XML_XINCLUDE_BUILD_FAILED,
2224 "failed to build node\n", NULL);
2225 return(-1);
2226 }
2227 end->type = XML_XINCLUDE_END;
2228 xmlAddNextSibling(cur, end);
Owen Taylor3473f882001-02-23 17:55:21 +00002229
Daniel Veillardc14c3892004-08-16 12:34:50 +00002230 /*
2231 * Add the list of nodes
2232 */
2233 while (list != NULL) {
2234 cur = list;
2235 list = list->next;
2236
2237 xmlAddPrevSibling(end, cur);
2238 }
Owen Taylor3473f882001-02-23 17:55:21 +00002239 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002240
2241
Owen Taylor3473f882001-02-23 17:55:21 +00002242 return(0);
2243}
2244
2245/**
2246 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002247 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002248 * @node: an XInclude node
2249 *
2250 * test if the node is an XInclude node
2251 *
2252 * Returns 1 true, 0 otherwise
2253 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002254static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002255xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002256 if (node == NULL)
2257 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002258 if (node->type != XML_ELEMENT_NODE)
2259 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002260 if (node->ns == NULL)
2261 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002262 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2263 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2264 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2265 if (ctxt->legacy == 0) {
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002266#if 0 /* wait for the XML Core Working Group to get something stable ! */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002267 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2268 "Deprecated XInclude namespace found, use %s",
2269 XINCLUDE_NS);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002270#endif
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002271 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002272 }
2273 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002274 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2275 xmlNodePtr child = node->children;
2276 int nb_fallback = 0;
2277
2278 while (child != NULL) {
2279 if ((child->type == XML_ELEMENT_NODE) &&
2280 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002281 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2282 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002283 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002284 xmlXIncludeErr(ctxt, node,
2285 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2286 "%s has an 'include' child\n",
2287 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002288 return(0);
2289 }
2290 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2291 nb_fallback++;
2292 }
2293 }
2294 child = child->next;
2295 }
2296 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002297 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2298 "%s has multiple fallback children\n",
2299 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002300 return(0);
2301 }
2302 return(1);
2303 }
2304 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2305 if ((node->parent == NULL) ||
2306 (node->parent->type != XML_ELEMENT_NODE) ||
2307 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002308 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2309 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002310 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002311 xmlXIncludeErr(ctxt, node,
2312 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2313 "%s is not the child of an 'include'\n",
2314 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002315 }
2316 }
2317 }
Owen Taylor3473f882001-02-23 17:55:21 +00002318 return(0);
2319}
2320
2321/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002322 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002323 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002324 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002325 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002326 *
2327 * Implement the XInclude substitution on the XML document @doc
2328 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002329 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002330 * or the number of substitutions done.
2331 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002332static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002333xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002334 xmlNodePtr cur;
2335 int ret = 0;
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002336 int i, start;
Owen Taylor3473f882001-02-23 17:55:21 +00002337
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002338 if ((doc == NULL) || (tree == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002339 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002340 if (ctxt == NULL)
2341 return(-1);
2342
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002343 if (doc->URL != NULL) {
2344 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2345 if (ret < 0)
2346 return(-1);
2347 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00002348 start = ctxt->incNr;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002349
Owen Taylor3473f882001-02-23 17:55:21 +00002350 /*
2351 * First phase: lookup the elements in the document
2352 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002353 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002354 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002355 xmlXIncludePreProcessNode(ctxt, cur);
William M. Brack7b0e2762004-05-12 09:33:23 +00002356 while ((cur != NULL) && (cur != tree->parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002357 /* TODO: need to work on entities -> stack */
2358 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002359 (cur->children->type != XML_ENTITY_DECL) &&
2360 (cur->children->type != XML_XINCLUDE_START) &&
2361 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002362 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002363 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002364 xmlXIncludePreProcessNode(ctxt, cur);
2365 } else if (cur->next != NULL) {
2366 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002367 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002368 xmlXIncludePreProcessNode(ctxt, cur);
2369 } else {
William M. Brack5d8d10b2004-04-16 08:11:26 +00002370 if (cur == tree)
2371 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002372 do {
2373 cur = cur->parent;
William M. Brack7b0e2762004-05-12 09:33:23 +00002374 if ((cur == NULL) || (cur == tree->parent))
2375 break; /* do */
Owen Taylor3473f882001-02-23 17:55:21 +00002376 if (cur->next != NULL) {
2377 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002378 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002379 xmlXIncludePreProcessNode(ctxt, cur);
2380 break; /* do */
2381 }
2382 } while (cur != NULL);
2383 }
2384 }
2385
2386 /*
2387 * Second Phase : collect the infosets fragments
2388 */
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002389 for (i = start;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002390 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002391 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002392 }
2393
2394 /*
2395 * Third phase: extend the original document infoset.
William M. Brack6b1a28d2004-02-06 11:24:44 +00002396 *
2397 * Originally we bypassed the inclusion if there were any errors
2398 * encountered on any of the XIncludes. A bug was raised (bug
2399 * 132588) requesting that we output the XIncludes without error,
2400 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2401 * give some other problems in the future, but for now it seems to
2402 * work ok.
2403 *
Owen Taylor3473f882001-02-23 17:55:21 +00002404 */
William M. Brack6b1a28d2004-02-06 11:24:44 +00002405 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
William M. Brack95af5942004-02-08 04:12:49 +00002406 if ((ctxt->incTab[i]->inc != NULL) ||
2407 (ctxt->incTab[i]->xptr != NULL) ||
2408 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002409 xmlXIncludeIncludeNode(ctxt, i);
Owen Taylor3473f882001-02-23 17:55:21 +00002410 }
2411
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002412 if (doc->URL != NULL)
2413 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002414 return(ret);
2415}
2416
2417/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002418 * xmlXIncludeSetFlags:
2419 * @ctxt: an XInclude processing context
2420 * @flags: a set of xmlParserOption used for parsing XML includes
2421 *
2422 * Set the flags used for further processing of XML resources.
2423 *
2424 * Returns 0 in case of success and -1 in case of error.
2425 */
2426int
2427xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2428 if (ctxt == NULL)
2429 return(-1);
2430 ctxt->parseFlags = flags;
2431 return(0);
2432}
Stefan Behnelb9590e92009-08-24 19:45:54 +02002433
2434/**
2435 * xmlXIncludeProcessTreeFlagsData:
2436 * @tree: an XML node
2437 * @flags: a set of xmlParserOption used for parsing XML includes
2438 * @data: application data that will be passed to the parser context
2439 * in the _private field of the parser context(s)
2440 *
2441 * Implement the XInclude substitution on the XML node @tree
2442 *
2443 * Returns 0 if no substitution were done, -1 if some processing failed
2444 * or the number of substitutions done.
2445 */
2446
2447int
2448xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
2449 xmlXIncludeCtxtPtr ctxt;
2450 int ret = 0;
2451
2452 if ((tree == NULL) || (tree->doc == NULL))
2453 return(-1);
2454
2455 ctxt = xmlXIncludeNewContext(tree->doc);
2456 if (ctxt == NULL)
2457 return(-1);
2458 ctxt->_private = data;
2459 ctxt->base = xmlStrdup((xmlChar *)tree->doc->URL);
2460 xmlXIncludeSetFlags(ctxt, flags);
2461 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2462 if ((ret >= 0) && (ctxt->nbErrors > 0))
2463 ret = -1;
2464
2465 xmlXIncludeFreeContext(ctxt);
2466 return(ret);
2467}
2468
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002469/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002470 * xmlXIncludeProcessFlagsData:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002471 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002472 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillard681e9042006-09-29 09:16:00 +00002473 * @data: application data that will be passed to the parser context
2474 * in the _private field of the parser context(s)
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002475 *
2476 * Implement the XInclude substitution on the XML document @doc
2477 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002478 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002479 * or the number of substitutions done.
2480 */
2481int
Daniel Veillard681e9042006-09-29 09:16:00 +00002482xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002483 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002484 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002485
2486 if (doc == NULL)
2487 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002488 tree = xmlDocGetRootElement(doc);
2489 if (tree == NULL)
2490 return(-1);
Stefan Behnelb9590e92009-08-24 19:45:54 +02002491 return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002492}
2493
2494/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002495 * xmlXIncludeProcessFlags:
2496 * @doc: an XML document
2497 * @flags: a set of xmlParserOption used for parsing XML includes
2498 *
2499 * Implement the XInclude substitution on the XML document @doc
2500 *
2501 * Returns 0 if no substitution were done, -1 if some processing failed
2502 * or the number of substitutions done.
2503 */
2504int
2505xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2506 return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2507}
2508
2509/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002510 * xmlXIncludeProcess:
2511 * @doc: an XML document
2512 *
2513 * Implement the XInclude substitution on the XML document @doc
2514 *
2515 * Returns 0 if no substitution were done, -1 if some processing failed
2516 * or the number of substitutions done.
2517 */
2518int
2519xmlXIncludeProcess(xmlDocPtr doc) {
2520 return(xmlXIncludeProcessFlags(doc, 0));
2521}
2522
2523/**
2524 * xmlXIncludeProcessTreeFlags:
2525 * @tree: a node in an XML document
2526 * @flags: a set of xmlParserOption used for parsing XML includes
2527 *
2528 * Implement the XInclude substitution for the given subtree
2529 *
2530 * Returns 0 if no substitution were done, -1 if some processing failed
2531 * or the number of substitutions done.
2532 */
2533int
2534xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2535 xmlXIncludeCtxtPtr ctxt;
2536 int ret = 0;
2537
2538 if ((tree == NULL) || (tree->doc == NULL))
2539 return(-1);
2540 ctxt = xmlXIncludeNewContext(tree->doc);
2541 if (ctxt == NULL)
2542 return(-1);
William M. Brackf7789b12004-06-07 08:57:27 +00002543 ctxt->base = xmlNodeGetBase(tree->doc, tree);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002544 xmlXIncludeSetFlags(ctxt, flags);
2545 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2546 if ((ret >= 0) && (ctxt->nbErrors > 0))
2547 ret = -1;
2548
2549 xmlXIncludeFreeContext(ctxt);
2550 return(ret);
2551}
2552
2553/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002554 * xmlXIncludeProcessTree:
2555 * @tree: a node in an XML document
2556 *
2557 * Implement the XInclude substitution for the given subtree
2558 *
2559 * Returns 0 if no substitution were done, -1 if some processing failed
2560 * or the number of substitutions done.
2561 */
2562int
2563xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002564 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002565}
2566
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002567/**
2568 * xmlXIncludeProcessNode:
2569 * @ctxt: an existing XInclude context
2570 * @node: a node in an XML document
2571 *
2572 * Implement the XInclude substitution for the given subtree reusing
2573 * the informations and data coming from the given context.
2574 *
2575 * Returns 0 if no substitution were done, -1 if some processing failed
2576 * or the number of substitutions done.
2577 */
2578int
2579xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2580 int ret = 0;
2581
2582 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2583 return(-1);
2584 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2585 if ((ret >= 0) && (ctxt->nbErrors > 0))
2586 ret = -1;
2587 return(ret);
2588}
2589
Owen Taylor3473f882001-02-23 17:55:21 +00002590#else /* !LIBXML_XINCLUDE_ENABLED */
2591#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002592#define bottom_xinclude
2593#include "elfgcchack.h"