diff options
author | Kim Alvefur <zash@zash.se> | 2013-04-04 20:05:35 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2013-04-04 20:05:35 +0200 |
commit | a575e13e1db3aa43806b4c45fe766b359b7fd263 (patch) | |
tree | b7dd186cce303172684161b4fbab54ca40cc1ec1 | |
parent | 52fe78c768f8098aedc87a6242dd31f6e49e188f (diff) | |
download | prosody-a575e13e1db3aa43806b4c45fe766b359b7fd263.tar.gz prosody-a575e13e1db3aa43806b4c45fe766b359b7fd263.zip |
util.stanza: Add stanza:find(), a light weight XPath-like method
-rw-r--r-- | util/stanza.lua | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/util/stanza.lua b/util/stanza.lua index 213ed506..59c88c4e 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -18,6 +18,7 @@ local pairs = pairs; local ipairs = ipairs; local type = type; local s_gsub = string.gsub; +local s_sub = string.sub; local s_find = string.find; local os = os; @@ -174,6 +175,31 @@ function stanza_mt:maptags(callback) return self; end +function stanza_mt:find(path) + local pos = 1; + local len = #path + 1; + + repeat + local xmlns, name, text; + local char = s_sub(path, pos, pos); + if char == "@" then + return self.attr[s_sub(path, pos + 1)]; + elseif char == "{" then + xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1); + end + name, text, pos = s_match(path, "^([^@/#]*)([/#]?)()", pos); + name = name ~= "" and name or nil; + if pos == len then + if text == "#" then + return self:get_child_text(name, xmlns); + end + return self:get_child(name, xmlns); + end + self = self:get_child(name, xmlns); + until not self +end + + local xml_escape do local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; |