blob: a2a45c7c560e532bbc4d6a59afd8d0b6dc6d2db0 [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. Brackf7eb7942003-12-31 07:59:17 +0000877 int level = 0, lastLevel = 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 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000953 tmp = xmlDocCopyNode(cur, target, 0);
William M. Brackf7eb7942003-12-31 07:59:17 +0000954 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000955 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000956 listParent = cur->parent;
957 } else {
958 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000959 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +0000960 else {
961 xmlAddChild(last, tmp);
962 lastLevel = level;
963 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000964 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000965 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000966
967 if (index2 > 1) {
968 end = xmlXIncludeGetNthChild(cur, index2 - 1);
969 index2 = 0;
970 }
971 if ((cur == start) && (index1 > 1)) {
972 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
973 index1 = 0;
974 } else {
975 cur = cur->children;
976 }
977 /*
978 * Now gather the remaining nodes from cur to end
979 */
980 continue; /* while */
981 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000982 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000983 if ((cur->type == XML_TEXT_NODE) ||
984 (cur->type == XML_CDATA_SECTION_NODE)) {
985 const xmlChar *content = cur->content;
986
987 if (content == NULL) {
988 tmp = xmlNewTextLen(NULL, 0);
989 } else {
990 if (index1 > 1) {
991 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +0000992 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000993 }
994 tmp = xmlNewText(content);
995 }
996 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000997 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +0000998 } else { /* Not text node */
William M. Brackf7eb7942003-12-31 07:59:17 +0000999 tmp = xmlDocCopyNode(cur, target, 0);
1000 list = last = tmp;
1001 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001002 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001003 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001004 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001005 index1 = 0;
1006 /*
1007 * Now gather the remaining nodes from cur to end
1008 */
1009 continue; /* while */
1010 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001011 }
1012 } else {
1013 tmp = NULL;
1014 switch (cur->type) {
1015 case XML_DTD_NODE:
1016 case XML_ELEMENT_DECL:
1017 case XML_ATTRIBUTE_DECL:
1018 case XML_ENTITY_NODE:
1019 /* Do not copy DTD informations */
1020 break;
1021 case XML_ENTITY_DECL:
1022 /* handle crossing entities -> stack needed */
1023 break;
1024 case XML_XINCLUDE_START:
1025 case XML_XINCLUDE_END:
1026 /* don't consider it part of the tree content */
1027 break;
1028 case XML_ATTRIBUTE_NODE:
1029 /* Humm, should not happen ! */
1030 break;
1031 default:
William M. Brack72ee48d2003-12-30 08:30:19 +00001032 tmp = xmlDocCopyNode(cur, target, 0);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001033 break;
1034 }
1035 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001036 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001037 xmlAddNextSibling(last, tmp);
1038 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001039 xmlAddChild(last, tmp);
1040 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001041 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001042 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001043 }
1044 }
1045 /*
1046 * Skip to next node in document order
1047 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001048 cur = xmlXPtrAdvanceNode(cur, &level);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001049 }
1050 return(list);
1051}
1052
1053/**
1054 * xmlXIncludeBuildNodeList:
1055 * @ctxt: the XInclude context
1056 * @target: the document target
1057 * @source: the document source
1058 * @obj: the XPointer result from the evaluation.
1059 *
1060 * Build a node list tree copy of the XPointer result.
1061 * This will drop Attributes and Namespace declarations.
1062 *
1063 * Returns an xmlNodePtr list or NULL.
1064 * the caller has to free the node tree.
1065 */
1066static xmlNodePtr
1067xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1068 xmlDocPtr source, xmlXPathObjectPtr obj) {
1069 xmlNodePtr list = NULL, last = NULL;
1070 int i;
1071
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001072 if (source == NULL)
1073 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001074 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1075 (obj == NULL))
1076 return(NULL);
1077 switch (obj->type) {
1078 case XPATH_NODESET: {
1079 xmlNodeSetPtr set = obj->nodesetval;
1080 if (set == NULL)
1081 return(NULL);
1082 for (i = 0;i < set->nodeNr;i++) {
1083 if (set->nodeTab[i] == NULL)
1084 continue;
1085 switch (set->nodeTab[i]->type) {
1086 case XML_TEXT_NODE:
1087 case XML_CDATA_SECTION_NODE:
1088 case XML_ELEMENT_NODE:
1089 case XML_ENTITY_REF_NODE:
1090 case XML_ENTITY_NODE:
1091 case XML_PI_NODE:
1092 case XML_COMMENT_NODE:
1093 case XML_DOCUMENT_NODE:
1094 case XML_HTML_DOCUMENT_NODE:
1095#ifdef LIBXML_DOCB_ENABLED
1096 case XML_DOCB_DOCUMENT_NODE:
1097#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001098 case XML_XINCLUDE_END:
1099 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001100 case XML_XINCLUDE_START: {
1101 xmlNodePtr tmp, cur = set->nodeTab[i];
1102
1103 cur = cur->next;
1104 while (cur != NULL) {
1105 switch(cur->type) {
1106 case XML_TEXT_NODE:
1107 case XML_CDATA_SECTION_NODE:
1108 case XML_ELEMENT_NODE:
1109 case XML_ENTITY_REF_NODE:
1110 case XML_ENTITY_NODE:
1111 case XML_PI_NODE:
1112 case XML_COMMENT_NODE:
1113 tmp = xmlXIncludeCopyNode(ctxt, target,
1114 source, cur);
1115 if (last == NULL) {
1116 list = last = tmp;
1117 } else {
1118 xmlAddNextSibling(last, tmp);
1119 last = tmp;
1120 }
1121 cur = cur->next;
1122 continue;
1123 default:
1124 break;
1125 }
1126 break;
1127 }
1128 continue;
1129 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001130 case XML_ATTRIBUTE_NODE:
1131 case XML_NAMESPACE_DECL:
1132 case XML_DOCUMENT_TYPE_NODE:
1133 case XML_DOCUMENT_FRAG_NODE:
1134 case XML_NOTATION_NODE:
1135 case XML_DTD_NODE:
1136 case XML_ELEMENT_DECL:
1137 case XML_ATTRIBUTE_DECL:
1138 case XML_ENTITY_DECL:
1139 continue; /* for */
1140 }
1141 if (last == NULL)
1142 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1143 set->nodeTab[i]);
1144 else {
1145 xmlAddNextSibling(last,
1146 xmlXIncludeCopyNode(ctxt, target, source,
1147 set->nodeTab[i]));
1148 if (last->next != NULL)
1149 last = last->next;
1150 }
1151 }
1152 break;
1153 }
1154 case XPATH_LOCATIONSET: {
1155 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1156 if (set == NULL)
1157 return(NULL);
1158 for (i = 0;i < set->locNr;i++) {
1159 if (last == NULL)
1160 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1161 set->locTab[i]);
1162 else
1163 xmlAddNextSibling(last,
1164 xmlXIncludeCopyXPointer(ctxt, target, source,
1165 set->locTab[i]));
1166 if (last != NULL) {
1167 while (last->next != NULL)
1168 last = last->next;
1169 }
1170 }
1171 break;
1172 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001173#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001174 case XPATH_RANGE:
1175 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001176#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001177 case XPATH_POINT:
1178 /* points are ignored in XInclude */
1179 break;
1180 default:
1181 break;
1182 }
1183 return(list);
1184}
1185/************************************************************************
1186 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001187 * XInclude I/O handling *
1188 * *
1189 ************************************************************************/
1190
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001191typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1192typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1193struct _xmlXIncludeMergeData {
1194 xmlDocPtr doc;
1195 xmlXIncludeCtxtPtr ctxt;
1196};
1197
Owen Taylor3473f882001-02-23 17:55:21 +00001198/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001199 * xmlXIncludeMergeOneEntity:
1200 * @ent: the entity
1201 * @doc: the including doc
1202 * @nr: the entity name
1203 *
1204 * Inplements the merge of one entity
1205 */
1206static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001207xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001208 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001209 xmlEntityPtr ret, prev;
1210 xmlDocPtr doc;
1211 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001212
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001213 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001214 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001215 ctxt = data->ctxt;
1216 doc = data->doc;
1217 if ((ctxt == NULL) || (doc == NULL))
1218 return;
1219 switch (ent->etype) {
1220 case XML_INTERNAL_PARAMETER_ENTITY:
1221 case XML_EXTERNAL_PARAMETER_ENTITY:
1222 case XML_INTERNAL_PREDEFINED_ENTITY:
1223 return;
1224 case XML_INTERNAL_GENERAL_ENTITY:
1225 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1226 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1227 break;
1228 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001229 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1230 ent->SystemID, ent->content);
1231 if (ret != NULL) {
1232 if (ent->URI != NULL)
1233 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001234 } else {
1235 prev = xmlGetDocEntity(doc, ent->name);
1236 if (prev != NULL) {
1237 if (ent->etype != prev->etype)
1238 goto error;
1239
1240 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1241 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1242 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001243 } else if ((ent->ExternalID != NULL) &&
1244 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001245 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1246 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001247 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1248 if (!xmlStrEqual(ent->content, prev->content))
1249 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001250 } else {
1251 goto error;
1252 }
1253
1254 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001255 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001256 return;
1257error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001258 switch (ent->etype) {
1259 case XML_INTERNAL_PARAMETER_ENTITY:
1260 case XML_EXTERNAL_PARAMETER_ENTITY:
1261 case XML_INTERNAL_PREDEFINED_ENTITY:
1262 case XML_INTERNAL_GENERAL_ENTITY:
1263 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1264 return;
1265 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1266 break;
1267 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001268 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1269 "mismatch in redefinition of entity %s\n",
1270 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001271}
1272
1273/**
1274 * xmlXIncludeMergeEntities:
1275 * @ctxt: an XInclude context
1276 * @doc: the including doc
1277 * @from: the included doc
1278 *
1279 * Inplements the entity merge
1280 *
1281 * Returns 0 if merge succeeded, -1 if some processing failed
1282 */
1283static int
1284xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1285 xmlDocPtr from) {
1286 xmlNodePtr cur;
1287 xmlDtdPtr target, source;
1288
1289 if (ctxt == NULL)
1290 return(-1);
1291
1292 if ((from == NULL) || (from->intSubset == NULL))
1293 return(0);
1294
1295 target = doc->intSubset;
1296 if (target == NULL) {
1297 cur = xmlDocGetRootElement(doc);
1298 if (cur == NULL)
1299 return(-1);
1300 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1301 if (target == NULL)
1302 return(-1);
1303 }
1304
1305 source = from->intSubset;
1306 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001307 xmlXIncludeMergeData data;
1308
1309 data.ctxt = ctxt;
1310 data.doc = doc;
1311
Daniel Veillard4287c572003-02-04 22:48:53 +00001312 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001313 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001314 }
1315 source = from->extSubset;
1316 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001317 xmlXIncludeMergeData data;
1318
1319 data.ctxt = ctxt;
1320 data.doc = doc;
1321
Daniel Veillard4287c572003-02-04 22:48:53 +00001322 /*
1323 * don't duplicate existing stuff when external subsets are the same
1324 */
1325 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1326 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1327 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001328 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001329 }
1330 }
1331 return(0);
1332}
1333
1334/**
Owen Taylor3473f882001-02-23 17:55:21 +00001335 * xmlXIncludeLoadDoc:
1336 * @ctxt: the XInclude context
1337 * @url: the associated URL
1338 * @nr: the xinclude node number
1339 *
1340 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001341 *
1342 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001343 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001344static int
Owen Taylor3473f882001-02-23 17:55:21 +00001345xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1346 xmlDocPtr doc;
1347 xmlURIPtr uri;
1348 xmlChar *URL;
1349 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001350 int i = 0;
1351
1352#ifdef DEBUG_XINCLUDE
1353 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1354#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001355 /*
1356 * Check the URL and remove any fragment identifier
1357 */
1358 uri = xmlParseURI((const char *)url);
1359 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001360 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1361 XML_XINCLUDE_HREF_URI,
1362 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001363 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001364 }
1365 if (uri->fragment != NULL) {
1366 fragment = (xmlChar *) uri->fragment;
1367 uri->fragment = NULL;
1368 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001369 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1370 (ctxt->incTab[nr]->fragment != NULL)) {
1371 if (fragment != NULL) xmlFree(fragment);
1372 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1373 }
Owen Taylor3473f882001-02-23 17:55:21 +00001374 URL = xmlSaveUri(uri);
1375 xmlFreeURI(uri);
1376 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001377 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1378 XML_XINCLUDE_HREF_URI,
1379 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001380 if (fragment != NULL)
1381 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001382 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001383 }
1384
1385 /*
1386 * Handling of references to the local document are done
1387 * directly through ctxt->doc.
1388 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001389 if ((URL[0] == 0) || (URL[0] == '#') ||
1390 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001391 doc = NULL;
1392 goto loaded;
1393 }
1394
1395 /*
1396 * Prevent reloading twice the document.
1397 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001398 for (i = 0; i < ctxt->incNr; i++) {
1399 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1400 (ctxt->incTab[i]->doc != NULL)) {
1401 doc = ctxt->incTab[i]->doc;
1402#ifdef DEBUG_XINCLUDE
1403 printf("Already loaded %s\n", URL);
1404#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001405 goto loaded;
1406 }
1407 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001408
Owen Taylor3473f882001-02-23 17:55:21 +00001409 /*
1410 * Load it.
1411 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001412#ifdef DEBUG_XINCLUDE
1413 printf("loading %s\n", URL);
1414#endif
Daniel Veillard98485322003-08-14 15:44:40 +00001415 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001416 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001417 xmlFree(URL);
1418 if (fragment != NULL)
1419 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001420 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001421 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001422 ctxt->incTab[nr]->doc = doc;
Daniel Veillard98485322003-08-14 15:44:40 +00001423 for (i = nr + 1; i < ctxt->incNr; i++) {
1424 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1425 ctxt->incTab[nr]->count++;
1426#ifdef DEBUG_XINCLUDE
1427 printf("Increasing %s count since reused\n", URL);
1428#endif
1429 break;
1430 }
1431 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001432
1433 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001434 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001435 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001436 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001437
1438 /*
1439 * We don't need the DTD anymore, free up space
1440 if (doc->intSubset != NULL) {
1441 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1442 xmlFreeNode((xmlNodePtr) doc->intSubset);
1443 doc->intSubset = NULL;
1444 }
1445 if (doc->extSubset != NULL) {
1446 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1447 xmlFreeNode((xmlNodePtr) doc->extSubset);
1448 doc->extSubset = NULL;
1449 }
1450 */
1451 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001452
1453loaded:
1454 if (fragment == NULL) {
1455 /*
1456 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001457 */
1458 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001459 {
1460 /* Hopefully a DTD declaration won't be copied from
1461 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001462 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001463 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001464 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001465 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001466 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001467 }
1468#ifdef LIBXML_XPTR_ENABLED
1469 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001470 /*
1471 * Computes the XPointer expression and make a copy used
1472 * as the replacement copy.
1473 */
1474 xmlXPathObjectPtr xptr;
1475 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001476 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001477
1478 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001479 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1480 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001481 } else {
1482 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1483 }
1484 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001485 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1486 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001487 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001488 xmlFree(URL);
1489 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001490 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001491 }
1492 xptr = xmlXPtrEval(fragment, xptrctxt);
1493 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001494 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1495 XML_XINCLUDE_XPTR_FAILED,
1496 "XPointer evaluation failed: #%s\n",
1497 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001498 xmlXPathFreeContext(xptrctxt);
1499 xmlFree(URL);
1500 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001501 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001502 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001503 switch (xptr->type) {
1504 case XPATH_UNDEFINED:
1505 case XPATH_BOOLEAN:
1506 case XPATH_NUMBER:
1507 case XPATH_STRING:
1508 case XPATH_POINT:
1509 case XPATH_USERS:
1510 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001511 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1512 XML_XINCLUDE_XPTR_RESULT,
1513 "XPointer is not a range: #%s\n",
1514 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001515 xmlXPathFreeContext(xptrctxt);
1516 xmlFree(URL);
1517 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001518 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001519 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001520 if ((xptr->nodesetval == NULL) ||
1521 (xptr->nodesetval->nodeNr <= 0)) {
1522 xmlXPathFreeContext(xptrctxt);
1523 xmlFree(URL);
1524 xmlFree(fragment);
1525 return(-1);
1526 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001527 case XPATH_RANGE:
1528 case XPATH_LOCATIONSET:
1529 break;
1530 }
1531 set = xptr->nodesetval;
1532 if (set != NULL) {
1533 for (i = 0;i < set->nodeNr;i++) {
1534 if (set->nodeTab[i] == NULL)
1535 continue;
1536 switch (set->nodeTab[i]->type) {
1537 case XML_TEXT_NODE:
1538 case XML_CDATA_SECTION_NODE:
1539 case XML_ELEMENT_NODE:
1540 case XML_ENTITY_REF_NODE:
1541 case XML_ENTITY_NODE:
1542 case XML_PI_NODE:
1543 case XML_COMMENT_NODE:
1544 case XML_DOCUMENT_NODE:
1545 case XML_HTML_DOCUMENT_NODE:
1546#ifdef LIBXML_DOCB_ENABLED
1547 case XML_DOCB_DOCUMENT_NODE:
1548#endif
1549 continue;
1550 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001551 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1552 XML_XINCLUDE_XPTR_RESULT,
1553 "XPointer selects an attribute: #%s\n",
1554 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001555 set->nodeTab[i] = NULL;
1556 continue;
1557 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001558 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1559 XML_XINCLUDE_XPTR_RESULT,
1560 "XPointer selects a namespace: #%s\n",
1561 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001562 set->nodeTab[i] = NULL;
1563 continue;
1564 case XML_DOCUMENT_TYPE_NODE:
1565 case XML_DOCUMENT_FRAG_NODE:
1566 case XML_NOTATION_NODE:
1567 case XML_DTD_NODE:
1568 case XML_ELEMENT_DECL:
1569 case XML_ATTRIBUTE_DECL:
1570 case XML_ENTITY_DECL:
1571 case XML_XINCLUDE_START:
1572 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001573 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1574 XML_XINCLUDE_XPTR_RESULT,
1575 "XPointer selects unexpected nodes: #%s\n",
1576 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001577 set->nodeTab[i] = NULL;
1578 set->nodeTab[i] = NULL;
1579 continue; /* for */
1580 }
1581 }
1582 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001583 if (doc == NULL) {
1584 ctxt->incTab[nr]->xptr = xptr;
1585 ctxt->incTab[nr]->inc = NULL;
1586 } else {
1587 ctxt->incTab[nr]->inc =
1588 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1589 xmlXPathFreeObject(xptr);
1590 }
Owen Taylor3473f882001-02-23 17:55:21 +00001591 xmlXPathFreeContext(xptrctxt);
1592 xmlFree(fragment);
1593 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001594#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001595
1596 /*
1597 * Do the xml:base fixup if needed
1598 */
1599 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1600 xmlNodePtr node;
1601
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001602 node = ctxt->incTab[nr]->inc;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001603 while (node != NULL) {
1604 if (node->type == XML_ELEMENT_NODE)
1605 xmlNodeSetBase(node, URL);
1606 node = node->next;
1607 }
1608 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001609 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1610 (ctxt->incTab[nr]->count <= 1)) {
1611#ifdef DEBUG_XINCLUDE
1612 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1613#endif
1614 xmlFreeDoc(ctxt->incTab[nr]->doc);
1615 ctxt->incTab[nr]->doc = NULL;
1616 }
Owen Taylor3473f882001-02-23 17:55:21 +00001617 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001618 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001619}
1620
1621/**
1622 * xmlXIncludeLoadTxt:
1623 * @ctxt: the XInclude context
1624 * @url: the associated URL
1625 * @nr: the xinclude node number
1626 *
1627 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001628 *
1629 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001630 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001631static int
Owen Taylor3473f882001-02-23 17:55:21 +00001632xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1633 xmlParserInputBufferPtr buf;
1634 xmlNodePtr node;
1635 xmlURIPtr uri;
1636 xmlChar *URL;
1637 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001638 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001639 xmlCharEncoding enc = (xmlCharEncoding) 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001640
Owen Taylor3473f882001-02-23 17:55:21 +00001641 /*
1642 * Check the URL and remove any fragment identifier
1643 */
1644 uri = xmlParseURI((const char *)url);
1645 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001646 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1647 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001648 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001649 }
1650 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001651 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1652 "fragment identifier forbidden for text: %s\n",
1653 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001654 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001655 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001656 }
1657 URL = xmlSaveUri(uri);
1658 xmlFreeURI(uri);
1659 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001660 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1661 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001662 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001663 }
1664
1665 /*
1666 * Handling of references to the local document are done
1667 * directly through ctxt->doc.
1668 */
1669 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001670 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1671 XML_XINCLUDE_TEXT_DOCUMENT,
1672 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001673 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001674 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001675 }
1676
1677 /*
1678 * Prevent reloading twice the document.
1679 */
1680 for (i = 0; i < ctxt->txtNr; i++) {
1681 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1682 node = xmlCopyNode(ctxt->txtTab[i], 1);
1683 goto loaded;
1684 }
1685 }
1686 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001687 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001688 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001689 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1690 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1691 }
1692 if (encoding != NULL) {
1693 /*
1694 * TODO: we should not have to remap to the xmlCharEncoding
1695 * predefined set, a better interface than
1696 * xmlParserInputBufferCreateFilename should allow any
1697 * encoding supported by iconv
1698 */
1699 enc = xmlParseCharEncoding((const char *) encoding);
1700 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001701 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1702 XML_XINCLUDE_UNKNOWN_ENCODING,
1703 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001704 xmlFree(encoding);
1705 xmlFree(URL);
1706 return(-1);
1707 }
1708 xmlFree(encoding);
1709 }
1710
1711 /*
1712 * Load it.
1713 */
1714 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001715 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001716 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001717 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001718 }
1719 node = xmlNewText(NULL);
1720
1721 /*
1722 * Scan all chars from the resource and add the to the node
1723 */
1724 while (xmlParserInputBufferRead(buf, 128) > 0) {
1725 int len;
1726 const xmlChar *content;
1727
1728 content = xmlBufferContent(buf->buffer);
1729 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001730 for (i = 0;i < len;) {
1731 int cur;
1732 int l;
1733
1734 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1735 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001736 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1737 XML_XINCLUDE_INVALID_CHAR,
1738 "%s contains invalid char\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001739 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001740 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001741 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001742 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001743 }
1744 xmlBufferShrink(buf->buffer, len);
1745 }
1746 xmlFreeParserInputBuffer(buf);
1747 xmlXIncludeAddTxt(ctxt, node, URL);
1748
1749loaded:
1750 /*
1751 * Add the element as the replacement copy.
1752 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001753 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001754 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001755 return(0);
1756}
1757
1758/**
1759 * xmlXIncludeLoadFallback:
1760 * @ctxt: the XInclude context
1761 * @fallback: the fallback node
1762 * @nr: the xinclude node number
1763 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001764 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001765 * in the XInclude context
1766 *
1767 * Returns 0 in case of success, -1 in case of failure
1768 */
1769static int
1770xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001771 xmlXIncludeCtxtPtr newctxt;
1772 int ret = 0;
1773
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001774 if ((fallback == NULL) || (ctxt == NULL))
1775 return(-1);
William M. Brackef245fd2004-02-06 09:33:59 +00001776 if (fallback->children != NULL) {
1777 /*
1778 * It's possible that the fallback also has 'includes'
1779 * (Bug 129969), so we re-process the fallback just in case
1780 */
1781 newctxt = xmlXIncludeNewContext(ctxt->doc);
1782 if (newctxt == NULL)
1783 return (-1);
1784 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1785 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
1786 if ((ret >=0) && (ctxt->nbErrors > 0))
1787 ret = -1;
1788 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001789
William M. Brackef245fd2004-02-06 09:33:59 +00001790 ctxt->incTab[nr]->inc = xmlCopyNodeList(fallback->children);
1791 } else {
1792 ctxt->incTab[nr]->inc = NULL;
1793 }
William M. Brackaae10522004-01-02 14:59:41 +00001794 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001795}
1796
1797/************************************************************************
1798 * *
1799 * XInclude Processing *
1800 * *
1801 ************************************************************************/
1802
1803/**
1804 * xmlXIncludePreProcessNode:
1805 * @ctxt: an XInclude context
1806 * @node: an XInclude node
1807 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001808 * Implement the XInclude preprocessing, currently just adding the element
1809 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001810 *
1811 * Returns the result list or NULL in case of error
1812 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001813static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001814xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1815 xmlXIncludeAddNode(ctxt, node);
1816 return(0);
1817}
1818
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001819/**
Owen Taylor3473f882001-02-23 17:55:21 +00001820 * xmlXIncludeLoadNode:
1821 * @ctxt: an XInclude context
1822 * @nr: the node number
1823 *
1824 * Find and load the infoset replacement for the given node.
1825 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001826 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001827 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001828static int
Owen Taylor3473f882001-02-23 17:55:21 +00001829xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1830 xmlNodePtr cur;
1831 xmlChar *href;
1832 xmlChar *parse;
1833 xmlChar *base;
1834 xmlChar *URI;
1835 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001836 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001837
1838 if (ctxt == NULL)
1839 return(-1);
1840 if ((nr < 0) || (nr >= ctxt->incNr))
1841 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001842 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001843 if (cur == NULL)
1844 return(-1);
1845
Owen Taylor3473f882001-02-23 17:55:21 +00001846 /*
1847 * read the attributes
1848 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001849 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00001850 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001851 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
1852 if (href == NULL)
1853 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001854 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001855 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00001856 if (parse != NULL) {
1857 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1858 xml = 1;
1859 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1860 xml = 0;
1861 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001862 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1863 XML_XINCLUDE_PARSE_VALUE,
1864 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00001865 if (href != NULL)
1866 xmlFree(href);
1867 if (parse != NULL)
1868 xmlFree(parse);
1869 return(-1);
1870 }
1871 }
1872
1873 /*
1874 * compute the URI
1875 */
1876 base = xmlNodeGetBase(ctxt->doc, cur);
1877 if (base == NULL) {
1878 URI = xmlBuildURI(href, ctxt->doc->URL);
1879 } else {
1880 URI = xmlBuildURI(href, base);
1881 }
1882 if (URI == NULL) {
1883 xmlChar *escbase;
1884 xmlChar *eschref;
1885 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001886 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00001887 */
1888 escbase = xmlURIEscape(base);
1889 eschref = xmlURIEscape(href);
1890 URI = xmlBuildURI(eschref, escbase);
1891 if (escbase != NULL)
1892 xmlFree(escbase);
1893 if (eschref != NULL)
1894 xmlFree(eschref);
1895 }
1896 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001897 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1898 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001899 if (parse != NULL)
1900 xmlFree(parse);
1901 if (href != NULL)
1902 xmlFree(href);
1903 if (base != NULL)
1904 xmlFree(base);
1905 return(-1);
1906 }
1907#ifdef DEBUG_XINCLUDE
1908 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
1909 xml ? "xml": "text");
1910 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
1911#endif
1912
1913 /*
1914 * Cleanup
1915 */
1916 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001917 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00001918 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
1919 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001920 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
1921 }
1922 if (ret < 0) {
1923 xmlNodePtr children;
1924
1925 /*
1926 * Time to try a fallback if availble
1927 */
1928#ifdef DEBUG_XINCLUDE
1929 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
1930#endif
1931 children = cur->children;
1932 while (children != NULL) {
1933 if ((children->type == XML_ELEMENT_NODE) &&
1934 (children->ns != NULL) &&
1935 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001936 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
1937 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001938 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00001939 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001940 break;
1941 }
1942 children = children->next;
1943 }
1944 }
1945 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001946 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1947 XML_XINCLUDE_NO_FALLBACK,
1948 "could not load %s, and no fallback was found\n",
1949 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001950 }
1951
1952 /*
1953 * Cleanup
1954 */
1955 if (URI != NULL)
1956 xmlFree(URI);
1957 if (parse != NULL)
1958 xmlFree(parse);
1959 if (href != NULL)
1960 xmlFree(href);
1961 if (base != NULL)
1962 xmlFree(base);
1963 return(0);
1964}
1965
1966/**
1967 * xmlXIncludeIncludeNode:
1968 * @ctxt: an XInclude context
1969 * @nr: the node number
1970 *
1971 * Inplement the infoset replacement for the given node
1972 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001973 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001974 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001975static int
Owen Taylor3473f882001-02-23 17:55:21 +00001976xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001977 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001978
1979 if (ctxt == NULL)
1980 return(-1);
1981 if ((nr < 0) || (nr >= ctxt->incNr))
1982 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001983 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001984 if (cur == NULL)
1985 return(-1);
1986
1987 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001988 * If we stored an XPointer a late computation may be needed
1989 */
1990 if ((ctxt->incTab[nr]->inc == NULL) &&
1991 (ctxt->incTab[nr]->xptr != NULL)) {
1992 ctxt->incTab[nr]->inc =
1993 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
1994 ctxt->incTab[nr]->xptr);
1995 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
1996 ctxt->incTab[nr]->xptr = NULL;
1997 }
1998 list = ctxt->incTab[nr]->inc;
1999 ctxt->incTab[nr]->inc = NULL;
2000
2001 /*
2002 * Check against the risk of generating a multi-rooted document
2003 */
2004 if ((cur->parent != NULL) &&
2005 (cur->parent->type != XML_ELEMENT_NODE)) {
2006 int nb_elem = 0;
2007
2008 tmp = list;
2009 while (tmp != NULL) {
2010 if (tmp->type == XML_ELEMENT_NODE)
2011 nb_elem++;
2012 tmp = tmp->next;
2013 }
2014 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002015 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2016 XML_XINCLUDE_MULTIPLE_ROOT,
2017 "XInclude error: would result in multiple root nodes\n",
2018 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002019 return(-1);
2020 }
2021 }
2022
2023 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002024 * Change the current node as an XInclude start one, and add an
2025 * entity end one
2026 */
2027 cur->type = XML_XINCLUDE_START;
2028 end = xmlNewNode(cur->ns, cur->name);
2029 if (end == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002030 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_BUILD_FAILED,
2031 "failed to build node\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002032 return(-1);
2033 }
2034 end->type = XML_XINCLUDE_END;
2035 xmlAddNextSibling(cur, end);
2036
2037 /*
2038 * Add the list of nodes
2039 */
Owen Taylor3473f882001-02-23 17:55:21 +00002040 while (list != NULL) {
2041 cur = list;
2042 list = list->next;
2043
2044 xmlAddPrevSibling(end, cur);
2045 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002046
2047
Owen Taylor3473f882001-02-23 17:55:21 +00002048 return(0);
2049}
2050
2051/**
2052 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002053 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002054 * @node: an XInclude node
2055 *
2056 * test if the node is an XInclude node
2057 *
2058 * Returns 1 true, 0 otherwise
2059 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002060static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002061xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002062 if (node == NULL)
2063 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002064 if (node->type != XML_ELEMENT_NODE)
2065 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002066 if (node->ns == NULL)
2067 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002068 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2069 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2070 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2071 if (ctxt->legacy == 0) {
2072 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2073 "Deprecated XInclude namespace found, use %s",
2074 XINCLUDE_NS);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002075 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002076 }
2077 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002078 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2079 xmlNodePtr child = node->children;
2080 int nb_fallback = 0;
2081
2082 while (child != NULL) {
2083 if ((child->type == XML_ELEMENT_NODE) &&
2084 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002085 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2086 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002087 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002088 xmlXIncludeErr(ctxt, node,
2089 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2090 "%s has an 'include' child\n",
2091 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002092 return(0);
2093 }
2094 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2095 nb_fallback++;
2096 }
2097 }
2098 child = child->next;
2099 }
2100 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002101 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2102 "%s has multiple fallback children\n",
2103 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002104 return(0);
2105 }
2106 return(1);
2107 }
2108 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2109 if ((node->parent == NULL) ||
2110 (node->parent->type != XML_ELEMENT_NODE) ||
2111 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002112 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2113 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002114 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002115 xmlXIncludeErr(ctxt, node,
2116 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2117 "%s is not the child of an 'include'\n",
2118 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002119 }
2120 }
2121 }
Owen Taylor3473f882001-02-23 17:55:21 +00002122 return(0);
2123}
2124
2125/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002126 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002127 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002128 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002129 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002130 *
2131 * Implement the XInclude substitution on the XML document @doc
2132 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002133 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002134 * or the number of substitutions done.
2135 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002136static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002137xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002138 xmlNodePtr cur;
2139 int ret = 0;
2140 int i;
2141
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002142 if ((doc == NULL) || (tree == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002143 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002144 if (ctxt == NULL)
2145 return(-1);
2146
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002147 if (doc->URL != NULL) {
2148 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2149 if (ret < 0)
2150 return(-1);
2151 }
2152
Owen Taylor3473f882001-02-23 17:55:21 +00002153 /*
2154 * First phase: lookup the elements in the document
2155 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002156 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002157 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002158 xmlXIncludePreProcessNode(ctxt, cur);
2159 while (cur != NULL) {
2160 /* TODO: need to work on entities -> stack */
2161 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002162 (cur->children->type != XML_ENTITY_DECL) &&
2163 (cur->children->type != XML_XINCLUDE_START) &&
2164 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002165 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002166 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002167 xmlXIncludePreProcessNode(ctxt, cur);
2168 } else if (cur->next != NULL) {
2169 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002170 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002171 xmlXIncludePreProcessNode(ctxt, cur);
2172 } else {
2173 do {
2174 cur = cur->parent;
2175 if (cur == NULL) break; /* do */
2176 if (cur->next != NULL) {
2177 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002178 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002179 xmlXIncludePreProcessNode(ctxt, cur);
2180 break; /* do */
2181 }
2182 } while (cur != NULL);
2183 }
2184 }
2185
2186 /*
2187 * Second Phase : collect the infosets fragments
2188 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002189 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002190 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002191 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002192 }
2193
2194 /*
2195 * Third phase: extend the original document infoset.
William M. Brack6b1a28d2004-02-06 11:24:44 +00002196 *
2197 * Originally we bypassed the inclusion if there were any errors
2198 * encountered on any of the XIncludes. A bug was raised (bug
2199 * 132588) requesting that we output the XIncludes without error,
2200 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2201 * give some other problems in the future, but for now it seems to
2202 * work ok.
2203 *
Owen Taylor3473f882001-02-23 17:55:21 +00002204 */
William M. Brack6b1a28d2004-02-06 11:24:44 +00002205 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
2206 if ((ctxt->incTab[i]->inc != NULL) || (ctxt->incTab[i]->xptr != NULL))
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002207 xmlXIncludeIncludeNode(ctxt, i);
Owen Taylor3473f882001-02-23 17:55:21 +00002208 }
2209
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002210 if (doc->URL != NULL)
2211 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002212 return(ret);
2213}
2214
2215/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002216 * xmlXIncludeSetFlags:
2217 * @ctxt: an XInclude processing context
2218 * @flags: a set of xmlParserOption used for parsing XML includes
2219 *
2220 * Set the flags used for further processing of XML resources.
2221 *
2222 * Returns 0 in case of success and -1 in case of error.
2223 */
2224int
2225xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2226 if (ctxt == NULL)
2227 return(-1);
2228 ctxt->parseFlags = flags;
2229 return(0);
2230}
2231
2232/**
2233 * xmlXIncludeProcessFlags:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002234 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002235 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002236 *
2237 * Implement the XInclude substitution on the XML document @doc
2238 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002239 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002240 * or the number of substitutions done.
2241 */
2242int
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002243xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002244 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002245 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002246 int ret = 0;
2247
2248 if (doc == NULL)
2249 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002250 tree = xmlDocGetRootElement(doc);
2251 if (tree == NULL)
2252 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002253 ctxt = xmlXIncludeNewContext(doc);
2254 if (ctxt == NULL)
2255 return(-1);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002256 xmlXIncludeSetFlags(ctxt, flags);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002257 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2258 if ((ret >= 0) && (ctxt->nbErrors > 0))
2259 ret = -1;
2260
2261 xmlXIncludeFreeContext(ctxt);
2262 return(ret);
2263}
2264
2265/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002266 * xmlXIncludeProcess:
2267 * @doc: an XML document
2268 *
2269 * Implement the XInclude substitution on the XML document @doc
2270 *
2271 * Returns 0 if no substitution were done, -1 if some processing failed
2272 * or the number of substitutions done.
2273 */
2274int
2275xmlXIncludeProcess(xmlDocPtr doc) {
2276 return(xmlXIncludeProcessFlags(doc, 0));
2277}
2278
2279/**
2280 * xmlXIncludeProcessTreeFlags:
2281 * @tree: a node in an XML document
2282 * @flags: a set of xmlParserOption used for parsing XML includes
2283 *
2284 * Implement the XInclude substitution for the given subtree
2285 *
2286 * Returns 0 if no substitution were done, -1 if some processing failed
2287 * or the number of substitutions done.
2288 */
2289int
2290xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2291 xmlXIncludeCtxtPtr ctxt;
2292 int ret = 0;
2293
2294 if ((tree == NULL) || (tree->doc == NULL))
2295 return(-1);
2296 ctxt = xmlXIncludeNewContext(tree->doc);
2297 if (ctxt == NULL)
2298 return(-1);
2299 xmlXIncludeSetFlags(ctxt, flags);
2300 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2301 if ((ret >= 0) && (ctxt->nbErrors > 0))
2302 ret = -1;
2303
2304 xmlXIncludeFreeContext(ctxt);
2305 return(ret);
2306}
2307
2308/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002309 * xmlXIncludeProcessTree:
2310 * @tree: a node in an XML document
2311 *
2312 * Implement the XInclude substitution for the given subtree
2313 *
2314 * Returns 0 if no substitution were done, -1 if some processing failed
2315 * or the number of substitutions done.
2316 */
2317int
2318xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002319 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002320}
2321
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002322/**
2323 * xmlXIncludeProcessNode:
2324 * @ctxt: an existing XInclude context
2325 * @node: a node in an XML document
2326 *
2327 * Implement the XInclude substitution for the given subtree reusing
2328 * the informations and data coming from the given context.
2329 *
2330 * Returns 0 if no substitution were done, -1 if some processing failed
2331 * or the number of substitutions done.
2332 */
2333int
2334xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2335 int ret = 0;
2336
2337 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2338 return(-1);
2339 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2340 if ((ret >= 0) && (ctxt->nbErrors > 0))
2341 ret = -1;
2342 return(ret);
2343}
2344
Owen Taylor3473f882001-02-23 17:55:21 +00002345#else /* !LIBXML_XINCLUDE_ENABLED */
2346#endif