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