annotate lib/redmine/codeset_util.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

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