To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / codeset_util.rb @ 1298:4f746d8966dd

History | View | Annotate | Download (4.45 KB)

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