libxml-ruby 2.9.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY +811 -790
  3. data/LICENSE +20 -20
  4. data/MANIFEST +166 -166
  5. data/README.rdoc +188 -184
  6. data/Rakefile +1 -1
  7. data/ext/libxml/libxml.c +80 -80
  8. data/ext/libxml/ruby_libxml.h +75 -75
  9. data/ext/libxml/ruby_xml.c +0 -84
  10. data/ext/libxml/ruby_xml.h +0 -10
  11. data/ext/libxml/ruby_xml_attr.h +12 -12
  12. data/ext/libxml/ruby_xml_attr_decl.c +153 -153
  13. data/ext/libxml/ruby_xml_attr_decl.h +11 -11
  14. data/ext/libxml/ruby_xml_attributes.c +275 -275
  15. data/ext/libxml/ruby_xml_attributes.h +15 -15
  16. data/ext/libxml/ruby_xml_document.c +21 -27
  17. data/ext/libxml/ruby_xml_document.h +11 -11
  18. data/ext/libxml/ruby_xml_dtd.c +2 -13
  19. data/ext/libxml/ruby_xml_dtd.h +9 -9
  20. data/ext/libxml/ruby_xml_error.c +996 -996
  21. data/ext/libxml/ruby_xml_error.h +12 -12
  22. data/ext/libxml/ruby_xml_html_parser.c +89 -92
  23. data/ext/libxml/ruby_xml_html_parser.h +10 -10
  24. data/ext/libxml/ruby_xml_html_parser_context.h +10 -10
  25. data/ext/libxml/ruby_xml_html_parser_options.c +46 -46
  26. data/ext/libxml/ruby_xml_html_parser_options.h +10 -10
  27. data/ext/libxml/ruby_xml_input_cbg.h +20 -20
  28. data/ext/libxml/ruby_xml_io.c +0 -1
  29. data/ext/libxml/ruby_xml_io.h +10 -10
  30. data/ext/libxml/ruby_xml_namespace.c +153 -153
  31. data/ext/libxml/ruby_xml_namespace.h +10 -10
  32. data/ext/libxml/ruby_xml_namespaces.c +293 -293
  33. data/ext/libxml/ruby_xml_namespaces.h +9 -9
  34. data/ext/libxml/ruby_xml_node.c +100 -143
  35. data/ext/libxml/ruby_xml_node.h +13 -11
  36. data/ext/libxml/ruby_xml_parser.c +91 -94
  37. data/ext/libxml/ruby_xml_parser.h +12 -12
  38. data/ext/libxml/ruby_xml_parser_context.h +10 -10
  39. data/ext/libxml/ruby_xml_parser_options.c +66 -66
  40. data/ext/libxml/ruby_xml_parser_options.h +12 -12
  41. data/ext/libxml/ruby_xml_reader.c +45 -37
  42. data/ext/libxml/ruby_xml_reader.h +17 -17
  43. data/ext/libxml/ruby_xml_relaxng.h +10 -10
  44. data/ext/libxml/ruby_xml_sax2_handler.c +3 -3
  45. data/ext/libxml/ruby_xml_sax2_handler.h +10 -10
  46. data/ext/libxml/ruby_xml_sax_parser.c +116 -120
  47. data/ext/libxml/ruby_xml_sax_parser.h +10 -10
  48. data/ext/libxml/ruby_xml_schema.h +809 -809
  49. data/ext/libxml/ruby_xml_schema_attribute.c +109 -109
  50. data/ext/libxml/ruby_xml_schema_attribute.h +15 -15
  51. data/ext/libxml/ruby_xml_schema_element.c +94 -94
  52. data/ext/libxml/ruby_xml_schema_element.h +14 -14
  53. data/ext/libxml/ruby_xml_schema_facet.c +52 -52
  54. data/ext/libxml/ruby_xml_schema_facet.h +13 -13
  55. data/ext/libxml/ruby_xml_schema_type.c +259 -259
  56. data/ext/libxml/ruby_xml_schema_type.h +9 -9
  57. data/ext/libxml/ruby_xml_version.h +9 -9
  58. data/ext/libxml/ruby_xml_writer.c +1136 -1136
  59. data/ext/libxml/ruby_xml_writer.h +10 -10
  60. data/ext/libxml/ruby_xml_xinclude.c +16 -16
  61. data/ext/libxml/ruby_xml_xinclude.h +11 -11
  62. data/ext/libxml/ruby_xml_xpath.c +42 -36
  63. data/ext/libxml/ruby_xml_xpath.h +13 -13
  64. data/ext/libxml/ruby_xml_xpath_context.c +1 -1
  65. data/ext/libxml/ruby_xml_xpath_context.h +9 -9
  66. data/ext/libxml/ruby_xml_xpath_expression.c +81 -81
  67. data/ext/libxml/ruby_xml_xpath_expression.h +10 -10
  68. data/ext/libxml/ruby_xml_xpath_object.c +5 -2
  69. data/ext/libxml/ruby_xml_xpath_object.h +17 -17
  70. data/ext/libxml/ruby_xml_xpointer.c +99 -99
  71. data/ext/libxml/ruby_xml_xpointer.h +11 -11
  72. data/ext/vc/libxml_ruby.sln +17 -15
  73. data/lib/libxml.rb +1 -6
  74. data/lib/libxml/node.rb +2 -78
  75. data/lib/libxml/parser.rb +0 -266
  76. data/lib/libxml/sax_parser.rb +0 -17
  77. data/lib/libxml/schema.rb +66 -66
  78. data/lib/libxml/schema/attribute.rb +19 -19
  79. data/lib/libxml/schema/element.rb +27 -27
  80. data/lib/libxml/schema/type.rb +29 -29
  81. data/script/benchmark/depixelate +634 -634
  82. data/script/benchmark/hamlet.xml +9054 -9054
  83. data/script/benchmark/parsecount +170 -170
  84. data/script/benchmark/throughput +41 -41
  85. data/script/test +6 -6
  86. data/test/c14n/given/example-1.xml +14 -14
  87. data/test/c14n/given/example-2.xml +11 -11
  88. data/test/c14n/given/example-3.xml +18 -18
  89. data/test/c14n/given/example-4.xml +9 -9
  90. data/test/c14n/given/example-5.xml +12 -12
  91. data/test/c14n/given/example-6.xml +2 -2
  92. data/test/c14n/given/example-7.xml +11 -11
  93. data/test/c14n/given/example-8.xml +11 -11
  94. data/test/c14n/given/example-8.xpath +9 -9
  95. data/test/c14n/result/1-1-without-comments/example-1 +3 -3
  96. data/test/c14n/result/1-1-without-comments/example-2 +10 -10
  97. data/test/c14n/result/1-1-without-comments/example-3 +13 -13
  98. data/test/c14n/result/1-1-without-comments/example-4 +8 -8
  99. data/test/c14n/result/1-1-without-comments/example-5 +2 -2
  100. data/test/c14n/result/with-comments/example-1 +5 -5
  101. data/test/c14n/result/with-comments/example-2 +10 -10
  102. data/test/c14n/result/with-comments/example-3 +13 -13
  103. data/test/c14n/result/with-comments/example-4 +8 -8
  104. data/test/c14n/result/with-comments/example-5 +3 -3
  105. data/test/c14n/result/without-comments/example-1 +3 -3
  106. data/test/c14n/result/without-comments/example-2 +10 -10
  107. data/test/c14n/result/without-comments/example-3 +13 -13
  108. data/test/c14n/result/without-comments/example-4 +8 -8
  109. data/test/c14n/result/without-comments/example-5 +2 -2
  110. data/test/model/atom.xml +12 -12
  111. data/test/model/bands.iso-8859-1.xml +4 -4
  112. data/test/model/bands.utf-8.xml +4 -4
  113. data/test/model/bands.xml +4 -4
  114. data/test/model/books.xml +153 -153
  115. data/test/model/merge_bug_data.xml +58 -58
  116. data/test/model/ruby-lang.html +238 -238
  117. data/test/model/rubynet.xml +79 -79
  118. data/test/model/shiporder.rnc +28 -28
  119. data/test/model/shiporder.rng +86 -86
  120. data/test/model/shiporder.xml +22 -22
  121. data/test/model/shiporder.xsd +39 -39
  122. data/test/model/soap.xml +27 -27
  123. data/test/model/xinclude.xml +4 -4
  124. data/test/tc_attributes.rb +0 -6
  125. data/test/tc_error.rb +157 -158
  126. data/test/tc_node.rb +33 -17
  127. data/test/tc_node_edit.rb +0 -15
  128. data/test/tc_node_pi.rb +39 -39
  129. data/test/tc_parser.rb +0 -48
  130. data/test/tc_reader.rb +12 -53
  131. data/test/tc_writer.rb +447 -447
  132. data/test/tc_xpath.rb +1 -1
  133. data/test/test_helper.rb +2 -2
  134. metadata +3 -8
  135. data/ext/libxml/extconf.h +0 -4
  136. data/lib/libxml/ns.rb +0 -22
  137. data/lib/libxml/properties.rb +0 -23
  138. data/lib/libxml/reader.rb +0 -29
  139. data/lib/libxml/xpath_object.rb +0 -16
@@ -1,9 +1,9 @@
1
- #ifndef __RXML_SCHEMA_TYPE__
2
- #define __RXML_SCHEMA_TYPE__
3
-
4
- extern VALUE cXMLSchemaType;
5
-
6
- VALUE rxml_wrap_schema_type(xmlSchemaTypePtr xtype);
7
- void rxml_init_schema_type(void);
8
-
9
- #endif
1
+ #ifndef __RXML_SCHEMA_TYPE__
2
+ #define __RXML_SCHEMA_TYPE__
3
+
4
+ extern VALUE cXMLSchemaType;
5
+
6
+ VALUE rxml_wrap_schema_type(xmlSchemaTypePtr xtype);
7
+ void rxml_init_schema_type(void);
8
+
9
+ #endif
@@ -1,9 +1,9 @@
1
- /* Don't nuke this block! It is used for automatically updating the
2
- * versions below. VERSION = string formatting, VERNUM = numbered
3
- * version for inline testing: increment both or none at all.*/
4
- #define RUBY_LIBXML_VERSION "2.9.0"
5
- #define RUBY_LIBXML_VERNUM 290
6
- #define RUBY_LIBXML_VER_MAJ 2
7
- #define RUBY_LIBXML_VER_MIN 9
8
- #define RUBY_LIBXML_VER_MIC 0
9
- #define RUBY_LIBXML_VER_PATCH 0
1
+ /* Don't nuke this block! It is used for automatically updating the
2
+ * versions below. VERSION = string formatting, VERNUM = numbered
3
+ * version for inline testing: increment both or none at all.*/
4
+ #define RUBY_LIBXML_VERSION "3.0.0"
5
+ #define RUBY_LIBXML_VERNUM 300
6
+ #define RUBY_LIBXML_VER_MAJ 3
7
+ #define RUBY_LIBXML_VER_MIN 0
8
+ #define RUBY_LIBXML_VER_MIC 0
9
+ #define RUBY_LIBXML_VER_PATCH 0
@@ -1,1136 +1,1136 @@
1
- #include "ruby_libxml.h"
2
- #include "ruby_xml_writer.h"
3
-
4
- VALUE cXMLWriter;
5
- static VALUE sEncoding, sStandalone;
6
-
7
- #ifdef LIBXML_WRITER_ENABLED
8
-
9
- /*
10
- * Document-class: LibXML::XML::Writer
11
- *
12
- * The XML::Writer class provides a simpler, alternative way to build a valid
13
- * XML document from scratch (forward-only) compared to a DOM approach (based
14
- * on XML::Document class).
15
- *
16
- * For a more in depth tutorial, albeit in C, see http://xmlsoft.org/xmlwriter.html
17
- */
18
-
19
- #include <libxml/xmlwriter.h>
20
-
21
-
22
- typedef enum {
23
- RXMLW_OUTPUT_NONE,
24
- RXMLW_OUTPUT_IO,
25
- RXMLW_OUTPUT_DOC,
26
- RXMLW_OUTPUT_STRING
27
- } rxmlw_output_type;
28
-
29
- typedef struct {
30
- VALUE output;
31
- #ifdef HAVE_RUBY_ENCODING_H
32
- rb_encoding *encoding;
33
- #endif /* HAVE_RUBY_ENCODING_H */
34
- xmlBufferPtr buffer;
35
- xmlTextWriterPtr writer;
36
- rxmlw_output_type output_type;
37
- int closed;
38
- } rxml_writer_object;
39
-
40
- #ifdef HAVE_RUBY_ENCODING_H
41
-
42
- #define /*VALUE*/ rxml_writer_c_to_ruby_string(/*const xmlChar **/ string, /*long*/ string_len) \
43
- rb_external_str_new_with_enc(string, string_len, rb_utf8_encoding())
44
-
45
- #define /*VALUE*/ rxml_writer_ruby_string_to_utf8(/*VALUE*/ string) \
46
- rb_str_conv_enc(string, rb_enc_get(string), rb_utf8_encoding())
47
- // rb_str_export_to_enc(string, rb_utf8_encoding())
48
-
49
- #define /*void*/ rxml_writer_free_utf8_string(/*VALUE*/ orig, /*VALUE*/ utf8) \
50
- do { \
51
- if (orig != utf8) { \
52
- rb_str_free(utf8); \
53
- } \
54
- } while (0);
55
-
56
- #else
57
-
58
- #define /*VALUE*/ rxml_writer_c_to_ruby_string(/*const xmlChar **/ string, /*long*/ string_len) \
59
- rb_str_new(string, string_len)
60
-
61
- #define /*VALUE*/ rxml_writer_ruby_string_to_utf8(/*VALUE*/ string) \
62
- string
63
-
64
- #define /*void*/ rxml_writer_free_utf8_string(/*VALUE*/ orig, /*VALUE*/ utf8) \
65
- /* NOP */
66
-
67
- #endif /* HAVE_RUBY_ENCODING_H */
68
-
69
- static void rxml_writer_free(rxml_writer_object *rwo)
70
- {
71
- #if 0 /* seems to be done by xmlFreeTextWriter */
72
- if (NULL != rwo->buffer) {
73
- xmlBufferFree(rwo->buffer);
74
- }
75
- #endif
76
-
77
- rwo->closed = 1;
78
- xmlFreeTextWriter(rwo->writer);
79
- xfree(rwo);
80
- }
81
-
82
- static void rxml_writer_mark(rxml_writer_object *rwo)
83
- {
84
- if (!NIL_P(rwo->output)) {
85
- rb_gc_mark(rwo->output);
86
- }
87
- }
88
-
89
- static VALUE rxml_writer_wrap(rxml_writer_object *rwo)
90
- {
91
- return Data_Wrap_Struct(cXMLWriter, rxml_writer_mark, rxml_writer_free, rwo);
92
- }
93
-
94
- static rxml_writer_object *rxml_textwriter_get(VALUE obj)
95
- {
96
- rxml_writer_object *rwo;
97
-
98
- Data_Get_Struct(obj, rxml_writer_object, rwo);
99
-
100
- return rwo;
101
- }
102
-
103
- int rxml_writer_write_callback(void *context, const char *buffer, int len)
104
- {
105
- rxml_writer_object *rwo = context;
106
-
107
- if(rwo->closed){
108
- return 0;
109
- }else{
110
- return rxml_write_callback((void *)rwo->output, buffer, len);
111
- }
112
- }
113
-
114
- /* ===== public class methods ===== */
115
-
116
- /* call-seq:
117
- * XML::Writer::io(io) -> XML::Writer
118
- *
119
- * Creates a XML::Writer which will write XML directly into an IO object.
120
- */
121
- static VALUE rxml_writer_io(VALUE klass, VALUE io)
122
- {
123
- #if 0
124
- typedef int (*xmlOutputCloseCallback)(void * context);
125
- typedef int (*xmlOutputWriteCallback)(void * context, const char * buffer, int len);
126
-
127
- ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
128
-
129
- xmlOutputBufferPtr xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite, xmlOutputCloseCallback ioclose, void * ioctx, xmlCharEncodingHandlerPtr encoder)
130
-
131
- xmlCharEncodingHandlerPtr xmlFindCharEncodingHandler(const char * name);
132
- #endif
133
- xmlOutputBufferPtr out;
134
- rxml_writer_object *rwo;
135
-
136
- rwo = ALLOC(rxml_writer_object);
137
- rwo->output = io;
138
- rwo->buffer = NULL;
139
- rwo->closed = 0;
140
- #ifdef HAVE_RUBY_ENCODING_H
141
- rwo->encoding = NULL;
142
- #endif /* HAVE_RUBY_ENCODING_H */
143
- rwo->output_type = RXMLW_OUTPUT_IO;
144
- if (NULL == (out = xmlOutputBufferCreateIO(rxml_writer_write_callback, NULL, (void *) rwo, NULL))) {
145
- rxml_raise(&xmlLastError);
146
- }
147
- if (NULL == (rwo->writer = xmlNewTextWriter(out))) {
148
- rxml_raise(&xmlLastError);
149
- }
150
-
151
- return rxml_writer_wrap(rwo);
152
- }
153
-
154
-
155
- /* call-seq:
156
- * XML::Writer::file(path) -> XML::Writer
157
- *
158
- * Creates a XML::Writer object which will write XML into the file with
159
- * the given name.
160
- */
161
- static VALUE rxml_writer_file(VALUE klass, VALUE filename)
162
- {
163
- rxml_writer_object *rwo;
164
-
165
- rwo = ALLOC(rxml_writer_object);
166
- rwo->output = Qnil;
167
- rwo->buffer = NULL;
168
- rwo->closed = 0;
169
- #ifdef HAVE_RUBY_ENCODING_H
170
- rwo->encoding = NULL;
171
- #endif /* HAVE_RUBY_ENCODING_H */
172
- rwo->output_type = RXMLW_OUTPUT_NONE;
173
- if (NULL == (rwo->writer = xmlNewTextWriterFilename(StringValueCStr(filename), 0))) {
174
- rxml_raise(&xmlLastError);
175
- }
176
-
177
- return rxml_writer_wrap(rwo);
178
- }
179
-
180
- /* call-seq:
181
- * XML::Writer::string -> XML::Writer
182
- *
183
- * Creates a XML::Writer which will write XML into memory, as string.
184
- */
185
- static VALUE rxml_writer_string(VALUE klass)
186
- {
187
- rxml_writer_object *rwo;
188
-
189
- rwo = ALLOC(rxml_writer_object);
190
- rwo->output = Qnil;
191
- rwo->closed = 0;
192
- #ifdef HAVE_RUBY_ENCODING_H
193
- rwo->encoding = NULL;
194
- #endif /* HAVE_RUBY_ENCODING_H */
195
- rwo->output_type = RXMLW_OUTPUT_STRING;
196
- if (NULL == (rwo->buffer = xmlBufferCreate())) {
197
- rxml_raise(&xmlLastError);
198
- }
199
- if (NULL == (rwo->writer = xmlNewTextWriterMemory(rwo->buffer, 0))) {
200
- xmlBufferFree(rwo->buffer);
201
- rxml_raise(&xmlLastError);
202
- }
203
-
204
- return rxml_writer_wrap(rwo);
205
- }
206
-
207
- /* call-seq:
208
- * XML::Writer::document -> XML::Writer
209
- *
210
- * Creates a XML::Writer which will write into an in memory XML::Document
211
- */
212
- static VALUE rxml_writer_doc(VALUE klass)
213
- {
214
- xmlDocPtr doc;
215
- rxml_writer_object *rwo;
216
-
217
- rwo = ALLOC(rxml_writer_object);
218
- rwo->buffer = NULL;
219
- rwo->output = Qnil;
220
- rwo->closed = 0;
221
- #ifdef HAVE_RUBY_ENCODING_H
222
- rwo->encoding = NULL;
223
- #endif /* HAVE_RUBY_ENCODING_H */
224
- rwo->output_type = RXMLW_OUTPUT_DOC;
225
- if (NULL == (rwo->writer = xmlNewTextWriterDoc(&doc, 0))) {
226
- rxml_raise(&xmlLastError);
227
- }
228
- rwo->output = rxml_document_wrap(doc);
229
-
230
- return rxml_writer_wrap(rwo);
231
- }
232
-
233
- /* ===== public instance methods ===== */
234
-
235
- /* call-seq:
236
- * writer.flush(empty? = true) -> (num|string)
237
- *
238
- * Flushes the output buffer. Returns the number of written bytes or
239
- * the current content of the internal buffer for a in memory XML::Writer.
240
- * If +empty?+ is +true+, and for a in memory XML::Writer, this internel
241
- * buffer is empty.
242
- */
243
- static VALUE rxml_writer_flush(int argc, VALUE *argv, VALUE self)
244
- {
245
- int ret;
246
- VALUE empty;
247
- rxml_writer_object *rwo;
248
-
249
- rb_scan_args(argc, argv, "01", &empty);
250
-
251
- rwo = rxml_textwriter_get(self);
252
- if (-1 == (ret = xmlTextWriterFlush(rwo->writer))) {
253
- rxml_raise(&xmlLastError);
254
- }
255
-
256
- if (NULL != rwo->buffer) {
257
- VALUE content;
258
-
259
- #ifdef HAVE_RUBY_ENCODING_H
260
- content = rb_external_str_new_with_enc((const char*)rwo->buffer->content, rwo->buffer->use, rwo->encoding);
261
- #else
262
- content = rb_str_new(rwo->buffer->content, rwo->buffer->use);
263
- #endif /* HAVE_RUBY_ENCODING_H */
264
- if (NIL_P(empty) || RTEST(empty)) { /* nil = default value = true */
265
- xmlBufferEmpty(rwo->buffer);
266
- }
267
-
268
- return content;
269
- } else {
270
- return INT2NUM(ret);
271
- }
272
- }
273
-
274
- /* call-seq:
275
- * writer.result -> (XML::Document|"string"|nil)
276
- *
277
- * Returns the associated result object to the XML::Writer creation.
278
- * A String for a XML::Writer object created with XML::Writer::string,
279
- * a XML::Document with XML::Writer::document, etc.
280
- */
281
- static VALUE rxml_writer_result(VALUE self)
282
- {
283
- VALUE ret = Qnil;
284
- rxml_writer_object *rwo = rxml_textwriter_get(self);
285
- int bytesWritten = xmlTextWriterFlush(rwo->writer);
286
-
287
- if (bytesWritten == -1) {
288
- rxml_raise(&xmlLastError);
289
- }
290
-
291
- switch (rwo->output_type) {
292
- case RXMLW_OUTPUT_DOC:
293
- ret = rwo->output;
294
- break;
295
- case RXMLW_OUTPUT_STRING:
296
- ret = rxml_writer_c_to_ruby_string((const char*)rwo->buffer->content, rwo->buffer->use);
297
- break;
298
- case RXMLW_OUTPUT_IO:
299
- case RXMLW_OUTPUT_NONE:
300
- break;
301
- default:
302
- rb_bug("unexpected output");
303
- break;
304
- }
305
-
306
- return ret;
307
- }
308
-
309
- /* ===== private helpers ===== */
310
-
311
- static VALUE numeric_rxml_writer_void(VALUE obj, int (*fn)(xmlTextWriterPtr))
312
- {
313
- int ret;
314
- rxml_writer_object *rwo;
315
-
316
- rwo = rxml_textwriter_get(obj);
317
- ret = fn(rwo->writer);
318
-
319
- return (-1 == ret ? Qfalse : Qtrue);
320
- }
321
-
322
- #define numeric_rxml_writer_string(/*VALUE*/ obj, /*VALUE*/ name_or_content, /*int (**/fn/*)(xmlTextWriterPtr, const xmlChar *)*/) \
323
- numeric_rxml_writer_va_strings(obj, Qundef, 1, fn, name_or_content)
324
-
325
- /**
326
- * This is quite ugly but thanks to libxml2 coding style, all xmlTextWriter*
327
- * calls can be redirected to a single function. This can be convenient to:
328
- * - avoid repeating yourself
329
- * - convert strings to UTF-8
330
- * - validate names
331
- * and so on
332
- **/
333
- #define XMLWRITER_MAX_STRING_ARGS 5
334
- static VALUE numeric_rxml_writer_va_strings(VALUE obj, VALUE pe, size_t strings_count, int (*fn)(ANYARGS), ...)
335
- {
336
- va_list ap;
337
- size_t argc;
338
- int ret = -1;
339
- rxml_writer_object *rwo;
340
- const xmlChar *argv[XMLWRITER_MAX_STRING_ARGS];
341
- VALUE utf8[XMLWRITER_MAX_STRING_ARGS], orig[XMLWRITER_MAX_STRING_ARGS];
342
-
343
- if (strings_count > XMLWRITER_MAX_STRING_ARGS) {
344
- rb_bug("more arguments than expected");
345
- }
346
- va_start(ap, fn);
347
- rwo = rxml_textwriter_get(obj);
348
- for (argc = 0; argc < strings_count; argc++) {
349
- VALUE arg;
350
-
351
- arg = va_arg(ap, VALUE);
352
- orig[argc] = arg;
353
- if (NIL_P(arg)) {
354
- utf8[argc] = Qnil;
355
- argv[argc] = NULL;
356
- } else {
357
- utf8[argc] = rxml_writer_ruby_string_to_utf8(orig[argc]);
358
- argv[argc] = BAD_CAST StringValueCStr(utf8[argc]);
359
- }
360
- }
361
- va_end(ap);
362
-
363
- if (Qundef == pe) {
364
- switch (strings_count) {
365
- case 0:
366
- ret = fn(rwo->writer);
367
- break;
368
- case 1:
369
- ret = fn(rwo->writer, argv[0]);
370
- break;
371
- case 2:
372
- ret = fn(rwo->writer, argv[0], argv[1]);
373
- break;
374
- case 3:
375
- ret = fn(rwo->writer, argv[0], argv[1], argv[2]);
376
- break;
377
- case 4:
378
- ret = fn(rwo->writer, argv[0], argv[1], argv[2], argv[3]);
379
- break;
380
- case 5:
381
- ret = fn(rwo->writer, argv[0], argv[1], argv[2], argv[3], argv[4]);
382
- break;
383
- default:
384
- break;
385
- }
386
- } else {
387
- int xpe;
388
-
389
- xpe = RTEST(pe);
390
- switch (strings_count) { /* strings_count doesn't include pe */
391
- case 0:
392
- ret = fn(rwo->writer, xpe);
393
- break;
394
- case 1:
395
- ret = fn(rwo->writer, xpe, argv[0]);
396
- break;
397
- case 2:
398
- ret = fn(rwo->writer, xpe, argv[0], argv[1]);
399
- break;
400
- case 3:
401
- ret = fn(rwo->writer, xpe, argv[0], argv[1], argv[2]);
402
- break;
403
- case 4:
404
- ret = fn(rwo->writer, xpe, argv[0], argv[1], argv[2], argv[3]);
405
- break;
406
- case 5:
407
- ret = fn(rwo->writer, xpe, argv[0], argv[1], argv[2], argv[3], argv[4]);
408
- break;
409
- default:
410
- break;
411
- }
412
- }
413
- #ifdef HAVE_RUBY_ENCODING_H
414
- while (--strings_count > 0) {
415
- if (!NIL_P(orig[strings_count])) {
416
- rxml_writer_free_utf8_string(orig[strings_count], utf8[strings_count]);
417
- }
418
- }
419
- #endif /* HAVE_RUBY_ENCODING_H */
420
-
421
- return (-1 == ret ? Qfalse : Qtrue);
422
- }
423
-
424
- /* ===== public instance methods ===== */
425
-
426
- #if LIBXML_VERSION >= 20605
427
- /* call-seq:
428
- * writer.set_indent(indentation) -> (true|false)
429
- *
430
- * Toggles indentation on or off. Returns +false+ on failure.
431
- *
432
- * Availability: libxml2 >= 2.6.5
433
- */
434
- static VALUE rxml_writer_set_indent(VALUE self, VALUE indentation)
435
- {
436
- int ret;
437
- rxml_writer_object *rwo;
438
-
439
- rwo = rxml_textwriter_get(self);
440
- ret = xmlTextWriterSetIndent(rwo->writer, RTEST(indentation));
441
-
442
- return (-1 == ret ? Qfalse : Qtrue);
443
- }
444
-
445
- /* call-seq:
446
- * writer.set_indent_string(string) -> (true|false)
447
- *
448
- * Sets the string to use to indent each element of the document.
449
- * Don't forget to enable indentation with set_indent. Returns
450
- * +false+ on failure.
451
- *
452
- * Availability: libxml2 >= 2.6.5
453
- */
454
- static VALUE rxml_writer_set_indent_string(VALUE self, VALUE indentation)
455
- {
456
- return numeric_rxml_writer_string(self, indentation, xmlTextWriterSetIndentString);
457
- }
458
- #endif /* LIBXML_VERSION >= 20605 */
459
-
460
- /* ===== public full tag interface ===== */
461
-
462
- /* write_<X> = start_<X> + write_string + end_<X> */
463
-
464
- /* call-seq:
465
- * writer.write_comment(content) -> (true|false)
466
- *
467
- * Writes a full comment tag, all at once. Returns +false+ on failure.
468
- * This is equivalent to start_comment + write_string(content) + end_comment.
469
- */
470
- static VALUE rxml_writer_write_comment(VALUE self, VALUE content)
471
- {
472
- return numeric_rxml_writer_string(self, content, xmlTextWriterWriteComment);
473
- }
474
-
475
- /* call-seq:
476
- * writer.write_cdata(content) -> (true|false)
477
- *
478
- * Writes a full CDATA section, all at once. Returns +false+ on failure.
479
- * This is equivalent to start_cdata + write_string(content) + end_cdata.
480
- */
481
- static VALUE rxml_writer_write_cdata(VALUE self, VALUE content)
482
- {
483
- return numeric_rxml_writer_string(self, content, xmlTextWriterWriteCDATA);
484
- }
485
-
486
- static VALUE rxml_writer_start_element(VALUE, VALUE);
487
- static VALUE rxml_writer_start_element_ns(int, VALUE *, VALUE);
488
- static VALUE rxml_writer_end_element(VALUE);
489
-
490
- /* call-seq:
491
- * writer.write_element(name, content) -> (true|false)
492
- *
493
- * Writes a full element tag, all at once. Returns +false+ on failure.
494
- * This is equivalent to start_element(name) + write_string(content) +
495
- * end_element.
496
- */
497
- static VALUE rxml_writer_write_element(int argc, VALUE *argv, VALUE self)
498
- {
499
- VALUE name, content;
500
-
501
- rb_scan_args(argc, argv, "11", &name, &content);
502
- if (Qnil == content) {
503
- if (Qfalse == rxml_writer_start_element(self, name)) {
504
- return Qfalse;
505
- }
506
- return rxml_writer_end_element(self);
507
- } else {
508
- return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteElement, name, content);
509
- }
510
- }
511
-
512
- #define ARRAY_SIZE(array) \
513
- (sizeof(array) / sizeof((array)[0]))
514
-
515
- /* call-seq:
516
- * writer.write_element_ns(prefix, name, namespaceURI, content) -> (true|false)
517
- *
518
- * Writes a full namespaced element tag, all at once. Returns +false+ on failure.
519
- * This is a shortcut for start_element_ns(prefix, name, namespaceURI) +
520
- * write_string(content) + end_element.
521
- *
522
- * Notes:
523
- * - by default, the xmlns: definition is repeated on every element. If you want
524
- * the prefix, but don't want the xmlns: declaration repeated, set +namespaceURI+
525
- * to nil or omit it. Don't forget to declare the namespace prefix somewhere
526
- * earlier.
527
- * - +content+ can be omitted for an empty tag
528
- */
529
- static VALUE rxml_writer_write_element_ns(int argc, VALUE *argv, VALUE self)
530
- {
531
- VALUE prefix, name, namespaceURI, content;
532
-
533
- rb_scan_args(argc, argv, "22", &prefix, &name, &namespaceURI, &content);
534
- if (Qnil == content) {
535
- VALUE argv[3] = { prefix, name, namespaceURI };
536
-
537
- if (Qfalse == rxml_writer_start_element_ns(ARRAY_SIZE(argv), argv, self)) {
538
- return Qfalse;
539
- }
540
- return rxml_writer_end_element(self);
541
- } else {
542
- return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteElementNS, prefix, name, namespaceURI, content);
543
- }
544
- }
545
-
546
- /* call-seq:
547
- * writer.write_attribute(name, content) -> (true|false)
548
- *
549
- * Writes a full attribute, all at once. Returns +false+ on failure.
550
- * Same as start_attribute(name) + write_string(content) + end_attribute.
551
- */
552
- static VALUE rxml_writer_write_attribute(VALUE self, VALUE name, VALUE content)
553
- {
554
- return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteAttribute, name, content);
555
- }
556
-
557
- /* call-seq:
558
- * writer.write_attribute_ns(prefix, name, namespaceURI, content) -> (true|false)
559
- *
560
- * Writes a full namespaced attribute, all at once. Returns +false+ on failure.
561
- * Same as start_attribute_ns(prefix, name, namespaceURI) +
562
- * write_string(content) + end_attribute.
563
- *
564
- * Notes:
565
- * - by default, the xmlns: definition is repeated on every element. If you want
566
- * the prefix, but don't want the xmlns: declaration repeated, set +namespaceURI+
567
- * to nil or omit it. Don't forget to declare the namespace prefix somewhere
568
- * earlier.
569
- * - +content+ can be omitted too for an empty attribute
570
- */
571
- static VALUE rxml_writer_write_attribute_ns(int argc, VALUE *argv, VALUE self)
572
- {
573
- VALUE prefix, name, namespaceURI, content;
574
-
575
- rb_scan_args(argc, argv, "22", &prefix, &name, &namespaceURI, &content);
576
-
577
- return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteAttributeNS, prefix, name, namespaceURI, content);
578
- }
579
-
580
- /* call-seq:
581
- * writer.write_pi(target, content) -> (true|false)
582
- *
583
- * Writes a full CDATA tag, all at once. Returns +false+ on failure.
584
- * This is a shortcut for start_pi(target) + write_string(content) + end_pi.
585
- */
586
- static VALUE rxml_writer_write_pi(VALUE self, VALUE target, VALUE content)
587
- {
588
- return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWritePI, target, content);
589
- }
590
-
591
- /* ===== public start/end interface ===== */
592
-
593
- /* call-seq:
594
- * writer.write_string(content) -> (true|false)
595
- *
596
- * Safely (problematic characters are internally translated to their
597
- * associated named entities) writes a string into the current node
598
- * (attribute, element, comment, ...). Returns +false+ on failure.
599
- */
600
- static VALUE rxml_writer_write_string(VALUE self, VALUE content)
601
- {
602
- return numeric_rxml_writer_string(self, content, xmlTextWriterWriteString);
603
- }
604
-
605
- /* call-seq:
606
- * writer.write_raw(content) -> (true|false)
607
- *
608
- * Writes the string +content+ as is, reserved characters are not
609
- * translated to their associated entities. Returns +false+ on failure.
610
- * Consider write_string to handle them.
611
- */
612
- static VALUE rxml_writer_write_raw(VALUE self, VALUE content)
613
- {
614
- return numeric_rxml_writer_string(self, content, xmlTextWriterWriteRaw);
615
- }
616
-
617
- /* call-seq:
618
- * writer.start_attribute(name) -> (true|false)
619
- *
620
- * Starts an attribute. Returns +false+ on failure.
621
- */
622
- static VALUE rxml_writer_start_attribute(VALUE self, VALUE name)
623
- {
624
- return numeric_rxml_writer_string(self, name, xmlTextWriterStartAttribute);
625
- }
626
-
627
- /* call-seq:
628
- * writer.start_attribute_ns(prefix, name, namespaceURI) -> (true|false)
629
- *
630
- * Starts a namespaced attribute. Returns +false+ on failure.
631
- *
632
- * Note: by default, the xmlns: definition is repeated on every element. If
633
- * you want the prefix, but don't want the xmlns: declaration repeated, set
634
- * +namespaceURI+ to nil or omit it. Don't forget to declare the namespace
635
- * prefix somewhere earlier.
636
- */
637
- static VALUE rxml_writer_start_attribute_ns(int argc, VALUE *argv, VALUE self)
638
- {
639
- VALUE prefix, name, namespaceURI;
640
-
641
- rb_scan_args(argc, argv, "21", &prefix, &name, &namespaceURI);
642
-
643
- return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartAttributeNS, prefix, name, namespaceURI);
644
- }
645
-
646
- /* call-seq:
647
- * writer.end_attribute -> (true|false)
648
- *
649
- * Ends an attribute, namespaced or not. Returns +false+ on failure.
650
- */
651
- static VALUE rxml_writer_end_attribute(VALUE self)
652
- {
653
- return numeric_rxml_writer_void(self, xmlTextWriterEndAttribute);
654
- }
655
-
656
- #if LIBXML_VERSION >= 20607
657
- /* call-seq:
658
- * writer.start_comment -> (true|false)
659
- *
660
- * Starts a comment. Returns +false+ on failure.
661
- * Note: libxml2 >= 2.6.7 required
662
- */
663
- static VALUE rxml_writer_start_comment(VALUE self)
664
- {
665
- return numeric_rxml_writer_void(self, xmlTextWriterStartComment);
666
- }
667
-
668
- /* call-seq:
669
- * writer.end_comment -> (true|false)
670
- *
671
- * Ends current comment, returns +false+ on failure.
672
- * Note: libxml2 >= 2.6.7 required
673
- */
674
- static VALUE rxml_writer_end_comment(VALUE self)
675
- {
676
- return numeric_rxml_writer_void(self, xmlTextWriterEndComment);
677
- }
678
- #endif /* LIBXML_VERSION >= 20607 */
679
-
680
- /* call-seq:
681
- * writer.start_element(name) -> (true|false)
682
- *
683
- * Starts a new element. Returns +false+ on failure.
684
- */
685
- static VALUE rxml_writer_start_element(VALUE self, VALUE name)
686
- {
687
- return numeric_rxml_writer_string(self, name, xmlTextWriterStartElement);
688
- }
689
-
690
- /* call-seq:
691
- * writer.start_element_ns(prefix, name, namespaceURI) -> (true|false)
692
- *
693
- * Starts a new namespaced element. Returns +false+ on failure.
694
- *
695
- * Note: by default, the xmlns: definition is repeated on every element. If
696
- * you want the prefix, but don't want the xmlns: declaration repeated, set
697
- * +namespaceURI+ to nil or omit it. Don't forget to declare the namespace
698
- * prefix somewhere earlier.
699
- */
700
- static VALUE rxml_writer_start_element_ns(int argc, VALUE *argv, VALUE self)
701
- {
702
- VALUE prefix, name, namespaceURI;
703
-
704
- rb_scan_args(argc, argv, "21", &prefix, &name, &namespaceURI);
705
-
706
- return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartElementNS, prefix, name, namespaceURI);
707
- }
708
-
709
- /* call-seq:
710
- * writer.end_element -> (true|false)
711
- *
712
- * Ends current element, namespaced or not. Returns +false+ on failure.
713
- */
714
- static VALUE rxml_writer_end_element(VALUE self)
715
- {
716
- return numeric_rxml_writer_void(self, xmlTextWriterEndElement);
717
- }
718
-
719
- /* call-seq:
720
- * writer.write_full_end_element -> (true|false)
721
- *
722
- * Ends current element, namespaced or not. Returns +false+ on failure.
723
- * This method writes an end tag even if the element is empty (<foo></foo>),
724
- * end_element does not (<foo/>).
725
- */
726
- static VALUE rxml_writer_full_end_element(VALUE self)
727
- {
728
- return numeric_rxml_writer_void(self, xmlTextWriterFullEndElement);
729
- }
730
-
731
- /* call-seq:
732
- * writer.start_cdata -> (true|false)
733
- *
734
- * Starts a new CDATA section. Returns +false+ on failure.
735
- */
736
- static VALUE rxml_writer_start_cdata(VALUE self)
737
- {
738
- return numeric_rxml_writer_void(self, xmlTextWriterStartCDATA);
739
- }
740
-
741
- /* call-seq:
742
- * writer.end_cdata -> (true|false)
743
- *
744
- * Ends current CDATA section. Returns +false+ on failure.
745
- */
746
- static VALUE rxml_writer_end_cdata(VALUE self)
747
- {
748
- return numeric_rxml_writer_void(self, xmlTextWriterEndCDATA);
749
- }
750
-
751
- /* call-seq:
752
- * writer.start_document -> (true|false)
753
- * writer.start_document(:encoding => XML::Encoding::UTF_8,
754
- * :standalone => true) -> (true|false)
755
- *
756
- * Starts a new document. Returns +false+ on failure.
757
- *
758
- * You may provide an optional hash table to control XML header that will be
759
- * generated. Valid options are:
760
- * - encoding: the output document encoding, defaults to nil (= UTF-8). Valid
761
- * values are the encoding constants defined on XML::Encoding
762
- * - standalone: nil (default) or a boolean to indicate if the document is
763
- * standalone or not
764
- */
765
- static VALUE rxml_writer_start_document(int argc, VALUE *argv, VALUE self)
766
- {
767
- int ret;
768
- VALUE options = Qnil;
769
- rxml_writer_object *rwo;
770
- const xmlChar *xencoding = NULL;
771
- const char *xstandalone = NULL;
772
-
773
- rb_scan_args(argc, argv, "01", &options);
774
- if (!NIL_P(options)) {
775
- VALUE encoding, standalone;
776
-
777
- encoding = standalone = Qnil;
778
- Check_Type(options, T_HASH);
779
- encoding = rb_hash_aref(options, sEncoding);
780
- xencoding = NIL_P(encoding) ? NULL : (const xmlChar*)xmlGetCharEncodingName(NUM2INT(encoding));
781
- standalone = rb_hash_aref(options, sStandalone);
782
- if (NIL_P(standalone)) {
783
- xstandalone = NULL;
784
- } else {
785
- xstandalone = RTEST(standalone) ? "yes" : "no";
786
- }
787
- }
788
- rwo = rxml_textwriter_get(self);
789
- #ifdef HAVE_RUBY_ENCODING_H
790
- rwo->encoding = rxml_figure_encoding(xencoding);
791
- #endif /* !HAVE_RUBY_ENCODING_H */
792
- ret = xmlTextWriterStartDocument(rwo->writer, NULL, (const char*)xencoding, xstandalone);
793
-
794
- return (-1 == ret ? Qfalse : Qtrue);
795
- }
796
-
797
- /* call-seq:
798
- * writer.end_document -> (true|false)
799
- *
800
- * Ends current document. Returns +false+ on failure.
801
- */
802
- static VALUE rxml_writer_end_document(VALUE self)
803
- {
804
- return numeric_rxml_writer_void(self, xmlTextWriterEndDocument);
805
- }
806
-
807
- /* call-seq:
808
- * writer.start_pi(target) -> (true|false)
809
- *
810
- * Starts a new processing instruction. Returns +false+ on failure.
811
- */
812
- static VALUE rxml_writer_start_pi(VALUE self, VALUE target)
813
- {
814
- return numeric_rxml_writer_string(self, target, xmlTextWriterStartPI);
815
- }
816
-
817
- /* call-seq:
818
- * writer.end_pi -> (true|false)
819
- *
820
- * Ends current processing instruction. Returns +false+ on failure.
821
- */
822
- static VALUE rxml_writer_end_pi(VALUE self)
823
- {
824
- return numeric_rxml_writer_void(self, xmlTextWriterEndPI);
825
- }
826
-
827
- /* call-seq:
828
- * writer.start_dtd(qualifiedName, publicId, systemId) -> (true|false)
829
- *
830
- * Starts a DTD. Returns +false+ on failure.
831
- */
832
- static VALUE rxml_writer_start_dtd(int argc, VALUE *argv, VALUE self)
833
- {
834
- VALUE name, pubid, sysid;
835
-
836
- rb_scan_args(argc, argv, "12", &name, &pubid, &sysid);
837
-
838
- return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartDTD, name, pubid, sysid);
839
- }
840
-
841
- /* call-seq:
842
- * writer.start_dtd_element(qualifiedName) -> (true|false)
843
- *
844
- * Starts a DTD element (<!ELEMENT ... >). Returns +false+ on failure.
845
- */
846
- static VALUE rxml_writer_start_dtd_element(VALUE self, VALUE name)
847
- {
848
- return numeric_rxml_writer_string(self, name, xmlTextWriterStartDTDElement);
849
- }
850
-
851
- /* call-seq:
852
- * writer.start_dtd_entity(name, pe = false) -> (true|false)
853
- *
854
- * Starts a DTD entity (<!ENTITY ... >). Returns +false+ on failure.
855
- */
856
- static VALUE rxml_writer_start_dtd_entity(int argc, VALUE *argv, VALUE self)
857
- {
858
- VALUE name, pe;
859
-
860
- rb_scan_args(argc, argv, "11", &name, &pe);
861
- if (NIL_P(pe)) {
862
- pe = Qfalse;
863
- }
864
-
865
- return numeric_rxml_writer_va_strings(self, pe, 1, xmlTextWriterStartDTDEntity, name);
866
- }
867
-
868
- /* call-seq:
869
- * writer.start_dtd_attlist(name) -> (true|false)
870
- *
871
- * Starts a DTD attribute list (<!ATTLIST ... >). Returns +false+ on failure.
872
- */
873
- static VALUE rxml_writer_start_dtd_attlist(VALUE self, VALUE name)
874
- {
875
- return numeric_rxml_writer_string(self, name, xmlTextWriterStartDTDAttlist);
876
- }
877
-
878
- /* call-seq:
879
- * writer.end_dtd -> (true|false)
880
- *
881
- * Ends current DTD, returns +false+ on failure.
882
- */
883
- static VALUE rxml_writer_end_dtd(VALUE self)
884
- {
885
- return numeric_rxml_writer_void(self, xmlTextWriterEndDTD);
886
- }
887
-
888
- /* call-seq:
889
- * writer.end_dtd_entity -> (true|false)
890
- *
891
- * Ends current DTD entity, returns +false+ on failure.
892
- */
893
- static VALUE rxml_writer_end_dtd_entity(VALUE self)
894
- {
895
- return numeric_rxml_writer_void(self, xmlTextWriterEndDTDEntity);
896
- }
897
-
898
- /* call-seq:
899
- * writer.end_dtd_attlist -> (true|false)
900
- *
901
- * Ends current DTD attribute list, returns +false+ on failure.
902
- */
903
- static VALUE rxml_writer_end_dtd_attlist(VALUE self)
904
- {
905
- return numeric_rxml_writer_void(self, xmlTextWriterEndDTDAttlist);
906
- }
907
-
908
- /* call-seq:
909
- * writer.end_dtd_element -> (true|false)
910
- *
911
- * Ends current DTD element, returns +false+ on failure.
912
- */
913
- static VALUE rxml_writer_end_dtd_element(VALUE self)
914
- {
915
- return numeric_rxml_writer_void(self, xmlTextWriterEndDTDElement);
916
- }
917
-
918
- /* call-seq:
919
- * writer.write_dtd(name [ [ [, publicId ], systemId ], subset ]) -> (true|false)
920
- *
921
- * Writes a DTD, all at once. Returns +false+ on failure.
922
- * - name: dtd name
923
- * - publicId: external subset public identifier, use nil for a SYSTEM doctype
924
- * - systemId: external subset system identifier
925
- * - subset: content
926
- *
927
- * Examples:
928
- * writer.write_dtd 'html'
929
- * #=> <!DOCTYPE html>
930
- * writer.write_dtd 'docbook', nil, 'http://www.docbook.org/xml/5.0/dtd/docbook.dtd'
931
- * #=> <!DOCTYPE docbook SYSTEM "http://www.docbook.org/xml/5.0/dtd/docbook.dtd">
932
- * writer.write_dtd 'html', '-//W3C//DTD XHTML 1.1//EN', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
933
- * #=> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
934
- * writer.write_dtd 'person', nil, nil, '<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>'
935
- * #=> <!DOCTYPE person [<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>]>
936
- */
937
- static VALUE rxml_writer_write_dtd(int argc, VALUE *argv, VALUE self)
938
- {
939
- VALUE name, pubid, sysid, subset;
940
-
941
- rb_scan_args(argc, argv, "13", &name, &pubid, &sysid, &subset);
942
-
943
- return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteDTD, name, pubid, sysid, subset);
944
- }
945
-
946
- /* call-seq:
947
- * writer.write_dtd_attlist(name, content) -> (true|false)
948
- *
949
- * Writes a DTD attribute list, all at once. Returns +false+ on failure.
950
- * writer.write_dtd_attlist 'id', 'ID #IMPLIED'
951
- * #=> <!ATTLIST id ID #IMPLIED>
952
- */
953
- static VALUE rxml_writer_write_dtd_attlist(VALUE self, VALUE name, VALUE content)
954
- {
955
- return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteDTDAttlist, name, content);
956
- }
957
-
958
- /* call-seq:
959
- * writer.write_dtd_element(name, content) -> (true|false)
960
- *
961
- * Writes a full DTD element, all at once. Returns +false+ on failure.
962
- * writer.write_dtd_element 'person', '(firstname,lastname)'
963
- * #=> <!ELEMENT person (firstname,lastname)>
964
- */
965
- static VALUE rxml_writer_write_dtd_element(VALUE self, VALUE name, VALUE content)
966
- {
967
- return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteDTDElement, name, content);
968
- }
969
-
970
- /* call-seq:
971
- * writer.write_dtd_entity(name, publicId, systemId, ndataid, content, pe) -> (true|false)
972
- *
973
- * Writes a DTD entity, all at once. Returns +false+ on failure.
974
- */
975
- static VALUE rxml_writer_write_dtd_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE content, VALUE pe)
976
- {
977
- return numeric_rxml_writer_va_strings(self, pe, 5, xmlTextWriterWriteDTDEntity, name, pubid, sysid, ndataid, content);
978
- }
979
-
980
- /* call-seq:
981
- * writer.write_dtd_external_entity(name, publicId, systemId, ndataid, pe) -> (true|false)
982
- *
983
- * Writes a DTD external entity. The entity must have been started
984
- * with start_dtd_entity. Returns +false+ on failure.
985
- * - name: the name of the DTD entity
986
- * - publicId: the public identifier, which is an alternative to the system identifier
987
- * - systemId: the system identifier, which is the URI of the DTD
988
- * - ndataid: the xml notation name
989
- * - pe: +true+ if this is a parameter entity (to be used only in the DTD
990
- * itself), +false+ if not
991
- */
992
- static VALUE rxml_writer_write_dtd_external_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE pe)
993
- {
994
- return numeric_rxml_writer_va_strings(self, pe, 4, xmlTextWriterWriteDTDExternalEntity, name, pubid, sysid, ndataid);
995
- }
996
-
997
- /* call-seq:
998
- * writer.write_dtd_external_entity_contents(publicId, systemId, ndataid) -> (true|false)
999
- *
1000
- * Writes the contents of a DTD external entity, all at once. Returns +false+ on failure.
1001
- */
1002
- static VALUE rxml_writer_write_dtd_external_entity_contents(VALUE self, VALUE pubid, VALUE sysid, VALUE ndataid)
1003
- {
1004
- return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterWriteDTDExternalEntityContents, pubid, sysid, ndataid);
1005
- }
1006
-
1007
- /* call-seq:
1008
- * writer.write_dtd_internal_entity(name, content, pe) -> (true|false)
1009
- *
1010
- * Writes a DTD internal entity, all at once. Returns +false+ on failure.
1011
- *
1012
- * Examples:
1013
- * writer.write_dtd_entity 'Shape', '(rect|circle|poly|default)', true
1014
- * #=> <!ENTITY % Shape "(rect|circle|poly|default)">
1015
- * writer.write_dtd_entity 'delta', '&#948;', false
1016
- * #=> <!ENTITY delta "&#948;">
1017
- */
1018
- static VALUE rxml_writer_write_dtd_internal_entity(VALUE self, VALUE name, VALUE content, VALUE pe)
1019
- {
1020
- return numeric_rxml_writer_va_strings(self, pe, 2, xmlTextWriterWriteDTDInternalEntity, name, content);
1021
- }
1022
-
1023
- /* call-seq:
1024
- * writer.write_dtd_notation(name, publicId, systemId) -> (true|false)
1025
- *
1026
- * Writes a DTD entity, all at once. Returns +false+ on failure.
1027
- */
1028
- static VALUE rxml_writer_write_dtd_notation(VALUE self, VALUE name, VALUE pubid, VALUE sysid)
1029
- {
1030
- return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterWriteDTDNotation, name, pubid, sysid);
1031
- }
1032
-
1033
- #if LIBXML_VERSION >= 20900
1034
- /* call-seq:
1035
- * writer.set_quote_char(...) -> (true|false)
1036
- *
1037
- * Sets the character used to quote attributes. Returns +false+ on failure.
1038
- *
1039
- * Notes:
1040
- * - only " (default) and ' characters are valid
1041
- * - availability: libxml2 >= 2.9.0
1042
- */
1043
- static VALUE rxml_writer_set_quote_char(VALUE self, VALUE quotechar)
1044
- {
1045
- int ret;
1046
- const char *xquotechar;
1047
- rxml_writer_object *rwo;
1048
-
1049
- rwo = rxml_textwriter_get(self);
1050
- xquotechar = StringValueCStr(quotechar);
1051
- ret = xmlTextWriterSetQuoteChar(rwo->writer, (xmlChar) xquotechar[0]);
1052
-
1053
- return (-1 == ret ? Qfalse : Qtrue);
1054
- }
1055
- #endif /* LIBXML_VERSION >= 20900 */
1056
-
1057
- #endif /* LIBXML_WRITER_ENABLED */
1058
-
1059
-
1060
- /* grep -P 'xmlTextWriter(Start|End|Write)(?!DTD|V?Format)[^(]+' /usr/include/libxml2/libxml/xmlwriter.h */
1061
- void rxml_init_writer(void)
1062
- {
1063
- sEncoding = ID2SYM(rb_intern("encoding"));
1064
- sStandalone = ID2SYM(rb_intern("standalone"));
1065
-
1066
- cXMLWriter = rb_define_class_under(mXML, "Writer", rb_cObject);
1067
-
1068
- #ifdef LIBXML_WRITER_ENABLED
1069
- rb_define_singleton_method(cXMLWriter, "io", rxml_writer_io, 1);
1070
- rb_define_singleton_method(cXMLWriter, "file", rxml_writer_file, 1);
1071
- rb_define_singleton_method(cXMLWriter, "document", rxml_writer_doc, 0);
1072
- rb_define_singleton_method(cXMLWriter, "string", rxml_writer_string, 0);
1073
-
1074
- /* misc */
1075
- #if LIBXML_VERSION >= 20605
1076
- rb_define_method(cXMLWriter, "set_indent", rxml_writer_set_indent, 1);
1077
- rb_define_method(cXMLWriter, "set_indent_string", rxml_writer_set_indent_string, 1);
1078
- #endif /* LIBXML_VERSION >= 20605 */
1079
- #if LIBXML_VERSION >= 20900
1080
- rb_define_method(cXMLWriter, "set_quote_char", rxml_writer_set_quote_char, 1);
1081
- #endif /* LIBXML_VERSION >= 20900 */
1082
- rb_define_method(cXMLWriter, "flush", rxml_writer_flush, -1);
1083
- rb_define_method(cXMLWriter, "start_dtd", rxml_writer_start_dtd, -1);
1084
- rb_define_method(cXMLWriter, "start_dtd_entity", rxml_writer_start_dtd_entity, -1);
1085
- rb_define_method(cXMLWriter, "start_dtd_attlist", rxml_writer_start_dtd_attlist, 1);
1086
- rb_define_method(cXMLWriter, "start_dtd_element", rxml_writer_start_dtd_element, 1);
1087
- rb_define_method(cXMLWriter, "write_dtd", rxml_writer_write_dtd, -1);
1088
- rb_define_method(cXMLWriter, "write_dtd_attlist", rxml_writer_write_dtd_attlist, 2);
1089
- rb_define_method(cXMLWriter, "write_dtd_element", rxml_writer_write_dtd_element, 2);
1090
- rb_define_method(cXMLWriter, "write_dtd_entity", rxml_writer_write_dtd_entity, 6);
1091
- rb_define_method(cXMLWriter, "write_dtd_external_entity", rxml_writer_write_dtd_external_entity, 5);
1092
- rb_define_method(cXMLWriter, "write_dtd_external_entity_contents", rxml_writer_write_dtd_external_entity_contents, 3);
1093
- rb_define_method(cXMLWriter, "write_dtd_internal_entity", rxml_writer_write_dtd_internal_entity, 3);
1094
- rb_define_method(cXMLWriter, "write_dtd_notation", rxml_writer_write_dtd_notation, 3);
1095
- rb_define_method(cXMLWriter, "end_dtd", rxml_writer_end_dtd, 0);
1096
- rb_define_method(cXMLWriter, "end_dtd_entity", rxml_writer_end_dtd_entity, 0);
1097
- rb_define_method(cXMLWriter, "end_dtd_attlist", rxml_writer_end_dtd_attlist, 0);
1098
- rb_define_method(cXMLWriter, "end_dtd_element", rxml_writer_end_dtd_element, 0);
1099
-
1100
- /* tag by parts */
1101
- rb_define_method(cXMLWriter, "write_raw", rxml_writer_write_raw, 1);
1102
- rb_define_method(cXMLWriter, "write_string", rxml_writer_write_string, 1);
1103
-
1104
- rb_define_method(cXMLWriter, "start_cdata", rxml_writer_start_cdata, 0);
1105
- rb_define_method(cXMLWriter, "end_cdata", rxml_writer_end_cdata, 0);
1106
- rb_define_method(cXMLWriter, "start_attribute", rxml_writer_start_attribute, 1);
1107
- rb_define_method(cXMLWriter, "start_attribute_ns", rxml_writer_start_attribute_ns, -1);
1108
- rb_define_method(cXMLWriter, "end_attribute", rxml_writer_end_attribute, 0);
1109
- rb_define_method(cXMLWriter, "start_element", rxml_writer_start_element, 1);
1110
- rb_define_method(cXMLWriter, "start_element_ns", rxml_writer_start_element_ns, -1);
1111
- rb_define_method(cXMLWriter, "end_element", rxml_writer_end_element, 0);
1112
- rb_define_method(cXMLWriter, "full_end_element", rxml_writer_full_end_element, 0);
1113
- rb_define_method(cXMLWriter, "start_document", rxml_writer_start_document, -1);
1114
- rb_define_method(cXMLWriter, "end_document", rxml_writer_end_document, 0);
1115
- #if LIBXML_VERSION >= 20607
1116
- rb_define_method(cXMLWriter, "start_comment", rxml_writer_start_comment, 0);
1117
- rb_define_method(cXMLWriter, "end_comment", rxml_writer_end_comment, 0);
1118
- #endif /* LIBXML_VERSION >= 20607 */
1119
- rb_define_method(cXMLWriter, "start_pi", rxml_writer_start_pi, 1);
1120
- rb_define_method(cXMLWriter, "end_pi", rxml_writer_end_pi, 0);
1121
-
1122
- /* full tag at once */
1123
- rb_define_method(cXMLWriter, "write_attribute", rxml_writer_write_attribute, 2);
1124
- rb_define_method(cXMLWriter, "write_attribute_ns", rxml_writer_write_attribute_ns, -1);
1125
- rb_define_method(cXMLWriter, "write_comment", rxml_writer_write_comment, 1);
1126
- rb_define_method(cXMLWriter, "write_cdata", rxml_writer_write_cdata, 1);
1127
- rb_define_method(cXMLWriter, "write_element", rxml_writer_write_element, -1);
1128
- rb_define_method(cXMLWriter, "write_element_ns", rxml_writer_write_element_ns, -1);
1129
- rb_define_method(cXMLWriter, "write_pi", rxml_writer_write_pi, 2);
1130
-
1131
- rb_define_method(cXMLWriter, "result", rxml_writer_result, 0);
1132
-
1133
- rb_undef_method(CLASS_OF(cXMLWriter), "new");
1134
- #endif
1135
- }
1136
-
1
+ #include "ruby_libxml.h"
2
+ #include "ruby_xml_writer.h"
3
+
4
+ VALUE cXMLWriter;
5
+ static VALUE sEncoding, sStandalone;
6
+
7
+ #ifdef LIBXML_WRITER_ENABLED
8
+
9
+ /*
10
+ * Document-class: LibXML::XML::Writer
11
+ *
12
+ * The XML::Writer class provides a simpler, alternative way to build a valid
13
+ * XML document from scratch (forward-only) compared to a DOM approach (based
14
+ * on XML::Document class).
15
+ *
16
+ * For a more in depth tutorial, albeit in C, see http://xmlsoft.org/xmlwriter.html
17
+ */
18
+
19
+ #include <libxml/xmlwriter.h>
20
+
21
+
22
+ typedef enum {
23
+ RXMLW_OUTPUT_NONE,
24
+ RXMLW_OUTPUT_IO,
25
+ RXMLW_OUTPUT_DOC,
26
+ RXMLW_OUTPUT_STRING
27
+ } rxmlw_output_type;
28
+
29
+ typedef struct {
30
+ VALUE output;
31
+ #ifdef HAVE_RUBY_ENCODING_H
32
+ rb_encoding *encoding;
33
+ #endif /* HAVE_RUBY_ENCODING_H */
34
+ xmlBufferPtr buffer;
35
+ xmlTextWriterPtr writer;
36
+ rxmlw_output_type output_type;
37
+ int closed;
38
+ } rxml_writer_object;
39
+
40
+ #ifdef HAVE_RUBY_ENCODING_H
41
+
42
+ #define /*VALUE*/ rxml_writer_c_to_ruby_string(/*const xmlChar **/ string, /*long*/ string_len) \
43
+ rb_external_str_new_with_enc(string, string_len, rb_utf8_encoding())
44
+
45
+ #define /*VALUE*/ rxml_writer_ruby_string_to_utf8(/*VALUE*/ string) \
46
+ rb_str_conv_enc(string, rb_enc_get(string), rb_utf8_encoding())
47
+ // rb_str_export_to_enc(string, rb_utf8_encoding())
48
+
49
+ #define /*void*/ rxml_writer_free_utf8_string(/*VALUE*/ orig, /*VALUE*/ utf8) \
50
+ do { \
51
+ if (orig != utf8) { \
52
+ rb_str_free(utf8); \
53
+ } \
54
+ } while (0);
55
+
56
+ #else
57
+
58
+ #define /*VALUE*/ rxml_writer_c_to_ruby_string(/*const xmlChar **/ string, /*long*/ string_len) \
59
+ rb_str_new(string, string_len)
60
+
61
+ #define /*VALUE*/ rxml_writer_ruby_string_to_utf8(/*VALUE*/ string) \
62
+ string
63
+
64
+ #define /*void*/ rxml_writer_free_utf8_string(/*VALUE*/ orig, /*VALUE*/ utf8) \
65
+ /* NOP */
66
+
67
+ #endif /* HAVE_RUBY_ENCODING_H */
68
+
69
+ static void rxml_writer_free(rxml_writer_object *rwo)
70
+ {
71
+ #if 0 /* seems to be done by xmlFreeTextWriter */
72
+ if (NULL != rwo->buffer) {
73
+ xmlBufferFree(rwo->buffer);
74
+ }
75
+ #endif
76
+
77
+ rwo->closed = 1;
78
+ xmlFreeTextWriter(rwo->writer);
79
+ xfree(rwo);
80
+ }
81
+
82
+ static void rxml_writer_mark(rxml_writer_object *rwo)
83
+ {
84
+ if (!NIL_P(rwo->output)) {
85
+ rb_gc_mark(rwo->output);
86
+ }
87
+ }
88
+
89
+ static VALUE rxml_writer_wrap(rxml_writer_object *rwo)
90
+ {
91
+ return Data_Wrap_Struct(cXMLWriter, rxml_writer_mark, rxml_writer_free, rwo);
92
+ }
93
+
94
+ static rxml_writer_object *rxml_textwriter_get(VALUE obj)
95
+ {
96
+ rxml_writer_object *rwo;
97
+
98
+ Data_Get_Struct(obj, rxml_writer_object, rwo);
99
+
100
+ return rwo;
101
+ }
102
+
103
+ int rxml_writer_write_callback(void *context, const char *buffer, int len)
104
+ {
105
+ rxml_writer_object *rwo = context;
106
+
107
+ if(rwo->closed){
108
+ return 0;
109
+ }else{
110
+ return rxml_write_callback((void *)rwo->output, buffer, len);
111
+ }
112
+ }
113
+
114
+ /* ===== public class methods ===== */
115
+
116
+ /* call-seq:
117
+ * XML::Writer::io(io) -> XML::Writer
118
+ *
119
+ * Creates a XML::Writer which will write XML directly into an IO object.
120
+ */
121
+ static VALUE rxml_writer_io(VALUE klass, VALUE io)
122
+ {
123
+ #if 0
124
+ typedef int (*xmlOutputCloseCallback)(void * context);
125
+ typedef int (*xmlOutputWriteCallback)(void * context, const char * buffer, int len);
126
+
127
+ ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
128
+
129
+ xmlOutputBufferPtr xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite, xmlOutputCloseCallback ioclose, void * ioctx, xmlCharEncodingHandlerPtr encoder)
130
+
131
+ xmlCharEncodingHandlerPtr xmlFindCharEncodingHandler(const char * name);
132
+ #endif
133
+ xmlOutputBufferPtr out;
134
+ rxml_writer_object *rwo;
135
+
136
+ rwo = ALLOC(rxml_writer_object);
137
+ rwo->output = io;
138
+ rwo->buffer = NULL;
139
+ rwo->closed = 0;
140
+ #ifdef HAVE_RUBY_ENCODING_H
141
+ rwo->encoding = NULL;
142
+ #endif /* HAVE_RUBY_ENCODING_H */
143
+ rwo->output_type = RXMLW_OUTPUT_IO;
144
+ if (NULL == (out = xmlOutputBufferCreateIO(rxml_writer_write_callback, NULL, (void *) rwo, NULL))) {
145
+ rxml_raise(&xmlLastError);
146
+ }
147
+ if (NULL == (rwo->writer = xmlNewTextWriter(out))) {
148
+ rxml_raise(&xmlLastError);
149
+ }
150
+
151
+ return rxml_writer_wrap(rwo);
152
+ }
153
+
154
+
155
+ /* call-seq:
156
+ * XML::Writer::file(path) -> XML::Writer
157
+ *
158
+ * Creates a XML::Writer object which will write XML into the file with
159
+ * the given name.
160
+ */
161
+ static VALUE rxml_writer_file(VALUE klass, VALUE filename)
162
+ {
163
+ rxml_writer_object *rwo;
164
+
165
+ rwo = ALLOC(rxml_writer_object);
166
+ rwo->output = Qnil;
167
+ rwo->buffer = NULL;
168
+ rwo->closed = 0;
169
+ #ifdef HAVE_RUBY_ENCODING_H
170
+ rwo->encoding = NULL;
171
+ #endif /* HAVE_RUBY_ENCODING_H */
172
+ rwo->output_type = RXMLW_OUTPUT_NONE;
173
+ if (NULL == (rwo->writer = xmlNewTextWriterFilename(StringValueCStr(filename), 0))) {
174
+ rxml_raise(&xmlLastError);
175
+ }
176
+
177
+ return rxml_writer_wrap(rwo);
178
+ }
179
+
180
+ /* call-seq:
181
+ * XML::Writer::string -> XML::Writer
182
+ *
183
+ * Creates a XML::Writer which will write XML into memory, as string.
184
+ */
185
+ static VALUE rxml_writer_string(VALUE klass)
186
+ {
187
+ rxml_writer_object *rwo;
188
+
189
+ rwo = ALLOC(rxml_writer_object);
190
+ rwo->output = Qnil;
191
+ rwo->closed = 0;
192
+ #ifdef HAVE_RUBY_ENCODING_H
193
+ rwo->encoding = NULL;
194
+ #endif /* HAVE_RUBY_ENCODING_H */
195
+ rwo->output_type = RXMLW_OUTPUT_STRING;
196
+ if (NULL == (rwo->buffer = xmlBufferCreate())) {
197
+ rxml_raise(&xmlLastError);
198
+ }
199
+ if (NULL == (rwo->writer = xmlNewTextWriterMemory(rwo->buffer, 0))) {
200
+ xmlBufferFree(rwo->buffer);
201
+ rxml_raise(&xmlLastError);
202
+ }
203
+
204
+ return rxml_writer_wrap(rwo);
205
+ }
206
+
207
+ /* call-seq:
208
+ * XML::Writer::document -> XML::Writer
209
+ *
210
+ * Creates a XML::Writer which will write into an in memory XML::Document
211
+ */
212
+ static VALUE rxml_writer_doc(VALUE klass)
213
+ {
214
+ xmlDocPtr doc;
215
+ rxml_writer_object *rwo;
216
+
217
+ rwo = ALLOC(rxml_writer_object);
218
+ rwo->buffer = NULL;
219
+ rwo->output = Qnil;
220
+ rwo->closed = 0;
221
+ #ifdef HAVE_RUBY_ENCODING_H
222
+ rwo->encoding = NULL;
223
+ #endif /* HAVE_RUBY_ENCODING_H */
224
+ rwo->output_type = RXMLW_OUTPUT_DOC;
225
+ if (NULL == (rwo->writer = xmlNewTextWriterDoc(&doc, 0))) {
226
+ rxml_raise(&xmlLastError);
227
+ }
228
+ rwo->output = rxml_document_wrap(doc);
229
+
230
+ return rxml_writer_wrap(rwo);
231
+ }
232
+
233
+ /* ===== public instance methods ===== */
234
+
235
+ /* call-seq:
236
+ * writer.flush(empty? = true) -> (num|string)
237
+ *
238
+ * Flushes the output buffer. Returns the number of written bytes or
239
+ * the current content of the internal buffer for a in memory XML::Writer.
240
+ * If +empty?+ is +true+, and for a in memory XML::Writer, this internel
241
+ * buffer is empty.
242
+ */
243
+ static VALUE rxml_writer_flush(int argc, VALUE *argv, VALUE self)
244
+ {
245
+ int ret;
246
+ VALUE empty;
247
+ rxml_writer_object *rwo;
248
+
249
+ rb_scan_args(argc, argv, "01", &empty);
250
+
251
+ rwo = rxml_textwriter_get(self);
252
+ if (-1 == (ret = xmlTextWriterFlush(rwo->writer))) {
253
+ rxml_raise(&xmlLastError);
254
+ }
255
+
256
+ if (NULL != rwo->buffer) {
257
+ VALUE content;
258
+
259
+ #ifdef HAVE_RUBY_ENCODING_H
260
+ content = rb_external_str_new_with_enc((const char*)rwo->buffer->content, rwo->buffer->use, rwo->encoding);
261
+ #else
262
+ content = rb_str_new(rwo->buffer->content, rwo->buffer->use);
263
+ #endif /* HAVE_RUBY_ENCODING_H */
264
+ if (NIL_P(empty) || RTEST(empty)) { /* nil = default value = true */
265
+ xmlBufferEmpty(rwo->buffer);
266
+ }
267
+
268
+ return content;
269
+ } else {
270
+ return INT2NUM(ret);
271
+ }
272
+ }
273
+
274
+ /* call-seq:
275
+ * writer.result -> (XML::Document|"string"|nil)
276
+ *
277
+ * Returns the associated result object to the XML::Writer creation.
278
+ * A String for a XML::Writer object created with XML::Writer::string,
279
+ * a XML::Document with XML::Writer::document, etc.
280
+ */
281
+ static VALUE rxml_writer_result(VALUE self)
282
+ {
283
+ VALUE ret = Qnil;
284
+ rxml_writer_object *rwo = rxml_textwriter_get(self);
285
+ int bytesWritten = xmlTextWriterFlush(rwo->writer);
286
+
287
+ if (bytesWritten == -1) {
288
+ rxml_raise(&xmlLastError);
289
+ }
290
+
291
+ switch (rwo->output_type) {
292
+ case RXMLW_OUTPUT_DOC:
293
+ ret = rwo->output;
294
+ break;
295
+ case RXMLW_OUTPUT_STRING:
296
+ ret = rxml_writer_c_to_ruby_string((const char*)rwo->buffer->content, rwo->buffer->use);
297
+ break;
298
+ case RXMLW_OUTPUT_IO:
299
+ case RXMLW_OUTPUT_NONE:
300
+ break;
301
+ default:
302
+ rb_bug("unexpected output");
303
+ break;
304
+ }
305
+
306
+ return ret;
307
+ }
308
+
309
+ /* ===== private helpers ===== */
310
+
311
+ static VALUE numeric_rxml_writer_void(VALUE obj, int (*fn)(xmlTextWriterPtr))
312
+ {
313
+ int ret;
314
+ rxml_writer_object *rwo;
315
+
316
+ rwo = rxml_textwriter_get(obj);
317
+ ret = fn(rwo->writer);
318
+
319
+ return (-1 == ret ? Qfalse : Qtrue);
320
+ }
321
+
322
+ #define numeric_rxml_writer_string(/*VALUE*/ obj, /*VALUE*/ name_or_content, /*int (**/fn/*)(xmlTextWriterPtr, const xmlChar *)*/) \
323
+ numeric_rxml_writer_va_strings(obj, Qundef, 1, fn, name_or_content)
324
+
325
+ /**
326
+ * This is quite ugly but thanks to libxml2 coding style, all xmlTextWriter*
327
+ * calls can be redirected to a single function. This can be convenient to:
328
+ * - avoid repeating yourself
329
+ * - convert strings to UTF-8
330
+ * - validate names
331
+ * and so on
332
+ **/
333
+ #define XMLWRITER_MAX_STRING_ARGS 5
334
+ static VALUE numeric_rxml_writer_va_strings(VALUE obj, VALUE pe, size_t strings_count, int (*fn)(ANYARGS), ...)
335
+ {
336
+ va_list ap;
337
+ size_t argc;
338
+ int ret = -1;
339
+ rxml_writer_object *rwo;
340
+ const xmlChar *argv[XMLWRITER_MAX_STRING_ARGS];
341
+ VALUE utf8[XMLWRITER_MAX_STRING_ARGS], orig[XMLWRITER_MAX_STRING_ARGS];
342
+
343
+ if (strings_count > XMLWRITER_MAX_STRING_ARGS) {
344
+ rb_bug("more arguments than expected");
345
+ }
346
+ va_start(ap, fn);
347
+ rwo = rxml_textwriter_get(obj);
348
+ for (argc = 0; argc < strings_count; argc++) {
349
+ VALUE arg;
350
+
351
+ arg = va_arg(ap, VALUE);
352
+ orig[argc] = arg;
353
+ if (NIL_P(arg)) {
354
+ utf8[argc] = Qnil;
355
+ argv[argc] = NULL;
356
+ } else {
357
+ utf8[argc] = rxml_writer_ruby_string_to_utf8(orig[argc]);
358
+ argv[argc] = BAD_CAST StringValueCStr(utf8[argc]);
359
+ }
360
+ }
361
+ va_end(ap);
362
+
363
+ if (Qundef == pe) {
364
+ switch (strings_count) {
365
+ case 0:
366
+ ret = fn(rwo->writer);
367
+ break;
368
+ case 1:
369
+ ret = fn(rwo->writer, argv[0]);
370
+ break;
371
+ case 2:
372
+ ret = fn(rwo->writer, argv[0], argv[1]);
373
+ break;
374
+ case 3:
375
+ ret = fn(rwo->writer, argv[0], argv[1], argv[2]);
376
+ break;
377
+ case 4:
378
+ ret = fn(rwo->writer, argv[0], argv[1], argv[2], argv[3]);
379
+ break;
380
+ case 5:
381
+ ret = fn(rwo->writer, argv[0], argv[1], argv[2], argv[3], argv[4]);
382
+ break;
383
+ default:
384
+ break;
385
+ }
386
+ } else {
387
+ int xpe;
388
+
389
+ xpe = RTEST(pe);
390
+ switch (strings_count) { /* strings_count doesn't include pe */
391
+ case 0:
392
+ ret = fn(rwo->writer, xpe);
393
+ break;
394
+ case 1:
395
+ ret = fn(rwo->writer, xpe, argv[0]);
396
+ break;
397
+ case 2:
398
+ ret = fn(rwo->writer, xpe, argv[0], argv[1]);
399
+ break;
400
+ case 3:
401
+ ret = fn(rwo->writer, xpe, argv[0], argv[1], argv[2]);
402
+ break;
403
+ case 4:
404
+ ret = fn(rwo->writer, xpe, argv[0], argv[1], argv[2], argv[3]);
405
+ break;
406
+ case 5:
407
+ ret = fn(rwo->writer, xpe, argv[0], argv[1], argv[2], argv[3], argv[4]);
408
+ break;
409
+ default:
410
+ break;
411
+ }
412
+ }
413
+ #ifdef HAVE_RUBY_ENCODING_H
414
+ while (--strings_count > 0) {
415
+ if (!NIL_P(orig[strings_count])) {
416
+ rxml_writer_free_utf8_string(orig[strings_count], utf8[strings_count]);
417
+ }
418
+ }
419
+ #endif /* HAVE_RUBY_ENCODING_H */
420
+
421
+ return (-1 == ret ? Qfalse : Qtrue);
422
+ }
423
+
424
+ /* ===== public instance methods ===== */
425
+
426
+ #if LIBXML_VERSION >= 20605
427
+ /* call-seq:
428
+ * writer.set_indent(indentation) -> (true|false)
429
+ *
430
+ * Toggles indentation on or off. Returns +false+ on failure.
431
+ *
432
+ * Availability: libxml2 >= 2.6.5
433
+ */
434
+ static VALUE rxml_writer_set_indent(VALUE self, VALUE indentation)
435
+ {
436
+ int ret;
437
+ rxml_writer_object *rwo;
438
+
439
+ rwo = rxml_textwriter_get(self);
440
+ ret = xmlTextWriterSetIndent(rwo->writer, RTEST(indentation));
441
+
442
+ return (-1 == ret ? Qfalse : Qtrue);
443
+ }
444
+
445
+ /* call-seq:
446
+ * writer.set_indent_string(string) -> (true|false)
447
+ *
448
+ * Sets the string to use to indent each element of the document.
449
+ * Don't forget to enable indentation with set_indent. Returns
450
+ * +false+ on failure.
451
+ *
452
+ * Availability: libxml2 >= 2.6.5
453
+ */
454
+ static VALUE rxml_writer_set_indent_string(VALUE self, VALUE indentation)
455
+ {
456
+ return numeric_rxml_writer_string(self, indentation, xmlTextWriterSetIndentString);
457
+ }
458
+ #endif /* LIBXML_VERSION >= 20605 */
459
+
460
+ /* ===== public full tag interface ===== */
461
+
462
+ /* write_<X> = start_<X> + write_string + end_<X> */
463
+
464
+ /* call-seq:
465
+ * writer.write_comment(content) -> (true|false)
466
+ *
467
+ * Writes a full comment tag, all at once. Returns +false+ on failure.
468
+ * This is equivalent to start_comment + write_string(content) + end_comment.
469
+ */
470
+ static VALUE rxml_writer_write_comment(VALUE self, VALUE content)
471
+ {
472
+ return numeric_rxml_writer_string(self, content, xmlTextWriterWriteComment);
473
+ }
474
+
475
+ /* call-seq:
476
+ * writer.write_cdata(content) -> (true|false)
477
+ *
478
+ * Writes a full CDATA section, all at once. Returns +false+ on failure.
479
+ * This is equivalent to start_cdata + write_string(content) + end_cdata.
480
+ */
481
+ static VALUE rxml_writer_write_cdata(VALUE self, VALUE content)
482
+ {
483
+ return numeric_rxml_writer_string(self, content, xmlTextWriterWriteCDATA);
484
+ }
485
+
486
+ static VALUE rxml_writer_start_element(VALUE, VALUE);
487
+ static VALUE rxml_writer_start_element_ns(int, VALUE *, VALUE);
488
+ static VALUE rxml_writer_end_element(VALUE);
489
+
490
+ /* call-seq:
491
+ * writer.write_element(name, content) -> (true|false)
492
+ *
493
+ * Writes a full element tag, all at once. Returns +false+ on failure.
494
+ * This is equivalent to start_element(name) + write_string(content) +
495
+ * end_element.
496
+ */
497
+ static VALUE rxml_writer_write_element(int argc, VALUE *argv, VALUE self)
498
+ {
499
+ VALUE name, content;
500
+
501
+ rb_scan_args(argc, argv, "11", &name, &content);
502
+ if (Qnil == content) {
503
+ if (Qfalse == rxml_writer_start_element(self, name)) {
504
+ return Qfalse;
505
+ }
506
+ return rxml_writer_end_element(self);
507
+ } else {
508
+ return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteElement, name, content);
509
+ }
510
+ }
511
+
512
+ #define ARRAY_SIZE(array) \
513
+ (sizeof(array) / sizeof((array)[0]))
514
+
515
+ /* call-seq:
516
+ * writer.write_element_ns(prefix, name, namespaceURI, content) -> (true|false)
517
+ *
518
+ * Writes a full namespaced element tag, all at once. Returns +false+ on failure.
519
+ * This is a shortcut for start_element_ns(prefix, name, namespaceURI) +
520
+ * write_string(content) + end_element.
521
+ *
522
+ * Notes:
523
+ * - by default, the xmlns: definition is repeated on every element. If you want
524
+ * the prefix, but don't want the xmlns: declaration repeated, set +namespaceURI+
525
+ * to nil or omit it. Don't forget to declare the namespace prefix somewhere
526
+ * earlier.
527
+ * - +content+ can be omitted for an empty tag
528
+ */
529
+ static VALUE rxml_writer_write_element_ns(int argc, VALUE *argv, VALUE self)
530
+ {
531
+ VALUE prefix, name, namespaceURI, content;
532
+
533
+ rb_scan_args(argc, argv, "22", &prefix, &name, &namespaceURI, &content);
534
+ if (Qnil == content) {
535
+ VALUE argv[3] = { prefix, name, namespaceURI };
536
+
537
+ if (Qfalse == rxml_writer_start_element_ns(ARRAY_SIZE(argv), argv, self)) {
538
+ return Qfalse;
539
+ }
540
+ return rxml_writer_end_element(self);
541
+ } else {
542
+ return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteElementNS, prefix, name, namespaceURI, content);
543
+ }
544
+ }
545
+
546
+ /* call-seq:
547
+ * writer.write_attribute(name, content) -> (true|false)
548
+ *
549
+ * Writes a full attribute, all at once. Returns +false+ on failure.
550
+ * Same as start_attribute(name) + write_string(content) + end_attribute.
551
+ */
552
+ static VALUE rxml_writer_write_attribute(VALUE self, VALUE name, VALUE content)
553
+ {
554
+ return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteAttribute, name, content);
555
+ }
556
+
557
+ /* call-seq:
558
+ * writer.write_attribute_ns(prefix, name, namespaceURI, content) -> (true|false)
559
+ *
560
+ * Writes a full namespaced attribute, all at once. Returns +false+ on failure.
561
+ * Same as start_attribute_ns(prefix, name, namespaceURI) +
562
+ * write_string(content) + end_attribute.
563
+ *
564
+ * Notes:
565
+ * - by default, the xmlns: definition is repeated on every element. If you want
566
+ * the prefix, but don't want the xmlns: declaration repeated, set +namespaceURI+
567
+ * to nil or omit it. Don't forget to declare the namespace prefix somewhere
568
+ * earlier.
569
+ * - +content+ can be omitted too for an empty attribute
570
+ */
571
+ static VALUE rxml_writer_write_attribute_ns(int argc, VALUE *argv, VALUE self)
572
+ {
573
+ VALUE prefix, name, namespaceURI, content;
574
+
575
+ rb_scan_args(argc, argv, "22", &prefix, &name, &namespaceURI, &content);
576
+
577
+ return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteAttributeNS, prefix, name, namespaceURI, content);
578
+ }
579
+
580
+ /* call-seq:
581
+ * writer.write_pi(target, content) -> (true|false)
582
+ *
583
+ * Writes a full CDATA tag, all at once. Returns +false+ on failure.
584
+ * This is a shortcut for start_pi(target) + write_string(content) + end_pi.
585
+ */
586
+ static VALUE rxml_writer_write_pi(VALUE self, VALUE target, VALUE content)
587
+ {
588
+ return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWritePI, target, content);
589
+ }
590
+
591
+ /* ===== public start/end interface ===== */
592
+
593
+ /* call-seq:
594
+ * writer.write_string(content) -> (true|false)
595
+ *
596
+ * Safely (problematic characters are internally translated to their
597
+ * associated named entities) writes a string into the current node
598
+ * (attribute, element, comment, ...). Returns +false+ on failure.
599
+ */
600
+ static VALUE rxml_writer_write_string(VALUE self, VALUE content)
601
+ {
602
+ return numeric_rxml_writer_string(self, content, xmlTextWriterWriteString);
603
+ }
604
+
605
+ /* call-seq:
606
+ * writer.write_raw(content) -> (true|false)
607
+ *
608
+ * Writes the string +content+ as is, reserved characters are not
609
+ * translated to their associated entities. Returns +false+ on failure.
610
+ * Consider write_string to handle them.
611
+ */
612
+ static VALUE rxml_writer_write_raw(VALUE self, VALUE content)
613
+ {
614
+ return numeric_rxml_writer_string(self, content, xmlTextWriterWriteRaw);
615
+ }
616
+
617
+ /* call-seq:
618
+ * writer.start_attribute(name) -> (true|false)
619
+ *
620
+ * Starts an attribute. Returns +false+ on failure.
621
+ */
622
+ static VALUE rxml_writer_start_attribute(VALUE self, VALUE name)
623
+ {
624
+ return numeric_rxml_writer_string(self, name, xmlTextWriterStartAttribute);
625
+ }
626
+
627
+ /* call-seq:
628
+ * writer.start_attribute_ns(prefix, name, namespaceURI) -> (true|false)
629
+ *
630
+ * Starts a namespaced attribute. Returns +false+ on failure.
631
+ *
632
+ * Note: by default, the xmlns: definition is repeated on every element. If
633
+ * you want the prefix, but don't want the xmlns: declaration repeated, set
634
+ * +namespaceURI+ to nil or omit it. Don't forget to declare the namespace
635
+ * prefix somewhere earlier.
636
+ */
637
+ static VALUE rxml_writer_start_attribute_ns(int argc, VALUE *argv, VALUE self)
638
+ {
639
+ VALUE prefix, name, namespaceURI;
640
+
641
+ rb_scan_args(argc, argv, "21", &prefix, &name, &namespaceURI);
642
+
643
+ return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartAttributeNS, prefix, name, namespaceURI);
644
+ }
645
+
646
+ /* call-seq:
647
+ * writer.end_attribute -> (true|false)
648
+ *
649
+ * Ends an attribute, namespaced or not. Returns +false+ on failure.
650
+ */
651
+ static VALUE rxml_writer_end_attribute(VALUE self)
652
+ {
653
+ return numeric_rxml_writer_void(self, xmlTextWriterEndAttribute);
654
+ }
655
+
656
+ #if LIBXML_VERSION >= 20607
657
+ /* call-seq:
658
+ * writer.start_comment -> (true|false)
659
+ *
660
+ * Starts a comment. Returns +false+ on failure.
661
+ * Note: libxml2 >= 2.6.7 required
662
+ */
663
+ static VALUE rxml_writer_start_comment(VALUE self)
664
+ {
665
+ return numeric_rxml_writer_void(self, xmlTextWriterStartComment);
666
+ }
667
+
668
+ /* call-seq:
669
+ * writer.end_comment -> (true|false)
670
+ *
671
+ * Ends current comment, returns +false+ on failure.
672
+ * Note: libxml2 >= 2.6.7 required
673
+ */
674
+ static VALUE rxml_writer_end_comment(VALUE self)
675
+ {
676
+ return numeric_rxml_writer_void(self, xmlTextWriterEndComment);
677
+ }
678
+ #endif /* LIBXML_VERSION >= 20607 */
679
+
680
+ /* call-seq:
681
+ * writer.start_element(name) -> (true|false)
682
+ *
683
+ * Starts a new element. Returns +false+ on failure.
684
+ */
685
+ static VALUE rxml_writer_start_element(VALUE self, VALUE name)
686
+ {
687
+ return numeric_rxml_writer_string(self, name, xmlTextWriterStartElement);
688
+ }
689
+
690
+ /* call-seq:
691
+ * writer.start_element_ns(prefix, name, namespaceURI) -> (true|false)
692
+ *
693
+ * Starts a new namespaced element. Returns +false+ on failure.
694
+ *
695
+ * Note: by default, the xmlns: definition is repeated on every element. If
696
+ * you want the prefix, but don't want the xmlns: declaration repeated, set
697
+ * +namespaceURI+ to nil or omit it. Don't forget to declare the namespace
698
+ * prefix somewhere earlier.
699
+ */
700
+ static VALUE rxml_writer_start_element_ns(int argc, VALUE *argv, VALUE self)
701
+ {
702
+ VALUE prefix, name, namespaceURI;
703
+
704
+ rb_scan_args(argc, argv, "21", &prefix, &name, &namespaceURI);
705
+
706
+ return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartElementNS, prefix, name, namespaceURI);
707
+ }
708
+
709
+ /* call-seq:
710
+ * writer.end_element -> (true|false)
711
+ *
712
+ * Ends current element, namespaced or not. Returns +false+ on failure.
713
+ */
714
+ static VALUE rxml_writer_end_element(VALUE self)
715
+ {
716
+ return numeric_rxml_writer_void(self, xmlTextWriterEndElement);
717
+ }
718
+
719
+ /* call-seq:
720
+ * writer.write_full_end_element -> (true|false)
721
+ *
722
+ * Ends current element, namespaced or not. Returns +false+ on failure.
723
+ * This method writes an end tag even if the element is empty (<foo></foo>),
724
+ * end_element does not (<foo/>).
725
+ */
726
+ static VALUE rxml_writer_full_end_element(VALUE self)
727
+ {
728
+ return numeric_rxml_writer_void(self, xmlTextWriterFullEndElement);
729
+ }
730
+
731
+ /* call-seq:
732
+ * writer.start_cdata -> (true|false)
733
+ *
734
+ * Starts a new CDATA section. Returns +false+ on failure.
735
+ */
736
+ static VALUE rxml_writer_start_cdata(VALUE self)
737
+ {
738
+ return numeric_rxml_writer_void(self, xmlTextWriterStartCDATA);
739
+ }
740
+
741
+ /* call-seq:
742
+ * writer.end_cdata -> (true|false)
743
+ *
744
+ * Ends current CDATA section. Returns +false+ on failure.
745
+ */
746
+ static VALUE rxml_writer_end_cdata(VALUE self)
747
+ {
748
+ return numeric_rxml_writer_void(self, xmlTextWriterEndCDATA);
749
+ }
750
+
751
+ /* call-seq:
752
+ * writer.start_document -> (true|false)
753
+ * writer.start_document(:encoding => XML::Encoding::UTF_8,
754
+ * :standalone => true) -> (true|false)
755
+ *
756
+ * Starts a new document. Returns +false+ on failure.
757
+ *
758
+ * You may provide an optional hash table to control XML header that will be
759
+ * generated. Valid options are:
760
+ * - encoding: the output document encoding, defaults to nil (= UTF-8). Valid
761
+ * values are the encoding constants defined on XML::Encoding
762
+ * - standalone: nil (default) or a boolean to indicate if the document is
763
+ * standalone or not
764
+ */
765
+ static VALUE rxml_writer_start_document(int argc, VALUE *argv, VALUE self)
766
+ {
767
+ int ret;
768
+ VALUE options = Qnil;
769
+ rxml_writer_object *rwo;
770
+ const xmlChar *xencoding = NULL;
771
+ const char *xstandalone = NULL;
772
+
773
+ rb_scan_args(argc, argv, "01", &options);
774
+ if (!NIL_P(options)) {
775
+ VALUE encoding, standalone;
776
+
777
+ encoding = standalone = Qnil;
778
+ Check_Type(options, T_HASH);
779
+ encoding = rb_hash_aref(options, sEncoding);
780
+ xencoding = NIL_P(encoding) ? NULL : (const xmlChar*)xmlGetCharEncodingName(NUM2INT(encoding));
781
+ standalone = rb_hash_aref(options, sStandalone);
782
+ if (NIL_P(standalone)) {
783
+ xstandalone = NULL;
784
+ } else {
785
+ xstandalone = RTEST(standalone) ? "yes" : "no";
786
+ }
787
+ }
788
+ rwo = rxml_textwriter_get(self);
789
+ #ifdef HAVE_RUBY_ENCODING_H
790
+ rwo->encoding = rxml_figure_encoding(xencoding);
791
+ #endif /* !HAVE_RUBY_ENCODING_H */
792
+ ret = xmlTextWriterStartDocument(rwo->writer, NULL, (const char*)xencoding, xstandalone);
793
+
794
+ return (-1 == ret ? Qfalse : Qtrue);
795
+ }
796
+
797
+ /* call-seq:
798
+ * writer.end_document -> (true|false)
799
+ *
800
+ * Ends current document. Returns +false+ on failure.
801
+ */
802
+ static VALUE rxml_writer_end_document(VALUE self)
803
+ {
804
+ return numeric_rxml_writer_void(self, xmlTextWriterEndDocument);
805
+ }
806
+
807
+ /* call-seq:
808
+ * writer.start_pi(target) -> (true|false)
809
+ *
810
+ * Starts a new processing instruction. Returns +false+ on failure.
811
+ */
812
+ static VALUE rxml_writer_start_pi(VALUE self, VALUE target)
813
+ {
814
+ return numeric_rxml_writer_string(self, target, xmlTextWriterStartPI);
815
+ }
816
+
817
+ /* call-seq:
818
+ * writer.end_pi -> (true|false)
819
+ *
820
+ * Ends current processing instruction. Returns +false+ on failure.
821
+ */
822
+ static VALUE rxml_writer_end_pi(VALUE self)
823
+ {
824
+ return numeric_rxml_writer_void(self, xmlTextWriterEndPI);
825
+ }
826
+
827
+ /* call-seq:
828
+ * writer.start_dtd(qualifiedName, publicId, systemId) -> (true|false)
829
+ *
830
+ * Starts a DTD. Returns +false+ on failure.
831
+ */
832
+ static VALUE rxml_writer_start_dtd(int argc, VALUE *argv, VALUE self)
833
+ {
834
+ VALUE name, pubid, sysid;
835
+
836
+ rb_scan_args(argc, argv, "12", &name, &pubid, &sysid);
837
+
838
+ return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartDTD, name, pubid, sysid);
839
+ }
840
+
841
+ /* call-seq:
842
+ * writer.start_dtd_element(qualifiedName) -> (true|false)
843
+ *
844
+ * Starts a DTD element (<!ELEMENT ... >). Returns +false+ on failure.
845
+ */
846
+ static VALUE rxml_writer_start_dtd_element(VALUE self, VALUE name)
847
+ {
848
+ return numeric_rxml_writer_string(self, name, xmlTextWriterStartDTDElement);
849
+ }
850
+
851
+ /* call-seq:
852
+ * writer.start_dtd_entity(name, pe = false) -> (true|false)
853
+ *
854
+ * Starts a DTD entity (<!ENTITY ... >). Returns +false+ on failure.
855
+ */
856
+ static VALUE rxml_writer_start_dtd_entity(int argc, VALUE *argv, VALUE self)
857
+ {
858
+ VALUE name, pe;
859
+
860
+ rb_scan_args(argc, argv, "11", &name, &pe);
861
+ if (NIL_P(pe)) {
862
+ pe = Qfalse;
863
+ }
864
+
865
+ return numeric_rxml_writer_va_strings(self, pe, 1, xmlTextWriterStartDTDEntity, name);
866
+ }
867
+
868
+ /* call-seq:
869
+ * writer.start_dtd_attlist(name) -> (true|false)
870
+ *
871
+ * Starts a DTD attribute list (<!ATTLIST ... >). Returns +false+ on failure.
872
+ */
873
+ static VALUE rxml_writer_start_dtd_attlist(VALUE self, VALUE name)
874
+ {
875
+ return numeric_rxml_writer_string(self, name, xmlTextWriterStartDTDAttlist);
876
+ }
877
+
878
+ /* call-seq:
879
+ * writer.end_dtd -> (true|false)
880
+ *
881
+ * Ends current DTD, returns +false+ on failure.
882
+ */
883
+ static VALUE rxml_writer_end_dtd(VALUE self)
884
+ {
885
+ return numeric_rxml_writer_void(self, xmlTextWriterEndDTD);
886
+ }
887
+
888
+ /* call-seq:
889
+ * writer.end_dtd_entity -> (true|false)
890
+ *
891
+ * Ends current DTD entity, returns +false+ on failure.
892
+ */
893
+ static VALUE rxml_writer_end_dtd_entity(VALUE self)
894
+ {
895
+ return numeric_rxml_writer_void(self, xmlTextWriterEndDTDEntity);
896
+ }
897
+
898
+ /* call-seq:
899
+ * writer.end_dtd_attlist -> (true|false)
900
+ *
901
+ * Ends current DTD attribute list, returns +false+ on failure.
902
+ */
903
+ static VALUE rxml_writer_end_dtd_attlist(VALUE self)
904
+ {
905
+ return numeric_rxml_writer_void(self, xmlTextWriterEndDTDAttlist);
906
+ }
907
+
908
+ /* call-seq:
909
+ * writer.end_dtd_element -> (true|false)
910
+ *
911
+ * Ends current DTD element, returns +false+ on failure.
912
+ */
913
+ static VALUE rxml_writer_end_dtd_element(VALUE self)
914
+ {
915
+ return numeric_rxml_writer_void(self, xmlTextWriterEndDTDElement);
916
+ }
917
+
918
+ /* call-seq:
919
+ * writer.write_dtd(name [ [ [, publicId ], systemId ], subset ]) -> (true|false)
920
+ *
921
+ * Writes a DTD, all at once. Returns +false+ on failure.
922
+ * - name: dtd name
923
+ * - publicId: external subset public identifier, use nil for a SYSTEM doctype
924
+ * - systemId: external subset system identifier
925
+ * - subset: content
926
+ *
927
+ * Examples:
928
+ * writer.write_dtd 'html'
929
+ * #=> <!DOCTYPE html>
930
+ * writer.write_dtd 'docbook', nil, 'http://www.docbook.org/xml/5.0/dtd/docbook.dtd'
931
+ * #=> <!DOCTYPE docbook SYSTEM "http://www.docbook.org/xml/5.0/dtd/docbook.dtd">
932
+ * writer.write_dtd 'html', '-//W3C//DTD XHTML 1.1//EN', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
933
+ * #=> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
934
+ * writer.write_dtd 'person', nil, nil, '<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>'
935
+ * #=> <!DOCTYPE person [<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>]>
936
+ */
937
+ static VALUE rxml_writer_write_dtd(int argc, VALUE *argv, VALUE self)
938
+ {
939
+ VALUE name, pubid, sysid, subset;
940
+
941
+ rb_scan_args(argc, argv, "13", &name, &pubid, &sysid, &subset);
942
+
943
+ return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteDTD, name, pubid, sysid, subset);
944
+ }
945
+
946
+ /* call-seq:
947
+ * writer.write_dtd_attlist(name, content) -> (true|false)
948
+ *
949
+ * Writes a DTD attribute list, all at once. Returns +false+ on failure.
950
+ * writer.write_dtd_attlist 'id', 'ID #IMPLIED'
951
+ * #=> <!ATTLIST id ID #IMPLIED>
952
+ */
953
+ static VALUE rxml_writer_write_dtd_attlist(VALUE self, VALUE name, VALUE content)
954
+ {
955
+ return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteDTDAttlist, name, content);
956
+ }
957
+
958
+ /* call-seq:
959
+ * writer.write_dtd_element(name, content) -> (true|false)
960
+ *
961
+ * Writes a full DTD element, all at once. Returns +false+ on failure.
962
+ * writer.write_dtd_element 'person', '(firstname,lastname)'
963
+ * #=> <!ELEMENT person (firstname,lastname)>
964
+ */
965
+ static VALUE rxml_writer_write_dtd_element(VALUE self, VALUE name, VALUE content)
966
+ {
967
+ return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteDTDElement, name, content);
968
+ }
969
+
970
+ /* call-seq:
971
+ * writer.write_dtd_entity(name, publicId, systemId, ndataid, content, pe) -> (true|false)
972
+ *
973
+ * Writes a DTD entity, all at once. Returns +false+ on failure.
974
+ */
975
+ static VALUE rxml_writer_write_dtd_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE content, VALUE pe)
976
+ {
977
+ return numeric_rxml_writer_va_strings(self, pe, 5, xmlTextWriterWriteDTDEntity, name, pubid, sysid, ndataid, content);
978
+ }
979
+
980
+ /* call-seq:
981
+ * writer.write_dtd_external_entity(name, publicId, systemId, ndataid, pe) -> (true|false)
982
+ *
983
+ * Writes a DTD external entity. The entity must have been started
984
+ * with start_dtd_entity. Returns +false+ on failure.
985
+ * - name: the name of the DTD entity
986
+ * - publicId: the public identifier, which is an alternative to the system identifier
987
+ * - systemId: the system identifier, which is the URI of the DTD
988
+ * - ndataid: the xml notation name
989
+ * - pe: +true+ if this is a parameter entity (to be used only in the DTD
990
+ * itself), +false+ if not
991
+ */
992
+ static VALUE rxml_writer_write_dtd_external_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE pe)
993
+ {
994
+ return numeric_rxml_writer_va_strings(self, pe, 4, xmlTextWriterWriteDTDExternalEntity, name, pubid, sysid, ndataid);
995
+ }
996
+
997
+ /* call-seq:
998
+ * writer.write_dtd_external_entity_contents(publicId, systemId, ndataid) -> (true|false)
999
+ *
1000
+ * Writes the contents of a DTD external entity, all at once. Returns +false+ on failure.
1001
+ */
1002
+ static VALUE rxml_writer_write_dtd_external_entity_contents(VALUE self, VALUE pubid, VALUE sysid, VALUE ndataid)
1003
+ {
1004
+ return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterWriteDTDExternalEntityContents, pubid, sysid, ndataid);
1005
+ }
1006
+
1007
+ /* call-seq:
1008
+ * writer.write_dtd_internal_entity(name, content, pe) -> (true|false)
1009
+ *
1010
+ * Writes a DTD internal entity, all at once. Returns +false+ on failure.
1011
+ *
1012
+ * Examples:
1013
+ * writer.write_dtd_entity 'Shape', '(rect|circle|poly|default)', true
1014
+ * #=> <!ENTITY % Shape "(rect|circle|poly|default)">
1015
+ * writer.write_dtd_entity 'delta', '&#948;', false
1016
+ * #=> <!ENTITY delta "&#948;">
1017
+ */
1018
+ static VALUE rxml_writer_write_dtd_internal_entity(VALUE self, VALUE name, VALUE content, VALUE pe)
1019
+ {
1020
+ return numeric_rxml_writer_va_strings(self, pe, 2, xmlTextWriterWriteDTDInternalEntity, name, content);
1021
+ }
1022
+
1023
+ /* call-seq:
1024
+ * writer.write_dtd_notation(name, publicId, systemId) -> (true|false)
1025
+ *
1026
+ * Writes a DTD entity, all at once. Returns +false+ on failure.
1027
+ */
1028
+ static VALUE rxml_writer_write_dtd_notation(VALUE self, VALUE name, VALUE pubid, VALUE sysid)
1029
+ {
1030
+ return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterWriteDTDNotation, name, pubid, sysid);
1031
+ }
1032
+
1033
+ #if LIBXML_VERSION >= 20900
1034
+ /* call-seq:
1035
+ * writer.set_quote_char(...) -> (true|false)
1036
+ *
1037
+ * Sets the character used to quote attributes. Returns +false+ on failure.
1038
+ *
1039
+ * Notes:
1040
+ * - only " (default) and ' characters are valid
1041
+ * - availability: libxml2 >= 2.9.0
1042
+ */
1043
+ static VALUE rxml_writer_set_quote_char(VALUE self, VALUE quotechar)
1044
+ {
1045
+ int ret;
1046
+ const char *xquotechar;
1047
+ rxml_writer_object *rwo;
1048
+
1049
+ rwo = rxml_textwriter_get(self);
1050
+ xquotechar = StringValueCStr(quotechar);
1051
+ ret = xmlTextWriterSetQuoteChar(rwo->writer, (xmlChar) xquotechar[0]);
1052
+
1053
+ return (-1 == ret ? Qfalse : Qtrue);
1054
+ }
1055
+ #endif /* LIBXML_VERSION >= 20900 */
1056
+
1057
+ #endif /* LIBXML_WRITER_ENABLED */
1058
+
1059
+
1060
+ /* grep -P 'xmlTextWriter(Start|End|Write)(?!DTD|V?Format)[^(]+' /usr/include/libxml2/libxml/xmlwriter.h */
1061
+ void rxml_init_writer(void)
1062
+ {
1063
+ sEncoding = ID2SYM(rb_intern("encoding"));
1064
+ sStandalone = ID2SYM(rb_intern("standalone"));
1065
+
1066
+ cXMLWriter = rb_define_class_under(mXML, "Writer", rb_cObject);
1067
+
1068
+ #ifdef LIBXML_WRITER_ENABLED
1069
+ rb_define_singleton_method(cXMLWriter, "io", rxml_writer_io, 1);
1070
+ rb_define_singleton_method(cXMLWriter, "file", rxml_writer_file, 1);
1071
+ rb_define_singleton_method(cXMLWriter, "document", rxml_writer_doc, 0);
1072
+ rb_define_singleton_method(cXMLWriter, "string", rxml_writer_string, 0);
1073
+
1074
+ /* misc */
1075
+ #if LIBXML_VERSION >= 20605
1076
+ rb_define_method(cXMLWriter, "set_indent", rxml_writer_set_indent, 1);
1077
+ rb_define_method(cXMLWriter, "set_indent_string", rxml_writer_set_indent_string, 1);
1078
+ #endif /* LIBXML_VERSION >= 20605 */
1079
+ #if LIBXML_VERSION >= 20900
1080
+ rb_define_method(cXMLWriter, "set_quote_char", rxml_writer_set_quote_char, 1);
1081
+ #endif /* LIBXML_VERSION >= 20900 */
1082
+ rb_define_method(cXMLWriter, "flush", rxml_writer_flush, -1);
1083
+ rb_define_method(cXMLWriter, "start_dtd", rxml_writer_start_dtd, -1);
1084
+ rb_define_method(cXMLWriter, "start_dtd_entity", rxml_writer_start_dtd_entity, -1);
1085
+ rb_define_method(cXMLWriter, "start_dtd_attlist", rxml_writer_start_dtd_attlist, 1);
1086
+ rb_define_method(cXMLWriter, "start_dtd_element", rxml_writer_start_dtd_element, 1);
1087
+ rb_define_method(cXMLWriter, "write_dtd", rxml_writer_write_dtd, -1);
1088
+ rb_define_method(cXMLWriter, "write_dtd_attlist", rxml_writer_write_dtd_attlist, 2);
1089
+ rb_define_method(cXMLWriter, "write_dtd_element", rxml_writer_write_dtd_element, 2);
1090
+ rb_define_method(cXMLWriter, "write_dtd_entity", rxml_writer_write_dtd_entity, 6);
1091
+ rb_define_method(cXMLWriter, "write_dtd_external_entity", rxml_writer_write_dtd_external_entity, 5);
1092
+ rb_define_method(cXMLWriter, "write_dtd_external_entity_contents", rxml_writer_write_dtd_external_entity_contents, 3);
1093
+ rb_define_method(cXMLWriter, "write_dtd_internal_entity", rxml_writer_write_dtd_internal_entity, 3);
1094
+ rb_define_method(cXMLWriter, "write_dtd_notation", rxml_writer_write_dtd_notation, 3);
1095
+ rb_define_method(cXMLWriter, "end_dtd", rxml_writer_end_dtd, 0);
1096
+ rb_define_method(cXMLWriter, "end_dtd_entity", rxml_writer_end_dtd_entity, 0);
1097
+ rb_define_method(cXMLWriter, "end_dtd_attlist", rxml_writer_end_dtd_attlist, 0);
1098
+ rb_define_method(cXMLWriter, "end_dtd_element", rxml_writer_end_dtd_element, 0);
1099
+
1100
+ /* tag by parts */
1101
+ rb_define_method(cXMLWriter, "write_raw", rxml_writer_write_raw, 1);
1102
+ rb_define_method(cXMLWriter, "write_string", rxml_writer_write_string, 1);
1103
+
1104
+ rb_define_method(cXMLWriter, "start_cdata", rxml_writer_start_cdata, 0);
1105
+ rb_define_method(cXMLWriter, "end_cdata", rxml_writer_end_cdata, 0);
1106
+ rb_define_method(cXMLWriter, "start_attribute", rxml_writer_start_attribute, 1);
1107
+ rb_define_method(cXMLWriter, "start_attribute_ns", rxml_writer_start_attribute_ns, -1);
1108
+ rb_define_method(cXMLWriter, "end_attribute", rxml_writer_end_attribute, 0);
1109
+ rb_define_method(cXMLWriter, "start_element", rxml_writer_start_element, 1);
1110
+ rb_define_method(cXMLWriter, "start_element_ns", rxml_writer_start_element_ns, -1);
1111
+ rb_define_method(cXMLWriter, "end_element", rxml_writer_end_element, 0);
1112
+ rb_define_method(cXMLWriter, "full_end_element", rxml_writer_full_end_element, 0);
1113
+ rb_define_method(cXMLWriter, "start_document", rxml_writer_start_document, -1);
1114
+ rb_define_method(cXMLWriter, "end_document", rxml_writer_end_document, 0);
1115
+ #if LIBXML_VERSION >= 20607
1116
+ rb_define_method(cXMLWriter, "start_comment", rxml_writer_start_comment, 0);
1117
+ rb_define_method(cXMLWriter, "end_comment", rxml_writer_end_comment, 0);
1118
+ #endif /* LIBXML_VERSION >= 20607 */
1119
+ rb_define_method(cXMLWriter, "start_pi", rxml_writer_start_pi, 1);
1120
+ rb_define_method(cXMLWriter, "end_pi", rxml_writer_end_pi, 0);
1121
+
1122
+ /* full tag at once */
1123
+ rb_define_method(cXMLWriter, "write_attribute", rxml_writer_write_attribute, 2);
1124
+ rb_define_method(cXMLWriter, "write_attribute_ns", rxml_writer_write_attribute_ns, -1);
1125
+ rb_define_method(cXMLWriter, "write_comment", rxml_writer_write_comment, 1);
1126
+ rb_define_method(cXMLWriter, "write_cdata", rxml_writer_write_cdata, 1);
1127
+ rb_define_method(cXMLWriter, "write_element", rxml_writer_write_element, -1);
1128
+ rb_define_method(cXMLWriter, "write_element_ns", rxml_writer_write_element_ns, -1);
1129
+ rb_define_method(cXMLWriter, "write_pi", rxml_writer_write_pi, 2);
1130
+
1131
+ rb_define_method(cXMLWriter, "result", rxml_writer_result, 0);
1132
+
1133
+ rb_undef_method(CLASS_OF(cXMLWriter), "new");
1134
+ #endif
1135
+ }
1136
+