Class: Blather::Stream::Parser

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

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.



12
13
14
15
16
17
18
19
# 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::DEFAULT_XML | Nokogiri::XML::ParseOptions::NOENT
end

Class Method Details

.debugObject



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

def self.debug; @@debug; end

.debug=(debug) ⇒ Object



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

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

Instance Method Details

#characters(chars = '') ⇒ Object



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

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) ⇒ Object



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

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) ⇒ Object

Raises:



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

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

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



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

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

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



28
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
# File 'lib/blather/stream/parser.rb', line 28

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'

  # $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



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

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