-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlDocument.lua
307 lines (249 loc) · 5.99 KB
/
XmlDocument.lua
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
local XmlDocument = {}
local XmlNode = {}
---------------------------------------------------------------------------
-- XmlDocument
---------------------------------------------------------------------------
local CreateNodeFromTable
function XmlDocument.New()
local self = {}
local tRoot = nil
function self:GetRoot()
return tRoot
end
function self:SetRoot(tNewRoot)
tRoot = tNewRoot
end
function self:NewNode(strTag, tAttributes)
return XmlNode.New(self, strTag, tAttributes)
end
function self:ToXmlDoc()
return XmlDoc.CreateFromTable(tRoot:ToTable())
end
function self:ToTable()
if tRoot then
return tRoot:ToTable()
end
end
function self:Serialize()
if tRoot then
return tRoot:Serialize()
end
end
return self
end
function XmlDocument.NewForm()
local self = {}
local tRoot = XmlNode.New(self, "Forms", {})
function self:GetRoot()
return tRoot
end
function self:SetRoot(tNewRoot)
tRoot = tNewRoot
end
function self:NewNode(strTag, tAttributes)
return XmlNode.New(self, strTag, tAttributes)
end
function self:NewFormNode(strName, tAttributes)
local tForm = self:NewNode("Form", tAttributes)
tForm:Attribute("Name", strName)
tForm:Attribute("Class", "Window")
return tForm
end
function self:NewControlNode(strName, strClass, tAttributes)
local tControl = self:NewNode("Control", tAttributes)
tControl:Attribute("Name", strName)
tControl:Attribute("Class", strClass)
return tControl
end
function self:NewEventNode(strName, strFunction)
local tEvent = self:NewNode("Event", {
Name = strName,
Function = strFunction
})
return tEvent
end
function self:LoadForm(strName, wndParent, tHandler)
return Apollo.LoadForm(self:ToXmlDoc(), strName, wndParent, tHandler)
end
function self:ToXmlDoc()
return XmlDoc.CreateFromTable(tRoot:ToTable())
end
function self:ToTable()
return tRoot:ToTable()
end
function self:Serialize()
return tRoot:Serialize()
end
return self
end
local function AddChildFromTable(tXml, tParent, tDoc)
local tNode = CreateNodeFromTable(tXml, tDoc)
tParent:AddChild(tNode)
for i,v in ipairs(tXml) do
AddChildFromTable(v, tNode, tDoc)
end
end
function XmlDocument.CreateFromTable(tXml)
local tDoc = XmlDocument.New()
local tRoot = CreateNodeFromTable(tXml, tDoc)
for i,v in ipairs(tXml) do
AddChildFromTable(v, tRoot, tDoc)
end
tDoc:SetRoot(tRoot)
return tDoc
end
function CreateNodeFromTable(tXml, tDoc)
local tNode = tDoc:NewNode(tXml.__XmlNode)
for k,v in pairs(tXml) do
tNode:Attribute(k, v)
end
return tNode
end
function XmlDocument.CreateFromFile(strPath)
local xmlDoc = XmlDoc.CreateFromFile(strPath)
if not xmlDoc then return end
return XmlDocument.CreateFromTable(xmlDoc:ToTable())
end
---------------------------------------------------------------------------
-- XmlNode (only used internally)
---------------------------------------------------------------------------
function XmlNode.New(tDoc, strTag, tAttributes)
tAttributes = true and tAttributes or {}
local self = {}
local tChildren = {}
local strText = ""
function self:GetDocument()
return tDoc
end
function self:SetDocument(tDocument)
tDoc = tDocument
end
function self:GetChildren()
return tChildren
end
function self:AddChild(tNode)
table.insert(tChildren, tNode)
tNode:SetDocument(tDoc)
return self
end
function self:RemoveChild(nId)
return table.remove(tChildren, nId)
end
function self:RemoveAllChildren()
tChildren = {}
end
function self:GetTag()
return strTag
end
function self:Attribute(strName, value)
if value ~= nil then
tAttributes[strName] = value
return self
else
return tAttributes[strName]
end
end
function self:Attributes(tAtts)
if tAtts then
tAttributes = tAtts
return self
else
return tAttributes
end
end
function self:Text(strTxt)
if strTxt then
strText = strTxt
else
return strText
end
end
function self:EachChild(fn)
for i,v in ipairs(tChildren) do
pcall(fn, v)
self:EachChild(fn)
end
end
function self:FindChild(fn)
local tNode = nil
for i,v in ipairs(tChildren) do
local bSuccess, bResult = pcall(fnFind, v)
if bSuccess == true and bResult == true then
tNode = v
break
else
tNode = v:Find(fn)
end
if tNode ~= nil then
break
end
end
return tNode
end
function self:FindChildByName(strName)
return self:FindChild(function(tNode)
return tNode:Attribute("Name") == strName
end)
end
function self:Clone()
return tDoc:NewNode(strTag, tAttributes)
end
function self:ToTable(tXml)
-- This node
local tNode = {__XmlNode = strTag}
for k,v in pairs(tAttributes) do
if k == "AnchorPoints" then
tNode["LAnchorPoint"] = v[1]
tNode["TAnchorPoint"] = v[2]
tNode["RAnchorPoint"] = v[3]
tNode["BAnchorPoint"] = v[4]
elseif k == "AnchorOffsets" then
tNode["LAnchorOffset"] = v[1]
tNode["TAnchorOffset"] = v[2]
tNode["RAnchorOffset"] = v[3]
tNode["BAnchorOffset"] = v[4]
else
tNode[k] = v
end
end
-- Children nodes
for i,v in ipairs(tChildren) do
v:ToTable(tNode)
end
if tXml then
table.insert(tXml, tNode)
end
return tNode
end
function self:Serialize(nLevel)
-- Recursion variables
nLevel = true and nLevel or 0
-- Indenting
local strIndent = ""
for i=1,nLevel do
strIndent = strIndent .. " "
end
local strXml = strIndent .. "<" .. strTag
-- Start tag
for k,v in pairs(tAttributes) do
strXml = strXml .. " " .. k .. "=\"" .. tostring(v) .. "\""
end
strXml = strXml .. ">\n"
-- Inner text or children, not both
if #tChildren > 0 then
-- Children add themselves to string
for i,v in ipairs(tChildren) do
strXml = strXml .. v:Serialize(nLevel + 1)
end
elseif strText then
strXml = strXml .. strText .. "\n"
end
-- End tag
strXml = strXml .. strIndent .. "</" .. strTag .. ">\n"
return strXml
end
return self
end
-- Register Packages
Apollo.RegisterPackage(XmlDocument, "Drafto:Lib:XmlDocument-1.0", 5, {})
--Apollo.RegisterPackage(XmlNode, "Drafto:Lib:XmlNode-2.0", 1, {})