Comparison

util/xmppstream.lua @ 3032:38459cffaf67

util.xmppstream: Stream objects now just have feed/reset methods
author Matthew Wild <mwild1@gmail.com>
date Sun, 09 May 2010 20:45:53 +0100
parent 2926:fc96d439453b
child 3424:9e0df614e5d0
comparison
equal deleted inserted replaced
3031:421890f3f247 3032:38459cffaf67
30 30
31 local ns_separator = "\1"; 31 local ns_separator = "\1";
32 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$"; 32 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
33 33
34 function new_sax_handlers(session, stream_callbacks) 34 function new_sax_handlers(session, stream_callbacks)
35 local chardata = {};
36 local xml_handlers = {}; 35 local xml_handlers = {};
36
37 local log = session.log or default_log; 37 local log = session.log or default_log;
38 38
39 local cb_streamopened = stream_callbacks.streamopened; 39 local cb_streamopened = stream_callbacks.streamopened;
40 local cb_streamclosed = stream_callbacks.streamclosed; 40 local cb_streamclosed = stream_callbacks.streamclosed;
41 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end; 41 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
45 local stream_tag = stream_ns..ns_separator..(stream_callbacks.stream_tag or "stream"); 45 local stream_tag = stream_ns..ns_separator..(stream_callbacks.stream_tag or "stream");
46 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error"); 46 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
47 47
48 local stream_default_ns = stream_callbacks.default_ns; 48 local stream_default_ns = stream_callbacks.default_ns;
49 49
50 local stanza; 50 local chardata, stanza = {};
51 function xml_handlers:StartElement(tagname, attr) 51 function xml_handlers:StartElement(tagname, attr)
52 if stanza and #chardata > 0 then 52 if stanza and #chardata > 0 then
53 -- We have some character data in the buffer 53 -- We have some character data in the buffer
54 stanza:text(t_concat(chardata)); 54 stanza:text(t_concat(chardata));
55 chardata = {}; 55 chardata = {};
138 cb_error(session, "parse-error", "unexpected-element-close", name); 138 cb_error(session, "parse-error", "unexpected-element-close", name);
139 end 139 end
140 stanza, chardata = nil, {}; 140 stanza, chardata = nil, {};
141 end 141 end
142 end 142 end
143 return xml_handlers; 143
144 local function reset()
145 stanza, chardata = nil, {};
146 end
147
148 return xml_handlers, { reset = reset };
144 end 149 end
145 150
146 function new(session, stream_callbacks) 151 function new(session, stream_callbacks)
147 return new_parser(new_sax_handlers(session, stream_callbacks), ns_separator); 152 local handlers, meta = new_sax_handlers(session, stream_callbacks);
153 local parser = new_parser(handlers, ns_separator);
154 local parse = parser.parse;
155
156 return {
157 reset = function ()
158 parser = new_parser(handlers, ns_separator);
159 parse = parser.parse;
160 meta.reset();
161 end,
162 feed = function (self, data)
163 return parse(parser, data);
164 end
165 };
148 end 166 end
149 167
150 return _M; 168 return _M;