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



87
88
89
90
91
92
93
# File 'lib/jabber4r/protocol.rb', line 87

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



79
80
81
# File 'lib/jabber4r/protocol.rb', line 79

def started
  @started
end

Class Method Details

.getsObject



89
90
91
# File 'lib/jabber4r/protocol.rb', line 89

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

Instance Method Details

#parseObject

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jabber4r/protocol.rb', line 99

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