diff options
author | Waqas Hussain <waqas20@gmail.com> | 2009-08-02 14:35:03 +0500 |
---|---|---|
committer | Waqas Hussain <waqas20@gmail.com> | 2009-08-02 14:35:03 +0500 |
commit | 1016cb7d48acda978809300f77b6c3dba335efd8 (patch) | |
tree | 4cede08a21c84c5ce8014a0c4f397ce822f837da /tools | |
parent | 4a38bcf39514e3a0fe8e08a1c4c2fd0136cd4d75 (diff) | |
download | prosody-1016cb7d48acda978809300f77b6c3dba335efd8.tar.gz prosody-1016cb7d48acda978809300f77b6c3dba335efd8.zip |
ejabberdsql2prosody: Added an XML parser
Diffstat (limited to 'tools')
-rw-r--r-- | tools/ejabberdsql2prosody.lua | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tools/ejabberdsql2prosody.lua b/tools/ejabberdsql2prosody.lua index 5b975269..c9f8beb6 100644 --- a/tools/ejabberdsql2prosody.lua +++ b/tools/ejabberdsql2prosody.lua @@ -156,6 +156,58 @@ return readFile(filename); ------ end +-- XML parser +local parse_xml = (function() + local entity_map = setmetatable({ + ["amp"] = "&"; + ["gt"] = ">"; + ["lt"] = "<"; + ["apos"] = "'"; + ["quot"] = "\""; + }, {__index = function(_, s) + if s:sub(1,1) == "#" then + if s:sub(2,2) == "x" then + return string.char(tonumber(s:sub(3), 16)); + else + return string.char(tonumber(s:sub(2))); + end + end + end + }); + local function xml_unescape(str) + return (str:gsub("&(.-);", entity_map)); + end + local function parse_tag(s) + local name,sattr=(s):gmatch("([^%s]+)(.*)")(); + local attr = {}; + for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end + return name, attr; + end + return function(xml) + local stanza = st.stanza("root"); + local regexp = "<([^>]*)>([^<]*)"; + for elem, text in xml:gmatch(regexp) do + if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions + elseif elem:sub(1,1) == "/" then -- end tag + elem = elem:sub(2); + stanza:up(); -- TODO check for start-end tag name match + elseif elem:sub(-1,-1) == "/" then -- empty tag + elem = elem:sub(1,-2); + local name,attr = parse_tag(elem); + stanza:tag(name, attr):up(); + else -- start tag + local name,attr = parse_tag(elem); + stanza:tag(name, attr); + end + if #text ~= 0 then -- text + stanza:text(xml_unescape(text)); + end + end + return stanza.tags[1]; + end +end)(); +-- end of XML parser + local arg, host = ...; local help = "/? -? ? /h -h /help -help --help"; if not(arg and host) or help:find(arg, 1, true) then |