/tools/Ruby/lib/ruby/1.8/xsd/iconvcharset.rb

http://github.com/agross/netopenspace · Ruby · 33 lines · 19 code · 9 blank · 5 comment · 0 complexity · c19c7052a66551bc95e8b9c209ce06b5 MD5 · raw file

  1. # XSD4R - Charset handling with iconv.
  2. # Copyright (C) 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
  3. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
  4. # redistribute it and/or modify it under the same terms of Ruby's license;
  5. # either the dual license version in 2003, or any later version.
  6. require 'iconv'
  7. module XSD
  8. class IconvCharset
  9. def self.safe_iconv(to, from, str)
  10. iconv = Iconv.new(to, from)
  11. out = ""
  12. begin
  13. out << iconv.iconv(str)
  14. rescue Iconv::IllegalSequence => e
  15. out << e.success
  16. ch, str = e.failed.split(//, 2)
  17. out << '?'
  18. warn("Failed to convert #{ch}")
  19. retry
  20. end
  21. return out
  22. end
  23. end
  24. end