10
dom, XMLRead,XMLWrite,
16
{ TXMLUnitResourcefileFormat }
18
TXMLUnitResourcefileFormat = class(TUnitResourcefileFormat)
20
class procedure QuickReadXML(s: TStream; out AComponentName, AClassName, ALCLVersion: string);
22
class function FindResourceDirective(Source: TObject): boolean; override;
23
class function ResourceDirectiveFilename: string; override;
24
class function GetUnitResourceFilename(AUnitFilenae: string): string; override;
25
class procedure TextStreamToBinStream(ATxtStream, ABinStream: TExtMemoryStream); override;
26
class procedure BinStreamToTextStream(ABinStream, ATextStream: TExtMemoryStream); override;
27
class function GetClassNameFromStream(s: TStream; out IsInherited: Boolean): shortstring; override;
28
class function CreateReader(s: TStream; var DestroyDriver: boolean): TReader; override;
29
class function CreateWriter(s: TStream; var DestroyDriver: boolean): TWriter; override;
30
class function QuickCheckResourceBuffer(PascalBuffer, LFMBuffer: TObject;
31
out LFMType, LFMComponentName, LFMClassName: string; out
32
LCLVersion: string; out MissingClasses: TStrings): TModalResult; override;
37
TXMLReader = class(TReader)
39
function CreateDriver(Stream: TStream; BufSize: Integer): TAbstractObjectReader; override;
44
TXMLObjectReader = class(TAbstractObjectReader)
46
FXMLDoc: TXMLDocument;
51
FReadingChilds: Boolean;
53
constructor create(AStream: TStream); virtual;
54
destructor Destroy; override;
55
function NextValue: TValueType; override;
56
function ReadValue: TValueType; override;
57
procedure BeginRootComponent; override;
58
procedure BeginComponent(var Flags: TFilerFlags; var AChildPos: Integer;
59
var CompClassName, CompName: String); override;
60
function BeginProperty: String; override;
62
//Please don't use read, better use ReadBinary whenever possible
63
procedure Read(var Buf; Count: LongInt); override;
64
{ All ReadXXX methods are called _after_ the value type has been read! }
65
procedure ReadBinary(const DestData: TMemoryStream); override;
66
function ReadCurrency: Currency; override;
67
function ReadIdent(ValueType: TValueType): String; override;
68
function ReadInt8: ShortInt; override;
69
function ReadInt16: SmallInt; override;
70
function ReadInt32: LongInt; override;
71
function ReadInt64: Int64; override;
72
function ReadSet(EnumType: Pointer): Integer; override;
73
function ReadStr: String; override;
74
function ReadString(StringType: TValueType): String; override;
75
function ReadWideString: WideString;override;
76
function ReadUnicodeString: UnicodeString;override;
77
procedure SkipComponent(SkipComponentInfos: Boolean); override;
78
procedure SkipValue; override;
83
TXMLWriter = class(TWriter)
85
function CreateDriver(Stream: TStream; BufSize: Integer): TAbstractObjectWriter; override;
90
TXMLObjectWriter = class(TAbstractObjectWriter)
93
FXMLDoc: TXMLDocument;
96
FCurNode: TDOMElement;
98
FIsStreamingProps: boolean;
102
constructor Create(Stream: TStream; BufSize: Integer);
103
destructor Destroy; override;
105
procedure BeginCollection; override;
106
procedure BeginComponent(Component: TComponent; Flags: TFilerFlags;
107
ChildPos: Integer); override;
108
procedure BeginList; override;
109
procedure EndList; override;
110
procedure BeginProperty(const PropName: String); override;
111
procedure EndProperty; override;
113
//Please don't use write, better use WriteBinary whenever possible
114
procedure Write(const Buffer; Count: Longint); override;
115
procedure WriteBinary(const Buffer; Count: LongInt); override;
116
procedure WriteBoolean(Value: Boolean); override;
118
procedure WriteCurrency(const Value: Currency); override;
119
procedure WriteIdent(const Ident: string); override;
120
procedure WriteInteger(Value: Int64); override;
121
procedure WriteUInt64(Value: QWord); override;
122
procedure WriteMethodName(const Name: String); override;
123
procedure WriteSet(Value: LongInt; SetType: Pointer); override;
124
procedure WriteString(const Value: String); override;
125
procedure WriteWideString(const Value: WideString); override;
126
procedure WriteUnicodeString(const Value: UnicodeString); override;
127
procedure WriteVariant(const VarValue: Variant);override;
129
procedure WriteFloat(const Value: Extended); override;
130
procedure WriteSingle(const Value: Single); override;
131
procedure WriteDate(const Value: TDateTime); override;
136
{ TFileDescPascalUnitWithXMLResource }
138
TFileDescPascalUnitWithXMLResource = class(TFileDescPascalUnitWithResource)
140
constructor Create; override;
141
function GetLocalizedName: string; override;
142
function GetLocalizedDescription: string; override;
143
function GetImplementationSource(const Filename, SourceName, ResourceName: string): string; override;
158
RegisterUnitResourcefileFormat(TXMLUnitResourcefileFormat);
159
RegisterProjectFileDescriptor(TFileDescPascalUnitWithXMLResource.Create,
163
{ TFileDescPascalUnitWithXMLResource }
165
constructor TFileDescPascalUnitWithXMLResource.Create;
168
ResourceClass:=TForm;
171
function TFileDescPascalUnitWithXMLResource.GetLocalizedName: string;
173
Result:='Form with XML resource file';
176
function TFileDescPascalUnitWithXMLResource.GetLocalizedDescription: string;
178
Result:='Create a new unit with a LCL form with XML resource file.';
181
function TFileDescPascalUnitWithXMLResource.GetImplementationSource(
182
const Filename, SourceName, ResourceName: string): string;
184
ResourceFilename: String;
188
case GetResourceType of
191
ResourceFilename:=TrimFilename(ExtractFilenameOnly(Filename)+DefaultResFileExt);
192
Result:='initialization'+LE+' {$I '+ResourceFilename+'}'+LE+LE;
194
rtRes: Result := '{$R *.xml}'+LE+LE;
200
procedure TXMLObjectWriter.CreateXML;
202
FXMLDoc := TXMLDocument.Create;
206
constructor TXMLObjectWriter.Create(Stream: TStream; BufSize: Integer);
212
destructor TXMLObjectWriter.Destroy;
218
procedure TXMLObjectWriter.BeginCollection;
223
procedure TXMLObjectWriter.BeginComponent(Component: TComponent;
224
Flags: TFilerFlags; ChildPos: Integer);
226
ANewNode : TDOMElement;
228
if not FXmlCreated then
233
ANewNode := FXMLDoc.CreateElement('object');
235
ANewNode.AttribStrings['type'] := Component.ClassName;
236
ANewNode.AttribStrings['name'] := Component.Name;
237
if not assigned(FObjNode) then
238
FXMLDoc.AppendChild(ANewNode)
240
FObjNode.AppendChild(ANewNode);
241
FObjNode := ANewNode;
242
FIsStreamingProps:=True;
245
procedure TXMLObjectWriter.BeginList;
250
procedure TXMLObjectWriter.EndList;
253
if FIsStreamingProps then
255
FIsStreamingProps:=false;
258
FObjNode := FObjNode.ParentNode;
261
WriteXMLFile(FXMLDoc,FStream);
264
procedure TXMLObjectWriter.BeginProperty(const PropName: String);
266
FCurNode := FXMLDoc.CreateElement('property');
267
FObjNode.AppendChild(FCurNode);
268
FCurNode.AttribStrings['name'] := PropName;
271
procedure TXMLObjectWriter.EndProperty;
276
procedure TXMLObjectWriter.Write(const Buffer; Count: Longint);
281
procedure TXMLObjectWriter.WriteBinary(const Buffer; Count: LongInt);
286
procedure TXMLObjectWriter.WriteBoolean(Value: Boolean);
290
FCurNode.AttribStrings['type'] := 'vatrue';
291
FCurNode.TextContent:='True';
295
FCurNode.AttribStrings['type'] := 'vafalse';
296
FCurNode.TextContent:='False';
300
procedure TXMLObjectWriter.WriteCurrency(const Value: Currency);
305
procedure TXMLObjectWriter.WriteIdent(const Ident: string);
307
FCurNode.AttribStrings['type'] := 'ident';
308
FCurNode.TextContent:=Ident;
311
procedure TXMLObjectWriter.WriteInteger(Value: Int64);
313
FCurNode.AttribStrings['type'] := 'int64';
314
FCurNode.TextContent:=inttostr(value);
317
procedure TXMLObjectWriter.WriteUInt64(Value: QWord);
319
FCurNode.AttribStrings['type'] := 'int64';
320
FCurNode.TextContent:=inttostr(value);
323
procedure TXMLObjectWriter.WriteMethodName(const Name: String);
325
FCurNode.AttribStrings['type'] := 'ident';
326
FCurNode.TextContent:=Name;
329
procedure TXMLObjectWriter.WriteSet(Value: LongInt; SetType: Pointer);
334
procedure TXMLObjectWriter.WriteString(const Value: String);
336
FCurNode.AttribStrings['type'] := 'string';
337
FCurNode.TextContent:=value;
340
procedure TXMLObjectWriter.WriteWideString(const Value: WideString);
345
procedure TXMLObjectWriter.WriteUnicodeString(const Value: UnicodeString);
350
procedure TXMLObjectWriter.WriteVariant(const VarValue: Variant);
355
procedure TXMLObjectWriter.WriteFloat(const Value: Extended);
360
procedure TXMLObjectWriter.WriteSingle(const Value: Single);
365
procedure TXMLObjectWriter.WriteDate(const Value: TDateTime);
372
function TXMLWriter.CreateDriver(Stream: TStream; BufSize: Integer
373
): TAbstractObjectWriter;
375
Result:=TXMLObjectWriter.Create(Stream,BufSize);
380
constructor TXMLObjectReader.create(AStream: TStream);
384
If (AStream=Nil) then
385
Raise EReadError.Create(SEmptyStreamIllegalReader);
390
destructor TXMLObjectReader.Destroy;
396
function TXMLObjectReader.NextValue: TValueType;
399
StoreObjNode: TDOMNode;
400
StoreReadingChilds: boolean;
402
StoreNode := FCurNode;
403
StoreObjNode := FObjNode;
404
StoreReadingChilds := FReadingChilds;
407
FObjNode:=StoreObjNode;
408
FReadingChilds:=StoreReadingChilds;
411
function TXMLObjectReader.ReadValue: TValueType;
414
if not assigned(FCurNode) then
416
if not FReadingChilds then
418
FCurNode := FObjNode.FirstChild;
419
while assigned(FCurNode) and (FCurNode.NodeName<>'object') do
420
FCurNode := FCurNode.NextSibling;
421
FReadingChilds:=true;
425
if assigned(FObjNode.NextSibling) then
426
FCurNode := FObjNode.NextSibling
427
else if assigned(FObjNode.ParentNode) then
428
FObjNode := FObjNode.ParentNode;
430
while assigned(FCurNode) and (FCurNode.NodeName<>'object') do
431
FCurNode := FCurNode.NextSibling;
436
if not FReadingChilds and (FCurNode.NodeName='property') then
438
FCurValue := FCurNode.TextContent;
439
if FCurNode.Attributes.GetNamedItem('type').NodeValue='int16' then
441
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='int64' then
443
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='string' then
445
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='vatrue' then
447
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='vafalse' then
449
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='ident' then
452
raise EReadError.CreateFmt('Unknown property type %s',[FCurNode.Attributes.GetNamedItem('type').NodeValue]);
455
if FReadingChilds and (FCurNode.NodeName='object') then
458
FCurNode := FCurNode.NextSibling;
459
while assigned(FCurNode) do
461
if FReadingChilds and (FCurNode.NodeName='object') then
463
if not FReadingChilds and (FCurNode.NodeName='property') then
465
FCurNode := FCurNode.NextSibling;
469
procedure TXMLObjectReader.BeginRootComponent;
473
ReadXMLFile(FXMLDoc, FStream);
474
FCurNode := FXMLDoc.FindNode('object');
475
if not assigned(FCurNode) then
476
raise EReadError.Create('Invalid XML-stream format: No object node found');
479
procedure TXMLObjectReader.BeginComponent(var Flags: TFilerFlags;
480
var AChildPos: Integer; var CompClassName, CompName: String);
483
FReadingChilds:=false;
485
assert(FObjNode.NodeName='object');
487
CompName:=FObjNode.Attributes.GetNamedItem('name').NodeValue;
488
CompClassName:=FObjNode.Attributes.GetNamedItem('type').NodeValue;
489
FCurNode := FObjNode.FirstChild;
490
while assigned(FCurNode) and (FCurNode.NodeName<>'property') do
491
FCurNode := FCurNode.NextSibling;
494
function TXMLObjectReader.BeginProperty: String;
496
if FCurNode.NodeName<>'property' then
497
raise exception.create('property-element expected but found '+FCurNode.NodeName);
498
result := FCurNode.Attributes.GetNamedItem('name').NodeValue;
501
procedure TXMLObjectReader.Read(var Buf; Count: LongInt);
506
procedure TXMLObjectReader.ReadBinary(const DestData: TMemoryStream);
511
function TXMLObjectReader.ReadCurrency: Currency;
516
function TXMLObjectReader.ReadIdent(ValueType: TValueType): String;
521
function TXMLObjectReader.ReadInt8: ShortInt;
523
result := strtoint(FCurValue);
526
function TXMLObjectReader.ReadInt16: SmallInt;
528
result := strtoint(FCurValue);
531
function TXMLObjectReader.ReadInt32: LongInt;
533
result := strtoint(FCurValue);
536
function TXMLObjectReader.ReadInt64: Int64;
538
result := StrToInt64(FCurValue);
541
function TXMLObjectReader.ReadSet(EnumType: Pointer): Integer;
546
function TXMLObjectReader.ReadStr: String;
551
function TXMLObjectReader.ReadString(StringType: TValueType): String;
556
function TXMLObjectReader.ReadWideString: WideString;
561
function TXMLObjectReader.ReadUnicodeString: UnicodeString;
566
procedure TXMLObjectReader.SkipComponent(SkipComponentInfos: Boolean);
571
procedure TXMLObjectReader.SkipValue;
578
function TXMLReader.CreateDriver(Stream: TStream; BufSize: Integer): TAbstractObjectReader;
580
Result := TXMLObjectReader.Create(Stream);
583
{ TXMLUnitResourcefileFormat }
585
class procedure TXMLUnitResourcefileFormat.QuickReadXML(s: TStream; out
586
AComponentName, AClassName, ALCLVersion: string);
588
AXMLDocument: TXMLDocument;
592
ReadXMLFile(AXMLDocument, s);
594
ObjNode := AXMLDocument.FindNode('lazarusinfo');
595
if assigned(ObjNode) then
597
ObjNode := ObjNode.FindNode('lclversion');
598
if assigned(ObjNode) then
599
ALCLVersion:=ObjNode.TextContent;
602
ObjNode := AXMLDocument.FindNode('object');
603
if not assigned(ObjNode) then
604
raise EReadError.Create('Invalid XML-stream format: No object node found');
605
AComponentName:=ObjNode.Attributes.GetNamedItem('name').NodeValue;
606
AClassName:=ObjNode.Attributes.GetNamedItem('type').NodeValue;
613
class function TXMLUnitResourcefileFormat.FindResourceDirective(Source: TObject): boolean;
618
// result := CodeToolBoss.FindResourceDirective(Source as TCodeBuffer,1,1,cb,nx,ny,nt, ResourceDirectiveFilename,false);
621
class function TXMLUnitResourcefileFormat.ResourceDirectiveFilename: string;
626
class function TXMLUnitResourcefileFormat.GetUnitResourceFilename(
627
AUnitFilenae: string): string;
629
result := ChangeFileExt(AUnitFilenae,'.xml');
632
class procedure TXMLUnitResourcefileFormat.TextStreamToBinStream(ATxtStream,
633
ABinStream: TExtMemoryStream);
635
ABinStream.LoadFromStream(ATxtStream);
638
class procedure TXMLUnitResourcefileFormat.BinStreamToTextStream(ABinStream,
639
ATextStream: TExtMemoryStream);
641
ATextStream.LoadFromStream(ABinStream);
644
class function TXMLUnitResourcefileFormat.GetClassNameFromStream(s: TStream;
645
out IsInherited: Boolean): shortstring;
652
QuickReadXML(s, AComponentName, AClassType, ALCLVersion);
653
s.Seek(0,soFromBeginning);
654
result := AClassType;
657
class function TXMLUnitResourcefileFormat.CreateReader(s: TStream;
658
var DestroyDriver: boolean): TReader;
660
result := TXMLReader.Create(s,4096);
663
class function TXMLUnitResourcefileFormat.CreateWriter(s: TStream;
664
var DestroyDriver: boolean): TWriter;
666
ADriver: TXMLObjectWriter;
668
ADriver:=TXMLObjectWriter.Create(s,4096);
669
result := TWriter.Create(ADriver);
670
DestroyDriver:=false;
673
class function TXMLUnitResourcefileFormat.QuickCheckResourceBuffer(
674
PascalBuffer, LFMBuffer: TObject; out LFMType, LFMComponentName,
675
LFMClassName: string; out LCLVersion: string; out MissingClasses: TStrings
680
ms := TStringStream.Create((LFMBuffer as TCodeBuffer).Source);
682
QuickReadXML(ms, LFMComponentName, LFMClassName, LCLVersion);
688
MissingClasses := nil;
693
unit xmlresourcefile;
701
LCLMemManager, forms,
702
dom, XMLRead,XMLWrite,
708
{ TXMLUnitResourcefileFormat }
710
TXMLUnitResourcefileFormat = class(TUnitResourcefileFormat)
712
class procedure QuickReadXML(s: TStream; out AComponentName, AClassName, ALCLVersion: string);
714
class function FindResourceDirective(Source: TObject): boolean; override;
715
class function ResourceDirectiveFilename: string; override;
716
class function GetUnitResourceFilename(AUnitFilenae: string): string; override;
717
class procedure TextStreamToBinStream(ATxtStream, ABinStream: TExtMemoryStream); override;
718
class procedure BinStreamToTextStream(ABinStream, ATextStream: TExtMemoryStream); override;
719
class function GetClassNameFromStream(s: TStream; out IsInherited: Boolean): shortstring; override;
720
class function CreateReader(s: TStream; var DestroyDriver: boolean): TReader; override;
721
class function CreateWriter(s: TStream; var DestroyDriver: boolean): TWriter; override;
722
class function QuickCheckResourceBuffer(PascalBuffer, LFMBuffer: TObject;
723
out LFMType, LFMComponentName, LFMClassName: string; out
724
LCLVersion: string; out MissingClasses: TStrings): TModalResult; override;
729
TXMLReader = class(TReader)
731
function CreateDriver(Stream: TStream; BufSize: Integer): TAbstractObjectReader; override;
736
TXMLObjectReader = class(TAbstractObjectReader)
738
FXMLDoc: TXMLDocument;
743
FReadingChilds: Boolean;
745
constructor create(AStream: TStream); virtual;
746
destructor Destroy; override;
747
function NextValue: TValueType; override;
748
function ReadValue: TValueType; override;
749
procedure BeginRootComponent; override;
750
procedure BeginComponent(var Flags: TFilerFlags; var AChildPos: Integer;
751
var CompClassName, CompName: String); override;
752
function BeginProperty: String; override;
754
//Please don't use read, better use ReadBinary whenever possible
755
procedure Read(var Buf; Count: LongInt); override;
756
{ All ReadXXX methods are called _after_ the value type has been read! }
757
procedure ReadBinary(const DestData: TMemoryStream); override;
758
function ReadCurrency: Currency; override;
759
function ReadIdent(ValueType: TValueType): String; override;
760
function ReadInt8: ShortInt; override;
761
function ReadInt16: SmallInt; override;
762
function ReadInt32: LongInt; override;
763
function ReadInt64: Int64; override;
764
function ReadSet(EnumType: Pointer): Integer; override;
765
function ReadStr: String; override;
766
function ReadString(StringType: TValueType): String; override;
767
function ReadWideString: WideString;override;
768
function ReadUnicodeString: UnicodeString;override;
769
procedure SkipComponent(SkipComponentInfos: Boolean); override;
770
procedure SkipValue; override;
775
TXMLWriter = class(TWriter)
777
function CreateDriver(Stream: TStream; BufSize: Integer): TAbstractObjectWriter; override;
782
TXMLObjectWriter = class(TAbstractObjectWriter)
784
FXMLCreated: boolean;
785
FXMLDoc: TXMLDocument;
788
FCurNode: TDOMElement;
790
FIsStreamingProps: boolean;
794
constructor Create(Stream: TStream; BufSize: Integer);
795
destructor Destroy; override;
797
procedure BeginCollection; override;
798
procedure BeginComponent(Component: TComponent; Flags: TFilerFlags;
799
ChildPos: Integer); override;
800
procedure BeginList; override;
801
procedure EndList; override;
802
procedure BeginProperty(const PropName: String); override;
803
procedure EndProperty; override;
805
//Please don't use write, better use WriteBinary whenever possible
806
procedure Write(const Buffer; Count: Longint); override;
807
procedure WriteBinary(const Buffer; Count: LongInt); override;
808
procedure WriteBoolean(Value: Boolean); override;
810
procedure WriteCurrency(const Value: Currency); override;
811
procedure WriteIdent(const Ident: string); override;
812
procedure WriteInteger(Value: Int64); override;
813
procedure WriteUInt64(Value: QWord); override;
814
procedure WriteMethodName(const Name: String); override;
815
procedure WriteSet(Value: LongInt; SetType: Pointer); override;
816
procedure WriteString(const Value: String); override;
817
procedure WriteWideString(const Value: WideString); override;
818
procedure WriteUnicodeString(const Value: UnicodeString); override;
819
procedure WriteVariant(const VarValue: Variant);override;
821
procedure WriteFloat(const Value: Extended); override;
822
procedure WriteSingle(const Value: Single); override;
823
procedure WriteDate(const Value: TDateTime); override;
828
{ TFileDescPascalUnitWithXMLResource }
830
TFileDescPascalUnitWithXMLResource = class(TFileDescPascalUnitWithResource)
832
constructor Create; override;
833
function GetLocalizedName: string; override;
834
function GetLocalizedDescription: string; override;
835
function GetImplementationSource(const Filename, SourceName, ResourceName: string): string; override;
850
RegisterUnitResourcefileFormat(TXMLUnitResourcefileFormat);
851
RegisterProjectFileDescriptor(TFileDescPascalUnitWithXMLResource.Create,
855
{ TFileDescPascalUnitWithXMLResource }
857
constructor TFileDescPascalUnitWithXMLResource.Create;
860
ResourceClass:=TForm;
863
function TFileDescPascalUnitWithXMLResource.GetLocalizedName: string;
865
Result:='Form with XML resource file';
868
function TFileDescPascalUnitWithXMLResource.GetLocalizedDescription: string;
870
Result:='Create a new unit with a LCL form with XML resource file.';
873
function TFileDescPascalUnitWithXMLResource.GetImplementationSource(
874
const Filename, SourceName, ResourceName: string): string;
876
ResourceFilename: String;
880
case GetResourceType of
883
ResourceFilename:=TrimFilename(ExtractFilenameOnly(Filename)+DefaultResFileExt);
884
Result:='initialization'+LE+' {$I '+ResourceFilename+'}'+LE+LE;
886
rtRes: Result := '{$R *.xml}'+LE+LE;
892
procedure TXMLObjectWriter.CreateXML;
894
FXMLDoc := TXMLDocument.Create;
898
constructor TXMLObjectWriter.Create(Stream: TStream; BufSize: Integer);
904
destructor TXMLObjectWriter.Destroy;
910
procedure TXMLObjectWriter.BeginCollection;
915
procedure TXMLObjectWriter.BeginComponent(Component: TComponent;
916
Flags: TFilerFlags; ChildPos: Integer);
918
ANewNode : TDOMElement;
920
if not FXmlCreated then
925
ANewNode := FXMLDoc.CreateElement('object');
927
ANewNode.AttribStrings['type'] := Component.ClassName;
928
ANewNode.AttribStrings['name'] := Component.Name;
929
if not assigned(FObjNode) then
930
FXMLDoc.AppendChild(ANewNode)
932
FObjNode.AppendChild(ANewNode);
933
FObjNode := ANewNode;
934
FIsStreamingProps:=True;
937
procedure TXMLObjectWriter.BeginList;
942
procedure TXMLObjectWriter.EndList;
945
if FIsStreamingProps then
947
FIsStreamingProps:=false;
950
FObjNode := FObjNode.ParentNode;
953
WriteXMLFile(FXMLDoc,FStream);
956
procedure TXMLObjectWriter.BeginProperty(const PropName: String);
958
FCurNode := FXMLDoc.CreateElement('property');
959
FObjNode.AppendChild(FCurNode);
960
FCurNode.AttribStrings['name'] := PropName;
963
procedure TXMLObjectWriter.EndProperty;
968
procedure TXMLObjectWriter.Write(const Buffer; Count: Longint);
973
procedure TXMLObjectWriter.WriteBinary(const Buffer; Count: LongInt);
978
procedure TXMLObjectWriter.WriteBoolean(Value: Boolean);
982
FCurNode.AttribStrings['type'] := 'vatrue';
983
FCurNode.TextContent:='True';
987
FCurNode.AttribStrings['type'] := 'vafalse';
988
FCurNode.TextContent:='False';
992
procedure TXMLObjectWriter.WriteCurrency(const Value: Currency);
997
procedure TXMLObjectWriter.WriteIdent(const Ident: string);
999
FCurNode.AttribStrings['type'] := 'ident';
1000
FCurNode.TextContent:=Ident;
1003
procedure TXMLObjectWriter.WriteInteger(Value: Int64);
1005
FCurNode.AttribStrings['type'] := 'int64';
1006
FCurNode.TextContent:=inttostr(value);
1009
procedure TXMLObjectWriter.WriteUInt64(Value: QWord);
1011
FCurNode.AttribStrings['type'] := 'int64';
1012
FCurNode.TextContent:=inttostr(value);
1015
procedure TXMLObjectWriter.WriteMethodName(const Name: String);
1017
FCurNode.AttribStrings['type'] := 'ident';
1018
FCurNode.TextContent:=Name;
1021
procedure TXMLObjectWriter.WriteSet(Value: LongInt; SetType: Pointer);
1026
procedure TXMLObjectWriter.WriteString(const Value: String);
1028
FCurNode.AttribStrings['type'] := 'string';
1029
FCurNode.TextContent:=value;
1032
procedure TXMLObjectWriter.WriteWideString(const Value: WideString);
1037
procedure TXMLObjectWriter.WriteUnicodeString(const Value: UnicodeString);
1042
procedure TXMLObjectWriter.WriteVariant(const VarValue: Variant);
1047
procedure TXMLObjectWriter.WriteFloat(const Value: Extended);
1052
procedure TXMLObjectWriter.WriteSingle(const Value: Single);
1057
procedure TXMLObjectWriter.WriteDate(const Value: TDateTime);
1064
function TXMLWriter.CreateDriver(Stream: TStream; BufSize: Integer
1065
): TAbstractObjectWriter;
1067
Result:=TXMLObjectWriter.Create(Stream,BufSize);
1070
{ TXMLObjectReader }
1072
constructor TXMLObjectReader.create(AStream: TStream);
1076
If (AStream=Nil) then
1077
Raise EReadError.Create(SEmptyStreamIllegalReader);
1082
destructor TXMLObjectReader.Destroy;
1088
function TXMLObjectReader.NextValue: TValueType;
1091
StoreObjNode: TDOMNode;
1092
StoreReadingChilds: boolean;
1094
StoreNode := FCurNode;
1095
StoreObjNode := FObjNode;
1096
StoreReadingChilds := FReadingChilds;
1097
result := ReadValue;
1098
FCurNode:=StoreNode;
1099
FObjNode:=StoreObjNode;
1100
FReadingChilds:=StoreReadingChilds;
1103
function TXMLObjectReader.ReadValue: TValueType;
1106
if not assigned(FCurNode) then
1108
if not FReadingChilds then
1110
FCurNode := FObjNode.FirstChild;
1111
while assigned(FCurNode) and (FCurNode.NodeName<>'object') do
1112
FCurNode := FCurNode.NextSibling;
1113
FReadingChilds:=true;
1117
if assigned(FObjNode.NextSibling) then
1118
FCurNode := FObjNode.NextSibling
1119
else if assigned(FObjNode.ParentNode) then
1120
FObjNode := FObjNode.ParentNode;
1122
while assigned(FCurNode) and (FCurNode.NodeName<>'object') do
1123
FCurNode := FCurNode.NextSibling;
1128
if not FReadingChilds and (FCurNode.NodeName='property') then
1130
FCurValue := FCurNode.TextContent;
1131
if FCurNode.Attributes.GetNamedItem('type').NodeValue='int16' then
1133
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='int64' then
1135
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='string' then
1137
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='vatrue' then
1139
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='vafalse' then
1141
else if FCurNode.Attributes.GetNamedItem('type').NodeValue='ident' then
1144
raise EReadError.CreateFmt('Unknown property type %s',[FCurNode.Attributes.GetNamedItem('type').NodeValue]);
1147
if FReadingChilds and (FCurNode.NodeName='object') then
1150
FCurNode := FCurNode.NextSibling;
1151
while assigned(FCurNode) do
1153
if FReadingChilds and (FCurNode.NodeName='object') then
1155
if not FReadingChilds and (FCurNode.NodeName='property') then
1157
FCurNode := FCurNode.NextSibling;
1161
procedure TXMLObjectReader.BeginRootComponent;
1165
ReadXMLFile(FXMLDoc, FStream);
1166
FCurNode := FXMLDoc.FindNode('object');
1167
if not assigned(FCurNode) then
1168
raise EReadError.Create('Invalid XML-stream format: No object node found');
1171
procedure TXMLObjectReader.BeginComponent(var Flags: TFilerFlags;
1172
var AChildPos: Integer; var CompClassName, CompName: String);
1175
FReadingChilds:=false;
1177
assert(FObjNode.NodeName='object');
1179
CompName:=FObjNode.Attributes.GetNamedItem('name').NodeValue;
1180
CompClassName:=FObjNode.Attributes.GetNamedItem('type').NodeValue;
1181
FCurNode := FObjNode.FirstChild;
1182
while assigned(FCurNode) and (FCurNode.NodeName<>'property') do
1183
FCurNode := FCurNode.NextSibling;
1186
function TXMLObjectReader.BeginProperty: String;
1188
if FCurNode.NodeName<>'property' then
1189
raise exception.create('property-element expected but found '+FCurNode.NodeName);
1190
result := FCurNode.Attributes.GetNamedItem('name').NodeValue;
1193
procedure TXMLObjectReader.Read(var Buf; Count: LongInt);
1198
procedure TXMLObjectReader.ReadBinary(const DestData: TMemoryStream);
1203
function TXMLObjectReader.ReadCurrency: Currency;
1208
function TXMLObjectReader.ReadIdent(ValueType: TValueType): String;
1210
result := FCurValue;
1213
function TXMLObjectReader.ReadInt8: ShortInt;
1215
result := strtoint(FCurValue);
1218
function TXMLObjectReader.ReadInt16: SmallInt;
1220
result := strtoint(FCurValue);
1223
function TXMLObjectReader.ReadInt32: LongInt;
1225
result := strtoint(FCurValue);
1228
function TXMLObjectReader.ReadInt64: Int64;
1230
result := StrToInt64(FCurValue);
1233
function TXMLObjectReader.ReadSet(EnumType: Pointer): Integer;
1238
function TXMLObjectReader.ReadStr: String;
1240
result := FCurValue;
1243
function TXMLObjectReader.ReadString(StringType: TValueType): String;
1245
result := FCurValue;
1248
function TXMLObjectReader.ReadWideString: WideString;
1250
result := FCurValue;
1253
function TXMLObjectReader.ReadUnicodeString: UnicodeString;
1255
result := FCurValue;
1258
procedure TXMLObjectReader.SkipComponent(SkipComponentInfos: Boolean);
1263
procedure TXMLObjectReader.SkipValue;
1270
function TXMLReader.CreateDriver(Stream: TStream; BufSize: Integer): TAbstractObjectReader;
1272
Result := TXMLObjectReader.Create(Stream);
1275
{ TXMLUnitResourcefileFormat }
1277
class procedure TXMLUnitResourcefileFormat.QuickReadXML(s: TStream; out
1278
AComponentName, AClassName, ALCLVersion: string);
1280
AXMLDocument: TXMLDocument;
1284
ReadXMLFile(AXMLDocument, s);
1286
ObjNode := AXMLDocument.FindNode('lazarusinfo');
1287
if assigned(ObjNode) then
1289
ObjNode := ObjNode.FindNode('lclversion');
1290
if assigned(ObjNode) then
1291
ALCLVersion:=ObjNode.TextContent;
1294
ObjNode := AXMLDocument.FindNode('object');
1295
if not assigned(ObjNode) then
1296
raise EReadError.Create('Invalid XML-stream format: No object node found');
1297
AComponentName:=ObjNode.Attributes.GetNamedItem('name').NodeValue;
1298
AClassName:=ObjNode.Attributes.GetNamedItem('type').NodeValue;
1305
class function TXMLUnitResourcefileFormat.FindResourceDirective(Source: TObject): boolean;
1310
// result := CodeToolBoss.FindResourceDirective(Source as TCodeBuffer,1,1,cb,nx,ny,nt, ResourceDirectiveFilename,false);
1313
class function TXMLUnitResourcefileFormat.ResourceDirectiveFilename: string;
1318
class function TXMLUnitResourcefileFormat.GetUnitResourceFilename(
1319
AUnitFilenae: string): string;
1321
result := ChangeFileExt(AUnitFilenae,'.xml');
1324
class procedure TXMLUnitResourcefileFormat.TextStreamToBinStream(ATxtStream,
1325
ABinStream: TExtMemoryStream);
1327
ABinStream.LoadFromStream(ATxtStream);
1330
class procedure TXMLUnitResourcefileFormat.BinStreamToTextStream(ABinStream,
1331
ATextStream: TExtMemoryStream);
1333
ATextStream.LoadFromStream(ABinStream);
1336
class function TXMLUnitResourcefileFormat.GetClassNameFromStream(s: TStream;
1337
out IsInherited: Boolean): shortstring;
1341
ALCLVersion: string;
1344
QuickReadXML(s, AComponentName, AClassType, ALCLVersion);
1345
s.Seek(0,soFromBeginning);
1346
result := AClassType;
1349
class function TXMLUnitResourcefileFormat.CreateReader(s: TStream;
1350
var DestroyDriver: boolean): TReader;
1352
result := TXMLReader.Create(s,4096);
1355
class function TXMLUnitResourcefileFormat.CreateWriter(s: TStream;
1356
var DestroyDriver: boolean): TWriter;
1358
ADriver: TXMLObjectWriter;
1360
ADriver:=TXMLObjectWriter.Create(s,4096);
1361
result := TWriter.Create(ADriver);
1362
DestroyDriver:=false;
1365
class function TXMLUnitResourcefileFormat.QuickCheckResourceBuffer(
1366
PascalBuffer, LFMBuffer: TObject; out LFMType, LFMComponentName,
1367
LFMClassName: string; out LCLVersion: string; out MissingClasses: TStrings
1372
ms := TStringStream.Create((LFMBuffer as TCodeBuffer).Source);
1374
QuickReadXML(ms, LFMComponentName, LFMClassName, LCLVersion);
1380
MissingClasses := nil;