Class: Blather::Stream::Parser
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- Blather::Stream::Parser
- 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
- #characters(chars = '') ⇒ Object
- #end_element_namespace(elem, prefix, uri) ⇒ Object
- #error(msg) ⇒ Object
- #finish ⇒ Object
-
#initialize(receiver) ⇒ Parser
constructor
A new instance of Parser.
- #receive_data(string) ⇒ Object (also: #<<)
- #start_element_namespace(elem, attrs, prefix, uri, namespaces) ⇒ Object
- #warning(msg) ⇒ Object
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. = Nokogiri::XML::ParseOptions::NOENT end |
Class Method Details
.debug ⇒ Object
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
76 77 78 79 |
# 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) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# 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) ⇒ Object
85 86 87 |
# File 'lib/blather/stream/parser.rb', line 85 def error(msg) raise ParseError.new(msg) end |
#finish ⇒ Object
89 90 91 92 |
# File 'lib/blather/stream/parser.rb', line 89 def finish @parser.finish rescue ParseError, RuntimeError end |
#receive_data(string) ⇒ Object Also known as: <<
21 22 23 24 25 26 27 |
# 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. end |
#start_element_namespace(elem, attrs, prefix, uri, namespaces) ⇒ Object
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 |
# 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 |