forked from GNOME/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmlsave.c
2890 lines (2593 loc) · 81.9 KB
/
xmlsave.c
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.
*
*/
#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/entities.h"
#include "private/error.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 {
void *_private;
int type;
int fd;
const xmlChar *filename;
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 *
* *
************************************************************************/
/**
* xmlSaveErrMemory:
* @extra: extra information
*
* Handle an out of memory condition
*/
static void
xmlSaveErrMemory(xmlOutputBufferPtr out)
{
if (out != NULL)
out->error = XML_ERR_NO_MEMORY;
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_OUTPUT, NULL);
}
/**
* xmlSaveErr:
* @code: the error number
* @node: the location of the error.
* @extra: extra information
*
* Handle an out of memory condition
*/
static void
xmlSaveErr(xmlOutputBufferPtr out, int code, xmlNodePtr node,
const char *extra)
{
const char *msg = NULL;
int res;
/* Don't overwrite memory errors */
if ((out != NULL) && (out->error == XML_ERR_NO_MEMORY))
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);
}
/************************************************************************
* *
* Special escaping routines *
* *
************************************************************************/
/*
* Tables generated with tools/genEscape.py
*/
static const char xmlEscapeContent[] = {
8, '&', '#', 'x', 'F', 'F', 'F', 'D', ';', 4, '&', '#',
'9', ';', 5, '&', '#', '1', '0', ';', 5, '&', '#', '1',
'3', ';', 6, '&', 'q', 'u', 'o', 't', ';', 5, '&', 'a',
'm', 'p', ';', 4, '&', 'l', 't', ';', 4, '&', 'g', 't',
';',
};
static const signed char xmlEscapeTab[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 20, 0, 0,
0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 33, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 39, -1, 44, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
static const signed char xmlEscapeTabAttr[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 0, 20, 0, 0,
0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, 26, -1, -1, -1, 33, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 39, -1, 44, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
static void
xmlSerializeText(xmlOutputBufferPtr buf, const xmlChar *string,
unsigned flags) {
const char *cur;
const signed char *tab;
if (string == NULL)
return;
if (flags & XML_ESCAPE_ATTR)
tab = xmlEscapeTabAttr;
else
tab = xmlEscapeTab;
cur = (const char *) string;
while (*cur != 0) {
const char *base;
int c;
int offset;
base = cur;
offset = -1;
while (1) {
c = (unsigned char) *cur;
if (c < 0x80) {
offset = tab[c];
if (offset >= 0)
break;
} else if (flags & XML_ESCAPE_NON_ASCII) {
break;
}
cur += 1;
}
if (cur > base)
xmlOutputBufferWrite(buf, cur - base, base);
if (offset >= 0) {
if (c == 0)
break;
xmlOutputBufferWrite(buf, xmlEscapeContent[offset],
&xmlEscapeContent[offset+1]);
cur += 1;
} else {
char tempBuf[12];
int tempSize;
int val = 0, len = 4;
val = xmlGetUTF8Char((const xmlChar *) cur, &len);
if (val < 0) {
val = 0xFFFD;
cur += 1;
} else {
if (!IS_CHAR(val))
val = 0xFFFD;
cur += len;
}
tempSize = xmlSerializeHexCharRef(tempBuf, val);
xmlOutputBufferWrite(buf, tempSize, tempBuf);
}
}
}
/************************************************************************
* *
* Allocation and deallocation *
* *
************************************************************************/
/**
* xmlSaveSetIndentString:
* @ctxt: save context
* @indent: indent string
*
* Sets the indent string.
*
* Available since 2.14.0.
*
* Returns 0 on success, -1 if the string is NULL, empty or too long.
*/
int
xmlSaveSetIndentString(xmlSaveCtxtPtr 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);
}
/**
* xmlSaveCtxtInit:
* @ctxt: the saving context
*
* Initialize a saving context
*/
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;
}
/**
* xmlFreeSaveCtxt:
*
* 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);
}
/**
* xmlNewSaveCtxt:
*
* 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) {
int 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, flags);
}
/**
* xmlSaveWriteAttrContent:
* @ctxt: save context
* @attr: the attribute pointer
*
* Serialize the attribute in the buffer
*/
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;
}
}
/**
* xmlBufDumpNotationDecl:
* @buf: the XML buffer output
* @nota: A notation declaration
*
* This will dump the content the notation declaration as an XML DTD definition
*/
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");
}
/**
* xmlBufDumpNotationDeclScan:
* @nota: A notation declaration
* @buf: the XML buffer output
*
* This is called with the hash scan function, and just reverses args
*/
static void
xmlBufDumpNotationDeclScan(void *nota, void *buf,
const xmlChar *name ATTRIBUTE_UNUSED) {
xmlBufDumpNotationDecl((xmlOutputBufferPtr) buf, (xmlNotationPtr) nota);
}
/**
* xmlBufDumpNotationTable:
* @buf: an xmlBufPtr output
* @table: A notation table
*
* This will dump the content of the notation table as an XML DTD definition
*/
static void
xmlBufDumpNotationTable(xmlOutputBufferPtr buf, xmlNotationTablePtr table) {
xmlHashScan(table, xmlBufDumpNotationDeclScan, buf);
}
/**
* xmlBufDumpElementOccur:
* @buf: output buffer
* @cur: element table
*
* Dump the occurrence operator of an element.
*/
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;
}
}
/**
* xmlBufDumpElementContent:
* @buf: output buffer
* @content: element table
*
* This will dump the content of the element table as an XML DTD definition
*/
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);
}
/**
* xmlBufDumpElementDecl:
* @buf: an xmlBufPtr output
* @elem: An element table
*
* This will dump the content of the element declaration as an XML
* DTD definition
*/
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");
}
/**
* xmlBufDumpEnumeration:
* @buf: output buffer
* @enum: An enumeration
*
* This will dump the content of the 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, ")");
}
/**
* xmlBufDumpAttributeDecl:
* @buf: output buffer
* @attr: An attribute declaration
*
* This will dump the content of the attribute declaration as an XML
* DTD definition
*/
static void
xmlBufDumpAttributeDecl(xmlOutputBufferPtr buf, xmlAttributePtr attr) {
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, 1, " ");
xmlOutputBufferWriteQuotedString(buf, attr->defaultValue);
}
xmlOutputBufferWrite(buf, 2, ">\n");
}
/**
* xmlBufDumpEntityContent:
* @buf: output buffer
* @content: entity content.
*
* This will dump the quoted string value, taking care of the special
* treatment required by %
*/
static void
xmlBufDumpEntityContent(xmlOutputBufferPtr buf, const xmlChar *content) {
if (xmlStrchr(content, '%')) {
const char * base, *cur;
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, "\"");
} else {
xmlOutputBufferWriteQuotedString(buf, content);
}
}
/**
* xmlBufDumpEntityDecl:
* @buf: an xmlBufPtr output
* @ent: An entity table
*
* This will dump the content of the entity table as an XML DTD definition
*/
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)) {
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;
if ((encoding != NULL) && (buf->encoder == NULL) && (buf->conv == NULL)) {
xmlCharEncodingHandler *handler;
int res;
res = xmlOpenCharEncodingHandler(encoding, /* output */ 1, &handler);
if (res != XML_ERR_OK) {
xmlSaveErr(buf, res, NULL, encoding);
return(-1);
}
buf->conv = xmlBufCreate(4000 /* MINLEN */);
if (buf->conv == NULL) {
xmlCharEncCloseFunc(handler);
xmlSaveErrMemory(buf);
return(-1);
}
buf->encoder = handler;
/*
* 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);
xmlCharEncCloseFunc(buf->encoder);
xmlBufFree(buf->conv);
buf->encoder = NULL;
buf->conv = NULL;
return(0);
}
#ifdef LIBXML_HTML_ENABLED
static void
xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
#endif
static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
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);
}
/**
* xmlOutputBufferWriteWSNonSig:
* @ctxt: The save context
* @extra: Number of extra indents to apply to ctxt->level
*
* Write out formatting for non-significant whitespace output.
*/
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);
}
}
/**
* xmlNsDumpOutput:
* @buf: the XML buffer output
* @cur: a namespace
* @ctxt: the output save context. Optional.
*
* Dump a local Namespace definition.
* Should be called in the context of attributes dumps.
* If @ctxt is supplied, @buf should be its buffer.
*/
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, escapeFlags);
xmlOutputBufferWrite(buf, 1, "\"");
}
}
/**
* xmlNsListDumpOutputCtxt
* @ctxt: the save context
* @cur: the first namespace
*
* Dump a list of local namespace definitions to a save context.
* Should be called in the context of attribute dumps.
*/
static void
xmlNsListDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
while (cur != NULL) {
xmlNsDumpOutput(ctxt->buf, cur, ctxt);
cur = cur->next;
}
}
/**
* xmlNsListDumpOutput:
* @buf: the XML buffer output
* @cur: the first namespace
*
* Dump a list of local Namespace definitions.
* Should be called in the context of attributes dumps.
*/
void
xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
while (cur != NULL) {
xmlNsDumpOutput(buf, cur, NULL);
cur = cur->next;
}
}
/**
* xmlDtdDumpOutput:
* @buf: the XML buffer output
* @dtd: the pointer to the DTD
*
* Dump the XML document DTD, if any.
*/
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, "]>");
}
/**
* xmlAttrDumpOutput:
* @buf: the XML buffer output
* @cur: the attribute pointer
*
* Dump an XML attribute
*/
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