blob: 56f735005095701c2527cb7c4a5abf3e2cc2f942 [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);
461 xmlFreeDoc(pctxt->myDoc);
462 }
Daniel Veillard98485322003-08-14 15:44:40 +0000463 pctxt->myDoc = NULL;
464 }
465 xmlFreeParserCtxt(pctxt);
466
467 return(ret);
468}
469
470/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000471 * xmlXIncludeAddNode:
472 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000473 * @cur: the new node
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000474 *
475 * Add a new node to process to an XInclude context
476 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000477static int
478xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
479 xmlXIncludeRefPtr ref;
480 xmlURIPtr uri;
481 xmlChar *URL;
482 xmlChar *fragment = NULL;
483 xmlChar *href;
484 xmlChar *parse;
485 xmlChar *base;
486 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000487 int xml = 1, i; /* default Issue 64 */
488 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000489
490
491 if (ctxt == NULL)
492 return(-1);
493 if (cur == NULL)
494 return(-1);
495
496#ifdef DEBUG_XINCLUDE
497 xmlGenericError(xmlGenericErrorContext, "Add node\n");
498#endif
499 /*
500 * read the attributes
501 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000502 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000503 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000504 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
505 if (href == NULL)
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000506 return(-1);
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000507 local = 1;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000508 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000509 if (href[0] == '#')
510 local = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000511 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000512 if (parse != NULL) {
513 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
514 xml = 1;
515 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
516 xml = 0;
517 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000518 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
519 "invalid value %s for 'parse'\n", parse);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000520 if (href != NULL)
521 xmlFree(href);
522 if (parse != NULL)
523 xmlFree(parse);
524 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000525 }
526 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000527
528 /*
529 * compute the URI
530 */
531 base = xmlNodeGetBase(ctxt->doc, cur);
532 if (base == NULL) {
533 URI = xmlBuildURI(href, ctxt->doc->URL);
534 } else {
535 URI = xmlBuildURI(href, base);
536 }
537 if (URI == NULL) {
538 xmlChar *escbase;
539 xmlChar *eschref;
540 /*
541 * Some escaping may be needed
542 */
543 escbase = xmlURIEscape(base);
544 eschref = xmlURIEscape(href);
545 URI = xmlBuildURI(eschref, escbase);
546 if (escbase != NULL)
547 xmlFree(escbase);
548 if (eschref != NULL)
549 xmlFree(eschref);
550 }
551 if (parse != NULL)
552 xmlFree(parse);
553 if (href != NULL)
554 xmlFree(href);
555 if (base != NULL)
556 xmlFree(base);
557 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000558 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
559 "failed build URL\n", NULL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000560 return(-1);
561 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000562 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000563
564 /*
565 * Check the URL and remove any fragment identifier
566 */
567 uri = xmlParseURI((const char *)URI);
568 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000569 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
570 "invalid value URI %s\n", URI);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000571 if (fragment != NULL)
572 xmlFree(fragment);
573 xmlFree(URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000574 return(-1);
575 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000576
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000577 if (uri->fragment != NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000578 if (ctxt->legacy != 0) {
579 if (fragment == NULL) {
580 fragment = (xmlChar *) uri->fragment;
581 } else {
582 xmlFree(uri->fragment);
583 }
584 } else {
585 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
586 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
587 URI);
588 if (fragment != NULL)
589 xmlFree(fragment);
590 xmlFreeURI(uri);
591 xmlFree(URI);
592 return(-1);
593 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000594 uri->fragment = NULL;
595 }
596 URL = xmlSaveUri(uri);
597 xmlFreeURI(uri);
598 xmlFree(URI);
599 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000600 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
601 "invalid value URI %s\n", URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000602 if (fragment != NULL)
603 xmlFree(fragment);
604 return(-1);
605 }
606
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000607 /*
608 * Check the URL against the stack for recursions
609 */
610 if (!local) {
611 for (i = 0;i < ctxt->urlNr;i++) {
612 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000613 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
614 "detected a recursion in %s\n", URL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000615 return(-1);
616 }
617 }
618 }
619
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000620 ref = xmlXIncludeNewRef(ctxt, URL, cur);
621 if (ref == NULL) {
622 return(-1);
623 }
624 ref->fragment = fragment;
625 ref->doc = NULL;
626 ref->xml = xml;
627 ref->count = 1;
628 xmlFree(URL);
629 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000630}
631
632/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000633 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000634 * @ctxt: the XInclude context
635 * @doc: the new document
636 * @url: the associated URL
637 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000638 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000639 */
640static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000641xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000642 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000643 xmlXIncludeCtxtPtr newctxt;
644 int i;
645
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000646 /*
647 * Avoid recursion in already substitued resources
648 for (i = 0;i < ctxt->urlNr;i++) {
649 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
650 return;
651 }
652 */
653
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000654#ifdef DEBUG_XINCLUDE
655 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
656#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000657 /*
658 * Handle recursion here.
659 */
660
661 newctxt = xmlXIncludeNewContext(doc);
662 if (newctxt != NULL) {
663 /*
664 * Copy the existing document set
665 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000666 newctxt->incMax = ctxt->incMax;
667 newctxt->incNr = ctxt->incNr;
668 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
669 sizeof(newctxt->incTab[0]));
670 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000671 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000672 xmlFree(newctxt);
673 return;
674 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000675 /*
676 * copy the urlTab
677 */
678 newctxt->urlMax = ctxt->urlMax;
679 newctxt->urlNr = ctxt->urlNr;
680 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000681
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000682 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000683 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000684 */
685 newctxt->incBase = ctxt->incNr;
686 for (i = 0;i < ctxt->incNr;i++) {
687 newctxt->incTab[i] = ctxt->incTab[i];
688 newctxt->incTab[i]->count++; /* prevent the recursion from
689 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000690 }
Daniel Veillard8edf1c52003-07-22 20:52:14 +0000691 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000692 for (i = 0;i < ctxt->incNr;i++) {
693 newctxt->incTab[i]->count--;
694 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000695 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000696
697 /* urlTab may have been reallocated */
698 ctxt->urlTab = newctxt->urlTab;
699 ctxt->urlMax = newctxt->urlMax;
700
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000701 newctxt->urlMax = 0;
702 newctxt->urlNr = 0;
703 newctxt->urlTab = NULL;
704
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000705 xmlXIncludeFreeContext(newctxt);
706 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000707#ifdef DEBUG_XINCLUDE
708 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
709#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000710}
711
712/**
713 * xmlXIncludeAddTxt:
714 * @ctxt: the XInclude context
715 * @txt: the new text node
716 * @url: the associated URL
717 *
718 * Add a new txtument to the list
719 */
720static void
721xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000722#ifdef DEBUG_XINCLUDE
723 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
724#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000725 if (ctxt->txtMax == 0) {
726 ctxt->txtMax = 4;
727 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
728 sizeof(ctxt->txtTab[0]));
729 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000730 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000731 return;
732 }
733 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
734 sizeof(ctxt->txturlTab[0]));
735 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000736 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000737 return;
738 }
739 }
740 if (ctxt->txtNr >= ctxt->txtMax) {
741 ctxt->txtMax *= 2;
742 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
743 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
744 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000745 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000746 return;
747 }
748 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000749 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000750 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000751 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000752 return;
753 }
754 }
755 ctxt->txtTab[ctxt->txtNr] = txt;
756 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
757 ctxt->txtNr++;
758}
759
Owen Taylor3473f882001-02-23 17:55:21 +0000760/************************************************************************
761 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000762 * Node copy with specific semantic *
763 * *
764 ************************************************************************/
765
766/**
767 * xmlXIncludeCopyNode:
768 * @ctxt: the XInclude context
769 * @target: the document target
770 * @source: the document source
771 * @elem: the element
772 *
773 * Make a copy of the node while preserving the XInclude semantic
774 * of the Infoset copy
775 */
776static xmlNodePtr
777xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
778 xmlDocPtr source, xmlNodePtr elem) {
779 xmlNodePtr result = NULL;
780
781 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
782 (elem == NULL))
783 return(NULL);
784 if (elem->type == XML_DTD_NODE)
785 return(NULL);
786 result = xmlDocCopyNode(elem, target, 1);
787 return(result);
788}
789
790/**
791 * xmlXIncludeCopyNodeList:
792 * @ctxt: the XInclude context
793 * @target: the document target
794 * @source: the document source
795 * @elem: the element list
796 *
797 * Make a copy of the node list while preserving the XInclude semantic
798 * of the Infoset copy
799 */
800static xmlNodePtr
801xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
802 xmlDocPtr source, xmlNodePtr elem) {
803 xmlNodePtr cur, res, result = NULL, last = NULL;
804
805 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
806 (elem == NULL))
807 return(NULL);
808 cur = elem;
809 while (cur != NULL) {
810 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
811 if (res != NULL) {
812 if (result == NULL) {
813 result = last = res;
814 } else {
815 last->next = res;
816 res->prev = last;
817 last = res;
818 }
819 }
820 cur = cur->next;
821 }
822 return(result);
823}
824
825/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000826 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000827 * @cur: the node
828 * @no: the child number
829 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000830 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000831 */
832static xmlNodePtr
833xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
834 int i;
835 if (cur == NULL)
836 return(cur);
837 cur = cur->children;
838 for (i = 0;i <= no;cur = cur->next) {
839 if (cur == NULL)
840 return(cur);
841 if ((cur->type == XML_ELEMENT_NODE) ||
842 (cur->type == XML_DOCUMENT_NODE) ||
843 (cur->type == XML_HTML_DOCUMENT_NODE)) {
844 i++;
845 if (i == no)
846 break;
847 }
848 }
849 return(cur);
850}
851
William M. Brackf7eb7942003-12-31 07:59:17 +0000852xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000853/**
854 * xmlXIncludeCopyRange:
855 * @ctxt: the XInclude context
856 * @target: the document target
857 * @source: the document source
858 * @obj: the XPointer result from the evaluation.
859 *
860 * Build a node list tree copy of the XPointer result.
861 *
862 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000863 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000864 */
865static xmlNodePtr
866xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
867 xmlDocPtr source, xmlXPathObjectPtr range) {
868 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000869 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
870 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000871 /* pointers to traversal nodes */
872 xmlNodePtr start, cur, end;
873 int index1, index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000874 int level = 0, lastLevel = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000875
876 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
877 (range == NULL))
878 return(NULL);
879 if (range->type != XPATH_RANGE)
880 return(NULL);
881 start = (xmlNodePtr) range->user;
882
883 if (start == NULL)
884 return(NULL);
885 end = range->user2;
886 if (end == NULL)
887 return(xmlDocCopyNode(start, target, 1));
888
889 cur = start;
890 index1 = range->index;
891 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000892 /*
893 * level is depth of the current node under consideration
894 * list is the pointer to the root of the output tree
895 * listParent is a pointer to the parent of output tree (within
896 the included file) in case we need to add another level
897 * last is a pointer to the last node added to the output tree
898 * lastLevel is the depth of last (relative to the root)
899 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000900 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000901 /*
902 * Check if our output tree needs a parent
903 */
904 if (level < 0) {
905 while (level < 0) {
906 tmp2 = xmlDocCopyNode(listParent, target, 0);
907 xmlAddChild(tmp2, list);
908 list = tmp2;
909 listParent = listParent->parent;
910 level++;
911 }
912 last = list;
913 lastLevel = 0;
914 }
915 /*
916 * Check whether we need to change our insertion point
917 */
918 while (level < lastLevel) {
919 last = last->parent;
920 lastLevel --;
921 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000922 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000923 if (cur->type == XML_TEXT_NODE) {
924 const xmlChar *content = cur->content;
925 int len;
926
927 if (content == NULL) {
928 tmp = xmlNewTextLen(NULL, 0);
929 } else {
930 len = index2;
931 if ((cur == start) && (index1 > 1)) {
932 content += (index1 - 1);
933 len -= (index1 - 1);
934 index1 = 0;
935 } else {
936 len = index2;
937 }
938 tmp = xmlNewTextLen(content, len);
939 }
940 /* single sub text node selection */
941 if (list == NULL)
942 return(tmp);
943 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +0000944 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000945 xmlAddNextSibling(last, tmp);
946 else
William M. Brackf7eb7942003-12-31 07:59:17 +0000947 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000948 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +0000949 } else { /* ending node not a text node */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000950 tmp = xmlDocCopyNode(cur, target, 0);
William M. Brackf7eb7942003-12-31 07:59:17 +0000951 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000952 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000953 listParent = cur->parent;
954 } else {
955 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000956 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +0000957 else {
958 xmlAddChild(last, tmp);
959 lastLevel = level;
960 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000961 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000962 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000963
964 if (index2 > 1) {
965 end = xmlXIncludeGetNthChild(cur, index2 - 1);
966 index2 = 0;
967 }
968 if ((cur == start) && (index1 > 1)) {
969 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
970 index1 = 0;
971 } else {
972 cur = cur->children;
973 }
974 /*
975 * Now gather the remaining nodes from cur to end
976 */
977 continue; /* while */
978 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000979 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000980 if ((cur->type == XML_TEXT_NODE) ||
981 (cur->type == XML_CDATA_SECTION_NODE)) {
982 const xmlChar *content = cur->content;
983
984 if (content == NULL) {
985 tmp = xmlNewTextLen(NULL, 0);
986 } else {
987 if (index1 > 1) {
988 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +0000989 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000990 }
991 tmp = xmlNewText(content);
992 }
993 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000994 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +0000995 } else { /* Not text node */
William M. Brackf7eb7942003-12-31 07:59:17 +0000996 tmp = xmlDocCopyNode(cur, target, 0);
997 list = last = tmp;
998 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +0000999 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001000 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001001 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001002 index1 = 0;
1003 /*
1004 * Now gather the remaining nodes from cur to end
1005 */
1006 continue; /* while */
1007 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001008 }
1009 } else {
1010 tmp = NULL;
1011 switch (cur->type) {
1012 case XML_DTD_NODE:
1013 case XML_ELEMENT_DECL:
1014 case XML_ATTRIBUTE_DECL:
1015 case XML_ENTITY_NODE:
1016 /* Do not copy DTD informations */
1017 break;
1018 case XML_ENTITY_DECL:
1019 /* handle crossing entities -> stack needed */
1020 break;
1021 case XML_XINCLUDE_START:
1022 case XML_XINCLUDE_END:
1023 /* don't consider it part of the tree content */
1024 break;
1025 case XML_ATTRIBUTE_NODE:
1026 /* Humm, should not happen ! */
1027 break;
1028 default:
William M. Brack72ee48d2003-12-30 08:30:19 +00001029 tmp = xmlDocCopyNode(cur, target, 0);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001030 break;
1031 }
1032 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001033 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001034 xmlAddNextSibling(last, tmp);
1035 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001036 xmlAddChild(last, tmp);
1037 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001038 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001039 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001040 }
1041 }
1042 /*
1043 * Skip to next node in document order
1044 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001045 cur = xmlXPtrAdvanceNode(cur, &level);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001046 }
1047 return(list);
1048}
1049
1050/**
1051 * xmlXIncludeBuildNodeList:
1052 * @ctxt: the XInclude context
1053 * @target: the document target
1054 * @source: the document source
1055 * @obj: the XPointer result from the evaluation.
1056 *
1057 * Build a node list tree copy of the XPointer result.
1058 * This will drop Attributes and Namespace declarations.
1059 *
1060 * Returns an xmlNodePtr list or NULL.
1061 * the caller has to free the node tree.
1062 */
1063static xmlNodePtr
1064xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1065 xmlDocPtr source, xmlXPathObjectPtr obj) {
1066 xmlNodePtr list = NULL, last = NULL;
1067 int i;
1068
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001069 if (source == NULL)
1070 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001071 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1072 (obj == NULL))
1073 return(NULL);
1074 switch (obj->type) {
1075 case XPATH_NODESET: {
1076 xmlNodeSetPtr set = obj->nodesetval;
1077 if (set == NULL)
1078 return(NULL);
1079 for (i = 0;i < set->nodeNr;i++) {
1080 if (set->nodeTab[i] == NULL)
1081 continue;
1082 switch (set->nodeTab[i]->type) {
1083 case XML_TEXT_NODE:
1084 case XML_CDATA_SECTION_NODE:
1085 case XML_ELEMENT_NODE:
1086 case XML_ENTITY_REF_NODE:
1087 case XML_ENTITY_NODE:
1088 case XML_PI_NODE:
1089 case XML_COMMENT_NODE:
1090 case XML_DOCUMENT_NODE:
1091 case XML_HTML_DOCUMENT_NODE:
1092#ifdef LIBXML_DOCB_ENABLED
1093 case XML_DOCB_DOCUMENT_NODE:
1094#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001095 case XML_XINCLUDE_END:
1096 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001097 case XML_XINCLUDE_START: {
1098 xmlNodePtr tmp, cur = set->nodeTab[i];
1099
1100 cur = cur->next;
1101 while (cur != NULL) {
1102 switch(cur->type) {
1103 case XML_TEXT_NODE:
1104 case XML_CDATA_SECTION_NODE:
1105 case XML_ELEMENT_NODE:
1106 case XML_ENTITY_REF_NODE:
1107 case XML_ENTITY_NODE:
1108 case XML_PI_NODE:
1109 case XML_COMMENT_NODE:
1110 tmp = xmlXIncludeCopyNode(ctxt, target,
1111 source, cur);
1112 if (last == NULL) {
1113 list = last = tmp;
1114 } else {
1115 xmlAddNextSibling(last, tmp);
1116 last = tmp;
1117 }
1118 cur = cur->next;
1119 continue;
1120 default:
1121 break;
1122 }
1123 break;
1124 }
1125 continue;
1126 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001127 case XML_ATTRIBUTE_NODE:
1128 case XML_NAMESPACE_DECL:
1129 case XML_DOCUMENT_TYPE_NODE:
1130 case XML_DOCUMENT_FRAG_NODE:
1131 case XML_NOTATION_NODE:
1132 case XML_DTD_NODE:
1133 case XML_ELEMENT_DECL:
1134 case XML_ATTRIBUTE_DECL:
1135 case XML_ENTITY_DECL:
1136 continue; /* for */
1137 }
1138 if (last == NULL)
1139 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1140 set->nodeTab[i]);
1141 else {
1142 xmlAddNextSibling(last,
1143 xmlXIncludeCopyNode(ctxt, target, source,
1144 set->nodeTab[i]));
1145 if (last->next != NULL)
1146 last = last->next;
1147 }
1148 }
1149 break;
1150 }
1151 case XPATH_LOCATIONSET: {
1152 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1153 if (set == NULL)
1154 return(NULL);
1155 for (i = 0;i < set->locNr;i++) {
1156 if (last == NULL)
1157 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1158 set->locTab[i]);
1159 else
1160 xmlAddNextSibling(last,
1161 xmlXIncludeCopyXPointer(ctxt, target, source,
1162 set->locTab[i]));
1163 if (last != NULL) {
1164 while (last->next != NULL)
1165 last = last->next;
1166 }
1167 }
1168 break;
1169 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001170#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001171 case XPATH_RANGE:
1172 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001173#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001174 case XPATH_POINT:
1175 /* points are ignored in XInclude */
1176 break;
1177 default:
1178 break;
1179 }
1180 return(list);
1181}
1182/************************************************************************
1183 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001184 * XInclude I/O handling *
1185 * *
1186 ************************************************************************/
1187
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001188typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1189typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1190struct _xmlXIncludeMergeData {
1191 xmlDocPtr doc;
1192 xmlXIncludeCtxtPtr ctxt;
1193};
1194
Owen Taylor3473f882001-02-23 17:55:21 +00001195/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001196 * xmlXIncludeMergeOneEntity:
1197 * @ent: the entity
1198 * @doc: the including doc
1199 * @nr: the entity name
1200 *
1201 * Inplements the merge of one entity
1202 */
1203static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001204xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001205 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001206 xmlEntityPtr ret, prev;
1207 xmlDocPtr doc;
1208 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001209
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001210 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001211 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001212 ctxt = data->ctxt;
1213 doc = data->doc;
1214 if ((ctxt == NULL) || (doc == NULL))
1215 return;
1216 switch (ent->etype) {
1217 case XML_INTERNAL_PARAMETER_ENTITY:
1218 case XML_EXTERNAL_PARAMETER_ENTITY:
1219 case XML_INTERNAL_PREDEFINED_ENTITY:
1220 return;
1221 case XML_INTERNAL_GENERAL_ENTITY:
1222 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1223 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1224 break;
1225 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001226 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1227 ent->SystemID, ent->content);
1228 if (ret != NULL) {
1229 if (ent->URI != NULL)
1230 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001231 } else {
1232 prev = xmlGetDocEntity(doc, ent->name);
1233 if (prev != NULL) {
1234 if (ent->etype != prev->etype)
1235 goto error;
1236
1237 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1238 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1239 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001240 } else if ((ent->ExternalID != NULL) &&
1241 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001242 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1243 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001244 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1245 if (!xmlStrEqual(ent->content, prev->content))
1246 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001247 } else {
1248 goto error;
1249 }
1250
1251 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001252 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001253 return;
1254error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001255 switch (ent->etype) {
1256 case XML_INTERNAL_PARAMETER_ENTITY:
1257 case XML_EXTERNAL_PARAMETER_ENTITY:
1258 case XML_INTERNAL_PREDEFINED_ENTITY:
1259 case XML_INTERNAL_GENERAL_ENTITY:
1260 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1261 return;
1262 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1263 break;
1264 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001265 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1266 "mismatch in redefinition of entity %s\n",
1267 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001268}
1269
1270/**
1271 * xmlXIncludeMergeEntities:
1272 * @ctxt: an XInclude context
1273 * @doc: the including doc
1274 * @from: the included doc
1275 *
1276 * Inplements the entity merge
1277 *
1278 * Returns 0 if merge succeeded, -1 if some processing failed
1279 */
1280static int
1281xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1282 xmlDocPtr from) {
1283 xmlNodePtr cur;
1284 xmlDtdPtr target, source;
1285
1286 if (ctxt == NULL)
1287 return(-1);
1288
1289 if ((from == NULL) || (from->intSubset == NULL))
1290 return(0);
1291
1292 target = doc->intSubset;
1293 if (target == NULL) {
1294 cur = xmlDocGetRootElement(doc);
1295 if (cur == NULL)
1296 return(-1);
1297 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1298 if (target == NULL)
1299 return(-1);
1300 }
1301
1302 source = from->intSubset;
1303 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001304 xmlXIncludeMergeData data;
1305
1306 data.ctxt = ctxt;
1307 data.doc = doc;
1308
Daniel Veillard4287c572003-02-04 22:48:53 +00001309 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001310 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001311 }
1312 source = from->extSubset;
1313 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001314 xmlXIncludeMergeData data;
1315
1316 data.ctxt = ctxt;
1317 data.doc = doc;
1318
Daniel Veillard4287c572003-02-04 22:48:53 +00001319 /*
1320 * don't duplicate existing stuff when external subsets are the same
1321 */
1322 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1323 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1324 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001325 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001326 }
1327 }
1328 return(0);
1329}
1330
1331/**
Owen Taylor3473f882001-02-23 17:55:21 +00001332 * xmlXIncludeLoadDoc:
1333 * @ctxt: the XInclude context
1334 * @url: the associated URL
1335 * @nr: the xinclude node number
1336 *
1337 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001338 *
1339 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001340 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001341static int
Owen Taylor3473f882001-02-23 17:55:21 +00001342xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1343 xmlDocPtr doc;
1344 xmlURIPtr uri;
1345 xmlChar *URL;
1346 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001347 int i = 0;
1348
1349#ifdef DEBUG_XINCLUDE
1350 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1351#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001352 /*
1353 * Check the URL and remove any fragment identifier
1354 */
1355 uri = xmlParseURI((const char *)url);
1356 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001357 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1358 XML_XINCLUDE_HREF_URI,
1359 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001360 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001361 }
1362 if (uri->fragment != NULL) {
1363 fragment = (xmlChar *) uri->fragment;
1364 uri->fragment = NULL;
1365 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001366 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1367 (ctxt->incTab[nr]->fragment != NULL)) {
1368 if (fragment != NULL) xmlFree(fragment);
1369 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1370 }
Owen Taylor3473f882001-02-23 17:55:21 +00001371 URL = xmlSaveUri(uri);
1372 xmlFreeURI(uri);
1373 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001374 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1375 XML_XINCLUDE_HREF_URI,
1376 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001377 if (fragment != NULL)
1378 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001379 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001380 }
1381
1382 /*
1383 * Handling of references to the local document are done
1384 * directly through ctxt->doc.
1385 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001386 if ((URL[0] == 0) || (URL[0] == '#') ||
1387 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001388 doc = NULL;
1389 goto loaded;
1390 }
1391
1392 /*
1393 * Prevent reloading twice the document.
1394 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001395 for (i = 0; i < ctxt->incNr; i++) {
1396 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1397 (ctxt->incTab[i]->doc != NULL)) {
1398 doc = ctxt->incTab[i]->doc;
1399#ifdef DEBUG_XINCLUDE
1400 printf("Already loaded %s\n", URL);
1401#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001402 goto loaded;
1403 }
1404 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001405
Owen Taylor3473f882001-02-23 17:55:21 +00001406 /*
1407 * Load it.
1408 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001409#ifdef DEBUG_XINCLUDE
1410 printf("loading %s\n", URL);
1411#endif
Daniel Veillard98485322003-08-14 15:44:40 +00001412 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001413 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001414 xmlFree(URL);
1415 if (fragment != NULL)
1416 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001417 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001418 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001419 ctxt->incTab[nr]->doc = doc;
Daniel Veillard98485322003-08-14 15:44:40 +00001420 for (i = nr + 1; i < ctxt->incNr; i++) {
1421 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1422 ctxt->incTab[nr]->count++;
1423#ifdef DEBUG_XINCLUDE
1424 printf("Increasing %s count since reused\n", URL);
1425#endif
1426 break;
1427 }
1428 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001429
1430 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001431 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001432 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001433 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001434
1435 /*
1436 * We don't need the DTD anymore, free up space
1437 if (doc->intSubset != NULL) {
1438 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1439 xmlFreeNode((xmlNodePtr) doc->intSubset);
1440 doc->intSubset = NULL;
1441 }
1442 if (doc->extSubset != NULL) {
1443 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1444 xmlFreeNode((xmlNodePtr) doc->extSubset);
1445 doc->extSubset = NULL;
1446 }
1447 */
1448 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001449
1450loaded:
1451 if (fragment == NULL) {
1452 /*
1453 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001454 */
1455 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001456 {
1457 /* Hopefully a DTD declaration won't be copied from
1458 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001459 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001460 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001461 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001462 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001463 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001464 }
1465#ifdef LIBXML_XPTR_ENABLED
1466 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001467 /*
1468 * Computes the XPointer expression and make a copy used
1469 * as the replacement copy.
1470 */
1471 xmlXPathObjectPtr xptr;
1472 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001473 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001474
1475 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001476 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1477 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001478 } else {
1479 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1480 }
1481 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001482 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1483 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001484 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001485 xmlFree(URL);
1486 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001487 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001488 }
1489 xptr = xmlXPtrEval(fragment, xptrctxt);
1490 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001491 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1492 XML_XINCLUDE_XPTR_FAILED,
1493 "XPointer evaluation failed: #%s\n",
1494 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001495 xmlXPathFreeContext(xptrctxt);
1496 xmlFree(URL);
1497 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001498 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001499 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001500 switch (xptr->type) {
1501 case XPATH_UNDEFINED:
1502 case XPATH_BOOLEAN:
1503 case XPATH_NUMBER:
1504 case XPATH_STRING:
1505 case XPATH_POINT:
1506 case XPATH_USERS:
1507 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001508 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1509 XML_XINCLUDE_XPTR_RESULT,
1510 "XPointer is not a range: #%s\n",
1511 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001512 xmlXPathFreeContext(xptrctxt);
1513 xmlFree(URL);
1514 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001515 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001516 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001517 if ((xptr->nodesetval == NULL) ||
1518 (xptr->nodesetval->nodeNr <= 0)) {
1519 xmlXPathFreeContext(xptrctxt);
1520 xmlFree(URL);
1521 xmlFree(fragment);
1522 return(-1);
1523 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001524 case XPATH_RANGE:
1525 case XPATH_LOCATIONSET:
1526 break;
1527 }
1528 set = xptr->nodesetval;
1529 if (set != NULL) {
1530 for (i = 0;i < set->nodeNr;i++) {
1531 if (set->nodeTab[i] == NULL)
1532 continue;
1533 switch (set->nodeTab[i]->type) {
1534 case XML_TEXT_NODE:
1535 case XML_CDATA_SECTION_NODE:
1536 case XML_ELEMENT_NODE:
1537 case XML_ENTITY_REF_NODE:
1538 case XML_ENTITY_NODE:
1539 case XML_PI_NODE:
1540 case XML_COMMENT_NODE:
1541 case XML_DOCUMENT_NODE:
1542 case XML_HTML_DOCUMENT_NODE:
1543#ifdef LIBXML_DOCB_ENABLED
1544 case XML_DOCB_DOCUMENT_NODE:
1545#endif
1546 continue;
1547 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001548 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1549 XML_XINCLUDE_XPTR_RESULT,
1550 "XPointer selects an attribute: #%s\n",
1551 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001552 set->nodeTab[i] = NULL;
1553 continue;
1554 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001555 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1556 XML_XINCLUDE_XPTR_RESULT,
1557 "XPointer selects a namespace: #%s\n",
1558 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001559 set->nodeTab[i] = NULL;
1560 continue;
1561 case XML_DOCUMENT_TYPE_NODE:
1562 case XML_DOCUMENT_FRAG_NODE:
1563 case XML_NOTATION_NODE:
1564 case XML_DTD_NODE:
1565 case XML_ELEMENT_DECL:
1566 case XML_ATTRIBUTE_DECL:
1567 case XML_ENTITY_DECL:
1568 case XML_XINCLUDE_START:
1569 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001570 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1571 XML_XINCLUDE_XPTR_RESULT,
1572 "XPointer selects unexpected nodes: #%s\n",
1573 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001574 set->nodeTab[i] = NULL;
1575 set->nodeTab[i] = NULL;
1576 continue; /* for */
1577 }
1578 }
1579 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001580 if (doc == NULL) {
1581 ctxt->incTab[nr]->xptr = xptr;
1582 ctxt->incTab[nr]->inc = NULL;
1583 } else {
1584 ctxt->incTab[nr]->inc =
1585 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1586 xmlXPathFreeObject(xptr);
1587 }
Owen Taylor3473f882001-02-23 17:55:21 +00001588 xmlXPathFreeContext(xptrctxt);
1589 xmlFree(fragment);
1590 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001591#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001592
1593 /*
1594 * Do the xml:base fixup if needed
1595 */
1596 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1597 xmlNodePtr node;
1598
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001599 node = ctxt->incTab[nr]->inc;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001600 while (node != NULL) {
1601 if (node->type == XML_ELEMENT_NODE)
1602 xmlNodeSetBase(node, URL);
1603 node = node->next;
1604 }
1605 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001606 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1607 (ctxt->incTab[nr]->count <= 1)) {
1608#ifdef DEBUG_XINCLUDE
1609 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1610#endif
1611 xmlFreeDoc(ctxt->incTab[nr]->doc);
1612 ctxt->incTab[nr]->doc = NULL;
1613 }
Owen Taylor3473f882001-02-23 17:55:21 +00001614 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001615 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001616}
1617
1618/**
1619 * xmlXIncludeLoadTxt:
1620 * @ctxt: the XInclude context
1621 * @url: the associated URL
1622 * @nr: the xinclude node number
1623 *
1624 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001625 *
1626 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001627 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001628static int
Owen Taylor3473f882001-02-23 17:55:21 +00001629xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1630 xmlParserInputBufferPtr buf;
1631 xmlNodePtr node;
1632 xmlURIPtr uri;
1633 xmlChar *URL;
1634 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001635 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001636 xmlCharEncoding enc = (xmlCharEncoding) 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001637
Owen Taylor3473f882001-02-23 17:55:21 +00001638 /*
1639 * Check the URL and remove any fragment identifier
1640 */
1641 uri = xmlParseURI((const char *)url);
1642 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001643 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1644 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001645 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001646 }
1647 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001648 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1649 "fragment identifier forbidden for text: %s\n",
1650 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001651 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001652 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001653 }
1654 URL = xmlSaveUri(uri);
1655 xmlFreeURI(uri);
1656 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001657 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1658 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001659 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001660 }
1661
1662 /*
1663 * Handling of references to the local document are done
1664 * directly through ctxt->doc.
1665 */
1666 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001667 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1668 XML_XINCLUDE_TEXT_DOCUMENT,
1669 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001670 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001671 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001672 }
1673
1674 /*
1675 * Prevent reloading twice the document.
1676 */
1677 for (i = 0; i < ctxt->txtNr; i++) {
1678 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1679 node = xmlCopyNode(ctxt->txtTab[i], 1);
1680 goto loaded;
1681 }
1682 }
1683 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001684 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001685 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001686 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1687 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1688 }
1689 if (encoding != NULL) {
1690 /*
1691 * TODO: we should not have to remap to the xmlCharEncoding
1692 * predefined set, a better interface than
1693 * xmlParserInputBufferCreateFilename should allow any
1694 * encoding supported by iconv
1695 */
1696 enc = xmlParseCharEncoding((const char *) encoding);
1697 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001698 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1699 XML_XINCLUDE_UNKNOWN_ENCODING,
1700 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001701 xmlFree(encoding);
1702 xmlFree(URL);
1703 return(-1);
1704 }
1705 xmlFree(encoding);
1706 }
1707
1708 /*
1709 * Load it.
1710 */
1711 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001712 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001713 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001714 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001715 }
1716 node = xmlNewText(NULL);
1717
1718 /*
1719 * Scan all chars from the resource and add the to the node
1720 */
1721 while (xmlParserInputBufferRead(buf, 128) > 0) {
1722 int len;
1723 const xmlChar *content;
1724
1725 content = xmlBufferContent(buf->buffer);
1726 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001727 for (i = 0;i < len;) {
1728 int cur;
1729 int l;
1730
1731 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1732 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001733 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1734 XML_XINCLUDE_INVALID_CHAR,
1735 "%s contains invalid char\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001736 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001737 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001738 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001739 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001740 }
1741 xmlBufferShrink(buf->buffer, len);
1742 }
1743 xmlFreeParserInputBuffer(buf);
1744 xmlXIncludeAddTxt(ctxt, node, URL);
1745
1746loaded:
1747 /*
1748 * Add the element as the replacement copy.
1749 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001750 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001751 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001752 return(0);
1753}
1754
1755/**
1756 * xmlXIncludeLoadFallback:
1757 * @ctxt: the XInclude context
1758 * @fallback: the fallback node
1759 * @nr: the xinclude node number
1760 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001761 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001762 * in the XInclude context
1763 *
1764 * Returns 0 in case of success, -1 in case of failure
1765 */
1766static int
1767xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001768 xmlXIncludeCtxtPtr newctxt;
1769 int ret = 0;
1770
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001771 if ((fallback == NULL) || (ctxt == NULL))
1772 return(-1);
William M. Brackaae10522004-01-02 14:59:41 +00001773 /*
1774 * It's possible that the fallback also has 'includes'
1775 * (Bug 129969), so we re-process the fallback just in case
1776 */
1777 newctxt = xmlXIncludeNewContext(ctxt->doc);
1778 if (newctxt == NULL)
1779 return (-1);
1780 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1781 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
1782 if ((ret >=0) && (ctxt->nbErrors > 0))
1783 ret = -1;
1784 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001785
Daniel Veillard06503452002-12-13 10:42:08 +00001786 ctxt->incTab[nr]->inc = xmlCopyNodeList(fallback->children);
William M. Brackaae10522004-01-02 14:59:41 +00001787 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001788}
1789
1790/************************************************************************
1791 * *
1792 * XInclude Processing *
1793 * *
1794 ************************************************************************/
1795
1796/**
1797 * xmlXIncludePreProcessNode:
1798 * @ctxt: an XInclude context
1799 * @node: an XInclude node
1800 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001801 * Implement the XInclude preprocessing, currently just adding the element
1802 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001803 *
1804 * Returns the result list or NULL in case of error
1805 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001806static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001807xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1808 xmlXIncludeAddNode(ctxt, node);
1809 return(0);
1810}
1811
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001812/**
Owen Taylor3473f882001-02-23 17:55:21 +00001813 * xmlXIncludeLoadNode:
1814 * @ctxt: an XInclude context
1815 * @nr: the node number
1816 *
1817 * Find and load the infoset replacement for the given node.
1818 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001819 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001820 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001821static int
Owen Taylor3473f882001-02-23 17:55:21 +00001822xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1823 xmlNodePtr cur;
1824 xmlChar *href;
1825 xmlChar *parse;
1826 xmlChar *base;
1827 xmlChar *URI;
1828 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001829 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001830
1831 if (ctxt == NULL)
1832 return(-1);
1833 if ((nr < 0) || (nr >= ctxt->incNr))
1834 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001835 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001836 if (cur == NULL)
1837 return(-1);
1838
Owen Taylor3473f882001-02-23 17:55:21 +00001839 /*
1840 * read the attributes
1841 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001842 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00001843 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001844 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
1845 if (href == NULL)
1846 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001847 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001848 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00001849 if (parse != NULL) {
1850 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1851 xml = 1;
1852 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1853 xml = 0;
1854 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001855 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1856 XML_XINCLUDE_PARSE_VALUE,
1857 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00001858 if (href != NULL)
1859 xmlFree(href);
1860 if (parse != NULL)
1861 xmlFree(parse);
1862 return(-1);
1863 }
1864 }
1865
1866 /*
1867 * compute the URI
1868 */
1869 base = xmlNodeGetBase(ctxt->doc, cur);
1870 if (base == NULL) {
1871 URI = xmlBuildURI(href, ctxt->doc->URL);
1872 } else {
1873 URI = xmlBuildURI(href, base);
1874 }
1875 if (URI == NULL) {
1876 xmlChar *escbase;
1877 xmlChar *eschref;
1878 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001879 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00001880 */
1881 escbase = xmlURIEscape(base);
1882 eschref = xmlURIEscape(href);
1883 URI = xmlBuildURI(eschref, escbase);
1884 if (escbase != NULL)
1885 xmlFree(escbase);
1886 if (eschref != NULL)
1887 xmlFree(eschref);
1888 }
1889 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001890 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1891 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001892 if (parse != NULL)
1893 xmlFree(parse);
1894 if (href != NULL)
1895 xmlFree(href);
1896 if (base != NULL)
1897 xmlFree(base);
1898 return(-1);
1899 }
1900#ifdef DEBUG_XINCLUDE
1901 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
1902 xml ? "xml": "text");
1903 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
1904#endif
1905
1906 /*
1907 * Cleanup
1908 */
1909 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001910 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00001911 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
1912 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001913 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
1914 }
1915 if (ret < 0) {
1916 xmlNodePtr children;
1917
1918 /*
1919 * Time to try a fallback if availble
1920 */
1921#ifdef DEBUG_XINCLUDE
1922 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
1923#endif
1924 children = cur->children;
1925 while (children != NULL) {
1926 if ((children->type == XML_ELEMENT_NODE) &&
1927 (children->ns != NULL) &&
1928 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001929 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
1930 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001931 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00001932 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001933 break;
1934 }
1935 children = children->next;
1936 }
1937 }
1938 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001939 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1940 XML_XINCLUDE_NO_FALLBACK,
1941 "could not load %s, and no fallback was found\n",
1942 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001943 }
1944
1945 /*
1946 * Cleanup
1947 */
1948 if (URI != NULL)
1949 xmlFree(URI);
1950 if (parse != NULL)
1951 xmlFree(parse);
1952 if (href != NULL)
1953 xmlFree(href);
1954 if (base != NULL)
1955 xmlFree(base);
1956 return(0);
1957}
1958
1959/**
1960 * xmlXIncludeIncludeNode:
1961 * @ctxt: an XInclude context
1962 * @nr: the node number
1963 *
1964 * Inplement the infoset replacement for the given node
1965 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001966 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001967 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001968static int
Owen Taylor3473f882001-02-23 17:55:21 +00001969xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001970 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001971
1972 if (ctxt == NULL)
1973 return(-1);
1974 if ((nr < 0) || (nr >= ctxt->incNr))
1975 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001976 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001977 if (cur == NULL)
1978 return(-1);
1979
1980 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001981 * If we stored an XPointer a late computation may be needed
1982 */
1983 if ((ctxt->incTab[nr]->inc == NULL) &&
1984 (ctxt->incTab[nr]->xptr != NULL)) {
1985 ctxt->incTab[nr]->inc =
1986 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
1987 ctxt->incTab[nr]->xptr);
1988 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
1989 ctxt->incTab[nr]->xptr = NULL;
1990 }
1991 list = ctxt->incTab[nr]->inc;
1992 ctxt->incTab[nr]->inc = NULL;
1993
1994 /*
1995 * Check against the risk of generating a multi-rooted document
1996 */
1997 if ((cur->parent != NULL) &&
1998 (cur->parent->type != XML_ELEMENT_NODE)) {
1999 int nb_elem = 0;
2000
2001 tmp = list;
2002 while (tmp != NULL) {
2003 if (tmp->type == XML_ELEMENT_NODE)
2004 nb_elem++;
2005 tmp = tmp->next;
2006 }
2007 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002008 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2009 XML_XINCLUDE_MULTIPLE_ROOT,
2010 "XInclude error: would result in multiple root nodes\n",
2011 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002012 return(-1);
2013 }
2014 }
2015
2016 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002017 * Change the current node as an XInclude start one, and add an
2018 * entity end one
2019 */
2020 cur->type = XML_XINCLUDE_START;
2021 end = xmlNewNode(cur->ns, cur->name);
2022 if (end == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002023 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_BUILD_FAILED,
2024 "failed to build node\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002025 return(-1);
2026 }
2027 end->type = XML_XINCLUDE_END;
2028 xmlAddNextSibling(cur, end);
2029
2030 /*
2031 * Add the list of nodes
2032 */
Owen Taylor3473f882001-02-23 17:55:21 +00002033 while (list != NULL) {
2034 cur = list;
2035 list = list->next;
2036
2037 xmlAddPrevSibling(end, cur);
2038 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002039
2040
Owen Taylor3473f882001-02-23 17:55:21 +00002041 return(0);
2042}
2043
2044/**
2045 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002046 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002047 * @node: an XInclude node
2048 *
2049 * test if the node is an XInclude node
2050 *
2051 * Returns 1 true, 0 otherwise
2052 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002053static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002054xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002055 if (node == NULL)
2056 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002057 if (node->type != XML_ELEMENT_NODE)
2058 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002059 if (node->ns == NULL)
2060 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002061 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2062 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2063 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2064 if (ctxt->legacy == 0) {
2065 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2066 "Deprecated XInclude namespace found, use %s",
2067 XINCLUDE_NS);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002068 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002069 }
2070 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002071 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2072 xmlNodePtr child = node->children;
2073 int nb_fallback = 0;
2074
2075 while (child != NULL) {
2076 if ((child->type == XML_ELEMENT_NODE) &&
2077 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002078 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2079 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002080 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002081 xmlXIncludeErr(ctxt, node,
2082 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2083 "%s has an 'include' child\n",
2084 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002085 return(0);
2086 }
2087 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2088 nb_fallback++;
2089 }
2090 }
2091 child = child->next;
2092 }
2093 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002094 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2095 "%s has multiple fallback children\n",
2096 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002097 return(0);
2098 }
2099 return(1);
2100 }
2101 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2102 if ((node->parent == NULL) ||
2103 (node->parent->type != XML_ELEMENT_NODE) ||
2104 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002105 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2106 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002107 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002108 xmlXIncludeErr(ctxt, node,
2109 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2110 "%s is not the child of an 'include'\n",
2111 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002112 }
2113 }
2114 }
Owen Taylor3473f882001-02-23 17:55:21 +00002115 return(0);
2116}
2117
2118/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002119 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002120 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002121 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002122 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002123 *
2124 * Implement the XInclude substitution on the XML document @doc
2125 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002126 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002127 * or the number of substitutions done.
2128 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002129static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002130xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002131 xmlNodePtr cur;
2132 int ret = 0;
2133 int i;
2134
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002135 if ((doc == NULL) || (tree == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002136 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002137 if (ctxt == NULL)
2138 return(-1);
2139
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002140 if (doc->URL != NULL) {
2141 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2142 if (ret < 0)
2143 return(-1);
2144 }
2145
Owen Taylor3473f882001-02-23 17:55:21 +00002146 /*
2147 * First phase: lookup the elements in the document
2148 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002149 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002150 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002151 xmlXIncludePreProcessNode(ctxt, cur);
2152 while (cur != NULL) {
2153 /* TODO: need to work on entities -> stack */
2154 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002155 (cur->children->type != XML_ENTITY_DECL) &&
2156 (cur->children->type != XML_XINCLUDE_START) &&
2157 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002158 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002159 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002160 xmlXIncludePreProcessNode(ctxt, cur);
2161 } else if (cur->next != NULL) {
2162 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002163 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002164 xmlXIncludePreProcessNode(ctxt, cur);
2165 } else {
2166 do {
2167 cur = cur->parent;
2168 if (cur == NULL) break; /* do */
2169 if (cur->next != NULL) {
2170 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002171 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002172 xmlXIncludePreProcessNode(ctxt, cur);
2173 break; /* do */
2174 }
2175 } while (cur != NULL);
2176 }
2177 }
2178
2179 /*
2180 * Second Phase : collect the infosets fragments
2181 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002182 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002183 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002184 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002185 }
2186
2187 /*
2188 * Third phase: extend the original document infoset.
2189 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002190 if (ctxt->nbErrors == 0) {
2191 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
2192 xmlXIncludeIncludeNode(ctxt, i);
2193 }
Owen Taylor3473f882001-02-23 17:55:21 +00002194 }
2195
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002196 if (doc->URL != NULL)
2197 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002198 return(ret);
2199}
2200
2201/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002202 * xmlXIncludeSetFlags:
2203 * @ctxt: an XInclude processing context
2204 * @flags: a set of xmlParserOption used for parsing XML includes
2205 *
2206 * Set the flags used for further processing of XML resources.
2207 *
2208 * Returns 0 in case of success and -1 in case of error.
2209 */
2210int
2211xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2212 if (ctxt == NULL)
2213 return(-1);
2214 ctxt->parseFlags = flags;
2215 return(0);
2216}
2217
2218/**
2219 * xmlXIncludeProcessFlags:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002220 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002221 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002222 *
2223 * Implement the XInclude substitution on the XML document @doc
2224 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002225 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002226 * or the number of substitutions done.
2227 */
2228int
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002229xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002230 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002231 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002232 int ret = 0;
2233
2234 if (doc == NULL)
2235 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002236 tree = xmlDocGetRootElement(doc);
2237 if (tree == NULL)
2238 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002239 ctxt = xmlXIncludeNewContext(doc);
2240 if (ctxt == NULL)
2241 return(-1);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002242 xmlXIncludeSetFlags(ctxt, flags);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002243 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2244 if ((ret >= 0) && (ctxt->nbErrors > 0))
2245 ret = -1;
2246
2247 xmlXIncludeFreeContext(ctxt);
2248 return(ret);
2249}
2250
2251/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002252 * xmlXIncludeProcess:
2253 * @doc: an XML document
2254 *
2255 * Implement the XInclude substitution on the XML document @doc
2256 *
2257 * Returns 0 if no substitution were done, -1 if some processing failed
2258 * or the number of substitutions done.
2259 */
2260int
2261xmlXIncludeProcess(xmlDocPtr doc) {
2262 return(xmlXIncludeProcessFlags(doc, 0));
2263}
2264
2265/**
2266 * xmlXIncludeProcessTreeFlags:
2267 * @tree: a node in an XML document
2268 * @flags: a set of xmlParserOption used for parsing XML includes
2269 *
2270 * Implement the XInclude substitution for the given subtree
2271 *
2272 * Returns 0 if no substitution were done, -1 if some processing failed
2273 * or the number of substitutions done.
2274 */
2275int
2276xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2277 xmlXIncludeCtxtPtr ctxt;
2278 int ret = 0;
2279
2280 if ((tree == NULL) || (tree->doc == NULL))
2281 return(-1);
2282 ctxt = xmlXIncludeNewContext(tree->doc);
2283 if (ctxt == NULL)
2284 return(-1);
2285 xmlXIncludeSetFlags(ctxt, flags);
2286 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2287 if ((ret >= 0) && (ctxt->nbErrors > 0))
2288 ret = -1;
2289
2290 xmlXIncludeFreeContext(ctxt);
2291 return(ret);
2292}
2293
2294/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002295 * xmlXIncludeProcessTree:
2296 * @tree: a node in an XML document
2297 *
2298 * Implement the XInclude substitution for the given subtree
2299 *
2300 * Returns 0 if no substitution were done, -1 if some processing failed
2301 * or the number of substitutions done.
2302 */
2303int
2304xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002305 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002306}
2307
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002308/**
2309 * xmlXIncludeProcessNode:
2310 * @ctxt: an existing XInclude context
2311 * @node: a node in an XML document
2312 *
2313 * Implement the XInclude substitution for the given subtree reusing
2314 * the informations and data coming from the given context.
2315 *
2316 * Returns 0 if no substitution were done, -1 if some processing failed
2317 * or the number of substitutions done.
2318 */
2319int
2320xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2321 int ret = 0;
2322
2323 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2324 return(-1);
2325 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2326 if ((ret >= 0) && (ctxt->nbErrors > 0))
2327 ret = -1;
2328 return(ret);
2329}
2330
Owen Taylor3473f882001-02-23 17:55:21 +00002331#else /* !LIBXML_XINCLUDE_ENABLED */
2332#endif