Class: Jabber::Protocol::REXMLJabberParser

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber4r/protocol.rb

Overview

The REXMLJabberParser uses REXML to parse the incoming XML stream of the Jabber protocol and fires ParsedXMLElements at the Connection instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, listener) ⇒ REXMLJabberParser

Constructs a parser for the supplied stream (socket input)

stream
IO

Socket input stream

listener
Object.receive(ParsedXMLElement)

The listener (usually a Jabber::Protocol::Connection instance



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/jabber4r/protocol.rb', line 169

def initialize(stream, listener)
  @stream = stream

  # this hack fixes REXML version "2.7.3" and "2.7.4"
  if REXML::Version=="2.7.3" || REXML::Version=="2.7.4"
    def @stream.read(len=nil)
      len = 100 unless len
      super(len)
    end
    def @stream.gets(char=nil)
      super(">")
    end
    def @stream.readline(char=nil)
      super(">")
    end
    def @stream.readlines(char=nil)
      super(">")
    end
  end

  @listener = listener
  @current = nil
end

Instance Attribute Details

#startedObject (readonly)

status if the parser is started



161
162
163
# File 'lib/jabber4r/protocol.rb', line 161

def started
  @started
end

Instance Method Details

#parseObject

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/jabber4r/protocol.rb', line 197

def parse
  #puts "PARSE"
  @started = false
  begin
    parser = REXML::Parsers::SAX2Parser.new @stream

    parser.listen(:end_document) do
      raise Jabber::ConnectionForceCloseError
    end

    parser.listen( :start_element ) do |uri, localname, qname, attributes|
      puts "START ELEMENT"
      case qname
      when "stream:stream"
        openstream = ParsedXMLElement.new(qname)
        attributes.each { |attr, value| openstream.add_attribute(attr, value) }
        @listener.receive(openstream)
              @started = true
      else
        if @current.nil?
          @current = ParsedXMLElement.new(qname)
        else
          @current = @current.add_child(qname)
        end
        attributes.each { |attr, value| @current.add_attribute(attr, value) }
      end
    end
    parser.listen( :end_element ) do  |uri, localname, qname|
      puts "END ELEMENT"
      case qname
      when "stream:stream"
        @started = false
      else
        @listener.receive(@current) unless @current.element_parent
        @current = @current.element_parent
      end
    end
    parser.listen( :characters ) do | text |
      puts "CHARACTERS"
      @current.append_data(text) if @current
    end
    parser.listen( :cdata ) do | text |
      puts "CDATA"
      @current.append_data(text) if @current
    end
    parser.parse
  rescue REXML::ParseException => e
    puts "FAIL"

    puts e.backtrace.join "\n"
    puts e.message
    @listener.parse_failure
  rescue Jabber::ConnectionForceCloseError => e
    @listener.parse_failure(e)
  end
end