Line | |
---|
1 | # XSD4R - Charset handling with iconv.
|
---|
2 | # Copyright (C) 2003 NAKAMURA, Hiroshi <[email protected]>.
|
---|
3 |
|
---|
4 | # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
|
---|
5 | # redistribute it and/or modify it under the same terms of Ruby's license;
|
---|
6 | # either the dual license version in 2003, or any later version.
|
---|
7 |
|
---|
8 |
|
---|
9 | require 'iconv'
|
---|
10 |
|
---|
11 |
|
---|
12 | module XSD
|
---|
13 |
|
---|
14 |
|
---|
15 | class IconvCharset
|
---|
16 | def self.safe_iconv(to, from, str)
|
---|
17 | iconv = Iconv.new(to, from)
|
---|
18 | out = ""
|
---|
19 | begin
|
---|
20 | out << iconv.iconv(str)
|
---|
21 | rescue Iconv::IllegalSequence => e
|
---|
22 | out << e.success
|
---|
23 | ch, str = e.failed.split(//, 2)
|
---|
24 | out << '?'
|
---|
25 | warn("Failed to convert #{ch}")
|
---|
26 | retry
|
---|
27 | end
|
---|
28 | return out
|
---|
29 | end
|
---|
30 | end
|
---|
31 |
|
---|
32 |
|
---|
33 | end
|
---|
Note:
See
TracBrowser
for help on using the repository browser.