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: |
| 1082 | xmlGenericError(xmlGenericErrorContext, |
| 1083 | "XInclude: mismatch in redefinition of entity %s\n", ent->name); |
| 1084 | ctxt->nbErrors++; |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * xmlXIncludeMergeEntities: |
| 1089 | * @ctxt: an XInclude context |
| 1090 | * @doc: the including doc |
| 1091 | * @from: the included doc |
| 1092 | * |
| 1093 | * Inplements the entity merge |
| 1094 | * |
| 1095 | * Returns 0 if merge succeeded, -1 if some processing failed |
| 1096 | */ |
| 1097 | static int |
| 1098 | xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, |
| 1099 | xmlDocPtr from) { |
| 1100 | xmlNodePtr cur; |
| 1101 | xmlDtdPtr target, source; |
| 1102 | |
| 1103 | if (ctxt == NULL) |
| 1104 | return(-1); |
| 1105 | |
| 1106 | if ((from == NULL) || (from->intSubset == NULL)) |
| 1107 | return(0); |
| 1108 | |
| 1109 | target = doc->intSubset; |
| 1110 | if (target == NULL) { |
| 1111 | cur = xmlDocGetRootElement(doc); |
| 1112 | if (cur == NULL) |
| 1113 | return(-1); |
| 1114 | target = xmlCreateIntSubset(doc, cur->name, NULL, NULL); |
| 1115 | if (target == NULL) |
| 1116 | return(-1); |
| 1117 | } |
| 1118 | |
| 1119 | source = from->intSubset; |
| 1120 | if ((source != NULL) && (source->entities != NULL)) { |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1121 | xmlXIncludeMergeData data; |
| 1122 | |
| 1123 | data.ctxt = ctxt; |
| 1124 | data.doc = doc; |
| 1125 | |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1126 | xmlHashScan((xmlHashTablePtr) source->entities, |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1127 | (xmlHashScanner) xmlXIncludeMergeEntity, &data); |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1128 | } |
| 1129 | source = from->extSubset; |
| 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 | /* |
| 1137 | * don't duplicate existing stuff when external subsets are the same |
| 1138 | */ |
| 1139 | if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) && |
| 1140 | (!xmlStrEqual(target->SystemID, source->SystemID))) { |
| 1141 | xmlHashScan((xmlHashTablePtr) source->entities, |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1142 | (xmlHashScanner) xmlXIncludeMergeEntity, &data); |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | return(0); |
| 1146 | } |
| 1147 | |
| 1148 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1149 | * xmlXIncludeLoadDoc: |
| 1150 | * @ctxt: the XInclude context |
| 1151 | * @url: the associated URL |
| 1152 | * @nr: the xinclude node number |
| 1153 | * |
| 1154 | * Load the document, and store the result in the XInclude context |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1155 | * |
| 1156 | * Returns 0 in case of success, -1 in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1157 | */ |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1158 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1159 | xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { |
| 1160 | xmlDocPtr doc; |
| 1161 | xmlURIPtr uri; |
| 1162 | xmlChar *URL; |
| 1163 | xmlChar *fragment = NULL; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1164 | int i = 0; |
| 1165 | |
| 1166 | #ifdef DEBUG_XINCLUDE |
| 1167 | xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr); |
| 1168 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1169 | /* |
| 1170 | * Check the URL and remove any fragment identifier |
| 1171 | */ |
| 1172 | uri = xmlParseURI((const char *)url); |
| 1173 | if (uri == NULL) { |
| 1174 | xmlGenericError(xmlGenericErrorContext, |
| 1175 | "XInclude: invalid value URI %s\n", url); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1176 | ctxt->nbErrors++; |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1177 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1178 | } |
| 1179 | if (uri->fragment != NULL) { |
| 1180 | fragment = (xmlChar *) uri->fragment; |
| 1181 | uri->fragment = NULL; |
| 1182 | } |
| 1183 | URL = xmlSaveUri(uri); |
| 1184 | xmlFreeURI(uri); |
| 1185 | if (URL == NULL) { |
| 1186 | xmlGenericError(xmlGenericErrorContext, |
| 1187 | "XInclude: invalid value URI %s\n", url); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1188 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1189 | if (fragment != NULL) |
| 1190 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1191 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * Handling of references to the local document are done |
| 1196 | * directly through ctxt->doc. |
| 1197 | */ |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1198 | if ((URL[0] == 0) || (URL[0] == '#') || |
| 1199 | ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1200 | doc = NULL; |
| 1201 | goto loaded; |
| 1202 | } |
| 1203 | |
| 1204 | /* |
| 1205 | * Prevent reloading twice the document. |
| 1206 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1207 | for (i = 0; i < ctxt->incNr; i++) { |
| 1208 | if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) && |
| 1209 | (ctxt->incTab[i]->doc != NULL)) { |
| 1210 | doc = ctxt->incTab[i]->doc; |
| 1211 | #ifdef DEBUG_XINCLUDE |
| 1212 | printf("Already loaded %s\n", URL); |
| 1213 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1214 | goto loaded; |
| 1215 | } |
| 1216 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1217 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1218 | /* |
| 1219 | * Load it. |
| 1220 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1221 | #ifdef DEBUG_XINCLUDE |
| 1222 | printf("loading %s\n", URL); |
| 1223 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1224 | doc = xmlParseFile((const char *)URL); |
| 1225 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1226 | xmlFree(URL); |
| 1227 | if (fragment != NULL) |
| 1228 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1229 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1230 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1231 | ctxt->incTab[nr]->doc = doc; |
| 1232 | |
| 1233 | /* |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1234 | * Make sure we have all entities fixed up |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1235 | */ |
Daniel Veillard | 4287c57 | 2003-02-04 22:48:53 +0000 | [diff] [blame] | 1236 | xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1237 | |
| 1238 | /* |
| 1239 | * We don't need the DTD anymore, free up space |
| 1240 | if (doc->intSubset != NULL) { |
| 1241 | xmlUnlinkNode((xmlNodePtr) doc->intSubset); |
| 1242 | xmlFreeNode((xmlNodePtr) doc->intSubset); |
| 1243 | doc->intSubset = NULL; |
| 1244 | } |
| 1245 | if (doc->extSubset != NULL) { |
| 1246 | xmlUnlinkNode((xmlNodePtr) doc->extSubset); |
| 1247 | xmlFreeNode((xmlNodePtr) doc->extSubset); |
| 1248 | doc->extSubset = NULL; |
| 1249 | } |
| 1250 | */ |
| 1251 | xmlXIncludeRecurseDoc(ctxt, doc, URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1252 | |
| 1253 | loaded: |
| 1254 | if (fragment == NULL) { |
| 1255 | /* |
| 1256 | * Add the top children list as the replacement copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1257 | */ |
| 1258 | if (doc == NULL) |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 1259 | { |
| 1260 | /* Hopefully a DTD declaration won't be copied from |
| 1261 | * the same document */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1262 | ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children); |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 1263 | } else { |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1264 | ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc, |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1265 | doc, doc->children); |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 1266 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1267 | } else { |
| 1268 | /* |
| 1269 | * Computes the XPointer expression and make a copy used |
| 1270 | * as the replacement copy. |
| 1271 | */ |
| 1272 | xmlXPathObjectPtr xptr; |
| 1273 | xmlXPathContextPtr xptrctxt; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1274 | xmlNodeSetPtr set; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1275 | |
| 1276 | if (doc == NULL) { |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1277 | xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref, |
| 1278 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1279 | } else { |
| 1280 | xptrctxt = xmlXPtrNewContext(doc, NULL, NULL); |
| 1281 | } |
| 1282 | if (xptrctxt == NULL) { |
| 1283 | xmlGenericError(xmlGenericErrorContext, |
| 1284 | "XInclude: could create XPointer context\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1285 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1286 | xmlFree(URL); |
| 1287 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1288 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1289 | } |
| 1290 | xptr = xmlXPtrEval(fragment, xptrctxt); |
| 1291 | if (xptr == NULL) { |
| 1292 | xmlGenericError(xmlGenericErrorContext, |
| 1293 | "XInclude: XPointer evaluation failed: #%s\n", |
| 1294 | fragment); |
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 | xmlXPathFreeContext(xptrctxt); |
| 1297 | xmlFree(URL); |
| 1298 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1299 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1300 | } |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1301 | switch (xptr->type) { |
| 1302 | case XPATH_UNDEFINED: |
| 1303 | case XPATH_BOOLEAN: |
| 1304 | case XPATH_NUMBER: |
| 1305 | case XPATH_STRING: |
| 1306 | case XPATH_POINT: |
| 1307 | case XPATH_USERS: |
| 1308 | case XPATH_XSLT_TREE: |
| 1309 | xmlGenericError(xmlGenericErrorContext, |
| 1310 | "XInclude: XPointer is not a range: #%s\n", |
| 1311 | fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1312 | ctxt->nbErrors++; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1313 | xmlXPathFreeContext(xptrctxt); |
| 1314 | xmlFree(URL); |
| 1315 | xmlFree(fragment); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1316 | return(-1); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1317 | case XPATH_NODESET: |
| 1318 | case XPATH_RANGE: |
| 1319 | case XPATH_LOCATIONSET: |
| 1320 | break; |
| 1321 | } |
| 1322 | set = xptr->nodesetval; |
| 1323 | if (set != NULL) { |
| 1324 | for (i = 0;i < set->nodeNr;i++) { |
| 1325 | if (set->nodeTab[i] == NULL) |
| 1326 | continue; |
| 1327 | switch (set->nodeTab[i]->type) { |
| 1328 | case XML_TEXT_NODE: |
| 1329 | case XML_CDATA_SECTION_NODE: |
| 1330 | case XML_ELEMENT_NODE: |
| 1331 | case XML_ENTITY_REF_NODE: |
| 1332 | case XML_ENTITY_NODE: |
| 1333 | case XML_PI_NODE: |
| 1334 | case XML_COMMENT_NODE: |
| 1335 | case XML_DOCUMENT_NODE: |
| 1336 | case XML_HTML_DOCUMENT_NODE: |
| 1337 | #ifdef LIBXML_DOCB_ENABLED |
| 1338 | case XML_DOCB_DOCUMENT_NODE: |
| 1339 | #endif |
| 1340 | continue; |
| 1341 | case XML_ATTRIBUTE_NODE: |
| 1342 | xmlGenericError(xmlGenericErrorContext, |
| 1343 | "XInclude: XPointer selects an attribute: #%s\n", |
| 1344 | fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1345 | ctxt->nbErrors++; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1346 | set->nodeTab[i] = NULL; |
| 1347 | continue; |
| 1348 | case XML_NAMESPACE_DECL: |
| 1349 | xmlGenericError(xmlGenericErrorContext, |
| 1350 | "XInclude: XPointer selects a namespace: #%s\n", |
| 1351 | fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1352 | ctxt->nbErrors++; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1353 | set->nodeTab[i] = NULL; |
| 1354 | continue; |
| 1355 | case XML_DOCUMENT_TYPE_NODE: |
| 1356 | case XML_DOCUMENT_FRAG_NODE: |
| 1357 | case XML_NOTATION_NODE: |
| 1358 | case XML_DTD_NODE: |
| 1359 | case XML_ELEMENT_DECL: |
| 1360 | case XML_ATTRIBUTE_DECL: |
| 1361 | case XML_ENTITY_DECL: |
| 1362 | case XML_XINCLUDE_START: |
| 1363 | case XML_XINCLUDE_END: |
| 1364 | xmlGenericError(xmlGenericErrorContext, |
| 1365 | "XInclude: XPointer selects unexpected nodes: #%s\n", |
| 1366 | fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1367 | ctxt->nbErrors++; |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 1368 | set->nodeTab[i] = NULL; |
| 1369 | set->nodeTab[i] = NULL; |
| 1370 | continue; /* for */ |
| 1371 | } |
| 1372 | } |
| 1373 | } |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1374 | if (doc == NULL) { |
| 1375 | ctxt->incTab[nr]->xptr = xptr; |
| 1376 | ctxt->incTab[nr]->inc = NULL; |
| 1377 | } else { |
| 1378 | ctxt->incTab[nr]->inc = |
| 1379 | xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr); |
| 1380 | xmlXPathFreeObject(xptr); |
| 1381 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1382 | xmlXPathFreeContext(xptrctxt); |
| 1383 | xmlFree(fragment); |
| 1384 | } |
Daniel Veillard | c4bad4a | 2002-08-14 14:45:25 +0000 | [diff] [blame] | 1385 | |
| 1386 | /* |
| 1387 | * Do the xml:base fixup if needed |
| 1388 | */ |
| 1389 | if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) { |
| 1390 | xmlNodePtr node; |
| 1391 | |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1392 | node = ctxt->incTab[nr]->inc; |
Daniel Veillard | c4bad4a | 2002-08-14 14:45:25 +0000 | [diff] [blame] | 1393 | while (node != NULL) { |
| 1394 | if (node->type == XML_ELEMENT_NODE) |
| 1395 | xmlNodeSetBase(node, URL); |
| 1396 | node = node->next; |
| 1397 | } |
| 1398 | } |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1399 | if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) && |
| 1400 | (ctxt->incTab[nr]->count <= 1)) { |
| 1401 | #ifdef DEBUG_XINCLUDE |
| 1402 | printf("freeing %s\n", ctxt->incTab[nr]->doc->URL); |
| 1403 | #endif |
| 1404 | xmlFreeDoc(ctxt->incTab[nr]->doc); |
| 1405 | ctxt->incTab[nr]->doc = NULL; |
| 1406 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1407 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1408 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * xmlXIncludeLoadTxt: |
| 1413 | * @ctxt: the XInclude context |
| 1414 | * @url: the associated URL |
| 1415 | * @nr: the xinclude node number |
| 1416 | * |
| 1417 | * Load the content, and store the result in the XInclude context |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1418 | * |
| 1419 | * Returns 0 in case of success, -1 in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1420 | */ |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1421 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1422 | xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { |
| 1423 | xmlParserInputBufferPtr buf; |
| 1424 | xmlNodePtr node; |
| 1425 | xmlURIPtr uri; |
| 1426 | xmlChar *URL; |
| 1427 | int i; |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1428 | xmlChar *encoding = NULL; |
| 1429 | xmlCharEncoding enc = 0; |
| 1430 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1431 | /* |
| 1432 | * Check the URL and remove any fragment identifier |
| 1433 | */ |
| 1434 | uri = xmlParseURI((const char *)url); |
| 1435 | if (uri == NULL) { |
| 1436 | xmlGenericError(xmlGenericErrorContext, |
| 1437 | "XInclude: invalid value URI %s\n", url); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1438 | ctxt->nbErrors++; |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1439 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1440 | } |
| 1441 | if (uri->fragment != NULL) { |
| 1442 | xmlGenericError(xmlGenericErrorContext, |
| 1443 | "XInclude: fragment identifier forbidden for text: %s\n", |
| 1444 | uri->fragment); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1445 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1446 | xmlFreeURI(uri); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1447 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1448 | } |
| 1449 | URL = xmlSaveUri(uri); |
| 1450 | xmlFreeURI(uri); |
| 1451 | if (URL == NULL) { |
| 1452 | xmlGenericError(xmlGenericErrorContext, |
| 1453 | "XInclude: invalid value URI %s\n", url); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1454 | ctxt->nbErrors++; |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1455 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Handling of references to the local document are done |
| 1460 | * directly through ctxt->doc. |
| 1461 | */ |
| 1462 | if (URL[0] == 0) { |
| 1463 | xmlGenericError(xmlGenericErrorContext, |
| 1464 | "XInclude: text serialization of document not available\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1465 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1466 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1467 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | /* |
| 1471 | * Prevent reloading twice the document. |
| 1472 | */ |
| 1473 | for (i = 0; i < ctxt->txtNr; i++) { |
| 1474 | if (xmlStrEqual(URL, ctxt->txturlTab[i])) { |
| 1475 | node = xmlCopyNode(ctxt->txtTab[i], 1); |
| 1476 | goto loaded; |
| 1477 | } |
| 1478 | } |
| 1479 | /* |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1480 | * Try to get the encoding if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1481 | */ |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1482 | if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) { |
| 1483 | encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING); |
| 1484 | } |
| 1485 | if (encoding != NULL) { |
| 1486 | /* |
| 1487 | * TODO: we should not have to remap to the xmlCharEncoding |
| 1488 | * predefined set, a better interface than |
| 1489 | * xmlParserInputBufferCreateFilename should allow any |
| 1490 | * encoding supported by iconv |
| 1491 | */ |
| 1492 | enc = xmlParseCharEncoding((const char *) encoding); |
| 1493 | if (enc == XML_CHAR_ENCODING_ERROR) { |
| 1494 | xmlGenericError(xmlGenericErrorContext, |
| 1495 | "XInclude: encoding %s not supported\n", encoding); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1496 | ctxt->nbErrors++; |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1497 | xmlFree(encoding); |
| 1498 | xmlFree(URL); |
| 1499 | return(-1); |
| 1500 | } |
| 1501 | xmlFree(encoding); |
| 1502 | } |
| 1503 | |
| 1504 | /* |
| 1505 | * Load it. |
| 1506 | */ |
| 1507 | buf = xmlParserInputBufferCreateFilename((const char *)URL, enc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1508 | if (buf == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1509 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1510 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1511 | } |
| 1512 | node = xmlNewText(NULL); |
| 1513 | |
| 1514 | /* |
| 1515 | * Scan all chars from the resource and add the to the node |
| 1516 | */ |
| 1517 | while (xmlParserInputBufferRead(buf, 128) > 0) { |
| 1518 | int len; |
| 1519 | const xmlChar *content; |
| 1520 | |
| 1521 | content = xmlBufferContent(buf->buffer); |
| 1522 | len = xmlBufferLength(buf->buffer); |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1523 | for (i = 0;i < len;) { |
| 1524 | int cur; |
| 1525 | int l; |
| 1526 | |
| 1527 | cur = xmlStringCurrentChar(NULL, &content[i], &l); |
| 1528 | if (!IS_CHAR(cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1529 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1530 | "XInclude: %s contains invalid char %d\n", URL, cur); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1531 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1532 | } else { |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1533 | xmlNodeAddContentLen(node, &content[i], l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1534 | } |
Daniel Veillard | d076a20 | 2002-11-20 13:28:31 +0000 | [diff] [blame] | 1535 | i += l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1536 | } |
| 1537 | xmlBufferShrink(buf->buffer, len); |
| 1538 | } |
| 1539 | xmlFreeParserInputBuffer(buf); |
| 1540 | xmlXIncludeAddTxt(ctxt, node, URL); |
| 1541 | |
| 1542 | loaded: |
| 1543 | /* |
| 1544 | * Add the element as the replacement copy. |
| 1545 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1546 | ctxt->incTab[nr]->inc = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1547 | xmlFree(URL); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1548 | return(0); |
| 1549 | } |
| 1550 | |
| 1551 | /** |
| 1552 | * xmlXIncludeLoadFallback: |
| 1553 | * @ctxt: the XInclude context |
| 1554 | * @fallback: the fallback node |
| 1555 | * @nr: the xinclude node number |
| 1556 | * |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1557 | * Load the content of the fallback node, and store the result |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1558 | * in the XInclude context |
| 1559 | * |
| 1560 | * Returns 0 in case of success, -1 in case of failure |
| 1561 | */ |
| 1562 | static int |
| 1563 | xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) { |
| 1564 | if ((fallback == NULL) || (ctxt == NULL)) |
| 1565 | return(-1); |
| 1566 | |
Daniel Veillard | 0650345 | 2002-12-13 10:42:08 +0000 | [diff] [blame] | 1567 | ctxt->incTab[nr]->inc = xmlCopyNodeList(fallback->children); |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1568 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | /************************************************************************ |
| 1572 | * * |
| 1573 | * XInclude Processing * |
| 1574 | * * |
| 1575 | ************************************************************************/ |
| 1576 | |
| 1577 | /** |
| 1578 | * xmlXIncludePreProcessNode: |
| 1579 | * @ctxt: an XInclude context |
| 1580 | * @node: an XInclude node |
| 1581 | * |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 1582 | * Implement the XInclude preprocessing, currently just adding the element |
| 1583 | * for further processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1584 | * |
| 1585 | * Returns the result list or NULL in case of error |
| 1586 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1587 | static xmlNodePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1588 | xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { |
| 1589 | xmlXIncludeAddNode(ctxt, node); |
| 1590 | return(0); |
| 1591 | } |
| 1592 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 1593 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1594 | /** |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1595 | * xmlXIncludePreloadNode: |
| 1596 | * @ctxt: an XInclude context |
| 1597 | * @nr: the node number |
| 1598 | * |
| 1599 | * Do some precomputations and preload shared documents |
| 1600 | * |
| 1601 | * Returns 0 if substitution succeeded, -1 if some processing failed |
| 1602 | */ |
| 1603 | static int |
| 1604 | xmlXIncludePreloadNode(xmlXIncludeCtxtPtr ctxt, int nr) { |
| 1605 | xmlNodePtr cur; |
| 1606 | xmlChar *href; |
| 1607 | xmlChar *parse; |
| 1608 | xmlChar *base; |
| 1609 | xmlChar *URI; |
| 1610 | int xml = 1; /* default Issue 64 */ |
| 1611 | xmlURIPtr uri; |
| 1612 | xmlChar *URL; |
| 1613 | xmlChar *fragment = NULL; |
| 1614 | int i; |
| 1615 | |
| 1616 | |
| 1617 | if (ctxt == NULL) |
| 1618 | return(-1); |
| 1619 | if ((nr < 0) || (nr >= ctxt->incNr)) |
| 1620 | return(-1); |
| 1621 | cur = ctxt->incTab[nr]->ref; |
| 1622 | if (cur == NULL) |
| 1623 | return(-1); |
| 1624 | |
| 1625 | /* |
| 1626 | * read the attributes |
| 1627 | */ |
| 1628 | href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF); |
| 1629 | if (href == NULL) { |
| 1630 | href = xmlGetProp(cur, XINCLUDE_HREF); |
| 1631 | if (href == NULL) { |
| 1632 | xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n"); |
| 1633 | return(-1); |
| 1634 | } |
| 1635 | } |
| 1636 | parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE); |
| 1637 | if (parse == NULL) { |
| 1638 | parse = xmlGetProp(cur, XINCLUDE_PARSE); |
| 1639 | } |
| 1640 | if (parse != NULL) { |
| 1641 | if (xmlStrEqual(parse, XINCLUDE_PARSE_XML)) |
| 1642 | xml = 1; |
| 1643 | else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) |
| 1644 | xml = 0; |
| 1645 | else { |
| 1646 | xmlGenericError(xmlGenericErrorContext, |
| 1647 | "XInclude: invalid value %s for %s\n", |
| 1648 | parse, XINCLUDE_PARSE); |
| 1649 | if (href != NULL) |
| 1650 | xmlFree(href); |
| 1651 | if (parse != NULL) |
| 1652 | xmlFree(parse); |
| 1653 | return(-1); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | /* |
| 1658 | * compute the URI |
| 1659 | */ |
| 1660 | base = xmlNodeGetBase(ctxt->doc, cur); |
| 1661 | if (base == NULL) { |
| 1662 | URI = xmlBuildURI(href, ctxt->doc->URL); |
| 1663 | } else { |
| 1664 | URI = xmlBuildURI(href, base); |
| 1665 | } |
| 1666 | if (URI == NULL) { |
| 1667 | xmlChar *escbase; |
| 1668 | xmlChar *eschref; |
| 1669 | /* |
| 1670 | * Some escaping may be needed |
| 1671 | */ |
| 1672 | escbase = xmlURIEscape(base); |
| 1673 | eschref = xmlURIEscape(href); |
| 1674 | URI = xmlBuildURI(eschref, escbase); |
| 1675 | if (escbase != NULL) |
| 1676 | xmlFree(escbase); |
| 1677 | if (eschref != NULL) |
| 1678 | xmlFree(eschref); |
| 1679 | } |
| 1680 | if (parse != NULL) |
| 1681 | xmlFree(parse); |
| 1682 | if (href != NULL) |
| 1683 | xmlFree(href); |
| 1684 | if (base != NULL) |
| 1685 | xmlFree(base); |
| 1686 | if (URI == NULL) { |
| 1687 | xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1688 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1689 | return(-1); |
| 1690 | } |
| 1691 | |
| 1692 | /* |
| 1693 | * Check the URL and remove any fragment identifier |
| 1694 | */ |
| 1695 | uri = xmlParseURI((const char *)URI); |
| 1696 | if (uri == NULL) { |
| 1697 | xmlGenericError(xmlGenericErrorContext, |
| 1698 | "XInclude: invalid value URI %s\n", URI); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1699 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1700 | xmlFree(URI); |
| 1701 | return(-1); |
| 1702 | } |
| 1703 | if (uri->fragment != NULL) { |
| 1704 | fragment = (xmlChar *) uri->fragment; |
| 1705 | uri->fragment = NULL; |
| 1706 | } |
| 1707 | URL = xmlSaveUri(uri); |
| 1708 | xmlFreeURI(uri); |
| 1709 | if (URL == NULL) { |
| 1710 | xmlGenericError(xmlGenericErrorContext, |
| 1711 | "XInclude: invalid value URI %s\n", URI); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1712 | ctxt->nbErrors++; |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1713 | if (fragment != NULL) |
| 1714 | xmlFree(fragment); |
| 1715 | xmlFree(URI); |
| 1716 | return(-1); |
| 1717 | } |
| 1718 | xmlFree(URI); |
| 1719 | if (fragment != NULL) |
| 1720 | xmlFree(fragment); |
| 1721 | |
| 1722 | for (i = 0; i < nr; i++) { |
| 1723 | if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) { |
| 1724 | #ifdef DEBUG_XINCLUDE |
| 1725 | printf("Incrementing count for %d : %s\n", i, ctxt->incTab[i]->URI); |
| 1726 | #endif |
| 1727 | ctxt->incTab[i]->count++; |
| 1728 | break; |
| 1729 | } |
| 1730 | } |
| 1731 | xmlFree(URL); |
| 1732 | return(0); |
| 1733 | } |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 1734 | #endif |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1735 | |
| 1736 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1737 | * xmlXIncludeLoadNode: |
| 1738 | * @ctxt: an XInclude context |
| 1739 | * @nr: the node number |
| 1740 | * |
| 1741 | * Find and load the infoset replacement for the given node. |
| 1742 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1743 | * Returns 0 if substitution succeeded, -1 if some processing failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1744 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1745 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1746 | xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) { |
| 1747 | xmlNodePtr cur; |
| 1748 | xmlChar *href; |
| 1749 | xmlChar *parse; |
| 1750 | xmlChar *base; |
| 1751 | xmlChar *URI; |
| 1752 | int xml = 1; /* default Issue 64 */ |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1753 | int ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1754 | |
| 1755 | if (ctxt == NULL) |
| 1756 | return(-1); |
| 1757 | if ((nr < 0) || (nr >= ctxt->incNr)) |
| 1758 | return(-1); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1759 | cur = ctxt->incTab[nr]->ref; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1760 | if (cur == NULL) |
| 1761 | return(-1); |
| 1762 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1763 | /* |
| 1764 | * read the attributes |
| 1765 | */ |
| 1766 | href = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_HREF); |
| 1767 | if (href == NULL) { |
| 1768 | href = xmlGetProp(cur, XINCLUDE_HREF); |
| 1769 | if (href == NULL) { |
| 1770 | xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1771 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1772 | return(-1); |
| 1773 | } |
| 1774 | } |
| 1775 | parse = xmlGetNsProp(cur, XINCLUDE_NS, XINCLUDE_PARSE); |
| 1776 | if (parse == NULL) { |
| 1777 | parse = xmlGetProp(cur, XINCLUDE_PARSE); |
| 1778 | } |
| 1779 | if (parse != NULL) { |
| 1780 | if (xmlStrEqual(parse, XINCLUDE_PARSE_XML)) |
| 1781 | xml = 1; |
| 1782 | else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) |
| 1783 | xml = 0; |
| 1784 | else { |
| 1785 | xmlGenericError(xmlGenericErrorContext, |
| 1786 | "XInclude: invalid value %s for %s\n", |
| 1787 | parse, XINCLUDE_PARSE); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1788 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1789 | if (href != NULL) |
| 1790 | xmlFree(href); |
| 1791 | if (parse != NULL) |
| 1792 | xmlFree(parse); |
| 1793 | return(-1); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | /* |
| 1798 | * compute the URI |
| 1799 | */ |
| 1800 | base = xmlNodeGetBase(ctxt->doc, cur); |
| 1801 | if (base == NULL) { |
| 1802 | URI = xmlBuildURI(href, ctxt->doc->URL); |
| 1803 | } else { |
| 1804 | URI = xmlBuildURI(href, base); |
| 1805 | } |
| 1806 | if (URI == NULL) { |
| 1807 | xmlChar *escbase; |
| 1808 | xmlChar *eschref; |
| 1809 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1810 | * Some escaping may be needed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1811 | */ |
| 1812 | escbase = xmlURIEscape(base); |
| 1813 | eschref = xmlURIEscape(href); |
| 1814 | URI = xmlBuildURI(eschref, escbase); |
| 1815 | if (escbase != NULL) |
| 1816 | xmlFree(escbase); |
| 1817 | if (eschref != NULL) |
| 1818 | xmlFree(eschref); |
| 1819 | } |
| 1820 | if (URI == NULL) { |
| 1821 | xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1822 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1823 | if (parse != NULL) |
| 1824 | xmlFree(parse); |
| 1825 | if (href != NULL) |
| 1826 | xmlFree(href); |
| 1827 | if (base != NULL) |
| 1828 | xmlFree(base); |
| 1829 | return(-1); |
| 1830 | } |
| 1831 | #ifdef DEBUG_XINCLUDE |
| 1832 | xmlGenericError(xmlGenericErrorContext, "parse: %s\n", |
| 1833 | xml ? "xml": "text"); |
| 1834 | xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI); |
| 1835 | #endif |
| 1836 | |
| 1837 | /* |
| 1838 | * Cleanup |
| 1839 | */ |
| 1840 | if (xml) { |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1841 | ret = xmlXIncludeLoadDoc(ctxt, URI, nr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1842 | /* xmlXIncludeGetFragment(ctxt, cur, URI); */ |
| 1843 | } else { |
Daniel Veillard | e3b7d9a | 2002-08-14 14:11:30 +0000 | [diff] [blame] | 1844 | ret = xmlXIncludeLoadTxt(ctxt, URI, nr); |
| 1845 | } |
| 1846 | if (ret < 0) { |
| 1847 | xmlNodePtr children; |
| 1848 | |
| 1849 | /* |
| 1850 | * Time to try a fallback if availble |
| 1851 | */ |
| 1852 | #ifdef DEBUG_XINCLUDE |
| 1853 | xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n"); |
| 1854 | #endif |
| 1855 | children = cur->children; |
| 1856 | while (children != NULL) { |
| 1857 | if ((children->type == XML_ELEMENT_NODE) && |
| 1858 | (children->ns != NULL) && |
| 1859 | (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) && |
| 1860 | (xmlStrEqual(children->ns->href, XINCLUDE_NS))) { |
| 1861 | ret = xmlXIncludeLoadFallback(ctxt, children, nr); |
| 1862 | if (ret == 0) |
| 1863 | break; |
| 1864 | } |
| 1865 | children = children->next; |
| 1866 | } |
| 1867 | } |
| 1868 | if (ret < 0) { |
| 1869 | xmlGenericError(xmlGenericErrorContext, |
| 1870 | "XInclude: could not load %s, and no fallback was found\n", |
| 1871 | URI); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1872 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | /* |
| 1876 | * Cleanup |
| 1877 | */ |
| 1878 | if (URI != NULL) |
| 1879 | xmlFree(URI); |
| 1880 | if (parse != NULL) |
| 1881 | xmlFree(parse); |
| 1882 | if (href != NULL) |
| 1883 | xmlFree(href); |
| 1884 | if (base != NULL) |
| 1885 | xmlFree(base); |
| 1886 | return(0); |
| 1887 | } |
| 1888 | |
| 1889 | /** |
| 1890 | * xmlXIncludeIncludeNode: |
| 1891 | * @ctxt: an XInclude context |
| 1892 | * @nr: the node number |
| 1893 | * |
| 1894 | * Inplement the infoset replacement for the given node |
| 1895 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1896 | * Returns 0 if substitution succeeded, -1 if some processing failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1897 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1898 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1899 | xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) { |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1900 | xmlNodePtr cur, end, list, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1901 | |
| 1902 | if (ctxt == NULL) |
| 1903 | return(-1); |
| 1904 | if ((nr < 0) || (nr >= ctxt->incNr)) |
| 1905 | return(-1); |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 1906 | cur = ctxt->incTab[nr]->ref; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1907 | if (cur == NULL) |
| 1908 | return(-1); |
| 1909 | |
| 1910 | /* |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1911 | * If we stored an XPointer a late computation may be needed |
| 1912 | */ |
| 1913 | if ((ctxt->incTab[nr]->inc == NULL) && |
| 1914 | (ctxt->incTab[nr]->xptr != NULL)) { |
| 1915 | ctxt->incTab[nr]->inc = |
| 1916 | xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc, |
| 1917 | ctxt->incTab[nr]->xptr); |
| 1918 | xmlXPathFreeObject(ctxt->incTab[nr]->xptr); |
| 1919 | ctxt->incTab[nr]->xptr = NULL; |
| 1920 | } |
| 1921 | list = ctxt->incTab[nr]->inc; |
| 1922 | ctxt->incTab[nr]->inc = NULL; |
| 1923 | |
| 1924 | /* |
| 1925 | * Check against the risk of generating a multi-rooted document |
| 1926 | */ |
| 1927 | if ((cur->parent != NULL) && |
| 1928 | (cur->parent->type != XML_ELEMENT_NODE)) { |
| 1929 | int nb_elem = 0; |
| 1930 | |
| 1931 | tmp = list; |
| 1932 | while (tmp != NULL) { |
| 1933 | if (tmp->type == XML_ELEMENT_NODE) |
| 1934 | nb_elem++; |
| 1935 | tmp = tmp->next; |
| 1936 | } |
| 1937 | if (nb_elem > 1) { |
| 1938 | xmlGenericError(xmlGenericErrorContext, |
| 1939 | "XInclude error: would result in multiple root nodes\n"); |
| 1940 | ctxt->nbErrors++; |
| 1941 | return(-1); |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1946 | * Change the current node as an XInclude start one, and add an |
| 1947 | * entity end one |
| 1948 | */ |
| 1949 | cur->type = XML_XINCLUDE_START; |
| 1950 | end = xmlNewNode(cur->ns, cur->name); |
| 1951 | if (end == NULL) { |
| 1952 | xmlGenericError(xmlGenericErrorContext, |
| 1953 | "XInclude: failed to build node\n"); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 1954 | ctxt->nbErrors++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1955 | return(-1); |
| 1956 | } |
| 1957 | end->type = XML_XINCLUDE_END; |
| 1958 | xmlAddNextSibling(cur, end); |
| 1959 | |
| 1960 | /* |
| 1961 | * Add the list of nodes |
| 1962 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1963 | while (list != NULL) { |
| 1964 | cur = list; |
| 1965 | list = list->next; |
| 1966 | |
| 1967 | xmlAddPrevSibling(end, cur); |
| 1968 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 1969 | |
| 1970 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1971 | return(0); |
| 1972 | } |
| 1973 | |
| 1974 | /** |
| 1975 | * xmlXIncludeTestNode: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1976 | * @ctxt: the XInclude processing context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1977 | * @node: an XInclude node |
| 1978 | * |
| 1979 | * test if the node is an XInclude node |
| 1980 | * |
| 1981 | * Returns 1 true, 0 otherwise |
| 1982 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1983 | static int |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1984 | xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1985 | if (node == NULL) |
| 1986 | return(0); |
| 1987 | if (node->ns == NULL) |
| 1988 | return(0); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 1989 | if (xmlStrEqual(node->ns->href, XINCLUDE_NS)) { |
| 1990 | if (xmlStrEqual(node->name, XINCLUDE_NODE)) { |
| 1991 | xmlNodePtr child = node->children; |
| 1992 | int nb_fallback = 0; |
| 1993 | |
| 1994 | while (child != NULL) { |
| 1995 | if ((child->type == XML_ELEMENT_NODE) && |
| 1996 | (child->ns != NULL) && |
| 1997 | (xmlStrEqual(child->ns->href, XINCLUDE_NS))) { |
| 1998 | if (xmlStrEqual(child->name, XINCLUDE_NODE)) { |
| 1999 | xmlGenericError(xmlGenericErrorContext, |
| 2000 | "XInclude: %s has an %s child\n", |
| 2001 | XINCLUDE_NODE, XINCLUDE_NODE); |
| 2002 | ctxt->nbErrors++; |
| 2003 | return(0); |
| 2004 | } |
| 2005 | if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) { |
| 2006 | nb_fallback++; |
| 2007 | } |
| 2008 | } |
| 2009 | child = child->next; |
| 2010 | } |
| 2011 | if (nb_fallback > 1) { |
| 2012 | xmlGenericError(xmlGenericErrorContext, |
| 2013 | "XInclude: %s has %d %s children\n", |
| 2014 | XINCLUDE_NODE, nb_fallback, XINCLUDE_FALLBACK); |
| 2015 | ctxt->nbErrors++; |
| 2016 | return(0); |
| 2017 | } |
| 2018 | return(1); |
| 2019 | } |
| 2020 | if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) { |
| 2021 | if ((node->parent == NULL) || |
| 2022 | (node->parent->type != XML_ELEMENT_NODE) || |
| 2023 | (node->parent->ns == NULL) || |
| 2024 | (!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) || |
| 2025 | (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) { |
| 2026 | xmlGenericError(xmlGenericErrorContext, |
| 2027 | "XInclude: %s is not the child of an %s\n", |
| 2028 | XINCLUDE_FALLBACK, XINCLUDE_NODE); |
| 2029 | ctxt->nbErrors++; |
| 2030 | } |
| 2031 | } |
| 2032 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2033 | return(0); |
| 2034 | } |
| 2035 | |
| 2036 | /** |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2037 | * xmlXIncludeDoProcess: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2038 | * @ctxt: the XInclude processing context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2039 | * @doc: an XML document |
| 2040 | * |
| 2041 | * Implement the XInclude substitution on the XML document @doc |
| 2042 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2043 | * Returns 0 if no substitution were done, -1 if some processing failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2044 | * or the number of substitutions done. |
| 2045 | */ |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2046 | static int |
| 2047 | xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2048 | xmlNodePtr cur; |
| 2049 | int ret = 0; |
| 2050 | int i; |
| 2051 | |
| 2052 | if (doc == NULL) |
| 2053 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2054 | if (ctxt == NULL) |
| 2055 | return(-1); |
| 2056 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2057 | if (doc->URL != NULL) { |
| 2058 | ret = xmlXIncludeURLPush(ctxt, doc->URL); |
| 2059 | if (ret < 0) |
| 2060 | return(-1); |
| 2061 | } |
| 2062 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2063 | /* |
| 2064 | * First phase: lookup the elements in the document |
| 2065 | */ |
| 2066 | cur = xmlDocGetRootElement(doc); |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2067 | if (xmlXIncludeTestNode(ctxt, cur) == 1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2068 | xmlXIncludePreProcessNode(ctxt, cur); |
| 2069 | while (cur != NULL) { |
| 2070 | /* TODO: need to work on entities -> stack */ |
| 2071 | if ((cur->children != NULL) && |
| 2072 | (cur->children->type != XML_ENTITY_DECL)) { |
| 2073 | cur = cur->children; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2074 | if (xmlXIncludeTestNode(ctxt, cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2075 | xmlXIncludePreProcessNode(ctxt, cur); |
| 2076 | } else if (cur->next != NULL) { |
| 2077 | cur = cur->next; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2078 | if (xmlXIncludeTestNode(ctxt, cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2079 | xmlXIncludePreProcessNode(ctxt, cur); |
| 2080 | } else { |
| 2081 | do { |
| 2082 | cur = cur->parent; |
| 2083 | if (cur == NULL) break; /* do */ |
| 2084 | if (cur->next != NULL) { |
| 2085 | cur = cur->next; |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2086 | if (xmlXIncludeTestNode(ctxt, cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2087 | xmlXIncludePreProcessNode(ctxt, cur); |
| 2088 | break; /* do */ |
| 2089 | } |
| 2090 | } while (cur != NULL); |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | /* |
| 2095 | * Second Phase : collect the infosets fragments |
| 2096 | */ |
Daniel Veillard | bbc72c3 | 2002-09-05 10:52:10 +0000 | [diff] [blame] | 2097 | for (i = ctxt->incBase;i < ctxt->incNr; i++) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2098 | xmlXIncludeLoadNode(ctxt, i); |
Daniel Veillard | 97fd567 | 2003-02-07 13:01:54 +0000 | [diff] [blame] | 2099 | ret++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | /* |
| 2103 | * Third phase: extend the original document infoset. |
| 2104 | */ |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2105 | if (ctxt->nbErrors == 0) { |
| 2106 | for (i = ctxt->incBase;i < ctxt->incNr; i++) { |
| 2107 | xmlXIncludeIncludeNode(ctxt, i); |
| 2108 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 2111 | if (doc->URL != NULL) |
| 2112 | xmlXIncludeURLPop(ctxt); |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2113 | return(ret); |
| 2114 | } |
| 2115 | |
| 2116 | /** |
| 2117 | * xmlXIncludeProcess: |
| 2118 | * @doc: an XML document |
| 2119 | * |
| 2120 | * Implement the XInclude substitution on the XML document @doc |
| 2121 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2122 | * Returns 0 if no substitution were done, -1 if some processing failed |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2123 | * or the number of substitutions done. |
| 2124 | */ |
| 2125 | int |
| 2126 | xmlXIncludeProcess(xmlDocPtr doc) { |
| 2127 | xmlXIncludeCtxtPtr ctxt; |
| 2128 | int ret = 0; |
| 2129 | |
| 2130 | if (doc == NULL) |
| 2131 | return(-1); |
| 2132 | ctxt = xmlXIncludeNewContext(doc); |
| 2133 | if (ctxt == NULL) |
| 2134 | return(-1); |
| 2135 | ret = xmlXIncludeDoProcess(ctxt, doc); |
Daniel Veillard | d581b7e | 2003-02-11 18:03:05 +0000 | [diff] [blame] | 2136 | if ((ret >= 0) && (ctxt->nbErrors > 0)) |
| 2137 | ret = -1; |
Daniel Veillard | d16df9f | 2001-05-23 13:44:21 +0000 | [diff] [blame] | 2138 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2139 | xmlXIncludeFreeContext(ctxt); |
| 2140 | return(ret); |
| 2141 | } |
| 2142 | |
| 2143 | #else /* !LIBXML_XINCLUDE_ENABLED */ |
| 2144 | #endif |