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