Comparison

tools/ejabberdsql2prosody.lua @ 5215:b3d837c1ccf1

tools/ejabberdsql2prosody: Use util.xml.
author Waqas Hussain <waqas20@gmail.com>
date Mon, 03 Dec 2012 05:34:46 +0500
parent 4496:11983a1e92c0
child 5696:9fba74a28e0c
comparison
equal deleted inserted replaced
5214:1430c6f36621 5215:b3d837c1ccf1
10 prosody = {}; 10 prosody = {};
11 11
12 package.path = package.path ..";../?.lua"; 12 package.path = package.path ..";../?.lua";
13 local serialize = require "util.serialization".serialize; 13 local serialize = require "util.serialization".serialize;
14 local st = require "util.stanza"; 14 local st = require "util.stanza";
15 local parse_xml = require "util.xml".parse;
15 package.loaded["util.logger"] = {init = function() return function() end; end} 16 package.loaded["util.logger"] = {init = function() return function() end; end}
16 local dm = require "util.datamanager" 17 local dm = require "util.datamanager"
17 dm.set_data_path("data"); 18 dm.set_data_path("data");
18 19
19 function parseFile(filename) 20 function parseFile(filename)
164 165
165 return readFile(filename); 166 return readFile(filename);
166 167
167 ------ 168 ------
168 end 169 end
169
170 -- XML parser
171 local parse_xml = (function()
172 local entity_map = setmetatable({
173 ["amp"] = "&";
174 ["gt"] = ">";
175 ["lt"] = "<";
176 ["apos"] = "'";
177 ["quot"] = "\"";
178 }, {__index = function(_, s)
179 if s:sub(1,1) == "#" then
180 if s:sub(2,2) == "x" then
181 return string.char(tonumber(s:sub(3), 16));
182 else
183 return string.char(tonumber(s:sub(2)));
184 end
185 end
186 end
187 });
188 local function xml_unescape(str)
189 return (str:gsub("&(.-);", entity_map));
190 end
191 local function parse_tag(s)
192 local name,sattr=(s):gmatch("([^%s]+)(.*)")();
193 local attr = {};
194 for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end
195 return name, attr;
196 end
197 return function(xml)
198 local stanza = st.stanza("root");
199 local regexp = "<([^>]*)>([^<]*)";
200 for elem, text in xml:gmatch(regexp) do
201 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions
202 elseif elem:sub(1,1) == "/" then -- end tag
203 elem = elem:sub(2);
204 stanza:up(); -- TODO check for start-end tag name match
205 elseif elem:sub(-1,-1) == "/" then -- empty tag
206 elem = elem:sub(1,-2);
207 local name,attr = parse_tag(elem);
208 stanza:tag(name, attr):up();
209 else -- start tag
210 local name,attr = parse_tag(elem);
211 stanza:tag(name, attr);
212 end
213 if #text ~= 0 then -- text
214 stanza:text(xml_unescape(text));
215 end
216 end
217 return stanza.tags[1];
218 end
219 end)();
220 -- end of XML parser
221 170
222 local arg, host = ...; 171 local arg, host = ...;
223 local help = "/? -? ? /h -h /help -help --help"; 172 local help = "/? -? ? /h -h /help -help --help";
224 if not(arg and host) or help:find(arg, 1, true) then 173 if not(arg and host) or help:find(arg, 1, true) then
225 print([[ejabberd SQL DB dump importer for Prosody 174 print([[ejabberd SQL DB dump importer for Prosody