libxml-ruby 2.8.0 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY +842 -775
  3. data/LICENSE +20 -20
  4. data/MANIFEST +166 -166
  5. data/README.rdoc +217 -184
  6. data/Rakefile +90 -78
  7. data/ext/libxml/extconf.h +3 -0
  8. data/ext/libxml/extconf.rb +61 -116
  9. data/ext/libxml/libxml.c +80 -76
  10. data/ext/libxml/ruby_libxml.h +67 -75
  11. data/ext/libxml/ruby_xml.c +933 -893
  12. data/ext/libxml/ruby_xml.h +10 -10
  13. data/ext/libxml/ruby_xml_attr.c +333 -333
  14. data/ext/libxml/ruby_xml_attr.h +12 -12
  15. data/ext/libxml/ruby_xml_attr_decl.c +153 -153
  16. data/ext/libxml/ruby_xml_attr_decl.h +11 -11
  17. data/ext/libxml/ruby_xml_attributes.c +275 -275
  18. data/ext/libxml/ruby_xml_attributes.h +15 -15
  19. data/ext/libxml/ruby_xml_cbg.c +85 -85
  20. data/ext/libxml/ruby_xml_document.c +1123 -1147
  21. data/ext/libxml/ruby_xml_document.h +11 -11
  22. data/ext/libxml/ruby_xml_dtd.c +248 -268
  23. data/ext/libxml/ruby_xml_dtd.h +9 -9
  24. data/ext/libxml/ruby_xml_encoding.c +250 -260
  25. data/ext/libxml/ruby_xml_encoding.h +16 -19
  26. data/ext/libxml/ruby_xml_error.c +996 -996
  27. data/ext/libxml/ruby_xml_error.h +12 -12
  28. data/ext/libxml/ruby_xml_html_parser.c +89 -92
  29. data/ext/libxml/ruby_xml_html_parser.h +10 -10
  30. data/ext/libxml/ruby_xml_html_parser_context.c +337 -338
  31. data/ext/libxml/ruby_xml_html_parser_context.h +10 -10
  32. data/ext/libxml/ruby_xml_html_parser_options.c +46 -46
  33. data/ext/libxml/ruby_xml_html_parser_options.h +10 -10
  34. data/ext/libxml/ruby_xml_input_cbg.c +191 -191
  35. data/ext/libxml/ruby_xml_input_cbg.h +20 -20
  36. data/ext/libxml/ruby_xml_io.c +47 -50
  37. data/ext/libxml/ruby_xml_io.h +10 -10
  38. data/ext/libxml/ruby_xml_namespace.c +153 -153
  39. data/ext/libxml/ruby_xml_namespace.h +10 -10
  40. data/ext/libxml/ruby_xml_namespaces.c +293 -293
  41. data/ext/libxml/ruby_xml_namespaces.h +9 -9
  42. data/ext/libxml/ruby_xml_node.c +1402 -1452
  43. data/ext/libxml/ruby_xml_node.h +13 -11
  44. data/ext/libxml/ruby_xml_parser.c +91 -94
  45. data/ext/libxml/ruby_xml_parser.h +12 -12
  46. data/ext/libxml/ruby_xml_parser_context.c +999 -1001
  47. data/ext/libxml/ruby_xml_parser_context.h +10 -10
  48. data/ext/libxml/ruby_xml_parser_options.c +66 -66
  49. data/ext/libxml/ruby_xml_parser_options.h +12 -12
  50. data/ext/libxml/ruby_xml_reader.c +1239 -1228
  51. data/ext/libxml/ruby_xml_reader.h +17 -17
  52. data/ext/libxml/ruby_xml_relaxng.c +110 -111
  53. data/ext/libxml/ruby_xml_relaxng.h +10 -10
  54. data/ext/libxml/ruby_xml_sax2_handler.c +326 -328
  55. data/ext/libxml/ruby_xml_sax2_handler.h +10 -10
  56. data/ext/libxml/ruby_xml_sax_parser.c +116 -120
  57. data/ext/libxml/ruby_xml_sax_parser.h +10 -10
  58. data/ext/libxml/ruby_xml_schema.c +278 -301
  59. data/ext/libxml/ruby_xml_schema.h +809 -809
  60. data/ext/libxml/ruby_xml_schema_attribute.c +109 -109
  61. data/ext/libxml/ruby_xml_schema_attribute.h +15 -15
  62. data/ext/libxml/ruby_xml_schema_element.c +95 -94
  63. data/ext/libxml/ruby_xml_schema_element.h +14 -14
  64. data/ext/libxml/ruby_xml_schema_facet.c +52 -52
  65. data/ext/libxml/ruby_xml_schema_facet.h +13 -13
  66. data/ext/libxml/ruby_xml_schema_type.c +232 -259
  67. data/ext/libxml/ruby_xml_schema_type.h +9 -9
  68. data/ext/libxml/ruby_xml_version.h +9 -9
  69. data/ext/libxml/ruby_xml_writer.c +1133 -1137
  70. data/ext/libxml/ruby_xml_writer.h +10 -10
  71. data/ext/libxml/ruby_xml_xinclude.c +16 -16
  72. data/ext/libxml/ruby_xml_xinclude.h +11 -11
  73. data/ext/libxml/ruby_xml_xpath.c +194 -188
  74. data/ext/libxml/ruby_xml_xpath.h +13 -13
  75. data/ext/libxml/ruby_xml_xpath_context.c +360 -361
  76. data/ext/libxml/ruby_xml_xpath_context.h +9 -9
  77. data/ext/libxml/ruby_xml_xpath_expression.c +81 -81
  78. data/ext/libxml/ruby_xml_xpath_expression.h +10 -10
  79. data/ext/libxml/ruby_xml_xpath_object.c +338 -335
  80. data/ext/libxml/ruby_xml_xpath_object.h +17 -17
  81. data/ext/libxml/ruby_xml_xpointer.c +99 -99
  82. data/ext/libxml/ruby_xml_xpointer.h +11 -11
  83. data/ext/vc/libxml_ruby.sln +17 -15
  84. data/lib/libxml-ruby.rb +30 -0
  85. data/lib/libxml.rb +3 -33
  86. data/lib/libxml/node.rb +2 -78
  87. data/lib/libxml/parser.rb +0 -266
  88. data/lib/libxml/sax_parser.rb +0 -17
  89. data/lib/libxml/schema.rb +47 -66
  90. data/lib/libxml/schema/attribute.rb +19 -19
  91. data/lib/libxml/schema/element.rb +19 -27
  92. data/lib/libxml/schema/type.rb +21 -29
  93. data/libxml-ruby.gemspec +48 -44
  94. data/script/benchmark/depixelate +634 -634
  95. data/script/benchmark/hamlet.xml +9054 -9054
  96. data/script/benchmark/parsecount +170 -170
  97. data/script/benchmark/throughput +41 -41
  98. data/script/test +6 -6
  99. data/setup.rb +0 -1
  100. data/test/c14n/given/example-1.xml +14 -14
  101. data/test/c14n/given/example-2.xml +11 -11
  102. data/test/c14n/given/example-3.xml +18 -18
  103. data/test/c14n/given/example-4.xml +9 -9
  104. data/test/c14n/given/example-5.xml +12 -12
  105. data/test/c14n/given/example-6.xml +2 -2
  106. data/test/c14n/given/example-7.xml +11 -11
  107. data/test/c14n/given/example-8.xml +11 -11
  108. data/test/c14n/given/example-8.xpath +9 -9
  109. data/test/c14n/result/1-1-without-comments/example-1 +3 -3
  110. data/test/c14n/result/1-1-without-comments/example-2 +10 -10
  111. data/test/c14n/result/1-1-without-comments/example-3 +13 -13
  112. data/test/c14n/result/1-1-without-comments/example-4 +8 -8
  113. data/test/c14n/result/1-1-without-comments/example-5 +2 -2
  114. data/test/c14n/result/with-comments/example-1 +5 -5
  115. data/test/c14n/result/with-comments/example-2 +10 -10
  116. data/test/c14n/result/with-comments/example-3 +13 -13
  117. data/test/c14n/result/with-comments/example-4 +8 -8
  118. data/test/c14n/result/with-comments/example-5 +3 -3
  119. data/test/c14n/result/without-comments/example-1 +3 -3
  120. data/test/c14n/result/without-comments/example-2 +10 -10
  121. data/test/c14n/result/without-comments/example-3 +13 -13
  122. data/test/c14n/result/without-comments/example-4 +8 -8
  123. data/test/c14n/result/without-comments/example-5 +2 -2
  124. data/test/model/atom.xml +12 -12
  125. data/test/model/bands.iso-8859-1.xml +4 -4
  126. data/test/model/bands.utf-8.xml +4 -4
  127. data/test/model/bands.xml +4 -4
  128. data/test/model/books.xml +153 -153
  129. data/test/model/cwm_1_0.xml +11336 -0
  130. data/test/model/merge_bug_data.xml +58 -58
  131. data/test/model/ruby-lang.html +238 -238
  132. data/test/model/rubynet.xml +79 -79
  133. data/test/model/shiporder.rnc +28 -28
  134. data/test/model/shiporder.rng +86 -86
  135. data/test/model/shiporder.xml +22 -22
  136. data/test/model/shiporder.xsd +39 -39
  137. data/test/model/soap.xml +27 -27
  138. data/test/model/xinclude.xml +4 -4
  139. data/test/test.xml +2 -0
  140. data/test/{tc_attr.rb → test_attr.rb} +23 -25
  141. data/test/{tc_attr_decl.rb → test_attr_decl.rb} +13 -14
  142. data/test/{tc_attributes.rb → test_attributes.rb} +11 -18
  143. data/test/{tc_canonicalize.rb → test_canonicalize.rb} +36 -41
  144. data/test/test_deprecated_require.rb +12 -0
  145. data/test/{tc_document.rb → test_document.rb} +32 -27
  146. data/test/test_document_write.rb +146 -0
  147. data/test/{tc_dtd.rb → test_dtd.rb} +28 -29
  148. data/test/{tc_encoding.rb → test_encoding.rb} +129 -126
  149. data/test/{tc_encoding_sax.rb → test_encoding_sax.rb} +7 -6
  150. data/test/test_error.rb +178 -0
  151. data/test/test_helper.rb +3 -10
  152. data/test/test_html_parser.rb +162 -0
  153. data/test/test_html_parser_context.rb +23 -0
  154. data/test/test_namespace.rb +60 -0
  155. data/test/{tc_namespaces.rb → test_namespaces.rb} +34 -44
  156. data/test/{tc_node.rb → test_node.rb} +68 -47
  157. data/test/{tc_node_cdata.rb → test_node_cdata.rb} +12 -13
  158. data/test/{tc_node_comment.rb → test_node_comment.rb} +7 -8
  159. data/test/{tc_node_copy.rb → test_node_copy.rb} +4 -6
  160. data/test/{tc_node_edit.rb → test_node_edit.rb} +23 -41
  161. data/test/{tc_node_pi.rb → test_node_pi.rb} +37 -40
  162. data/test/{tc_node_text.rb → test_node_text.rb} +10 -12
  163. data/test/{tc_node_write.rb → test_node_write.rb} +18 -29
  164. data/test/test_node_xlink.rb +28 -0
  165. data/test/test_parser.rb +324 -0
  166. data/test/{tc_parser_context.rb → test_parser_context.rb} +41 -42
  167. data/test/{tc_properties.rb → test_properties.rb} +6 -7
  168. data/test/test_reader.rb +363 -0
  169. data/test/test_relaxng.rb +53 -0
  170. data/test/{tc_sax_parser.rb → test_sax_parser.rb} +36 -37
  171. data/test/{tc_schema.rb → test_schema.rb} +43 -37
  172. data/test/test_suite.rb +38 -40
  173. data/test/{tc_traversal.rb → test_traversal.rb} +5 -6
  174. data/test/{tc_writer.rb → test_writer.rb} +468 -448
  175. data/test/{tc_xinclude.rb → test_xinclude.rb} +4 -5
  176. data/test/test_xml.rb +262 -0
  177. data/test/{tc_xpath.rb → test_xpath.rb} +31 -32
  178. data/test/{tc_xpath_context.rb → test_xpath_context.rb} +8 -9
  179. data/test/test_xpath_expression.rb +37 -0
  180. data/test/{tc_xpointer.rb → test_xpointer.rb} +16 -18
  181. metadata +117 -95
  182. data/lib/libxml/ns.rb +0 -22
  183. data/lib/libxml/properties.rb +0 -23
  184. data/lib/libxml/reader.rb +0 -29
  185. data/lib/libxml/xpath_object.rb +0 -16
  186. data/test/etc_doc_to_s.rb +0 -21
  187. data/test/ets_doc_file.rb +0 -17
  188. data/test/ets_doc_to_s.rb +0 -23
  189. data/test/ets_gpx.rb +0 -28
  190. data/test/ets_node_gc.rb +0 -23
  191. data/test/ets_test.xml +0 -2
  192. data/test/ets_tsr.rb +0 -11
  193. data/test/tc_deprecated_require.rb +0 -13
  194. data/test/tc_document_write.rb +0 -196
  195. data/test/tc_error.rb +0 -180
  196. data/test/tc_html_parser.rb +0 -153
  197. data/test/tc_html_parser_context.rb +0 -24
  198. data/test/tc_namespace.rb +0 -62
  199. data/test/tc_node_xlink.rb +0 -29
  200. data/test/tc_parser.rb +0 -381
  201. data/test/tc_reader.rb +0 -400
  202. data/test/tc_relaxng.rb +0 -54
  203. data/test/tc_xml.rb +0 -226
  204. data/test/tc_xpath_expression.rb +0 -38
@@ -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.8.0"
5
- #define RUBY_LIBXML_VERNUM 280
6
- #define RUBY_LIBXML_VER_MAJ 2
7
- #define RUBY_LIBXML_VER_MIN 8
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.2.1"
5
+ #define RUBY_LIBXML_VERNUM 321
6
+ #define RUBY_LIBXML_VER_MAJ 3
7
+ #define RUBY_LIBXML_VER_MIN 2
8
+ #define RUBY_LIBXML_VER_MIC 1
9
+ #define RUBY_LIBXML_VER_PATCH 0
@@ -1,1137 +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
- 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(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;
284
- rxml_writer_object *rwo;
285
-
286
- ret = Qnil;
287
- rwo = rxml_textwriter_get(self);
288
- if (-1 == (ret = xmlTextWriterFlush(rwo->writer))) {
289
- rxml_raise(&xmlLastError);
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(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
- int argc, ret;
338
- rxml_writer_object *rwo;
339
- const xmlChar *argv[XMLWRITER_MAX_STRING_ARGS];
340
- VALUE utf8[XMLWRITER_MAX_STRING_ARGS], orig[XMLWRITER_MAX_STRING_ARGS];
341
-
342
- if (strings_count > XMLWRITER_MAX_STRING_ARGS) {
343
- rb_bug("more arguments than expected");
344
- }
345
- ret = -1;
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;
769
- rxml_writer_object *rwo;
770
- const char *xencoding, *xstandalone;
771
-
772
- options = Qnil;
773
- xstandalone = xencoding = NULL;
774
- rb_scan_args(argc, argv, "01", &options);
775
- if (!NIL_P(options)) {
776
- VALUE encoding, standalone;
777
-
778
- encoding = standalone = Qnil;
779
- Check_Type(options, T_HASH);
780
- encoding = rb_hash_aref(options, sEncoding);
781
- xencoding = NIL_P(encoding) ? NULL : xmlGetCharEncodingName(NUM2INT(encoding));
782
- standalone = rb_hash_aref(options, sStandalone);
783
- if (NIL_P(standalone)) {
784
- xstandalone = NULL;
785
- } else {
786
- xstandalone = RTEST(standalone) ? "yes" : "no";
787
- }
788
- }
789
- rwo = rxml_textwriter_get(self);
790
- #ifdef HAVE_RUBY_ENCODING_H
791
- rwo->encoding = rxml_figure_encoding(xencoding);
792
- #endif /* !HAVE_RUBY_ENCODING_H */
793
- ret = xmlTextWriterStartDocument(rwo->writer, NULL, xencoding, xstandalone);
794
-
795
- return (-1 == ret ? Qfalse : Qtrue);
796
- }
797
-
798
- /* call-seq:
799
- * writer.end_document -> (true|false)
800
- *
801
- * Ends current document. Returns +false+ on failure.
802
- */
803
- static VALUE rxml_writer_end_document(VALUE self)
804
- {
805
- return numeric_rxml_writer_void(self, xmlTextWriterEndDocument);
806
- }
807
-
808
- /* call-seq:
809
- * writer.start_pi(target) -> (true|false)
810
- *
811
- * Starts a new processing instruction. Returns +false+ on failure.
812
- */
813
- static VALUE rxml_writer_start_pi(VALUE self, VALUE target)
814
- {
815
- return numeric_rxml_writer_string(self, target, xmlTextWriterStartPI);
816
- }
817
-
818
- /* call-seq:
819
- * writer.end_pi -> (true|false)
820
- *
821
- * Ends current processing instruction. Returns +false+ on failure.
822
- */
823
- static VALUE rxml_writer_end_pi(VALUE self)
824
- {
825
- return numeric_rxml_writer_void(self, xmlTextWriterEndPI);
826
- }
827
-
828
- /* call-seq:
829
- * writer.start_dtd(qualifiedName, publicId, systemId) -> (true|false)
830
- *
831
- * Starts a DTD. Returns +false+ on failure.
832
- */
833
- static VALUE rxml_writer_start_dtd(int argc, VALUE *argv, VALUE self)
834
- {
835
- VALUE name, pubid, sysid;
836
-
837
- rb_scan_args(argc, argv, "12", &name, &pubid, &sysid);
838
-
839
- return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterStartDTD, name, pubid, sysid);
840
- }
841
-
842
- /* call-seq:
843
- * writer.start_dtd_element(qualifiedName) -> (true|false)
844
- *
845
- * Starts a DTD element (<!ELEMENT ... >). Returns +false+ on failure.
846
- */
847
- static VALUE rxml_writer_start_dtd_element(VALUE self, VALUE name)
848
- {
849
- return numeric_rxml_writer_string(self, name, xmlTextWriterStartDTDElement);
850
- }
851
-
852
- /* call-seq:
853
- * writer.start_dtd_entity(name, pe = false) -> (true|false)
854
- *
855
- * Starts a DTD entity (<!ENTITY ... >). Returns +false+ on failure.
856
- */
857
- static VALUE rxml_writer_start_dtd_entity(int argc, VALUE *argv, VALUE self)
858
- {
859
- VALUE name, pe;
860
-
861
- rb_scan_args(argc, argv, "11", &name, &pe);
862
- if (NIL_P(pe)) {
863
- pe = Qfalse;
864
- }
865
-
866
- return numeric_rxml_writer_va_strings(self, pe, 1, xmlTextWriterStartDTDEntity, name);
867
- }
868
-
869
- /* call-seq:
870
- * writer.start_dtd_attlist(name) -> (true|false)
871
- *
872
- * Starts a DTD attribute list (<!ATTLIST ... >). Returns +false+ on failure.
873
- */
874
- static VALUE rxml_writer_start_dtd_attlist(VALUE self, VALUE name)
875
- {
876
- return numeric_rxml_writer_string(self, name, xmlTextWriterStartDTDAttlist);
877
- }
878
-
879
- /* call-seq:
880
- * writer.end_dtd -> (true|false)
881
- *
882
- * Ends current DTD, returns +false+ on failure.
883
- */
884
- static VALUE rxml_writer_end_dtd(VALUE self)
885
- {
886
- return numeric_rxml_writer_void(self, xmlTextWriterEndDTD);
887
- }
888
-
889
- /* call-seq:
890
- * writer.end_dtd_entity -> (true|false)
891
- *
892
- * Ends current DTD entity, returns +false+ on failure.
893
- */
894
- static VALUE rxml_writer_end_dtd_entity(VALUE self)
895
- {
896
- return numeric_rxml_writer_void(self, xmlTextWriterEndDTDEntity);
897
- }
898
-
899
- /* call-seq:
900
- * writer.end_dtd_attlist -> (true|false)
901
- *
902
- * Ends current DTD attribute list, returns +false+ on failure.
903
- */
904
- static VALUE rxml_writer_end_dtd_attlist(VALUE self)
905
- {
906
- return numeric_rxml_writer_void(self, xmlTextWriterEndDTDAttlist);
907
- }
908
-
909
- /* call-seq:
910
- * writer.end_dtd_element -> (true|false)
911
- *
912
- * Ends current DTD element, returns +false+ on failure.
913
- */
914
- static VALUE rxml_writer_end_dtd_element(VALUE self)
915
- {
916
- return numeric_rxml_writer_void(self, xmlTextWriterEndDTDElement);
917
- }
918
-
919
- /* call-seq:
920
- * writer.write_dtd(name [ [ [, publicId ], systemId ], subset ]) -> (true|false)
921
- *
922
- * Writes a DTD, all at once. Returns +false+ on failure.
923
- * - name: dtd name
924
- * - publicId: external subset public identifier, use nil for a SYSTEM doctype
925
- * - systemId: external subset system identifier
926
- * - subset: content
927
- *
928
- * Examples:
929
- * writer.write_dtd 'html'
930
- * #=> <!DOCTYPE html>
931
- * writer.write_dtd 'docbook', nil, 'http://www.docbook.org/xml/5.0/dtd/docbook.dtd'
932
- * #=> <!DOCTYPE docbook SYSTEM "http://www.docbook.org/xml/5.0/dtd/docbook.dtd">
933
- * writer.write_dtd 'html', '-//W3C//DTD XHTML 1.1//EN', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
934
- * #=> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
935
- * writer.write_dtd 'person', nil, nil, '<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>'
936
- * #=> <!DOCTYPE person [<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>]>
937
- */
938
- static VALUE rxml_writer_write_dtd(int argc, VALUE *argv, VALUE self)
939
- {
940
- VALUE name, pubid, sysid, subset;
941
-
942
- rb_scan_args(argc, argv, "13", &name, &pubid, &sysid, &subset);
943
-
944
- return numeric_rxml_writer_va_strings(self, Qundef, 4, xmlTextWriterWriteDTD, name, pubid, sysid, subset);
945
- }
946
-
947
- /* call-seq:
948
- * writer.write_dtd_attlist(name, content) -> (true|false)
949
- *
950
- * Writes a DTD attribute list, all at once. Returns +false+ on failure.
951
- * writer.write_dtd_attlist 'id', 'ID #IMPLIED'
952
- * #=> <!ATTLIST id ID #IMPLIED>
953
- */
954
- static VALUE rxml_writer_write_dtd_attlist(VALUE self, VALUE name, VALUE content)
955
- {
956
- return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteDTDAttlist, name, content);
957
- }
958
-
959
- /* call-seq:
960
- * writer.write_dtd_element(name, content) -> (true|false)
961
- *
962
- * Writes a full DTD element, all at once. Returns +false+ on failure.
963
- * writer.write_dtd_element 'person', '(firstname,lastname)'
964
- * #=> <!ELEMENT person (firstname,lastname)>
965
- */
966
- static VALUE rxml_writer_write_dtd_element(VALUE self, VALUE name, VALUE content)
967
- {
968
- return numeric_rxml_writer_va_strings(self, Qundef, 2, xmlTextWriterWriteDTDElement, name, content);
969
- }
970
-
971
- /* call-seq:
972
- * writer.write_dtd_entity(name, publicId, systemId, ndataid, content, pe) -> (true|false)
973
- *
974
- * Writes a DTD entity, all at once. Returns +false+ on failure.
975
- */
976
- static VALUE rxml_writer_write_dtd_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE content, VALUE pe)
977
- {
978
- return numeric_rxml_writer_va_strings(self, pe, 5, xmlTextWriterWriteDTDEntity, name, pubid, sysid, ndataid, content);
979
- }
980
-
981
- /* call-seq:
982
- * writer.write_dtd_external_entity(name, publicId, systemId, ndataid, pe) -> (true|false)
983
- *
984
- * Writes a DTD external entity. The entity must have been started
985
- * with start_dtd_entity. Returns +false+ on failure.
986
- * - name: the name of the DTD entity
987
- * - publicId: the public identifier, which is an alternative to the system identifier
988
- * - systemId: the system identifier, which is the URI of the DTD
989
- * - ndataid: the xml notation name
990
- * - pe: +true+ if this is a parameter entity (to be used only in the DTD
991
- * itself), +false+ if not
992
- */
993
- static VALUE rxml_writer_write_dtd_external_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE pe)
994
- {
995
- return numeric_rxml_writer_va_strings(self, pe, 4, xmlTextWriterWriteDTDExternalEntity, name, pubid, sysid, ndataid);
996
- }
997
-
998
- /* call-seq:
999
- * writer.write_dtd_external_entity_contents(publicId, systemId, ndataid) -> (true|false)
1000
- *
1001
- * Writes the contents of a DTD external entity, all at once. Returns +false+ on failure.
1002
- */
1003
- static VALUE rxml_writer_write_dtd_external_entity_contents(VALUE self, VALUE pubid, VALUE sysid, VALUE ndataid)
1004
- {
1005
- return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterWriteDTDExternalEntityContents, pubid, sysid, ndataid);
1006
- }
1007
-
1008
- /* call-seq:
1009
- * writer.write_dtd_internal_entity(name, content, pe) -> (true|false)
1010
- *
1011
- * Writes a DTD internal entity, all at once. Returns +false+ on failure.
1012
- *
1013
- * Examples:
1014
- * writer.write_dtd_entity 'Shape', '(rect|circle|poly|default)', true
1015
- * #=> <!ENTITY % Shape "(rect|circle|poly|default)">
1016
- * writer.write_dtd_entity 'delta', '&#948;', false
1017
- * #=> <!ENTITY delta "&#948;">
1018
- */
1019
- static VALUE rxml_writer_write_dtd_internal_entity(VALUE self, VALUE name, VALUE content, VALUE pe)
1020
- {
1021
- return numeric_rxml_writer_va_strings(self, pe, 2, xmlTextWriterWriteDTDInternalEntity, name, content);
1022
- }
1023
-
1024
- /* call-seq:
1025
- * writer.write_dtd_notation(name, publicId, systemId) -> (true|false)
1026
- *
1027
- * Writes a DTD entity, all at once. Returns +false+ on failure.
1028
- */
1029
- static VALUE rxml_writer_write_dtd_notation(VALUE self, VALUE name, VALUE pubid, VALUE sysid)
1030
- {
1031
- return numeric_rxml_writer_va_strings(self, Qundef, 3, xmlTextWriterWriteDTDNotation, name, pubid, sysid);
1032
- }
1033
-
1034
- #if LIBXML_VERSION >= 20900
1035
- /* call-seq:
1036
- * writer.set_quote_char(...) -> (true|false)
1037
- *
1038
- * Sets the character used to quote attributes. Returns +false+ on failure.
1039
- *
1040
- * Notes:
1041
- * - only " (default) and ' characters are valid
1042
- * - availability: libxml2 >= 2.9.0
1043
- */
1044
- static VALUE rxml_writer_set_quote_char(VALUE self, VALUE quotechar)
1045
- {
1046
- int ret;
1047
- const char *xquotechar;
1048
- rxml_writer_object *rwo;
1049
-
1050
- rwo = rxml_textwriter_get(self);
1051
- xquotechar = StringValueCStr(quotechar);
1052
- ret = xmlTextWriterSetQuoteChar(rwo->writer, (xmlChar) xquotechar[0]);
1053
-
1054
- return (-1 == ret ? Qfalse : Qtrue);
1055
- }
1056
- #endif /* LIBXML_VERSION >= 20900 */
1057
-
1058
- #endif /* LIBXML_WRITER_ENABLED */
1059
-
1060
-
1061
- /* grep -P 'xmlTextWriter(Start|End|Write)(?!DTD|V?Format)[^(]+' /usr/include/libxml2/libxml/xmlwriter.h */
1062
- void rxml_init_writer(void)
1063
- {
1064
- sEncoding = ID2SYM(rb_intern("encoding"));
1065
- sStandalone = ID2SYM(rb_intern("standalone"));
1066
-
1067
- cXMLWriter = rb_define_class_under(mXML, "Writer", rb_cObject);
1068
-
1069
- #ifdef LIBXML_WRITER_ENABLED
1070
- rb_define_singleton_method(cXMLWriter, "io", rxml_writer_io, 1);
1071
- rb_define_singleton_method(cXMLWriter, "file", rxml_writer_file, 1);
1072
- rb_define_singleton_method(cXMLWriter, "document", rxml_writer_doc, 0);
1073
- rb_define_singleton_method(cXMLWriter, "string", rxml_writer_string, 0);
1074
-
1075
- /* misc */
1076
- #if LIBXML_VERSION >= 20605
1077
- rb_define_method(cXMLWriter, "set_indent", rxml_writer_set_indent, 1);
1078
- rb_define_method(cXMLWriter, "set_indent_string", rxml_writer_set_indent_string, 1);
1079
- #endif /* LIBXML_VERSION >= 20605 */
1080
- #if LIBXML_VERSION >= 20900
1081
- rb_define_method(cXMLWriter, "set_quote_char", rxml_writer_set_quote_char, 1);
1082
- #endif /* LIBXML_VERSION >= 20900 */
1083
- rb_define_method(cXMLWriter, "flush", rxml_writer_flush, -1);
1084
- rb_define_method(cXMLWriter, "start_dtd", rxml_writer_start_dtd, -1);
1085
- rb_define_method(cXMLWriter, "start_dtd_entity", rxml_writer_start_dtd_entity, -1);
1086
- rb_define_method(cXMLWriter, "start_dtd_attlist", rxml_writer_start_dtd_attlist, 1);
1087
- rb_define_method(cXMLWriter, "start_dtd_element", rxml_writer_start_dtd_element, 1);
1088
- rb_define_method(cXMLWriter, "write_dtd", rxml_writer_write_dtd, -1);
1089
- rb_define_method(cXMLWriter, "write_dtd_attlist", rxml_writer_write_dtd_attlist, 2);
1090
- rb_define_method(cXMLWriter, "write_dtd_element", rxml_writer_write_dtd_element, 2);
1091
- rb_define_method(cXMLWriter, "write_dtd_entity", rxml_writer_write_dtd_entity, 6);
1092
- rb_define_method(cXMLWriter, "write_dtd_external_entity", rxml_writer_write_dtd_external_entity, 5);
1093
- rb_define_method(cXMLWriter, "write_dtd_external_entity_contents", rxml_writer_write_dtd_external_entity_contents, 3);
1094
- rb_define_method(cXMLWriter, "write_dtd_internal_entity", rxml_writer_write_dtd_internal_entity, 3);
1095
- rb_define_method(cXMLWriter, "write_dtd_notation", rxml_writer_write_dtd_notation, 3);
1096
- rb_define_method(cXMLWriter, "end_dtd", rxml_writer_end_dtd, 0);
1097
- rb_define_method(cXMLWriter, "end_dtd_entity", rxml_writer_end_dtd_entity, 0);
1098
- rb_define_method(cXMLWriter, "end_dtd_attlist", rxml_writer_end_dtd_attlist, 0);
1099
- rb_define_method(cXMLWriter, "end_dtd_element", rxml_writer_end_dtd_element, 0);
1100
-
1101
- /* tag by parts */
1102
- rb_define_method(cXMLWriter, "write_raw", rxml_writer_write_raw, 1);
1103
- rb_define_method(cXMLWriter, "write_string", rxml_writer_write_string, 1);
1104
-
1105
- rb_define_method(cXMLWriter, "start_cdata", rxml_writer_start_cdata, 0);
1106
- rb_define_method(cXMLWriter, "end_cdata", rxml_writer_end_cdata, 0);
1107
- rb_define_method(cXMLWriter, "start_attribute", rxml_writer_start_attribute, 1);
1108
- rb_define_method(cXMLWriter, "start_attribute_ns", rxml_writer_start_attribute_ns, -1);
1109
- rb_define_method(cXMLWriter, "end_attribute", rxml_writer_end_attribute, 0);
1110
- rb_define_method(cXMLWriter, "start_element", rxml_writer_start_element, 1);
1111
- rb_define_method(cXMLWriter, "start_element_ns", rxml_writer_start_element_ns, -1);
1112
- rb_define_method(cXMLWriter, "end_element", rxml_writer_end_element, 0);
1113
- rb_define_method(cXMLWriter, "full_end_element", rxml_writer_full_end_element, 0);
1114
- rb_define_method(cXMLWriter, "start_document", rxml_writer_start_document, -1);
1115
- rb_define_method(cXMLWriter, "end_document", rxml_writer_end_document, 0);
1116
- #if LIBXML_VERSION >= 20607
1117
- rb_define_method(cXMLWriter, "start_comment", rxml_writer_start_comment, 0);
1118
- rb_define_method(cXMLWriter, "end_comment", rxml_writer_end_comment, 0);
1119
- #endif /* LIBXML_VERSION >= 20607 */
1120
- rb_define_method(cXMLWriter, "start_pi", rxml_writer_start_pi, 1);
1121
- rb_define_method(cXMLWriter, "end_pi", rxml_writer_end_pi, 0);
1122
-
1123
- /* full tag at once */
1124
- rb_define_method(cXMLWriter, "write_attribute", rxml_writer_write_attribute, 2);
1125
- rb_define_method(cXMLWriter, "write_attribute_ns", rxml_writer_write_attribute_ns, -1);
1126
- rb_define_method(cXMLWriter, "write_comment", rxml_writer_write_comment, 1);
1127
- rb_define_method(cXMLWriter, "write_cdata", rxml_writer_write_cdata, 1);
1128
- rb_define_method(cXMLWriter, "write_element", rxml_writer_write_element, -1);
1129
- rb_define_method(cXMLWriter, "write_element_ns", rxml_writer_write_element_ns, -1);
1130
- rb_define_method(cXMLWriter, "write_pi", rxml_writer_write_pi, 2);
1131
-
1132
- rb_define_method(cXMLWriter, "result", rxml_writer_result, 0);
1133
-
1134
- rb_undef_method(CLASS_OF(cXMLWriter), "new");
1135
- #endif
1136
- }
1137
-
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
+