/tools/Ruby/lib/ruby/1.8/rexml/encoding.rb

http://github.com/agross/netopenspace · Ruby · 71 lines · 63 code · 3 blank · 5 comment · 13 complexity · 34320e328a5a2290519feabf5ebdb583 MD5 · raw file

  1. # -*- mode: ruby; ruby-indent-level: 2; indent-tabs-mode: t; tab-width: 2 -*- vim: sw=2 ts=2
  2. module REXML
  3. module Encoding
  4. @encoding_methods = {}
  5. def self.register(enc, &block)
  6. @encoding_methods[enc] = block
  7. end
  8. def self.apply(obj, enc)
  9. @encoding_methods[enc][obj]
  10. end
  11. def self.encoding_method(enc)
  12. @encoding_methods[enc]
  13. end
  14. # Native, default format is UTF-8, so it is declared here rather than in
  15. # an encodings/ definition.
  16. UTF_8 = 'UTF-8'
  17. UTF_16 = 'UTF-16'
  18. UNILE = 'UNILE'
  19. # ID ---> Encoding name
  20. attr_reader :encoding
  21. def encoding=( enc )
  22. old_verbosity = $VERBOSE
  23. begin
  24. $VERBOSE = false
  25. enc = enc.nil? ? nil : enc.upcase
  26. return false if defined? @encoding and enc == @encoding
  27. if enc and enc != UTF_8
  28. @encoding = enc
  29. raise ArgumentError, "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/
  30. @encoding.untaint
  31. begin
  32. require 'rexml/encodings/ICONV.rb'
  33. Encoding.apply(self, "ICONV")
  34. rescue LoadError, Exception
  35. begin
  36. enc_file = File.join( "rexml", "encodings", "#@encoding.rb" )
  37. require enc_file
  38. Encoding.apply(self, @encoding)
  39. rescue LoadError => err
  40. puts err.message
  41. raise ArgumentError, "No decoder found for encoding #@encoding. Please install iconv."
  42. end
  43. end
  44. else
  45. @encoding = UTF_8
  46. require 'rexml/encodings/UTF-8.rb'
  47. Encoding.apply(self, @encoding)
  48. end
  49. ensure
  50. $VERBOSE = old_verbosity
  51. end
  52. true
  53. end
  54. def check_encoding str
  55. # We have to recognize UTF-16, LSB UTF-16, and UTF-8
  56. if str[0] == 0xfe && str[1] == 0xff
  57. str[0,2] = ""
  58. return UTF_16
  59. elsif str[0] == 0xff && str[1] == 0xfe
  60. str[0,2] = ""
  61. return UNILE
  62. end
  63. str =~ /^\s*<\?xml\s+version\s*=\s*(['"]).*?\1\s+encoding\s*=\s*(["'])(.*?)\2/um
  64. return $3.upcase if $3
  65. return UTF_8
  66. end
  67. end
  68. end