Class Babylon::XmppParser
In: lib/babylon/xmpp_parser.rb
Parent: Nokogiri::XML::SAX::Document

This is the XML SAX Parser that accepts "pushed" content

Methods

Attributes

doc  [RW] 
elem  [RW] 
parser  [RW] 

Public Class methods

Initialize the parser and adds the callback that will be called upon stanza completion

[Source]

    # File lib/babylon/xmpp_parser.rb, line 11
11:     def initialize(callback)
12:       @callback = callback
13:       @buffer = ""
14:       super()
15:       reset
16:     end

Public Instance methods

Adds characters to the current element (being parsed)

[Source]

    # File lib/babylon/xmpp_parser.rb, line 33
33:     def characters(string)
34:       @buffer ||= ""
35:       @buffer += string 
36:     end

Clears the characters buffer

[Source]

    # File lib/babylon/xmpp_parser.rb, line 60
60:     def clear_characters_buffer
61:       if @buffer && @elem
62:         @buffer.strip!
63:         @elem.add_child(Nokogiri::XML::Text.new(Babylon.decode_xml(@buffer), @doc)) unless @buffer.empty?
64:         @buffer = nil # empty the buffer
65:       end
66:     end

Terminates the current element and calls the callback

[Source]

    # File lib/babylon/xmpp_parser.rb, line 70
70:     def end_element(name)
71:       clear_characters_buffer
72:       if @elem
73:         if @elem.parent == @doc
74:           # If we're actually finishing the stanza (a stanza is always a document's root)
75:           @callback.call(@elem) 
76:           # We delete the current element and the doc (1 doc per stanza policy)
77:           @elem = @doc = nil 
78:         else
79:           @elem = @elem.parent 
80:         end 
81:       else 
82:         # Not sure what to do since it seems we're not processing any element at this time, so how can one end?
83:       end 
84:     end

Pushes the received data to the parser. The parser will then callback the document‘s methods (start_tag, end_tag… etc)

[Source]

    # File lib/babylon/xmpp_parser.rb, line 27
27:     def push(data)
28:       @parser << data
29:     end

Resets the Pushed SAX Parser.

[Source]

    # File lib/babylon/xmpp_parser.rb, line 20
20:     def reset
21:       @parser = Nokogiri::XML::SAX::PushParser.new(self, "UTF-8")
22:       @elem = @doc = nil
23:     end

Instantiate a new current Element, adds the corresponding attributes and namespaces. The new element is eventually added to a parent element (if present). If no element is being parsed, then, we create a new document, to which we add this new element as root. (we create one document per stanza to avoid memory problems)

[Source]

    # File lib/babylon/xmpp_parser.rb, line 42
42:     def start_element(qname, attributes = [])
43:       clear_characters_buffer
44:       @doc ||= Nokogiri::XML::Document.new
45:       @elem ||= @doc # If we have no current element, then, we take the doc
46:       @elem = @elem.add_child(Nokogiri::XML::Element.new(qname, @doc))
47:       
48:       add_namespaces_and_attributes_to_current_node(attributes)
49:       
50:       if @elem.name == "stream:stream"
51:         # We activate the callback since this element  will never end.
52:         @callback.call(@elem)
53:         @doc = @elem = nil # Let's prepare for the next stanza
54:         # And then, we start a new Sax Push Parser
55:       end
56:     end

[Validate]