util/stanza.lua

changeset 1
b8787e859fd2
parent 0
3e3171b59028
child 2
9bb397205f26
--- a/util/stanza.lua	Fri Aug 22 21:09:04 2008 +0000
+++ b/util/stanza.lua	Sun Aug 24 01:51:02 2008 +0000
@@ -1,10 +1,11 @@
-local t_insert  =   table.insert;
-local t_remove  =   table.remove;
-local format    =  string.format;
-local tostring  =       tostring;
-local setmetatable= setmetatable;
-local pairs     =          pairs;
-local ipairs    =         ipairs;
+local t_insert      =  table.insert;
+local t_remove      =  table.remove;
+local format        = string.format;
+local tostring      =      tostring;
+local setmetatable  =  setmetatable;
+local pairs         =         pairs;
+local ipairs        =        ipairs;
+local type          =          type;
 
 module "stanza"
 
@@ -12,7 +13,7 @@
 stanza_mt.__index = stanza_mt;
 
 function stanza(name, attr)
-	local stanza = { name = name, attr = attr or {}, last_add = {}};
+	local stanza = { name = name, attr = attr or {}, tags = {}, last_add = {}};
 	return setmetatable(stanza, stanza_mt);
 end
 
@@ -46,6 +47,9 @@
 end
 
 function stanza_mt:add_child(child)
+	if type(child) == "table" then
+		t_insert(self.tags, child);
+	end
 	t_insert(self, child);
 end
 
@@ -55,6 +59,16 @@
 	end
 end
 
+function stanza_mt:children()
+	local i = 0;
+	return function (a)
+			i = i + 1
+			local v = a[i]
+			if v then return v; end
+		end, self, i;
+	                                    
+end
+
 function stanza_mt.__tostring(t)
 	local children_text = "";
 	for n, child in ipairs(t) do
@@ -63,14 +77,14 @@
 
 	local attr_string = "";
 	if t.attr then
-		for k, v in pairs(t.attr) do attr_string = attr_string .. format(" %s='%s'", k, tostring(v)); end
+		for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. format(" %s='%s'", k, tostring(v)); end end
 	end
 
 	return format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name);
 end
 
 function stanza_mt.__add(s1, s2)
-	return s:add_child(s2);
+	return s1:add_child(s2);
 end
 
 

mercurial