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