util.dataforms: Add method for converting XML dataforms to tables

Fri, 15 Feb 2013 20:45:31 +0100

author
Kim Alvefur <zash@zash.se>
date
Fri, 15 Feb 2013 20:45:31 +0100
changeset 335
9e69ee8542d4
parent 334
34c52f3b21c4
child 336
658c62c9ecc4

util.dataforms: Add method for converting XML dataforms to tables

util/dataforms.lua file | annotate | diff | comparison | revisions
--- a/util/dataforms.lua	Fri Feb 15 20:40:56 2013 +0100
+++ b/util/dataforms.lua	Fri Feb 15 20:45:31 2013 +0100
@@ -24,6 +24,44 @@
 	return setmetatable(layout, form_mt);
 end
 
+function from_stanza(stanza)
+	local layout = {
+		title = stanza:get_child_text("title");
+		instructions = stanza:get_child_text("instructions");
+	};
+	for tag in stanza:childtags("field") do
+		local field = {
+			name = tag.attr.var;
+			label = tag.attr.label;
+			type = tag.attr.type;
+			required = tag:get_child("required") and true or nil;
+			value = tag:get_child_text("value");
+		};
+		layout[#layout+1] = field;
+		if field.type then
+			local value = {};
+			if field.type:match"list%-" then
+				for tag in tag:childtags("option") do
+					value[#value+1] = { label = tag.attr.label, value = tag:get_child_text("value") };
+				end
+				for tag in tag:childtags("value") do
+					value[#value+1] = { label = tag.attr.label, value = tag:get_text(), default = true };
+				end
+			elseif field.type:match"%-multi" then
+				for tag in tag:childtags("value") do
+					value[#value+1] = tag.attr.label and { label = tag.attr.label, value = tag:get_text() } or tag:get_text();
+				end
+				if field.type == "text-multi" then
+					field.value = t_concat(value, "\n");
+				else
+					field.value = value;
+				end
+			end
+		end
+	end
+	return new(layout);
+end
+
 function form_t.form(layout, data, formtype)
 	local form = st.stanza("x", { xmlns = xmlns_forms, type = formtype or "form" });
 	if layout.title then

mercurial