util/stanza.lua
changeset 2077 e33658f6052c
parent 1984 f2b1f89e1d7c
child 2264 49580a13f71e
equal deleted inserted replaced
2076:de2ae849b0b3 2077:e33658f6052c
   134 local function _dostring(t, buf, self, xml_escape, parentns)
   134 local function _dostring(t, buf, self, xml_escape, parentns)
   135 	local nsid = 0;
   135 	local nsid = 0;
   136 	local name = t.name
   136 	local name = t.name
   137 	t_insert(buf, "<"..name);
   137 	t_insert(buf, "<"..name);
   138 	for k, v in pairs(t.attr) do
   138 	for k, v in pairs(t.attr) do
   139 		if s_find(k, "|", 1, true) then
   139 		if s_find(k, "\1", 1, true) then
   140 			local ns, attrk = s_match(k, "^([^|]+)|(.+)$");
   140 			local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
   141 			nsid = nsid + 1;
   141 			nsid = nsid + 1;
   142 			t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
   142 			t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
   143                elseif not(k == "xmlns" and v == parentns) then
   143 		elseif not(k == "xmlns" and v == parentns) then
   144 			t_insert(buf, " "..k.."='"..xml_escape(v).."'");
   144 			t_insert(buf, " "..k.."='"..xml_escape(v).."'");
   145 		end
   145 		end
   146 	end
   146 	end
   147 	local len = #t;
   147 	local len = #t;
   148 	if len == 0 then
   148 	if len == 0 then
   150 	else
   150 	else
   151 		t_insert(buf, ">");
   151 		t_insert(buf, ">");
   152 		for n=1,len do
   152 		for n=1,len do
   153 			local child = t[n];
   153 			local child = t[n];
   154 			if child.name then
   154 			if child.name then
   155                                self(child, buf, self, xml_escape, t.attr.xmlns);
   155 				self(child, buf, self, xml_escape, t.attr.xmlns);
   156 			else
   156 			else
   157 				t_insert(buf, xml_escape(child));
   157 				t_insert(buf, xml_escape(child));
   158 			end
   158 			end
   159 		end
   159 		end
   160 		t_insert(buf, "</"..name..">");
   160 		t_insert(buf, "</"..name..">");
   161 	end
   161 	end
   162 end
   162 end
   163 function stanza_mt.__tostring(t)
   163 function stanza_mt.__tostring(t)
   164 	local buf = {};
   164 	local buf = {};
   165        _dostring(t, buf, _dostring, xml_escape, nil);
   165 	_dostring(t, buf, _dostring, xml_escape, nil);
   166 	return t_concat(buf);
   166 	return t_concat(buf);
   167 end
   167 end
   168 
   168 
   169 function stanza_mt.top_tag(t)
   169 function stanza_mt.top_tag(t)
   170 	local attr_string = "";
   170 	local attr_string = "";
   208 function deserialize(stanza)
   208 function deserialize(stanza)
   209 	-- Set metatable
   209 	-- Set metatable
   210 	if stanza then
   210 	if stanza then
   211 		local attr = stanza.attr;
   211 		local attr = stanza.attr;
   212 		for i=1,#attr do attr[i] = nil; end
   212 		for i=1,#attr do attr[i] = nil; end
       
   213 		local attrx = {};
       
   214 		for att in pairs(attr) do
       
   215 			if s_find(att, "|", 1, true) and not s_find(k, "\1", 1, true) then
       
   216 				local ns,na = s_match(k, "^([^|]+)|(.+)$");
       
   217 				attrx[ns.."\1"..na] = attr[att];
       
   218 				attr[att] = nil;
       
   219 			end
       
   220 		end
       
   221 		for a,v in pairs(attrx) do
       
   222 			attr[x] = v;
       
   223 		end
   213 		setmetatable(stanza, stanza_mt);
   224 		setmetatable(stanza, stanza_mt);
   214 		for _, child in ipairs(stanza) do
   225 		for _, child in ipairs(stanza) do
   215 			if type(child) == "table" then
   226 			if type(child) == "table" then
   216 				deserialize(child);
   227 				deserialize(child);
   217 			end
   228 			end