-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbrookconfigurator.pas
192 lines (166 loc) · 5.45 KB
/
brookconfigurator.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
(*
Brook for Free Pascal
Copyright (C) 2014-2019 Silvio Clecio
See the file LICENSE.txt, included in this distribution,
for details about the copyright.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*)
{ Configurator class. }
unit BrookConfigurator;
{$i brook.inc}
interface
uses
BrookClasses, BrookUtils, BrookException, BrookMessages, BrookConsts, Classes,
SysUtils;
type
{ Handles exceptions for @link(TBrookConfigurator). }
EBrookConfigurator = class(EBrook);
{ Is a metaclass for @link(TBrookConfigurator) class. }
TBrookConfiguratorClass = class of TBrookConfigurator;
{ Is a type to configure event. }
TBrookConfigureEvent = procedure(ASender: TObject;
var AHandled: Boolean) of object;
{ Defines a pointer to the configure event.}
PBrookConfigureEvent = ^TBrookConfigureEvent;
{ Configures objects by means of string or file. }
TBrookConfigurator = class(TBrookComponent)
private
FAfterConfigure: TBrookConfigureEvent;
FBeforeConfgure: TBrookConfigureEvent;
FIgnoredParams: TStrings;
FParams: TStrings;
FTarget: TObject;
function GetProp(const AName: string): string;
function GetParam(const AName: string): string;
procedure SetIgnoredParams(AValue: TStrings);
procedure SetProp(const AName: string; const AValue: string);
procedure SetParam(const AName: string; const AValue: string);
procedure SetParams(AValue: TStrings);
protected
function GetTarget: TObject; virtual;
procedure SetTarget(AValue: TObject); virtual;
public
{ Creates an instance of a @link(TBrookConfigurator) class. }
constructor Create(AOwner: TComponent); override;
{ Frees an instance of @link(TBrookConfigurator) class. }
destructor Destroy; override;
{ Configures the target property. }
procedure Configure;
{ Defines the object to be configured. }
property Target: TObject read GetTarget write SetTarget;
{ Handles the target properties. }
property Prop[const AName: string]: string read GetProp write SetProp;
{ Handles a string list of params of a configuration. }
property Param[const AName: string]: string read GetParam
write SetParam; default;
{ Ignored params in the configuration. }
property IgnoredParams: TStrings read FIgnoredParams write SetIgnoredParams;
{ Params of the configuration. }
property Params: TStrings read FParams write SetParams;
{ Is triggered after configure. }
property AfterConfigure: TBrookConfigureEvent read FAfterConfigure
write FAfterConfigure;
{ Is triggered before configure. }
property BeforeConfgure: TBrookConfigureEvent read FBeforeConfgure
write FBeforeConfgure;
end;
implementation
constructor TBrookConfigurator.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FParams := TStringList.Create;
FIgnoredParams := TStringList.Create;
end;
destructor TBrookConfigurator.Destroy;
begin
FTarget := nil;
FIgnoredParams.Free;
FParams.Free;
inherited Destroy;
end;
function TBrookConfigurator.GetParam(const AName: string): string;
begin
Result := FParams.Values[AName];
end;
procedure TBrookConfigurator.SetIgnoredParams(AValue: TStrings);
begin
if Assigned(AValue) then
FIgnoredParams.Assign(AValue);
end;
function TBrookConfigurator.GetProp(const AName: string): string;
begin
if Assigned(FTarget) then
BrookObjectToString(FTarget, AName, Result)
else
Result := ES;
end;
procedure TBrookConfigurator.SetProp(const AName: string;
const AValue: string);
begin
if Assigned(FTarget) then
BrookStringToObject(FTarget, AName, AValue);
end;
function TBrookConfigurator.GetTarget: TObject;
begin
Result := FTarget;
end;
procedure TBrookConfigurator.SetParam(const AName: string; const AValue: string);
begin
FParams.Values[AName] := AValue;
end;
procedure TBrookConfigurator.SetParams(AValue: TStrings);
begin
if Assigned(AValue) then
FParams.Assign(AValue);
end;
procedure TBrookConfigurator.SetTarget(AValue: TObject);
begin
if AValue = Self then
FTarget := nil
else
FTarget := AValue;
end;
procedure TBrookConfigurator.Configure;
var
VOldDelim: Char;
VOldStrictDelim: Boolean;
VHandled: Boolean = False;
begin
if (csDesigning in ComponentState) then
Exit;
try
if Assigned(FBeforeConfgure) then
FBeforeConfgure(Self, VHandled);
if (not Assigned(FTarget)) or VHandled then
Exit;
if (FParams.Count = 0) and (BrookSettings.Configuration <> ES) then
begin
VOldStrictDelim := FParams.StrictDelimiter;
VOldDelim := FParams.Delimiter;
try
FParams.StrictDelimiter := True;
FParams.Delimiter := SC;
if (Pos(SC, BrookSettings.Configuration) <> 0) or
(Pos(EQ, BrookSettings.Configuration) <> 0) then
FParams.DelimitedText := BrookSettings.Configuration
else
begin
if not FileExists(BrookSettings.Configuration) then
raise EBrookConfigurator.CreateFmt(Self,
SBrookCfgFileNotFoundError, [BrookSettings.Configuration]);
FParams.LoadFromFile(BrookSettings.Configuration);
end;
finally
FParams.StrictDelimiter := VOldStrictDelim;
FParams.Delimiter := VOldDelim;
end;
end;
BrookStringsToObject(FTarget, FParams, FIgnoredParams);
finally
if Assigned(FAfterConfigure) then
FAfterConfigure(Self, VHandled);
end;
end;
end.