xmlstream.lua

Mon, 31 Jan 2011 03:27:53 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 31 Jan 2011 03:27:53 +0100
changeset 6
4afd0e6206b2
parent 4
e471f0014523
permissions
-rw-r--r--

Add special handling of author and link elements, and a fluffy bunny

0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local lxp = require "lxp";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local st = require "stanza";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local tostring = tostring;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local t_insert = table.insert;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local t_concat = table.concat;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local error = error;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
4
e471f0014523 Rename xmppstream to xmlstream, and make some changes to make it compatible with Prosody
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
19 module "xmlstream"
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local new_parser = lxp.new;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local ns_prefixes = {
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 ["http://www.w3.org/XML/1998/namespace"] = "xml";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 };
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local xmlns_streams = "http://etherx.jabber.org/streams";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local ns_separator = "\1";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 function new_sax_handlers(session, stream_callbacks)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local xml_handlers = {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local log = session.log;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local cb_streamopened = stream_callbacks.streamopened;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local cb_streamclosed = stream_callbacks.streamclosed;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local cb_handlestanza = stream_callbacks.handlestanza;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local stream_tag = stream_ns..ns_separator..(stream_callbacks.stream_tag or "stream");
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local stream_default_ns = stream_callbacks.default_ns;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local chardata, stanza = {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function xml_handlers:StartElement(tagname, attr)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 if stanza and #chardata > 0 then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 -- We have some character data in the buffer
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 stanza:text(t_concat(chardata));
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 chardata = {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local curr_ns,name = tagname:match(ns_pattern);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if name == "" then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 curr_ns, name = "", curr_ns;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if curr_ns ~= stream_default_ns then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 attr.xmlns = curr_ns;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 -- FIXME !!!!!
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 for i=1,#attr do
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local k = attr[i];
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 attr[i] = nil;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local ns, nm = k:match(ns_pattern);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 if nm ~= "" then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 ns = ns_prefixes[ns];
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if ns then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 attr[ns..":"..nm] = attr[k];
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 attr[k] = nil;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if not stanza then --if we are not currently inside a stanza
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 if session.notopen then
3
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
80 if cb_streamopened then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
81 cb_streamopened(session, attr, name);
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 return;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 stanza = st.stanza(name, attr);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 else -- we are inside a stanza, so add a tag
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 attr.xmlns = nil;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if curr_ns ~= stream_default_ns then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 attr.xmlns = curr_ns;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 stanza:tag(name, attr);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 function xml_handlers:CharacterData(data)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 if stanza then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 t_insert(chardata, data);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 function xml_handlers:EndElement(tagname)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 if stanza then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 if #chardata > 0 then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 -- We have some character data in the buffer
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 stanza:text(t_concat(chardata));
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 chardata = {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 -- Complete stanza
4
e471f0014523 Rename xmppstream to xmlstream, and make some changes to make it compatible with Prosody
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
107 local last_add = stanza.last_add;
e471f0014523 Rename xmppstream to xmlstream, and make some changes to make it compatible with Prosody
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
108 if not(last_add) or #last_add == 0 then
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 if tagname ~= stream_error_tag then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 cb_handlestanza(session, stanza);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 cb_error(session, "stream-error", stanza);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 stanza = nil;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 stanza:up();
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 else
3
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
119 if cb_streamclosed then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
120 cb_streamclosed(session);
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 stanza, chardata = nil, {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 local function reset()
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 stanza, chardata = nil, {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 return xml_handlers, { reset = reset };
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 function new(session, stream_callbacks)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 local handlers, meta = new_sax_handlers(session, stream_callbacks);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 local parser = new_parser(handlers, ns_separator);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local parse = parser.parse;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 return {
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 reset = function ()
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 parser = new_parser(handlers, ns_separator);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 parse = parser.parse;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 meta.reset();
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end,
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 feed = function (self, data)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 return parse(parser, data);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 };
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 return _M;

mercurial