Class: Kibosh::XMPP::Client::Connection::StreamParser::Document

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/kibosh/xmpp/client/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, stream, success, failure) ⇒ Document

Returns a new instance of Document.



20
21
22
23
24
25
26
27
28
# File 'lib/kibosh/xmpp/client/connection.rb', line 20

def initialize connection, stream, success, failure
  super()
  @connection = connection
  @stream = stream
  @success = success
  @failure = failure
  @established = false
  @current = {:parent => nil, :parameters => nil, :children => []}
end

Instance Method Details

#cdata_block(string) ⇒ Object



140
141
142
143
144
# File 'lib/kibosh/xmpp/client/connection.rb', line 140

def cdata_block string
  @current[:children].push :parent => @current,
                           :parameters => :cdata,
                           :children => string
end

#characters(string) ⇒ Object



134
135
136
137
138
# File 'lib/kibosh/xmpp/client/connection.rb', line 134

def characters string
  @current[:children].push :parent => @current,
                           :parameters => :characters,
                           :children => string
end

#end_element_namespace(name, prefix = nil, uri = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/kibosh/xmpp/client/connection.rb', line 57

def end_element_namespace name, prefix = nil, uri = nil
  @current = @current[:parent]
  if @current == nil 
    # should be EOS
    raise "hell: #{name} #{prefix} #{uri}" if name != "stream" or uri != "http://etherx.jabber.org/streams"
    @connection.close
  elsif @current[:parent] == nil

    # require 'pp'; pp @current

    body = @stream.body
    document = body.document

    add = lambda do |parent, children|
      children.each do |child|
        params = child[:parameters]
        if params == :characters
          document.create_text_node child[:children] do |node|
            parent.add_child node
          end
          next
        elsif params == :cdata
          parent.add_child(Nokogiri::XML::CDATA.new document, string)
          next
        end
        name, attrs, prefix, uri, ns_array = params
        document.create_element name do |node|
          nses = ns_array.inject({}) do |hash,pair|
            hash[pair[0]] = pair[1]
            hash
          end
          if prefix and prefix != "" and !nses.has_key? prefix
            if document.namespaces.has_key? prefix 
              raise document.namespaces[prefix].inspect
            end
            nses[prefix] = document.root.add_namespace prefix, uri
          end
          # p name, prefix, nses[prefix]
          if prefix
            node.namespace = nses[prefix]
          end
          attrs.each do |attr|
            node[attr.localname] = attr.value
          end
          ns_array.each do |pair|
            node.add_namespace *pair
          end
          add.call node, child[:children]
          parent.add_child node
        end
      end
    end

    add.call @stream.body, @current[:children]

    last = @current[:children].last[:parameters]
    if last[0] == "error"
      # p "last", last
      # p body.to_xml
      name, attrs, prefix, uri, ns_array = last
      # p ns_array
      if name == "error" and uri == "http://etherx.jabber.org/streams"
        # FIX: the exception classes expect responses, not bodies
        body["type"] = "terminate"
        body["condition"] = "remote-stream-error"
        # p body.to_xml
      end
    end

    # pp document

    @current = {:parent => nil, :parameters => nil, :children => []}

    @stream.ready!
  end
end

#error(error) ⇒ Object



146
147
148
# File 'lib/kibosh/xmpp/client/connection.rb', line 146

def error error
  raise "hell"
end

#start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) ⇒ 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
# File 'lib/kibosh/xmpp/client/connection.rb', line 30

def start_element_namespace name, attrs = [], prefix = nil, uri = nil, ns = []
  if !@established && name == "stream" && uri == "http://etherx.jabber.org/streams"
    if name == "stream" && uri == "http://etherx.jabber.org/streams"
      @established = true
      id = nil
      from = nil
      attrs.each do |attr|
        case attr.localname
        when "id"; id = attr.value
        when "from"
          @stream.body["from"] = from = attr.value
        end
      end
      @success.call "id" => id, "from" => from if @success
      @success = @failure = nil
    else
      @failiure.call if @failure
      @success = @failure = nil
    end
  else
    @current[:children].push :parent => @current,
                             :parameters => [ name, attrs, prefix, uri, ns ],
                             :children => []
    @current = @current[:children].last
  end
end