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) { |
| 718 | start = end = cur->content; |
| 719 | while (*end != '\0') { |
| 720 | if ((*end == ']') && (*(end + 1) == ']') && (*(end + 2) == '>')) { |
| 721 | end = end + 2; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 722 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 723 | xmlOutputBufferWrite(buf, end - start, (const char *)start); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 724 | xmlOutputBufferWrite(buf, 3, "]]>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 725 | start = end; |
| 726 | } |
| 727 | end++; |
| 728 | } |
| 729 | if (start != end) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 730 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 731 | xmlOutputBufferWriteString(buf, (const char *)start); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 732 | xmlOutputBufferWrite(buf, 3, "]]>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 733 | } |
| 734 | return; |
| 735 | } |
| 736 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 737 | xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 738 | return; |
| 739 | } |
| 740 | if (cur->type == XML_NAMESPACE_DECL) { |
| 741 | xmlNsDumpOutput(buf, (xmlNsPtr) cur); |
| 742 | return; |
| 743 | } |
| 744 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 745 | format = ctxt->format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 746 | if (format == 1) { |
| 747 | tmp = cur->children; |
| 748 | while (tmp != NULL) { |
| 749 | if ((tmp->type == XML_TEXT_NODE) || |
| 750 | (tmp->type == XML_CDATA_SECTION_NODE) || |
| 751 | (tmp->type == XML_ENTITY_REF_NODE)) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 752 | ctxt->format = 0; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 753 | break; |
| 754 | } |
| 755 | tmp = tmp->next; |
| 756 | } |
| 757 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 758 | xmlOutputBufferWrite(buf, 1, "<"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 759 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 760 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 761 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 765 | if (cur->nsDef) |
| 766 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 767 | if (cur->properties != NULL) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 768 | xmlAttrListDumpOutput(ctxt, cur->properties); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 769 | |
| 770 | if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) && |
| 771 | (cur->children == NULL) && (!xmlSaveNoEmptyTags)) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 772 | xmlOutputBufferWrite(buf, 2, "/>"); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 773 | ctxt->format = format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 774 | return; |
| 775 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 776 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 777 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 778 | xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 779 | } |
| 780 | if (cur->children != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 781 | if (ctxt->format) xmlOutputBufferWrite(buf, 1, "\n"); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 782 | if (ctxt->level >= 0) ctxt->level++; |
| 783 | xmlNodeListDumpOutput(ctxt, cur->children); |
| 784 | if (ctxt->level > 0) ctxt->level--; |
| 785 | if ((xmlIndentTreeOutput) && (ctxt->format)) |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 786 | xmlOutputBufferWrite(buf, ctxt->indent_size * |
| 787 | (ctxt->level > ctxt->indent_nr ? |
| 788 | ctxt->indent_nr : ctxt->level), |
| 789 | ctxt->indent); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 790 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 791 | xmlOutputBufferWrite(buf, 2, "</"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 792 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 793 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 794 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 798 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 799 | ctxt->format = format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /** |
| 803 | * xmlDocContentDumpOutput: |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 804 | * @cur: the document |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 805 | * |
| 806 | * Dump an XML document. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 807 | */ |
| 808 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 809 | xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 810 | #ifdef LIBXML_HTML_ENABLED |
| 811 | xmlDtdPtr dtd; |
| 812 | int is_xhtml = 0; |
| 813 | #endif |
| 814 | const xmlChar *oldenc = cur->encoding; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 815 | const xmlChar *encoding = ctxt->encoding; |
| 816 | xmlOutputBufferPtr buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 817 | |
| 818 | xmlInitParser(); |
| 819 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 820 | if (ctxt->encoding != NULL) |
| 821 | cur->encoding = BAD_CAST ctxt->encoding; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 822 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 823 | buf = ctxt->buf; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 824 | xmlOutputBufferWrite(buf, 14, "<?xml version="); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 825 | if (cur->version != NULL) |
| 826 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 827 | else |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 828 | xmlOutputBufferWrite(buf, 5, "\"1.0\""); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 829 | if (ctxt->encoding == NULL) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 830 | if (cur->encoding != NULL) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 831 | encoding = cur->encoding; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 832 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 833 | encoding = (const xmlChar *) |
| 834 | xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 835 | } |
| 836 | if (encoding != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 837 | xmlOutputBufferWrite(buf, 10, " encoding="); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 838 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 839 | } |
| 840 | switch (cur->standalone) { |
| 841 | case 0: |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 842 | xmlOutputBufferWrite(buf, 16, " standalone=\"no\""); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 843 | break; |
| 844 | case 1: |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 845 | xmlOutputBufferWrite(buf, 17, " standalone=\"yes\""); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 846 | break; |
| 847 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 848 | xmlOutputBufferWrite(buf, 3, "?>\n"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 849 | |
| 850 | #ifdef LIBXML_HTML_ENABLED |
| 851 | dtd = xmlGetIntSubset(cur); |
| 852 | if (dtd != NULL) { |
| 853 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 854 | if (is_xhtml < 0) is_xhtml = 0; |
| 855 | } |
| 856 | if (is_xhtml) { |
| 857 | if (encoding != NULL) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 858 | htmlSetMetaEncoding(cur, (const xmlChar *) ctxt->encoding); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 859 | else |
| 860 | htmlSetMetaEncoding(cur, BAD_CAST "UTF-8"); |
| 861 | } |
| 862 | #endif |
| 863 | if (cur->children != NULL) { |
| 864 | xmlNodePtr child = cur->children; |
| 865 | |
| 866 | while (child != NULL) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 867 | ctxt->level = 0; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 868 | #ifdef LIBXML_HTML_ENABLED |
| 869 | if (is_xhtml) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 870 | xhtmlNodeDumpOutput(ctxt, child); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 871 | else |
| 872 | #endif |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 873 | xmlNodeDumpOutputInternal(ctxt, child); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 874 | xmlOutputBufferWrite(buf, 1, "\n"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 875 | child = child->next; |
| 876 | } |
| 877 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 878 | if (ctxt->encoding != NULL) |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 879 | cur->encoding = oldenc; |
| 880 | } |
| 881 | |
| 882 | #ifdef LIBXML_HTML_ENABLED |
| 883 | /************************************************************************ |
| 884 | * * |
| 885 | * Functions specific to XHTML serialization * |
| 886 | * * |
| 887 | ************************************************************************/ |
| 888 | |
| 889 | /** |
| 890 | * xhtmlIsEmpty: |
| 891 | * @node: the node |
| 892 | * |
| 893 | * Check if a node is an empty xhtml node |
| 894 | * |
| 895 | * Returns 1 if the node is an empty node, 0 if not and -1 in case of error |
| 896 | */ |
| 897 | static int |
| 898 | xhtmlIsEmpty(xmlNodePtr node) { |
| 899 | if (node == NULL) |
| 900 | return(-1); |
| 901 | if (node->type != XML_ELEMENT_NODE) |
| 902 | return(0); |
| 903 | if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME))) |
| 904 | return(0); |
| 905 | if (node->children != NULL) |
| 906 | return(0); |
| 907 | switch (node->name[0]) { |
| 908 | case 'a': |
| 909 | if (xmlStrEqual(node->name, BAD_CAST "area")) |
| 910 | return(1); |
| 911 | return(0); |
| 912 | case 'b': |
| 913 | if (xmlStrEqual(node->name, BAD_CAST "br")) |
| 914 | return(1); |
| 915 | if (xmlStrEqual(node->name, BAD_CAST "base")) |
| 916 | return(1); |
| 917 | if (xmlStrEqual(node->name, BAD_CAST "basefont")) |
| 918 | return(1); |
| 919 | return(0); |
| 920 | case 'c': |
| 921 | if (xmlStrEqual(node->name, BAD_CAST "col")) |
| 922 | return(1); |
| 923 | return(0); |
| 924 | case 'f': |
| 925 | if (xmlStrEqual(node->name, BAD_CAST "frame")) |
| 926 | return(1); |
| 927 | return(0); |
| 928 | case 'h': |
| 929 | if (xmlStrEqual(node->name, BAD_CAST "hr")) |
| 930 | return(1); |
| 931 | return(0); |
| 932 | case 'i': |
| 933 | if (xmlStrEqual(node->name, BAD_CAST "img")) |
| 934 | return(1); |
| 935 | if (xmlStrEqual(node->name, BAD_CAST "input")) |
| 936 | return(1); |
| 937 | if (xmlStrEqual(node->name, BAD_CAST "isindex")) |
| 938 | return(1); |
| 939 | return(0); |
| 940 | case 'l': |
| 941 | if (xmlStrEqual(node->name, BAD_CAST "link")) |
| 942 | return(1); |
| 943 | return(0); |
| 944 | case 'm': |
| 945 | if (xmlStrEqual(node->name, BAD_CAST "meta")) |
| 946 | return(1); |
| 947 | return(0); |
| 948 | case 'p': |
| 949 | if (xmlStrEqual(node->name, BAD_CAST "param")) |
| 950 | return(1); |
| 951 | return(0); |
| 952 | } |
| 953 | return(0); |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * xhtmlAttrListDumpOutput: |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 958 | * @cur: the first attribute pointer |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 959 | * |
| 960 | * Dump a list of XML attributes |
| 961 | */ |
| 962 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 963 | xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 964 | xmlAttrPtr xml_lang = NULL; |
| 965 | xmlAttrPtr lang = NULL; |
| 966 | xmlAttrPtr name = NULL; |
| 967 | xmlAttrPtr id = NULL; |
| 968 | xmlNodePtr parent; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 969 | xmlOutputBufferPtr buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 970 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 971 | if (cur == NULL) return; |
| 972 | buf = ctxt->buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 973 | parent = cur->parent; |
| 974 | while (cur != NULL) { |
| 975 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id"))) |
| 976 | id = cur; |
| 977 | else |
| 978 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name"))) |
| 979 | name = cur; |
| 980 | else |
| 981 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang"))) |
| 982 | lang = cur; |
| 983 | else |
| 984 | if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) && |
| 985 | (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml"))) |
| 986 | xml_lang = cur; |
| 987 | else if ((cur->ns == NULL) && |
| 988 | ((cur->children == NULL) || |
| 989 | (cur->children->content == NULL) || |
| 990 | (cur->children->content[0] == 0)) && |
| 991 | (htmlIsBooleanAttr(cur->name))) { |
| 992 | if (cur->children != NULL) |
| 993 | xmlFreeNode(cur->children); |
| 994 | cur->children = xmlNewText(cur->name); |
| 995 | if (cur->children != NULL) |
| 996 | cur->children->parent = (xmlNodePtr) cur; |
| 997 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 998 | xmlAttrDumpOutput(ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 999 | cur = cur->next; |
| 1000 | } |
| 1001 | /* |
| 1002 | * C.8 |
| 1003 | */ |
| 1004 | if ((name != NULL) && (id == NULL)) { |
| 1005 | if ((parent != NULL) && (parent->name != NULL) && |
| 1006 | ((xmlStrEqual(parent->name, BAD_CAST "a")) || |
| 1007 | (xmlStrEqual(parent->name, BAD_CAST "p")) || |
| 1008 | (xmlStrEqual(parent->name, BAD_CAST "div")) || |
| 1009 | (xmlStrEqual(parent->name, BAD_CAST "img")) || |
| 1010 | (xmlStrEqual(parent->name, BAD_CAST "map")) || |
| 1011 | (xmlStrEqual(parent->name, BAD_CAST "applet")) || |
| 1012 | (xmlStrEqual(parent->name, BAD_CAST "form")) || |
| 1013 | (xmlStrEqual(parent->name, BAD_CAST "frame")) || |
| 1014 | (xmlStrEqual(parent->name, BAD_CAST "iframe")))) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1015 | xmlOutputBufferWrite(buf, 5, " id=\""); |
| 1016 | xmlAttrSerializeContent(buf, name); |
| 1017 | xmlOutputBufferWrite(buf, 1, "\""); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | /* |
| 1021 | * C.7. |
| 1022 | */ |
| 1023 | if ((lang != NULL) && (xml_lang == NULL)) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1024 | xmlOutputBufferWrite(buf, 11, " xml:lang=\""); |
| 1025 | xmlAttrSerializeContent(buf, lang); |
| 1026 | xmlOutputBufferWrite(buf, 1, "\""); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1027 | } else |
| 1028 | if ((xml_lang != NULL) && (lang == NULL)) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1029 | xmlOutputBufferWrite(buf, 7, " lang=\""); |
| 1030 | xmlAttrSerializeContent(buf, xml_lang); |
| 1031 | xmlOutputBufferWrite(buf, 1, "\""); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * xhtmlNodeListDumpOutput: |
| 1037 | * @buf: the XML buffer output |
| 1038 | * @doc: the XHTML document |
| 1039 | * @cur: the first node |
| 1040 | * @level: the imbrication level for indenting |
| 1041 | * @format: is formatting allowed |
| 1042 | * @encoding: an optional encoding string |
| 1043 | * |
| 1044 | * Dump an XML node list, recursive behaviour, children are printed too. |
| 1045 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 1046 | * or xmlKeepBlanksDefault(0) was called |
| 1047 | */ |
| 1048 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1049 | xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1050 | xmlOutputBufferPtr buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1051 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1052 | if (cur == NULL) return; |
| 1053 | buf = ctxt->buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1054 | while (cur != NULL) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1055 | if ((ctxt->format) && (xmlIndentTreeOutput) && |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1056 | (cur->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 1057 | xmlOutputBufferWrite(buf, ctxt->indent_size * |
| 1058 | (ctxt->level > ctxt->indent_nr ? |
| 1059 | ctxt->indent_nr : ctxt->level), |
| 1060 | ctxt->indent); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1061 | xhtmlNodeDumpOutput(ctxt, cur); |
| 1062 | if (ctxt->format) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1063 | xmlOutputBufferWrite(buf, 1, "\n"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1064 | } |
| 1065 | cur = cur->next; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * xhtmlNodeDumpOutput: |
| 1071 | * @buf: the XML buffer output |
| 1072 | * @doc: the XHTML document |
| 1073 | * @cur: the current node |
| 1074 | * @level: the imbrication level for indenting |
| 1075 | * @format: is formatting allowed |
| 1076 | * @encoding: an optional encoding string |
| 1077 | * |
| 1078 | * Dump an XHTML node, recursive behaviour, children are printed too. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1079 | */ |
| 1080 | static void |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1081 | xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 1082 | int format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1083 | xmlNodePtr tmp; |
| 1084 | xmlChar *start, *end; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1085 | xmlOutputBufferPtr buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1086 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1087 | if (cur == NULL) return; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1088 | if (cur->type == XML_XINCLUDE_START) |
| 1089 | return; |
| 1090 | if (cur->type == XML_XINCLUDE_END) |
| 1091 | return; |
| 1092 | if (cur->type == XML_DTD_NODE) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1093 | xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1094 | return; |
| 1095 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1096 | buf = ctxt->buf; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1097 | if (cur->type == XML_ELEMENT_DECL) { |
| 1098 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 1099 | return; |
| 1100 | } |
| 1101 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 1102 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 1103 | return; |
| 1104 | } |
| 1105 | if (cur->type == XML_ENTITY_DECL) { |
| 1106 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 1107 | return; |
| 1108 | } |
| 1109 | if (cur->type == XML_TEXT_NODE) { |
| 1110 | if (cur->content != NULL) { |
| 1111 | if ((cur->name == xmlStringText) || |
| 1112 | (cur->name != xmlStringTextNoenc)) { |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 1113 | xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1114 | } else { |
| 1115 | /* |
| 1116 | * Disable escaping, needed for XSLT |
| 1117 | */ |
| 1118 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | return; |
| 1123 | } |
| 1124 | if (cur->type == XML_PI_NODE) { |
| 1125 | if (cur->content != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1126 | xmlOutputBufferWrite(buf, 2, "<?"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1127 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 1128 | if (cur->content != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1129 | xmlOutputBufferWrite(buf, 1, " "); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1130 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 1131 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1132 | xmlOutputBufferWrite(buf, 2, "?>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1133 | } else { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1134 | xmlOutputBufferWrite(buf, 2, "<?"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1135 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1136 | xmlOutputBufferWrite(buf, 2, "?>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1137 | } |
| 1138 | return; |
| 1139 | } |
| 1140 | if (cur->type == XML_COMMENT_NODE) { |
| 1141 | if (cur->content != NULL) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1142 | xmlOutputBufferWrite(buf, 4, "<!--"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1143 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1144 | xmlOutputBufferWrite(buf, 3, "-->"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1145 | } |
| 1146 | return; |
| 1147 | } |
| 1148 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1149 | xmlOutputBufferWrite(buf, 1, "&"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1150 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1151 | xmlOutputBufferWrite(buf, 1, ";"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1152 | return; |
| 1153 | } |
| 1154 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 1155 | start = end = cur->content; |
| 1156 | while (*end != '\0') { |
| 1157 | if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') { |
| 1158 | end = end + 2; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1159 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1160 | xmlOutputBufferWrite(buf, end - start, (const char *)start); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1161 | xmlOutputBufferWrite(buf, 3, "]]>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1162 | start = end; |
| 1163 | } |
| 1164 | end++; |
| 1165 | } |
| 1166 | if (start != end) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1167 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1168 | xmlOutputBufferWriteString(buf, (const char *)start); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1169 | xmlOutputBufferWrite(buf, 3, "]]>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1170 | } |
| 1171 | return; |
| 1172 | } |
| 1173 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1174 | format = ctxt->format; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1175 | if (format == 1) { |
| 1176 | tmp = cur->children; |
| 1177 | while (tmp != NULL) { |
| 1178 | if ((tmp->type == XML_TEXT_NODE) || |
| 1179 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 1180 | format = 0; |
| 1181 | break; |
| 1182 | } |
| 1183 | tmp = tmp->next; |
| 1184 | } |
| 1185 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1186 | xmlOutputBufferWrite(buf, 1, "<"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1187 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 1188 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1189 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 1193 | if (cur->nsDef) |
| 1194 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 1195 | if ((xmlStrEqual(cur->name, BAD_CAST "html") && |
| 1196 | (cur->ns == NULL) && (cur->nsDef == NULL))) { |
| 1197 | /* |
| 1198 | * 3.1.1. Strictly Conforming Documents A.3.1.1 3/ |
| 1199 | */ |
| 1200 | xmlOutputBufferWriteString(buf, |
| 1201 | " xmlns=\"http://www.w3.org/1999/xhtml\""); |
| 1202 | } |
| 1203 | if (cur->properties != NULL) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1204 | xhtmlAttrListDumpOutput(ctxt, cur->properties); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1205 | |
| 1206 | if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) { |
| 1207 | if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) && |
| 1208 | (xhtmlIsEmpty(cur) == 1)) { |
| 1209 | /* |
| 1210 | * C.2. Empty Elements |
| 1211 | */ |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1212 | xmlOutputBufferWrite(buf, 3, " />"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1213 | } else { |
| 1214 | /* |
| 1215 | * C.3. Element Minimization and Empty Element Content |
| 1216 | */ |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1217 | xmlOutputBufferWrite(buf, 3, "></"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1218 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 1219 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1220 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1221 | } |
| 1222 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1223 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1224 | } |
| 1225 | return; |
| 1226 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1227 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1228 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 1229 | xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * 4.8. Script and Style elements |
| 1234 | */ |
| 1235 | if ((cur->type == XML_ELEMENT_NODE) && |
| 1236 | ((xmlStrEqual(cur->name, BAD_CAST "script")) || |
| 1237 | (xmlStrEqual(cur->name, BAD_CAST "style"))) && |
| 1238 | ((cur->ns == NULL) || |
| 1239 | (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) { |
| 1240 | xmlNodePtr child = cur->children; |
| 1241 | |
| 1242 | while (child != NULL) { |
| 1243 | if ((child->type == XML_TEXT_NODE) || |
| 1244 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 1245 | /* |
| 1246 | * Apparently CDATA escaping for style just break on IE, |
| 1247 | * mozilla and galeon, so ... |
| 1248 | */ |
| 1249 | if (xmlStrEqual(cur->name, BAD_CAST "style") && |
| 1250 | (xmlStrchr(child->content, '<') == NULL) && |
| 1251 | (xmlStrchr(child->content, '>') == NULL) && |
| 1252 | (xmlStrchr(child->content, '&') == NULL)) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1253 | int level = ctxt->level; |
| 1254 | int indent = ctxt->format; |
| 1255 | |
| 1256 | ctxt->level = 0; |
| 1257 | ctxt->format = 0; |
| 1258 | xhtmlNodeDumpOutput(ctxt, child); |
| 1259 | ctxt->level = level; |
| 1260 | ctxt->format = indent; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1261 | } else { |
| 1262 | start = end = child->content; |
| 1263 | while (*end != '\0') { |
| 1264 | if (*end == ']' && |
| 1265 | *(end + 1) == ']' && |
| 1266 | *(end + 2) == '>') { |
| 1267 | end = end + 2; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1268 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1269 | xmlOutputBufferWrite(buf, end - start, |
| 1270 | (const char *)start); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1271 | xmlOutputBufferWrite(buf, 3, "]]>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1272 | start = end; |
| 1273 | } |
| 1274 | end++; |
| 1275 | } |
| 1276 | if (start != end) { |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1277 | xmlOutputBufferWrite(buf, 9, "<![CDATA["); |
| 1278 | xmlOutputBufferWrite(buf, end - start, |
| 1279 | (const char *)start); |
| 1280 | xmlOutputBufferWrite(buf, 3, "]]>"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | } else { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1284 | int level = ctxt->level; |
| 1285 | int indent = ctxt->format; |
| 1286 | |
| 1287 | ctxt->level = 0; |
| 1288 | ctxt->format = 0; |
| 1289 | xhtmlNodeDumpOutput(ctxt, child); |
| 1290 | ctxt->level = level; |
| 1291 | ctxt->format = indent; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1292 | } |
| 1293 | child = child->next; |
| 1294 | } |
| 1295 | } else if (cur->children != NULL) { |
Daniel Veillard | f0244ce | 2004-05-09 23:48:39 +0000 | [diff] [blame] | 1296 | int indent = ctxt->format; |
| 1297 | |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1298 | if (format) xmlOutputBufferWrite(buf, 1, "\n"); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1299 | if (ctxt->level >= 0) ctxt->level++; |
Daniel Veillard | f0244ce | 2004-05-09 23:48:39 +0000 | [diff] [blame] | 1300 | ctxt->format = format; |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1301 | xhtmlNodeListDumpOutput(ctxt, cur->children); |
| 1302 | if (ctxt->level > 0) ctxt->level--; |
Daniel Veillard | f0244ce | 2004-05-09 23:48:39 +0000 | [diff] [blame] | 1303 | ctxt->format = indent; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1304 | if ((xmlIndentTreeOutput) && (format)) |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 1305 | xmlOutputBufferWrite(buf, ctxt->indent_size * |
| 1306 | (ctxt->level > ctxt->indent_nr ? |
| 1307 | ctxt->indent_nr : ctxt->level), |
| 1308 | ctxt->indent); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1309 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1310 | xmlOutputBufferWrite(buf, 2, "</"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1311 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 1312 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1313 | xmlOutputBufferWrite(buf, 1, ":"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1317 | xmlOutputBufferWrite(buf, 1, ">"); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1318 | } |
| 1319 | #endif |
| 1320 | |
| 1321 | /************************************************************************ |
| 1322 | * * |
| 1323 | * Public entry points * |
| 1324 | * * |
| 1325 | ************************************************************************/ |
| 1326 | |
| 1327 | /** |
| 1328 | * xmlSaveToFd: |
| 1329 | * @fd: a file descriptor number |
| 1330 | * @encoding: the encoding name to use or NULL |
| 1331 | * @options: a set of xmlSaveOptions |
| 1332 | * |
| 1333 | * Create a document saving context serializing to a file descriptor |
| 1334 | * with the encoding and the options given. |
| 1335 | * |
| 1336 | * Returns a new serialization context or NULL in case of error. |
| 1337 | */ |
| 1338 | xmlSaveCtxtPtr |
| 1339 | xmlSaveToFd(int fd, const char *encoding, int options) |
| 1340 | { |
| 1341 | xmlSaveCtxtPtr ret; |
| 1342 | |
| 1343 | ret = xmlNewSaveCtxt(encoding, options); |
| 1344 | if (ret == NULL) return(NULL); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1345 | ret->buf = xmlOutputBufferCreateFd(fd, ret->handler); |
| 1346 | if (ret->buf == NULL) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1347 | xmlFreeSaveCtxt(ret); |
| 1348 | return(NULL); |
| 1349 | } |
| 1350 | return(ret); |
| 1351 | } |
| 1352 | |
| 1353 | /** |
| 1354 | * xmlSaveToFilename: |
| 1355 | * @filename: a file name or an URL |
| 1356 | * @encoding: the encoding name to use or NULL |
| 1357 | * @options: a set of xmlSaveOptions |
| 1358 | * |
| 1359 | * Create a document saving context serializing to a filename or possibly |
| 1360 | * to an URL (but this is less reliable) with the encoding and the options |
| 1361 | * given. |
| 1362 | * |
| 1363 | * Returns a new serialization context or NULL in case of error. |
| 1364 | */ |
| 1365 | xmlSaveCtxtPtr |
| 1366 | xmlSaveToFilename(const char *filename, const char *encoding, int options) |
| 1367 | { |
| 1368 | xmlSaveCtxtPtr ret; |
| 1369 | int compression = 0; /* TODO handle compression option */ |
| 1370 | |
| 1371 | ret = xmlNewSaveCtxt(encoding, options); |
| 1372 | if (ret == NULL) return(NULL); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1373 | ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler, |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1374 | compression); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1375 | if (ret->buf == NULL) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1376 | xmlFreeSaveCtxt(ret); |
| 1377 | return(NULL); |
| 1378 | } |
| 1379 | return(ret); |
| 1380 | } |
| 1381 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1382 | /** |
| 1383 | * xmlSaveToBuffer: |
| 1384 | * @buffer: a buffer |
| 1385 | * @encoding: the encoding name to use or NULL |
| 1386 | * @options: a set of xmlSaveOptions |
| 1387 | * |
| 1388 | * Create a document saving context serializing to a buffer |
| 1389 | * with the encoding and the options given |
| 1390 | * |
| 1391 | * Returns a new serialization context or NULL in case of error. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1392 | xmlSaveCtxtPtr |
| 1393 | xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options) |
| 1394 | { |
| 1395 | TODO |
| 1396 | return(NULL); |
| 1397 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1398 | */ |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1399 | |
| 1400 | /** |
| 1401 | * xmlSaveToIO: |
| 1402 | * @iowrite: an I/O write function |
| 1403 | * @ioclose: an I/O close function |
| 1404 | * @ioctx: an I/O handler |
| 1405 | * @encoding: the encoding name to use or NULL |
| 1406 | * @options: a set of xmlSaveOptions |
| 1407 | * |
| 1408 | * Create a document saving context serializing to a file descriptor |
| 1409 | * with the encoding and the options given |
| 1410 | * |
| 1411 | * Returns a new serialization context or NULL in case of error. |
| 1412 | */ |
| 1413 | xmlSaveCtxtPtr |
| 1414 | xmlSaveToIO(xmlOutputWriteCallback iowrite, |
| 1415 | xmlOutputCloseCallback ioclose, |
| 1416 | void *ioctx, const char *encoding, int options) |
| 1417 | { |
| 1418 | xmlSaveCtxtPtr ret; |
| 1419 | |
| 1420 | ret = xmlNewSaveCtxt(encoding, options); |
| 1421 | if (ret == NULL) return(NULL); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1422 | ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler); |
| 1423 | if (ret->buf == NULL) { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1424 | xmlFreeSaveCtxt(ret); |
| 1425 | return(NULL); |
| 1426 | } |
| 1427 | return(ret); |
| 1428 | } |
| 1429 | |
| 1430 | /** |
| 1431 | * xmlSaveDoc: |
| 1432 | * @ctxt: a document saving context |
| 1433 | * @doc: a document |
| 1434 | * |
| 1435 | * Save a full document to a saving context |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 1436 | * TODO: The function is not fully implemented yet as it does not return the |
| 1437 | * byte count but 0 instead |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1438 | * |
| 1439 | * Returns the number of byte written or -1 in case of error |
| 1440 | */ |
| 1441 | long |
| 1442 | xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc) |
| 1443 | { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1444 | long ret = 0; |
| 1445 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1446 | if ((ctxt == NULL) || (doc == NULL)) return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1447 | xmlDocContentDumpOutput(ctxt, doc); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1448 | return(ret); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * xmlSaveTree: |
| 1453 | * @ctxt: a document saving context |
| 1454 | * @node: a document |
| 1455 | * |
| 1456 | * Save a subtree starting at the node parameter to a saving context |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 1457 | * TODO: The function is not fully implemented yet as it does not return the |
| 1458 | * byte count but 0 instead |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1459 | * |
| 1460 | * Returns the number of byte written or -1 in case of error |
| 1461 | */ |
| 1462 | long |
| 1463 | xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node) |
| 1464 | { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1465 | long ret = 0; |
| 1466 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1467 | if ((ctxt == NULL) || (node == NULL)) return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1468 | xmlNodeDumpOutputInternal(ctxt, node); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1469 | return(ret); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /** |
| 1473 | * xmlSaveFlush: |
| 1474 | * @ctxt: a document saving context |
| 1475 | * |
| 1476 | * Flush a document saving context, i.e. make sure that all bytes have |
| 1477 | * been output. |
| 1478 | * |
| 1479 | * Returns the number of byte written or -1 in case of error. |
| 1480 | */ |
| 1481 | int |
| 1482 | xmlSaveFlush(xmlSaveCtxtPtr ctxt) |
| 1483 | { |
| 1484 | if (ctxt == NULL) return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1485 | if (ctxt->buf == NULL) return(-1); |
| 1486 | return(xmlOutputBufferFlush(ctxt->buf)); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /** |
| 1490 | * xmlSaveClose: |
| 1491 | * @ctxt: a document saving context |
| 1492 | * |
| 1493 | * Close a document saving context, i.e. make sure that all bytes have |
| 1494 | * been output and free the associated data. |
| 1495 | * |
| 1496 | * Returns the number of byte written or -1 in case of error. |
| 1497 | */ |
| 1498 | int |
| 1499 | xmlSaveClose(xmlSaveCtxtPtr ctxt) |
| 1500 | { |
| 1501 | int ret; |
| 1502 | |
| 1503 | if (ctxt == NULL) return(-1); |
| 1504 | ret = xmlSaveFlush(ctxt); |
| 1505 | xmlFreeSaveCtxt(ctxt); |
| 1506 | return(ret); |
| 1507 | } |
| 1508 | |
Daniel Veillard | 3995bc3 | 2004-05-15 18:57:31 +0000 | [diff] [blame] | 1509 | /** |
| 1510 | * xmlSaveSetEscape: |
| 1511 | * @ctxt: a document saving context |
| 1512 | * @escape: the escaping function |
| 1513 | * |
| 1514 | * Set a custom escaping function to be used for text in element content |
| 1515 | * |
| 1516 | * Returns 0 if successful or -1 in case of error. |
| 1517 | */ |
| 1518 | int |
| 1519 | xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape) |
| 1520 | { |
| 1521 | if (ctxt == NULL) return(-1); |
| 1522 | ctxt->escape = escape; |
| 1523 | return(0); |
| 1524 | } |
| 1525 | |
| 1526 | /** |
| 1527 | * xmlSaveSetAttrEscape: |
| 1528 | * @ctxt: a document saving context |
| 1529 | * @escape: the escaping function |
| 1530 | * |
| 1531 | * Set a custom escaping function to be used for text in attribute content |
| 1532 | * |
| 1533 | * Returns 0 if successful or -1 in case of error. |
| 1534 | */ |
| 1535 | int |
| 1536 | xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape) |
| 1537 | { |
| 1538 | if (ctxt == NULL) return(-1); |
| 1539 | ctxt->escapeAttr = escape; |
| 1540 | return(0); |
| 1541 | } |
| 1542 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1543 | /************************************************************************ |
| 1544 | * * |
| 1545 | * Public entry points based on buffers * |
| 1546 | * * |
| 1547 | ************************************************************************/ |
| 1548 | /** |
| 1549 | * xmlAttrSerializeTxtContent: |
| 1550 | * @buf: the XML buffer output |
| 1551 | * @doc: the document |
| 1552 | * @attr: the attribute node |
| 1553 | * @string: the text content |
| 1554 | * |
| 1555 | * Serialize text attribute values to an xml simple buffer |
| 1556 | */ |
| 1557 | void |
| 1558 | xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc, |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1559 | xmlAttrPtr attr, const xmlChar * string) |
| 1560 | { |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1561 | xmlChar *base, *cur; |
| 1562 | |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1563 | if (string == NULL) |
| 1564 | return; |
| 1565 | base = cur = (xmlChar *) string; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1566 | while (*cur != 0) { |
| 1567 | if (*cur == '\n') { |
| 1568 | if (base != cur) |
| 1569 | xmlBufferAdd(buf, base, cur - base); |
| 1570 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
| 1571 | cur++; |
| 1572 | base = cur; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1573 | } else if (*cur == '\r') { |
| 1574 | if (base != cur) |
| 1575 | xmlBufferAdd(buf, base, cur - base); |
| 1576 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
| 1577 | cur++; |
| 1578 | base = cur; |
| 1579 | } else if (*cur == '\t') { |
| 1580 | if (base != cur) |
| 1581 | xmlBufferAdd(buf, base, cur - base); |
| 1582 | xmlBufferAdd(buf, BAD_CAST "	", 4); |
| 1583 | cur++; |
| 1584 | base = cur; |
| 1585 | } else if (*cur == '"') { |
| 1586 | if (base != cur) |
| 1587 | xmlBufferAdd(buf, base, cur - base); |
| 1588 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 1589 | cur++; |
| 1590 | base = cur; |
| 1591 | } else if (*cur == '<') { |
| 1592 | if (base != cur) |
| 1593 | xmlBufferAdd(buf, base, cur - base); |
| 1594 | xmlBufferAdd(buf, BAD_CAST "<", 4); |
| 1595 | cur++; |
| 1596 | base = cur; |
| 1597 | } else if (*cur == '>') { |
| 1598 | if (base != cur) |
| 1599 | xmlBufferAdd(buf, base, cur - base); |
| 1600 | xmlBufferAdd(buf, BAD_CAST ">", 4); |
| 1601 | cur++; |
| 1602 | base = cur; |
| 1603 | } else if (*cur == '&') { |
| 1604 | if (base != cur) |
| 1605 | xmlBufferAdd(buf, base, cur - base); |
| 1606 | xmlBufferAdd(buf, BAD_CAST "&", 5); |
| 1607 | cur++; |
| 1608 | base = cur; |
| 1609 | } else if ((*cur >= 0x80) && ((doc == NULL) || |
| 1610 | (doc->encoding == NULL))) { |
| 1611 | /* |
| 1612 | * We assume we have UTF-8 content. |
| 1613 | */ |
| 1614 | unsigned char tmp[10]; |
| 1615 | int val = 0, l = 1; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1616 | |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1617 | if (base != cur) |
| 1618 | xmlBufferAdd(buf, base, cur - base); |
| 1619 | if (*cur < 0xC0) { |
| 1620 | xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL); |
| 1621 | if (doc != NULL) |
| 1622 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 1623 | xmlSerializeHexCharRef(tmp, *cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1624 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1625 | cur++; |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1626 | base = cur; |
| 1627 | continue; |
| 1628 | } else if (*cur < 0xE0) { |
| 1629 | val = (cur[0]) & 0x1F; |
| 1630 | val <<= 6; |
| 1631 | val |= (cur[1]) & 0x3F; |
| 1632 | l = 2; |
| 1633 | } else if (*cur < 0xF0) { |
| 1634 | val = (cur[0]) & 0x0F; |
| 1635 | val <<= 6; |
| 1636 | val |= (cur[1]) & 0x3F; |
| 1637 | val <<= 6; |
| 1638 | val |= (cur[2]) & 0x3F; |
| 1639 | l = 3; |
| 1640 | } else if (*cur < 0xF8) { |
| 1641 | val = (cur[0]) & 0x07; |
| 1642 | val <<= 6; |
| 1643 | val |= (cur[1]) & 0x3F; |
| 1644 | val <<= 6; |
| 1645 | val |= (cur[2]) & 0x3F; |
| 1646 | val <<= 6; |
| 1647 | val |= (cur[3]) & 0x3F; |
| 1648 | l = 4; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1649 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1650 | if ((l == 1) || (!IS_CHAR(val))) { |
| 1651 | xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL); |
| 1652 | if (doc != NULL) |
| 1653 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 1654 | |
| 1655 | xmlSerializeHexCharRef(tmp, *cur); |
| 1656 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 1657 | cur++; |
| 1658 | base = cur; |
| 1659 | continue; |
| 1660 | } |
| 1661 | /* |
| 1662 | * We could do multiple things here. Just save |
| 1663 | * as a char ref |
| 1664 | */ |
| 1665 | xmlSerializeHexCharRef(tmp, val); |
| 1666 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 1667 | cur += l; |
| 1668 | base = cur; |
| 1669 | } else { |
| 1670 | cur++; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1671 | } |
Daniel Veillard | 7a6361f | 2004-05-15 16:37:50 +0000 | [diff] [blame] | 1672 | } |
| 1673 | if (base != cur) |
| 1674 | xmlBufferAdd(buf, base, cur - base); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | /** |
| 1678 | * xmlNodeDump: |
| 1679 | * @buf: the XML buffer output |
| 1680 | * @doc: the document |
| 1681 | * @cur: the current node |
| 1682 | * @level: the imbrication level for indenting |
| 1683 | * @format: is formatting allowed |
| 1684 | * |
| 1685 | * Dump an XML node, recursive behaviour,children are printed too. |
| 1686 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 1687 | * or xmlKeepBlanksDefault(0) was called |
| 1688 | * |
| 1689 | * Returns the number of bytes written to the buffer or -1 in case of error |
| 1690 | */ |
| 1691 | int |
| 1692 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 1693 | int format) |
| 1694 | { |
| 1695 | unsigned int use; |
| 1696 | int ret; |
| 1697 | xmlOutputBufferPtr outbuf; |
| 1698 | |
| 1699 | xmlInitParser(); |
| 1700 | |
| 1701 | if (cur == NULL) { |
| 1702 | #ifdef DEBUG_TREE |
| 1703 | xmlGenericError(xmlGenericErrorContext, |
| 1704 | "xmlNodeDump : node == NULL\n"); |
| 1705 | #endif |
| 1706 | return (-1); |
| 1707 | } |
| 1708 | if (buf == NULL) { |
| 1709 | #ifdef DEBUG_TREE |
| 1710 | xmlGenericError(xmlGenericErrorContext, |
| 1711 | "xmlNodeDump : buf == NULL\n"); |
| 1712 | #endif |
| 1713 | return (-1); |
| 1714 | } |
| 1715 | outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); |
| 1716 | if (outbuf == NULL) { |
| 1717 | xmlSaveErrMemory("creating buffer"); |
| 1718 | return (-1); |
| 1719 | } |
| 1720 | memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer)); |
| 1721 | outbuf->buffer = buf; |
| 1722 | outbuf->encoder = NULL; |
| 1723 | outbuf->writecallback = NULL; |
| 1724 | outbuf->closecallback = NULL; |
| 1725 | outbuf->context = NULL; |
| 1726 | outbuf->written = 0; |
| 1727 | |
| 1728 | use = buf->use; |
| 1729 | xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL); |
| 1730 | xmlFree(outbuf); |
| 1731 | ret = buf->use - use; |
| 1732 | return (ret); |
| 1733 | } |
| 1734 | |
| 1735 | /** |
| 1736 | * xmlElemDump: |
| 1737 | * @f: the FILE * for the output |
| 1738 | * @doc: the document |
| 1739 | * @cur: the current node |
| 1740 | * |
| 1741 | * Dump an XML/HTML node, recursive behaviour, children are printed too. |
| 1742 | */ |
| 1743 | void |
| 1744 | xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur) |
| 1745 | { |
| 1746 | xmlOutputBufferPtr outbuf; |
| 1747 | |
| 1748 | xmlInitParser(); |
| 1749 | |
| 1750 | if (cur == NULL) { |
| 1751 | #ifdef DEBUG_TREE |
| 1752 | xmlGenericError(xmlGenericErrorContext, |
| 1753 | "xmlElemDump : cur == NULL\n"); |
| 1754 | #endif |
| 1755 | return; |
| 1756 | } |
| 1757 | #ifdef DEBUG_TREE |
| 1758 | if (doc == NULL) { |
| 1759 | xmlGenericError(xmlGenericErrorContext, |
| 1760 | "xmlElemDump : doc == NULL\n"); |
| 1761 | } |
| 1762 | #endif |
| 1763 | |
| 1764 | outbuf = xmlOutputBufferCreateFile(f, NULL); |
| 1765 | if (outbuf == NULL) |
| 1766 | return; |
| 1767 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 1768 | #ifdef LIBXML_HTML_ENABLED |
| 1769 | htmlNodeDumpOutput(outbuf, doc, cur, NULL); |
| 1770 | #else |
| 1771 | xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n"); |
| 1772 | #endif /* LIBXML_HTML_ENABLED */ |
| 1773 | } else |
| 1774 | xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL); |
| 1775 | xmlOutputBufferClose(outbuf); |
| 1776 | } |
| 1777 | |
| 1778 | /************************************************************************ |
| 1779 | * * |
| 1780 | * Saving functions front-ends * |
| 1781 | * * |
| 1782 | ************************************************************************/ |
| 1783 | |
| 1784 | /** |
| 1785 | * xmlNodeDumpOutput: |
| 1786 | * @buf: the XML buffer output |
| 1787 | * @doc: the document |
| 1788 | * @cur: the current node |
| 1789 | * @level: the imbrication level for indenting |
| 1790 | * @format: is formatting allowed |
| 1791 | * @encoding: an optional encoding string |
| 1792 | * |
| 1793 | * Dump an XML node, recursive behaviour, children are printed too. |
| 1794 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 1795 | * or xmlKeepBlanksDefault(0) was called |
| 1796 | */ |
| 1797 | void |
| 1798 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 1799 | int level, int format, const char *encoding) |
| 1800 | { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1801 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1802 | #ifdef LIBXML_HTML_ENABLED |
| 1803 | xmlDtdPtr dtd; |
| 1804 | int is_xhtml = 0; |
| 1805 | #endif |
| 1806 | |
| 1807 | xmlInitParser(); |
| 1808 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 1809 | if ((buf == NULL) || (cur == NULL)) return; |
| 1810 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1811 | memset(&ctxt, 0, sizeof(ctxt)); |
| 1812 | ctxt.doc = doc; |
| 1813 | ctxt.buf = buf; |
| 1814 | ctxt.level = level; |
| 1815 | ctxt.format = format; |
| 1816 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 1817 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1818 | |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1819 | #ifdef LIBXML_HTML_ENABLED |
| 1820 | dtd = xmlGetIntSubset(doc); |
| 1821 | if (dtd != NULL) { |
| 1822 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 1823 | if (is_xhtml < 0) |
| 1824 | is_xhtml = 0; |
| 1825 | if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) && |
| 1826 | (cur->type == XML_ELEMENT_NODE) && |
| 1827 | (xmlStrEqual(cur->name, BAD_CAST "html"))) { |
| 1828 | if (encoding != NULL) |
| 1829 | htmlSetMetaEncoding((htmlDocPtr) doc, |
| 1830 | (const xmlChar *) encoding); |
| 1831 | else |
| 1832 | htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8"); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | if (is_xhtml) |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1837 | xhtmlNodeDumpOutput(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1838 | else |
| 1839 | #endif |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1840 | xmlNodeDumpOutputInternal(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * xmlDocDumpFormatMemoryEnc: |
| 1845 | * @out_doc: Document to generate XML text from |
| 1846 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 1847 | * @doc_txt_len: Length of the generated XML text |
| 1848 | * @txt_encoding: Character encoding to use when generating XML text |
| 1849 | * @format: should formatting spaces been added |
| 1850 | * |
| 1851 | * Dump the current DOM tree into memory using the character encoding specified |
| 1852 | * by the caller. Note it is up to the caller of this function to free the |
| 1853 | * allocated memory with xmlFree(). |
| 1854 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 1855 | * or xmlKeepBlanksDefault(0) was called |
| 1856 | */ |
| 1857 | |
| 1858 | void |
| 1859 | xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 1860 | int * doc_txt_len, const char * txt_encoding, |
| 1861 | int format) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1862 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1863 | int dummy = 0; |
| 1864 | xmlOutputBufferPtr out_buff = NULL; |
| 1865 | xmlCharEncodingHandlerPtr conv_hdlr = NULL; |
| 1866 | |
| 1867 | if (doc_txt_len == NULL) { |
| 1868 | doc_txt_len = &dummy; /* Continue, caller just won't get length */ |
| 1869 | } |
| 1870 | |
| 1871 | if (doc_txt_ptr == NULL) { |
| 1872 | *doc_txt_len = 0; |
| 1873 | return; |
| 1874 | } |
| 1875 | |
| 1876 | *doc_txt_ptr = NULL; |
| 1877 | *doc_txt_len = 0; |
| 1878 | |
| 1879 | if (out_doc == NULL) { |
| 1880 | /* No document, no output */ |
| 1881 | return; |
| 1882 | } |
| 1883 | |
| 1884 | /* |
| 1885 | * Validate the encoding value, if provided. |
| 1886 | * This logic is copied from xmlSaveFileEnc. |
| 1887 | */ |
| 1888 | |
| 1889 | if (txt_encoding == NULL) |
| 1890 | txt_encoding = (const char *) out_doc->encoding; |
| 1891 | if (txt_encoding != NULL) { |
| 1892 | conv_hdlr = xmlFindCharEncodingHandler(txt_encoding); |
| 1893 | if ( conv_hdlr == NULL ) { |
| 1894 | xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc, |
| 1895 | txt_encoding); |
| 1896 | return; |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { |
| 1901 | xmlSaveErrMemory("creating buffer"); |
| 1902 | return; |
| 1903 | } |
| 1904 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1905 | memset(&ctxt, 0, sizeof(ctxt)); |
| 1906 | ctxt.doc = out_doc; |
| 1907 | ctxt.buf = out_buff; |
| 1908 | ctxt.level = 0; |
| 1909 | ctxt.format = format; |
| 1910 | ctxt.encoding = (const xmlChar *) txt_encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 1911 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1912 | xmlDocContentDumpOutput(&ctxt, out_doc); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1913 | xmlOutputBufferFlush(out_buff); |
| 1914 | if (out_buff->conv != NULL) { |
| 1915 | *doc_txt_len = out_buff->conv->use; |
| 1916 | *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len); |
| 1917 | } else { |
| 1918 | *doc_txt_len = out_buff->buffer->use; |
| 1919 | *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len); |
| 1920 | } |
| 1921 | (void)xmlOutputBufferClose(out_buff); |
| 1922 | |
| 1923 | if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) { |
| 1924 | *doc_txt_len = 0; |
| 1925 | xmlSaveErrMemory("creating output"); |
| 1926 | } |
| 1927 | |
| 1928 | return; |
| 1929 | } |
| 1930 | |
| 1931 | /** |
| 1932 | * xmlDocDumpMemory: |
| 1933 | * @cur: the document |
| 1934 | * @mem: OUT: the memory pointer |
| 1935 | * @size: OUT: the memory length |
| 1936 | * |
| 1937 | * Dump an XML document in memory and return the #xmlChar * and it's size |
| 1938 | * in bytes. It's up to the caller to free the memory with xmlFree(). |
| 1939 | * The resulting byte array is zero terminated, though the last 0 is not |
| 1940 | * included in the returned size. |
| 1941 | */ |
| 1942 | void |
| 1943 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
| 1944 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0); |
| 1945 | } |
| 1946 | |
| 1947 | /** |
| 1948 | * xmlDocDumpFormatMemory: |
| 1949 | * @cur: the document |
| 1950 | * @mem: OUT: the memory pointer |
| 1951 | * @size: OUT: the memory length |
| 1952 | * @format: should formatting spaces been added |
| 1953 | * |
| 1954 | * |
| 1955 | * Dump an XML document in memory and return the #xmlChar * and it's size. |
| 1956 | * It's up to the caller to free the memory with xmlFree(). |
| 1957 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 1958 | * or xmlKeepBlanksDefault(0) was called |
| 1959 | */ |
| 1960 | void |
| 1961 | xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) { |
| 1962 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format); |
| 1963 | } |
| 1964 | |
| 1965 | /** |
| 1966 | * xmlDocDumpMemoryEnc: |
| 1967 | * @out_doc: Document to generate XML text from |
| 1968 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 1969 | * @doc_txt_len: Length of the generated XML text |
| 1970 | * @txt_encoding: Character encoding to use when generating XML text |
| 1971 | * |
| 1972 | * Dump the current DOM tree into memory using the character encoding specified |
| 1973 | * by the caller. Note it is up to the caller of this function to free the |
| 1974 | * allocated memory with xmlFree(). |
| 1975 | */ |
| 1976 | |
| 1977 | void |
| 1978 | xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 1979 | int * doc_txt_len, const char * txt_encoding) { |
| 1980 | xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len, |
| 1981 | txt_encoding, 0); |
| 1982 | } |
| 1983 | |
| 1984 | /** |
| 1985 | * xmlDocFormatDump: |
| 1986 | * @f: the FILE* |
| 1987 | * @cur: the document |
| 1988 | * @format: should formatting spaces been added |
| 1989 | * |
| 1990 | * Dump an XML document to an open FILE. |
| 1991 | * |
| 1992 | * returns: the number of bytes written or -1 in case of failure. |
| 1993 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 1994 | * or xmlKeepBlanksDefault(0) was called |
| 1995 | */ |
| 1996 | int |
| 1997 | xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 1998 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 1999 | xmlOutputBufferPtr buf; |
| 2000 | const char * encoding; |
| 2001 | xmlCharEncodingHandlerPtr handler = NULL; |
| 2002 | int ret; |
| 2003 | |
| 2004 | if (cur == NULL) { |
| 2005 | #ifdef DEBUG_TREE |
| 2006 | xmlGenericError(xmlGenericErrorContext, |
| 2007 | "xmlDocDump : document == NULL\n"); |
| 2008 | #endif |
| 2009 | return(-1); |
| 2010 | } |
| 2011 | encoding = (const char *) cur->encoding; |
| 2012 | |
| 2013 | if (encoding != NULL) { |
| 2014 | handler = xmlFindCharEncodingHandler(encoding); |
| 2015 | if (handler == NULL) { |
| 2016 | xmlFree((char *) cur->encoding); |
| 2017 | cur->encoding = NULL; |
| 2018 | } |
| 2019 | } |
| 2020 | buf = xmlOutputBufferCreateFile(f, handler); |
| 2021 | if (buf == NULL) return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2022 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2023 | ctxt.doc = cur; |
| 2024 | ctxt.buf = buf; |
| 2025 | ctxt.level = 0; |
| 2026 | ctxt.format = format; |
| 2027 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2028 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2029 | xmlDocContentDumpOutput(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2030 | |
| 2031 | ret = xmlOutputBufferClose(buf); |
| 2032 | return(ret); |
| 2033 | } |
| 2034 | |
| 2035 | /** |
| 2036 | * xmlDocDump: |
| 2037 | * @f: the FILE* |
| 2038 | * @cur: the document |
| 2039 | * |
| 2040 | * Dump an XML document to an open FILE. |
| 2041 | * |
| 2042 | * returns: the number of bytes written or -1 in case of failure. |
| 2043 | */ |
| 2044 | int |
| 2045 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
| 2046 | return(xmlDocFormatDump (f, cur, 0)); |
| 2047 | } |
| 2048 | |
| 2049 | /** |
| 2050 | * xmlSaveFileTo: |
| 2051 | * @buf: an output I/O buffer |
| 2052 | * @cur: the document |
| 2053 | * @encoding: the encoding if any assuming the I/O layer handles the trancoding |
| 2054 | * |
| 2055 | * Dump an XML document to an I/O buffer. |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2056 | * Warning ! This call xmlOutputBufferClose() on buf which is not available |
| 2057 | * after this call. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2058 | * |
| 2059 | * returns: the number of bytes written or -1 in case of failure. |
| 2060 | */ |
| 2061 | int |
| 2062 | xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2063 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2064 | int ret; |
| 2065 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2066 | if (buf == NULL) return(-1); |
| 2067 | if (cur == NULL) { |
| 2068 | xmlOutputBufferClose(buf); |
| 2069 | return(-1); |
| 2070 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2071 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2072 | ctxt.doc = cur; |
| 2073 | ctxt.buf = buf; |
| 2074 | ctxt.level = 0; |
| 2075 | ctxt.format = 0; |
| 2076 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2077 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2078 | xmlDocContentDumpOutput(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2079 | ret = xmlOutputBufferClose(buf); |
| 2080 | return(ret); |
| 2081 | } |
| 2082 | |
| 2083 | /** |
| 2084 | * xmlSaveFormatFileTo: |
| 2085 | * @buf: an output I/O buffer |
| 2086 | * @cur: the document |
| 2087 | * @encoding: the encoding if any assuming the I/O layer handles the trancoding |
| 2088 | * @format: should formatting spaces been added |
| 2089 | * |
| 2090 | * Dump an XML document to an I/O buffer. |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2091 | * Warning ! This call xmlOutputBufferClose() on buf which is not available |
| 2092 | * after this call. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2093 | * |
| 2094 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2095 | */ |
| 2096 | int |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2097 | xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 2098 | const char *encoding, int format) |
| 2099 | { |
| 2100 | xmlSaveCtxt ctxt; |
| 2101 | int ret; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2102 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2103 | if (buf == NULL) return(-1); |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 2104 | if ((cur == NULL) || |
| 2105 | ((cur->type != XML_DOCUMENT_NODE) && |
| 2106 | (cur->type != XML_HTML_DOCUMENT_NODE))) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 2107 | xmlOutputBufferClose(buf); |
| 2108 | return(-1); |
| 2109 | } |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2110 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2111 | ctxt.doc = cur; |
| 2112 | ctxt.buf = buf; |
| 2113 | ctxt.level = 0; |
| 2114 | ctxt.format = format; |
| 2115 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2116 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2117 | xmlDocContentDumpOutput(&ctxt, cur); |
| 2118 | ret = xmlOutputBufferClose(buf); |
| 2119 | return (ret); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | /** |
| 2123 | * xmlSaveFormatFileEnc: |
| 2124 | * @filename: the filename or URL to output |
| 2125 | * @cur: the document being saved |
| 2126 | * @encoding: the name of the encoding to use or NULL. |
| 2127 | * @format: should formatting spaces be added. |
| 2128 | * |
| 2129 | * Dump an XML document to a file or an URL. |
| 2130 | * |
| 2131 | * Returns the number of bytes written or -1 in case of error. |
| 2132 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2133 | * or xmlKeepBlanksDefault(0) was called |
| 2134 | */ |
| 2135 | int |
| 2136 | xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur, |
| 2137 | const char * encoding, int format ) { |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2138 | xmlSaveCtxt ctxt; |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2139 | xmlOutputBufferPtr buf; |
| 2140 | xmlCharEncodingHandlerPtr handler = NULL; |
| 2141 | int ret; |
| 2142 | |
| 2143 | if (cur == NULL) |
| 2144 | return(-1); |
| 2145 | |
| 2146 | if (encoding == NULL) |
| 2147 | encoding = (const char *) cur->encoding; |
| 2148 | |
| 2149 | if (encoding != NULL) { |
| 2150 | |
| 2151 | handler = xmlFindCharEncodingHandler(encoding); |
| 2152 | if (handler == NULL) |
| 2153 | return(-1); |
| 2154 | } |
| 2155 | |
| 2156 | #ifdef HAVE_ZLIB_H |
| 2157 | if (cur->compression < 0) cur->compression = xmlGetCompressMode(); |
| 2158 | #endif |
| 2159 | /* |
| 2160 | * save the content to a temp buffer. |
| 2161 | */ |
| 2162 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
| 2163 | if (buf == NULL) return(-1); |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2164 | memset(&ctxt, 0, sizeof(ctxt)); |
| 2165 | ctxt.doc = cur; |
| 2166 | ctxt.buf = buf; |
| 2167 | ctxt.level = 0; |
| 2168 | ctxt.format = format; |
| 2169 | ctxt.encoding = (const xmlChar *) encoding; |
Daniel Veillard | 753086a | 2004-03-28 16:12:44 +0000 | [diff] [blame] | 2170 | xmlSaveCtxtInit(&ctxt); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2171 | |
Daniel Veillard | 32b7cdb | 2004-03-15 13:46:37 +0000 | [diff] [blame] | 2172 | xmlDocContentDumpOutput(&ctxt, cur); |
Daniel Veillard | 1a8741c | 2004-03-04 13:40:59 +0000 | [diff] [blame] | 2173 | |
| 2174 | ret = xmlOutputBufferClose(buf); |
| 2175 | return(ret); |
| 2176 | } |
| 2177 | |
| 2178 | |
| 2179 | /** |
| 2180 | * xmlSaveFileEnc: |
| 2181 | * @filename: the filename (or URL) |
| 2182 | * @cur: the document |
| 2183 | * @encoding: the name of an encoding (or NULL) |
| 2184 | * |
| 2185 | * Dump an XML document, converting it to the given encoding |
| 2186 | * |
| 2187 | * returns: the number of bytes written or -1 in case of failure. |
| 2188 | */ |
| 2189 | int |
| 2190 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 2191 | return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) ); |
| 2192 | } |
| 2193 | |
| 2194 | /** |
| 2195 | * xmlSaveFormatFile: |
| 2196 | * @filename: the filename (or URL) |
| 2197 | * @cur: the document |
| 2198 | * @format: should formatting spaces been added |
| 2199 | * |
| 2200 | * Dump an XML document to a file. Will use compression if |
| 2201 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 2202 | * used. If @format is set then the document will be indented on output. |
| 2203 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 2204 | * or xmlKeepBlanksDefault(0) was called |
| 2205 | * |
| 2206 | * returns: the number of bytes written or -1 in case of failure. |
| 2207 | */ |
| 2208 | int |
| 2209 | xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) { |
| 2210 | return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) ); |
| 2211 | } |
| 2212 | |
| 2213 | /** |
| 2214 | * xmlSaveFile: |
| 2215 | * @filename: the filename (or URL) |
| 2216 | * @cur: the document |
| 2217 | * |
| 2218 | * Dump an XML document to a file. Will use compression if |
| 2219 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 2220 | * used. |
| 2221 | * returns: the number of bytes written or -1 in case of failure. |
| 2222 | */ |
| 2223 | int |
| 2224 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
| 2225 | return(xmlSaveFormatFileEnc(filename, cur, NULL, 0)); |
| 2226 | } |
| 2227 | |
| 2228 | #endif /* LIBXML_OUTPUT_ENABLED */ |
| 2229 | |