Comparison

util/stanza.lua @ 5776:bd0ff8ae98a8

Remove all trailing whitespace
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 09 Aug 2013 17:48:21 +0200
parent 5435:f56e449a63e3
child 6468:3728c30da4e3
comparison
equal deleted inserted replaced
5775:a6c2b8933507 5776:bd0ff8ae98a8
1 -- Prosody IM 1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild 2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain 3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 9
97 function stanza_mt:get_child(name, xmlns) 97 function stanza_mt:get_child(name, xmlns)
98 for _, child in ipairs(self.tags) do 98 for _, child in ipairs(self.tags) do
99 if (not name or child.name == name) 99 if (not name or child.name == name)
100 and ((not xmlns and self.attr.xmlns == child.attr.xmlns) 100 and ((not xmlns and self.attr.xmlns == child.attr.xmlns)
101 or child.attr.xmlns == xmlns) then 101 or child.attr.xmlns == xmlns) then
102 102
103 return child; 103 return child;
104 end 104 end
105 end 105 end
106 end 106 end
107 107
150 end 150 end
151 151
152 function stanza_mt:maptags(callback) 152 function stanza_mt:maptags(callback)
153 local tags, curr_tag = self.tags, 1; 153 local tags, curr_tag = self.tags, 1;
154 local n_children, n_tags = #self, #tags; 154 local n_children, n_tags = #self, #tags;
155 155
156 local i = 1; 156 local i = 1;
157 while curr_tag <= n_tags and n_tags > 0 do 157 while curr_tag <= n_tags and n_tags > 0 do
158 if self[i] == tags[curr_tag] then 158 if self[i] == tags[curr_tag] then
159 local ret = callback(self[i]); 159 local ret = callback(self[i]);
160 if ret == nil then 160 if ret == nil then
256 end 256 end
257 end 257 end
258 258
259 function stanza_mt.get_error(stanza) 259 function stanza_mt.get_error(stanza)
260 local type, condition, text; 260 local type, condition, text;
261 261
262 local error_tag = stanza:get_child("error"); 262 local error_tag = stanza:get_child("error");
263 if not error_tag then 263 if not error_tag then
264 return nil, nil, nil; 264 return nil, nil, nil;
265 end 265 end
266 type = error_tag.attr.type; 266 type = error_tag.attr.type;
267 267
268 for _, child in ipairs(error_tag.tags) do 268 for _, child in ipairs(error_tag.tags) do
269 if child.attr.xmlns == xmlns_stanzas then 269 if child.attr.xmlns == xmlns_stanzas then
270 if not text and child.name == "text" then 270 if not text and child.name == "text" then
271 text = child:get_text(); 271 text = child:get_text();
272 elseif not condition then 272 elseif not condition then
331 end 331 end
332 end 332 end
333 stanza.tags = tags; 333 stanza.tags = tags;
334 end 334 end
335 end 335 end
336 336
337 return stanza; 337 return stanza;
338 end 338 end
339 339
340 local function _clone(stanza) 340 local function _clone(stanza)
341 local attr, tags = {}, {}; 341 local attr, tags = {}, {};
388 if do_pretty_printing then 388 if do_pretty_printing then
389 local style_attrk = getstyle("yellow"); 389 local style_attrk = getstyle("yellow");
390 local style_attrv = getstyle("red"); 390 local style_attrv = getstyle("red");
391 local style_tagname = getstyle("red"); 391 local style_tagname = getstyle("red");
392 local style_punc = getstyle("magenta"); 392 local style_punc = getstyle("magenta");
393 393
394 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); 394 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
395 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); 395 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
396 --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); 396 --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
397 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); 397 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
398 function stanza_mt.pretty_print(t) 398 function stanza_mt.pretty_print(t)
409 if t.attr then 409 if t.attr then
410 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end 410 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
411 end 411 end
412 return s_format(tag_format, t.name, attr_string, children_text, t.name); 412 return s_format(tag_format, t.name, attr_string, children_text, t.name);
413 end 413 end
414 414
415 function stanza_mt.pretty_top_tag(t) 415 function stanza_mt.pretty_top_tag(t)
416 local attr_string = ""; 416 local attr_string = "";
417 if t.attr then 417 if t.attr then
418 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end 418 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
419 end 419 end