Software /
code /
prosody
Comparison
util/stanza.lua @ 5424:7318527c6dea
util.stanza: Add stanza:find(), a light weight XPath-like method
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 04 Apr 2013 20:05:35 +0200 |
parent | 5414:efec29eb4cdd |
child | 5435:f56e449a63e3 |
comparison
equal
deleted
inserted
replaced
5423:4acc1598f391 | 5424:7318527c6dea |
---|---|
16 local setmetatable = setmetatable; | 16 local setmetatable = setmetatable; |
17 local pairs = pairs; | 17 local pairs = pairs; |
18 local ipairs = ipairs; | 18 local ipairs = ipairs; |
19 local type = type; | 19 local type = type; |
20 local s_gsub = string.gsub; | 20 local s_gsub = string.gsub; |
21 local s_sub = string.sub; | |
21 local s_find = string.find; | 22 local s_find = string.find; |
22 local os = os; | 23 local os = os; |
23 | 24 |
24 local do_pretty_printing = not os.getenv("WINDIR"); | 25 local do_pretty_printing = not os.getenv("WINDIR"); |
25 local getstyle, getstring; | 26 local getstyle, getstring; |
172 i = i + 1; | 173 i = i + 1; |
173 end | 174 end |
174 return self; | 175 return self; |
175 end | 176 end |
176 | 177 |
178 function stanza_mt:find(path) | |
179 local pos = 1; | |
180 local len = #path + 1; | |
181 | |
182 repeat | |
183 local xmlns, name, text; | |
184 local char = s_sub(path, pos, pos); | |
185 if char == "@" then | |
186 return self.attr[s_sub(path, pos + 1)]; | |
187 elseif char == "{" then | |
188 xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1); | |
189 end | |
190 name, text, pos = s_match(path, "^([^@/#]*)([/#]?)()", pos); | |
191 name = name ~= "" and name or nil; | |
192 if pos == len then | |
193 if text == "#" then | |
194 return self:get_child_text(name, xmlns); | |
195 end | |
196 return self:get_child(name, xmlns); | |
197 end | |
198 self = self:get_child(name, xmlns); | |
199 until not self | |
200 end | |
201 | |
202 | |
177 local xml_escape | 203 local xml_escape |
178 do | 204 do |
179 local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; | 205 local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; |
180 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end | 206 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end |
181 _M.xml_escape = xml_escape; | 207 _M.xml_escape = xml_escape; |