libxml-ruby 4.1.2 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/{HISTORY → CHANGELOG.md} +875 -887
  3. data/LICENSE +20 -20
  4. data/README.md +67 -0
  5. data/Rakefile +77 -98
  6. data/ext/libxml/extconf.rb +39 -20
  7. data/ext/libxml/libxml.c +67 -79
  8. data/ext/libxml/ruby_libxml.h +43 -44
  9. data/ext/libxml/ruby_xml.c +556 -899
  10. data/ext/libxml/ruby_xml.h +9 -10
  11. data/ext/libxml/ruby_xml_attr.c +338 -333
  12. data/ext/libxml/ruby_xml_attr.h +13 -12
  13. data/ext/libxml/ruby_xml_attr_decl.c +19 -12
  14. data/ext/libxml/ruby_xml_attr_decl.h +11 -11
  15. data/ext/libxml/ruby_xml_attributes.c +18 -9
  16. data/ext/libxml/ruby_xml_attributes.h +17 -15
  17. data/ext/libxml/ruby_xml_document.c +1142 -1129
  18. data/ext/libxml/ruby_xml_document.h +12 -11
  19. data/ext/libxml/ruby_xml_dtd.c +287 -248
  20. data/ext/libxml/ruby_xml_dtd.h +10 -9
  21. data/ext/libxml/ruby_xml_encoding.c +250 -250
  22. data/ext/libxml/ruby_xml_encoding.h +20 -18
  23. data/ext/libxml/ruby_xml_error.c +1003 -996
  24. data/ext/libxml/ruby_xml_error.h +14 -14
  25. data/ext/libxml/ruby_xml_html_parser.c +91 -91
  26. data/ext/libxml/ruby_xml_html_parser.h +10 -10
  27. data/ext/libxml/ruby_xml_html_parser_context.c +364 -337
  28. data/ext/libxml/ruby_xml_html_parser_context.h +11 -10
  29. data/ext/libxml/ruby_xml_html_parser_options.c +48 -48
  30. data/ext/libxml/ruby_xml_html_parser_options.h +10 -10
  31. data/ext/libxml/ruby_xml_input_cbg.c +216 -188
  32. data/ext/libxml/ruby_xml_input_cbg.h +20 -20
  33. data/ext/libxml/ruby_xml_io.c +49 -47
  34. data/ext/libxml/ruby_xml_io.h +10 -10
  35. data/ext/libxml/ruby_xml_namespace.c +159 -154
  36. data/ext/libxml/ruby_xml_namespace.h +11 -10
  37. data/ext/libxml/ruby_xml_namespaces.c +313 -293
  38. data/ext/libxml/ruby_xml_namespaces.h +9 -9
  39. data/ext/libxml/ruby_xml_node.c +1433 -1398
  40. data/ext/libxml/ruby_xml_node.h +15 -13
  41. data/ext/libxml/ruby_xml_parser.c +91 -91
  42. data/ext/libxml/ruby_xml_parser.h +10 -10
  43. data/ext/libxml/ruby_xml_parser_context.c +1016 -1001
  44. data/ext/libxml/ruby_xml_parser_context.h +11 -10
  45. data/ext/libxml/ruby_xml_parser_options.c +74 -66
  46. data/ext/libxml/ruby_xml_parser_options.h +10 -10
  47. data/ext/libxml/ruby_xml_reader.c +40 -31
  48. data/ext/libxml/ruby_xml_reader.h +14 -14
  49. data/ext/libxml/ruby_xml_registry.c +31 -0
  50. data/ext/libxml/ruby_xml_registry.h +22 -0
  51. data/ext/libxml/ruby_xml_relaxng.c +21 -5
  52. data/ext/libxml/ruby_xml_relaxng.h +9 -8
  53. data/ext/libxml/ruby_xml_sax2_handler.c +326 -326
  54. data/ext/libxml/ruby_xml_sax2_handler.h +10 -10
  55. data/ext/libxml/ruby_xml_sax_parser.c +108 -116
  56. data/ext/libxml/ruby_xml_sax_parser.h +10 -10
  57. data/ext/libxml/ruby_xml_schema.c +22 -15
  58. data/ext/libxml/ruby_xml_schema.h +26 -25
  59. data/ext/libxml/ruby_xml_schema_attribute.c +7 -7
  60. data/ext/libxml/ruby_xml_schema_attribute.h +37 -37
  61. data/ext/libxml/ruby_xml_schema_element.c +8 -8
  62. data/ext/libxml/ruby_xml_schema_element.h +11 -11
  63. data/ext/libxml/ruby_xml_schema_facet.c +50 -50
  64. data/ext/libxml/ruby_xml_schema_facet.h +9 -9
  65. data/ext/libxml/ruby_xml_schema_type.c +12 -19
  66. data/ext/libxml/ruby_xml_schema_type.h +9 -9
  67. data/ext/libxml/ruby_xml_version.h +9 -9
  68. data/ext/libxml/ruby_xml_writer.c +183 -185
  69. data/ext/libxml/ruby_xml_writer.h +6 -6
  70. data/ext/libxml/ruby_xml_xinclude.c +20 -20
  71. data/ext/libxml/ruby_xml_xinclude.h +11 -11
  72. data/ext/libxml/ruby_xml_xpath.c +195 -195
  73. data/ext/libxml/ruby_xml_xpath.h +15 -15
  74. data/ext/libxml/ruby_xml_xpath_context.c +408 -362
  75. data/ext/libxml/ruby_xml_xpath_context.h +9 -9
  76. data/ext/libxml/ruby_xml_xpath_expression.c +12 -6
  77. data/ext/libxml/ruby_xml_xpath_expression.h +11 -10
  78. data/ext/libxml/ruby_xml_xpath_object.c +69 -54
  79. data/ext/libxml/ruby_xml_xpath_object.h +19 -17
  80. data/ext/vc/libxml_ruby/libxml_ruby.vcxproj +271 -0
  81. data/ext/xcode/libxml-ruby.xcodeproj/project.pbxproj +633 -0
  82. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  83. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  84. data/ext/xcode/libxml-ruby.xcodeproj/xcshareddata/xcschemes/libxml-ruby.xcscheme +80 -0
  85. data/lib/libxml/attr.rb +122 -122
  86. data/lib/libxml/attr_decl.rb +80 -80
  87. data/lib/libxml/attributes.rb +13 -13
  88. data/lib/libxml/document.rb +165 -194
  89. data/lib/libxml/error.rb +122 -95
  90. data/lib/libxml/html_parser.rb +80 -96
  91. data/lib/libxml/namespace.rb +61 -61
  92. data/lib/libxml/namespaces.rb +37 -37
  93. data/lib/libxml/node.rb +321 -323
  94. data/lib/libxml/parser.rb +94 -100
  95. data/lib/libxml/sax_callbacks.rb +179 -179
  96. data/lib/libxml/sax_parser.rb +40 -40
  97. data/lib/libxml/schema/attribute.rb +27 -19
  98. data/lib/libxml/schema/element.rb +20 -0
  99. data/lib/libxml/schema/type.rb +44 -21
  100. data/lib/libxml/schema.rb +47 -47
  101. data/lib/libxml/tree.rb +28 -28
  102. data/lib/libxml-ruby.rb +30 -30
  103. data/libxml-ruby.gemspec +46 -48
  104. data/test/c14n/result/1-1-without-comments/example-1 +3 -3
  105. data/test/c14n/result/1-1-without-comments/example-2 +10 -10
  106. data/test/c14n/result/1-1-without-comments/example-3 +13 -13
  107. data/test/c14n/result/1-1-without-comments/example-4 +8 -8
  108. data/test/c14n/result/1-1-without-comments/example-5 +2 -2
  109. data/test/c14n/result/with-comments/example-1 +5 -5
  110. data/test/c14n/result/with-comments/example-2 +10 -10
  111. data/test/c14n/result/with-comments/example-3 +13 -13
  112. data/test/c14n/result/with-comments/example-4 +8 -8
  113. data/test/c14n/result/with-comments/example-5 +3 -3
  114. data/test/c14n/result/without-comments/example-1 +3 -3
  115. data/test/c14n/result/without-comments/example-2 +10 -10
  116. data/test/c14n/result/without-comments/example-3 +13 -13
  117. data/test/c14n/result/without-comments/example-4 +8 -8
  118. data/test/c14n/result/without-comments/example-5 +2 -2
  119. data/test/test_attr.rb +179 -180
  120. data/test/test_attr_decl.rb +131 -131
  121. data/test/test_attributes.rb +135 -135
  122. data/test/test_canonicalize.rb +122 -120
  123. data/test/test_document.rb +138 -132
  124. data/test/test_document_write.rb +142 -145
  125. data/test/test_dtd.rb +134 -129
  126. data/test/test_encoding.rb +126 -129
  127. data/test/test_encoding_sax.rb +114 -114
  128. data/test/test_error.rb +197 -194
  129. data/test/test_helper.rb +21 -13
  130. data/test/test_html_parser.rb +166 -162
  131. data/test/test_html_parser_context.rb +22 -22
  132. data/test/test_input_callbacks.rb +36 -0
  133. data/test/test_namespace.rb +58 -60
  134. data/test/test_namespaces.rb +200 -200
  135. data/test/test_node.rb +251 -237
  136. data/test/test_node_cdata.rb +50 -50
  137. data/test/test_node_comment.rb +32 -32
  138. data/test/test_node_copy.rb +40 -40
  139. data/test/test_node_edit.rb +176 -158
  140. data/test/test_node_pi.rb +37 -37
  141. data/test/test_node_text.rb +69 -69
  142. data/test/test_node_write.rb +93 -96
  143. data/test/test_node_xlink.rb +28 -28
  144. data/test/test_parser.rb +297 -324
  145. data/test/test_parser_context.rb +198 -198
  146. data/test/test_properties.rb +38 -38
  147. data/test/test_reader.rb +413 -364
  148. data/test/test_relaxng.rb +59 -53
  149. data/test/test_sax_parser.rb +345 -326
  150. data/test/test_schema.rb +28 -0
  151. data/test/test_traversal.rb +152 -152
  152. data/test/test_writer.rb +469 -468
  153. data/test/test_xinclude.rb +20 -20
  154. data/test/test_xml.rb +164 -267
  155. data/test/test_xpath.rb +244 -244
  156. data/test/test_xpath_context.rb +87 -87
  157. data/test/test_xpath_expression.rb +37 -37
  158. metadata +49 -31
  159. data/README.rdoc +0 -217
  160. data/ext/libxml/extconf.h +0 -3
  161. data/ext/libxml/ruby_xml_cbg.c +0 -85
  162. data/lib/libxml/hpricot.rb +0 -78
  163. data/lib/libxml.rb +0 -5
  164. data/lib/xml/libxml.rb +0 -10
  165. data/lib/xml.rb +0 -14
  166. data/script/benchmark/depixelate +0 -634
  167. data/script/benchmark/hamlet.xml +0 -9055
  168. data/script/benchmark/parsecount +0 -170
  169. data/script/benchmark/sock_entries.xml +0 -507
  170. data/script/benchmark/throughput +0 -41
  171. data/script/test +0 -6
  172. data/test/test_deprecated_require.rb +0 -12
@@ -1,11 +1,12 @@
1
- /* Please see the LICENSE file for copyright and distribution information */
2
-
3
- #ifndef __RXML_DOCUMENT__
4
- #define __RXML_DOCUMENT__
5
-
6
- extern VALUE cXMLDocument;
7
- void rxml_init_document();
8
- VALUE rxml_document_wrap(xmlDocPtr xnode);
9
-
10
- typedef xmlChar * xmlCharPtr;
11
- #endif
1
+ /* Please see the LICENSE file for copyright and distribution information */
2
+
3
+ #ifndef __RXML_DOCUMENT__
4
+ #define __RXML_DOCUMENT__
5
+
6
+ extern VALUE cXMLDocument;
7
+ extern const rb_data_type_t rxml_document_data_type;
8
+ void rxml_init_document(void);
9
+ VALUE rxml_document_wrap(xmlDocPtr xnode);
10
+
11
+ typedef xmlChar * xmlCharPtr;
12
+ #endif
@@ -1,248 +1,287 @@
1
- #include "ruby_libxml.h"
2
- #include "ruby_xml_dtd.h"
3
-
4
- /*
5
- * Document-class: LibXML::XML::Dtd
6
- *
7
- * The XML::Dtd class is used to prepare DTD's for validation of xml
8
- * documents.
9
- *
10
- * DTDs can be created from a string or a pair of public and system identifiers.
11
- * Once a Dtd object is instantiated, an XML document can be validated by the
12
- * XML::Document#validate method providing the XML::Dtd object as parameeter.
13
- * The method will raise an exception if the document is
14
- * not valid.
15
- *
16
- * Basic usage:
17
- *
18
- * # parse DTD
19
- * dtd = XML::Dtd.new(<<EOF)
20
- * <!ELEMENT root (item*) >
21
- * <!ELEMENT item (#PCDATA) >
22
- * EOF
23
- *
24
- * # parse xml document to be validated
25
- * instance = XML::Document.file('instance.xml')
26
- *
27
- * # validate
28
- * instance.validate(dtd)
29
- */
30
-
31
- VALUE cXMLDtd;
32
-
33
- void rxml_dtd_free(xmlDtdPtr xdtd)
34
- {
35
- if (xdtd->doc == NULL && xdtd->parent == NULL)
36
- xmlFreeDtd(xdtd);
37
- }
38
-
39
- void rxml_dtd_mark(xmlDtdPtr xdtd)
40
- {
41
- if (xdtd && xdtd->doc)
42
- {
43
- VALUE doc = (VALUE)xdtd->doc->_private;
44
- rb_gc_mark(doc);
45
- }
46
- }
47
-
48
- static VALUE rxml_dtd_alloc(VALUE klass)
49
- {
50
- return Data_Wrap_Struct(klass, rxml_dtd_mark, rxml_dtd_free, NULL);
51
- }
52
-
53
- VALUE rxml_dtd_wrap(xmlDtdPtr xdtd)
54
- {
55
- return Data_Wrap_Struct(cXMLDtd, NULL, NULL, xdtd);
56
- }
57
-
58
- /*
59
- * call-seq:
60
- * dtd.external_id -> "string"
61
- *
62
- * Obtain this dtd's external identifer (for a PUBLIC DTD).
63
- */
64
- static VALUE rxml_dtd_external_id_get(VALUE self)
65
- {
66
- xmlDtdPtr xdtd;
67
- Data_Get_Struct(self, xmlDtd, xdtd);
68
-
69
-
70
- if (xdtd->ExternalID == NULL)
71
- return (Qnil);
72
- else
73
- return (rxml_new_cstr( xdtd->ExternalID, NULL));
74
- }
75
-
76
- /*
77
- * call-seq:
78
- * dtd.name -> "string"
79
- *
80
- * Obtain this dtd's name.
81
- */
82
- static VALUE rxml_dtd_name_get(VALUE self)
83
- {
84
- xmlDtdPtr xdtd;
85
- Data_Get_Struct(self, xmlDtd, xdtd);
86
-
87
-
88
- if (xdtd->name == NULL)
89
- return (Qnil);
90
- else
91
- return (rxml_new_cstr( xdtd->name, NULL));
92
- }
93
-
94
-
95
- /*
96
- * call-seq:
97
- * dtd.uri -> "string"
98
- *
99
- * Obtain this dtd's URI (for a SYSTEM or PUBLIC DTD).
100
- */
101
- static VALUE rxml_dtd_uri_get(VALUE self)
102
- {
103
- xmlDtdPtr xdtd;
104
- Data_Get_Struct(self, xmlDtd, xdtd);
105
-
106
- if (xdtd->SystemID == NULL)
107
- return (Qnil);
108
- else
109
- return (rxml_new_cstr( xdtd->SystemID, NULL));
110
- }
111
-
112
- /*
113
- * call-seq:
114
- * node.type -> num
115
- *
116
- * Obtain this node's type identifier.
117
- */
118
- static VALUE rxml_dtd_type(VALUE self)
119
- {
120
- xmlDtdPtr xdtd;
121
- Data_Get_Struct(self, xmlDtd, xdtd);
122
- return (INT2NUM(xdtd->type));
123
- }
124
-
125
- /*
126
- * call-seq:
127
- * XML::Dtd.new("DTD string") -> dtd
128
- * XML::Dtd.new("public", "system") -> dtd
129
- * XML::Dtd.new("name", "public", "system", document) -> external subset dtd
130
- * XML::Dtd.new("name", "public", "system", document, false) -> internal subset dtd
131
- * XML::Dtd.new("name", "public", "system", document, true) -> internal subset dtd
132
- *
133
- * Create a new Dtd from the specified public and system
134
- * identifiers.
135
- */
136
- static VALUE rxml_dtd_initialize(int argc, VALUE *argv, VALUE self)
137
- {
138
- VALUE external, system, dtd_string;
139
- xmlParserInputBufferPtr buffer;
140
- xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
141
- xmlChar *new_string;
142
- xmlDtdPtr xdtd;
143
-
144
- // 1 argument -- string --> parsujeme jako dtd
145
- // 2 arguments -- public, system --> bude se hledat
146
- // 3 arguments -- public, system, name --> creates an external subset (any parameter may be nil)
147
- // 4 arguments -- public, system, name, doc --> creates an external subset (any parameter may be nil)
148
- // 5 arguments -- public, system, name, doc, true --> creates an internal subset (all but last parameter may be nil)
149
- switch (argc)
150
- {
151
- case 3:
152
- case 4:
153
- case 5: {
154
- VALUE name, doc, internal;
155
- const xmlChar *xname = NULL, *xpublic = NULL, *xsystem = NULL;
156
- xmlDocPtr xdoc = NULL;
157
-
158
- rb_scan_args(argc, argv, "32", &external, &system, &name, &doc, &internal);
159
-
160
- if (external != Qnil) {
161
- Check_Type(external, T_STRING);
162
- xpublic = (const xmlChar*) StringValuePtr(external);
163
- }
164
- if (system != Qnil) {
165
- Check_Type(system, T_STRING);
166
- xsystem = (const xmlChar*) StringValuePtr(system);
167
- }
168
- if (name != Qnil) {
169
- Check_Type(name, T_STRING);
170
- xname = (const xmlChar*) StringValuePtr(name);
171
- }
172
- if (doc != Qnil) {
173
- if (rb_obj_is_kind_of(doc, cXMLDocument) == Qfalse)
174
- rb_raise(rb_eTypeError, "Must pass an LibXML::XML::Document object");
175
- Data_Get_Struct(doc, xmlDoc, xdoc);
176
- }
177
-
178
- if (internal == Qnil || internal == Qfalse)
179
- xdtd = xmlNewDtd(xdoc, xname, xpublic, xsystem);
180
- else
181
- xdtd = xmlCreateIntSubset(xdoc, xname, xpublic, xsystem);
182
-
183
- if (xdtd == NULL)
184
- rxml_raise(&xmlLastError);
185
-
186
- /* Document will free this dtd now. */
187
- RDATA(self)->dfree = NULL;
188
- DATA_PTR(self) = xdtd;
189
-
190
- xmlSetTreeDoc((xmlNodePtr) xdtd, xdoc);
191
- }
192
- break;
193
-
194
- case 2:
195
- rb_scan_args(argc, argv, "20", &external, &system);
196
-
197
- Check_Type(external, T_STRING);
198
- Check_Type(system, T_STRING);
199
-
200
- xdtd = xmlParseDTD((xmlChar*) StringValuePtr(external),
201
- (xmlChar*) StringValuePtr(system));
202
-
203
- if (xdtd == NULL)
204
- rxml_raise(&xmlLastError);
205
-
206
- DATA_PTR(self) = xdtd;
207
-
208
- xmlSetTreeDoc((xmlNodePtr) xdtd, NULL);
209
- break;
210
-
211
- case 1:
212
- rb_scan_args(argc, argv, "10", &dtd_string);
213
- Check_Type(dtd_string, T_STRING);
214
-
215
- /* Note that buffer is freed by xmlParserInputBufferPush*/
216
- buffer = xmlAllocParserInputBuffer(enc);
217
- new_string = xmlStrdup((xmlChar*) StringValuePtr(dtd_string));
218
- xmlParserInputBufferPush(buffer, xmlStrlen(new_string),
219
- (const char*) new_string);
220
-
221
- xdtd = xmlIOParseDTD(NULL, buffer, enc);
222
-
223
- if (xdtd == NULL)
224
- rxml_raise(&xmlLastError);
225
-
226
- xmlFree(new_string);
227
-
228
- DATA_PTR(self) = xdtd;
229
- break;
230
-
231
- default:
232
- rb_raise(rb_eArgError, "wrong number of arguments");
233
- }
234
-
235
- return self;
236
- }
237
-
238
- void rxml_init_dtd(void)
239
- {
240
- cXMLDtd = rb_define_class_under(mXML, "Dtd", rb_cObject);
241
- rb_define_alloc_func(cXMLDtd, rxml_dtd_alloc);
242
- rb_define_method(cXMLDtd, "initialize", rxml_dtd_initialize, -1);
243
- rb_define_method(cXMLDtd, "external_id", rxml_dtd_external_id_get, 0);
244
- rb_define_method(cXMLDtd, "name", rxml_dtd_name_get, 0);
245
- rb_define_method(cXMLDtd, "uri", rxml_dtd_uri_get, 0);
246
- rb_define_method(cXMLDtd, "node_type", rxml_dtd_type, 0);
247
- rb_define_alias(cXMLDtd, "system_id", "uri");
248
- }
1
+ #include "ruby_libxml.h"
2
+ #include "ruby_xml_dtd.h"
3
+
4
+ /*
5
+ * Document-class: LibXML::XML::Dtd
6
+ *
7
+ * The XML::Dtd class is used to prepare DTD's for validation of xml
8
+ * documents.
9
+ *
10
+ * DTDs can be created from a string or a pair of public and system identifiers.
11
+ * Once a Dtd object is instantiated, an XML document can be validated by the
12
+ * XML::Document#validate method providing the XML::Dtd object as parameeter.
13
+ * The method will raise an exception if the document is
14
+ * not valid.
15
+ *
16
+ * Basic usage:
17
+ *
18
+ * # parse DTD
19
+ * dtd = XML::Dtd.new(<<EOF)
20
+ * <!ELEMENT root (item*) >
21
+ * <!ELEMENT item (#PCDATA) >
22
+ * EOF
23
+ *
24
+ * # parse xml document to be validated
25
+ * instance = XML::Document.file('instance.xml')
26
+ *
27
+ * # validate
28
+ * instance.validate(dtd)
29
+ */
30
+
31
+ #ifndef TYPED_DATA_EMBEDDED
32
+ #define TYPED_DATA_EMBEDDED ((VALUE)1)
33
+ #endif
34
+
35
+ VALUE cXMLDtd;
36
+
37
+ static void rxml_dtd_free(void* data)
38
+ {
39
+ xmlDtdPtr xdtd = (xmlDtdPtr)data;
40
+ if (!xdtd) return;
41
+ if (xdtd->doc == NULL && xdtd->parent == NULL)
42
+ xmlFreeDtd(xdtd);
43
+ }
44
+
45
+ static void rxml_dtd_mark(void* data)
46
+ {
47
+ xmlDtdPtr xdtd = (xmlDtdPtr)data;
48
+ if (xdtd && xdtd->doc)
49
+ {
50
+ VALUE doc = rxml_registry_lookup(xdtd->doc);
51
+ if (!NIL_P(doc))
52
+ rb_gc_mark(doc);
53
+ }
54
+ }
55
+
56
+ const rb_data_type_t rxml_dtd_data_type = {
57
+ .wrap_struct_name = "LibXML::XML::Dtd",
58
+ .function = { .dmark = rxml_dtd_mark, .dfree = NULL },
59
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
60
+ };
61
+
62
+ static const rb_data_type_t rxml_dtd_managed_data_type = {
63
+ .wrap_struct_name = "LibXML::XML::Dtd (managed)",
64
+ .function = { .dmark = rxml_dtd_mark, .dfree = rxml_dtd_free },
65
+ .parent = &rxml_dtd_data_type,
66
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
67
+ };
68
+
69
+ static VALUE rxml_dtd_alloc(VALUE klass)
70
+ {
71
+ return TypedData_Wrap_Struct(klass, &rxml_dtd_managed_data_type, NULL);
72
+ }
73
+
74
+ VALUE rxml_dtd_wrap(xmlDtdPtr xdtd)
75
+ {
76
+ return TypedData_Wrap_Struct(cXMLDtd, &rxml_dtd_data_type, xdtd);
77
+ }
78
+
79
+ /*
80
+ * call-seq:
81
+ * dtd.external_id -> "string"
82
+ *
83
+ * Obtain this dtd's external identifer (for a PUBLIC DTD).
84
+ */
85
+ static VALUE rxml_dtd_external_id_get(VALUE self)
86
+ {
87
+ xmlDtdPtr xdtd;
88
+ TypedData_Get_Struct(self, xmlDtd, &rxml_dtd_data_type, xdtd);
89
+
90
+
91
+ if (xdtd->ExternalID == NULL)
92
+ return (Qnil);
93
+ else
94
+ return (rxml_new_cstr( xdtd->ExternalID, NULL));
95
+ }
96
+
97
+ /*
98
+ * call-seq:
99
+ * dtd.name -> "string"
100
+ *
101
+ * Obtain this dtd's name.
102
+ */
103
+ static VALUE rxml_dtd_name_get(VALUE self)
104
+ {
105
+ xmlDtdPtr xdtd;
106
+ TypedData_Get_Struct(self, xmlDtd, &rxml_dtd_data_type, xdtd);
107
+
108
+
109
+ if (xdtd->name == NULL)
110
+ return (Qnil);
111
+ else
112
+ return (rxml_new_cstr( xdtd->name, NULL));
113
+ }
114
+
115
+
116
+ /*
117
+ * call-seq:
118
+ * dtd.uri -> "string"
119
+ *
120
+ * Obtain this dtd's URI (for a SYSTEM or PUBLIC DTD).
121
+ */
122
+ static VALUE rxml_dtd_uri_get(VALUE self)
123
+ {
124
+ xmlDtdPtr xdtd;
125
+ TypedData_Get_Struct(self, xmlDtd, &rxml_dtd_data_type, xdtd);
126
+
127
+ if (xdtd->SystemID == NULL)
128
+ return (Qnil);
129
+ else
130
+ return (rxml_new_cstr( xdtd->SystemID, NULL));
131
+ }
132
+
133
+ /*
134
+ * call-seq:
135
+ * node.type -> num
136
+ *
137
+ * Obtain this node's type identifier.
138
+ */
139
+ static VALUE rxml_dtd_type(VALUE self)
140
+ {
141
+ xmlDtdPtr xdtd;
142
+ TypedData_Get_Struct(self, xmlDtd, &rxml_dtd_data_type, xdtd);
143
+ return (INT2NUM(xdtd->type));
144
+ }
145
+
146
+ /*
147
+ * call-seq:
148
+ * XML::Dtd.new(dtd_string) -> dtd
149
+ * XML::Dtd.new(external_id, system_id) -> dtd
150
+ * XML::Dtd.new(external_id, system_id, name, document, internal) -> dtd
151
+ *
152
+ * Create a new Dtd from the specified public and system identifiers:
153
+ *
154
+ * * The first usage creates a DTD from a string and requires 1 parameter.
155
+ * * The second usage loads and parses an external DTD and requires 2 parameters.
156
+ * * The third usage creates a new internal or external DTD and requires 2 parameters and 3 optional parameters.
157
+ * The DTD is then attached to the specified document if it is not nil.
158
+ *
159
+ * Parameters:
160
+ *
161
+ * dtd_string - A string that contains a complete DTD
162
+ * external_id - A string that specifies the DTD's external name. For example, "-//W3C//DTD XHTML 1.0 Transitional//EN"
163
+ * system_id - A string that specififies the DTD's system name. For example, "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
164
+ * name - A string that specifies the DTD's name. For example "xhtml1".
165
+ * document - A xml document.
166
+ * internal - Boolean value indicating whether this is an internal or external DTD. Optional. If not specified
167
+ * then external is assumed.
168
+ */
169
+ static VALUE rxml_dtd_initialize(int argc, VALUE *argv, VALUE self)
170
+ {
171
+ xmlDtdPtr xdtd;
172
+ VALUE external, system;
173
+
174
+ switch (argc)
175
+ {
176
+ case 3:
177
+ case 4:
178
+ case 5:
179
+ {
180
+ const xmlChar *xname = NULL, *xpublic = NULL, *xsystem = NULL;
181
+ xmlDocPtr xdoc = NULL;
182
+
183
+ VALUE name, doc, internal;
184
+ rb_scan_args(argc, argv, "23", &external, &system, &name, &doc, &internal);
185
+
186
+ if (external != Qnil)
187
+ {
188
+ Check_Type(external, T_STRING);
189
+ xpublic = (const xmlChar*) StringValuePtr(external);
190
+ }
191
+
192
+ if (system != Qnil)
193
+ {
194
+ Check_Type(system, T_STRING);
195
+ xsystem = (const xmlChar*) StringValuePtr(system);
196
+ }
197
+
198
+ if (name != Qnil)
199
+ {
200
+ Check_Type(name, T_STRING);
201
+ xname = (const xmlChar*)StringValuePtr(name);
202
+ }
203
+
204
+ if (doc != Qnil)
205
+ {
206
+ if (rb_obj_is_kind_of(doc, cXMLDocument) == Qfalse)
207
+ rb_raise(rb_eTypeError, "Must pass an LibXML::XML::Document object");
208
+ TypedData_Get_Struct(doc, xmlDoc, &rxml_document_data_type, xdoc);
209
+ }
210
+
211
+ if (internal == Qnil || internal == Qfalse)
212
+ xdtd = xmlNewDtd(xdoc, xname, xpublic, xsystem);
213
+ else
214
+ xdtd = xmlCreateIntSubset(xdoc, xname, xpublic, xsystem);
215
+
216
+ if (xdtd == NULL)
217
+ rxml_raise(xmlGetLastError());
218
+
219
+ /* The document will free the dtd so Ruby should not */
220
+ {
221
+ VALUE *type_ptr = (VALUE *)&RTYPEDDATA(self)->type;
222
+ *type_ptr = (*type_ptr & TYPED_DATA_EMBEDDED) | (VALUE)&rxml_dtd_data_type;
223
+ }
224
+ RTYPEDDATA_DATA(self) = xdtd;
225
+
226
+ xmlSetTreeDoc((xmlNodePtr) xdtd, xdoc);
227
+ }
228
+ break;
229
+
230
+ case 2:
231
+ {
232
+ rb_scan_args(argc, argv, "20", &external, &system);
233
+
234
+ Check_Type(external, T_STRING);
235
+ Check_Type(system, T_STRING);
236
+
237
+ xdtd = xmlParseDTD((xmlChar*) StringValuePtr(external), (xmlChar*) StringValuePtr(system));
238
+
239
+ if (xdtd == NULL)
240
+ rxml_raise(xmlGetLastError());
241
+
242
+ RTYPEDDATA_DATA(self) = xdtd;
243
+
244
+ xmlSetTreeDoc((xmlNodePtr) xdtd, NULL);
245
+ break;
246
+ }
247
+ case 1:
248
+ {
249
+ VALUE dtd_string;
250
+ rb_scan_args(argc, argv, "10", &dtd_string);
251
+ Check_Type(dtd_string, T_STRING);
252
+
253
+ /* Note that buffer is freed by xmlParserInputBufferPush*/
254
+ xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
255
+ xmlParserInputBufferPtr buffer = xmlAllocParserInputBuffer(enc);
256
+ xmlChar *new_string = xmlStrdup((xmlChar*) StringValuePtr(dtd_string));
257
+ xmlParserInputBufferPush(buffer, xmlStrlen(new_string),
258
+ (const char*) new_string);
259
+
260
+ xdtd = xmlIOParseDTD(NULL, buffer, enc);
261
+
262
+ if (xdtd == NULL)
263
+ rxml_raise(xmlGetLastError());
264
+
265
+ xmlFree(new_string);
266
+
267
+ RTYPEDDATA_DATA(self) = xdtd;
268
+ break;
269
+ }
270
+ default:
271
+ rb_raise(rb_eArgError, "wrong number of arguments");
272
+ }
273
+
274
+ return self;
275
+ }
276
+
277
+ void rxml_init_dtd(void)
278
+ {
279
+ cXMLDtd = rb_define_class_under(mXML, "Dtd", rb_cObject);
280
+ rb_define_alloc_func(cXMLDtd, rxml_dtd_alloc);
281
+ rb_define_method(cXMLDtd, "initialize", rxml_dtd_initialize, -1);
282
+ rb_define_method(cXMLDtd, "external_id", rxml_dtd_external_id_get, 0);
283
+ rb_define_method(cXMLDtd, "name", rxml_dtd_name_get, 0);
284
+ rb_define_method(cXMLDtd, "uri", rxml_dtd_uri_get, 0);
285
+ rb_define_method(cXMLDtd, "node_type", rxml_dtd_type, 0);
286
+ rb_define_alias(cXMLDtd, "system_id", "uri");
287
+ }
@@ -1,9 +1,10 @@
1
- #ifndef __RXML_DTD__
2
- #define __RXML_DTD__
3
-
4
- extern VALUE cXMLDtd;
5
-
6
- void rxml_init_dtd(void);
7
- VALUE rxml_dtd_wrap(xmlDtdPtr xdtd);
8
-
9
- #endif
1
+ #ifndef __RXML_DTD__
2
+ #define __RXML_DTD__
3
+
4
+ extern VALUE cXMLDtd;
5
+ extern const rb_data_type_t rxml_dtd_data_type;
6
+
7
+ void rxml_init_dtd(void);
8
+ VALUE rxml_dtd_wrap(xmlDtdPtr xdtd);
9
+
10
+ #endif