Comparison

util/stanza.lua @ 1431:9fe9ba693f4a

util.stanza: Serializer optimizations, and nicer output for empty elements
author Waqas Hussain <waqas20@gmail.com>
date Sat, 27 Jun 2009 08:38:52 +0500
parent 1420:1576a5aa52f8
child 1517:22be7637a64d
comparison
equal deleted inserted replaced
1430:39169cf8d36f 1431:9fe9ba693f4a
22 local next = next; 22 local next = next;
23 local print = print; 23 local print = print;
24 local unpack = unpack; 24 local unpack = unpack;
25 local s_gsub = string.gsub; 25 local s_gsub = string.gsub;
26 local s_char = string.char; 26 local s_char = string.char;
27 local s_find = string.find;
27 local os = os; 28 local os = os;
28 29
29 local do_pretty_printing = not os.getenv("WINDIR"); 30 local do_pretty_printing = not os.getenv("WINDIR");
30 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; 31 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
31 32
120 local xml_escape = (function() 121 local xml_escape = (function()
121 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" }; 122 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
122 return function(str) return (s_gsub(str, "['&<>\"]", escape_table)); end 123 return function(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
123 end)(); 124 end)();
124 local function _dostring(t, buf, self, xml_escape) 125 local function _dostring(t, buf, self, xml_escape)
125 local nsid, ns, attrk = 0; 126 local nsid = 0;
126 t_insert(buf, "<"..t.name); 127 local name = t.name
128 t_insert(buf, "<"..name);
127 for k, v in pairs(t.attr) do 129 for k, v in pairs(t.attr) do
128 ns, attrk = s_match(k, "^([^|]+)|(.+)$"); 130 if s_find(k, "|", 1, true) then
129 if ns then 131 local ns, attrk = s_match(k, "^([^|]+)|(.+)$");
130 nsid = nsid + 1; 132 nsid = nsid + 1;
131 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'"); 133 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
132 else 134 else
133 t_insert(buf, " "..k.."='"..xml_escape(v).."'"); 135 t_insert(buf, " "..k.."='"..xml_escape(v).."'");
134 end 136 end
135 end 137 end
136 t_insert(buf, ">"); 138 local len = #t;
137 for n=1,#t do 139 if len == 0 then
138 local child = t[n]; 140 t_insert(buf, "/>");
139 if child.name then 141 else
140 self(child, buf, self, xml_escape); 142 t_insert(buf, ">");
141 else 143 for n=1,len do
142 t_insert(buf, xml_escape(child)); 144 local child = t[n];
143 end 145 if child.name then
144 end 146 self(child, buf, self, xml_escape);
145 t_insert(buf, "</"..t.name..">"); 147 else
148 t_insert(buf, xml_escape(child));
149 end
150 end
151 t_insert(buf, "</"..name..">");
152 end
146 end 153 end
147 function stanza_mt.__tostring(t) 154 function stanza_mt.__tostring(t)
148 local buf = {}; 155 local buf = {};
149 _dostring(t, buf, _dostring, xml_escape); 156 _dostring(t, buf, _dostring, xml_escape);
150 return t_concat(buf); 157 return t_concat(buf);