Merge 0.10->trunk
[prosody.git] / util / xml.lua
index 6dbed65d2f5b1c144890b17cec5511386a8643cd..9e12f0dff1b6856cf94a798d2291ead3c223fea9 100644 (file)
@@ -2,7 +2,7 @@
 local st = require "util.stanza";
 local lxp = require "lxp";
 
-module("xml")
+local _ENV = nil;
 
 local parse_xml = (function()
        local ns_prefixes = {
@@ -11,8 +11,20 @@ local parse_xml = (function()
        local ns_separator = "\1";
        local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
        return function(xml)
+               --luacheck: ignore 212/self
                local handler = {};
                local stanza = st.stanza("root");
+               local namespaces = {}
+               function handler:StartNamespaceDecl(prefix, url)
+                       if prefix ~= nil then
+                               namespaces[prefix] = url
+                       end
+               end
+               function handler:EndNamespaceDecl(prefix)
+                       if prefix ~= nil then
+                               namespaces[prefix] = nil
+                       end
+               end
                function handler:StartElement(tagname, attr)
                        local curr_ns,name = tagname:match(ns_pattern);
                        if name == "" then
@@ -33,12 +45,16 @@ local parse_xml = (function()
                                        end
                                end
                        end
-                       stanza:tag(name, attr);
+                       local n = {}
+                       for prefix, url in pairs(namespaces) do
+                               n[prefix] = url
+                       end
+                       stanza:tag(name, attr, n);
                end
                function handler:CharacterData(data)
                        stanza:text(data);
                end
-               function handler:EndElement(tagname)
+               function handler:EndElement()
                        stanza:up();
                end
                local parser = lxp.new(handler, "\1");
@@ -53,5 +69,6 @@ local parse_xml = (function()
        end;
 end)();
 
-parse = parse_xml;
-return _M;
+return {
+       parse = parse_xml;
+};