libxml-ruby 1.1.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/{CHANGES → HISTORY} +35 -0
  2. data/LICENSE +1 -0
  3. data/MANIFEST +165 -0
  4. data/{README → README.rdoc} +2 -2
  5. data/Rakefile +47 -147
  6. data/ext/libxml/libxml.c +17 -1
  7. data/ext/libxml/ruby_libxml.h +9 -1
  8. data/ext/libxml/ruby_xml_attr.c +3 -3
  9. data/ext/libxml/ruby_xml_attr_decl.c +32 -32
  10. data/ext/libxml/ruby_xml_attributes.c +1 -1
  11. data/ext/libxml/ruby_xml_cbg.c +1 -2
  12. data/ext/libxml/ruby_xml_document.c +26 -4
  13. data/ext/libxml/ruby_xml_dtd.c +3 -3
  14. data/ext/libxml/ruby_xml_encoding.c +92 -5
  15. data/ext/libxml/ruby_xml_encoding.h +4 -0
  16. data/ext/libxml/ruby_xml_namespace.c +2 -2
  17. data/ext/libxml/ruby_xml_node.c +14 -14
  18. data/ext/libxml/ruby_xml_parser_context.c +8 -8
  19. data/ext/libxml/ruby_xml_reader.c +98 -43
  20. data/ext/libxml/ruby_xml_sax2_handler.c +13 -1
  21. data/ext/libxml/ruby_xml_sax_parser.c +4 -5
  22. data/ext/libxml/ruby_xml_version.h +5 -5
  23. data/ext/libxml/ruby_xml_xpath_context.c +9 -6
  24. data/ext/libxml/ruby_xml_xpath_object.c +1 -1
  25. data/ext/vc/libxml_ruby.sln +4 -4
  26. data/lib/libxml.rb +29 -29
  27. data/lib/libxml/attr.rb +112 -110
  28. data/lib/libxml/attr_decl.rb +2 -0
  29. data/lib/libxml/attributes.rb +13 -11
  30. data/lib/libxml/document.rb +192 -190
  31. data/lib/libxml/error.rb +89 -87
  32. data/lib/libxml/hpricot.rb +77 -75
  33. data/lib/libxml/html_parser.rb +96 -94
  34. data/lib/libxml/namespace.rb +61 -59
  35. data/lib/libxml/namespaces.rb +37 -35
  36. data/lib/libxml/node.rb +398 -384
  37. data/lib/libxml/ns.rb +21 -19
  38. data/lib/libxml/parser.rb +366 -364
  39. data/lib/libxml/properties.rb +22 -20
  40. data/lib/libxml/reader.rb +2 -0
  41. data/lib/libxml/sax_callbacks.rb +179 -177
  42. data/lib/libxml/sax_parser.rb +57 -55
  43. data/lib/libxml/tree.rb +28 -26
  44. data/lib/libxml/xpath_object.rb +15 -13
  45. data/lib/xml.rb +16 -14
  46. data/lib/xml/libxml.rb +10 -8
  47. data/libxml-ruby.gemspec +50 -0
  48. data/script/benchmark/depixelate +634 -0
  49. data/script/benchmark/hamlet.xml +9055 -0
  50. data/script/benchmark/parsecount +170 -0
  51. data/script/benchmark/sock_entries.xml +507 -0
  52. data/script/benchmark/throughput +41 -0
  53. data/script/test +6 -0
  54. data/test/etc_doc_to_s.rb +21 -19
  55. data/test/ets_doc_file.rb +17 -15
  56. data/test/ets_doc_to_s.rb +23 -21
  57. data/test/ets_gpx.rb +28 -26
  58. data/test/ets_node_gc.rb +23 -21
  59. data/test/ets_tsr.rb +11 -9
  60. data/test/model/bands.iso-8859-1.xml +5 -0
  61. data/test/model/bands.utf-8.xml +5 -0
  62. data/test/rb-magic-comment.rb +33 -0
  63. data/test/tc_attr.rb +181 -170
  64. data/test/tc_attr_decl.rb +3 -1
  65. data/test/tc_attributes.rb +134 -132
  66. data/test/tc_deprecated_require.rb +13 -11
  67. data/test/tc_document.rb +119 -113
  68. data/test/tc_document_write.rb +186 -117
  69. data/test/tc_dtd.rb +125 -123
  70. data/test/tc_error.rb +3 -1
  71. data/test/tc_html_parser.rb +139 -137
  72. data/test/tc_namespace.rb +61 -58
  73. data/test/tc_namespaces.rb +176 -173
  74. data/test/tc_node.rb +257 -180
  75. data/test/tc_node_cdata.rb +51 -49
  76. data/test/tc_node_comment.rb +33 -30
  77. data/test/tc_node_copy.rb +42 -40
  78. data/test/tc_node_edit.rb +159 -157
  79. data/test/tc_node_text.rb +71 -69
  80. data/test/tc_node_write.rb +41 -16
  81. data/test/tc_node_xlink.rb +29 -26
  82. data/test/tc_parser.rb +335 -329
  83. data/test/tc_parser_context.rb +188 -185
  84. data/test/tc_properties.rb +39 -36
  85. data/test/tc_reader.rb +297 -283
  86. data/test/tc_relaxng.rb +54 -51
  87. data/test/tc_sax_parser.rb +275 -273
  88. data/test/tc_schema.rb +53 -51
  89. data/test/tc_traversal.rb +222 -220
  90. data/test/tc_xinclude.rb +21 -19
  91. data/test/tc_xml.rb +3 -1
  92. data/test/tc_xpath.rb +195 -193
  93. data/test/tc_xpath_context.rb +80 -78
  94. data/test/tc_xpath_expression.rb +38 -35
  95. data/test/tc_xpointer.rb +74 -72
  96. data/test/test_helper.rb +14 -0
  97. data/test/test_suite.rb +39 -33
  98. metadata +65 -105
  99. data/doc/css/normal.css +0 -182
  100. data/doc/img/raze-tiny.png +0 -0
  101. data/doc/img/red-cube.jpg +0 -0
  102. data/doc/img/xml-ruby.png +0 -0
  103. data/doc/index.xml +0 -43
  104. data/doc/install.xml +0 -77
  105. data/doc/layout.rhtml +0 -38
  106. data/doc/layout.xsl +0 -67
  107. data/doc/license.xml +0 -32
  108. data/doc/log/changelog.xml +0 -1324
  109. data/doc/log/changelog.xsl +0 -42
  110. data/ext/libxml/Makefile +0 -156
  111. data/ext/libxml/extconf.h +0 -5
  112. data/ext/libxml/libxml-ruby.so.a +0 -0
  113. data/ext/libxml/libxml.o +0 -0
  114. data/ext/libxml/libxml_ruby.so +0 -0
  115. data/ext/libxml/mkmf.log +0 -129
  116. data/ext/libxml/ruby_xml.o +0 -0
  117. data/ext/libxml/ruby_xml_attr.o +0 -0
  118. data/ext/libxml/ruby_xml_attr_decl.o +0 -0
  119. data/ext/libxml/ruby_xml_attributes.o +0 -0
  120. data/ext/libxml/ruby_xml_cbg.o +0 -0
  121. data/ext/libxml/ruby_xml_document.o +0 -0
  122. data/ext/libxml/ruby_xml_dtd.o +0 -0
  123. data/ext/libxml/ruby_xml_encoding.o +0 -0
  124. data/ext/libxml/ruby_xml_error.o +0 -0
  125. data/ext/libxml/ruby_xml_html_parser.o +0 -0
  126. data/ext/libxml/ruby_xml_html_parser_context.o +0 -0
  127. data/ext/libxml/ruby_xml_html_parser_options.o +0 -0
  128. data/ext/libxml/ruby_xml_input_cbg.o +0 -0
  129. data/ext/libxml/ruby_xml_io.o +0 -0
  130. data/ext/libxml/ruby_xml_namespace.o +0 -0
  131. data/ext/libxml/ruby_xml_namespaces.o +0 -0
  132. data/ext/libxml/ruby_xml_node.o +0 -0
  133. data/ext/libxml/ruby_xml_parser.o +0 -0
  134. data/ext/libxml/ruby_xml_parser_context.o +0 -0
  135. data/ext/libxml/ruby_xml_parser_options.o +0 -0
  136. data/ext/libxml/ruby_xml_reader.o +0 -0
  137. data/ext/libxml/ruby_xml_relaxng.o +0 -0
  138. data/ext/libxml/ruby_xml_sax2_handler.o +0 -0
  139. data/ext/libxml/ruby_xml_sax_parser.o +0 -0
  140. data/ext/libxml/ruby_xml_schema.o +0 -0
  141. data/ext/libxml/ruby_xml_xinclude.o +0 -0
  142. data/ext/libxml/ruby_xml_xpath.o +0 -0
  143. data/ext/libxml/ruby_xml_xpath_context.o +0 -0
  144. data/ext/libxml/ruby_xml_xpath_expression.o +0 -0
  145. data/ext/libxml/ruby_xml_xpath_object.o +0 -0
  146. data/ext/libxml/ruby_xml_xpointer.o +0 -0
data/ext/libxml/libxml.c CHANGED
@@ -6,6 +6,7 @@
6
6
  #include <util.h>
7
7
  #endif
8
8
 
9
+
9
10
  VALUE mLibXML;
10
11
 
11
12
  static void rxml_init_memory(void)
@@ -20,6 +21,21 @@ static void rxml_init_memory(void)
20
21
  );*/
21
22
  }
22
23
 
24
+ VALUE rxml_str_new2(const char* xstr, const char* xencoding)
25
+ {
26
+ #ifdef HAVE_RUBY_ENCODING_H
27
+ if (xencoding)
28
+ {
29
+ xmlCharEncoding xmlEncoding = xmlParseCharEncoding(xencoding);
30
+ VALUE encoding = rxml_xml_encoding_to_rb_encoding(mXMLEncoding, xmlEncoding);
31
+ rb_encoding* xencodingPtr = (rb_encoding*) RDATA(encoding)->data;
32
+ return rb_external_str_new_with_enc(xstr, strlen(xstr), xencodingPtr);
33
+ }
34
+ #endif
35
+ return rb_str_new2(xstr);
36
+ }
37
+
38
+
23
39
  #if defined(_WIN32)
24
40
  __declspec(dllexport)
25
41
  #endif
@@ -28,7 +44,7 @@ void Init_libxml_ruby(void)
28
44
  /* The libxml gem provides Ruby language bindings for GNOME's Libxml2
29
45
  * XML toolkit. To get started you may:
30
46
  *
31
- * require 'xml'
47
+ * require 'test_helper'
32
48
  * document = XML::Document.new
33
49
  *
34
50
  * However, when creating an application or library you plan to
@@ -40,12 +40,20 @@
40
40
  #define RHASH_TBL(s) (RHASH(s)->tbl)
41
41
  #endif
42
42
 
43
- // not in Ruby 1.9
43
+ // Not in Ruby 1.9
44
44
  #ifndef GetWriteFile
45
45
  #define GetWriteFile(fp) rb_io_stdio_file(fp)
46
46
  #define OpenFile rb_io_t
47
47
  #endif
48
48
 
49
+ // Encoding support added in Ruby 1.9.*
50
+ #ifdef HAVE_RUBY_ENCODING_H
51
+ #include <ruby/encoding.h>
52
+ #endif
53
+
54
+ // Ruby 1.8/1.9 encoding compatibility
55
+ VALUE rxml_str_new2(const char* xstr, const char* xencoding);
56
+
49
57
  #ifdef LIBXML_DEBUG_ENABLED
50
58
  #include <libxml/xpathInternals.h>
51
59
  #endif
@@ -9,7 +9,7 @@
9
9
  *
10
10
  * Basic Usage:
11
11
  *
12
- * require 'xml'
12
+ * require 'test_helper'
13
13
  *
14
14
  * doc = XML::Document.new(<some_file>)
15
15
  * attribute = doc.root.attributes.get_attribute_ns('http://www.w3.org/1999/xlink', 'href')
@@ -188,7 +188,7 @@ static VALUE rxml_attr_name_get(VALUE self)
188
188
  if (xattr->name == NULL)
189
189
  return Qnil;
190
190
  else
191
- return rb_str_new2((const char*) xattr->name);
191
+ return rxml_str_new2((const char*) xattr->name, xattr->doc->encoding);
192
192
  }
193
193
 
194
194
  /*
@@ -304,7 +304,7 @@ VALUE rxml_attr_value_get(VALUE self)
304
304
 
305
305
  if (value != NULL)
306
306
  {
307
- result = rb_str_new2((const char*) value);
307
+ result = rxml_str_new2((const char*) value, xattr->doc->encoding);
308
308
  xmlFree(value);
309
309
  }
310
310
  return result;
@@ -15,28 +15,28 @@
15
15
 
16
16
  VALUE cXMLAttrDecl;
17
17
 
18
- void rxml_attr_decl_mark(xmlAttributePtr xattribute)
18
+ void rxml_attr_decl_mark(xmlAttributePtr xattr)
19
19
  {
20
- if (xattribute->_private == NULL)
20
+ if (xattr->_private == NULL)
21
21
  {
22
22
  rb_warning("AttrDecl is not bound! (%s:%d)", __FILE__, __LINE__);
23
23
  return;
24
24
  }
25
25
 
26
- rxml_node_mark((xmlNodePtr) xattribute);
26
+ rxml_node_mark((xmlNodePtr) xattr);
27
27
  }
28
28
 
29
- VALUE rxml_attr_decl_wrap(xmlAttributePtr xattribute)
29
+ VALUE rxml_attr_decl_wrap(xmlAttributePtr xattr)
30
30
  {
31
31
  VALUE result;
32
32
 
33
33
  // This node is already wrapped
34
- if (xattribute->_private != NULL)
35
- return (VALUE) xattribute->_private;
34
+ if (xattr->_private != NULL)
35
+ return (VALUE) xattr->_private;
36
36
 
37
- result = Data_Wrap_Struct(cXMLAttrDecl, rxml_attr_decl_mark, NULL, xattribute);
37
+ result = Data_Wrap_Struct(cXMLAttrDecl, rxml_attr_decl_mark, NULL, xattr);
38
38
 
39
- xattribute->_private = (void*) result;
39
+ xattr->_private = (void*) result;
40
40
 
41
41
  return result;
42
42
  }
@@ -49,12 +49,12 @@ VALUE rxml_attr_decl_wrap(xmlAttributePtr xattribute)
49
49
  */
50
50
  static VALUE rxml_attr_decl_doc_get(VALUE self)
51
51
  {
52
- xmlAttributePtr xattribute;
53
- Data_Get_Struct(self, xmlAttribute, xattribute);
54
- if (xattribute->doc == NULL)
52
+ xmlAttributePtr xattr;
53
+ Data_Get_Struct(self, xmlAttribute, xattr);
54
+ if (xattr->doc == NULL)
55
55
  return Qnil;
56
56
  else
57
- return rxml_document_wrap(xattribute->doc);
57
+ return rxml_document_wrap(xattr->doc);
58
58
  }
59
59
 
60
60
 
@@ -66,13 +66,13 @@ static VALUE rxml_attr_decl_doc_get(VALUE self)
66
66
  */
67
67
  static VALUE rxml_attr_decl_name_get(VALUE self)
68
68
  {
69
- xmlAttributePtr xattribute;
70
- Data_Get_Struct(self, xmlAttribute, xattribute);
69
+ xmlAttributePtr xattr;
70
+ Data_Get_Struct(self, xmlAttribute, xattr);
71
71
 
72
- if (xattribute->name == NULL)
72
+ if (xattr->name == NULL)
73
73
  return Qnil;
74
74
  else
75
- return rb_str_new2((const char*) xattribute->name);
75
+ return rxml_str_new2((const char*) xattr->name, xattr->doc->encoding);
76
76
  }
77
77
 
78
78
  /*
@@ -83,12 +83,12 @@ static VALUE rxml_attr_decl_name_get(VALUE self)
83
83
  */
84
84
  static VALUE rxml_attr_decl_next_get(VALUE self)
85
85
  {
86
- xmlAttributePtr xattribute;
87
- Data_Get_Struct(self, xmlAttribute, xattribute);
88
- if (xattribute->next == NULL)
86
+ xmlAttributePtr xattr;
87
+ Data_Get_Struct(self, xmlAttribute, xattr);
88
+ if (xattr->next == NULL)
89
89
  return Qnil;
90
90
  else
91
- return rxml_attr_decl_wrap((xmlAttributePtr)xattribute->next);
91
+ return rxml_attr_decl_wrap((xmlAttributePtr)xattr->next);
92
92
  }
93
93
 
94
94
  /*
@@ -113,13 +113,13 @@ static VALUE rxml_attr_decl_node_type(VALUE self)
113
113
  */
114
114
  static VALUE rxml_attr_decl_parent_get(VALUE self)
115
115
  {
116
- xmlAttributePtr xattribute;
117
- Data_Get_Struct(self, xmlAttribute, xattribute);
116
+ xmlAttributePtr xattr;
117
+ Data_Get_Struct(self, xmlAttribute, xattr);
118
118
 
119
- if (xattribute->parent == NULL)
119
+ if (xattr->parent == NULL)
120
120
  return Qnil;
121
121
  else
122
- return rxml_dtd_wrap(xattribute->parent);
122
+ return rxml_dtd_wrap(xattr->parent);
123
123
  }
124
124
 
125
125
  /*
@@ -131,13 +131,13 @@ static VALUE rxml_attr_decl_parent_get(VALUE self)
131
131
  */
132
132
  static VALUE rxml_attr_decl_prev_get(VALUE self)
133
133
  {
134
- xmlAttributePtr xattribute;
135
- Data_Get_Struct(self, xmlAttribute, xattribute);
134
+ xmlAttributePtr xattr;
135
+ Data_Get_Struct(self, xmlAttribute, xattr);
136
136
 
137
- if (xattribute->prev == NULL)
137
+ if (xattr->prev == NULL)
138
138
  return Qnil;
139
139
  else
140
- return rxml_attr_decl_wrap((xmlAttributePtr)xattribute->prev);
140
+ return rxml_attr_decl_wrap((xmlAttributePtr)xattr->prev);
141
141
  }
142
142
 
143
143
  /*
@@ -148,12 +148,12 @@ static VALUE rxml_attr_decl_prev_get(VALUE self)
148
148
  */
149
149
  VALUE rxml_attr_decl_value_get(VALUE self)
150
150
  {
151
- xmlAttributePtr xattribute;
151
+ xmlAttributePtr xattr;
152
152
 
153
- Data_Get_Struct(self, xmlAttribute, xattribute);
153
+ Data_Get_Struct(self, xmlAttribute, xattr);
154
154
 
155
- if (xattribute->defaultValue)
156
- return rb_str_new2((const char *)xattribute->defaultValue);
155
+ if (xattr->defaultValue)
156
+ return rxml_str_new2((const char *)xattr->defaultValue, xattr->doc->encoding);
157
157
  else
158
158
  return Qnil;
159
159
  }
@@ -8,7 +8,7 @@
8
8
  * Provides access to an element's attributes (XML::Attr).
9
9
  *
10
10
  * Basic Usage:
11
- * require 'xml'
11
+ * require 'test_helper'
12
12
  *
13
13
  * doc = XML::Document.new(<some_file>)
14
14
  * attributes = doc.root.attributes
@@ -42,8 +42,7 @@ void* deb_Open(char const *filename)
42
42
  deb_doc = (deb_doc_context*) malloc(sizeof(deb_doc_context));
43
43
 
44
44
  res = rb_funcall(rb_funcall(rb_mKernel, rb_intern("const_get"), 1,
45
- rb_str_new2("DEBSystem")), rb_intern("document_query"), 1, rb_str_new2(
46
- filename));
45
+ rb_str_new2("DEBSystem")), rb_intern("document_query"), 1, rb_str_new2(filename));
47
46
  deb_doc->buffer = strdup(StringValuePtr(res));
48
47
  //deb_doc->buffer = strdup("<serepes>serepes</serepes>");
49
48
 
@@ -282,7 +282,7 @@ static VALUE rxml_document_debug(VALUE self)
282
282
  * call-seq:
283
283
  * document.encoding -> XML::Encoding::UTF_8
284
284
  *
285
- * Obtain the encoding specified by this document.
285
+ * Returns the LibXML encoding constant specified by this document.
286
286
  */
287
287
  static VALUE rxml_document_encoding_get(VALUE self)
288
288
  {
@@ -294,6 +294,25 @@ static VALUE rxml_document_encoding_get(VALUE self)
294
294
  return INT2NUM(xmlParseCharEncoding(xencoding));
295
295
  }
296
296
 
297
+
298
+ /*
299
+ * call-seq:
300
+ * document.rb_encoding -> Encoding
301
+ *
302
+ * Returns the Ruby encoding specified by this document
303
+ * (available on Ruby 1.9.x and higher).
304
+ */
305
+ static VALUE rxml_document_rb_encoding_get(VALUE self)
306
+ {
307
+ xmlDocPtr xdoc;
308
+ const char *xencoding;
309
+ VALUE encoding;
310
+ Data_Get_Struct(self, xmlDoc, xdoc);
311
+
312
+ xencoding = (const char*)xdoc->encoding;
313
+ return rxml_xml_encoding_to_rb_encoding(mXMLEncoding, xmlParseCharEncoding(xencoding));
314
+ }
315
+
297
316
  /*
298
317
  * call-seq:
299
318
  * document.encoding = XML::Encoding::UTF_8
@@ -664,7 +683,7 @@ static VALUE rxml_document_to_s(int argc, VALUE *argv, VALUE self)
664
683
  Data_Get_Struct(self, xmlDoc, xdoc);
665
684
  xmlDocDumpFormatMemoryEnc(xdoc, &buffer, &length, xencoding, indent);
666
685
 
667
- result = rb_str_new((const char*) buffer, length);
686
+ result = rxml_str_new2((const char*) buffer, xencoding);
668
687
  xmlFree(buffer);
669
688
  return result;
670
689
  }
@@ -683,7 +702,7 @@ static VALUE rxml_document_url_get(VALUE self)
683
702
  if (xdoc->URL == NULL)
684
703
  return (Qnil);
685
704
  else
686
- return (rb_str_new2((const char*) xdoc->URL));
705
+ return (rxml_str_new2((const char*) xdoc->URL, xdoc->encoding));
687
706
  }
688
707
 
689
708
  /*
@@ -700,7 +719,7 @@ static VALUE rxml_document_version_get(VALUE self)
700
719
  if (xdoc->version == NULL)
701
720
  return (Qnil);
702
721
  else
703
- return (rb_str_new2((const char*) xdoc->version));
722
+ return (rxml_str_new2((const char*) xdoc->version, xdoc->encoding));
704
723
  }
705
724
 
706
725
  /*
@@ -909,6 +928,9 @@ void rxml_init_document(void)
909
928
  rb_define_method(cXMLDocument, "compression?", rxml_document_compression_q, 0);
910
929
  rb_define_method(cXMLDocument, "debug", rxml_document_debug, 0);
911
930
  rb_define_method(cXMLDocument, "encoding", rxml_document_encoding_get, 0);
931
+ #ifdef HAVE_RUBY_ENCODING_H
932
+ rb_define_method(cXMLDocument, "rb_encoding", rxml_document_rb_encoding_get, 0);
933
+ #endif
912
934
  rb_define_method(cXMLDocument, "encoding=", rxml_document_encoding_set, 1);
913
935
  rb_define_method(cXMLDocument, "import", rxml_document_import, 1);
914
936
  rb_define_method(cXMLDocument, "last", rxml_document_last_get, 0);
@@ -90,7 +90,7 @@ static VALUE rxml_dtd_external_id_get(VALUE self)
90
90
  if (xdtd->ExternalID == NULL)
91
91
  return (Qnil);
92
92
  else
93
- return (rb_str_new2((const char*) xdtd->ExternalID));
93
+ return (rxml_str_new2((const char*) xdtd->ExternalID, xdtd->doc ? xdtd->doc->encoding : NULL));
94
94
  }
95
95
 
96
96
  /*
@@ -108,7 +108,7 @@ static VALUE rxml_dtd_name_get(VALUE self)
108
108
  if (xdtd->name == NULL)
109
109
  return (Qnil);
110
110
  else
111
- return (rb_str_new2((const char*) xdtd->name));
111
+ return (rxml_str_new2((const char*) xdtd->name, xdtd->doc ? xdtd->doc->encoding : NULL));
112
112
  }
113
113
 
114
114
 
@@ -127,7 +127,7 @@ static VALUE rxml_dtd_uri_get(VALUE self)
127
127
  if (xdtd->SystemID == NULL)
128
128
  return (Qnil);
129
129
  else
130
- return (rb_str_new2((const char*) xdtd->SystemID));
130
+ return (rxml_str_new2((const char*) xdtd->SystemID, xdtd->doc ? xdtd->doc->encoding : NULL));
131
131
  }
132
132
 
133
133
  /*
@@ -41,7 +41,6 @@
41
41
 
42
42
  VALUE mXMLEncoding;
43
43
 
44
-
45
44
  /*
46
45
  * call-seq:
47
46
  * Input.s_to_encoding("UTF_8") -> XML::Encoding::UTF_8
@@ -64,25 +63,113 @@ static VALUE rxml_encoding_from_s(VALUE klass, VALUE encoding)
64
63
  * call-seq:
65
64
  * Input.encoding_to_s(Input::ENCODING) -> "encoding"
66
65
  *
67
- * Converts an encoding contstant defined on the XML::Encoding
66
+ * Converts an encoding constant defined on the XML::Encoding
68
67
  * class to its text representation.
69
68
  */
70
69
  static VALUE rxml_encoding_to_s(VALUE klass, VALUE encoding)
71
70
  {
72
- const char* xecoding = xmlGetCharEncodingName(NUM2INT(encoding));
71
+ const char* xencoding = xmlGetCharEncodingName(NUM2INT(encoding));
73
72
 
74
- if (!xecoding)
73
+ if (!xencoding)
75
74
  return Qnil;
76
75
  else
77
- return rb_str_new2(xecoding);
76
+ return rxml_str_new2(xencoding, xencoding);
78
77
  }
79
78
 
79
+ #ifdef HAVE_RUBY_ENCODING_H
80
+ VALUE rxml_xml_encoding_to_rb_encoding(VALUE klass, xmlCharEncoding xmlEncoding)
81
+ {
82
+ ID encoding_name;
83
+
84
+ switch (xmlEncoding)
85
+ {
86
+ case XML_CHAR_ENCODING_UTF8:
87
+ encoding_name = rb_intern("UTF_8");
88
+ break;
89
+ case XML_CHAR_ENCODING_UTF16LE:
90
+ encoding_name = rb_intern("UTF_16LE");
91
+ break;
92
+ case XML_CHAR_ENCODING_UTF16BE:
93
+ encoding_name = rb_intern("UTF_16BE");
94
+ break;
95
+ case XML_CHAR_ENCODING_UCS4LE:
96
+ encoding_name = rb_intern("UCS_4LE");
97
+ break;
98
+ case XML_CHAR_ENCODING_UCS4BE:
99
+ encoding_name = rb_intern("UCS_4BE");
100
+ break;
101
+ case XML_CHAR_ENCODING_UCS2:
102
+ encoding_name = rb_intern("UCS_2");
103
+ break;
104
+ case XML_CHAR_ENCODING_8859_1:
105
+ encoding_name = rb_intern("ISO8859_1");
106
+ break;
107
+ case XML_CHAR_ENCODING_8859_2:
108
+ encoding_name = rb_intern("ISO8859_2");
109
+ break;
110
+ case XML_CHAR_ENCODING_8859_3:
111
+ encoding_name = rb_intern("ISO8859_3");
112
+ break;
113
+ case XML_CHAR_ENCODING_8859_4:
114
+ encoding_name = rb_intern("ISO8859_4");
115
+ break;
116
+ case XML_CHAR_ENCODING_8859_5:
117
+ encoding_name = rb_intern("ISO8859_5");
118
+ break;
119
+ case XML_CHAR_ENCODING_8859_6:
120
+ encoding_name = rb_intern("ISO8859_6");
121
+ break;
122
+ case XML_CHAR_ENCODING_8859_7:
123
+ encoding_name = rb_intern("ISO8859_7");
124
+ break;
125
+ case XML_CHAR_ENCODING_8859_8:
126
+ encoding_name = rb_intern("ISO8859_8");
127
+ break;
128
+ case XML_CHAR_ENCODING_8859_9:
129
+ encoding_name = rb_intern("ISO8859_9");
130
+ break;
131
+ case XML_CHAR_ENCODING_2022_JP:
132
+ encoding_name = rb_intern("ISO_2022_JP");
133
+ break;
134
+ case XML_CHAR_ENCODING_SHIFT_JIS:
135
+ encoding_name = rb_intern("SHIFT_JIS");
136
+ break;
137
+ case XML_CHAR_ENCODING_EUC_JP:
138
+ encoding_name = rb_intern("EUC_JP");
139
+ break;
140
+ case XML_CHAR_ENCODING_ASCII:
141
+ encoding_name = rb_intern("US-ASCII");
142
+ break;
143
+ default:
144
+ /* Covers XML_CHAR_ENCODING_ERROR, XML_CHAR_ENCODING_NONE, XML_CHAR_ENCODING_EBCDIC */
145
+ encoding_name = rb_intern("ASCII_8BIT");
146
+ }
147
+ return rb_const_get(rb_cEncoding, encoding_name);
148
+ }
149
+
150
+ /*
151
+ * call-seq:
152
+ * Input.encoding_to_rb_encoding(Input::ENCODING) -> Encoding
153
+ *
154
+ * Converts an encoding constant defined on the XML::Encoding
155
+ * class to a Ruby encoding object (available on Ruby 1.9.* and higher).
156
+ */
157
+ VALUE rxml_encoding_to_rb_encoding(VALUE klass, VALUE encoding)
158
+ {
159
+ return rxml_xml_encoding_to_rb_encoding(klass, NUM2INT(encoding));
160
+ }
161
+ #endif
162
+
80
163
  void rxml_init_encoding(void)
81
164
  {
82
165
  mXMLEncoding = rb_define_module_under(mXML, "Encoding");
83
166
  rb_define_module_function(mXMLEncoding, "from_s", rxml_encoding_from_s, 1);
84
167
  rb_define_module_function(mXMLEncoding, "to_s", rxml_encoding_to_s, 1);
85
168
 
169
+ #ifdef HAVE_RUBY_ENCODING_H
170
+ // rb_define_module_function(mXMLEncoding, "to_rb_encoding", rxml_encoding_to_rb_encoding, 2);
171
+ #endif
172
+
86
173
  /* -1: No char encoding detected. */
87
174
  rb_define_const(mXMLEncoding, "ERROR", INT2NUM(XML_CHAR_ENCODING_ERROR));
88
175
  /* 0: No char encoding detected. */