Class: REXML::Parsers::StreamParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rexml/parsers/streamparser.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, listener) ⇒ StreamParser

Returns a new instance of StreamParser.



7
8
9
10
11
# File 'lib/rexml/parsers/streamparser.rb', line 7

def initialize source, listener
  @listener = listener
  @parser = BaseParser.new( source )
  @tag_stack = []
end

Instance Method Details

#add_listener(listener) ⇒ Object



13
14
15
# File 'lib/rexml/parsers/streamparser.rb', line 13

def add_listener( listener )
  @parser.add_listener( listener )
end

#parseObject



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
# File 'lib/rexml/parsers/streamparser.rb', line 17

def parse
  # entity string
  while true
    event = @parser.pull
    case event[0]
    when :end_document
      unless @tag_stack.empty?
        tag_path = "/" + @tag_stack.join("/")
        raise ParseException.new("Missing end tag for '#{tag_path}'",
                                 @parser.source)
      end
      return
    when :start_element
      @tag_stack << event[1]
      attrs = event[2].each do |n, v|
        event[2][n] = @parser.unnormalize( v )
      end
      @listener.tag_start( event[1], attrs )
    when :end_element
      @listener.tag_end( event[1] )
      @tag_stack.pop
    when :text
      normalized = @parser.unnormalize( event[1] )
      @listener.text( normalized )
    when :processing_instruction
      @listener.instruction( *event[1,2] )
    when :start_doctype
      @listener.doctype( *event[1..-1] )
    when :end_doctype
      # FIXME: remove this condition for milestone:3.2
      @listener.doctype_end if @listener.respond_to? :doctype_end
    when :comment, :attlistdecl, :cdata, :xmldecl, :elementdecl
      @listener.send( event[0].to_s, *event[1..-1] )
    when :entitydecl, :notationdecl
      @listener.send( event[0].to_s, event[1..-1] )
    when :externalentity
      entity_reference = event[1]
      content = entity_reference.gsub(/\A%|;\z/, "")
      @listener.entity(content)
    end
  end
end