summaryrefslogtreecommitdiff
path: root/lib/rexml/encodings
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rexml/encodings')
-rw-r--r--lib/rexml/encodings/EUC-JP.rb17
-rw-r--r--lib/rexml/encodings/EUC-JP_decl.rb6
-rw-r--r--lib/rexml/encodings/ISO-8859-1.rb23
-rw-r--r--lib/rexml/encodings/ISO-8859-1_decl.rb6
-rw-r--r--lib/rexml/encodings/Shift-JIS.rb17
-rw-r--r--lib/rexml/encodings/Shift-JIS_decl.rb6
-rw-r--r--lib/rexml/encodings/Shift_JIS.rb17
-rw-r--r--lib/rexml/encodings/UNILE.rb27
-rw-r--r--lib/rexml/encodings/UNILE_decl.rb6
-rw-r--r--lib/rexml/encodings/US-ASCII.rb23
-rw-r--r--lib/rexml/encodings/US-ASCII_decl.rb6
-rw-r--r--lib/rexml/encodings/UTF-16.rb27
-rw-r--r--lib/rexml/encodings/UTF-16_decl.rb6
13 files changed, 187 insertions, 0 deletions
diff --git a/lib/rexml/encodings/EUC-JP.rb b/lib/rexml/encodings/EUC-JP.rb
new file mode 100644
index 0000000000..cedd6751e7
--- /dev/null
+++ b/lib/rexml/encodings/EUC-JP.rb
@@ -0,0 +1,17 @@
+begin
+ require 'uconv'
+
+ module REXML
+ module Encoding
+ def from_euc_jp(str)
+ return Uconv::euctou8(str)
+ end
+
+ def to_euc_jp content
+ return Uconv::u8toeuc(content)
+ end
+ end
+ end
+rescue LoadError
+ raise "uconv is required for Japanese encoding support."
+end
diff --git a/lib/rexml/encodings/EUC-JP_decl.rb b/lib/rexml/encodings/EUC-JP_decl.rb
new file mode 100644
index 0000000000..4c7cd828a6
--- /dev/null
+++ b/lib/rexml/encodings/EUC-JP_decl.rb
@@ -0,0 +1,6 @@
+module REXML
+ module Encoding
+ EUC_JP = 'EUC-JP'
+ claim( EUC_JP )
+ end
+end
diff --git a/lib/rexml/encodings/ISO-8859-1.rb b/lib/rexml/encodings/ISO-8859-1.rb
new file mode 100644
index 0000000000..98c5aff3b2
--- /dev/null
+++ b/lib/rexml/encodings/ISO-8859-1.rb
@@ -0,0 +1,23 @@
+module REXML
+ module Encoding
+ # Convert from UTF-8
+ def to_iso_8859_1 content
+ array_utf8 = content.unpack('U*')
+ array_enc = []
+ array_utf8.each do |num|
+ if num <= 0xFF
+ array_enc << num
+ else
+ # Numeric entity (&#nnnn;); shard by Stefan Scholl
+ array_enc.concat "&\##{num};".unpack('C*')
+ end
+ end
+ array_enc.pack('C*')
+ end
+
+ # Convert to UTF-8
+ def from_iso_8859_1(str)
+ str.unpack('C*').pack('U*')
+ end
+ end
+end
diff --git a/lib/rexml/encodings/ISO-8859-1_decl.rb b/lib/rexml/encodings/ISO-8859-1_decl.rb
new file mode 100644
index 0000000000..a738d30472
--- /dev/null
+++ b/lib/rexml/encodings/ISO-8859-1_decl.rb
@@ -0,0 +1,6 @@
+module REXML
+ module Encoding
+ ISO_8859_1 = 'ISO-8859-1'
+ claim( ISO_8859_1 )
+ end
+end
diff --git a/lib/rexml/encodings/Shift-JIS.rb b/lib/rexml/encodings/Shift-JIS.rb
new file mode 100644
index 0000000000..8650174538
--- /dev/null
+++ b/lib/rexml/encodings/Shift-JIS.rb
@@ -0,0 +1,17 @@
+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
diff --git a/lib/rexml/encodings/Shift-JIS_decl.rb b/lib/rexml/encodings/Shift-JIS_decl.rb
new file mode 100644
index 0000000000..66f650144a
--- /dev/null
+++ b/lib/rexml/encodings/Shift-JIS_decl.rb
@@ -0,0 +1,6 @@
+module REXML
+ module Encoding
+ claim( 'Shift-JIS' )
+ claim( 'Shift_JIS' )
+ end
+end
diff --git a/lib/rexml/encodings/Shift_JIS.rb b/lib/rexml/encodings/Shift_JIS.rb
new file mode 100644
index 0000000000..8650174538
--- /dev/null
+++ b/lib/rexml/encodings/Shift_JIS.rb
@@ -0,0 +1,17 @@
+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
diff --git a/lib/rexml/encodings/UNILE.rb b/lib/rexml/encodings/UNILE.rb
new file mode 100644
index 0000000000..74bed14340
--- /dev/null
+++ b/lib/rexml/encodings/UNILE.rb
@@ -0,0 +1,27 @@
+module REXML
+ module Encoding
+ def to_unile content
+ array_utf8 = content.unpack("U*")
+ array_enc = []
+ array_utf8.each do |num|
+ if ((num>>16) > 0)
+ array_enc << ??
+ array_enc << 0
+ else
+ array_enc << (num & 0xFF)
+ array_enc << (num >> 8)
+ end
+ end
+ array_enc.pack('C*')
+ end
+
+ def from_unile(str)
+ array_enc=str.unpack('C*')
+ array_utf8 = []
+ 2.step(array_enc.size-1, 2){|i|
+ array_utf8 << (array_enc.at(i) + array_enc.at(i+1)*0x100)
+ }
+ array_utf8.pack('U*')
+ end
+ end
+end
diff --git a/lib/rexml/encodings/UNILE_decl.rb b/lib/rexml/encodings/UNILE_decl.rb
new file mode 100644
index 0000000000..9e1c11dc03
--- /dev/null
+++ b/lib/rexml/encodings/UNILE_decl.rb
@@ -0,0 +1,6 @@
+module REXML
+ module Encoding
+ UNILE = 'UNILE'
+ claim( UNILE, /^\377\376/ )
+ end
+end
diff --git a/lib/rexml/encodings/US-ASCII.rb b/lib/rexml/encodings/US-ASCII.rb
new file mode 100644
index 0000000000..4ca2c82a83
--- /dev/null
+++ b/lib/rexml/encodings/US-ASCII.rb
@@ -0,0 +1,23 @@
+module REXML
+ module Encoding
+ # Convert from UTF-8
+ def to_us_ascii content
+ array_utf8 = content.unpack('U*')
+ array_enc = []
+ array_utf8.each do |num|
+ if num <= 0xFF
+ array_enc << num
+ else
+ # Numeric entity (&#nnnn;); shard by Stefan Scholl
+ array_enc.concat "&\##{num};".unpack('C*')
+ end
+ end
+ array_enc.pack('C*')
+ end
+
+ # Convert to UTF-8
+ def from_us_ascii(str)
+ str.unpack('C*').pack('U*')
+ end
+ end
+end
diff --git a/lib/rexml/encodings/US-ASCII_decl.rb b/lib/rexml/encodings/US-ASCII_decl.rb
new file mode 100644
index 0000000000..1e69234fff
--- /dev/null
+++ b/lib/rexml/encodings/US-ASCII_decl.rb
@@ -0,0 +1,6 @@
+module REXML
+ module Encoding
+ US_ASCII = 'US-ASCII'
+ claim( US_ASCII )
+ end
+end
diff --git a/lib/rexml/encodings/UTF-16.rb b/lib/rexml/encodings/UTF-16.rb
new file mode 100644
index 0000000000..2aeef76a0c
--- /dev/null
+++ b/lib/rexml/encodings/UTF-16.rb
@@ -0,0 +1,27 @@
+module REXML
+ module Encoding
+ def to_utf_16 content
+ array_utf8 = content.unpack("U*")
+ array_enc = []
+ array_utf8.each do |num|
+ if ((num>>16) > 0)
+ array_enc << 0
+ array_enc << ??
+ else
+ array_enc << (num >> 8)
+ array_enc << (num & 0xFF)
+ end
+ end
+ array_enc.pack('C*')
+ end
+
+ def from_utf_16(str)
+ array_enc=str.unpack('C*')
+ array_utf8 = []
+ 2.step(arrayEnc.size-1, 2){|i|
+ array_utf8 << (array_enc.at(i+1) + array_enc.at(i)*0x100)
+ }
+ array_utf8.pack('U*')
+ end
+ end
+end
diff --git a/lib/rexml/encodings/UTF-16_decl.rb b/lib/rexml/encodings/UTF-16_decl.rb
new file mode 100644
index 0000000000..f405a9f259
--- /dev/null
+++ b/lib/rexml/encodings/UTF-16_decl.rb
@@ -0,0 +1,6 @@
+module REXML
+ module Encoding
+ UTF_16 = 'UTF-16'
+ claim( UTF_16, /^\376\377/ )
+ end
+end