Annotate

util/datamapper.lua @ 11453:f0037234b2e9

util.datamapper: Enumerated elements E.g. error conditions or chat states.
author Kim Alvefur <zash@zash.se>
date Fri, 12 Mar 2021 01:33:15 +0100
parent 11451:ee4f2296e7df
child 11454:1d9c1893cc5e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require("util.stanza");
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local function toboolean(s)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 if s == "true" or s == "1" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 return true
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 elseif s == "false" or s == "0" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 return false
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local function parse_object(schema, s)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local out = {}
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 if schema.properties then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 for prop, propschema in pairs(schema.properties) do
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local name = prop
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local namespace = s.attr.xmlns;
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local prefix = nil
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local is_attribute = false
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local is_text = false
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
21 local name_is_value = false;
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
22 local single_attribute
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
23 local enums
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local proptype
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 if type(propschema) == "table" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 proptype = propschema.type
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 elseif type(propschema) == "string" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 proptype = propschema
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 if type(propschema) == "table" and propschema.xml then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 if propschema.xml.name then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 name = propschema.xml.name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 if propschema.xml.namespace then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 namespace = propschema.xml.namespace
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 if propschema.xml.prefix then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 prefix = propschema.xml.prefix
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 if propschema.xml.attribute then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 is_attribute = true
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 elseif propschema.xml.text then
11451
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
45
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 is_text = true
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
47 elseif propschema.xml.x_name_is_value then
11451
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
48
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
49 name_is_value = true
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
50 elseif propschema.xml.x_single_attribute then
11451
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
51
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
52 single_attribute = propschema.xml.x_single_attribute
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
54 if propschema["const"] then
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
55 enums = {propschema["const"]}
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
56 elseif propschema["enum"] then
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
57 enums = propschema["enum"]
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
58 end
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
61 if name_is_value then
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
62 local c
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
63 if proptype == "boolean" then
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
64 c = s:get_child(name, namespace);
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
65 elseif enums and proptype == "string" then
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
66
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
67 for i = 1, #enums do
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
68 c = s:get_child(enums[i], namespace);
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
69 if c then
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
70 break
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
71 end
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
72 end
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
73 else
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
74 c = s:get_child(nil, namespace);
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
75 end
11438
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
76 if c and proptype == "string" then
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
77 out[prop] = c.name;
11438
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
78 elseif proptype == "boolean" and c then
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
79 out[prop] = true;
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
80 end
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
81 elseif is_attribute then
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 local attr = name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 if prefix then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 attr = prefix .. ":" .. name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 elseif namespace ~= s.attr.xmlns then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 attr = namespace .. "\1" .. name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 if proptype == "string" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 out[prop] = s.attr[attr]
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 elseif proptype == "integer" or proptype == "number" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 out[prop] = tonumber(s.attr[attr])
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 elseif proptype == "boolean" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 out[prop] = toboolean(s.attr[attr])
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 elseif is_text then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 if proptype == "string" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 out[prop] = s:get_text()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 elseif proptype == "integer" or proptype == "number" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 out[prop] = tonumber(s:get_text())
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
105 elseif single_attribute then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
106 local c = s:get_child(name, namespace)
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
107 local a = c and c.attr[single_attribute]
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
108 if proptype == "string" then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
109 out[prop] = a
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
110 elseif proptype == "integer" or proptype == "number" then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
111 out[prop] = tonumber(a)
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
112 elseif proptype == "boolean" then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
113 out[prop] = toboolean(a)
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
114 end
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 else
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 if proptype == "string" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 out[prop] = s:get_child_text(name, namespace)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 elseif proptype == "integer" or proptype == "number" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 out[prop] = tonumber(s:get_child_text(name, namespace))
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 elseif proptype == "object" and type(propschema) == "table" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 local c = s:get_child(name, namespace)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 if c then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 out[prop] = parse_object(propschema, c);
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 return out
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 local function parse(schema, s)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 if schema.type == "object" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 return parse_object(schema, s)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
141 local function unparse(schema, t, current_name, current_ns)
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
142 if schema.type == "object" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
143
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
144 if schema.xml then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
145 if schema.xml.name then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
146 current_name = schema.xml.name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
147 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
148 if schema.xml.namespace then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
149 current_ns = schema.xml.namespace
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
150 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
151
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
152 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
153
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
154 local out = st.stanza(current_name, {xmlns = current_ns})
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
155
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
156 for prop, propschema in pairs(schema.properties) do
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
157 local v = t[prop]
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
158
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
159 if v ~= nil then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
160 local proptype
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
161 if type(propschema) == "table" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
162 proptype = propschema.type
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
163 elseif type(propschema) == "string" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
164 proptype = propschema
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
165 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
166
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
167 local name = prop
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
168 local namespace = current_ns
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
169 local prefix = nil
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
170 local is_attribute = false
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
171 local is_text = false
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
172 local name_is_value = false;
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
173 local single_attribute
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
174
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
175 if type(propschema) == "table" and propschema.xml then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
176
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
177 if propschema.xml.name then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
178 name = propschema.xml.name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
179 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
180 if propschema.xml.namespace then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
181 namespace = propschema.xml.namespace
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
182 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
183
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
184 if propschema.xml.prefix then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
185 prefix = propschema.xml.prefix
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
186 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
187
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
188 if propschema.xml.attribute then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
189 is_attribute = true
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
190 elseif propschema.xml.text then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
191 is_text = true
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
192 elseif propschema.xml.x_name_is_value then
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
193 name_is_value = true
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
194 elseif propschema.xml.x_single_attribute then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
195 single_attribute = propschema.xml.x_single_attribute
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
196 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
197 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
198
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
199 if is_attribute then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
200 local attr = name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
201 if prefix then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
202 attr = prefix .. ":" .. name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
203 elseif namespace ~= current_ns then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
204 attr = namespace .. "\1" .. name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
205 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
206
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
207 if proptype == "string" and type(v) == "string" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
208 out.attr[attr] = v
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
209 elseif proptype == "number" and type(v) == "number" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
210 out.attr[attr] = string.format("%g", v)
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
211 elseif proptype == "integer" and type(v) == "number" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
212 out.attr[attr] = string.format("%d", v)
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
213 elseif proptype == "boolean" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
214 out.attr[attr] = v and "1" or "0"
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
215 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
216 elseif is_text then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
217 if type(v) == "string" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
218 out:text(v)
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
219 end
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
220 elseif single_attribute then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
221 local propattr = {}
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
222
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
223 if namespace ~= current_ns then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
224 propattr.xmlns = namespace
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
225 end
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
226
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
227 if proptype == "string" and type(v) == "string" then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
228 propattr[single_attribute] = v
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
229 elseif proptype == "number" and type(v) == "number" then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
230 propattr[single_attribute] = string.format("%g", v)
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
231 elseif proptype == "integer" and type(v) == "number" then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
232 propattr[single_attribute] = string.format("%d", v)
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
233 elseif proptype == "boolean" and type(v) == "boolean" then
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
234 propattr[single_attribute] = v and "1" or "0"
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
235 end
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
236 out:tag(name, propattr):up();
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
237
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
238 else
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
239 local propattr
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
240 if namespace ~= current_ns then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
241 propattr = {xmlns = namespace}
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
242 end
11438
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
243 if name_is_value then
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
244 if proptype == "string" and type(v) == "string" then
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
245 out:tag(v, propattr):up();
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
246 elseif proptype == "boolean" and v == true then
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
247 out:tag(name, propattr):up();
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
248 end
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
249 elseif proptype == "string" and type(v) == "string" then
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
250 out:text_tag(name, v, propattr)
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
251 elseif proptype == "number" and type(v) == "number" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
252 out:text_tag(name, string.format("%g", v), propattr)
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
253 elseif proptype == "integer" and type(v) == "number" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
254 out:text_tag(name, string.format("%d", v), propattr)
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
255 elseif proptype == "boolean" and type(v) == "boolean" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
256 out:text_tag(name, v and "1" or "0", propattr)
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
257 elseif proptype == "object" and type(propschema) == "table" and type(v) == "table" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
258 local c = unparse(propschema, v, name, namespace);
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
259 if c then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
260 out:add_direct_child(c);
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
261 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
262
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
263 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
264 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
265 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
266 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
267 return out
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
268
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
269 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
270 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
271
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
272 return {parse = parse; unparse = unparse}