Class: ETL::Parser::Listener

Inherits:
Object
  • Object
show all
Includes:
REXML::SAX2Listener
Defined in:
lib/etl/parser/sax_parser.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(parser, &block) ⇒ Listener

Returns a new instance of Listener.



64
65
66
67
68
69
# File 'lib/etl/parser/sax_parser.rb', line 64

def initialize(parser, &block)
  @parser = parser
  @row = {}
  @value = nil
  @proc = Proc.new(&block)
end

Instance Method Details

#cdata(text) ⇒ Object



70
71
72
# File 'lib/etl/parser/sax_parser.rb', line 70

def cdata(text)    
  @value << text
end

#characters(text) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/etl/parser/sax_parser.rb', line 73

def characters(text)
  text = text.strip
  if (!text.nil? && text != '')
    @value ||= ''
    @value << text
  end
end

#end_documentObject



83
84
85
# File 'lib/etl/parser/sax_parser.rb', line 83

def end_document
  
end

#end_element(uri, localname, qname) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/etl/parser/sax_parser.rb', line 101

def end_element(uri, localname, qname)
  element = @path.elements.last
  
  @parser.fields.each do |field|
    #puts "#{@path} match? #{field.path}"
    if @path.match?(field.path)
      #puts "field.path: #{field.path}"
      if !field.path.is_attribute?
        @row[field.name] = @value
      end
    end
  end
  
  #puts @path.to_s
  if @path.match?(@parser.write_trigger)
    #puts "matched: #{@path} =~ #{@parser.write_trigger}"
    #puts "calling proc with #{@row.inspect}"
    @proc.call(@row.clone)
  end
  
  @value = nil
  @path.elements.pop
end

#progress(position) ⇒ Object



124
125
126
# File 'lib/etl/parser/sax_parser.rb', line 124

def progress(position)
  @position = position
end

#start_documentObject



80
81
82
# File 'lib/etl/parser/sax_parser.rb', line 80

def start_document
  @path = XPath::Path.new
end

#start_element(uri, localname, qname, attributes) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/etl/parser/sax_parser.rb', line 86

def start_element(uri, localname, qname, attributes)
  element = XPath::Element.new(localname, attributes)
  @path.elements << element
  
  @parser.fields.each do |field|
    #puts "#{@path} match? #{field.path}"
    if @path.match?(field.path)
      #puts "field.path: #{field.path}"
      if field.path.is_attribute?
        #puts "setting @row[#{field.name}] to #{element.attributes[field.path.attribute]}"
        @row[field.name] = element.attributes[field.path.attribute]
      end
    end
  end
end