PageRenderTime 149ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/External.LCA_RESTRICTED/Languages/Ruby/redist-libs/ruby/1.9.1/rexml/encoding.rb

#
Ruby | 71 lines | 63 code | 3 blank | 5 comment | 9 complexity | 74e506a7b1082e18d296bfe1962e66d2 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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,2] == "\xfe\xff"
  57. str[0,2] = ""
  58. return UTF_16
  59. elsif str[0,2] == "\xff\xfe"
  60. str[0,2] = ""
  61. return UNILE
  62. end
  63. str =~ /^\s*<\?xml\s+version\s*=\s*(['"]).*?\1\s+encoding\s*=\s*(["'])(.*?)\2/m
  64. return $3.upcase if $3
  65. return UTF_8
  66. end
  67. end
  68. end