| Class | Jabber::StreamParser |
| In: |
lib/xmpp4r/streamparser.rb
|
| Parent: | Object |
The StreamParser uses REXML to parse the incoming XML stream of the Jabber protocol and fires XMPPStanza at the Connection instance.
| started | [R] | status if the parser is started |
Constructs a parser for the supplied stream (socket input)
| stream: | [IO] Socket input stream |
| listener: | [Object.receive(XMPPStanza)] The listener (usually a Jabber::Protocol::Connection instance) |
# File lib/xmpp4r/streamparser.rb, line 26
26: def initialize(stream, listener)
27: @stream = stream
28: @listener = listener
29: @current = nil
30: end
Begins parsing the XML stream and does not return until the stream closes.
# File lib/xmpp4r/streamparser.rb, line 36
36: def parse
37: @started = false
38: begin
39: parser = REXML::Parsers::SAX2Parser.new @stream
40:
41: parser.listen( :start_element ) do |uri, localname, qname, attributes|
42: e = REXML::Element.new(qname)
43: e.add_attributes attributes
44: @current = @current.nil? ? e : @current.add_element(e)
45:
46: if @current.name == 'stream' and !@started
47: @started = true
48: @listener.receive(@current)
49: @current = nil
50: end
51: end
52:
53: parser.listen( :end_element ) do |uri, localname, qname|
54: if qname == 'stream:stream' and @current.nil?
55: @started = false
56: @listener.parser_end
57: else
58: @listener.receive(@current) unless @current.parent
59: @current = @current.parent
60: end
61: end
62:
63: parser.listen( :characters ) do | text |
64: @current.add(REXML::Text.new(text.to_s, @current.whitespace, nil, true)) if @current
65: end
66:
67: parser.listen( :cdata ) do | text |
68: @current.add(REXML::CData.new(text)) if @current
69: end
70:
71: parser.parse
72: rescue REXML::ParseException => e
73: @listener.parse_failure(e)
74: end
75: end