Method: Jabber::StreamParser#parse

Defined in:
lib/xmpp4r/streamparser.rb

#parseObject

Begins parsing the XML stream and does not return until the stream closes.



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
# File 'lib/xmpp4r/streamparser.rb', line 36

def parse
  @started = false
  begin
    parser = REXML::Parsers::SAX2Parser.new @stream

    parser.listen( :start_element ) do |uri, localname, qname, attributes|
      e = REXML::Element.new(qname)
      e.add_attributes attributes
      @current = @current.nil? ? e : @current.add_element(e)

      if @current.name == 'stream' and !@started
        @started = true
        @listener.receive(@current)
        @current = nil
      end
    end

    parser.listen( :end_element ) do  |uri, localname, qname|
      if qname == 'stream:stream' and @current.nil?
        @started = false
        @listener.parser_end
      else
        @listener.receive(@current) unless @current.parent
        @current = @current.parent
      end
    end

    parser.listen( :characters ) do | text |
      @current.add(REXML::Text.new(text.to_s, @current.whitespace, nil, true)) if @current
    end

    parser.listen( :cdata ) do | text |
      @current.add(REXML::CData.new(text)) if @current
    end

    parser.parse
  rescue REXML::ParseException => e
    @listener.parse_failure(e)
  end
end