Class: Jabber::Protocol::ExpatJabberParser

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

Overview

The ExpatJabberParser uses XMLParser (expat) to parse the incoming XML stream of the Jabber protocol and fires ParsedXMLElements at the Connection instance.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, listener) ⇒ ExpatJabberParser

Constructs a parser for the supplied stream (socket input)

stream
IO

Socket input stream

listener
#receive(ParsedXMLElement)

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



1230
1231
1232
1233
1234
1235
1236
# File 'lib/jabber4r/protocol.rb', line 1230

def initialize(stream, listener)
  @stream = stream
  def @stream.gets
    super(">")
  end
  @listener = listener
end

Instance Attribute Details

#startedObject (readonly)

status if the parser is started



1222
1223
1224
# File 'lib/jabber4r/protocol.rb', line 1222

def started
  @started
end

Class Method Details

.getsObject



1232
1233
1234
# File 'lib/jabber4r/protocol.rb', line 1232

def @stream.gets
  super(">")
end

Instance Method Details

#parseObject

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



1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
# File 'lib/jabber4r/protocol.rb', line 1242

def parse
  @started = false
  
  parser = XMLParser.new("UTF-8")
  def parser.unknownEncoding(e)
    raise "Unknown encoding #{e.to_s}"
  end
  def parser.default
  end
  
  begin
    parser.parse(@stream) do |type, name, data|
      begin
      case type
        when XMLParser::START_ELEM
          case name
            when "stream:stream"
              openstream = ParsedXMLElement.new(name)
              data.each {|key, value| openstream.add_attribute(key, value)}
              @listener.receive(openstream)
              @started = true
            else 
              if @current.nil?
                @current = ParsedXMLElement.new(name.clone)
              else
                @current = @current.add_child(name.clone)
              end
              data.each {|key, value| @current.add_attribute(key.clone, value.clone)}
          end
        when XMLParser::CDATA
          @current.append_data(data.clone) if @current
        when XMLParser::END_ELEM
          case name
            when "stream:stream"
              @started = false
            else
              @listener.receive(@current) unless @current.element_parent
              @current = @current.element_parent
          end
      end
      rescue
        puts  "Error #{$!}"
      end
    end
  rescue XMLParserError
    line = parser.line
    print "XML Parsing error(#{line}): #{$!}\n"
  end
end