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



1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
# File 'lib/jabber4r/protocol.rb', line 1312

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



1304
1305
1306
# File 'lib/jabber4r/protocol.rb', line 1304

def started
  @started
end

Instance Method Details

#parseObject

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



1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
# File 'lib/jabber4r/protocol.rb', line 1340

def parse
  @started = false
  begin
    parser = REXML::Parsers::SAX2Parser.new @stream 
    parser.listen( :start_element ) do |uri, localname, qname, attributes|
      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|
      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 |
      @current.append_data(text) if @current
    end
    parser.listen( :cdata ) do | text |
      @current.append_data(text) if @current
    end
    parser.parse
  rescue REXML::ParseException
    @listener.parse_failure
  end
end