Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xmlsave.c: Implemetation of the document serializer |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * daniel@veillard.com |
| 7 | */ |
| 8 | |
| 9 | #define IN_LIBXML |
| 10 | #include "libxml.h" |
| 11 | |
| 12 | #include <string.h> |
| 13 | #include <libxml/xmlmemory.h> |
| 14 | #include <libxml/parserInternals.h> |
| 15 | #include <libxml/tree.h> |
| 16 | #include <libxml/xmlsave.h> |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 17 | |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 18 | #define MAX_INDENT 60 |
| 19 | |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 20 | #include <libxml/HTMLtree.h> |
| 21 | |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 22 | #include "buf.h" |
| 23 | #include "enc.h" |
| 24 | #include "save.h" |
| 25 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 26 | /************************************************************************ |
| 27 | * * |
| 28 | * XHTML detection * |
| 29 | * * |
| 30 | ************************************************************************/ |
| 31 | #define XHTML_STRICT_PUBLIC_ID BAD_CAST \ |
| 32 | "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 33 | #define XHTML_STRICT_SYSTEM_ID BAD_CAST \ |
| 34 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" |
| 35 | #define XHTML_FRAME_PUBLIC_ID BAD_CAST \ |
| 36 | "-//W3C//DTD XHTML 1.0 Frameset//EN" |
| 37 | #define XHTML_FRAME_SYSTEM_ID BAD_CAST \ |
| 38 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" |
| 39 | #define XHTML_TRANS_PUBLIC_ID BAD_CAST \ |
| 40 | "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 41 | #define XHTML_TRANS_SYSTEM_ID BAD_CAST \ |
| 42 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" |
| 43 | |
| 44 | #define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml" |
| 45 | /** |
| 46 | * xmlIsXHTML: |
| 47 | * @systemID: the system identifier |
| 48 | * @publicID: the public identifier |
| 49 | * |
| 50 | * Try to find if the document correspond to an XHTML DTD |
| 51 | * |
| 52 | * Returns 1 if true, 0 if not and -1 in case of error |
| 53 | */ |
| 54 | int |
| 55 | xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) { |
| 56 | if ((systemID == NULL) && (publicID == NULL)) |
| 57 | return(-1); |
| 58 | if (publicID != NULL) { |
| 59 | if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1); |
| 60 | if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1); |
| 61 | if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1); |
| 62 | } |
| 63 | if (systemID != NULL) { |
| 64 | if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1); |
| 65 | if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1); |
| 66 | if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1); |
| 67 | } |
| 68 | return(0); |
| 69 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 70 | |
| 71 | #ifdef LIBXML_OUTPUT_ENABLED |
| 72 | |
| 73 | #define TODO \ |
| 74 | xmlGenericError(xmlGenericErrorContext, \ |
| 75 | "Unimplemented block at %s:%d\n", \ |
| 76 | __FILE__, __LINE__); |
| 77 | |
| 78 | struct _xmlSaveCtxt { |
| 79 | void *_private; |
| 80 | int type; |
| 81 | int fd; |
| 82 | const xmlChar *filename; |
| 83 | const xmlChar *encoding; |
| 84 | xmlCharEncodingHandlerPtr handler; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 85 | xmlOutputBufferPtr buf; |
| 86 | xmlDocPtr doc; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 87 | int options; |
| 88 | int level; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 89 | int format; |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 90 | char indent[MAX_INDENT + 1]; /* array for indenting output */ |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 91 | int indent_nr; |
| 92 | int indent_size; |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 93 | xmlCharEncodingOutputFunc escape; /* used for element content */ |
| 94 | xmlCharEncodingOutputFunc escapeAttr;/* used for attribute content */ |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /************************************************************************ |
| 98 | * * |
| 99 | * Output error handlers * |
| 100 | * * |
| 101 | ************************************************************************/ |
| 102 | /** |
| 103 | * xmlSaveErrMemory: |
| 104 | * @extra: extra informations |
| 105 | * |
| 106 | * Handle an out of memory condition |
| 107 | */ |
| 108 | static void |
| 109 | xmlSaveErrMemory(const char *extra) |
| 110 | { |
| 111 | __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * xmlSaveErr: |
| 116 | * @code: the error number |
| 117 | * @node: the location of the error. |
| 118 | * @extra: extra informations |
| 119 | * |
| 120 | * Handle an out of memory condition |
| 121 | */ |
| 122 | static void |
| 123 | xmlSaveErr(int code, xmlNodePtr node, const char *extra) |
| 124 | { |
| 125 | const char *msg = NULL; |
| 126 | |
| 127 | switch(code) { |
| 128 | case XML_SAVE_NOT_UTF8: |
Rob Richards | 417b74d | 2006-08-15 23:14:24 +0000 | [diff] [blame] | 129 | msg = "string is not in UTF-8\n"; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 130 | break; |
| 131 | case XML_SAVE_CHAR_INVALID: |
Rob Richards | 417b74d | 2006-08-15 23:14:24 +0000 | [diff] [blame] | 132 | msg = "invalid character value\n"; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 133 | break; |
| 134 | case XML_SAVE_UNKNOWN_ENCODING: |
Rob Richards | 417b74d | 2006-08-15 23:14:24 +0000 | [diff] [blame] | 135 | msg = "unknown encoding %s\n"; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 136 | break; |
| 137 | case XML_SAVE_NO_DOCTYPE: |
Rob Richards | 417b74d | 2006-08-15 23:14:24 +0000 | [diff] [blame] | 138 | msg = "document has no DOCTYPE\n"; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 139 | break; |
| 140 | default: |
Rob Richards | 417b74d | 2006-08-15 23:14:24 +0000 | [diff] [blame] | 141 | msg = "unexpected error number\n"; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 142 | } |
| 143 | __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra); |
| 144 | } |
| 145 | |
| 146 | /************************************************************************ |
| 147 | * * |
Daniel Veillard | 83a75e0 | 2004-05-14 21:50:42 +0000 | [diff] [blame] | 148 | * Special escaping routines * |
| 149 | * * |
| 150 | ************************************************************************/ |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 151 | static unsigned char * |
| 152 | xmlSerializeHexCharRef(unsigned char *out, int val) { |
| 153 | unsigned char *ptr; |
| 154 | |
| 155 | *out++ = '&'; |
| 156 | *out++ = '#'; |
| 157 | *out++ = 'x'; |
| 158 | if (val < 0x10) ptr = out; |
| 159 | else if (val < 0x100) ptr = out + 1; |
| 160 | else if (val < 0x1000) ptr = out + 2; |
| 161 | else if (val < 0x10000) ptr = out + 3; |
| 162 | else if (val < 0x100000) ptr = out + 4; |
| 163 | else ptr = out + 5; |
| 164 | out = ptr + 1; |
| 165 | while (val > 0) { |
| 166 | switch (val & 0xF) { |
| 167 | case 0: *ptr-- = '0'; break; |
| 168 | case 1: *ptr-- = '1'; break; |
| 169 | case 2: *ptr-- = '2'; break; |
| 170 | case 3: *ptr-- = '3'; break; |
| 171 | case 4: *ptr-- = '4'; break; |
| 172 | case 5: *ptr-- = '5'; break; |
| 173 | case 6: *ptr-- = '6'; break; |
| 174 | case 7: *ptr-- = '7'; break; |
| 175 | case 8: *ptr-- = '8'; break; |
| 176 | case 9: *ptr-- = '9'; break; |
| 177 | case 0xA: *ptr-- = 'A'; break; |
| 178 | case 0xB: *ptr-- = 'B'; break; |
| 179 | case 0xC: *ptr-- = 'C'; break; |
| 180 | case 0xD: *ptr-- = 'D'; break; |
| 181 | case 0xE: *ptr-- = 'E'; break; |
| 182 | case 0xF: *ptr-- = 'F'; break; |
| 183 | default: *ptr-- = '0'; break; |
| 184 | } |
| 185 | val >>= 4; |
| 186 | } |
| 187 | *out++ = ';'; |
| 188 | *out = 0; |
| 189 | return(out); |
| 190 | } |
| 191 | |
Daniel Veillard | 83a75e0 | 2004-05-14 21:50:42 +0000 | [diff] [blame] | 192 | /** |
| 193 | * xmlEscapeEntities: |
| 194 | * @out: a pointer to an array of bytes to store the result |
| 195 | * @outlen: the length of @out |
| 196 | * @in: a pointer to an array of unescaped UTF-8 bytes |
| 197 | * @inlen: the length of @in |
| 198 | * |
| 199 | * Take a block of UTF-8 chars in and escape them. Used when there is no |
| 200 | * encoding specified. |
| 201 | * |
| 202 | * Returns 0 if success, or -1 otherwise |
| 203 | * The value of @inlen after return is the number of octets consumed |
| 204 | * if the return value is positive, else unpredictable. |
| 205 | * The value of @outlen after return is the number of octets consumed. |
| 206 | */ |
| 207 | static int |
| 208 | xmlEscapeEntities(unsigned char* out, int *outlen, |
| 209 | const xmlChar* in, int *inlen) { |
| 210 | unsigned char* outstart = out; |
| 211 | const unsigned char* base = in; |
| 212 | unsigned char* outend = out + *outlen; |
| 213 | const unsigned char* inend; |
| 214 | int val; |
| 215 | |
| 216 | inend = in + (*inlen); |
| 217 | |
| 218 | while ((in < inend) && (out < outend)) { |
| 219 | if (*in == '<') { |
| 220 | if (outend - out < 4) break; |
| 221 | *out++ = '&'; |
| 222 | *out++ = 'l'; |
| 223 | *out++ = 't'; |
| 224 | *out++ = ';'; |
| 225 | in++; |
| 226 | continue; |
| 227 | } else if (*in == '>') { |
| 228 | if (outend - out < 4) break; |
| 229 | *out++ = '&'; |
| 230 | *out++ = 'g'; |
| 231 | *out++ = 't'; |
| 232 | *out++ = ';'; |
| 233 | in++; |
| 234 | continue; |
| 235 | } else if (*in == '&') { |
| 236 | if (outend - out < 5) break; |
| 237 | *out++ = '&'; |
| 238 | *out++ = 'a'; |
| 239 | *out++ = 'm'; |
| 240 | *out++ = 'p'; |
| 241 | *out++ = ';'; |
| 242 | in++; |
| 243 | continue; |
| 244 | } else if (((*in >= 0x20) && (*in < 0x80)) || |
| 245 | (*in == '\n') || (*in == '\t')) { |
| 246 | /* |
| 247 | * default case, just copy ! |
| 248 | */ |
| 249 | *out++ = *in++; |
| 250 | continue; |
| 251 | } else if (*in >= 0x80) { |
| 252 | /* |
| 253 | * We assume we have UTF-8 input. |
| 254 | */ |
Daniel Veillard | 0795348 | 2012-01-22 17:42:35 +0800 | [diff] [blame] | 255 | if (outend - out < 11) break; |
Daniel Veillard | 83a75e0 | 2004-05-14 21:50:42 +0000 | [diff] [blame] | 256 | |
| 257 | if (*in < 0xC0) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 258 | xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL); |
Daniel Veillard | 83a75e0 | 2004-05-14 21:50:42 +0000 | [diff] [blame] | 259 | in++; |
| 260 | goto error; |
| 261 | } else if (*in < 0xE0) { |
| 262 | if (inend - in < 2) break; |
| 263 | val = (in[0]) & 0x1F; |
| 264 | val <<= 6; |
| 265 | val |= (in[1]) & 0x3F; |
| 266 | in += 2; |
| 267 | } else if (*in < 0xF0) { |
| 268 | if (inend - in < 3) break; |
| 269 | val = (in[0]) & 0x0F; |
| 270 | val <<= 6; |
| 271 | val |= (in[1]) & 0x3F; |
| 272 | val <<= 6; |
| 273 | val |= (in[2]) & 0x3F; |
| 274 | in += 3; |
| 275 | } else if (*in < 0xF8) { |
| 276 | if (inend - in < 4) break; |
| 277 | val = (in[0]) & 0x07; |
| 278 | val <<= 6; |
| 279 | val |= (in[1]) & 0x3F; |
| 280 | val <<= 6; |
| 281 | val |= (in[2]) & 0x3F; |
| 282 | val <<= 6; |
| 283 | val |= (in[3]) & 0x3F; |
| 284 | in += 4; |
| 285 | } else { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 286 | xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL); |
Daniel Veillard | 83a75e0 | 2004-05-14 21:50:42 +0000 | [diff] [blame] | 287 | in++; |
| 288 | goto error; |
| 289 | } |
| 290 | if (!IS_CHAR(val)) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 291 | xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL); |
Daniel Veillard | 83a75e0 | 2004-05-14 21:50:42 +0000 | [diff] [blame] | 292 | in++; |
| 293 | goto error; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * We could do multiple things here. Just save as a char ref |
| 298 | */ |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 299 | out = xmlSerializeHexCharRef(out, val); |
Daniel Veillard | 83a75e0 | 2004-05-14 21:50:42 +0000 | [diff] [blame] | 300 | } else if (IS_BYTE_CHAR(*in)) { |
| 301 | if (outend - out < 6) break; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 302 | out = xmlSerializeHexCharRef(out, *in++); |
Daniel Veillard | 83a75e0 | 2004-05-14 21:50:42 +0000 | [diff] [blame] | 303 | } else { |
| 304 | xmlGenericError(xmlGenericErrorContext, |
| 305 | "xmlEscapeEntities : char out of range\n"); |
| 306 | in++; |
| 307 | goto error; |
| 308 | } |
| 309 | } |
| 310 | *outlen = out - outstart; |
| 311 | *inlen = in - base; |
| 312 | return(0); |
| 313 | error: |
| 314 | *outlen = out - outstart; |
| 315 | *inlen = in - base; |
| 316 | return(-1); |
| 317 | } |
| 318 | |
| 319 | /************************************************************************ |
| 320 | * * |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 321 | * Allocation and deallocation * |
| 322 | * * |
| 323 | ************************************************************************/ |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 324 | /** |
| 325 | * xmlSaveCtxtInit: |
| 326 | * @ctxt: the saving context |
| 327 | * |
| 328 | * Initialize a saving context |
| 329 | */ |
| 330 | static void |
| 331 | xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt) |
| 332 | { |
| 333 | int i; |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 334 | int len; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 335 | |
| 336 | if (ctxt == NULL) return; |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 337 | if ((ctxt->encoding == NULL) && (ctxt->escape == NULL)) |
| 338 | ctxt->escape = xmlEscapeEntities; |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 339 | len = xmlStrlen((xmlChar *)xmlTreeIndentString); |
| 340 | if ((xmlTreeIndentString == NULL) || (len == 0)) { |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 341 | memset(&ctxt->indent[0], 0, MAX_INDENT + 1); |
| 342 | } else { |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 343 | ctxt->indent_size = len; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 344 | ctxt->indent_nr = MAX_INDENT / ctxt->indent_size; |
| 345 | for (i = 0;i < ctxt->indent_nr;i++) |
| 346 | memcpy(&ctxt->indent[i * ctxt->indent_size], xmlTreeIndentString, |
| 347 | ctxt->indent_size); |
| 348 | ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0; |
| 349 | } |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 350 | |
Daniel Veillard | 9a00fd2 | 2005-11-09 08:56:26 +0000 | [diff] [blame] | 351 | if (xmlSaveNoEmptyTags) { |
| 352 | ctxt->options |= XML_SAVE_NO_EMPTY; |
| 353 | } |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 354 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 355 | |
| 356 | /** |
| 357 | * xmlFreeSaveCtxt: |
| 358 | * |
| 359 | * Free a saving context, destroying the ouptut in any remaining buffer |
| 360 | */ |
| 361 | static void |
| 362 | xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt) |
| 363 | { |
| 364 | if (ctxt == NULL) return; |
| 365 | if (ctxt->encoding != NULL) |
| 366 | xmlFree((char *) ctxt->encoding); |
Daniel Veillard | e2161a6 | 2004-04-29 17:14:25 +0000 | [diff] [blame] | 367 | if (ctxt->buf != NULL) |
| 368 | xmlOutputBufferClose(ctxt->buf); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 369 | xmlFree(ctxt); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * xmlNewSaveCtxt: |
| 374 | * |
| 375 | * Create a new saving context |
| 376 | * |
| 377 | * Returns the new structure or NULL in case of error |
| 378 | */ |
| 379 | static xmlSaveCtxtPtr |
| 380 | xmlNewSaveCtxt(const char *encoding, int options) |
| 381 | { |
| 382 | xmlSaveCtxtPtr ret; |
| 383 | |
| 384 | ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt)); |
| 385 | if (ret == NULL) { |
| 386 | xmlSaveErrMemory("creating saving context"); |
| 387 | return ( NULL ); |
| 388 | } |
| 389 | memset(ret, 0, sizeof(xmlSaveCtxt)); |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 390 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 391 | if (encoding != NULL) { |
| 392 | ret->handler = xmlFindCharEncodingHandler(encoding); |
| 393 | if (ret->handler == NULL) { |
| 394 | xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding); |
| 395 | xmlFreeSaveCtxt(ret); |
| 396 | return(NULL); |
| 397 | } |
| 398 | ret->encoding = xmlStrdup((const xmlChar *)encoding); |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 399 | ret->escape = NULL; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 400 | } |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 401 | xmlSaveCtxtInit(ret); |
| 402 | |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 403 | /* |
| 404 | * Use the options |
| 405 | */ |
| 406 | |
Daniel Veillard | 9a00fd2 | 2005-11-09 08:56:26 +0000 | [diff] [blame] | 407 | /* Re-check this option as it may already have been set */ |
| 408 | if ((ret->options & XML_SAVE_NO_EMPTY) && ! (options & XML_SAVE_NO_EMPTY)) { |
| 409 | options |= XML_SAVE_NO_EMPTY; |
| 410 | } |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 411 | |
| 412 | ret->options = options; |
| 413 | if (options & XML_SAVE_FORMAT) |
| 414 | ret->format = 1; |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 415 | else if (options & XML_SAVE_WSNONSIG) |
| 416 | ret->format = 2; |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 417 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 418 | return(ret); |
| 419 | } |
| 420 | |
| 421 | /************************************************************************ |
| 422 | * * |
| 423 | * Dumping XML tree content to a simple buffer * |
| 424 | * * |
| 425 | ************************************************************************/ |
| 426 | /** |
| 427 | * xmlAttrSerializeContent: |
| 428 | * @buf: the XML buffer output |
| 429 | * @doc: the document |
| 430 | * @attr: the attribute pointer |
| 431 | * |
| 432 | * Serialize the attribute in the buffer |
| 433 | */ |
| 434 | static void |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 435 | xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr) |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 436 | { |
| 437 | xmlNodePtr children; |
| 438 | |
| 439 | children = attr->children; |
| 440 | while (children != NULL) { |
| 441 | switch (children->type) { |
| 442 | case XML_TEXT_NODE: |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 443 | xmlBufAttrSerializeTxtContent(buf->buffer, attr->doc, |
| 444 | attr, children->content); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 445 | break; |
| 446 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 447 | xmlBufAdd(buf->buffer, BAD_CAST "&", 1); |
| 448 | xmlBufAdd(buf->buffer, children->name, |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 449 | xmlStrlen(children->name)); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 450 | xmlBufAdd(buf->buffer, BAD_CAST ";", 1); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 451 | break; |
| 452 | default: |
| 453 | /* should not happen unless we have a badly built tree */ |
| 454 | break; |
| 455 | } |
| 456 | children = children->next; |
| 457 | } |
| 458 | } |
| 459 | |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 460 | /** |
| 461 | * xmlBufDumpNotationTable: |
| 462 | * @buf: an xmlBufPtr output |
| 463 | * @table: A notation table |
| 464 | * |
| 465 | * This will dump the content of the notation table as an XML DTD definition |
| 466 | */ |
| 467 | void |
| 468 | xmlBufDumpNotationTable(xmlBufPtr buf, xmlNotationTablePtr table) { |
| 469 | xmlBufferPtr buffer; |
| 470 | |
| 471 | buffer = xmlBufferCreate(); |
| 472 | if (buffer == NULL) { |
| 473 | /* |
| 474 | * TODO set the error in buf |
| 475 | */ |
| 476 | return; |
| 477 | } |
| 478 | xmlDumpNotationTable(buffer, table); |
| 479 | xmlBufMergeBuffer(buf, buffer); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * xmlBufDumpElementDecl: |
| 484 | * @buf: an xmlBufPtr output |
| 485 | * @elem: An element table |
| 486 | * |
| 487 | * This will dump the content of the element declaration as an XML |
| 488 | * DTD definition |
| 489 | */ |
| 490 | void |
| 491 | xmlBufDumpElementDecl(xmlBufPtr buf, xmlElementPtr elem) { |
| 492 | xmlBufferPtr buffer; |
| 493 | |
| 494 | buffer = xmlBufferCreate(); |
| 495 | if (buffer == NULL) { |
| 496 | /* |
| 497 | * TODO set the error in buf |
| 498 | */ |
| 499 | return; |
| 500 | } |
| 501 | xmlDumpElementDecl(buffer, elem); |
| 502 | xmlBufMergeBuffer(buf, buffer); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * xmlBufDumpAttributeDecl: |
| 507 | * @buf: an xmlBufPtr output |
| 508 | * @attr: An attribute declaration |
| 509 | * |
| 510 | * This will dump the content of the attribute declaration as an XML |
| 511 | * DTD definition |
| 512 | */ |
| 513 | void |
| 514 | xmlBufDumpAttributeDecl(xmlBufPtr buf, xmlAttributePtr attr) { |
| 515 | xmlBufferPtr buffer; |
| 516 | |
| 517 | buffer = xmlBufferCreate(); |
| 518 | if (buffer == NULL) { |
| 519 | /* |
| 520 | * TODO set the error in buf |
| 521 | */ |
| 522 | return; |
| 523 | } |
| 524 | xmlDumpAttributeDecl(buffer, attr); |
| 525 | xmlBufMergeBuffer(buf, buffer); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * xmlBufDumpEntityDecl: |
| 530 | * @buf: an xmlBufPtr output |
| 531 | * @ent: An entity table |
| 532 | * |
| 533 | * This will dump the content of the entity table as an XML DTD definition |
| 534 | */ |
| 535 | void |
| 536 | xmlBufDumpEntityDecl(xmlBufPtr buf, xmlEntityPtr ent) { |
| 537 | xmlBufferPtr buffer; |
| 538 | |
| 539 | buffer = xmlBufferCreate(); |
| 540 | if (buffer == NULL) { |
| 541 | /* |
| 542 | * TODO set the error in buf |
| 543 | */ |
| 544 | return; |
| 545 | } |
| 546 | xmlDumpEntityDecl(buffer, ent); |
| 547 | xmlBufMergeBuffer(buf, buffer); |
| 548 | } |
| 549 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 550 | /************************************************************************ |
| 551 | * * |
| 552 | * Dumping XML tree content to an I/O output buffer * |
| 553 | * * |
| 554 | ************************************************************************/ |
| 555 | |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 556 | static int xmlSaveSwitchEncoding(xmlSaveCtxtPtr ctxt, const char *encoding) { |
| 557 | xmlOutputBufferPtr buf = ctxt->buf; |
| 558 | |
| 559 | if ((encoding != NULL) && (buf->encoder == NULL) && (buf->conv == NULL)) { |
| 560 | buf->encoder = xmlFindCharEncodingHandler((const char *)encoding); |
| 561 | if (buf->encoder == NULL) { |
| 562 | xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, |
| 563 | (const char *)encoding); |
| 564 | return(-1); |
| 565 | } |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 566 | buf->conv = xmlBufCreate(); |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 567 | if (buf->conv == NULL) { |
| 568 | xmlCharEncCloseFunc(buf->encoder); |
| 569 | xmlSaveErrMemory("creating encoding buffer"); |
| 570 | return(-1); |
| 571 | } |
| 572 | /* |
| 573 | * initialize the state, e.g. if outputting a BOM |
| 574 | */ |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 575 | xmlCharEncOutput(buf, 1); |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 576 | } |
| 577 | return(0); |
| 578 | } |
| 579 | |
| 580 | static int xmlSaveClearEncoding(xmlSaveCtxtPtr ctxt) { |
| 581 | xmlOutputBufferPtr buf = ctxt->buf; |
| 582 | xmlOutputBufferFlush(buf); |
| 583 | xmlCharEncCloseFunc(buf->encoder); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 584 | xmlBufFree(buf->conv); |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 585 | buf->encoder = NULL; |
| 586 | buf->conv = NULL; |
| 587 | return(0); |
| 588 | } |
| 589 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 590 | #ifdef LIBXML_HTML_ENABLED |
| 591 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 592 | xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 593 | #endif |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 594 | static void xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur); |
| 595 | static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 596 | void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur); |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 597 | static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 598 | |
| 599 | /** |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 600 | * xmlOutputBufferWriteWSNonSig: |
| 601 | * @ctxt: The save context |
| 602 | * @extra: Number of extra indents to apply to ctxt->level |
| 603 | * |
| 604 | * Write out formatting for non-significant whitespace output. |
| 605 | */ |
| 606 | static void |
| 607 | xmlOutputBufferWriteWSNonSig(xmlSaveCtxtPtr ctxt, int extra) |
| 608 | { |
| 609 | int i; |
| 610 | if ((ctxt == NULL) || (ctxt->buf == NULL)) |
| 611 | return; |
| 612 | xmlOutputBufferWrite(ctxt->buf, 1, "\n"); |
| 613 | for (i = 0; i < (ctxt->level + extra); i += ctxt->indent_nr) { |
| 614 | xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size * |
| 615 | ((ctxt->level + extra - i) > ctxt->indent_nr ? |
| 616 | ctxt->indent_nr : (ctxt->level + extra - i)), |
| 617 | ctxt->indent); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /** |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 622 | * xmlNsDumpOutput: |
| 623 | * @buf: the XML buffer output |
| 624 | * @cur: a namespace |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 625 | * @ctxt: the output save context. Optional. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 626 | * |
| 627 | * Dump a local Namespace definition. |
| 628 | * Should be called in the context of attributes dumps. |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 629 | * If @ctxt is supplied, @buf should be its buffer. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 630 | */ |
| 631 | static void |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 632 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur, xmlSaveCtxtPtr ctxt) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 633 | if ((cur == NULL) || (buf == NULL)) return; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 634 | if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) { |
| 635 | if (xmlStrEqual(cur->prefix, BAD_CAST "xml")) |
| 636 | return; |
| 637 | |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 638 | if (ctxt != NULL && ctxt->format == 2) |
| 639 | xmlOutputBufferWriteWSNonSig(ctxt, 2); |
| 640 | else |
| 641 | xmlOutputBufferWrite(buf, 1, " "); |
| 642 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 643 | /* Within the context of an element attributes */ |
| 644 | if (cur->prefix != NULL) { |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 645 | xmlOutputBufferWrite(buf, 6, "xmlns:"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 646 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 647 | } else |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 648 | xmlOutputBufferWrite(buf, 5, "xmlns"); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 649 | xmlOutputBufferWrite(buf, 1, "="); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 650 | xmlBufWriteQuotedString(buf->buffer, cur->href); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | /** |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 655 | * xmlNsDumpOutputCtxt |
| 656 | * @ctxt: the save context |
| 657 | * @cur: a namespace |
| 658 | * |
| 659 | * Dump a local Namespace definition to a save context. |
| 660 | * Should be called in the context of attribute dumps. |
| 661 | */ |
| 662 | static void |
| 663 | xmlNsDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) { |
| 664 | xmlNsDumpOutput(ctxt->buf, cur, ctxt); |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * xmlNsListDumpOutputCtxt |
| 669 | * @ctxt: the save context |
| 670 | * @cur: the first namespace |
| 671 | * |
| 672 | * Dump a list of local namespace definitions to a save context. |
| 673 | * Should be called in the context of attribute dumps. |
| 674 | */ |
| 675 | static void |
| 676 | xmlNsListDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) { |
| 677 | while (cur != NULL) { |
| 678 | xmlNsDumpOutput(ctxt->buf, cur, ctxt); |
| 679 | cur = cur->next; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /** |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 684 | * xmlNsListDumpOutput: |
| 685 | * @buf: the XML buffer output |
| 686 | * @cur: the first namespace |
| 687 | * |
| 688 | * Dump a list of local Namespace definitions. |
| 689 | * Should be called in the context of attributes dumps. |
| 690 | */ |
| 691 | void |
| 692 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 693 | while (cur != NULL) { |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 694 | xmlNsDumpOutput(buf, cur, NULL); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 695 | cur = cur->next; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * xmlDtdDumpOutput: |
| 701 | * @buf: the XML buffer output |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 702 | * @dtd: the pointer to the DTD |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 703 | * |
| 704 | * Dump the XML document DTD, if any. |
| 705 | */ |
| 706 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 707 | xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) { |
| 708 | xmlOutputBufferPtr buf; |
| 709 | int format, level; |
| 710 | xmlDocPtr doc; |
| 711 | |
| 712 | if (dtd == NULL) return; |
| 713 | if ((ctxt == NULL) || (ctxt->buf == NULL)) |
| 714 | return; |
| 715 | buf = ctxt->buf; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 716 | xmlOutputBufferWrite(buf, 10, "<!DOCTYPE "); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 717 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 718 | if (dtd->ExternalID != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 719 | xmlOutputBufferWrite(buf, 8, " PUBLIC "); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 720 | xmlBufWriteQuotedString(buf->buffer, dtd->ExternalID); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 721 | xmlOutputBufferWrite(buf, 1, " "); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 722 | xmlBufWriteQuotedString(buf->buffer, dtd->SystemID); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 723 | } else if (dtd->SystemID != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 724 | xmlOutputBufferWrite(buf, 8, " SYSTEM "); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 725 | xmlBufWriteQuotedString(buf->buffer, dtd->SystemID); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 726 | } |
| 727 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
Daniel Veillard | 41c4a75 | 2004-09-08 20:55:38 +0000 | [diff] [blame] | 728 | (dtd->attributes == NULL) && (dtd->notations == NULL) && |
| 729 | (dtd->pentities == NULL)) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 730 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 731 | return; |
| 732 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 733 | xmlOutputBufferWrite(buf, 3, " [\n"); |
Daniel Veillard | 41c4a75 | 2004-09-08 20:55:38 +0000 | [diff] [blame] | 734 | /* |
| 735 | * Dump the notations first they are not in the DTD children list |
| 736 | * Do this only on a standalone DTD or on the internal subset though. |
| 737 | */ |
| 738 | if ((dtd->notations != NULL) && ((dtd->doc == NULL) || |
| 739 | (dtd->doc->intSubset == dtd))) { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 740 | xmlBufDumpNotationTable(buf->buffer, |
| 741 | (xmlNotationTablePtr) dtd->notations); |
Daniel Veillard | da3b29a | 2004-08-14 11:15:13 +0000 | [diff] [blame] | 742 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 743 | format = ctxt->format; |
| 744 | level = ctxt->level; |
| 745 | doc = ctxt->doc; |
| 746 | ctxt->format = 0; |
| 747 | ctxt->level = -1; |
| 748 | ctxt->doc = dtd->doc; |
| 749 | xmlNodeListDumpOutput(ctxt, dtd->children); |
| 750 | ctxt->format = format; |
| 751 | ctxt->level = level; |
| 752 | ctxt->doc = doc; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 753 | xmlOutputBufferWrite(buf, 2, "]>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /** |
| 757 | * xmlAttrDumpOutput: |
| 758 | * @buf: the XML buffer output |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 759 | * @cur: the attribute pointer |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 760 | * |
| 761 | * Dump an XML attribute |
| 762 | */ |
| 763 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 764 | xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) { |
| 765 | xmlOutputBufferPtr buf; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 766 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 767 | if (cur == NULL) return; |
| 768 | buf = ctxt->buf; |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 769 | if (buf == NULL) return; |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 770 | if (ctxt->format == 2) |
| 771 | xmlOutputBufferWriteWSNonSig(ctxt, 2); |
| 772 | else |
| 773 | xmlOutputBufferWrite(buf, 1, " "); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 774 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 775 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 776 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 777 | } |
| 778 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 779 | xmlOutputBufferWrite(buf, 2, "=\""); |
| 780 | xmlAttrSerializeContent(buf, cur); |
| 781 | xmlOutputBufferWrite(buf, 1, "\""); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /** |
| 785 | * xmlAttrListDumpOutput: |
| 786 | * @buf: the XML buffer output |
| 787 | * @doc: the document |
| 788 | * @cur: the first attribute pointer |
| 789 | * @encoding: an optional encoding string |
| 790 | * |
| 791 | * Dump a list of XML attributes |
| 792 | */ |
| 793 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 794 | xmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) { |
| 795 | if (cur == NULL) return; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 796 | while (cur != NULL) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 797 | xmlAttrDumpOutput(ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 798 | cur = cur->next; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | |
| 803 | |
| 804 | /** |
| 805 | * xmlNodeListDumpOutput: |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 806 | * @cur: the first node |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 807 | * |
| 808 | * Dump an XML node list, recursive behaviour, children are printed too. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 809 | */ |
| 810 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 811 | xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 812 | xmlOutputBufferPtr buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 813 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 814 | if (cur == NULL) return; |
| 815 | buf = ctxt->buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 816 | while (cur != NULL) { |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 817 | if ((ctxt->format == 1) && (xmlIndentTreeOutput) && |
Daniel Veillard | bd44484 | 2007-03-20 08:47:29 +0000 | [diff] [blame] | 818 | ((cur->type == XML_ELEMENT_NODE) || |
| 819 | (cur->type == XML_COMMENT_NODE) || |
| 820 | (cur->type == XML_PI_NODE))) |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 821 | xmlOutputBufferWrite(buf, ctxt->indent_size * |
| 822 | (ctxt->level > ctxt->indent_nr ? |
| 823 | ctxt->indent_nr : ctxt->level), |
| 824 | ctxt->indent); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 825 | xmlNodeDumpOutputInternal(ctxt, cur); |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 826 | if (ctxt->format == 1) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 827 | xmlOutputBufferWrite(buf, 1, "\n"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 828 | } |
| 829 | cur = cur->next; |
| 830 | } |
| 831 | } |
| 832 | |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 833 | #ifdef LIBXML_HTML_ENABLED |
| 834 | /** |
| 835 | * xmlNodeDumpOutputInternal: |
| 836 | * @cur: the current node |
| 837 | * |
| 838 | * Dump an HTML node, recursive behaviour, children are printed too. |
| 839 | */ |
| 840 | static int |
| 841 | htmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) { |
| 842 | const xmlChar *oldenc = NULL; |
| 843 | const xmlChar *oldctxtenc = ctxt->encoding; |
| 844 | const xmlChar *encoding = ctxt->encoding; |
| 845 | xmlOutputBufferPtr buf = ctxt->buf; |
| 846 | int switched_encoding = 0; |
| 847 | xmlDocPtr doc; |
| 848 | |
| 849 | xmlInitParser(); |
| 850 | |
Daniel Veillard | 141ebfa | 2009-09-02 14:58:13 +0200 | [diff] [blame] | 851 | doc = cur->doc; |
| 852 | if (doc != NULL) { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 853 | oldenc = doc->encoding; |
| 854 | if (ctxt->encoding != NULL) { |
| 855 | doc->encoding = BAD_CAST ctxt->encoding; |
| 856 | } else if (doc->encoding != NULL) { |
| 857 | encoding = doc->encoding; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | if ((encoding != NULL) && (doc != NULL)) |
| 862 | htmlSetMetaEncoding(doc, (const xmlChar *) encoding); |
| 863 | if ((encoding == NULL) && (doc != NULL)) |
| 864 | encoding = htmlGetMetaEncoding(doc); |
| 865 | if (encoding == NULL) |
| 866 | encoding = BAD_CAST "HTML"; |
| 867 | if ((encoding != NULL) && (oldctxtenc == NULL) && |
| 868 | (buf->encoder == NULL) && (buf->conv == NULL)) { |
| 869 | if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) { |
| 870 | doc->encoding = oldenc; |
| 871 | return(-1); |
| 872 | } |
| 873 | switched_encoding = 1; |
| 874 | } |
| 875 | if (ctxt->options & XML_SAVE_FORMAT) |
| 876 | htmlNodeDumpFormatOutput(buf, doc, cur, |
| 877 | (const char *)encoding, 1); |
| 878 | else |
| 879 | htmlNodeDumpFormatOutput(buf, doc, cur, |
| 880 | (const char *)encoding, 0); |
| 881 | /* |
| 882 | * Restore the state of the saving context at the end of the document |
| 883 | */ |
| 884 | if ((switched_encoding) && (oldctxtenc == NULL)) { |
| 885 | xmlSaveClearEncoding(ctxt); |
| 886 | } |
| 887 | if (doc != NULL) |
| 888 | doc->encoding = oldenc; |
| 889 | return(0); |
| 890 | } |
| 891 | #endif |
| 892 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 893 | /** |
| 894 | * xmlNodeDumpOutputInternal: |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 895 | * @cur: the current node |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 896 | * |
| 897 | * Dump an XML node, recursive behaviour, children are printed too. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 898 | */ |
| 899 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 900 | xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 901 | int format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 902 | xmlNodePtr tmp; |
| 903 | xmlChar *start, *end; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 904 | xmlOutputBufferPtr buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 905 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 906 | if (cur == NULL) return; |
| 907 | buf = ctxt->buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 908 | if (cur->type == XML_XINCLUDE_START) |
| 909 | return; |
| 910 | if (cur->type == XML_XINCLUDE_END) |
| 911 | return; |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 912 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 913 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 914 | xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur); |
| 915 | return; |
| 916 | } |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 917 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 918 | if (ctxt->options & XML_SAVE_XHTML) { |
| 919 | xhtmlNodeDumpOutput(ctxt, cur); |
| 920 | return; |
| 921 | } |
| 922 | if (((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL) && |
| 923 | (cur->doc->type == XML_HTML_DOCUMENT_NODE) && |
| 924 | ((ctxt->options & XML_SAVE_AS_XML) == 0)) || |
| 925 | (ctxt->options & XML_SAVE_AS_HTML)) { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 926 | htmlNodeDumpOutputInternal(ctxt, cur); |
| 927 | return; |
| 928 | } |
| 929 | #endif |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 930 | if (cur->type == XML_DTD_NODE) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 931 | xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 932 | return; |
| 933 | } |
| 934 | if (cur->type == XML_DOCUMENT_FRAG_NODE) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 935 | xmlNodeListDumpOutput(ctxt, cur->children); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 936 | return; |
| 937 | } |
| 938 | if (cur->type == XML_ELEMENT_DECL) { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 939 | xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 940 | return; |
| 941 | } |
| 942 | if (cur->type == XML_ATTRIBUTE_DECL) { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 943 | xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 944 | return; |
| 945 | } |
| 946 | if (cur->type == XML_ENTITY_DECL) { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 947 | xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 948 | return; |
| 949 | } |
| 950 | if (cur->type == XML_TEXT_NODE) { |
| 951 | if (cur->content != NULL) { |
William M. Brack | 4e1c2db | 2005-02-11 10:58:55 +0000 | [diff] [blame] | 952 | if (cur->name != xmlStringTextNoenc) { |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 953 | xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 954 | } else { |
| 955 | /* |
| 956 | * Disable escaping, needed for XSLT |
| 957 | */ |
| 958 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | return; |
| 963 | } |
| 964 | if (cur->type == XML_PI_NODE) { |
| 965 | if (cur->content != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 966 | xmlOutputBufferWrite(buf, 2, "<?"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 967 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 968 | if (cur->content != NULL) { |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 969 | if (ctxt->format == 2) |
| 970 | xmlOutputBufferWriteWSNonSig(ctxt, 0); |
| 971 | else |
| 972 | xmlOutputBufferWrite(buf, 1, " "); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 973 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 974 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 975 | xmlOutputBufferWrite(buf, 2, "?>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 976 | } else { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 977 | xmlOutputBufferWrite(buf, 2, "<?"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 978 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 979 | if (ctxt->format == 2) |
| 980 | xmlOutputBufferWriteWSNonSig(ctxt, 0); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 981 | xmlOutputBufferWrite(buf, 2, "?>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 982 | } |
| 983 | return; |
| 984 | } |
| 985 | if (cur->type == XML_COMMENT_NODE) { |
| 986 | if (cur->content != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 987 | xmlOutputBufferWrite(buf, 4, "<!--"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 988 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 989 | xmlOutputBufferWrite(buf, 3, "-->"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 990 | } |
| 991 | return; |
| 992 | } |
| 993 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 994 | xmlOutputBufferWrite(buf, 1, "&"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 995 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 996 | xmlOutputBufferWrite(buf, 1, ";"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 997 | return; |
| 998 | } |
| 999 | if (cur->type == XML_CDATA_SECTION_NODE) { |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 1000 | if (cur->content == NULL || *cur->content == '\0') { |
| 1001 | xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>"); |
Daniel Veillard | 7cd517c | 2005-05-20 18:47:22 +0000 | [diff] [blame] | 1002 | } else { |
| 1003 | start = end = cur->content; |
| 1004 | while (*end != '\0') { |
| 1005 | if ((*end == ']') && (*(end + 1) == ']') && |
| 1006 | (*(end + 2) == '>')) { |
| 1007 | end = end + 2; |
| 1008 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
| 1009 | xmlOutputBufferWrite(buf, end - start, (const char *)start); |
| 1010 | xmlOutputBufferWrite(buf, 3, "]]>"); |
| 1011 | start = end; |
| 1012 | } |
| 1013 | end++; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1014 | } |
Daniel Veillard | 7cd517c | 2005-05-20 18:47:22 +0000 | [diff] [blame] | 1015 | if (start != end) { |
| 1016 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
| 1017 | xmlOutputBufferWriteString(buf, (const char *)start); |
| 1018 | xmlOutputBufferWrite(buf, 3, "]]>"); |
| 1019 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1020 | } |
| 1021 | return; |
| 1022 | } |
| 1023 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1024 | xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1025 | return; |
| 1026 | } |
| 1027 | if (cur->type == XML_NAMESPACE_DECL) { |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 1028 | xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1032 | format = ctxt->format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1033 | if (format == 1) { |
| 1034 | tmp = cur->children; |
| 1035 | while (tmp != NULL) { |
| 1036 | if ((tmp->type == XML_TEXT_NODE) || |
| 1037 | (tmp->type == XML_CDATA_SECTION_NODE) || |
| 1038 | (tmp->type == XML_ENTITY_REF_NODE)) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1039 | ctxt->format = 0; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1040 | break; |
| 1041 | } |
| 1042 | tmp = tmp->next; |
| 1043 | } |
| 1044 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1045 | xmlOutputBufferWrite(buf, 1, "<"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1046 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 1047 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1048 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 1052 | if (cur->nsDef) |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 1053 | xmlNsListDumpOutputCtxt(ctxt, cur->nsDef); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1054 | if (cur->properties != NULL) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1055 | xmlAttrListDumpOutput(ctxt, cur->properties); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1056 | |
| 1057 | if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) && |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 1058 | (cur->children == NULL) && ((ctxt->options & XML_SAVE_NO_EMPTY) == 0)) { |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 1059 | if (ctxt->format == 2) |
| 1060 | xmlOutputBufferWriteWSNonSig(ctxt, 0); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1061 | xmlOutputBufferWrite(buf, 2, "/>"); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1062 | ctxt->format = format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1063 | return; |
| 1064 | } |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 1065 | if (ctxt->format == 2) |
| 1066 | xmlOutputBufferWriteWSNonSig(ctxt, 1); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1067 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1068 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 1069 | xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1070 | } |
| 1071 | if (cur->children != NULL) { |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 1072 | if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n"); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1073 | if (ctxt->level >= 0) ctxt->level++; |
| 1074 | xmlNodeListDumpOutput(ctxt, cur->children); |
| 1075 | if (ctxt->level > 0) ctxt->level--; |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 1076 | if ((xmlIndentTreeOutput) && (ctxt->format == 1)) |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 1077 | xmlOutputBufferWrite(buf, ctxt->indent_size * |
| 1078 | (ctxt->level > ctxt->indent_nr ? |
| 1079 | ctxt->indent_nr : ctxt->level), |
| 1080 | ctxt->indent); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1081 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1082 | xmlOutputBufferWrite(buf, 2, "</"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1083 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 1084 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1085 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 1089 | if (ctxt->format == 2) |
| 1090 | xmlOutputBufferWriteWSNonSig(ctxt, 0); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1091 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1092 | ctxt->format = format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * xmlDocContentDumpOutput: |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1097 | * @cur: the document |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1098 | * |
| 1099 | * Dump an XML document. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1100 | */ |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1101 | static int |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1102 | xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1103 | #ifdef LIBXML_HTML_ENABLED |
| 1104 | xmlDtdPtr dtd; |
| 1105 | int is_xhtml = 0; |
| 1106 | #endif |
| 1107 | const xmlChar *oldenc = cur->encoding; |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1108 | const xmlChar *oldctxtenc = ctxt->encoding; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1109 | const xmlChar *encoding = ctxt->encoding; |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1110 | xmlCharEncodingOutputFunc oldescape = ctxt->escape; |
| 1111 | xmlCharEncodingOutputFunc oldescapeAttr = ctxt->escapeAttr; |
| 1112 | xmlOutputBufferPtr buf = ctxt->buf; |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1113 | xmlCharEncoding enc; |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1114 | int switched_encoding = 0; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1115 | |
| 1116 | xmlInitParser(); |
| 1117 | |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1118 | if ((cur->type != XML_HTML_DOCUMENT_NODE) && |
| 1119 | (cur->type != XML_DOCUMENT_NODE)) |
| 1120 | return(-1); |
| 1121 | |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1122 | if (ctxt->encoding != NULL) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1123 | cur->encoding = BAD_CAST ctxt->encoding; |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1124 | } else if (cur->encoding != NULL) { |
| 1125 | encoding = cur->encoding; |
| 1126 | } else if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
| 1127 | encoding = (const xmlChar *) |
| 1128 | xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
| 1129 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1130 | |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 1131 | if (((cur->type == XML_HTML_DOCUMENT_NODE) && |
| 1132 | ((ctxt->options & XML_SAVE_AS_XML) == 0) && |
| 1133 | ((ctxt->options & XML_SAVE_XHTML) == 0)) || |
| 1134 | (ctxt->options & XML_SAVE_AS_HTML)) { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1135 | #ifdef LIBXML_HTML_ENABLED |
| 1136 | if (encoding != NULL) |
| 1137 | htmlSetMetaEncoding(cur, (const xmlChar *) encoding); |
| 1138 | if (encoding == NULL) |
| 1139 | encoding = htmlGetMetaEncoding(cur); |
| 1140 | if (encoding == NULL) |
| 1141 | encoding = BAD_CAST "HTML"; |
| 1142 | if ((encoding != NULL) && (oldctxtenc == NULL) && |
| 1143 | (buf->encoder == NULL) && (buf->conv == NULL)) { |
| 1144 | if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) { |
| 1145 | cur->encoding = oldenc; |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1146 | return(-1); |
| 1147 | } |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1148 | } |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1149 | if (ctxt->options & XML_SAVE_FORMAT) |
| 1150 | htmlDocContentDumpFormatOutput(buf, cur, |
| 1151 | (const char *)encoding, 1); |
Daniel Veillard | 100e180 | 2005-08-08 14:44:11 +0000 | [diff] [blame] | 1152 | else |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1153 | htmlDocContentDumpFormatOutput(buf, cur, |
| 1154 | (const char *)encoding, 0); |
| 1155 | if (ctxt->encoding != NULL) |
| 1156 | cur->encoding = oldenc; |
| 1157 | return(0); |
| 1158 | #else |
| 1159 | return(-1); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1160 | #endif |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 1161 | } else if ((cur->type == XML_DOCUMENT_NODE) || |
| 1162 | (ctxt->options & XML_SAVE_AS_XML) || |
| 1163 | (ctxt->options & XML_SAVE_XHTML)) { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1164 | enc = xmlParseCharEncoding((const char*) encoding); |
| 1165 | if ((encoding != NULL) && (oldctxtenc == NULL) && |
| 1166 | (buf->encoder == NULL) && (buf->conv == NULL) && |
| 1167 | ((ctxt->options & XML_SAVE_NO_DECL) == 0)) { |
| 1168 | if ((enc != XML_CHAR_ENCODING_UTF8) && |
| 1169 | (enc != XML_CHAR_ENCODING_NONE) && |
| 1170 | (enc != XML_CHAR_ENCODING_ASCII)) { |
| 1171 | /* |
| 1172 | * we need to switch to this encoding but just for this |
| 1173 | * document since we output the XMLDecl the conversion |
| 1174 | * must be done to not generate not well formed documents. |
| 1175 | */ |
| 1176 | if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) { |
| 1177 | cur->encoding = oldenc; |
| 1178 | return(-1); |
| 1179 | } |
| 1180 | switched_encoding = 1; |
| 1181 | } |
| 1182 | if (ctxt->escape == xmlEscapeEntities) |
| 1183 | ctxt->escape = NULL; |
| 1184 | if (ctxt->escapeAttr == xmlEscapeEntities) |
| 1185 | ctxt->escapeAttr = NULL; |
| 1186 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1187 | |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1188 | |
| 1189 | /* |
| 1190 | * Save the XML declaration |
| 1191 | */ |
| 1192 | if ((ctxt->options & XML_SAVE_NO_DECL) == 0) { |
| 1193 | xmlOutputBufferWrite(buf, 14, "<?xml version="); |
| 1194 | if (cur->version != NULL) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 1195 | xmlBufWriteQuotedString(buf->buffer, cur->version); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1196 | else |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1197 | xmlOutputBufferWrite(buf, 5, "\"1.0\""); |
| 1198 | if (encoding != NULL) { |
| 1199 | xmlOutputBufferWrite(buf, 10, " encoding="); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 1200 | xmlBufWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1201 | } |
| 1202 | switch (cur->standalone) { |
| 1203 | case 0: |
| 1204 | xmlOutputBufferWrite(buf, 16, " standalone=\"no\""); |
| 1205 | break; |
| 1206 | case 1: |
| 1207 | xmlOutputBufferWrite(buf, 17, " standalone=\"yes\""); |
| 1208 | break; |
| 1209 | } |
| 1210 | xmlOutputBufferWrite(buf, 3, "?>\n"); |
| 1211 | } |
| 1212 | |
| 1213 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 1214 | if (ctxt->options & XML_SAVE_XHTML) |
| 1215 | is_xhtml = 1; |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1216 | if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) { |
| 1217 | dtd = xmlGetIntSubset(cur); |
| 1218 | if (dtd != NULL) { |
| 1219 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 1220 | if (is_xhtml < 0) is_xhtml = 0; |
| 1221 | } |
| 1222 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1223 | #endif |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1224 | if (cur->children != NULL) { |
| 1225 | xmlNodePtr child = cur->children; |
| 1226 | |
| 1227 | while (child != NULL) { |
| 1228 | ctxt->level = 0; |
| 1229 | #ifdef LIBXML_HTML_ENABLED |
| 1230 | if (is_xhtml) |
| 1231 | xhtmlNodeDumpOutput(ctxt, child); |
| 1232 | else |
| 1233 | #endif |
| 1234 | xmlNodeDumpOutputInternal(ctxt, child); |
| 1235 | xmlOutputBufferWrite(buf, 1, "\n"); |
| 1236 | child = child->next; |
| 1237 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1238 | } |
| 1239 | } |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1240 | |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1241 | /* |
| 1242 | * Restore the state of the saving context at the end of the document |
| 1243 | */ |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1244 | if ((switched_encoding) && (oldctxtenc == NULL)) { |
| 1245 | xmlSaveClearEncoding(ctxt); |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1246 | ctxt->escape = oldescape; |
| 1247 | ctxt->escapeAttr = oldescapeAttr; |
| 1248 | } |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 1249 | cur->encoding = oldenc; |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1250 | return(0); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | #ifdef LIBXML_HTML_ENABLED |
| 1254 | /************************************************************************ |
| 1255 | * * |
| 1256 | * Functions specific to XHTML serialization * |
| 1257 | * * |
| 1258 | ************************************************************************/ |
| 1259 | |
| 1260 | /** |
| 1261 | * xhtmlIsEmpty: |
| 1262 | * @node: the node |
| 1263 | * |
| 1264 | * Check if a node is an empty xhtml node |
| 1265 | * |
| 1266 | * Returns 1 if the node is an empty node, 0 if not and -1 in case of error |
| 1267 | */ |
| 1268 | static int |
| 1269 | xhtmlIsEmpty(xmlNodePtr node) { |
| 1270 | if (node == NULL) |
| 1271 | return(-1); |
| 1272 | if (node->type != XML_ELEMENT_NODE) |
| 1273 | return(0); |
| 1274 | if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME))) |
| 1275 | return(0); |
| 1276 | if (node->children != NULL) |
| 1277 | return(0); |
| 1278 | switch (node->name[0]) { |
| 1279 | case 'a': |
| 1280 | if (xmlStrEqual(node->name, BAD_CAST "area")) |
| 1281 | return(1); |
| 1282 | return(0); |
| 1283 | case 'b': |
| 1284 | if (xmlStrEqual(node->name, BAD_CAST "br")) |
| 1285 | return(1); |
| 1286 | if (xmlStrEqual(node->name, BAD_CAST "base")) |
| 1287 | return(1); |
| 1288 | if (xmlStrEqual(node->name, BAD_CAST "basefont")) |
| 1289 | return(1); |
| 1290 | return(0); |
| 1291 | case 'c': |
| 1292 | if (xmlStrEqual(node->name, BAD_CAST "col")) |
| 1293 | return(1); |
| 1294 | return(0); |
| 1295 | case 'f': |
| 1296 | if (xmlStrEqual(node->name, BAD_CAST "frame")) |
| 1297 | return(1); |
| 1298 | return(0); |
| 1299 | case 'h': |
| 1300 | if (xmlStrEqual(node->name, BAD_CAST "hr")) |
| 1301 | return(1); |
| 1302 | return(0); |
| 1303 | case 'i': |
| 1304 | if (xmlStrEqual(node->name, BAD_CAST "img")) |
| 1305 | return(1); |
| 1306 | if (xmlStrEqual(node->name, BAD_CAST "input")) |
| 1307 | return(1); |
| 1308 | if (xmlStrEqual(node->name, BAD_CAST "isindex")) |
| 1309 | return(1); |
| 1310 | return(0); |
| 1311 | case 'l': |
| 1312 | if (xmlStrEqual(node->name, BAD_CAST "link")) |
| 1313 | return(1); |
| 1314 | return(0); |
| 1315 | case 'm': |
| 1316 | if (xmlStrEqual(node->name, BAD_CAST "meta")) |
| 1317 | return(1); |
| 1318 | return(0); |
| 1319 | case 'p': |
| 1320 | if (xmlStrEqual(node->name, BAD_CAST "param")) |
| 1321 | return(1); |
| 1322 | return(0); |
| 1323 | } |
| 1324 | return(0); |
| 1325 | } |
| 1326 | |
| 1327 | /** |
| 1328 | * xhtmlAttrListDumpOutput: |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1329 | * @cur: the first attribute pointer |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1330 | * |
| 1331 | * Dump a list of XML attributes |
| 1332 | */ |
| 1333 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1334 | xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1335 | xmlAttrPtr xml_lang = NULL; |
| 1336 | xmlAttrPtr lang = NULL; |
| 1337 | xmlAttrPtr name = NULL; |
| 1338 | xmlAttrPtr id = NULL; |
| 1339 | xmlNodePtr parent; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1340 | xmlOutputBufferPtr buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1341 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1342 | if (cur == NULL) return; |
| 1343 | buf = ctxt->buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1344 | parent = cur->parent; |
| 1345 | while (cur != NULL) { |
| 1346 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id"))) |
| 1347 | id = cur; |
| 1348 | else |
| 1349 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name"))) |
| 1350 | name = cur; |
| 1351 | else |
| 1352 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang"))) |
| 1353 | lang = cur; |
| 1354 | else |
| 1355 | if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) && |
| 1356 | (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml"))) |
| 1357 | xml_lang = cur; |
| 1358 | else if ((cur->ns == NULL) && |
| 1359 | ((cur->children == NULL) || |
| 1360 | (cur->children->content == NULL) || |
| 1361 | (cur->children->content[0] == 0)) && |
| 1362 | (htmlIsBooleanAttr(cur->name))) { |
| 1363 | if (cur->children != NULL) |
| 1364 | xmlFreeNode(cur->children); |
| 1365 | cur->children = xmlNewText(cur->name); |
| 1366 | if (cur->children != NULL) |
| 1367 | cur->children->parent = (xmlNodePtr) cur; |
| 1368 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1369 | xmlAttrDumpOutput(ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1370 | cur = cur->next; |
| 1371 | } |
| 1372 | /* |
| 1373 | * C.8 |
| 1374 | */ |
| 1375 | if ((name != NULL) && (id == NULL)) { |
| 1376 | if ((parent != NULL) && (parent->name != NULL) && |
| 1377 | ((xmlStrEqual(parent->name, BAD_CAST "a")) || |
| 1378 | (xmlStrEqual(parent->name, BAD_CAST "p")) || |
| 1379 | (xmlStrEqual(parent->name, BAD_CAST "div")) || |
| 1380 | (xmlStrEqual(parent->name, BAD_CAST "img")) || |
| 1381 | (xmlStrEqual(parent->name, BAD_CAST "map")) || |
| 1382 | (xmlStrEqual(parent->name, BAD_CAST "applet")) || |
| 1383 | (xmlStrEqual(parent->name, BAD_CAST "form")) || |
| 1384 | (xmlStrEqual(parent->name, BAD_CAST "frame")) || |
| 1385 | (xmlStrEqual(parent->name, BAD_CAST "iframe")))) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1386 | xmlOutputBufferWrite(buf, 5, " id=\""); |
| 1387 | xmlAttrSerializeContent(buf, name); |
| 1388 | xmlOutputBufferWrite(buf, 1, "\""); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | /* |
| 1392 | * C.7. |
| 1393 | */ |
| 1394 | if ((lang != NULL) && (xml_lang == NULL)) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1395 | xmlOutputBufferWrite(buf, 11, " xml:lang=\""); |
| 1396 | xmlAttrSerializeContent(buf, lang); |
| 1397 | xmlOutputBufferWrite(buf, 1, "\""); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1398 | } else |
| 1399 | if ((xml_lang != NULL) && (lang == NULL)) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1400 | xmlOutputBufferWrite(buf, 7, " lang=\""); |
| 1401 | xmlAttrSerializeContent(buf, xml_lang); |
| 1402 | xmlOutputBufferWrite(buf, 1, "\""); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | /** |
| 1407 | * xhtmlNodeListDumpOutput: |
| 1408 | * @buf: the XML buffer output |
| 1409 | * @doc: the XHTML document |
| 1410 | * @cur: the first node |
| 1411 | * @level: the imbrication level for indenting |
| 1412 | * @format: is formatting allowed |
| 1413 | * @encoding: an optional encoding string |
| 1414 | * |
| 1415 | * Dump an XML node list, recursive behaviour, children are printed too. |
| 1416 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 1417 | * or xmlKeepBlanksDefault(0) was called |
| 1418 | */ |
| 1419 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1420 | xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1421 | xmlOutputBufferPtr buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1422 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1423 | if (cur == NULL) return; |
| 1424 | buf = ctxt->buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1425 | while (cur != NULL) { |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 1426 | if ((ctxt->format == 1) && (xmlIndentTreeOutput) && |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1427 | (cur->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 1428 | xmlOutputBufferWrite(buf, ctxt->indent_size * |
| 1429 | (ctxt->level > ctxt->indent_nr ? |
| 1430 | ctxt->indent_nr : ctxt->level), |
| 1431 | ctxt->indent); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1432 | xhtmlNodeDumpOutput(ctxt, cur); |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 1433 | if (ctxt->format == 1) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1434 | xmlOutputBufferWrite(buf, 1, "\n"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1435 | } |
| 1436 | cur = cur->next; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | /** |
| 1441 | * xhtmlNodeDumpOutput: |
| 1442 | * @buf: the XML buffer output |
| 1443 | * @doc: the XHTML document |
| 1444 | * @cur: the current node |
| 1445 | * @level: the imbrication level for indenting |
| 1446 | * @format: is formatting allowed |
| 1447 | * @encoding: an optional encoding string |
| 1448 | * |
| 1449 | * Dump an XHTML node, recursive behaviour, children are printed too. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1450 | */ |
| 1451 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1452 | xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) { |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1453 | int format, addmeta = 0; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1454 | xmlNodePtr tmp; |
| 1455 | xmlChar *start, *end; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1456 | xmlOutputBufferPtr buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1457 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1458 | if (cur == NULL) return; |
Daniel Veillard | 60071ae | 2005-09-12 00:03:43 +0000 | [diff] [blame] | 1459 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 1460 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 1461 | xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur); |
| 1462 | return; |
| 1463 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1464 | if (cur->type == XML_XINCLUDE_START) |
| 1465 | return; |
| 1466 | if (cur->type == XML_XINCLUDE_END) |
| 1467 | return; |
| 1468 | if (cur->type == XML_DTD_NODE) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1469 | xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1470 | return; |
| 1471 | } |
Rob Richards | 2e2691b | 2005-10-21 14:45:16 +0000 | [diff] [blame] | 1472 | if (cur->type == XML_DOCUMENT_FRAG_NODE) { |
| 1473 | xhtmlNodeListDumpOutput(ctxt, cur->children); |
| 1474 | return; |
| 1475 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1476 | buf = ctxt->buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1477 | if (cur->type == XML_ELEMENT_DECL) { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 1478 | xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1479 | return; |
| 1480 | } |
| 1481 | if (cur->type == XML_ATTRIBUTE_DECL) { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 1482 | xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1483 | return; |
| 1484 | } |
| 1485 | if (cur->type == XML_ENTITY_DECL) { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 1486 | xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1487 | return; |
| 1488 | } |
| 1489 | if (cur->type == XML_TEXT_NODE) { |
| 1490 | if (cur->content != NULL) { |
| 1491 | if ((cur->name == xmlStringText) || |
| 1492 | (cur->name != xmlStringTextNoenc)) { |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 1493 | xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1494 | } else { |
| 1495 | /* |
| 1496 | * Disable escaping, needed for XSLT |
| 1497 | */ |
| 1498 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | return; |
| 1503 | } |
| 1504 | if (cur->type == XML_PI_NODE) { |
| 1505 | if (cur->content != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1506 | xmlOutputBufferWrite(buf, 2, "<?"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1507 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 1508 | if (cur->content != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1509 | xmlOutputBufferWrite(buf, 1, " "); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1510 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 1511 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1512 | xmlOutputBufferWrite(buf, 2, "?>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1513 | } else { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1514 | xmlOutputBufferWrite(buf, 2, "<?"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1515 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1516 | xmlOutputBufferWrite(buf, 2, "?>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1517 | } |
| 1518 | return; |
| 1519 | } |
| 1520 | if (cur->type == XML_COMMENT_NODE) { |
| 1521 | if (cur->content != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1522 | xmlOutputBufferWrite(buf, 4, "<!--"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1523 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1524 | xmlOutputBufferWrite(buf, 3, "-->"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1525 | } |
| 1526 | return; |
| 1527 | } |
| 1528 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1529 | xmlOutputBufferWrite(buf, 1, "&"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1530 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1531 | xmlOutputBufferWrite(buf, 1, ";"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1532 | return; |
| 1533 | } |
| 1534 | if (cur->type == XML_CDATA_SECTION_NODE) { |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 1535 | if (cur->content == NULL || *cur->content == '\0') { |
| 1536 | xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>"); |
| 1537 | } else { |
| 1538 | start = end = cur->content; |
| 1539 | while (*end != '\0') { |
| 1540 | if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') { |
| 1541 | end = end + 2; |
| 1542 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
| 1543 | xmlOutputBufferWrite(buf, end - start, (const char *)start); |
| 1544 | xmlOutputBufferWrite(buf, 3, "]]>"); |
| 1545 | start = end; |
| 1546 | } |
| 1547 | end++; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1548 | } |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 1549 | if (start != end) { |
| 1550 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
| 1551 | xmlOutputBufferWriteString(buf, (const char *)start); |
| 1552 | xmlOutputBufferWrite(buf, 3, "]]>"); |
| 1553 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1554 | } |
| 1555 | return; |
| 1556 | } |
Daniel Veillard | a76a81f | 2007-10-10 08:28:18 +0000 | [diff] [blame] | 1557 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 1558 | xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur); |
| 1559 | return; |
| 1560 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1561 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1562 | format = ctxt->format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1563 | if (format == 1) { |
| 1564 | tmp = cur->children; |
| 1565 | while (tmp != NULL) { |
| 1566 | if ((tmp->type == XML_TEXT_NODE) || |
| 1567 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 1568 | format = 0; |
| 1569 | break; |
| 1570 | } |
| 1571 | tmp = tmp->next; |
| 1572 | } |
| 1573 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1574 | xmlOutputBufferWrite(buf, 1, "<"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1575 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 1576 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1577 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 1581 | if (cur->nsDef) |
Adam Spragg | d2e6231 | 2010-11-03 15:33:40 +0100 | [diff] [blame] | 1582 | xmlNsListDumpOutputCtxt(ctxt, cur->nsDef); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1583 | if ((xmlStrEqual(cur->name, BAD_CAST "html") && |
| 1584 | (cur->ns == NULL) && (cur->nsDef == NULL))) { |
| 1585 | /* |
| 1586 | * 3.1.1. Strictly Conforming Documents A.3.1.1 3/ |
| 1587 | */ |
| 1588 | xmlOutputBufferWriteString(buf, |
| 1589 | " xmlns=\"http://www.w3.org/1999/xhtml\""); |
| 1590 | } |
| 1591 | if (cur->properties != NULL) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1592 | xhtmlAttrListDumpOutput(ctxt, cur->properties); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1593 | |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1594 | if ((cur->type == XML_ELEMENT_NODE) && |
| 1595 | (cur->parent != NULL) && |
| 1596 | (cur->parent->parent == (xmlNodePtr) cur->doc) && |
| 1597 | xmlStrEqual(cur->name, BAD_CAST"head") && |
| 1598 | xmlStrEqual(cur->parent->name, BAD_CAST"html")) { |
| 1599 | |
| 1600 | tmp = cur->children; |
| 1601 | while (tmp != NULL) { |
| 1602 | if (xmlStrEqual(tmp->name, BAD_CAST"meta")) { |
| 1603 | xmlChar *httpequiv; |
| 1604 | |
| 1605 | httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv"); |
Rob Richards | 07b7200 | 2005-09-03 14:56:36 +0000 | [diff] [blame] | 1606 | if (httpequiv != NULL) { |
| 1607 | if (xmlStrcasecmp(httpequiv, BAD_CAST"Content-Type") == 0) { |
| 1608 | xmlFree(httpequiv); |
| 1609 | break; |
| 1610 | } |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1611 | xmlFree(httpequiv); |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1612 | } |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1613 | } |
| 1614 | tmp = tmp->next; |
| 1615 | } |
| 1616 | if (tmp == NULL) |
| 1617 | addmeta = 1; |
| 1618 | } |
| 1619 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1620 | if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) { |
| 1621 | if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) && |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1622 | ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1623 | /* |
| 1624 | * C.2. Empty Elements |
| 1625 | */ |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1626 | xmlOutputBufferWrite(buf, 3, " />"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1627 | } else { |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1628 | if (addmeta == 1) { |
| 1629 | xmlOutputBufferWrite(buf, 1, ">"); |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 1630 | if (ctxt->format == 1) { |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 1631 | xmlOutputBufferWrite(buf, 1, "\n"); |
| 1632 | if (xmlIndentTreeOutput) |
| 1633 | xmlOutputBufferWrite(buf, ctxt->indent_size * |
| 1634 | (ctxt->level + 1 > ctxt->indent_nr ? |
| 1635 | ctxt->indent_nr : ctxt->level + 1), ctxt->indent); |
| 1636 | } |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1637 | xmlOutputBufferWriteString(buf, |
| 1638 | "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="); |
| 1639 | if (ctxt->encoding) { |
| 1640 | xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding); |
| 1641 | } else { |
| 1642 | xmlOutputBufferWrite(buf, 5, "UTF-8"); |
| 1643 | } |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 1644 | xmlOutputBufferWrite(buf, 4, "\" />"); |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 1645 | if (ctxt->format == 1) |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 1646 | xmlOutputBufferWrite(buf, 1, "\n"); |
| 1647 | } else { |
| 1648 | xmlOutputBufferWrite(buf, 1, ">"); |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1649 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1650 | /* |
| 1651 | * C.3. Element Minimization and Empty Element Content |
| 1652 | */ |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 1653 | xmlOutputBufferWrite(buf, 2, "</"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1654 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 1655 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1656 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1657 | } |
| 1658 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1659 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1660 | } |
| 1661 | return; |
| 1662 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1663 | xmlOutputBufferWrite(buf, 1, ">"); |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1664 | if (addmeta == 1) { |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 1665 | if (ctxt->format == 1) { |
Rob Richards | 2ce51c0 | 2005-09-12 12:16:35 +0000 | [diff] [blame] | 1666 | xmlOutputBufferWrite(buf, 1, "\n"); |
| 1667 | if (xmlIndentTreeOutput) |
| 1668 | xmlOutputBufferWrite(buf, ctxt->indent_size * |
| 1669 | (ctxt->level + 1 > ctxt->indent_nr ? |
| 1670 | ctxt->indent_nr : ctxt->level + 1), ctxt->indent); |
| 1671 | } |
Rob Richards | 31f7302 | 2005-08-26 15:33:26 +0000 | [diff] [blame] | 1672 | xmlOutputBufferWriteString(buf, |
| 1673 | "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="); |
| 1674 | if (ctxt->encoding) { |
| 1675 | xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding); |
| 1676 | } else { |
| 1677 | xmlOutputBufferWrite(buf, 5, "UTF-8"); |
| 1678 | } |
| 1679 | xmlOutputBufferWrite(buf, 4, "\" />"); |
| 1680 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1681 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 1682 | xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Kasimier T. Buchcik | 7b4e2e2 | 2006-07-13 13:07:11 +0000 | [diff] [blame] | 1685 | #if 0 |
| 1686 | /* |
| 1687 | * This was removed due to problems with HTML processors. |
| 1688 | * See bug #345147. |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1689 | */ |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1690 | /* |
| 1691 | * 4.8. Script and Style elements |
| 1692 | */ |
| 1693 | if ((cur->type == XML_ELEMENT_NODE) && |
| 1694 | ((xmlStrEqual(cur->name, BAD_CAST "script")) || |
| 1695 | (xmlStrEqual(cur->name, BAD_CAST "style"))) && |
| 1696 | ((cur->ns == NULL) || |
| 1697 | (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) { |
| 1698 | xmlNodePtr child = cur->children; |
| 1699 | |
| 1700 | while (child != NULL) { |
Daniel Veillard | dbd6105 | 2005-09-12 14:03:26 +0000 | [diff] [blame] | 1701 | if (child->type == XML_TEXT_NODE) { |
| 1702 | if ((xmlStrchr(child->content, '<') == NULL) && |
| 1703 | (xmlStrchr(child->content, '&') == NULL) && |
| 1704 | (xmlStrstr(child->content, BAD_CAST "]]>") == NULL)) { |
| 1705 | /* Nothing to escape, so just output as is... */ |
| 1706 | /* FIXME: Should we do something about "--" also? */ |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1707 | int level = ctxt->level; |
| 1708 | int indent = ctxt->format; |
| 1709 | |
| 1710 | ctxt->level = 0; |
| 1711 | ctxt->format = 0; |
Daniel Veillard | dbd6105 | 2005-09-12 14:03:26 +0000 | [diff] [blame] | 1712 | xmlOutputBufferWriteString(buf, (const char *) child->content); |
| 1713 | /* (We cannot use xhtmlNodeDumpOutput() here because |
| 1714 | * we wish to leave '>' unescaped!) */ |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1715 | ctxt->level = level; |
| 1716 | ctxt->format = indent; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1717 | } else { |
Daniel Veillard | dbd6105 | 2005-09-12 14:03:26 +0000 | [diff] [blame] | 1718 | /* We must use a CDATA section. Unfortunately, |
| 1719 | * this will break CSS and JavaScript when read by |
| 1720 | * a browser in HTML4-compliant mode. :-( */ |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1721 | start = end = child->content; |
| 1722 | while (*end != '\0') { |
| 1723 | if (*end == ']' && |
| 1724 | *(end + 1) == ']' && |
| 1725 | *(end + 2) == '>') { |
| 1726 | end = end + 2; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1727 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1728 | xmlOutputBufferWrite(buf, end - start, |
| 1729 | (const char *)start); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1730 | xmlOutputBufferWrite(buf, 3, "]]>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1731 | start = end; |
| 1732 | } |
| 1733 | end++; |
| 1734 | } |
| 1735 | if (start != end) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1736 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
| 1737 | xmlOutputBufferWrite(buf, end - start, |
| 1738 | (const char *)start); |
| 1739 | xmlOutputBufferWrite(buf, 3, "]]>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | } else { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1743 | int level = ctxt->level; |
| 1744 | int indent = ctxt->format; |
| 1745 | |
| 1746 | ctxt->level = 0; |
| 1747 | ctxt->format = 0; |
| 1748 | xhtmlNodeDumpOutput(ctxt, child); |
| 1749 | ctxt->level = level; |
| 1750 | ctxt->format = indent; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1751 | } |
| 1752 | child = child->next; |
| 1753 | } |
Kasimier T. Buchcik | 7b4e2e2 | 2006-07-13 13:07:11 +0000 | [diff] [blame] | 1754 | } |
| 1755 | #endif |
| 1756 | |
| 1757 | if (cur->children != NULL) { |
Daniel Veillard | f0244ce | 2004-05-09 23:48:39 +0000 | [diff] [blame] | 1758 | int indent = ctxt->format; |
| 1759 | |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 1760 | if (format == 1) xmlOutputBufferWrite(buf, 1, "\n"); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1761 | if (ctxt->level >= 0) ctxt->level++; |
Daniel Veillard | f0244ce | 2004-05-09 23:48:39 +0000 | [diff] [blame] | 1762 | ctxt->format = format; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1763 | xhtmlNodeListDumpOutput(ctxt, cur->children); |
| 1764 | if (ctxt->level > 0) ctxt->level--; |
Daniel Veillard | f0244ce | 2004-05-09 23:48:39 +0000 | [diff] [blame] | 1765 | ctxt->format = indent; |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 1766 | if ((xmlIndentTreeOutput) && (format == 1)) |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 1767 | xmlOutputBufferWrite(buf, ctxt->indent_size * |
| 1768 | (ctxt->level > ctxt->indent_nr ? |
| 1769 | ctxt->indent_nr : ctxt->level), |
| 1770 | ctxt->indent); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1771 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1772 | xmlOutputBufferWrite(buf, 2, "</"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1773 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 1774 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1775 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1779 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1780 | } |
| 1781 | #endif |
| 1782 | |
| 1783 | /************************************************************************ |
| 1784 | * * |
| 1785 | * Public entry points * |
| 1786 | * * |
| 1787 | ************************************************************************/ |
| 1788 | |
| 1789 | /** |
| 1790 | * xmlSaveToFd: |
| 1791 | * @fd: a file descriptor number |
| 1792 | * @encoding: the encoding name to use or NULL |
| 1793 | * @options: a set of xmlSaveOptions |
| 1794 | * |
| 1795 | * Create a document saving context serializing to a file descriptor |
| 1796 | * with the encoding and the options given. |
| 1797 | * |
| 1798 | * Returns a new serialization context or NULL in case of error. |
| 1799 | */ |
| 1800 | xmlSaveCtxtPtr |
| 1801 | xmlSaveToFd(int fd, const char *encoding, int options) |
| 1802 | { |
| 1803 | xmlSaveCtxtPtr ret; |
| 1804 | |
| 1805 | ret = xmlNewSaveCtxt(encoding, options); |
| 1806 | if (ret == NULL) return(NULL); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1807 | ret->buf = xmlOutputBufferCreateFd(fd, ret->handler); |
| 1808 | if (ret->buf == NULL) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1809 | xmlFreeSaveCtxt(ret); |
| 1810 | return(NULL); |
| 1811 | } |
| 1812 | return(ret); |
| 1813 | } |
| 1814 | |
| 1815 | /** |
| 1816 | * xmlSaveToFilename: |
| 1817 | * @filename: a file name or an URL |
| 1818 | * @encoding: the encoding name to use or NULL |
| 1819 | * @options: a set of xmlSaveOptions |
| 1820 | * |
| 1821 | * Create a document saving context serializing to a filename or possibly |
| 1822 | * to an URL (but this is less reliable) with the encoding and the options |
| 1823 | * given. |
| 1824 | * |
| 1825 | * Returns a new serialization context or NULL in case of error. |
| 1826 | */ |
| 1827 | xmlSaveCtxtPtr |
| 1828 | xmlSaveToFilename(const char *filename, const char *encoding, int options) |
| 1829 | { |
| 1830 | xmlSaveCtxtPtr ret; |
| 1831 | int compression = 0; /* TODO handle compression option */ |
| 1832 | |
| 1833 | ret = xmlNewSaveCtxt(encoding, options); |
| 1834 | if (ret == NULL) return(NULL); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1835 | ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler, |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1836 | compression); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1837 | if (ret->buf == NULL) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1838 | xmlFreeSaveCtxt(ret); |
| 1839 | return(NULL); |
| 1840 | } |
| 1841 | return(ret); |
| 1842 | } |
| 1843 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1844 | /** |
| 1845 | * xmlSaveToBuffer: |
| 1846 | * @buffer: a buffer |
| 1847 | * @encoding: the encoding name to use or NULL |
| 1848 | * @options: a set of xmlSaveOptions |
| 1849 | * |
| 1850 | * Create a document saving context serializing to a buffer |
| 1851 | * with the encoding and the options given |
| 1852 | * |
| 1853 | * Returns a new serialization context or NULL in case of error. |
Daniel Veillard | 9a00fd2 | 2005-11-09 08:56:26 +0000 | [diff] [blame] | 1854 | */ |
| 1855 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1856 | xmlSaveCtxtPtr |
| 1857 | xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options) |
| 1858 | { |
Daniel Veillard | 9a00fd2 | 2005-11-09 08:56:26 +0000 | [diff] [blame] | 1859 | xmlSaveCtxtPtr ret; |
| 1860 | xmlOutputBufferPtr out_buff; |
| 1861 | xmlCharEncodingHandlerPtr handler; |
| 1862 | |
| 1863 | ret = xmlNewSaveCtxt(encoding, options); |
| 1864 | if (ret == NULL) return(NULL); |
| 1865 | |
| 1866 | if (encoding != NULL) { |
| 1867 | handler = xmlFindCharEncodingHandler(encoding); |
| 1868 | if (handler == NULL) { |
| 1869 | xmlFree(ret); |
| 1870 | return(NULL); |
| 1871 | } |
| 1872 | } else |
| 1873 | handler = NULL; |
| 1874 | out_buff = xmlOutputBufferCreateBuffer(buffer, handler); |
| 1875 | if (out_buff == NULL) { |
| 1876 | xmlFree(ret); |
| 1877 | if (handler) xmlCharEncCloseFunc(handler); |
| 1878 | return(NULL); |
| 1879 | } |
| 1880 | |
| 1881 | ret->buf = out_buff; |
| 1882 | return(ret); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1883 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1884 | |
| 1885 | /** |
| 1886 | * xmlSaveToIO: |
| 1887 | * @iowrite: an I/O write function |
| 1888 | * @ioclose: an I/O close function |
| 1889 | * @ioctx: an I/O handler |
| 1890 | * @encoding: the encoding name to use or NULL |
| 1891 | * @options: a set of xmlSaveOptions |
| 1892 | * |
| 1893 | * Create a document saving context serializing to a file descriptor |
| 1894 | * with the encoding and the options given |
| 1895 | * |
| 1896 | * Returns a new serialization context or NULL in case of error. |
| 1897 | */ |
| 1898 | xmlSaveCtxtPtr |
| 1899 | xmlSaveToIO(xmlOutputWriteCallback iowrite, |
| 1900 | xmlOutputCloseCallback ioclose, |
| 1901 | void *ioctx, const char *encoding, int options) |
| 1902 | { |
| 1903 | xmlSaveCtxtPtr ret; |
| 1904 | |
| 1905 | ret = xmlNewSaveCtxt(encoding, options); |
| 1906 | if (ret == NULL) return(NULL); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1907 | ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler); |
| 1908 | if (ret->buf == NULL) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1909 | xmlFreeSaveCtxt(ret); |
| 1910 | return(NULL); |
| 1911 | } |
| 1912 | return(ret); |
| 1913 | } |
| 1914 | |
| 1915 | /** |
| 1916 | * xmlSaveDoc: |
| 1917 | * @ctxt: a document saving context |
| 1918 | * @doc: a document |
| 1919 | * |
| 1920 | * Save a full document to a saving context |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 1921 | * TODO: The function is not fully implemented yet as it does not return the |
| 1922 | * byte count but 0 instead |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1923 | * |
| 1924 | * Returns the number of byte written or -1 in case of error |
| 1925 | */ |
| 1926 | long |
| 1927 | xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc) |
| 1928 | { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1929 | long ret = 0; |
| 1930 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1931 | if ((ctxt == NULL) || (doc == NULL)) return(-1); |
Daniel Veillard | dab39b5 | 2006-10-16 23:22:10 +0000 | [diff] [blame] | 1932 | if (xmlDocContentDumpOutput(ctxt, doc) < 0) |
| 1933 | return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1934 | return(ret); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | /** |
| 1938 | * xmlSaveTree: |
| 1939 | * @ctxt: a document saving context |
Daniel Veillard | 681e904 | 2006-09-29 09:16:00 +0000 | [diff] [blame] | 1940 | * @node: the top node of the subtree to save |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1941 | * |
| 1942 | * Save a subtree starting at the node parameter to a saving context |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 1943 | * TODO: The function is not fully implemented yet as it does not return the |
| 1944 | * byte count but 0 instead |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1945 | * |
| 1946 | * Returns the number of byte written or -1 in case of error |
| 1947 | */ |
| 1948 | long |
| 1949 | xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node) |
| 1950 | { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1951 | long ret = 0; |
| 1952 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1953 | if ((ctxt == NULL) || (node == NULL)) return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1954 | xmlNodeDumpOutputInternal(ctxt, node); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1955 | return(ret); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | /** |
| 1959 | * xmlSaveFlush: |
| 1960 | * @ctxt: a document saving context |
| 1961 | * |
| 1962 | * Flush a document saving context, i.e. make sure that all bytes have |
| 1963 | * been output. |
| 1964 | * |
| 1965 | * Returns the number of byte written or -1 in case of error. |
| 1966 | */ |
| 1967 | int |
| 1968 | xmlSaveFlush(xmlSaveCtxtPtr ctxt) |
| 1969 | { |
| 1970 | if (ctxt == NULL) return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1971 | if (ctxt->buf == NULL) return(-1); |
| 1972 | return(xmlOutputBufferFlush(ctxt->buf)); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | /** |
| 1976 | * xmlSaveClose: |
| 1977 | * @ctxt: a document saving context |
| 1978 | * |
| 1979 | * Close a document saving context, i.e. make sure that all bytes have |
| 1980 | * been output and free the associated data. |
| 1981 | * |
| 1982 | * Returns the number of byte written or -1 in case of error. |
| 1983 | */ |
| 1984 | int |
| 1985 | xmlSaveClose(xmlSaveCtxtPtr ctxt) |
| 1986 | { |
| 1987 | int ret; |
| 1988 | |
| 1989 | if (ctxt == NULL) return(-1); |
| 1990 | ret = xmlSaveFlush(ctxt); |
| 1991 | xmlFreeSaveCtxt(ctxt); |
| 1992 | return(ret); |
| 1993 | } |
| 1994 | |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 1995 | /** |
| 1996 | * xmlSaveSetEscape: |
| 1997 | * @ctxt: a document saving context |
| 1998 | * @escape: the escaping function |
| 1999 | * |
| 2000 | * Set a custom escaping function to be used for text in element content |
| 2001 | * |
| 2002 | * Returns 0 if successful or -1 in case of error. |
| 2003 | */ |
| 2004 | int |
| 2005 | xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape) |
| 2006 | { |
| 2007 | if (ctxt == NULL) return(-1); |
| 2008 | ctxt->escape = escape; |
| 2009 | return(0); |
| 2010 | } |
| 2011 | |
| 2012 | /** |
| 2013 | * xmlSaveSetAttrEscape: |
| 2014 | * @ctxt: a document saving context |
| 2015 | * @escape: the escaping function |
| 2016 | * |
| 2017 | * Set a custom escaping function to be used for text in attribute content |
| 2018 | * |
| 2019 | * Returns 0 if successful or -1 in case of error. |
| 2020 | */ |
| 2021 | int |
| 2022 | xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape) |
| 2023 | { |
| 2024 | if (ctxt == NULL) return(-1); |
| 2025 | ctxt->escapeAttr = escape; |
| 2026 | return(0); |
| 2027 | } |
| 2028 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2029 | /************************************************************************ |
| 2030 | * * |
| 2031 | * Public entry points based on buffers * |
| 2032 | * * |
| 2033 | ************************************************************************/ |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2034 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2035 | /** |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2036 | * xmlBufAttrSerializeTxtContent: |
| 2037 | * @buf: and xmlBufPtr output |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2038 | * @doc: the document |
| 2039 | * @attr: the attribute node |
| 2040 | * @string: the text content |
| 2041 | * |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2042 | * Serialize text attribute values to an xmlBufPtr |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2043 | */ |
| 2044 | void |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2045 | xmlBufAttrSerializeTxtContent(xmlBufPtr buf, xmlDocPtr doc, |
| 2046 | xmlAttrPtr attr, const xmlChar * string) |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2047 | { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2048 | xmlChar *base, *cur; |
| 2049 | |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2050 | if (string == NULL) |
| 2051 | return; |
| 2052 | base = cur = (xmlChar *) string; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2053 | while (*cur != 0) { |
| 2054 | if (*cur == '\n') { |
| 2055 | if (base != cur) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2056 | xmlBufAdd(buf, base, cur - base); |
| 2057 | xmlBufAdd(buf, BAD_CAST " ", 5); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2058 | cur++; |
| 2059 | base = cur; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2060 | } else if (*cur == '\r') { |
| 2061 | if (base != cur) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2062 | xmlBufAdd(buf, base, cur - base); |
| 2063 | xmlBufAdd(buf, BAD_CAST " ", 5); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2064 | cur++; |
| 2065 | base = cur; |
| 2066 | } else if (*cur == '\t') { |
| 2067 | if (base != cur) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2068 | xmlBufAdd(buf, base, cur - base); |
| 2069 | xmlBufAdd(buf, BAD_CAST "	", 4); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2070 | cur++; |
| 2071 | base = cur; |
| 2072 | } else if (*cur == '"') { |
| 2073 | if (base != cur) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2074 | xmlBufAdd(buf, base, cur - base); |
| 2075 | xmlBufAdd(buf, BAD_CAST """, 6); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2076 | cur++; |
| 2077 | base = cur; |
| 2078 | } else if (*cur == '<') { |
| 2079 | if (base != cur) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2080 | xmlBufAdd(buf, base, cur - base); |
| 2081 | xmlBufAdd(buf, BAD_CAST "<", 4); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2082 | cur++; |
| 2083 | base = cur; |
| 2084 | } else if (*cur == '>') { |
| 2085 | if (base != cur) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2086 | xmlBufAdd(buf, base, cur - base); |
| 2087 | xmlBufAdd(buf, BAD_CAST ">", 4); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2088 | cur++; |
| 2089 | base = cur; |
| 2090 | } else if (*cur == '&') { |
| 2091 | if (base != cur) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2092 | xmlBufAdd(buf, base, cur - base); |
| 2093 | xmlBufAdd(buf, BAD_CAST "&", 5); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2094 | cur++; |
| 2095 | base = cur; |
| 2096 | } else if ((*cur >= 0x80) && ((doc == NULL) || |
| 2097 | (doc->encoding == NULL))) { |
| 2098 | /* |
| 2099 | * We assume we have UTF-8 content. |
| 2100 | */ |
Daniel Veillard | 0795348 | 2012-01-22 17:42:35 +0800 | [diff] [blame] | 2101 | unsigned char tmp[12]; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2102 | int val = 0, l = 1; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2103 | |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2104 | if (base != cur) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2105 | xmlBufAdd(buf, base, cur - base); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2106 | if (*cur < 0xC0) { |
| 2107 | xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL); |
| 2108 | if (doc != NULL) |
| 2109 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 2110 | xmlSerializeHexCharRef(tmp, *cur); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2111 | xmlBufAdd(buf, (xmlChar *) tmp, -1); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2112 | cur++; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2113 | base = cur; |
| 2114 | continue; |
| 2115 | } else if (*cur < 0xE0) { |
| 2116 | val = (cur[0]) & 0x1F; |
| 2117 | val <<= 6; |
| 2118 | val |= (cur[1]) & 0x3F; |
| 2119 | l = 2; |
| 2120 | } else if (*cur < 0xF0) { |
| 2121 | val = (cur[0]) & 0x0F; |
| 2122 | val <<= 6; |
| 2123 | val |= (cur[1]) & 0x3F; |
| 2124 | val <<= 6; |
| 2125 | val |= (cur[2]) & 0x3F; |
| 2126 | l = 3; |
| 2127 | } else if (*cur < 0xF8) { |
| 2128 | val = (cur[0]) & 0x07; |
| 2129 | val <<= 6; |
| 2130 | val |= (cur[1]) & 0x3F; |
| 2131 | val <<= 6; |
| 2132 | val |= (cur[2]) & 0x3F; |
| 2133 | val <<= 6; |
| 2134 | val |= (cur[3]) & 0x3F; |
| 2135 | l = 4; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2136 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2137 | if ((l == 1) || (!IS_CHAR(val))) { |
| 2138 | xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL); |
| 2139 | if (doc != NULL) |
| 2140 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2141 | |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2142 | xmlSerializeHexCharRef(tmp, *cur); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2143 | xmlBufAdd(buf, (xmlChar *) tmp, -1); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2144 | cur++; |
| 2145 | base = cur; |
| 2146 | continue; |
| 2147 | } |
| 2148 | /* |
| 2149 | * We could do multiple things here. Just save |
| 2150 | * as a char ref |
| 2151 | */ |
| 2152 | xmlSerializeHexCharRef(tmp, val); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2153 | xmlBufAdd(buf, (xmlChar *) tmp, -1); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2154 | cur += l; |
| 2155 | base = cur; |
| 2156 | } else { |
| 2157 | cur++; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2158 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 2159 | } |
| 2160 | if (base != cur) |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2161 | xmlBufAdd(buf, base, cur - base); |
| 2162 | } |
| 2163 | |
| 2164 | /** |
| 2165 | * xmlAttrSerializeTxtContent: |
| 2166 | * @buf: the XML buffer output |
| 2167 | * @doc: the document |
| 2168 | * @attr: the attribute node |
| 2169 | * @string: the text content |
| 2170 | * |
| 2171 | * Serialize text attribute values to an xml simple buffer |
| 2172 | */ |
| 2173 | void |
| 2174 | xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc, |
| 2175 | xmlAttrPtr attr, const xmlChar * string) |
| 2176 | { |
| 2177 | xmlBufPtr buffer; |
| 2178 | |
| 2179 | if ((buf == NULL) || (string == NULL)) |
| 2180 | return; |
| 2181 | buffer = xmlBufFromBuffer(buf); |
| 2182 | if (buffer == NULL) |
| 2183 | return; |
| 2184 | xmlBufAttrSerializeTxtContent(buffer, doc, attr, string); |
| 2185 | xmlBufBackToBuffer(buffer); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | /** |
| 2189 | * xmlNodeDump: |
| 2190 | * @buf: the XML buffer output |
| 2191 | * @doc: the document |
| 2192 | * @cur: the current node |
| 2193 | * @level: the imbrication level for indenting |
| 2194 | * @format: is formatting allowed |
| 2195 | * |
| 2196 | * Dump an XML node, recursive behaviour,children are printed too. |
| 2197 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2198 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2199 | * Since this is using xmlBuffer structures it is limited to 2GB and somehow |
| 2200 | * deprecated, use xmlBufNodeDump() instead. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2201 | * |
| 2202 | * Returns the number of bytes written to the buffer or -1 in case of error |
| 2203 | */ |
| 2204 | int |
| 2205 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 2206 | int format) |
| 2207 | { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2208 | xmlBufPtr buffer; |
| 2209 | int ret; |
| 2210 | |
| 2211 | if ((buf == NULL) || (cur == NULL)) |
| 2212 | return(-1); |
| 2213 | buffer = xmlBufFromBuffer(buf); |
| 2214 | if (buffer == NULL) |
| 2215 | return(-1); |
| 2216 | ret = xmlBufNodeDump(buffer, doc, cur, level, format); |
| 2217 | xmlBufBackToBuffer(buffer); |
| 2218 | if (ret > INT_MAX) |
| 2219 | return(-1); |
| 2220 | return((int) ret); |
| 2221 | } |
| 2222 | |
| 2223 | /** |
| 2224 | * xmlBufNodeDump: |
| 2225 | * @buf: the XML buffer output |
| 2226 | * @doc: the document |
| 2227 | * @cur: the current node |
| 2228 | * @level: the imbrication level for indenting |
| 2229 | * @format: is formatting allowed |
| 2230 | * |
| 2231 | * Dump an XML node, recursive behaviour,children are printed too. |
| 2232 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2233 | * or xmlKeepBlanksDefault(0) was called |
| 2234 | * |
| 2235 | * Returns the number of bytes written to the buffer, in case of error 0 |
| 2236 | * is returned or @buf stores the error |
| 2237 | */ |
| 2238 | |
| 2239 | size_t |
| 2240 | xmlBufNodeDump(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 2241 | int format) |
| 2242 | { |
| 2243 | size_t use; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2244 | int ret; |
| 2245 | xmlOutputBufferPtr outbuf; |
| 2246 | |
| 2247 | xmlInitParser(); |
| 2248 | |
| 2249 | if (cur == NULL) { |
| 2250 | #ifdef DEBUG_TREE |
| 2251 | xmlGenericError(xmlGenericErrorContext, |
| 2252 | "xmlNodeDump : node == NULL\n"); |
| 2253 | #endif |
| 2254 | return (-1); |
| 2255 | } |
| 2256 | if (buf == NULL) { |
| 2257 | #ifdef DEBUG_TREE |
| 2258 | xmlGenericError(xmlGenericErrorContext, |
| 2259 | "xmlNodeDump : buf == NULL\n"); |
| 2260 | #endif |
| 2261 | return (-1); |
| 2262 | } |
| 2263 | outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); |
| 2264 | if (outbuf == NULL) { |
| 2265 | xmlSaveErrMemory("creating buffer"); |
| 2266 | return (-1); |
| 2267 | } |
| 2268 | memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer)); |
| 2269 | outbuf->buffer = buf; |
| 2270 | outbuf->encoder = NULL; |
| 2271 | outbuf->writecallback = NULL; |
| 2272 | outbuf->closecallback = NULL; |
| 2273 | outbuf->context = NULL; |
| 2274 | outbuf->written = 0; |
| 2275 | |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2276 | use = xmlBufUse(buf); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2277 | xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL); |
| 2278 | xmlFree(outbuf); |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2279 | ret = xmlBufUse(buf) - use; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2280 | return (ret); |
| 2281 | } |
| 2282 | |
| 2283 | /** |
| 2284 | * xmlElemDump: |
| 2285 | * @f: the FILE * for the output |
| 2286 | * @doc: the document |
| 2287 | * @cur: the current node |
| 2288 | * |
| 2289 | * Dump an XML/HTML node, recursive behaviour, children are printed too. |
| 2290 | */ |
| 2291 | void |
| 2292 | xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur) |
| 2293 | { |
| 2294 | xmlOutputBufferPtr outbuf; |
| 2295 | |
| 2296 | xmlInitParser(); |
| 2297 | |
| 2298 | if (cur == NULL) { |
| 2299 | #ifdef DEBUG_TREE |
| 2300 | xmlGenericError(xmlGenericErrorContext, |
| 2301 | "xmlElemDump : cur == NULL\n"); |
| 2302 | #endif |
| 2303 | return; |
| 2304 | } |
| 2305 | #ifdef DEBUG_TREE |
| 2306 | if (doc == NULL) { |
| 2307 | xmlGenericError(xmlGenericErrorContext, |
| 2308 | "xmlElemDump : doc == NULL\n"); |
| 2309 | } |
| 2310 | #endif |
| 2311 | |
| 2312 | outbuf = xmlOutputBufferCreateFile(f, NULL); |
| 2313 | if (outbuf == NULL) |
| 2314 | return; |
| 2315 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 2316 | #ifdef LIBXML_HTML_ENABLED |
| 2317 | htmlNodeDumpOutput(outbuf, doc, cur, NULL); |
| 2318 | #else |
| 2319 | xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n"); |
| 2320 | #endif /* LIBXML_HTML_ENABLED */ |
| 2321 | } else |
| 2322 | xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL); |
| 2323 | xmlOutputBufferClose(outbuf); |
| 2324 | } |
| 2325 | |
| 2326 | /************************************************************************ |
| 2327 | * * |
| 2328 | * Saving functions front-ends * |
| 2329 | * * |
| 2330 | ************************************************************************/ |
| 2331 | |
| 2332 | /** |
| 2333 | * xmlNodeDumpOutput: |
| 2334 | * @buf: the XML buffer output |
| 2335 | * @doc: the document |
| 2336 | * @cur: the current node |
| 2337 | * @level: the imbrication level for indenting |
| 2338 | * @format: is formatting allowed |
| 2339 | * @encoding: an optional encoding string |
| 2340 | * |
| 2341 | * Dump an XML node, recursive behaviour, children are printed too. |
| 2342 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2343 | * or xmlKeepBlanksDefault(0) was called |
| 2344 | */ |
| 2345 | void |
| 2346 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 2347 | int level, int format, const char *encoding) |
| 2348 | { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2349 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2350 | #ifdef LIBXML_HTML_ENABLED |
| 2351 | xmlDtdPtr dtd; |
| 2352 | int is_xhtml = 0; |
| 2353 | #endif |
| 2354 | |
| 2355 | xmlInitParser(); |
| 2356 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 2357 | if ((buf == NULL) || (cur == NULL)) return; |
| 2358 | |
Daniel Veillard | 64354ea | 2005-03-31 15:22:56 +0000 | [diff] [blame] | 2359 | if (encoding == NULL) |
| 2360 | encoding = "UTF-8"; |
| 2361 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2362 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2363 | ctxt.doc = doc; |
| 2364 | ctxt.buf = buf; |
| 2365 | ctxt.level = level; |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 2366 | ctxt.format = format ? 1 : 0; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2367 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2368 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 2369 | ctxt.options |= XML_SAVE_AS_XML; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2370 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2371 | #ifdef LIBXML_HTML_ENABLED |
| 2372 | dtd = xmlGetIntSubset(doc); |
| 2373 | if (dtd != NULL) { |
Daniel Veillard | 33b20b7 | 2005-09-12 21:43:20 +0000 | [diff] [blame] | 2374 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 2375 | if (is_xhtml < 0) |
| 2376 | is_xhtml = 0; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | if (is_xhtml) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2380 | xhtmlNodeDumpOutput(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2381 | else |
| 2382 | #endif |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2383 | xmlNodeDumpOutputInternal(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | /** |
| 2387 | * xmlDocDumpFormatMemoryEnc: |
| 2388 | * @out_doc: Document to generate XML text from |
| 2389 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 2390 | * @doc_txt_len: Length of the generated XML text |
| 2391 | * @txt_encoding: Character encoding to use when generating XML text |
| 2392 | * @format: should formatting spaces been added |
| 2393 | * |
| 2394 | * Dump the current DOM tree into memory using the character encoding specified |
| 2395 | * by the caller. Note it is up to the caller of this function to free the |
| 2396 | * allocated memory with xmlFree(). |
| 2397 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2398 | * or xmlKeepBlanksDefault(0) was called |
| 2399 | */ |
| 2400 | |
| 2401 | void |
| 2402 | xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 2403 | int * doc_txt_len, const char * txt_encoding, |
| 2404 | int format) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2405 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2406 | int dummy = 0; |
| 2407 | xmlOutputBufferPtr out_buff = NULL; |
| 2408 | xmlCharEncodingHandlerPtr conv_hdlr = NULL; |
| 2409 | |
| 2410 | if (doc_txt_len == NULL) { |
| 2411 | doc_txt_len = &dummy; /* Continue, caller just won't get length */ |
| 2412 | } |
| 2413 | |
| 2414 | if (doc_txt_ptr == NULL) { |
| 2415 | *doc_txt_len = 0; |
| 2416 | return; |
| 2417 | } |
| 2418 | |
| 2419 | *doc_txt_ptr = NULL; |
| 2420 | *doc_txt_len = 0; |
| 2421 | |
| 2422 | if (out_doc == NULL) { |
| 2423 | /* No document, no output */ |
| 2424 | return; |
| 2425 | } |
| 2426 | |
| 2427 | /* |
| 2428 | * Validate the encoding value, if provided. |
| 2429 | * This logic is copied from xmlSaveFileEnc. |
| 2430 | */ |
| 2431 | |
| 2432 | if (txt_encoding == NULL) |
| 2433 | txt_encoding = (const char *) out_doc->encoding; |
| 2434 | if (txt_encoding != NULL) { |
| 2435 | conv_hdlr = xmlFindCharEncodingHandler(txt_encoding); |
| 2436 | if ( conv_hdlr == NULL ) { |
| 2437 | xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc, |
| 2438 | txt_encoding); |
| 2439 | return; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { |
| 2444 | xmlSaveErrMemory("creating buffer"); |
| 2445 | return; |
| 2446 | } |
| 2447 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2448 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2449 | ctxt.doc = out_doc; |
| 2450 | ctxt.buf = out_buff; |
| 2451 | ctxt.level = 0; |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 2452 | ctxt.format = format ? 1 : 0; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2453 | ctxt.encoding = (const xmlChar *) txt_encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2454 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 2455 | ctxt.options |= XML_SAVE_AS_XML; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2456 | xmlDocContentDumpOutput(&ctxt, out_doc); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2457 | xmlOutputBufferFlush(out_buff); |
| 2458 | if (out_buff->conv != NULL) { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2459 | *doc_txt_len = xmlBufUse(out_buff->conv); |
| 2460 | *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->conv), *doc_txt_len); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2461 | } else { |
Daniel Veillard | 50cdab5 | 2012-07-16 14:52:00 +0800 | [diff] [blame] | 2462 | *doc_txt_len = xmlBufUse(out_buff->buffer); |
| 2463 | *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->buffer),*doc_txt_len); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2464 | } |
| 2465 | (void)xmlOutputBufferClose(out_buff); |
| 2466 | |
| 2467 | if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) { |
| 2468 | *doc_txt_len = 0; |
| 2469 | xmlSaveErrMemory("creating output"); |
| 2470 | } |
| 2471 | |
| 2472 | return; |
| 2473 | } |
| 2474 | |
| 2475 | /** |
| 2476 | * xmlDocDumpMemory: |
| 2477 | * @cur: the document |
| 2478 | * @mem: OUT: the memory pointer |
| 2479 | * @size: OUT: the memory length |
| 2480 | * |
| 2481 | * Dump an XML document in memory and return the #xmlChar * and it's size |
| 2482 | * in bytes. It's up to the caller to free the memory with xmlFree(). |
| 2483 | * The resulting byte array is zero terminated, though the last 0 is not |
| 2484 | * included in the returned size. |
| 2485 | */ |
| 2486 | void |
| 2487 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
| 2488 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0); |
| 2489 | } |
| 2490 | |
| 2491 | /** |
| 2492 | * xmlDocDumpFormatMemory: |
| 2493 | * @cur: the document |
| 2494 | * @mem: OUT: the memory pointer |
| 2495 | * @size: OUT: the memory length |
| 2496 | * @format: should formatting spaces been added |
| 2497 | * |
| 2498 | * |
| 2499 | * Dump an XML document in memory and return the #xmlChar * and it's size. |
| 2500 | * It's up to the caller to free the memory with xmlFree(). |
| 2501 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2502 | * or xmlKeepBlanksDefault(0) was called |
| 2503 | */ |
| 2504 | void |
| 2505 | xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) { |
| 2506 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format); |
| 2507 | } |
| 2508 | |
| 2509 | /** |
| 2510 | * xmlDocDumpMemoryEnc: |
| 2511 | * @out_doc: Document to generate XML text from |
| 2512 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 2513 | * @doc_txt_len: Length of the generated XML text |
| 2514 | * @txt_encoding: Character encoding to use when generating XML text |
| 2515 | * |
| 2516 | * Dump the current DOM tree into memory using the character encoding specified |
| 2517 | * by the caller. Note it is up to the caller of this function to free the |
| 2518 | * allocated memory with xmlFree(). |
| 2519 | */ |
| 2520 | |
| 2521 | void |
| 2522 | xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 2523 | int * doc_txt_len, const char * txt_encoding) { |
| 2524 | xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len, |
| 2525 | txt_encoding, 0); |
| 2526 | } |
| 2527 | |
| 2528 | /** |
| 2529 | * xmlDocFormatDump: |
| 2530 | * @f: the FILE* |
| 2531 | * @cur: the document |
| 2532 | * @format: should formatting spaces been added |
| 2533 | * |
| 2534 | * Dump an XML document to an open FILE. |
| 2535 | * |
| 2536 | * returns: the number of bytes written or -1 in case of failure. |
| 2537 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2538 | * or xmlKeepBlanksDefault(0) was called |
| 2539 | */ |
| 2540 | int |
| 2541 | xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2542 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2543 | xmlOutputBufferPtr buf; |
| 2544 | const char * encoding; |
| 2545 | xmlCharEncodingHandlerPtr handler = NULL; |
| 2546 | int ret; |
| 2547 | |
| 2548 | if (cur == NULL) { |
| 2549 | #ifdef DEBUG_TREE |
| 2550 | xmlGenericError(xmlGenericErrorContext, |
| 2551 | "xmlDocDump : document == NULL\n"); |
| 2552 | #endif |
| 2553 | return(-1); |
| 2554 | } |
| 2555 | encoding = (const char *) cur->encoding; |
| 2556 | |
| 2557 | if (encoding != NULL) { |
Daniel Veillard | 3814a36 | 2007-07-26 11:41:46 +0000 | [diff] [blame] | 2558 | handler = xmlFindCharEncodingHandler(encoding); |
| 2559 | if (handler == NULL) { |
| 2560 | xmlFree((char *) cur->encoding); |
| 2561 | cur->encoding = NULL; |
| 2562 | encoding = NULL; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2563 | } |
Daniel Veillard | 3814a36 | 2007-07-26 11:41:46 +0000 | [diff] [blame] | 2564 | } |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2565 | buf = xmlOutputBufferCreateFile(f, handler); |
| 2566 | if (buf == NULL) return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2567 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2568 | ctxt.doc = cur; |
| 2569 | ctxt.buf = buf; |
| 2570 | ctxt.level = 0; |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 2571 | ctxt.format = format ? 1 : 0; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2572 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2573 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 2574 | ctxt.options |= XML_SAVE_AS_XML; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2575 | xmlDocContentDumpOutput(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2576 | |
| 2577 | ret = xmlOutputBufferClose(buf); |
| 2578 | return(ret); |
| 2579 | } |
| 2580 | |
| 2581 | /** |
| 2582 | * xmlDocDump: |
| 2583 | * @f: the FILE* |
| 2584 | * @cur: the document |
| 2585 | * |
| 2586 | * Dump an XML document to an open FILE. |
| 2587 | * |
| 2588 | * returns: the number of bytes written or -1 in case of failure. |
| 2589 | */ |
| 2590 | int |
| 2591 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
| 2592 | return(xmlDocFormatDump (f, cur, 0)); |
| 2593 | } |
| 2594 | |
| 2595 | /** |
| 2596 | * xmlSaveFileTo: |
| 2597 | * @buf: an output I/O buffer |
| 2598 | * @cur: the document |
| 2599 | * @encoding: the encoding if any assuming the I/O layer handles the trancoding |
| 2600 | * |
| 2601 | * Dump an XML document to an I/O buffer. |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2602 | * Warning ! This call xmlOutputBufferClose() on buf which is not available |
| 2603 | * after this call. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2604 | * |
| 2605 | * returns: the number of bytes written or -1 in case of failure. |
| 2606 | */ |
| 2607 | int |
| 2608 | xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2609 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2610 | int ret; |
| 2611 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2612 | if (buf == NULL) return(-1); |
| 2613 | if (cur == NULL) { |
| 2614 | xmlOutputBufferClose(buf); |
| 2615 | return(-1); |
| 2616 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2617 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2618 | ctxt.doc = cur; |
| 2619 | ctxt.buf = buf; |
| 2620 | ctxt.level = 0; |
| 2621 | ctxt.format = 0; |
| 2622 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2623 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 2624 | ctxt.options |= XML_SAVE_AS_XML; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2625 | xmlDocContentDumpOutput(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2626 | ret = xmlOutputBufferClose(buf); |
| 2627 | return(ret); |
| 2628 | } |
| 2629 | |
| 2630 | /** |
| 2631 | * xmlSaveFormatFileTo: |
| 2632 | * @buf: an output I/O buffer |
| 2633 | * @cur: the document |
| 2634 | * @encoding: the encoding if any assuming the I/O layer handles the trancoding |
| 2635 | * @format: should formatting spaces been added |
| 2636 | * |
| 2637 | * Dump an XML document to an I/O buffer. |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2638 | * Warning ! This call xmlOutputBufferClose() on buf which is not available |
| 2639 | * after this call. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2640 | * |
| 2641 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2642 | */ |
| 2643 | int |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2644 | xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 2645 | const char *encoding, int format) |
| 2646 | { |
| 2647 | xmlSaveCtxt ctxt; |
| 2648 | int ret; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2649 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2650 | if (buf == NULL) return(-1); |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 2651 | if ((cur == NULL) || |
| 2652 | ((cur->type != XML_DOCUMENT_NODE) && |
| 2653 | (cur->type != XML_HTML_DOCUMENT_NODE))) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2654 | xmlOutputBufferClose(buf); |
| 2655 | return(-1); |
| 2656 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2657 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2658 | ctxt.doc = cur; |
| 2659 | ctxt.buf = buf; |
| 2660 | ctxt.level = 0; |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 2661 | ctxt.format = format ? 1 : 0; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2662 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2663 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 2664 | ctxt.options |= XML_SAVE_AS_XML; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2665 | xmlDocContentDumpOutput(&ctxt, cur); |
| 2666 | ret = xmlOutputBufferClose(buf); |
| 2667 | return (ret); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | /** |
| 2671 | * xmlSaveFormatFileEnc: |
| 2672 | * @filename: the filename or URL to output |
| 2673 | * @cur: the document being saved |
| 2674 | * @encoding: the name of the encoding to use or NULL. |
| 2675 | * @format: should formatting spaces be added. |
| 2676 | * |
| 2677 | * Dump an XML document to a file or an URL. |
| 2678 | * |
| 2679 | * Returns the number of bytes written or -1 in case of error. |
| 2680 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2681 | * or xmlKeepBlanksDefault(0) was called |
| 2682 | */ |
| 2683 | int |
| 2684 | xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur, |
| 2685 | const char * encoding, int format ) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2686 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2687 | xmlOutputBufferPtr buf; |
| 2688 | xmlCharEncodingHandlerPtr handler = NULL; |
| 2689 | int ret; |
| 2690 | |
| 2691 | if (cur == NULL) |
| 2692 | return(-1); |
| 2693 | |
| 2694 | if (encoding == NULL) |
| 2695 | encoding = (const char *) cur->encoding; |
| 2696 | |
| 2697 | if (encoding != NULL) { |
| 2698 | |
| 2699 | handler = xmlFindCharEncodingHandler(encoding); |
| 2700 | if (handler == NULL) |
| 2701 | return(-1); |
| 2702 | } |
| 2703 | |
| 2704 | #ifdef HAVE_ZLIB_H |
| 2705 | if (cur->compression < 0) cur->compression = xmlGetCompressMode(); |
| 2706 | #endif |
| 2707 | /* |
| 2708 | * save the content to a temp buffer. |
| 2709 | */ |
| 2710 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
| 2711 | if (buf == NULL) return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2712 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2713 | ctxt.doc = cur; |
| 2714 | ctxt.buf = buf; |
| 2715 | ctxt.level = 0; |
Adam Spragg | 8b87713 | 2010-11-01 14:24:56 +0100 | [diff] [blame] | 2716 | ctxt.format = format ? 1 : 0; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2717 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2718 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 856d928 | 2008-09-25 14:31:40 +0000 | [diff] [blame] | 2719 | ctxt.options |= XML_SAVE_AS_XML; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2720 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2721 | xmlDocContentDumpOutput(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2722 | |
| 2723 | ret = xmlOutputBufferClose(buf); |
| 2724 | return(ret); |
| 2725 | } |
| 2726 | |
| 2727 | |
| 2728 | /** |
| 2729 | * xmlSaveFileEnc: |
| 2730 | * @filename: the filename (or URL) |
| 2731 | * @cur: the document |
| 2732 | * @encoding: the name of an encoding (or NULL) |
| 2733 | * |
| 2734 | * Dump an XML document, converting it to the given encoding |
| 2735 | * |
| 2736 | * returns: the number of bytes written or -1 in case of failure. |
| 2737 | */ |
| 2738 | int |
| 2739 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 2740 | return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) ); |
| 2741 | } |
| 2742 | |
| 2743 | /** |
| 2744 | * xmlSaveFormatFile: |
| 2745 | * @filename: the filename (or URL) |
| 2746 | * @cur: the document |
| 2747 | * @format: should formatting spaces been added |
| 2748 | * |
| 2749 | * Dump an XML document to a file. Will use compression if |
| 2750 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 2751 | * used. If @format is set then the document will be indented on output. |
| 2752 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2753 | * or xmlKeepBlanksDefault(0) was called |
| 2754 | * |
| 2755 | * returns: the number of bytes written or -1 in case of failure. |
| 2756 | */ |
| 2757 | int |
| 2758 | xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) { |
| 2759 | return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) ); |
| 2760 | } |
| 2761 | |
| 2762 | /** |
| 2763 | * xmlSaveFile: |
| 2764 | * @filename: the filename (or URL) |
| 2765 | * @cur: the document |
| 2766 | * |
| 2767 | * Dump an XML document to a file. Will use compression if |
| 2768 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 2769 | * used. |
| 2770 | * returns: the number of bytes written or -1 in case of failure. |
| 2771 | */ |
| 2772 | int |
| 2773 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
| 2774 | return(xmlSaveFormatFileEnc(filename, cur, NULL, 0)); |
| 2775 | } |
| 2776 | |
| 2777 | #endif /* LIBXML_OUTPUT_ENABLED */ |
| 2778 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 2779 | #define bottom_xmlsave |
| 2780 | #include "elfgcchack.h" |