Class: Blather::Stream::Parser

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/blather/stream/parser.rb

Overview

:nodoc:

Constant Summary collapse

NS_TO_IGNORE =
%w[jabber:client jabber:component:accept]
@@debug =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver) ⇒ Parser

Returns a new instance of Parser.



13
14
15
16
17
18
19
20
# File 'lib/blather/stream/parser.rb', line 13

def initialize(receiver)
  @receiver = receiver
  @current = nil
  @namespaces = {}
  @namespace_definitions = []
  @parser = Nokogiri::XML::SAX::PushParser.new self
  @parser.options = Nokogiri::XML::ParseOptions::DEFAULT_XML
end

Class Method Details

.debugObject



10
# File 'lib/blather/stream/parser.rb', line 10

def self.debug; @@debug; end

.debug=(debug) ⇒ Object



11
# File 'lib/blather/stream/parser.rb', line 11

def self.debug=(debug); @@debug = debug; end

Instance Method Details

#characters(chars = '') ⇒ Object



82
83
84
85
# File 'lib/blather/stream/parser.rb', line 82

def characters(chars = '')
  Blather.logger.debug "CHARS: #{chars}" if @@debug
  @current << Nokogiri::XML::Text.new(chars, @current.document) if @current
end

#end_element_namespace(elem, prefix, uri) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/blather/stream/parser.rb', line 67

def end_element_namespace(elem, prefix, uri)
  Blather.logger.debug "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) ⇒ Object

Raises:



91
92
93
# File 'lib/blather/stream/parser.rb', line 91

def error(msg)
  raise ParseError.new(msg)
end

#receive_data(string) ⇒ Object Also known as: <<



22
23
24
25
26
# File 'lib/blather/stream/parser.rb', line 22

def receive_data(string)
  Blather.logger.debug "PARSING: (#{string})" if @@debug
  @parser << string
  self
end

#start_element_namespace(elem, attrs, prefix, uri, namespaces) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/blather/stream/parser.rb', line 29

def start_element_namespace(elem, attrs, prefix, uri, namespaces)
  Blather.logger.debug "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]]

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

  deliver(node) if elem == 'stream'

  # $stderr.puts "\n\n"
  # $stderr.puts [elem, attrs, prefix, uri, namespaces].inspect
  # $stderr.puts @namespaces.inspect
  # $stderr.puts [@namespaces[[prefix, uri]].prefix, @namespaces[[prefix, uri]].href].inspect if @namespaces[[prefix, uri]]
  # $stderr.puts node.inspect
  # $stderr.puts node.document.to_s.gsub(/\n\s*/,'')
end

#warning(msg) ⇒ Object



87
88
89
# File 'lib/blather/stream/parser.rb', line 87

def warning(msg)
  Blather.logger.debug "PARSE WARNING: #{msg}" if @@debug
end