| Class | Jabber::StreamParser |
| In: |
lib/xmpp4r/streamparser.rb
|
| Parent: | Object |
The REXMLJabberParser uses REXML to parse the incoming XML stream of the Jabber protocol and fires XMLStanzas 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(XMLStanza)] The listener (usually a Jabber::Protocol::Connection instance |
# File lib/xmpp4r/streamparser.rb, line 26
26: def initialize(stream, listener)
27: @stream = stream
28: # this hack fixes REXML version "2.7.3"
29: if REXML::Version=="2.7.3"
30: def @stream.read(len=nil)
31: len = 100 unless len
32: super(len)
33: end
34: end
35: @listener = listener
36: @current = nil
37: end
Begins parsing the XML stream and does not return until the stream closes.
# File lib/xmpp4r/streamparser.rb, line 43
43: def parseparse
44: @started = false
45: begin
46: parser = REXML::Parsers::SAX2Parser.new @stream
47: parser.listen( :start_element ) do |uri, localname, qname, attributes|
48: if @current.nil?
49: @current = REXML::Element::new(qname)
50: else
51: e = REXML::Element::new(qname)
52: @current = @current.add_element(e)
53: end
54: @current.add_attributes attributes
55: if @current.name == 'stream'
56: @listener.receive(@current)
57: @started = true
58: end
59: end
60: parser.listen( :end_element ) do |uri, localname, qname|
61: case qname
62: when "stream:stream"
63: @started = false
64: else
65: @listener.receive(@current) if @current.parent.name == 'stream'
66: @current = @current.parent
67: end
68: end
69: parser.listen( :characters ) do | text |
70: @current.text = (@current.text.nil? ? '' : @current.text) + text
71: end
72: parser.listen( :cdata ) do | text |
73: raise "Not implemented !"
74: end
75: parser.parse
76: rescue REXML::ParseException
77: @listener.parse_failure
78: end
79: end