libxml-ruby 3.2.2-x64-mingw-ucrt

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