-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomxml.pas
344 lines (291 loc) · 6.19 KB
/
customxml.pas
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
unit customxml;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, StrUtils, Utility, Variants,
laz2_DOM, laz2_XMLRead, laz2_XMLWrite, laz2_XMLCfg, laz2_XMLUtils{, laz2_XMLStreaming};
type
tNewFileProc = procedure of object;
rStackNode = specialize rNode<TDOMNode>;
pStackNode = ^rStackNode;
eNodeLocation = (lAll, lChildNodes, lThisNode);
{ tXMLFile }
tXMLFile = class
//OnNewFile: tNewFileProc;
Doc: TXMLDocument;
RootNode: TDOMElement;
Stack: pStackNode;
{function GetIntValue(Criteria: string): longint;
function GetRealValue(Criteria: string): longint;
function GetStringValue(Criteria: string): longint;}
//property OnCreateNew: tNewFileProc read fNew write fNew;
private
FCurrentNode: TDOMNode;
Modified: boolean;
FileName: string;
procedure Push(n: TDomNode);
function Pop: TDomNode;
function GetCurrentNode: TDOMNode;
public
constructor CreateNew(fn: string; Overwrite: boolean = false; RootNodeName: string = '');
constructor Open(fn: string);
property CurrentNode: TDOMNode read GetCurrentNode;
procedure AddNode(Name: string);
function GetValue(Name: string): variant;
procedure SetValue(Name: string; Value: variant);
function FindNode(Name: string; Location: eNodeLocation = lThisNode): boolean;
function Next: boolean;
function Prev: boolean;
function Back: boolean;
procedure BackToRoot;
function IterateNodesWithName(Name: string): boolean;
function Save: integer;
destructor Destroy; override;
end;
implementation
{ tXMLFile }
function tXMLFile.GetValue(Name: string): variant;
var
PassNode: TDOMNode;
e, a: longint;
d: double;
s: string;
begin
s:= TDOMElement(Stack^.Value).GetAttribute(Name);
val(s, a, e);
if e = 0 then
Result:= a
else
begin
val(s, d, e);
if e = 0 then
Result:= d
else
Result:= s;
end;
end;
procedure tXMLFile.SetValue(Name: string; Value: variant);
begin
try
TDOMElement(Stack^.Value).SetAttribute(Name, VarToStr(Value));
Modified:= true;
except
on E: Exception do
begin
WriteLog(E.Message);
//Result:= -1;
end;
end;
end;
procedure tXMLFile.AddNode(Name: string);
var
DNode: TDOMNode;
begin
DNode := Doc.CreateElement(Name);
Stack^.Value.AppendChild(DNode);
Push(DNode);
Modified:= true;
end;
function tXMLFile.FindNode(Name: string; Location: eNodeLocation): boolean;
var
p: pStackNode;
DNode: TDOMNode;
begin
case Location of
lAll: DNode:= nil;
lChildNodes:
begin
{
s:= Copy2SymbDel(Name, '.');
PassNode:= RootNode.FindNode(s);
if PassNode = nil then
begin
PassNode:= Doc.CreateElement(s);
s:= VarToStr(Value);
TDOMElement(PassNode).SetAttribute(Name, s); //wrong
Node.AppendChild(PassNode);
end
else
begin
// PassNode.
end;
PassNode:= Doc.DocumentElement;
while pos('.', Name) <> 0 do
begin
s:= Copy2SymbDel(Name, '.');
// Retrieve the specified node
Node:= PassNode;
PassNode:= PassNode.FindNode(s);
if PassNode = nil then
begin
PassNode:= Doc.CreateElement(s);
Node.AppendChild(PassNode);
end;
end; }
DNode:= nil;
end;
lThisNode: DNode:= Stack^.Value.FindNode(Name);
end;
//writeln(Doc.TextContent);
if DNode <> nil then
begin
Push(DNode);
writeln(Stack^.Value.TextContent);
Result:= true;
end
else
Result:= false;
end;
function tXMLFile.Next: boolean;
var
NNode: TDOMNode;
begin
NNode:= CurrentNode.NextSibling;
if NNode <> nil then
begin
Result:= true;
Pop;
Push(NNode);
end
else
Result:= false;
end;
function tXMLFile.Prev: boolean;
var
NNode: TDOMNode;
begin
NNode:= CurrentNode.PreviousSibling;
if NNode <> nil then
begin
Result:= true;
Pop;
Push(NNode);
end
else
Result:= false;
end;
function tXMLFile.Back: boolean;
begin
if Stack^.Next <> nil then
begin
Result:= true;
Pop
end
else
Result:= false;
end;
procedure tXMLFile.BackToRoot;
begin
while Stack^.Next <> nil do
Pop // to free memory
end;
function tXMLFile.IterateNodesWithName(Name: string): boolean;
begin
{while IterateNodesWithName('Name') do
begin
...
end;
Back;
while IterateNodesWithName('NextName') do
begin
...
end;
}
if CurrentNode.NodeName = Name then
begin
Result:= Next;
if CurrentNode.NodeName <> Name then
begin
Result:= false;
Prev;
end
end
else
Result:= FindNode(Name);
//writeln(CurrentNode.NodeName);
end;
procedure tXMLFile.Push(n: TDomNode);
var
p: pStackNode;
begin
new(p);
with p^ do
begin
Value:= n;
Next:= Stack;
end;
Stack:= p;
end;
function tXMLFile.Pop: TDomNode;
var
p: pStackNode;
begin
Result:= Stack^.Value;
p:= Stack;
Stack:= Stack^.Next;
dispose(p);
end;
function tXMLFile.GetCurrentNode: TDOMNode;
begin
Result:= Stack^.Value;
end;
constructor tXMLFile.CreateNew(fn: string; Overwrite: boolean = false; RootNodeName: string = '');
begin
FileName:= fn;
try
if not Overwrite then
if FileExists(fn) then
raise Exception.Create('File already exists, overwrite not permitted');
Modified:= true;
if RootNodeName = '' then
RootNodeName:= fn;
Doc:= tXMLDocument.Create;
RootNode:= Doc.CreateElement(RootNodeName);
new(Stack);
Stack^.Next:= nil;
Stack^.Value:= RootNode;
Doc.AppendChild(RootNode);
except
on E: Exception do
begin
WriteLog(E.Message);
//E.Free;
Free;
//Result:= -1;
end;
end;
end;
constructor tXMLFile.Open(fn: string);
begin
Modified:= false;
FileName:= fn;
try
if not FileExists(fn) then
raise Exception.Create('File does not exist');
ReadXMLFile(Doc, FileName);
RootNode:= Doc.DocumentElement;
new(Stack);
Stack^.Next:= nil;
Stack^.Value:= RootNode;
except
on E: Exception do
begin
WriteLog(E.Message);
//E.Free;
Free;
end;
end;
end;
function tXMLFile.Save: integer;
begin
WriteXMLFile(Doc, FileName);
Result:= 1;
end;
destructor tXMLFile.Destroy;
begin
if Modified then
Save;
freeandnil(Doc);
inherited Destroy;
end;
end.