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