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