Mercurial > prosody-hg
comparison util/xml.lua @ 6978:30c96a5db360
util.stanza, util.xml, util.xmppstream: Add support for tracking defined namespaces and their prefix (stanza.namespaces), knowing/preserving prefix names is required for some applications (thanks daurnimator)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 08 Dec 2015 23:15:42 +0000 |
| parents | 5de6b93d0190 |
| children | c9af793b2d8f |
comparison
equal
deleted
inserted
replaced
| 6977:450db0b83fe9 | 6978:30c96a5db360 |
|---|---|
| 12 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$"; | 12 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$"; |
| 13 return function(xml) | 13 return function(xml) |
| 14 --luacheck: ignore 212/self | 14 --luacheck: ignore 212/self |
| 15 local handler = {}; | 15 local handler = {}; |
| 16 local stanza = st.stanza("root"); | 16 local stanza = st.stanza("root"); |
| 17 local namespaces = {} | |
| 18 function handler:StartNamespaceDecl(prefix, url) | |
| 19 if prefix ~= nil then | |
| 20 namespaces[prefix] = url | |
| 21 end | |
| 22 end | |
| 23 function handler:EndNamespaceDecl(prefix) | |
| 24 if prefix ~= nil then | |
| 25 namespaces[prefix] = nil | |
| 26 end | |
| 27 end | |
| 17 function handler:StartElement(tagname, attr) | 28 function handler:StartElement(tagname, attr) |
| 18 local curr_ns,name = tagname:match(ns_pattern); | 29 local curr_ns,name = tagname:match(ns_pattern); |
| 19 if name == "" then | 30 if name == "" then |
| 20 curr_ns, name = "", curr_ns; | 31 curr_ns, name = "", curr_ns; |
| 21 end | 32 end |
| 32 attr[ns..":"..nm] = attr[k]; | 43 attr[ns..":"..nm] = attr[k]; |
| 33 attr[k] = nil; | 44 attr[k] = nil; |
| 34 end | 45 end |
| 35 end | 46 end |
| 36 end | 47 end |
| 37 stanza:tag(name, attr); | 48 local n = {} |
| 49 for prefix, url in pairs(namespaces) do | |
| 50 n[prefix] = url | |
| 51 end | |
| 52 stanza:tag(name, attr, n); | |
| 38 end | 53 end |
| 39 function handler:CharacterData(data) | 54 function handler:CharacterData(data) |
| 40 stanza:text(data); | 55 stanza:text(data); |
| 41 end | 56 end |
| 42 function handler:EndElement() | 57 function handler:EndElement() |
