This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.System.Xml.pas
1026 lines (868 loc) · 22.9 KB
/
EvilWorks.System.Xml.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*============================================================================================================
EvilLibrary by Vedran Vuk 2010-2012
Name: EvilWorks.Xml
Description: Barebones Xml class. Don't expect it to follow standards. It parses XML, ok.
File last change date: August 11th. 2012
File version: 0.0.1
Licence: Free as in beer.
===========================================================================================================*)
unit EvilWorks.System.Xml;
interface
uses
System.Classes,
System.SysUtils,
EvilWorks.System.StrUtils;
const
SAttributeNotFound = 'Attribute "%s" not found.';
SNodeNotFound = 'Node "%s" not found.';
SMalformedXml = 'Malformed XML on line %d.';
SCantHaveTextAndChildren = 'Node cannot have text and child nodes.';
type
{ Exceptions }
EXml = class(Exception);
EMalformedXml = class(EXml);
EAttributeNotFound = class(EXml);
ENodeNotFound = class(EXml);
{ Forward declarations }
TXmlAttribute = class;
TXmlAttributes = class;
TXmlNode = class;
TXmlNodes = class;
TXml = class;
{ TXmlAttribute }
TXmlAttribute = class(TPersistent)
private
FOwner: TXmlAttributes;
FName : string;
FValue: string;
procedure SetName(const aValue: string);
procedure SetValue(const aValue: string);
public
constructor Create(aOwner: TXmlAttributes); virtual;
procedure Assign(aSource: TPersistent); override;
property Owner: TXmlAttributes read FOwner;
property name: string read FName write SetName;
property Value: string read FValue write SetValue;
end;
{ TXmlAttributes }
TXmlAttributes = class(TPersistent)
private
FOwner: TXmlNode;
FList: TList;
function GetCount: integer;
function GetByIndex(const aIndex: integer): TXmlAttribute;
function GetByName(const aName: string): TXmlAttribute;
procedure SetByName(const aName: string; const aValue: TXmlAttribute);
procedure SetByIndex(const aIndex: integer; const aValue: TXmlAttribute);
protected
function Add: TXmlAttribute; overload;
public
constructor Create(aOwner: TXmlNode); virtual;
destructor Destroy; override;
procedure Assign(aSource: TPersistent); override;
property Owner: TXmlNode read FOwner;
procedure Add(const aName, aValue: string); overload;
procedure Insert(const aName, aValue: string; const aIndex: integer);
procedure Delete(const aName: string); overload;
procedure Delete(const aIndex: integer); overload;
procedure Clear;
procedure Exchange(const aIndexA, aIndexB: integer);
procedure Move(const aFromIdx, aToIdx: integer);
function First: TXmlAttribute;
function Last: TXmlAttribute;
function Find(const aName: string): TXmlAttribute;
function IndexOf(const aName: string): integer; overload;
function IndexOf(aAttribute: TXmlAttribute): integer; overload;
function AttributeExists(const aName: string): boolean;
property Attributes[const aName: string]: TXmlAttribute read GetByName write SetByName;
property Items[const aIndex: integer]: TXmlAttribute read GetByIndex write SetByIndex; default;
property Count: integer read GetCount;
end;
{ TXmlNode }
TXmlNode = class(TPersistent)
private
FOwner: TXmlNodes;
FText : string;
FAttributes: TXmlAttributes;
FName : string;
FNodes : TXmlNodes;
procedure SetText(const aValue: string);
procedure SetAttributes(const aValue: TXmlAttributes);
procedure SetName(const aValue: string);
procedure SetNodes(const aValue: TXmlNodes);
public
constructor Create(aOwner: TXmlNodes); virtual;
destructor Destroy; override;
procedure Assign(aSource: TPersistent); override;
property Owner: TXmlNodes read FOwner;
published
property Attributes: TXmlAttributes read FAttributes write SetAttributes;
property Nodes : TXmlNodes read FNodes write SetNodes;
property name : string read FName write SetName;
property Text : string read FText write SetText;
end;
{ TXmlNodes }
TXmlNodes = class(TPersistent)
private
FOwner: TXmlNode;
FList: TList;
function GetByIndex(const aIndex: integer): TXmlNode;
function GetByName(const aName: string): TXmlNode;
function GetCount: integer;
procedure SetByIndex(const aIndex: integer; const aValue: TXmlNode);
procedure SetByName(const aName: string; const aValue: TXmlNode);
protected
function Add: TXmlNode; overload;
public
constructor Create(aOwner: TXmlNode); virtual;
destructor Destroy; override;
procedure Assign(aSource: TPersistent); override;
property Owner: TXmlNode read FOwner;
function Add(const aName: string; aText: string = CEmpty): TXmlNode; overload;
function Insert(const aName, aText: string; const aIndex: integer): TXmlNode;
procedure Delete(const aName: string); overload;
procedure Delete(const aIndex: integer); overload;
procedure Clear;
procedure Exchange(const aIndexA, aIndexB: integer);
procedure Move(const aFromIdx, aToIdx: integer);
function First: TXmlNode;
function Last: TXmlNode;
function Find(const aName: string): TXmlNode;
function IndexOf(const aName: string): integer; overload;
function IndexOf(aNode: TXmlNode): integer; overload;
function NodeExists(const aName: string): boolean;
property Nodes[const aName: string]: TXmlNode read GetByName write SetByName;
property Items[const aIndex: integer]: TXmlNode read GetByIndex write SetByIndex; default;
property Count: integer read GetCount;
end;
{ TXmlHeader }
TXmlHeader = class(TPersistent)
private
FAttributes: TXmlAttributes;
procedure SetAttributes(const aValue: TXmlAttributes);
public
constructor Create; virtual;
destructor Destroy; override;
procedure Assign(aSource: TPersistent); override;
property Attributes: TXmlAttributes read FAttributes write SetAttributes;
end;
{ TXml }
TXml = class(TPersistent)
private
FHeader : TXmlHeader;
FRoot : TXmlNode;
FIndentString: string;
procedure SetRoot(const aValue: TXmlNode);
procedure SetIndentString(const aValue: string);
procedure SetHeader(const aValue: TXmlHeader);
protected
procedure Malformed(const aLine: integer);
procedure Parse(aReader: TStreamReader);
procedure Serialize(aWriter: TStreamWriter);
public
constructor Create; virtual;
destructor Destroy; override;
procedure Assign(aSource: TPersistent); override;
procedure Clear;
procedure LoadFromFile(const aFileName: string);
procedure LoadFromStream(aStream: TStream);
procedure SaveToFile(const aFileName: string);
procedure SaveToStream(aStream: TStream);
property Header: TXmlHeader read FHeader write SetHeader;
property Root: TXmlNode read FRoot write SetRoot;
property IndentString: string read FIndentString write SetIndentString;
end;
implementation
{ TXmlAttribute }
constructor TXmlAttribute.Create(aOwner: TXmlAttributes);
begin
FOwner := aOwner;
end;
procedure TXmlAttribute.Assign(aSource: TPersistent);
begin
if (aSource is TXmlAttribute) then
begin
name := TXmlAttribute(aSource).name;
Value := TXmlAttribute(aSource).Value;
end;
end;
procedure TXmlAttribute.SetName(const aValue: string);
begin
if (FName = aValue) then
Exit;
FName := aValue;
end;
procedure TXmlAttribute.SetValue(const aValue: string);
begin
if (FValue = aValue) then
Exit;
FValue := aValue;
end;
{ TXmlAttributes }
constructor TXmlAttributes.Create(aOwner: TXmlNode);
begin
FOwner := aOwner;
FList := TList.Create;
end;
destructor TXmlAttributes.Destroy;
begin
Clear;
FList.Free;
inherited;
end;
procedure TXmlAttributes.Assign(aSource: TPersistent);
var
i: integer;
begin
if (aSource is TXmlAttributes) then
begin
Self.Clear;
for i := 0 to TXmlAttributes(aSource).Count - 1 do
Self.Add.Assign(TXmlAttributes(aSource).Items[i]);
end;
end;
function TXmlAttributes.Add: TXmlAttribute;
begin
Result := TXmlAttribute.Create(Self);
FList.Add(Result);
end;
procedure TXmlAttributes.Add(const aName, aValue: string);
begin
Insert(aName, aValue, FList.Count);
end;
procedure TXmlAttributes.Insert(const aName, aValue: string; const aIndex: integer);
var
attr: TXmlAttribute;
begin
attr := TXmlAttribute.Create(Self);
attr.name := aName;
attr.Value := aValue;
FList.Insert(aIndex, attr);
end;
procedure TXmlAttributes.Delete(const aName: string);
var
i: integer;
begin
i := IndexOf(aName);
if (i = - 1) then
raise EAttributeNotFound.CreateFmt(SAttributeNotFound, [aName]);
TXmlAttribute(FList[i]).Free;
FList.Delete(i);
end;
procedure TXmlAttributes.Delete(const aIndex: integer);
begin
TXmlAttribute(FList[aIndex]).Free;
FList.Delete(aIndex);
end;
procedure TXmlAttributes.Clear;
var
i: integer;
begin
for i := FList.Count - 1 downto 0 do
begin
TXmlAttribute(FList[i]).Free;
FList.Delete(i);
end;
end;
procedure TXmlAttributes.Exchange(const aIndexA, aIndexB: integer);
begin
FList.Exchange(aIndexA, aIndexB);
end;
procedure TXmlAttributes.Move(const aFromIdx, aToIdx: integer);
begin
FList.Move(aFromIdx, aToIdx);
end;
function TXmlAttributes.Find(const aName: string): TXmlAttribute;
var
i: integer;
begin
Result := nil;
for i := 0 to FList.Count - 1 do
if (SameText(TXmlAttribute(FList[i]).name, aName)) then
Exit(TXmlAttribute(FList[i]));
end;
function TXmlAttributes.IndexOf(const aName: string): integer;
var
i: integer;
begin
Result := - 1;
for i := 0 to FList.Count - 1 do
if (SameText(TXmlAttribute(FList[i]).name, aName)) then
Exit(i);
end;
function TXmlAttributes.AttributeExists(const aName: string): boolean;
begin
Result := (IndexOf(aName) > - 1);
end;
function TXmlAttributes.IndexOf(aAttribute: TXmlAttribute): integer;
var
i: integer;
begin
Result := - 1;
for i := 0 to FList.Count - 1 do
if (TXmlAttribute(FList[i]) = aAttribute) then
Exit(i);
end;
function TXmlAttributes.First: TXmlAttribute;
begin
if (FList.Count = 0) then
Exit(nil);
Result := TXmlAttribute(FList[0]);
end;
function TXmlAttributes.Last: TXmlAttribute;
begin
if (FList.Count = 0) then
Exit(nil);
Result := TXmlAttribute(FList[FList.Count - 1]);
end;
function TXmlAttributes.GetCount: integer;
begin
Result := FList.Count;
end;
function TXmlAttributes.GetByIndex(const aIndex: integer): TXmlAttribute;
begin
Result := TXmlAttribute(FList[aIndex]);
end;
function TXmlAttributes.GetByName(const aName: string): TXmlAttribute;
begin
Result := Find(aName);
end;
procedure TXmlAttributes.SetByIndex(const aIndex: integer; const aValue: TXmlAttribute);
begin
TXmlAttribute(FList[aIndex]).Assign(aValue);
end;
procedure TXmlAttributes.SetByName(const aName: string; const aValue: TXmlAttribute);
var
attr: TXmlAttribute;
begin
attr := Find(aName);
if (attr = nil) then
raise EAttributeNotFound.CreateFmt(SAttributeNotFound, [aName]);
attr.Assign(aValue);
end;
{ TXmlNode }
constructor TXmlNode.Create(aOwner: TXmlNodes);
begin
FOwner := aOwner;
FAttributes := TXmlAttributes.Create(Self);
FNodes := TXmlNodes.Create(Self);
end;
destructor TXmlNode.Destroy;
begin
FNodes.Free;
FAttributes.Free;
inherited;
end;
procedure TXmlNode.Assign(aSource: TPersistent);
begin
if (aSource is TXmlNode) then
begin
name := TXmlNode(aSource).name;
Text := TXmlNode(aSource).Text;
Attributes.Assign(TXmlNode(aSource).Attributes);
Nodes.Assign(TXmlNode(aSource).Nodes);
end;
end;
procedure TXmlNode.SetAttributes(const aValue: TXmlAttributes);
begin
FAttributes.Assign(aValue);
end;
procedure TXmlNode.SetName(const aValue: string);
begin
if (FName = aValue) then
Exit;
FName := aValue;
end;
procedure TXmlNode.SetNodes(const aValue: TXmlNodes);
begin
FNodes.Assign(aValue);
end;
procedure TXmlNode.SetText(const aValue: string);
begin
if (FText = aValue) then
Exit;
FText := aValue;
end;
{ TXmlNodes }
constructor TXmlNodes.Create(aOwner: TXmlNode);
begin
FOwner := aOwner;
FList := TList.Create;
end;
destructor TXmlNodes.Destroy;
begin
Clear;
FList.Free;
inherited;
end;
procedure TXmlNodes.Assign(aSource: TPersistent);
var
i: integer;
begin
if (aSource is TXmlNodes) then
begin
Self.Clear;
for i := 0 to TXmlNodes(aSource).Count - 1 do
Self.Add.Assign(TXmlNodes(aSource).Items[i]);
end;
end;
function TXmlNodes.Add: TXmlNode;
begin
Result := TXmlNode.Create(Self);
FList.Add(Result);
end;
function TXmlNodes.Add(const aName: string; aText: string): TXmlNode;
begin
Result := Insert(aName, aText, FList.Count);
end;
function TXmlNodes.Insert(const aName, aText: string; const aIndex: integer): TXmlNode;
begin
Result := TXmlNode.Create(Self);
Result.name := aName;
Result.Text := aText;
FList.Insert(aIndex, Result);
end;
procedure TXmlNodes.Delete(const aName: string);
var
i: integer;
begin
i := IndexOf(aName);
if (i = - 1) then
raise ENodeNotFound.CreateFmt(SNodeNotFound, [aName]);
TXmlNode(FList[i]).Free;
FList.Delete(i);
end;
procedure TXmlNodes.Delete(const aIndex: integer);
begin
TXmlNode(FList[aIndex]).Free;
FList.Delete(aIndex);
end;
procedure TXmlNodes.Clear;
var
i: integer;
begin
for i := FList.Count - 1 downto 0 do
begin
TXmlNode(FList[i]).Free;
FList.Delete(i);
end;
end;
procedure TXmlNodes.Exchange(const aIndexA, aIndexB: integer);
begin
FList.Exchange(aIndexA, aIndexB);
end;
procedure TXmlNodes.Move(const aFromIdx, aToIdx: integer);
begin
FList.Move(aFromIdx, aToIdx);
end;
function TXmlNodes.Find(const aName: string): TXmlNode;
var
i: integer;
begin
Result := nil;
for i := 0 to FList.Count - 1 do
if (SameText(TXmlNode(FList[i]).name, aName)) then
Exit(TXmlNode(FList[i]));
end;
function TXmlNodes.IndexOf(const aName: string): integer;
var
i: integer;
begin
Result := - 1;
for i := 0 to FList.Count - 1 do
if (SameText(TXmlNode(FList[i]).name, aName)) then
Exit(i);
end;
function TXmlNodes.IndexOf(aNode: TXmlNode): integer;
var
i: integer;
begin
Result := - 1;
for i := 0 to FList.Count - 1 do
if (TXmlNode(FList[i]) = aNode) then
Exit(i);
end;
function TXmlNodes.NodeExists(const aName: string): boolean;
begin
Result := (IndexOf(aName) <> - 1);
end;
function TXmlNodes.First: TXmlNode;
begin
if (FList.Count = 0) then
Exit(nil);
Result := TXmlNode(FList[0]);
end;
function TXmlNodes.Last: TXmlNode;
begin
if (FList.Count = 0) then
Exit(nil);
Result := TXmlNode(FList[FList.Count - 1]);
end;
function TXmlNodes.GetCount: integer;
begin
Result := FList.Count;
end;
function TXmlNodes.GetByIndex(const aIndex: integer): TXmlNode;
begin
Result := TXmlNode(FList[aIndex]);
end;
function TXmlNodes.GetByName(const aName: string): TXmlNode;
begin
Result := Find(aName);
end;
procedure TXmlNodes.SetByIndex(const aIndex: integer; const aValue: TXmlNode);
begin
TXmlNode(FList[aIndex]).Assign(aValue);
end;
procedure TXmlNodes.SetByName(const aName: string; const aValue: TXmlNode);
var
node: TXmlNode;
begin
node := Find(aName);
if (node = nil) then
raise ENodeNotFound.CreateFmt(SNodeNotFound, [aName]);
node.Assign(aValue);
end;
{ TXmlHeader }
constructor TXmlHeader.Create;
begin
FAttributes := TXmlAttributes.Create(nil);
end;
destructor TXmlHeader.Destroy;
begin
FAttributes.Free;
inherited;
end;
procedure TXmlHeader.Assign(aSource: TPersistent);
begin
if (aSource is TXmlHeader) then
begin
Attributes.Assign(TXmlHeader(aSource).Attributes);
end;
end;
procedure TXmlHeader.SetAttributes(const aValue: TXmlAttributes);
begin
FAttributes.Assign(aValue);
end;
{ TXml }
constructor TXml.Create;
begin
FHeader := TXmlHeader.Create;
FRoot := TXmlNode.Create(nil);
FIndentString := #9;
end;
destructor TXml.Destroy;
begin
FRoot.Free;
FHeader.Free;
inherited;
end;
procedure TXml.Assign(aSource: TPersistent);
begin
if (aSource is TXml) then
begin
Header.Assign(TXml(aSource).Header);
Root.Assign(TXml(aSource).Root);
IndentString := TXml(aSource).IndentString;
end;
end;
procedure TXml.Clear;
begin
Root.Nodes.Clear;
Root.Attributes.Clear;
Root.name := '';
Root.Text := '';
end;
procedure TXml.Malformed(const aLine: integer);
begin
Clear;
raise EMalformedXml.CreateFmt(SMalformedXml, [aLine]);
end;
procedure TXml.Parse(aReader: TStreamReader);
var
lineIdx: integer;
procedure ParseAttributes(const aText: string; aAttributes: TXmlAttributes);
var
tokens: TArray<string>;
token : string;
key : string;
Value : string;
begin
// Split into an array of attribute="value" tokens.
tokens := TextSplit(aText, CSpace, CDoubleQuote, [soCSSep, soCSQot, soQuoted]);
for token in tokens do
begin
// Skip Node name.
if (TextPos(token, CEquals, True) = 0) then
Continue;
key := Trim(TextFetchLeft(token, CEquals, True));
Value := Trim(TextFetchRight(token, CEquals, True));
if (key = EmptyStr) then
Malformed(lineIdx);
if (TextEnclosed(Value, CDoubleQuote, True) = False) then
Malformed(lineIdx);
aAttributes.Add(key, TextUnquote(Value));
end;
end;
var
lineStr : string;
lineElements: TArray<string>;
lineElement : string;
tempInt : integer;
tempStr : string;
currNode : TXmlNode;
tempNode : TXmlNode;
headerExpected : boolean;
nodeTextExpected: boolean;
inComment : boolean;
inText : boolean;
begin
Clear;
lineIdx := 0;
currNode := FRoot;
headerExpected := True;
nodeTextExpected := False;
inComment := False;
inText := False;
while (aReader.EndOfStream = False) do
begin
lineStr := Trim(aReader.ReadLine);
Inc(lineIdx);
// Check for multi line comment end, and remove it from lineStr.
if (inComment) then
begin
tempInt := TextPos(lineStr, '-->', True);
if (tempInt <> 0) then
begin
lineStr := TextCopy(lineStr, tempInt + 3, MaxInt);
inComment := False;
if (lineStr = CEmpty) then
Continue;
end
else
Continue;
end;
// If there are multiple tags/texts/comments on a lineStr, split it.
lineElements := TextSplitMarkup(lineStr);
for lineElement in lineElements do
begin
if (TextEnclosed(lineElement, '<', '>', True)) then
begin
tempStr := TextUnEnclose(lineElement, '<', '>', True);
// Check if its a header.
if (TextEnclosed(tempStr, '?xml', '?')) then
begin
if (headerExpected = False) then
Malformed(lineIdx);
if (inText) then
Malformed(lineIdx);
ParseAttributes(TextUnEnclose(tempStr, '?xml', '?'), FHeader.Attributes);
headerExpected := False;
end
// Check if its a comment.
else if (TextEnclosed(tempStr, '!--', '--')) then
begin
{ Skip, we wont load comments. }
Continue;
end
// Check if its self ending tag.
else if (TextEnds(tempStr, '/', True)) then
begin
tempNode := currNode.Nodes.Add(TextFetchLeft(tempStr, CSpace, True), CEmpty);
if (tempNode.name = CEmpty) then
Malformed(lineIdx);
if (inText) then
Malformed(lineIdx);
ParseAttributes(TextUnEnclose(tempStr, '', '/', True), tempNode.Attributes);
end
// Closing tag
else if (TextBegins(tempStr, '/', True)) then
begin
if (currNode <> FRoot) then
currNode := currNode.Owner.Owner;
if (currNode = nil) then
Malformed(lineIdx);
inText := False;
nodeTextExpected := False;
end
// Should be an opening tag.
else
begin
if (inText) then
Malformed(lineIdx);
if (TextPos(tempStr, CSpace, True) <> 0) then
begin
if (currNode = FRoot) and (currNode.name = EmptyStr) then
begin
FRoot.name := TextFetchLeft(tempStr, CSpace, True);
if (FRoot.name = EmptyStr) then
Malformed(lineIdx);
ParseAttributes(tempStr, FRoot.Attributes);
end
else
begin
currNode := currNode.Nodes.Add(TextFetchLeft(tempStr, CSpace, True), CEmpty);
if (currNode.name = CEmpty) then
Malformed(lineIdx);
ParseAttributes(tempStr, currNode.Attributes);
end;
end
else
begin
if (currNode = FRoot) and (currNode.name = EmptyStr) then
begin
FRoot.name := tempStr;
if (FRoot.name = EmptyStr) then
Malformed(lineIdx);
end
else
begin
currNode := currNode.Nodes.Add(tempStr, CEmpty);
if (currNode.name = CEmpty) then
Malformed(lineIdx);
end;
end;
nodeTextExpected := True;
end;
end
else
begin
// Check for start of multi line comment.
if (TextBegins(lineElement, '<!--', True)) then
begin
inComment := True;
end
// Should be node text content.
else if (inComment = False) then
begin
if (nodeTextExpected = False) then
Malformed(lineIdx);
inText := True;
currNode.Text := currNode.Text + CCrLf + lineElement;
end
else
Malformed(lineIdx);
end;
end;
end;
end;
procedure TXml.Serialize(aWriter: TStreamWriter);
var
indentDepth: integer;
function GetIndentString: string;
var
i: integer;
begin
Result := '';
for i := 0 to indentDepth - 1 do
Result := Result + FIndentString;
end;
function GetAttributesText(aAttributes: TXmlAttributes): string;
var
i: integer;
begin
Result := '';
for i := 0 to aAttributes.Count - 1 do
begin
Result := Result + aAttributes[i].name + '="' + aAttributes[i].Value + '"';
if (i <> aAttributes.Count - 1) then
Result := Result + ' ';
end;
end;
procedure WriteNode(aNode: TXmlNode);
var
str : string;
i : integer;
needsClose: boolean;
begin
needsClose := False;
if (aNode.Attributes.Count > 0) then
str := GetIndentString + '<' + aNode.name + ' ' + GetAttributesText(aNode.Attributes)
else
str := GetIndentString + '<' + aNode.name;
if (aNode.Text = EmptyStr) and (aNode.Nodes.Count = 0) then
str := str + ' />'
else if (aNode.Text <> EmptyStr) then
begin
str := str + '>' + aNode.Text + '</' + aNode.name + '>';
if (aNode.Nodes.Count > 0) then
raise EXml.Create(SCantHaveTextAndChildren);
end
else
str := str + '>';
aWriter.WriteLine(str);
if (aNode.Nodes.Count > 0) then
begin
needsClose := True;
Inc(indentDepth);
for i := 0 to aNode.Nodes.Count - 1 do
WriteNode(aNode.Nodes[i]);
Dec(indentDepth);
end;
if (needsClose) then
aWriter.WriteLine(GetIndentString + '</' + aNode.name + '>');
end;
begin
indentDepth := 0;
aWriter.WriteLine('<?xml ' + GetAttributesText(FHeader.Attributes) + ' ?>');
WriteNode(FRoot);
end;
procedure TXml.LoadFromFile(const aFileName: string);
var
str: TFileStream;
begin
str := TFileStream.Create(aFileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(str);
finally
str.Free;
end;
end;
procedure TXml.LoadFromStream(aStream: TStream);
var
reader: TStreamReader;
begin
reader := TStreamReader.Create(aStream);
try
Parse(reader);
finally
reader.Free;
end;
end;
procedure TXml.SaveToFile(const aFileName: string);
var
str: TFileStream;
begin
str := TFileStream.Create(aFileName, fmOpenWrite or fmCreate or fmShareDenyWrite);
try
SaveToStream(str);
finally
str.Free;
end;
end;
procedure TXml.SaveToStream(aStream: TStream);
var
writer: TStreamWriter;
begin