blob: 0b34c3e31f6a0b9e8e17e88a1680ebe88e28f07e [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;
457 xmlFreeDoc(pctxt->myDoc);
458 pctxt->myDoc = NULL;
459 }
460 xmlFreeParserCtxt(pctxt);
461
462 return(ret);
463}
464
465/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000466 * xmlXIncludeAddNode:
467 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000468 * @cur: the new node
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000469 *
470 * Add a new node to process to an XInclude context
471 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000472static int
473xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
474 xmlXIncludeRefPtr ref;
475 xmlURIPtr uri;
476 xmlChar *URL;
477 xmlChar *fragment = NULL;
478 xmlChar *href;
479 xmlChar *parse;
480 xmlChar *base;
481 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000482 int xml = 1, i; /* default Issue 64 */
483 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000484
485
486 if (ctxt == NULL)
487 return(-1);
488 if (cur == NULL)
489 return(-1);
490
491#ifdef DEBUG_XINCLUDE
492 xmlGenericError(xmlGenericErrorContext, "Add node\n");
493#endif
494 /*
495 * read the attributes
496 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000497 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000498 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000499 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
500 if (href == NULL)
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000501 return(-1);
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000502 local = 1;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000503 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000504 if (href[0] == '#')
505 local = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000506 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000507 if (parse != NULL) {
508 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
509 xml = 1;
510 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
511 xml = 0;
512 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000513 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
514 "invalid value %s for 'parse'\n", parse);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000515 if (href != NULL)
516 xmlFree(href);
517 if (parse != NULL)
518 xmlFree(parse);
519 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000520 }
521 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000522
523 /*
524 * compute the URI
525 */
526 base = xmlNodeGetBase(ctxt->doc, cur);
527 if (base == NULL) {
528 URI = xmlBuildURI(href, ctxt->doc->URL);
529 } else {
530 URI = xmlBuildURI(href, base);
531 }
532 if (URI == NULL) {
533 xmlChar *escbase;
534 xmlChar *eschref;
535 /*
536 * Some escaping may be needed
537 */
538 escbase = xmlURIEscape(base);
539 eschref = xmlURIEscape(href);
540 URI = xmlBuildURI(eschref, escbase);
541 if (escbase != NULL)
542 xmlFree(escbase);
543 if (eschref != NULL)
544 xmlFree(eschref);
545 }
546 if (parse != NULL)
547 xmlFree(parse);
548 if (href != NULL)
549 xmlFree(href);
550 if (base != NULL)
551 xmlFree(base);
552 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000553 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
554 "failed build URL\n", NULL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000555 return(-1);
556 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000557 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000558
559 /*
560 * Check the URL and remove any fragment identifier
561 */
562 uri = xmlParseURI((const char *)URI);
563 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000564 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
565 "invalid value URI %s\n", URI);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000566 if (fragment != NULL)
567 xmlFree(fragment);
568 xmlFree(URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000569 return(-1);
570 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000571
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000572 if (uri->fragment != NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000573 if (ctxt->legacy != 0) {
574 if (fragment == NULL) {
575 fragment = (xmlChar *) uri->fragment;
576 } else {
577 xmlFree(uri->fragment);
578 }
579 } else {
580 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
581 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
582 URI);
583 if (fragment != NULL)
584 xmlFree(fragment);
585 xmlFreeURI(uri);
586 xmlFree(URI);
587 return(-1);
588 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000589 uri->fragment = NULL;
590 }
591 URL = xmlSaveUri(uri);
592 xmlFreeURI(uri);
593 xmlFree(URI);
594 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000595 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
596 "invalid value URI %s\n", URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000597 if (fragment != NULL)
598 xmlFree(fragment);
599 return(-1);
600 }
601
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000602 /*
603 * Check the URL against the stack for recursions
604 */
605 if (!local) {
606 for (i = 0;i < ctxt->urlNr;i++) {
607 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000608 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
609 "detected a recursion in %s\n", URL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000610 return(-1);
611 }
612 }
613 }
614
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000615 ref = xmlXIncludeNewRef(ctxt, URL, cur);
616 if (ref == NULL) {
617 return(-1);
618 }
619 ref->fragment = fragment;
620 ref->doc = NULL;
621 ref->xml = xml;
622 ref->count = 1;
623 xmlFree(URL);
624 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000625}
626
627/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000628 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000629 * @ctxt: the XInclude context
630 * @doc: the new document
631 * @url: the associated URL
632 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000633 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000634 */
635static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000636xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000637 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000638 xmlXIncludeCtxtPtr newctxt;
639 int i;
640
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000641 /*
642 * Avoid recursion in already substitued resources
643 for (i = 0;i < ctxt->urlNr;i++) {
644 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
645 return;
646 }
647 */
648
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000649#ifdef DEBUG_XINCLUDE
650 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
651#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000652 /*
653 * Handle recursion here.
654 */
655
656 newctxt = xmlXIncludeNewContext(doc);
657 if (newctxt != NULL) {
658 /*
659 * Copy the existing document set
660 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000661 newctxt->incMax = ctxt->incMax;
662 newctxt->incNr = ctxt->incNr;
663 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
664 sizeof(newctxt->incTab[0]));
665 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000666 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000667 xmlFree(newctxt);
668 return;
669 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000670 /*
671 * copy the urlTab
672 */
673 newctxt->urlMax = ctxt->urlMax;
674 newctxt->urlNr = ctxt->urlNr;
675 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000676
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000677 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000678 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000679 */
680 newctxt->incBase = ctxt->incNr;
681 for (i = 0;i < ctxt->incNr;i++) {
682 newctxt->incTab[i] = ctxt->incTab[i];
683 newctxt->incTab[i]->count++; /* prevent the recursion from
684 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000685 }
Daniel Veillard8edf1c52003-07-22 20:52:14 +0000686 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000687 for (i = 0;i < ctxt->incNr;i++) {
688 newctxt->incTab[i]->count--;
689 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000690 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000691
692 /* urlTab may have been reallocated */
693 ctxt->urlTab = newctxt->urlTab;
694 ctxt->urlMax = newctxt->urlMax;
695
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000696 newctxt->urlMax = 0;
697 newctxt->urlNr = 0;
698 newctxt->urlTab = NULL;
699
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000700 xmlXIncludeFreeContext(newctxt);
701 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000702#ifdef DEBUG_XINCLUDE
703 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
704#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000705}
706
707/**
708 * xmlXIncludeAddTxt:
709 * @ctxt: the XInclude context
710 * @txt: the new text node
711 * @url: the associated URL
712 *
713 * Add a new txtument to the list
714 */
715static void
716xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000717#ifdef DEBUG_XINCLUDE
718 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
719#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000720 if (ctxt->txtMax == 0) {
721 ctxt->txtMax = 4;
722 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
723 sizeof(ctxt->txtTab[0]));
724 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000725 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000726 return;
727 }
728 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
729 sizeof(ctxt->txturlTab[0]));
730 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000731 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000732 return;
733 }
734 }
735 if (ctxt->txtNr >= ctxt->txtMax) {
736 ctxt->txtMax *= 2;
737 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
738 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
739 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000740 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000741 return;
742 }
743 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000744 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000745 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000746 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000747 return;
748 }
749 }
750 ctxt->txtTab[ctxt->txtNr] = txt;
751 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
752 ctxt->txtNr++;
753}
754
Owen Taylor3473f882001-02-23 17:55:21 +0000755/************************************************************************
756 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000757 * Node copy with specific semantic *
758 * *
759 ************************************************************************/
760
761/**
762 * xmlXIncludeCopyNode:
763 * @ctxt: the XInclude context
764 * @target: the document target
765 * @source: the document source
766 * @elem: the element
767 *
768 * Make a copy of the node while preserving the XInclude semantic
769 * of the Infoset copy
770 */
771static xmlNodePtr
772xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
773 xmlDocPtr source, xmlNodePtr elem) {
774 xmlNodePtr result = NULL;
775
776 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
777 (elem == NULL))
778 return(NULL);
779 if (elem->type == XML_DTD_NODE)
780 return(NULL);
781 result = xmlDocCopyNode(elem, target, 1);
782 return(result);
783}
784
785/**
786 * xmlXIncludeCopyNodeList:
787 * @ctxt: the XInclude context
788 * @target: the document target
789 * @source: the document source
790 * @elem: the element list
791 *
792 * Make a copy of the node list while preserving the XInclude semantic
793 * of the Infoset copy
794 */
795static xmlNodePtr
796xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
797 xmlDocPtr source, xmlNodePtr elem) {
798 xmlNodePtr cur, res, result = NULL, last = NULL;
799
800 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
801 (elem == NULL))
802 return(NULL);
803 cur = elem;
804 while (cur != NULL) {
805 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
806 if (res != NULL) {
807 if (result == NULL) {
808 result = last = res;
809 } else {
810 last->next = res;
811 res->prev = last;
812 last = res;
813 }
814 }
815 cur = cur->next;
816 }
817 return(result);
818}
819
820/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000821 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000822 * @cur: the node
823 * @no: the child number
824 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000825 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000826 */
827static xmlNodePtr
828xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
829 int i;
830 if (cur == NULL)
831 return(cur);
832 cur = cur->children;
833 for (i = 0;i <= no;cur = cur->next) {
834 if (cur == NULL)
835 return(cur);
836 if ((cur->type == XML_ELEMENT_NODE) ||
837 (cur->type == XML_DOCUMENT_NODE) ||
838 (cur->type == XML_HTML_DOCUMENT_NODE)) {
839 i++;
840 if (i == no)
841 break;
842 }
843 }
844 return(cur);
845}
846
William M. Brackf7eb7942003-12-31 07:59:17 +0000847xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000848/**
849 * xmlXIncludeCopyRange:
850 * @ctxt: the XInclude context
851 * @target: the document target
852 * @source: the document source
853 * @obj: the XPointer result from the evaluation.
854 *
855 * Build a node list tree copy of the XPointer result.
856 *
857 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000858 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000859 */
860static xmlNodePtr
861xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
862 xmlDocPtr source, xmlXPathObjectPtr range) {
863 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000864 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
865 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000866 /* pointers to traversal nodes */
867 xmlNodePtr start, cur, end;
868 int index1, index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000869 int level = 0, lastLevel = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000870
871 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
872 (range == NULL))
873 return(NULL);
874 if (range->type != XPATH_RANGE)
875 return(NULL);
876 start = (xmlNodePtr) range->user;
877
878 if (start == NULL)
879 return(NULL);
880 end = range->user2;
881 if (end == NULL)
882 return(xmlDocCopyNode(start, target, 1));
883
884 cur = start;
885 index1 = range->index;
886 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000887 /*
888 * level is depth of the current node under consideration
889 * list is the pointer to the root of the output tree
890 * listParent is a pointer to the parent of output tree (within
891 the included file) in case we need to add another level
892 * last is a pointer to the last node added to the output tree
893 * lastLevel is the depth of last (relative to the root)
894 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000895 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000896 /*
897 * Check if our output tree needs a parent
898 */
899 if (level < 0) {
900 while (level < 0) {
901 tmp2 = xmlDocCopyNode(listParent, target, 0);
902 xmlAddChild(tmp2, list);
903 list = tmp2;
904 listParent = listParent->parent;
905 level++;
906 }
907 last = list;
908 lastLevel = 0;
909 }
910 /*
911 * Check whether we need to change our insertion point
912 */
913 while (level < lastLevel) {
914 last = last->parent;
915 lastLevel --;
916 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000917 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000918 if (cur->type == XML_TEXT_NODE) {
919 const xmlChar *content = cur->content;
920 int len;
921
922 if (content == NULL) {
923 tmp = xmlNewTextLen(NULL, 0);
924 } else {
925 len = index2;
926 if ((cur == start) && (index1 > 1)) {
927 content += (index1 - 1);
928 len -= (index1 - 1);
929 index1 = 0;
930 } else {
931 len = index2;
932 }
933 tmp = xmlNewTextLen(content, len);
934 }
935 /* single sub text node selection */
936 if (list == NULL)
937 return(tmp);
938 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +0000939 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000940 xmlAddNextSibling(last, tmp);
941 else
William M. Brackf7eb7942003-12-31 07:59:17 +0000942 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000943 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +0000944 } else { /* ending node not a text node */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000945 tmp = xmlDocCopyNode(cur, target, 0);
William M. Brackf7eb7942003-12-31 07:59:17 +0000946 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000947 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000948 listParent = cur->parent;
949 } else {
950 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000951 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +0000952 else {
953 xmlAddChild(last, tmp);
954 lastLevel = level;
955 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000956 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000957 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000958
959 if (index2 > 1) {
960 end = xmlXIncludeGetNthChild(cur, index2 - 1);
961 index2 = 0;
962 }
963 if ((cur == start) && (index1 > 1)) {
964 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
965 index1 = 0;
966 } else {
967 cur = cur->children;
968 }
969 /*
970 * Now gather the remaining nodes from cur to end
971 */
972 continue; /* while */
973 }
William M. Brackf7eb7942003-12-31 07:59:17 +0000974 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000975 if ((cur->type == XML_TEXT_NODE) ||
976 (cur->type == XML_CDATA_SECTION_NODE)) {
977 const xmlChar *content = cur->content;
978
979 if (content == NULL) {
980 tmp = xmlNewTextLen(NULL, 0);
981 } else {
982 if (index1 > 1) {
983 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +0000984 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000985 }
986 tmp = xmlNewText(content);
987 }
988 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +0000989 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +0000990 } else { /* Not text node */
William M. Brackf7eb7942003-12-31 07:59:17 +0000991 tmp = xmlDocCopyNode(cur, target, 0);
992 list = last = tmp;
993 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +0000994 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000995 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +0000996 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000997 index1 = 0;
998 /*
999 * Now gather the remaining nodes from cur to end
1000 */
1001 continue; /* while */
1002 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001003 }
1004 } else {
1005 tmp = NULL;
1006 switch (cur->type) {
1007 case XML_DTD_NODE:
1008 case XML_ELEMENT_DECL:
1009 case XML_ATTRIBUTE_DECL:
1010 case XML_ENTITY_NODE:
1011 /* Do not copy DTD informations */
1012 break;
1013 case XML_ENTITY_DECL:
1014 /* handle crossing entities -> stack needed */
1015 break;
1016 case XML_XINCLUDE_START:
1017 case XML_XINCLUDE_END:
1018 /* don't consider it part of the tree content */
1019 break;
1020 case XML_ATTRIBUTE_NODE:
1021 /* Humm, should not happen ! */
1022 break;
1023 default:
William M. Brack72ee48d2003-12-30 08:30:19 +00001024 tmp = xmlDocCopyNode(cur, target, 0);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001025 break;
1026 }
1027 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001028 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001029 xmlAddNextSibling(last, tmp);
1030 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001031 xmlAddChild(last, tmp);
1032 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001033 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001034 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001035 }
1036 }
1037 /*
1038 * Skip to next node in document order
1039 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001040 cur = xmlXPtrAdvanceNode(cur, &level);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001041 }
1042 return(list);
1043}
1044
1045/**
1046 * xmlXIncludeBuildNodeList:
1047 * @ctxt: the XInclude context
1048 * @target: the document target
1049 * @source: the document source
1050 * @obj: the XPointer result from the evaluation.
1051 *
1052 * Build a node list tree copy of the XPointer result.
1053 * This will drop Attributes and Namespace declarations.
1054 *
1055 * Returns an xmlNodePtr list or NULL.
1056 * the caller has to free the node tree.
1057 */
1058static xmlNodePtr
1059xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1060 xmlDocPtr source, xmlXPathObjectPtr obj) {
1061 xmlNodePtr list = NULL, last = NULL;
1062 int i;
1063
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001064 if (source == NULL)
1065 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001066 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1067 (obj == NULL))
1068 return(NULL);
1069 switch (obj->type) {
1070 case XPATH_NODESET: {
1071 xmlNodeSetPtr set = obj->nodesetval;
1072 if (set == NULL)
1073 return(NULL);
1074 for (i = 0;i < set->nodeNr;i++) {
1075 if (set->nodeTab[i] == NULL)
1076 continue;
1077 switch (set->nodeTab[i]->type) {
1078 case XML_TEXT_NODE:
1079 case XML_CDATA_SECTION_NODE:
1080 case XML_ELEMENT_NODE:
1081 case XML_ENTITY_REF_NODE:
1082 case XML_ENTITY_NODE:
1083 case XML_PI_NODE:
1084 case XML_COMMENT_NODE:
1085 case XML_DOCUMENT_NODE:
1086 case XML_HTML_DOCUMENT_NODE:
1087#ifdef LIBXML_DOCB_ENABLED
1088 case XML_DOCB_DOCUMENT_NODE:
1089#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001090 case XML_XINCLUDE_END:
1091 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001092 case XML_XINCLUDE_START: {
1093 xmlNodePtr tmp, cur = set->nodeTab[i];
1094
1095 cur = cur->next;
1096 while (cur != NULL) {
1097 switch(cur->type) {
1098 case XML_TEXT_NODE:
1099 case XML_CDATA_SECTION_NODE:
1100 case XML_ELEMENT_NODE:
1101 case XML_ENTITY_REF_NODE:
1102 case XML_ENTITY_NODE:
1103 case XML_PI_NODE:
1104 case XML_COMMENT_NODE:
1105 tmp = xmlXIncludeCopyNode(ctxt, target,
1106 source, cur);
1107 if (last == NULL) {
1108 list = last = tmp;
1109 } else {
1110 xmlAddNextSibling(last, tmp);
1111 last = tmp;
1112 }
1113 cur = cur->next;
1114 continue;
1115 default:
1116 break;
1117 }
1118 break;
1119 }
1120 continue;
1121 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001122 case XML_ATTRIBUTE_NODE:
1123 case XML_NAMESPACE_DECL:
1124 case XML_DOCUMENT_TYPE_NODE:
1125 case XML_DOCUMENT_FRAG_NODE:
1126 case XML_NOTATION_NODE:
1127 case XML_DTD_NODE:
1128 case XML_ELEMENT_DECL:
1129 case XML_ATTRIBUTE_DECL:
1130 case XML_ENTITY_DECL:
1131 continue; /* for */
1132 }
1133 if (last == NULL)
1134 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1135 set->nodeTab[i]);
1136 else {
1137 xmlAddNextSibling(last,
1138 xmlXIncludeCopyNode(ctxt, target, source,
1139 set->nodeTab[i]));
1140 if (last->next != NULL)
1141 last = last->next;
1142 }
1143 }
1144 break;
1145 }
1146 case XPATH_LOCATIONSET: {
1147 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1148 if (set == NULL)
1149 return(NULL);
1150 for (i = 0;i < set->locNr;i++) {
1151 if (last == NULL)
1152 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1153 set->locTab[i]);
1154 else
1155 xmlAddNextSibling(last,
1156 xmlXIncludeCopyXPointer(ctxt, target, source,
1157 set->locTab[i]));
1158 if (last != NULL) {
1159 while (last->next != NULL)
1160 last = last->next;
1161 }
1162 }
1163 break;
1164 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001165#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001166 case XPATH_RANGE:
1167 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001168#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001169 case XPATH_POINT:
1170 /* points are ignored in XInclude */
1171 break;
1172 default:
1173 break;
1174 }
1175 return(list);
1176}
1177/************************************************************************
1178 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001179 * XInclude I/O handling *
1180 * *
1181 ************************************************************************/
1182
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001183typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1184typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1185struct _xmlXIncludeMergeData {
1186 xmlDocPtr doc;
1187 xmlXIncludeCtxtPtr ctxt;
1188};
1189
Owen Taylor3473f882001-02-23 17:55:21 +00001190/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001191 * xmlXIncludeMergeOneEntity:
1192 * @ent: the entity
1193 * @doc: the including doc
1194 * @nr: the entity name
1195 *
1196 * Inplements the merge of one entity
1197 */
1198static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001199xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001200 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001201 xmlEntityPtr ret, prev;
1202 xmlDocPtr doc;
1203 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001204
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001205 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001206 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001207 ctxt = data->ctxt;
1208 doc = data->doc;
1209 if ((ctxt == NULL) || (doc == NULL))
1210 return;
1211 switch (ent->etype) {
1212 case XML_INTERNAL_PARAMETER_ENTITY:
1213 case XML_EXTERNAL_PARAMETER_ENTITY:
1214 case XML_INTERNAL_PREDEFINED_ENTITY:
1215 return;
1216 case XML_INTERNAL_GENERAL_ENTITY:
1217 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1218 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1219 break;
1220 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001221 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1222 ent->SystemID, ent->content);
1223 if (ret != NULL) {
1224 if (ent->URI != NULL)
1225 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001226 } else {
1227 prev = xmlGetDocEntity(doc, ent->name);
1228 if (prev != NULL) {
1229 if (ent->etype != prev->etype)
1230 goto error;
1231
1232 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1233 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1234 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001235 } else if ((ent->ExternalID != NULL) &&
1236 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001237 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1238 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001239 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1240 if (!xmlStrEqual(ent->content, prev->content))
1241 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001242 } else {
1243 goto error;
1244 }
1245
1246 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001247 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001248 return;
1249error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001250 switch (ent->etype) {
1251 case XML_INTERNAL_PARAMETER_ENTITY:
1252 case XML_EXTERNAL_PARAMETER_ENTITY:
1253 case XML_INTERNAL_PREDEFINED_ENTITY:
1254 case XML_INTERNAL_GENERAL_ENTITY:
1255 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1256 return;
1257 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1258 break;
1259 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001260 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1261 "mismatch in redefinition of entity %s\n",
1262 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001263}
1264
1265/**
1266 * xmlXIncludeMergeEntities:
1267 * @ctxt: an XInclude context
1268 * @doc: the including doc
1269 * @from: the included doc
1270 *
1271 * Inplements the entity merge
1272 *
1273 * Returns 0 if merge succeeded, -1 if some processing failed
1274 */
1275static int
1276xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1277 xmlDocPtr from) {
1278 xmlNodePtr cur;
1279 xmlDtdPtr target, source;
1280
1281 if (ctxt == NULL)
1282 return(-1);
1283
1284 if ((from == NULL) || (from->intSubset == NULL))
1285 return(0);
1286
1287 target = doc->intSubset;
1288 if (target == NULL) {
1289 cur = xmlDocGetRootElement(doc);
1290 if (cur == NULL)
1291 return(-1);
1292 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1293 if (target == NULL)
1294 return(-1);
1295 }
1296
1297 source = from->intSubset;
1298 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001299 xmlXIncludeMergeData data;
1300
1301 data.ctxt = ctxt;
1302 data.doc = doc;
1303
Daniel Veillard4287c572003-02-04 22:48:53 +00001304 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001305 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001306 }
1307 source = from->extSubset;
1308 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001309 xmlXIncludeMergeData data;
1310
1311 data.ctxt = ctxt;
1312 data.doc = doc;
1313
Daniel Veillard4287c572003-02-04 22:48:53 +00001314 /*
1315 * don't duplicate existing stuff when external subsets are the same
1316 */
1317 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1318 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1319 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001320 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001321 }
1322 }
1323 return(0);
1324}
1325
1326/**
Owen Taylor3473f882001-02-23 17:55:21 +00001327 * xmlXIncludeLoadDoc:
1328 * @ctxt: the XInclude context
1329 * @url: the associated URL
1330 * @nr: the xinclude node number
1331 *
1332 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001333 *
1334 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001335 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001336static int
Owen Taylor3473f882001-02-23 17:55:21 +00001337xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1338 xmlDocPtr doc;
1339 xmlURIPtr uri;
1340 xmlChar *URL;
1341 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001342 int i = 0;
1343
1344#ifdef DEBUG_XINCLUDE
1345 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1346#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001347 /*
1348 * Check the URL and remove any fragment identifier
1349 */
1350 uri = xmlParseURI((const char *)url);
1351 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001352 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1353 XML_XINCLUDE_HREF_URI,
1354 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001355 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001356 }
1357 if (uri->fragment != NULL) {
1358 fragment = (xmlChar *) uri->fragment;
1359 uri->fragment = NULL;
1360 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001361 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1362 (ctxt->incTab[nr]->fragment != NULL)) {
1363 if (fragment != NULL) xmlFree(fragment);
1364 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1365 }
Owen Taylor3473f882001-02-23 17:55:21 +00001366 URL = xmlSaveUri(uri);
1367 xmlFreeURI(uri);
1368 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001369 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1370 XML_XINCLUDE_HREF_URI,
1371 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001372 if (fragment != NULL)
1373 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001374 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001375 }
1376
1377 /*
1378 * Handling of references to the local document are done
1379 * directly through ctxt->doc.
1380 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001381 if ((URL[0] == 0) || (URL[0] == '#') ||
1382 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001383 doc = NULL;
1384 goto loaded;
1385 }
1386
1387 /*
1388 * Prevent reloading twice the document.
1389 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001390 for (i = 0; i < ctxt->incNr; i++) {
1391 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1392 (ctxt->incTab[i]->doc != NULL)) {
1393 doc = ctxt->incTab[i]->doc;
1394#ifdef DEBUG_XINCLUDE
1395 printf("Already loaded %s\n", URL);
1396#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001397 goto loaded;
1398 }
1399 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001400
Owen Taylor3473f882001-02-23 17:55:21 +00001401 /*
1402 * Load it.
1403 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001404#ifdef DEBUG_XINCLUDE
1405 printf("loading %s\n", URL);
1406#endif
Daniel Veillard98485322003-08-14 15:44:40 +00001407 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001408 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001409 xmlFree(URL);
1410 if (fragment != NULL)
1411 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001412 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001413 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001414 ctxt->incTab[nr]->doc = doc;
Daniel Veillard98485322003-08-14 15:44:40 +00001415 for (i = nr + 1; i < ctxt->incNr; i++) {
1416 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1417 ctxt->incTab[nr]->count++;
1418#ifdef DEBUG_XINCLUDE
1419 printf("Increasing %s count since reused\n", URL);
1420#endif
1421 break;
1422 }
1423 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001424
1425 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001426 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001427 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001428 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001429
1430 /*
1431 * We don't need the DTD anymore, free up space
1432 if (doc->intSubset != NULL) {
1433 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1434 xmlFreeNode((xmlNodePtr) doc->intSubset);
1435 doc->intSubset = NULL;
1436 }
1437 if (doc->extSubset != NULL) {
1438 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1439 xmlFreeNode((xmlNodePtr) doc->extSubset);
1440 doc->extSubset = NULL;
1441 }
1442 */
1443 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001444
1445loaded:
1446 if (fragment == NULL) {
1447 /*
1448 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001449 */
1450 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001451 {
1452 /* Hopefully a DTD declaration won't be copied from
1453 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001454 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001455 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001456 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001457 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001458 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001459 }
1460#ifdef LIBXML_XPTR_ENABLED
1461 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001462 /*
1463 * Computes the XPointer expression and make a copy used
1464 * as the replacement copy.
1465 */
1466 xmlXPathObjectPtr xptr;
1467 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001468 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001469
1470 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001471 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1472 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001473 } else {
1474 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1475 }
1476 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001477 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1478 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001479 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001480 xmlFree(URL);
1481 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001482 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001483 }
1484 xptr = xmlXPtrEval(fragment, xptrctxt);
1485 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001486 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1487 XML_XINCLUDE_XPTR_FAILED,
1488 "XPointer evaluation failed: #%s\n",
1489 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001490 xmlXPathFreeContext(xptrctxt);
1491 xmlFree(URL);
1492 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001493 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001494 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001495 switch (xptr->type) {
1496 case XPATH_UNDEFINED:
1497 case XPATH_BOOLEAN:
1498 case XPATH_NUMBER:
1499 case XPATH_STRING:
1500 case XPATH_POINT:
1501 case XPATH_USERS:
1502 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001503 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1504 XML_XINCLUDE_XPTR_RESULT,
1505 "XPointer is not a range: #%s\n",
1506 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001507 xmlXPathFreeContext(xptrctxt);
1508 xmlFree(URL);
1509 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001510 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001511 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001512 if ((xptr->nodesetval == NULL) ||
1513 (xptr->nodesetval->nodeNr <= 0)) {
1514 xmlXPathFreeContext(xptrctxt);
1515 xmlFree(URL);
1516 xmlFree(fragment);
1517 return(-1);
1518 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001519 case XPATH_RANGE:
1520 case XPATH_LOCATIONSET:
1521 break;
1522 }
1523 set = xptr->nodesetval;
1524 if (set != NULL) {
1525 for (i = 0;i < set->nodeNr;i++) {
1526 if (set->nodeTab[i] == NULL)
1527 continue;
1528 switch (set->nodeTab[i]->type) {
1529 case XML_TEXT_NODE:
1530 case XML_CDATA_SECTION_NODE:
1531 case XML_ELEMENT_NODE:
1532 case XML_ENTITY_REF_NODE:
1533 case XML_ENTITY_NODE:
1534 case XML_PI_NODE:
1535 case XML_COMMENT_NODE:
1536 case XML_DOCUMENT_NODE:
1537 case XML_HTML_DOCUMENT_NODE:
1538#ifdef LIBXML_DOCB_ENABLED
1539 case XML_DOCB_DOCUMENT_NODE:
1540#endif
1541 continue;
1542 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001543 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1544 XML_XINCLUDE_XPTR_RESULT,
1545 "XPointer selects an attribute: #%s\n",
1546 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001547 set->nodeTab[i] = NULL;
1548 continue;
1549 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001550 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1551 XML_XINCLUDE_XPTR_RESULT,
1552 "XPointer selects a namespace: #%s\n",
1553 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001554 set->nodeTab[i] = NULL;
1555 continue;
1556 case XML_DOCUMENT_TYPE_NODE:
1557 case XML_DOCUMENT_FRAG_NODE:
1558 case XML_NOTATION_NODE:
1559 case XML_DTD_NODE:
1560 case XML_ELEMENT_DECL:
1561 case XML_ATTRIBUTE_DECL:
1562 case XML_ENTITY_DECL:
1563 case XML_XINCLUDE_START:
1564 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001565 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1566 XML_XINCLUDE_XPTR_RESULT,
1567 "XPointer selects unexpected nodes: #%s\n",
1568 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001569 set->nodeTab[i] = NULL;
1570 set->nodeTab[i] = NULL;
1571 continue; /* for */
1572 }
1573 }
1574 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001575 if (doc == NULL) {
1576 ctxt->incTab[nr]->xptr = xptr;
1577 ctxt->incTab[nr]->inc = NULL;
1578 } else {
1579 ctxt->incTab[nr]->inc =
1580 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1581 xmlXPathFreeObject(xptr);
1582 }
Owen Taylor3473f882001-02-23 17:55:21 +00001583 xmlXPathFreeContext(xptrctxt);
1584 xmlFree(fragment);
1585 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001586#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001587
1588 /*
1589 * Do the xml:base fixup if needed
1590 */
1591 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1592 xmlNodePtr node;
1593
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001594 node = ctxt->incTab[nr]->inc;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001595 while (node != NULL) {
1596 if (node->type == XML_ELEMENT_NODE)
1597 xmlNodeSetBase(node, URL);
1598 node = node->next;
1599 }
1600 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001601 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1602 (ctxt->incTab[nr]->count <= 1)) {
1603#ifdef DEBUG_XINCLUDE
1604 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1605#endif
1606 xmlFreeDoc(ctxt->incTab[nr]->doc);
1607 ctxt->incTab[nr]->doc = NULL;
1608 }
Owen Taylor3473f882001-02-23 17:55:21 +00001609 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001610 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001611}
1612
1613/**
1614 * xmlXIncludeLoadTxt:
1615 * @ctxt: the XInclude context
1616 * @url: the associated URL
1617 * @nr: the xinclude node number
1618 *
1619 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001620 *
1621 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001622 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001623static int
Owen Taylor3473f882001-02-23 17:55:21 +00001624xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1625 xmlParserInputBufferPtr buf;
1626 xmlNodePtr node;
1627 xmlURIPtr uri;
1628 xmlChar *URL;
1629 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001630 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001631 xmlCharEncoding enc = (xmlCharEncoding) 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001632
Owen Taylor3473f882001-02-23 17:55:21 +00001633 /*
1634 * Check the URL and remove any fragment identifier
1635 */
1636 uri = xmlParseURI((const char *)url);
1637 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001638 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1639 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001640 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001641 }
1642 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001643 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1644 "fragment identifier forbidden for text: %s\n",
1645 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001646 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001647 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001648 }
1649 URL = xmlSaveUri(uri);
1650 xmlFreeURI(uri);
1651 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001652 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1653 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001654 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001655 }
1656
1657 /*
1658 * Handling of references to the local document are done
1659 * directly through ctxt->doc.
1660 */
1661 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001662 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1663 XML_XINCLUDE_TEXT_DOCUMENT,
1664 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001665 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001666 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001667 }
1668
1669 /*
1670 * Prevent reloading twice the document.
1671 */
1672 for (i = 0; i < ctxt->txtNr; i++) {
1673 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1674 node = xmlCopyNode(ctxt->txtTab[i], 1);
1675 goto loaded;
1676 }
1677 }
1678 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001679 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001680 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001681 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1682 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1683 }
1684 if (encoding != NULL) {
1685 /*
1686 * TODO: we should not have to remap to the xmlCharEncoding
1687 * predefined set, a better interface than
1688 * xmlParserInputBufferCreateFilename should allow any
1689 * encoding supported by iconv
1690 */
1691 enc = xmlParseCharEncoding((const char *) encoding);
1692 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001693 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1694 XML_XINCLUDE_UNKNOWN_ENCODING,
1695 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001696 xmlFree(encoding);
1697 xmlFree(URL);
1698 return(-1);
1699 }
1700 xmlFree(encoding);
1701 }
1702
1703 /*
1704 * Load it.
1705 */
1706 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001707 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001708 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001709 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001710 }
1711 node = xmlNewText(NULL);
1712
1713 /*
1714 * Scan all chars from the resource and add the to the node
1715 */
1716 while (xmlParserInputBufferRead(buf, 128) > 0) {
1717 int len;
1718 const xmlChar *content;
1719
1720 content = xmlBufferContent(buf->buffer);
1721 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001722 for (i = 0;i < len;) {
1723 int cur;
1724 int l;
1725
1726 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1727 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001728 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1729 XML_XINCLUDE_INVALID_CHAR,
1730 "%s contains invalid char\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001731 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001732 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001733 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001734 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001735 }
1736 xmlBufferShrink(buf->buffer, len);
1737 }
1738 xmlFreeParserInputBuffer(buf);
1739 xmlXIncludeAddTxt(ctxt, node, URL);
1740
1741loaded:
1742 /*
1743 * Add the element as the replacement copy.
1744 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001745 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001746 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001747 return(0);
1748}
1749
1750/**
1751 * xmlXIncludeLoadFallback:
1752 * @ctxt: the XInclude context
1753 * @fallback: the fallback node
1754 * @nr: the xinclude node number
1755 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001756 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001757 * in the XInclude context
1758 *
1759 * Returns 0 in case of success, -1 in case of failure
1760 */
1761static int
1762xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
1763 if ((fallback == NULL) || (ctxt == NULL))
1764 return(-1);
1765
Daniel Veillard06503452002-12-13 10:42:08 +00001766 ctxt->incTab[nr]->inc = xmlCopyNodeList(fallback->children);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001767 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001768}
1769
1770/************************************************************************
1771 * *
1772 * XInclude Processing *
1773 * *
1774 ************************************************************************/
1775
1776/**
1777 * xmlXIncludePreProcessNode:
1778 * @ctxt: an XInclude context
1779 * @node: an XInclude node
1780 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001781 * Implement the XInclude preprocessing, currently just adding the element
1782 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001783 *
1784 * Returns the result list or NULL in case of error
1785 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001786static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001787xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1788 xmlXIncludeAddNode(ctxt, node);
1789 return(0);
1790}
1791
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001792/**
Owen Taylor3473f882001-02-23 17:55:21 +00001793 * xmlXIncludeLoadNode:
1794 * @ctxt: an XInclude context
1795 * @nr: the node number
1796 *
1797 * Find and load the infoset replacement for the given node.
1798 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001799 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001800 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001801static int
Owen Taylor3473f882001-02-23 17:55:21 +00001802xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1803 xmlNodePtr cur;
1804 xmlChar *href;
1805 xmlChar *parse;
1806 xmlChar *base;
1807 xmlChar *URI;
1808 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001809 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001810
1811 if (ctxt == NULL)
1812 return(-1);
1813 if ((nr < 0) || (nr >= ctxt->incNr))
1814 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001815 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001816 if (cur == NULL)
1817 return(-1);
1818
Owen Taylor3473f882001-02-23 17:55:21 +00001819 /*
1820 * read the attributes
1821 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001822 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00001823 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001824 /* @@@@ */
1825 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1826 XML_XINCLUDE_NO_HREF, "no href\n", NULL);
1827 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001828 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001829 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00001830 if (parse != NULL) {
1831 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1832 xml = 1;
1833 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1834 xml = 0;
1835 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001836 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1837 XML_XINCLUDE_PARSE_VALUE,
1838 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00001839 if (href != NULL)
1840 xmlFree(href);
1841 if (parse != NULL)
1842 xmlFree(parse);
1843 return(-1);
1844 }
1845 }
1846
1847 /*
1848 * compute the URI
1849 */
1850 base = xmlNodeGetBase(ctxt->doc, cur);
1851 if (base == NULL) {
1852 URI = xmlBuildURI(href, ctxt->doc->URL);
1853 } else {
1854 URI = xmlBuildURI(href, base);
1855 }
1856 if (URI == NULL) {
1857 xmlChar *escbase;
1858 xmlChar *eschref;
1859 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001860 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00001861 */
1862 escbase = xmlURIEscape(base);
1863 eschref = xmlURIEscape(href);
1864 URI = xmlBuildURI(eschref, escbase);
1865 if (escbase != NULL)
1866 xmlFree(escbase);
1867 if (eschref != NULL)
1868 xmlFree(eschref);
1869 }
1870 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001871 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1872 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001873 if (parse != NULL)
1874 xmlFree(parse);
1875 if (href != NULL)
1876 xmlFree(href);
1877 if (base != NULL)
1878 xmlFree(base);
1879 return(-1);
1880 }
1881#ifdef DEBUG_XINCLUDE
1882 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
1883 xml ? "xml": "text");
1884 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
1885#endif
1886
1887 /*
1888 * Cleanup
1889 */
1890 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001891 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00001892 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
1893 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001894 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
1895 }
1896 if (ret < 0) {
1897 xmlNodePtr children;
1898
1899 /*
1900 * Time to try a fallback if availble
1901 */
1902#ifdef DEBUG_XINCLUDE
1903 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
1904#endif
1905 children = cur->children;
1906 while (children != NULL) {
1907 if ((children->type == XML_ELEMENT_NODE) &&
1908 (children->ns != NULL) &&
1909 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001910 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
1911 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001912 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00001913 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001914 break;
1915 }
1916 children = children->next;
1917 }
1918 }
1919 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001920 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1921 XML_XINCLUDE_NO_FALLBACK,
1922 "could not load %s, and no fallback was found\n",
1923 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001924 }
1925
1926 /*
1927 * Cleanup
1928 */
1929 if (URI != NULL)
1930 xmlFree(URI);
1931 if (parse != NULL)
1932 xmlFree(parse);
1933 if (href != NULL)
1934 xmlFree(href);
1935 if (base != NULL)
1936 xmlFree(base);
1937 return(0);
1938}
1939
1940/**
1941 * xmlXIncludeIncludeNode:
1942 * @ctxt: an XInclude context
1943 * @nr: the node number
1944 *
1945 * Inplement the infoset replacement for the given node
1946 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001947 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001948 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001949static int
Owen Taylor3473f882001-02-23 17:55:21 +00001950xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001951 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001952
1953 if (ctxt == NULL)
1954 return(-1);
1955 if ((nr < 0) || (nr >= ctxt->incNr))
1956 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001957 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001958 if (cur == NULL)
1959 return(-1);
1960
1961 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001962 * If we stored an XPointer a late computation may be needed
1963 */
1964 if ((ctxt->incTab[nr]->inc == NULL) &&
1965 (ctxt->incTab[nr]->xptr != NULL)) {
1966 ctxt->incTab[nr]->inc =
1967 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
1968 ctxt->incTab[nr]->xptr);
1969 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
1970 ctxt->incTab[nr]->xptr = NULL;
1971 }
1972 list = ctxt->incTab[nr]->inc;
1973 ctxt->incTab[nr]->inc = NULL;
1974
1975 /*
1976 * Check against the risk of generating a multi-rooted document
1977 */
1978 if ((cur->parent != NULL) &&
1979 (cur->parent->type != XML_ELEMENT_NODE)) {
1980 int nb_elem = 0;
1981
1982 tmp = list;
1983 while (tmp != NULL) {
1984 if (tmp->type == XML_ELEMENT_NODE)
1985 nb_elem++;
1986 tmp = tmp->next;
1987 }
1988 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001989 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1990 XML_XINCLUDE_MULTIPLE_ROOT,
1991 "XInclude error: would result in multiple root nodes\n",
1992 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001993 return(-1);
1994 }
1995 }
1996
1997 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001998 * Change the current node as an XInclude start one, and add an
1999 * entity end one
2000 */
2001 cur->type = XML_XINCLUDE_START;
2002 end = xmlNewNode(cur->ns, cur->name);
2003 if (end == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002004 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_BUILD_FAILED,
2005 "failed to build node\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002006 return(-1);
2007 }
2008 end->type = XML_XINCLUDE_END;
2009 xmlAddNextSibling(cur, end);
2010
2011 /*
2012 * Add the list of nodes
2013 */
Owen Taylor3473f882001-02-23 17:55:21 +00002014 while (list != NULL) {
2015 cur = list;
2016 list = list->next;
2017
2018 xmlAddPrevSibling(end, cur);
2019 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002020
2021
Owen Taylor3473f882001-02-23 17:55:21 +00002022 return(0);
2023}
2024
2025/**
2026 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002027 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002028 * @node: an XInclude node
2029 *
2030 * test if the node is an XInclude node
2031 *
2032 * Returns 1 true, 0 otherwise
2033 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002034static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002035xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002036 if (node == NULL)
2037 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002038 if (node->type != XML_ELEMENT_NODE)
2039 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002040 if (node->ns == NULL)
2041 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002042 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2043 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2044 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2045 if (ctxt->legacy == 0) {
2046 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2047 "Deprecated XInclude namespace found, use %s",
2048 XINCLUDE_NS);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002049 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002050 }
2051 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002052 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2053 xmlNodePtr child = node->children;
2054 int nb_fallback = 0;
2055
2056 while (child != NULL) {
2057 if ((child->type == XML_ELEMENT_NODE) &&
2058 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002059 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2060 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002061 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002062 xmlXIncludeErr(ctxt, node,
2063 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2064 "%s has an 'include' child\n",
2065 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002066 return(0);
2067 }
2068 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2069 nb_fallback++;
2070 }
2071 }
2072 child = child->next;
2073 }
2074 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002075 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2076 "%s has multiple fallback children\n",
2077 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002078 return(0);
2079 }
2080 return(1);
2081 }
2082 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2083 if ((node->parent == NULL) ||
2084 (node->parent->type != XML_ELEMENT_NODE) ||
2085 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002086 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2087 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002088 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002089 xmlXIncludeErr(ctxt, node,
2090 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2091 "%s is not the child of an 'include'\n",
2092 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002093 }
2094 }
2095 }
Owen Taylor3473f882001-02-23 17:55:21 +00002096 return(0);
2097}
2098
2099/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002100 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002101 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002102 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002103 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002104 *
2105 * Implement the XInclude substitution on the XML document @doc
2106 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002107 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002108 * or the number of substitutions done.
2109 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002110static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002111xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002112 xmlNodePtr cur;
2113 int ret = 0;
2114 int i;
2115
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002116 if ((doc == NULL) || (tree == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002117 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002118 if (ctxt == NULL)
2119 return(-1);
2120
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002121 if (doc->URL != NULL) {
2122 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2123 if (ret < 0)
2124 return(-1);
2125 }
2126
Owen Taylor3473f882001-02-23 17:55:21 +00002127 /*
2128 * First phase: lookup the elements in the document
2129 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002130 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002131 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002132 xmlXIncludePreProcessNode(ctxt, cur);
2133 while (cur != NULL) {
2134 /* TODO: need to work on entities -> stack */
2135 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002136 (cur->children->type != XML_ENTITY_DECL) &&
2137 (cur->children->type != XML_XINCLUDE_START) &&
2138 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002139 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002140 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002141 xmlXIncludePreProcessNode(ctxt, cur);
2142 } else if (cur->next != NULL) {
2143 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002144 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002145 xmlXIncludePreProcessNode(ctxt, cur);
2146 } else {
2147 do {
2148 cur = cur->parent;
2149 if (cur == NULL) break; /* do */
2150 if (cur->next != NULL) {
2151 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002152 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002153 xmlXIncludePreProcessNode(ctxt, cur);
2154 break; /* do */
2155 }
2156 } while (cur != NULL);
2157 }
2158 }
2159
2160 /*
2161 * Second Phase : collect the infosets fragments
2162 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002163 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002164 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002165 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002166 }
2167
2168 /*
2169 * Third phase: extend the original document infoset.
2170 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002171 if (ctxt->nbErrors == 0) {
2172 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
2173 xmlXIncludeIncludeNode(ctxt, i);
2174 }
Owen Taylor3473f882001-02-23 17:55:21 +00002175 }
2176
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002177 if (doc->URL != NULL)
2178 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002179 return(ret);
2180}
2181
2182/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002183 * xmlXIncludeSetFlags:
2184 * @ctxt: an XInclude processing context
2185 * @flags: a set of xmlParserOption used for parsing XML includes
2186 *
2187 * Set the flags used for further processing of XML resources.
2188 *
2189 * Returns 0 in case of success and -1 in case of error.
2190 */
2191int
2192xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2193 if (ctxt == NULL)
2194 return(-1);
2195 ctxt->parseFlags = flags;
2196 return(0);
2197}
2198
2199/**
2200 * xmlXIncludeProcessFlags:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002201 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002202 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002203 *
2204 * Implement the XInclude substitution on the XML document @doc
2205 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002206 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002207 * or the number of substitutions done.
2208 */
2209int
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002210xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002211 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002212 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002213 int ret = 0;
2214
2215 if (doc == NULL)
2216 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002217 tree = xmlDocGetRootElement(doc);
2218 if (tree == NULL)
2219 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002220 ctxt = xmlXIncludeNewContext(doc);
2221 if (ctxt == NULL)
2222 return(-1);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002223 xmlXIncludeSetFlags(ctxt, flags);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002224 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2225 if ((ret >= 0) && (ctxt->nbErrors > 0))
2226 ret = -1;
2227
2228 xmlXIncludeFreeContext(ctxt);
2229 return(ret);
2230}
2231
2232/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002233 * xmlXIncludeProcess:
2234 * @doc: an XML document
2235 *
2236 * Implement the XInclude substitution on the XML document @doc
2237 *
2238 * Returns 0 if no substitution were done, -1 if some processing failed
2239 * or the number of substitutions done.
2240 */
2241int
2242xmlXIncludeProcess(xmlDocPtr doc) {
2243 return(xmlXIncludeProcessFlags(doc, 0));
2244}
2245
2246/**
2247 * xmlXIncludeProcessTreeFlags:
2248 * @tree: a node in an XML document
2249 * @flags: a set of xmlParserOption used for parsing XML includes
2250 *
2251 * Implement the XInclude substitution for the given subtree
2252 *
2253 * Returns 0 if no substitution were done, -1 if some processing failed
2254 * or the number of substitutions done.
2255 */
2256int
2257xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2258 xmlXIncludeCtxtPtr ctxt;
2259 int ret = 0;
2260
2261 if ((tree == NULL) || (tree->doc == NULL))
2262 return(-1);
2263 ctxt = xmlXIncludeNewContext(tree->doc);
2264 if (ctxt == NULL)
2265 return(-1);
2266 xmlXIncludeSetFlags(ctxt, flags);
2267 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2268 if ((ret >= 0) && (ctxt->nbErrors > 0))
2269 ret = -1;
2270
2271 xmlXIncludeFreeContext(ctxt);
2272 return(ret);
2273}
2274
2275/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002276 * xmlXIncludeProcessTree:
2277 * @tree: a node in an XML document
2278 *
2279 * Implement the XInclude substitution for the given subtree
2280 *
2281 * Returns 0 if no substitution were done, -1 if some processing failed
2282 * or the number of substitutions done.
2283 */
2284int
2285xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002286 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002287}
2288
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002289/**
2290 * xmlXIncludeProcessNode:
2291 * @ctxt: an existing XInclude context
2292 * @node: a node in an XML document
2293 *
2294 * Implement the XInclude substitution for the given subtree reusing
2295 * the informations and data coming from the given context.
2296 *
2297 * Returns 0 if no substitution were done, -1 if some processing failed
2298 * or the number of substitutions done.
2299 */
2300int
2301xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2302 int ret = 0;
2303
2304 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2305 return(-1);
2306 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2307 if ((ret >= 0) && (ctxt->nbErrors > 0))
2308 ret = -1;
2309 return(ret);
2310}
2311
Owen Taylor3473f882001-02-23 17:55:21 +00002312#else /* !LIBXML_XINCLUDE_ENABLED */
2313#endif