/
ruby_xml_writer.c
1138 lines (999 loc) · 35 KB
/
ruby_xml_writer.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
#include "ruby_libxml.h"
#include "ruby_xml_writer.h"
#ifdef LIBXML_WRITER_ENABLED
#include <libxml/xmlwriter.h>
#endif
VALUE cXMLWriter;
static VALUE sEncoding, sStandalone;
#ifdef LIBXML_WRITER_ENABLED
/*
* Document-class: LibXML::XML::Writer
*
* The XML::Writer class provides a simpler, alternative way to build a valid
* XML document from scratch (forward-only) compared to a DOM approach (based
* on XML::Document class).
*
* For a more in depth tutorial, albeit in C, see http://xmlsoft.org/xmlwriter.html
*/
#include <libxml/xmlwriter.h>
typedef enum
{
RXMLW_OUTPUT_NONE,
RXMLW_OUTPUT_IO,
RXMLW_OUTPUT_DOC,
RXMLW_OUTPUT_STRING
} rxmlw_output_type;
typedef struct
{
VALUE output;
rb_encoding* encoding;
xmlBufferPtr buffer;
xmlTextWriterPtr writer;
rxmlw_output_type output_type;
int closed;
} rxml_writer_object;
static void rxml_writer_free(rxml_writer_object* rwo)
{
#if 0 /* seems to be done by xmlFreeTextWriter */
if (NULL != rwo->buffer)
{
xmlBufferFree(rwo->buffer);
}
#endif
rwo->closed = 1;
xmlFreeTextWriter(rwo->writer);
xfree(rwo);
}
static void rxml_writer_mark(rxml_writer_object* rwo)
{
if (!NIL_P(rwo->output))
{
rb_gc_mark(rwo->output);
}
}
static VALUE rxml_writer_wrap(rxml_writer_object* rwo)
{
return Data_Wrap_Struct(cXMLWriter, rxml_writer_mark, rxml_writer_free, rwo);
}
static rxml_writer_object* rxml_textwriter_get(VALUE obj)
{
rxml_writer_object* rwo;
Data_Get_Struct(obj, rxml_writer_object, rwo);
return rwo;
}
int rxml_writer_write_callback(void* context, const char* buffer, int len)
{
rxml_writer_object* rwo = context;
if (rwo->closed)
{
return 0;
}
else
{
return rxml_write_callback(rwo->output, buffer, len);
}
}
/* ===== public class methods ===== */
/* call-seq:
* XML::Writer::io(io) -> XML::Writer
*
* Creates a XML::Writer which will write XML directly into an IO object.
*/
static VALUE rxml_writer_io(VALUE klass, VALUE io)
{
xmlOutputBufferPtr out;
rxml_writer_object* rwo;
rwo = ALLOC(rxml_writer_object);
rwo->output = io;
rwo->buffer = NULL;
rwo->closed = 0;
rwo->encoding = rb_enc_get(io);
if (!rwo->encoding)
rwo->encoding = rb_utf8_encoding();
rwo->output_type = RXMLW_OUTPUT_IO;
xmlCharEncodingHandlerPtr encodingHdlr = xmlFindCharEncodingHandler(rwo->encoding->name);
if (NULL == (out = xmlOutputBufferCreateIO(rxml_writer_write_callback, NULL, (void*)rwo, encodingHdlr)))
{
rxml_raise(xmlGetLastError());
}
if (NULL == (rwo->writer = xmlNewTextWriter(out)))
{
rxml_raise(xmlGetLastError());
}
return rxml_writer_wrap(rwo);
}
/* call-seq:
* XML::Writer::file(path) -> XML::Writer
*
* Creates a XML::Writer object which will write XML into the file with
* the given name.
*/
static VALUE rxml_writer_file(VALUE klass, VALUE filename)
{
rxml_writer_object* rwo;
rwo = ALLOC(rxml_writer_object);
rwo->output = Qnil;
rwo->buffer = NULL;
rwo->closed = 0;
rwo->encoding = rb_utf8_encoding();
rwo->output_type = RXMLW_OUTPUT_NONE;
if (NULL == (rwo->writer = xmlNewTextWriterFilename(StringValueCStr(filename), 0)))
{
rxml_raise(xmlGetLastError());
}
return rxml_writer_wrap(rwo);
}
/* call-seq:
* XML::Writer::string -> XML::Writer
*
* Creates a XML::Writer which will write XML into memory, as string.
*/
static VALUE rxml_writer_string(VALUE klass)
{
rxml_writer_object* rwo;
rwo = ALLOC(rxml_writer_object);
rwo->output = Qnil;
rwo->closed = 0;
rwo->encoding = rb_utf8_encoding();
rwo->output_type = RXMLW_OUTPUT_STRING;
if (NULL == (rwo->buffer = xmlBufferCreate()))
{
rxml_raise(xmlGetLastError());
}
if (NULL == (rwo->writer = xmlNewTextWriterMemory(rwo->buffer, 0)))
{
xmlBufferFree(rwo->buffer);
rxml_raise(xmlGetLastError());
}
return rxml_writer_wrap(rwo);
}
/* call-seq:
* XML::Writer::document -> XML::Writer
*
* Creates a XML::Writer which will write into an in memory XML::Document
*/
static VALUE rxml_writer_doc(VALUE klass)
{
xmlDocPtr doc;
rxml_writer_object* rwo;
rwo = ALLOC(rxml_writer_object);
rwo->buffer = NULL;
rwo->output = Qnil;
rwo->closed = 0;
rwo->encoding = rb_utf8_encoding();
rwo->output_type = RXMLW_OUTPUT_DOC;
if (NULL == (rwo->writer = xmlNewTextWriterDoc(&doc, 0)))
{
rxml_raise(xmlGetLastError());
}
rwo->output = rxml_document_wrap(doc);
return rxml_writer_wrap(rwo);
}
/* ===== public instance methods ===== */
/* call-seq:
* writer.flush(empty? = true) -> (num|string)
*
* Flushes the output buffer. Returns the number of written bytes or
* the current content of the internal buffer for a in memory XML::Writer.
* If +empty?+ is +true+, and for a in memory XML::Writer, this internel
* buffer is empty.
*/
static VALUE rxml_writer_flush(int argc, VALUE* argv, VALUE self)
{
int ret;
VALUE empty;
rxml_writer_object* rwo;
rb_scan_args(argc, argv, "01", &empty);
rwo = rxml_textwriter_get(self);
if (-1 == (ret = xmlTextWriterFlush(rwo->writer)))
{
rxml_raise(xmlGetLastError());
}
if (NULL != rwo->buffer)
{
VALUE content;
content = rb_external_str_new_with_enc((const char*)rwo->buffer->content, rwo->buffer->use, rwo->encoding);
if (NIL_P(empty) || RTEST(empty))
{ /* nil = default value = true */
xmlBufferEmpty(rwo->buffer);
}
return content;
}
else
{
return INT2NUM(ret);
}
}
/* call-seq:
* writer.result -> (XML::Document|"string"|nil)
*
* Returns the associated result object to the XML::Writer creation.
* A String for a XML::Writer object created with XML::Writer::string,
* a XML::Document with XML::Writer::document, etc.
*/
static VALUE rxml_writer_result(VALUE self)
{
VALUE ret = Qnil;
rxml_writer_object* rwo = rxml_textwriter_get(self);
int bytesWritten = xmlTextWriterFlush(rwo->writer);
if (bytesWritten == -1)
{
rxml_raise(xmlGetLastError());
}
switch (rwo->output_type)
{
case RXMLW_OUTPUT_DOC:
ret = rwo->output;
break;
case RXMLW_OUTPUT_STRING:
ret = rb_external_str_new_with_enc((const char*)rwo->buffer->content, rwo->buffer->use, rwo->encoding);
break;
case RXMLW_OUTPUT_IO:
case RXMLW_OUTPUT_NONE:
break;
default:
rb_bug("unexpected output");
break;
}
return ret;
}
/* ===== private helpers ===== */
static VALUE numeric_rxml_writer_void(VALUE obj, int (*fn)(xmlTextWriterPtr))
{
int ret;
rxml_writer_object* rwo;
rwo = rxml_textwriter_get(obj);
ret = fn(rwo->writer);
return (-1 == ret ? Qfalse : Qtrue);
}
#define numeric_rxml_writer_string(/*VALUE*/ obj, /*VALUE*/ name_or_content, /*int (**/fn/*)(xmlTextWriterPtr, const xmlChar *)*/) \
numeric_rxml_writer_va_strings(obj, Qundef, 1, fn, name_or_content)
/**
* This is quite ugly but thanks to libxml2 coding style, all xmlTextWriter*
* calls can be redirected to a single function. This can be convenient to:
* - avoid repeating yourself
* - convert strings to UTF-8
* - validate names
* and so on
**/
#define XMLWRITER_MAX_STRING_ARGS 5
static VALUE numeric_rxml_writer_va_strings(VALUE obj, VALUE pe, size_t strings_count, int (*fn)(ANYARGS), ...)
{
va_list ap;
size_t argc;
int ret = -1;
rxml_writer_object* rwo;
const xmlChar* argv[XMLWRITER_MAX_STRING_ARGS];
VALUE utf8[XMLWRITER_MAX_STRING_ARGS], orig[XMLWRITER_MAX_STRING_ARGS];
if (strings_count > XMLWRITER_MAX_STRING_ARGS)
{
rb_bug("more arguments than expected");
}
va_start(ap, fn);
rwo = rxml_textwriter_get(obj);
for (argc = 0; argc < strings_count; argc++)
{
VALUE arg;
arg = va_arg(ap, VALUE);
orig[argc] = arg;
if (NIL_P(arg))
{
utf8[argc] = Qnil;
argv[argc] = NULL;
}
else
{
utf8[argc] = rb_str_conv_enc(orig[argc], rb_enc_get(orig[argc]), rwo->encoding);
argv[argc] = BAD_CAST StringValueCStr(utf8[argc]);
}
}
va_end(ap);
if (Qundef == pe)
{
switch (strings_count)
{
case 0:
ret = fn(rwo->writer);
break;
case 1:
ret = fn(rwo->writer, argv[0]);
break;
case 2:
ret = fn(rwo->writer, argv[0], argv[1]);
break;
case 3:
ret = fn(rwo->writer, argv[0], argv[1], argv[2]);
break;
case 4:
ret = fn(rwo->writer, argv[0], argv[1], argv[2], argv[3]);
break;
case 5:
ret = fn(rwo->writer, argv[0], argv[1], argv[2], argv[3], argv[4]);
break;
default:
break;
}
}
else
{
int xpe;
xpe = RTEST(pe);
switch (strings_count)
{ /* strings_count doesn't include pe */
case 0:
ret = fn(rwo->writer, xpe);
break;
case 1:
ret = fn(rwo->writer, xpe, argv[0]);
break;
case 2:
ret = fn(rwo->writer, xpe, argv[0], argv[1]);
break;
case 3:
ret = fn(rwo->writer, xpe, argv[0], argv[1], argv[2]);
break;
case 4:
ret = fn(rwo->writer, xpe, argv[0], argv[1], argv[2], argv[3]);
break;
case 5:
ret = fn(rwo->writer, xpe, argv[0], argv[1], argv[2], argv[3], argv[4]);
break;
default:
break;
}
}
while (--strings_count > 0)
{
if (!NIL_P(orig[strings_count]))
{
if (orig[strings_count] != utf8[strings_count])
{
rb_str_free(utf8[strings_count]);
}
}
}
return (-1 == ret ? Qfalse : Qtrue);
}
/* ===== public instance methods ===== */
#if LIBXML_VERSION >= 20605
/* call-seq:
* writer.set_indent(indentation) -> (true|false)
*
* Toggles indentation on or off. Returns +false+ on failure.
*
* Availability: libxml2 >= 2.6.5
*/
static VALUE rxml_writer_set_indent(VALUE self, VALUE indentation)
{
int ret;
rxml_writer_object* rwo;
rwo = rxml_textwriter_get(self);
ret = xmlTextWriterSetIndent(rwo->writer, RTEST(indentation));
return (-1 == ret ? Qfalse : Qtrue);
}
/* call-seq:
* writer.set_indent_string(string) -> (true|false)
*
* Sets the string to use to indent each element of the document.
* Don't forget to enable indentation with set_indent. Returns
* +false+ on failure.
*
* Availability: libxml2 >= 2.6.5
*/
static VALUE rxml_writer_set_indent_string(VALUE self, VALUE indentation)
{
return numeric_rxml_writer_string(self, indentation, xmlTextWriterSetIndentString);
}
#endif /* LIBXML_VERSION >= 20605 */
/* ===== public full tag interface ===== */
/* write_<X> = start_<X> + write_string + end_<X> */
/* call-seq:
* writer.write_comment(content) -> (true|false)
*
* Writes a full comment tag, all at once. Returns +false+ on failure.
* This is equivalent to start_comment + write_string(content) + end_comment.
*/
static VALUE rxml_writer_write_comment(VALUE self, VALUE content)
{
return numeric_rxml_writer_string(self, content, xmlTextWriterWriteComment);
}
/* call-seq:
* writer.write_cdata(content) -> (true|false)
*
* Writes a full CDATA section, all at once. Returns +false+ on failure.
* This is equivalent to start_cdata + write_string(content) + end_cdata.
*/
static VALUE rxml_writer_write_cdata(VALUE self, VALUE content)
{
return numeric_rxml_writer_string(self, content, xmlTextWriterWriteCDATA);
}
static VALUE rxml_writer_start_element(VALUE, VALUE);
static VALUE rxml_writer_start_element_ns(int, VALUE*, VALUE);
static VALUE rxml_writer_end_element(VALUE);
/* call-seq:
* writer.write_element(name, content) -> (true|false)
*
* Writes a full element tag, all at once. Returns +false+ on failure.
* This is equivalent to start_element(name) + write_string(content) +
* end_element.
*/
static VALUE rxml_writer_write_element(int argc, VALUE* argv, VALUE self)
{
VALUE name, content;
rb_scan_args(argc, argv, "11", &name, &content);
if (Qnil == content)
{
if (Qfalse == rxml_writer_start_element(self, name))
{
return Qfalse;
}
return rxml_writer_end_element(self);
}
else
{
return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteElement, name, content);
}
}
#define ARRAY_SIZE(array) \
(sizeof(array) / sizeof((array)[0]))
/* call-seq:
* writer.write_element_ns(prefix, name, namespaceURI, content) -> (true|false)
*
* Writes a full namespaced element tag, all at once. Returns +false+ on failure.
* This is a shortcut for start_element_ns(prefix, name, namespaceURI) +
* write_string(content) + end_element.
*
* Notes:
* - by default, the xmlns: definition is repeated on every element. If you want
* the prefix, but don't want the xmlns: declaration repeated, set +namespaceURI+
* to nil or omit it. Don't forget to declare the namespace prefix somewhere
* earlier.
* - +content+ can be omitted for an empty tag
*/
static VALUE rxml_writer_write_element_ns(int argc, VALUE* argv, VALUE self)
{
VALUE prefix, name, namespaceURI, content;
rb_scan_args(argc, argv, "22", &prefix, &name, &namespaceURI, &content);
if (Qnil == content)
{
VALUE argv[3] = { prefix, name, namespaceURI };
if (Qfalse == rxml_writer_start_element_ns(ARRAY_SIZE(argv), argv, self))
{
return Qfalse;
}
return rxml_writer_end_element(self);
}
else
{
return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteElementNS, prefix, name, namespaceURI, content);
}
}
/* call-seq:
* writer.write_attribute(name, content) -> (true|false)
*
* Writes a full attribute, all at once. Returns +false+ on failure.
* Same as start_attribute(name) + write_string(content) + end_attribute.
*/
static VALUE rxml_writer_write_attribute(VALUE self, VALUE name, VALUE content)
{
return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteAttribute, name, content);
}
/* call-seq:
* writer.write_attribute_ns(prefix, name, namespaceURI, content) -> (true|false)
*
* Writes a full namespaced attribute, all at once. Returns +false+ on failure.
* Same as start_attribute_ns(prefix, name, namespaceURI) +
* write_string(content) + end_attribute.
*
* Notes:
* - by default, the xmlns: definition is repeated on every element. If you want
* the prefix, but don't want the xmlns: declaration repeated, set +namespaceURI+
* to nil or omit it. Don't forget to declare the namespace prefix somewhere
* earlier.
* - +content+ can be omitted too for an empty attribute
*/
static VALUE rxml_writer_write_attribute_ns(int argc, VALUE* argv, VALUE self)
{
VALUE prefix, name, namespaceURI, content;
rb_scan_args(argc, argv, "22", &prefix, &name, &namespaceURI, &content);
return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteAttributeNS, prefix, name, namespaceURI, content);
}
/* call-seq:
* writer.write_pi(target, content) -> (true|false)
*
* Writes a full CDATA tag, all at once. Returns +false+ on failure.
* This is a shortcut for start_pi(target) + write_string(content) + end_pi.
*/
static VALUE rxml_writer_write_pi(VALUE self, VALUE target, VALUE content)
{
return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWritePI, target, content);
}
/* ===== public start/end interface ===== */
/* call-seq:
* writer.write_string(content) -> (true|false)
*
* Safely (problematic characters are internally translated to their
* associated named entities) writes a string into the current node
* (attribute, element, comment, ...). Returns +false+ on failure.
*/
static VALUE rxml_writer_write_string(VALUE self, VALUE content)
{
return numeric_rxml_writer_string(self, content, xmlTextWriterWriteString);
}
/* call-seq:
* writer.write_raw(content) -> (true|false)
*
* Writes the string +content+ as is, reserved characters are not
* translated to their associated entities. Returns +false+ on failure.
* Consider write_string to handle them.
*/
static VALUE rxml_writer_write_raw(VALUE self, VALUE content)
{
return numeric_rxml_writer_string(self, content, xmlTextWriterWriteRaw);
}
/* call-seq:
* writer.start_attribute(name) -> (true|false)
*
* Starts an attribute. Returns +false+ on failure.
*/
static VALUE rxml_writer_start_attribute(VALUE self, VALUE name)
{
return numeric_rxml_writer_string(self, name, xmlTextWriterStartAttribute);
}
/* call-seq:
* writer.start_attribute_ns(prefix, name, namespaceURI) -> (true|false)
*
* Starts a namespaced attribute. Returns +false+ on failure.
*
* Note: by default, the xmlns: definition is repeated on every element. If
* you want the prefix, but don't want the xmlns: declaration repeated, set
* +namespaceURI+ to nil or omit it. Don't forget to declare the namespace
* prefix somewhere earlier.
*/
static VALUE rxml_writer_start_attribute_ns(int argc, VALUE* argv, VALUE self)
{
VALUE prefix, name, namespaceURI;
rb_scan_args(argc, argv, "21", &prefix, &name, &namespaceURI);
return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartAttributeNS, prefix, name, namespaceURI);
}
/* call-seq:
* writer.end_attribute -> (true|false)
*
* Ends an attribute, namespaced or not. Returns +false+ on failure.
*/
static VALUE rxml_writer_end_attribute(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndAttribute);
}
#if LIBXML_VERSION >= 20607
/* call-seq:
* writer.start_comment -> (true|false)
*
* Starts a comment. Returns +false+ on failure.
* Note: libxml2 >= 2.6.7 required
*/
static VALUE rxml_writer_start_comment(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterStartComment);
}
/* call-seq:
* writer.end_comment -> (true|false)
*
* Ends current comment, returns +false+ on failure.
* Note: libxml2 >= 2.6.7 required
*/
static VALUE rxml_writer_end_comment(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndComment);
}
#endif /* LIBXML_VERSION >= 20607 */
/* call-seq:
* writer.start_element(name) -> (true|false)
*
* Starts a new element. Returns +false+ on failure.
*/
static VALUE rxml_writer_start_element(VALUE self, VALUE name)
{
return numeric_rxml_writer_string(self, name, xmlTextWriterStartElement);
}
/* call-seq:
* writer.start_element_ns(prefix, name, namespaceURI) -> (true|false)
*
* Starts a new namespaced element. Returns +false+ on failure.
*
* Note: by default, the xmlns: definition is repeated on every element. If
* you want the prefix, but don't want the xmlns: declaration repeated, set
* +namespaceURI+ to nil or omit it. Don't forget to declare the namespace
* prefix somewhere earlier.
*/
static VALUE rxml_writer_start_element_ns(int argc, VALUE* argv, VALUE self)
{
VALUE prefix, name, namespaceURI;
rb_scan_args(argc, argv, "21", &prefix, &name, &namespaceURI);
return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartElementNS, prefix, name, namespaceURI);
}
/* call-seq:
* writer.end_element -> (true|false)
*
* Ends current element, namespaced or not. Returns +false+ on failure.
*/
static VALUE rxml_writer_end_element(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndElement);
}
/* call-seq:
* writer.write_full_end_element -> (true|false)
*
* Ends current element, namespaced or not. Returns +false+ on failure.
* This method writes an end tag even if the element is empty (<foo></foo>),
* end_element does not (<foo/>).
*/
static VALUE rxml_writer_full_end_element(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterFullEndElement);
}
/* call-seq:
* writer.start_cdata -> (true|false)
*
* Starts a new CDATA section. Returns +false+ on failure.
*/
static VALUE rxml_writer_start_cdata(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterStartCDATA);
}
/* call-seq:
* writer.end_cdata -> (true|false)
*
* Ends current CDATA section. Returns +false+ on failure.
*/
static VALUE rxml_writer_end_cdata(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndCDATA);
}
/* call-seq:
* writer.start_document -> (true|false)
* writer.start_document(:encoding => XML::Encoding::UTF_8,
* :standalone => true) -> (true|false)
*
* Starts a new document. Returns +false+ on failure.
*
* You may provide an optional hash table to control XML header that will be
* generated. Valid options are:
* - encoding: the output document encoding, defaults to nil (= UTF-8). Valid
* values are the encoding constants defined on XML::Encoding
* - standalone: nil (default) or a boolean to indicate if the document is
* standalone or not
*/
static VALUE rxml_writer_start_document(int argc, VALUE* argv, VALUE self)
{
int ret;
VALUE options = Qnil;
rxml_writer_object* rwo;
const xmlChar* xencoding = NULL;
const char* xstandalone = NULL;
rb_scan_args(argc, argv, "01", &options);
if (!NIL_P(options))
{
VALUE encoding, standalone;
encoding = standalone = Qnil;
Check_Type(options, T_HASH);
encoding = rb_hash_aref(options, sEncoding);
xencoding = NIL_P(encoding) ? NULL : (const xmlChar*)xmlGetCharEncodingName(NUM2INT(encoding));
standalone = rb_hash_aref(options, sStandalone);
if (NIL_P(standalone))
{
xstandalone = NULL;
}
else
{
xstandalone = RTEST(standalone) ? "yes" : "no";
}
}
rwo = rxml_textwriter_get(self);
rwo->encoding = rxml_figure_encoding(xencoding);
ret = xmlTextWriterStartDocument(rwo->writer, NULL, (const char*)xencoding, xstandalone);
return (-1 == ret ? Qfalse : Qtrue);
}
/* call-seq:
* writer.end_document -> (true|false)
*
* Ends current document. Returns +false+ on failure.
*/
static VALUE rxml_writer_end_document(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndDocument);
}
/* call-seq:
* writer.start_pi(target) -> (true|false)
*
* Starts a new processing instruction. Returns +false+ on failure.
*/
static VALUE rxml_writer_start_pi(VALUE self, VALUE target)
{
return numeric_rxml_writer_string(self, target, xmlTextWriterStartPI);
}
/* call-seq:
* writer.end_pi -> (true|false)
*
* Ends current processing instruction. Returns +false+ on failure.
*/
static VALUE rxml_writer_end_pi(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndPI);
}
/* call-seq:
* writer.start_dtd(qualifiedName, publicId, systemId) -> (true|false)
*
* Starts a DTD. Returns +false+ on failure.
*/
static VALUE rxml_writer_start_dtd(int argc, VALUE* argv, VALUE self)
{
VALUE name, pubid, sysid;
rb_scan_args(argc, argv, "12", &name, &pubid, &sysid);
return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartDTD, name, pubid, sysid);
}
/* call-seq:
* writer.start_dtd_element(qualifiedName) -> (true|false)
*
* Starts a DTD element (<!ELEMENT ... >). Returns +false+ on failure.
*/
static VALUE rxml_writer_start_dtd_element(VALUE self, VALUE name)
{
return numeric_rxml_writer_string(self, name, xmlTextWriterStartDTDElement);
}
/* call-seq:
* writer.start_dtd_entity(name, pe = false) -> (true|false)
*
* Starts a DTD entity (<!ENTITY ... >). Returns +false+ on failure.
*/
static VALUE rxml_writer_start_dtd_entity(int argc, VALUE* argv, VALUE self)
{
VALUE name, pe;
rb_scan_args(argc, argv, "11", &name, &pe);
if (NIL_P(pe))
{
pe = Qfalse;
}
return numeric_rxml_writer_va_strings(self, pe, 1, xmlTextWriterStartDTDEntity, name);
}
/* call-seq:
* writer.start_dtd_attlist(name) -> (true|false)
*
* Starts a DTD attribute list (<!ATTLIST ... >). Returns +false+ on failure.
*/
static VALUE rxml_writer_start_dtd_attlist(VALUE self, VALUE name)
{
return numeric_rxml_writer_string(self, name, xmlTextWriterStartDTDAttlist);
}
/* call-seq:
* writer.end_dtd -> (true|false)
*
* Ends current DTD, returns +false+ on failure.
*/
static VALUE rxml_writer_end_dtd(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndDTD);
}
/* call-seq:
* writer.end_dtd_entity -> (true|false)
*
* Ends current DTD entity, returns +false+ on failure.
*/
static VALUE rxml_writer_end_dtd_entity(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndDTDEntity);
}
/* call-seq:
* writer.end_dtd_attlist -> (true|false)
*
* Ends current DTD attribute list, returns +false+ on failure.
*/
static VALUE rxml_writer_end_dtd_attlist(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndDTDAttlist);
}
/* call-seq:
* writer.end_dtd_element -> (true|false)
*
* Ends current DTD element, returns +false+ on failure.
*/
static VALUE rxml_writer_end_dtd_element(VALUE self)
{
return numeric_rxml_writer_void(self, xmlTextWriterEndDTDElement);
}
/* call-seq:
* writer.write_dtd(name [ [ [, publicId ], systemId ], subset ]) -> (true|false)
*
* Writes a DTD, all at once. Returns +false+ on failure.
* - name: dtd name
* - publicId: external subset public identifier, use nil for a SYSTEM doctype
* - systemId: external subset system identifier
* - subset: content
*
* Examples:
* writer.write_dtd 'html'
* #=> <!DOCTYPE html>
* writer.write_dtd 'docbook', nil, 'http://www.docbook.org/xml/5.0/dtd/docbook.dtd'
* #=> <!DOCTYPE docbook SYSTEM "http://www.docbook.org/xml/5.0/dtd/docbook.dtd">
* writer.write_dtd 'html', '-//W3C//DTD XHTML 1.1//EN', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
* #=> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
* writer.write_dtd 'person', nil, nil, '<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>'
* #=> <!DOCTYPE person [<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>]>
*/
static VALUE rxml_writer_write_dtd(int argc, VALUE* argv, VALUE self)
{
VALUE name, pubid, sysid, subset;
rb_scan_args(argc, argv, "13", &name, &pubid, &sysid, &subset);
return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteDTD, name, pubid, sysid, subset);
}
/* call-seq:
* writer.write_dtd_attlist(name, content) -> (true|false)
*
* Writes a DTD attribute list, all at once. Returns +false+ on failure.
* writer.write_dtd_attlist 'id', 'ID #IMPLIED'
* #=> <!ATTLIST id ID #IMPLIED>
*/
static VALUE rxml_writer_write_dtd_attlist(VALUE self, VALUE name, VALUE content)
{
return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteDTDAttlist, name, content);
}
/* call-seq:
* writer.write_dtd_element(name, content) -> (true|false)
*
* Writes a full DTD element, all at once. Returns +false+ on failure.
* writer.write_dtd_element 'person', '(firstname,lastname)'
* #=> <!ELEMENT person (firstname,lastname)>
*/
static VALUE rxml_writer_write_dtd_element(VALUE self, VALUE name, VALUE content)
{
return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteDTDElement, name, content);
}
/* call-seq:
* writer.write_dtd_entity(name, publicId, systemId, ndataid, content, pe) -> (true|false)
*
* Writes a DTD entity, all at once. Returns +false+ on failure.
*/
static VALUE rxml_writer_write_dtd_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE content, VALUE pe)
{
return numeric_rxml_writer_va_strings(self, pe, 5, xmlTextWriterWriteDTDEntity, name, pubid, sysid, ndataid, content);
}
/* call-seq:
* writer.write_dtd_external_entity(name, publicId, systemId, ndataid, pe) -> (true|false)
*
* Writes a DTD external entity. The entity must have been started
* with start_dtd_entity. Returns +false+ on failure.
* - name: the name of the DTD entity
* - publicId: the public identifier, which is an alternative to the system identifier
* - systemId: the system identifier, which is the URI of the DTD
* - ndataid: the xml notation name
* - pe: +true+ if this is a parameter entity (to be used only in the DTD
* itself), +false+ if not
*/
static VALUE rxml_writer_write_dtd_external_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE pe)
{
return numeric_rxml_writer_va_strings(self, pe, 4, xmlTextWriterWriteDTDExternalEntity, name, pubid, sysid, ndataid);
}
/* call-seq:
* writer.write_dtd_external_entity_contents(publicId, systemId, ndataid) -> (true|false)
*