blob: 51812149a3635093725c3279979361472b4b78ce [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;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001067 } else if ((ent->ExternalID != NULL) &&
1068 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001069 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1070 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001071 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1072 if (!xmlStrEqual(ent->content, prev->content))
1073 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001074 } else {
1075 goto error;
1076 }
1077
1078 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001079 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001080 return;
1081error:
1082 xmlGenericError(xmlGenericErrorContext,
1083 "XInclude: mismatch in redefinition of entity %s\n", ent->name);
1084 ctxt->nbErrors++;
Daniel Veillard4287c572003-02-04 22:48:53 +00001085}
1086
1087/**
1088 * xmlXIncludeMergeEntities:
1089 * @ctxt: an XInclude context
1090 * @doc: the including doc
1091 * @from: the included doc
1092 *
1093 * Inplements the entity merge
1094 *
1095 * Returns 0 if merge succeeded, -1 if some processing failed
1096 */
1097static int
1098xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1099 xmlDocPtr from) {
1100 xmlNodePtr cur;
1101 xmlDtdPtr target, source;
1102
1103 if (ctxt == NULL)
1104 return(-1);
1105
1106 if ((from == NULL) || (from->intSubset == NULL))
1107 return(0);
1108
1109 target = doc->intSubset;
1110 if (target == NULL) {
1111 cur = xmlDocGetRootElement(doc);
1112 if (cur == NULL)
1113 return(-1);
1114 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1115 if (target == NULL)
1116 return(-1);
1117 }
1118
1119 source = from->intSubset;
1120 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001121 xmlXIncludeMergeData data;
1122
1123 data.ctxt = ctxt;
1124 data.doc = doc;
1125
Daniel Veillard4287c572003-02-04 22:48:53 +00001126 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001127 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001128 }
1129 source = from->extSubset;
1130 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001131 xmlXIncludeMergeData data;
1132
1133 data.ctxt = ctxt;
1134 data.doc = doc;
1135
Daniel Veillard4287c572003-02-04 22:48:53 +00001136 /*
1137 * don't duplicate existing stuff when external subsets are the same
1138 */
1139 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1140 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1141 xmlHashScan((xmlHashTablePtr) source->entities,
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001142 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001143 }
1144 }
1145 return(0);
1146}
1147
1148/**
Owen Taylor3473f882001-02-23 17:55:21 +00001149 * xmlXIncludeLoadDoc:
1150 * @ctxt: the XInclude context
1151 * @url: the associated URL
1152 * @nr: the xinclude node number
1153 *
1154 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001155 *
1156 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001157 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001158static int
Owen Taylor3473f882001-02-23 17:55:21 +00001159xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1160 xmlDocPtr doc;
1161 xmlURIPtr uri;
1162 xmlChar *URL;
1163 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001164 int i = 0;
1165
1166#ifdef DEBUG_XINCLUDE
1167 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1168#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001169 /*
1170 * Check the URL and remove any fragment identifier
1171 */
1172 uri = xmlParseURI((const char *)url);
1173 if (uri == NULL) {
1174 xmlGenericError(xmlGenericErrorContext,
1175 "XInclude: invalid value URI %s\n", url);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001176 ctxt->nbErrors++;
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001177 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001178 }
1179 if (uri->fragment != NULL) {
1180 fragment = (xmlChar *) uri->fragment;
1181 uri->fragment = NULL;
1182 }
1183 URL = xmlSaveUri(uri);
1184 xmlFreeURI(uri);
1185 if (URL == NULL) {
1186 xmlGenericError(xmlGenericErrorContext,
1187 "XInclude: invalid value URI %s\n", url);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001188 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001189 if (fragment != NULL)
1190 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001191 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001192 }
1193
1194 /*
1195 * Handling of references to the local document are done
1196 * directly through ctxt->doc.
1197 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001198 if ((URL[0] == 0) || (URL[0] == '#') ||
1199 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001200 doc = NULL;
1201 goto loaded;
1202 }
1203
1204 /*
1205 * Prevent reloading twice the document.
1206 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001207 for (i = 0; i < ctxt->incNr; i++) {
1208 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1209 (ctxt->incTab[i]->doc != NULL)) {
1210 doc = ctxt->incTab[i]->doc;
1211#ifdef DEBUG_XINCLUDE
1212 printf("Already loaded %s\n", URL);
1213#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001214 goto loaded;
1215 }
1216 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001217
Owen Taylor3473f882001-02-23 17:55:21 +00001218 /*
1219 * Load it.
1220 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001221#ifdef DEBUG_XINCLUDE
1222 printf("loading %s\n", URL);
1223#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001224 doc = xmlParseFile((const char *)URL);
1225 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001226 xmlFree(URL);
1227 if (fragment != NULL)
1228 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001229 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001230 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001231 ctxt->incTab[nr]->doc = doc;
1232
1233 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001234 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001235 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001236 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001237
1238 /*
1239 * We don't need the DTD anymore, free up space
1240 if (doc->intSubset != NULL) {
1241 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1242 xmlFreeNode((xmlNodePtr) doc->intSubset);
1243 doc->intSubset = NULL;
1244 }
1245 if (doc->extSubset != NULL) {
1246 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1247 xmlFreeNode((xmlNodePtr) doc->extSubset);
1248 doc->extSubset = NULL;
1249 }
1250 */
1251 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001252
1253loaded:
1254 if (fragment == NULL) {
1255 /*
1256 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001257 */
1258 if (doc == NULL)
Daniel Veillard4497e692001-06-09 14:19:02 +00001259 {
1260 /* Hopefully a DTD declaration won't be copied from
1261 * the same document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001262 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001263 } else {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001264 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001265 doc, doc->children);
Daniel Veillard4497e692001-06-09 14:19:02 +00001266 }
Owen Taylor3473f882001-02-23 17:55:21 +00001267 } else {
1268 /*
1269 * Computes the XPointer expression and make a copy used
1270 * as the replacement copy.
1271 */
1272 xmlXPathObjectPtr xptr;
1273 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001274 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001275
1276 if (doc == NULL) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001277 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1278 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001279 } else {
1280 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1281 }
1282 if (xptrctxt == NULL) {
1283 xmlGenericError(xmlGenericErrorContext,
1284 "XInclude: could create XPointer context\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001285 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001286 xmlFree(URL);
1287 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001288 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001289 }
1290 xptr = xmlXPtrEval(fragment, xptrctxt);
1291 if (xptr == NULL) {
1292 xmlGenericError(xmlGenericErrorContext,
1293 "XInclude: XPointer evaluation failed: #%s\n",
1294 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001295 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001296 xmlXPathFreeContext(xptrctxt);
1297 xmlFree(URL);
1298 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001299 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001300 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001301 switch (xptr->type) {
1302 case XPATH_UNDEFINED:
1303 case XPATH_BOOLEAN:
1304 case XPATH_NUMBER:
1305 case XPATH_STRING:
1306 case XPATH_POINT:
1307 case XPATH_USERS:
1308 case XPATH_XSLT_TREE:
1309 xmlGenericError(xmlGenericErrorContext,
1310 "XInclude: XPointer is not a range: #%s\n",
1311 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001312 ctxt->nbErrors++;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001313 xmlXPathFreeContext(xptrctxt);
1314 xmlFree(URL);
1315 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001316 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001317 case XPATH_NODESET:
1318 case XPATH_RANGE:
1319 case XPATH_LOCATIONSET:
1320 break;
1321 }
1322 set = xptr->nodesetval;
1323 if (set != NULL) {
1324 for (i = 0;i < set->nodeNr;i++) {
1325 if (set->nodeTab[i] == NULL)
1326 continue;
1327 switch (set->nodeTab[i]->type) {
1328 case XML_TEXT_NODE:
1329 case XML_CDATA_SECTION_NODE:
1330 case XML_ELEMENT_NODE:
1331 case XML_ENTITY_REF_NODE:
1332 case XML_ENTITY_NODE:
1333 case XML_PI_NODE:
1334 case XML_COMMENT_NODE:
1335 case XML_DOCUMENT_NODE:
1336 case XML_HTML_DOCUMENT_NODE:
1337#ifdef LIBXML_DOCB_ENABLED
1338 case XML_DOCB_DOCUMENT_NODE:
1339#endif
1340 continue;
1341 case XML_ATTRIBUTE_NODE:
1342 xmlGenericError(xmlGenericErrorContext,
1343 "XInclude: XPointer selects an attribute: #%s\n",
1344 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001345 ctxt->nbErrors++;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001346 set->nodeTab[i] = NULL;
1347 continue;
1348 case XML_NAMESPACE_DECL:
1349 xmlGenericError(xmlGenericErrorContext,
1350 "XInclude: XPointer selects a namespace: #%s\n",
1351 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001352 ctxt->nbErrors++;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001353 set->nodeTab[i] = NULL;
1354 continue;
1355 case XML_DOCUMENT_TYPE_NODE:
1356 case XML_DOCUMENT_FRAG_NODE:
1357 case XML_NOTATION_NODE:
1358 case XML_DTD_NODE:
1359 case XML_ELEMENT_DECL:
1360 case XML_ATTRIBUTE_DECL:
1361 case XML_ENTITY_DECL:
1362 case XML_XINCLUDE_START:
1363 case XML_XINCLUDE_END:
1364 xmlGenericError(xmlGenericErrorContext,
1365 "XInclude: XPointer selects unexpected nodes: #%s\n",
1366 fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001367 ctxt->nbErrors++;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001368 set->nodeTab[i] = NULL;
1369 set->nodeTab[i] = NULL;
1370 continue; /* for */
1371 }
1372 }
1373 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001374 if (doc == NULL) {
1375 ctxt->incTab[nr]->xptr = xptr;
1376 ctxt->incTab[nr]->inc = NULL;
1377 } else {
1378 ctxt->incTab[nr]->inc =
1379 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1380 xmlXPathFreeObject(xptr);
1381 }
Owen Taylor3473f882001-02-23 17:55:21 +00001382 xmlXPathFreeContext(xptrctxt);
1383 xmlFree(fragment);
1384 }
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001385
1386 /*
1387 * Do the xml:base fixup if needed
1388 */
1389 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1390 xmlNodePtr node;
1391
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001392 node = ctxt->incTab[nr]->inc;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001393 while (node != NULL) {
1394 if (node->type == XML_ELEMENT_NODE)
1395 xmlNodeSetBase(node, URL);
1396 node = node->next;
1397 }
1398 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001399 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1400 (ctxt->incTab[nr]->count <= 1)) {
1401#ifdef DEBUG_XINCLUDE
1402 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1403#endif
1404 xmlFreeDoc(ctxt->incTab[nr]->doc);
1405 ctxt->incTab[nr]->doc = NULL;
1406 }
Owen Taylor3473f882001-02-23 17:55:21 +00001407 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001408 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001409}
1410
1411/**
1412 * xmlXIncludeLoadTxt:
1413 * @ctxt: the XInclude context
1414 * @url: the associated URL
1415 * @nr: the xinclude node number
1416 *
1417 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001418 *
1419 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001420 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001421static int
Owen Taylor3473f882001-02-23 17:55:21 +00001422xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1423 xmlParserInputBufferPtr buf;
1424 xmlNodePtr node;
1425 xmlURIPtr uri;
1426 xmlChar *URL;
1427 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001428 xmlChar *encoding = NULL;
1429 xmlCharEncoding enc = 0;
1430
Owen Taylor3473f882001-02-23 17:55:21 +00001431 /*
1432 * Check the URL and remove any fragment identifier
1433 */
1434 uri = xmlParseURI((const char *)url);
1435 if (uri == NULL) {
1436 xmlGenericError(xmlGenericErrorContext,
1437 "XInclude: invalid value URI %s\n", url);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001438 ctxt->nbErrors++;
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001439 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001440 }
1441 if (uri->fragment != NULL) {
1442 xmlGenericError(xmlGenericErrorContext,
1443 "XInclude: fragment identifier forbidden for text: %s\n",
1444 uri->fragment);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001445 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001446 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001447 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001448 }
1449 URL = xmlSaveUri(uri);
1450 xmlFreeURI(uri);
1451 if (URL == NULL) {
1452 xmlGenericError(xmlGenericErrorContext,
1453 "XInclude: invalid value URI %s\n", url);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001454 ctxt->nbErrors++;
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001455 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001456 }
1457
1458 /*
1459 * Handling of references to the local document are done
1460 * directly through ctxt->doc.
1461 */
1462 if (URL[0] == 0) {
1463 xmlGenericError(xmlGenericErrorContext,
1464 "XInclude: text serialization of document not available\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001465 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001466 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001467 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001468 }
1469
1470 /*
1471 * Prevent reloading twice the document.
1472 */
1473 for (i = 0; i < ctxt->txtNr; i++) {
1474 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1475 node = xmlCopyNode(ctxt->txtTab[i], 1);
1476 goto loaded;
1477 }
1478 }
1479 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001480 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001481 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001482 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1483 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1484 }
1485 if (encoding != NULL) {
1486 /*
1487 * TODO: we should not have to remap to the xmlCharEncoding
1488 * predefined set, a better interface than
1489 * xmlParserInputBufferCreateFilename should allow any
1490 * encoding supported by iconv
1491 */
1492 enc = xmlParseCharEncoding((const char *) encoding);
1493 if (enc == XML_CHAR_ENCODING_ERROR) {
1494 xmlGenericError(xmlGenericErrorContext,
1495 "XInclude: encoding %s not supported\n", encoding);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001496 ctxt->nbErrors++;
Daniel Veillardd076a202002-11-20 13:28:31 +00001497 xmlFree(encoding);
1498 xmlFree(URL);
1499 return(-1);
1500 }
1501 xmlFree(encoding);
1502 }
1503
1504 /*
1505 * Load it.
1506 */
1507 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001508 if (buf == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001509 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001510 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001511 }
1512 node = xmlNewText(NULL);
1513
1514 /*
1515 * Scan all chars from the resource and add the to the node
1516 */
1517 while (xmlParserInputBufferRead(buf, 128) > 0) {
1518 int len;
1519 const xmlChar *content;
1520
1521 content = xmlBufferContent(buf->buffer);
1522 len = xmlBufferLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001523 for (i = 0;i < len;) {
1524 int cur;
1525 int l;
1526
1527 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1528 if (!IS_CHAR(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001529 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd076a202002-11-20 13:28:31 +00001530 "XInclude: %s contains invalid char %d\n", URL, cur);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001531 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001532 } else {
Daniel Veillardd076a202002-11-20 13:28:31 +00001533 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001534 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001535 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001536 }
1537 xmlBufferShrink(buf->buffer, len);
1538 }
1539 xmlFreeParserInputBuffer(buf);
1540 xmlXIncludeAddTxt(ctxt, node, URL);
1541
1542loaded:
1543 /*
1544 * Add the element as the replacement copy.
1545 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001546 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001547 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001548 return(0);
1549}
1550
1551/**
1552 * xmlXIncludeLoadFallback:
1553 * @ctxt: the XInclude context
1554 * @fallback: the fallback node
1555 * @nr: the xinclude node number
1556 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001557 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001558 * in the XInclude context
1559 *
1560 * Returns 0 in case of success, -1 in case of failure
1561 */
1562static int
1563xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
1564 if ((fallback == NULL) || (ctxt == NULL))
1565 return(-1);
1566
Daniel Veillard06503452002-12-13 10:42:08 +00001567 ctxt->incTab[nr]->inc = xmlCopyNodeList(fallback->children);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001568 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001569}
1570
1571/************************************************************************
1572 * *
1573 * XInclude Processing *
1574 * *
1575 ************************************************************************/
1576
1577/**
1578 * xmlXIncludePreProcessNode:
1579 * @ctxt: an XInclude context
1580 * @node: an XInclude node
1581 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00001582 * Implement the XInclude preprocessing, currently just adding the element
1583 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00001584 *
1585 * Returns the result list or NULL in case of error
1586 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001587static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001588xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1589 xmlXIncludeAddNode(ctxt, node);
1590 return(0);
1591}
1592
Daniel Veillard118aed72002-09-24 14:13:13 +00001593#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00001594/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001595 * xmlXIncludePreloadNode:
1596 * @ctxt: an XInclude context
1597 * @nr: the node number
1598 *
1599 * Do some precomputations and preload shared documents
1600 *
1601 * Returns 0 if substitution succeeded, -1 if some processing failed
1602 */
1603static int
1604xmlXIncludePreloadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1605 xmlNodePtr cur;
1606 xmlChar *href;
1607 xmlChar *parse;
1608 xmlChar *base;
1609 xmlChar *URI;
1610 int xml = 1; /* default Issue 64 */
1611 xmlURIPtr uri;
1612 xmlChar *URL;
1613 xmlChar *fragment = NULL;
1614 int i;
1615
1616
1617 if (ctxt == NULL)
1618 return(-1);
1619 if ((nr < 0) || (nr >= ctxt->incNr))
1620 return(-1);
1621 cur = ctxt->incTab[nr]->ref;
1622 if (cur == NULL)
1623 return(-1);
1624
1625 /*
1626 * read the attributes
1627 */
1628 href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF);
1629 if (href == NULL) {
1630 href = xmlGetProp(cur, XINCLUDE_HREF);
1631 if (href == NULL) {
1632 xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n");
1633 return(-1);
1634 }
1635 }
1636 parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE);
1637 if (parse == NULL) {
1638 parse = xmlGetProp(cur, XINCLUDE_PARSE);
1639 }
1640 if (parse != NULL) {
1641 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1642 xml = 1;
1643 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1644 xml = 0;
1645 else {
1646 xmlGenericError(xmlGenericErrorContext,
1647 "XInclude: invalid value %s for %s\n",
1648 parse, XINCLUDE_PARSE);
1649 if (href != NULL)
1650 xmlFree(href);
1651 if (parse != NULL)
1652 xmlFree(parse);
1653 return(-1);
1654 }
1655 }
1656
1657 /*
1658 * compute the URI
1659 */
1660 base = xmlNodeGetBase(ctxt->doc, cur);
1661 if (base == NULL) {
1662 URI = xmlBuildURI(href, ctxt->doc->URL);
1663 } else {
1664 URI = xmlBuildURI(href, base);
1665 }
1666 if (URI == NULL) {
1667 xmlChar *escbase;
1668 xmlChar *eschref;
1669 /*
1670 * Some escaping may be needed
1671 */
1672 escbase = xmlURIEscape(base);
1673 eschref = xmlURIEscape(href);
1674 URI = xmlBuildURI(eschref, escbase);
1675 if (escbase != NULL)
1676 xmlFree(escbase);
1677 if (eschref != NULL)
1678 xmlFree(eschref);
1679 }
1680 if (parse != NULL)
1681 xmlFree(parse);
1682 if (href != NULL)
1683 xmlFree(href);
1684 if (base != NULL)
1685 xmlFree(base);
1686 if (URI == NULL) {
1687 xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001688 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001689 return(-1);
1690 }
1691
1692 /*
1693 * Check the URL and remove any fragment identifier
1694 */
1695 uri = xmlParseURI((const char *)URI);
1696 if (uri == NULL) {
1697 xmlGenericError(xmlGenericErrorContext,
1698 "XInclude: invalid value URI %s\n", URI);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001699 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001700 xmlFree(URI);
1701 return(-1);
1702 }
1703 if (uri->fragment != NULL) {
1704 fragment = (xmlChar *) uri->fragment;
1705 uri->fragment = NULL;
1706 }
1707 URL = xmlSaveUri(uri);
1708 xmlFreeURI(uri);
1709 if (URL == NULL) {
1710 xmlGenericError(xmlGenericErrorContext,
1711 "XInclude: invalid value URI %s\n", URI);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001712 ctxt->nbErrors++;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001713 if (fragment != NULL)
1714 xmlFree(fragment);
1715 xmlFree(URI);
1716 return(-1);
1717 }
1718 xmlFree(URI);
1719 if (fragment != NULL)
1720 xmlFree(fragment);
1721
1722 for (i = 0; i < nr; i++) {
1723 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1724#ifdef DEBUG_XINCLUDE
1725 printf("Incrementing count for %d : %s\n", i, ctxt->incTab[i]->URI);
1726#endif
1727 ctxt->incTab[i]->count++;
1728 break;
1729 }
1730 }
1731 xmlFree(URL);
1732 return(0);
1733}
Daniel Veillard118aed72002-09-24 14:13:13 +00001734#endif
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001735
1736/**
Owen Taylor3473f882001-02-23 17:55:21 +00001737 * xmlXIncludeLoadNode:
1738 * @ctxt: an XInclude context
1739 * @nr: the node number
1740 *
1741 * Find and load the infoset replacement for the given node.
1742 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001743 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001744 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001745static int
Owen Taylor3473f882001-02-23 17:55:21 +00001746xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1747 xmlNodePtr cur;
1748 xmlChar *href;
1749 xmlChar *parse;
1750 xmlChar *base;
1751 xmlChar *URI;
1752 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001753 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001754
1755 if (ctxt == NULL)
1756 return(-1);
1757 if ((nr < 0) || (nr >= ctxt->incNr))
1758 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001759 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001760 if (cur == NULL)
1761 return(-1);
1762
Owen Taylor3473f882001-02-23 17:55:21 +00001763 /*
1764 * read the attributes
1765 */
1766 href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF);
1767 if (href == NULL) {
1768 href = xmlGetProp(cur, XINCLUDE_HREF);
1769 if (href == NULL) {
1770 xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001771 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001772 return(-1);
1773 }
1774 }
1775 parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE);
1776 if (parse == NULL) {
1777 parse = xmlGetProp(cur, XINCLUDE_PARSE);
1778 }
1779 if (parse != NULL) {
1780 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1781 xml = 1;
1782 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1783 xml = 0;
1784 else {
1785 xmlGenericError(xmlGenericErrorContext,
1786 "XInclude: invalid value %s for %s\n",
1787 parse, XINCLUDE_PARSE);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001788 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001789 if (href != NULL)
1790 xmlFree(href);
1791 if (parse != NULL)
1792 xmlFree(parse);
1793 return(-1);
1794 }
1795 }
1796
1797 /*
1798 * compute the URI
1799 */
1800 base = xmlNodeGetBase(ctxt->doc, cur);
1801 if (base == NULL) {
1802 URI = xmlBuildURI(href, ctxt->doc->URL);
1803 } else {
1804 URI = xmlBuildURI(href, base);
1805 }
1806 if (URI == NULL) {
1807 xmlChar *escbase;
1808 xmlChar *eschref;
1809 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001810 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00001811 */
1812 escbase = xmlURIEscape(base);
1813 eschref = xmlURIEscape(href);
1814 URI = xmlBuildURI(eschref, escbase);
1815 if (escbase != NULL)
1816 xmlFree(escbase);
1817 if (eschref != NULL)
1818 xmlFree(eschref);
1819 }
1820 if (URI == NULL) {
1821 xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001822 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001823 if (parse != NULL)
1824 xmlFree(parse);
1825 if (href != NULL)
1826 xmlFree(href);
1827 if (base != NULL)
1828 xmlFree(base);
1829 return(-1);
1830 }
1831#ifdef DEBUG_XINCLUDE
1832 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
1833 xml ? "xml": "text");
1834 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
1835#endif
1836
1837 /*
1838 * Cleanup
1839 */
1840 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001841 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00001842 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
1843 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001844 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
1845 }
1846 if (ret < 0) {
1847 xmlNodePtr children;
1848
1849 /*
1850 * Time to try a fallback if availble
1851 */
1852#ifdef DEBUG_XINCLUDE
1853 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
1854#endif
1855 children = cur->children;
1856 while (children != NULL) {
1857 if ((children->type == XML_ELEMENT_NODE) &&
1858 (children->ns != NULL) &&
1859 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
1860 (xmlStrEqual(children->ns->href, XINCLUDE_NS))) {
1861 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
1862 if (ret == 0)
1863 break;
1864 }
1865 children = children->next;
1866 }
1867 }
1868 if (ret < 0) {
1869 xmlGenericError(xmlGenericErrorContext,
1870 "XInclude: could not load %s, and no fallback was found\n",
1871 URI);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001872 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001873 }
1874
1875 /*
1876 * Cleanup
1877 */
1878 if (URI != NULL)
1879 xmlFree(URI);
1880 if (parse != NULL)
1881 xmlFree(parse);
1882 if (href != NULL)
1883 xmlFree(href);
1884 if (base != NULL)
1885 xmlFree(base);
1886 return(0);
1887}
1888
1889/**
1890 * xmlXIncludeIncludeNode:
1891 * @ctxt: an XInclude context
1892 * @nr: the node number
1893 *
1894 * Inplement the infoset replacement for the given node
1895 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001896 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00001897 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001898static int
Owen Taylor3473f882001-02-23 17:55:21 +00001899xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001900 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001901
1902 if (ctxt == NULL)
1903 return(-1);
1904 if ((nr < 0) || (nr >= ctxt->incNr))
1905 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001906 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00001907 if (cur == NULL)
1908 return(-1);
1909
1910 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001911 * If we stored an XPointer a late computation may be needed
1912 */
1913 if ((ctxt->incTab[nr]->inc == NULL) &&
1914 (ctxt->incTab[nr]->xptr != NULL)) {
1915 ctxt->incTab[nr]->inc =
1916 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
1917 ctxt->incTab[nr]->xptr);
1918 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
1919 ctxt->incTab[nr]->xptr = NULL;
1920 }
1921 list = ctxt->incTab[nr]->inc;
1922 ctxt->incTab[nr]->inc = NULL;
1923
1924 /*
1925 * Check against the risk of generating a multi-rooted document
1926 */
1927 if ((cur->parent != NULL) &&
1928 (cur->parent->type != XML_ELEMENT_NODE)) {
1929 int nb_elem = 0;
1930
1931 tmp = list;
1932 while (tmp != NULL) {
1933 if (tmp->type == XML_ELEMENT_NODE)
1934 nb_elem++;
1935 tmp = tmp->next;
1936 }
1937 if (nb_elem > 1) {
1938 xmlGenericError(xmlGenericErrorContext,
1939 "XInclude error: would result in multiple root nodes\n");
1940 ctxt->nbErrors++;
1941 return(-1);
1942 }
1943 }
1944
1945 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001946 * Change the current node as an XInclude start one, and add an
1947 * entity end one
1948 */
1949 cur->type = XML_XINCLUDE_START;
1950 end = xmlNewNode(cur->ns, cur->name);
1951 if (end == NULL) {
1952 xmlGenericError(xmlGenericErrorContext,
1953 "XInclude: failed to build node\n");
Daniel Veillardd581b7e2003-02-11 18:03:05 +00001954 ctxt->nbErrors++;
Owen Taylor3473f882001-02-23 17:55:21 +00001955 return(-1);
1956 }
1957 end->type = XML_XINCLUDE_END;
1958 xmlAddNextSibling(cur, end);
1959
1960 /*
1961 * Add the list of nodes
1962 */
Owen Taylor3473f882001-02-23 17:55:21 +00001963 while (list != NULL) {
1964 cur = list;
1965 list = list->next;
1966
1967 xmlAddPrevSibling(end, cur);
1968 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001969
1970
Owen Taylor3473f882001-02-23 17:55:21 +00001971 return(0);
1972}
1973
1974/**
1975 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001976 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00001977 * @node: an XInclude node
1978 *
1979 * test if the node is an XInclude node
1980 *
1981 * Returns 1 true, 0 otherwise
1982 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001983static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001984xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001985 if (node == NULL)
1986 return(0);
1987 if (node->ns == NULL)
1988 return(0);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001989 if (xmlStrEqual(node->ns->href, XINCLUDE_NS)) {
1990 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
1991 xmlNodePtr child = node->children;
1992 int nb_fallback = 0;
1993
1994 while (child != NULL) {
1995 if ((child->type == XML_ELEMENT_NODE) &&
1996 (child->ns != NULL) &&
1997 (xmlStrEqual(child->ns->href, XINCLUDE_NS))) {
1998 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
1999 xmlGenericError(xmlGenericErrorContext,
2000 "XInclude: %s has an %s child\n",
2001 XINCLUDE_NODE, XINCLUDE_NODE);
2002 ctxt->nbErrors++;
2003 return(0);
2004 }
2005 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2006 nb_fallback++;
2007 }
2008 }
2009 child = child->next;
2010 }
2011 if (nb_fallback > 1) {
2012 xmlGenericError(xmlGenericErrorContext,
2013 "XInclude: %s has %d %s children\n",
2014 XINCLUDE_NODE, nb_fallback, XINCLUDE_FALLBACK);
2015 ctxt->nbErrors++;
2016 return(0);
2017 }
2018 return(1);
2019 }
2020 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2021 if ((node->parent == NULL) ||
2022 (node->parent->type != XML_ELEMENT_NODE) ||
2023 (node->parent->ns == NULL) ||
2024 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) ||
2025 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
2026 xmlGenericError(xmlGenericErrorContext,
2027 "XInclude: %s is not the child of an %s\n",
2028 XINCLUDE_FALLBACK, XINCLUDE_NODE);
2029 ctxt->nbErrors++;
2030 }
2031 }
2032 }
Owen Taylor3473f882001-02-23 17:55:21 +00002033 return(0);
2034}
2035
2036/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002037 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002038 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002039 * @doc: an XML document
2040 *
2041 * Implement the XInclude substitution on the XML document @doc
2042 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002043 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002044 * or the number of substitutions done.
2045 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002046static int
2047xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00002048 xmlNodePtr cur;
2049 int ret = 0;
2050 int i;
2051
2052 if (doc == NULL)
2053 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002054 if (ctxt == NULL)
2055 return(-1);
2056
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002057 if (doc->URL != NULL) {
2058 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2059 if (ret < 0)
2060 return(-1);
2061 }
2062
Owen Taylor3473f882001-02-23 17:55:21 +00002063 /*
2064 * First phase: lookup the elements in the document
2065 */
2066 cur = xmlDocGetRootElement(doc);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002067 if (xmlXIncludeTestNode(ctxt, cur) == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00002068 xmlXIncludePreProcessNode(ctxt, cur);
2069 while (cur != NULL) {
2070 /* TODO: need to work on entities -> stack */
2071 if ((cur->children != NULL) &&
2072 (cur->children->type != XML_ENTITY_DECL)) {
2073 cur = cur->children;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002074 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002075 xmlXIncludePreProcessNode(ctxt, cur);
2076 } else if (cur->next != NULL) {
2077 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002078 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002079 xmlXIncludePreProcessNode(ctxt, cur);
2080 } else {
2081 do {
2082 cur = cur->parent;
2083 if (cur == NULL) break; /* do */
2084 if (cur->next != NULL) {
2085 cur = cur->next;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002086 if (xmlXIncludeTestNode(ctxt, cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002087 xmlXIncludePreProcessNode(ctxt, cur);
2088 break; /* do */
2089 }
2090 } while (cur != NULL);
2091 }
2092 }
2093
2094 /*
2095 * Second Phase : collect the infosets fragments
2096 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002097 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002098 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002099 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002100 }
2101
2102 /*
2103 * Third phase: extend the original document infoset.
2104 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002105 if (ctxt->nbErrors == 0) {
2106 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
2107 xmlXIncludeIncludeNode(ctxt, i);
2108 }
Owen Taylor3473f882001-02-23 17:55:21 +00002109 }
2110
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002111 if (doc->URL != NULL)
2112 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002113 return(ret);
2114}
2115
2116/**
2117 * xmlXIncludeProcess:
2118 * @doc: an XML document
2119 *
2120 * Implement the XInclude substitution on the XML document @doc
2121 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002122 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002123 * or the number of substitutions done.
2124 */
2125int
2126xmlXIncludeProcess(xmlDocPtr doc) {
2127 xmlXIncludeCtxtPtr ctxt;
2128 int ret = 0;
2129
2130 if (doc == NULL)
2131 return(-1);
2132 ctxt = xmlXIncludeNewContext(doc);
2133 if (ctxt == NULL)
2134 return(-1);
2135 ret = xmlXIncludeDoProcess(ctxt, doc);
Daniel Veillardd581b7e2003-02-11 18:03:05 +00002136 if ((ret >= 0) && (ctxt->nbErrors > 0))
2137 ret = -1;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002138
Owen Taylor3473f882001-02-23 17:55:21 +00002139 xmlXIncludeFreeContext(ctxt);
2140 return(ret);
2141}
2142
2143#else /* !LIBXML_XINCLUDE_ENABLED */
2144#endif