util/stanza.lua
changeset 2482 a1570e371258
parent 2264 49580a13f71e
child 2526 401ff68413a1
equal deleted inserted replaced
2481:9b407a6acf39 2482:a1570e371258
    63 	return self;
    63 	return self;
    64 end
    64 end
    65 
    65 
    66 function stanza_mt:text(text)
    66 function stanza_mt:text(text)
    67 	(self.last_add[#self.last_add] or self):add_direct_child(text);
    67 	(self.last_add[#self.last_add] or self):add_direct_child(text);
    68 	return self; 
    68 	return self;
    69 end
    69 end
    70 
    70 
    71 function stanza_mt:up()
    71 function stanza_mt:up()
    72 	t_remove(self.last_add);
    72 	t_remove(self.last_add);
    73 	return self;
    73 	return self;
    93 	return self;
    93 	return self;
    94 end
    94 end
    95 
    95 
    96 function stanza_mt:get_child(name, xmlns)
    96 function stanza_mt:get_child(name, xmlns)
    97 	for _, child in ipairs(self.tags) do
    97 	for _, child in ipairs(self.tags) do
    98 		if (not name or child.name == name) 
    98 		if (not name or child.name == name)
    99 			and ((not xmlns and self.attr.xmlns == child.attr.xmlns)
    99 			and ((not xmlns and self.attr.xmlns == child.attr.xmlns)
   100 				or child.attr.xmlns == xmlns) then
   100 				or child.attr.xmlns == xmlns) then
   101 			
   101 			
   102 			return child;
   102 			return child;
   103 		end
   103 		end
   104 	end
   104 	end
   105 end
   105 end
   106 
   106 
   107 function stanza_mt:child_with_name(name)
   107 function stanza_mt:child_with_name(name)
   108 	for _, child in ipairs(self.tags) do	
   108 	for _, child in ipairs(self.tags) do
   109 		if child.name == name then return child; end
   109 		if child.name == name then return child; end
   110 	end
   110 	end
   111 end
   111 end
   112 
   112 
   113 function stanza_mt:child_with_ns(ns)
   113 function stanza_mt:child_with_ns(ns)
   114 	for _, child in ipairs(self.tags) do	
   114 	for _, child in ipairs(self.tags) do
   115 		if child.attr.xmlns == ns then return child; end
   115 		if child.attr.xmlns == ns then return child; end
   116 	end
   116 	end
   117 end
   117 end
   118 
   118 
   119 function stanza_mt:children()
   119 function stanza_mt:children()
   121 	return function (a)
   121 	return function (a)
   122 			i = i + 1
   122 			i = i + 1
   123 			local v = a[i]
   123 			local v = a[i]
   124 			if v then return v; end
   124 			if v then return v; end
   125 		end, self, i;
   125 		end, self, i;
   126 	                                    
       
   127 end
   126 end
   128 function stanza_mt:childtags()
   127 function stanza_mt:childtags()
   129 	local i = 0;
   128 	local i = 0;
   130 	return function (a)
   129 	return function (a)
   131 			i = i + 1
   130 			i = i + 1
   132 			local v = self.tags[i]
   131 			local v = self.tags[i]
   133 			if v then return v; end
   132 			if v then return v; end
   134 		end, self.tags[1], i;
   133 		end, self.tags[1], i;
   135 	                                    
       
   136 end
   134 end
   137 
   135 
   138 local xml_escape
   136 local xml_escape
   139 do
   137 do
   140 	local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
   138 	local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
   315 	--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, ">");
   313 	--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, ">");
   316 	local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
   314 	local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
   317 	function stanza_mt.pretty_print(t)
   315 	function stanza_mt.pretty_print(t)
   318 		local children_text = "";
   316 		local children_text = "";
   319 		for n, child in ipairs(t) do
   317 		for n, child in ipairs(t) do
   320 			if type(child) == "string" then	
   318 			if type(child) == "string" then
   321 				children_text = children_text .. xml_escape(child);
   319 				children_text = children_text .. xml_escape(child);
   322 			else
   320 			else
   323 				children_text = children_text .. child:pretty_print();
   321 				children_text = children_text .. child:pretty_print();
   324 			end
   322 			end
   325 		end
   323 		end