Module | XSD::Charset |
In: |
lib/xsd/charset.rb
|
EncodingConvertMap | = | {} | Maps | |
CharsetMap | = | { 'NONE' => 'us-ascii', 'EUC' => 'euc-jp', 'SJIS' => 'shift_jis', 'UTF8' => 'utf-8', 'X_ISO_8859_1' => 'iso-8859-1', 'X_UNKNOWN' => nil, } | ||
USASCIIRegexp | = | Regexp.new("\\A#{us_ascii}*\\z", nil, "NONE") | ||
EUCRegexp | = | Regexp.new("\\A#{character_euc}*\\z", nil, "NONE") | ||
SJISRegexp | = | Regexp.new("\\A#{character_sjis}*\\z", nil, "NONE") | ||
UTF8Regexp | = | Regexp.new("\\A#{character_utf8}*\\z", nil, "NONE") |
# File lib/xsd/charset.rb, line 116 116: def Charset.charset_label(encoding) 117: CharsetMap[encoding.upcase] 118: end
# File lib/xsd/charset.rb, line 120 120: def Charset.charset_str(label) 121: if CharsetMap.respond_to?(:key) 122: CharsetMap.key(label.downcase) || 'X_UNKNOWN' 123: else 124: CharsetMap.index(label.downcase) || 'X_UNKNOWN' 125: end 126: end
# File lib/xsd/charset.rb, line 88 88: def Charset.encoding=(encoding) 89: warn("xsd charset is set to #{encoding}") if $DEBUG 90: @internal_encoding = encoding 91: end
# File lib/xsd/charset.rb, line 105 105: def Charset.encoding_conv(str, enc_from, enc_to) 106: if enc_from == enc_to or enc_from == 'NONE' or enc_to == 'NONE' 107: str 108: elsif converter = EncodingConvertMap[[enc_from, enc_to]] 109: converter.call(str) 110: else 111: raise CharsetConversionError.new( 112: "Converter not found: #{enc_from} -> #{enc_to}") 113: end 114: end
# File lib/xsd/charset.rb, line 101 101: def Charset.encoding_from_xml(str, charset) 102: encoding_conv(str, charset_str(charset), @internal_encoding) 103: end
# File lib/xsd/charset.rb, line 97 97: def Charset.encoding_to_xml(str, charset) 98: encoding_conv(str, @internal_encoding, charset_str(charset)) 99: end
# File lib/xsd/charset.rb, line 26 26: def Charset.init 27: EncodingConvertMap[['UTF8', 'X_ISO8859_1']] = 28: Proc.new { |str| str.unpack('U*').pack('C*') } 29: EncodingConvertMap[['X_ISO8859_1', 'UTF8']] = 30: Proc.new { |str| str.unpack('C*').pack('U*') } 31: begin 32: require 'xsd/iconvcharset' 33: @internal_encoding = 'UTF8' 34: sjtag = (/(mswin|bccwin|mingw|cygwin|emx)/ =~ RUBY_PLATFORM) ? 'cp932' : 35: 'shift_jis' 36: EncodingConvertMap[['UTF8', 'EUC' ]] = 37: Proc.new { |str| IconvCharset.safe_iconv("euc-jp", "utf-8", str) } 38: EncodingConvertMap[['EUC' , 'UTF8']] = 39: Proc.new { |str| IconvCharset.safe_iconv("utf-8", "euc-jp", str) } 40: EncodingConvertMap[['EUC' , 'SJIS']] = 41: Proc.new { |str| IconvCharset.safe_iconv(sjtag, "euc-jp", str) } 42: EncodingConvertMap[['UTF8', 'SJIS']] = 43: Proc.new { |str| IconvCharset.safe_iconv(sjtag, "utf-8", str) } 44: EncodingConvertMap[['SJIS', 'UTF8']] = 45: Proc.new { |str| IconvCharset.safe_iconv("utf-8", sjtag, str) } 46: EncodingConvertMap[['SJIS', 'EUC' ]] = 47: Proc.new { |str| IconvCharset.safe_iconv("euc-jp", sjtag, str) } 48: rescue LoadError 49: begin 50: require 'nkf' 51: EncodingConvertMap[['EUC' , 'SJIS']] = 52: Proc.new { |str| NKF.nkf('-sXm0', str) } 53: EncodingConvertMap[['SJIS', 'EUC' ]] = 54: Proc.new { |str| NKF.nkf('-eXm0', str) } 55: rescue LoadError 56: end 57: 58: begin 59: require 'uconv' 60: @internal_encoding = 'UTF8' 61: EncodingConvertMap[['UTF8', 'EUC' ]] = Uconv.method(:u8toeuc) 62: EncodingConvertMap[['UTF8', 'SJIS']] = Uconv.method(:u8tosjis) 63: EncodingConvertMap[['EUC' , 'UTF8']] = Uconv.method(:euctou8) 64: EncodingConvertMap[['SJIS', 'UTF8']] = Uconv.method(:sjistou8) 65: rescue LoadError 66: end 67: end 68: end
# File lib/xsd/charset.rb, line 170 170: def Charset.is_ces(str, code = $KCODE) 171: case code 172: when 'NONE' 173: is_us_ascii(str) 174: when 'UTF8' 175: is_utf8(str) 176: when 'EUC' 177: is_euc(str) 178: when 'SJIS' 179: is_sjis(str) 180: else 181: raise UnknownCharsetError.new("Unknown charset: #{code}") 182: end 183: end
# File lib/xsd/charset.rb, line 154 154: def Charset.is_us_ascii(str) 155: USASCIIRegexp =~ str 156: end