blob: ab6409688d0ad9a00b7244c6469f8beff8754870 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xinclude.c : Code to implement XInclude processing
3 *
Daniel Veillarde74d2e12003-12-09 11:35:37 +00004 * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
5 * http://www.w3.org/TR/2003/WD-xinclude-20031110
Owen Taylor3473f882001-02-23 17:55:21 +00006 *
7 * See Copyright for the status of this software.
8 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00009 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000010 */
11
Daniel Veillard34ce8be2002-03-18 19:37:11 +000012#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000013#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000014
Owen Taylor3473f882001-02-23 17:55:21 +000015#include <string.h>
16#include <libxml/xmlmemory.h>
17#include <libxml/tree.h>
18#include <libxml/parser.h>
19#include <libxml/uri.h>
20#include <libxml/xpointer.h>
21#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000022#include <libxml/xmlerror.h>
Daniel Veillardd076a202002-11-20 13:28:31 +000023#include <libxml/encoding.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000024#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000025
26#ifdef LIBXML_XINCLUDE_ENABLED
27#include <libxml/xinclude.h>
28
Owen Taylor3473f882001-02-23 17:55:21 +000029
Daniel Veillardf4b4f982003-02-13 11:02:08 +000030#define XINCLUDE_MAX_DEPTH 40
31
Daniel Veillard98485322003-08-14 15:44:40 +000032/* #define DEBUG_XINCLUDE */
Daniel Veillard017b1082001-06-21 11:20:21 +000033#ifdef DEBUG_XINCLUDE
34#ifdef LIBXML_DEBUG_ENABLED
35#include <libxml/debugXML.h>
36#endif
37#endif
Owen Taylor3473f882001-02-23 17:55:21 +000038
39/************************************************************************
40 * *
William M. Brack72ee48d2003-12-30 08:30:19 +000041 * XInclude context handling *
Owen Taylor3473f882001-02-23 17:55:21 +000042 * *
43 ************************************************************************/
44
45/*
46 * An XInclude context
47 */
Daniel Veillardedac3c92001-02-26 01:36:19 +000048typedef xmlChar *xmlURL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +000049
50typedef struct _xmlXIncludeRef xmlXIncludeRef;
51typedef xmlXIncludeRef *xmlXIncludeRefPtr;
52struct _xmlXIncludeRef {
William M. Brack72ee48d2003-12-30 08:30:19 +000053 xmlChar *URI; /* the fully resolved resource URL */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000054 xmlChar *fragment; /* the fragment in the URI */
55 xmlDocPtr doc; /* the parsed document */
56 xmlNodePtr ref; /* the node making the reference in the source */
57 xmlNodePtr inc; /* the included copy */
58 int xml; /* xml or txt */
59 int count; /* how many refs use that specific doc */
Daniel Veillardf4b4f982003-02-13 11:02:08 +000060 xmlXPathObjectPtr xptr; /* the xpointer if needed */
William M. Brack95af5942004-02-08 04:12:49 +000061 int emptyFb; /* flag to show fallback empty */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000062};
63
Owen Taylor3473f882001-02-23 17:55:21 +000064struct _xmlXIncludeCtxt {
65 xmlDocPtr doc; /* the source document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000066 int incBase; /* the first include for this document */
Owen Taylor3473f882001-02-23 17:55:21 +000067 int incNr; /* number of includes */
68 int incMax; /* size of includes tab */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000069 xmlXIncludeRefPtr *incTab; /* array of included references */
70
Owen Taylor3473f882001-02-23 17:55:21 +000071 int txtNr; /* number of unparsed documents */
72 int txtMax; /* size of unparsed documents tab */
73 xmlNodePtr *txtTab; /* array of unparsed text nodes */
William M. Brack72ee48d2003-12-30 08:30:19 +000074 xmlURL *txturlTab; /* array of unparsed text URLs */
Daniel Veillardd581b7e2003-02-11 18:03:05 +000075
Daniel Veillardf4b4f982003-02-13 11:02:08 +000076 xmlChar * url; /* the current URL processed */
William M. Brack72ee48d2003-12-30 08:30:19 +000077 int urlNr; /* number of URLs stacked */
78 int urlMax; /* size of URL stack */
79 xmlChar * *urlTab; /* URL stack */
Daniel Veillardf4b4f982003-02-13 11:02:08 +000080
Daniel Veillardd581b7e2003-02-11 18:03:05 +000081 int nbErrors; /* the number of errors detected */
Daniel Veillardb5fa0202003-12-08 17:41:29 +000082 int legacy; /* using XINCLUDE_OLD_NS */
Daniel Veillarde74d2e12003-12-09 11:35:37 +000083 int parseFlags; /* the flags used for parsing XML documents */
William M. Brackf7789b12004-06-07 08:57:27 +000084 xmlChar * base; /* the current xml:base */
Owen Taylor3473f882001-02-23 17:55:21 +000085};
86
Daniel Veillardd16df9f2001-05-23 13:44:21 +000087static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +000088xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree);
Owen Taylor3473f882001-02-23 17:55:21 +000089
Daniel Veillard98485322003-08-14 15:44:40 +000090
Daniel Veillardcd6ff282003-10-08 22:38:13 +000091/************************************************************************
92 * *
Daniel Veillard69d2c172003-10-09 11:46:07 +000093 * XInclude error handler *
Daniel Veillardcd6ff282003-10-08 22:38:13 +000094 * *
95 ************************************************************************/
96
Daniel Veillard98485322003-08-14 15:44:40 +000097/**
Daniel Veillardcd6ff282003-10-08 22:38:13 +000098 * xmlXIncludeErrMemory:
William M. Brack72ee48d2003-12-30 08:30:19 +000099 * @extra: extra information
Daniel Veillard98485322003-08-14 15:44:40 +0000100 *
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000101 * Handle an out of memory condition
Daniel Veillard98485322003-08-14 15:44:40 +0000102 */
103static void
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000104xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
105 const char *extra)
Daniel Veillard98485322003-08-14 15:44:40 +0000106{
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000107 if (ctxt != NULL)
108 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000109 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000110 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
111 extra, NULL, NULL, 0, 0,
112 "Memory allocation failed : %s\n", extra);
113}
Daniel Veillard98485322003-08-14 15:44:40 +0000114
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000115/**
116 * xmlXIncludeErr:
117 * @ctxt: the XInclude context
118 * @node: the context node
119 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000120 * @extra: extra information
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000121 *
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000122 * Handle an XInclude error
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000123 */
124static void
125xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
126 const char *msg, const xmlChar *extra)
127{
128 if (ctxt != NULL)
129 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000130 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000131 error, XML_ERR_ERROR, NULL, 0,
132 (const char *) extra, NULL, NULL, 0, 0,
133 msg, (const char *) extra);
Daniel Veillard98485322003-08-14 15:44:40 +0000134}
135
Daniel Veillardf54cd532004-02-25 11:52:31 +0000136#if 0
Owen Taylor3473f882001-02-23 17:55:21 +0000137/**
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000138 * xmlXIncludeWarn:
139 * @ctxt: the XInclude context
140 * @node: the context node
141 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000142 * @extra: extra information
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000143 *
144 * Emit an XInclude warning.
145 */
146static void
147xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
148 const char *msg, const xmlChar *extra)
149{
150 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
151 error, XML_ERR_WARNING, NULL, 0,
152 (const char *) extra, NULL, NULL, 0, 0,
153 msg, (const char *) extra);
154}
Daniel Veillardf54cd532004-02-25 11:52:31 +0000155#endif
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000156
157/**
158 * xmlXIncludeGetProp:
159 * @ctxt: the XInclude context
160 * @cur: the node
161 * @name: the attribute name
162 *
163 * Get an XInclude attribute
164 *
165 * Returns the value (to be freed) or NULL if not found
166 */
167static xmlChar *
168xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
169 const xmlChar *name) {
170 xmlChar *ret;
171
172 ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
173 if (ret != NULL)
174 return(ret);
175 if (ctxt->legacy != 0) {
176 ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
177 if (ret != NULL)
178 return(ret);
179 }
180 ret = xmlGetProp(cur, name);
181 return(ret);
182}
183/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000184 * xmlXIncludeFreeRef:
185 * @ref: the XInclude reference
186 *
187 * Free an XInclude reference
188 */
189static void
190xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
191 if (ref == NULL)
192 return;
193#ifdef DEBUG_XINCLUDE
194 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
195#endif
196 if (ref->doc != NULL) {
197#ifdef DEBUG_XINCLUDE
198 xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
199#endif
200 xmlFreeDoc(ref->doc);
201 }
202 if (ref->URI != NULL)
203 xmlFree(ref->URI);
204 if (ref->fragment != NULL)
205 xmlFree(ref->fragment);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000206 if (ref->xptr != NULL)
207 xmlXPathFreeObject(ref->xptr);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000208 xmlFree(ref);
209}
210
211/**
212 * xmlXIncludeNewRef:
213 * @ctxt: the XInclude context
214 * @URI: the resource URI
215 *
216 * Creates a new reference within an XInclude context
217 *
218 * Returns the new set
219 */
220static xmlXIncludeRefPtr
221xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
222 xmlNodePtr ref) {
223 xmlXIncludeRefPtr ret;
224
225#ifdef DEBUG_XINCLUDE
226 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
227#endif
228 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000229 if (ret == NULL) {
230 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000231 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000232 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000233 memset(ret, 0, sizeof(xmlXIncludeRef));
234 if (URI == NULL)
235 ret->URI = NULL;
236 else
237 ret->URI = xmlStrdup(URI);
238 ret->fragment = NULL;
239 ret->ref = ref;
240 ret->doc = 0;
241 ret->count = 0;
242 ret->xml = 0;
243 ret->inc = NULL;
244 if (ctxt->incMax == 0) {
245 ctxt->incMax = 4;
246 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
247 sizeof(ctxt->incTab[0]));
248 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000249 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000250 xmlXIncludeFreeRef(ret);
251 return(NULL);
252 }
253 }
254 if (ctxt->incNr >= ctxt->incMax) {
255 ctxt->incMax *= 2;
256 ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
257 ctxt->incMax * sizeof(ctxt->incTab[0]));
258 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000259 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000260 xmlXIncludeFreeRef(ret);
261 return(NULL);
262 }
263 }
264 ctxt->incTab[ctxt->incNr++] = ret;
265 return(ret);
266}
267
268/**
Owen Taylor3473f882001-02-23 17:55:21 +0000269 * xmlXIncludeNewContext:
270 * @doc: an XML Document
271 *
272 * Creates a new XInclude context
273 *
274 * Returns the new set
275 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000276xmlXIncludeCtxtPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000277xmlXIncludeNewContext(xmlDocPtr doc) {
278 xmlXIncludeCtxtPtr ret;
279
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000280#ifdef DEBUG_XINCLUDE
281 xmlGenericError(xmlGenericErrorContext, "New context\n");
282#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000283 if (doc == NULL)
284 return(NULL);
285 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000286 if (ret == NULL) {
287 xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
288 "creating XInclude context");
Owen Taylor3473f882001-02-23 17:55:21 +0000289 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000290 }
Owen Taylor3473f882001-02-23 17:55:21 +0000291 memset(ret, 0, sizeof(xmlXIncludeCtxt));
292 ret->doc = doc;
293 ret->incNr = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000294 ret->incBase = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000295 ret->incMax = 0;
296 ret->incTab = NULL;
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000297 ret->nbErrors = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000298 return(ret);
299}
300
301/**
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000302 * xmlXIncludeURLPush:
303 * @ctxt: the parser context
304 * @value: the url
305 *
306 * Pushes a new url on top of the url stack
307 *
308 * Returns -1 in case of error, the index in the stack otherwise
309 */
310static int
311xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
312 const xmlChar *value)
313{
314 if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000315 xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION,
316 "detected a recursion in %s\n", value);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000317 return(-1);
318 }
319 if (ctxt->urlTab == NULL) {
320 ctxt->urlMax = 4;
321 ctxt->urlNr = 0;
322 ctxt->urlTab = (xmlChar * *) xmlMalloc(
323 ctxt->urlMax * sizeof(ctxt->urlTab[0]));
324 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000325 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000326 return (-1);
327 }
328 }
329 if (ctxt->urlNr >= ctxt->urlMax) {
330 ctxt->urlMax *= 2;
331 ctxt->urlTab =
332 (xmlChar * *) xmlRealloc(ctxt->urlTab,
333 ctxt->urlMax *
334 sizeof(ctxt->urlTab[0]));
335 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000336 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000337 return (-1);
338 }
339 }
340 ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
341 return (ctxt->urlNr++);
342}
343
344/**
345 * xmlXIncludeURLPop:
346 * @ctxt: the parser context
347 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000348 * Pops the top URL from the URL stack
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000349 */
350static void
351xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
352{
353 xmlChar * ret;
354
355 if (ctxt->urlNr <= 0)
356 return;
357 ctxt->urlNr--;
358 if (ctxt->urlNr > 0)
359 ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
360 else
361 ctxt->url = NULL;
362 ret = ctxt->urlTab[ctxt->urlNr];
363 ctxt->urlTab[ctxt->urlNr] = 0;
364 if (ret != NULL)
365 xmlFree(ret);
366}
367
368/**
Owen Taylor3473f882001-02-23 17:55:21 +0000369 * xmlXIncludeFreeContext:
370 * @ctxt: the XInclude context
371 *
372 * Free an XInclude context
373 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000374void
Owen Taylor3473f882001-02-23 17:55:21 +0000375xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
376 int i;
377
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000378#ifdef DEBUG_XINCLUDE
379 xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
380#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000381 if (ctxt == NULL)
382 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000383 while (ctxt->urlNr > 0)
384 xmlXIncludeURLPop(ctxt);
385 if (ctxt->urlTab != NULL)
386 xmlFree(ctxt->urlTab);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000387 for (i = 0;i < ctxt->incNr;i++) {
388 if (ctxt->incTab[i] != NULL)
389 xmlXIncludeFreeRef(ctxt->incTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +0000390 }
391 for (i = 0;i < ctxt->txtNr;i++) {
392 if (ctxt->txturlTab[i] != NULL)
393 xmlFree(ctxt->txturlTab[i]);
394 }
395 if (ctxt->incTab != NULL)
396 xmlFree(ctxt->incTab);
Owen Taylor3473f882001-02-23 17:55:21 +0000397 if (ctxt->txtTab != NULL)
398 xmlFree(ctxt->txtTab);
399 if (ctxt->txturlTab != NULL)
400 xmlFree(ctxt->txturlTab);
Daniel Veillardce244ad2004-11-05 10:03:46 +0000401 if (ctxt->base != NULL) {
402 xmlFree(ctxt->base);
403 }
Owen Taylor3473f882001-02-23 17:55:21 +0000404 xmlFree(ctxt);
405}
406
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000407/**
Daniel Veillard98485322003-08-14 15:44:40 +0000408 * xmlXIncludeParseFile:
409 * @ctxt: the XInclude context
410 * @URL: the URL or file path
411 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000412 * parse a document for XInclude
Daniel Veillard98485322003-08-14 15:44:40 +0000413 */
414static xmlDocPtr
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000415xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
Daniel Veillard98485322003-08-14 15:44:40 +0000416 xmlDocPtr ret;
417 xmlParserCtxtPtr pctxt;
418 char *directory = NULL;
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000419 xmlParserInputPtr inputStream;
Daniel Veillard98485322003-08-14 15:44:40 +0000420
421 xmlInitParser();
422
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000423 pctxt = xmlNewParserCtxt();
Daniel Veillard98485322003-08-14 15:44:40 +0000424 if (pctxt == NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000425 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
Daniel Veillard98485322003-08-14 15:44:40 +0000426 return(NULL);
427 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000428 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000429 * try to ensure that new documents included are actually
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000430 * built with the same dictionary as the including document.
431 */
432 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL) &&
433 (pctxt->dict != NULL)) {
434 xmlDictFree(pctxt->dict);
435 pctxt->dict = ctxt->doc->dict;
436 xmlDictReference(pctxt->dict);
437 }
438
439 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
440
441 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
442 if (inputStream == NULL) {
443 xmlFreeParserCtxt(pctxt);
444 return(NULL);
445 }
446
447 inputPush(pctxt, inputStream);
Daniel Veillard98485322003-08-14 15:44:40 +0000448
449 if ((pctxt->directory == NULL) && (directory == NULL))
450 directory = xmlParserGetDirectory(URL);
451 if ((pctxt->directory == NULL) && (directory != NULL))
452 pctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
453
William M. Bracka22da292005-02-12 01:08:22 +0000454 pctxt->loadsubset |= XML_DETECT_IDS;
Daniel Veillard98485322003-08-14 15:44:40 +0000455
456 xmlParseDocument(pctxt);
457
William M. Brack1ff42132003-12-31 14:05:15 +0000458 if (pctxt->wellFormed) {
Daniel Veillard98485322003-08-14 15:44:40 +0000459 ret = pctxt->myDoc;
William M. Brack1ff42132003-12-31 14:05:15 +0000460 }
Daniel Veillard98485322003-08-14 15:44:40 +0000461 else {
462 ret = NULL;
Daniel Veillard500a1de2004-03-22 15:22:58 +0000463 if (pctxt->myDoc != NULL)
Daniel Veillard4773df22004-01-23 13:15:13 +0000464 xmlFreeDoc(pctxt->myDoc);
Daniel Veillard98485322003-08-14 15:44:40 +0000465 pctxt->myDoc = NULL;
466 }
467 xmlFreeParserCtxt(pctxt);
468
469 return(ret);
470}
471
472/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000473 * xmlXIncludeAddNode:
474 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000475 * @cur: the new node
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000476 *
477 * Add a new node to process to an XInclude context
478 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000479static int
480xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
481 xmlXIncludeRefPtr ref;
482 xmlURIPtr uri;
483 xmlChar *URL;
484 xmlChar *fragment = NULL;
485 xmlChar *href;
486 xmlChar *parse;
487 xmlChar *base;
488 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000489 int xml = 1, i; /* default Issue 64 */
490 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000491
492
493 if (ctxt == NULL)
494 return(-1);
495 if (cur == NULL)
496 return(-1);
497
498#ifdef DEBUG_XINCLUDE
499 xmlGenericError(xmlGenericErrorContext, "Add node\n");
500#endif
501 /*
502 * read the attributes
503 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000504 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000505 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000506 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
507 if (href == NULL)
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000508 return(-1);
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000509 local = 1;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000510 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000511 if (href[0] == '#')
512 local = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000513 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000514 if (parse != NULL) {
515 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
516 xml = 1;
517 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
518 xml = 0;
519 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000520 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
521 "invalid value %s for 'parse'\n", parse);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000522 if (href != NULL)
523 xmlFree(href);
524 if (parse != NULL)
525 xmlFree(parse);
526 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000527 }
528 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000529
530 /*
531 * compute the URI
532 */
533 base = xmlNodeGetBase(ctxt->doc, cur);
534 if (base == NULL) {
535 URI = xmlBuildURI(href, ctxt->doc->URL);
536 } else {
537 URI = xmlBuildURI(href, base);
538 }
539 if (URI == NULL) {
540 xmlChar *escbase;
541 xmlChar *eschref;
542 /*
543 * Some escaping may be needed
544 */
545 escbase = xmlURIEscape(base);
546 eschref = xmlURIEscape(href);
547 URI = xmlBuildURI(eschref, escbase);
548 if (escbase != NULL)
549 xmlFree(escbase);
550 if (eschref != NULL)
551 xmlFree(eschref);
552 }
553 if (parse != NULL)
554 xmlFree(parse);
555 if (href != NULL)
556 xmlFree(href);
557 if (base != NULL)
558 xmlFree(base);
559 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000560 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
561 "failed build URL\n", NULL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000562 return(-1);
563 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000564 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000565
566 /*
567 * Check the URL and remove any fragment identifier
568 */
569 uri = xmlParseURI((const char *)URI);
570 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000571 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
572 "invalid value URI %s\n", URI);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000573 if (fragment != NULL)
574 xmlFree(fragment);
575 xmlFree(URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000576 return(-1);
577 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000578
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000579 if (uri->fragment != NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000580 if (ctxt->legacy != 0) {
581 if (fragment == NULL) {
582 fragment = (xmlChar *) uri->fragment;
583 } else {
584 xmlFree(uri->fragment);
585 }
586 } else {
587 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
588 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
589 URI);
590 if (fragment != NULL)
591 xmlFree(fragment);
592 xmlFreeURI(uri);
593 xmlFree(URI);
594 return(-1);
595 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000596 uri->fragment = NULL;
597 }
598 URL = xmlSaveUri(uri);
599 xmlFreeURI(uri);
600 xmlFree(URI);
601 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000602 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
603 "invalid value URI %s\n", URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000604 if (fragment != NULL)
605 xmlFree(fragment);
606 return(-1);
607 }
608
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000609 /*
610 * Check the URL against the stack for recursions
611 */
612 if (!local) {
613 for (i = 0;i < ctxt->urlNr;i++) {
614 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000615 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
616 "detected a recursion in %s\n", URL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000617 return(-1);
618 }
619 }
620 }
621
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000622 ref = xmlXIncludeNewRef(ctxt, URL, cur);
623 if (ref == NULL) {
624 return(-1);
625 }
626 ref->fragment = fragment;
627 ref->doc = NULL;
628 ref->xml = xml;
629 ref->count = 1;
630 xmlFree(URL);
631 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000632}
633
634/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000635 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000636 * @ctxt: the XInclude context
637 * @doc: the new document
638 * @url: the associated URL
639 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000640 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000641 */
642static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000643xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000644 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000645 xmlXIncludeCtxtPtr newctxt;
646 int i;
647
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000648 /*
649 * Avoid recursion in already substitued resources
650 for (i = 0;i < ctxt->urlNr;i++) {
651 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
652 return;
653 }
654 */
655
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000656#ifdef DEBUG_XINCLUDE
657 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
658#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000659 /*
660 * Handle recursion here.
661 */
662
663 newctxt = xmlXIncludeNewContext(doc);
664 if (newctxt != NULL) {
665 /*
666 * Copy the existing document set
667 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000668 newctxt->incMax = ctxt->incMax;
669 newctxt->incNr = ctxt->incNr;
670 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
671 sizeof(newctxt->incTab[0]));
672 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000673 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000674 xmlFree(newctxt);
675 return;
676 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000677 /*
678 * copy the urlTab
679 */
680 newctxt->urlMax = ctxt->urlMax;
681 newctxt->urlNr = ctxt->urlNr;
682 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000683
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000684 /*
William M. Brackf7789b12004-06-07 08:57:27 +0000685 * Inherit the existing base
686 */
Daniel Veillardce244ad2004-11-05 10:03:46 +0000687 newctxt->base = xmlStrdup(ctxt->base);
William M. Brackf7789b12004-06-07 08:57:27 +0000688
689 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000690 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000691 */
692 newctxt->incBase = ctxt->incNr;
693 for (i = 0;i < ctxt->incNr;i++) {
694 newctxt->incTab[i] = ctxt->incTab[i];
695 newctxt->incTab[i]->count++; /* prevent the recursion from
696 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000697 }
William M. Bracka11e4832004-03-07 11:03:43 +0000698 /*
699 * The new context should also inherit the Parse Flags
700 * (bug 132597)
701 */
702 newctxt->parseFlags = ctxt->parseFlags;
Daniel Veillard8edf1c52003-07-22 20:52:14 +0000703 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000704 for (i = 0;i < ctxt->incNr;i++) {
705 newctxt->incTab[i]->count--;
706 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000707 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000708
709 /* urlTab may have been reallocated */
710 ctxt->urlTab = newctxt->urlTab;
711 ctxt->urlMax = newctxt->urlMax;
712
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000713 newctxt->urlMax = 0;
714 newctxt->urlNr = 0;
715 newctxt->urlTab = NULL;
716
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000717 xmlXIncludeFreeContext(newctxt);
718 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000719#ifdef DEBUG_XINCLUDE
720 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
721#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000722}
723
724/**
725 * xmlXIncludeAddTxt:
726 * @ctxt: the XInclude context
727 * @txt: the new text node
728 * @url: the associated URL
729 *
730 * Add a new txtument to the list
731 */
732static void
733xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000734#ifdef DEBUG_XINCLUDE
735 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
736#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000737 if (ctxt->txtMax == 0) {
738 ctxt->txtMax = 4;
739 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
740 sizeof(ctxt->txtTab[0]));
741 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000742 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000743 return;
744 }
745 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
746 sizeof(ctxt->txturlTab[0]));
747 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000748 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000749 return;
750 }
751 }
752 if (ctxt->txtNr >= ctxt->txtMax) {
753 ctxt->txtMax *= 2;
754 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
755 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
756 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000757 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000758 return;
759 }
760 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000761 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000762 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000763 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000764 return;
765 }
766 }
767 ctxt->txtTab[ctxt->txtNr] = txt;
768 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
769 ctxt->txtNr++;
770}
771
Owen Taylor3473f882001-02-23 17:55:21 +0000772/************************************************************************
773 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000774 * Node copy with specific semantic *
775 * *
776 ************************************************************************/
777
778/**
779 * xmlXIncludeCopyNode:
780 * @ctxt: the XInclude context
781 * @target: the document target
782 * @source: the document source
783 * @elem: the element
784 *
785 * Make a copy of the node while preserving the XInclude semantic
786 * of the Infoset copy
787 */
788static xmlNodePtr
789xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
790 xmlDocPtr source, xmlNodePtr elem) {
791 xmlNodePtr result = NULL;
792
793 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
794 (elem == NULL))
795 return(NULL);
796 if (elem->type == XML_DTD_NODE)
797 return(NULL);
798 result = xmlDocCopyNode(elem, target, 1);
799 return(result);
800}
801
802/**
803 * xmlXIncludeCopyNodeList:
804 * @ctxt: the XInclude context
805 * @target: the document target
806 * @source: the document source
807 * @elem: the element list
808 *
809 * Make a copy of the node list while preserving the XInclude semantic
810 * of the Infoset copy
811 */
812static xmlNodePtr
813xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
814 xmlDocPtr source, xmlNodePtr elem) {
815 xmlNodePtr cur, res, result = NULL, last = NULL;
816
817 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
818 (elem == NULL))
819 return(NULL);
820 cur = elem;
821 while (cur != NULL) {
822 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
823 if (res != NULL) {
824 if (result == NULL) {
825 result = last = res;
826 } else {
827 last->next = res;
828 res->prev = last;
829 last = res;
830 }
831 }
832 cur = cur->next;
833 }
834 return(result);
835}
836
837/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000838 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000839 * @cur: the node
840 * @no: the child number
841 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000842 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000843 */
844static xmlNodePtr
845xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
846 int i;
847 if (cur == NULL)
848 return(cur);
849 cur = cur->children;
850 for (i = 0;i <= no;cur = cur->next) {
851 if (cur == NULL)
852 return(cur);
853 if ((cur->type == XML_ELEMENT_NODE) ||
854 (cur->type == XML_DOCUMENT_NODE) ||
855 (cur->type == XML_HTML_DOCUMENT_NODE)) {
856 i++;
857 if (i == no)
858 break;
859 }
860 }
861 return(cur);
862}
863
William M. Brackf7eb7942003-12-31 07:59:17 +0000864xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000865/**
866 * xmlXIncludeCopyRange:
867 * @ctxt: the XInclude context
868 * @target: the document target
869 * @source: the document source
870 * @obj: the XPointer result from the evaluation.
871 *
872 * Build a node list tree copy of the XPointer result.
873 *
874 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000875 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000876 */
877static xmlNodePtr
878xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
879 xmlDocPtr source, xmlXPathObjectPtr range) {
880 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000881 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
882 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000883 /* pointers to traversal nodes */
884 xmlNodePtr start, cur, end;
885 int index1, index2;
William M. Brack6bdacd72004-02-07 08:53:23 +0000886 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000887
888 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
889 (range == NULL))
890 return(NULL);
891 if (range->type != XPATH_RANGE)
892 return(NULL);
893 start = (xmlNodePtr) range->user;
894
895 if (start == NULL)
896 return(NULL);
897 end = range->user2;
898 if (end == NULL)
899 return(xmlDocCopyNode(start, target, 1));
900
901 cur = start;
902 index1 = range->index;
903 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000904 /*
905 * level is depth of the current node under consideration
906 * list is the pointer to the root of the output tree
907 * listParent is a pointer to the parent of output tree (within
908 the included file) in case we need to add another level
909 * last is a pointer to the last node added to the output tree
910 * lastLevel is the depth of last (relative to the root)
911 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000912 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000913 /*
914 * Check if our output tree needs a parent
915 */
916 if (level < 0) {
917 while (level < 0) {
William M. Brack57e9e912004-03-09 16:19:02 +0000918 /* copy must include namespaces and properties */
919 tmp2 = xmlDocCopyNode(listParent, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000920 xmlAddChild(tmp2, list);
921 list = tmp2;
922 listParent = listParent->parent;
923 level++;
924 }
925 last = list;
926 lastLevel = 0;
927 }
928 /*
929 * Check whether we need to change our insertion point
930 */
931 while (level < lastLevel) {
932 last = last->parent;
933 lastLevel --;
934 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000935 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000936 if (cur->type == XML_TEXT_NODE) {
937 const xmlChar *content = cur->content;
938 int len;
939
940 if (content == NULL) {
941 tmp = xmlNewTextLen(NULL, 0);
942 } else {
943 len = index2;
944 if ((cur == start) && (index1 > 1)) {
945 content += (index1 - 1);
946 len -= (index1 - 1);
947 index1 = 0;
948 } else {
949 len = index2;
950 }
951 tmp = xmlNewTextLen(content, len);
952 }
953 /* single sub text node selection */
954 if (list == NULL)
955 return(tmp);
956 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +0000957 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000958 xmlAddNextSibling(last, tmp);
959 else
William M. Brackf7eb7942003-12-31 07:59:17 +0000960 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000961 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +0000962 } else { /* ending node not a text node */
William M. Brack6bdacd72004-02-07 08:53:23 +0000963 endLevel = level; /* remember the level of the end node */
964 endFlag = 1;
William M. Brack57e9e912004-03-09 16:19:02 +0000965 /* last node - need to take care of properties + namespaces */
966 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000967 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000968 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000969 listParent = cur->parent;
970 } else {
971 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000972 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +0000973 else {
974 xmlAddChild(last, tmp);
975 lastLevel = level;
976 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000977 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000978 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000979
980 if (index2 > 1) {
981 end = xmlXIncludeGetNthChild(cur, index2 - 1);
982 index2 = 0;
983 }
984 if ((cur == start) && (index1 > 1)) {
985 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
986 index1 = 0;
William M. Brack6bdacd72004-02-07 08:53:23 +0000987 } else {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000988 cur = cur->children;
989 }
William M. Brack6bdacd72004-02-07 08:53:23 +0000990 level++; /* increment level to show change */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000991 /*
992 * Now gather the remaining nodes from cur to end
993 */
William M. Brack6bdacd72004-02-07 08:53:23 +0000994 continue; /* while */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000995 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000996 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000997 if ((cur->type == XML_TEXT_NODE) ||
998 (cur->type == XML_CDATA_SECTION_NODE)) {
999 const xmlChar *content = cur->content;
1000
1001 if (content == NULL) {
1002 tmp = xmlNewTextLen(NULL, 0);
1003 } else {
1004 if (index1 > 1) {
1005 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +00001006 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001007 }
1008 tmp = xmlNewText(content);
1009 }
1010 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001011 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001012 } else { /* Not text node */
William M. Brack57e9e912004-03-09 16:19:02 +00001013 /*
1014 * start of the range - need to take care of
1015 * properties and namespaces
1016 */
1017 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +00001018 list = last = tmp;
1019 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001020 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001021 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001022 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001023 index1 = 0;
1024 /*
1025 * Now gather the remaining nodes from cur to end
1026 */
1027 continue; /* while */
1028 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001029 }
1030 } else {
1031 tmp = NULL;
1032 switch (cur->type) {
1033 case XML_DTD_NODE:
1034 case XML_ELEMENT_DECL:
1035 case XML_ATTRIBUTE_DECL:
1036 case XML_ENTITY_NODE:
1037 /* Do not copy DTD informations */
1038 break;
1039 case XML_ENTITY_DECL:
1040 /* handle crossing entities -> stack needed */
1041 break;
1042 case XML_XINCLUDE_START:
1043 case XML_XINCLUDE_END:
1044 /* don't consider it part of the tree content */
1045 break;
1046 case XML_ATTRIBUTE_NODE:
1047 /* Humm, should not happen ! */
1048 break;
1049 default:
William M. Brack57e9e912004-03-09 16:19:02 +00001050 /*
1051 * Middle of the range - need to take care of
1052 * properties and namespaces
1053 */
1054 tmp = xmlDocCopyNode(cur, target, 2);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001055 break;
1056 }
1057 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001058 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001059 xmlAddNextSibling(last, tmp);
1060 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001061 xmlAddChild(last, tmp);
1062 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001063 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001064 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001065 }
1066 }
1067 /*
1068 * Skip to next node in document order
1069 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001070 cur = xmlXPtrAdvanceNode(cur, &level);
William M. Brack6bdacd72004-02-07 08:53:23 +00001071 if (endFlag && (level >= endLevel))
1072 break;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001073 }
1074 return(list);
1075}
1076
1077/**
1078 * xmlXIncludeBuildNodeList:
1079 * @ctxt: the XInclude context
1080 * @target: the document target
1081 * @source: the document source
1082 * @obj: the XPointer result from the evaluation.
1083 *
1084 * Build a node list tree copy of the XPointer result.
1085 * This will drop Attributes and Namespace declarations.
1086 *
1087 * Returns an xmlNodePtr list or NULL.
1088 * the caller has to free the node tree.
1089 */
1090static xmlNodePtr
1091xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1092 xmlDocPtr source, xmlXPathObjectPtr obj) {
1093 xmlNodePtr list = NULL, last = NULL;
1094 int i;
1095
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001096 if (source == NULL)
1097 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001098 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1099 (obj == NULL))
1100 return(NULL);
1101 switch (obj->type) {
1102 case XPATH_NODESET: {
1103 xmlNodeSetPtr set = obj->nodesetval;
1104 if (set == NULL)
1105 return(NULL);
1106 for (i = 0;i < set->nodeNr;i++) {
1107 if (set->nodeTab[i] == NULL)
1108 continue;
1109 switch (set->nodeTab[i]->type) {
1110 case XML_TEXT_NODE:
1111 case XML_CDATA_SECTION_NODE:
1112 case XML_ELEMENT_NODE:
1113 case XML_ENTITY_REF_NODE:
1114 case XML_ENTITY_NODE:
1115 case XML_PI_NODE:
1116 case XML_COMMENT_NODE:
1117 case XML_DOCUMENT_NODE:
1118 case XML_HTML_DOCUMENT_NODE:
1119#ifdef LIBXML_DOCB_ENABLED
1120 case XML_DOCB_DOCUMENT_NODE:
1121#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001122 case XML_XINCLUDE_END:
1123 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001124 case XML_XINCLUDE_START: {
1125 xmlNodePtr tmp, cur = set->nodeTab[i];
1126
1127 cur = cur->next;
1128 while (cur != NULL) {
1129 switch(cur->type) {
1130 case XML_TEXT_NODE:
1131 case XML_CDATA_SECTION_NODE:
1132 case XML_ELEMENT_NODE:
1133 case XML_ENTITY_REF_NODE:
1134 case XML_ENTITY_NODE:
1135 case XML_PI_NODE:
1136 case XML_COMMENT_NODE:
1137 tmp = xmlXIncludeCopyNode(ctxt, target,
1138 source, cur);
1139 if (last == NULL) {
1140 list = last = tmp;
1141 } else {
1142 xmlAddNextSibling(last, tmp);
1143 last = tmp;
1144 }
1145 cur = cur->next;
1146 continue;
1147 default:
1148 break;
1149 }
1150 break;
1151 }
1152 continue;
1153 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001154 case XML_ATTRIBUTE_NODE:
1155 case XML_NAMESPACE_DECL:
1156 case XML_DOCUMENT_TYPE_NODE:
1157 case XML_DOCUMENT_FRAG_NODE:
1158 case XML_NOTATION_NODE:
1159 case XML_DTD_NODE:
1160 case XML_ELEMENT_DECL:
1161 case XML_ATTRIBUTE_DECL:
1162 case XML_ENTITY_DECL:
1163 continue; /* for */
1164 }
1165 if (last == NULL)
1166 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1167 set->nodeTab[i]);
1168 else {
1169 xmlAddNextSibling(last,
1170 xmlXIncludeCopyNode(ctxt, target, source,
1171 set->nodeTab[i]));
1172 if (last->next != NULL)
1173 last = last->next;
1174 }
1175 }
1176 break;
1177 }
1178 case XPATH_LOCATIONSET: {
1179 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1180 if (set == NULL)
1181 return(NULL);
1182 for (i = 0;i < set->locNr;i++) {
1183 if (last == NULL)
1184 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1185 set->locTab[i]);
1186 else
1187 xmlAddNextSibling(last,
1188 xmlXIncludeCopyXPointer(ctxt, target, source,
1189 set->locTab[i]));
1190 if (last != NULL) {
1191 while (last->next != NULL)
1192 last = last->next;
1193 }
1194 }
1195 break;
1196 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001197#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001198 case XPATH_RANGE:
1199 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001200#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001201 case XPATH_POINT:
1202 /* points are ignored in XInclude */
1203 break;
1204 default:
1205 break;
1206 }
1207 return(list);
1208}
1209/************************************************************************
1210 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001211 * XInclude I/O handling *
1212 * *
1213 ************************************************************************/
1214
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001215typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1216typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1217struct _xmlXIncludeMergeData {
1218 xmlDocPtr doc;
1219 xmlXIncludeCtxtPtr ctxt;
1220};
1221
Owen Taylor3473f882001-02-23 17:55:21 +00001222/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001223 * xmlXIncludeMergeOneEntity:
1224 * @ent: the entity
1225 * @doc: the including doc
1226 * @nr: the entity name
1227 *
1228 * Inplements the merge of one entity
1229 */
1230static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001231xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001232 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001233 xmlEntityPtr ret, prev;
1234 xmlDocPtr doc;
1235 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001236
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001237 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001238 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001239 ctxt = data->ctxt;
1240 doc = data->doc;
1241 if ((ctxt == NULL) || (doc == NULL))
1242 return;
1243 switch (ent->etype) {
1244 case XML_INTERNAL_PARAMETER_ENTITY:
1245 case XML_EXTERNAL_PARAMETER_ENTITY:
1246 case XML_INTERNAL_PREDEFINED_ENTITY:
1247 return;
1248 case XML_INTERNAL_GENERAL_ENTITY:
1249 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1250 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1251 break;
1252 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001253 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1254 ent->SystemID, ent->content);
1255 if (ret != NULL) {
1256 if (ent->URI != NULL)
1257 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001258 } else {
1259 prev = xmlGetDocEntity(doc, ent->name);
1260 if (prev != NULL) {
1261 if (ent->etype != prev->etype)
1262 goto error;
1263
1264 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1265 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1266 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001267 } else if ((ent->ExternalID != NULL) &&
1268 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001269 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1270 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001271 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1272 if (!xmlStrEqual(ent->content, prev->content))
1273 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001274 } else {
1275 goto error;
1276 }
1277
1278 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001279 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001280 return;
1281error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001282 switch (ent->etype) {
1283 case XML_INTERNAL_PARAMETER_ENTITY:
1284 case XML_EXTERNAL_PARAMETER_ENTITY:
1285 case XML_INTERNAL_PREDEFINED_ENTITY:
1286 case XML_INTERNAL_GENERAL_ENTITY:
1287 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1288 return;
1289 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1290 break;
1291 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001292 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1293 "mismatch in redefinition of entity %s\n",
1294 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001295}
1296
1297/**
1298 * xmlXIncludeMergeEntities:
1299 * @ctxt: an XInclude context
1300 * @doc: the including doc
1301 * @from: the included doc
1302 *
1303 * Inplements the entity merge
1304 *
1305 * Returns 0 if merge succeeded, -1 if some processing failed
1306 */
1307static int
1308xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1309 xmlDocPtr from) {
1310 xmlNodePtr cur;
1311 xmlDtdPtr target, source;
1312
1313 if (ctxt == NULL)
1314 return(-1);
1315
1316 if ((from == NULL) || (from->intSubset == NULL))
1317 return(0);
1318
1319 target = doc->intSubset;
1320 if (target == NULL) {
1321 cur = xmlDocGetRootElement(doc);
1322 if (cur == NULL)
1323 return(-1);
1324 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1325 if (target == NULL)
1326 return(-1);
1327 }
1328
1329 source = from->intSubset;
1330 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001331 xmlXIncludeMergeData data;
1332
1333 data.ctxt = ctxt;
1334 data.doc = doc;
1335
Daniel Veillard4287c572003-02-04 22:48:53 +00001336 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001337 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001338 }
1339 source = from->extSubset;
1340 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001341 xmlXIncludeMergeData data;
1342
1343 data.ctxt = ctxt;
1344 data.doc = doc;
1345
Daniel Veillard4287c572003-02-04 22:48:53 +00001346 /*
1347 * don't duplicate existing stuff when external subsets are the same
1348 */
1349 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1350 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1351 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001352 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001353 }
1354 }
1355 return(0);
1356}
1357
1358/**
Owen Taylor3473f882001-02-23 17:55:21 +00001359 * xmlXIncludeLoadDoc:
1360 * @ctxt: the XInclude context
1361 * @url: the associated URL
1362 * @nr: the xinclude node number
1363 *
1364 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001365 *
1366 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001367 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001368static int
Owen Taylor3473f882001-02-23 17:55:21 +00001369xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1370 xmlDocPtr doc;
1371 xmlURIPtr uri;
1372 xmlChar *URL;
1373 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001374 int i = 0;
William M. Brack4d59e222004-03-08 14:42:31 +00001375#ifdef LIBXML_XPTR_ENABLED
1376 int saveFlags;
1377#endif
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001378
1379#ifdef DEBUG_XINCLUDE
1380 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1381#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001382 /*
1383 * Check the URL and remove any fragment identifier
1384 */
1385 uri = xmlParseURI((const char *)url);
1386 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001387 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1388 XML_XINCLUDE_HREF_URI,
1389 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001390 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001391 }
1392 if (uri->fragment != NULL) {
1393 fragment = (xmlChar *) uri->fragment;
1394 uri->fragment = NULL;
1395 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001396 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1397 (ctxt->incTab[nr]->fragment != NULL)) {
1398 if (fragment != NULL) xmlFree(fragment);
1399 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1400 }
Owen Taylor3473f882001-02-23 17:55:21 +00001401 URL = xmlSaveUri(uri);
1402 xmlFreeURI(uri);
1403 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001404 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1405 XML_XINCLUDE_HREF_URI,
1406 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001407 if (fragment != NULL)
1408 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001409 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001410 }
1411
1412 /*
1413 * Handling of references to the local document are done
1414 * directly through ctxt->doc.
1415 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001416 if ((URL[0] == 0) || (URL[0] == '#') ||
1417 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001418 doc = NULL;
1419 goto loaded;
1420 }
1421
1422 /*
1423 * Prevent reloading twice the document.
1424 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001425 for (i = 0; i < ctxt->incNr; i++) {
1426 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1427 (ctxt->incTab[i]->doc != NULL)) {
1428 doc = ctxt->incTab[i]->doc;
1429#ifdef DEBUG_XINCLUDE
1430 printf("Already loaded %s\n", URL);
1431#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001432 goto loaded;
1433 }
1434 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001435
Owen Taylor3473f882001-02-23 17:55:21 +00001436 /*
1437 * Load it.
1438 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001439#ifdef DEBUG_XINCLUDE
1440 printf("loading %s\n", URL);
1441#endif
William M. Brack4d59e222004-03-08 14:42:31 +00001442#ifdef LIBXML_XPTR_ENABLED
1443 /*
1444 * If this is an XPointer evaluation, we want to assure that
1445 * all entities have been resolved prior to processing the
1446 * referenced document
1447 */
1448 saveFlags = ctxt->parseFlags;
1449 if (fragment != NULL) { /* if this is an XPointer eval */
1450 ctxt->parseFlags |= XML_PARSE_NOENT;
1451 }
1452#endif
1453
Daniel Veillard98485322003-08-14 15:44:40 +00001454 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
William M. Brack4d59e222004-03-08 14:42:31 +00001455#ifdef LIBXML_XPTR_ENABLED
1456 ctxt->parseFlags = saveFlags;
1457#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001458 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001459 xmlFree(URL);
1460 if (fragment != NULL)
1461 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001462 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001463 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001464 ctxt->incTab[nr]->doc = doc;
William M. Brackb85c9202004-07-26 00:20:13 +00001465 /*
1466 * It's possible that the requested URL has been mapped to a
1467 * completely different location (e.g. through a catalog entry).
1468 * To check for this, we compare the URL with that of the doc
1469 * and change it if they disagree (bug 146988).
1470 */
1471 if (!xmlStrEqual(URL, doc->URL)) {
1472 xmlFree(URL);
1473 URL = xmlStrdup(doc->URL);
1474 }
Daniel Veillard98485322003-08-14 15:44:40 +00001475 for (i = nr + 1; i < ctxt->incNr; i++) {
1476 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1477 ctxt->incTab[nr]->count++;
1478#ifdef DEBUG_XINCLUDE
1479 printf("Increasing %s count since reused\n", URL);
1480#endif
1481 break;
1482 }
1483 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001484
1485 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001486 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001487 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001488 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001489
1490 /*
1491 * We don't need the DTD anymore, free up space
1492 if (doc->intSubset != NULL) {
1493 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1494 xmlFreeNode((xmlNodePtr) doc->intSubset);
1495 doc->intSubset = NULL;
1496 }
1497 if (doc->extSubset != NULL) {
1498 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1499 xmlFreeNode((xmlNodePtr) doc->extSubset);
1500 doc->extSubset = NULL;
1501 }
1502 */
1503 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001504
1505loaded:
1506 if (fragment == NULL) {
1507 /*
1508 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001509 */
1510 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001511 {
1512 /* Hopefully a DTD declaration won't be copied from
1513 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001514 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001515 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001516 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001517 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001518 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001519 }
1520#ifdef LIBXML_XPTR_ENABLED
1521 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001522 /*
1523 * Computes the XPointer expression and make a copy used
1524 * as the replacement copy.
1525 */
1526 xmlXPathObjectPtr xptr;
1527 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001528 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001529
1530 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001531 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1532 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001533 } else {
1534 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1535 }
1536 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001537 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1538 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001539 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001540 xmlFree(URL);
1541 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001542 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001543 }
1544 xptr = xmlXPtrEval(fragment, xptrctxt);
1545 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001546 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1547 XML_XINCLUDE_XPTR_FAILED,
1548 "XPointer evaluation failed: #%s\n",
1549 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001550 xmlXPathFreeContext(xptrctxt);
1551 xmlFree(URL);
1552 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001553 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001554 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001555 switch (xptr->type) {
1556 case XPATH_UNDEFINED:
1557 case XPATH_BOOLEAN:
1558 case XPATH_NUMBER:
1559 case XPATH_STRING:
1560 case XPATH_POINT:
1561 case XPATH_USERS:
1562 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001563 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1564 XML_XINCLUDE_XPTR_RESULT,
1565 "XPointer is not a range: #%s\n",
1566 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001567 xmlXPathFreeContext(xptrctxt);
1568 xmlFree(URL);
1569 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001570 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001571 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001572 if ((xptr->nodesetval == NULL) ||
1573 (xptr->nodesetval->nodeNr <= 0)) {
1574 xmlXPathFreeContext(xptrctxt);
1575 xmlFree(URL);
1576 xmlFree(fragment);
1577 return(-1);
1578 }
William M. Brackabf598b2004-06-08 02:01:28 +00001579
Daniel Veillard39196eb2001-06-19 18:09:42 +00001580 case XPATH_RANGE:
1581 case XPATH_LOCATIONSET:
1582 break;
1583 }
1584 set = xptr->nodesetval;
1585 if (set != NULL) {
1586 for (i = 0;i < set->nodeNr;i++) {
1587 if (set->nodeTab[i] == NULL)
1588 continue;
1589 switch (set->nodeTab[i]->type) {
1590 case XML_TEXT_NODE:
1591 case XML_CDATA_SECTION_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001592 case XML_ENTITY_REF_NODE:
1593 case XML_ENTITY_NODE:
1594 case XML_PI_NODE:
1595 case XML_COMMENT_NODE:
1596 case XML_DOCUMENT_NODE:
1597 case XML_HTML_DOCUMENT_NODE:
1598#ifdef LIBXML_DOCB_ENABLED
1599 case XML_DOCB_DOCUMENT_NODE:
1600#endif
1601 continue;
William M. Brackabf598b2004-06-08 02:01:28 +00001602 case XML_ELEMENT_NODE: {
1603 xmlChar *nodeBase;
1604 xmlNodePtr el = set->nodeTab[i];
1605
1606 nodeBase = xmlNodeGetBase(el->doc, el);
1607 if (nodeBase != NULL) {
1608 if (!xmlStrEqual(nodeBase, el->doc->URL))
1609 xmlNodeSetBase(el, nodeBase);
1610 xmlFree(nodeBase);
1611 }
1612 continue;
1613 }
1614
Daniel Veillard39196eb2001-06-19 18:09:42 +00001615 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001616 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1617 XML_XINCLUDE_XPTR_RESULT,
1618 "XPointer selects an attribute: #%s\n",
1619 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001620 set->nodeTab[i] = NULL;
1621 continue;
1622 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001623 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1624 XML_XINCLUDE_XPTR_RESULT,
1625 "XPointer selects a namespace: #%s\n",
1626 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001627 set->nodeTab[i] = NULL;
1628 continue;
1629 case XML_DOCUMENT_TYPE_NODE:
1630 case XML_DOCUMENT_FRAG_NODE:
1631 case XML_NOTATION_NODE:
1632 case XML_DTD_NODE:
1633 case XML_ELEMENT_DECL:
1634 case XML_ATTRIBUTE_DECL:
1635 case XML_ENTITY_DECL:
1636 case XML_XINCLUDE_START:
1637 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001638 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1639 XML_XINCLUDE_XPTR_RESULT,
1640 "XPointer selects unexpected nodes: #%s\n",
1641 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001642 set->nodeTab[i] = NULL;
1643 set->nodeTab[i] = NULL;
1644 continue; /* for */
1645 }
1646 }
1647 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001648 if (doc == NULL) {
1649 ctxt->incTab[nr]->xptr = xptr;
1650 ctxt->incTab[nr]->inc = NULL;
1651 } else {
1652 ctxt->incTab[nr]->inc =
1653 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1654 xmlXPathFreeObject(xptr);
1655 }
Owen Taylor3473f882001-02-23 17:55:21 +00001656 xmlXPathFreeContext(xptrctxt);
1657 xmlFree(fragment);
1658 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001659#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001660
1661 /*
1662 * Do the xml:base fixup if needed
1663 */
1664 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1665 xmlNodePtr node;
William M. Brack0b13a092005-06-01 03:37:59 +00001666 xmlChar *base;
William M. Brackabf598b2004-06-08 02:01:28 +00001667 xmlChar *curBase;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001668
William M. Brackf7789b12004-06-07 08:57:27 +00001669 /*
William M. Brack0b13a092005-06-01 03:37:59 +00001670 * The base is only adjusted if "necessary", i.e. if the xinclude node
1671 * has a base specified, or the URL is relative
William M. Brackf7789b12004-06-07 08:57:27 +00001672 */
William M. Brack0b13a092005-06-01 03:37:59 +00001673 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1674 XML_XML_NAMESPACE);
1675 if (base == NULL) {
1676 /*
1677 * No xml:base on the xinclude node, so we check whether the
1678 * URI base is different than (relative to) the context base
1679 */
1680 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1681 if (curBase == NULL) { /* Error return */
1682 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
William M. Brackf7789b12004-06-07 08:57:27 +00001683 XML_XINCLUDE_HREF_URI,
1684 "trying to build relative URI from %s\n", URL);
William M. Brack0b13a092005-06-01 03:37:59 +00001685 } else {
1686 /* If the URI doesn't contain a slash, it's not relative */
1687 if (!xmlStrchr(curBase, (xmlChar) '/'))
1688 xmlFree(curBase);
1689 else
1690 base = curBase;
William M. Brackf7789b12004-06-07 08:57:27 +00001691 }
William M. Brack0b13a092005-06-01 03:37:59 +00001692 }
1693 if (base != NULL) { /* Adjustment may be needed */
1694 node = ctxt->incTab[nr]->inc;
1695 while (node != NULL) {
1696 /* Only work on element nodes */
1697 if (node->type == XML_ELEMENT_NODE) {
1698 curBase = xmlNodeGetBase(node->doc, node);
1699 /* If no current base, set it */
1700 if (curBase == NULL) {
1701 xmlNodeSetBase(node, base);
1702 } else {
1703 /*
1704 * If the current base is the same as the
1705 * URL of the document, then reset it to be
1706 * the specified xml:base or the relative URI
1707 */
1708 if (xmlStrEqual(curBase, node->doc->URL)) {
1709 xmlNodeSetBase(node, base);
1710 } else {
1711 /*
1712 * If the element already has an xml:base
1713 * set, then relativise it if necessary
1714 */
1715 xmlChar *xmlBase;
1716 xmlBase = xmlGetNsProp(node,
1717 BAD_CAST "base",
1718 XML_XML_NAMESPACE);
1719 if (xmlBase != NULL) {
1720 xmlChar *relBase;
1721 relBase = xmlBuildURI(xmlBase, base);
1722 if (relBase == NULL) { /* error */
1723 xmlXIncludeErr(ctxt,
1724 ctxt->incTab[nr]->ref,
1725 XML_XINCLUDE_HREF_URI,
1726 "trying to rebuild base from %s\n",
1727 xmlBase);
1728 } else {
1729 xmlNodeSetBase(node, relBase);
1730 xmlFree(relBase);
1731 }
1732 xmlFree(xmlBase);
1733 }
1734 }
1735 xmlFree(curBase);
1736 }
1737 }
1738 node = node->next;
1739 }
1740 xmlFree(base);
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001741 }
1742 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001743 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1744 (ctxt->incTab[nr]->count <= 1)) {
1745#ifdef DEBUG_XINCLUDE
1746 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1747#endif
1748 xmlFreeDoc(ctxt->incTab[nr]->doc);
1749 ctxt->incTab[nr]->doc = NULL;
1750 }
Owen Taylor3473f882001-02-23 17:55:21 +00001751 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001752 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001753}
1754
1755/**
1756 * xmlXIncludeLoadTxt:
1757 * @ctxt: the XInclude context
1758 * @url: the associated URL
1759 * @nr: the xinclude node number
1760 *
1761 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001762 *
1763 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001764 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001765static int
Owen Taylor3473f882001-02-23 17:55:21 +00001766xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1767 xmlParserInputBufferPtr buf;
1768 xmlNodePtr node;
1769 xmlURIPtr uri;
1770 xmlChar *URL;
1771 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001772 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001773 xmlCharEncoding enc = (xmlCharEncoding) 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001774
Owen Taylor3473f882001-02-23 17:55:21 +00001775 /*
1776 * Check the URL and remove any fragment identifier
1777 */
1778 uri = xmlParseURI((const char *)url);
1779 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001780 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1781 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001782 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001783 }
1784 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001785 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1786 "fragment identifier forbidden for text: %s\n",
1787 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001788 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001789 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001790 }
1791 URL = xmlSaveUri(uri);
1792 xmlFreeURI(uri);
1793 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001794 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1795 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001796 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001797 }
1798
1799 /*
1800 * Handling of references to the local document are done
1801 * directly through ctxt->doc.
1802 */
1803 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001804 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1805 XML_XINCLUDE_TEXT_DOCUMENT,
1806 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001807 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001808 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001809 }
1810
1811 /*
1812 * Prevent reloading twice the document.
1813 */
1814 for (i = 0; i < ctxt->txtNr; i++) {
1815 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1816 node = xmlCopyNode(ctxt->txtTab[i], 1);
1817 goto loaded;
1818 }
1819 }
1820 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001821 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001822 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001823 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1824 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1825 }
1826 if (encoding != NULL) {
1827 /*
1828 * TODO: we should not have to remap to the xmlCharEncoding
1829 * predefined set, a better interface than
1830 * xmlParserInputBufferCreateFilename should allow any
1831 * encoding supported by iconv
1832 */
1833 enc = xmlParseCharEncoding((const char *) encoding);
1834 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001835 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1836 XML_XINCLUDE_UNKNOWN_ENCODING,
1837 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001838 xmlFree(encoding);
1839 xmlFree(URL);
1840 return(-1);
1841 }
1842 xmlFree(encoding);
1843 }
1844
1845 /*
1846 * Load it.
1847 */
1848 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001849 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001850 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001851 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001852 }
1853 node = xmlNewText(NULL);
1854
1855 /*
1856 * Scan all chars from the resource and add the to the node
1857 */
1858 while (xmlParserInputBufferRead(buf, 128) > 0) {
1859 int len;
1860 const xmlChar *content;
1861
1862 content = xmlBufferContent(buf->buffer);
1863 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001864 for (i = 0;i < len;) {
1865 int cur;
1866 int l;
1867
1868 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1869 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001870 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1871 XML_XINCLUDE_INVALID_CHAR,
1872 "%s contains invalid char\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001873 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001874 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001875 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001876 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001877 }
1878 xmlBufferShrink(buf->buffer, len);
1879 }
1880 xmlFreeParserInputBuffer(buf);
1881 xmlXIncludeAddTxt(ctxt, node, URL);
1882
1883loaded:
1884 /*
1885 * Add the element as the replacement copy.
1886 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001887 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001888 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001889 return(0);
1890}
1891
1892/**
1893 * xmlXIncludeLoadFallback:
1894 * @ctxt: the XInclude context
1895 * @fallback: the fallback node
1896 * @nr: the xinclude node number
1897 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001898 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001899 * in the XInclude context
1900 *
1901 * Returns 0 in case of success, -1 in case of failure
1902 */
1903static int
1904xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001905 xmlXIncludeCtxtPtr newctxt;
1906 int ret = 0;
1907
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001908 if ((fallback == NULL) || (ctxt == NULL))
1909 return(-1);
William M. Brackef245fd2004-02-06 09:33:59 +00001910 if (fallback->children != NULL) {
1911 /*
1912 * It's possible that the fallback also has 'includes'
1913 * (Bug 129969), so we re-process the fallback just in case
1914 */
1915 newctxt = xmlXIncludeNewContext(ctxt->doc);
1916 if (newctxt == NULL)
1917 return (-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00001918 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
William M. Brackef245fd2004-02-06 09:33:59 +00001919 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1920 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
William M. Brack87640d52004-04-17 14:58:15 +00001921 if (ctxt->nbErrors > 0)
William M. Brackef245fd2004-02-06 09:33:59 +00001922 ret = -1;
William M. Brack87640d52004-04-17 14:58:15 +00001923 else if (ret > 0)
1924 ret = 0; /* xmlXIncludeDoProcess can return +ve number */
William M. Brackef245fd2004-02-06 09:33:59 +00001925 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001926
Daniel Veillard03a53c32004-10-26 16:06:51 +00001927 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1928 fallback->children);
William M. Brackef245fd2004-02-06 09:33:59 +00001929 } else {
1930 ctxt->incTab[nr]->inc = NULL;
William M. Brack95af5942004-02-08 04:12:49 +00001931 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
William M. Brackef245fd2004-02-06 09:33:59 +00001932 }
William M. Brackaae10522004-01-02 14:59:41 +00001933 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001934}
1935
1936/************************************************************************
1937 * *
1938 * XInclude Processing *
1939 * *
1940 ************************************************************************/
1941
1942/**
1943 * xmlXIncludePreProcessNode:
1944 * @ctxt: an XInclude context
1945 * @node: an XInclude node
1946 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001947 * Implement the XInclude preprocessing, currently just adding the element
1948 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001949 *
1950 * Returns the result list or NULL in case of error
1951 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001952static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001953xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1954 xmlXIncludeAddNode(ctxt, node);
1955 return(0);
1956}
1957
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001958/**
Owen Taylor3473f882001-02-23 17:55:21 +00001959 * xmlXIncludeLoadNode:
1960 * @ctxt: an XInclude context
1961 * @nr: the node number
1962 *
1963 * Find and load the infoset replacement for the given node.
1964 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001965 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001966 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001967static int
Owen Taylor3473f882001-02-23 17:55:21 +00001968xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1969 xmlNodePtr cur;
1970 xmlChar *href;
1971 xmlChar *parse;
1972 xmlChar *base;
William M. Brackf7789b12004-06-07 08:57:27 +00001973 xmlChar *oldBase;
Owen Taylor3473f882001-02-23 17:55:21 +00001974 xmlChar *URI;
1975 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001976 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001977
1978 if (ctxt == NULL)
1979 return(-1);
1980 if ((nr < 0) || (nr >= ctxt->incNr))
1981 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001982 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001983 if (cur == NULL)
1984 return(-1);
1985
Owen Taylor3473f882001-02-23 17:55:21 +00001986 /*
1987 * read the attributes
1988 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001989 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00001990 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001991 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
1992 if (href == NULL)
1993 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001994 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001995 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00001996 if (parse != NULL) {
1997 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1998 xml = 1;
1999 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2000 xml = 0;
2001 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002002 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2003 XML_XINCLUDE_PARSE_VALUE,
2004 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00002005 if (href != NULL)
2006 xmlFree(href);
2007 if (parse != NULL)
2008 xmlFree(parse);
2009 return(-1);
2010 }
2011 }
2012
2013 /*
2014 * compute the URI
2015 */
2016 base = xmlNodeGetBase(ctxt->doc, cur);
2017 if (base == NULL) {
2018 URI = xmlBuildURI(href, ctxt->doc->URL);
2019 } else {
2020 URI = xmlBuildURI(href, base);
2021 }
2022 if (URI == NULL) {
2023 xmlChar *escbase;
2024 xmlChar *eschref;
2025 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002026 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00002027 */
2028 escbase = xmlURIEscape(base);
2029 eschref = xmlURIEscape(href);
2030 URI = xmlBuildURI(eschref, escbase);
2031 if (escbase != NULL)
2032 xmlFree(escbase);
2033 if (eschref != NULL)
2034 xmlFree(eschref);
2035 }
2036 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002037 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2038 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002039 if (parse != NULL)
2040 xmlFree(parse);
2041 if (href != NULL)
2042 xmlFree(href);
2043 if (base != NULL)
2044 xmlFree(base);
2045 return(-1);
2046 }
2047#ifdef DEBUG_XINCLUDE
2048 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2049 xml ? "xml": "text");
2050 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2051#endif
2052
2053 /*
William M. Brackf7789b12004-06-07 08:57:27 +00002054 * Save the base for this include (saving the current one)
Owen Taylor3473f882001-02-23 17:55:21 +00002055 */
William M. Brackf7789b12004-06-07 08:57:27 +00002056 oldBase = ctxt->base;
2057 ctxt->base = base;
2058
Owen Taylor3473f882001-02-23 17:55:21 +00002059 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002060 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00002061 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2062 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002063 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2064 }
William M. Brackf7789b12004-06-07 08:57:27 +00002065
2066 /*
2067 * Restore the original base before checking for fallback
2068 */
2069 ctxt->base = oldBase;
2070
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002071 if (ret < 0) {
2072 xmlNodePtr children;
2073
2074 /*
2075 * Time to try a fallback if availble
2076 */
2077#ifdef DEBUG_XINCLUDE
2078 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2079#endif
2080 children = cur->children;
2081 while (children != NULL) {
2082 if ((children->type == XML_ELEMENT_NODE) &&
2083 (children->ns != NULL) &&
2084 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002085 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2086 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002087 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00002088 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002089 break;
2090 }
2091 children = children->next;
2092 }
2093 }
2094 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002095 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2096 XML_XINCLUDE_NO_FALLBACK,
2097 "could not load %s, and no fallback was found\n",
2098 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002099 }
2100
2101 /*
2102 * Cleanup
2103 */
2104 if (URI != NULL)
2105 xmlFree(URI);
2106 if (parse != NULL)
2107 xmlFree(parse);
2108 if (href != NULL)
2109 xmlFree(href);
2110 if (base != NULL)
2111 xmlFree(base);
2112 return(0);
2113}
2114
2115/**
2116 * xmlXIncludeIncludeNode:
2117 * @ctxt: an XInclude context
2118 * @nr: the node number
2119 *
2120 * Inplement the infoset replacement for the given node
2121 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002122 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002123 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002124static int
Owen Taylor3473f882001-02-23 17:55:21 +00002125xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002126 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002127
2128 if (ctxt == NULL)
2129 return(-1);
2130 if ((nr < 0) || (nr >= ctxt->incNr))
2131 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002132 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00002133 if (cur == NULL)
2134 return(-1);
2135
2136 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002137 * If we stored an XPointer a late computation may be needed
2138 */
2139 if ((ctxt->incTab[nr]->inc == NULL) &&
2140 (ctxt->incTab[nr]->xptr != NULL)) {
2141 ctxt->incTab[nr]->inc =
2142 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
2143 ctxt->incTab[nr]->xptr);
2144 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2145 ctxt->incTab[nr]->xptr = NULL;
2146 }
2147 list = ctxt->incTab[nr]->inc;
2148 ctxt->incTab[nr]->inc = NULL;
2149
2150 /*
2151 * Check against the risk of generating a multi-rooted document
2152 */
2153 if ((cur->parent != NULL) &&
2154 (cur->parent->type != XML_ELEMENT_NODE)) {
2155 int nb_elem = 0;
2156
2157 tmp = list;
2158 while (tmp != NULL) {
2159 if (tmp->type == XML_ELEMENT_NODE)
2160 nb_elem++;
2161 tmp = tmp->next;
2162 }
2163 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002164 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2165 XML_XINCLUDE_MULTIPLE_ROOT,
2166 "XInclude error: would result in multiple root nodes\n",
2167 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002168 return(-1);
2169 }
2170 }
2171
Daniel Veillardc14c3892004-08-16 12:34:50 +00002172 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2173 /*
2174 * Add the list of nodes
2175 */
2176 while (list != NULL) {
2177 end = list;
2178 list = list->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002179
Daniel Veillardc14c3892004-08-16 12:34:50 +00002180 xmlAddPrevSibling(cur, end);
2181 }
2182 xmlUnlinkNode(cur);
2183 xmlFreeNode(cur);
2184 } else {
2185 /*
2186 * Change the current node as an XInclude start one, and add an
2187 * XInclude end one
2188 */
2189 cur->type = XML_XINCLUDE_START;
Daniel Veillard03a53c32004-10-26 16:06:51 +00002190 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
Daniel Veillardc14c3892004-08-16 12:34:50 +00002191 if (end == NULL) {
2192 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2193 XML_XINCLUDE_BUILD_FAILED,
2194 "failed to build node\n", NULL);
2195 return(-1);
2196 }
2197 end->type = XML_XINCLUDE_END;
2198 xmlAddNextSibling(cur, end);
Owen Taylor3473f882001-02-23 17:55:21 +00002199
Daniel Veillardc14c3892004-08-16 12:34:50 +00002200 /*
2201 * Add the list of nodes
2202 */
2203 while (list != NULL) {
2204 cur = list;
2205 list = list->next;
2206
2207 xmlAddPrevSibling(end, cur);
2208 }
Owen Taylor3473f882001-02-23 17:55:21 +00002209 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002210
2211
Owen Taylor3473f882001-02-23 17:55:21 +00002212 return(0);
2213}
2214
2215/**
2216 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002217 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002218 * @node: an XInclude node
2219 *
2220 * test if the node is an XInclude node
2221 *
2222 * Returns 1 true, 0 otherwise
2223 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002224static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002225xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002226 if (node == NULL)
2227 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002228 if (node->type != XML_ELEMENT_NODE)
2229 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002230 if (node->ns == NULL)
2231 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002232 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2233 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2234 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2235 if (ctxt->legacy == 0) {
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002236#if 0 /* wait for the XML Core Working Group to get something stable ! */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002237 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2238 "Deprecated XInclude namespace found, use %s",
2239 XINCLUDE_NS);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002240#endif
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002241 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002242 }
2243 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002244 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2245 xmlNodePtr child = node->children;
2246 int nb_fallback = 0;
2247
2248 while (child != NULL) {
2249 if ((child->type == XML_ELEMENT_NODE) &&
2250 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002251 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2252 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002253 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002254 xmlXIncludeErr(ctxt, node,
2255 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2256 "%s has an 'include' child\n",
2257 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002258 return(0);
2259 }
2260 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2261 nb_fallback++;
2262 }
2263 }
2264 child = child->next;
2265 }
2266 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002267 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2268 "%s has multiple fallback children\n",
2269 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002270 return(0);
2271 }
2272 return(1);
2273 }
2274 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2275 if ((node->parent == NULL) ||
2276 (node->parent->type != XML_ELEMENT_NODE) ||
2277 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002278 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2279 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002280 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002281 xmlXIncludeErr(ctxt, node,
2282 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2283 "%s is not the child of an 'include'\n",
2284 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002285 }
2286 }
2287 }
Owen Taylor3473f882001-02-23 17:55:21 +00002288 return(0);
2289}
2290
2291/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002292 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002293 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002294 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002295 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002296 *
2297 * Implement the XInclude substitution on the XML document @doc
2298 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002299 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002300 * or the number of substitutions done.
2301 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002302static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002303xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002304 xmlNodePtr cur;
2305 int ret = 0;
2306 int i;
2307
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002308 if ((doc == NULL) || (tree == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002309 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002310 if (ctxt == NULL)
2311 return(-1);
2312
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002313 if (doc->URL != NULL) {
2314 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2315 if (ret < 0)
2316 return(-1);
2317 }
2318
Owen Taylor3473f882001-02-23 17:55:21 +00002319 /*
2320 * First phase: lookup the elements in the document
2321 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002322 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002323 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002324 xmlXIncludePreProcessNode(ctxt, cur);
William M. Brack7b0e2762004-05-12 09:33:23 +00002325 while ((cur != NULL) && (cur != tree->parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002326 /* TODO: need to work on entities -> stack */
2327 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002328 (cur->children->type != XML_ENTITY_DECL) &&
2329 (cur->children->type != XML_XINCLUDE_START) &&
2330 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002331 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002332 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002333 xmlXIncludePreProcessNode(ctxt, cur);
2334 } else if (cur->next != NULL) {
2335 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002336 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002337 xmlXIncludePreProcessNode(ctxt, cur);
2338 } else {
William M. Brack5d8d10b2004-04-16 08:11:26 +00002339 if (cur == tree)
2340 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002341 do {
2342 cur = cur->parent;
William M. Brack7b0e2762004-05-12 09:33:23 +00002343 if ((cur == NULL) || (cur == tree->parent))
2344 break; /* do */
Owen Taylor3473f882001-02-23 17:55:21 +00002345 if (cur->next != NULL) {
2346 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002347 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002348 xmlXIncludePreProcessNode(ctxt, cur);
2349 break; /* do */
2350 }
2351 } while (cur != NULL);
2352 }
2353 }
2354
2355 /*
2356 * Second Phase : collect the infosets fragments
2357 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002358 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002359 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002360 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002361 }
2362
2363 /*
2364 * Third phase: extend the original document infoset.
William M. Brack6b1a28d2004-02-06 11:24:44 +00002365 *
2366 * Originally we bypassed the inclusion if there were any errors
2367 * encountered on any of the XIncludes. A bug was raised (bug
2368 * 132588) requesting that we output the XIncludes without error,
2369 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2370 * give some other problems in the future, but for now it seems to
2371 * work ok.
2372 *
Owen Taylor3473f882001-02-23 17:55:21 +00002373 */
William M. Brack6b1a28d2004-02-06 11:24:44 +00002374 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
William M. Brack95af5942004-02-08 04:12:49 +00002375 if ((ctxt->incTab[i]->inc != NULL) ||
2376 (ctxt->incTab[i]->xptr != NULL) ||
2377 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002378 xmlXIncludeIncludeNode(ctxt, i);
Owen Taylor3473f882001-02-23 17:55:21 +00002379 }
2380
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002381 if (doc->URL != NULL)
2382 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002383 return(ret);
2384}
2385
2386/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002387 * xmlXIncludeSetFlags:
2388 * @ctxt: an XInclude processing context
2389 * @flags: a set of xmlParserOption used for parsing XML includes
2390 *
2391 * Set the flags used for further processing of XML resources.
2392 *
2393 * Returns 0 in case of success and -1 in case of error.
2394 */
2395int
2396xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2397 if (ctxt == NULL)
2398 return(-1);
2399 ctxt->parseFlags = flags;
2400 return(0);
2401}
2402
2403/**
2404 * xmlXIncludeProcessFlags:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002405 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002406 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002407 *
2408 * Implement the XInclude substitution on the XML document @doc
2409 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002410 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002411 * or the number of substitutions done.
2412 */
2413int
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002414xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002415 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002416 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002417 int ret = 0;
2418
2419 if (doc == NULL)
2420 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002421 tree = xmlDocGetRootElement(doc);
2422 if (tree == NULL)
2423 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002424 ctxt = xmlXIncludeNewContext(doc);
2425 if (ctxt == NULL)
2426 return(-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00002427 ctxt->base = xmlStrdup((xmlChar *)doc->URL);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002428 xmlXIncludeSetFlags(ctxt, flags);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002429 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2430 if ((ret >= 0) && (ctxt->nbErrors > 0))
2431 ret = -1;
2432
2433 xmlXIncludeFreeContext(ctxt);
2434 return(ret);
2435}
2436
2437/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002438 * xmlXIncludeProcess:
2439 * @doc: an XML document
2440 *
2441 * Implement the XInclude substitution on the XML document @doc
2442 *
2443 * Returns 0 if no substitution were done, -1 if some processing failed
2444 * or the number of substitutions done.
2445 */
2446int
2447xmlXIncludeProcess(xmlDocPtr doc) {
2448 return(xmlXIncludeProcessFlags(doc, 0));
2449}
2450
2451/**
2452 * xmlXIncludeProcessTreeFlags:
2453 * @tree: a node in an XML document
2454 * @flags: a set of xmlParserOption used for parsing XML includes
2455 *
2456 * Implement the XInclude substitution for the given subtree
2457 *
2458 * Returns 0 if no substitution were done, -1 if some processing failed
2459 * or the number of substitutions done.
2460 */
2461int
2462xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2463 xmlXIncludeCtxtPtr ctxt;
2464 int ret = 0;
2465
2466 if ((tree == NULL) || (tree->doc == NULL))
2467 return(-1);
2468 ctxt = xmlXIncludeNewContext(tree->doc);
2469 if (ctxt == NULL)
2470 return(-1);
William M. Brackf7789b12004-06-07 08:57:27 +00002471 ctxt->base = xmlNodeGetBase(tree->doc, tree);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002472 xmlXIncludeSetFlags(ctxt, flags);
2473 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2474 if ((ret >= 0) && (ctxt->nbErrors > 0))
2475 ret = -1;
2476
2477 xmlXIncludeFreeContext(ctxt);
2478 return(ret);
2479}
2480
2481/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002482 * xmlXIncludeProcessTree:
2483 * @tree: a node in an XML document
2484 *
2485 * Implement the XInclude substitution for the given subtree
2486 *
2487 * Returns 0 if no substitution were done, -1 if some processing failed
2488 * or the number of substitutions done.
2489 */
2490int
2491xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002492 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002493}
2494
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002495/**
2496 * xmlXIncludeProcessNode:
2497 * @ctxt: an existing XInclude context
2498 * @node: a node in an XML document
2499 *
2500 * Implement the XInclude substitution for the given subtree reusing
2501 * the informations and data coming from the given context.
2502 *
2503 * Returns 0 if no substitution were done, -1 if some processing failed
2504 * or the number of substitutions done.
2505 */
2506int
2507xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2508 int ret = 0;
2509
2510 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2511 return(-1);
2512 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2513 if ((ret >= 0) && (ctxt->nbErrors > 0))
2514 ret = -1;
2515 return(ret);
2516}
2517
Owen Taylor3473f882001-02-23 17:55:21 +00002518#else /* !LIBXML_XINCLUDE_ENABLED */
2519#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002520#define bottom_xinclude
2521#include "elfgcchack.h"