Class: Fluent::Config::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/config/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basepath, iterator, fname, i = 0) ⇒ Parser

Returns a new instance of Parser.



30
31
32
33
34
35
# File 'lib/fluent/config/parser.rb', line 30

def initialize(basepath, iterator, fname, i = 0)
  @basepath = basepath
  @iterator = iterator
  @i = i
  @fname = fname
end

Class Method Details

.parse(io, fname, basepath = Dir.pwd) ⇒ Object



25
26
27
28
# File 'lib/fluent/config/parser.rb', line 25

def self.parse(io, fname, basepath = Dir.pwd)
  attrs, elems = Parser.new(basepath, io.each_line, fname).parse!(true)
  Element.new('ROOT', '', attrs, elems)
end

Instance Method Details

#parse!(allow_include, elem_name = nil, attrs = {}, elems = []) ⇒ Object



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
# File 'lib/fluent/config/parser.rb', line 37

def parse!(allow_include, elem_name = nil, attrs = {}, elems = [])
  while line = @iterator.next
    line.force_encoding('UTF-8')
    @i += 1
    line.lstrip!
    line.gsub!(/\s*(?:\#.*)?$/,'')
    if line.empty?
      next
    elsif m = /^\<include\s*(.*)\s*\/\>$/.match(line)
      value = m[1].strip
      process_include(attrs, elems, value, allow_include)
    elsif m = /^\<([a-zA-Z0-9_]+)\s*(.+?)?\>$/.match(line)
      e_name = m[1]
      e_arg = m[2] || ""
      e_attrs, e_elems = parse!(false, e_name)
      elems << Element.new(e_name, e_arg, e_attrs, e_elems)
    elsif line == "</#{elem_name}>"
      break
    elsif m = /^([a-zA-Z0-9_]+)\s*(.*)$/.match(line)
      key = m[1]
      value = m[2]
      if allow_include && key == 'include'
        process_include(attrs, elems, value)
      else
        attrs[key] = value
      end
      next
    else
      raise ConfigParseError, "parse error at #{@fname} line #{@i}"
    end
  end

  return attrs, elems
rescue StopIteration
  return attrs, elems
end

#process_include(attrs, elems, uri, allow_include = true) ⇒ Object



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
# File 'lib/fluent/config/parser.rb', line 74

def process_include(attrs, elems, uri, allow_include = true)
  u = URI.parse(uri)
  if u.scheme == 'file' || u.path == uri  # file path
    path = u.path
    if path[0] != ?/
      pattern = File.expand_path("#{@basepath}/#{path}")
    else
      pattern = path
    end

    Dir.glob(pattern).sort.each { |entry|
      basepath = File.dirname(entry)
      fname = File.basename(entry)
      File.open(entry) { |f|
        Parser.new(basepath, f.each_line, fname).parse!(allow_include, nil, attrs, elems)
      }
    }

  else
    basepath = '/'
    fname = path
    require 'open-uri'
    URI.open(uri) {|f|
      Parser.new(basepath, f.each_line, fname).parse!(allow_include, nil, attrs, elems)
    }
  end

rescue SystemCallError => e
  raise ConfigParseError, "include error at #{@fname} line #{@i}: #{e.to_s}"
end