class Blather::Stream::Parser

@private

Constants

NS_TO_IGNORE

Public Class Methods

debug() click to toggle source
# File lib/blather/stream/parser.rb, line 9
def self.debug; @@debug; end
debug=(debug) click to toggle source
# File lib/blather/stream/parser.rb, line 10
def self.debug=(debug); @@debug = debug; end
new(receiver) click to toggle source
# File lib/blather/stream/parser.rb, line 12
def initialize(receiver)
  @receiver = receiver
  @current = nil
  @namespaces = {}
  @namespace_definitions = []
  @parser = Nokogiri::XML::SAX::PushParser.new self
  @parser.options = Nokogiri::XML::ParseOptions::NOENT
end

Public Instance Methods

<<(string)
Alias for: receive_data
characters(chars = '') click to toggle source
# File lib/blather/stream/parser.rb, line 76
def characters(chars = '')
  Blather.log "CHARS: #{chars}" if @@debug
  @current << Nokogiri::XML::Text.new(chars, @current.document) if @current
end
end_element_namespace(elem, prefix, uri) click to toggle source
# File lib/blather/stream/parser.rb, line 61
def end_element_namespace(elem, prefix, uri)
  Blather.log "END ELEM: #{{:elem => elem, :prefix => prefix, :uri => uri}.inspect}" if @@debug

  if elem == 'stream'
    node = XMPPNode.new('end')
    node.namespace = {prefix => uri}
    deliver node
  elsif @current.parent != @current.document
    @namespace_definitions.pop
    @current = @current.parent
  else
    deliver @current
  end
end
error(msg) click to toggle source
# File lib/blather/stream/parser.rb, line 85
def error(msg)
  raise ParseError.new(msg)
end
finish() click to toggle source
# File lib/blather/stream/parser.rb, line 89
def finish
  @parser.finish
rescue ParseError, RuntimeError
end
receive_data(string) click to toggle source
# File lib/blather/stream/parser.rb, line 21
def receive_data(string)
  Blather.log "PARSING: (#{string})" if @@debug
  @parser << string
  self
rescue Nokogiri::XML::SyntaxError => e
  error e.message
end
Also aliased as: <<
start_element_namespace(elem, attrs, prefix, uri, namespaces) click to toggle source
# File lib/blather/stream/parser.rb, line 30
def start_element_namespace(elem, attrs, prefix, uri, namespaces)
  Blather.log "START ELEM: (#{{:elem => elem, :attrs => attrs, :prefix => prefix, :uri => uri, :ns => namespaces}.inspect})" if @@debug

  args = [elem]
  args << @current.document if @current
  node = XMPPNode.new *args
  node.document.root = node unless @current

  attrs.each do |attr|
    node[attr.localname] = attr.value
  end

  ns_keys = namespaces.map { |pre, href| pre }
  namespaces.delete_if { |pre, href| NS_TO_IGNORE.include? href }
  @namespace_definitions.push []
  namespaces.each do |pre, href|
    next if @namespace_definitions.flatten.include?(@namespaces[[pre, href]])
    ns = node.add_namespace(pre, href)
    @namespaces[[pre, href]] ||= ns
  end
  @namespaces[[prefix, uri]] ||= node.add_namespace(prefix, uri) if prefix && !ns_keys.include?(prefix)
  node.namespace = @namespaces[[prefix, uri]]

  unless @receiver.stopped?
    @current << node if @current
    @current = node
  end

  deliver(node) if elem == 'stream'
end
warning(msg) click to toggle source
# File lib/blather/stream/parser.rb, line 81
def warning(msg)
  Blather.log "PARSE WARNING: #{msg}" if @@debug
end

Private Instance Methods

deliver(node) click to toggle source
# File lib/blather/stream/parser.rb, line 95
def deliver(node)
  @current, @namespaces, @namespace_definitions = nil, {}, []
  @receiver.receive node
end