Class: Seiun::XMLParsers::StreamListener

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/seiun/xml_parsers/stream_listener.rb

Instance Method Summary collapse

Constructor Details

#initialize(find_tag, callback) ⇒ StreamListener

Returns a new instance of StreamListener.



6
7
8
9
10
# File 'lib/seiun/xml_parsers/stream_listener.rb', line 6

def initialize(find_tag, callback)
  @find_tag = find_tag
  @callback = callback
  @stack = []
end

Instance Method Details

#tag_end(name) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/seiun/xml_parsers/stream_listener.rb', line 32

def tag_end(name)
  if @stack.size == 1 && name == @find_tag
    @callback.call(Marshal.load(Marshal.dump(@current)))
    @current, @stack = nil, []
  elsif @current
    pop_tag = @stack.pop
  end
end

#tag_start(name, attrs) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/seiun/xml_parsers/stream_listener.rb', line 12

def tag_start(name, attrs)
  if @stack.empty? && name == @find_tag
    element = []
    element << attrs unless attrs.empty?
    @current = element
    @stack = [ element ]
  elsif @current
    element = []
    element << attrs unless attrs.empty?
    @stack.last << { name => element }
    @stack.push(element)
  end
end

#text(text) ⇒ Object



26
27
28
29
30
# File 'lib/seiun/xml_parsers/stream_listener.rb', line 26

def text(text)
  text = text.strip
  return if text.empty?
  @stack.last << text if @current
end