annotate .svn/pristine/22/229053adf4d0f0cdc30b283c6478a18c47c6fe1d.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 if RUBY_VERSION < '1.9'
Chris@1295 2 require 'iconv'
Chris@1295 3 end
Chris@1295 4
Chris@1295 5 module Redmine
Chris@1295 6 module CodesetUtil
Chris@1295 7
Chris@1295 8 def self.replace_invalid_utf8(str)
Chris@1295 9 return str if str.nil?
Chris@1295 10 if str.respond_to?(:force_encoding)
Chris@1295 11 str.force_encoding('UTF-8')
Chris@1295 12 if ! str.valid_encoding?
Chris@1295 13 str = str.encode("US-ASCII", :invalid => :replace,
Chris@1295 14 :undef => :replace, :replace => '?').encode("UTF-8")
Chris@1295 15 end
Chris@1295 16 elsif RUBY_PLATFORM == 'java'
Chris@1295 17 begin
Chris@1295 18 ic = Iconv.new('UTF-8', 'UTF-8')
Chris@1295 19 str = ic.iconv(str)
Chris@1295 20 rescue
Chris@1295 21 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@1295 22 end
Chris@1295 23 else
Chris@1295 24 ic = Iconv.new('UTF-8', 'UTF-8')
Chris@1295 25 txtar = ""
Chris@1295 26 begin
Chris@1295 27 txtar += ic.iconv(str)
Chris@1295 28 rescue Iconv::IllegalSequence
Chris@1295 29 txtar += $!.success
Chris@1295 30 str = '?' + $!.failed[1,$!.failed.length]
Chris@1295 31 retry
Chris@1295 32 rescue
Chris@1295 33 txtar += $!.success
Chris@1295 34 end
Chris@1295 35 str = txtar
Chris@1295 36 end
Chris@1295 37 str
Chris@1295 38 end
Chris@1295 39
Chris@1295 40 def self.to_utf8(str, encoding)
Chris@1295 41 return str if str.nil?
Chris@1295 42 str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
Chris@1295 43 if str.empty?
Chris@1295 44 str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
Chris@1295 45 return str
Chris@1295 46 end
Chris@1295 47 enc = encoding.blank? ? "UTF-8" : encoding
Chris@1295 48 if str.respond_to?(:force_encoding)
Chris@1295 49 if enc.upcase != "UTF-8"
Chris@1295 50 str.force_encoding(enc)
Chris@1295 51 str = str.encode("UTF-8", :invalid => :replace,
Chris@1295 52 :undef => :replace, :replace => '?')
Chris@1295 53 else
Chris@1295 54 str.force_encoding("UTF-8")
Chris@1295 55 if ! str.valid_encoding?
Chris@1295 56 str = str.encode("US-ASCII", :invalid => :replace,
Chris@1295 57 :undef => :replace, :replace => '?').encode("UTF-8")
Chris@1295 58 end
Chris@1295 59 end
Chris@1295 60 elsif RUBY_PLATFORM == 'java'
Chris@1295 61 begin
Chris@1295 62 ic = Iconv.new('UTF-8', enc)
Chris@1295 63 str = ic.iconv(str)
Chris@1295 64 rescue
Chris@1295 65 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@1295 66 end
Chris@1295 67 else
Chris@1295 68 ic = Iconv.new('UTF-8', enc)
Chris@1295 69 txtar = ""
Chris@1295 70 begin
Chris@1295 71 txtar += ic.iconv(str)
Chris@1295 72 rescue Iconv::IllegalSequence
Chris@1295 73 txtar += $!.success
Chris@1295 74 str = '?' + $!.failed[1,$!.failed.length]
Chris@1295 75 retry
Chris@1295 76 rescue
Chris@1295 77 txtar += $!.success
Chris@1295 78 end
Chris@1295 79 str = txtar
Chris@1295 80 end
Chris@1295 81 str
Chris@1295 82 end
Chris@1295 83
Chris@1295 84 def self.to_utf8_by_setting(str)
Chris@1295 85 return str if str.nil?
Chris@1295 86 str = self.to_utf8_by_setting_internal(str)
Chris@1295 87 if str.respond_to?(:force_encoding)
Chris@1295 88 str.force_encoding('UTF-8')
Chris@1295 89 end
Chris@1295 90 str
Chris@1295 91 end
Chris@1295 92
Chris@1295 93 def self.to_utf8_by_setting_internal(str)
Chris@1295 94 return str if str.nil?
Chris@1295 95 if str.respond_to?(:force_encoding)
Chris@1295 96 str.force_encoding('ASCII-8BIT')
Chris@1295 97 end
Chris@1295 98 return str if str.empty?
Chris@1295 99 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
Chris@1295 100 if str.respond_to?(:force_encoding)
Chris@1295 101 str.force_encoding('UTF-8')
Chris@1295 102 end
Chris@1295 103 encodings = Setting.repositories_encodings.split(',').collect(&:strip)
Chris@1295 104 encodings.each do |encoding|
Chris@1295 105 if str.respond_to?(:force_encoding)
Chris@1295 106 begin
Chris@1295 107 str.force_encoding(encoding)
Chris@1295 108 utf8 = str.encode('UTF-8')
Chris@1295 109 return utf8 if utf8.valid_encoding?
Chris@1295 110 rescue
Chris@1295 111 # do nothing here and try the next encoding
Chris@1295 112 end
Chris@1295 113 else
Chris@1295 114 begin
Chris@1295 115 return Iconv.conv('UTF-8', encoding, str)
Chris@1295 116 rescue Iconv::Failure
Chris@1295 117 # do nothing here and try the next encoding
Chris@1295 118 end
Chris@1295 119 end
Chris@1295 120 end
Chris@1295 121 str = self.replace_invalid_utf8(str)
Chris@1295 122 if str.respond_to?(:force_encoding)
Chris@1295 123 str.force_encoding('UTF-8')
Chris@1295 124 end
Chris@1295 125 str
Chris@1295 126 end
Chris@1295 127
Chris@1295 128 def self.from_utf8(str, encoding)
Chris@1295 129 str ||= ''
Chris@1295 130 if str.respond_to?(:force_encoding)
Chris@1295 131 str.force_encoding('UTF-8')
Chris@1295 132 if encoding.upcase != 'UTF-8'
Chris@1295 133 str = str.encode(encoding, :invalid => :replace,
Chris@1295 134 :undef => :replace, :replace => '?')
Chris@1295 135 else
Chris@1295 136 str = self.replace_invalid_utf8(str)
Chris@1295 137 end
Chris@1295 138 elsif RUBY_PLATFORM == 'java'
Chris@1295 139 begin
Chris@1295 140 ic = Iconv.new(encoding, 'UTF-8')
Chris@1295 141 str = ic.iconv(str)
Chris@1295 142 rescue
Chris@1295 143 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@1295 144 end
Chris@1295 145 else
Chris@1295 146 ic = Iconv.new(encoding, 'UTF-8')
Chris@1295 147 txtar = ""
Chris@1295 148 begin
Chris@1295 149 txtar += ic.iconv(str)
Chris@1295 150 rescue Iconv::IllegalSequence
Chris@1295 151 txtar += $!.success
Chris@1295 152 str = '?' + $!.failed[1, $!.failed.length]
Chris@1295 153 retry
Chris@1295 154 rescue
Chris@1295 155 txtar += $!.success
Chris@1295 156 end
Chris@1295 157 str = txtar
Chris@1295 158 end
Chris@1295 159 end
Chris@1295 160 end
Chris@1295 161 end