blob: d1c816f359fa467011b54ff38529b4f74db4dbb2 [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 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000061};
62
Owen Taylor3473f882001-02-23 17:55:21 +000063struct _xmlXIncludeCtxt {
64 xmlDocPtr doc; /* the source document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000065 int incBase; /* the first include for this document */
Owen Taylor3473f882001-02-23 17:55:21 +000066 int incNr; /* number of includes */
67 int incMax; /* size of includes tab */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000068 xmlXIncludeRefPtr *incTab; /* array of included references */
69
Owen Taylor3473f882001-02-23 17:55:21 +000070 int txtNr; /* number of unparsed documents */
71 int txtMax; /* size of unparsed documents tab */
72 xmlNodePtr *txtTab; /* array of unparsed text nodes */
William M. Brack72ee48d2003-12-30 08:30:19 +000073 xmlURL *txturlTab; /* array of unparsed text URLs */
Daniel Veillardd581b7e2003-02-11 18:03:05 +000074
Daniel Veillardf4b4f982003-02-13 11:02:08 +000075 xmlChar * url; /* the current URL processed */
William M. Brack72ee48d2003-12-30 08:30:19 +000076 int urlNr; /* number of URLs stacked */
77 int urlMax; /* size of URL stack */
78 xmlChar * *urlTab; /* URL stack */
Daniel Veillardf4b4f982003-02-13 11:02:08 +000079
Daniel Veillardd581b7e2003-02-11 18:03:05 +000080 int nbErrors; /* the number of errors detected */
Daniel Veillardb5fa0202003-12-08 17:41:29 +000081 int legacy; /* using XINCLUDE_OLD_NS */
Daniel Veillarde74d2e12003-12-09 11:35:37 +000082 int parseFlags; /* the flags used for parsing XML documents */
Owen Taylor3473f882001-02-23 17:55:21 +000083};
84
Daniel Veillardd16df9f2001-05-23 13:44:21 +000085static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +000086xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree);
Owen Taylor3473f882001-02-23 17:55:21 +000087
Daniel Veillard98485322003-08-14 15:44:40 +000088
Daniel Veillardcd6ff282003-10-08 22:38:13 +000089/************************************************************************
90 * *
Daniel Veillard69d2c172003-10-09 11:46:07 +000091 * XInclude error handler *
Daniel Veillardcd6ff282003-10-08 22:38:13 +000092 * *
93 ************************************************************************/
94
Daniel Veillard98485322003-08-14 15:44:40 +000095/**
Daniel Veillardcd6ff282003-10-08 22:38:13 +000096 * xmlXIncludeErrMemory:
William M. Brack72ee48d2003-12-30 08:30:19 +000097 * @extra: extra information
Daniel Veillard98485322003-08-14 15:44:40 +000098 *
Daniel Veillardcd6ff282003-10-08 22:38:13 +000099 * Handle an out of memory condition
Daniel Veillard98485322003-08-14 15:44:40 +0000100 */
101static void
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000102xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
103 const char *extra)
Daniel Veillard98485322003-08-14 15:44:40 +0000104{
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000105 if (ctxt != NULL)
106 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000107 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000108 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
109 extra, NULL, NULL, 0, 0,
110 "Memory allocation failed : %s\n", extra);
111}
Daniel Veillard98485322003-08-14 15:44:40 +0000112
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000113/**
114 * xmlXIncludeErr:
115 * @ctxt: the XInclude context
116 * @node: the context node
117 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000118 * @extra: extra information
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000119 *
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000120 * Handle an XInclude error
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000121 */
122static void
123xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
124 const char *msg, const xmlChar *extra)
125{
126 if (ctxt != NULL)
127 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000128 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000129 error, XML_ERR_ERROR, NULL, 0,
130 (const char *) extra, NULL, NULL, 0, 0,
131 msg, (const char *) extra);
Daniel Veillard98485322003-08-14 15:44:40 +0000132}
133
Owen Taylor3473f882001-02-23 17:55:21 +0000134/**
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000135 * xmlXIncludeWarn:
136 * @ctxt: the XInclude context
137 * @node: the context node
138 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000139 * @extra: extra information
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000140 *
141 * Emit an XInclude warning.
142 */
143static void
144xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
145 const char *msg, const xmlChar *extra)
146{
147 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
148 error, XML_ERR_WARNING, NULL, 0,
149 (const char *) extra, NULL, NULL, 0, 0,
150 msg, (const char *) extra);
151}
152
153/**
154 * xmlXIncludeGetProp:
155 * @ctxt: the XInclude context
156 * @cur: the node
157 * @name: the attribute name
158 *
159 * Get an XInclude attribute
160 *
161 * Returns the value (to be freed) or NULL if not found
162 */
163static xmlChar *
164xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
165 const xmlChar *name) {
166 xmlChar *ret;
167
168 ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
169 if (ret != NULL)
170 return(ret);
171 if (ctxt->legacy != 0) {
172 ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
173 if (ret != NULL)
174 return(ret);
175 }
176 ret = xmlGetProp(cur, name);
177 return(ret);
178}
179/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000180 * xmlXIncludeFreeRef:
181 * @ref: the XInclude reference
182 *
183 * Free an XInclude reference
184 */
185static void
186xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
187 if (ref == NULL)
188 return;
189#ifdef DEBUG_XINCLUDE
190 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
191#endif
192 if (ref->doc != NULL) {
193#ifdef DEBUG_XINCLUDE
194 xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
195#endif
196 xmlFreeDoc(ref->doc);
197 }
198 if (ref->URI != NULL)
199 xmlFree(ref->URI);
200 if (ref->fragment != NULL)
201 xmlFree(ref->fragment);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000202 if (ref->xptr != NULL)
203 xmlXPathFreeObject(ref->xptr);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000204 xmlFree(ref);
205}
206
207/**
208 * xmlXIncludeNewRef:
209 * @ctxt: the XInclude context
210 * @URI: the resource URI
211 *
212 * Creates a new reference within an XInclude context
213 *
214 * Returns the new set
215 */
216static xmlXIncludeRefPtr
217xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
218 xmlNodePtr ref) {
219 xmlXIncludeRefPtr ret;
220
221#ifdef DEBUG_XINCLUDE
222 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
223#endif
224 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000225 if (ret == NULL) {
226 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000227 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000228 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000229 memset(ret, 0, sizeof(xmlXIncludeRef));
230 if (URI == NULL)
231 ret->URI = NULL;
232 else
233 ret->URI = xmlStrdup(URI);
234 ret->fragment = NULL;
235 ret->ref = ref;
236 ret->doc = 0;
237 ret->count = 0;
238 ret->xml = 0;
239 ret->inc = NULL;
240 if (ctxt->incMax == 0) {
241 ctxt->incMax = 4;
242 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
243 sizeof(ctxt->incTab[0]));
244 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000245 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000246 xmlXIncludeFreeRef(ret);
247 return(NULL);
248 }
249 }
250 if (ctxt->incNr >= ctxt->incMax) {
251 ctxt->incMax *= 2;
252 ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
253 ctxt->incMax * sizeof(ctxt->incTab[0]));
254 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000255 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000256 xmlXIncludeFreeRef(ret);
257 return(NULL);
258 }
259 }
260 ctxt->incTab[ctxt->incNr++] = ret;
261 return(ret);
262}
263
264/**
Owen Taylor3473f882001-02-23 17:55:21 +0000265 * xmlXIncludeNewContext:
266 * @doc: an XML Document
267 *
268 * Creates a new XInclude context
269 *
270 * Returns the new set
271 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000272xmlXIncludeCtxtPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000273xmlXIncludeNewContext(xmlDocPtr doc) {
274 xmlXIncludeCtxtPtr ret;
275
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000276#ifdef DEBUG_XINCLUDE
277 xmlGenericError(xmlGenericErrorContext, "New context\n");
278#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000279 if (doc == NULL)
280 return(NULL);
281 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000282 if (ret == NULL) {
283 xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
284 "creating XInclude context");
Owen Taylor3473f882001-02-23 17:55:21 +0000285 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000286 }
Owen Taylor3473f882001-02-23 17:55:21 +0000287 memset(ret, 0, sizeof(xmlXIncludeCtxt));
288 ret->doc = doc;
289 ret->incNr = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000290 ret->incBase = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000291 ret->incMax = 0;
292 ret->incTab = NULL;
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000293 ret->nbErrors = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000294 return(ret);
295}
296
297/**
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000298 * xmlXIncludeURLPush:
299 * @ctxt: the parser context
300 * @value: the url
301 *
302 * Pushes a new url on top of the url stack
303 *
304 * Returns -1 in case of error, the index in the stack otherwise
305 */
306static int
307xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
308 const xmlChar *value)
309{
310 if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000311 xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION,
312 "detected a recursion in %s\n", value);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000313 return(-1);
314 }
315 if (ctxt->urlTab == NULL) {
316 ctxt->urlMax = 4;
317 ctxt->urlNr = 0;
318 ctxt->urlTab = (xmlChar * *) xmlMalloc(
319 ctxt->urlMax * sizeof(ctxt->urlTab[0]));
320 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000321 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000322 return (-1);
323 }
324 }
325 if (ctxt->urlNr >= ctxt->urlMax) {
326 ctxt->urlMax *= 2;
327 ctxt->urlTab =
328 (xmlChar * *) xmlRealloc(ctxt->urlTab,
329 ctxt->urlMax *
330 sizeof(ctxt->urlTab[0]));
331 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000332 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000333 return (-1);
334 }
335 }
336 ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
337 return (ctxt->urlNr++);
338}
339
340/**
341 * xmlXIncludeURLPop:
342 * @ctxt: the parser context
343 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000344 * Pops the top URL from the URL stack
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000345 */
346static void
347xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
348{
349 xmlChar * ret;
350
351 if (ctxt->urlNr <= 0)
352 return;
353 ctxt->urlNr--;
354 if (ctxt->urlNr > 0)
355 ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
356 else
357 ctxt->url = NULL;
358 ret = ctxt->urlTab[ctxt->urlNr];
359 ctxt->urlTab[ctxt->urlNr] = 0;
360 if (ret != NULL)
361 xmlFree(ret);
362}
363
364/**
Owen Taylor3473f882001-02-23 17:55:21 +0000365 * xmlXIncludeFreeContext:
366 * @ctxt: the XInclude context
367 *
368 * Free an XInclude context
369 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000370void
Owen Taylor3473f882001-02-23 17:55:21 +0000371xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
372 int i;
373
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000374#ifdef DEBUG_XINCLUDE
375 xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
376#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000377 if (ctxt == NULL)
378 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000379 while (ctxt->urlNr > 0)
380 xmlXIncludeURLPop(ctxt);
381 if (ctxt->urlTab != NULL)
382 xmlFree(ctxt->urlTab);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000383 for (i = 0;i < ctxt->incNr;i++) {
384 if (ctxt->incTab[i] != NULL)
385 xmlXIncludeFreeRef(ctxt->incTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +0000386 }
387 for (i = 0;i < ctxt->txtNr;i++) {
388 if (ctxt->txturlTab[i] != NULL)
389 xmlFree(ctxt->txturlTab[i]);
390 }
391 if (ctxt->incTab != NULL)
392 xmlFree(ctxt->incTab);
Owen Taylor3473f882001-02-23 17:55:21 +0000393 if (ctxt->txtTab != NULL)
394 xmlFree(ctxt->txtTab);
395 if (ctxt->txturlTab != NULL)
396 xmlFree(ctxt->txturlTab);
Owen Taylor3473f882001-02-23 17:55:21 +0000397 xmlFree(ctxt);
398}
399
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000400/**
Daniel Veillard98485322003-08-14 15:44:40 +0000401 * xmlXIncludeParseFile:
402 * @ctxt: the XInclude context
403 * @URL: the URL or file path
404 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000405 * parse a document for XInclude
Daniel Veillard98485322003-08-14 15:44:40 +0000406 */
407static xmlDocPtr
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000408xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
Daniel Veillard98485322003-08-14 15:44:40 +0000409 xmlDocPtr ret;
410 xmlParserCtxtPtr pctxt;
411 char *directory = NULL;
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000412 xmlParserInputPtr inputStream;
Daniel Veillard98485322003-08-14 15:44:40 +0000413
414 xmlInitParser();
415
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000416 pctxt = xmlNewParserCtxt();
Daniel Veillard98485322003-08-14 15:44:40 +0000417 if (pctxt == NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000418 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
Daniel Veillard98485322003-08-14 15:44:40 +0000419 return(NULL);
420 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000421 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000422 * try to ensure that new documents included are actually
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000423 * built with the same dictionary as the including document.
424 */
425 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL) &&
426 (pctxt->dict != NULL)) {
427 xmlDictFree(pctxt->dict);
428 pctxt->dict = ctxt->doc->dict;
429 xmlDictReference(pctxt->dict);
430 }
431
432 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
433
434 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
435 if (inputStream == NULL) {
436 xmlFreeParserCtxt(pctxt);
437 return(NULL);
438 }
439
440 inputPush(pctxt, inputStream);
Daniel Veillard98485322003-08-14 15:44:40 +0000441
442 if ((pctxt->directory == NULL) && (directory == NULL))
443 directory = xmlParserGetDirectory(URL);
444 if ((pctxt->directory == NULL) && (directory != NULL))
445 pctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
446
447 pctxt->loadsubset = XML_DETECT_IDS;
448
449 xmlParseDocument(pctxt);
450
William M. Brack1ff42132003-12-31 14:05:15 +0000451 if (pctxt->wellFormed) {
Daniel Veillard98485322003-08-14 15:44:40 +0000452 ret = pctxt->myDoc;
William M. Brack1ff42132003-12-31 14:05:15 +0000453 xmlDictReference(pctxt->dict);
454 }
Daniel Veillard98485322003-08-14 15:44:40 +0000455 else {
456 ret = NULL;
Daniel Veillard4773df22004-01-23 13:15:13 +0000457 if (pctxt->myDoc != NULL) {
458 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL) &&
459 (pctxt->myDoc->dict == ctxt->doc->dict))
460 xmlDictReference(ctxt->doc->dict);
William M. Brackb2d25dd2004-02-04 00:51:21 +0000461 else if ((pctxt->dict != NULL) &&
462 (pctxt->dict == pctxt->myDoc->dict))
463 xmlDictReference(pctxt->dict);
Daniel Veillard4773df22004-01-23 13:15:13 +0000464 xmlFreeDoc(pctxt->myDoc);
465 }
Daniel Veillard98485322003-08-14 15:44:40 +0000466 pctxt->myDoc = NULL;
467 }
468 xmlFreeParserCtxt(pctxt);
469
470 return(ret);
471}
472
473/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000474 * xmlXIncludeAddNode:
475 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000476 * @cur: the new node
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000477 *
478 * Add a new node to process to an XInclude context
479 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000480static int
481xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
482 xmlXIncludeRefPtr ref;
483 xmlURIPtr uri;
484 xmlChar *URL;
485 xmlChar *fragment = NULL;
486 xmlChar *href;
487 xmlChar *parse;
488 xmlChar *base;
489 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000490 int xml = 1, i; /* default Issue 64 */
491 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000492
493
494 if (ctxt == NULL)
495 return(-1);
496 if (cur == NULL)
497 return(-1);
498
499#ifdef DEBUG_XINCLUDE
500 xmlGenericError(xmlGenericErrorContext, "Add node\n");
501#endif
502 /*
503 * read the attributes
504 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000505 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000506 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000507 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
508 if (href == NULL)
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000509 return(-1);
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000510 local = 1;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000511 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000512 if (href[0] == '#')
513 local = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000514 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000515 if (parse != NULL) {
516 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
517 xml = 1;
518 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
519 xml = 0;
520 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000521 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
522 "invalid value %s for 'parse'\n", parse);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000523 if (href != NULL)
524 xmlFree(href);
525 if (parse != NULL)
526 xmlFree(parse);
527 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000528 }
529 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000530
531 /*
532 * compute the URI
533 */
534 base = xmlNodeGetBase(ctxt->doc, cur);
535 if (base == NULL) {
536 URI = xmlBuildURI(href, ctxt->doc->URL);
537 } else {
538 URI = xmlBuildURI(href, base);
539 }
540 if (URI == NULL) {
541 xmlChar *escbase;
542 xmlChar *eschref;
543 /*
544 * Some escaping may be needed
545 */
546 escbase = xmlURIEscape(base);
547 eschref = xmlURIEscape(href);
548 URI = xmlBuildURI(eschref, escbase);
549 if (escbase != NULL)
550 xmlFree(escbase);
551 if (eschref != NULL)
552 xmlFree(eschref);
553 }
554 if (parse != NULL)
555 xmlFree(parse);
556 if (href != NULL)
557 xmlFree(href);
558 if (base != NULL)
559 xmlFree(base);
560 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000561 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
562 "failed build URL\n", NULL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000563 return(-1);
564 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000565 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000566
567 /*
568 * Check the URL and remove any fragment identifier
569 */
570 uri = xmlParseURI((const char *)URI);
571 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000572 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
573 "invalid value URI %s\n", URI);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000574 if (fragment != NULL)
575 xmlFree(fragment);
576 xmlFree(URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000577 return(-1);
578 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000579
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000580 if (uri->fragment != NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000581 if (ctxt->legacy != 0) {
582 if (fragment == NULL) {
583 fragment = (xmlChar *) uri->fragment;
584 } else {
585 xmlFree(uri->fragment);
586 }
587 } else {
588 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
589 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
590 URI);
591 if (fragment != NULL)
592 xmlFree(fragment);
593 xmlFreeURI(uri);
594 xmlFree(URI);
595 return(-1);
596 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000597 uri->fragment = NULL;
598 }
599 URL = xmlSaveUri(uri);
600 xmlFreeURI(uri);
601 xmlFree(URI);
602 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000603 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
604 "invalid value URI %s\n", URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000605 if (fragment != NULL)
606 xmlFree(fragment);
607 return(-1);
608 }
609
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000610 /*
611 * Check the URL against the stack for recursions
612 */
613 if (!local) {
614 for (i = 0;i < ctxt->urlNr;i++) {
615 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000616 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
617 "detected a recursion in %s\n", URL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000618 return(-1);
619 }
620 }
621 }
622
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000623 ref = xmlXIncludeNewRef(ctxt, URL, cur);
624 if (ref == NULL) {
625 return(-1);
626 }
627 ref->fragment = fragment;
628 ref->doc = NULL;
629 ref->xml = xml;
630 ref->count = 1;
631 xmlFree(URL);
632 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000633}
634
635/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000636 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000637 * @ctxt: the XInclude context
638 * @doc: the new document
639 * @url: the associated URL
640 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000641 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000642 */
643static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000644xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000645 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000646 xmlXIncludeCtxtPtr newctxt;
647 int i;
648
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000649 /*
650 * Avoid recursion in already substitued resources
651 for (i = 0;i < ctxt->urlNr;i++) {
652 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
653 return;
654 }
655 */
656
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000657#ifdef DEBUG_XINCLUDE
658 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
659#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000660 /*
661 * Handle recursion here.
662 */
663
664 newctxt = xmlXIncludeNewContext(doc);
665 if (newctxt != NULL) {
666 /*
667 * Copy the existing document set
668 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000669 newctxt->incMax = ctxt->incMax;
670 newctxt->incNr = ctxt->incNr;
671 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
672 sizeof(newctxt->incTab[0]));
673 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000674 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000675 xmlFree(newctxt);
676 return;
677 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000678 /*
679 * copy the urlTab
680 */
681 newctxt->urlMax = ctxt->urlMax;
682 newctxt->urlNr = ctxt->urlNr;
683 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000684
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000685 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000686 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000687 */
688 newctxt->incBase = ctxt->incNr;
689 for (i = 0;i < ctxt->incNr;i++) {
690 newctxt->incTab[i] = ctxt->incTab[i];
691 newctxt->incTab[i]->count++; /* prevent the recursion from
692 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000693 }
Daniel Veillard8edf1c52003-07-22 20:52:14 +0000694 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000695 for (i = 0;i < ctxt->incNr;i++) {
696 newctxt->incTab[i]->count--;
697 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000698 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000699
700 /* urlTab may have been reallocated */
701 ctxt->urlTab = newctxt->urlTab;
702 ctxt->urlMax = newctxt->urlMax;
703
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000704 newctxt->urlMax = 0;
705 newctxt->urlNr = 0;
706 newctxt->urlTab = NULL;
707
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000708 xmlXIncludeFreeContext(newctxt);
709 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000710#ifdef DEBUG_XINCLUDE
711 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
712#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000713}
714
715/**
716 * xmlXIncludeAddTxt:
717 * @ctxt: the XInclude context
718 * @txt: the new text node
719 * @url: the associated URL
720 *
721 * Add a new txtument to the list
722 */
723static void
724xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000725#ifdef DEBUG_XINCLUDE
726 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
727#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000728 if (ctxt->txtMax == 0) {
729 ctxt->txtMax = 4;
730 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
731 sizeof(ctxt->txtTab[0]));
732 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000733 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000734 return;
735 }
736 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
737 sizeof(ctxt->txturlTab[0]));
738 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000739 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000740 return;
741 }
742 }
743 if (ctxt->txtNr >= ctxt->txtMax) {
744 ctxt->txtMax *= 2;
745 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
746 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
747 if (ctxt->txtTab == 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 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000752 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000753 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000754 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000755 return;
756 }
757 }
758 ctxt->txtTab[ctxt->txtNr] = txt;
759 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
760 ctxt->txtNr++;
761}
762
Owen Taylor3473f882001-02-23 17:55:21 +0000763/************************************************************************
764 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000765 * Node copy with specific semantic *
766 * *
767 ************************************************************************/
768
769/**
770 * xmlXIncludeCopyNode:
771 * @ctxt: the XInclude context
772 * @target: the document target
773 * @source: the document source
774 * @elem: the element
775 *
776 * Make a copy of the node while preserving the XInclude semantic
777 * of the Infoset copy
778 */
779static xmlNodePtr
780xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
781 xmlDocPtr source, xmlNodePtr elem) {
782 xmlNodePtr result = NULL;
783
784 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
785 (elem == NULL))
786 return(NULL);
787 if (elem->type == XML_DTD_NODE)
788 return(NULL);
789 result = xmlDocCopyNode(elem, target, 1);
790 return(result);
791}
792
793/**
794 * xmlXIncludeCopyNodeList:
795 * @ctxt: the XInclude context
796 * @target: the document target
797 * @source: the document source
798 * @elem: the element list
799 *
800 * Make a copy of the node list while preserving the XInclude semantic
801 * of the Infoset copy
802 */
803static xmlNodePtr
804xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
805 xmlDocPtr source, xmlNodePtr elem) {
806 xmlNodePtr cur, res, result = NULL, last = NULL;
807
808 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
809 (elem == NULL))
810 return(NULL);
811 cur = elem;
812 while (cur != NULL) {
813 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
814 if (res != NULL) {
815 if (result == NULL) {
816 result = last = res;
817 } else {
818 last->next = res;
819 res->prev = last;
820 last = res;
821 }
822 }
823 cur = cur->next;
824 }
825 return(result);
826}
827
828/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000829 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000830 * @cur: the node
831 * @no: the child number
832 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000833 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000834 */
835static xmlNodePtr
836xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
837 int i;
838 if (cur == NULL)
839 return(cur);
840 cur = cur->children;
841 for (i = 0;i <= no;cur = cur->next) {
842 if (cur == NULL)
843 return(cur);
844 if ((cur->type == XML_ELEMENT_NODE) ||
845 (cur->type == XML_DOCUMENT_NODE) ||
846 (cur->type == XML_HTML_DOCUMENT_NODE)) {
847 i++;
848 if (i == no)
849 break;
850 }
851 }
852 return(cur);
853}
854
William M. Brackf7eb7942003-12-31 07:59:17 +0000855xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000856/**
857 * xmlXIncludeCopyRange:
858 * @ctxt: the XInclude context
859 * @target: the document target
860 * @source: the document source
861 * @obj: the XPointer result from the evaluation.
862 *
863 * Build a node list tree copy of the XPointer result.
864 *
865 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000866 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000867 */
868static xmlNodePtr
869xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
870 xmlDocPtr source, xmlXPathObjectPtr range) {
871 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000872 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
873 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000874 /* pointers to traversal nodes */
875 xmlNodePtr start, cur, end;
876 int index1, index2;
William M. Brack6bdacd72004-02-07 08:53:23 +0000877 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000878
879 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
880 (range == NULL))
881 return(NULL);
882 if (range->type != XPATH_RANGE)
883 return(NULL);
884 start = (xmlNodePtr) range->user;
885
886 if (start == NULL)
887 return(NULL);
888 end = range->user2;
889 if (end == NULL)
890 return(xmlDocCopyNode(start, target, 1));
891
892 cur = start;
893 index1 = range->index;
894 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000895 /*
896 * level is depth of the current node under consideration
897 * list is the pointer to the root of the output tree
898 * listParent is a pointer to the parent of output tree (within
899 the included file) in case we need to add another level
900 * last is a pointer to the last node added to the output tree
901 * lastLevel is the depth of last (relative to the root)
902 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000903 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000904 /*
905 * Check if our output tree needs a parent
906 */
907 if (level < 0) {
908 while (level < 0) {
909 tmp2 = xmlDocCopyNode(listParent, target, 0);
910 xmlAddChild(tmp2, list);
911 list = tmp2;
912 listParent = listParent->parent;
913 level++;
914 }
915 last = list;
916 lastLevel = 0;
917 }
918 /*
919 * Check whether we need to change our insertion point
920 */
921 while (level < lastLevel) {
922 last = last->parent;
923 lastLevel --;
924 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000925 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000926 if (cur->type == XML_TEXT_NODE) {
927 const xmlChar *content = cur->content;
928 int len;
929
930 if (content == NULL) {
931 tmp = xmlNewTextLen(NULL, 0);
932 } else {
933 len = index2;
934 if ((cur == start) && (index1 > 1)) {
935 content += (index1 - 1);
936 len -= (index1 - 1);
937 index1 = 0;
938 } else {
939 len = index2;
940 }
941 tmp = xmlNewTextLen(content, len);
942 }
943 /* single sub text node selection */
944 if (list == NULL)
945 return(tmp);
946 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +0000947 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000948 xmlAddNextSibling(last, tmp);
949 else
William M. Brackf7eb7942003-12-31 07:59:17 +0000950 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000951 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +0000952 } else { /* ending node not a text node */
William M. Brack6bdacd72004-02-07 08:53:23 +0000953 endLevel = level; /* remember the level of the end node */
954 endFlag = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000955 tmp = xmlDocCopyNode(cur, target, 0);
William M. Brackf7eb7942003-12-31 07:59:17 +0000956 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000957 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000958 listParent = cur->parent;
959 } else {
960 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000961 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +0000962 else {
963 xmlAddChild(last, tmp);
964 lastLevel = level;
965 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000966 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000967 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000968
969 if (index2 > 1) {
970 end = xmlXIncludeGetNthChild(cur, index2 - 1);
971 index2 = 0;
972 }
973 if ((cur == start) && (index1 > 1)) {
974 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
975 index1 = 0;
William M. Brack6bdacd72004-02-07 08:53:23 +0000976 } else {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000977 cur = cur->children;
978 }
William M. Brack6bdacd72004-02-07 08:53:23 +0000979 level++; /* increment level to show change */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000980 /*
981 * Now gather the remaining nodes from cur to end
982 */
William M. Brack6bdacd72004-02-07 08:53:23 +0000983 continue; /* while */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000984 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000985 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000986 if ((cur->type == XML_TEXT_NODE) ||
987 (cur->type == XML_CDATA_SECTION_NODE)) {
988 const xmlChar *content = cur->content;
989
990 if (content == NULL) {
991 tmp = xmlNewTextLen(NULL, 0);
992 } else {
993 if (index1 > 1) {
994 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +0000995 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000996 }
997 tmp = xmlNewText(content);
998 }
999 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001000 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001001 } else { /* Not text node */
William M. Brackf7eb7942003-12-31 07:59:17 +00001002 tmp = xmlDocCopyNode(cur, target, 0);
1003 list = last = tmp;
1004 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001005 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001006 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001007 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001008 index1 = 0;
1009 /*
1010 * Now gather the remaining nodes from cur to end
1011 */
1012 continue; /* while */
1013 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001014 }
1015 } else {
1016 tmp = NULL;
1017 switch (cur->type) {
1018 case XML_DTD_NODE:
1019 case XML_ELEMENT_DECL:
1020 case XML_ATTRIBUTE_DECL:
1021 case XML_ENTITY_NODE:
1022 /* Do not copy DTD informations */
1023 break;
1024 case XML_ENTITY_DECL:
1025 /* handle crossing entities -> stack needed */
1026 break;
1027 case XML_XINCLUDE_START:
1028 case XML_XINCLUDE_END:
1029 /* don't consider it part of the tree content */
1030 break;
1031 case XML_ATTRIBUTE_NODE:
1032 /* Humm, should not happen ! */
1033 break;
1034 default:
William M. Brack72ee48d2003-12-30 08:30:19 +00001035 tmp = xmlDocCopyNode(cur, target, 0);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001036 break;
1037 }
1038 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001039 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001040 xmlAddNextSibling(last, tmp);
1041 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001042 xmlAddChild(last, tmp);
1043 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001044 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001045 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001046 }
1047 }
1048 /*
1049 * Skip to next node in document order
1050 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001051 cur = xmlXPtrAdvanceNode(cur, &level);
William M. Brack6bdacd72004-02-07 08:53:23 +00001052 if (endFlag && (level >= endLevel))
1053 break;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001054 }
1055 return(list);
1056}
1057
1058/**
1059 * xmlXIncludeBuildNodeList:
1060 * @ctxt: the XInclude context
1061 * @target: the document target
1062 * @source: the document source
1063 * @obj: the XPointer result from the evaluation.
1064 *
1065 * Build a node list tree copy of the XPointer result.
1066 * This will drop Attributes and Namespace declarations.
1067 *
1068 * Returns an xmlNodePtr list or NULL.
1069 * the caller has to free the node tree.
1070 */
1071static xmlNodePtr
1072xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1073 xmlDocPtr source, xmlXPathObjectPtr obj) {
1074 xmlNodePtr list = NULL, last = NULL;
1075 int i;
1076
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001077 if (source == NULL)
1078 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001079 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1080 (obj == NULL))
1081 return(NULL);
1082 switch (obj->type) {
1083 case XPATH_NODESET: {
1084 xmlNodeSetPtr set = obj->nodesetval;
1085 if (set == NULL)
1086 return(NULL);
1087 for (i = 0;i < set->nodeNr;i++) {
1088 if (set->nodeTab[i] == NULL)
1089 continue;
1090 switch (set->nodeTab[i]->type) {
1091 case XML_TEXT_NODE:
1092 case XML_CDATA_SECTION_NODE:
1093 case XML_ELEMENT_NODE:
1094 case XML_ENTITY_REF_NODE:
1095 case XML_ENTITY_NODE:
1096 case XML_PI_NODE:
1097 case XML_COMMENT_NODE:
1098 case XML_DOCUMENT_NODE:
1099 case XML_HTML_DOCUMENT_NODE:
1100#ifdef LIBXML_DOCB_ENABLED
1101 case XML_DOCB_DOCUMENT_NODE:
1102#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001103 case XML_XINCLUDE_END:
1104 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001105 case XML_XINCLUDE_START: {
1106 xmlNodePtr tmp, cur = set->nodeTab[i];
1107
1108 cur = cur->next;
1109 while (cur != NULL) {
1110 switch(cur->type) {
1111 case XML_TEXT_NODE:
1112 case XML_CDATA_SECTION_NODE:
1113 case XML_ELEMENT_NODE:
1114 case XML_ENTITY_REF_NODE:
1115 case XML_ENTITY_NODE:
1116 case XML_PI_NODE:
1117 case XML_COMMENT_NODE:
1118 tmp = xmlXIncludeCopyNode(ctxt, target,
1119 source, cur);
1120 if (last == NULL) {
1121 list = last = tmp;
1122 } else {
1123 xmlAddNextSibling(last, tmp);
1124 last = tmp;
1125 }
1126 cur = cur->next;
1127 continue;
1128 default:
1129 break;
1130 }
1131 break;
1132 }
1133 continue;
1134 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001135 case XML_ATTRIBUTE_NODE:
1136 case XML_NAMESPACE_DECL:
1137 case XML_DOCUMENT_TYPE_NODE:
1138 case XML_DOCUMENT_FRAG_NODE:
1139 case XML_NOTATION_NODE:
1140 case XML_DTD_NODE:
1141 case XML_ELEMENT_DECL:
1142 case XML_ATTRIBUTE_DECL:
1143 case XML_ENTITY_DECL:
1144 continue; /* for */
1145 }
1146 if (last == NULL)
1147 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1148 set->nodeTab[i]);
1149 else {
1150 xmlAddNextSibling(last,
1151 xmlXIncludeCopyNode(ctxt, target, source,
1152 set->nodeTab[i]));
1153 if (last->next != NULL)
1154 last = last->next;
1155 }
1156 }
1157 break;
1158 }
1159 case XPATH_LOCATIONSET: {
1160 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1161 if (set == NULL)
1162 return(NULL);
1163 for (i = 0;i < set->locNr;i++) {
1164 if (last == NULL)
1165 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1166 set->locTab[i]);
1167 else
1168 xmlAddNextSibling(last,
1169 xmlXIncludeCopyXPointer(ctxt, target, source,
1170 set->locTab[i]));
1171 if (last != NULL) {
1172 while (last->next != NULL)
1173 last = last->next;
1174 }
1175 }
1176 break;
1177 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001178#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001179 case XPATH_RANGE:
1180 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001181#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001182 case XPATH_POINT:
1183 /* points are ignored in XInclude */
1184 break;
1185 default:
1186 break;
1187 }
1188 return(list);
1189}
1190/************************************************************************
1191 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001192 * XInclude I/O handling *
1193 * *
1194 ************************************************************************/
1195
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001196typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1197typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1198struct _xmlXIncludeMergeData {
1199 xmlDocPtr doc;
1200 xmlXIncludeCtxtPtr ctxt;
1201};
1202
Owen Taylor3473f882001-02-23 17:55:21 +00001203/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001204 * xmlXIncludeMergeOneEntity:
1205 * @ent: the entity
1206 * @doc: the including doc
1207 * @nr: the entity name
1208 *
1209 * Inplements the merge of one entity
1210 */
1211static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001212xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001213 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001214 xmlEntityPtr ret, prev;
1215 xmlDocPtr doc;
1216 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001217
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001218 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001219 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001220 ctxt = data->ctxt;
1221 doc = data->doc;
1222 if ((ctxt == NULL) || (doc == NULL))
1223 return;
1224 switch (ent->etype) {
1225 case XML_INTERNAL_PARAMETER_ENTITY:
1226 case XML_EXTERNAL_PARAMETER_ENTITY:
1227 case XML_INTERNAL_PREDEFINED_ENTITY:
1228 return;
1229 case XML_INTERNAL_GENERAL_ENTITY:
1230 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1231 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1232 break;
1233 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001234 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1235 ent->SystemID, ent->content);
1236 if (ret != NULL) {
1237 if (ent->URI != NULL)
1238 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001239 } else {
1240 prev = xmlGetDocEntity(doc, ent->name);
1241 if (prev != NULL) {
1242 if (ent->etype != prev->etype)
1243 goto error;
1244
1245 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1246 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1247 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001248 } else if ((ent->ExternalID != NULL) &&
1249 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001250 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1251 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001252 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1253 if (!xmlStrEqual(ent->content, prev->content))
1254 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001255 } else {
1256 goto error;
1257 }
1258
1259 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001260 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001261 return;
1262error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001263 switch (ent->etype) {
1264 case XML_INTERNAL_PARAMETER_ENTITY:
1265 case XML_EXTERNAL_PARAMETER_ENTITY:
1266 case XML_INTERNAL_PREDEFINED_ENTITY:
1267 case XML_INTERNAL_GENERAL_ENTITY:
1268 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1269 return;
1270 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1271 break;
1272 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001273 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1274 "mismatch in redefinition of entity %s\n",
1275 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001276}
1277
1278/**
1279 * xmlXIncludeMergeEntities:
1280 * @ctxt: an XInclude context
1281 * @doc: the including doc
1282 * @from: the included doc
1283 *
1284 * Inplements the entity merge
1285 *
1286 * Returns 0 if merge succeeded, -1 if some processing failed
1287 */
1288static int
1289xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1290 xmlDocPtr from) {
1291 xmlNodePtr cur;
1292 xmlDtdPtr target, source;
1293
1294 if (ctxt == NULL)
1295 return(-1);
1296
1297 if ((from == NULL) || (from->intSubset == NULL))
1298 return(0);
1299
1300 target = doc->intSubset;
1301 if (target == NULL) {
1302 cur = xmlDocGetRootElement(doc);
1303 if (cur == NULL)
1304 return(-1);
1305 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1306 if (target == NULL)
1307 return(-1);
1308 }
1309
1310 source = from->intSubset;
1311 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001312 xmlXIncludeMergeData data;
1313
1314 data.ctxt = ctxt;
1315 data.doc = doc;
1316
Daniel Veillard4287c572003-02-04 22:48:53 +00001317 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001318 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001319 }
1320 source = from->extSubset;
1321 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001322 xmlXIncludeMergeData data;
1323
1324 data.ctxt = ctxt;
1325 data.doc = doc;
1326
Daniel Veillard4287c572003-02-04 22:48:53 +00001327 /*
1328 * don't duplicate existing stuff when external subsets are the same
1329 */
1330 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1331 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1332 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001333 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001334 }
1335 }
1336 return(0);
1337}
1338
1339/**
Owen Taylor3473f882001-02-23 17:55:21 +00001340 * xmlXIncludeLoadDoc:
1341 * @ctxt: the XInclude context
1342 * @url: the associated URL
1343 * @nr: the xinclude node number
1344 *
1345 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001346 *
1347 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001348 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001349static int
Owen Taylor3473f882001-02-23 17:55:21 +00001350xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1351 xmlDocPtr doc;
1352 xmlURIPtr uri;
1353 xmlChar *URL;
1354 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001355 int i = 0;
1356
1357#ifdef DEBUG_XINCLUDE
1358 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1359#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001360 /*
1361 * Check the URL and remove any fragment identifier
1362 */
1363 uri = xmlParseURI((const char *)url);
1364 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001365 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1366 XML_XINCLUDE_HREF_URI,
1367 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001368 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001369 }
1370 if (uri->fragment != NULL) {
1371 fragment = (xmlChar *) uri->fragment;
1372 uri->fragment = NULL;
1373 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001374 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1375 (ctxt->incTab[nr]->fragment != NULL)) {
1376 if (fragment != NULL) xmlFree(fragment);
1377 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1378 }
Owen Taylor3473f882001-02-23 17:55:21 +00001379 URL = xmlSaveUri(uri);
1380 xmlFreeURI(uri);
1381 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001382 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1383 XML_XINCLUDE_HREF_URI,
1384 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001385 if (fragment != NULL)
1386 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001387 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001388 }
1389
1390 /*
1391 * Handling of references to the local document are done
1392 * directly through ctxt->doc.
1393 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001394 if ((URL[0] == 0) || (URL[0] == '#') ||
1395 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001396 doc = NULL;
1397 goto loaded;
1398 }
1399
1400 /*
1401 * Prevent reloading twice the document.
1402 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001403 for (i = 0; i < ctxt->incNr; i++) {
1404 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1405 (ctxt->incTab[i]->doc != NULL)) {
1406 doc = ctxt->incTab[i]->doc;
1407#ifdef DEBUG_XINCLUDE
1408 printf("Already loaded %s\n", URL);
1409#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001410 goto loaded;
1411 }
1412 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001413
Owen Taylor3473f882001-02-23 17:55:21 +00001414 /*
1415 * Load it.
1416 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001417#ifdef DEBUG_XINCLUDE
1418 printf("loading %s\n", URL);
1419#endif
Daniel Veillard98485322003-08-14 15:44:40 +00001420 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001421 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001422 xmlFree(URL);
1423 if (fragment != NULL)
1424 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001425 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001426 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001427 ctxt->incTab[nr]->doc = doc;
Daniel Veillard98485322003-08-14 15:44:40 +00001428 for (i = nr + 1; i < ctxt->incNr; i++) {
1429 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1430 ctxt->incTab[nr]->count++;
1431#ifdef DEBUG_XINCLUDE
1432 printf("Increasing %s count since reused\n", URL);
1433#endif
1434 break;
1435 }
1436 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001437
1438 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001439 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001440 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001441 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001442
1443 /*
1444 * We don't need the DTD anymore, free up space
1445 if (doc->intSubset != NULL) {
1446 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1447 xmlFreeNode((xmlNodePtr) doc->intSubset);
1448 doc->intSubset = NULL;
1449 }
1450 if (doc->extSubset != NULL) {
1451 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1452 xmlFreeNode((xmlNodePtr) doc->extSubset);
1453 doc->extSubset = NULL;
1454 }
1455 */
1456 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001457
1458loaded:
1459 if (fragment == NULL) {
1460 /*
1461 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001462 */
1463 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001464 {
1465 /* Hopefully a DTD declaration won't be copied from
1466 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001467 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001468 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001469 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001470 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001471 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001472 }
1473#ifdef LIBXML_XPTR_ENABLED
1474 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001475 /*
1476 * Computes the XPointer expression and make a copy used
1477 * as the replacement copy.
1478 */
1479 xmlXPathObjectPtr xptr;
1480 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001481 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001482
1483 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001484 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1485 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001486 } else {
1487 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1488 }
1489 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001490 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1491 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001492 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001493 xmlFree(URL);
1494 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001495 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001496 }
1497 xptr = xmlXPtrEval(fragment, xptrctxt);
1498 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001499 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1500 XML_XINCLUDE_XPTR_FAILED,
1501 "XPointer evaluation failed: #%s\n",
1502 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001503 xmlXPathFreeContext(xptrctxt);
1504 xmlFree(URL);
1505 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001506 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001507 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001508 switch (xptr->type) {
1509 case XPATH_UNDEFINED:
1510 case XPATH_BOOLEAN:
1511 case XPATH_NUMBER:
1512 case XPATH_STRING:
1513 case XPATH_POINT:
1514 case XPATH_USERS:
1515 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001516 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1517 XML_XINCLUDE_XPTR_RESULT,
1518 "XPointer is not a range: #%s\n",
1519 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001520 xmlXPathFreeContext(xptrctxt);
1521 xmlFree(URL);
1522 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001523 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001524 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001525 if ((xptr->nodesetval == NULL) ||
1526 (xptr->nodesetval->nodeNr <= 0)) {
1527 xmlXPathFreeContext(xptrctxt);
1528 xmlFree(URL);
1529 xmlFree(fragment);
1530 return(-1);
1531 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001532 case XPATH_RANGE:
1533 case XPATH_LOCATIONSET:
1534 break;
1535 }
1536 set = xptr->nodesetval;
1537 if (set != NULL) {
1538 for (i = 0;i < set->nodeNr;i++) {
1539 if (set->nodeTab[i] == NULL)
1540 continue;
1541 switch (set->nodeTab[i]->type) {
1542 case XML_TEXT_NODE:
1543 case XML_CDATA_SECTION_NODE:
1544 case XML_ELEMENT_NODE:
1545 case XML_ENTITY_REF_NODE:
1546 case XML_ENTITY_NODE:
1547 case XML_PI_NODE:
1548 case XML_COMMENT_NODE:
1549 case XML_DOCUMENT_NODE:
1550 case XML_HTML_DOCUMENT_NODE:
1551#ifdef LIBXML_DOCB_ENABLED
1552 case XML_DOCB_DOCUMENT_NODE:
1553#endif
1554 continue;
1555 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001556 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1557 XML_XINCLUDE_XPTR_RESULT,
1558 "XPointer selects an attribute: #%s\n",
1559 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001560 set->nodeTab[i] = NULL;
1561 continue;
1562 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001563 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1564 XML_XINCLUDE_XPTR_RESULT,
1565 "XPointer selects a namespace: #%s\n",
1566 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001567 set->nodeTab[i] = NULL;
1568 continue;
1569 case XML_DOCUMENT_TYPE_NODE:
1570 case XML_DOCUMENT_FRAG_NODE:
1571 case XML_NOTATION_NODE:
1572 case XML_DTD_NODE:
1573 case XML_ELEMENT_DECL:
1574 case XML_ATTRIBUTE_DECL:
1575 case XML_ENTITY_DECL:
1576 case XML_XINCLUDE_START:
1577 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001578 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1579 XML_XINCLUDE_XPTR_RESULT,
1580 "XPointer selects unexpected nodes: #%s\n",
1581 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001582 set->nodeTab[i] = NULL;
1583 set->nodeTab[i] = NULL;
1584 continue; /* for */
1585 }
1586 }
1587 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001588 if (doc == NULL) {
1589 ctxt->incTab[nr]->xptr = xptr;
1590 ctxt->incTab[nr]->inc = NULL;
1591 } else {
1592 ctxt->incTab[nr]->inc =
1593 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1594 xmlXPathFreeObject(xptr);
1595 }
Owen Taylor3473f882001-02-23 17:55:21 +00001596 xmlXPathFreeContext(xptrctxt);
1597 xmlFree(fragment);
1598 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001599#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001600
1601 /*
1602 * Do the xml:base fixup if needed
1603 */
1604 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1605 xmlNodePtr node;
1606
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001607 node = ctxt->incTab[nr]->inc;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001608 while (node != NULL) {
1609 if (node->type == XML_ELEMENT_NODE)
1610 xmlNodeSetBase(node, URL);
1611 node = node->next;
1612 }
1613 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001614 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1615 (ctxt->incTab[nr]->count <= 1)) {
1616#ifdef DEBUG_XINCLUDE
1617 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1618#endif
1619 xmlFreeDoc(ctxt->incTab[nr]->doc);
1620 ctxt->incTab[nr]->doc = NULL;
1621 }
Owen Taylor3473f882001-02-23 17:55:21 +00001622 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001623 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001624}
1625
1626/**
1627 * xmlXIncludeLoadTxt:
1628 * @ctxt: the XInclude context
1629 * @url: the associated URL
1630 * @nr: the xinclude node number
1631 *
1632 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001633 *
1634 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001635 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001636static int
Owen Taylor3473f882001-02-23 17:55:21 +00001637xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1638 xmlParserInputBufferPtr buf;
1639 xmlNodePtr node;
1640 xmlURIPtr uri;
1641 xmlChar *URL;
1642 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001643 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001644 xmlCharEncoding enc = (xmlCharEncoding) 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001645
Owen Taylor3473f882001-02-23 17:55:21 +00001646 /*
1647 * Check the URL and remove any fragment identifier
1648 */
1649 uri = xmlParseURI((const char *)url);
1650 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001651 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1652 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001653 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001654 }
1655 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001656 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1657 "fragment identifier forbidden for text: %s\n",
1658 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001659 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001660 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001661 }
1662 URL = xmlSaveUri(uri);
1663 xmlFreeURI(uri);
1664 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001665 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1666 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001667 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001668 }
1669
1670 /*
1671 * Handling of references to the local document are done
1672 * directly through ctxt->doc.
1673 */
1674 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001675 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1676 XML_XINCLUDE_TEXT_DOCUMENT,
1677 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001678 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001679 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001680 }
1681
1682 /*
1683 * Prevent reloading twice the document.
1684 */
1685 for (i = 0; i < ctxt->txtNr; i++) {
1686 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1687 node = xmlCopyNode(ctxt->txtTab[i], 1);
1688 goto loaded;
1689 }
1690 }
1691 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001692 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001693 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001694 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1695 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1696 }
1697 if (encoding != NULL) {
1698 /*
1699 * TODO: we should not have to remap to the xmlCharEncoding
1700 * predefined set, a better interface than
1701 * xmlParserInputBufferCreateFilename should allow any
1702 * encoding supported by iconv
1703 */
1704 enc = xmlParseCharEncoding((const char *) encoding);
1705 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001706 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1707 XML_XINCLUDE_UNKNOWN_ENCODING,
1708 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001709 xmlFree(encoding);
1710 xmlFree(URL);
1711 return(-1);
1712 }
1713 xmlFree(encoding);
1714 }
1715
1716 /*
1717 * Load it.
1718 */
1719 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001720 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001721 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001722 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001723 }
1724 node = xmlNewText(NULL);
1725
1726 /*
1727 * Scan all chars from the resource and add the to the node
1728 */
1729 while (xmlParserInputBufferRead(buf, 128) > 0) {
1730 int len;
1731 const xmlChar *content;
1732
1733 content = xmlBufferContent(buf->buffer);
1734 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001735 for (i = 0;i < len;) {
1736 int cur;
1737 int l;
1738
1739 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1740 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001741 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1742 XML_XINCLUDE_INVALID_CHAR,
1743 "%s contains invalid char\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001744 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001745 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001746 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001747 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001748 }
1749 xmlBufferShrink(buf->buffer, len);
1750 }
1751 xmlFreeParserInputBuffer(buf);
1752 xmlXIncludeAddTxt(ctxt, node, URL);
1753
1754loaded:
1755 /*
1756 * Add the element as the replacement copy.
1757 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001758 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001759 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001760 return(0);
1761}
1762
1763/**
1764 * xmlXIncludeLoadFallback:
1765 * @ctxt: the XInclude context
1766 * @fallback: the fallback node
1767 * @nr: the xinclude node number
1768 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001769 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001770 * in the XInclude context
1771 *
1772 * Returns 0 in case of success, -1 in case of failure
1773 */
1774static int
1775xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001776 xmlXIncludeCtxtPtr newctxt;
1777 int ret = 0;
1778
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001779 if ((fallback == NULL) || (ctxt == NULL))
1780 return(-1);
William M. Brackef245fd2004-02-06 09:33:59 +00001781 if (fallback->children != NULL) {
1782 /*
1783 * It's possible that the fallback also has 'includes'
1784 * (Bug 129969), so we re-process the fallback just in case
1785 */
1786 newctxt = xmlXIncludeNewContext(ctxt->doc);
1787 if (newctxt == NULL)
1788 return (-1);
1789 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1790 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
1791 if ((ret >=0) && (ctxt->nbErrors > 0))
1792 ret = -1;
1793 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001794
William M. Brackef245fd2004-02-06 09:33:59 +00001795 ctxt->incTab[nr]->inc = xmlCopyNodeList(fallback->children);
1796 } else {
1797 ctxt->incTab[nr]->inc = NULL;
1798 }
William M. Brackaae10522004-01-02 14:59:41 +00001799 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001800}
1801
1802/************************************************************************
1803 * *
1804 * XInclude Processing *
1805 * *
1806 ************************************************************************/
1807
1808/**
1809 * xmlXIncludePreProcessNode:
1810 * @ctxt: an XInclude context
1811 * @node: an XInclude node
1812 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001813 * Implement the XInclude preprocessing, currently just adding the element
1814 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001815 *
1816 * Returns the result list or NULL in case of error
1817 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001818static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001819xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1820 xmlXIncludeAddNode(ctxt, node);
1821 return(0);
1822}
1823
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001824/**
Owen Taylor3473f882001-02-23 17:55:21 +00001825 * xmlXIncludeLoadNode:
1826 * @ctxt: an XInclude context
1827 * @nr: the node number
1828 *
1829 * Find and load the infoset replacement for the given node.
1830 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001831 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001832 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001833static int
Owen Taylor3473f882001-02-23 17:55:21 +00001834xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1835 xmlNodePtr cur;
1836 xmlChar *href;
1837 xmlChar *parse;
1838 xmlChar *base;
1839 xmlChar *URI;
1840 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001841 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001842
1843 if (ctxt == NULL)
1844 return(-1);
1845 if ((nr < 0) || (nr >= ctxt->incNr))
1846 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001847 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001848 if (cur == NULL)
1849 return(-1);
1850
Owen Taylor3473f882001-02-23 17:55:21 +00001851 /*
1852 * read the attributes
1853 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001854 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00001855 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001856 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
1857 if (href == NULL)
1858 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001859 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001860 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00001861 if (parse != NULL) {
1862 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1863 xml = 1;
1864 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1865 xml = 0;
1866 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001867 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1868 XML_XINCLUDE_PARSE_VALUE,
1869 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00001870 if (href != NULL)
1871 xmlFree(href);
1872 if (parse != NULL)
1873 xmlFree(parse);
1874 return(-1);
1875 }
1876 }
1877
1878 /*
1879 * compute the URI
1880 */
1881 base = xmlNodeGetBase(ctxt->doc, cur);
1882 if (base == NULL) {
1883 URI = xmlBuildURI(href, ctxt->doc->URL);
1884 } else {
1885 URI = xmlBuildURI(href, base);
1886 }
1887 if (URI == NULL) {
1888 xmlChar *escbase;
1889 xmlChar *eschref;
1890 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001891 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00001892 */
1893 escbase = xmlURIEscape(base);
1894 eschref = xmlURIEscape(href);
1895 URI = xmlBuildURI(eschref, escbase);
1896 if (escbase != NULL)
1897 xmlFree(escbase);
1898 if (eschref != NULL)
1899 xmlFree(eschref);
1900 }
1901 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001902 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1903 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001904 if (parse != NULL)
1905 xmlFree(parse);
1906 if (href != NULL)
1907 xmlFree(href);
1908 if (base != NULL)
1909 xmlFree(base);
1910 return(-1);
1911 }
1912#ifdef DEBUG_XINCLUDE
1913 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
1914 xml ? "xml": "text");
1915 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
1916#endif
1917
1918 /*
1919 * Cleanup
1920 */
1921 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001922 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00001923 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
1924 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001925 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
1926 }
1927 if (ret < 0) {
1928 xmlNodePtr children;
1929
1930 /*
1931 * Time to try a fallback if availble
1932 */
1933#ifdef DEBUG_XINCLUDE
1934 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
1935#endif
1936 children = cur->children;
1937 while (children != NULL) {
1938 if ((children->type == XML_ELEMENT_NODE) &&
1939 (children->ns != NULL) &&
1940 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001941 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
1942 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001943 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00001944 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001945 break;
1946 }
1947 children = children->next;
1948 }
1949 }
1950 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001951 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1952 XML_XINCLUDE_NO_FALLBACK,
1953 "could not load %s, and no fallback was found\n",
1954 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001955 }
1956
1957 /*
1958 * Cleanup
1959 */
1960 if (URI != NULL)
1961 xmlFree(URI);
1962 if (parse != NULL)
1963 xmlFree(parse);
1964 if (href != NULL)
1965 xmlFree(href);
1966 if (base != NULL)
1967 xmlFree(base);
1968 return(0);
1969}
1970
1971/**
1972 * xmlXIncludeIncludeNode:
1973 * @ctxt: an XInclude context
1974 * @nr: the node number
1975 *
1976 * Inplement the infoset replacement for the given node
1977 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001978 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001979 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001980static int
Owen Taylor3473f882001-02-23 17:55:21 +00001981xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001982 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001983
1984 if (ctxt == NULL)
1985 return(-1);
1986 if ((nr < 0) || (nr >= ctxt->incNr))
1987 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001988 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001989 if (cur == NULL)
1990 return(-1);
1991
1992 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001993 * If we stored an XPointer a late computation may be needed
1994 */
1995 if ((ctxt->incTab[nr]->inc == NULL) &&
1996 (ctxt->incTab[nr]->xptr != NULL)) {
1997 ctxt->incTab[nr]->inc =
1998 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
1999 ctxt->incTab[nr]->xptr);
2000 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2001 ctxt->incTab[nr]->xptr = NULL;
2002 }
2003 list = ctxt->incTab[nr]->inc;
2004 ctxt->incTab[nr]->inc = NULL;
2005
2006 /*
2007 * Check against the risk of generating a multi-rooted document
2008 */
2009 if ((cur->parent != NULL) &&
2010 (cur->parent->type != XML_ELEMENT_NODE)) {
2011 int nb_elem = 0;
2012
2013 tmp = list;
2014 while (tmp != NULL) {
2015 if (tmp->type == XML_ELEMENT_NODE)
2016 nb_elem++;
2017 tmp = tmp->next;
2018 }
2019 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002020 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2021 XML_XINCLUDE_MULTIPLE_ROOT,
2022 "XInclude error: would result in multiple root nodes\n",
2023 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002024 return(-1);
2025 }
2026 }
2027
2028 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002029 * Change the current node as an XInclude start one, and add an
2030 * entity end one
2031 */
2032 cur->type = XML_XINCLUDE_START;
2033 end = xmlNewNode(cur->ns, cur->name);
2034 if (end == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002035 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_BUILD_FAILED,
2036 "failed to build node\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002037 return(-1);
2038 }
2039 end->type = XML_XINCLUDE_END;
2040 xmlAddNextSibling(cur, end);
2041
2042 /*
2043 * Add the list of nodes
2044 */
Owen Taylor3473f882001-02-23 17:55:21 +00002045 while (list != NULL) {
2046 cur = list;
2047 list = list->next;
2048
2049 xmlAddPrevSibling(end, cur);
2050 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002051
2052
Owen Taylor3473f882001-02-23 17:55:21 +00002053 return(0);
2054}
2055
2056/**
2057 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002058 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002059 * @node: an XInclude node
2060 *
2061 * test if the node is an XInclude node
2062 *
2063 * Returns 1 true, 0 otherwise
2064 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002065static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002066xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002067 if (node == NULL)
2068 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002069 if (node->type != XML_ELEMENT_NODE)
2070 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002071 if (node->ns == NULL)
2072 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002073 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2074 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2075 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2076 if (ctxt->legacy == 0) {
2077 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2078 "Deprecated XInclude namespace found, use %s",
2079 XINCLUDE_NS);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002080 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002081 }
2082 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002083 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2084 xmlNodePtr child = node->children;
2085 int nb_fallback = 0;
2086
2087 while (child != NULL) {
2088 if ((child->type == XML_ELEMENT_NODE) &&
2089 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002090 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2091 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002092 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002093 xmlXIncludeErr(ctxt, node,
2094 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2095 "%s has an 'include' child\n",
2096 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002097 return(0);
2098 }
2099 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2100 nb_fallback++;
2101 }
2102 }
2103 child = child->next;
2104 }
2105 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002106 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2107 "%s has multiple fallback children\n",
2108 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002109 return(0);
2110 }
2111 return(1);
2112 }
2113 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2114 if ((node->parent == NULL) ||
2115 (node->parent->type != XML_ELEMENT_NODE) ||
2116 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002117 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2118 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002119 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002120 xmlXIncludeErr(ctxt, node,
2121 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2122 "%s is not the child of an 'include'\n",
2123 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002124 }
2125 }
2126 }
Owen Taylor3473f882001-02-23 17:55:21 +00002127 return(0);
2128}
2129
2130/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002131 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002132 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002133 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002134 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002135 *
2136 * Implement the XInclude substitution on the XML document @doc
2137 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002138 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002139 * or the number of substitutions done.
2140 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002141static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002142xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002143 xmlNodePtr cur;
2144 int ret = 0;
2145 int i;
2146
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002147 if ((doc == NULL) || (tree == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002148 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002149 if (ctxt == NULL)
2150 return(-1);
2151
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002152 if (doc->URL != NULL) {
2153 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2154 if (ret < 0)
2155 return(-1);
2156 }
2157
Owen Taylor3473f882001-02-23 17:55:21 +00002158 /*
2159 * First phase: lookup the elements in the document
2160 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002161 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002162 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002163 xmlXIncludePreProcessNode(ctxt, cur);
2164 while (cur != NULL) {
2165 /* TODO: need to work on entities -> stack */
2166 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002167 (cur->children->type != XML_ENTITY_DECL) &&
2168 (cur->children->type != XML_XINCLUDE_START) &&
2169 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002170 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002171 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002172 xmlXIncludePreProcessNode(ctxt, cur);
2173 } else if (cur->next != NULL) {
2174 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002175 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002176 xmlXIncludePreProcessNode(ctxt, cur);
2177 } else {
2178 do {
2179 cur = cur->parent;
2180 if (cur == NULL) break; /* do */
2181 if (cur->next != NULL) {
2182 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002183 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002184 xmlXIncludePreProcessNode(ctxt, cur);
2185 break; /* do */
2186 }
2187 } while (cur != NULL);
2188 }
2189 }
2190
2191 /*
2192 * Second Phase : collect the infosets fragments
2193 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002194 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002195 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002196 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002197 }
2198
2199 /*
2200 * Third phase: extend the original document infoset.
William M. Brack6b1a28d2004-02-06 11:24:44 +00002201 *
2202 * Originally we bypassed the inclusion if there were any errors
2203 * encountered on any of the XIncludes. A bug was raised (bug
2204 * 132588) requesting that we output the XIncludes without error,
2205 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2206 * give some other problems in the future, but for now it seems to
2207 * work ok.
2208 *
Owen Taylor3473f882001-02-23 17:55:21 +00002209 */
William M. Brack6b1a28d2004-02-06 11:24:44 +00002210 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
2211 if ((ctxt->incTab[i]->inc != NULL) || (ctxt->incTab[i]->xptr != NULL))
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002212 xmlXIncludeIncludeNode(ctxt, i);
Owen Taylor3473f882001-02-23 17:55:21 +00002213 }
2214
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002215 if (doc->URL != NULL)
2216 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002217 return(ret);
2218}
2219
2220/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002221 * xmlXIncludeSetFlags:
2222 * @ctxt: an XInclude processing context
2223 * @flags: a set of xmlParserOption used for parsing XML includes
2224 *
2225 * Set the flags used for further processing of XML resources.
2226 *
2227 * Returns 0 in case of success and -1 in case of error.
2228 */
2229int
2230xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2231 if (ctxt == NULL)
2232 return(-1);
2233 ctxt->parseFlags = flags;
2234 return(0);
2235}
2236
2237/**
2238 * xmlXIncludeProcessFlags:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002239 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002240 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002241 *
2242 * Implement the XInclude substitution on the XML document @doc
2243 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002244 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002245 * or the number of substitutions done.
2246 */
2247int
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002248xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002249 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002250 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002251 int ret = 0;
2252
2253 if (doc == NULL)
2254 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002255 tree = xmlDocGetRootElement(doc);
2256 if (tree == NULL)
2257 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002258 ctxt = xmlXIncludeNewContext(doc);
2259 if (ctxt == NULL)
2260 return(-1);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002261 xmlXIncludeSetFlags(ctxt, flags);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002262 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2263 if ((ret >= 0) && (ctxt->nbErrors > 0))
2264 ret = -1;
2265
2266 xmlXIncludeFreeContext(ctxt);
2267 return(ret);
2268}
2269
2270/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002271 * xmlXIncludeProcess:
2272 * @doc: an XML document
2273 *
2274 * Implement the XInclude substitution on the XML document @doc
2275 *
2276 * Returns 0 if no substitution were done, -1 if some processing failed
2277 * or the number of substitutions done.
2278 */
2279int
2280xmlXIncludeProcess(xmlDocPtr doc) {
2281 return(xmlXIncludeProcessFlags(doc, 0));
2282}
2283
2284/**
2285 * xmlXIncludeProcessTreeFlags:
2286 * @tree: a node in an XML document
2287 * @flags: a set of xmlParserOption used for parsing XML includes
2288 *
2289 * Implement the XInclude substitution for the given subtree
2290 *
2291 * Returns 0 if no substitution were done, -1 if some processing failed
2292 * or the number of substitutions done.
2293 */
2294int
2295xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2296 xmlXIncludeCtxtPtr ctxt;
2297 int ret = 0;
2298
2299 if ((tree == NULL) || (tree->doc == NULL))
2300 return(-1);
2301 ctxt = xmlXIncludeNewContext(tree->doc);
2302 if (ctxt == NULL)
2303 return(-1);
2304 xmlXIncludeSetFlags(ctxt, flags);
2305 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2306 if ((ret >= 0) && (ctxt->nbErrors > 0))
2307 ret = -1;
2308
2309 xmlXIncludeFreeContext(ctxt);
2310 return(ret);
2311}
2312
2313/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002314 * xmlXIncludeProcessTree:
2315 * @tree: a node in an XML document
2316 *
2317 * Implement the XInclude substitution for the given subtree
2318 *
2319 * Returns 0 if no substitution were done, -1 if some processing failed
2320 * or the number of substitutions done.
2321 */
2322int
2323xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002324 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002325}
2326
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002327/**
2328 * xmlXIncludeProcessNode:
2329 * @ctxt: an existing XInclude context
2330 * @node: a node in an XML document
2331 *
2332 * Implement the XInclude substitution for the given subtree reusing
2333 * the informations and data coming from the given context.
2334 *
2335 * Returns 0 if no substitution were done, -1 if some processing failed
2336 * or the number of substitutions done.
2337 */
2338int
2339xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2340 int ret = 0;
2341
2342 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2343 return(-1);
2344 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2345 if ((ret >= 0) && (ctxt->nbErrors > 0))
2346 ret = -1;
2347 return(ret);
2348}
2349
Owen Taylor3473f882001-02-23 17:55:21 +00002350#else /* !LIBXML_XINCLUDE_ENABLED */
2351#endif