File: xmlgrep.rb

package info (click to toggle)
libxml-parser-ruby 0.5.16-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 596 kB
  • ctags: 702
  • sloc: ruby: 4,474; ansic: 1,254; xml: 542; makefile: 53
file content (122 lines) | stat: -rwxr-xr-x 2,226 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /usr/local/bin/ruby

## Expat for Ruby sample
## 1998 by yoshidam
##
## XML եΥƥФɽԤʤ

require 'parsearg'
require 'xmlparser'
require 'uconv'
require 'nkf'

class XMLRetry<Exception; end

def xmlgrep (pattern, file = nil, printfile = 0, printline = 0)

  ## file open
  if !file
    f = $stdin
  else
    begin
      f = open(file, "r")
    rescue
      $stderr.print "#{$0}: #{$!}\n";
      return
    end
  end


  ## read body
  xml = f.read
  f.close

  ## dummy default handler
  parser = XMLParser.new
  def parser.unknownEncoding(e)
    raise XMLRetry, e
  end
  def parser.default
  end

  ## start to parse
  current = 0
  begin
    parser.parse(xml) do |type, name, data|
      case type
      when XMLParser::START_ELEM

      when XMLParser::END_ELEM

      when XMLParser::CDATA
        next if data =~ /^\s*$/;
        data = Uconv.u8toeuc(data)

        if data =~ pattern
          line = parser.line
          print "#{file}:" if (file && printfile == 1)
          print "#{line}:" if printline == 1
          parser.defaultCurrent
          current = 1
        end
      when XMLParser::PI

      else
        next if current == 0
        data.gsub!("\n", "\\n")
        print Uconv.u8toeuc("#{data}\n")
        current = 0
      end
    end
  rescue XMLRetry
    newencoding = nil
    e = $!.to_s
    if e =~ /^iso-2022-jp$/i
      xml = NKF.nkf("-Je", xml)
      newencoding = "EUC-JP"
    end
    parser = XMLParser.new(newencoding)
    def parser.default; end
    retry
  rescue XMLParserError
    line = parser.line
    print "#{$0}: #{$!} (#{file}:#{line})\n"
  end
end

def usage
  $stderr.print "Usage: #{$0} [-n] [-e] <expr> <files...>\n"
  exit 1
end

parseArgs(0, nil, "n", "e:")
if (ARGV.length == 0 && $OPT_e == nil)
  usage
end
if ($OPT_e)
  expr = $OPT_e
else
  expr = ARGV.shift
end

begin
  pattern = Regexp.new(expr);
rescue
  $stderr.print "#{$0}: #{$!}\n"
  exit 1
end

if (ARGV.length > 1)
  ARGV.each do |file|
    xmlgrep(pattern, file, 1, $OPT_n ? 1 : 0)
  end
elsif (ARGV.length == 1)
  xmlgrep(pattern, ARGV[0], 0, $OPT_n ? 1 : 0)
else
  xmlgrep(pattern, nil, 0, $OPT_n ? 1 : 0)
end

__END__
## Local variables:
## mode: ruby
## End: