util/stanza.lua

changeset 3999
257d13734d32
parent 3766
0d7137fee360
child 4000
279a3f9f540c
equal deleted inserted replaced
3998:13a5a8df7c34 3999:257d13734d32
178 end 178 end
179 179
180 local xml_escape 180 local xml_escape
181 do 181 do
182 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" }; 182 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
183 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end 183 function xml_escape(str) return (s_gsub(tostring(str), "['&<>\"]", escape_table)); end
184 _M.xml_escape = xml_escape; 184 _M.xml_escape = xml_escape;
185 end 185 end
186 186
187 local function _dostring(t, buf, self, xml_escape, parentns) 187 local function _dostring(t, buf, self, xml_escape, parentns)
188 local nsid = 0; 188 local nsid = 0;
227 return s_format("<%s%s>", t.name, attr_string); 227 return s_format("<%s%s>", t.name, attr_string);
228 end 228 end
229 229
230 function stanza_mt.get_text(t) 230 function stanza_mt.get_text(t)
231 if #t.tags == 0 then 231 if #t.tags == 0 then
232 return t_concat(t); 232 local s = {};
233 for i, v in ipairs(t) do
234 s[i] = tostring(v);
235 end
236 return t_concat(s);
233 end 237 end
234 end 238 end
235 239
236 function stanza_mt.get_error(stanza) 240 function stanza_mt.get_error(stanza)
237 local type, condition, text; 241 local type, condition, text;
271 end 275 end
272 276
273 function preserialize(stanza) 277 function preserialize(stanza)
274 local s = { name = stanza.name, attr = stanza.attr }; 278 local s = { name = stanza.name, attr = stanza.attr };
275 for _, child in ipairs(stanza) do 279 for _, child in ipairs(stanza) do
276 if type(child) == "table" then 280 if child.tags then
277 t_insert(s, preserialize(child)); 281 t_insert(s, preserialize(child));
278 else 282 else
279 t_insert(s, child); 283 t_insert(s, tostring(child));
280 end 284 end
281 end 285 end
282 return s; 286 return s;
283 end 287 end
284 288
298 for a,v in pairs(attrx) do 302 for a,v in pairs(attrx) do
299 attr[a] = v; 303 attr[a] = v;
300 end 304 end
301 setmetatable(stanza, stanza_mt); 305 setmetatable(stanza, stanza_mt);
302 for _, child in ipairs(stanza) do 306 for _, child in ipairs(stanza) do
303 if type(child) == "table" then 307 if child.tags then
304 deserialize(child); 308 deserialize(child);
305 end 309 end
306 end 310 end
307 if not stanza.tags then 311 if not stanza.tags then
308 -- Rebuild tags 312 -- Rebuild tags

mercurial