File: buildertest.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 (58 lines) | stat: -rwxr-xr-x 1,236 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
#! /usr/local/bin/ruby

require 'xmltreebuilder'
require 'kconv'
include Kconv
require 'uconv'
include Uconv


def Uconv.unknown_unicode_handler(u)
  return ''
#  return "#[#{format('%04x', u)}]"
end

builder = XML::SimpleTreeBuilder.new
def builder.nameConverter(str)
  u8toeuc(str)
end
def builder.cdataConverter(str)
  u8toeuc(str)
end

## empty file
if ((xml = $<.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($<.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

begin
  tree = builder.parse(xml)
rescue XMLParserError
  line = builder.line
  print "#{$0}: #{$!} (in line #{line})\n"
  exit 1
end
#print tree.to_s.gsub(/\#\[([0-9a-f]{4})\]/, "&#x\\1;"), "\n"
tree.dump