util.xmppstream: Check to make sure parser.stop is present before calling it.
authorWaqas Hussain <waqas20@gmail.com>
Sat, 4 Jun 2011 20:57:43 +0000 (01:57 +0500)
committerWaqas Hussain <waqas20@gmail.com>
Sat, 4 Jun 2011 20:57:43 +0000 (01:57 +0500)
util/xmppstream.lua

index 7042042424ab984271979404a0772c707237d06c..ac4d77afc77aea269dd34f9a1893a02d6e9b6a4d 100644 (file)
@@ -9,13 +9,10 @@
 
 local lxp = require "lxp";
 local st = require "util.stanza";
-local stanza_mt = st.stanza_mt;
 
 local tostring = tostring;
 local t_insert = table.insert;
 local t_concat = table.concat;
-local t_remove = table.remove;
-local setmetatable = setmetatable;
 
 local default_log = require "util.logger".init("xmppstream");
 
@@ -66,13 +63,12 @@ function new_sax_handlers(session, stream_callbacks)
        
        local stream_default_ns = stream_callbacks.default_ns;
        
-       local stack = {};
        local chardata, stanza = {};
        local non_streamns_depth = 0;
        function xml_handlers:StartElement(tagname, attr)
                if stanza and #chardata > 0 then
                        -- We have some character data in the buffer
-                       t_insert(stanza, t_concat(chardata));
+                       stanza:text(t_concat(chardata));
                        chardata = {};
                end
                local curr_ns,name = tagname:match(ns_pattern);
@@ -116,13 +112,9 @@ function new_sax_handlers(session, stream_callbacks)
                                cb_error(session, "invalid-top-level-element");
                        end
                        
-                       stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
+                       stanza = st.stanza(name, attr);
                else -- we are inside a stanza, so add a tag
-                       t_insert(stack, stanza);
-                       local oldstanza = stanza;
-                       stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
-                       t_insert(oldstanza, stanza);
-                       t_insert(oldstanza.tags, stanza);
+                       stanza:tag(name, attr);
                end
        end
        function xml_handlers:CharacterData(data)
@@ -137,11 +129,12 @@ function new_sax_handlers(session, stream_callbacks)
                if stanza then
                        if #chardata > 0 then
                                -- We have some character data in the buffer
-                               t_insert(stanza, t_concat(chardata));
+                               stanza:text(t_concat(chardata));
                                chardata = {};
                        end
                        -- Complete stanza
-                       if #stack == 0 then
+                       local last_add = stanza.last_add;
+                       if not last_add or #last_add == 0 then
                                if tagname ~= stream_error_tag then
                                        cb_handlestanza(session, stanza);
                                else
@@ -149,7 +142,7 @@ function new_sax_handlers(session, stream_callbacks)
                                end
                                stanza = nil;
                        else
-                               stanza = t_remove(stack);
+                               stanza:up();
                        end
                else
                        if tagname == stream_tag then
@@ -164,13 +157,12 @@ function new_sax_handlers(session, stream_callbacks)
                                cb_error(session, "parse-error", "unexpected-element-close", name);
                        end
                        stanza, chardata = nil, {};
-                       stack = {};
                end
        end
 
        local function restricted_handler(parser)
                cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1.");
-               if not parser:stop() then
+               if not parser.stop or not parser:stop() then
                        error("Failed to abort parsing");
                end
        end
@@ -183,7 +175,6 @@ function new_sax_handlers(session, stream_callbacks)
        
        local function reset()
                stanza, chardata = nil, {};
-               stack = {};
        end
        
        local function set_session(stream, new_session)