-
Notifications
You must be signed in to change notification settings - Fork 0
/
OmniXMLPersistent.pas
770 lines (693 loc) · 24 KB
/
OmniXMLPersistent.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
(*******************************************************************************
* The contents of this file are 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/ *
* *
* 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. *
* *
* The Original Code is mr_Storage_XML.pas *
* *
* The Initial Developer of the Original Code is Miha Remec, *
* http://www.MihaRemec.com/ *
* *
* Contributor(s): *
* Miha Vrhovnik, Primoz Gabrijelcic, John *
*******************************************************************************)
unit OmniXMLPersistent;
interface
{$I OmniXML.inc}
{$IFDEF OmniXML_HasZeroBasedStrings}
{$ZEROBASEDSTRINGS OFF}
{$ENDIF}
// if you want to use MS XML parser, uncomment (in your program!!!)
// the following compiler directive {.$DEFINE USE_MSXML}
uses
Classes, SysUtils,
{$IFDEF VCL} Controls, {$ENDIF}
TypInfo,
{$IFDEF HAS_UNIT_VARIANTS} Variants, {$ENDIF}
OEncoding, OmniXML, OmniXML_Types,
{$IFDEF USE_MSXML} OmniXML_MSXML, {$ENDIF}
OmniXMLUtils;
type
TPropsFormat = (pfAuto, pfAttributes, pfNodes);
EOmniXMLPersistent = class(Exception);
{$IFDEF VisualCLX}
TTime = type TDateTime;
TDate = type TDateTime;
{$ENDIF}
type
TOmniXMLWriter = class
protected
Doc: IXMLDocument;
procedure WriteProperty(Instance: TPersistent; PropInfo: PPropInfo;
Element: IXMLElement; WriteDefaultValues: Boolean);
procedure InternalWriteText(Root: IXMLElement; Name, Value: XmlString);
procedure WriteCollection(Collection: TCollection; Root: IXMLElement);
public
PropsFormat: TPropsFormat;
constructor Create(Doc: IXMLDocument; const PropFormat: TPropsFormat = pfAuto);
class procedure SaveToFile(const Instance: TPersistent; const FileName: string;
const PropFormat: TPropsFormat = pfAuto; const OutputFormat: TOutputFormat = ofNone);
class procedure SaveXML(const Instance: TPersistent; var XML: XmlString;
const PropFormat: TPropsFormat = pfAuto; const OutputFormat: TOutputFormat = ofNone);
procedure Write(Instance: TPersistent; Root: IXMLElement;
const WriteRoot: Boolean = True; const CheckIfEmpty: Boolean = True;
const WriteDefaultValues: Boolean = False);
end;
TOmniXMLReader = class
protected
function FindElement(const Root: IXMLElement; const TagName: XmlString): IXMLElement;
procedure ReadProperty(Instance: TPersistent; PropInfo: Pointer; Element: IXMLElement);
function InternalReadText(Root: IXMLElement; Name: XmlString; var Value: XmlString): Boolean;
procedure ReadCollection(Collection: TCollection; Root: IXMLElement);
public
PropsFormat: TPropsFormat;
constructor Create(const PropFormat: TPropsFormat = pfAuto);
class procedure LoadFromFile(Instance: TPersistent; FileName: string); overload;
class procedure LoadFromFile(Collection: TCollection; FileName: string); overload;
class procedure LoadXML(Instance: TPersistent; const XML: XmlString); overload;
procedure Read(Instance: TPersistent; Root: IXMLElement; const ReadRoot: Boolean = False);
end;
var
DefaultPropFormat: TPropsFormat = pfNodes;
implementation
{$IFDEF UNICODE}
const
CP_ACP = 0; // default to ANSI code page
CP_RawByteString = $FFFF; // codepage of RawByteString string type
{$ENDIF} // UNICODE
const
COLLECTIONITEM_NODENAME = 'o'; // do not change!
PROP_FORMAT = 'PropFormat'; // do not change!
StringS_COUNT_NODENAME = 'Count'; // do not change!
StringS_PREFIX = 'l'; // do not change!
var
PropFormatValues: array[TPropsFormat] of string = ('auto', 'attr', 'node');
function IsElementEmpty(Element: IXMLElement; PropsFormat: TPropsFormat): Boolean;
begin
Result := ((PropsFormat = pfAttributes) and (Element.Attributes.Length = 0)) or
((PropsFormat = pfNodes) and (Element.ChildNodes.Length = 0));
end;
procedure CreateDocument(var XMLDoc: IXMLDocument; var Root: IXMLElement;
RootNodeName: XmlString);
begin
XMLDoc := CreateXMLDoc;
Root := XMLDoc.CreateElement(RootNodeName);
XMLDoc.DocumentElement := Root;
end;
procedure Load(var XMLDoc: IXMLDocument; var XMLRoot: IXMLElement;
var PropsFormat: TPropsFormat);
var
i: TPropsFormat;
PropFormatValue: XmlString;
begin
// set root element
XMLRoot := XMLDoc.documentElement;
PropsFormat := pfNodes;
if XMLRoot = nil then
Exit;
PropFormatValue := XMLRoot.GetAttribute(PROP_FORMAT);
for i := Low(TPropsFormat) to High(TPropsFormat) do begin
if SameText(PropFormatValue, PropFormatValues[i]) then begin
PropsFormat := i;
Break;
end;
end;
end;
procedure LoadDocument(const FileName: string; var XMLDoc: IXMLDocument;
var XMLRoot: IXMLElement; var PropsFormat: TPropsFormat);
begin
XMLDoc := CreateXMLDoc;
{ TODO : implement and test preserveWhiteSpace }
XMLDoc.preserveWhiteSpace := True;
XMLDoc.Load(FileName);
Load(XMLDoc, XMLRoot, PropsFormat);
end;
{ TOmniXMLWriter }
class procedure TOmniXMLWriter.SaveToFile(const Instance: TPersistent;
const FileName: string; const PropFormat: TPropsFormat = pfAuto;
const OutputFormat: TOutputFormat = ofNone);
var
XMLDoc: IXMLDocument;
Root: IXMLElement;
Writer: TOmniXMLWriter;
begin
if Instance is TCollection then
CreateDocument(XMLDoc, Root, Instance.ClassName)
else
CreateDocument(XMLDoc, Root, 'data');
Writer := TOmniXMLWriter.Create(XMLDoc, PropFormat);
try
if Instance is TCollection then
Writer.WriteCollection(TCollection(Instance), Root)
else
Writer.Write(Instance, Root);
finally
Writer.Free;
end;
{$IFNDEF USE_MSXML}
XMLDoc.Save(FileName, OutputFormat);
{$ELSE}
XMLDoc.Save(FileName);
{$ENDIF}
end;
class procedure TOmniXMLWriter.SaveXML(const Instance: TPersistent; var XML: XmlString;
const PropFormat: TPropsFormat; const OutputFormat: TOutputFormat);
var
XMLDoc: IXMLDocument;
Root: IXMLElement;
Writer: TOmniXMLWriter;
begin
if Instance is TCollection then
CreateDocument(XMLDoc, Root, Instance.ClassName)
else
CreateDocument(XMLDoc, Root, 'data');
Writer := TOmniXMLWriter.Create(XMLDoc, PropFormat);
try
if Instance is TCollection then
Writer.WriteCollection(TCollection(Instance), Root)
else
Writer.Write(Instance, Root);
finally
Writer.Free;
end;
XML := XMLDoc.XML;
end;
constructor TOmniXMLWriter.Create(Doc: IXMLDocument; const PropFormat: TPropsFormat = pfAuto);
begin
Self.Doc := Doc;
if PropFormat <> pfAuto then
PropsFormat := PropFormat
else
PropsFormat := DefaultPropFormat;
Doc.DocumentElement.SetAttribute(PROP_FORMAT, PropFormatValues[PropsFormat]);
end;
procedure TOmniXMLWriter.InternalWriteText(Root: IXMLElement; Name, Value: XmlString);
var
PropNode: IXMLElement;
begin
case PropsFormat of
pfAttributes: Root.SetAttribute(Name, Value);
pfNodes:
begin
PropNode := Doc.CreateElement(Name);
PropNode.Text := Value;
Root.appendChild(PropNode);
end;
end;
end;
procedure TOmniXMLWriter.WriteCollection(Collection: TCollection; Root: IXMLElement);
var
i: Integer;
begin
for i := 0 to Collection.Count - 1 do
Write(Collection.Items[i], Root, True, False);
end;
procedure TOmniXMLWriter.WriteProperty(Instance: TPersistent; PropInfo: PPropInfo;
Element: IXMLElement; WriteDefaultValues: Boolean);
var
PropType: PTypeInfo;
procedure WriteStrProp;
var
Value: XmlString;
begin
if PropType^.Kind = tkWString then
Value := GetWideStrProp(Instance, PropInfo)
else
Value := GetStrProp(Instance, PropInfo);
if Value <> '' then
InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), Value);
end;
procedure WriteOrdProp;
var
Value: Longint;
begin
Value := GetOrdProp(Instance, PropInfo);
if WriteDefaultValues or (Value <> PPropInfo(PropInfo)^.Default) then begin
case PropType^.Kind of
tkInteger: InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), XMLIntToStr(Value));
tkChar:
begin
{$IFDEF UNICODE}
InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), Char(Value));
{$ELSE}
InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), UTF8Decode(AnsiToUtf8(Char(Value))));
{$ENDIF} // UNICODE
end;
tkWChar: InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), WideChar(Value));
tkSet: InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name),
GetSetProp(Instance, PPropInfo(PropInfo), True));
tkEnumeration:
begin
if PropType = System.TypeInfo(Boolean) then
InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), XMLBoolToStr(Boolean(Value)))
else if PropType^.Kind = tkInteger then
InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), XMLIntToStr(Value))
// 2003-05-27 (mr): added tkEnumeration processing
else if PropType^.Kind = tkEnumeration then
InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), GetEnumName(PropType, Value));
end;
end;
end;
end;
procedure WriteFloatProp;
var
Value: Real;
begin
Value := GetFloatProp(Instance, PropInfo);
if Value <> 0 then
InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), XMLRealToStr(Value));
end;
procedure WriteDateTimeProp;
var
Value: TDateTime;
begin
Value := VarAsType(GetFloatProp(Instance, PropInfo), varDate);
if Value <> 0 then
InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), XMLDateTimeToStrEx(Value));
end;
procedure WriteInt64Prop;
var
Value: Int64;
begin
Value := GetInt64Prop(Instance, PropInfo);
if Value <> 0 then
InternalWriteText(Element, XmlString(PPropInfo(PropInfo)^.Name), XMLInt64ToStr(Value));
end;
procedure WriteObjectProp;
var
Value: TObject;
PropNode: IXMLElement;
procedure WriteStrings(const Strings: TStrings);
var
i: Integer;
begin
SetNodeAttrInt(PropNode, StringS_COUNT_NODENAME, Strings.Count);
for i := 0 to Strings.Count - 1 do begin
if Strings[i] <> '' then
InternalWriteText(PropNode, StringS_PREFIX + IntToStr(i), Strings[i]);
end;
end;
begin
Value := TObject(GetOrdProp(Instance, PropInfo));
if (Value <> nil) and (Value is TPersistent) then begin
PropNode := Doc.CreateElement(XmlString(PPropInfo(PropInfo)^.Name));
// write object's properties
Write(TPersistent(Value), PropNode, False, True, WriteDefaultValues);
if Value is TCollection then begin
WriteCollection(TCollection(Value), PropNode);
if not IsElementEmpty(PropNode, pfNodes) then
Element.AppendChild(PropNode);
end
else if Value is TStrings then begin
WriteStrings(TStrings(Value));
Element.AppendChild(PropNode);
end
else if not IsElementEmpty(PropNode, PropsFormat) then
Element.AppendChild(PropNode);
end;
end;
begin
if (PPropInfo(PropInfo)^.GetProc <> nil) then begin
PropType := {$IFDEF FPC}@{$ENDIF}PPropInfo(PropInfo).PropType{$IFNDEF FPC}^{$ENDIF};
case PropType^.Kind of
tkInteger, tkChar, tkWChar, tkEnumeration, tkSet: WriteOrdProp;
tkString, tkLString, tkWString {$IFDEF UNICODE}, tkUString {$ENDIF}: WriteStrProp;
tkFloat:
if (PropType = System.TypeInfo(TDateTime)) or (PropType = System.TypeInfo(TTime))
or (PropType = System.TypeInfo(TDate)) then
WriteDateTimeProp
else
WriteFloatProp;
tkInt64: WriteInt64Prop;
tkClass: WriteObjectProp;
end;
end;
end;
procedure TOmniXMLWriter.Write(Instance: TPersistent; Root: IXMLElement;
const WriteRoot: Boolean; const CheckIfEmpty: boolean; const WriteDefaultValues: Boolean);
var
PropCount: Integer;
PropList: PPropList;
i: Integer;
PropInfo: PPropInfo;
Element: IXMLElement;
begin
PropCount := GetTypeData(Instance.ClassInfo)^.PropCount;
if PropCount = 0 then
Exit;
if Instance is TCollectionItem then
Element := Doc.CreateElement(COLLECTIONITEM_NODENAME)
else if WriteRoot then
Element := Doc.CreateElement(Instance.ClassName)
else
Element := Root;
GetMem(PropList, PropCount * SizeOf(Pointer));
try
GetPropInfos(Instance.ClassInfo, PropList);
for i := 0 to PropCount - 1 do begin
PropInfo := PropList^[I];
if PropInfo = nil then
Break;
if IsStoredProp(Instance, PropInfo) then
WriteProperty(Instance, PropInfo, Element, WriteDefaultValues)
end;
finally
FreeMem(PropList, PropCount * SizeOf(Pointer));
end;
if WriteRoot then begin
if CheckIfEmpty and IsElementEmpty(Element, PropsFormat) then
Exit
else begin
if Root <> nil then
Root.appendChild(Element)
else
Doc.documentElement := Element;
end;
end;
end;
{ TOmniXMLReader }
class procedure TOmniXMLReader.LoadXML(Instance: TPersistent; const XML: XmlString);
var
XMLDoc: IXMLDocument;
XMLRoot: IXMLElement;
Reader: TOmniXMLReader;
PropsFormat: TPropsFormat;
begin
XMLDoc := CreateXMLDoc;
{ TODO : implement and test preserveWhiteSpace }
XMLDoc.preserveWhiteSpace := True;
XMLDoc.LoadXML(XML);
Load(XMLDoc, XMLRoot, PropsFormat);
Reader := TOmniXMLReader.Create(PropsFormat);
try
if Instance is TCollection then
Reader.ReadCollection(TCollection(Instance), XMLRoot)
else
Reader.Read(Instance, XMLRoot, True);
finally
Reader.Free;
end;
end;
class procedure TOmniXMLReader.LoadFromFile(Instance: TPersistent; FileName: string);
var
XMLDoc: IXMLDocument;
XMLRoot: IXMLElement;
Reader: TOmniXMLReader;
PropsFormat: TPropsFormat;
begin
// read document
LoadDocument(FileName, XMLDoc, XMLRoot, PropsFormat);
Reader := TOmniXMLReader.Create(PropsFormat);
try
if Instance is TCollection then
Reader.ReadCollection(TCollection(Instance), XMLRoot)
else
Reader.Read(Instance, XMLRoot, True);
finally
Reader.Free;
end;
end;
class procedure TOmniXMLReader.LoadFromFile(Collection: TCollection; FileName: string);
var
XMLDoc: IXMLDocument;
XMLRoot: IXMLElement;
Reader: TOmniXMLReader;
PropsFormat: TPropsFormat;
begin
// read document
LoadDocument(FileName, XMLDoc, XMLRoot, PropsFormat);
Reader := TOmniXMLReader.Create(PropsFormat);
try
Reader.ReadCollection(Collection, XMLRoot);
finally
Reader.Free;
end;
end;
constructor TOmniXMLReader.Create(const PropFormat: TPropsFormat = pfAuto);
begin
if PropFormat = pfAuto then
raise EOmniXMLPersistent.Create('Auto PropFormat not allowed here.');
PropsFormat := PropFormat;
end;
function TOmniXMLReader.FindElement(const Root: IXMLElement; const TagName: XmlString): IXMLElement;
var
i: Integer;
begin
Result := nil;
if Root = nil then
Exit;
i := 0;
while (Result = nil) and (i < Root.ChildNodes.Length) do begin
if (Root.ChildNodes.Item[i].NodeType = ELEMENT_NODE)
and (CompareText(Root.ChildNodes.Item[i].NodeName, TagName) = 0) then
Result := Root.ChildNodes.Item[i] as IXMLElement
else
Inc(i);
end;
end;
function TOmniXMLReader.InternalReadText(Root: IXMLElement; Name: XmlString; var Value: XmlString): Boolean;
var
PropNode: IXMLElement;
AttrNode: IXMLNode;
begin
case PropsFormat of
pfAttributes:
begin
AttrNode := Root.Attributes.GetNamedItem(Name);
Result := AttrNode <> nil;
if Result then
Value := AttrNode.NodeValue;
end;
pfNodes:
begin
PropNode := FindElement(Root, Name);
Result := PropNode <> nil;
if Result then
Value := PropNode.Text;
end;
else
Result := False;
end;
end;
procedure TOmniXMLReader.ReadCollection(Collection: TCollection; Root: IXMLElement);
var
i: Integer;
Item: TCollectionItem;
begin
Collection.Clear;
if Root = nil then
Exit;
for i := 0 to Root.ChildNodes.Length - 1 do begin
if Root.ChildNodes.Item[i].NodeType = ELEMENT_NODE then begin
if Root.ChildNodes.Item[i].NodeName = COLLECTIONITEM_NODENAME then begin
Item := Collection.Add;
Read(Item, Root.ChildNodes.Item[i] as IXMLElement, False);
end;
end;
end;
end;
procedure TOmniXMLReader.ReadProperty(Instance: TPersistent; PropInfo: Pointer;
Element: IXMLElement);
var
PropType: PTypeInfo;
procedure ReadFloatProp;
var
Value: Extended;
Text: XmlString;
begin
if InternalReadText(Element, XmlString(PPropInfo(PropInfo)^.Name), Text) then
Value := XMLStrToRealDef(Text, 0)
else
Value := 0;
SetFloatProp(Instance, PropInfo, Value)
end;
procedure ReadDateTimeProp;
var
Value: TDateTime;
Text: XmlString;
begin
if InternalReadText(Element, XmlString(PPropInfo(PropInfo)^.Name), Text) then begin
if XMLStrToDateTime(Text, Value) then
SetFloatProp(Instance, PropInfo, Value)
else
raise EOmniXMLPersistent.CreateFmt('Error in datetime property %s', [PPropInfo(PropInfo)^.Name]);
end
else
SetFloatProp(Instance, PropInfo, 0); // 2004-02-02
end;
procedure ReadStrProp;
var
Value: XmlString;
{$IFDEF UNICODE}
TypeData: PTypeData;
CP: Word;
RBS: RawByteString;
{$ENDIF} // UNICODE
begin
if not InternalReadText(Element, XmlString(PPropInfo(PropInfo)^.Name), Value) then
Value := '';
case PropType^.Kind of
tkWString: SetWideStrProp(Instance, PropInfo, Value);
{$IFDEF UNICODE}
tkLString:
begin
TypeData := GetTypeData(PropType);
CP := TypeData^.CodePage;
case CP of
CP_ACP: SetAnsiStrProp(Instance, PropInfo, AnsiString(Value)); // default code page
CP_UNICODE: SetUnicodeStrProp(Instance, PropInfo, Value); // Unicode code page
else
// convert to valid codepage using RawByteString
RBS := UTF8Encode(Value);
if CP <> CP_RawByteString then
SetCodePage(RBS, CP, True);
SetAnsiStrProp(Instance, PropInfo, RBS);
end;
end;
{$ENDIF} // UNICODE
else
SetStrProp(Instance, PropInfo, Value);
end;
end;
procedure ReadOrdProp;
var
Value: XmlString;
IntValue: Integer;
BoolValue: Boolean;
begin
if InternalReadText(Element, XmlString(PPropInfo(PropInfo)^.Name), Value) then begin
case PropType^.Kind of
tkInteger:
if XMLStrToInt(Value, IntValue) then
SetOrdProp(Instance, PropInfo, XMLStrToIntDef(Value, 0))
else
raise EOmniXMLPersistent.CreateFmt('Invalid integer value (%s).', [Value]);
tkChar:
begin
{$IFDEF UNICODE}
SetOrdProp(Instance, PropInfo, Ord(Char(Value[1])));
{$ELSE}
SetOrdProp(Instance, PropInfo, Ord(Char(Utf8ToAnsi(UTF8Encode(Value))[1])));
{$ENDIF} // UNICODE
end;
tkWChar: SetOrdProp(Instance, PropInfo, Cardinal(Value[1]));
tkSet: SetSetProp(Instance, PropInfo, Value);
tkEnumeration:
begin
if PropType = System.TypeInfo(Boolean) then begin
if XMLStrToBool(Value, BoolValue) then
SetOrdProp(Instance, PropInfo, Ord(BoolValue))
else
raise EOmniXMLPersistent.CreateFmt('Invalid boolean value (%s).', [Value]);
end
else if PropType^.Kind = tkInteger then begin
if XMLStrToInt(Value, IntValue) then
SetOrdProp(Instance, PropInfo, IntValue)
else
raise EOmniXMLPersistent.CreateFmt('Invalid enum value (%s).', [Value]);
end
// 2003-05-27 (mr): added tkEnumeration processing
else if PropType^.Kind = tkEnumeration then
SetEnumProp(Instance, PropInfo, Value);
end;
end;
end
else
SetOrdProp(Instance, PropInfo, PPropInfo(PropInfo)^.Default)
end;
procedure ReadInt64Prop;
var
Value: XmlString;
IntValue: Int64;
begin
if InternalReadText(Element, XmlString(PPropInfo(PropInfo)^.Name), Value) then begin
if XMLStrToInt64(Value, IntValue) then
SetInt64Prop(Instance, PropInfo, IntValue)
else
raise EOmniXMLPersistent.CreateFmt('Invalid int64 value (%s).', [Value]);
end
else
SetFloatProp(Instance, PropInfo, 0)
end;
procedure ReadObjectProp;
var
Value: TObject;
PropNode: IXMLElement;
procedure ReadStrings(const Strings: TStrings);
var
i: Integer;
Count: Integer;
Value: XmlString;
begin
Strings.Clear;
Count := GetNodeAttrInt(PropNode, StringS_COUNT_NODENAME, 0);
for i := 0 to Count - 1 do
Strings.Add('');
for i := 0 to Strings.Count - 1 do begin
if InternalReadText(PropNode, StringS_PREFIX + IntToStr(i), Value) then
Strings[i] := Value;
end;
end;
begin
Value := TObject(GetOrdProp(Instance, PropInfo));
if (Value <> nil) and (Value is TPersistent) then begin
PropNode := FindElement(Element, XmlString(PPropInfo(PropInfo)^.Name));
Read(TPersistent(Value), PropNode);
if Value is TCollection then
ReadCollection(TCollection(Value), PropNode)
else if Value is TStrings then
ReadStrings(TStrings(Value));
end;
end;
begin
PropType := PPropInfo(PropInfo)^.PropType^;
if (PPropInfo(PropInfo)^.GetProc <> nil) and
((PropType^.Kind = tkClass) or (PPropInfo(PropInfo)^.SetProc <> nil)) and
(IsStoredProp(Instance, PPropInfo(PropInfo))) then
begin
case PropType^.Kind of
tkInteger, tkChar, tkWChar, tkEnumeration, tkSet: ReadOrdProp;
tkString, tkLString, tkWString {$IFDEF UNICODE}, tkUString {$ENDIF}: ReadStrProp;
tkFloat:
if (PropType = System.TypeInfo(TDateTime)) or (PropType = System.TypeInfo(TTime))
or (PropType = System.TypeInfo(TDate)) then
ReadDateTimeProp
else
ReadFloatProp;
tkInt64: ReadInt64Prop;
tkClass: ReadObjectProp;
end;
end;
end;
procedure TOmniXMLReader.Read(Instance: TPersistent; Root: IXMLElement; const ReadRoot: Boolean);
var
PropCount: Integer;
PropList: PPropList;
i: Integer;
PropInfo: PPropInfo;
begin
if ReadRoot then
Root := FindElement(Root, Instance.ClassName);
if Root = nil then
Exit;
PropCount := GetTypeData(Instance.ClassInfo)^.PropCount;
if PropCount > 0 then begin
GetMem(PropList, PropCount * SizeOf(Pointer));
try
GetPropInfos(Instance.ClassInfo, PropList);
for i := 0 to PropCount - 1 do begin
PropInfo := PropList^[I];
if PropInfo = nil then
Break;
ReadProperty(Instance, PropInfo, Root);
end;
finally
FreeMem(PropList, PropCount * SizeOf(Pointer));
end;
end;
end;
end.