blob: 9ba6c40394356f38c05707651062a17489baa177 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xinclude.c : Code to implement XInclude processing
3 *
Daniel Veillardbbd22452001-05-23 12:02:27 +00004 * World Wide Web Consortium W3C Last Call Working Draft 16 May 2001
5 * http://www.w3.org/TR/2001/WD-xinclude-20010516/
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
12/*
13 * TODO: compute XPointers nodesets
Daniel Veillardd16df9f2001-05-23 13:44:21 +000014 * TODO: add an node intermediate API and handle recursion at this level
Owen Taylor3473f882001-02-23 17:55:21 +000015 */
16
Daniel Veillard34ce8be2002-03-18 19:37:11 +000017#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000018#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000019
Owen Taylor3473f882001-02-23 17:55:21 +000020#include <string.h>
21#include <libxml/xmlmemory.h>
22#include <libxml/tree.h>
23#include <libxml/parser.h>
24#include <libxml/uri.h>
25#include <libxml/xpointer.h>
26#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000027#include <libxml/xmlerror.h>
Daniel Veillardd076a202002-11-20 13:28:31 +000028#include <libxml/encoding.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000029#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000030
31#ifdef LIBXML_XINCLUDE_ENABLED
32#include <libxml/xinclude.h>
33
Daniel Veillardbbd22452001-05-23 12:02:27 +000034#define XINCLUDE_NS (const xmlChar *) "http://www.w3.org/2001/XInclude"
Owen Taylor3473f882001-02-23 17:55:21 +000035#define XINCLUDE_NODE (const xmlChar *) "include"
Daniel Veillard58e44c92002-08-02 22:19:49 +000036#define XINCLUDE_FALLBACK (const xmlChar *) "fallback"
Owen Taylor3473f882001-02-23 17:55:21 +000037#define XINCLUDE_HREF (const xmlChar *) "href"
38#define XINCLUDE_PARSE (const xmlChar *) "parse"
39#define XINCLUDE_PARSE_XML (const xmlChar *) "xml"
40#define XINCLUDE_PARSE_TEXT (const xmlChar *) "text"
Daniel Veillardd076a202002-11-20 13:28:31 +000041#define XINCLUDE_PARSE_ENCODING (const xmlChar *) "encoding"
Owen Taylor3473f882001-02-23 17:55:21 +000042
Daniel Veillardf4b4f982003-02-13 11:02:08 +000043#define XINCLUDE_MAX_DEPTH 40
44
Owen Taylor3473f882001-02-23 17:55:21 +000045/* #define DEBUG_XINCLUDE */
Daniel Veillard017b1082001-06-21 11:20:21 +000046#ifdef DEBUG_XINCLUDE
47#ifdef LIBXML_DEBUG_ENABLED
48#include <libxml/debugXML.h>
49#endif
50#endif
Owen Taylor3473f882001-02-23 17:55:21 +000051
52/************************************************************************
53 * *
54 * XInclude contexts handling *
55 * *
56 ************************************************************************/
57
58/*
59 * An XInclude context
60 */
Daniel Veillardedac3c92001-02-26 01:36:19 +000061typedef xmlChar *xmlURL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +000062
63typedef struct _xmlXIncludeRef xmlXIncludeRef;
64typedef xmlXIncludeRef *xmlXIncludeRefPtr;
65struct _xmlXIncludeRef {
66 xmlChar *URI; /* the rully resolved resource URL */
67 xmlChar *fragment; /* the fragment in the URI */
68 xmlDocPtr doc; /* the parsed document */
69 xmlNodePtr ref; /* the node making the reference in the source */
70 xmlNodePtr inc; /* the included copy */
71 int xml; /* xml or txt */
72 int count; /* how many refs use that specific doc */
Daniel Veillardf4b4f982003-02-13 11:02:08 +000073 xmlXPathObjectPtr xptr; /* the xpointer if needed */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000074};
75
Owen Taylor3473f882001-02-23 17:55:21 +000076typedef struct _xmlXIncludeCtxt xmlXIncludeCtxt;
77typedef xmlXIncludeCtxt *xmlXIncludeCtxtPtr;
78struct _xmlXIncludeCtxt {
79 xmlDocPtr doc; /* the source document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000080 int incBase; /* the first include for this document */
Owen Taylor3473f882001-02-23 17:55:21 +000081 int incNr; /* number of includes */
82 int incMax; /* size of includes tab */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000083 xmlXIncludeRefPtr *incTab; /* array of included references */
84
Owen Taylor3473f882001-02-23 17:55:21 +000085 int txtNr; /* number of unparsed documents */
86 int txtMax; /* size of unparsed documents tab */
87 xmlNodePtr *txtTab; /* array of unparsed text nodes */
Daniel Veillardedac3c92001-02-26 01:36:19 +000088 xmlURL *txturlTab; /* array of unparsed txtuments URLs */
Daniel Veillardd581b7e2003-02-11 18:03:05 +000089
Daniel Veillardf4b4f982003-02-13 11:02:08 +000090 xmlChar * url; /* the current URL processed */
91 int urlNr; /* number of url stacked */
92 int urlMax; /* size of url stack */
93 xmlChar * *urlTab; /* url stack */
94
Daniel Veillardd581b7e2003-02-11 18:03:05 +000095 int nbErrors; /* the number of errors detected */
Owen Taylor3473f882001-02-23 17:55:21 +000096};
97
Daniel Veillardd16df9f2001-05-23 13:44:21 +000098static int
99xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc);
Owen Taylor3473f882001-02-23 17:55:21 +0000100
101/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000102 * xmlXIncludeFreeRef:
103 * @ref: the XInclude reference
104 *
105 * Free an XInclude reference
106 */
107static void
108xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
109 if (ref == NULL)
110 return;
111#ifdef DEBUG_XINCLUDE
112 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
113#endif
114 if (ref->doc != NULL) {
115#ifdef DEBUG_XINCLUDE
116 xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
117#endif
118 xmlFreeDoc(ref->doc);
119 }
120 if (ref->URI != NULL)
121 xmlFree(ref->URI);
122 if (ref->fragment != NULL)
123 xmlFree(ref->fragment);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000124 if (ref->xptr != NULL)
125 xmlXPathFreeObject(ref->xptr);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000126 xmlFree(ref);
127}
128
129/**
130 * xmlXIncludeNewRef:
131 * @ctxt: the XInclude context
132 * @URI: the resource URI
133 *
134 * Creates a new reference within an XInclude context
135 *
136 * Returns the new set
137 */
138static xmlXIncludeRefPtr
139xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
140 xmlNodePtr ref) {
141 xmlXIncludeRefPtr ret;
142
143#ifdef DEBUG_XINCLUDE
144 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
145#endif
146 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
147 if (ret == NULL)
148 return(NULL);
149 memset(ret, 0, sizeof(xmlXIncludeRef));
150 if (URI == NULL)
151 ret->URI = NULL;
152 else
153 ret->URI = xmlStrdup(URI);
154 ret->fragment = NULL;
155 ret->ref = ref;
156 ret->doc = 0;
157 ret->count = 0;
158 ret->xml = 0;
159 ret->inc = NULL;
160 if (ctxt->incMax == 0) {
161 ctxt->incMax = 4;
162 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
163 sizeof(ctxt->incTab[0]));
164 if (ctxt->incTab == NULL) {
165 xmlGenericError(xmlGenericErrorContext,
166 "malloc failed !\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000167 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000168 xmlXIncludeFreeRef(ret);
169 return(NULL);
170 }
171 }
172 if (ctxt->incNr >= ctxt->incMax) {
173 ctxt->incMax *= 2;
174 ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
175 ctxt->incMax * sizeof(ctxt->incTab[0]));
176 if (ctxt->incTab == NULL) {
177 xmlGenericError(xmlGenericErrorContext,
178 "realloc failed !\n");
179 xmlXIncludeFreeRef(ret);
180 return(NULL);
181 }
182 }
183 ctxt->incTab[ctxt->incNr++] = ret;
184 return(ret);
185}
186
187/**
Owen Taylor3473f882001-02-23 17:55:21 +0000188 * xmlXIncludeNewContext:
189 * @doc: an XML Document
190 *
191 * Creates a new XInclude context
192 *
193 * Returns the new set
194 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000195static xmlXIncludeCtxtPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000196xmlXIncludeNewContext(xmlDocPtr doc) {
197 xmlXIncludeCtxtPtr ret;
198
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000199#ifdef DEBUG_XINCLUDE
200 xmlGenericError(xmlGenericErrorContext, "New context\n");
201#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000202 if (doc == NULL)
203 return(NULL);
204 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
205 if (ret == NULL)
206 return(NULL);
207 memset(ret, 0, sizeof(xmlXIncludeCtxt));
208 ret->doc = doc;
209 ret->incNr = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000210 ret->incBase = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000211 ret->incMax = 0;
212 ret->incTab = NULL;
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000213 ret->nbErrors = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000214 return(ret);
215}
216
217/**
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000218 * xmlXIncludeURLPush:
219 * @ctxt: the parser context
220 * @value: the url
221 *
222 * Pushes a new url on top of the url stack
223 *
224 * Returns -1 in case of error, the index in the stack otherwise
225 */
226static int
227xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
228 const xmlChar *value)
229{
230 if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
231 xmlGenericError(xmlGenericErrorContext,
232 "XInclude: detected a recursion in %s\n",
233 value);
234 ctxt->nbErrors++;
235 return(-1);
236 }
237 if (ctxt->urlTab == NULL) {
238 ctxt->urlMax = 4;
239 ctxt->urlNr = 0;
240 ctxt->urlTab = (xmlChar * *) xmlMalloc(
241 ctxt->urlMax * sizeof(ctxt->urlTab[0]));
242 if (ctxt->urlTab == NULL) {
243 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
244 return (-1);
245 }
246 }
247 if (ctxt->urlNr >= ctxt->urlMax) {
248 ctxt->urlMax *= 2;
249 ctxt->urlTab =
250 (xmlChar * *) xmlRealloc(ctxt->urlTab,
251 ctxt->urlMax *
252 sizeof(ctxt->urlTab[0]));
253 if (ctxt->urlTab == NULL) {
254 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
255 return (-1);
256 }
257 }
258 ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
259 return (ctxt->urlNr++);
260}
261
262/**
263 * xmlXIncludeURLPop:
264 * @ctxt: the parser context
265 *
266 * Pops the top url from the url stack
267 */
268static void
269xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
270{
271 xmlChar * ret;
272
273 if (ctxt->urlNr <= 0)
274 return;
275 ctxt->urlNr--;
276 if (ctxt->urlNr > 0)
277 ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
278 else
279 ctxt->url = NULL;
280 ret = ctxt->urlTab[ctxt->urlNr];
281 ctxt->urlTab[ctxt->urlNr] = 0;
282 if (ret != NULL)
283 xmlFree(ret);
284}
285
286/**
Owen Taylor3473f882001-02-23 17:55:21 +0000287 * xmlXIncludeFreeContext:
288 * @ctxt: the XInclude context
289 *
290 * Free an XInclude context
291 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000292static void
Owen Taylor3473f882001-02-23 17:55:21 +0000293xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
294 int i;
295
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000296#ifdef DEBUG_XINCLUDE
297 xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
298#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000299 if (ctxt == NULL)
300 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000301 while (ctxt->urlNr > 0)
302 xmlXIncludeURLPop(ctxt);
303 if (ctxt->urlTab != NULL)
304 xmlFree(ctxt->urlTab);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000305 for (i = 0;i < ctxt->incNr;i++) {
306 if (ctxt->incTab[i] != NULL)
307 xmlXIncludeFreeRef(ctxt->incTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +0000308 }
309 for (i = 0;i < ctxt->txtNr;i++) {
310 if (ctxt->txturlTab[i] != NULL)
311 xmlFree(ctxt->txturlTab[i]);
312 }
313 if (ctxt->incTab != NULL)
314 xmlFree(ctxt->incTab);
Owen Taylor3473f882001-02-23 17:55:21 +0000315 if (ctxt->txtTab != NULL)
316 xmlFree(ctxt->txtTab);
317 if (ctxt->txturlTab != NULL)
318 xmlFree(ctxt->txturlTab);
Owen Taylor3473f882001-02-23 17:55:21 +0000319 xmlFree(ctxt);
320}
321
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000322/**
323 * xmlXIncludeAddNode:
324 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000325 * @cur: the new node
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000326 *
327 * Add a new node to process to an XInclude context
328 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000329static int
330xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
331 xmlXIncludeRefPtr ref;
332 xmlURIPtr uri;
333 xmlChar *URL;
334 xmlChar *fragment = NULL;
335 xmlChar *href;
336 xmlChar *parse;
337 xmlChar *base;
338 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000339 int xml = 1, i; /* default Issue 64 */
340 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000341
342
343 if (ctxt == NULL)
344 return(-1);
345 if (cur == NULL)
346 return(-1);
347
348#ifdef DEBUG_XINCLUDE
349 xmlGenericError(xmlGenericErrorContext, "Add node\n");
350#endif
351 /*
352 * read the attributes
353 */
354 href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF);
355 if (href == NULL) {
356 href = xmlGetProp(cur, XINCLUDE_HREF);
357 if (href == NULL) {
358 xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000359 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000360 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000361 }
362 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000363 if (href[0] == '#')
364 local = 1;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000365 parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE);
366 if (parse == NULL) {
367 parse = xmlGetProp(cur, XINCLUDE_PARSE);
368 }
369 if (parse != NULL) {
370 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
371 xml = 1;
372 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
373 xml = 0;
374 else {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000375 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000376 "XInclude: invalid value %s for %s\n",
377 parse, XINCLUDE_PARSE);
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000378 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000379 if (href != NULL)
380 xmlFree(href);
381 if (parse != NULL)
382 xmlFree(parse);
383 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000384 }
385 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000386
387 /*
388 * compute the URI
389 */
390 base = xmlNodeGetBase(ctxt->doc, cur);
391 if (base == NULL) {
392 URI = xmlBuildURI(href, ctxt->doc->URL);
393 } else {
394 URI = xmlBuildURI(href, base);
395 }
396 if (URI == NULL) {
397 xmlChar *escbase;
398 xmlChar *eschref;
399 /*
400 * Some escaping may be needed
401 */
402 escbase = xmlURIEscape(base);
403 eschref = xmlURIEscape(href);
404 URI = xmlBuildURI(eschref, escbase);
405 if (escbase != NULL)
406 xmlFree(escbase);
407 if (eschref != NULL)
408 xmlFree(eschref);
409 }
410 if (parse != NULL)
411 xmlFree(parse);
412 if (href != NULL)
413 xmlFree(href);
414 if (base != NULL)
415 xmlFree(base);
416 if (URI == NULL) {
417 xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000418 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000419 return(-1);
420 }
421
422 /*
423 * Check the URL and remove any fragment identifier
424 */
425 uri = xmlParseURI((const char *)URI);
426 if (uri == NULL) {
427 xmlGenericError(xmlGenericErrorContext,
428 "XInclude: invalid value URI %s\n", URI);
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000429 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000430 return(-1);
431 }
432 if (uri->fragment != NULL) {
433 fragment = (xmlChar *) uri->fragment;
434 uri->fragment = NULL;
435 }
436 URL = xmlSaveUri(uri);
437 xmlFreeURI(uri);
438 xmlFree(URI);
439 if (URL == NULL) {
440 xmlGenericError(xmlGenericErrorContext,
441 "XInclude: invalid value URI %s\n", URI);
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000442 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000443 if (fragment != NULL)
444 xmlFree(fragment);
445 return(-1);
446 }
447
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000448 /*
449 * Check the URL against the stack for recursions
450 */
451 if (!local) {
452 for (i = 0;i < ctxt->urlNr;i++) {
453 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
454 xmlGenericError(xmlGenericErrorContext,
455 "XInclude: detected a recursion in %s\n",
456 URL);
457 ctxt->nbErrors++;
458 return(-1);
459 }
460 }
461 }
462
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000463 ref = xmlXIncludeNewRef(ctxt, URL, cur);
464 if (ref == NULL) {
465 return(-1);
466 }
467 ref->fragment = fragment;
468 ref->doc = NULL;
469 ref->xml = xml;
470 ref->count = 1;
471 xmlFree(URL);
472 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000473}
474
475/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000476 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000477 * @ctxt: the XInclude context
478 * @doc: the new document
479 * @url: the associated URL
480 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000481 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000482 */
483static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000484xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000485 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000486 xmlXIncludeCtxtPtr newctxt;
487 int i;
488
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000489 /*
490 * Avoid recursion in already substitued resources
491 for (i = 0;i < ctxt->urlNr;i++) {
492 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
493 return;
494 }
495 */
496
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000497#ifdef DEBUG_XINCLUDE
498 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
499#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000500 /*
501 * Handle recursion here.
502 */
503
504 newctxt = xmlXIncludeNewContext(doc);
505 if (newctxt != NULL) {
506 /*
507 * Copy the existing document set
508 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000509 newctxt->incMax = ctxt->incMax;
510 newctxt->incNr = ctxt->incNr;
511 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
512 sizeof(newctxt->incTab[0]));
513 if (newctxt->incTab == NULL) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000514 xmlGenericError(xmlGenericErrorContext,
515 "malloc failed !\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000516 ctxt->nbErrors++;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000517 xmlFree(newctxt);
518 return;
519 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000520 /*
521 * copy the urlTab
522 */
523 newctxt->urlMax = ctxt->urlMax;
524 newctxt->urlNr = ctxt->urlNr;
525 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000526
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000527 /*
528 * Inherit the documents already in use by others includes
529 */
530 newctxt->incBase = ctxt->incNr;
531 for (i = 0;i < ctxt->incNr;i++) {
532 newctxt->incTab[i] = ctxt->incTab[i];
533 newctxt->incTab[i]->count++; /* prevent the recursion from
534 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000535 }
536 xmlXIncludeDoProcess(newctxt, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000537 for (i = 0;i < ctxt->incNr;i++) {
538 newctxt->incTab[i]->count--;
539 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000540 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000541
542 /* urlTab may have been reallocated */
543 ctxt->urlTab = newctxt->urlTab;
544 ctxt->urlMax = newctxt->urlMax;
545
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000546 newctxt->urlMax = 0;
547 newctxt->urlNr = 0;
548 newctxt->urlTab = NULL;
549
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000550 xmlXIncludeFreeContext(newctxt);
551 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000552#ifdef DEBUG_XINCLUDE
553 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
554#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000555}
556
557/**
558 * xmlXIncludeAddTxt:
559 * @ctxt: the XInclude context
560 * @txt: the new text node
561 * @url: the associated URL
562 *
563 * Add a new txtument to the list
564 */
565static void
566xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000567#ifdef DEBUG_XINCLUDE
568 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
569#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000570 if (ctxt->txtMax == 0) {
571 ctxt->txtMax = 4;
572 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
573 sizeof(ctxt->txtTab[0]));
574 if (ctxt->txtTab == NULL) {
575 xmlGenericError(xmlGenericErrorContext,
576 "malloc failed !\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000577 ctxt->nbErrors++;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000578 return;
579 }
580 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
581 sizeof(ctxt->txturlTab[0]));
582 if (ctxt->txturlTab == NULL) {
583 xmlGenericError(xmlGenericErrorContext,
584 "malloc failed !\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000585 ctxt->nbErrors++;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000586 return;
587 }
588 }
589 if (ctxt->txtNr >= ctxt->txtMax) {
590 ctxt->txtMax *= 2;
591 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
592 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
593 if (ctxt->txtTab == NULL) {
594 xmlGenericError(xmlGenericErrorContext,
595 "realloc failed !\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000596 ctxt->nbErrors++;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000597 return;
598 }
599 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000600 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000601 if (ctxt->txturlTab == NULL) {
602 xmlGenericError(xmlGenericErrorContext,
603 "realloc failed !\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000604 ctxt->nbErrors++;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000605 return;
606 }
607 }
608 ctxt->txtTab[ctxt->txtNr] = txt;
609 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
610 ctxt->txtNr++;
611}
612
Owen Taylor3473f882001-02-23 17:55:21 +0000613/************************************************************************
614 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000615 * Node copy with specific semantic *
616 * *
617 ************************************************************************/
618
619/**
620 * xmlXIncludeCopyNode:
621 * @ctxt: the XInclude context
622 * @target: the document target
623 * @source: the document source
624 * @elem: the element
625 *
626 * Make a copy of the node while preserving the XInclude semantic
627 * of the Infoset copy
628 */
629static xmlNodePtr
630xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
631 xmlDocPtr source, xmlNodePtr elem) {
632 xmlNodePtr result = NULL;
633
634 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
635 (elem == NULL))
636 return(NULL);
637 if (elem->type == XML_DTD_NODE)
638 return(NULL);
639 result = xmlDocCopyNode(elem, target, 1);
640 return(result);
641}
642
643/**
644 * xmlXIncludeCopyNodeList:
645 * @ctxt: the XInclude context
646 * @target: the document target
647 * @source: the document source
648 * @elem: the element list
649 *
650 * Make a copy of the node list while preserving the XInclude semantic
651 * of the Infoset copy
652 */
653static xmlNodePtr
654xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
655 xmlDocPtr source, xmlNodePtr elem) {
656 xmlNodePtr cur, res, result = NULL, last = NULL;
657
658 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
659 (elem == NULL))
660 return(NULL);
661 cur = elem;
662 while (cur != NULL) {
663 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
664 if (res != NULL) {
665 if (result == NULL) {
666 result = last = res;
667 } else {
668 last->next = res;
669 res->prev = last;
670 last = res;
671 }
672 }
673 cur = cur->next;
674 }
675 return(result);
676}
677
678/**
679 * xmlXInclueGetNthChild:
680 * @cur: the node
681 * @no: the child number
682 *
683 * Returns the @no'th element child of @cur or NULL
684 */
685static xmlNodePtr
686xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
687 int i;
688 if (cur == NULL)
689 return(cur);
690 cur = cur->children;
691 for (i = 0;i <= no;cur = cur->next) {
692 if (cur == NULL)
693 return(cur);
694 if ((cur->type == XML_ELEMENT_NODE) ||
695 (cur->type == XML_DOCUMENT_NODE) ||
696 (cur->type == XML_HTML_DOCUMENT_NODE)) {
697 i++;
698 if (i == no)
699 break;
700 }
701 }
702 return(cur);
703}
704
705xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur);
706
707/**
708 * xmlXIncludeCopyRange:
709 * @ctxt: the XInclude context
710 * @target: the document target
711 * @source: the document source
712 * @obj: the XPointer result from the evaluation.
713 *
714 * Build a node list tree copy of the XPointer result.
715 *
716 * Returns an xmlNodePtr list or NULL.
717 * the caller has to free the node tree.
718 */
719static xmlNodePtr
720xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
721 xmlDocPtr source, xmlXPathObjectPtr range) {
722 /* pointers to generated nodes */
723 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
724 /* pointers to traversal nodes */
725 xmlNodePtr start, cur, end;
726 int index1, index2;
727
728 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
729 (range == NULL))
730 return(NULL);
731 if (range->type != XPATH_RANGE)
732 return(NULL);
733 start = (xmlNodePtr) range->user;
734
735 if (start == NULL)
736 return(NULL);
737 end = range->user2;
738 if (end == NULL)
739 return(xmlDocCopyNode(start, target, 1));
740
741 cur = start;
742 index1 = range->index;
743 index2 = range->index2;
744 while (cur != NULL) {
745 if (cur == end) {
746 if (cur->type == XML_TEXT_NODE) {
747 const xmlChar *content = cur->content;
748 int len;
749
750 if (content == NULL) {
751 tmp = xmlNewTextLen(NULL, 0);
752 } else {
753 len = index2;
754 if ((cur == start) && (index1 > 1)) {
755 content += (index1 - 1);
756 len -= (index1 - 1);
757 index1 = 0;
758 } else {
759 len = index2;
760 }
761 tmp = xmlNewTextLen(content, len);
762 }
763 /* single sub text node selection */
764 if (list == NULL)
765 return(tmp);
766 /* prune and return full set */
767 if (last != NULL)
768 xmlAddNextSibling(last, tmp);
769 else
770 xmlAddChild(parent, tmp);
771 return(list);
772 } else {
773 tmp = xmlDocCopyNode(cur, target, 0);
774 if (list == NULL)
775 list = tmp;
776 else {
777 if (last != NULL)
778 xmlAddNextSibling(last, tmp);
779 else
780 xmlAddChild(parent, tmp);
781 }
782 last = NULL;
783 parent = tmp;
784
785 if (index2 > 1) {
786 end = xmlXIncludeGetNthChild(cur, index2 - 1);
787 index2 = 0;
788 }
789 if ((cur == start) && (index1 > 1)) {
790 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
791 index1 = 0;
792 } else {
793 cur = cur->children;
794 }
795 /*
796 * Now gather the remaining nodes from cur to end
797 */
798 continue; /* while */
799 }
800 } else if ((cur == start) &&
801 (list == NULL) /* looks superfluous but ... */ ) {
802 if ((cur->type == XML_TEXT_NODE) ||
803 (cur->type == XML_CDATA_SECTION_NODE)) {
804 const xmlChar *content = cur->content;
805
806 if (content == NULL) {
807 tmp = xmlNewTextLen(NULL, 0);
808 } else {
809 if (index1 > 1) {
810 content += (index1 - 1);
811 }
812 tmp = xmlNewText(content);
813 }
814 last = list = tmp;
815 } else {
816 if ((cur == start) && (index1 > 1)) {
817 tmp = xmlDocCopyNode(cur, target, 0);
818 list = tmp;
819 parent = tmp;
820 last = NULL;
821 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
822 index1 = 0;
823 /*
824 * Now gather the remaining nodes from cur to end
825 */
826 continue; /* while */
827 }
828 tmp = xmlDocCopyNode(cur, target, 1);
829 list = tmp;
830 parent = NULL;
831 last = tmp;
832 }
833 } else {
834 tmp = NULL;
835 switch (cur->type) {
836 case XML_DTD_NODE:
837 case XML_ELEMENT_DECL:
838 case XML_ATTRIBUTE_DECL:
839 case XML_ENTITY_NODE:
840 /* Do not copy DTD informations */
841 break;
842 case XML_ENTITY_DECL:
843 /* handle crossing entities -> stack needed */
844 break;
845 case XML_XINCLUDE_START:
846 case XML_XINCLUDE_END:
847 /* don't consider it part of the tree content */
848 break;
849 case XML_ATTRIBUTE_NODE:
850 /* Humm, should not happen ! */
851 break;
852 default:
853 tmp = xmlDocCopyNode(cur, target, 1);
854 break;
855 }
856 if (tmp != NULL) {
857 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
858 return(NULL);
859 }
860 if (last != NULL)
861 xmlAddNextSibling(last, tmp);
862 else {
863 xmlAddChild(parent, tmp);
864 last = tmp;
865 }
866 }
867 }
868 /*
869 * Skip to next node in document order
870 */
871 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
872 return(NULL);
873 }
874 cur = xmlXPtrAdvanceNode(cur);
875 }
876 return(list);
877}
878
879/**
880 * xmlXIncludeBuildNodeList:
881 * @ctxt: the XInclude context
882 * @target: the document target
883 * @source: the document source
884 * @obj: the XPointer result from the evaluation.
885 *
886 * Build a node list tree copy of the XPointer result.
887 * This will drop Attributes and Namespace declarations.
888 *
889 * Returns an xmlNodePtr list or NULL.
890 * the caller has to free the node tree.
891 */
892static xmlNodePtr
893xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
894 xmlDocPtr source, xmlXPathObjectPtr obj) {
895 xmlNodePtr list = NULL, last = NULL;
896 int i;
897
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000898 if (source == NULL)
899 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000900 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
901 (obj == NULL))
902 return(NULL);
903 switch (obj->type) {
904 case XPATH_NODESET: {
905 xmlNodeSetPtr set = obj->nodesetval;
906 if (set == NULL)
907 return(NULL);
908 for (i = 0;i < set->nodeNr;i++) {
909 if (set->nodeTab[i] == NULL)
910 continue;
911 switch (set->nodeTab[i]->type) {
912 case XML_TEXT_NODE:
913 case XML_CDATA_SECTION_NODE:
914 case XML_ELEMENT_NODE:
915 case XML_ENTITY_REF_NODE:
916 case XML_ENTITY_NODE:
917 case XML_PI_NODE:
918 case XML_COMMENT_NODE:
919 case XML_DOCUMENT_NODE:
920 case XML_HTML_DOCUMENT_NODE:
921#ifdef LIBXML_DOCB_ENABLED
922 case XML_DOCB_DOCUMENT_NODE:
923#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000924 case XML_XINCLUDE_END:
925 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000926 case XML_XINCLUDE_START: {
927 xmlNodePtr tmp, cur = set->nodeTab[i];
928
929 cur = cur->next;
930 while (cur != NULL) {
931 switch(cur->type) {
932 case XML_TEXT_NODE:
933 case XML_CDATA_SECTION_NODE:
934 case XML_ELEMENT_NODE:
935 case XML_ENTITY_REF_NODE:
936 case XML_ENTITY_NODE:
937 case XML_PI_NODE:
938 case XML_COMMENT_NODE:
939 tmp = xmlXIncludeCopyNode(ctxt, target,
940 source, cur);
941 if (last == NULL) {
942 list = last = tmp;
943 } else {
944 xmlAddNextSibling(last, tmp);
945 last = tmp;
946 }
947 cur = cur->next;
948 continue;
949 default:
950 break;
951 }
952 break;
953 }
954 continue;
955 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000956 case XML_ATTRIBUTE_NODE:
957 case XML_NAMESPACE_DECL:
958 case XML_DOCUMENT_TYPE_NODE:
959 case XML_DOCUMENT_FRAG_NODE:
960 case XML_NOTATION_NODE:
961 case XML_DTD_NODE:
962 case XML_ELEMENT_DECL:
963 case XML_ATTRIBUTE_DECL:
964 case XML_ENTITY_DECL:
965 continue; /* for */
966 }
967 if (last == NULL)
968 list = last = xmlXIncludeCopyNode(ctxt, target, source,
969 set->nodeTab[i]);
970 else {
971 xmlAddNextSibling(last,
972 xmlXIncludeCopyNode(ctxt, target, source,
973 set->nodeTab[i]));
974 if (last->next != NULL)
975 last = last->next;
976 }
977 }
978 break;
979 }
980 case XPATH_LOCATIONSET: {
981 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
982 if (set == NULL)
983 return(NULL);
984 for (i = 0;i < set->locNr;i++) {
985 if (last == NULL)
986 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
987 set->locTab[i]);
988 else
989 xmlAddNextSibling(last,
990 xmlXIncludeCopyXPointer(ctxt, target, source,
991 set->locTab[i]));
992 if (last != NULL) {
993 while (last->next != NULL)
994 last = last->next;
995 }
996 }
997 break;
998 }
999 case XPATH_RANGE:
1000 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
1001 case XPATH_POINT:
1002 /* points are ignored in XInclude */
1003 break;
1004 default:
1005 break;
1006 }
1007 return(list);
1008}
1009/************************************************************************
1010 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001011 * XInclude I/O handling *
1012 * *
1013 ************************************************************************/
1014
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001015typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1016typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1017struct _xmlXIncludeMergeData {
1018 xmlDocPtr doc;
1019 xmlXIncludeCtxtPtr ctxt;
1020};
1021
Owen Taylor3473f882001-02-23 17:55:21 +00001022/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001023 * xmlXIncludeMergeOneEntity:
1024 * @ent: the entity
1025 * @doc: the including doc
1026 * @nr: the entity name
1027 *
1028 * Inplements the merge of one entity
1029 */
1030static void
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001031xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Daniel Veillard4287c572003-02-04 22:48:53 +00001032 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001033 xmlEntityPtr ret, prev;
1034 xmlDocPtr doc;
1035 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001036
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001037 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001038 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001039 ctxt = data->ctxt;
1040 doc = data->doc;
1041 if ((ctxt == NULL) || (doc == NULL))
1042 return;
1043 switch (ent->etype) {
1044 case XML_INTERNAL_PARAMETER_ENTITY:
1045 case XML_EXTERNAL_PARAMETER_ENTITY:
1046 case XML_INTERNAL_PREDEFINED_ENTITY:
1047 return;
1048 case XML_INTERNAL_GENERAL_ENTITY:
1049 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1050 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1051 break;
1052 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001053 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1054 ent->SystemID, ent->content);
1055 if (ret != NULL) {
1056 if (ent->URI != NULL)
1057 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001058 } else {
1059 prev = xmlGetDocEntity(doc, ent->name);
1060 if (prev != NULL) {
1061 if (ent->etype != prev->etype)
1062 goto error;
1063
1064 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1065 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1066 goto error;
1067 } else if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1068 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1069 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001070 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1071 if (!xmlStrEqual(ent->content, prev->content))
1072 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001073 } else {
1074 goto error;
1075 }
1076
1077 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001078 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001079 return;
1080error:
1081 xmlGenericError(xmlGenericErrorContext,
1082 "XInclude: mismatch in redefinition of entity %s\n", ent->name);
1083 ctxt->nbErrors++;
Daniel Veillard4287c572003-02-04 22:48:53 +00001084}
1085
1086/**
1087 * xmlXIncludeMergeEntities:
1088 * @ctxt: an XInclude context
1089 * @doc: the including doc
1090 * @from: the included doc
1091 *
1092 * Inplements the entity merge
1093 *
1094 * Returns 0 if merge succeeded, -1 if some processing failed
1095 */
1096static int
1097xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1098 xmlDocPtr from) {
1099 xmlNodePtr cur;
1100 xmlDtdPtr target, source;
1101
1102 if (ctxt == NULL)
1103 return(-1);
1104
1105 if ((from == NULL) || (from->intSubset == NULL))
1106 return(0);
1107
1108 target = doc->intSubset;
1109 if (target == NULL) {
1110 cur = xmlDocGetRootElement(doc);
1111 if (cur == NULL)
1112 return(-1);
1113 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1114 if (target == NULL)
1115 return(-1);
1116 }
1117
1118 source = from->intSubset;
1119 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001120 xmlXIncludeMergeData data;
1121
1122 data.ctxt = ctxt;
1123 data.doc = doc;
1124
Daniel Veillard4287c572003-02-04 22:48:53 +00001125 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001126 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001127 }
1128 source = from->extSubset;
1129 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001130 xmlXIncludeMergeData data;
1131
1132 data.ctxt = ctxt;
1133 data.doc = doc;
1134
Daniel Veillard4287c572003-02-04 22:48:53 +00001135 /*
1136 * don't duplicate existing stuff when external subsets are the same
1137 */
1138 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1139 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1140 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001141 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001142 }
1143 }
1144 return(0);
1145}
1146
1147/**
Owen Taylor3473f882001-02-23 17:55:21 +00001148 * xmlXIncludeLoadDoc:
1149 * @ctxt: the XInclude context
1150 * @url: the associated URL
1151 * @nr: the xinclude node number
1152 *
1153 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001154 *
1155 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001156 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001157static int
Owen Taylor3473f882001-02-23 17:55:21 +00001158xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1159 xmlDocPtr doc;
1160 xmlURIPtr uri;
1161 xmlChar *URL;
1162 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001163 int i = 0;
1164
1165#ifdef DEBUG_XINCLUDE
1166 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1167#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001168 /*
1169 * Check the URL and remove any fragment identifier
1170 */
1171 uri = xmlParseURI((const char *)url);
1172 if (uri == NULL) {
1173 xmlGenericError(xmlGenericErrorContext,
1174 "XInclude: invalid value URI %s\n", url);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001175 ctxt->nbErrors++;
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001176 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001177 }
1178 if (uri->fragment != NULL) {
1179 fragment = (xmlChar *) uri->fragment;
1180 uri->fragment = NULL;
1181 }
1182 URL = xmlSaveUri(uri);
1183 xmlFreeURI(uri);
1184 if (URL == NULL) {
1185 xmlGenericError(xmlGenericErrorContext,
1186 "XInclude: invalid value URI %s\n", url);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001187 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001188 if (fragment != NULL)
1189 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001190 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001191 }
1192
1193 /*
1194 * Handling of references to the local document are done
1195 * directly through ctxt->doc.
1196 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001197 if ((URL[0] == 0) || (URL[0] == '#') ||
1198 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001199 doc = NULL;
1200 goto loaded;
1201 }
1202
1203 /*
1204 * Prevent reloading twice the document.
1205 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001206 for (i = 0; i < ctxt->incNr; i++) {
1207 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1208 (ctxt->incTab[i]->doc != NULL)) {
1209 doc = ctxt->incTab[i]->doc;
1210#ifdef DEBUG_XINCLUDE
1211 printf("Already loaded %s\n", URL);
1212#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001213 goto loaded;
1214 }
1215 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001216
Owen Taylor3473f882001-02-23 17:55:21 +00001217 /*
1218 * Load it.
1219 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001220#ifdef DEBUG_XINCLUDE
1221 printf("loading %s\n", URL);
1222#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001223 doc = xmlParseFile((const char *)URL);
1224 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001225 xmlFree(URL);
1226 if (fragment != NULL)
1227 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001228 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001229 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001230 ctxt->incTab[nr]->doc = doc;
1231
1232 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001233 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001234 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001235 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001236
1237 /*
1238 * We don't need the DTD anymore, free up space
1239 if (doc->intSubset != NULL) {
1240 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1241 xmlFreeNode((xmlNodePtr) doc->intSubset);
1242 doc->intSubset = NULL;
1243 }
1244 if (doc->extSubset != NULL) {
1245 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1246 xmlFreeNode((xmlNodePtr) doc->extSubset);
1247 doc->extSubset = NULL;
1248 }
1249 */
1250 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001251
1252loaded:
1253 if (fragment == NULL) {
1254 /*
1255 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001256 */
1257 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001258 {
1259 /* Hopefully a DTD declaration won't be copied from
1260 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001261 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001262 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001263 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001264 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001265 }
Owen Taylor3473f882001-02-23 17:55:21 +00001266 } else {
1267 /*
1268 * Computes the XPointer expression and make a copy used
1269 * as the replacement copy.
1270 */
1271 xmlXPathObjectPtr xptr;
1272 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001273 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001274
1275 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001276 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1277 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001278 } else {
1279 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1280 }
1281 if (xptrctxt == NULL) {
1282 xmlGenericError(xmlGenericErrorContext,
1283 "XInclude: could create XPointer context\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001284 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001285 xmlFree(URL);
1286 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001287 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001288 }
1289 xptr = xmlXPtrEval(fragment, xptrctxt);
1290 if (xptr == NULL) {
1291 xmlGenericError(xmlGenericErrorContext,
1292 "XInclude: XPointer evaluation failed: #%s\n",
1293 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001294 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001295 xmlXPathFreeContext(xptrctxt);
1296 xmlFree(URL);
1297 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001298 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001299 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001300 switch (xptr->type) {
1301 case XPATH_UNDEFINED:
1302 case XPATH_BOOLEAN:
1303 case XPATH_NUMBER:
1304 case XPATH_STRING:
1305 case XPATH_POINT:
1306 case XPATH_USERS:
1307 case XPATH_XSLT_TREE:
1308 xmlGenericError(xmlGenericErrorContext,
1309 "XInclude: XPointer is not a range: #%s\n",
1310 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001311 ctxt->nbErrors++;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001312 xmlXPathFreeContext(xptrctxt);
1313 xmlFree(URL);
1314 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001315 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001316 case XPATH_NODESET:
1317 case XPATH_RANGE:
1318 case XPATH_LOCATIONSET:
1319 break;
1320 }
1321 set = xptr->nodesetval;
1322 if (set != NULL) {
1323 for (i = 0;i < set->nodeNr;i++) {
1324 if (set->nodeTab[i] == NULL)
1325 continue;
1326 switch (set->nodeTab[i]->type) {
1327 case XML_TEXT_NODE:
1328 case XML_CDATA_SECTION_NODE:
1329 case XML_ELEMENT_NODE:
1330 case XML_ENTITY_REF_NODE:
1331 case XML_ENTITY_NODE:
1332 case XML_PI_NODE:
1333 case XML_COMMENT_NODE:
1334 case XML_DOCUMENT_NODE:
1335 case XML_HTML_DOCUMENT_NODE:
1336#ifdef LIBXML_DOCB_ENABLED
1337 case XML_DOCB_DOCUMENT_NODE:
1338#endif
1339 continue;
1340 case XML_ATTRIBUTE_NODE:
1341 xmlGenericError(xmlGenericErrorContext,
1342 "XInclude: XPointer selects an attribute: #%s\n",
1343 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001344 ctxt->nbErrors++;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001345 set->nodeTab[i] = NULL;
1346 continue;
1347 case XML_NAMESPACE_DECL:
1348 xmlGenericError(xmlGenericErrorContext,
1349 "XInclude: XPointer selects a namespace: #%s\n",
1350 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001351 ctxt->nbErrors++;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001352 set->nodeTab[i] = NULL;
1353 continue;
1354 case XML_DOCUMENT_TYPE_NODE:
1355 case XML_DOCUMENT_FRAG_NODE:
1356 case XML_NOTATION_NODE:
1357 case XML_DTD_NODE:
1358 case XML_ELEMENT_DECL:
1359 case XML_ATTRIBUTE_DECL:
1360 case XML_ENTITY_DECL:
1361 case XML_XINCLUDE_START:
1362 case XML_XINCLUDE_END:
1363 xmlGenericError(xmlGenericErrorContext,
1364 "XInclude: XPointer selects unexpected nodes: #%s\n",
1365 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001366 ctxt->nbErrors++;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001367 set->nodeTab[i] = NULL;
1368 set->nodeTab[i] = NULL;
1369 continue; /* for */
1370 }
1371 }
1372 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001373 if (doc == NULL) {
1374 ctxt->incTab[nr]->xptr = xptr;
1375 ctxt->incTab[nr]->inc = NULL;
1376 } else {
1377 ctxt->incTab[nr]->inc =
1378 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1379 xmlXPathFreeObject(xptr);
1380 }
Owen Taylor3473f882001-02-23 17:55:21 +00001381 xmlXPathFreeContext(xptrctxt);
1382 xmlFree(fragment);
1383 }
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001384
1385 /*
1386 * Do the xml:base fixup if needed
1387 */
1388 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1389 xmlNodePtr node;
1390
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001391 node = ctxt->incTab[nr]->inc;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001392 while (node != NULL) {
1393 if (node->type == XML_ELEMENT_NODE)
1394 xmlNodeSetBase(node, URL);
1395 node = node->next;
1396 }
1397 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001398 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1399 (ctxt->incTab[nr]->count <= 1)) {
1400#ifdef DEBUG_XINCLUDE
1401 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1402#endif
1403 xmlFreeDoc(ctxt->incTab[nr]->doc);
1404 ctxt->incTab[nr]->doc = NULL;
1405 }
Owen Taylor3473f882001-02-23 17:55:21 +00001406 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001407 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001408}
1409
1410/**
1411 * xmlXIncludeLoadTxt:
1412 * @ctxt: the XInclude context
1413 * @url: the associated URL
1414 * @nr: the xinclude node number
1415 *
1416 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001417 *
1418 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001419 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001420static int
Owen Taylor3473f882001-02-23 17:55:21 +00001421xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1422 xmlParserInputBufferPtr buf;
1423 xmlNodePtr node;
1424 xmlURIPtr uri;
1425 xmlChar *URL;
1426 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001427 xmlChar *encoding = NULL;
1428 xmlCharEncoding enc = 0;
1429
Owen Taylor3473f882001-02-23 17:55:21 +00001430 /*
1431 * Check the URL and remove any fragment identifier
1432 */
1433 uri = xmlParseURI((const char *)url);
1434 if (uri == NULL) {
1435 xmlGenericError(xmlGenericErrorContext,
1436 "XInclude: invalid value URI %s\n", url);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001437 ctxt->nbErrors++;
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001438 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001439 }
1440 if (uri->fragment != NULL) {
1441 xmlGenericError(xmlGenericErrorContext,
1442 "XInclude: fragment identifier forbidden for text: %s\n",
1443 uri->fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001444 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001445 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001446 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001447 }
1448 URL = xmlSaveUri(uri);
1449 xmlFreeURI(uri);
1450 if (URL == NULL) {
1451 xmlGenericError(xmlGenericErrorContext,
1452 "XInclude: invalid value URI %s\n", url);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001453 ctxt->nbErrors++;
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001454 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001455 }
1456
1457 /*
1458 * Handling of references to the local document are done
1459 * directly through ctxt->doc.
1460 */
1461 if (URL[0] == 0) {
1462 xmlGenericError(xmlGenericErrorContext,
1463 "XInclude: text serialization of document not available\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001464 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001465 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001466 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001467 }
1468
1469 /*
1470 * Prevent reloading twice the document.
1471 */
1472 for (i = 0; i < ctxt->txtNr; i++) {
1473 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1474 node = xmlCopyNode(ctxt->txtTab[i], 1);
1475 goto loaded;
1476 }
1477 }
1478 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001479 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001480 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001481 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1482 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1483 }
1484 if (encoding != NULL) {
1485 /*
1486 * TODO: we should not have to remap to the xmlCharEncoding
1487 * predefined set, a better interface than
1488 * xmlParserInputBufferCreateFilename should allow any
1489 * encoding supported by iconv
1490 */
1491 enc = xmlParseCharEncoding((const char *) encoding);
1492 if (enc == XML_CHAR_ENCODING_ERROR) {
1493 xmlGenericError(xmlGenericErrorContext,
1494 "XInclude: encoding %s not supported\n", encoding);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001495 ctxt->nbErrors++;
Daniel Veillardd076a202002-11-20 13:28:31 +00001496 xmlFree(encoding);
1497 xmlFree(URL);
1498 return(-1);
1499 }
1500 xmlFree(encoding);
1501 }
1502
1503 /*
1504 * Load it.
1505 */
1506 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001507 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001508 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001509 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001510 }
1511 node = xmlNewText(NULL);
1512
1513 /*
1514 * Scan all chars from the resource and add the to the node
1515 */
1516 while (xmlParserInputBufferRead(buf, 128) > 0) {
1517 int len;
1518 const xmlChar *content;
1519
1520 content = xmlBufferContent(buf->buffer);
1521 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001522 for (i = 0;i < len;) {
1523 int cur;
1524 int l;
1525
1526 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1527 if (!IS_CHAR(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001528 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd076a202002-11-20 13:28:31 +00001529 "XInclude: %s contains invalid char %d\n", URL, cur);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001530 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001531 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001532 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001533 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001534 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001535 }
1536 xmlBufferShrink(buf->buffer, len);
1537 }
1538 xmlFreeParserInputBuffer(buf);
1539 xmlXIncludeAddTxt(ctxt, node, URL);
1540
1541loaded:
1542 /*
1543 * Add the element as the replacement copy.
1544 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001545 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001546 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001547 return(0);
1548}
1549
1550/**
1551 * xmlXIncludeLoadFallback:
1552 * @ctxt: the XInclude context
1553 * @fallback: the fallback node
1554 * @nr: the xinclude node number
1555 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001556 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001557 * in the XInclude context
1558 *
1559 * Returns 0 in case of success, -1 in case of failure
1560 */
1561static int
1562xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
1563 if ((fallback == NULL) || (ctxt == NULL))
1564 return(-1);
1565
Daniel Veillard06503452002-12-13 10:42:08 +00001566 ctxt->incTab[nr]->inc = xmlCopyNodeList(fallback->children);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001567 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001568}
1569
1570/************************************************************************
1571 * *
1572 * XInclude Processing *
1573 * *
1574 ************************************************************************/
1575
1576/**
1577 * xmlXIncludePreProcessNode:
1578 * @ctxt: an XInclude context
1579 * @node: an XInclude node
1580 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001581 * Implement the XInclude preprocessing, currently just adding the element
1582 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001583 *
1584 * Returns the result list or NULL in case of error
1585 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001586static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001587xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1588 xmlXIncludeAddNode(ctxt, node);
1589 return(0);
1590}
1591
Daniel Veillard118aed72002-09-24 14:13:13 +00001592#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00001593/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001594 * xmlXIncludePreloadNode:
1595 * @ctxt: an XInclude context
1596 * @nr: the node number
1597 *
1598 * Do some precomputations and preload shared documents
1599 *
1600 * Returns 0 if substitution succeeded, -1 if some processing failed
1601 */
1602static int
1603xmlXIncludePreloadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1604 xmlNodePtr cur;
1605 xmlChar *href;
1606 xmlChar *parse;
1607 xmlChar *base;
1608 xmlChar *URI;
1609 int xml = 1; /* default Issue 64 */
1610 xmlURIPtr uri;
1611 xmlChar *URL;
1612 xmlChar *fragment = NULL;
1613 int i;
1614
1615
1616 if (ctxt == NULL)
1617 return(-1);
1618 if ((nr < 0) || (nr >= ctxt->incNr))
1619 return(-1);
1620 cur = ctxt->incTab[nr]->ref;
1621 if (cur == NULL)
1622 return(-1);
1623
1624 /*
1625 * read the attributes
1626 */
1627 href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF);
1628 if (href == NULL) {
1629 href = xmlGetProp(cur, XINCLUDE_HREF);
1630 if (href == NULL) {
1631 xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n");
1632 return(-1);
1633 }
1634 }
1635 parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE);
1636 if (parse == NULL) {
1637 parse = xmlGetProp(cur, XINCLUDE_PARSE);
1638 }
1639 if (parse != NULL) {
1640 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1641 xml = 1;
1642 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1643 xml = 0;
1644 else {
1645 xmlGenericError(xmlGenericErrorContext,
1646 "XInclude: invalid value %s for %s\n",
1647 parse, XINCLUDE_PARSE);
1648 if (href != NULL)
1649 xmlFree(href);
1650 if (parse != NULL)
1651 xmlFree(parse);
1652 return(-1);
1653 }
1654 }
1655
1656 /*
1657 * compute the URI
1658 */
1659 base = xmlNodeGetBase(ctxt->doc, cur);
1660 if (base == NULL) {
1661 URI = xmlBuildURI(href, ctxt->doc->URL);
1662 } else {
1663 URI = xmlBuildURI(href, base);
1664 }
1665 if (URI == NULL) {
1666 xmlChar *escbase;
1667 xmlChar *eschref;
1668 /*
1669 * Some escaping may be needed
1670 */
1671 escbase = xmlURIEscape(base);
1672 eschref = xmlURIEscape(href);
1673 URI = xmlBuildURI(eschref, escbase);
1674 if (escbase != NULL)
1675 xmlFree(escbase);
1676 if (eschref != NULL)
1677 xmlFree(eschref);
1678 }
1679 if (parse != NULL)
1680 xmlFree(parse);
1681 if (href != NULL)
1682 xmlFree(href);
1683 if (base != NULL)
1684 xmlFree(base);
1685 if (URI == NULL) {
1686 xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001687 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001688 return(-1);
1689 }
1690
1691 /*
1692 * Check the URL and remove any fragment identifier
1693 */
1694 uri = xmlParseURI((const char *)URI);
1695 if (uri == NULL) {
1696 xmlGenericError(xmlGenericErrorContext,
1697 "XInclude: invalid value URI %s\n", URI);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001698 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001699 xmlFree(URI);
1700 return(-1);
1701 }
1702 if (uri->fragment != NULL) {
1703 fragment = (xmlChar *) uri->fragment;
1704 uri->fragment = NULL;
1705 }
1706 URL = xmlSaveUri(uri);
1707 xmlFreeURI(uri);
1708 if (URL == NULL) {
1709 xmlGenericError(xmlGenericErrorContext,
1710 "XInclude: invalid value URI %s\n", URI);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001711 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001712 if (fragment != NULL)
1713 xmlFree(fragment);
1714 xmlFree(URI);
1715 return(-1);
1716 }
1717 xmlFree(URI);
1718 if (fragment != NULL)
1719 xmlFree(fragment);
1720
1721 for (i = 0; i < nr; i++) {
1722 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1723#ifdef DEBUG_XINCLUDE
1724 printf("Incrementing count for %d : %s\n", i, ctxt->incTab[i]->URI);
1725#endif
1726 ctxt->incTab[i]->count++;
1727 break;
1728 }
1729 }
1730 xmlFree(URL);
1731 return(0);
1732}
Daniel Veillard118aed72002-09-24 14:13:13 +00001733#endif
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001734
1735/**
Owen Taylor3473f882001-02-23 17:55:21 +00001736 * xmlXIncludeLoadNode:
1737 * @ctxt: an XInclude context
1738 * @nr: the node number
1739 *
1740 * Find and load the infoset replacement for the given node.
1741 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001742 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001743 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001744static int
Owen Taylor3473f882001-02-23 17:55:21 +00001745xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1746 xmlNodePtr cur;
1747 xmlChar *href;
1748 xmlChar *parse;
1749 xmlChar *base;
1750 xmlChar *URI;
1751 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001752 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001753
1754 if (ctxt == NULL)
1755 return(-1);
1756 if ((nr < 0) || (nr >= ctxt->incNr))
1757 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001758 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001759 if (cur == NULL)
1760 return(-1);
1761
Owen Taylor3473f882001-02-23 17:55:21 +00001762 /*
1763 * read the attributes
1764 */
1765 href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF);
1766 if (href == NULL) {
1767 href = xmlGetProp(cur, XINCLUDE_HREF);
1768 if (href == NULL) {
1769 xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001770 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001771 return(-1);
1772 }
1773 }
1774 parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE);
1775 if (parse == NULL) {
1776 parse = xmlGetProp(cur, XINCLUDE_PARSE);
1777 }
1778 if (parse != NULL) {
1779 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1780 xml = 1;
1781 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1782 xml = 0;
1783 else {
1784 xmlGenericError(xmlGenericErrorContext,
1785 "XInclude: invalid value %s for %s\n",
1786 parse, XINCLUDE_PARSE);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001787 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001788 if (href != NULL)
1789 xmlFree(href);
1790 if (parse != NULL)
1791 xmlFree(parse);
1792 return(-1);
1793 }
1794 }
1795
1796 /*
1797 * compute the URI
1798 */
1799 base = xmlNodeGetBase(ctxt->doc, cur);
1800 if (base == NULL) {
1801 URI = xmlBuildURI(href, ctxt->doc->URL);
1802 } else {
1803 URI = xmlBuildURI(href, base);
1804 }
1805 if (URI == NULL) {
1806 xmlChar *escbase;
1807 xmlChar *eschref;
1808 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001809 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00001810 */
1811 escbase = xmlURIEscape(base);
1812 eschref = xmlURIEscape(href);
1813 URI = xmlBuildURI(eschref, escbase);
1814 if (escbase != NULL)
1815 xmlFree(escbase);
1816 if (eschref != NULL)
1817 xmlFree(eschref);
1818 }
1819 if (URI == NULL) {
1820 xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001821 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001822 if (parse != NULL)
1823 xmlFree(parse);
1824 if (href != NULL)
1825 xmlFree(href);
1826 if (base != NULL)
1827 xmlFree(base);
1828 return(-1);
1829 }
1830#ifdef DEBUG_XINCLUDE
1831 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
1832 xml ? "xml": "text");
1833 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
1834#endif
1835
1836 /*
1837 * Cleanup
1838 */
1839 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001840 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00001841 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
1842 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001843 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
1844 }
1845 if (ret < 0) {
1846 xmlNodePtr children;
1847
1848 /*
1849 * Time to try a fallback if availble
1850 */
1851#ifdef DEBUG_XINCLUDE
1852 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
1853#endif
1854 children = cur->children;
1855 while (children != NULL) {
1856 if ((children->type == XML_ELEMENT_NODE) &&
1857 (children->ns != NULL) &&
1858 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
1859 (xmlStrEqual(children->ns->href, XINCLUDE_NS))) {
1860 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
1861 if (ret == 0)
1862 break;
1863 }
1864 children = children->next;
1865 }
1866 }
1867 if (ret < 0) {
1868 xmlGenericError(xmlGenericErrorContext,
1869 "XInclude: could not load %s, and no fallback was found\n",
1870 URI);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001871 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001872 }
1873
1874 /*
1875 * Cleanup
1876 */
1877 if (URI != NULL)
1878 xmlFree(URI);
1879 if (parse != NULL)
1880 xmlFree(parse);
1881 if (href != NULL)
1882 xmlFree(href);
1883 if (base != NULL)
1884 xmlFree(base);
1885 return(0);
1886}
1887
1888/**
1889 * xmlXIncludeIncludeNode:
1890 * @ctxt: an XInclude context
1891 * @nr: the node number
1892 *
1893 * Inplement the infoset replacement for the given node
1894 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001895 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001896 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001897static int
Owen Taylor3473f882001-02-23 17:55:21 +00001898xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001899 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001900
1901 if (ctxt == NULL)
1902 return(-1);
1903 if ((nr < 0) || (nr >= ctxt->incNr))
1904 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001905 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001906 if (cur == NULL)
1907 return(-1);
1908
1909 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001910 * If we stored an XPointer a late computation may be needed
1911 */
1912 if ((ctxt->incTab[nr]->inc == NULL) &&
1913 (ctxt->incTab[nr]->xptr != NULL)) {
1914 ctxt->incTab[nr]->inc =
1915 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
1916 ctxt->incTab[nr]->xptr);
1917 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
1918 ctxt->incTab[nr]->xptr = NULL;
1919 }
1920 list = ctxt->incTab[nr]->inc;
1921 ctxt->incTab[nr]->inc = NULL;
1922
1923 /*
1924 * Check against the risk of generating a multi-rooted document
1925 */
1926 if ((cur->parent != NULL) &&
1927 (cur->parent->type != XML_ELEMENT_NODE)) {
1928 int nb_elem = 0;
1929
1930 tmp = list;
1931 while (tmp != NULL) {
1932 if (tmp->type == XML_ELEMENT_NODE)
1933 nb_elem++;
1934 tmp = tmp->next;
1935 }
1936 if (nb_elem > 1) {
1937 xmlGenericError(xmlGenericErrorContext,
1938 "XInclude error: would result in multiple root nodes\n");
1939 ctxt->nbErrors++;
1940 return(-1);
1941 }
1942 }
1943
1944 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001945 * Change the current node as an XInclude start one, and add an
1946 * entity end one
1947 */
1948 cur->type = XML_XINCLUDE_START;
1949 end = xmlNewNode(cur->ns, cur->name);
1950 if (end == NULL) {
1951 xmlGenericError(xmlGenericErrorContext,
1952 "XInclude: failed to build node\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001953 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001954 return(-1);
1955 }
1956 end->type = XML_XINCLUDE_END;
1957 xmlAddNextSibling(cur, end);
1958
1959 /*
1960 * Add the list of nodes
1961 */
Owen Taylor3473f882001-02-23 17:55:21 +00001962 while (list != NULL) {
1963 cur = list;
1964 list = list->next;
1965
1966 xmlAddPrevSibling(end, cur);
1967 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001968
1969
Owen Taylor3473f882001-02-23 17:55:21 +00001970 return(0);
1971}
1972
1973/**
1974 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001975 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00001976 * @node: an XInclude node
1977 *
1978 * test if the node is an XInclude node
1979 *
1980 * Returns 1 true, 0 otherwise
1981 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001982static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001983xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001984 if (node == NULL)
1985 return(0);
1986 if (node->ns == NULL)
1987 return(0);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001988 if (xmlStrEqual(node->ns->href, XINCLUDE_NS)) {
1989 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
1990 xmlNodePtr child = node->children;
1991 int nb_fallback = 0;
1992
1993 while (child != NULL) {
1994 if ((child->type == XML_ELEMENT_NODE) &&
1995 (child->ns != NULL) &&
1996 (xmlStrEqual(child->ns->href, XINCLUDE_NS))) {
1997 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
1998 xmlGenericError(xmlGenericErrorContext,
1999 "XInclude: %s has an %s child\n",
2000 XINCLUDE_NODE, XINCLUDE_NODE);
2001 ctxt->nbErrors++;
2002 return(0);
2003 }
2004 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2005 nb_fallback++;
2006 }
2007 }
2008 child = child->next;
2009 }
2010 if (nb_fallback > 1) {
2011 xmlGenericError(xmlGenericErrorContext,
2012 "XInclude: %s has %d %s children\n",
2013 XINCLUDE_NODE, nb_fallback, XINCLUDE_FALLBACK);
2014 ctxt->nbErrors++;
2015 return(0);
2016 }
2017 return(1);
2018 }
2019 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2020 if ((node->parent == NULL) ||
2021 (node->parent->type != XML_ELEMENT_NODE) ||
2022 (node->parent->ns == NULL) ||
2023 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) ||
2024 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
2025 xmlGenericError(xmlGenericErrorContext,
2026 "XInclude: %s is not the child of an %s\n",
2027 XINCLUDE_FALLBACK, XINCLUDE_NODE);
2028 ctxt->nbErrors++;
2029 }
2030 }
2031 }
Owen Taylor3473f882001-02-23 17:55:21 +00002032 return(0);
2033}
2034
2035/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002036 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002037 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002038 * @doc: an XML document
2039 *
2040 * Implement the XInclude substitution on the XML document @doc
2041 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002042 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002043 * or the number of substitutions done.
2044 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002045static int
2046xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00002047 xmlNodePtr cur;
2048 int ret = 0;
2049 int i;
2050
2051 if (doc == NULL)
2052 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002053 if (ctxt == NULL)
2054 return(-1);
2055
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002056 if (doc->URL != NULL) {
2057 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2058 if (ret < 0)
2059 return(-1);
2060 }
2061
Owen Taylor3473f882001-02-23 17:55:21 +00002062 /*
2063 * First phase: lookup the elements in the document
2064 */
2065 cur = xmlDocGetRootElement(doc);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002066 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002067 xmlXIncludePreProcessNode(ctxt, cur);
2068 while (cur != NULL) {
2069 /* TODO: need to work on entities -> stack */
2070 if ((cur->children != NULL) &&
2071 (cur->children->type != XML_ENTITY_DECL)) {
2072 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002073 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002074 xmlXIncludePreProcessNode(ctxt, cur);
2075 } else if (cur->next != NULL) {
2076 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002077 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002078 xmlXIncludePreProcessNode(ctxt, cur);
2079 } else {
2080 do {
2081 cur = cur->parent;
2082 if (cur == NULL) break; /* do */
2083 if (cur->next != NULL) {
2084 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002085 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002086 xmlXIncludePreProcessNode(ctxt, cur);
2087 break; /* do */
2088 }
2089 } while (cur != NULL);
2090 }
2091 }
2092
2093 /*
2094 * Second Phase : collect the infosets fragments
2095 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002096 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002097 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002098 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002099 }
2100
2101 /*
2102 * Third phase: extend the original document infoset.
2103 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002104 if (ctxt->nbErrors == 0) {
2105 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
2106 xmlXIncludeIncludeNode(ctxt, i);
2107 }
Owen Taylor3473f882001-02-23 17:55:21 +00002108 }
2109
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002110 if (doc->URL != NULL)
2111 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002112 return(ret);
2113}
2114
2115/**
2116 * xmlXIncludeProcess:
2117 * @doc: an XML document
2118 *
2119 * Implement the XInclude substitution on the XML document @doc
2120 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002121 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002122 * or the number of substitutions done.
2123 */
2124int
2125xmlXIncludeProcess(xmlDocPtr doc) {
2126 xmlXIncludeCtxtPtr ctxt;
2127 int ret = 0;
2128
2129 if (doc == NULL)
2130 return(-1);
2131 ctxt = xmlXIncludeNewContext(doc);
2132 if (ctxt == NULL)
2133 return(-1);
2134 ret = xmlXIncludeDoProcess(ctxt, doc);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00002135 if ((ret >= 0) && (ctxt->nbErrors > 0))
2136 ret = -1;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002137
Owen Taylor3473f882001-02-23 17:55:21 +00002138 xmlXIncludeFreeContext(ctxt);
2139 return(ret);
2140}
2141
2142#else /* !LIBXML_XINCLUDE_ENABLED */
2143#endif