Class: XML::DOM::JapaneseBuilder

Inherits:
Builder show all
Includes:
Kconv, Uconv
Defined in:
lib/xml/dom/builder-ja.rb

Constant Summary

Constants included from XML::DOM

Attribute

Instance Attribute Summary

Attributes inherited from Builder

#createCDATASection, #createEntityReference

Instance Method Summary collapse

Methods inherited from Builder

#__initialize__, #character, #comment, #defaultHandler, #endCdata, #endElement, #externalEntityRef, new, #parse, #processingInstruction, #startCdata, #startElement, #text

Methods included from XML::DOM

tou16

Instance Method Details

#cdataConverter(str) ⇒ Object



18
19
20
# File 'lib/xml/dom/builder-ja.rb', line 18

def cdataConverter(str)
  u8toeuc(str)
end

#nameConverter(str) ⇒ Object



15
16
17
# File 'lib/xml/dom/builder-ja.rb', line 15

def nameConverter(str)
  u8toeuc(str)
end

#parseStream(stream, trim = false) ⇒ Object



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
# File 'lib/xml/dom/builder-ja.rb', line 22

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