Applied Nobu's patch to the XML document encoding structure in REXML. It
passes all of REXML's native tests as well as a couple of others, and should fix potential threading issues. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ff866f0a8f
commit
47bd6a4f37
@ -1,6 +1,16 @@
|
|||||||
|
# -*- mode: ruby; ruby-indent-level: 2; indent-tabs-mode: t; tab-width: 2 -*- vim: sw=2 ts=2
|
||||||
module REXML
|
module REXML
|
||||||
module Encoding
|
module Encoding
|
||||||
@@uconv_available = false
|
@encoding_methods = {}
|
||||||
|
def self.register(enc, &block)
|
||||||
|
@encoding_methods[enc] = block
|
||||||
|
end
|
||||||
|
def self.apply(obj, enc)
|
||||||
|
@encoding_methods[enc][obj]
|
||||||
|
end
|
||||||
|
def self.encoding_method(enc)
|
||||||
|
@encoding_methods[enc]
|
||||||
|
end
|
||||||
|
|
||||||
# Native, default format is UTF-8, so it is declared here rather than in
|
# Native, default format is UTF-8, so it is declared here rather than in
|
||||||
# an encodings/ definition.
|
# an encodings/ definition.
|
||||||
@ -18,26 +28,24 @@ module REXML
|
|||||||
if enc and enc != UTF_8
|
if enc and enc != UTF_8
|
||||||
@encoding = enc.upcase
|
@encoding = enc.upcase
|
||||||
begin
|
begin
|
||||||
load 'rexml/encodings/ICONV.rb'
|
require 'rexml/encodings/ICONV.rb'
|
||||||
instance_eval @@__REXML_encoding_methods
|
Encoding.apply(self, "ICONV")
|
||||||
Iconv::iconv( UTF_8, @encoding, "" )
|
|
||||||
rescue LoadError, Exception => err
|
rescue LoadError, Exception => err
|
||||||
raise "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/
|
raise ArgumentError, "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/
|
||||||
@encoding.untaint
|
@encoding.untaint
|
||||||
enc_file = File.join( "rexml", "encodings", "#@encoding.rb" )
|
enc_file = File.join( "rexml", "encodings", "#@encoding.rb" )
|
||||||
begin
|
begin
|
||||||
load enc_file
|
require enc_file
|
||||||
instance_eval @@__REXML_encoding_methods
|
Encoding.apply(self, @encoding)
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
puts $!.message
|
puts $!.message
|
||||||
raise Exception.new( "No decoder found for encoding #@encoding. Please install iconv." )
|
raise ArgumentError, "No decoder found for encoding #@encoding. Please install iconv."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
enc = UTF_8
|
@encoding = UTF_8
|
||||||
@encoding = enc.upcase
|
require 'rexml/encodings/UTF-8.rb'
|
||||||
load 'rexml/encodings/UTF-8.rb'
|
Encoding.apply(self, @encoding)
|
||||||
instance_eval @@__REXML_encoding_methods
|
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
$VERBOSE = old_verbosity
|
$VERBOSE = old_verbosity
|
||||||
|
@ -1,37 +1,20 @@
|
|||||||
begin
|
|
||||||
require 'iconv'
|
|
||||||
|
|
||||||
module REXML
|
|
||||||
module Encoding
|
|
||||||
@@__REXML_encoding_methods =<<-EOL
|
|
||||||
def decode(str)
|
|
||||||
return Iconv::iconv("utf-8", "euc-jp", str)[0]
|
|
||||||
end
|
|
||||||
|
|
||||||
def encode content
|
|
||||||
return Iconv::iconv("euc-jp", "utf-8", content)[0]
|
|
||||||
end
|
|
||||||
EOL
|
|
||||||
end
|
|
||||||
end
|
|
||||||
rescue LoadError
|
|
||||||
begin
|
|
||||||
require 'uconv'
|
require 'uconv'
|
||||||
|
|
||||||
module REXML
|
module REXML
|
||||||
module Encoding
|
module Encoding
|
||||||
@@__REXML_encoding_methods =<<-EOL
|
def decode_eucjp(str)
|
||||||
def decode(str)
|
Uconv::euctou8(str)
|
||||||
return Uconv::euctou8(str)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def encode content
|
def encode_eucjp content
|
||||||
return Uconv::u8toeuc(content)
|
Uconv::u8toeuc(content)
|
||||||
end
|
end
|
||||||
EOL
|
|
||||||
|
register("EUC-JP") do |obj|
|
||||||
|
class << obj
|
||||||
|
alias decode decode_eucjp
|
||||||
|
alias encode encode_eucjp
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue LoadError
|
|
||||||
raise "uconv or iconv is required for Japanese encoding support."
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,14 +3,20 @@ raise LoadError unless defined? Iconv
|
|||||||
|
|
||||||
module REXML
|
module REXML
|
||||||
module Encoding
|
module Encoding
|
||||||
@@__REXML_encoding_methods =<<-EOL
|
def decode_iconv(str)
|
||||||
def decode( str )
|
Iconv.conv(UTF_8, @encoding, str)
|
||||||
return Iconv::iconv("utf-8", @encoding, str)[0]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def encode( content )
|
def encode_iconv(content)
|
||||||
return Iconv::iconv(@encoding, "utf-8", content)[0]
|
Iconv.conv(@encoding, UTF_8, content)
|
||||||
|
end
|
||||||
|
|
||||||
|
register("ICONV") do |obj|
|
||||||
|
Iconv.conv(UTF_8, obj.encoding, nil)
|
||||||
|
class << obj
|
||||||
|
alias decode decode_iconv
|
||||||
|
alias encode encode_iconv
|
||||||
|
end
|
||||||
end
|
end
|
||||||
EOL
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,25 +1,7 @@
|
|||||||
|
require 'rexml/encodings/US-ASCII'
|
||||||
|
|
||||||
module REXML
|
module REXML
|
||||||
module Encoding
|
module Encoding
|
||||||
@@__REXML_encoding_methods = %q~
|
register("ISO-8859-1", &encoding_method("US-ASCII"))
|
||||||
# Convert from UTF-8
|
|
||||||
def encode 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 decode(str)
|
|
||||||
str.unpack('C*').pack('U*')
|
|
||||||
end
|
|
||||||
~
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,37 +1,22 @@
|
|||||||
begin
|
|
||||||
require 'iconv'
|
|
||||||
|
|
||||||
module REXML
|
|
||||||
module Encoding
|
|
||||||
@@__REXML_encoding_methods =<<-EOL
|
|
||||||
def decode(str)
|
|
||||||
return Iconv::iconv("utf-8", "shift_jis", str)[0]
|
|
||||||
end
|
|
||||||
|
|
||||||
def encode content
|
|
||||||
return Iconv::iconv("shift_jis", "utf-8", content)[0]
|
|
||||||
end
|
|
||||||
EOL
|
|
||||||
end
|
|
||||||
end
|
|
||||||
rescue LoadError
|
|
||||||
begin
|
|
||||||
require 'uconv'
|
require 'uconv'
|
||||||
|
|
||||||
module REXML
|
module REXML
|
||||||
module Encoding
|
module Encoding
|
||||||
@@__REXML_encoding_methods =<<-EOL
|
def decode_sjis content
|
||||||
def encode(content)
|
|
||||||
Uconv::u8tosjis(content)
|
Uconv::u8tosjis(content)
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode(str)
|
def encode_sjis(str)
|
||||||
Uconv::sjistou8(str)
|
Uconv::sjistou8(str)
|
||||||
end
|
end
|
||||||
EOL
|
|
||||||
|
b = proc do |obj|
|
||||||
|
class << obj
|
||||||
|
alias decode decode_sjis
|
||||||
|
alias encode encode_sjis
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue LoadError
|
register("SHIFT-JIS", &b)
|
||||||
raise "uconv or iconv is required for Japanese encoding support."
|
register("SHIFT_JIS", &b)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
module REXML
|
module REXML
|
||||||
module Encoding
|
module Encoding
|
||||||
@@__REXML_encoding_methods =<<-EOL
|
def encode_unile content
|
||||||
def encode content
|
|
||||||
array_utf8 = content.unpack("U*")
|
array_utf8 = content.unpack("U*")
|
||||||
array_enc = []
|
array_enc = []
|
||||||
array_utf8.each do |num|
|
array_utf8.each do |num|
|
||||||
@ -16,7 +15,7 @@ module REXML
|
|||||||
array_enc.pack('C*')
|
array_enc.pack('C*')
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode(str)
|
def decode_unile(str)
|
||||||
array_enc=str.unpack('C*')
|
array_enc=str.unpack('C*')
|
||||||
array_utf8 = []
|
array_utf8 = []
|
||||||
2.step(array_enc.size-1, 2){|i|
|
2.step(array_enc.size-1, 2){|i|
|
||||||
@ -24,6 +23,12 @@ module REXML
|
|||||||
}
|
}
|
||||||
array_utf8.pack('U*')
|
array_utf8.pack('U*')
|
||||||
end
|
end
|
||||||
EOL
|
|
||||||
|
register(UNILE) do |obj|
|
||||||
|
class << obj
|
||||||
|
alias decode decode_unile
|
||||||
|
alias encode encode_unile
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
module REXML
|
module REXML
|
||||||
module Encoding
|
module Encoding
|
||||||
@@__REXML_encoding_methods = %q~
|
|
||||||
# Convert from UTF-8
|
# Convert from UTF-8
|
||||||
def encode content
|
def encode_ascii content
|
||||||
array_utf8 = content.unpack('U*')
|
array_utf8 = content.unpack('U*')
|
||||||
array_enc = []
|
array_enc = []
|
||||||
array_utf8.each do |num|
|
array_utf8.each do |num|
|
||||||
@ -17,9 +16,15 @@ module REXML
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Convert to UTF-8
|
# Convert to UTF-8
|
||||||
def decode(str)
|
def decode_ascii(str)
|
||||||
str.unpack('C*').pack('U*')
|
str.unpack('C*').pack('U*')
|
||||||
end
|
end
|
||||||
~
|
|
||||||
|
register("US-ASCII") do |obj|
|
||||||
|
class << obj
|
||||||
|
alias decode decode_ascii
|
||||||
|
alias encode encode_ascii
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
module REXML
|
module REXML
|
||||||
module Encoding
|
module Encoding
|
||||||
@@__REXML_encoding_methods =<<-EOL
|
def encode_utf16 content
|
||||||
def encode content
|
|
||||||
array_utf8 = content.unpack("U*")
|
array_utf8 = content.unpack("U*")
|
||||||
array_enc = []
|
array_enc = []
|
||||||
array_utf8.each do |num|
|
array_utf8.each do |num|
|
||||||
@ -16,7 +15,7 @@ module REXML
|
|||||||
array_enc.pack('C*')
|
array_enc.pack('C*')
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode(str)
|
def decode_utf16(str)
|
||||||
array_enc=str.unpack('C*')
|
array_enc=str.unpack('C*')
|
||||||
array_utf8 = []
|
array_utf8 = []
|
||||||
2.step(array_enc.size-1, 2){|i|
|
2.step(array_enc.size-1, 2){|i|
|
||||||
@ -24,6 +23,12 @@ module REXML
|
|||||||
}
|
}
|
||||||
array_utf8.pack('U*')
|
array_utf8.pack('U*')
|
||||||
end
|
end
|
||||||
EOL
|
|
||||||
|
register(UTF_16) do |obj|
|
||||||
|
class << obj
|
||||||
|
alias decode decode_utf16
|
||||||
|
alias encode encode_utf16
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
module REXML
|
module REXML
|
||||||
module Encoding
|
module Encoding
|
||||||
@@__REXML_encoding_methods =<<-EOL
|
def encode_utf8 content
|
||||||
def encode content
|
|
||||||
content
|
content
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode(str)
|
def decode_utf8(str)
|
||||||
str
|
str
|
||||||
end
|
end
|
||||||
EOL
|
|
||||||
|
register(UTF_8) do |obj|
|
||||||
|
class << obj
|
||||||
|
alias decode decode_utf8
|
||||||
|
alias encode encode_utf8
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user