Software /
code /
prosody
Comparison
tools/ejabberdsql2prosody.lua @ 1628:c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 02 Aug 2009 14:35:03 +0500 |
parent | 1611:e20f90743863 |
child | 1629:f6af348cf497 |
comparison
equal
deleted
inserted
replaced
1623:099364ec1ab2 | 1628:c3ce7cbd123d |
---|---|
153 | 153 |
154 return readFile(filename); | 154 return readFile(filename); |
155 | 155 |
156 ------ | 156 ------ |
157 end | 157 end |
158 | |
159 -- XML parser | |
160 local parse_xml = (function() | |
161 local entity_map = setmetatable({ | |
162 ["amp"] = "&"; | |
163 ["gt"] = ">"; | |
164 ["lt"] = "<"; | |
165 ["apos"] = "'"; | |
166 ["quot"] = "\""; | |
167 }, {__index = function(_, s) | |
168 if s:sub(1,1) == "#" then | |
169 if s:sub(2,2) == "x" then | |
170 return string.char(tonumber(s:sub(3), 16)); | |
171 else | |
172 return string.char(tonumber(s:sub(2))); | |
173 end | |
174 end | |
175 end | |
176 }); | |
177 local function xml_unescape(str) | |
178 return (str:gsub("&(.-);", entity_map)); | |
179 end | |
180 local function parse_tag(s) | |
181 local name,sattr=(s):gmatch("([^%s]+)(.*)")(); | |
182 local attr = {}; | |
183 for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end | |
184 return name, attr; | |
185 end | |
186 return function(xml) | |
187 local stanza = st.stanza("root"); | |
188 local regexp = "<([^>]*)>([^<]*)"; | |
189 for elem, text in xml:gmatch(regexp) do | |
190 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions | |
191 elseif elem:sub(1,1) == "/" then -- end tag | |
192 elem = elem:sub(2); | |
193 stanza:up(); -- TODO check for start-end tag name match | |
194 elseif elem:sub(-1,-1) == "/" then -- empty tag | |
195 elem = elem:sub(1,-2); | |
196 local name,attr = parse_tag(elem); | |
197 stanza:tag(name, attr):up(); | |
198 else -- start tag | |
199 local name,attr = parse_tag(elem); | |
200 stanza:tag(name, attr); | |
201 end | |
202 if #text ~= 0 then -- text | |
203 stanza:text(xml_unescape(text)); | |
204 end | |
205 end | |
206 return stanza.tags[1]; | |
207 end | |
208 end)(); | |
209 -- end of XML parser | |
158 | 210 |
159 local arg, host = ...; | 211 local arg, host = ...; |
160 local help = "/? -? ? /h -h /help -help --help"; | 212 local help = "/? -? ? /h -h /help -help --help"; |
161 if not(arg and host) or help:find(arg, 1, true) then | 213 if not(arg and host) or help:find(arg, 1, true) then |
162 print([[ejabberd SQL DB dump importer for Prosody | 214 print([[ejabberd SQL DB dump importer for Prosody |