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