forked from GNOME/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathxmlsave.c
More file actions
2681 lines (2390 loc) · 76.4 KB
/
xmlsave.c
File metadata and controls
2681 lines (2390 loc) · 76.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* xmlsave.c: Implementation of the document serializer
*
* See Copyright for the status of this software.
*
* Author: Daniel Veillard
*/
#define IN_LIBXML
#include "libxml.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/parserInternals.h>
#include <libxml/tree.h>
#include <libxml/xmlsave.h>
#define MAX_INDENT 60
#include <libxml/HTMLtree.h>
#include "private/buf.h"
#include "private/enc.h"
#include "private/error.h"
#include "private/html.h"
#include "private/io.h"
#include "private/save.h"
#ifdef LIBXML_OUTPUT_ENABLED
#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
struct _xmlSaveCtxt {
const xmlChar *encoding;
xmlCharEncodingHandlerPtr handler;
xmlOutputBufferPtr buf;
int options;
int level;
int format;
char indent[MAX_INDENT + 1]; /* array for indenting output */
int indent_nr;
int indent_size;
xmlCharEncodingOutputFunc escape; /* used for element content */
};
/************************************************************************
* *
* Output error handlers *
* *
************************************************************************/
/**
* Handle an out of memory condition
*
* @param out an output buffer
*/
static void
xmlSaveErrMemory(xmlOutputBufferPtr out)
{
if (out != NULL)
out->error = XML_ERR_NO_MEMORY;
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_OUTPUT, NULL);
}
/**
* Handle an out of memory condition
*
* @param out an output buffer
* @param code the error number
* @param node the location of the error.
* @param extra extra information
*/
static void
xmlSaveErr(xmlOutputBufferPtr out, int code, xmlNodePtr node,
const char *extra)
{
const char *msg = NULL;
int res;
/* Don't overwrite catastrophic errors */
if ((out != NULL) &&
(out->error != XML_ERR_OK) &&
(xmlIsCatastrophicError(XML_ERR_FATAL, out->error)))
return;
if (code == XML_ERR_NO_MEMORY) {
xmlSaveErrMemory(out);
return;
}
if (out != NULL)
out->error = code;
if (code == XML_ERR_UNSUPPORTED_ENCODING) {
msg = "Unsupported encoding: %s";
} else {
msg = xmlErrString(code);
extra = NULL;
}
res = xmlRaiseError(NULL, NULL, NULL, NULL, node,
XML_FROM_OUTPUT, code, XML_ERR_ERROR, NULL, 0,
extra, NULL, NULL, 0, 0,
msg, extra);
if (res < 0)
xmlSaveErrMemory(out);
}
/************************************************************************
* *
* Allocation and deallocation *
* *
************************************************************************/
/**
* Sets the indent string.
*
* @since 2.14.0
*
* @param ctxt save context
* @param indent indent string
* @returns 0 on success, -1 if the string is NULL, empty or too long.
*/
int
xmlSaveSetIndentString(xmlSaveCtxt *ctxt, const char *indent) {
size_t len;
int i;
if ((ctxt == NULL) || (indent == NULL))
return(-1);
len = strlen(indent);
if ((len <= 0) || (len > MAX_INDENT))
return(-1);
ctxt->indent_size = len;
ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
for (i = 0; i < ctxt->indent_nr; i++)
memcpy(&ctxt->indent[i * ctxt->indent_size], indent, len);
return(0);
}
/**
* Initialize a saving context
*
* @param ctxt the saving context
* @param options save options
*/
static void
xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt, int options)
{
if (ctxt == NULL) return;
xmlSaveSetIndentString(ctxt, xmlTreeIndentString);
if (options & XML_SAVE_FORMAT)
ctxt->format = 1;
else if (options & XML_SAVE_WSNONSIG)
ctxt->format = 2;
if (((options & XML_SAVE_EMPTY) == 0) &&
(xmlSaveNoEmptyTags))
options |= XML_SAVE_NO_EMPTY;
ctxt->options = options;
}
/**
* Free a saving context, destroying the output in any remaining buffer
*/
static void
xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
{
if (ctxt == NULL) return;
if (ctxt->encoding != NULL)
xmlFree((char *) ctxt->encoding);
if (ctxt->buf != NULL)
xmlOutputBufferClose(ctxt->buf);
xmlFree(ctxt);
}
/**
* Create a new saving context
*
* @returns the new structure or NULL in case of error
*/
static xmlSaveCtxtPtr
xmlNewSaveCtxt(const char *encoding, int options)
{
xmlSaveCtxtPtr ret;
ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
if (ret == NULL) {
xmlSaveErrMemory(NULL);
return ( NULL );
}
memset(ret, 0, sizeof(xmlSaveCtxt));
if (encoding != NULL) {
xmlParserErrors res;
res = xmlOpenCharEncodingHandler(encoding, /* output */ 1,
&ret->handler);
if (res != XML_ERR_OK) {
xmlSaveErr(NULL, res, NULL, encoding);
xmlFreeSaveCtxt(ret);
return(NULL);
}
ret->encoding = xmlStrdup((const xmlChar *)encoding);
}
xmlSaveCtxtInit(ret, options);
return(ret);
}
/************************************************************************
* *
* Dumping XML tree content to a simple buffer *
* *
************************************************************************/
static void
xmlSaveWriteText(xmlSaveCtxt *ctxt, const xmlChar *text, unsigned flags) {
if (ctxt->encoding == NULL)
flags |= XML_ESCAPE_NON_ASCII;
xmlSerializeText(ctxt->buf, text, SIZE_MAX, flags);
}
/**
* Serialize the attribute in the buffer
*
* @param ctxt save context
* @param attr the attribute pointer
*/
static void
xmlSaveWriteAttrContent(xmlSaveCtxt *ctxt, xmlAttrPtr attr)
{
xmlNodePtr children;
xmlOutputBufferPtr buf = ctxt->buf;
children = attr->children;
while (children != NULL) {
switch (children->type) {
case XML_TEXT_NODE:
xmlSaveWriteText(ctxt, children->content, XML_ESCAPE_ATTR);
break;
case XML_ENTITY_REF_NODE:
xmlOutputBufferWrite(buf, 1, "&");
xmlOutputBufferWriteString(buf, (const char *) children->name);
xmlOutputBufferWrite(buf, 1, ";");
break;
default:
/* should not happen unless we have a badly built tree */
break;
}
children = children->next;
}
}
/**
* This will dump the content the notation declaration as an XML DTD definition
*
* @param buf the XML buffer output
* @param nota A notation declaration
*/
static void
xmlBufDumpNotationDecl(xmlOutputBufferPtr buf, xmlNotationPtr nota) {
xmlOutputBufferWrite(buf, 11, "<!NOTATION ");
xmlOutputBufferWriteString(buf, (const char *) nota->name);
if (nota->PublicID != NULL) {
xmlOutputBufferWrite(buf, 8, " PUBLIC ");
xmlOutputBufferWriteQuotedString(buf, nota->PublicID);
if (nota->SystemID != NULL) {
xmlOutputBufferWrite(buf, 1, " ");
xmlOutputBufferWriteQuotedString(buf, nota->SystemID);
}
} else {
xmlOutputBufferWrite(buf, 8, " SYSTEM ");
xmlOutputBufferWriteQuotedString(buf, nota->SystemID);
}
xmlOutputBufferWrite(buf, 3, " >\n");
}
/**
* This is called with the hash scan function, and just reverses args
*
* @param nota A notation declaration
* @param buf the XML buffer output
* @param name unused
*/
static void
xmlBufDumpNotationDeclScan(void *nota, void *buf,
const xmlChar *name ATTRIBUTE_UNUSED) {
xmlBufDumpNotationDecl((xmlOutputBufferPtr) buf, (xmlNotationPtr) nota);
}
/**
* This will dump the content of the notation table as an XML DTD definition
*
* @param buf an xmlBuf output
* @param table A notation table
*/
static void
xmlBufDumpNotationTable(xmlOutputBufferPtr buf, xmlNotationTablePtr table) {
xmlHashScan(table, xmlBufDumpNotationDeclScan, buf);
}
/**
* Dump the occurrence operator of an element.
*
* @param buf output buffer
* @param cur element table
*/
static void
xmlBufDumpElementOccur(xmlOutputBufferPtr buf, xmlElementContentPtr cur) {
switch (cur->ocur) {
case XML_ELEMENT_CONTENT_ONCE:
break;
case XML_ELEMENT_CONTENT_OPT:
xmlOutputBufferWrite(buf, 1, "?");
break;
case XML_ELEMENT_CONTENT_MULT:
xmlOutputBufferWrite(buf, 1, "*");
break;
case XML_ELEMENT_CONTENT_PLUS:
xmlOutputBufferWrite(buf, 1, "+");
break;
}
}
/**
* This will dump the content of the element table as an XML DTD definition
*
* @param buf output buffer
* @param content element table
*/
static void
xmlBufDumpElementContent(xmlOutputBufferPtr buf,
xmlElementContentPtr content) {
xmlElementContentPtr cur;
if (content == NULL) return;
xmlOutputBufferWrite(buf, 1, "(");
cur = content;
do {
if (cur == NULL) return;
switch (cur->type) {
case XML_ELEMENT_CONTENT_PCDATA:
xmlOutputBufferWrite(buf, 7, "#PCDATA");
break;
case XML_ELEMENT_CONTENT_ELEMENT:
if (cur->prefix != NULL) {
xmlOutputBufferWriteString(buf,
(const char *) cur->prefix);
xmlOutputBufferWrite(buf, 1, ":");
}
xmlOutputBufferWriteString(buf, (const char *) cur->name);
break;
case XML_ELEMENT_CONTENT_SEQ:
case XML_ELEMENT_CONTENT_OR:
if ((cur != content) &&
(cur->parent != NULL) &&
((cur->type != cur->parent->type) ||
(cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
xmlOutputBufferWrite(buf, 1, "(");
cur = cur->c1;
continue;
}
while (cur != content) {
xmlElementContentPtr parent = cur->parent;
if (parent == NULL) return;
if (((cur->type == XML_ELEMENT_CONTENT_OR) ||
(cur->type == XML_ELEMENT_CONTENT_SEQ)) &&
((cur->type != parent->type) ||
(cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
xmlOutputBufferWrite(buf, 1, ")");
xmlBufDumpElementOccur(buf, cur);
if (cur == parent->c1) {
if (parent->type == XML_ELEMENT_CONTENT_SEQ)
xmlOutputBufferWrite(buf, 3, " , ");
else if (parent->type == XML_ELEMENT_CONTENT_OR)
xmlOutputBufferWrite(buf, 3, " | ");
cur = parent->c2;
break;
}
cur = parent;
}
} while (cur != content);
xmlOutputBufferWrite(buf, 1, ")");
xmlBufDumpElementOccur(buf, content);
}
/**
* This will dump the content of the element declaration as an XML
* DTD definition
*
* @param buf an xmlBuf output
* @param elem An element table
*/
static void
xmlBufDumpElementDecl(xmlOutputBufferPtr buf, xmlElementPtr elem) {
xmlOutputBufferWrite(buf, 10, "<!ELEMENT ");
if (elem->prefix != NULL) {
xmlOutputBufferWriteString(buf, (const char *) elem->prefix);
xmlOutputBufferWrite(buf, 1, ":");
}
xmlOutputBufferWriteString(buf, (const char *) elem->name);
xmlOutputBufferWrite(buf, 1, " ");
switch (elem->etype) {
case XML_ELEMENT_TYPE_EMPTY:
xmlOutputBufferWrite(buf, 5, "EMPTY");
break;
case XML_ELEMENT_TYPE_ANY:
xmlOutputBufferWrite(buf, 3, "ANY");
break;
case XML_ELEMENT_TYPE_MIXED:
case XML_ELEMENT_TYPE_ELEMENT:
xmlBufDumpElementContent(buf, elem->content);
break;
default:
/* assert(0); */
break;
}
xmlOutputBufferWrite(buf, 2, ">\n");
}
/**
* This will dump the content of the enumeration
*
* @param buf output buffer
* @param cur an enumeration
*/
static void
xmlBufDumpEnumeration(xmlOutputBufferPtr buf, xmlEnumerationPtr cur) {
while (cur != NULL) {
xmlOutputBufferWriteString(buf, (const char *) cur->name);
if (cur->next != NULL)
xmlOutputBufferWrite(buf, 3, " | ");
cur = cur->next;
}
xmlOutputBufferWrite(buf, 1, ")");
}
/**
* This will dump the content of the attribute declaration as an XML
* DTD definition
*
* @param ctxt save context
* @param attr an attribute declaration
*/
static void
xmlSaveWriteAttributeDecl(xmlSaveCtxtPtr ctxt, xmlAttributePtr attr) {
xmlOutputBufferPtr buf = ctxt->buf;
xmlOutputBufferWrite(buf, 10, "<!ATTLIST ");
xmlOutputBufferWriteString(buf, (const char *) attr->elem);
xmlOutputBufferWrite(buf, 1, " ");
if (attr->prefix != NULL) {
xmlOutputBufferWriteString(buf, (const char *) attr->prefix);
xmlOutputBufferWrite(buf, 1, ":");
}
xmlOutputBufferWriteString(buf, (const char *) attr->name);
switch (attr->atype) {
case XML_ATTRIBUTE_CDATA:
xmlOutputBufferWrite(buf, 6, " CDATA");
break;
case XML_ATTRIBUTE_ID:
xmlOutputBufferWrite(buf, 3, " ID");
break;
case XML_ATTRIBUTE_IDREF:
xmlOutputBufferWrite(buf, 6, " IDREF");
break;
case XML_ATTRIBUTE_IDREFS:
xmlOutputBufferWrite(buf, 7, " IDREFS");
break;
case XML_ATTRIBUTE_ENTITY:
xmlOutputBufferWrite(buf, 7, " ENTITY");
break;
case XML_ATTRIBUTE_ENTITIES:
xmlOutputBufferWrite(buf, 9, " ENTITIES");
break;
case XML_ATTRIBUTE_NMTOKEN:
xmlOutputBufferWrite(buf, 8, " NMTOKEN");
break;
case XML_ATTRIBUTE_NMTOKENS:
xmlOutputBufferWrite(buf, 9, " NMTOKENS");
break;
case XML_ATTRIBUTE_ENUMERATION:
xmlOutputBufferWrite(buf, 2, " (");
xmlBufDumpEnumeration(buf, attr->tree);
break;
case XML_ATTRIBUTE_NOTATION:
xmlOutputBufferWrite(buf, 11, " NOTATION (");
xmlBufDumpEnumeration(buf, attr->tree);
break;
default:
/* assert(0); */
break;
}
switch (attr->def) {
case XML_ATTRIBUTE_NONE:
break;
case XML_ATTRIBUTE_REQUIRED:
xmlOutputBufferWrite(buf, 10, " #REQUIRED");
break;
case XML_ATTRIBUTE_IMPLIED:
xmlOutputBufferWrite(buf, 9, " #IMPLIED");
break;
case XML_ATTRIBUTE_FIXED:
xmlOutputBufferWrite(buf, 7, " #FIXED");
break;
default:
/* assert(0); */
break;
}
if (attr->defaultValue != NULL) {
xmlOutputBufferWrite(buf, 2, " \"");
xmlSaveWriteText(ctxt, attr->defaultValue, XML_ESCAPE_ATTR);
xmlOutputBufferWrite(buf, 1, "\"");
}
xmlOutputBufferWrite(buf, 2, ">\n");
}
/**
* This will dump the quoted string value, taking care of the special
* treatment required by %
*
* @param buf output buffer
* @param content entity content.
*/
static void
xmlBufDumpEntityContent(xmlOutputBufferPtr buf, const xmlChar *content) {
const char * base, *cur;
if (content == NULL)
return;
xmlOutputBufferWrite(buf, 1, "\"");
base = cur = (const char *) content;
while (*cur != 0) {
if (*cur == '"') {
if (base != cur)
xmlOutputBufferWrite(buf, cur - base, base);
xmlOutputBufferWrite(buf, 6, """);
cur++;
base = cur;
} else if (*cur == '%') {
if (base != cur)
xmlOutputBufferWrite(buf, cur - base, base);
xmlOutputBufferWrite(buf, 6, "%");
cur++;
base = cur;
} else {
cur++;
}
}
if (base != cur)
xmlOutputBufferWrite(buf, cur - base, base);
xmlOutputBufferWrite(buf, 1, "\"");
}
/**
* This will dump the content of the entity table as an XML DTD definition
*
* @param buf an xmlBuf output
* @param ent An entity table
*/
static void
xmlBufDumpEntityDecl(xmlOutputBufferPtr buf, xmlEntityPtr ent) {
if ((ent->etype == XML_INTERNAL_PARAMETER_ENTITY) ||
(ent->etype == XML_EXTERNAL_PARAMETER_ENTITY))
xmlOutputBufferWrite(buf, 11, "<!ENTITY % ");
else
xmlOutputBufferWrite(buf, 9, "<!ENTITY ");
xmlOutputBufferWriteString(buf, (const char *) ent->name);
xmlOutputBufferWrite(buf, 1, " ");
if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) ||
(ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) ||
(ent->etype == XML_EXTERNAL_PARAMETER_ENTITY)) {
if (ent->ExternalID != NULL) {
xmlOutputBufferWrite(buf, 7, "PUBLIC ");
xmlOutputBufferWriteQuotedString(buf, ent->ExternalID);
xmlOutputBufferWrite(buf, 1, " ");
} else {
xmlOutputBufferWrite(buf, 7, "SYSTEM ");
}
xmlOutputBufferWriteQuotedString(buf, ent->SystemID);
}
if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
if (ent->content != NULL) { /* Should be true ! */
xmlOutputBufferWrite(buf, 7, " NDATA ");
if (ent->orig != NULL)
xmlOutputBufferWriteString(buf, (const char *) ent->orig);
else
xmlOutputBufferWriteString(buf, (const char *) ent->content);
}
}
if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
(ent->etype == XML_INTERNAL_PARAMETER_ENTITY)) {
/*
* We could save the original quote character and avoid
* calling xmlOutputBufferWriteQuotedString here.
*/
if (ent->orig != NULL)
xmlOutputBufferWriteQuotedString(buf, ent->orig);
else
xmlBufDumpEntityContent(buf, ent->content);
}
xmlOutputBufferWrite(buf, 2, ">\n");
}
/************************************************************************
* *
* Dumping XML tree content to an I/O output buffer *
* *
************************************************************************/
static int
xmlSaveSwitchEncoding(xmlSaveCtxtPtr ctxt, const char *encoding) {
xmlOutputBufferPtr buf = ctxt->buf;
xmlCharEncodingHandler *handler;
xmlParserErrors res;
/* shouldn't happen */
if ((buf->encoder != NULL) || (buf->conv != NULL))
return(-1);
res = xmlOpenCharEncodingHandler(encoding, /* output */ 1, &handler);
if (res != XML_ERR_OK) {
xmlSaveErr(buf, res, NULL, encoding);
return(-1);
}
if (handler != NULL) {
xmlBufPtr newbuf;
newbuf = xmlBufCreate(4000 /* MINLEN */);
if (newbuf == NULL) {
xmlCharEncCloseFunc(handler);
xmlSaveErrMemory(buf);
return(-1);
}
buf->conv = buf->buffer;
buf->buffer = newbuf;
buf->encoder = handler;
}
ctxt->encoding = (const xmlChar *) encoding;
/*
* initialize the state, e.g. if outputting a BOM
*/
xmlCharEncOutput(buf, 1);
return(0);
}
static int
xmlSaveClearEncoding(xmlSaveCtxtPtr ctxt) {
xmlOutputBufferPtr buf = ctxt->buf;
xmlOutputBufferFlush(buf);
if (buf->encoder != NULL) {
xmlCharEncCloseFunc(buf->encoder);
buf->encoder = NULL;
xmlBufFree(buf->buffer);
buf->buffer = buf->conv;
buf->conv = NULL;
}
ctxt->encoding = NULL;
return(0);
}
#ifdef LIBXML_HTML_ENABLED
static void
xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
#endif
static void
xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
static int
xmlSaveDocInternal(xmlSaveCtxtPtr ctxt, xmlDocPtr cur,
const xmlChar *encoding);
static void
xmlSaveWriteIndent(xmlSaveCtxtPtr ctxt, int extra)
{
int level;
if ((ctxt->options & XML_SAVE_NO_INDENT) ||
(((ctxt->options & XML_SAVE_INDENT) == 0) &&
(xmlIndentTreeOutput == 0)))
return;
level = ctxt->level + extra;
if (level > ctxt->indent_nr)
level = ctxt->indent_nr;
xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size * level, ctxt->indent);
}
/**
* Write out formatting for non-significant whitespace output.
*
* @param ctxt The save context
* @param extra Number of extra indents to apply to ctxt->level
*/
static void
xmlOutputBufferWriteWSNonSig(xmlSaveCtxtPtr ctxt, int extra)
{
int i;
if ((ctxt == NULL) || (ctxt->buf == NULL))
return;
xmlOutputBufferWrite(ctxt->buf, 1, "\n");
for (i = 0; i < (ctxt->level + extra); i += ctxt->indent_nr) {
xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size *
((ctxt->level + extra - i) > ctxt->indent_nr ?
ctxt->indent_nr : (ctxt->level + extra - i)),
ctxt->indent);
}
}
/**
* Dump a local Namespace definition.
* Should be called in the context of attributes dumps.
* If `ctxt` is supplied, `buf` should be its buffer.
*
* @param buf the XML buffer output
* @param cur a namespace
* @param ctxt the output save context. Optional.
*/
static void
xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur, xmlSaveCtxtPtr ctxt) {
unsigned escapeFlags = XML_ESCAPE_ATTR;
if ((cur == NULL) || (buf == NULL)) return;
if ((ctxt == NULL) || (ctxt->encoding == NULL))
escapeFlags |= XML_ESCAPE_NON_ASCII;
if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
return;
if (ctxt != NULL && ctxt->format == 2)
xmlOutputBufferWriteWSNonSig(ctxt, 2);
else
xmlOutputBufferWrite(buf, 1, " ");
/* Within the context of an element attributes */
if (cur->prefix != NULL) {
xmlOutputBufferWrite(buf, 6, "xmlns:");
xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
} else
xmlOutputBufferWrite(buf, 5, "xmlns");
xmlOutputBufferWrite(buf, 2, "=\"");
xmlSerializeText(buf, cur->href, SIZE_MAX, escapeFlags);
xmlOutputBufferWrite(buf, 1, "\"");
}
}
/**
* Dump a list of local namespace definitions to a save context.
* Should be called in the context of attribute dumps.
*
* @param ctxt the save context
* @param cur the first namespace
*/
static void
xmlNsListDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
while (cur != NULL) {
xmlNsDumpOutput(ctxt->buf, cur, ctxt);
cur = cur->next;
}
}
/**
* Serialize a list of namespace definitions.
*
* @param buf the XML buffer output
* @param cur the first namespace
*/
void
xmlNsListDumpOutput(xmlOutputBuffer *buf, xmlNs *cur) {
while (cur != NULL) {
xmlNsDumpOutput(buf, cur, NULL);
cur = cur->next;
}
}
/**
* Dump the XML document DTD, if any.
*
* @param ctxt the save context
* @param dtd the pointer to the DTD
*/
static void
xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
xmlOutputBufferPtr buf;
xmlNodePtr cur;
int format, level;
if (dtd == NULL) return;
if ((ctxt == NULL) || (ctxt->buf == NULL))
return;
buf = ctxt->buf;
xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
xmlOutputBufferWriteString(buf, (const char *)dtd->name);
if (dtd->ExternalID != NULL) {
xmlOutputBufferWrite(buf, 8, " PUBLIC ");
xmlOutputBufferWriteQuotedString(buf, dtd->ExternalID);
xmlOutputBufferWrite(buf, 1, " ");
xmlOutputBufferWriteQuotedString(buf, dtd->SystemID);
} else if (dtd->SystemID != NULL) {
xmlOutputBufferWrite(buf, 8, " SYSTEM ");
xmlOutputBufferWriteQuotedString(buf, dtd->SystemID);
}
if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
(dtd->attributes == NULL) && (dtd->notations == NULL) &&
(dtd->pentities == NULL)) {
xmlOutputBufferWrite(buf, 1, ">");
return;
}
xmlOutputBufferWrite(buf, 3, " [\n");
/*
* Dump the notations first they are not in the DTD children list
* Do this only on a standalone DTD or on the internal subset though.
*/
if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
(dtd->doc->intSubset == dtd))) {
xmlBufDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
}
format = ctxt->format;
level = ctxt->level;
ctxt->format = 0;
ctxt->level = -1;
for (cur = dtd->children; cur != NULL; cur = cur->next) {
xmlNodeDumpOutputInternal(ctxt, cur);
}
ctxt->format = format;
ctxt->level = level;
xmlOutputBufferWrite(buf, 2, "]>");
}
/**
* Dump an XML attribute
*
* @param ctxt the save context
* @param cur the attribute pointer
*/
static void
xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
xmlOutputBufferPtr buf;
if (cur == NULL) return;
buf = ctxt->buf;
if (buf == NULL) return;
if (ctxt->format == 2)
xmlOutputBufferWriteWSNonSig(ctxt, 2);
else
xmlOutputBufferWrite(buf, 1, " ");
if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
xmlOutputBufferWrite(buf, 1, ":");
}
xmlOutputBufferWriteString(buf, (const char *)cur->name);
xmlOutputBufferWrite(buf, 2, "=\"");
#ifdef LIBXML_HTML_ENABLED
if ((ctxt->options & XML_SAVE_XHTML) &&
(cur->ns == NULL) &&
((cur->children == NULL) ||
(cur->children->content == NULL) ||
(cur->children->content[0] == 0)) &&
(htmlIsBooleanAttr(cur->name))) {
xmlOutputBufferWriteString(buf, (const char *) cur->name);
} else
#endif
{
xmlSaveWriteAttrContent(ctxt, cur);
}
xmlOutputBufferWrite(buf, 1, "\"");
}
#ifdef LIBXML_HTML_ENABLED
/**
* Dump an HTML node, recursive behaviour, children are printed too.
*
* @param ctxt the save context
* @param cur the current node
*/
static int
htmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
int switched_encoding = 0;
int format = 0;
xmlInitParser();
if (ctxt->encoding == NULL) {
const char *encoding = NULL;
if (cur->doc != NULL)
encoding = (char *) cur->doc->encoding;
if (encoding == NULL)
encoding = "HTML";
if (xmlSaveSwitchEncoding(ctxt, encoding) < 0)
return(-1);
switched_encoding = 1;
}
if (ctxt->options & XML_SAVE_FORMAT)
format = 1;
htmlNodeDumpInternal(ctxt->buf, cur, (char *) ctxt->encoding, format);
if (switched_encoding) {
xmlSaveClearEncoding(ctxt);
}
return(0);
}
#endif
/**
* Dump an XML node, recursive behaviour, children are printed too.
*
* @param ctxt the save context
* @param cur the current node
*/
static void
xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
int format = ctxt->format;
xmlNodePtr tmp, root, unformattedNode = NULL, parent;
xmlAttrPtr attr;
xmlChar *start, *end;
xmlOutputBufferPtr buf;
if (cur == NULL) return;
buf = ctxt->buf;
root = cur;
parent = cur->parent;
while (1) {
switch (cur->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
xmlSaveDocInternal(ctxt, (xmlDocPtr) cur, ctxt->encoding);
break;
case XML_DTD_NODE:
xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
break;
case XML_DOCUMENT_FRAG_NODE:
/* Always validate cur->parent when descending. */
if ((cur->parent == parent) && (cur->children != NULL)) {
parent = cur;
cur = cur->children;
continue;
}
break;
case XML_ELEMENT_DECL:
xmlBufDumpElementDecl(buf, (xmlElementPtr) cur);
break;
case XML_ATTRIBUTE_DECL:
xmlSaveWriteAttributeDecl(ctxt, (xmlAttributePtr) cur);
break;