|
Revision 1824, 0.7 kB
(checked in by nahi, 1 year ago)
|
- Copyright notice updated. add '2000-2007' uniformly.
|
- Property svn:eol-style set to
native
- Property svn:keywords set to
author date id revision
|
| Line | |
|---|
| 1 |
# XSD4R - Charset handling with iconv. |
|---|
| 2 |
# Copyright (C) 2000-2007 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. |
|---|
| 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 |
|---|