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