Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xinclude.c : Code to implement XInclude processing |
| 3 | * |
Daniel Veillard | bbd2245 | 2001-05-23 12:02:27 +0000 | [diff] [blame] | 4 | * World Wide Web Consortium W3C Last Call Working Draft 16 May 2001 |
| 5 | * http://www.w3.org/TR/2001/WD-xinclude-20010516/ |
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 | |
| 12 | /* |
| 13 | * TODO: compute XPointers nodesets |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 14 | * TODO: add an node intermediate API and handle recursion at this level |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 17 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 18 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 19 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <libxml/xmlmemory.h> |
| 22 | #include <libxml/tree.h> |
| 23 | #include <libxml/parser.h> |
| 24 | #include <libxml/uri.h> |
| 25 | #include <libxml/xpointer.h> |
| 26 | #include <libxml/parserInternals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 27 | #include <libxml/xmlerror.h> |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 28 | #include <libxml/encoding.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 29 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 32 | #include <libxml/xinclude.h> |
| 33 | |
Daniel Veillard | bbd2245 | 2001-05-23 12:02:27 +0000 | [diff] [blame] | 34 | #define XINCLUDE_NS (const xmlChar *) "http://www.w3.org/2001/XInclude" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 35 | #define XINCLUDE_NODE (const xmlChar *) "include" |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 36 | #define XINCLUDE_FALLBACK (const xmlChar *) "fallback" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 37 | #define XINCLUDE_HREF (const xmlChar *) "href" |
| 38 | #define XINCLUDE_PARSE (const xmlChar *) "parse" |
| 39 | #define XINCLUDE_PARSE_XML (const xmlChar *) "xml" |
| 40 | #define XINCLUDE_PARSE_TEXT (const xmlChar *) "text" |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 41 | #define XINCLUDE_PARSE_ENCODING (const xmlChar *) "encoding" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 42 | |
| 43 | /* #define DEBUG_XINCLUDE */ |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 44 | #ifdef DEBUG_XINCLUDE |
| 45 | #ifdef LIBXML_DEBUG_ENABLED |
| 46 | #include <libxml/debugXML.h> |
| 47 | #endif |
| 48 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 49 | |
| 50 | /************************************************************************ |
| 51 | * * |
| 52 | * XInclude contexts handling * |
| 53 | * * |
| 54 | ************************************************************************/ |
| 55 | |
| 56 | /* |
| 57 | * An XInclude context |
| 58 | */ |
Daniel Veillard | edac3c9 | 2001-02-26 01:36:19 +0000 | [diff] [blame] | 59 | typedef xmlChar *xmlURL; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 60 | |
| 61 | typedef struct _xmlXIncludeRef xmlXIncludeRef; |
| 62 | typedef xmlXIncludeRef *xmlXIncludeRefPtr; |
| 63 | struct _xmlXIncludeRef { |
| 64 | xmlChar *URI; /* the rully resolved resource URL */ |
| 65 | xmlChar *fragment; /* the fragment in the URI */ |
| 66 | xmlDocPtr doc; /* the parsed document */ |
| 67 | xmlNodePtr ref; /* the node making the reference in the source */ |
| 68 | xmlNodePtr inc; /* the included copy */ |
| 69 | int xml; /* xml or txt */ |
| 70 | int count; /* how many refs use that specific doc */ |
| 71 | }; |
| 72 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 73 | typedef struct _xmlXIncludeCtxt xmlXIncludeCtxt; |
| 74 | typedef xmlXIncludeCtxt *xmlXIncludeCtxtPtr; |
| 75 | struct _xmlXIncludeCtxt { |
| 76 | xmlDocPtr doc; /* the source document */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 77 | int incBase; /* the first include for this document */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 78 | int incNr; /* number of includes */ |
| 79 | int incMax; /* size of includes tab */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 80 | xmlXIncludeRefPtr *incTab; /* array of included references */ |
| 81 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 82 | int txtNr; /* number of unparsed documents */ |
| 83 | int txtMax; /* size of unparsed documents tab */ |
| 84 | xmlNodePtr *txtTab; /* array of unparsed text nodes */ |
Daniel Veillard | edac3c9 | 2001-02-26 01:36:19 +0000 | [diff] [blame] | 85 | xmlURL *txturlTab; /* array of unparsed txtuments URLs */ |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 86 | |
| 87 | int nbErrors; /* the number of errors detected */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 90 | static int |
| 91 | xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 94 | * xmlXIncludeFreeRef: |
| 95 | * @ref: the XInclude reference |
| 96 | * |
| 97 | * Free an XInclude reference |
| 98 | */ |
| 99 | static void |
| 100 | xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) { |
| 101 | if (ref == NULL) |
| 102 | return; |
| 103 | #ifdef DEBUG_XINCLUDE |
| 104 | xmlGenericError(xmlGenericErrorContext, "Freeing ref\n"); |
| 105 | #endif |
| 106 | if (ref->doc != NULL) { |
| 107 | #ifdef DEBUG_XINCLUDE |
| 108 | xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI); |
| 109 | #endif |
| 110 | xmlFreeDoc(ref->doc); |
| 111 | } |
| 112 | if (ref->URI != NULL) |
| 113 | xmlFree(ref->URI); |
| 114 | if (ref->fragment != NULL) |
| 115 | xmlFree(ref->fragment); |
| 116 | xmlFree(ref); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * xmlXIncludeNewRef: |
| 121 | * @ctxt: the XInclude context |
| 122 | * @URI: the resource URI |
| 123 | * |
| 124 | * Creates a new reference within an XInclude context |
| 125 | * |
| 126 | * Returns the new set |
| 127 | */ |
| 128 | static xmlXIncludeRefPtr |
| 129 | xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI, |
| 130 | xmlNodePtr ref) { |
| 131 | xmlXIncludeRefPtr ret; |
| 132 | |
| 133 | #ifdef DEBUG_XINCLUDE |
| 134 | xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI); |
| 135 | #endif |
| 136 | ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef)); |
| 137 | if (ret == NULL) |
| 138 | return(NULL); |
| 139 | memset(ret, 0, sizeof(xmlXIncludeRef)); |
| 140 | if (URI == NULL) |
| 141 | ret->URI = NULL; |
| 142 | else |
| 143 | ret->URI = xmlStrdup(URI); |
| 144 | ret->fragment = NULL; |
| 145 | ret->ref = ref; |
| 146 | ret->doc = 0; |
| 147 | ret->count = 0; |
| 148 | ret->xml = 0; |
| 149 | ret->inc = NULL; |
| 150 | if (ctxt->incMax == 0) { |
| 151 | ctxt->incMax = 4; |
| 152 | ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax * |
| 153 | sizeof(ctxt->incTab[0])); |
| 154 | if (ctxt->incTab == NULL) { |
| 155 | xmlGenericError(xmlGenericErrorContext, |
| 156 | "malloc failed !\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 157 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 158 | xmlXIncludeFreeRef(ret); |
| 159 | return(NULL); |
| 160 | } |
| 161 | } |
| 162 | if (ctxt->incNr >= ctxt->incMax) { |
| 163 | ctxt->incMax *= 2; |
| 164 | ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab, |
| 165 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 166 | if (ctxt->incTab == NULL) { |
| 167 | xmlGenericError(xmlGenericErrorContext, |
| 168 | "realloc failed !\n"); |
| 169 | xmlXIncludeFreeRef(ret); |
| 170 | return(NULL); |
| 171 | } |
| 172 | } |
| 173 | ctxt->incTab[ctxt->incNr++] = ret; |
| 174 | return(ret); |
| 175 | } |
| 176 | |
| 177 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 178 | * xmlXIncludeNewContext: |
| 179 | * @doc: an XML Document |
| 180 | * |
| 181 | * Creates a new XInclude context |
| 182 | * |
| 183 | * Returns the new set |
| 184 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 185 | static xmlXIncludeCtxtPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 186 | xmlXIncludeNewContext(xmlDocPtr doc) { |
| 187 | xmlXIncludeCtxtPtr ret; |
| 188 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 189 | #ifdef DEBUG_XINCLUDE |
| 190 | xmlGenericError(xmlGenericErrorContext, "New context\n"); |
| 191 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 192 | if (doc == NULL) |
| 193 | return(NULL); |
| 194 | ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt)); |
| 195 | if (ret == NULL) |
| 196 | return(NULL); |
| 197 | memset(ret, 0, sizeof(xmlXIncludeCtxt)); |
| 198 | ret->doc = doc; |
| 199 | ret->incNr = 0; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 200 | ret->incBase = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 201 | ret->incMax = 0; |
| 202 | ret->incTab = NULL; |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 203 | ret->nbErrors = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 204 | return(ret); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * xmlXIncludeFreeContext: |
| 209 | * @ctxt: the XInclude context |
| 210 | * |
| 211 | * Free an XInclude context |
| 212 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 213 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 214 | xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) { |
| 215 | int i; |
| 216 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 217 | #ifdef DEBUG_XINCLUDE |
| 218 | xmlGenericError(xmlGenericErrorContext, "Freeing context\n"); |
| 219 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 220 | if (ctxt == NULL) |
| 221 | return; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 222 | for (i = 0;i < ctxt->incNr;i++) { |
| 223 | if (ctxt->incTab[i] != NULL) |
| 224 | xmlXIncludeFreeRef(ctxt->incTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 225 | } |
| 226 | for (i = 0;i < ctxt->txtNr;i++) { |
| 227 | if (ctxt->txturlTab[i] != NULL) |
| 228 | xmlFree(ctxt->txturlTab[i]); |
| 229 | } |
| 230 | if (ctxt->incTab != NULL) |
| 231 | xmlFree(ctxt->incTab); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 232 | if (ctxt->txtTab != NULL) |
| 233 | xmlFree(ctxt->txtTab); |
| 234 | if (ctxt->txturlTab != NULL) |
| 235 | xmlFree(ctxt->txturlTab); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 236 | xmlFree(ctxt); |
| 237 | } |
| 238 | |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 239 | /** |
| 240 | * xmlXIncludeAddNode: |
| 241 | * @ctxt: the XInclude context |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 242 | * @cur: the new node |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 243 | * |
| 244 | * Add a new node to process to an XInclude context |
| 245 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 246 | static int |
| 247 | xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) { |
| 248 | xmlXIncludeRefPtr ref; |
| 249 | xmlURIPtr uri; |
| 250 | xmlChar *URL; |
| 251 | xmlChar *fragment = NULL; |
| 252 | xmlChar *href; |
| 253 | xmlChar *parse; |
| 254 | xmlChar *base; |
| 255 | xmlChar *URI; |
| 256 | int xml = 1; /* default Issue 64 */ |
| 257 | |
| 258 | |
| 259 | if (ctxt == NULL) |
| 260 | return(-1); |
| 261 | if (cur == NULL) |
| 262 | return(-1); |
| 263 | |
| 264 | #ifdef DEBUG_XINCLUDE |
| 265 | xmlGenericError(xmlGenericErrorContext, "Add node\n"); |
| 266 | #endif |
| 267 | /* |
| 268 | * read the attributes |
| 269 | */ |
| 270 | href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF); |
| 271 | if (href == NULL) { |
| 272 | href = xmlGetProp(cur, XINCLUDE_HREF); |
| 273 | if (href == NULL) { |
| 274 | xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 275 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 276 | return(-1); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 279 | parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE); |
| 280 | if (parse == NULL) { |
| 281 | parse = xmlGetProp(cur, XINCLUDE_PARSE); |
| 282 | } |
| 283 | if (parse != NULL) { |
| 284 | if (xmlStrEqual(parse, XINCLUDE_PARSE_XML)) |
| 285 | xml = 1; |
| 286 | else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) |
| 287 | xml = 0; |
| 288 | else { |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 289 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 290 | "XInclude: invalid value %s for %s\n", |
| 291 | parse, XINCLUDE_PARSE); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 292 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 293 | if (href != NULL) |
| 294 | xmlFree(href); |
| 295 | if (parse != NULL) |
| 296 | xmlFree(parse); |
| 297 | return(-1); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * compute the URI |
| 303 | */ |
| 304 | base = xmlNodeGetBase(ctxt->doc, cur); |
| 305 | if (base == NULL) { |
| 306 | URI = xmlBuildURI(href, ctxt->doc->URL); |
| 307 | } else { |
| 308 | URI = xmlBuildURI(href, base); |
| 309 | } |
| 310 | if (URI == NULL) { |
| 311 | xmlChar *escbase; |
| 312 | xmlChar *eschref; |
| 313 | /* |
| 314 | * Some escaping may be needed |
| 315 | */ |
| 316 | escbase = xmlURIEscape(base); |
| 317 | eschref = xmlURIEscape(href); |
| 318 | URI = xmlBuildURI(eschref, escbase); |
| 319 | if (escbase != NULL) |
| 320 | xmlFree(escbase); |
| 321 | if (eschref != NULL) |
| 322 | xmlFree(eschref); |
| 323 | } |
| 324 | if (parse != NULL) |
| 325 | xmlFree(parse); |
| 326 | if (href != NULL) |
| 327 | xmlFree(href); |
| 328 | if (base != NULL) |
| 329 | xmlFree(base); |
| 330 | if (URI == NULL) { |
| 331 | xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 332 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 333 | return(-1); |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * Check the URL and remove any fragment identifier |
| 338 | */ |
| 339 | uri = xmlParseURI((const char *)URI); |
| 340 | if (uri == NULL) { |
| 341 | xmlGenericError(xmlGenericErrorContext, |
| 342 | "XInclude: invalid value URI %s\n", URI); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 343 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 344 | return(-1); |
| 345 | } |
| 346 | if (uri->fragment != NULL) { |
| 347 | fragment = (xmlChar *) uri->fragment; |
| 348 | uri->fragment = NULL; |
| 349 | } |
| 350 | URL = xmlSaveUri(uri); |
| 351 | xmlFreeURI(uri); |
| 352 | xmlFree(URI); |
| 353 | if (URL == NULL) { |
| 354 | xmlGenericError(xmlGenericErrorContext, |
| 355 | "XInclude: invalid value URI %s\n", URI); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 356 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 357 | if (fragment != NULL) |
| 358 | xmlFree(fragment); |
| 359 | return(-1); |
| 360 | } |
| 361 | |
| 362 | ref = xmlXIncludeNewRef(ctxt, URL, cur); |
| 363 | if (ref == NULL) { |
| 364 | return(-1); |
| 365 | } |
| 366 | ref->fragment = fragment; |
| 367 | ref->doc = NULL; |
| 368 | ref->xml = xml; |
| 369 | ref->count = 1; |
| 370 | xmlFree(URL); |
| 371 | return(0); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 375 | * xmlXIncludeRecurseDoc: |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 376 | * @ctxt: the XInclude context |
| 377 | * @doc: the new document |
| 378 | * @url: the associated URL |
| 379 | * |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 380 | * The XInclude recursive nature is handled at this point. |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 381 | */ |
| 382 | static void |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 383 | xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 384 | const xmlURL url ATTRIBUTE_UNUSED) { |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 385 | xmlXIncludeCtxtPtr newctxt; |
| 386 | int i; |
| 387 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 388 | #ifdef DEBUG_XINCLUDE |
| 389 | xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL); |
| 390 | #endif |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 391 | /* |
| 392 | * Handle recursion here. |
| 393 | */ |
| 394 | |
| 395 | newctxt = xmlXIncludeNewContext(doc); |
| 396 | if (newctxt != NULL) { |
| 397 | /* |
| 398 | * Copy the existing document set |
| 399 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 400 | newctxt->incMax = ctxt->incMax; |
| 401 | newctxt->incNr = ctxt->incNr; |
| 402 | newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax * |
| 403 | sizeof(newctxt->incTab[0])); |
| 404 | if (newctxt->incTab == NULL) { |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 405 | xmlGenericError(xmlGenericErrorContext, |
| 406 | "malloc failed !\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 407 | ctxt->nbErrors++; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 408 | xmlFree(newctxt); |
| 409 | return; |
| 410 | } |
| 411 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 412 | /* |
| 413 | * Inherit the documents already in use by others includes |
| 414 | */ |
| 415 | newctxt->incBase = ctxt->incNr; |
| 416 | for (i = 0;i < ctxt->incNr;i++) { |
| 417 | newctxt->incTab[i] = ctxt->incTab[i]; |
| 418 | newctxt->incTab[i]->count++; /* prevent the recursion from |
| 419 | freeing it */ |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 420 | } |
| 421 | xmlXIncludeDoProcess(newctxt, doc); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 422 | for (i = 0;i < ctxt->incNr;i++) { |
| 423 | newctxt->incTab[i]->count--; |
| 424 | newctxt->incTab[i] = NULL; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 425 | } |
| 426 | xmlXIncludeFreeContext(newctxt); |
| 427 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 428 | #ifdef DEBUG_XINCLUDE |
| 429 | xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url); |
| 430 | #endif |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
| 434 | * xmlXIncludeAddTxt: |
| 435 | * @ctxt: the XInclude context |
| 436 | * @txt: the new text node |
| 437 | * @url: the associated URL |
| 438 | * |
| 439 | * Add a new txtument to the list |
| 440 | */ |
| 441 | static void |
| 442 | xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) { |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 443 | #ifdef DEBUG_XINCLUDE |
| 444 | xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url); |
| 445 | #endif |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 446 | if (ctxt->txtMax == 0) { |
| 447 | ctxt->txtMax = 4; |
| 448 | ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax * |
| 449 | sizeof(ctxt->txtTab[0])); |
| 450 | if (ctxt->txtTab == NULL) { |
| 451 | xmlGenericError(xmlGenericErrorContext, |
| 452 | "malloc failed !\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 453 | ctxt->nbErrors++; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax * |
| 457 | sizeof(ctxt->txturlTab[0])); |
| 458 | if (ctxt->txturlTab == NULL) { |
| 459 | xmlGenericError(xmlGenericErrorContext, |
| 460 | "malloc failed !\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 461 | ctxt->nbErrors++; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 462 | return; |
| 463 | } |
| 464 | } |
| 465 | if (ctxt->txtNr >= ctxt->txtMax) { |
| 466 | ctxt->txtMax *= 2; |
| 467 | ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab, |
| 468 | ctxt->txtMax * sizeof(ctxt->txtTab[0])); |
| 469 | if (ctxt->txtTab == NULL) { |
| 470 | xmlGenericError(xmlGenericErrorContext, |
| 471 | "realloc failed !\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 472 | ctxt->nbErrors++; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab, |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 476 | ctxt->txtMax * sizeof(ctxt->txturlTab[0])); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 477 | if (ctxt->txturlTab == NULL) { |
| 478 | xmlGenericError(xmlGenericErrorContext, |
| 479 | "realloc failed !\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 480 | ctxt->nbErrors++; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | } |
| 484 | ctxt->txtTab[ctxt->txtNr] = txt; |
| 485 | ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url); |
| 486 | ctxt->txtNr++; |
| 487 | } |
| 488 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 489 | /************************************************************************ |
| 490 | * * |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 491 | * Node copy with specific semantic * |
| 492 | * * |
| 493 | ************************************************************************/ |
| 494 | |
| 495 | /** |
| 496 | * xmlXIncludeCopyNode: |
| 497 | * @ctxt: the XInclude context |
| 498 | * @target: the document target |
| 499 | * @source: the document source |
| 500 | * @elem: the element |
| 501 | * |
| 502 | * Make a copy of the node while preserving the XInclude semantic |
| 503 | * of the Infoset copy |
| 504 | */ |
| 505 | static xmlNodePtr |
| 506 | xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, |
| 507 | xmlDocPtr source, xmlNodePtr elem) { |
| 508 | xmlNodePtr result = NULL; |
| 509 | |
| 510 | if ((ctxt == NULL) || (target == NULL) || (source == NULL) || |
| 511 | (elem == NULL)) |
| 512 | return(NULL); |
| 513 | if (elem->type == XML_DTD_NODE) |
| 514 | return(NULL); |
| 515 | result = xmlDocCopyNode(elem, target, 1); |
| 516 | return(result); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * xmlXIncludeCopyNodeList: |
| 521 | * @ctxt: the XInclude context |
| 522 | * @target: the document target |
| 523 | * @source: the document source |
| 524 | * @elem: the element list |
| 525 | * |
| 526 | * Make a copy of the node list while preserving the XInclude semantic |
| 527 | * of the Infoset copy |
| 528 | */ |
| 529 | static xmlNodePtr |
| 530 | xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, |
| 531 | xmlDocPtr source, xmlNodePtr elem) { |
| 532 | xmlNodePtr cur, res, result = NULL, last = NULL; |
| 533 | |
| 534 | if ((ctxt == NULL) || (target == NULL) || (source == NULL) || |
| 535 | (elem == NULL)) |
| 536 | return(NULL); |
| 537 | cur = elem; |
| 538 | while (cur != NULL) { |
| 539 | res = xmlXIncludeCopyNode(ctxt, target, source, cur); |
| 540 | if (res != NULL) { |
| 541 | if (result == NULL) { |
| 542 | result = last = res; |
| 543 | } else { |
| 544 | last->next = res; |
| 545 | res->prev = last; |
| 546 | last = res; |
| 547 | } |
| 548 | } |
| 549 | cur = cur->next; |
| 550 | } |
| 551 | return(result); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * xmlXInclueGetNthChild: |
| 556 | * @cur: the node |
| 557 | * @no: the child number |
| 558 | * |
| 559 | * Returns the @no'th element child of @cur or NULL |
| 560 | */ |
| 561 | static xmlNodePtr |
| 562 | xmlXIncludeGetNthChild(xmlNodePtr cur, int no) { |
| 563 | int i; |
| 564 | if (cur == NULL) |
| 565 | return(cur); |
| 566 | cur = cur->children; |
| 567 | for (i = 0;i <= no;cur = cur->next) { |
| 568 | if (cur == NULL) |
| 569 | return(cur); |
| 570 | if ((cur->type == XML_ELEMENT_NODE) || |
| 571 | (cur->type == XML_DOCUMENT_NODE) || |
| 572 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 573 | i++; |
| 574 | if (i == no) |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | return(cur); |
| 579 | } |
| 580 | |
| 581 | xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur); |
| 582 | |
| 583 | /** |
| 584 | * xmlXIncludeCopyRange: |
| 585 | * @ctxt: the XInclude context |
| 586 | * @target: the document target |
| 587 | * @source: the document source |
| 588 | * @obj: the XPointer result from the evaluation. |
| 589 | * |
| 590 | * Build a node list tree copy of the XPointer result. |
| 591 | * |
| 592 | * Returns an xmlNodePtr list or NULL. |
| 593 | * the caller has to free the node tree. |
| 594 | */ |
| 595 | static xmlNodePtr |
| 596 | xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, |
| 597 | xmlDocPtr source, xmlXPathObjectPtr range) { |
| 598 | /* pointers to generated nodes */ |
| 599 | xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp; |
| 600 | /* pointers to traversal nodes */ |
| 601 | xmlNodePtr start, cur, end; |
| 602 | int index1, index2; |
| 603 | |
| 604 | if ((ctxt == NULL) || (target == NULL) || (source == NULL) || |
| 605 | (range == NULL)) |
| 606 | return(NULL); |
| 607 | if (range->type != XPATH_RANGE) |
| 608 | return(NULL); |
| 609 | start = (xmlNodePtr) range->user; |
| 610 | |
| 611 | if (start == NULL) |
| 612 | return(NULL); |
| 613 | end = range->user2; |
| 614 | if (end == NULL) |
| 615 | return(xmlDocCopyNode(start, target, 1)); |
| 616 | |
| 617 | cur = start; |
| 618 | index1 = range->index; |
| 619 | index2 = range->index2; |
| 620 | while (cur != NULL) { |
| 621 | if (cur == end) { |
| 622 | if (cur->type == XML_TEXT_NODE) { |
| 623 | const xmlChar *content = cur->content; |
| 624 | int len; |
| 625 | |
| 626 | if (content == NULL) { |
| 627 | tmp = xmlNewTextLen(NULL, 0); |
| 628 | } else { |
| 629 | len = index2; |
| 630 | if ((cur == start) && (index1 > 1)) { |
| 631 | content += (index1 - 1); |
| 632 | len -= (index1 - 1); |
| 633 | index1 = 0; |
| 634 | } else { |
| 635 | len = index2; |
| 636 | } |
| 637 | tmp = xmlNewTextLen(content, len); |
| 638 | } |
| 639 | /* single sub text node selection */ |
| 640 | if (list == NULL) |
| 641 | return(tmp); |
| 642 | /* prune and return full set */ |
| 643 | if (last != NULL) |
| 644 | xmlAddNextSibling(last, tmp); |
| 645 | else |
| 646 | xmlAddChild(parent, tmp); |
| 647 | return(list); |
| 648 | } else { |
| 649 | tmp = xmlDocCopyNode(cur, target, 0); |
| 650 | if (list == NULL) |
| 651 | list = tmp; |
| 652 | else { |
| 653 | if (last != NULL) |
| 654 | xmlAddNextSibling(last, tmp); |
| 655 | else |
| 656 | xmlAddChild(parent, tmp); |
| 657 | } |
| 658 | last = NULL; |
| 659 | parent = tmp; |
| 660 | |
| 661 | if (index2 > 1) { |
| 662 | end = xmlXIncludeGetNthChild(cur, index2 - 1); |
| 663 | index2 = 0; |
| 664 | } |
| 665 | if ((cur == start) && (index1 > 1)) { |
| 666 | cur = xmlXIncludeGetNthChild(cur, index1 - 1); |
| 667 | index1 = 0; |
| 668 | } else { |
| 669 | cur = cur->children; |
| 670 | } |
| 671 | /* |
| 672 | * Now gather the remaining nodes from cur to end |
| 673 | */ |
| 674 | continue; /* while */ |
| 675 | } |
| 676 | } else if ((cur == start) && |
| 677 | (list == NULL) /* looks superfluous but ... */ ) { |
| 678 | if ((cur->type == XML_TEXT_NODE) || |
| 679 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 680 | const xmlChar *content = cur->content; |
| 681 | |
| 682 | if (content == NULL) { |
| 683 | tmp = xmlNewTextLen(NULL, 0); |
| 684 | } else { |
| 685 | if (index1 > 1) { |
| 686 | content += (index1 - 1); |
| 687 | } |
| 688 | tmp = xmlNewText(content); |
| 689 | } |
| 690 | last = list = tmp; |
| 691 | } else { |
| 692 | if ((cur == start) && (index1 > 1)) { |
| 693 | tmp = xmlDocCopyNode(cur, target, 0); |
| 694 | list = tmp; |
| 695 | parent = tmp; |
| 696 | last = NULL; |
| 697 | cur = xmlXIncludeGetNthChild(cur, index1 - 1); |
| 698 | index1 = 0; |
| 699 | /* |
| 700 | * Now gather the remaining nodes from cur to end |
| 701 | */ |
| 702 | continue; /* while */ |
| 703 | } |
| 704 | tmp = xmlDocCopyNode(cur, target, 1); |
| 705 | list = tmp; |
| 706 | parent = NULL; |
| 707 | last = tmp; |
| 708 | } |
| 709 | } else { |
| 710 | tmp = NULL; |
| 711 | switch (cur->type) { |
| 712 | case XML_DTD_NODE: |
| 713 | case XML_ELEMENT_DECL: |
| 714 | case XML_ATTRIBUTE_DECL: |
| 715 | case XML_ENTITY_NODE: |
| 716 | /* Do not copy DTD informations */ |
| 717 | break; |
| 718 | case XML_ENTITY_DECL: |
| 719 | /* handle crossing entities -> stack needed */ |
| 720 | break; |
| 721 | case XML_XINCLUDE_START: |
| 722 | case XML_XINCLUDE_END: |
| 723 | /* don't consider it part of the tree content */ |
| 724 | break; |
| 725 | case XML_ATTRIBUTE_NODE: |
| 726 | /* Humm, should not happen ! */ |
| 727 | break; |
| 728 | default: |
| 729 | tmp = xmlDocCopyNode(cur, target, 1); |
| 730 | break; |
| 731 | } |
| 732 | if (tmp != NULL) { |
| 733 | if ((list == NULL) || ((last == NULL) && (parent == NULL))) { |
| 734 | return(NULL); |
| 735 | } |
| 736 | if (last != NULL) |
| 737 | xmlAddNextSibling(last, tmp); |
| 738 | else { |
| 739 | xmlAddChild(parent, tmp); |
| 740 | last = tmp; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | /* |
| 745 | * Skip to next node in document order |
| 746 | */ |
| 747 | if ((list == NULL) || ((last == NULL) && (parent == NULL))) { |
| 748 | return(NULL); |
| 749 | } |
| 750 | cur = xmlXPtrAdvanceNode(cur); |
| 751 | } |
| 752 | return(list); |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * xmlXIncludeBuildNodeList: |
| 757 | * @ctxt: the XInclude context |
| 758 | * @target: the document target |
| 759 | * @source: the document source |
| 760 | * @obj: the XPointer result from the evaluation. |
| 761 | * |
| 762 | * Build a node list tree copy of the XPointer result. |
| 763 | * This will drop Attributes and Namespace declarations. |
| 764 | * |
| 765 | * Returns an xmlNodePtr list or NULL. |
| 766 | * the caller has to free the node tree. |
| 767 | */ |
| 768 | static xmlNodePtr |
| 769 | xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target, |
| 770 | xmlDocPtr source, xmlXPathObjectPtr obj) { |
| 771 | xmlNodePtr list = NULL, last = NULL; |
| 772 | int i; |
| 773 | |
| 774 | if ((ctxt == NULL) || (target == NULL) || (source == NULL) || |
| 775 | (obj == NULL)) |
| 776 | return(NULL); |
| 777 | switch (obj->type) { |
| 778 | case XPATH_NODESET: { |
| 779 | xmlNodeSetPtr set = obj->nodesetval; |
| 780 | if (set == NULL) |
| 781 | return(NULL); |
| 782 | for (i = 0;i < set->nodeNr;i++) { |
| 783 | if (set->nodeTab[i] == NULL) |
| 784 | continue; |
| 785 | switch (set->nodeTab[i]->type) { |
| 786 | case XML_TEXT_NODE: |
| 787 | case XML_CDATA_SECTION_NODE: |
| 788 | case XML_ELEMENT_NODE: |
| 789 | case XML_ENTITY_REF_NODE: |
| 790 | case XML_ENTITY_NODE: |
| 791 | case XML_PI_NODE: |
| 792 | case XML_COMMENT_NODE: |
| 793 | case XML_DOCUMENT_NODE: |
| 794 | case XML_HTML_DOCUMENT_NODE: |
| 795 | #ifdef LIBXML_DOCB_ENABLED |
| 796 | case XML_DOCB_DOCUMENT_NODE: |
| 797 | #endif |
| 798 | case XML_XINCLUDE_START: |
| 799 | case XML_XINCLUDE_END: |
| 800 | break; |
| 801 | case XML_ATTRIBUTE_NODE: |
| 802 | case XML_NAMESPACE_DECL: |
| 803 | case XML_DOCUMENT_TYPE_NODE: |
| 804 | case XML_DOCUMENT_FRAG_NODE: |
| 805 | case XML_NOTATION_NODE: |
| 806 | case XML_DTD_NODE: |
| 807 | case XML_ELEMENT_DECL: |
| 808 | case XML_ATTRIBUTE_DECL: |
| 809 | case XML_ENTITY_DECL: |
| 810 | continue; /* for */ |
| 811 | } |
| 812 | if (last == NULL) |
| 813 | list = last = xmlXIncludeCopyNode(ctxt, target, source, |
| 814 | set->nodeTab[i]); |
| 815 | else { |
| 816 | xmlAddNextSibling(last, |
| 817 | xmlXIncludeCopyNode(ctxt, target, source, |
| 818 | set->nodeTab[i])); |
| 819 | if (last->next != NULL) |
| 820 | last = last->next; |
| 821 | } |
| 822 | } |
| 823 | break; |
| 824 | } |
| 825 | case XPATH_LOCATIONSET: { |
| 826 | xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user; |
| 827 | if (set == NULL) |
| 828 | return(NULL); |
| 829 | for (i = 0;i < set->locNr;i++) { |
| 830 | if (last == NULL) |
| 831 | list = last = xmlXIncludeCopyXPointer(ctxt, target, source, |
| 832 | set->locTab[i]); |
| 833 | else |
| 834 | xmlAddNextSibling(last, |
| 835 | xmlXIncludeCopyXPointer(ctxt, target, source, |
| 836 | set->locTab[i])); |
| 837 | if (last != NULL) { |
| 838 | while (last->next != NULL) |
| 839 | last = last->next; |
| 840 | } |
| 841 | } |
| 842 | break; |
| 843 | } |
| 844 | case XPATH_RANGE: |
| 845 | return(xmlXIncludeCopyRange(ctxt, target, source, obj)); |
| 846 | case XPATH_POINT: |
| 847 | /* points are ignored in XInclude */ |
| 848 | break; |
| 849 | default: |
| 850 | break; |
| 851 | } |
| 852 | return(list); |
| 853 | } |
| 854 | /************************************************************************ |
| 855 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 856 | * XInclude I/O handling * |
| 857 | * * |
| 858 | ************************************************************************/ |
| 859 | |
| 860 | /** |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 861 | * xmlXIncludeMergeOneEntity: |
| 862 | * @ent: the entity |
| 863 | * @doc: the including doc |
| 864 | * @nr: the entity name |
| 865 | * |
| 866 | * Inplements the merge of one entity |
| 867 | */ |
| 868 | static void |
| 869 | xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlDocPtr doc, |
| 870 | xmlChar *name ATTRIBUTE_UNUSED) { |
| 871 | xmlEntityPtr ret; |
| 872 | |
| 873 | if ((ent == NULL) || (doc == NULL)) |
| 874 | return; |
| 875 | ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID, |
| 876 | ent->SystemID, ent->content); |
| 877 | if (ret != NULL) { |
| 878 | if (ent->URI != NULL) |
| 879 | ret->URI = xmlStrdup(ent->URI); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * xmlXIncludeMergeEntities: |
| 885 | * @ctxt: an XInclude context |
| 886 | * @doc: the including doc |
| 887 | * @from: the included doc |
| 888 | * |
| 889 | * Inplements the entity merge |
| 890 | * |
| 891 | * Returns 0 if merge succeeded, -1 if some processing failed |
| 892 | */ |
| 893 | static int |
| 894 | xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, |
| 895 | xmlDocPtr from) { |
| 896 | xmlNodePtr cur; |
| 897 | xmlDtdPtr target, source; |
| 898 | |
| 899 | if (ctxt == NULL) |
| 900 | return(-1); |
| 901 | |
| 902 | if ((from == NULL) || (from->intSubset == NULL)) |
| 903 | return(0); |
| 904 | |
| 905 | target = doc->intSubset; |
| 906 | if (target == NULL) { |
| 907 | cur = xmlDocGetRootElement(doc); |
| 908 | if (cur == NULL) |
| 909 | return(-1); |
| 910 | target = xmlCreateIntSubset(doc, cur->name, NULL, NULL); |
| 911 | if (target == NULL) |
| 912 | return(-1); |
| 913 | } |
| 914 | |
| 915 | source = from->intSubset; |
| 916 | if ((source != NULL) && (source->entities != NULL)) { |
| 917 | xmlHashScan((xmlHashTablePtr) source->entities, |
| 918 | (xmlHashScanner) xmlXIncludeMergeEntity, doc); |
| 919 | } |
| 920 | source = from->extSubset; |
| 921 | if ((source != NULL) && (source->entities != NULL)) { |
| 922 | /* |
| 923 | * don't duplicate existing stuff when external subsets are the same |
| 924 | */ |
| 925 | if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) && |
| 926 | (!xmlStrEqual(target->SystemID, source->SystemID))) { |
| 927 | xmlHashScan((xmlHashTablePtr) source->entities, |
| 928 | (xmlHashScanner) xmlXIncludeMergeEntity, doc); |
| 929 | } |
| 930 | } |
| 931 | return(0); |
| 932 | } |
| 933 | |
| 934 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | * xmlXIncludeLoadDoc: |
| 936 | * @ctxt: the XInclude context |
| 937 | * @url: the associated URL |
| 938 | * @nr: the xinclude node number |
| 939 | * |
| 940 | * Load the document, and store the result in the XInclude context |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 941 | * |
| 942 | * Returns 0 in case of success, -1 in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 943 | */ |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 944 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 945 | xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { |
| 946 | xmlDocPtr doc; |
| 947 | xmlURIPtr uri; |
| 948 | xmlChar *URL; |
| 949 | xmlChar *fragment = NULL; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 950 | int i = 0; |
| 951 | |
| 952 | #ifdef DEBUG_XINCLUDE |
| 953 | xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr); |
| 954 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 955 | /* |
| 956 | * Check the URL and remove any fragment identifier |
| 957 | */ |
| 958 | uri = xmlParseURI((const char *)url); |
| 959 | if (uri == NULL) { |
| 960 | xmlGenericError(xmlGenericErrorContext, |
| 961 | "XInclude: invalid value URI %s\n", url); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 962 | ctxt->nbErrors++; |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 963 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 964 | } |
| 965 | if (uri->fragment != NULL) { |
| 966 | fragment = (xmlChar *) uri->fragment; |
| 967 | uri->fragment = NULL; |
| 968 | } |
| 969 | URL = xmlSaveUri(uri); |
| 970 | xmlFreeURI(uri); |
| 971 | if (URL == NULL) { |
| 972 | xmlGenericError(xmlGenericErrorContext, |
| 973 | "XInclude: invalid value URI %s\n", url); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 974 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 975 | if (fragment != NULL) |
| 976 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 977 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Handling of references to the local document are done |
| 982 | * directly through ctxt->doc. |
| 983 | */ |
| 984 | if ((URL[0] == 0) || (URL[0] == '#')) { |
| 985 | doc = NULL; |
| 986 | goto loaded; |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * Prevent reloading twice the document. |
| 991 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 992 | for (i = 0; i < ctxt->incNr; i++) { |
| 993 | if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) && |
| 994 | (ctxt->incTab[i]->doc != NULL)) { |
| 995 | doc = ctxt->incTab[i]->doc; |
| 996 | #ifdef DEBUG_XINCLUDE |
| 997 | printf("Already loaded %s\n", URL); |
| 998 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 999 | goto loaded; |
| 1000 | } |
| 1001 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1002 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1003 | /* |
| 1004 | * Load it. |
| 1005 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1006 | #ifdef DEBUG_XINCLUDE |
| 1007 | printf("loading %s\n", URL); |
| 1008 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1009 | doc = xmlParseFile((const char *)URL); |
| 1010 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1011 | xmlFree(URL); |
| 1012 | if (fragment != NULL) |
| 1013 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1014 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1015 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1016 | ctxt->incTab[nr]->doc = doc; |
| 1017 | |
| 1018 | /* |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1019 | * Make sure we have all entities fixed up |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1020 | */ |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1021 | xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1022 | |
| 1023 | /* |
| 1024 | * We don't need the DTD anymore, free up space |
| 1025 | if (doc->intSubset != NULL) { |
| 1026 | xmlUnlinkNode((xmlNodePtr) doc->intSubset); |
| 1027 | xmlFreeNode((xmlNodePtr) doc->intSubset); |
| 1028 | doc->intSubset = NULL; |
| 1029 | } |
| 1030 | if (doc->extSubset != NULL) { |
| 1031 | xmlUnlinkNode((xmlNodePtr) doc->extSubset); |
| 1032 | xmlFreeNode((xmlNodePtr) doc->extSubset); |
| 1033 | doc->extSubset = NULL; |
| 1034 | } |
| 1035 | */ |
| 1036 | xmlXIncludeRecurseDoc(ctxt, doc, URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1037 | |
| 1038 | loaded: |
| 1039 | if (fragment == NULL) { |
| 1040 | /* |
| 1041 | * Add the top children list as the replacement copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1042 | */ |
| 1043 | if (doc == NULL) |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 1044 | { |
| 1045 | /* Hopefully a DTD declaration won't be copied from |
| 1046 | * the same document */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1047 | ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children); |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 1048 | } else { |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1049 | ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc, |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1050 | doc, doc->children); |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 1051 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1052 | } else { |
| 1053 | /* |
| 1054 | * Computes the XPointer expression and make a copy used |
| 1055 | * as the replacement copy. |
| 1056 | */ |
| 1057 | xmlXPathObjectPtr xptr; |
| 1058 | xmlXPathContextPtr xptrctxt; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1059 | xmlNodeSetPtr set; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1060 | |
| 1061 | if (doc == NULL) { |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1062 | xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref, |
| 1063 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1064 | } else { |
| 1065 | xptrctxt = xmlXPtrNewContext(doc, NULL, NULL); |
| 1066 | } |
| 1067 | if (xptrctxt == NULL) { |
| 1068 | xmlGenericError(xmlGenericErrorContext, |
| 1069 | "XInclude: could create XPointer context\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1070 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1071 | xmlFree(URL); |
| 1072 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1073 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1074 | } |
| 1075 | xptr = xmlXPtrEval(fragment, xptrctxt); |
| 1076 | if (xptr == NULL) { |
| 1077 | xmlGenericError(xmlGenericErrorContext, |
| 1078 | "XInclude: XPointer evaluation failed: #%s\n", |
| 1079 | fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1080 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1081 | xmlXPathFreeContext(xptrctxt); |
| 1082 | xmlFree(URL); |
| 1083 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1084 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1085 | } |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1086 | switch (xptr->type) { |
| 1087 | case XPATH_UNDEFINED: |
| 1088 | case XPATH_BOOLEAN: |
| 1089 | case XPATH_NUMBER: |
| 1090 | case XPATH_STRING: |
| 1091 | case XPATH_POINT: |
| 1092 | case XPATH_USERS: |
| 1093 | case XPATH_XSLT_TREE: |
| 1094 | xmlGenericError(xmlGenericErrorContext, |
| 1095 | "XInclude: XPointer is not a range: #%s\n", |
| 1096 | fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1097 | ctxt->nbErrors++; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1098 | xmlXPathFreeContext(xptrctxt); |
| 1099 | xmlFree(URL); |
| 1100 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1101 | return(-1); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1102 | case XPATH_NODESET: |
| 1103 | case XPATH_RANGE: |
| 1104 | case XPATH_LOCATIONSET: |
| 1105 | break; |
| 1106 | } |
| 1107 | set = xptr->nodesetval; |
| 1108 | if (set != NULL) { |
| 1109 | for (i = 0;i < set->nodeNr;i++) { |
| 1110 | if (set->nodeTab[i] == NULL) |
| 1111 | continue; |
| 1112 | switch (set->nodeTab[i]->type) { |
| 1113 | case XML_TEXT_NODE: |
| 1114 | case XML_CDATA_SECTION_NODE: |
| 1115 | case XML_ELEMENT_NODE: |
| 1116 | case XML_ENTITY_REF_NODE: |
| 1117 | case XML_ENTITY_NODE: |
| 1118 | case XML_PI_NODE: |
| 1119 | case XML_COMMENT_NODE: |
| 1120 | case XML_DOCUMENT_NODE: |
| 1121 | case XML_HTML_DOCUMENT_NODE: |
| 1122 | #ifdef LIBXML_DOCB_ENABLED |
| 1123 | case XML_DOCB_DOCUMENT_NODE: |
| 1124 | #endif |
| 1125 | continue; |
| 1126 | case XML_ATTRIBUTE_NODE: |
| 1127 | xmlGenericError(xmlGenericErrorContext, |
| 1128 | "XInclude: XPointer selects an attribute: #%s\n", |
| 1129 | fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1130 | ctxt->nbErrors++; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1131 | set->nodeTab[i] = NULL; |
| 1132 | continue; |
| 1133 | case XML_NAMESPACE_DECL: |
| 1134 | xmlGenericError(xmlGenericErrorContext, |
| 1135 | "XInclude: XPointer selects a namespace: #%s\n", |
| 1136 | fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1137 | ctxt->nbErrors++; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1138 | set->nodeTab[i] = NULL; |
| 1139 | continue; |
| 1140 | case XML_DOCUMENT_TYPE_NODE: |
| 1141 | case XML_DOCUMENT_FRAG_NODE: |
| 1142 | case XML_NOTATION_NODE: |
| 1143 | case XML_DTD_NODE: |
| 1144 | case XML_ELEMENT_DECL: |
| 1145 | case XML_ATTRIBUTE_DECL: |
| 1146 | case XML_ENTITY_DECL: |
| 1147 | case XML_XINCLUDE_START: |
| 1148 | case XML_XINCLUDE_END: |
| 1149 | xmlGenericError(xmlGenericErrorContext, |
| 1150 | "XInclude: XPointer selects unexpected nodes: #%s\n", |
| 1151 | fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1152 | ctxt->nbErrors++; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1153 | set->nodeTab[i] = NULL; |
| 1154 | set->nodeTab[i] = NULL; |
| 1155 | continue; /* for */ |
| 1156 | } |
| 1157 | } |
| 1158 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1159 | ctxt->incTab[nr]->inc = |
| 1160 | xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1161 | xmlXPathFreeObject(xptr); |
| 1162 | xmlXPathFreeContext(xptrctxt); |
| 1163 | xmlFree(fragment); |
| 1164 | } |
Daniel Veillard | c4bad4a | 2002-08-14 14:45:25 +0000 | [diff] [blame] | 1165 | |
| 1166 | /* |
| 1167 | * Do the xml:base fixup if needed |
| 1168 | */ |
| 1169 | if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) { |
| 1170 | xmlNodePtr node; |
| 1171 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1172 | node = ctxt->incTab[nr]->inc; |
Daniel Veillard | c4bad4a | 2002-08-14 14:45:25 +0000 | [diff] [blame] | 1173 | while (node != NULL) { |
| 1174 | if (node->type == XML_ELEMENT_NODE) |
| 1175 | xmlNodeSetBase(node, URL); |
| 1176 | node = node->next; |
| 1177 | } |
| 1178 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1179 | if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) && |
| 1180 | (ctxt->incTab[nr]->count <= 1)) { |
| 1181 | #ifdef DEBUG_XINCLUDE |
| 1182 | printf("freeing %s\n", ctxt->incTab[nr]->doc->URL); |
| 1183 | #endif |
| 1184 | xmlFreeDoc(ctxt->incTab[nr]->doc); |
| 1185 | ctxt->incTab[nr]->doc = NULL; |
| 1186 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1187 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1188 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /** |
| 1192 | * xmlXIncludeLoadTxt: |
| 1193 | * @ctxt: the XInclude context |
| 1194 | * @url: the associated URL |
| 1195 | * @nr: the xinclude node number |
| 1196 | * |
| 1197 | * Load the content, and store the result in the XInclude context |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1198 | * |
| 1199 | * Returns 0 in case of success, -1 in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1200 | */ |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1201 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1202 | xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { |
| 1203 | xmlParserInputBufferPtr buf; |
| 1204 | xmlNodePtr node; |
| 1205 | xmlURIPtr uri; |
| 1206 | xmlChar *URL; |
| 1207 | int i; |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1208 | xmlChar *encoding = NULL; |
| 1209 | xmlCharEncoding enc = 0; |
| 1210 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1211 | /* |
| 1212 | * Check the URL and remove any fragment identifier |
| 1213 | */ |
| 1214 | uri = xmlParseURI((const char *)url); |
| 1215 | if (uri == NULL) { |
| 1216 | xmlGenericError(xmlGenericErrorContext, |
| 1217 | "XInclude: invalid value URI %s\n", url); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1218 | ctxt->nbErrors++; |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1219 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1220 | } |
| 1221 | if (uri->fragment != NULL) { |
| 1222 | xmlGenericError(xmlGenericErrorContext, |
| 1223 | "XInclude: fragment identifier forbidden for text: %s\n", |
| 1224 | uri->fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1225 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1226 | xmlFreeURI(uri); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1227 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1228 | } |
| 1229 | URL = xmlSaveUri(uri); |
| 1230 | xmlFreeURI(uri); |
| 1231 | if (URL == NULL) { |
| 1232 | xmlGenericError(xmlGenericErrorContext, |
| 1233 | "XInclude: invalid value URI %s\n", url); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1234 | ctxt->nbErrors++; |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1235 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * Handling of references to the local document are done |
| 1240 | * directly through ctxt->doc. |
| 1241 | */ |
| 1242 | if (URL[0] == 0) { |
| 1243 | xmlGenericError(xmlGenericErrorContext, |
| 1244 | "XInclude: text serialization of document not available\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1245 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1246 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1247 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * Prevent reloading twice the document. |
| 1252 | */ |
| 1253 | for (i = 0; i < ctxt->txtNr; i++) { |
| 1254 | if (xmlStrEqual(URL, ctxt->txturlTab[i])) { |
| 1255 | node = xmlCopyNode(ctxt->txtTab[i], 1); |
| 1256 | goto loaded; |
| 1257 | } |
| 1258 | } |
| 1259 | /* |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1260 | * Try to get the encoding if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1261 | */ |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1262 | if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) { |
| 1263 | encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING); |
| 1264 | } |
| 1265 | if (encoding != NULL) { |
| 1266 | /* |
| 1267 | * TODO: we should not have to remap to the xmlCharEncoding |
| 1268 | * predefined set, a better interface than |
| 1269 | * xmlParserInputBufferCreateFilename should allow any |
| 1270 | * encoding supported by iconv |
| 1271 | */ |
| 1272 | enc = xmlParseCharEncoding((const char *) encoding); |
| 1273 | if (enc == XML_CHAR_ENCODING_ERROR) { |
| 1274 | xmlGenericError(xmlGenericErrorContext, |
| 1275 | "XInclude: encoding %s not supported\n", encoding); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1276 | ctxt->nbErrors++; |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1277 | xmlFree(encoding); |
| 1278 | xmlFree(URL); |
| 1279 | return(-1); |
| 1280 | } |
| 1281 | xmlFree(encoding); |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Load it. |
| 1286 | */ |
| 1287 | buf = xmlParserInputBufferCreateFilename((const char *)URL, enc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1288 | if (buf == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1289 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1290 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1291 | } |
| 1292 | node = xmlNewText(NULL); |
| 1293 | |
| 1294 | /* |
| 1295 | * Scan all chars from the resource and add the to the node |
| 1296 | */ |
| 1297 | while (xmlParserInputBufferRead(buf, 128) > 0) { |
| 1298 | int len; |
| 1299 | const xmlChar *content; |
| 1300 | |
| 1301 | content = xmlBufferContent(buf->buffer); |
| 1302 | len = xmlBufferLength(buf->buffer); |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1303 | for (i = 0;i < len;) { |
| 1304 | int cur; |
| 1305 | int l; |
| 1306 | |
| 1307 | cur = xmlStringCurrentChar(NULL, &content[i], &l); |
| 1308 | if (!IS_CHAR(cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1309 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1310 | "XInclude: %s contains invalid char %d\n", URL, cur); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1311 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1312 | } else { |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1313 | xmlNodeAddContentLen(node, &content[i], l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1314 | } |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1315 | i += l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1316 | } |
| 1317 | xmlBufferShrink(buf->buffer, len); |
| 1318 | } |
| 1319 | xmlFreeParserInputBuffer(buf); |
| 1320 | xmlXIncludeAddTxt(ctxt, node, URL); |
| 1321 | |
| 1322 | loaded: |
| 1323 | /* |
| 1324 | * Add the element as the replacement copy. |
| 1325 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1326 | ctxt->incTab[nr]->inc = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1327 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1328 | return(0); |
| 1329 | } |
| 1330 | |
| 1331 | /** |
| 1332 | * xmlXIncludeLoadFallback: |
| 1333 | * @ctxt: the XInclude context |
| 1334 | * @fallback: the fallback node |
| 1335 | * @nr: the xinclude node number |
| 1336 | * |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1337 | * Load the content of the fallback node, and store the result |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1338 | * in the XInclude context |
| 1339 | * |
| 1340 | * Returns 0 in case of success, -1 in case of failure |
| 1341 | */ |
| 1342 | static int |
| 1343 | xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) { |
| 1344 | if ((fallback == NULL) || (ctxt == NULL)) |
| 1345 | return(-1); |
| 1346 | |
Daniel Veillard | 0650345 | 2002-12-13 10:42:08 +0000 | [diff] [blame] | 1347 | ctxt->incTab[nr]->inc = xmlCopyNodeList(fallback->children); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1348 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /************************************************************************ |
| 1352 | * * |
| 1353 | * XInclude Processing * |
| 1354 | * * |
| 1355 | ************************************************************************/ |
| 1356 | |
| 1357 | /** |
| 1358 | * xmlXIncludePreProcessNode: |
| 1359 | * @ctxt: an XInclude context |
| 1360 | * @node: an XInclude node |
| 1361 | * |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 1362 | * Implement the XInclude preprocessing, currently just adding the element |
| 1363 | * for further processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1364 | * |
| 1365 | * Returns the result list or NULL in case of error |
| 1366 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1367 | static xmlNodePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1368 | xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { |
| 1369 | xmlXIncludeAddNode(ctxt, node); |
| 1370 | return(0); |
| 1371 | } |
| 1372 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 1373 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1374 | /** |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1375 | * xmlXIncludePreloadNode: |
| 1376 | * @ctxt: an XInclude context |
| 1377 | * @nr: the node number |
| 1378 | * |
| 1379 | * Do some precomputations and preload shared documents |
| 1380 | * |
| 1381 | * Returns 0 if substitution succeeded, -1 if some processing failed |
| 1382 | */ |
| 1383 | static int |
| 1384 | xmlXIncludePreloadNode(xmlXIncludeCtxtPtr ctxt, int nr) { |
| 1385 | xmlNodePtr cur; |
| 1386 | xmlChar *href; |
| 1387 | xmlChar *parse; |
| 1388 | xmlChar *base; |
| 1389 | xmlChar *URI; |
| 1390 | int xml = 1; /* default Issue 64 */ |
| 1391 | xmlURIPtr uri; |
| 1392 | xmlChar *URL; |
| 1393 | xmlChar *fragment = NULL; |
| 1394 | int i; |
| 1395 | |
| 1396 | |
| 1397 | if (ctxt == NULL) |
| 1398 | return(-1); |
| 1399 | if ((nr < 0) || (nr >= ctxt->incNr)) |
| 1400 | return(-1); |
| 1401 | cur = ctxt->incTab[nr]->ref; |
| 1402 | if (cur == NULL) |
| 1403 | return(-1); |
| 1404 | |
| 1405 | /* |
| 1406 | * read the attributes |
| 1407 | */ |
| 1408 | href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF); |
| 1409 | if (href == NULL) { |
| 1410 | href = xmlGetProp(cur, XINCLUDE_HREF); |
| 1411 | if (href == NULL) { |
| 1412 | xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n"); |
| 1413 | return(-1); |
| 1414 | } |
| 1415 | } |
| 1416 | parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE); |
| 1417 | if (parse == NULL) { |
| 1418 | parse = xmlGetProp(cur, XINCLUDE_PARSE); |
| 1419 | } |
| 1420 | if (parse != NULL) { |
| 1421 | if (xmlStrEqual(parse, XINCLUDE_PARSE_XML)) |
| 1422 | xml = 1; |
| 1423 | else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) |
| 1424 | xml = 0; |
| 1425 | else { |
| 1426 | xmlGenericError(xmlGenericErrorContext, |
| 1427 | "XInclude: invalid value %s for %s\n", |
| 1428 | parse, XINCLUDE_PARSE); |
| 1429 | if (href != NULL) |
| 1430 | xmlFree(href); |
| 1431 | if (parse != NULL) |
| 1432 | xmlFree(parse); |
| 1433 | return(-1); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | /* |
| 1438 | * compute the URI |
| 1439 | */ |
| 1440 | base = xmlNodeGetBase(ctxt->doc, cur); |
| 1441 | if (base == NULL) { |
| 1442 | URI = xmlBuildURI(href, ctxt->doc->URL); |
| 1443 | } else { |
| 1444 | URI = xmlBuildURI(href, base); |
| 1445 | } |
| 1446 | if (URI == NULL) { |
| 1447 | xmlChar *escbase; |
| 1448 | xmlChar *eschref; |
| 1449 | /* |
| 1450 | * Some escaping may be needed |
| 1451 | */ |
| 1452 | escbase = xmlURIEscape(base); |
| 1453 | eschref = xmlURIEscape(href); |
| 1454 | URI = xmlBuildURI(eschref, escbase); |
| 1455 | if (escbase != NULL) |
| 1456 | xmlFree(escbase); |
| 1457 | if (eschref != NULL) |
| 1458 | xmlFree(eschref); |
| 1459 | } |
| 1460 | if (parse != NULL) |
| 1461 | xmlFree(parse); |
| 1462 | if (href != NULL) |
| 1463 | xmlFree(href); |
| 1464 | if (base != NULL) |
| 1465 | xmlFree(base); |
| 1466 | if (URI == NULL) { |
| 1467 | xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1468 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1469 | return(-1); |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * Check the URL and remove any fragment identifier |
| 1474 | */ |
| 1475 | uri = xmlParseURI((const char *)URI); |
| 1476 | if (uri == NULL) { |
| 1477 | xmlGenericError(xmlGenericErrorContext, |
| 1478 | "XInclude: invalid value URI %s\n", URI); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1479 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1480 | xmlFree(URI); |
| 1481 | return(-1); |
| 1482 | } |
| 1483 | if (uri->fragment != NULL) { |
| 1484 | fragment = (xmlChar *) uri->fragment; |
| 1485 | uri->fragment = NULL; |
| 1486 | } |
| 1487 | URL = xmlSaveUri(uri); |
| 1488 | xmlFreeURI(uri); |
| 1489 | if (URL == NULL) { |
| 1490 | xmlGenericError(xmlGenericErrorContext, |
| 1491 | "XInclude: invalid value URI %s\n", URI); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1492 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1493 | if (fragment != NULL) |
| 1494 | xmlFree(fragment); |
| 1495 | xmlFree(URI); |
| 1496 | return(-1); |
| 1497 | } |
| 1498 | xmlFree(URI); |
| 1499 | if (fragment != NULL) |
| 1500 | xmlFree(fragment); |
| 1501 | |
| 1502 | for (i = 0; i < nr; i++) { |
| 1503 | if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) { |
| 1504 | #ifdef DEBUG_XINCLUDE |
| 1505 | printf("Incrementing count for %d : %s\n", i, ctxt->incTab[i]->URI); |
| 1506 | #endif |
| 1507 | ctxt->incTab[i]->count++; |
| 1508 | break; |
| 1509 | } |
| 1510 | } |
| 1511 | xmlFree(URL); |
| 1512 | return(0); |
| 1513 | } |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 1514 | #endif |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1515 | |
| 1516 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1517 | * xmlXIncludeLoadNode: |
| 1518 | * @ctxt: an XInclude context |
| 1519 | * @nr: the node number |
| 1520 | * |
| 1521 | * Find and load the infoset replacement for the given node. |
| 1522 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1523 | * Returns 0 if substitution succeeded, -1 if some processing failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1524 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1525 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1526 | xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) { |
| 1527 | xmlNodePtr cur; |
| 1528 | xmlChar *href; |
| 1529 | xmlChar *parse; |
| 1530 | xmlChar *base; |
| 1531 | xmlChar *URI; |
| 1532 | int xml = 1; /* default Issue 64 */ |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1533 | int ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1534 | |
| 1535 | if (ctxt == NULL) |
| 1536 | return(-1); |
| 1537 | if ((nr < 0) || (nr >= ctxt->incNr)) |
| 1538 | return(-1); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1539 | cur = ctxt->incTab[nr]->ref; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1540 | if (cur == NULL) |
| 1541 | return(-1); |
| 1542 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1543 | /* |
| 1544 | * read the attributes |
| 1545 | */ |
| 1546 | href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF); |
| 1547 | if (href == NULL) { |
| 1548 | href = xmlGetProp(cur, XINCLUDE_HREF); |
| 1549 | if (href == NULL) { |
| 1550 | xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1551 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1552 | return(-1); |
| 1553 | } |
| 1554 | } |
| 1555 | parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE); |
| 1556 | if (parse == NULL) { |
| 1557 | parse = xmlGetProp(cur, XINCLUDE_PARSE); |
| 1558 | } |
| 1559 | if (parse != NULL) { |
| 1560 | if (xmlStrEqual(parse, XINCLUDE_PARSE_XML)) |
| 1561 | xml = 1; |
| 1562 | else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) |
| 1563 | xml = 0; |
| 1564 | else { |
| 1565 | xmlGenericError(xmlGenericErrorContext, |
| 1566 | "XInclude: invalid value %s for %s\n", |
| 1567 | parse, XINCLUDE_PARSE); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1568 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1569 | if (href != NULL) |
| 1570 | xmlFree(href); |
| 1571 | if (parse != NULL) |
| 1572 | xmlFree(parse); |
| 1573 | return(-1); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * compute the URI |
| 1579 | */ |
| 1580 | base = xmlNodeGetBase(ctxt->doc, cur); |
| 1581 | if (base == NULL) { |
| 1582 | URI = xmlBuildURI(href, ctxt->doc->URL); |
| 1583 | } else { |
| 1584 | URI = xmlBuildURI(href, base); |
| 1585 | } |
| 1586 | if (URI == NULL) { |
| 1587 | xmlChar *escbase; |
| 1588 | xmlChar *eschref; |
| 1589 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1590 | * Some escaping may be needed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1591 | */ |
| 1592 | escbase = xmlURIEscape(base); |
| 1593 | eschref = xmlURIEscape(href); |
| 1594 | URI = xmlBuildURI(eschref, escbase); |
| 1595 | if (escbase != NULL) |
| 1596 | xmlFree(escbase); |
| 1597 | if (eschref != NULL) |
| 1598 | xmlFree(eschref); |
| 1599 | } |
| 1600 | if (URI == NULL) { |
| 1601 | xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1602 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1603 | if (parse != NULL) |
| 1604 | xmlFree(parse); |
| 1605 | if (href != NULL) |
| 1606 | xmlFree(href); |
| 1607 | if (base != NULL) |
| 1608 | xmlFree(base); |
| 1609 | return(-1); |
| 1610 | } |
| 1611 | #ifdef DEBUG_XINCLUDE |
| 1612 | xmlGenericError(xmlGenericErrorContext, "parse: %s\n", |
| 1613 | xml ? "xml": "text"); |
| 1614 | xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI); |
| 1615 | #endif |
| 1616 | |
| 1617 | /* |
| 1618 | * Cleanup |
| 1619 | */ |
| 1620 | if (xml) { |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1621 | ret = xmlXIncludeLoadDoc(ctxt, URI, nr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1622 | /* xmlXIncludeGetFragment(ctxt, cur, URI); */ |
| 1623 | } else { |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1624 | ret = xmlXIncludeLoadTxt(ctxt, URI, nr); |
| 1625 | } |
| 1626 | if (ret < 0) { |
| 1627 | xmlNodePtr children; |
| 1628 | |
| 1629 | /* |
| 1630 | * Time to try a fallback if availble |
| 1631 | */ |
| 1632 | #ifdef DEBUG_XINCLUDE |
| 1633 | xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n"); |
| 1634 | #endif |
| 1635 | children = cur->children; |
| 1636 | while (children != NULL) { |
| 1637 | if ((children->type == XML_ELEMENT_NODE) && |
| 1638 | (children->ns != NULL) && |
| 1639 | (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) && |
| 1640 | (xmlStrEqual(children->ns->href, XINCLUDE_NS))) { |
| 1641 | ret = xmlXIncludeLoadFallback(ctxt, children, nr); |
| 1642 | if (ret == 0) |
| 1643 | break; |
| 1644 | } |
| 1645 | children = children->next; |
| 1646 | } |
| 1647 | } |
| 1648 | if (ret < 0) { |
| 1649 | xmlGenericError(xmlGenericErrorContext, |
| 1650 | "XInclude: could not load %s, and no fallback was found\n", |
| 1651 | URI); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1652 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /* |
| 1656 | * Cleanup |
| 1657 | */ |
| 1658 | if (URI != NULL) |
| 1659 | xmlFree(URI); |
| 1660 | if (parse != NULL) |
| 1661 | xmlFree(parse); |
| 1662 | if (href != NULL) |
| 1663 | xmlFree(href); |
| 1664 | if (base != NULL) |
| 1665 | xmlFree(base); |
| 1666 | return(0); |
| 1667 | } |
| 1668 | |
| 1669 | /** |
| 1670 | * xmlXIncludeIncludeNode: |
| 1671 | * @ctxt: an XInclude context |
| 1672 | * @nr: the node number |
| 1673 | * |
| 1674 | * Inplement the infoset replacement for the given node |
| 1675 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1676 | * Returns 0 if substitution succeeded, -1 if some processing failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1677 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1678 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1679 | xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) { |
| 1680 | xmlNodePtr cur, end, list; |
| 1681 | |
| 1682 | if (ctxt == NULL) |
| 1683 | return(-1); |
| 1684 | if ((nr < 0) || (nr >= ctxt->incNr)) |
| 1685 | return(-1); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1686 | cur = ctxt->incTab[nr]->ref; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1687 | if (cur == NULL) |
| 1688 | return(-1); |
| 1689 | |
| 1690 | /* |
| 1691 | * Change the current node as an XInclude start one, and add an |
| 1692 | * entity end one |
| 1693 | */ |
| 1694 | cur->type = XML_XINCLUDE_START; |
| 1695 | end = xmlNewNode(cur->ns, cur->name); |
| 1696 | if (end == NULL) { |
| 1697 | xmlGenericError(xmlGenericErrorContext, |
| 1698 | "XInclude: failed to build node\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1699 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1700 | return(-1); |
| 1701 | } |
| 1702 | end->type = XML_XINCLUDE_END; |
| 1703 | xmlAddNextSibling(cur, end); |
| 1704 | |
| 1705 | /* |
| 1706 | * Add the list of nodes |
| 1707 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1708 | list = ctxt->incTab[nr]->inc; |
| 1709 | ctxt->incTab[nr]->inc = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1710 | while (list != NULL) { |
| 1711 | cur = list; |
| 1712 | list = list->next; |
| 1713 | |
| 1714 | xmlAddPrevSibling(end, cur); |
| 1715 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1716 | |
| 1717 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1718 | return(0); |
| 1719 | } |
| 1720 | |
| 1721 | /** |
| 1722 | * xmlXIncludeTestNode: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1723 | * @node: an XInclude node |
| 1724 | * |
| 1725 | * test if the node is an XInclude node |
| 1726 | * |
| 1727 | * Returns 1 true, 0 otherwise |
| 1728 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1729 | static int |
| 1730 | xmlXIncludeTestNode(xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1731 | if (node == NULL) |
| 1732 | return(0); |
| 1733 | if (node->ns == NULL) |
| 1734 | return(0); |
| 1735 | if ((xmlStrEqual(node->name, XINCLUDE_NODE)) && |
| 1736 | (xmlStrEqual(node->ns->href, XINCLUDE_NS))) return(1); |
| 1737 | return(0); |
| 1738 | } |
| 1739 | |
| 1740 | /** |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 1741 | * xmlXIncludeDoProcess: |
| 1742 | * @ctxt: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1743 | * @doc: an XML document |
| 1744 | * |
| 1745 | * Implement the XInclude substitution on the XML document @doc |
| 1746 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1747 | * Returns 0 if no substitution were done, -1 if some processing failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1748 | * or the number of substitutions done. |
| 1749 | */ |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 1750 | static int |
| 1751 | xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1752 | xmlNodePtr cur; |
| 1753 | int ret = 0; |
| 1754 | int i; |
| 1755 | |
| 1756 | if (doc == NULL) |
| 1757 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1758 | if (ctxt == NULL) |
| 1759 | return(-1); |
| 1760 | |
| 1761 | /* |
| 1762 | * First phase: lookup the elements in the document |
| 1763 | */ |
| 1764 | cur = xmlDocGetRootElement(doc); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1765 | if (xmlXIncludeTestNode(cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1766 | xmlXIncludePreProcessNode(ctxt, cur); |
| 1767 | while (cur != NULL) { |
| 1768 | /* TODO: need to work on entities -> stack */ |
| 1769 | if ((cur->children != NULL) && |
| 1770 | (cur->children->type != XML_ENTITY_DECL)) { |
| 1771 | cur = cur->children; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1772 | if (xmlXIncludeTestNode(cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1773 | xmlXIncludePreProcessNode(ctxt, cur); |
| 1774 | } else if (cur->next != NULL) { |
| 1775 | cur = cur->next; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1776 | if (xmlXIncludeTestNode(cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1777 | xmlXIncludePreProcessNode(ctxt, cur); |
| 1778 | } else { |
| 1779 | do { |
| 1780 | cur = cur->parent; |
| 1781 | if (cur == NULL) break; /* do */ |
| 1782 | if (cur->next != NULL) { |
| 1783 | cur = cur->next; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1784 | if (xmlXIncludeTestNode(cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1785 | xmlXIncludePreProcessNode(ctxt, cur); |
| 1786 | break; /* do */ |
| 1787 | } |
| 1788 | } while (cur != NULL); |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * Second Phase : collect the infosets fragments |
| 1794 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1795 | for (i = ctxt->incBase;i < ctxt->incNr; i++) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1796 | xmlXIncludeLoadNode(ctxt, i); |
Daniel Veillard | 97fd567 | 2003-02-07 13:01:54 +0000 | [diff] [blame] | 1797 | ret++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * Third phase: extend the original document infoset. |
| 1802 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1803 | for (i = ctxt->incBase;i < ctxt->incNr; i++) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1804 | xmlXIncludeIncludeNode(ctxt, i); |
| 1805 | } |
| 1806 | |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 1807 | return(ret); |
| 1808 | } |
| 1809 | |
| 1810 | /** |
| 1811 | * xmlXIncludeProcess: |
| 1812 | * @doc: an XML document |
| 1813 | * |
| 1814 | * Implement the XInclude substitution on the XML document @doc |
| 1815 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1816 | * Returns 0 if no substitution were done, -1 if some processing failed |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 1817 | * or the number of substitutions done. |
| 1818 | */ |
| 1819 | int |
| 1820 | xmlXIncludeProcess(xmlDocPtr doc) { |
| 1821 | xmlXIncludeCtxtPtr ctxt; |
| 1822 | int ret = 0; |
| 1823 | |
| 1824 | if (doc == NULL) |
| 1825 | return(-1); |
| 1826 | ctxt = xmlXIncludeNewContext(doc); |
| 1827 | if (ctxt == NULL) |
| 1828 | return(-1); |
| 1829 | ret = xmlXIncludeDoProcess(ctxt, doc); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame^] | 1830 | if ((ret >= 0) && (ctxt->nbErrors > 0)) |
| 1831 | ret = -1; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 1832 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1833 | xmlXIncludeFreeContext(ctxt); |
| 1834 | return(ret); |
| 1835 | } |
| 1836 | |
| 1837 | #else /* !LIBXML_XINCLUDE_ENABLED */ |
| 1838 | #endif |