Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xinclude.c : Code to implement XInclude processing |
| 3 | * |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 4 | * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003 |
| 5 | * http://www.w3.org/TR/2003/WD-xinclude-20031110 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6 | * |
| 7 | * See Copyright for the status of this software. |
| 8 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 9 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 12 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 13 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15 | #include <string.h> |
| 16 | #include <libxml/xmlmemory.h> |
| 17 | #include <libxml/tree.h> |
| 18 | #include <libxml/parser.h> |
| 19 | #include <libxml/uri.h> |
| 20 | #include <libxml/xpointer.h> |
| 21 | #include <libxml/parserInternals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 22 | #include <libxml/xmlerror.h> |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 23 | #include <libxml/encoding.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 24 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 25 | |
| 26 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 27 | #include <libxml/xinclude.h> |
| 28 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 29 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 30 | #define XINCLUDE_MAX_DEPTH 40 |
| 31 | |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 32 | /* #define DEBUG_XINCLUDE */ |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 33 | #ifdef DEBUG_XINCLUDE |
| 34 | #ifdef LIBXML_DEBUG_ENABLED |
| 35 | #include <libxml/debugXML.h> |
| 36 | #endif |
| 37 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 38 | |
| 39 | /************************************************************************ |
| 40 | * * |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 41 | * XInclude context handling * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 42 | * * |
| 43 | ************************************************************************/ |
| 44 | |
| 45 | /* |
| 46 | * An XInclude context |
| 47 | */ |
Daniel Veillard | edac3c9 | 2001-02-26 01:36:19 +0000 | [diff] [blame] | 48 | typedef xmlChar *xmlURL; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 49 | |
| 50 | typedef struct _xmlXIncludeRef xmlXIncludeRef; |
| 51 | typedef xmlXIncludeRef *xmlXIncludeRefPtr; |
| 52 | struct _xmlXIncludeRef { |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 53 | xmlChar *URI; /* the fully resolved resource URL */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 54 | xmlChar *fragment; /* the fragment in the URI */ |
| 55 | xmlDocPtr doc; /* the parsed document */ |
| 56 | xmlNodePtr ref; /* the node making the reference in the source */ |
| 57 | xmlNodePtr inc; /* the included copy */ |
| 58 | int xml; /* xml or txt */ |
| 59 | int count; /* how many refs use that specific doc */ |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 60 | xmlXPathObjectPtr xptr; /* the xpointer if needed */ |
William M. Brack | 95af594 | 2004-02-08 04:12:49 +0000 | [diff] [blame] | 61 | int emptyFb; /* flag to show fallback empty */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 64 | struct _xmlXIncludeCtxt { |
| 65 | xmlDocPtr doc; /* the source document */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 66 | int incBase; /* the first include for this document */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 67 | int incNr; /* number of includes */ |
| 68 | int incMax; /* size of includes tab */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 69 | xmlXIncludeRefPtr *incTab; /* array of included references */ |
| 70 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 71 | int txtNr; /* number of unparsed documents */ |
| 72 | int txtMax; /* size of unparsed documents tab */ |
| 73 | xmlNodePtr *txtTab; /* array of unparsed text nodes */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 74 | xmlURL *txturlTab; /* array of unparsed text URLs */ |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 75 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 76 | xmlChar * url; /* the current URL processed */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 77 | int urlNr; /* number of URLs stacked */ |
| 78 | int urlMax; /* size of URL stack */ |
| 79 | xmlChar * *urlTab; /* URL stack */ |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 80 | |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 81 | int nbErrors; /* the number of errors detected */ |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 82 | int legacy; /* using XINCLUDE_OLD_NS */ |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 83 | int parseFlags; /* the flags used for parsing XML documents */ |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 84 | xmlChar * base; /* the current xml:base */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 87 | static int |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 88 | xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 89 | |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 90 | |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 91 | /************************************************************************ |
| 92 | * * |
Daniel Veillard | 69d2c17 | 2003-10-09 11:46:07 +0000 | [diff] [blame] | 93 | * XInclude error handler * |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 94 | * * |
| 95 | ************************************************************************/ |
| 96 | |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 97 | /** |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 98 | * xmlXIncludeErrMemory: |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 99 | * @extra: extra information |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 100 | * |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 101 | * Handle an out of memory condition |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 102 | */ |
| 103 | static void |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 104 | xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, |
| 105 | const char *extra) |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 106 | { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 107 | if (ctxt != NULL) |
| 108 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 109 | __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE, |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 110 | XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, |
| 111 | extra, NULL, NULL, 0, 0, |
| 112 | "Memory allocation failed : %s\n", extra); |
| 113 | } |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 114 | |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 115 | /** |
| 116 | * xmlXIncludeErr: |
| 117 | * @ctxt: the XInclude context |
| 118 | * @node: the context node |
| 119 | * @msg: the error message |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 120 | * @extra: extra information |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 121 | * |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 122 | * Handle an XInclude error |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 123 | */ |
| 124 | static void |
| 125 | xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error, |
| 126 | const char *msg, const xmlChar *extra) |
| 127 | { |
| 128 | if (ctxt != NULL) |
| 129 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 130 | __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE, |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 131 | error, XML_ERR_ERROR, NULL, 0, |
| 132 | (const char *) extra, NULL, NULL, 0, 0, |
| 133 | msg, (const char *) extra); |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 136 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 137 | /** |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 138 | * xmlXIncludeWarn: |
| 139 | * @ctxt: the XInclude context |
| 140 | * @node: the context node |
| 141 | * @msg: the error message |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 142 | * @extra: extra information |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 143 | * |
| 144 | * Emit an XInclude warning. |
| 145 | */ |
| 146 | static void |
| 147 | xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error, |
| 148 | const char *msg, const xmlChar *extra) |
| 149 | { |
| 150 | __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE, |
| 151 | error, XML_ERR_WARNING, NULL, 0, |
| 152 | (const char *) extra, NULL, NULL, 0, 0, |
| 153 | msg, (const char *) extra); |
| 154 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 155 | #endif |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * xmlXIncludeGetProp: |
| 159 | * @ctxt: the XInclude context |
| 160 | * @cur: the node |
| 161 | * @name: the attribute name |
| 162 | * |
| 163 | * Get an XInclude attribute |
| 164 | * |
| 165 | * Returns the value (to be freed) or NULL if not found |
| 166 | */ |
| 167 | static xmlChar * |
| 168 | xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur, |
| 169 | const xmlChar *name) { |
| 170 | xmlChar *ret; |
| 171 | |
| 172 | ret = xmlGetNsProp(cur, XINCLUDE_NS, name); |
| 173 | if (ret != NULL) |
| 174 | return(ret); |
| 175 | if (ctxt->legacy != 0) { |
| 176 | ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name); |
| 177 | if (ret != NULL) |
| 178 | return(ret); |
| 179 | } |
| 180 | ret = xmlGetProp(cur, name); |
| 181 | return(ret); |
| 182 | } |
| 183 | /** |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 184 | * xmlXIncludeFreeRef: |
| 185 | * @ref: the XInclude reference |
| 186 | * |
| 187 | * Free an XInclude reference |
| 188 | */ |
| 189 | static void |
| 190 | xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) { |
| 191 | if (ref == NULL) |
| 192 | return; |
| 193 | #ifdef DEBUG_XINCLUDE |
| 194 | xmlGenericError(xmlGenericErrorContext, "Freeing ref\n"); |
| 195 | #endif |
| 196 | if (ref->doc != NULL) { |
| 197 | #ifdef DEBUG_XINCLUDE |
| 198 | xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI); |
| 199 | #endif |
| 200 | xmlFreeDoc(ref->doc); |
| 201 | } |
| 202 | if (ref->URI != NULL) |
| 203 | xmlFree(ref->URI); |
| 204 | if (ref->fragment != NULL) |
| 205 | xmlFree(ref->fragment); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 206 | if (ref->xptr != NULL) |
| 207 | xmlXPathFreeObject(ref->xptr); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 208 | xmlFree(ref); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * xmlXIncludeNewRef: |
| 213 | * @ctxt: the XInclude context |
| 214 | * @URI: the resource URI |
| 215 | * |
| 216 | * Creates a new reference within an XInclude context |
| 217 | * |
| 218 | * Returns the new set |
| 219 | */ |
| 220 | static xmlXIncludeRefPtr |
| 221 | xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI, |
| 222 | xmlNodePtr ref) { |
| 223 | xmlXIncludeRefPtr ret; |
| 224 | |
| 225 | #ifdef DEBUG_XINCLUDE |
| 226 | xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI); |
| 227 | #endif |
| 228 | ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef)); |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 229 | if (ret == NULL) { |
| 230 | xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context"); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 231 | return(NULL); |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 232 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 233 | memset(ret, 0, sizeof(xmlXIncludeRef)); |
| 234 | if (URI == NULL) |
| 235 | ret->URI = NULL; |
| 236 | else |
| 237 | ret->URI = xmlStrdup(URI); |
| 238 | ret->fragment = NULL; |
| 239 | ret->ref = ref; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 240 | ret->doc = NULL; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 241 | ret->count = 0; |
| 242 | ret->xml = 0; |
| 243 | ret->inc = NULL; |
| 244 | if (ctxt->incMax == 0) { |
| 245 | ctxt->incMax = 4; |
| 246 | ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax * |
| 247 | sizeof(ctxt->incTab[0])); |
| 248 | if (ctxt->incTab == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 249 | xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context"); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 250 | xmlXIncludeFreeRef(ret); |
| 251 | return(NULL); |
| 252 | } |
| 253 | } |
| 254 | if (ctxt->incNr >= ctxt->incMax) { |
| 255 | ctxt->incMax *= 2; |
| 256 | ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab, |
| 257 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 258 | if (ctxt->incTab == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 259 | xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context"); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 260 | xmlXIncludeFreeRef(ret); |
| 261 | return(NULL); |
| 262 | } |
| 263 | } |
| 264 | ctxt->incTab[ctxt->incNr++] = ret; |
| 265 | return(ret); |
| 266 | } |
| 267 | |
| 268 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 269 | * xmlXIncludeNewContext: |
| 270 | * @doc: an XML Document |
| 271 | * |
| 272 | * Creates a new XInclude context |
| 273 | * |
| 274 | * Returns the new set |
| 275 | */ |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 276 | xmlXIncludeCtxtPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 277 | xmlXIncludeNewContext(xmlDocPtr doc) { |
| 278 | xmlXIncludeCtxtPtr ret; |
| 279 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 280 | #ifdef DEBUG_XINCLUDE |
| 281 | xmlGenericError(xmlGenericErrorContext, "New context\n"); |
| 282 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 283 | if (doc == NULL) |
| 284 | return(NULL); |
| 285 | ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt)); |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 286 | if (ret == NULL) { |
| 287 | xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc, |
| 288 | "creating XInclude context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 289 | return(NULL); |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 290 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 291 | memset(ret, 0, sizeof(xmlXIncludeCtxt)); |
| 292 | ret->doc = doc; |
| 293 | ret->incNr = 0; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 294 | ret->incBase = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 295 | ret->incMax = 0; |
| 296 | ret->incTab = NULL; |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 297 | ret->nbErrors = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 298 | return(ret); |
| 299 | } |
| 300 | |
| 301 | /** |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 302 | * xmlXIncludeURLPush: |
| 303 | * @ctxt: the parser context |
| 304 | * @value: the url |
| 305 | * |
| 306 | * Pushes a new url on top of the url stack |
| 307 | * |
| 308 | * Returns -1 in case of error, the index in the stack otherwise |
| 309 | */ |
| 310 | static int |
| 311 | xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt, |
| 312 | const xmlChar *value) |
| 313 | { |
| 314 | if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 315 | xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION, |
| 316 | "detected a recursion in %s\n", value); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 317 | return(-1); |
| 318 | } |
| 319 | if (ctxt->urlTab == NULL) { |
| 320 | ctxt->urlMax = 4; |
| 321 | ctxt->urlNr = 0; |
| 322 | ctxt->urlTab = (xmlChar * *) xmlMalloc( |
| 323 | ctxt->urlMax * sizeof(ctxt->urlTab[0])); |
| 324 | if (ctxt->urlTab == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 325 | xmlXIncludeErrMemory(ctxt, NULL, "adding URL"); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 326 | return (-1); |
| 327 | } |
| 328 | } |
| 329 | if (ctxt->urlNr >= ctxt->urlMax) { |
| 330 | ctxt->urlMax *= 2; |
| 331 | ctxt->urlTab = |
| 332 | (xmlChar * *) xmlRealloc(ctxt->urlTab, |
| 333 | ctxt->urlMax * |
| 334 | sizeof(ctxt->urlTab[0])); |
| 335 | if (ctxt->urlTab == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 336 | xmlXIncludeErrMemory(ctxt, NULL, "adding URL"); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 337 | return (-1); |
| 338 | } |
| 339 | } |
| 340 | ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value); |
| 341 | return (ctxt->urlNr++); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * xmlXIncludeURLPop: |
| 346 | * @ctxt: the parser context |
| 347 | * |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 348 | * Pops the top URL from the URL stack |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 349 | */ |
| 350 | static void |
| 351 | xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt) |
| 352 | { |
| 353 | xmlChar * ret; |
| 354 | |
| 355 | if (ctxt->urlNr <= 0) |
| 356 | return; |
| 357 | ctxt->urlNr--; |
| 358 | if (ctxt->urlNr > 0) |
| 359 | ctxt->url = ctxt->urlTab[ctxt->urlNr - 1]; |
| 360 | else |
| 361 | ctxt->url = NULL; |
| 362 | ret = ctxt->urlTab[ctxt->urlNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 363 | ctxt->urlTab[ctxt->urlNr] = NULL; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 364 | if (ret != NULL) |
| 365 | xmlFree(ret); |
| 366 | } |
| 367 | |
| 368 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 369 | * xmlXIncludeFreeContext: |
| 370 | * @ctxt: the XInclude context |
| 371 | * |
| 372 | * Free an XInclude context |
| 373 | */ |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 374 | void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 375 | xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) { |
| 376 | int i; |
| 377 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 378 | #ifdef DEBUG_XINCLUDE |
| 379 | xmlGenericError(xmlGenericErrorContext, "Freeing context\n"); |
| 380 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 381 | if (ctxt == NULL) |
| 382 | return; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 383 | while (ctxt->urlNr > 0) |
| 384 | xmlXIncludeURLPop(ctxt); |
| 385 | if (ctxt->urlTab != NULL) |
| 386 | xmlFree(ctxt->urlTab); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 387 | for (i = 0;i < ctxt->incNr;i++) { |
| 388 | if (ctxt->incTab[i] != NULL) |
| 389 | xmlXIncludeFreeRef(ctxt->incTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 390 | } |
| 391 | for (i = 0;i < ctxt->txtNr;i++) { |
| 392 | if (ctxt->txturlTab[i] != NULL) |
| 393 | xmlFree(ctxt->txturlTab[i]); |
| 394 | } |
| 395 | if (ctxt->incTab != NULL) |
| 396 | xmlFree(ctxt->incTab); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 397 | if (ctxt->txtTab != NULL) |
| 398 | xmlFree(ctxt->txtTab); |
| 399 | if (ctxt->txturlTab != NULL) |
| 400 | xmlFree(ctxt->txturlTab); |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 401 | if (ctxt->base != NULL) { |
| 402 | xmlFree(ctxt->base); |
| 403 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 404 | xmlFree(ctxt); |
| 405 | } |
| 406 | |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 407 | /** |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 408 | * xmlXIncludeParseFile: |
| 409 | * @ctxt: the XInclude context |
| 410 | * @URL: the URL or file path |
| 411 | * |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 412 | * parse a document for XInclude |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 413 | */ |
| 414 | static xmlDocPtr |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 415 | xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) { |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 416 | xmlDocPtr ret; |
| 417 | xmlParserCtxtPtr pctxt; |
| 418 | char *directory = NULL; |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 419 | xmlParserInputPtr inputStream; |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 420 | |
| 421 | xmlInitParser(); |
| 422 | |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 423 | pctxt = xmlNewParserCtxt(); |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 424 | if (pctxt == NULL) { |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 425 | xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context"); |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 426 | return(NULL); |
| 427 | } |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 428 | /* |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 429 | * try to ensure that new documents included are actually |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 430 | * built with the same dictionary as the including document. |
| 431 | */ |
| 432 | if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL) && |
| 433 | (pctxt->dict != NULL)) { |
| 434 | xmlDictFree(pctxt->dict); |
| 435 | pctxt->dict = ctxt->doc->dict; |
| 436 | xmlDictReference(pctxt->dict); |
| 437 | } |
| 438 | |
| 439 | xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD); |
| 440 | |
| 441 | inputStream = xmlLoadExternalEntity(URL, NULL, pctxt); |
| 442 | if (inputStream == NULL) { |
| 443 | xmlFreeParserCtxt(pctxt); |
| 444 | return(NULL); |
| 445 | } |
| 446 | |
| 447 | inputPush(pctxt, inputStream); |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 448 | |
| 449 | if ((pctxt->directory == NULL) && (directory == NULL)) |
| 450 | directory = xmlParserGetDirectory(URL); |
| 451 | if ((pctxt->directory == NULL) && (directory != NULL)) |
| 452 | pctxt->directory = (char *) xmlStrdup((xmlChar *) directory); |
| 453 | |
William M. Brack | a22da29 | 2005-02-12 01:08:22 +0000 | [diff] [blame] | 454 | pctxt->loadsubset |= XML_DETECT_IDS; |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 455 | |
| 456 | xmlParseDocument(pctxt); |
| 457 | |
William M. Brack | 1ff4213 | 2003-12-31 14:05:15 +0000 | [diff] [blame] | 458 | if (pctxt->wellFormed) { |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 459 | ret = pctxt->myDoc; |
William M. Brack | 1ff4213 | 2003-12-31 14:05:15 +0000 | [diff] [blame] | 460 | } |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 461 | else { |
| 462 | ret = NULL; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 463 | if (pctxt->myDoc != NULL) |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 464 | xmlFreeDoc(pctxt->myDoc); |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 465 | pctxt->myDoc = NULL; |
| 466 | } |
| 467 | xmlFreeParserCtxt(pctxt); |
| 468 | |
| 469 | return(ret); |
| 470 | } |
| 471 | |
| 472 | /** |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 473 | * xmlXIncludeAddNode: |
| 474 | * @ctxt: the XInclude context |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 475 | * @cur: the new node |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 476 | * |
| 477 | * Add a new node to process to an XInclude context |
| 478 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 479 | static int |
| 480 | xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) { |
| 481 | xmlXIncludeRefPtr ref; |
| 482 | xmlURIPtr uri; |
| 483 | xmlChar *URL; |
| 484 | xmlChar *fragment = NULL; |
| 485 | xmlChar *href; |
| 486 | xmlChar *parse; |
| 487 | xmlChar *base; |
| 488 | xmlChar *URI; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 489 | int xml = 1, i; /* default Issue 64 */ |
| 490 | int local = 0; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 491 | |
| 492 | |
| 493 | if (ctxt == NULL) |
| 494 | return(-1); |
| 495 | if (cur == NULL) |
| 496 | return(-1); |
| 497 | |
| 498 | #ifdef DEBUG_XINCLUDE |
| 499 | xmlGenericError(xmlGenericErrorContext, "Add node\n"); |
| 500 | #endif |
| 501 | /* |
| 502 | * read the attributes |
| 503 | */ |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 504 | href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 505 | if (href == NULL) { |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 506 | href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */ |
| 507 | if (href == NULL) |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 508 | return(-1); |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 509 | local = 1; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 510 | } |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 511 | if (href[0] == '#') |
| 512 | local = 1; |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 513 | parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 514 | if (parse != NULL) { |
| 515 | if (xmlStrEqual(parse, XINCLUDE_PARSE_XML)) |
| 516 | xml = 1; |
| 517 | else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) |
| 518 | xml = 0; |
| 519 | else { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 520 | xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE, |
| 521 | "invalid value %s for 'parse'\n", parse); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 522 | if (href != NULL) |
| 523 | xmlFree(href); |
| 524 | if (parse != NULL) |
| 525 | xmlFree(parse); |
| 526 | return(-1); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 529 | |
| 530 | /* |
| 531 | * compute the URI |
| 532 | */ |
| 533 | base = xmlNodeGetBase(ctxt->doc, cur); |
| 534 | if (base == NULL) { |
| 535 | URI = xmlBuildURI(href, ctxt->doc->URL); |
| 536 | } else { |
| 537 | URI = xmlBuildURI(href, base); |
| 538 | } |
| 539 | if (URI == NULL) { |
| 540 | xmlChar *escbase; |
| 541 | xmlChar *eschref; |
| 542 | /* |
| 543 | * Some escaping may be needed |
| 544 | */ |
| 545 | escbase = xmlURIEscape(base); |
| 546 | eschref = xmlURIEscape(href); |
| 547 | URI = xmlBuildURI(eschref, escbase); |
| 548 | if (escbase != NULL) |
| 549 | xmlFree(escbase); |
| 550 | if (eschref != NULL) |
| 551 | xmlFree(eschref); |
| 552 | } |
| 553 | if (parse != NULL) |
| 554 | xmlFree(parse); |
| 555 | if (href != NULL) |
| 556 | xmlFree(href); |
| 557 | if (base != NULL) |
| 558 | xmlFree(base); |
| 559 | if (URI == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 560 | xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, |
| 561 | "failed build URL\n", NULL); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 562 | return(-1); |
| 563 | } |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 564 | fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 565 | |
| 566 | /* |
| 567 | * Check the URL and remove any fragment identifier |
| 568 | */ |
| 569 | uri = xmlParseURI((const char *)URI); |
| 570 | if (uri == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 571 | xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, |
| 572 | "invalid value URI %s\n", URI); |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 573 | if (fragment != NULL) |
| 574 | xmlFree(fragment); |
| 575 | xmlFree(URI); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 576 | return(-1); |
| 577 | } |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 578 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 579 | if (uri->fragment != NULL) { |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 580 | if (ctxt->legacy != 0) { |
| 581 | if (fragment == NULL) { |
| 582 | fragment = (xmlChar *) uri->fragment; |
| 583 | } else { |
| 584 | xmlFree(uri->fragment); |
| 585 | } |
| 586 | } else { |
| 587 | xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID, |
| 588 | "Invalid fragment identifier in URI %s use the xpointer attribute\n", |
| 589 | URI); |
| 590 | if (fragment != NULL) |
| 591 | xmlFree(fragment); |
| 592 | xmlFreeURI(uri); |
| 593 | xmlFree(URI); |
| 594 | return(-1); |
| 595 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 596 | uri->fragment = NULL; |
| 597 | } |
| 598 | URL = xmlSaveUri(uri); |
| 599 | xmlFreeURI(uri); |
| 600 | xmlFree(URI); |
| 601 | if (URL == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 602 | xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, |
| 603 | "invalid value URI %s\n", URI); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 604 | if (fragment != NULL) |
| 605 | xmlFree(fragment); |
| 606 | return(-1); |
| 607 | } |
| 608 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 609 | /* |
| 610 | * Check the URL against the stack for recursions |
| 611 | */ |
| 612 | if (!local) { |
| 613 | for (i = 0;i < ctxt->urlNr;i++) { |
| 614 | if (xmlStrEqual(URL, ctxt->urlTab[i])) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 615 | xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION, |
| 616 | "detected a recursion in %s\n", URL); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 617 | return(-1); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 622 | ref = xmlXIncludeNewRef(ctxt, URL, cur); |
| 623 | if (ref == NULL) { |
| 624 | return(-1); |
| 625 | } |
| 626 | ref->fragment = fragment; |
| 627 | ref->doc = NULL; |
| 628 | ref->xml = xml; |
| 629 | ref->count = 1; |
| 630 | xmlFree(URL); |
| 631 | return(0); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /** |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 635 | * xmlXIncludeRecurseDoc: |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 636 | * @ctxt: the XInclude context |
| 637 | * @doc: the new document |
| 638 | * @url: the associated URL |
| 639 | * |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 640 | * The XInclude recursive nature is handled at this point. |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 641 | */ |
| 642 | static void |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 643 | xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 644 | const xmlURL url ATTRIBUTE_UNUSED) { |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 645 | xmlXIncludeCtxtPtr newctxt; |
| 646 | int i; |
| 647 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 648 | /* |
| 649 | * Avoid recursion in already substitued resources |
| 650 | for (i = 0;i < ctxt->urlNr;i++) { |
| 651 | if (xmlStrEqual(doc->URL, ctxt->urlTab[i])) |
| 652 | return; |
| 653 | } |
| 654 | */ |
| 655 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 656 | #ifdef DEBUG_XINCLUDE |
| 657 | xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL); |
| 658 | #endif |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 659 | /* |
| 660 | * Handle recursion here. |
| 661 | */ |
| 662 | |
| 663 | newctxt = xmlXIncludeNewContext(doc); |
| 664 | if (newctxt != NULL) { |
| 665 | /* |
| 666 | * Copy the existing document set |
| 667 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 668 | newctxt->incMax = ctxt->incMax; |
| 669 | newctxt->incNr = ctxt->incNr; |
| 670 | newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax * |
| 671 | sizeof(newctxt->incTab[0])); |
| 672 | if (newctxt->incTab == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 673 | xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc"); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 674 | xmlFree(newctxt); |
| 675 | return; |
| 676 | } |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 677 | /* |
| 678 | * copy the urlTab |
| 679 | */ |
| 680 | newctxt->urlMax = ctxt->urlMax; |
| 681 | newctxt->urlNr = ctxt->urlNr; |
| 682 | newctxt->urlTab = ctxt->urlTab; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 683 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 684 | /* |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 685 | * Inherit the existing base |
| 686 | */ |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 687 | newctxt->base = xmlStrdup(ctxt->base); |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 688 | |
| 689 | /* |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 690 | * Inherit the documents already in use by other includes |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 691 | */ |
| 692 | newctxt->incBase = ctxt->incNr; |
| 693 | for (i = 0;i < ctxt->incNr;i++) { |
| 694 | newctxt->incTab[i] = ctxt->incTab[i]; |
| 695 | newctxt->incTab[i]->count++; /* prevent the recursion from |
| 696 | freeing it */ |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 697 | } |
William M. Brack | a11e483 | 2004-03-07 11:03:43 +0000 | [diff] [blame] | 698 | /* |
| 699 | * The new context should also inherit the Parse Flags |
| 700 | * (bug 132597) |
| 701 | */ |
| 702 | newctxt->parseFlags = ctxt->parseFlags; |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 703 | xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc)); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 704 | for (i = 0;i < ctxt->incNr;i++) { |
| 705 | newctxt->incTab[i]->count--; |
| 706 | newctxt->incTab[i] = NULL; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 707 | } |
Daniel Veillard | d9b7283 | 2003-03-27 14:24:00 +0000 | [diff] [blame] | 708 | |
| 709 | /* urlTab may have been reallocated */ |
| 710 | ctxt->urlTab = newctxt->urlTab; |
| 711 | ctxt->urlMax = newctxt->urlMax; |
| 712 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 713 | newctxt->urlMax = 0; |
| 714 | newctxt->urlNr = 0; |
| 715 | newctxt->urlTab = NULL; |
| 716 | |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 717 | xmlXIncludeFreeContext(newctxt); |
| 718 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 719 | #ifdef DEBUG_XINCLUDE |
| 720 | xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url); |
| 721 | #endif |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /** |
| 725 | * xmlXIncludeAddTxt: |
| 726 | * @ctxt: the XInclude context |
| 727 | * @txt: the new text node |
| 728 | * @url: the associated URL |
| 729 | * |
| 730 | * Add a new txtument to the list |
| 731 | */ |
| 732 | static void |
| 733 | xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) { |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 734 | #ifdef DEBUG_XINCLUDE |
| 735 | xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url); |
| 736 | #endif |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 737 | if (ctxt->txtMax == 0) { |
| 738 | ctxt->txtMax = 4; |
| 739 | ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax * |
| 740 | sizeof(ctxt->txtTab[0])); |
| 741 | if (ctxt->txtTab == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 742 | xmlXIncludeErrMemory(ctxt, NULL, "processing text"); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 743 | return; |
| 744 | } |
| 745 | ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax * |
| 746 | sizeof(ctxt->txturlTab[0])); |
| 747 | if (ctxt->txturlTab == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 748 | xmlXIncludeErrMemory(ctxt, NULL, "processing text"); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 749 | return; |
| 750 | } |
| 751 | } |
| 752 | if (ctxt->txtNr >= ctxt->txtMax) { |
| 753 | ctxt->txtMax *= 2; |
| 754 | ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab, |
| 755 | ctxt->txtMax * sizeof(ctxt->txtTab[0])); |
| 756 | if (ctxt->txtTab == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 757 | xmlXIncludeErrMemory(ctxt, NULL, "processing text"); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 758 | return; |
| 759 | } |
| 760 | ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab, |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 761 | ctxt->txtMax * sizeof(ctxt->txturlTab[0])); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 762 | if (ctxt->txturlTab == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 763 | xmlXIncludeErrMemory(ctxt, NULL, "processing text"); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 764 | return; |
| 765 | } |
| 766 | } |
| 767 | ctxt->txtTab[ctxt->txtNr] = txt; |
| 768 | ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url); |
| 769 | ctxt->txtNr++; |
| 770 | } |
| 771 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 772 | /************************************************************************ |
| 773 | * * |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 774 | * Node copy with specific semantic * |
| 775 | * * |
| 776 | ************************************************************************/ |
| 777 | |
| 778 | /** |
| 779 | * xmlXIncludeCopyNode: |
| 780 | * @ctxt: the XInclude context |
| 781 | * @target: the document target |
| 782 | * @source: the document source |
| 783 | * @elem: the element |
| 784 | * |
| 785 | * Make a copy of the node while preserving the XInclude semantic |
| 786 | * of the Infoset copy |
| 787 | */ |
| 788 | static xmlNodePtr |
| 789 | xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, |
| 790 | xmlDocPtr source, xmlNodePtr elem) { |
| 791 | xmlNodePtr result = NULL; |
| 792 | |
| 793 | if ((ctxt == NULL) || (target == NULL) || (source == NULL) || |
| 794 | (elem == NULL)) |
| 795 | return(NULL); |
| 796 | if (elem->type == XML_DTD_NODE) |
| 797 | return(NULL); |
| 798 | result = xmlDocCopyNode(elem, target, 1); |
| 799 | return(result); |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * xmlXIncludeCopyNodeList: |
| 804 | * @ctxt: the XInclude context |
| 805 | * @target: the document target |
| 806 | * @source: the document source |
| 807 | * @elem: the element list |
| 808 | * |
| 809 | * Make a copy of the node list while preserving the XInclude semantic |
| 810 | * of the Infoset copy |
| 811 | */ |
| 812 | static xmlNodePtr |
| 813 | xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, |
| 814 | xmlDocPtr source, xmlNodePtr elem) { |
| 815 | xmlNodePtr cur, res, result = NULL, last = NULL; |
| 816 | |
| 817 | if ((ctxt == NULL) || (target == NULL) || (source == NULL) || |
| 818 | (elem == NULL)) |
| 819 | return(NULL); |
| 820 | cur = elem; |
| 821 | while (cur != NULL) { |
| 822 | res = xmlXIncludeCopyNode(ctxt, target, source, cur); |
| 823 | if (res != NULL) { |
| 824 | if (result == NULL) { |
| 825 | result = last = res; |
| 826 | } else { |
| 827 | last->next = res; |
| 828 | res->prev = last; |
| 829 | last = res; |
| 830 | } |
| 831 | } |
| 832 | cur = cur->next; |
| 833 | } |
| 834 | return(result); |
| 835 | } |
| 836 | |
| 837 | /** |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 838 | * xmlXIncludeGetNthChild: |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 839 | * @cur: the node |
| 840 | * @no: the child number |
| 841 | * |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 842 | * Returns the @n'th element child of @cur or NULL |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 843 | */ |
| 844 | static xmlNodePtr |
| 845 | xmlXIncludeGetNthChild(xmlNodePtr cur, int no) { |
| 846 | int i; |
| 847 | if (cur == NULL) |
| 848 | return(cur); |
| 849 | cur = cur->children; |
| 850 | for (i = 0;i <= no;cur = cur->next) { |
| 851 | if (cur == NULL) |
| 852 | return(cur); |
| 853 | if ((cur->type == XML_ELEMENT_NODE) || |
| 854 | (cur->type == XML_DOCUMENT_NODE) || |
| 855 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 856 | i++; |
| 857 | if (i == no) |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | return(cur); |
| 862 | } |
| 863 | |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 864 | xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */ |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 865 | /** |
| 866 | * xmlXIncludeCopyRange: |
| 867 | * @ctxt: the XInclude context |
| 868 | * @target: the document target |
| 869 | * @source: the document source |
| 870 | * @obj: the XPointer result from the evaluation. |
| 871 | * |
| 872 | * Build a node list tree copy of the XPointer result. |
| 873 | * |
| 874 | * Returns an xmlNodePtr list or NULL. |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 875 | * The caller has to free the node tree. |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 876 | */ |
| 877 | static xmlNodePtr |
| 878 | xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, |
| 879 | xmlDocPtr source, xmlXPathObjectPtr range) { |
| 880 | /* pointers to generated nodes */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 881 | xmlNodePtr list = NULL, last = NULL, listParent = NULL; |
| 882 | xmlNodePtr tmp, tmp2; |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 883 | /* pointers to traversal nodes */ |
| 884 | xmlNodePtr start, cur, end; |
| 885 | int index1, index2; |
William M. Brack | 6bdacd7 | 2004-02-07 08:53:23 +0000 | [diff] [blame] | 886 | int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0; |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 887 | |
| 888 | if ((ctxt == NULL) || (target == NULL) || (source == NULL) || |
| 889 | (range == NULL)) |
| 890 | return(NULL); |
| 891 | if (range->type != XPATH_RANGE) |
| 892 | return(NULL); |
| 893 | start = (xmlNodePtr) range->user; |
| 894 | |
| 895 | if (start == NULL) |
| 896 | return(NULL); |
| 897 | end = range->user2; |
| 898 | if (end == NULL) |
| 899 | return(xmlDocCopyNode(start, target, 1)); |
| 900 | |
| 901 | cur = start; |
| 902 | index1 = range->index; |
| 903 | index2 = range->index2; |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 904 | /* |
| 905 | * level is depth of the current node under consideration |
| 906 | * list is the pointer to the root of the output tree |
| 907 | * listParent is a pointer to the parent of output tree (within |
| 908 | the included file) in case we need to add another level |
| 909 | * last is a pointer to the last node added to the output tree |
| 910 | * lastLevel is the depth of last (relative to the root) |
| 911 | */ |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 912 | while (cur != NULL) { |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 913 | /* |
| 914 | * Check if our output tree needs a parent |
| 915 | */ |
| 916 | if (level < 0) { |
| 917 | while (level < 0) { |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 918 | /* copy must include namespaces and properties */ |
| 919 | tmp2 = xmlDocCopyNode(listParent, target, 2); |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 920 | xmlAddChild(tmp2, list); |
| 921 | list = tmp2; |
| 922 | listParent = listParent->parent; |
| 923 | level++; |
| 924 | } |
| 925 | last = list; |
| 926 | lastLevel = 0; |
| 927 | } |
| 928 | /* |
| 929 | * Check whether we need to change our insertion point |
| 930 | */ |
| 931 | while (level < lastLevel) { |
| 932 | last = last->parent; |
| 933 | lastLevel --; |
| 934 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 935 | if (cur == end) { /* Are we at the end of the range? */ |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 936 | if (cur->type == XML_TEXT_NODE) { |
| 937 | const xmlChar *content = cur->content; |
| 938 | int len; |
| 939 | |
| 940 | if (content == NULL) { |
| 941 | tmp = xmlNewTextLen(NULL, 0); |
| 942 | } else { |
| 943 | len = index2; |
| 944 | if ((cur == start) && (index1 > 1)) { |
| 945 | content += (index1 - 1); |
| 946 | len -= (index1 - 1); |
| 947 | index1 = 0; |
| 948 | } else { |
| 949 | len = index2; |
| 950 | } |
| 951 | tmp = xmlNewTextLen(content, len); |
| 952 | } |
| 953 | /* single sub text node selection */ |
| 954 | if (list == NULL) |
| 955 | return(tmp); |
| 956 | /* prune and return full set */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 957 | if (level == lastLevel) |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 958 | xmlAddNextSibling(last, tmp); |
| 959 | else |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 960 | xmlAddChild(last, tmp); |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 961 | return(list); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 962 | } else { /* ending node not a text node */ |
William M. Brack | 6bdacd7 | 2004-02-07 08:53:23 +0000 | [diff] [blame] | 963 | endLevel = level; /* remember the level of the end node */ |
| 964 | endFlag = 1; |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 965 | /* last node - need to take care of properties + namespaces */ |
| 966 | tmp = xmlDocCopyNode(cur, target, 2); |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 967 | if (list == NULL) { |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 968 | list = tmp; |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 969 | listParent = cur->parent; |
| 970 | } else { |
| 971 | if (level == lastLevel) |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 972 | xmlAddNextSibling(last, tmp); |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 973 | else { |
| 974 | xmlAddChild(last, tmp); |
| 975 | lastLevel = level; |
| 976 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 977 | } |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 978 | last = tmp; |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 979 | |
| 980 | if (index2 > 1) { |
| 981 | end = xmlXIncludeGetNthChild(cur, index2 - 1); |
| 982 | index2 = 0; |
| 983 | } |
| 984 | if ((cur == start) && (index1 > 1)) { |
| 985 | cur = xmlXIncludeGetNthChild(cur, index1 - 1); |
| 986 | index1 = 0; |
William M. Brack | 6bdacd7 | 2004-02-07 08:53:23 +0000 | [diff] [blame] | 987 | } else { |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 988 | cur = cur->children; |
| 989 | } |
William M. Brack | 6bdacd7 | 2004-02-07 08:53:23 +0000 | [diff] [blame] | 990 | level++; /* increment level to show change */ |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 991 | /* |
| 992 | * Now gather the remaining nodes from cur to end |
| 993 | */ |
William M. Brack | 6bdacd7 | 2004-02-07 08:53:23 +0000 | [diff] [blame] | 994 | continue; /* while */ |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 995 | } |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 996 | } else if (cur == start) { /* Not at the end, are we at start? */ |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 997 | if ((cur->type == XML_TEXT_NODE) || |
| 998 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 999 | const xmlChar *content = cur->content; |
| 1000 | |
| 1001 | if (content == NULL) { |
| 1002 | tmp = xmlNewTextLen(NULL, 0); |
| 1003 | } else { |
| 1004 | if (index1 > 1) { |
| 1005 | content += (index1 - 1); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 1006 | index1 = 0; |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1007 | } |
| 1008 | tmp = xmlNewText(content); |
| 1009 | } |
| 1010 | last = list = tmp; |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 1011 | listParent = cur->parent; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 1012 | } else { /* Not text node */ |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 1013 | /* |
| 1014 | * start of the range - need to take care of |
| 1015 | * properties and namespaces |
| 1016 | */ |
| 1017 | tmp = xmlDocCopyNode(cur, target, 2); |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 1018 | list = last = tmp; |
| 1019 | listParent = cur->parent; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 1020 | if (index1 > 1) { /* Do we need to position? */ |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1021 | cur = xmlXIncludeGetNthChild(cur, index1 - 1); |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 1022 | level = lastLevel = 1; |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1023 | index1 = 0; |
| 1024 | /* |
| 1025 | * Now gather the remaining nodes from cur to end |
| 1026 | */ |
| 1027 | continue; /* while */ |
| 1028 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } else { |
| 1031 | tmp = NULL; |
| 1032 | switch (cur->type) { |
| 1033 | case XML_DTD_NODE: |
| 1034 | case XML_ELEMENT_DECL: |
| 1035 | case XML_ATTRIBUTE_DECL: |
| 1036 | case XML_ENTITY_NODE: |
| 1037 | /* Do not copy DTD informations */ |
| 1038 | break; |
| 1039 | case XML_ENTITY_DECL: |
| 1040 | /* handle crossing entities -> stack needed */ |
| 1041 | break; |
| 1042 | case XML_XINCLUDE_START: |
| 1043 | case XML_XINCLUDE_END: |
| 1044 | /* don't consider it part of the tree content */ |
| 1045 | break; |
| 1046 | case XML_ATTRIBUTE_NODE: |
| 1047 | /* Humm, should not happen ! */ |
| 1048 | break; |
| 1049 | default: |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 1050 | /* |
| 1051 | * Middle of the range - need to take care of |
| 1052 | * properties and namespaces |
| 1053 | */ |
| 1054 | tmp = xmlDocCopyNode(cur, target, 2); |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | } |
| 1057 | if (tmp != NULL) { |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 1058 | if (level == lastLevel) |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1059 | xmlAddNextSibling(last, tmp); |
| 1060 | else { |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 1061 | xmlAddChild(last, tmp); |
| 1062 | lastLevel = level; |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1063 | } |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 1064 | last = tmp; |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | /* |
| 1068 | * Skip to next node in document order |
| 1069 | */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 1070 | cur = xmlXPtrAdvanceNode(cur, &level); |
William M. Brack | 6bdacd7 | 2004-02-07 08:53:23 +0000 | [diff] [blame] | 1071 | if (endFlag && (level >= endLevel)) |
| 1072 | break; |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1073 | } |
| 1074 | return(list); |
| 1075 | } |
| 1076 | |
| 1077 | /** |
| 1078 | * xmlXIncludeBuildNodeList: |
| 1079 | * @ctxt: the XInclude context |
| 1080 | * @target: the document target |
| 1081 | * @source: the document source |
| 1082 | * @obj: the XPointer result from the evaluation. |
| 1083 | * |
| 1084 | * Build a node list tree copy of the XPointer result. |
| 1085 | * This will drop Attributes and Namespace declarations. |
| 1086 | * |
| 1087 | * Returns an xmlNodePtr list or NULL. |
| 1088 | * the caller has to free the node tree. |
| 1089 | */ |
| 1090 | static xmlNodePtr |
| 1091 | xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, |
| 1092 | xmlDocPtr source, xmlXPathObjectPtr obj) { |
| 1093 | xmlNodePtr list = NULL, last = NULL; |
| 1094 | int i; |
| 1095 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1096 | if (source == NULL) |
| 1097 | source = ctxt->doc; |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1098 | if ((ctxt == NULL) || (target == NULL) || (source == NULL) || |
| 1099 | (obj == NULL)) |
| 1100 | return(NULL); |
| 1101 | switch (obj->type) { |
| 1102 | case XPATH_NODESET: { |
| 1103 | xmlNodeSetPtr set = obj->nodesetval; |
| 1104 | if (set == NULL) |
| 1105 | return(NULL); |
| 1106 | for (i = 0;i < set->nodeNr;i++) { |
| 1107 | if (set->nodeTab[i] == NULL) |
| 1108 | continue; |
| 1109 | switch (set->nodeTab[i]->type) { |
| 1110 | case XML_TEXT_NODE: |
| 1111 | case XML_CDATA_SECTION_NODE: |
| 1112 | case XML_ELEMENT_NODE: |
| 1113 | case XML_ENTITY_REF_NODE: |
| 1114 | case XML_ENTITY_NODE: |
| 1115 | case XML_PI_NODE: |
| 1116 | case XML_COMMENT_NODE: |
| 1117 | case XML_DOCUMENT_NODE: |
| 1118 | case XML_HTML_DOCUMENT_NODE: |
| 1119 | #ifdef LIBXML_DOCB_ENABLED |
| 1120 | case XML_DOCB_DOCUMENT_NODE: |
| 1121 | #endif |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1122 | case XML_XINCLUDE_END: |
| 1123 | break; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1124 | case XML_XINCLUDE_START: { |
| 1125 | xmlNodePtr tmp, cur = set->nodeTab[i]; |
| 1126 | |
| 1127 | cur = cur->next; |
| 1128 | while (cur != NULL) { |
| 1129 | switch(cur->type) { |
| 1130 | case XML_TEXT_NODE: |
| 1131 | case XML_CDATA_SECTION_NODE: |
| 1132 | case XML_ELEMENT_NODE: |
| 1133 | case XML_ENTITY_REF_NODE: |
| 1134 | case XML_ENTITY_NODE: |
| 1135 | case XML_PI_NODE: |
| 1136 | case XML_COMMENT_NODE: |
| 1137 | tmp = xmlXIncludeCopyNode(ctxt, target, |
| 1138 | source, cur); |
| 1139 | if (last == NULL) { |
| 1140 | list = last = tmp; |
| 1141 | } else { |
| 1142 | xmlAddNextSibling(last, tmp); |
| 1143 | last = tmp; |
| 1144 | } |
| 1145 | cur = cur->next; |
| 1146 | continue; |
| 1147 | default: |
| 1148 | break; |
| 1149 | } |
| 1150 | break; |
| 1151 | } |
| 1152 | continue; |
| 1153 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1154 | case XML_ATTRIBUTE_NODE: |
| 1155 | case XML_NAMESPACE_DECL: |
| 1156 | case XML_DOCUMENT_TYPE_NODE: |
| 1157 | case XML_DOCUMENT_FRAG_NODE: |
| 1158 | case XML_NOTATION_NODE: |
| 1159 | case XML_DTD_NODE: |
| 1160 | case XML_ELEMENT_DECL: |
| 1161 | case XML_ATTRIBUTE_DECL: |
| 1162 | case XML_ENTITY_DECL: |
| 1163 | continue; /* for */ |
| 1164 | } |
| 1165 | if (last == NULL) |
| 1166 | list = last = xmlXIncludeCopyNode(ctxt, target, source, |
| 1167 | set->nodeTab[i]); |
| 1168 | else { |
| 1169 | xmlAddNextSibling(last, |
| 1170 | xmlXIncludeCopyNode(ctxt, target, source, |
| 1171 | set->nodeTab[i])); |
| 1172 | if (last->next != NULL) |
| 1173 | last = last->next; |
| 1174 | } |
| 1175 | } |
| 1176 | break; |
| 1177 | } |
| 1178 | case XPATH_LOCATIONSET: { |
| 1179 | xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user; |
| 1180 | if (set == NULL) |
| 1181 | return(NULL); |
| 1182 | for (i = 0;i < set->locNr;i++) { |
| 1183 | if (last == NULL) |
| 1184 | list = last = xmlXIncludeCopyXPointer(ctxt, target, source, |
| 1185 | set->locTab[i]); |
| 1186 | else |
| 1187 | xmlAddNextSibling(last, |
| 1188 | xmlXIncludeCopyXPointer(ctxt, target, source, |
| 1189 | set->locTab[i])); |
| 1190 | if (last != NULL) { |
| 1191 | while (last->next != NULL) |
| 1192 | last = last->next; |
| 1193 | } |
| 1194 | } |
| 1195 | break; |
| 1196 | } |
Daniel Veillard | 10acc2f | 2003-09-01 20:59:40 +0000 | [diff] [blame] | 1197 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1198 | case XPATH_RANGE: |
| 1199 | return(xmlXIncludeCopyRange(ctxt, target, source, obj)); |
Daniel Veillard | 10acc2f | 2003-09-01 20:59:40 +0000 | [diff] [blame] | 1200 | #endif |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1201 | case XPATH_POINT: |
| 1202 | /* points are ignored in XInclude */ |
| 1203 | break; |
| 1204 | default: |
| 1205 | break; |
| 1206 | } |
| 1207 | return(list); |
| 1208 | } |
| 1209 | /************************************************************************ |
| 1210 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1211 | * XInclude I/O handling * |
| 1212 | * * |
| 1213 | ************************************************************************/ |
| 1214 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1215 | typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData; |
| 1216 | typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr; |
| 1217 | struct _xmlXIncludeMergeData { |
| 1218 | xmlDocPtr doc; |
| 1219 | xmlXIncludeCtxtPtr ctxt; |
| 1220 | }; |
| 1221 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1222 | /** |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1223 | * xmlXIncludeMergeOneEntity: |
| 1224 | * @ent: the entity |
| 1225 | * @doc: the including doc |
| 1226 | * @nr: the entity name |
| 1227 | * |
| 1228 | * Inplements the merge of one entity |
| 1229 | */ |
| 1230 | static void |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1231 | xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data, |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1232 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1233 | xmlEntityPtr ret, prev; |
| 1234 | xmlDocPtr doc; |
| 1235 | xmlXIncludeCtxtPtr ctxt; |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1236 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1237 | if ((ent == NULL) || (data == NULL)) |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1238 | return; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1239 | ctxt = data->ctxt; |
| 1240 | doc = data->doc; |
| 1241 | if ((ctxt == NULL) || (doc == NULL)) |
| 1242 | return; |
| 1243 | switch (ent->etype) { |
| 1244 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 1245 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 1246 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 1247 | return; |
| 1248 | case XML_INTERNAL_GENERAL_ENTITY: |
| 1249 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 1250 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 1251 | break; |
| 1252 | } |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1253 | ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID, |
| 1254 | ent->SystemID, ent->content); |
| 1255 | if (ret != NULL) { |
| 1256 | if (ent->URI != NULL) |
| 1257 | ret->URI = xmlStrdup(ent->URI); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1258 | } else { |
| 1259 | prev = xmlGetDocEntity(doc, ent->name); |
| 1260 | if (prev != NULL) { |
| 1261 | if (ent->etype != prev->etype) |
| 1262 | goto error; |
| 1263 | |
| 1264 | if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) { |
| 1265 | if (!xmlStrEqual(ent->SystemID, prev->SystemID)) |
| 1266 | goto error; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1267 | } else if ((ent->ExternalID != NULL) && |
| 1268 | (prev->ExternalID != NULL)) { |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1269 | if (!xmlStrEqual(ent->ExternalID, prev->ExternalID)) |
| 1270 | goto error; |
Daniel Veillard | 2406abd | 2003-02-24 18:16:47 +0000 | [diff] [blame] | 1271 | } else if ((ent->content != NULL) && (prev->content != NULL)) { |
| 1272 | if (!xmlStrEqual(ent->content, prev->content)) |
| 1273 | goto error; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1274 | } else { |
| 1275 | goto error; |
| 1276 | } |
| 1277 | |
| 1278 | } |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1279 | } |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1280 | return; |
| 1281 | error: |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1282 | switch (ent->etype) { |
| 1283 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 1284 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 1285 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 1286 | case XML_INTERNAL_GENERAL_ENTITY: |
| 1287 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 1288 | return; |
| 1289 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 1290 | break; |
| 1291 | } |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1292 | xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH, |
| 1293 | "mismatch in redefinition of entity %s\n", |
| 1294 | ent->name); |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * xmlXIncludeMergeEntities: |
| 1299 | * @ctxt: an XInclude context |
| 1300 | * @doc: the including doc |
| 1301 | * @from: the included doc |
| 1302 | * |
| 1303 | * Inplements the entity merge |
| 1304 | * |
| 1305 | * Returns 0 if merge succeeded, -1 if some processing failed |
| 1306 | */ |
| 1307 | static int |
| 1308 | xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, |
| 1309 | xmlDocPtr from) { |
| 1310 | xmlNodePtr cur; |
| 1311 | xmlDtdPtr target, source; |
| 1312 | |
| 1313 | if (ctxt == NULL) |
| 1314 | return(-1); |
| 1315 | |
| 1316 | if ((from == NULL) || (from->intSubset == NULL)) |
| 1317 | return(0); |
| 1318 | |
| 1319 | target = doc->intSubset; |
| 1320 | if (target == NULL) { |
| 1321 | cur = xmlDocGetRootElement(doc); |
| 1322 | if (cur == NULL) |
| 1323 | return(-1); |
| 1324 | target = xmlCreateIntSubset(doc, cur->name, NULL, NULL); |
| 1325 | if (target == NULL) |
| 1326 | return(-1); |
| 1327 | } |
| 1328 | |
| 1329 | source = from->intSubset; |
| 1330 | if ((source != NULL) && (source->entities != NULL)) { |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1331 | xmlXIncludeMergeData data; |
| 1332 | |
| 1333 | data.ctxt = ctxt; |
| 1334 | data.doc = doc; |
| 1335 | |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1336 | xmlHashScan((xmlHashTablePtr) source->entities, |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1337 | (xmlHashScanner) xmlXIncludeMergeEntity, &data); |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1338 | } |
| 1339 | source = from->extSubset; |
| 1340 | if ((source != NULL) && (source->entities != NULL)) { |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1341 | xmlXIncludeMergeData data; |
| 1342 | |
| 1343 | data.ctxt = ctxt; |
| 1344 | data.doc = doc; |
| 1345 | |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1346 | /* |
| 1347 | * don't duplicate existing stuff when external subsets are the same |
| 1348 | */ |
| 1349 | if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) && |
| 1350 | (!xmlStrEqual(target->SystemID, source->SystemID))) { |
| 1351 | xmlHashScan((xmlHashTablePtr) source->entities, |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1352 | (xmlHashScanner) xmlXIncludeMergeEntity, &data); |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | return(0); |
| 1356 | } |
| 1357 | |
| 1358 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1359 | * xmlXIncludeLoadDoc: |
| 1360 | * @ctxt: the XInclude context |
| 1361 | * @url: the associated URL |
| 1362 | * @nr: the xinclude node number |
| 1363 | * |
| 1364 | * Load the document, and store the result in the XInclude context |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1365 | * |
| 1366 | * Returns 0 in case of success, -1 in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1367 | */ |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1368 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1369 | xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { |
| 1370 | xmlDocPtr doc; |
| 1371 | xmlURIPtr uri; |
| 1372 | xmlChar *URL; |
| 1373 | xmlChar *fragment = NULL; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1374 | int i = 0; |
William M. Brack | 4d59e22 | 2004-03-08 14:42:31 +0000 | [diff] [blame] | 1375 | #ifdef LIBXML_XPTR_ENABLED |
| 1376 | int saveFlags; |
| 1377 | #endif |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1378 | |
| 1379 | #ifdef DEBUG_XINCLUDE |
| 1380 | xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr); |
| 1381 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1382 | /* |
| 1383 | * Check the URL and remove any fragment identifier |
| 1384 | */ |
| 1385 | uri = xmlParseURI((const char *)url); |
| 1386 | if (uri == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1387 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1388 | XML_XINCLUDE_HREF_URI, |
| 1389 | "invalid value URI %s\n", url); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1390 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1391 | } |
| 1392 | if (uri->fragment != NULL) { |
| 1393 | fragment = (xmlChar *) uri->fragment; |
| 1394 | uri->fragment = NULL; |
| 1395 | } |
Daniel Veillard | b98d082 | 2003-12-24 11:06:25 +0000 | [diff] [blame] | 1396 | if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) && |
| 1397 | (ctxt->incTab[nr]->fragment != NULL)) { |
| 1398 | if (fragment != NULL) xmlFree(fragment); |
| 1399 | fragment = xmlStrdup(ctxt->incTab[nr]->fragment); |
| 1400 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1401 | URL = xmlSaveUri(uri); |
| 1402 | xmlFreeURI(uri); |
| 1403 | if (URL == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1404 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1405 | XML_XINCLUDE_HREF_URI, |
| 1406 | "invalid value URI %s\n", url); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1407 | if (fragment != NULL) |
| 1408 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1409 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | /* |
| 1413 | * Handling of references to the local document are done |
| 1414 | * directly through ctxt->doc. |
| 1415 | */ |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1416 | if ((URL[0] == 0) || (URL[0] == '#') || |
| 1417 | ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1418 | doc = NULL; |
| 1419 | goto loaded; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * Prevent reloading twice the document. |
| 1424 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1425 | for (i = 0; i < ctxt->incNr; i++) { |
| 1426 | if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) && |
| 1427 | (ctxt->incTab[i]->doc != NULL)) { |
| 1428 | doc = ctxt->incTab[i]->doc; |
| 1429 | #ifdef DEBUG_XINCLUDE |
| 1430 | printf("Already loaded %s\n", URL); |
| 1431 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1432 | goto loaded; |
| 1433 | } |
| 1434 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1435 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1436 | /* |
| 1437 | * Load it. |
| 1438 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1439 | #ifdef DEBUG_XINCLUDE |
| 1440 | printf("loading %s\n", URL); |
| 1441 | #endif |
William M. Brack | 4d59e22 | 2004-03-08 14:42:31 +0000 | [diff] [blame] | 1442 | #ifdef LIBXML_XPTR_ENABLED |
| 1443 | /* |
| 1444 | * If this is an XPointer evaluation, we want to assure that |
| 1445 | * all entities have been resolved prior to processing the |
| 1446 | * referenced document |
| 1447 | */ |
| 1448 | saveFlags = ctxt->parseFlags; |
| 1449 | if (fragment != NULL) { /* if this is an XPointer eval */ |
| 1450 | ctxt->parseFlags |= XML_PARSE_NOENT; |
| 1451 | } |
| 1452 | #endif |
| 1453 | |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 1454 | doc = xmlXIncludeParseFile(ctxt, (const char *)URL); |
William M. Brack | 4d59e22 | 2004-03-08 14:42:31 +0000 | [diff] [blame] | 1455 | #ifdef LIBXML_XPTR_ENABLED |
| 1456 | ctxt->parseFlags = saveFlags; |
| 1457 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1458 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1459 | xmlFree(URL); |
| 1460 | if (fragment != NULL) |
| 1461 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1462 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1463 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1464 | ctxt->incTab[nr]->doc = doc; |
William M. Brack | b85c920 | 2004-07-26 00:20:13 +0000 | [diff] [blame] | 1465 | /* |
| 1466 | * It's possible that the requested URL has been mapped to a |
| 1467 | * completely different location (e.g. through a catalog entry). |
| 1468 | * To check for this, we compare the URL with that of the doc |
| 1469 | * and change it if they disagree (bug 146988). |
| 1470 | */ |
| 1471 | if (!xmlStrEqual(URL, doc->URL)) { |
| 1472 | xmlFree(URL); |
| 1473 | URL = xmlStrdup(doc->URL); |
| 1474 | } |
Daniel Veillard | 9848532 | 2003-08-14 15:44:40 +0000 | [diff] [blame] | 1475 | for (i = nr + 1; i < ctxt->incNr; i++) { |
| 1476 | if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) { |
| 1477 | ctxt->incTab[nr]->count++; |
| 1478 | #ifdef DEBUG_XINCLUDE |
| 1479 | printf("Increasing %s count since reused\n", URL); |
| 1480 | #endif |
| 1481 | break; |
| 1482 | } |
| 1483 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1484 | |
| 1485 | /* |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1486 | * Make sure we have all entities fixed up |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1487 | */ |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1488 | xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1489 | |
| 1490 | /* |
| 1491 | * We don't need the DTD anymore, free up space |
| 1492 | if (doc->intSubset != NULL) { |
| 1493 | xmlUnlinkNode((xmlNodePtr) doc->intSubset); |
| 1494 | xmlFreeNode((xmlNodePtr) doc->intSubset); |
| 1495 | doc->intSubset = NULL; |
| 1496 | } |
| 1497 | if (doc->extSubset != NULL) { |
| 1498 | xmlUnlinkNode((xmlNodePtr) doc->extSubset); |
| 1499 | xmlFreeNode((xmlNodePtr) doc->extSubset); |
| 1500 | doc->extSubset = NULL; |
| 1501 | } |
| 1502 | */ |
| 1503 | xmlXIncludeRecurseDoc(ctxt, doc, URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1504 | |
| 1505 | loaded: |
| 1506 | if (fragment == NULL) { |
| 1507 | /* |
| 1508 | * Add the top children list as the replacement copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1509 | */ |
| 1510 | if (doc == NULL) |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 1511 | { |
| 1512 | /* Hopefully a DTD declaration won't be copied from |
| 1513 | * the same document */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1514 | ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children); |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 1515 | } else { |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1516 | ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc, |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1517 | doc, doc->children); |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 1518 | } |
Daniel Veillard | 10acc2f | 2003-09-01 20:59:40 +0000 | [diff] [blame] | 1519 | } |
| 1520 | #ifdef LIBXML_XPTR_ENABLED |
| 1521 | else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1522 | /* |
| 1523 | * Computes the XPointer expression and make a copy used |
| 1524 | * as the replacement copy. |
| 1525 | */ |
| 1526 | xmlXPathObjectPtr xptr; |
| 1527 | xmlXPathContextPtr xptrctxt; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1528 | xmlNodeSetPtr set; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1529 | |
| 1530 | if (doc == NULL) { |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1531 | xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref, |
| 1532 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1533 | } else { |
| 1534 | xptrctxt = xmlXPtrNewContext(doc, NULL, NULL); |
| 1535 | } |
| 1536 | if (xptrctxt == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1537 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1538 | XML_XINCLUDE_XPTR_FAILED, |
Daniel Veillard | a152c4d | 2003-11-19 16:24:26 +0000 | [diff] [blame] | 1539 | "could not create XPointer context\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1540 | xmlFree(URL); |
| 1541 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1542 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1543 | } |
| 1544 | xptr = xmlXPtrEval(fragment, xptrctxt); |
| 1545 | if (xptr == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1546 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1547 | XML_XINCLUDE_XPTR_FAILED, |
| 1548 | "XPointer evaluation failed: #%s\n", |
| 1549 | fragment); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1550 | xmlXPathFreeContext(xptrctxt); |
| 1551 | xmlFree(URL); |
| 1552 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1553 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1554 | } |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1555 | switch (xptr->type) { |
| 1556 | case XPATH_UNDEFINED: |
| 1557 | case XPATH_BOOLEAN: |
| 1558 | case XPATH_NUMBER: |
| 1559 | case XPATH_STRING: |
| 1560 | case XPATH_POINT: |
| 1561 | case XPATH_USERS: |
| 1562 | case XPATH_XSLT_TREE: |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1563 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1564 | XML_XINCLUDE_XPTR_RESULT, |
| 1565 | "XPointer is not a range: #%s\n", |
| 1566 | fragment); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1567 | xmlXPathFreeContext(xptrctxt); |
| 1568 | xmlFree(URL); |
| 1569 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1570 | return(-1); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1571 | case XPATH_NODESET: |
Daniel Veillard | 798ae54 | 2003-11-03 17:13:52 +0000 | [diff] [blame] | 1572 | if ((xptr->nodesetval == NULL) || |
| 1573 | (xptr->nodesetval->nodeNr <= 0)) { |
| 1574 | xmlXPathFreeContext(xptrctxt); |
| 1575 | xmlFree(URL); |
| 1576 | xmlFree(fragment); |
| 1577 | return(-1); |
| 1578 | } |
William M. Brack | abf598b | 2004-06-08 02:01:28 +0000 | [diff] [blame] | 1579 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1580 | case XPATH_RANGE: |
| 1581 | case XPATH_LOCATIONSET: |
| 1582 | break; |
| 1583 | } |
| 1584 | set = xptr->nodesetval; |
| 1585 | if (set != NULL) { |
| 1586 | for (i = 0;i < set->nodeNr;i++) { |
| 1587 | if (set->nodeTab[i] == NULL) |
| 1588 | continue; |
| 1589 | switch (set->nodeTab[i]->type) { |
| 1590 | case XML_TEXT_NODE: |
| 1591 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1592 | case XML_ENTITY_REF_NODE: |
| 1593 | case XML_ENTITY_NODE: |
| 1594 | case XML_PI_NODE: |
| 1595 | case XML_COMMENT_NODE: |
| 1596 | case XML_DOCUMENT_NODE: |
| 1597 | case XML_HTML_DOCUMENT_NODE: |
| 1598 | #ifdef LIBXML_DOCB_ENABLED |
| 1599 | case XML_DOCB_DOCUMENT_NODE: |
| 1600 | #endif |
| 1601 | continue; |
William M. Brack | abf598b | 2004-06-08 02:01:28 +0000 | [diff] [blame] | 1602 | case XML_ELEMENT_NODE: { |
| 1603 | xmlChar *nodeBase; |
| 1604 | xmlNodePtr el = set->nodeTab[i]; |
| 1605 | |
| 1606 | nodeBase = xmlNodeGetBase(el->doc, el); |
| 1607 | if (nodeBase != NULL) { |
| 1608 | if (!xmlStrEqual(nodeBase, el->doc->URL)) |
| 1609 | xmlNodeSetBase(el, nodeBase); |
| 1610 | xmlFree(nodeBase); |
| 1611 | } |
| 1612 | continue; |
| 1613 | } |
| 1614 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1615 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1616 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1617 | XML_XINCLUDE_XPTR_RESULT, |
| 1618 | "XPointer selects an attribute: #%s\n", |
| 1619 | fragment); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1620 | set->nodeTab[i] = NULL; |
| 1621 | continue; |
| 1622 | case XML_NAMESPACE_DECL: |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1623 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1624 | XML_XINCLUDE_XPTR_RESULT, |
| 1625 | "XPointer selects a namespace: #%s\n", |
| 1626 | fragment); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1627 | set->nodeTab[i] = NULL; |
| 1628 | continue; |
| 1629 | case XML_DOCUMENT_TYPE_NODE: |
| 1630 | case XML_DOCUMENT_FRAG_NODE: |
| 1631 | case XML_NOTATION_NODE: |
| 1632 | case XML_DTD_NODE: |
| 1633 | case XML_ELEMENT_DECL: |
| 1634 | case XML_ATTRIBUTE_DECL: |
| 1635 | case XML_ENTITY_DECL: |
| 1636 | case XML_XINCLUDE_START: |
| 1637 | case XML_XINCLUDE_END: |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1638 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1639 | XML_XINCLUDE_XPTR_RESULT, |
| 1640 | "XPointer selects unexpected nodes: #%s\n", |
| 1641 | fragment); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1642 | set->nodeTab[i] = NULL; |
| 1643 | set->nodeTab[i] = NULL; |
| 1644 | continue; /* for */ |
| 1645 | } |
| 1646 | } |
| 1647 | } |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1648 | if (doc == NULL) { |
| 1649 | ctxt->incTab[nr]->xptr = xptr; |
| 1650 | ctxt->incTab[nr]->inc = NULL; |
| 1651 | } else { |
| 1652 | ctxt->incTab[nr]->inc = |
| 1653 | xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr); |
| 1654 | xmlXPathFreeObject(xptr); |
| 1655 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1656 | xmlXPathFreeContext(xptrctxt); |
| 1657 | xmlFree(fragment); |
| 1658 | } |
Daniel Veillard | 10acc2f | 2003-09-01 20:59:40 +0000 | [diff] [blame] | 1659 | #endif |
Daniel Veillard | c4bad4a | 2002-08-14 14:45:25 +0000 | [diff] [blame] | 1660 | |
| 1661 | /* |
| 1662 | * Do the xml:base fixup if needed |
| 1663 | */ |
| 1664 | if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) { |
| 1665 | xmlNodePtr node; |
William M. Brack | 0b13a09 | 2005-06-01 03:37:59 +0000 | [diff] [blame] | 1666 | xmlChar *base; |
William M. Brack | abf598b | 2004-06-08 02:01:28 +0000 | [diff] [blame] | 1667 | xmlChar *curBase; |
Daniel Veillard | c4bad4a | 2002-08-14 14:45:25 +0000 | [diff] [blame] | 1668 | |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 1669 | /* |
William M. Brack | 0b13a09 | 2005-06-01 03:37:59 +0000 | [diff] [blame] | 1670 | * The base is only adjusted if "necessary", i.e. if the xinclude node |
| 1671 | * has a base specified, or the URL is relative |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 1672 | */ |
William M. Brack | 0b13a09 | 2005-06-01 03:37:59 +0000 | [diff] [blame] | 1673 | base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base", |
| 1674 | XML_XML_NAMESPACE); |
| 1675 | if (base == NULL) { |
| 1676 | /* |
| 1677 | * No xml:base on the xinclude node, so we check whether the |
| 1678 | * URI base is different than (relative to) the context base |
| 1679 | */ |
| 1680 | curBase = xmlBuildRelativeURI(URL, ctxt->base); |
| 1681 | if (curBase == NULL) { /* Error return */ |
| 1682 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 1683 | XML_XINCLUDE_HREF_URI, |
| 1684 | "trying to build relative URI from %s\n", URL); |
William M. Brack | 0b13a09 | 2005-06-01 03:37:59 +0000 | [diff] [blame] | 1685 | } else { |
| 1686 | /* If the URI doesn't contain a slash, it's not relative */ |
| 1687 | if (!xmlStrchr(curBase, (xmlChar) '/')) |
| 1688 | xmlFree(curBase); |
| 1689 | else |
| 1690 | base = curBase; |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 1691 | } |
William M. Brack | 0b13a09 | 2005-06-01 03:37:59 +0000 | [diff] [blame] | 1692 | } |
| 1693 | if (base != NULL) { /* Adjustment may be needed */ |
| 1694 | node = ctxt->incTab[nr]->inc; |
| 1695 | while (node != NULL) { |
| 1696 | /* Only work on element nodes */ |
| 1697 | if (node->type == XML_ELEMENT_NODE) { |
| 1698 | curBase = xmlNodeGetBase(node->doc, node); |
| 1699 | /* If no current base, set it */ |
| 1700 | if (curBase == NULL) { |
| 1701 | xmlNodeSetBase(node, base); |
| 1702 | } else { |
| 1703 | /* |
| 1704 | * If the current base is the same as the |
| 1705 | * URL of the document, then reset it to be |
| 1706 | * the specified xml:base or the relative URI |
| 1707 | */ |
| 1708 | if (xmlStrEqual(curBase, node->doc->URL)) { |
| 1709 | xmlNodeSetBase(node, base); |
| 1710 | } else { |
| 1711 | /* |
| 1712 | * If the element already has an xml:base |
| 1713 | * set, then relativise it if necessary |
| 1714 | */ |
| 1715 | xmlChar *xmlBase; |
| 1716 | xmlBase = xmlGetNsProp(node, |
| 1717 | BAD_CAST "base", |
| 1718 | XML_XML_NAMESPACE); |
| 1719 | if (xmlBase != NULL) { |
| 1720 | xmlChar *relBase; |
| 1721 | relBase = xmlBuildURI(xmlBase, base); |
| 1722 | if (relBase == NULL) { /* error */ |
| 1723 | xmlXIncludeErr(ctxt, |
| 1724 | ctxt->incTab[nr]->ref, |
| 1725 | XML_XINCLUDE_HREF_URI, |
| 1726 | "trying to rebuild base from %s\n", |
| 1727 | xmlBase); |
| 1728 | } else { |
| 1729 | xmlNodeSetBase(node, relBase); |
| 1730 | xmlFree(relBase); |
| 1731 | } |
| 1732 | xmlFree(xmlBase); |
| 1733 | } |
| 1734 | } |
| 1735 | xmlFree(curBase); |
| 1736 | } |
| 1737 | } |
| 1738 | node = node->next; |
| 1739 | } |
| 1740 | xmlFree(base); |
Daniel Veillard | c4bad4a | 2002-08-14 14:45:25 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1743 | if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) && |
| 1744 | (ctxt->incTab[nr]->count <= 1)) { |
| 1745 | #ifdef DEBUG_XINCLUDE |
| 1746 | printf("freeing %s\n", ctxt->incTab[nr]->doc->URL); |
| 1747 | #endif |
| 1748 | xmlFreeDoc(ctxt->incTab[nr]->doc); |
| 1749 | ctxt->incTab[nr]->doc = NULL; |
| 1750 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1751 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1752 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | /** |
| 1756 | * xmlXIncludeLoadTxt: |
| 1757 | * @ctxt: the XInclude context |
| 1758 | * @url: the associated URL |
| 1759 | * @nr: the xinclude node number |
| 1760 | * |
| 1761 | * Load the content, and store the result in the XInclude context |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1762 | * |
| 1763 | * Returns 0 in case of success, -1 in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1764 | */ |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1765 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1766 | xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { |
| 1767 | xmlParserInputBufferPtr buf; |
| 1768 | xmlNodePtr node; |
| 1769 | xmlURIPtr uri; |
| 1770 | xmlChar *URL; |
| 1771 | int i; |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1772 | xmlChar *encoding = NULL; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1773 | xmlCharEncoding enc = (xmlCharEncoding) 0; |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1774 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1775 | /* |
| 1776 | * Check the URL and remove any fragment identifier |
| 1777 | */ |
| 1778 | uri = xmlParseURI((const char *)url); |
| 1779 | if (uri == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1780 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI, |
| 1781 | "invalid value URI %s\n", url); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1782 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1783 | } |
| 1784 | if (uri->fragment != NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1785 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT, |
| 1786 | "fragment identifier forbidden for text: %s\n", |
| 1787 | (const xmlChar *) uri->fragment); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1788 | xmlFreeURI(uri); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1789 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1790 | } |
| 1791 | URL = xmlSaveUri(uri); |
| 1792 | xmlFreeURI(uri); |
| 1793 | if (URL == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1794 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI, |
| 1795 | "invalid value URI %s\n", url); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1796 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * Handling of references to the local document are done |
| 1801 | * directly through ctxt->doc. |
| 1802 | */ |
| 1803 | if (URL[0] == 0) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1804 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1805 | XML_XINCLUDE_TEXT_DOCUMENT, |
| 1806 | "text serialization of document not available\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1807 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1808 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /* |
| 1812 | * Prevent reloading twice the document. |
| 1813 | */ |
| 1814 | for (i = 0; i < ctxt->txtNr; i++) { |
| 1815 | if (xmlStrEqual(URL, ctxt->txturlTab[i])) { |
| 1816 | node = xmlCopyNode(ctxt->txtTab[i], 1); |
| 1817 | goto loaded; |
| 1818 | } |
| 1819 | } |
| 1820 | /* |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1821 | * Try to get the encoding if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1822 | */ |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1823 | if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) { |
| 1824 | encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING); |
| 1825 | } |
| 1826 | if (encoding != NULL) { |
| 1827 | /* |
| 1828 | * TODO: we should not have to remap to the xmlCharEncoding |
| 1829 | * predefined set, a better interface than |
| 1830 | * xmlParserInputBufferCreateFilename should allow any |
| 1831 | * encoding supported by iconv |
| 1832 | */ |
| 1833 | enc = xmlParseCharEncoding((const char *) encoding); |
| 1834 | if (enc == XML_CHAR_ENCODING_ERROR) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1835 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1836 | XML_XINCLUDE_UNKNOWN_ENCODING, |
| 1837 | "encoding %s not supported\n", encoding); |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1838 | xmlFree(encoding); |
| 1839 | xmlFree(URL); |
| 1840 | return(-1); |
| 1841 | } |
| 1842 | xmlFree(encoding); |
| 1843 | } |
| 1844 | |
| 1845 | /* |
| 1846 | * Load it. |
| 1847 | */ |
| 1848 | buf = xmlParserInputBufferCreateFilename((const char *)URL, enc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1849 | if (buf == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1850 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1851 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1852 | } |
| 1853 | node = xmlNewText(NULL); |
| 1854 | |
| 1855 | /* |
| 1856 | * Scan all chars from the resource and add the to the node |
| 1857 | */ |
| 1858 | while (xmlParserInputBufferRead(buf, 128) > 0) { |
| 1859 | int len; |
| 1860 | const xmlChar *content; |
| 1861 | |
| 1862 | content = xmlBufferContent(buf->buffer); |
| 1863 | len = xmlBufferLength(buf->buffer); |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1864 | for (i = 0;i < len;) { |
| 1865 | int cur; |
| 1866 | int l; |
| 1867 | |
| 1868 | cur = xmlStringCurrentChar(NULL, &content[i], &l); |
| 1869 | if (!IS_CHAR(cur)) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 1870 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 1871 | XML_XINCLUDE_INVALID_CHAR, |
| 1872 | "%s contains invalid char\n", URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1873 | } else { |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1874 | xmlNodeAddContentLen(node, &content[i], l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1875 | } |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1876 | i += l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1877 | } |
| 1878 | xmlBufferShrink(buf->buffer, len); |
| 1879 | } |
| 1880 | xmlFreeParserInputBuffer(buf); |
| 1881 | xmlXIncludeAddTxt(ctxt, node, URL); |
| 1882 | |
| 1883 | loaded: |
| 1884 | /* |
| 1885 | * Add the element as the replacement copy. |
| 1886 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1887 | ctxt->incTab[nr]->inc = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1888 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1889 | return(0); |
| 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | * xmlXIncludeLoadFallback: |
| 1894 | * @ctxt: the XInclude context |
| 1895 | * @fallback: the fallback node |
| 1896 | * @nr: the xinclude node number |
| 1897 | * |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1898 | * Load the content of the fallback node, and store the result |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1899 | * in the XInclude context |
| 1900 | * |
| 1901 | * Returns 0 in case of success, -1 in case of failure |
| 1902 | */ |
| 1903 | static int |
| 1904 | xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) { |
William M. Brack | aae1052 | 2004-01-02 14:59:41 +0000 | [diff] [blame] | 1905 | xmlXIncludeCtxtPtr newctxt; |
| 1906 | int ret = 0; |
| 1907 | |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1908 | if ((fallback == NULL) || (ctxt == NULL)) |
| 1909 | return(-1); |
William M. Brack | ef245fd | 2004-02-06 09:33:59 +0000 | [diff] [blame] | 1910 | if (fallback->children != NULL) { |
| 1911 | /* |
| 1912 | * It's possible that the fallback also has 'includes' |
| 1913 | * (Bug 129969), so we re-process the fallback just in case |
| 1914 | */ |
| 1915 | newctxt = xmlXIncludeNewContext(ctxt->doc); |
| 1916 | if (newctxt == NULL) |
| 1917 | return (-1); |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 1918 | newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */ |
William M. Brack | ef245fd | 2004-02-06 09:33:59 +0000 | [diff] [blame] | 1919 | xmlXIncludeSetFlags(newctxt, ctxt->parseFlags); |
| 1920 | ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children); |
William M. Brack | 87640d5 | 2004-04-17 14:58:15 +0000 | [diff] [blame] | 1921 | if (ctxt->nbErrors > 0) |
William M. Brack | ef245fd | 2004-02-06 09:33:59 +0000 | [diff] [blame] | 1922 | ret = -1; |
William M. Brack | 87640d5 | 2004-04-17 14:58:15 +0000 | [diff] [blame] | 1923 | else if (ret > 0) |
| 1924 | ret = 0; /* xmlXIncludeDoProcess can return +ve number */ |
William M. Brack | ef245fd | 2004-02-06 09:33:59 +0000 | [diff] [blame] | 1925 | xmlXIncludeFreeContext(newctxt); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1926 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 1927 | ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc, |
| 1928 | fallback->children); |
William M. Brack | ef245fd | 2004-02-06 09:33:59 +0000 | [diff] [blame] | 1929 | } else { |
| 1930 | ctxt->incTab[nr]->inc = NULL; |
William M. Brack | 95af594 | 2004-02-08 04:12:49 +0000 | [diff] [blame] | 1931 | ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */ |
William M. Brack | ef245fd | 2004-02-06 09:33:59 +0000 | [diff] [blame] | 1932 | } |
William M. Brack | aae1052 | 2004-01-02 14:59:41 +0000 | [diff] [blame] | 1933 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | /************************************************************************ |
| 1937 | * * |
| 1938 | * XInclude Processing * |
| 1939 | * * |
| 1940 | ************************************************************************/ |
| 1941 | |
| 1942 | /** |
| 1943 | * xmlXIncludePreProcessNode: |
| 1944 | * @ctxt: an XInclude context |
| 1945 | * @node: an XInclude node |
| 1946 | * |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 1947 | * Implement the XInclude preprocessing, currently just adding the element |
| 1948 | * for further processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1949 | * |
| 1950 | * Returns the result list or NULL in case of error |
| 1951 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1952 | static xmlNodePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1953 | xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { |
| 1954 | xmlXIncludeAddNode(ctxt, node); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1955 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1958 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1959 | * xmlXIncludeLoadNode: |
| 1960 | * @ctxt: an XInclude context |
| 1961 | * @nr: the node number |
| 1962 | * |
| 1963 | * Find and load the infoset replacement for the given node. |
| 1964 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1965 | * Returns 0 if substitution succeeded, -1 if some processing failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1966 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1967 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1968 | xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) { |
| 1969 | xmlNodePtr cur; |
| 1970 | xmlChar *href; |
| 1971 | xmlChar *parse; |
| 1972 | xmlChar *base; |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 1973 | xmlChar *oldBase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1974 | xmlChar *URI; |
| 1975 | int xml = 1; /* default Issue 64 */ |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1976 | int ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1977 | |
| 1978 | if (ctxt == NULL) |
| 1979 | return(-1); |
| 1980 | if ((nr < 0) || (nr >= ctxt->incNr)) |
| 1981 | return(-1); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1982 | cur = ctxt->incTab[nr]->ref; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1983 | if (cur == NULL) |
| 1984 | return(-1); |
| 1985 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1986 | /* |
| 1987 | * read the attributes |
| 1988 | */ |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 1989 | href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1990 | if (href == NULL) { |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 1991 | href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */ |
| 1992 | if (href == NULL) |
| 1993 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1994 | } |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 1995 | parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1996 | if (parse != NULL) { |
| 1997 | if (xmlStrEqual(parse, XINCLUDE_PARSE_XML)) |
| 1998 | xml = 1; |
| 1999 | else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) |
| 2000 | xml = 0; |
| 2001 | else { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 2002 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 2003 | XML_XINCLUDE_PARSE_VALUE, |
| 2004 | "invalid value %s for 'parse'\n", parse); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2005 | if (href != NULL) |
| 2006 | xmlFree(href); |
| 2007 | if (parse != NULL) |
| 2008 | xmlFree(parse); |
| 2009 | return(-1); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | /* |
| 2014 | * compute the URI |
| 2015 | */ |
| 2016 | base = xmlNodeGetBase(ctxt->doc, cur); |
| 2017 | if (base == NULL) { |
| 2018 | URI = xmlBuildURI(href, ctxt->doc->URL); |
| 2019 | } else { |
| 2020 | URI = xmlBuildURI(href, base); |
| 2021 | } |
| 2022 | if (URI == NULL) { |
| 2023 | xmlChar *escbase; |
| 2024 | xmlChar *eschref; |
| 2025 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2026 | * Some escaping may be needed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2027 | */ |
| 2028 | escbase = xmlURIEscape(base); |
| 2029 | eschref = xmlURIEscape(href); |
| 2030 | URI = xmlBuildURI(eschref, escbase); |
| 2031 | if (escbase != NULL) |
| 2032 | xmlFree(escbase); |
| 2033 | if (eschref != NULL) |
| 2034 | xmlFree(eschref); |
| 2035 | } |
| 2036 | if (URI == NULL) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 2037 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 2038 | XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2039 | if (parse != NULL) |
| 2040 | xmlFree(parse); |
| 2041 | if (href != NULL) |
| 2042 | xmlFree(href); |
| 2043 | if (base != NULL) |
| 2044 | xmlFree(base); |
| 2045 | return(-1); |
| 2046 | } |
| 2047 | #ifdef DEBUG_XINCLUDE |
| 2048 | xmlGenericError(xmlGenericErrorContext, "parse: %s\n", |
| 2049 | xml ? "xml": "text"); |
| 2050 | xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI); |
| 2051 | #endif |
| 2052 | |
| 2053 | /* |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 2054 | * Save the base for this include (saving the current one) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2055 | */ |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 2056 | oldBase = ctxt->base; |
| 2057 | ctxt->base = base; |
| 2058 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2059 | if (xml) { |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 2060 | ret = xmlXIncludeLoadDoc(ctxt, URI, nr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2061 | /* xmlXIncludeGetFragment(ctxt, cur, URI); */ |
| 2062 | } else { |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 2063 | ret = xmlXIncludeLoadTxt(ctxt, URI, nr); |
| 2064 | } |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 2065 | |
| 2066 | /* |
| 2067 | * Restore the original base before checking for fallback |
| 2068 | */ |
| 2069 | ctxt->base = oldBase; |
| 2070 | |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 2071 | if (ret < 0) { |
| 2072 | xmlNodePtr children; |
| 2073 | |
| 2074 | /* |
| 2075 | * Time to try a fallback if availble |
| 2076 | */ |
| 2077 | #ifdef DEBUG_XINCLUDE |
| 2078 | xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n"); |
| 2079 | #endif |
| 2080 | children = cur->children; |
| 2081 | while (children != NULL) { |
| 2082 | if ((children->type == XML_ELEMENT_NODE) && |
| 2083 | (children->ns != NULL) && |
| 2084 | (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) && |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 2085 | ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) || |
| 2086 | (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) { |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 2087 | ret = xmlXIncludeLoadFallback(ctxt, children, nr); |
William M. Brack | 1ff4213 | 2003-12-31 14:05:15 +0000 | [diff] [blame] | 2088 | if (ret == 0) |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 2089 | break; |
| 2090 | } |
| 2091 | children = children->next; |
| 2092 | } |
| 2093 | } |
| 2094 | if (ret < 0) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 2095 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 2096 | XML_XINCLUDE_NO_FALLBACK, |
| 2097 | "could not load %s, and no fallback was found\n", |
| 2098 | URI); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | /* |
| 2102 | * Cleanup |
| 2103 | */ |
| 2104 | if (URI != NULL) |
| 2105 | xmlFree(URI); |
| 2106 | if (parse != NULL) |
| 2107 | xmlFree(parse); |
| 2108 | if (href != NULL) |
| 2109 | xmlFree(href); |
| 2110 | if (base != NULL) |
| 2111 | xmlFree(base); |
| 2112 | return(0); |
| 2113 | } |
| 2114 | |
| 2115 | /** |
| 2116 | * xmlXIncludeIncludeNode: |
| 2117 | * @ctxt: an XInclude context |
| 2118 | * @nr: the node number |
| 2119 | * |
| 2120 | * Inplement the infoset replacement for the given node |
| 2121 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2122 | * Returns 0 if substitution succeeded, -1 if some processing failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2123 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2124 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2125 | xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) { |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2126 | xmlNodePtr cur, end, list, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2127 | |
| 2128 | if (ctxt == NULL) |
| 2129 | return(-1); |
| 2130 | if ((nr < 0) || (nr >= ctxt->incNr)) |
| 2131 | return(-1); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 2132 | cur = ctxt->incTab[nr]->ref; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2133 | if (cur == NULL) |
| 2134 | return(-1); |
| 2135 | |
| 2136 | /* |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2137 | * If we stored an XPointer a late computation may be needed |
| 2138 | */ |
| 2139 | if ((ctxt->incTab[nr]->inc == NULL) && |
| 2140 | (ctxt->incTab[nr]->xptr != NULL)) { |
| 2141 | ctxt->incTab[nr]->inc = |
| 2142 | xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc, |
| 2143 | ctxt->incTab[nr]->xptr); |
| 2144 | xmlXPathFreeObject(ctxt->incTab[nr]->xptr); |
| 2145 | ctxt->incTab[nr]->xptr = NULL; |
| 2146 | } |
| 2147 | list = ctxt->incTab[nr]->inc; |
| 2148 | ctxt->incTab[nr]->inc = NULL; |
| 2149 | |
| 2150 | /* |
| 2151 | * Check against the risk of generating a multi-rooted document |
| 2152 | */ |
| 2153 | if ((cur->parent != NULL) && |
| 2154 | (cur->parent->type != XML_ELEMENT_NODE)) { |
| 2155 | int nb_elem = 0; |
| 2156 | |
| 2157 | tmp = list; |
| 2158 | while (tmp != NULL) { |
| 2159 | if (tmp->type == XML_ELEMENT_NODE) |
| 2160 | nb_elem++; |
| 2161 | tmp = tmp->next; |
| 2162 | } |
| 2163 | if (nb_elem > 1) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 2164 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 2165 | XML_XINCLUDE_MULTIPLE_ROOT, |
| 2166 | "XInclude error: would result in multiple root nodes\n", |
| 2167 | NULL); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2168 | return(-1); |
| 2169 | } |
| 2170 | } |
| 2171 | |
Daniel Veillard | c14c389 | 2004-08-16 12:34:50 +0000 | [diff] [blame] | 2172 | if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) { |
| 2173 | /* |
| 2174 | * Add the list of nodes |
| 2175 | */ |
| 2176 | while (list != NULL) { |
| 2177 | end = list; |
| 2178 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2179 | |
Daniel Veillard | c14c389 | 2004-08-16 12:34:50 +0000 | [diff] [blame] | 2180 | xmlAddPrevSibling(cur, end); |
| 2181 | } |
| 2182 | xmlUnlinkNode(cur); |
| 2183 | xmlFreeNode(cur); |
| 2184 | } else { |
| 2185 | /* |
| 2186 | * Change the current node as an XInclude start one, and add an |
| 2187 | * XInclude end one |
| 2188 | */ |
| 2189 | cur->type = XML_XINCLUDE_START; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2190 | end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL); |
Daniel Veillard | c14c389 | 2004-08-16 12:34:50 +0000 | [diff] [blame] | 2191 | if (end == NULL) { |
| 2192 | xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, |
| 2193 | XML_XINCLUDE_BUILD_FAILED, |
| 2194 | "failed to build node\n", NULL); |
| 2195 | return(-1); |
| 2196 | } |
| 2197 | end->type = XML_XINCLUDE_END; |
| 2198 | xmlAddNextSibling(cur, end); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2199 | |
Daniel Veillard | c14c389 | 2004-08-16 12:34:50 +0000 | [diff] [blame] | 2200 | /* |
| 2201 | * Add the list of nodes |
| 2202 | */ |
| 2203 | while (list != NULL) { |
| 2204 | cur = list; |
| 2205 | list = list->next; |
| 2206 | |
| 2207 | xmlAddPrevSibling(end, cur); |
| 2208 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2209 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 2210 | |
| 2211 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2212 | return(0); |
| 2213 | } |
| 2214 | |
| 2215 | /** |
| 2216 | * xmlXIncludeTestNode: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2217 | * @ctxt: the XInclude processing context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2218 | * @node: an XInclude node |
| 2219 | * |
| 2220 | * test if the node is an XInclude node |
| 2221 | * |
| 2222 | * Returns 1 true, 0 otherwise |
| 2223 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2224 | static int |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2225 | xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2226 | if (node == NULL) |
| 2227 | return(0); |
Daniel Veillard | ffe4f5e | 2003-07-06 17:35:43 +0000 | [diff] [blame] | 2228 | if (node->type != XML_ELEMENT_NODE) |
| 2229 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2230 | if (node->ns == NULL) |
| 2231 | return(0); |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 2232 | if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) || |
| 2233 | (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) { |
| 2234 | if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) { |
| 2235 | if (ctxt->legacy == 0) { |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 2236 | #if 0 /* wait for the XML Core Working Group to get something stable ! */ |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 2237 | xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS, |
| 2238 | "Deprecated XInclude namespace found, use %s", |
| 2239 | XINCLUDE_NS); |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 2240 | #endif |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 2241 | ctxt->legacy = 1; |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 2242 | } |
| 2243 | } |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2244 | if (xmlStrEqual(node->name, XINCLUDE_NODE)) { |
| 2245 | xmlNodePtr child = node->children; |
| 2246 | int nb_fallback = 0; |
| 2247 | |
| 2248 | while (child != NULL) { |
| 2249 | if ((child->type == XML_ELEMENT_NODE) && |
| 2250 | (child->ns != NULL) && |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 2251 | ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) || |
| 2252 | (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) { |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2253 | if (xmlStrEqual(child->name, XINCLUDE_NODE)) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 2254 | xmlXIncludeErr(ctxt, node, |
| 2255 | XML_XINCLUDE_INCLUDE_IN_INCLUDE, |
| 2256 | "%s has an 'include' child\n", |
| 2257 | XINCLUDE_NODE); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2258 | return(0); |
| 2259 | } |
| 2260 | if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) { |
| 2261 | nb_fallback++; |
| 2262 | } |
| 2263 | } |
| 2264 | child = child->next; |
| 2265 | } |
| 2266 | if (nb_fallback > 1) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 2267 | xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE, |
| 2268 | "%s has multiple fallback children\n", |
| 2269 | XINCLUDE_NODE); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2270 | return(0); |
| 2271 | } |
| 2272 | return(1); |
| 2273 | } |
| 2274 | if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) { |
| 2275 | if ((node->parent == NULL) || |
| 2276 | (node->parent->type != XML_ELEMENT_NODE) || |
| 2277 | (node->parent->ns == NULL) || |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 2278 | ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) && |
| 2279 | (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) || |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2280 | (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 2281 | xmlXIncludeErr(ctxt, node, |
| 2282 | XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE, |
| 2283 | "%s is not the child of an 'include'\n", |
| 2284 | XINCLUDE_FALLBACK); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2288 | return(0); |
| 2289 | } |
| 2290 | |
| 2291 | /** |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2292 | * xmlXIncludeDoProcess: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2293 | * @ctxt: the XInclude processing context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2294 | * @doc: an XML document |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 2295 | * @tree: the top of the tree to process |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2296 | * |
| 2297 | * Implement the XInclude substitution on the XML document @doc |
| 2298 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2299 | * Returns 0 if no substitution were done, -1 if some processing failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2300 | * or the number of substitutions done. |
| 2301 | */ |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2302 | static int |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 2303 | xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2304 | xmlNodePtr cur; |
| 2305 | int ret = 0; |
Daniel Veillard | e0fd93f | 2005-08-10 13:39:10 +0000 | [diff] [blame] | 2306 | int i, start; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2307 | |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 2308 | if ((doc == NULL) || (tree == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2309 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2310 | if (ctxt == NULL) |
| 2311 | return(-1); |
| 2312 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2313 | if (doc->URL != NULL) { |
| 2314 | ret = xmlXIncludeURLPush(ctxt, doc->URL); |
| 2315 | if (ret < 0) |
| 2316 | return(-1); |
| 2317 | } |
Daniel Veillard | e0fd93f | 2005-08-10 13:39:10 +0000 | [diff] [blame] | 2318 | if (tree) |
| 2319 | start = ctxt->incNr; |
| 2320 | else |
| 2321 | start = ctxt->incBase; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2322 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2323 | /* |
| 2324 | * First phase: lookup the elements in the document |
| 2325 | */ |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 2326 | cur = tree; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2327 | if (xmlXIncludeTestNode(ctxt, cur) == 1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2328 | xmlXIncludePreProcessNode(ctxt, cur); |
William M. Brack | 7b0e276 | 2004-05-12 09:33:23 +0000 | [diff] [blame] | 2329 | while ((cur != NULL) && (cur != tree->parent)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2330 | /* TODO: need to work on entities -> stack */ |
| 2331 | if ((cur->children != NULL) && |
Daniel Veillard | ffe4f5e | 2003-07-06 17:35:43 +0000 | [diff] [blame] | 2332 | (cur->children->type != XML_ENTITY_DECL) && |
| 2333 | (cur->children->type != XML_XINCLUDE_START) && |
| 2334 | (cur->children->type != XML_XINCLUDE_END)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2335 | cur = cur->children; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2336 | if (xmlXIncludeTestNode(ctxt, cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2337 | xmlXIncludePreProcessNode(ctxt, cur); |
| 2338 | } else if (cur->next != NULL) { |
| 2339 | cur = cur->next; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2340 | if (xmlXIncludeTestNode(ctxt, cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2341 | xmlXIncludePreProcessNode(ctxt, cur); |
| 2342 | } else { |
William M. Brack | 5d8d10b | 2004-04-16 08:11:26 +0000 | [diff] [blame] | 2343 | if (cur == tree) |
| 2344 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2345 | do { |
| 2346 | cur = cur->parent; |
William M. Brack | 7b0e276 | 2004-05-12 09:33:23 +0000 | [diff] [blame] | 2347 | if ((cur == NULL) || (cur == tree->parent)) |
| 2348 | break; /* do */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2349 | if (cur->next != NULL) { |
| 2350 | cur = cur->next; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2351 | if (xmlXIncludeTestNode(ctxt, cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2352 | xmlXIncludePreProcessNode(ctxt, cur); |
| 2353 | break; /* do */ |
| 2354 | } |
| 2355 | } while (cur != NULL); |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | /* |
| 2360 | * Second Phase : collect the infosets fragments |
| 2361 | */ |
Daniel Veillard | e0fd93f | 2005-08-10 13:39:10 +0000 | [diff] [blame] | 2362 | for (i = start;i < ctxt->incNr; i++) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2363 | xmlXIncludeLoadNode(ctxt, i); |
Daniel Veillard | 97fd567 | 2003-02-07 13:01:54 +0000 | [diff] [blame] | 2364 | ret++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | /* |
| 2368 | * Third phase: extend the original document infoset. |
William M. Brack | 6b1a28d | 2004-02-06 11:24:44 +0000 | [diff] [blame] | 2369 | * |
| 2370 | * Originally we bypassed the inclusion if there were any errors |
| 2371 | * encountered on any of the XIncludes. A bug was raised (bug |
| 2372 | * 132588) requesting that we output the XIncludes without error, |
| 2373 | * so the check for inc!=NULL || xptr!=NULL was put in. This may |
| 2374 | * give some other problems in the future, but for now it seems to |
| 2375 | * work ok. |
| 2376 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2377 | */ |
William M. Brack | 6b1a28d | 2004-02-06 11:24:44 +0000 | [diff] [blame] | 2378 | for (i = ctxt->incBase;i < ctxt->incNr; i++) { |
William M. Brack | 95af594 | 2004-02-08 04:12:49 +0000 | [diff] [blame] | 2379 | if ((ctxt->incTab[i]->inc != NULL) || |
| 2380 | (ctxt->incTab[i]->xptr != NULL) || |
| 2381 | (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */ |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2382 | xmlXIncludeIncludeNode(ctxt, i); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2385 | if (doc->URL != NULL) |
| 2386 | xmlXIncludeURLPop(ctxt); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2387 | return(ret); |
| 2388 | } |
| 2389 | |
| 2390 | /** |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 2391 | * xmlXIncludeSetFlags: |
| 2392 | * @ctxt: an XInclude processing context |
| 2393 | * @flags: a set of xmlParserOption used for parsing XML includes |
| 2394 | * |
| 2395 | * Set the flags used for further processing of XML resources. |
| 2396 | * |
| 2397 | * Returns 0 in case of success and -1 in case of error. |
| 2398 | */ |
| 2399 | int |
| 2400 | xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) { |
| 2401 | if (ctxt == NULL) |
| 2402 | return(-1); |
| 2403 | ctxt->parseFlags = flags; |
| 2404 | return(0); |
| 2405 | } |
| 2406 | |
| 2407 | /** |
| 2408 | * xmlXIncludeProcessFlags: |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2409 | * @doc: an XML document |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 2410 | * @flags: a set of xmlParserOption used for parsing XML includes |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2411 | * |
| 2412 | * Implement the XInclude substitution on the XML document @doc |
| 2413 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2414 | * Returns 0 if no substitution were done, -1 if some processing failed |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2415 | * or the number of substitutions done. |
| 2416 | */ |
| 2417 | int |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 2418 | xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) { |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2419 | xmlXIncludeCtxtPtr ctxt; |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 2420 | xmlNodePtr tree; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2421 | int ret = 0; |
| 2422 | |
| 2423 | if (doc == NULL) |
| 2424 | return(-1); |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 2425 | tree = xmlDocGetRootElement(doc); |
| 2426 | if (tree == NULL) |
| 2427 | return(-1); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2428 | ctxt = xmlXIncludeNewContext(doc); |
| 2429 | if (ctxt == NULL) |
| 2430 | return(-1); |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 2431 | ctxt->base = xmlStrdup((xmlChar *)doc->URL); |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 2432 | xmlXIncludeSetFlags(ctxt, flags); |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 2433 | ret = xmlXIncludeDoProcess(ctxt, doc, tree); |
| 2434 | if ((ret >= 0) && (ctxt->nbErrors > 0)) |
| 2435 | ret = -1; |
| 2436 | |
| 2437 | xmlXIncludeFreeContext(ctxt); |
| 2438 | return(ret); |
| 2439 | } |
| 2440 | |
| 2441 | /** |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 2442 | * xmlXIncludeProcess: |
| 2443 | * @doc: an XML document |
| 2444 | * |
| 2445 | * Implement the XInclude substitution on the XML document @doc |
| 2446 | * |
| 2447 | * Returns 0 if no substitution were done, -1 if some processing failed |
| 2448 | * or the number of substitutions done. |
| 2449 | */ |
| 2450 | int |
| 2451 | xmlXIncludeProcess(xmlDocPtr doc) { |
| 2452 | return(xmlXIncludeProcessFlags(doc, 0)); |
| 2453 | } |
| 2454 | |
| 2455 | /** |
| 2456 | * xmlXIncludeProcessTreeFlags: |
| 2457 | * @tree: a node in an XML document |
| 2458 | * @flags: a set of xmlParserOption used for parsing XML includes |
| 2459 | * |
| 2460 | * Implement the XInclude substitution for the given subtree |
| 2461 | * |
| 2462 | * Returns 0 if no substitution were done, -1 if some processing failed |
| 2463 | * or the number of substitutions done. |
| 2464 | */ |
| 2465 | int |
| 2466 | xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) { |
| 2467 | xmlXIncludeCtxtPtr ctxt; |
| 2468 | int ret = 0; |
| 2469 | |
| 2470 | if ((tree == NULL) || (tree->doc == NULL)) |
| 2471 | return(-1); |
| 2472 | ctxt = xmlXIncludeNewContext(tree->doc); |
| 2473 | if (ctxt == NULL) |
| 2474 | return(-1); |
William M. Brack | f7789b1 | 2004-06-07 08:57:27 +0000 | [diff] [blame] | 2475 | ctxt->base = xmlNodeGetBase(tree->doc, tree); |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 2476 | xmlXIncludeSetFlags(ctxt, flags); |
| 2477 | ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree); |
| 2478 | if ((ret >= 0) && (ctxt->nbErrors > 0)) |
| 2479 | ret = -1; |
| 2480 | |
| 2481 | xmlXIncludeFreeContext(ctxt); |
| 2482 | return(ret); |
| 2483 | } |
| 2484 | |
| 2485 | /** |
Daniel Veillard | 8edf1c5 | 2003-07-22 20:52:14 +0000 | [diff] [blame] | 2486 | * xmlXIncludeProcessTree: |
| 2487 | * @tree: a node in an XML document |
| 2488 | * |
| 2489 | * Implement the XInclude substitution for the given subtree |
| 2490 | * |
| 2491 | * Returns 0 if no substitution were done, -1 if some processing failed |
| 2492 | * or the number of substitutions done. |
| 2493 | */ |
| 2494 | int |
| 2495 | xmlXIncludeProcessTree(xmlNodePtr tree) { |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 2496 | return(xmlXIncludeProcessTreeFlags(tree, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 2499 | /** |
| 2500 | * xmlXIncludeProcessNode: |
| 2501 | * @ctxt: an existing XInclude context |
| 2502 | * @node: a node in an XML document |
| 2503 | * |
| 2504 | * Implement the XInclude substitution for the given subtree reusing |
| 2505 | * the informations and data coming from the given context. |
| 2506 | * |
| 2507 | * Returns 0 if no substitution were done, -1 if some processing failed |
| 2508 | * or the number of substitutions done. |
| 2509 | */ |
| 2510 | int |
| 2511 | xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { |
| 2512 | int ret = 0; |
| 2513 | |
| 2514 | if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL)) |
| 2515 | return(-1); |
| 2516 | ret = xmlXIncludeDoProcess(ctxt, node->doc, node); |
| 2517 | if ((ret >= 0) && (ctxt->nbErrors > 0)) |
| 2518 | ret = -1; |
| 2519 | return(ret); |
| 2520 | } |
| 2521 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2522 | #else /* !LIBXML_XINCLUDE_ENABLED */ |
| 2523 | #endif |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 2524 | #define bottom_xinclude |
| 2525 | #include "elfgcchack.h" |