blob: a42793e4747b7da4229349091390f47974ef9573 [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
Daniel Veillard345ee8b2012-07-16 14:40:37 +080029#include "buf.h"
Owen Taylor3473f882001-02-23 17:55:21 +000030
Daniel Veillardf4b4f982003-02-13 11:02:08 +000031#define XINCLUDE_MAX_DEPTH 40
32
Daniel Veillard98485322003-08-14 15:44:40 +000033/* #define DEBUG_XINCLUDE */
Daniel Veillard017b1082001-06-21 11:20:21 +000034#ifdef DEBUG_XINCLUDE
35#ifdef LIBXML_DEBUG_ENABLED
36#include <libxml/debugXML.h>
37#endif
38#endif
Owen Taylor3473f882001-02-23 17:55:21 +000039
40/************************************************************************
41 * *
William M. Brack72ee48d2003-12-30 08:30:19 +000042 * XInclude context handling *
Owen Taylor3473f882001-02-23 17:55:21 +000043 * *
44 ************************************************************************/
45
46/*
47 * An XInclude context
48 */
Daniel Veillardedac3c92001-02-26 01:36:19 +000049typedef xmlChar *xmlURL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +000050
51typedef struct _xmlXIncludeRef xmlXIncludeRef;
52typedef xmlXIncludeRef *xmlXIncludeRefPtr;
53struct _xmlXIncludeRef {
William M. Brack72ee48d2003-12-30 08:30:19 +000054 xmlChar *URI; /* the fully resolved resource URL */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000055 xmlChar *fragment; /* the fragment in the URI */
56 xmlDocPtr doc; /* the parsed document */
57 xmlNodePtr ref; /* the node making the reference in the source */
58 xmlNodePtr inc; /* the included copy */
59 int xml; /* xml or txt */
60 int count; /* how many refs use that specific doc */
Daniel Veillardf4b4f982003-02-13 11:02:08 +000061 xmlXPathObjectPtr xptr; /* the xpointer if needed */
William M. Brack95af5942004-02-08 04:12:49 +000062 int emptyFb; /* flag to show fallback empty */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000063};
64
Owen Taylor3473f882001-02-23 17:55:21 +000065struct _xmlXIncludeCtxt {
66 xmlDocPtr doc; /* the source document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000067 int incBase; /* the first include for this document */
Owen Taylor3473f882001-02-23 17:55:21 +000068 int incNr; /* number of includes */
69 int incMax; /* size of includes tab */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000070 xmlXIncludeRefPtr *incTab; /* array of included references */
71
Owen Taylor3473f882001-02-23 17:55:21 +000072 int txtNr; /* number of unparsed documents */
73 int txtMax; /* size of unparsed documents tab */
74 xmlNodePtr *txtTab; /* array of unparsed text nodes */
William M. Brack72ee48d2003-12-30 08:30:19 +000075 xmlURL *txturlTab; /* array of unparsed text URLs */
Daniel Veillardd581b7e2003-02-11 18:03:05 +000076
Daniel Veillardf4b4f982003-02-13 11:02:08 +000077 xmlChar * url; /* the current URL processed */
William M. Brack72ee48d2003-12-30 08:30:19 +000078 int urlNr; /* number of URLs stacked */
79 int urlMax; /* size of URL stack */
80 xmlChar * *urlTab; /* URL stack */
Daniel Veillardf4b4f982003-02-13 11:02:08 +000081
Daniel Veillardd581b7e2003-02-11 18:03:05 +000082 int nbErrors; /* the number of errors detected */
Daniel Veillardb5fa0202003-12-08 17:41:29 +000083 int legacy; /* using XINCLUDE_OLD_NS */
Daniel Veillarde74d2e12003-12-09 11:35:37 +000084 int parseFlags; /* the flags used for parsing XML documents */
William M. Brackf7789b12004-06-07 08:57:27 +000085 xmlChar * base; /* the current xml:base */
Daniel Veillard681e9042006-09-29 09:16:00 +000086
87 void *_private; /* application data */
Owen Taylor3473f882001-02-23 17:55:21 +000088};
89
Daniel Veillardd16df9f2001-05-23 13:44:21 +000090static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +000091xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree);
Owen Taylor3473f882001-02-23 17:55:21 +000092
Daniel Veillard98485322003-08-14 15:44:40 +000093
Daniel Veillardcd6ff282003-10-08 22:38:13 +000094/************************************************************************
95 * *
Daniel Veillard69d2c172003-10-09 11:46:07 +000096 * XInclude error handler *
Daniel Veillardcd6ff282003-10-08 22:38:13 +000097 * *
98 ************************************************************************/
99
Daniel Veillard98485322003-08-14 15:44:40 +0000100/**
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000101 * xmlXIncludeErrMemory:
William M. Brack72ee48d2003-12-30 08:30:19 +0000102 * @extra: extra information
Daniel Veillard98485322003-08-14 15:44:40 +0000103 *
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000104 * Handle an out of memory condition
Daniel Veillard98485322003-08-14 15:44:40 +0000105 */
106static void
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000107xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
108 const char *extra)
Daniel Veillard98485322003-08-14 15:44:40 +0000109{
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000110 if (ctxt != NULL)
111 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000112 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000113 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
114 extra, NULL, NULL, 0, 0,
115 "Memory allocation failed : %s\n", extra);
116}
Daniel Veillard98485322003-08-14 15:44:40 +0000117
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000118/**
119 * xmlXIncludeErr:
120 * @ctxt: the XInclude context
121 * @node: the context node
122 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000123 * @extra: extra information
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000124 *
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000125 * Handle an XInclude error
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000126 */
127static void
128xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
129 const char *msg, const xmlChar *extra)
130{
131 if (ctxt != NULL)
132 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000133 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000134 error, XML_ERR_ERROR, NULL, 0,
135 (const char *) extra, NULL, NULL, 0, 0,
136 msg, (const char *) extra);
Daniel Veillard98485322003-08-14 15:44:40 +0000137}
138
Daniel Veillardf54cd532004-02-25 11:52:31 +0000139#if 0
Owen Taylor3473f882001-02-23 17:55:21 +0000140/**
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000141 * xmlXIncludeWarn:
142 * @ctxt: the XInclude context
143 * @node: the context node
144 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000145 * @extra: extra information
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000146 *
147 * Emit an XInclude warning.
148 */
149static void
150xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
151 const char *msg, const xmlChar *extra)
152{
153 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
154 error, XML_ERR_WARNING, NULL, 0,
155 (const char *) extra, NULL, NULL, 0, 0,
156 msg, (const char *) extra);
157}
Daniel Veillardf54cd532004-02-25 11:52:31 +0000158#endif
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000159
160/**
161 * xmlXIncludeGetProp:
162 * @ctxt: the XInclude context
163 * @cur: the node
164 * @name: the attribute name
165 *
166 * Get an XInclude attribute
167 *
168 * Returns the value (to be freed) or NULL if not found
169 */
170static xmlChar *
171xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
172 const xmlChar *name) {
173 xmlChar *ret;
174
175 ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
176 if (ret != NULL)
177 return(ret);
178 if (ctxt->legacy != 0) {
179 ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
180 if (ret != NULL)
181 return(ret);
182 }
183 ret = xmlGetProp(cur, name);
184 return(ret);
185}
186/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000187 * xmlXIncludeFreeRef:
188 * @ref: the XInclude reference
189 *
190 * Free an XInclude reference
191 */
192static void
193xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
194 if (ref == NULL)
195 return;
196#ifdef DEBUG_XINCLUDE
197 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
198#endif
199 if (ref->doc != NULL) {
200#ifdef DEBUG_XINCLUDE
201 xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
202#endif
203 xmlFreeDoc(ref->doc);
204 }
205 if (ref->URI != NULL)
206 xmlFree(ref->URI);
207 if (ref->fragment != NULL)
208 xmlFree(ref->fragment);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000209 if (ref->xptr != NULL)
210 xmlXPathFreeObject(ref->xptr);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000211 xmlFree(ref);
212}
213
214/**
215 * xmlXIncludeNewRef:
216 * @ctxt: the XInclude context
217 * @URI: the resource URI
218 *
219 * Creates a new reference within an XInclude context
220 *
221 * Returns the new set
222 */
223static xmlXIncludeRefPtr
224xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
225 xmlNodePtr ref) {
226 xmlXIncludeRefPtr ret;
227
228#ifdef DEBUG_XINCLUDE
229 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
230#endif
231 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000232 if (ret == NULL) {
233 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000234 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000235 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000236 memset(ret, 0, sizeof(xmlXIncludeRef));
237 if (URI == NULL)
238 ret->URI = NULL;
239 else
240 ret->URI = xmlStrdup(URI);
241 ret->fragment = NULL;
242 ret->ref = ref;
Daniel Veillard24505b02005-07-28 23:49:35 +0000243 ret->doc = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000244 ret->count = 0;
245 ret->xml = 0;
246 ret->inc = NULL;
247 if (ctxt->incMax == 0) {
248 ctxt->incMax = 4;
249 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
250 sizeof(ctxt->incTab[0]));
251 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000252 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000253 xmlXIncludeFreeRef(ret);
254 return(NULL);
255 }
256 }
257 if (ctxt->incNr >= ctxt->incMax) {
258 ctxt->incMax *= 2;
259 ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
260 ctxt->incMax * sizeof(ctxt->incTab[0]));
261 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000262 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000263 xmlXIncludeFreeRef(ret);
264 return(NULL);
265 }
266 }
267 ctxt->incTab[ctxt->incNr++] = ret;
268 return(ret);
269}
270
271/**
Owen Taylor3473f882001-02-23 17:55:21 +0000272 * xmlXIncludeNewContext:
273 * @doc: an XML Document
274 *
275 * Creates a new XInclude context
276 *
277 * Returns the new set
278 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000279xmlXIncludeCtxtPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000280xmlXIncludeNewContext(xmlDocPtr doc) {
281 xmlXIncludeCtxtPtr ret;
282
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000283#ifdef DEBUG_XINCLUDE
284 xmlGenericError(xmlGenericErrorContext, "New context\n");
285#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000286 if (doc == NULL)
287 return(NULL);
288 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000289 if (ret == NULL) {
290 xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
291 "creating XInclude context");
Owen Taylor3473f882001-02-23 17:55:21 +0000292 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000293 }
Owen Taylor3473f882001-02-23 17:55:21 +0000294 memset(ret, 0, sizeof(xmlXIncludeCtxt));
295 ret->doc = doc;
296 ret->incNr = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000297 ret->incBase = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000298 ret->incMax = 0;
299 ret->incTab = NULL;
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000300 ret->nbErrors = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000301 return(ret);
302}
303
304/**
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000305 * xmlXIncludeURLPush:
306 * @ctxt: the parser context
307 * @value: the url
308 *
309 * Pushes a new url on top of the url stack
310 *
311 * Returns -1 in case of error, the index in the stack otherwise
312 */
313static int
314xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
315 const xmlChar *value)
316{
317 if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000318 xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION,
319 "detected a recursion in %s\n", value);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000320 return(-1);
321 }
322 if (ctxt->urlTab == NULL) {
323 ctxt->urlMax = 4;
324 ctxt->urlNr = 0;
325 ctxt->urlTab = (xmlChar * *) xmlMalloc(
326 ctxt->urlMax * sizeof(ctxt->urlTab[0]));
327 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000328 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000329 return (-1);
330 }
331 }
332 if (ctxt->urlNr >= ctxt->urlMax) {
333 ctxt->urlMax *= 2;
334 ctxt->urlTab =
335 (xmlChar * *) xmlRealloc(ctxt->urlTab,
336 ctxt->urlMax *
337 sizeof(ctxt->urlTab[0]));
338 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000339 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000340 return (-1);
341 }
342 }
343 ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
344 return (ctxt->urlNr++);
345}
346
347/**
348 * xmlXIncludeURLPop:
349 * @ctxt: the parser context
350 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000351 * Pops the top URL from the URL stack
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000352 */
353static void
354xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
355{
356 xmlChar * ret;
357
358 if (ctxt->urlNr <= 0)
359 return;
360 ctxt->urlNr--;
361 if (ctxt->urlNr > 0)
362 ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
363 else
364 ctxt->url = NULL;
365 ret = ctxt->urlTab[ctxt->urlNr];
Daniel Veillard24505b02005-07-28 23:49:35 +0000366 ctxt->urlTab[ctxt->urlNr] = NULL;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000367 if (ret != NULL)
368 xmlFree(ret);
369}
370
371/**
Owen Taylor3473f882001-02-23 17:55:21 +0000372 * xmlXIncludeFreeContext:
373 * @ctxt: the XInclude context
374 *
375 * Free an XInclude context
376 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000377void
Owen Taylor3473f882001-02-23 17:55:21 +0000378xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
379 int i;
380
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000381#ifdef DEBUG_XINCLUDE
382 xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
383#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000384 if (ctxt == NULL)
385 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000386 while (ctxt->urlNr > 0)
387 xmlXIncludeURLPop(ctxt);
388 if (ctxt->urlTab != NULL)
389 xmlFree(ctxt->urlTab);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000390 for (i = 0;i < ctxt->incNr;i++) {
391 if (ctxt->incTab[i] != NULL)
392 xmlXIncludeFreeRef(ctxt->incTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +0000393 }
Daniel Veillard11ce4002006-03-10 00:36:23 +0000394 if (ctxt->txturlTab != NULL) {
395 for (i = 0;i < ctxt->txtNr;i++) {
396 if (ctxt->txturlTab[i] != NULL)
397 xmlFree(ctxt->txturlTab[i]);
398 }
Owen Taylor3473f882001-02-23 17:55:21 +0000399 }
400 if (ctxt->incTab != NULL)
401 xmlFree(ctxt->incTab);
Owen Taylor3473f882001-02-23 17:55:21 +0000402 if (ctxt->txtTab != NULL)
403 xmlFree(ctxt->txtTab);
404 if (ctxt->txturlTab != NULL)
405 xmlFree(ctxt->txturlTab);
Daniel Veillardce244ad2004-11-05 10:03:46 +0000406 if (ctxt->base != NULL) {
407 xmlFree(ctxt->base);
408 }
Owen Taylor3473f882001-02-23 17:55:21 +0000409 xmlFree(ctxt);
410}
411
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000412/**
Daniel Veillard98485322003-08-14 15:44:40 +0000413 * xmlXIncludeParseFile:
414 * @ctxt: the XInclude context
415 * @URL: the URL or file path
416 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000417 * parse a document for XInclude
Daniel Veillard98485322003-08-14 15:44:40 +0000418 */
419static xmlDocPtr
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000420xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
Daniel Veillard98485322003-08-14 15:44:40 +0000421 xmlDocPtr ret;
422 xmlParserCtxtPtr pctxt;
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000423 xmlParserInputPtr inputStream;
Daniel Veillard98485322003-08-14 15:44:40 +0000424
425 xmlInitParser();
426
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000427 pctxt = xmlNewParserCtxt();
Daniel Veillard98485322003-08-14 15:44:40 +0000428 if (pctxt == NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000429 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
Daniel Veillard98485322003-08-14 15:44:40 +0000430 return(NULL);
431 }
Daniel Veillard681e9042006-09-29 09:16:00 +0000432
433 /*
434 * pass in the application data to the parser context.
435 */
436 pctxt->_private = ctxt->_private;
437
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000438 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000439 * try to ensure that new documents included are actually
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000440 * built with the same dictionary as the including document.
441 */
Daniel Veillardcb6f5252009-08-25 19:24:15 +0200442 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
443 if (pctxt->dict != NULL)
444 xmlDictFree(pctxt->dict);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000445 pctxt->dict = ctxt->doc->dict;
446 xmlDictReference(pctxt->dict);
447 }
448
449 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
450
451 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
452 if (inputStream == NULL) {
453 xmlFreeParserCtxt(pctxt);
454 return(NULL);
455 }
456
457 inputPush(pctxt, inputStream);
Daniel Veillard98485322003-08-14 15:44:40 +0000458
Daniel Veillard37d2d162008-03-14 10:54:00 +0000459 if (pctxt->directory == NULL)
460 pctxt->directory = xmlParserGetDirectory(URL);
Daniel Veillard98485322003-08-14 15:44:40 +0000461
William M. Bracka22da292005-02-12 01:08:22 +0000462 pctxt->loadsubset |= XML_DETECT_IDS;
Daniel Veillard98485322003-08-14 15:44:40 +0000463
464 xmlParseDocument(pctxt);
465
William M. Brack1ff42132003-12-31 14:05:15 +0000466 if (pctxt->wellFormed) {
Daniel Veillard98485322003-08-14 15:44:40 +0000467 ret = pctxt->myDoc;
William M. Brack1ff42132003-12-31 14:05:15 +0000468 }
Daniel Veillard98485322003-08-14 15:44:40 +0000469 else {
470 ret = NULL;
Daniel Veillard500a1de2004-03-22 15:22:58 +0000471 if (pctxt->myDoc != NULL)
Daniel Veillard4773df22004-01-23 13:15:13 +0000472 xmlFreeDoc(pctxt->myDoc);
Daniel Veillard98485322003-08-14 15:44:40 +0000473 pctxt->myDoc = NULL;
474 }
475 xmlFreeParserCtxt(pctxt);
476
477 return(ret);
478}
479
480/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000481 * xmlXIncludeAddNode:
482 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000483 * @cur: the new node
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000484 *
485 * Add a new node to process to an XInclude context
486 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000487static int
488xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
489 xmlXIncludeRefPtr ref;
490 xmlURIPtr uri;
491 xmlChar *URL;
492 xmlChar *fragment = NULL;
493 xmlChar *href;
494 xmlChar *parse;
495 xmlChar *base;
496 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000497 int xml = 1, i; /* default Issue 64 */
498 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000499
500
501 if (ctxt == NULL)
502 return(-1);
503 if (cur == NULL)
504 return(-1);
505
506#ifdef DEBUG_XINCLUDE
507 xmlGenericError(xmlGenericErrorContext, "Add node\n");
508#endif
509 /*
510 * read the attributes
511 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000512 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000513 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000514 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
515 if (href == NULL)
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000516 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000517 }
Daniel Veillardb242b082008-02-08 09:56:31 +0000518 if ((href[0] == '#') || (href[0] == 0))
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000519 local = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000520 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000521 if (parse != NULL) {
522 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
523 xml = 1;
524 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
525 xml = 0;
526 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000527 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
528 "invalid value %s for 'parse'\n", parse);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000529 if (href != NULL)
530 xmlFree(href);
531 if (parse != NULL)
532 xmlFree(parse);
533 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000534 }
535 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000536
537 /*
538 * compute the URI
539 */
540 base = xmlNodeGetBase(ctxt->doc, cur);
541 if (base == NULL) {
542 URI = xmlBuildURI(href, ctxt->doc->URL);
543 } else {
544 URI = xmlBuildURI(href, base);
545 }
546 if (URI == NULL) {
547 xmlChar *escbase;
548 xmlChar *eschref;
549 /*
550 * Some escaping may be needed
551 */
552 escbase = xmlURIEscape(base);
553 eschref = xmlURIEscape(href);
554 URI = xmlBuildURI(eschref, escbase);
555 if (escbase != NULL)
556 xmlFree(escbase);
557 if (eschref != NULL)
558 xmlFree(eschref);
559 }
560 if (parse != NULL)
561 xmlFree(parse);
562 if (href != NULL)
563 xmlFree(href);
564 if (base != NULL)
565 xmlFree(base);
566 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000567 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
568 "failed build URL\n", NULL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000569 return(-1);
570 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000571 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000572
573 /*
574 * Check the URL and remove any fragment identifier
575 */
576 uri = xmlParseURI((const char *)URI);
577 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000578 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
579 "invalid value URI %s\n", URI);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000580 if (fragment != NULL)
581 xmlFree(fragment);
582 xmlFree(URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000583 return(-1);
584 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000585
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000586 if (uri->fragment != NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000587 if (ctxt->legacy != 0) {
588 if (fragment == NULL) {
589 fragment = (xmlChar *) uri->fragment;
590 } else {
591 xmlFree(uri->fragment);
592 }
593 } else {
594 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
595 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
596 URI);
597 if (fragment != NULL)
598 xmlFree(fragment);
599 xmlFreeURI(uri);
600 xmlFree(URI);
601 return(-1);
602 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000603 uri->fragment = NULL;
604 }
605 URL = xmlSaveUri(uri);
606 xmlFreeURI(uri);
607 xmlFree(URI);
608 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000609 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
610 "invalid value URI %s\n", URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000611 if (fragment != NULL)
612 xmlFree(fragment);
613 return(-1);
614 }
615
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000616 /*
Daniel Veillardb242b082008-02-08 09:56:31 +0000617 * If local and xml then we need a fragment
618 */
619 if ((local == 1) && (xml == 1) &&
620 ((fragment == NULL) || (fragment[0] == 0))) {
621 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
622 "detected a local recursion with no xpointer in %s\n",
623 URL);
624 if (fragment != NULL)
625 xmlFree(fragment);
626 return(-1);
627 }
628
629 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000630 * Check the URL against the stack for recursions
631 */
Daniel Veillardbf630c02006-06-06 08:21:41 +0000632 if ((!local) && (xml == 1)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000633 for (i = 0;i < ctxt->urlNr;i++) {
634 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000635 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
636 "detected a recursion in %s\n", URL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000637 return(-1);
638 }
639 }
640 }
641
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000642 ref = xmlXIncludeNewRef(ctxt, URL, cur);
643 if (ref == NULL) {
644 return(-1);
645 }
646 ref->fragment = fragment;
647 ref->doc = NULL;
648 ref->xml = xml;
649 ref->count = 1;
650 xmlFree(URL);
651 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000652}
653
654/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000655 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000656 * @ctxt: the XInclude context
657 * @doc: the new document
658 * @url: the associated URL
659 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000660 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000661 */
662static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000663xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000664 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000665 xmlXIncludeCtxtPtr newctxt;
666 int i;
667
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000668 /*
669 * Avoid recursion in already substitued resources
670 for (i = 0;i < ctxt->urlNr;i++) {
671 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
672 return;
673 }
674 */
675
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000676#ifdef DEBUG_XINCLUDE
677 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
678#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000679 /*
680 * Handle recursion here.
681 */
682
683 newctxt = xmlXIncludeNewContext(doc);
684 if (newctxt != NULL) {
685 /*
Daniel Veillarda6585822006-12-04 09:21:28 +0000686 * Copy the private user data
687 */
688 newctxt->_private = ctxt->_private;
689 /*
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000690 * Copy the existing document set
691 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000692 newctxt->incMax = ctxt->incMax;
693 newctxt->incNr = ctxt->incNr;
694 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
695 sizeof(newctxt->incTab[0]));
696 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000697 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000698 xmlFree(newctxt);
699 return;
700 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000701 /*
702 * copy the urlTab
703 */
704 newctxt->urlMax = ctxt->urlMax;
705 newctxt->urlNr = ctxt->urlNr;
706 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000707
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000708 /*
William M. Brackf7789b12004-06-07 08:57:27 +0000709 * Inherit the existing base
710 */
Daniel Veillardce244ad2004-11-05 10:03:46 +0000711 newctxt->base = xmlStrdup(ctxt->base);
William M. Brackf7789b12004-06-07 08:57:27 +0000712
713 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000714 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000715 */
716 newctxt->incBase = ctxt->incNr;
717 for (i = 0;i < ctxt->incNr;i++) {
718 newctxt->incTab[i] = ctxt->incTab[i];
719 newctxt->incTab[i]->count++; /* prevent the recursion from
720 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000721 }
William M. Bracka11e4832004-03-07 11:03:43 +0000722 /*
723 * The new context should also inherit the Parse Flags
724 * (bug 132597)
725 */
726 newctxt->parseFlags = ctxt->parseFlags;
Daniel Veillard8edf1c52003-07-22 20:52:14 +0000727 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000728 for (i = 0;i < ctxt->incNr;i++) {
729 newctxt->incTab[i]->count--;
730 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000731 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000732
733 /* urlTab may have been reallocated */
734 ctxt->urlTab = newctxt->urlTab;
735 ctxt->urlMax = newctxt->urlMax;
736
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000737 newctxt->urlMax = 0;
738 newctxt->urlNr = 0;
739 newctxt->urlTab = NULL;
740
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000741 xmlXIncludeFreeContext(newctxt);
742 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000743#ifdef DEBUG_XINCLUDE
744 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
745#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000746}
747
748/**
749 * xmlXIncludeAddTxt:
750 * @ctxt: the XInclude context
751 * @txt: the new text node
752 * @url: the associated URL
753 *
754 * Add a new txtument to the list
755 */
756static void
757xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000758#ifdef DEBUG_XINCLUDE
759 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
760#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000761 if (ctxt->txtMax == 0) {
762 ctxt->txtMax = 4;
763 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
764 sizeof(ctxt->txtTab[0]));
765 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000766 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000767 return;
768 }
769 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
770 sizeof(ctxt->txturlTab[0]));
771 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000772 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000773 return;
774 }
775 }
776 if (ctxt->txtNr >= ctxt->txtMax) {
777 ctxt->txtMax *= 2;
778 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
779 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
780 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000781 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000782 return;
783 }
784 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000785 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000786 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000787 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000788 return;
789 }
790 }
791 ctxt->txtTab[ctxt->txtNr] = txt;
792 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
793 ctxt->txtNr++;
794}
795
Owen Taylor3473f882001-02-23 17:55:21 +0000796/************************************************************************
797 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000798 * Node copy with specific semantic *
799 * *
800 ************************************************************************/
801
Daniel Veillardcb6f5252009-08-25 19:24:15 +0200802static xmlNodePtr
803xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
804 xmlDocPtr source, xmlNodePtr elem);
805
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000806/**
807 * xmlXIncludeCopyNode:
808 * @ctxt: the XInclude context
809 * @target: the document target
810 * @source: the document source
811 * @elem: the element
812 *
813 * Make a copy of the node while preserving the XInclude semantic
814 * of the Infoset copy
815 */
816static xmlNodePtr
817xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
818 xmlDocPtr source, xmlNodePtr elem) {
819 xmlNodePtr result = NULL;
820
821 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
822 (elem == NULL))
823 return(NULL);
824 if (elem->type == XML_DTD_NODE)
825 return(NULL);
Daniel Veillardcb6f5252009-08-25 19:24:15 +0200826 if (elem->type == XML_DOCUMENT_NODE)
827 result = xmlXIncludeCopyNodeList(ctxt, target, source, elem->children);
828 else
829 result = xmlDocCopyNode(elem, target, 1);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000830 return(result);
831}
832
833/**
834 * xmlXIncludeCopyNodeList:
835 * @ctxt: the XInclude context
836 * @target: the document target
837 * @source: the document source
838 * @elem: the element list
839 *
840 * Make a copy of the node list while preserving the XInclude semantic
841 * of the Infoset copy
842 */
843static xmlNodePtr
844xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
845 xmlDocPtr source, xmlNodePtr elem) {
846 xmlNodePtr cur, res, result = NULL, last = NULL;
847
848 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
849 (elem == NULL))
850 return(NULL);
851 cur = elem;
852 while (cur != NULL) {
853 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
854 if (res != NULL) {
855 if (result == NULL) {
856 result = last = res;
857 } else {
858 last->next = res;
859 res->prev = last;
860 last = res;
861 }
862 }
863 cur = cur->next;
864 }
865 return(result);
866}
867
868/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000869 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000870 * @cur: the node
871 * @no: the child number
872 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000873 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000874 */
875static xmlNodePtr
876xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
877 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800878 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
879 return(NULL);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000880 cur = cur->children;
881 for (i = 0;i <= no;cur = cur->next) {
882 if (cur == NULL)
883 return(cur);
884 if ((cur->type == XML_ELEMENT_NODE) ||
885 (cur->type == XML_DOCUMENT_NODE) ||
886 (cur->type == XML_HTML_DOCUMENT_NODE)) {
887 i++;
888 if (i == no)
889 break;
890 }
891 }
892 return(cur);
893}
894
William M. Brackf7eb7942003-12-31 07:59:17 +0000895xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000896/**
897 * xmlXIncludeCopyRange:
898 * @ctxt: the XInclude context
899 * @target: the document target
900 * @source: the document source
901 * @obj: the XPointer result from the evaluation.
902 *
903 * Build a node list tree copy of the XPointer result.
904 *
905 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000906 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000907 */
908static xmlNodePtr
909xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
910 xmlDocPtr source, xmlXPathObjectPtr range) {
911 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000912 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
913 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000914 /* pointers to traversal nodes */
915 xmlNodePtr start, cur, end;
916 int index1, index2;
William M. Brack6bdacd72004-02-07 08:53:23 +0000917 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000918
919 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
920 (range == NULL))
921 return(NULL);
922 if (range->type != XPATH_RANGE)
923 return(NULL);
924 start = (xmlNodePtr) range->user;
925
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800926 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000927 return(NULL);
928 end = range->user2;
929 if (end == NULL)
930 return(xmlDocCopyNode(start, target, 1));
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800931 if (end->type == XML_NAMESPACE_DECL)
932 return(NULL);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000933
934 cur = start;
935 index1 = range->index;
936 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000937 /*
938 * level is depth of the current node under consideration
939 * list is the pointer to the root of the output tree
940 * listParent is a pointer to the parent of output tree (within
941 the included file) in case we need to add another level
942 * last is a pointer to the last node added to the output tree
943 * lastLevel is the depth of last (relative to the root)
944 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000945 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000946 /*
947 * Check if our output tree needs a parent
948 */
949 if (level < 0) {
950 while (level < 0) {
William M. Brack57e9e912004-03-09 16:19:02 +0000951 /* copy must include namespaces and properties */
952 tmp2 = xmlDocCopyNode(listParent, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000953 xmlAddChild(tmp2, list);
954 list = tmp2;
955 listParent = listParent->parent;
956 level++;
957 }
958 last = list;
959 lastLevel = 0;
960 }
961 /*
962 * Check whether we need to change our insertion point
963 */
964 while (level < lastLevel) {
965 last = last->parent;
966 lastLevel --;
967 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000968 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000969 if (cur->type == XML_TEXT_NODE) {
970 const xmlChar *content = cur->content;
971 int len;
972
973 if (content == NULL) {
974 tmp = xmlNewTextLen(NULL, 0);
975 } else {
976 len = index2;
977 if ((cur == start) && (index1 > 1)) {
978 content += (index1 - 1);
979 len -= (index1 - 1);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000980 } else {
981 len = index2;
982 }
983 tmp = xmlNewTextLen(content, len);
984 }
985 /* single sub text node selection */
986 if (list == NULL)
987 return(tmp);
988 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +0000989 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000990 xmlAddNextSibling(last, tmp);
991 else
William M. Brackf7eb7942003-12-31 07:59:17 +0000992 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000993 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +0000994 } else { /* ending node not a text node */
William M. Brack6bdacd72004-02-07 08:53:23 +0000995 endLevel = level; /* remember the level of the end node */
996 endFlag = 1;
William M. Brack57e9e912004-03-09 16:19:02 +0000997 /* last node - need to take care of properties + namespaces */
998 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000999 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001000 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001001 listParent = cur->parent;
1002 } else {
1003 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001004 xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +00001005 else {
1006 xmlAddChild(last, tmp);
1007 lastLevel = level;
1008 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001009 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001010 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001011
1012 if (index2 > 1) {
1013 end = xmlXIncludeGetNthChild(cur, index2 - 1);
1014 index2 = 0;
1015 }
1016 if ((cur == start) && (index1 > 1)) {
1017 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1018 index1 = 0;
William M. Brack6bdacd72004-02-07 08:53:23 +00001019 } else {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001020 cur = cur->children;
1021 }
William M. Brack6bdacd72004-02-07 08:53:23 +00001022 level++; /* increment level to show change */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001023 /*
1024 * Now gather the remaining nodes from cur to end
1025 */
William M. Brack6bdacd72004-02-07 08:53:23 +00001026 continue; /* while */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001027 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001028 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001029 if ((cur->type == XML_TEXT_NODE) ||
1030 (cur->type == XML_CDATA_SECTION_NODE)) {
1031 const xmlChar *content = cur->content;
1032
1033 if (content == NULL) {
1034 tmp = xmlNewTextLen(NULL, 0);
1035 } else {
1036 if (index1 > 1) {
1037 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +00001038 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001039 }
1040 tmp = xmlNewText(content);
1041 }
1042 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001043 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001044 } else { /* Not text node */
William M. Brack57e9e912004-03-09 16:19:02 +00001045 /*
1046 * start of the range - need to take care of
1047 * properties and namespaces
1048 */
1049 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +00001050 list = last = tmp;
1051 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001052 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001053 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001054 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001055 index1 = 0;
1056 /*
1057 * Now gather the remaining nodes from cur to end
1058 */
1059 continue; /* while */
1060 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001061 }
1062 } else {
1063 tmp = NULL;
1064 switch (cur->type) {
1065 case XML_DTD_NODE:
1066 case XML_ELEMENT_DECL:
1067 case XML_ATTRIBUTE_DECL:
1068 case XML_ENTITY_NODE:
1069 /* Do not copy DTD informations */
1070 break;
1071 case XML_ENTITY_DECL:
1072 /* handle crossing entities -> stack needed */
1073 break;
1074 case XML_XINCLUDE_START:
1075 case XML_XINCLUDE_END:
1076 /* don't consider it part of the tree content */
1077 break;
1078 case XML_ATTRIBUTE_NODE:
1079 /* Humm, should not happen ! */
1080 break;
1081 default:
William M. Brack57e9e912004-03-09 16:19:02 +00001082 /*
1083 * Middle of the range - need to take care of
1084 * properties and namespaces
1085 */
1086 tmp = xmlDocCopyNode(cur, target, 2);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001087 break;
1088 }
1089 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001090 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001091 xmlAddNextSibling(last, tmp);
1092 else {
William M. Brackf7eb7942003-12-31 07:59:17 +00001093 xmlAddChild(last, tmp);
1094 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001095 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001096 last = tmp;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001097 }
1098 }
1099 /*
1100 * Skip to next node in document order
1101 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001102 cur = xmlXPtrAdvanceNode(cur, &level);
William M. Brack6bdacd72004-02-07 08:53:23 +00001103 if (endFlag && (level >= endLevel))
1104 break;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001105 }
1106 return(list);
1107}
1108
1109/**
1110 * xmlXIncludeBuildNodeList:
1111 * @ctxt: the XInclude context
1112 * @target: the document target
1113 * @source: the document source
1114 * @obj: the XPointer result from the evaluation.
1115 *
1116 * Build a node list tree copy of the XPointer result.
1117 * This will drop Attributes and Namespace declarations.
1118 *
1119 * Returns an xmlNodePtr list or NULL.
1120 * the caller has to free the node tree.
1121 */
1122static xmlNodePtr
1123xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1124 xmlDocPtr source, xmlXPathObjectPtr obj) {
1125 xmlNodePtr list = NULL, last = NULL;
1126 int i;
1127
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001128 if (source == NULL)
1129 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001130 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1131 (obj == NULL))
1132 return(NULL);
1133 switch (obj->type) {
1134 case XPATH_NODESET: {
1135 xmlNodeSetPtr set = obj->nodesetval;
1136 if (set == NULL)
1137 return(NULL);
1138 for (i = 0;i < set->nodeNr;i++) {
1139 if (set->nodeTab[i] == NULL)
1140 continue;
1141 switch (set->nodeTab[i]->type) {
1142 case XML_TEXT_NODE:
1143 case XML_CDATA_SECTION_NODE:
1144 case XML_ELEMENT_NODE:
1145 case XML_ENTITY_REF_NODE:
1146 case XML_ENTITY_NODE:
1147 case XML_PI_NODE:
1148 case XML_COMMENT_NODE:
1149 case XML_DOCUMENT_NODE:
1150 case XML_HTML_DOCUMENT_NODE:
1151#ifdef LIBXML_DOCB_ENABLED
1152 case XML_DOCB_DOCUMENT_NODE:
1153#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001154 case XML_XINCLUDE_END:
1155 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001156 case XML_XINCLUDE_START: {
1157 xmlNodePtr tmp, cur = set->nodeTab[i];
1158
1159 cur = cur->next;
1160 while (cur != NULL) {
1161 switch(cur->type) {
1162 case XML_TEXT_NODE:
1163 case XML_CDATA_SECTION_NODE:
1164 case XML_ELEMENT_NODE:
1165 case XML_ENTITY_REF_NODE:
1166 case XML_ENTITY_NODE:
1167 case XML_PI_NODE:
1168 case XML_COMMENT_NODE:
1169 tmp = xmlXIncludeCopyNode(ctxt, target,
1170 source, cur);
1171 if (last == NULL) {
1172 list = last = tmp;
1173 } else {
1174 xmlAddNextSibling(last, tmp);
1175 last = tmp;
1176 }
1177 cur = cur->next;
1178 continue;
1179 default:
1180 break;
1181 }
1182 break;
1183 }
1184 continue;
1185 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001186 case XML_ATTRIBUTE_NODE:
1187 case XML_NAMESPACE_DECL:
1188 case XML_DOCUMENT_TYPE_NODE:
1189 case XML_DOCUMENT_FRAG_NODE:
1190 case XML_NOTATION_NODE:
1191 case XML_DTD_NODE:
1192 case XML_ELEMENT_DECL:
1193 case XML_ATTRIBUTE_DECL:
1194 case XML_ENTITY_DECL:
1195 continue; /* for */
1196 }
1197 if (last == NULL)
1198 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1199 set->nodeTab[i]);
1200 else {
1201 xmlAddNextSibling(last,
1202 xmlXIncludeCopyNode(ctxt, target, source,
1203 set->nodeTab[i]));
1204 if (last->next != NULL)
1205 last = last->next;
1206 }
1207 }
1208 break;
1209 }
1210 case XPATH_LOCATIONSET: {
1211 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1212 if (set == NULL)
1213 return(NULL);
1214 for (i = 0;i < set->locNr;i++) {
1215 if (last == NULL)
1216 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1217 set->locTab[i]);
1218 else
1219 xmlAddNextSibling(last,
1220 xmlXIncludeCopyXPointer(ctxt, target, source,
1221 set->locTab[i]));
1222 if (last != NULL) {
1223 while (last->next != NULL)
1224 last = last->next;
1225 }
1226 }
1227 break;
1228 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001229#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001230 case XPATH_RANGE:
1231 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001232#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001233 case XPATH_POINT:
1234 /* points are ignored in XInclude */
1235 break;
1236 default:
1237 break;
1238 }
1239 return(list);
1240}
1241/************************************************************************
1242 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001243 * XInclude I/O handling *
1244 * *
1245 ************************************************************************/
1246
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001247typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1248typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1249struct _xmlXIncludeMergeData {
1250 xmlDocPtr doc;
1251 xmlXIncludeCtxtPtr ctxt;
1252};
1253
Owen Taylor3473f882001-02-23 17:55:21 +00001254/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001255 * xmlXIncludeMergeOneEntity:
1256 * @ent: the entity
1257 * @doc: the including doc
1258 * @nr: the entity name
1259 *
1260 * Inplements the merge of one entity
1261 */
1262static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001263xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001264 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001265 xmlEntityPtr ret, prev;
1266 xmlDocPtr doc;
1267 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001268
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001269 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001270 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001271 ctxt = data->ctxt;
1272 doc = data->doc;
1273 if ((ctxt == NULL) || (doc == NULL))
1274 return;
1275 switch (ent->etype) {
1276 case XML_INTERNAL_PARAMETER_ENTITY:
1277 case XML_EXTERNAL_PARAMETER_ENTITY:
1278 case XML_INTERNAL_PREDEFINED_ENTITY:
1279 return;
1280 case XML_INTERNAL_GENERAL_ENTITY:
1281 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1282 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1283 break;
1284 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001285 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1286 ent->SystemID, ent->content);
1287 if (ret != NULL) {
1288 if (ent->URI != NULL)
1289 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001290 } else {
1291 prev = xmlGetDocEntity(doc, ent->name);
1292 if (prev != NULL) {
1293 if (ent->etype != prev->etype)
1294 goto error;
1295
1296 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1297 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1298 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001299 } else if ((ent->ExternalID != NULL) &&
1300 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001301 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1302 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001303 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1304 if (!xmlStrEqual(ent->content, prev->content))
1305 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001306 } else {
1307 goto error;
1308 }
1309
1310 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001311 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001312 return;
1313error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001314 switch (ent->etype) {
1315 case XML_INTERNAL_PARAMETER_ENTITY:
1316 case XML_EXTERNAL_PARAMETER_ENTITY:
1317 case XML_INTERNAL_PREDEFINED_ENTITY:
1318 case XML_INTERNAL_GENERAL_ENTITY:
1319 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1320 return;
1321 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1322 break;
1323 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001324 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1325 "mismatch in redefinition of entity %s\n",
1326 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001327}
1328
1329/**
1330 * xmlXIncludeMergeEntities:
1331 * @ctxt: an XInclude context
1332 * @doc: the including doc
1333 * @from: the included doc
1334 *
1335 * Inplements the entity merge
1336 *
1337 * Returns 0 if merge succeeded, -1 if some processing failed
1338 */
1339static int
1340xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1341 xmlDocPtr from) {
1342 xmlNodePtr cur;
1343 xmlDtdPtr target, source;
1344
1345 if (ctxt == NULL)
1346 return(-1);
1347
1348 if ((from == NULL) || (from->intSubset == NULL))
1349 return(0);
1350
1351 target = doc->intSubset;
1352 if (target == NULL) {
1353 cur = xmlDocGetRootElement(doc);
1354 if (cur == NULL)
1355 return(-1);
1356 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1357 if (target == NULL)
1358 return(-1);
1359 }
1360
1361 source = from->intSubset;
1362 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001363 xmlXIncludeMergeData data;
1364
1365 data.ctxt = ctxt;
1366 data.doc = doc;
1367
Daniel Veillard4287c572003-02-04 22:48:53 +00001368 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001369 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001370 }
1371 source = from->extSubset;
1372 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001373 xmlXIncludeMergeData data;
1374
1375 data.ctxt = ctxt;
1376 data.doc = doc;
1377
Daniel Veillard4287c572003-02-04 22:48:53 +00001378 /*
1379 * don't duplicate existing stuff when external subsets are the same
1380 */
1381 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1382 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1383 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001384 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001385 }
1386 }
1387 return(0);
1388}
1389
1390/**
Owen Taylor3473f882001-02-23 17:55:21 +00001391 * xmlXIncludeLoadDoc:
1392 * @ctxt: the XInclude context
1393 * @url: the associated URL
1394 * @nr: the xinclude node number
1395 *
1396 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001397 *
1398 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001399 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001400static int
Owen Taylor3473f882001-02-23 17:55:21 +00001401xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1402 xmlDocPtr doc;
1403 xmlURIPtr uri;
1404 xmlChar *URL;
1405 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001406 int i = 0;
William M. Brack4d59e222004-03-08 14:42:31 +00001407#ifdef LIBXML_XPTR_ENABLED
1408 int saveFlags;
1409#endif
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001410
1411#ifdef DEBUG_XINCLUDE
1412 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1413#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001414 /*
1415 * Check the URL and remove any fragment identifier
1416 */
1417 uri = xmlParseURI((const char *)url);
1418 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001419 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1420 XML_XINCLUDE_HREF_URI,
1421 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001422 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001423 }
1424 if (uri->fragment != NULL) {
1425 fragment = (xmlChar *) uri->fragment;
1426 uri->fragment = NULL;
1427 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001428 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1429 (ctxt->incTab[nr]->fragment != NULL)) {
1430 if (fragment != NULL) xmlFree(fragment);
1431 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1432 }
Owen Taylor3473f882001-02-23 17:55:21 +00001433 URL = xmlSaveUri(uri);
1434 xmlFreeURI(uri);
1435 if (URL == NULL) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00001436 if (ctxt->incTab != NULL)
1437 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1438 XML_XINCLUDE_HREF_URI,
1439 "invalid value URI %s\n", url);
1440 else
1441 xmlXIncludeErr(ctxt, NULL,
1442 XML_XINCLUDE_HREF_URI,
1443 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001444 if (fragment != NULL)
1445 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001446 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001447 }
1448
1449 /*
1450 * Handling of references to the local document are done
1451 * directly through ctxt->doc.
1452 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001453 if ((URL[0] == 0) || (URL[0] == '#') ||
1454 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001455 doc = NULL;
1456 goto loaded;
1457 }
1458
1459 /*
1460 * Prevent reloading twice the document.
1461 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001462 for (i = 0; i < ctxt->incNr; i++) {
1463 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1464 (ctxt->incTab[i]->doc != NULL)) {
1465 doc = ctxt->incTab[i]->doc;
1466#ifdef DEBUG_XINCLUDE
1467 printf("Already loaded %s\n", URL);
1468#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001469 goto loaded;
1470 }
1471 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001472
Owen Taylor3473f882001-02-23 17:55:21 +00001473 /*
1474 * Load it.
1475 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001476#ifdef DEBUG_XINCLUDE
1477 printf("loading %s\n", URL);
1478#endif
William M. Brack4d59e222004-03-08 14:42:31 +00001479#ifdef LIBXML_XPTR_ENABLED
1480 /*
1481 * If this is an XPointer evaluation, we want to assure that
1482 * all entities have been resolved prior to processing the
1483 * referenced document
1484 */
1485 saveFlags = ctxt->parseFlags;
1486 if (fragment != NULL) { /* if this is an XPointer eval */
1487 ctxt->parseFlags |= XML_PARSE_NOENT;
1488 }
1489#endif
1490
Daniel Veillard98485322003-08-14 15:44:40 +00001491 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
William M. Brack4d59e222004-03-08 14:42:31 +00001492#ifdef LIBXML_XPTR_ENABLED
1493 ctxt->parseFlags = saveFlags;
1494#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001495 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001496 xmlFree(URL);
1497 if (fragment != NULL)
1498 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001499 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001500 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001501 ctxt->incTab[nr]->doc = doc;
William M. Brackb85c9202004-07-26 00:20:13 +00001502 /*
1503 * It's possible that the requested URL has been mapped to a
1504 * completely different location (e.g. through a catalog entry).
1505 * To check for this, we compare the URL with that of the doc
1506 * and change it if they disagree (bug 146988).
1507 */
1508 if (!xmlStrEqual(URL, doc->URL)) {
1509 xmlFree(URL);
1510 URL = xmlStrdup(doc->URL);
1511 }
Daniel Veillard98485322003-08-14 15:44:40 +00001512 for (i = nr + 1; i < ctxt->incNr; i++) {
1513 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1514 ctxt->incTab[nr]->count++;
1515#ifdef DEBUG_XINCLUDE
1516 printf("Increasing %s count since reused\n", URL);
1517#endif
1518 break;
1519 }
1520 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001521
1522 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001523 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001524 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001525 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001526
1527 /*
1528 * We don't need the DTD anymore, free up space
1529 if (doc->intSubset != NULL) {
1530 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1531 xmlFreeNode((xmlNodePtr) doc->intSubset);
1532 doc->intSubset = NULL;
1533 }
1534 if (doc->extSubset != NULL) {
1535 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1536 xmlFreeNode((xmlNodePtr) doc->extSubset);
1537 doc->extSubset = NULL;
1538 }
1539 */
1540 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001541
1542loaded:
1543 if (fragment == NULL) {
1544 /*
1545 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001546 */
1547 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001548 {
1549 /* Hopefully a DTD declaration won't be copied from
1550 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001551 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001552 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001553 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001554 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001555 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001556 }
1557#ifdef LIBXML_XPTR_ENABLED
1558 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001559 /*
1560 * Computes the XPointer expression and make a copy used
1561 * as the replacement copy.
1562 */
1563 xmlXPathObjectPtr xptr;
1564 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001565 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001566
1567 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001568 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1569 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001570 } else {
1571 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1572 }
1573 if (xptrctxt == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001574 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1575 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001576 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001577 xmlFree(URL);
1578 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001579 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001580 }
1581 xptr = xmlXPtrEval(fragment, xptrctxt);
1582 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001583 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1584 XML_XINCLUDE_XPTR_FAILED,
1585 "XPointer evaluation failed: #%s\n",
1586 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001587 xmlXPathFreeContext(xptrctxt);
1588 xmlFree(URL);
1589 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001590 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001591 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001592 switch (xptr->type) {
1593 case XPATH_UNDEFINED:
1594 case XPATH_BOOLEAN:
1595 case XPATH_NUMBER:
1596 case XPATH_STRING:
1597 case XPATH_POINT:
1598 case XPATH_USERS:
1599 case XPATH_XSLT_TREE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001600 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1601 XML_XINCLUDE_XPTR_RESULT,
1602 "XPointer is not a range: #%s\n",
1603 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001604 xmlXPathFreeContext(xptrctxt);
1605 xmlFree(URL);
1606 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001607 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001608 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001609 if ((xptr->nodesetval == NULL) ||
1610 (xptr->nodesetval->nodeNr <= 0)) {
1611 xmlXPathFreeContext(xptrctxt);
1612 xmlFree(URL);
1613 xmlFree(fragment);
1614 return(-1);
1615 }
William M. Brackabf598b2004-06-08 02:01:28 +00001616
Daniel Veillard39196eb2001-06-19 18:09:42 +00001617 case XPATH_RANGE:
1618 case XPATH_LOCATIONSET:
1619 break;
1620 }
1621 set = xptr->nodesetval;
1622 if (set != NULL) {
1623 for (i = 0;i < set->nodeNr;i++) {
1624 if (set->nodeTab[i] == NULL)
1625 continue;
1626 switch (set->nodeTab[i]->type) {
William M. Brackb0a94e82007-07-18 18:04:55 +00001627 case XML_ELEMENT_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001628 case XML_TEXT_NODE:
1629 case XML_CDATA_SECTION_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001630 case XML_ENTITY_REF_NODE:
1631 case XML_ENTITY_NODE:
1632 case XML_PI_NODE:
1633 case XML_COMMENT_NODE:
1634 case XML_DOCUMENT_NODE:
1635 case XML_HTML_DOCUMENT_NODE:
1636#ifdef LIBXML_DOCB_ENABLED
1637 case XML_DOCB_DOCUMENT_NODE:
1638#endif
1639 continue;
William M. Brackabf598b2004-06-08 02:01:28 +00001640
Daniel Veillard39196eb2001-06-19 18:09:42 +00001641 case XML_ATTRIBUTE_NODE:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001642 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1643 XML_XINCLUDE_XPTR_RESULT,
1644 "XPointer selects an attribute: #%s\n",
1645 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001646 set->nodeTab[i] = NULL;
1647 continue;
1648 case XML_NAMESPACE_DECL:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001649 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1650 XML_XINCLUDE_XPTR_RESULT,
1651 "XPointer selects a namespace: #%s\n",
1652 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001653 set->nodeTab[i] = NULL;
1654 continue;
1655 case XML_DOCUMENT_TYPE_NODE:
1656 case XML_DOCUMENT_FRAG_NODE:
1657 case XML_NOTATION_NODE:
1658 case XML_DTD_NODE:
1659 case XML_ELEMENT_DECL:
1660 case XML_ATTRIBUTE_DECL:
1661 case XML_ENTITY_DECL:
1662 case XML_XINCLUDE_START:
1663 case XML_XINCLUDE_END:
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001664 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1665 XML_XINCLUDE_XPTR_RESULT,
1666 "XPointer selects unexpected nodes: #%s\n",
1667 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001668 set->nodeTab[i] = NULL;
1669 set->nodeTab[i] = NULL;
1670 continue; /* for */
1671 }
1672 }
1673 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001674 if (doc == NULL) {
1675 ctxt->incTab[nr]->xptr = xptr;
1676 ctxt->incTab[nr]->inc = NULL;
1677 } else {
1678 ctxt->incTab[nr]->inc =
1679 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1680 xmlXPathFreeObject(xptr);
1681 }
Owen Taylor3473f882001-02-23 17:55:21 +00001682 xmlXPathFreeContext(xptrctxt);
1683 xmlFree(fragment);
1684 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001685#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001686
1687 /*
1688 * Do the xml:base fixup if needed
1689 */
Daniel Veillard54bd29b2008-08-26 07:26:55 +00001690 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/')) &&
1691 (!(ctxt->parseFlags & XML_PARSE_NOBASEFIX)) &&
1692 (!(doc->parseFlags & XML_PARSE_NOBASEFIX))) {
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001693 xmlNodePtr node;
William M. Brack0b13a092005-06-01 03:37:59 +00001694 xmlChar *base;
William M. Brackabf598b2004-06-08 02:01:28 +00001695 xmlChar *curBase;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001696
William M. Brackf7789b12004-06-07 08:57:27 +00001697 /*
William M. Brack0b13a092005-06-01 03:37:59 +00001698 * The base is only adjusted if "necessary", i.e. if the xinclude node
1699 * has a base specified, or the URL is relative
William M. Brackf7789b12004-06-07 08:57:27 +00001700 */
William M. Brack0b13a092005-06-01 03:37:59 +00001701 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1702 XML_XML_NAMESPACE);
1703 if (base == NULL) {
1704 /*
1705 * No xml:base on the xinclude node, so we check whether the
1706 * URI base is different than (relative to) the context base
1707 */
1708 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1709 if (curBase == NULL) { /* Error return */
1710 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
William M. Brackf7789b12004-06-07 08:57:27 +00001711 XML_XINCLUDE_HREF_URI,
1712 "trying to build relative URI from %s\n", URL);
William M. Brack0b13a092005-06-01 03:37:59 +00001713 } else {
1714 /* If the URI doesn't contain a slash, it's not relative */
1715 if (!xmlStrchr(curBase, (xmlChar) '/'))
1716 xmlFree(curBase);
1717 else
1718 base = curBase;
William M. Brackf7789b12004-06-07 08:57:27 +00001719 }
William M. Brack0b13a092005-06-01 03:37:59 +00001720 }
1721 if (base != NULL) { /* Adjustment may be needed */
1722 node = ctxt->incTab[nr]->inc;
1723 while (node != NULL) {
1724 /* Only work on element nodes */
1725 if (node->type == XML_ELEMENT_NODE) {
1726 curBase = xmlNodeGetBase(node->doc, node);
1727 /* If no current base, set it */
1728 if (curBase == NULL) {
1729 xmlNodeSetBase(node, base);
1730 } else {
1731 /*
1732 * If the current base is the same as the
1733 * URL of the document, then reset it to be
1734 * the specified xml:base or the relative URI
1735 */
1736 if (xmlStrEqual(curBase, node->doc->URL)) {
1737 xmlNodeSetBase(node, base);
1738 } else {
1739 /*
1740 * If the element already has an xml:base
1741 * set, then relativise it if necessary
1742 */
1743 xmlChar *xmlBase;
1744 xmlBase = xmlGetNsProp(node,
1745 BAD_CAST "base",
1746 XML_XML_NAMESPACE);
1747 if (xmlBase != NULL) {
1748 xmlChar *relBase;
1749 relBase = xmlBuildURI(xmlBase, base);
1750 if (relBase == NULL) { /* error */
1751 xmlXIncludeErr(ctxt,
1752 ctxt->incTab[nr]->ref,
1753 XML_XINCLUDE_HREF_URI,
1754 "trying to rebuild base from %s\n",
1755 xmlBase);
1756 } else {
1757 xmlNodeSetBase(node, relBase);
1758 xmlFree(relBase);
1759 }
1760 xmlFree(xmlBase);
1761 }
1762 }
1763 xmlFree(curBase);
1764 }
1765 }
1766 node = node->next;
1767 }
1768 xmlFree(base);
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001769 }
1770 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001771 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1772 (ctxt->incTab[nr]->count <= 1)) {
1773#ifdef DEBUG_XINCLUDE
1774 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1775#endif
1776 xmlFreeDoc(ctxt->incTab[nr]->doc);
1777 ctxt->incTab[nr]->doc = NULL;
1778 }
Owen Taylor3473f882001-02-23 17:55:21 +00001779 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001780 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001781}
1782
1783/**
1784 * xmlXIncludeLoadTxt:
1785 * @ctxt: the XInclude context
1786 * @url: the associated URL
1787 * @nr: the xinclude node number
1788 *
1789 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001790 *
1791 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001792 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001793static int
Owen Taylor3473f882001-02-23 17:55:21 +00001794xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1795 xmlParserInputBufferPtr buf;
1796 xmlNodePtr node;
1797 xmlURIPtr uri;
1798 xmlChar *URL;
1799 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001800 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001801 xmlCharEncoding enc = (xmlCharEncoding) 0;
Shaun McCance4cf73252012-05-10 20:59:33 +08001802 xmlParserCtxtPtr pctxt;
1803 xmlParserInputPtr inputStream;
Daniel Veillardd076a202002-11-20 13:28:31 +00001804
Owen Taylor3473f882001-02-23 17:55:21 +00001805 /*
1806 * Check the URL and remove any fragment identifier
1807 */
1808 uri = xmlParseURI((const char *)url);
1809 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001810 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1811 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001812 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001813 }
1814 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001815 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1816 "fragment identifier forbidden for text: %s\n",
1817 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001818 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001819 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001820 }
1821 URL = xmlSaveUri(uri);
1822 xmlFreeURI(uri);
1823 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001824 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1825 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001826 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001827 }
1828
1829 /*
1830 * Handling of references to the local document are done
1831 * directly through ctxt->doc.
1832 */
1833 if (URL[0] == 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001834 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1835 XML_XINCLUDE_TEXT_DOCUMENT,
1836 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001837 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001838 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001839 }
1840
1841 /*
1842 * Prevent reloading twice the document.
1843 */
1844 for (i = 0; i < ctxt->txtNr; i++) {
1845 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1846 node = xmlCopyNode(ctxt->txtTab[i], 1);
1847 goto loaded;
1848 }
1849 }
1850 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001851 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001852 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001853 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1854 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1855 }
1856 if (encoding != NULL) {
1857 /*
1858 * TODO: we should not have to remap to the xmlCharEncoding
1859 * predefined set, a better interface than
1860 * xmlParserInputBufferCreateFilename should allow any
1861 * encoding supported by iconv
1862 */
1863 enc = xmlParseCharEncoding((const char *) encoding);
1864 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001865 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1866 XML_XINCLUDE_UNKNOWN_ENCODING,
1867 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001868 xmlFree(encoding);
1869 xmlFree(URL);
1870 return(-1);
1871 }
1872 xmlFree(encoding);
1873 }
1874
1875 /*
1876 * Load it.
1877 */
Shaun McCance4cf73252012-05-10 20:59:33 +08001878 pctxt = xmlNewParserCtxt();
1879 inputStream = xmlLoadExternalEntity((const char*)URL, NULL, pctxt);
1880 if(inputStream == NULL) {
1881 xmlFreeParserCtxt(pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001882 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001883 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001884 }
Shaun McCance4cf73252012-05-10 20:59:33 +08001885 buf = inputStream->buf;
1886 if (buf == NULL) {
1887 xmlFreeInputStream (inputStream);
1888 xmlFreeParserCtxt(pctxt);
1889 xmlFree(URL);
1890 return(-1);
1891 }
1892 if (buf->encoder)
1893 xmlCharEncCloseFunc(buf->encoder);
1894 buf->encoder = xmlGetCharEncodingHandler(enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001895 node = xmlNewText(NULL);
1896
1897 /*
1898 * Scan all chars from the resource and add the to the node
1899 */
1900 while (xmlParserInputBufferRead(buf, 128) > 0) {
1901 int len;
1902 const xmlChar *content;
1903
Daniel Veillard345ee8b2012-07-16 14:40:37 +08001904 content = xmlBufContent(buf->buffer);
1905 len = xmlBufLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001906 for (i = 0;i < len;) {
1907 int cur;
1908 int l;
1909
1910 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1911 if (!IS_CHAR(cur)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001912 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1913 XML_XINCLUDE_INVALID_CHAR,
1914 "%s contains invalid char\n", URL);
William M. Brack53ce98c2007-02-13 00:37:20 +00001915 xmlFreeParserInputBuffer(buf);
1916 xmlFree(URL);
1917 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001918 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001919 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001920 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001921 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001922 }
Daniel Veillard345ee8b2012-07-16 14:40:37 +08001923 xmlBufShrink(buf->buffer, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001924 }
Shaun McCance4cf73252012-05-10 20:59:33 +08001925 xmlFreeParserCtxt(pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001926 xmlXIncludeAddTxt(ctxt, node, URL);
Shaun McCance4cf73252012-05-10 20:59:33 +08001927 xmlFreeInputStream(inputStream);
Owen Taylor3473f882001-02-23 17:55:21 +00001928
1929loaded:
1930 /*
1931 * Add the element as the replacement copy.
1932 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001933 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001934 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001935 return(0);
1936}
1937
1938/**
1939 * xmlXIncludeLoadFallback:
1940 * @ctxt: the XInclude context
1941 * @fallback: the fallback node
1942 * @nr: the xinclude node number
1943 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001944 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001945 * in the XInclude context
1946 *
1947 * Returns 0 in case of success, -1 in case of failure
1948 */
1949static int
1950xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001951 xmlXIncludeCtxtPtr newctxt;
1952 int ret = 0;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001953
1954 if ((fallback == NULL) || (fallback->type == XML_NAMESPACE_DECL) ||
1955 (ctxt == NULL))
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001956 return(-1);
William M. Brackef245fd2004-02-06 09:33:59 +00001957 if (fallback->children != NULL) {
1958 /*
1959 * It's possible that the fallback also has 'includes'
1960 * (Bug 129969), so we re-process the fallback just in case
1961 */
1962 newctxt = xmlXIncludeNewContext(ctxt->doc);
1963 if (newctxt == NULL)
1964 return (-1);
Daniel Veillarda6585822006-12-04 09:21:28 +00001965 newctxt->_private = ctxt->_private;
Daniel Veillardce244ad2004-11-05 10:03:46 +00001966 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
William M. Brackef245fd2004-02-06 09:33:59 +00001967 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1968 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
William M. Brack87640d52004-04-17 14:58:15 +00001969 if (ctxt->nbErrors > 0)
William M. Brackef245fd2004-02-06 09:33:59 +00001970 ret = -1;
William M. Brack87640d52004-04-17 14:58:15 +00001971 else if (ret > 0)
1972 ret = 0; /* xmlXIncludeDoProcess can return +ve number */
William M. Brackef245fd2004-02-06 09:33:59 +00001973 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001974
Daniel Veillard03a53c32004-10-26 16:06:51 +00001975 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1976 fallback->children);
William M. Brackef245fd2004-02-06 09:33:59 +00001977 } else {
1978 ctxt->incTab[nr]->inc = NULL;
William M. Brack95af5942004-02-08 04:12:49 +00001979 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
William M. Brackef245fd2004-02-06 09:33:59 +00001980 }
William M. Brackaae10522004-01-02 14:59:41 +00001981 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001982}
1983
1984/************************************************************************
1985 * *
1986 * XInclude Processing *
1987 * *
1988 ************************************************************************/
1989
1990/**
1991 * xmlXIncludePreProcessNode:
1992 * @ctxt: an XInclude context
1993 * @node: an XInclude node
1994 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001995 * Implement the XInclude preprocessing, currently just adding the element
1996 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001997 *
1998 * Returns the result list or NULL in case of error
1999 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002000static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002001xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2002 xmlXIncludeAddNode(ctxt, node);
Daniel Veillard24505b02005-07-28 23:49:35 +00002003 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002004}
2005
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002006/**
Owen Taylor3473f882001-02-23 17:55:21 +00002007 * xmlXIncludeLoadNode:
2008 * @ctxt: an XInclude context
2009 * @nr: the node number
2010 *
2011 * Find and load the infoset replacement for the given node.
2012 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002013 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002014 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002015static int
Owen Taylor3473f882001-02-23 17:55:21 +00002016xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
2017 xmlNodePtr cur;
2018 xmlChar *href;
2019 xmlChar *parse;
2020 xmlChar *base;
William M. Brackf7789b12004-06-07 08:57:27 +00002021 xmlChar *oldBase;
Owen Taylor3473f882001-02-23 17:55:21 +00002022 xmlChar *URI;
2023 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002024 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002025
2026 if (ctxt == NULL)
2027 return(-1);
2028 if ((nr < 0) || (nr >= ctxt->incNr))
2029 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002030 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00002031 if (cur == NULL)
2032 return(-1);
2033
Owen Taylor3473f882001-02-23 17:55:21 +00002034 /*
2035 * read the attributes
2036 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002037 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00002038 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00002039 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
2040 if (href == NULL)
2041 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002042 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002043 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00002044 if (parse != NULL) {
2045 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
2046 xml = 1;
2047 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2048 xml = 0;
2049 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002050 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2051 XML_XINCLUDE_PARSE_VALUE,
2052 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00002053 if (href != NULL)
2054 xmlFree(href);
2055 if (parse != NULL)
2056 xmlFree(parse);
2057 return(-1);
2058 }
2059 }
2060
2061 /*
2062 * compute the URI
2063 */
2064 base = xmlNodeGetBase(ctxt->doc, cur);
2065 if (base == NULL) {
2066 URI = xmlBuildURI(href, ctxt->doc->URL);
2067 } else {
2068 URI = xmlBuildURI(href, base);
2069 }
2070 if (URI == NULL) {
2071 xmlChar *escbase;
2072 xmlChar *eschref;
2073 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002074 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00002075 */
2076 escbase = xmlURIEscape(base);
2077 eschref = xmlURIEscape(href);
2078 URI = xmlBuildURI(eschref, escbase);
2079 if (escbase != NULL)
2080 xmlFree(escbase);
2081 if (eschref != NULL)
2082 xmlFree(eschref);
2083 }
2084 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002085 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2086 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002087 if (parse != NULL)
2088 xmlFree(parse);
2089 if (href != NULL)
2090 xmlFree(href);
2091 if (base != NULL)
2092 xmlFree(base);
2093 return(-1);
2094 }
2095#ifdef DEBUG_XINCLUDE
2096 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2097 xml ? "xml": "text");
2098 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2099#endif
2100
2101 /*
William M. Brackf7789b12004-06-07 08:57:27 +00002102 * Save the base for this include (saving the current one)
Owen Taylor3473f882001-02-23 17:55:21 +00002103 */
William M. Brackf7789b12004-06-07 08:57:27 +00002104 oldBase = ctxt->base;
2105 ctxt->base = base;
2106
Owen Taylor3473f882001-02-23 17:55:21 +00002107 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002108 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00002109 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2110 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002111 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2112 }
William M. Brackf7789b12004-06-07 08:57:27 +00002113
2114 /*
2115 * Restore the original base before checking for fallback
2116 */
2117 ctxt->base = oldBase;
2118
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002119 if (ret < 0) {
2120 xmlNodePtr children;
2121
2122 /*
2123 * Time to try a fallback if availble
2124 */
2125#ifdef DEBUG_XINCLUDE
2126 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2127#endif
2128 children = cur->children;
2129 while (children != NULL) {
2130 if ((children->type == XML_ELEMENT_NODE) &&
2131 (children->ns != NULL) &&
2132 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002133 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2134 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002135 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
William M. Brack1ff42132003-12-31 14:05:15 +00002136 if (ret == 0)
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002137 break;
2138 }
2139 children = children->next;
2140 }
2141 }
2142 if (ret < 0) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002143 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2144 XML_XINCLUDE_NO_FALLBACK,
2145 "could not load %s, and no fallback was found\n",
2146 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002147 }
2148
2149 /*
2150 * Cleanup
2151 */
2152 if (URI != NULL)
2153 xmlFree(URI);
2154 if (parse != NULL)
2155 xmlFree(parse);
2156 if (href != NULL)
2157 xmlFree(href);
2158 if (base != NULL)
2159 xmlFree(base);
2160 return(0);
2161}
2162
2163/**
2164 * xmlXIncludeIncludeNode:
2165 * @ctxt: an XInclude context
2166 * @nr: the node number
2167 *
2168 * Inplement the infoset replacement for the given node
2169 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002170 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002171 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002172static int
Owen Taylor3473f882001-02-23 17:55:21 +00002173xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002174 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002175
2176 if (ctxt == NULL)
2177 return(-1);
2178 if ((nr < 0) || (nr >= ctxt->incNr))
2179 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002180 cur = ctxt->incTab[nr]->ref;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002181 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002182 return(-1);
2183
2184 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002185 * If we stored an XPointer a late computation may be needed
2186 */
2187 if ((ctxt->incTab[nr]->inc == NULL) &&
2188 (ctxt->incTab[nr]->xptr != NULL)) {
2189 ctxt->incTab[nr]->inc =
2190 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
2191 ctxt->incTab[nr]->xptr);
2192 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2193 ctxt->incTab[nr]->xptr = NULL;
2194 }
2195 list = ctxt->incTab[nr]->inc;
2196 ctxt->incTab[nr]->inc = NULL;
2197
2198 /*
2199 * Check against the risk of generating a multi-rooted document
2200 */
2201 if ((cur->parent != NULL) &&
2202 (cur->parent->type != XML_ELEMENT_NODE)) {
2203 int nb_elem = 0;
2204
2205 tmp = list;
2206 while (tmp != NULL) {
2207 if (tmp->type == XML_ELEMENT_NODE)
2208 nb_elem++;
2209 tmp = tmp->next;
2210 }
2211 if (nb_elem > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002212 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2213 XML_XINCLUDE_MULTIPLE_ROOT,
2214 "XInclude error: would result in multiple root nodes\n",
2215 NULL);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002216 return(-1);
2217 }
2218 }
2219
Daniel Veillardc14c3892004-08-16 12:34:50 +00002220 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2221 /*
2222 * Add the list of nodes
2223 */
2224 while (list != NULL) {
2225 end = list;
2226 list = list->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002227
Daniel Veillardc14c3892004-08-16 12:34:50 +00002228 xmlAddPrevSibling(cur, end);
2229 }
2230 xmlUnlinkNode(cur);
2231 xmlFreeNode(cur);
2232 } else {
2233 /*
2234 * Change the current node as an XInclude start one, and add an
2235 * XInclude end one
2236 */
2237 cur->type = XML_XINCLUDE_START;
Daniel Veillard03a53c32004-10-26 16:06:51 +00002238 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
Daniel Veillardc14c3892004-08-16 12:34:50 +00002239 if (end == NULL) {
2240 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2241 XML_XINCLUDE_BUILD_FAILED,
2242 "failed to build node\n", NULL);
2243 return(-1);
2244 }
2245 end->type = XML_XINCLUDE_END;
2246 xmlAddNextSibling(cur, end);
Owen Taylor3473f882001-02-23 17:55:21 +00002247
Daniel Veillardc14c3892004-08-16 12:34:50 +00002248 /*
2249 * Add the list of nodes
2250 */
2251 while (list != NULL) {
2252 cur = list;
2253 list = list->next;
2254
2255 xmlAddPrevSibling(end, cur);
2256 }
Owen Taylor3473f882001-02-23 17:55:21 +00002257 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002258
2259
Owen Taylor3473f882001-02-23 17:55:21 +00002260 return(0);
2261}
2262
2263/**
2264 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002265 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002266 * @node: an XInclude node
2267 *
2268 * test if the node is an XInclude node
2269 *
2270 * Returns 1 true, 0 otherwise
2271 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002272static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002273xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002274 if (node == NULL)
2275 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002276 if (node->type != XML_ELEMENT_NODE)
2277 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002278 if (node->ns == NULL)
2279 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002280 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2281 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2282 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2283 if (ctxt->legacy == 0) {
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002284#if 0 /* wait for the XML Core Working Group to get something stable ! */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002285 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2286 "Deprecated XInclude namespace found, use %s",
2287 XINCLUDE_NS);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002288#endif
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002289 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002290 }
2291 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002292 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2293 xmlNodePtr child = node->children;
2294 int nb_fallback = 0;
2295
2296 while (child != NULL) {
2297 if ((child->type == XML_ELEMENT_NODE) &&
2298 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002299 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2300 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002301 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002302 xmlXIncludeErr(ctxt, node,
2303 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2304 "%s has an 'include' child\n",
2305 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002306 return(0);
2307 }
2308 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2309 nb_fallback++;
2310 }
2311 }
2312 child = child->next;
2313 }
2314 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002315 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2316 "%s has multiple fallback children\n",
2317 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002318 return(0);
2319 }
2320 return(1);
2321 }
2322 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2323 if ((node->parent == NULL) ||
2324 (node->parent->type != XML_ELEMENT_NODE) ||
2325 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002326 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2327 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002328 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002329 xmlXIncludeErr(ctxt, node,
2330 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2331 "%s is not the child of an 'include'\n",
2332 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002333 }
2334 }
2335 }
Owen Taylor3473f882001-02-23 17:55:21 +00002336 return(0);
2337}
2338
2339/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002340 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002341 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002342 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002343 * @tree: the top of the tree to process
Owen Taylor3473f882001-02-23 17:55:21 +00002344 *
2345 * Implement the XInclude substitution on the XML document @doc
2346 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002347 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002348 * or the number of substitutions done.
2349 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002350static int
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002351xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Owen Taylor3473f882001-02-23 17:55:21 +00002352 xmlNodePtr cur;
2353 int ret = 0;
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002354 int i, start;
Owen Taylor3473f882001-02-23 17:55:21 +00002355
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002356 if ((doc == NULL) || (tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002357 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002358 if (ctxt == NULL)
2359 return(-1);
2360
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002361 if (doc->URL != NULL) {
2362 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2363 if (ret < 0)
2364 return(-1);
2365 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00002366 start = ctxt->incNr;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002367
Owen Taylor3473f882001-02-23 17:55:21 +00002368 /*
2369 * First phase: lookup the elements in the document
2370 */
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002371 cur = tree;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002372 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002373 xmlXIncludePreProcessNode(ctxt, cur);
William M. Brack7b0e2762004-05-12 09:33:23 +00002374 while ((cur != NULL) && (cur != tree->parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002375 /* TODO: need to work on entities -> stack */
2376 if ((cur->children != NULL) &&
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002377 (cur->children->type != XML_ENTITY_DECL) &&
2378 (cur->children->type != XML_XINCLUDE_START) &&
2379 (cur->children->type != XML_XINCLUDE_END)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002380 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002381 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002382 xmlXIncludePreProcessNode(ctxt, cur);
2383 } else if (cur->next != NULL) {
2384 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002385 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002386 xmlXIncludePreProcessNode(ctxt, cur);
2387 } else {
William M. Brack5d8d10b2004-04-16 08:11:26 +00002388 if (cur == tree)
2389 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002390 do {
2391 cur = cur->parent;
William M. Brack7b0e2762004-05-12 09:33:23 +00002392 if ((cur == NULL) || (cur == tree->parent))
2393 break; /* do */
Owen Taylor3473f882001-02-23 17:55:21 +00002394 if (cur->next != NULL) {
2395 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002396 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002397 xmlXIncludePreProcessNode(ctxt, cur);
2398 break; /* do */
2399 }
2400 } while (cur != NULL);
2401 }
2402 }
2403
2404 /*
2405 * Second Phase : collect the infosets fragments
2406 */
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002407 for (i = start;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002408 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002409 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002410 }
2411
2412 /*
2413 * Third phase: extend the original document infoset.
William M. Brack6b1a28d2004-02-06 11:24:44 +00002414 *
2415 * Originally we bypassed the inclusion if there were any errors
2416 * encountered on any of the XIncludes. A bug was raised (bug
2417 * 132588) requesting that we output the XIncludes without error,
2418 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2419 * give some other problems in the future, but for now it seems to
2420 * work ok.
2421 *
Owen Taylor3473f882001-02-23 17:55:21 +00002422 */
William M. Brack6b1a28d2004-02-06 11:24:44 +00002423 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
William M. Brack95af5942004-02-08 04:12:49 +00002424 if ((ctxt->incTab[i]->inc != NULL) ||
2425 (ctxt->incTab[i]->xptr != NULL) ||
2426 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002427 xmlXIncludeIncludeNode(ctxt, i);
Owen Taylor3473f882001-02-23 17:55:21 +00002428 }
2429
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002430 if (doc->URL != NULL)
2431 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002432 return(ret);
2433}
2434
2435/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002436 * xmlXIncludeSetFlags:
2437 * @ctxt: an XInclude processing context
2438 * @flags: a set of xmlParserOption used for parsing XML includes
2439 *
2440 * Set the flags used for further processing of XML resources.
2441 *
2442 * Returns 0 in case of success and -1 in case of error.
2443 */
2444int
2445xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2446 if (ctxt == NULL)
2447 return(-1);
2448 ctxt->parseFlags = flags;
2449 return(0);
2450}
Stefan Behnelb9590e92009-08-24 19:45:54 +02002451
2452/**
2453 * xmlXIncludeProcessTreeFlagsData:
2454 * @tree: an XML node
2455 * @flags: a set of xmlParserOption used for parsing XML includes
2456 * @data: application data that will be passed to the parser context
2457 * in the _private field of the parser context(s)
2458 *
2459 * Implement the XInclude substitution on the XML node @tree
2460 *
2461 * Returns 0 if no substitution were done, -1 if some processing failed
2462 * or the number of substitutions done.
2463 */
2464
2465int
2466xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
2467 xmlXIncludeCtxtPtr ctxt;
2468 int ret = 0;
2469
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002470 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2471 (tree->doc == NULL))
Stefan Behnelb9590e92009-08-24 19:45:54 +02002472 return(-1);
2473
2474 ctxt = xmlXIncludeNewContext(tree->doc);
2475 if (ctxt == NULL)
2476 return(-1);
2477 ctxt->_private = data;
2478 ctxt->base = xmlStrdup((xmlChar *)tree->doc->URL);
2479 xmlXIncludeSetFlags(ctxt, flags);
2480 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2481 if ((ret >= 0) && (ctxt->nbErrors > 0))
2482 ret = -1;
2483
2484 xmlXIncludeFreeContext(ctxt);
2485 return(ret);
2486}
2487
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002488/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002489 * xmlXIncludeProcessFlagsData:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002490 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002491 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillard681e9042006-09-29 09:16:00 +00002492 * @data: application data that will be passed to the parser context
2493 * in the _private field of the parser context(s)
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002494 *
2495 * Implement the XInclude substitution on the XML document @doc
2496 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002497 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002498 * or the number of substitutions done.
2499 */
2500int
Daniel Veillard681e9042006-09-29 09:16:00 +00002501xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002502 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002503
2504 if (doc == NULL)
2505 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002506 tree = xmlDocGetRootElement(doc);
2507 if (tree == NULL)
2508 return(-1);
Stefan Behnelb9590e92009-08-24 19:45:54 +02002509 return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002510}
2511
2512/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002513 * xmlXIncludeProcessFlags:
2514 * @doc: an XML document
2515 * @flags: a set of xmlParserOption used for parsing XML includes
2516 *
2517 * Implement the XInclude substitution on the XML document @doc
2518 *
2519 * Returns 0 if no substitution were done, -1 if some processing failed
2520 * or the number of substitutions done.
2521 */
2522int
2523xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2524 return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2525}
2526
2527/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002528 * xmlXIncludeProcess:
2529 * @doc: an XML document
2530 *
2531 * Implement the XInclude substitution on the XML document @doc
2532 *
2533 * Returns 0 if no substitution were done, -1 if some processing failed
2534 * or the number of substitutions done.
2535 */
2536int
2537xmlXIncludeProcess(xmlDocPtr doc) {
2538 return(xmlXIncludeProcessFlags(doc, 0));
2539}
2540
2541/**
2542 * xmlXIncludeProcessTreeFlags:
2543 * @tree: a node in an XML document
2544 * @flags: a set of xmlParserOption used for parsing XML includes
2545 *
2546 * Implement the XInclude substitution for the given subtree
2547 *
2548 * Returns 0 if no substitution were done, -1 if some processing failed
2549 * or the number of substitutions done.
2550 */
2551int
2552xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2553 xmlXIncludeCtxtPtr ctxt;
2554 int ret = 0;
2555
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002556 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2557 (tree->doc == NULL))
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002558 return(-1);
2559 ctxt = xmlXIncludeNewContext(tree->doc);
2560 if (ctxt == NULL)
2561 return(-1);
William M. Brackf7789b12004-06-07 08:57:27 +00002562 ctxt->base = xmlNodeGetBase(tree->doc, tree);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002563 xmlXIncludeSetFlags(ctxt, flags);
2564 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2565 if ((ret >= 0) && (ctxt->nbErrors > 0))
2566 ret = -1;
2567
2568 xmlXIncludeFreeContext(ctxt);
2569 return(ret);
2570}
2571
2572/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002573 * xmlXIncludeProcessTree:
2574 * @tree: a node in an XML document
2575 *
2576 * Implement the XInclude substitution for the given subtree
2577 *
2578 * Returns 0 if no substitution were done, -1 if some processing failed
2579 * or the number of substitutions done.
2580 */
2581int
2582xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002583 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002584}
2585
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002586/**
2587 * xmlXIncludeProcessNode:
2588 * @ctxt: an existing XInclude context
2589 * @node: a node in an XML document
2590 *
2591 * Implement the XInclude substitution for the given subtree reusing
2592 * the informations and data coming from the given context.
2593 *
2594 * Returns 0 if no substitution were done, -1 if some processing failed
2595 * or the number of substitutions done.
2596 */
2597int
2598xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2599 int ret = 0;
2600
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002601 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
2602 (node->doc == NULL) || (ctxt == NULL))
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002603 return(-1);
2604 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2605 if ((ret >= 0) && (ctxt->nbErrors > 0))
2606 ret = -1;
2607 return(ret);
2608}
2609
Owen Taylor3473f882001-02-23 17:55:21 +00002610#else /* !LIBXML_XINCLUDE_ENABLED */
2611#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002612#define bottom_xinclude
2613#include "elfgcchack.h"