This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlview.lua
More file actions
212 lines (184 loc) · 4.44 KB
/
xmlview.lua
File metadata and controls
212 lines (184 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
require "xmliter"
-- require "util"
--[[ Work in progress. Quick summary:
xstring(node) => immediate string contents of node. returns nil,error on mixed content
xtext(node) => recursive string contents of node
Views
v = xmlview.string(node)
v.date => finds a child element named date and returns xstring(date)
]]
--[[
<logentry>
<type>INFO</info>
<time>12:50</time>
<receivedfrom>upstream1</receivedfrom>
<receivedfrom>upstream2</receivedfrom>
<body>This is a <b>test message</b></body>
<empty/>
</logentry>
tree=lxptree.parsestring(s)
sv = xmlview.string(tree)
print(sv.type) -- "INFO"
print(sv.time) -- "12:50"
print(sv.empty) -- ""
print(sv.receivedfrom) -- error "contains duplicate content"
print(sv.body) -- error "contains mixed content"
]]
local Public = {}
xmlview = Public
local tree_key = {"tree_key"}
local cachecomplete_key = {"cachecomplete_key"}
local populate_key = {"populate_key"}
local error_key = {"error_key"}
--local function text(tree) return tree[1] end
function xtext(tree)
if type(tree) == "string" then return tree end
-- fast-path some common cases
local first = tree[1]
if not first then return "" end
if type(first) == "string" and not tree[2] then
return first
end
local s = ""
for i, lz in xpairs(tree) do
s = s..xtext(lz)
end
return s
end
function xstring(tree)
if type(tree) == "string" then return tree end
-- fast-path some common cases
local first = tree[1]
if not first then return "" end
if type(first) == "string" and not tree[2] then
return first
end
local s = ""
for i, lz in xpairs(tree) do
if type(lz) == "string" then
s = s..lz
else
return nil, "contains mixed content"
end
end
return s
end
local viewmetatable = {}
function viewmetatable.__index(t, k)
if not rawget(t, cachecomplete_key) then
local populate = t[populate_key]
populate(t)
end
local error_table = rawget(t, error_key)
if error_table and error_table[k] then
local tablename = t[tree_key].name
error("in element "..tablename.." xmlview index "..k..": "..error_table[k])
end
return rawget(t, k)
end
local
function ensure_table(t)
if type(t) ~= "table" then
error("view must be on a table")
end
end
local
function populatestringcache(t)
local error_table = {}
t[error_key] = error_table
local root = t[tree_key]
for i, element, name in xnpairs(root) do
if rawget(t, name) then
error_table[name] = "contains duplicate content"
else
local s, msg = xstring(element)
if s then
t[name] = xstring(element)
else
error_table[name] = msg
end
end
end
for k,v in pairs(error_table) do
t[k] = nil
end
t[cachecomplete_key] = true
end
local
function string(x)
ensure_table(x)
local t = {[tree_key]=x, [populate_key]=populatestringcache}
setmetatable(t, viewmetatable)
return t
end
Public.string = string
local
function populatetextcache(t)
local error_table = {}
t[error_key] = error_table
local root = t[tree_key]
for i, element, name in xnpairs(root) do
if rawget(t, name) then
error_table[name] = "contains duplicate content"
else
t[name] = xtext(element)
end
end
for k,v in pairs(error_table) do
t[k] = nil
end
t[cachecomplete_key] = true
end
local
function text(x)
ensure_table(x)
local t = {[tree_key]=x, [populate_key]=populatetextcache}
setmetatable(t, viewmetatable)
return t
end
Public.text = text
local
function populatenodecache(t)
local error_table = {}
t[error_key] = error_table
local root = t[tree_key]
for i, element, name in xnpairs(root) do
if rawget(t, name) then
error_table[name] = "contains duplicate content"
else
t[name] = element
end
end
for k,v in pairs(error_table) do
t[k] = nil
end
t[cachecomplete_key] = true
end
local
function element(x)
ensure_table(x)
local t = {[tree_key]=x, [populate_key]=populatenodecache}
setmetatable(t, viewmetatable)
return t
end
Public.element = element
local
function populatenodescache(t)
local root = t[tree_key]
for i, element, name in xnpairs(root) do
local existing = rawget(t, name)
if not existing then
t[name] = {n=0}
end
table.insert(t[name], element)
end
t[cachecomplete_key] = true
end
local
function elements(x)
ensure_table(x)
local t = {[node_key]=x, [populate_key]=populatenodescache}
setmetatable(t, viewmetatable)
return t
end
Public.elements = elements