Uh oh!
There was an error while loading. Please reload this page.
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
Latest commit
212 lines (184 loc) · 4.44 KB
/
Copy pathxmlview.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"
]]
localPublic= {}
xmlview=Public
localtree_key= {"tree_key"}
localcachecomplete_key= {"cachecomplete_key"}
localpopulate_key= {"populate_key"}
localerror_key= {"error_key"}
--local function text(tree) return tree[1] end
functionxtext(tree)
iftype(tree) =="string" thenreturntreeend
-- fast-path some common cases
localfirst=tree[1]
ifnotfirstthenreturn"" end
iftype(first) =="string" andnottree[2] then
returnfirst
end
locals=""
fori, lzinxpairs(tree) do
s=s..xtext(lz)
end
returns
end
functionxstring(tree)
iftype(tree) =="string" thenreturntreeend
-- fast-path some common cases
localfirst=tree[1]
ifnotfirstthenreturn"" end
iftype(first) =="string" andnottree[2] then
returnfirst
end
locals=""
fori, lzinxpairs(tree) do
iftype(lz) =="string" then
s=s..lz
else
returnnil, "contains mixed content"
end
end
returns
end
localviewmetatable= {}
functionviewmetatable.__index(t, k)
ifnotrawget(t, cachecomplete_key) then
localpopulate=t[populate_key]
populate(t)
end
localerror_table=rawget(t, error_key)
iferror_tableanderror_table[k] then
localtablename=t[tree_key].name
error("in element "..tablename.." xmlview index "..k..": "..error_table[k])
end
returnrawget(t, k)
end
local
functionensure_table(t)
iftype(t) ~="table" then
error("view must be on a table")
end
end
local
functionpopulatestringcache(t)
localerror_table= {}
t[error_key] =error_table
localroot=t[tree_key]
fori, element, nameinxnpairs(root) do
ifrawget(t, name) then
error_table[name] ="contains duplicate content"
else
locals, msg=xstring(element)
ifsthen
t[name] =xstring(element)
else
error_table[name] =msg
end
end
end
fork,vinpairs(error_table) do
t[k] =nil
end
t[cachecomplete_key] =true
end
local
functionstring(x)
ensure_table(x)
localt= {[tree_key]=x, [populate_key]=populatestringcache}
setmetatable(t, viewmetatable)
returnt
end
Public.string=string
local
functionpopulatetextcache(t)
localerror_table= {}
t[error_key] =error_table
localroot=t[tree_key]
fori, element, nameinxnpairs(root) do
ifrawget(t, name) then
error_table[name] ="contains duplicate content"
else
t[name] =xtext(element)
end
end
fork,vinpairs(error_table) do
t[k] =nil
end
t[cachecomplete_key] =true
end
local
functiontext(x)
ensure_table(x)
localt= {[tree_key]=x, [populate_key]=populatetextcache}
setmetatable(t, viewmetatable)
returnt
end
Public.text=text
local
functionpopulatenodecache(t)
localerror_table= {}
t[error_key] =error_table
localroot=t[tree_key]
fori, element, nameinxnpairs(root) do
ifrawget(t, name) then
error_table[name] ="contains duplicate content"
else
t[name] =element
end
end
fork,vinpairs(error_table) do
t[k] =nil
end
t[cachecomplete_key] =true
end
local
functionelement(x)
ensure_table(x)
localt= {[tree_key]=x, [populate_key]=populatenodecache}
setmetatable(t, viewmetatable)
returnt
end
Public.element=element
local
functionpopulatenodescache(t)
localroot=t[tree_key]
fori, element, nameinxnpairs(root) do
localexisting=rawget(t, name)
ifnotexistingthen
t[name] = {n=0}
end
table.insert(t[name], element)
end
t[cachecomplete_key] =true
end
local
functionelements(x)
ensure_table(x)
localt= {[node_key]=x, [populate_key]=populatenodescache}
setmetatable(t, viewmetatable)
returnt
end
Public.elements=elements