1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
## -*- Ruby -*-
## Tree builder class for Japanese encoding
## 1998 by yoshidam
require 'xmltreebuilder'
module XML
class JapaneseTreeBuilder<SimpleTreeBuilder
require 'kconv'
include Kconv
require 'uconv'
include Uconv
def nameConverter(str)
u8toeuc(str)
end
def cdataConverter(str)
u8toeuc(str)
end
def parseStream(stream, trim = false)
## empty file
if ((xml = stream.gets).nil?); exit 1; end
## rewrite encoding in XML decl.
if xml =~ /^<\?xml\sversion=.+\sencoding=.EUC-JP./i
xml.sub!(/EUC-JP/i, "UTF-8")
encoding = 'EUC-JP'
elsif xml =~ /^<\?xml\sversion=.+\sencoding=.Shift_JIS./i
xml.sub!(/Shift_JIS/i, "UTF-8")
encoding = "Shift_JIS"
elsif xml =~ /^<\?xml\sversion=.+\sencoding=.ISO-2022-JP./i
xml.sub!(/ISO-2022-JP/i, "UTF-8")
encoding = "ISO-2022-JP"
end
## read body
xml += String(stream.read)
## convert body encoding
if encoding == "EUC-JP"
xml = euctou8(xml)
elsif encoding == "Shift_JIS"
xml = euctou8(kconv(xml, EUC, SJIS))
elsif encoding == "ISO-2022-JP"
xml = euctou8(kconv(xml, EUC, JIS))
end
return parse(xml, trim)
end
def Uconv.unknown_unicode_handler(u)
return ''
end
end
end
|