-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerySimpleXML.pas
608 lines (528 loc) · 16.1 KB
/
VerySimpleXML.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
{ VerySimpleXML v1.1 - a lightweight, one-unit XML reader/writer
by Dennis Spreen
http://blog.spreendigital.de/2011/11/10/verysimplexml-a-lightweight-delphi-xml-reader-and-writer/
(c) Copyrights 2012 Dennis D. Spreen <[email protected]>
This unit is free and can be used for any needs. The introduction of
any changes and the use of those changed library is permitted without
limitations. Only requirement:
This text must be present without changes in all modifications of library.
* The contents of this file are used with permission, subject to
* the Mozilla Public License Version 1.1 (the "License"); you may *
* not use this file except in compliance with the License. You may *
* obtain a copy of the License at *
* http: www.mozilla.org/MPL/MPL-1.1.html *
* *
* Software distributed under the License is distributed on an *
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or *
* implied. See the License for the specific language governing *
* rights and limitations under the License. *
}
{ Modified by Lewin Hodgman ([email protected]) for compatibility with Delphi 7
and Lazarus, as well as changes to better suit our needs }
unit VerySimpleXML;
interface
uses
Classes;
type
//TXmlList owns items and frees them when they are deleted from the list
TXmlList = class(TList)
protected
//This one function is enough to free all deleted/cleared/rewritten objects
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
end;
TXmlNodeList = class;
TXmlAttribute = class(TObject)
public
Name: String; // Attribute name
Value: String; // Attribute value (always as String)
end;
TXmlAttributeList = class(TXmlList)
public
function Find(AttrName: String): TXmlAttribute;
// Find an Attribute by Name (not case sensitive)
end;
TXmlNode = class(TObject)
private
FAttributes: TXmlAttributeList;
function GetAttribute(const AttrName: String): String;
procedure SetAttr(const AttrName: String; const Value: String);
public
Parent: TXmlNode; // NIL only for Root-Node
NodeName: String; // Node name
ChildNodes: TXmlNodeList; // Child nodes, never NIL
Text: String; // Node text content
Obj: TObject; // attached object
constructor Create; virtual;
destructor Destroy; override;
// Find a childnode by its name
function Find(Name: String): TXmlNode; overload;
// Find a childnode by Name/Attribute
function Find(Name, Attribute: String): TXmlNode; overload;
// Find a childnode by Name/Attribute/Value
function Find(Name, Attribute, Value: String): TXmlNode; overload;
// Return a list of childodes with given Name
function FindNodes(Name: String): TXmlNodeList; virtual;
// Returns True if the Attribute exits
function HasAttribute(const Name: String): Boolean; virtual;
// Returns True if this child nodes exists
function HasChild(const Name: String): Boolean; virtual;
// Add a child node and return it
function AddChild(const Name: String): TXmlNode; virtual;
function InsertChild(const Name: String; Pos: Integer): TXmlNode; virtual;
function SetText(Value: String): TXmlNode; virtual;
function SetAttribute(const AttrName: String;
const Value: String): TXmlNode; virtual;
property Attribute[const AttrName: String]: String read GetAttribute
write SetAttr; // Attributes of a Node, accessible by attribute name
end;
TXmlNodeList = class(TXmlList);
TXmlOnNodeSetText = procedure (Sender: TObject; Node: TXmlNode; Text: String) of
object;
TXmlVerySimple = class(TObject)
private
Lines: TStringList;
FOnNodeSetText: TXmlOnNodeSetText;
FOnNodeSetName: TXmlOnNodeSetText;
procedure Parse(Strings: TStringList);
procedure Walk(Lines: TStringList; Prefix: String; Node: TXmlNode);
function GetText: String;
public
Root: TXmlNode; // There is only one Root Node
Header: TXmlNode; // XML declarations are stored in here as Attributes
AllowSelfClosingTags: Boolean;
constructor Create; virtual;
destructor Destroy; override;
procedure Clear; virtual;
// Load XML from a file
procedure LoadFromFile(const FileName: String); virtual;
// Encoding is specified in Header-Node
procedure SaveToFile(const FileName: String); virtual;
procedure DefaultOnNodeSetText(Sender: TObject; Node: TXmlNode; Text: String);
procedure DefaultOnNodeSetName(Sender: TObject; Node: TXmlNode; Name: String);
property Text: String read GetText;
property OnNodeSetText: TXmlOnNodeSetText read FOnNodeSetText write FOnNodeSetText;
property OnNodeSetName: TXmlOnNodeSetText read FOnNodeSetName write FOnNodeSetName;
end;
function XMLEscape(Value: String): String;
function XMLUnescape(Value: String): String;
implementation
uses
SysUtils, StrUtils;
function XMLEscape(Value: String): String;
var I: Integer;
const ESCAPED_CHARS: set of Byte = [34,38,39,60,62,160..255];
begin
Result := '';
for I:=1 to Length(Value) do
if Ord(Value[I]) in ESCAPED_CHARS then
Result := Result + '&#'+IntToStr(Ord(Value[I]))+';'
else
Result := Result + Value[I];
end;
function XMLUnescape(Value: String): String;
begin
Result := StringReplace(Value, '<', '<', [rfReplaceAll] );
Result := StringReplace(Result, '>', '>', [rfReplaceAll]);
Result := StringReplace(Result, ''', chr(39), [rfReplaceAll]);
Result := StringReplace(Result, '"', '"', [rfReplaceAll]);
Result := StringReplace(Result, '&', '&', [rfReplaceAll]);
end;
//We were notified that the item is deleted from the list
procedure TXmlList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if (Action = lnDeleted) then
TObject(Ptr).Free;
end;
{ TXmlVerySimple }
procedure TXmlVerySimple.Clear;
begin
Root.Free;
Header.Free;
Root := TXmlNode.Create;
Header := TXmlNode.Create;
Header.NodeName := '?xml'; // Default XML Header
Header.Attribute['version'] := '1.0'; // Default XML Version
Lines.Clear;
end;
constructor TXmlVerySimple.Create;
begin
inherited;
Lines := TStringList.Create;
FOnNodeSetText := DefaultOnNodeSetText;
FOnNodeSetName := DefaultOnNodeSetName;
Clear;
end;
procedure TXmlVerySimple.DefaultOnNodeSetName(Sender: TObject; Node: TXmlNode;
Name: String);
begin
Node.NodeName := Name;
end;
procedure TXmlVerySimple.DefaultOnNodeSetText(Sender: TObject; Node: TXmlNode;
Text: String);
begin
Node.Text := Text;
end;
destructor TXmlVerySimple.Destroy;
begin
Root.Free;
Header.Free;
Lines.Free;
inherited;
end;
procedure TXmlVerySimple.LoadFromFile(const FileName: String);
var Strings: TStringList;
begin
Clear;
Strings := TStringList.Create;
Strings.LoadFromFile(FileName);
Parse(Strings);
Strings.Free;
end;
procedure TXmlVerySimple.Parse(Strings: TStringList);
var
Line: String;
IsTag, IsText: Boolean;
Tag, Text: String;
Parent, Node: TXmlNode;
Attribute: TXmlAttribute;
ALine, Attr, AttrText: String;
I, P: Integer;
IsSelfClosing: Boolean;
IsQuote: Boolean;
// Return a text ended by StopChar, respect quotation marks
function GetText(var Line: String; StartStr: String; StopChar: Char): String;
var
Chr: Char;
begin
while (Length(Line) > 0) and ((Line[1] <> StopChar) or (IsQuote)) do
begin
Chr := Line[1];
if Chr = '"' then
IsQuote := Not IsQuote;
StartStr := StartStr + Chr;
delete(Line, 1, 1);
end;
Result := StartStr;
end;
begin
if assigned(Root) then // Release previous nodes (if set)
Root.Free;
IsTag := False;
IsText := False;
IsQuote := False;
Node := NIL;
for I:=0 to Strings.Count-1 do
begin
Line := Strings[I];
while (Length(Line) > 0) do
begin
if (not IsTag) and (not IsText) then
begin
while (Length(Line) > 0) and (Line[1] <> '<') do
delete(Line, 1, 1);
if Length(Line) > 0 then
begin
IsTag := True;
delete(Line, 1, 1); // Delete openining tag
Tag := '';
end;
end;
if IsTag then
begin
Tag := GetText(Line, Tag, '>');
if (Length(Line) > 0) and (Line[1] = '>') then
begin
delete(Line, 1, 1);
IsTag := False;
if (Length(Tag) > 0) and (Tag[1] = '/') then
Node := Node.Parent
else
begin
Parent := Node;
IsText := True;
IsQuote := False;
Node := TXmlNode.Create;
if lowercase(copy(Tag, 1, 4)) = '?xml' then // check for xml header
begin
Tag := TrimRight(Tag);
if Tag[Length(Tag)]='?' then
delete(Tag, Length(Tag), 1);
Header.Free;
Header := Node;
end;
// Self-Closing Tag
if (Length(Tag) > 0) and (Tag[Length(Tag)] = '/') then
begin
IsSelfClosing := True;
delete(Tag, Length(Tag), 1);
end
else
IsSelfClosing := False;
P := pos(' ', Tag);
if P <> 0 then // Tag name has attributes
begin
ALine := Tag;
delete(Tag, P, Length(Tag));
delete(ALine, 1, P);
while Length(ALine) > 0 do
begin
Attr := GetText(ALine, '', '='); // Get Attribute Name
AttrText := GetText(ALine, '', ' '); // Get Attribute Value
if Length(AttrText) > 0 then
begin
delete(AttrText, 1, 1); // Remove blank
if AttrText[1] = '"' then // Remove start/end quotation marks
begin
delete(AttrText, 1, 1);
if AttrText[Length(AttrText)] = '"' then
delete(AttrText, Length(AttrText), 1);
end;
end;
if Length(ALine) > 0 then
delete(ALine, 1, 1);
// Header node (Attr='?') does not support Attributes
if not((Node = Header) and (Attr = '?')) then
begin
Attribute := TXmlAttribute.Create;
Attribute.Name := Attr;
Attribute.Value := AttrText;
Node.FAttributes.Add(Attribute);
end;
IsQuote := False;
end;
end;
FOnNodeSetName(Self, Node, Tag);
Node.Parent := Parent;
if assigned(Parent) then
Parent.ChildNodes.Add(Node)
else if Node = Header then
begin
IsText := False;
Node := NIL;
end
else
Root := Node;
Text := '';
if IsSelfClosing then
Node := Node.Parent;
end;
end;
end;
if IsText then
begin
Text := GetText(Line, Text, '<');
if (Length(Line) > 0) and (Line[1] = '<') then
begin
IsText := False;
while (Length(Text) > 0) and (Text[1] = ' ') do
delete(Text, 1, 1);
FOnNodeSetText(Self, Node, XMLUnescape(Text));
end;
end;
end;
end;
end;
procedure TXmlVerySimple.SaveToFile(const FileName: String);
begin
GetText;
Lines.SaveToFile(FileName);
end;
procedure TXmlVerySimple.Walk(Lines: TStringList; Prefix: String;
Node: TXmlNode);
var
Attribute: TXmlAttribute;
OriginalPrefix: String;
S: String;
IsSelfClosing: Boolean;
I: Integer;
begin
S := Prefix + '<' + Node.NodeName;
for I:=0 to Node.FAttributes.Count-1 do
begin
Attribute := Node.FAttributes[I];
S := S + ' ' + Attribute.Name + '="' + Attribute.Value + '"';
end;
if Node = Header then
S := S + ' ?';
IsSelfClosing := AllowSelfClosingTags and (Length(Node.Text) = 0) and (Node.ChildNodes.Count = 0) and
(Node <> Header);
if IsSelfClosing then
S := S + ' /';
S := S + '>';
if Length(Node.Text) > 0 then
S := S + XMLEscape(Node.Text);
if (Node.ChildNodes.Count = 0) and ((Length(Node.Text) > 0) or not AllowSelfClosingTags) and
(Node <> Header) then
begin
S := S + '</' + Node.NodeName + '>';
Lines.Add(S);
end
else
begin
Lines.Add(S);
OriginalPrefix := Prefix;
Prefix := Prefix + ' ';
for I:=0 to Node.ChildNodes.Count-1 do
Walk(Lines, Prefix, Node.ChildNodes[I]);
if (Node <> Header) and (not IsSelfClosing) then
Lines.Add(OriginalPrefix + '</' + Node.NodeName + '>');
end;
end;
function TXmlVerySimple.GetText: String;
begin
Lines.Clear;
// Create XML introduction
Walk(Lines, '', Header);
// Create nodes representation
Walk(Lines, '', Root);
Result := Lines.Text;
end;
{ TXmlNode }
function TXmlNode.AddChild(const Name: String): TXmlNode;
begin
Result := TXmlNode.Create;
Result.NodeName := Name;
Result.Parent := Self;
ChildNodes.Add(Result);
end;
constructor TXmlNode.Create;
begin
ChildNodes := TXmlNodeList.Create;
Parent := NIL;
FAttributes := TXmlAttributeList.Create;
end;
destructor TXmlNode.Destroy;
begin
FAttributes.Free;
ChildNodes.Free;
inherited;
end;
function TXmlNode.Find(Name: String): TXmlNode;
var
I: Integer;
begin
Result := NIL;
Name := lowercase(Name);
for I:=0 to ChildNodes.Count-1 do
if lowercase(TXmlNode(ChildNodes[I]).NodeName) = Name then
begin
Result := ChildNodes[I];
Break;
end;
end;
function TXmlNode.Find(Name, Attribute, Value: String): TXmlNode;
var
Node: TXmlNode;
I: Integer;
begin
Result := NIL;
Name := lowercase(Name);
for I:=0 to ChildNodes.Count-1 do
begin
Node := ChildNodes[I];
if (lowercase(Node.NodeName) = Name) and (Node.HasAttribute(Attribute)) and
(Node.Attribute[Attribute] = Value) then
begin
Result := Node;
Break;
end;
end;
end;
function TXmlNode.Find(Name, Attribute: String): TXmlNode;
var
Node: TXmlNode;
I: Integer;
begin
Result := NIL;
Name := lowercase(Name);
for I:=0 to ChildNodes.Count-1 do
begin
Node := ChildNodes[I];
if (lowercase(Node.NodeName) = Name) and (Node.HasAttribute(Attribute)) then
begin
Result := Node;
Break;
end;
end;
end;
function TXmlNode.FindNodes(Name: String): TXmlNodeList;
var
Node: TXmlNode;
I: Integer;
begin
Result := TXmlNodeList.Create;
Name := lowercase(Name);
for I:=0 to ChildNodes.Count-1 do
begin
Node := ChildNodes[I];
if (lowercase(Node.NodeName) = Name) then
Result.Add(Node);
end;
end;
function TXmlNode.GetAttribute(const AttrName: String): String;
var
Attribute: TXmlAttribute;
begin
Attribute := FAttributes.Find(AttrName);
if assigned(Attribute) then
Result := Attribute.Value
else
Result := '';
end;
function TXmlNode.HasAttribute(const Name: String): Boolean;
begin
Result := assigned(FAttributes.Find(Name));
end;
function TXmlNode.HasChild(const Name: String): Boolean;
begin
Result := assigned(Find(Name));
end;
function TXmlNode.InsertChild(const Name: String; Pos: Integer): TXmlNode;
begin
Result := TXmlNode.Create;
Result.NodeName := Name;
Result.Parent := Self;
ChildNodes.Insert(Pos, Result);
end;
procedure TXmlNode.SetAttr(const AttrName, Value: String);
begin
SetAttribute(AttrName, Value);
end;
function TXmlNode.SetAttribute(const AttrName, Value: String): TXmlNode;
var
Attribute: TXmlAttribute;
begin
Attribute := FAttributes.Find(AttrName); // Search for given name
if not assigned(Attribute) then // If attribute is not found, create one
begin
Attribute := TXmlAttribute.Create;
FAttributes.Add(Attribute);
end;
Attribute.Name := AttrName; // this allows "name-style" rewriting
Attribute.Value := Value;
Result := Self;
end;
function TXmlNode.SetText(Value: String): TXmlNode;
begin
Text := Value;
Result := Self;
end;
{ TXmlAttributeList }
function TXmlAttributeList.Find(AttrName: String): TXmlAttribute;
var
Attribute: TXmlAttribute;
I: Integer;
begin
Result := NIL;
AttrName := lowercase(AttrName);
for I:=0 to Count-1 do
begin
Attribute := Self[I];
if lowercase(Attribute.Name) = AttrName then
begin
Result := Attribute;
Break;
end;
end;
end;
end.