-
Notifications
You must be signed in to change notification settings - Fork 25
/
xmlsave.c
3005 lines (2740 loc) · 86.5 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/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 */
xmlCharEncodingOutputFunc escapeAttr;/* used for attribute 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;
switch(code) {
case XML_SAVE_NOT_UTF8:
msg = "string is not in UTF-8\n";
break;
case XML_SAVE_CHAR_INVALID:
msg = "invalid character value\n";
break;
case XML_SAVE_UNKNOWN_ENCODING:
msg = "unknown encoding %s\n";
break;
case XML_SAVE_NO_DOCTYPE:
msg = "document has no DOCTYPE\n";
break;
default:
msg = "unexpected error number\n";
}
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 *
* *
************************************************************************/
static unsigned char *
xmlSerializeHexCharRef(unsigned char *out, int val) {
unsigned char *ptr;
*out++ = '&';
*out++ = '#';
*out++ = 'x';
if (val < 0x10) ptr = out;
else if (val < 0x100) ptr = out + 1;
else if (val < 0x1000) ptr = out + 2;
else if (val < 0x10000) ptr = out + 3;
else if (val < 0x100000) ptr = out + 4;
else ptr = out + 5;
out = ptr + 1;
while (val > 0) {
switch (val & 0xF) {
case 0: *ptr-- = '0'; break;
case 1: *ptr-- = '1'; break;
case 2: *ptr-- = '2'; break;
case 3: *ptr-- = '3'; break;
case 4: *ptr-- = '4'; break;
case 5: *ptr-- = '5'; break;
case 6: *ptr-- = '6'; break;
case 7: *ptr-- = '7'; break;
case 8: *ptr-- = '8'; break;
case 9: *ptr-- = '9'; break;
case 0xA: *ptr-- = 'A'; break;
case 0xB: *ptr-- = 'B'; break;
case 0xC: *ptr-- = 'C'; break;
case 0xD: *ptr-- = 'D'; break;
case 0xE: *ptr-- = 'E'; break;
case 0xF: *ptr-- = 'F'; break;
default: *ptr-- = '0'; break;
}
val >>= 4;
}
*out++ = ';';
*out = 0;
return(out);
}
/**
* xmlEscapeEntities:
* @out: a pointer to an array of bytes to store the result
* @outlen: the length of @out
* @in: a pointer to an array of unescaped UTF-8 bytes
* @inlen: the length of @in
*
* Take a block of UTF-8 chars in and escape them. Used when there is no
* encoding specified.
*
* Returns 0 if success, or -1 otherwise
* The value of @inlen after return is the number of octets consumed
* if the return value is positive, else unpredictable.
* The value of @outlen after return is the number of octets consumed.
*/
static int
xmlEscapeEntities(unsigned char* out, int *outlen,
const xmlChar* in, int *inlen) {
unsigned char* outstart = out;
const unsigned char* base = in;
unsigned char* outend = out + *outlen;
const unsigned char* inend;
int val;
inend = in + (*inlen);
while ((in < inend) && (out < outend)) {
if (*in == '<') {
if (outend - out < 4) break;
*out++ = '&';
*out++ = 'l';
*out++ = 't';
*out++ = ';';
in++;
continue;
} else if (*in == '>') {
if (outend - out < 4) break;
*out++ = '&';
*out++ = 'g';
*out++ = 't';
*out++ = ';';
in++;
continue;
} else if (*in == '&') {
if (outend - out < 5) break;
*out++ = '&';
*out++ = 'a';
*out++ = 'm';
*out++ = 'p';
*out++ = ';';
in++;
continue;
} else if (*in == 0xD) {
if (outend - out < 5) break;
*out++ = '&';
*out++ = '#';
*out++ = 'x';
*out++ = 'D';
*out++ = ';';
in++;
} else if (((*in >= 0x20) && (*in < 0x80)) ||
(*in == 0xA) || (*in == 0x9)) {
/*
* default case, just copy !
*/
*out++ = *in++;
continue;
} else if (*in < 0x80) {
/* invalid control char */
if (outend - out < 8) break;
out = xmlSerializeHexCharRef(out, 0xFFFD);
in++;
} else {
int len;
if (outend - out < 11) break;
len = inend - in;
val = xmlGetUTF8Char(in, &len);
if (val < 0) {
val = 0xFFFD;
in++;
} else {
if (!IS_CHAR(val))
val = 0xFFFD;
in += len;
}
/*
* We could do multiple things here. Just save as a char ref
*/
out = xmlSerializeHexCharRef(out, val);
}
}
*outlen = out - outstart;
*inlen = in - base;
return(0);
}
/************************************************************************
* *
* Allocation and deallocation *
* *
************************************************************************/
/**
* xmlSaveCtxtInit:
* @ctxt: the saving context
*
* Initialize a saving context
*/
static void
xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
{
int i;
int len;
if (ctxt == NULL) return;
if ((ctxt->encoding == NULL) && (ctxt->escape == NULL))
ctxt->escape = xmlEscapeEntities;
len = xmlStrlen((xmlChar *)xmlTreeIndentString);
if ((xmlTreeIndentString == NULL) || (len == 0)) {
memset(&ctxt->indent[0], 0, MAX_INDENT + 1);
} else {
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], xmlTreeIndentString,
ctxt->indent_size);
ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0;
}
if (xmlSaveNoEmptyTags) {
ctxt->options |= XML_SAVE_NO_EMPTY;
}
}
/**
* 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);
ret->escape = NULL;
}
xmlSaveCtxtInit(ret);
/*
* Use the options
*/
/* Re-check this option as it may already have been set */
if ((ret->options & XML_SAVE_NO_EMPTY) && ! (options & XML_SAVE_NO_EMPTY)) {
options |= XML_SAVE_NO_EMPTY;
}
ret->options = options;
if (options & XML_SAVE_FORMAT)
ret->format = 1;
else if (options & XML_SAVE_WSNONSIG)
ret->format = 2;
return(ret);
}
/************************************************************************
* *
* Dumping XML tree content to a simple buffer *
* *
************************************************************************/
/**
* xmlAttrSerializeContent:
* @buf: the XML buffer output
* @doc: the document
* @attr: the attribute pointer
*
* Serialize the attribute in the buffer
*/
static void
xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr)
{
xmlNodePtr children;
children = attr->children;
while (children != NULL) {
switch (children->type) {
case XML_TEXT_NODE:
xmlBufAttrSerializeTxtContent(buf, attr->doc,
children->content);
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();
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);
/**
* 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, xmlDocPtr doc, xmlNsPtr cur,
xmlSaveCtxtPtr ctxt) {
if ((cur == NULL) || (buf == NULL)) return;
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, "=\"");
xmlBufAttrSerializeTxtContent(buf, doc, cur->href);
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, xmlDocPtr doc, xmlNsPtr cur) {
while (cur != NULL) {
xmlNsDumpOutput(ctxt->buf, doc, 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, NULL, 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