forked from kiyolee/libxml2-win-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlsave.c
2780 lines (2590 loc) · 75.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: Implemetation of the document serializer
*
* See Copyright for the status of this software.
*
* daniel@veillard.com
*/
#define IN_LIBXML
#include "libxml.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 "buf.h"
#include "enc.h"
#include "save.h"
/************************************************************************
* *
* XHTML detection *
* *
************************************************************************/
#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
"-//W3C//DTD XHTML 1.0 Strict//EN"
#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
"-//W3C//DTD XHTML 1.0 Frameset//EN"
#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
"-//W3C//DTD XHTML 1.0 Transitional//EN"
#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
/**
* xmlIsXHTML:
* @systemID: the system identifier
* @publicID: the public identifier
*
* Try to find if the document correspond to an XHTML DTD
*
* Returns 1 if true, 0 if not and -1 in case of error
*/
int
xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
if ((systemID == NULL) && (publicID == NULL))
return(-1);
if (publicID != NULL) {
if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
}
if (systemID != NULL) {
if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
}
return(0);
}
#ifdef LIBXML_OUTPUT_ENABLED
#define TODO \
xmlGenericError(xmlGenericErrorContext, \
"Unimplemented block at %s:%d\n", \
__FILE__, __LINE__);
struct _xmlSaveCtxt {
void *_private;
int type;
int fd;
const xmlChar *filename;
const xmlChar *encoding;
xmlCharEncodingHandlerPtr handler;
xmlOutputBufferPtr buf;
xmlDocPtr doc;
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 informations
*
* Handle an out of memory condition
*/
static void
xmlSaveErrMemory(const char *extra)
{
__xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
}
/**
* xmlSaveErr:
* @code: the error number
* @node: the location of the error.
* @extra: extra informations
*
* Handle an out of memory condition
*/
static void
xmlSaveErr(int code, xmlNodePtr node, const char *extra)
{
const char *msg = NULL;
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";
}
__xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
}
/************************************************************************
* *
* 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 >= 0x20) && (*in < 0x80)) ||
(*in == '\n') || (*in == '\t')) {
/*
* default case, just copy !
*/
*out++ = *in++;
continue;
} else if (*in >= 0x80) {
/*
* We assume we have UTF-8 input.
*/
if (outend - out < 11) break;
if (*in < 0xC0) {
xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL);
in++;
goto error;
} else if (*in < 0xE0) {
if (inend - in < 2) break;
val = (in[0]) & 0x1F;
val <<= 6;
val |= (in[1]) & 0x3F;
in += 2;
} else if (*in < 0xF0) {
if (inend - in < 3) break;
val = (in[0]) & 0x0F;
val <<= 6;
val |= (in[1]) & 0x3F;
val <<= 6;
val |= (in[2]) & 0x3F;
in += 3;
} else if (*in < 0xF8) {
if (inend - in < 4) break;
val = (in[0]) & 0x07;
val <<= 6;
val |= (in[1]) & 0x3F;
val <<= 6;
val |= (in[2]) & 0x3F;
val <<= 6;
val |= (in[3]) & 0x3F;
in += 4;
} else {
xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
in++;
goto error;
}
if (!IS_CHAR(val)) {
xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
in++;
goto error;
}
/*
* We could do multiple things here. Just save as a char ref
*/
out = xmlSerializeHexCharRef(out, val);
} else if (IS_BYTE_CHAR(*in)) {
if (outend - out < 6) break;
out = xmlSerializeHexCharRef(out, *in++);
} else {
xmlGenericError(xmlGenericErrorContext,
"xmlEscapeEntities : char out of range\n");
in++;
goto error;
}
}
*outlen = out - outstart;
*inlen = in - base;
return(0);
error:
*outlen = out - outstart;
*inlen = in - base;
return(-1);
}
/************************************************************************
* *
* 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 ouptut 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("creating saving context");
return ( NULL );
}
memset(ret, 0, sizeof(xmlSaveCtxt));
if (encoding != NULL) {
ret->handler = xmlFindCharEncodingHandler(encoding);
if (ret->handler == NULL) {
xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, 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->buffer, attr->doc,
attr, children->content);
break;
case XML_ENTITY_REF_NODE:
xmlBufAdd(buf->buffer, BAD_CAST "&", 1);
xmlBufAdd(buf->buffer, children->name,
xmlStrlen(children->name));
xmlBufAdd(buf->buffer, BAD_CAST ";", 1);
break;
default:
/* should not happen unless we have a badly built tree */
break;
}
children = children->next;
}
}
/**
* xmlBufDumpNotationTable:
* @buf: an xmlBufPtr output
* @table: A notation table
*
* This will dump the content of the notation table as an XML DTD definition
*/
void
xmlBufDumpNotationTable(xmlBufPtr buf, xmlNotationTablePtr table) {
xmlBufferPtr buffer;
buffer = xmlBufferCreate();
if (buffer == NULL) {
/*
* TODO set the error in buf
*/
return;
}
xmlDumpNotationTable(buffer, table);
xmlBufMergeBuffer(buf, buffer);
}
/**
* xmlBufDumpElementDecl:
* @buf: an xmlBufPtr output
* @elem: An element table
*
* This will dump the content of the element declaration as an XML
* DTD definition
*/
void
xmlBufDumpElementDecl(xmlBufPtr buf, xmlElementPtr elem) {
xmlBufferPtr buffer;
buffer = xmlBufferCreate();
if (buffer == NULL) {
/*
* TODO set the error in buf
*/
return;
}
xmlDumpElementDecl(buffer, elem);
xmlBufMergeBuffer(buf, buffer);
}
/**
* xmlBufDumpAttributeDecl:
* @buf: an xmlBufPtr output
* @attr: An attribute declaration
*
* This will dump the content of the attribute declaration as an XML
* DTD definition
*/
void
xmlBufDumpAttributeDecl(xmlBufPtr buf, xmlAttributePtr attr) {
xmlBufferPtr buffer;
buffer = xmlBufferCreate();
if (buffer == NULL) {
/*
* TODO set the error in buf
*/
return;
}
xmlDumpAttributeDecl(buffer, attr);
xmlBufMergeBuffer(buf, buffer);
}
/**
* xmlBufDumpEntityDecl:
* @buf: an xmlBufPtr output
* @ent: An entity table
*
* This will dump the content of the entity table as an XML DTD definition
*/
void
xmlBufDumpEntityDecl(xmlBufPtr buf, xmlEntityPtr ent) {
xmlBufferPtr buffer;
buffer = xmlBufferCreate();
if (buffer == NULL) {
/*
* TODO set the error in buf
*/
return;
}
xmlDumpEntityDecl(buffer, ent);
xmlBufMergeBuffer(buf, buffer);
}
/************************************************************************
* *
* 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)) {
buf->encoder = xmlFindCharEncodingHandler((const char *)encoding);
if (buf->encoder == NULL) {
xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL,
(const char *)encoding);
return(-1);
}
buf->conv = xmlBufCreate();
if (buf->conv == NULL) {
xmlCharEncCloseFunc(buf->encoder);
xmlSaveErrMemory("creating encoding buffer");
return(-1);
}
/*
* 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 xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr 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, 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, 1, "=");
xmlBufWriteQuotedString(buf->buffer, cur->href);
}
}
/**
* xmlNsDumpOutputCtxt
* @ctxt: the save context
* @cur: a namespace
*
* Dump a local Namespace definition to a save context.
* Should be called in the context of attribute dumps.
*/
static void
xmlNsDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
xmlNsDumpOutput(ctxt->buf, cur, ctxt);
}
/**
* 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;
int format, level;
xmlDocPtr doc;
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 ");
xmlBufWriteQuotedString(buf->buffer, dtd->ExternalID);
xmlOutputBufferWrite(buf, 1, " ");
xmlBufWriteQuotedString(buf->buffer, dtd->SystemID);
} else if (dtd->SystemID != NULL) {
xmlOutputBufferWrite(buf, 8, " SYSTEM ");
xmlBufWriteQuotedString(buf->buffer, 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->buffer,
(xmlNotationTablePtr) dtd->notations);
}
format = ctxt->format;
level = ctxt->level;
doc = ctxt->doc;
ctxt->format = 0;
ctxt->level = -1;
ctxt->doc = dtd->doc;
xmlNodeListDumpOutput(ctxt, dtd->children);
ctxt->format = format;
ctxt->level = level;
ctxt->doc = doc;
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, "=\"");
xmlAttrSerializeContent(buf, cur);
xmlOutputBufferWrite(buf, 1, "\"");
}
/**
* xmlAttrListDumpOutput:
* @buf: the XML buffer output
* @doc: the document
* @cur: the first attribute pointer
* @encoding: an optional encoding string
*
* Dump a list of XML attributes
*/
static void
xmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
if (cur == NULL) return;
while (cur != NULL) {
xmlAttrDumpOutput(ctxt, cur);
cur = cur->next;
}
}
/**
* xmlNodeListDumpOutput:
* @cur: the first node
*
* Dump an XML node list, recursive behaviour, children are printed too.
*/
static void
xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
xmlOutputBufferPtr buf;
if (cur == NULL) return;
buf = ctxt->buf;
while (cur != NULL) {
if ((ctxt->format == 1) && (xmlIndentTreeOutput) &&
((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_COMMENT_NODE) ||
(cur->type == XML_PI_NODE)))
xmlOutputBufferWrite(buf, ctxt->indent_size *
(ctxt->level > ctxt->indent_nr ?
ctxt->indent_nr : ctxt->level),
ctxt->indent);
xmlNodeDumpOutputInternal(ctxt, cur);
if (ctxt->format == 1) {
xmlOutputBufferWrite(buf, 1, "\n");
}
cur = cur->next;
}
}
#ifdef LIBXML_HTML_ENABLED
/**
* xmlNodeDumpOutputInternal:
* @cur: the current node
*
* Dump an HTML node, recursive behaviour, children are printed too.
*/
static int
htmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
const xmlChar *oldenc = NULL;
const xmlChar *oldctxtenc = ctxt->encoding;
const xmlChar *encoding = ctxt->encoding;
xmlOutputBufferPtr buf = ctxt->buf;
int switched_encoding = 0;
xmlDocPtr doc;
xmlInitParser();
doc = cur->doc;
if (doc != NULL) {
oldenc = doc->encoding;
if (ctxt->encoding != NULL) {
doc->encoding = BAD_CAST ctxt->encoding;
} else if (doc->encoding != NULL) {
encoding = doc->encoding;
}
}
if ((encoding != NULL) && (doc != NULL))
htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
if ((encoding == NULL) && (doc != NULL))
encoding = htmlGetMetaEncoding(doc);
if (encoding == NULL)
encoding = BAD_CAST "HTML";
if ((encoding != NULL) && (oldctxtenc == NULL) &&
(buf->encoder == NULL) && (buf->conv == NULL)) {
if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
doc->encoding = oldenc;
return(-1);
}
switched_encoding = 1;
}
if (ctxt->options & XML_SAVE_FORMAT)
htmlNodeDumpFormatOutput(buf, doc, cur,
(const char *)encoding, 1);
else
htmlNodeDumpFormatOutput(buf, doc, cur,
(const char *)encoding, 0);
/*
* Restore the state of the saving context at the end of the document
*/
if ((switched_encoding) && (oldctxtenc == NULL)) {
xmlSaveClearEncoding(ctxt);
}
if (doc != NULL)
doc->encoding = oldenc;
return(0);
}
#endif
/**
* xmlNodeDumpOutputInternal:
* @cur: the current node
*
* Dump an XML node, recursive behaviour, children are printed too.
*/
static void
xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
int format;
xmlNodePtr tmp;
xmlChar *start, *end;
xmlOutputBufferPtr buf;
if (cur == NULL) return;
buf = ctxt->buf;
if (cur->type == XML_XINCLUDE_START)
return;
if (cur->type == XML_XINCLUDE_END)
return;
if ((cur->type == XML_DOCUMENT_NODE) ||
(cur->type == XML_HTML_DOCUMENT_NODE)) {
xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
return;
}
#ifdef LIBXML_HTML_ENABLED
if (ctxt->options & XML_SAVE_XHTML) {
xhtmlNodeDumpOutput(ctxt, cur);
return;
}
if (((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL) &&
(cur->doc->type == XML_HTML_DOCUMENT_NODE) &&
((ctxt->options & XML_SAVE_AS_XML) == 0)) ||
(ctxt->options & XML_SAVE_AS_HTML)) {
htmlNodeDumpOutputInternal(ctxt, cur);
return;
}
#endif
if (cur->type == XML_DTD_NODE) {
xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
return;
}
if (cur->type == XML_DOCUMENT_FRAG_NODE) {
xmlNodeListDumpOutput(ctxt, cur->children);
return;
}
if (cur->type == XML_ELEMENT_DECL) {
xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
return;
}
if (cur->type == XML_ATTRIBUTE_DECL) {
xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
return;
}
if (cur->type == XML_ENTITY_DECL) {
xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
return;
}
if (cur->type == XML_TEXT_NODE) {
if (cur->content != NULL) {
if (cur->name != xmlStringTextNoenc) {
xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
} else {
/*
* Disable escaping, needed for XSLT
*/
xmlOutputBufferWriteString(buf, (const char *) cur->content);
}
}
return;
}
if (cur->type == XML_PI_NODE) {
if (cur->content != NULL) {
xmlOutputBufferWrite(buf, 2, "<?");
xmlOutputBufferWriteString(buf, (const char *)cur->name);
if (cur->content != NULL) {
if (ctxt->format == 2)
xmlOutputBufferWriteWSNonSig(ctxt, 0);
else
xmlOutputBufferWrite(buf, 1, " ");
xmlOutputBufferWriteString(buf, (const char *)cur->content);
}
xmlOutputBufferWrite(buf, 2, "?>");
} else {
xmlOutputBufferWrite(buf, 2, "<?");
xmlOutputBufferWriteString(buf, (const char *)cur->name);
if (ctxt->format == 2)
xmlOutputBufferWriteWSNonSig(ctxt, 0);
xmlOutputBufferWrite(buf, 2, "?>");
}
return;
}
if (cur->type == XML_COMMENT_NODE) {
if (cur->content != NULL) {
xmlOutputBufferWrite(buf, 4, "<!--");
xmlOutputBufferWriteString(buf, (const char *)cur->content);
xmlOutputBufferWrite(buf, 3, "-->");
}
return;
}
if (cur->type == XML_ENTITY_REF_NODE) {
xmlOutputBufferWrite(buf, 1, "&");
xmlOutputBufferWriteString(buf, (const char *)cur->name);
xmlOutputBufferWrite(buf, 1, ";");
return;
}
if (cur->type == XML_CDATA_SECTION_NODE) {
if (cur->content == NULL || *cur->content == '\0') {