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