summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/rexml/encoding.rb10
-rw-r--r--lib/rexml/encodings/EUC-JP.rb17
-rw-r--r--lib/rexml/encodings/Shift-JIS.rb21
-rw-r--r--lib/rexml/encodings/Shift_JIS.rb18
4 files changed, 41 insertions, 25 deletions
diff --git a/lib/rexml/encoding.rb b/lib/rexml/encoding.rb
index 3d7dcd6260..2d62e49fac 100644
--- a/lib/rexml/encoding.rb
+++ b/lib/rexml/encoding.rb
@@ -8,7 +8,7 @@ module REXML
if match
ENCODING_CLAIMS[ match ] = encoding_str
else
- ENCODING_CLAIMS[ /^\s*<?xml\s*version=(['"]).*?\1\s*encoding=(["'])#{encoding_str}\2/ ] = encoding_str
+ ENCODING_CLAIMS[ /^\s*<?xml\s*version=(['"]).*?\1\s*encoding=(["'])#{encoding_str}\2/i ] = encoding_str
end
end
@@ -20,9 +20,11 @@ module REXML
# ID ---> Encoding name
attr_reader :encoding
def encoding=( enc )
- enc = UTF_8 unless enc
- @encoding = enc.upcase
- require "rexml/encodings/#@encoding" unless @encoding == UTF_8
+ enc = UTF_8 unless enc
+ rv = ENCODING_CLAIMS.find{|k,v| /#{v}/i =~ enc }
+ enc = rv[1] if rv
+ @encoding = enc
+ require "rexml/encodings/#@encoding" unless @encoding == UTF_8
end
def check_encoding str
diff --git a/lib/rexml/encodings/EUC-JP.rb b/lib/rexml/encodings/EUC-JP.rb
index cedd6751e7..23a1c3c657 100644
--- a/lib/rexml/encodings/EUC-JP.rb
+++ b/lib/rexml/encodings/EUC-JP.rb
@@ -13,5 +13,20 @@ begin
end
end
rescue LoadError
- raise "uconv is required for Japanese encoding support."
+ begin
+ require 'iconv'
+ module REXML
+ module Encoding
+ def from_euc_jp(str)
+ return Iconv::iconv("utf-8", "euc-jp", str)[0]
+ end
+
+ def to_euc_jp content
+ return Iconv::iconv("euc-jp", "utf-8", content)[0]
+ end
+ end
+ end
+ rescue LoadError
+ raise "uconv or iconv is required for Japanese encoding support."
+ end
end
diff --git a/lib/rexml/encodings/Shift-JIS.rb b/lib/rexml/encodings/Shift-JIS.rb
index 8650174538..e805456ea7 100644
--- a/lib/rexml/encodings/Shift-JIS.rb
+++ b/lib/rexml/encodings/Shift-JIS.rb
@@ -3,15 +3,30 @@ begin
module REXML
module Encoding
- def to_shift_jis content
+ def from_shift_jis(str)
Uconv::u8tosjis(content)
end
- def from_shift_jis(str)
+ def to_shift_jis content
Uconv::sjistou8(str)
end
end
end
rescue LoadError
- raise "uconv is required for Japanese encoding support."
+ begin
+ require 'iconv'
+ module REXML
+ module Encoding
+ def from_shift_jis(str)
+ return Iconv::iconv("utf-8", "shift-jis", str)[0]
+ end
+
+ def to_shift_jis content
+ return Iconv::iconv("euc-jp", "shift-jis", content)[0]
+ end
+ end
+ end
+ rescue LoadError
+ raise "uconv or iconv is required for Japanese encoding support."
+ end
end
diff --git a/lib/rexml/encodings/Shift_JIS.rb b/lib/rexml/encodings/Shift_JIS.rb
index 8650174538..6e8f759373 100644
--- a/lib/rexml/encodings/Shift_JIS.rb
+++ b/lib/rexml/encodings/Shift_JIS.rb
@@ -1,17 +1 @@
-begin
- require 'uconv'
-
- module REXML
- module Encoding
- def to_shift_jis content
- Uconv::u8tosjis(content)
- end
-
- def from_shift_jis(str)
- Uconv::sjistou8(str)
- end
- end
- end
-rescue LoadError
- raise "uconv is required for Japanese encoding support."
-end
+require 'rexml/encodings/Shift-JIS'