def fix_character_encoding(element)
meth = 'Channel#fix_character_encoding'
enc = $config['character_encoding'] || 'ISO-8859-1'
if $HAVE_LIB['iconv'] && $config['use_iconv']
unless $iconv
enc += '//TRANSLIT' if $config['use_iconv_translit']
$iconv = Iconv.new(enc, 'UTF-8')
end
element_text = element.text.to_s
begin
ret = $iconv.iconv(element_text) << $iconv.iconv(nil)
rescue Iconv::IllegalSequence => e
success_str = e.success
ch, pending_str = e.failed.split(//, 2)
ch_int = ch.to_i
t = Time.now
if String::UNICODE_LUT.has_key?(ch_int)
err_str = _('converting unicode')
$log.warn(meth) { "#{err_str} ##{ch_int}" }
element_text = success_str + UNICODE_LUT[ch_int] + pending_str
else
if $config['iconv_munge_illegal']
err_str = _('munging unicode')
$log.warn(meth) { "#{err_str} ##{ch_int}" }
munge_str = $config['unicode_munge_str']
element_text = success_str + munge_str + pending_str
else
err_str = _('dropping unicode')
$log.warn(meth) { "#{err_str} ##{ch_int}" }
element_text = success_str + pending_str
end
end
retry
end
else
ret = (REXML::Output.new('', enc) << element.text).to_s
end
ret.unescape_html
end