1 # -*- mode: ruby; ruby-indent-level: 2; indent-tabs-mode: t; tab-width: 2 -*- vim: sw=2 ts=2
5 def self.register(enc, &block)
6 @encoding_methods[enc] = block
8 def self.apply(obj, enc)
9 @encoding_methods[enc][obj]
11 def self.encoding_method(enc)
12 @encoding_methods[enc]
15 # Native, default format is UTF-8, so it is declared here rather than in
16 # an encodings/ definition.
21 # ID ---> Encoding name
24 old_verbosity = $VERBOSE
27 enc = enc.nil? ? nil : enc.upcase
28 return false if defined? @encoding and enc == @encoding
29 if enc and enc != UTF_8
31 raise ArgumentError, "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/
34 require 'booh/rexml/encodings/ICONV.rb'
35 Encoding.apply(self, "ICONV")
36 rescue LoadError, Exception
38 enc_file = File.join( "rexml", "encodings", "#@encoding.rb" )
40 Encoding.apply(self, @encoding)
41 rescue LoadError => err
43 raise ArgumentError, "No decoder found for encoding #@encoding. Please install iconv."
48 require 'booh/rexml/encodings/UTF-8.rb'
49 Encoding.apply(self, @encoding)
52 $VERBOSE = old_verbosity
57 def check_encoding str
58 # We have to recognize UTF-16, LSB UTF-16, and UTF-8
59 return UTF_16 if /\A\xfe\xff/n =~ str
60 return UNILE if /\A\xff\xfe/n =~ str
61 str =~ /^\s*<?xml\s*version\s*=\s*(['"]).*?\2\s*encoding\s*=\s*(["'])(.*?)\2/um
62 return $1.upcase if $1