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