File: xmlevent.rb.old

package info (click to toggle)
libxml-parser-ruby 0.6.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 676 kB
  • ctags: 800
  • sloc: ruby: 5,723; ansic: 1,734; xml: 574; makefile: 152
file content (74 lines) | stat: -rwxr-xr-x 1,628 bytes parent folder | download | duplicates (2)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/local/bin/ruby

require 'xmlparser'
require 'kconv'
include Kconv
require 'uconv'
include Uconv

class SampleParser<XMLParser
  def startElement(name, attr)
    line = self.line
    column = self.column
    byteIndex = self.byteIndex
    print "L#{line}, #{column}, #{byteIndex}\n"
    attr.each do |key, value|
      print u8toeuc("A#{key} CDATA #{value}\n")
    end
    print u8toeuc("(#{name}\n")
    self.defaultCurrent
  end
  private :startElement

  def endElement(name)
    print u8toeuc(")#{name}\n")
  end 
  private :endElement

  def character(data)
    data.gsub!("\n", "\\n")
    print u8toeuc("-#{data}\n")
  end
  private :character

  def processingInstruction(target, data)
    data.gsub!("\n", "\\n")
    print u8toeuc("?#{target} #{data}\n")
  end
  private :processingInstruction

  def default(data)
    data.gsub!("\n", "\\n")
    print u8toeuc("//#{data}\n")
  end
  private :default

end

xml = $<.gets
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
xml += $<.read
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

parser = SampleParser.new
begin
  parser.parse(xml)
rescue XMLParserError
  line = parser.line
  print "Parse error(#{line}): #{$!}\n"
end