brookconfigurator.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. (*
  2. Brook for Free Pascal
  3. Copyright (C) 2014-2019 Silvio Clecio
  4. See the file LICENSE.txt, included in this distribution,
  5. for details about the copyright.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. *)
  10. { Configurator class. }
  11. unit BrookConfigurator;
  12. {$i brook.inc}
  13. interface
  14. uses
  15. BrookClasses, BrookUtils, BrookException, BrookMessages, BrookConsts, Classes,
  16. SysUtils;
  17. type
  18. { Handles exceptions for @link(TBrookConfigurator). }
  19. EBrookConfigurator = class(EBrook);
  20. { Is a metaclass for @link(TBrookConfigurator) class. }
  21. TBrookConfiguratorClass = class of TBrookConfigurator;
  22. { Is a type to configure event. }
  23. TBrookConfigureEvent = procedure(ASender: TObject;
  24. var AHandled: Boolean) of object;
  25. { Defines a pointer to the configure event.}
  26. PBrookConfigureEvent = ^TBrookConfigureEvent;
  27. { Configures objects by means of string or file. }
  28. TBrookConfigurator = class(TBrookComponent)
  29. private
  30. FAfterConfigure: TBrookConfigureEvent;
  31. FBeforeConfgure: TBrookConfigureEvent;
  32. FIgnoredParams: TStrings;
  33. FParams: TStrings;
  34. FTarget: TObject;
  35. function GetProp(const AName: string): string;
  36. function GetParam(const AName: string): string;
  37. procedure SetIgnoredParams(AValue: TStrings);
  38. procedure SetProp(const AName: string; const AValue: string);
  39. procedure SetParam(const AName: string; const AValue: string);
  40. procedure SetParams(AValue: TStrings);
  41. protected
  42. function GetTarget: TObject; virtual;
  43. procedure SetTarget(AValue: TObject); virtual;
  44. public
  45. { Creates an instance of a @link(TBrookConfigurator) class. }
  46. constructor Create(AOwner: TComponent); override;
  47. { Frees an instance of @link(TBrookConfigurator) class. }
  48. destructor Destroy; override;
  49. { Configures the target property. }
  50. procedure Configure;
  51. { Defines the object to be configured. }
  52. property Target: TObject read GetTarget write SetTarget;
  53. { Handles the target properties. }
  54. property Prop[const AName: string]: string read GetProp write SetProp;
  55. { Handles a string list of params of a configuration. }
  56. property Param[const AName: string]: string read GetParam
  57. write SetParam; default;
  58. { Ignored params in the configuration. }
  59. property IgnoredParams: TStrings read FIgnoredParams write SetIgnoredParams;
  60. { Params of the configuration. }
  61. property Params: TStrings read FParams write SetParams;
  62. { Is triggered after configure. }
  63. property AfterConfigure: TBrookConfigureEvent read FAfterConfigure
  64. write FAfterConfigure;
  65. { Is triggered before configure. }
  66. property BeforeConfgure: TBrookConfigureEvent read FBeforeConfgure
  67. write FBeforeConfgure;
  68. end;
  69. implementation
  70. constructor TBrookConfigurator.Create(AOwner: TComponent);
  71. begin
  72. inherited Create(AOwner);
  73. FParams := TStringList.Create;
  74. FIgnoredParams := TStringList.Create;
  75. end;
  76. destructor TBrookConfigurator.Destroy;
  77. begin
  78. FTarget := nil;
  79. FIgnoredParams.Free;
  80. FParams.Free;
  81. inherited Destroy;
  82. end;
  83. function TBrookConfigurator.GetParam(const AName: string): string;
  84. begin
  85. Result := FParams.Values[AName];
  86. end;
  87. procedure TBrookConfigurator.SetIgnoredParams(AValue: TStrings);
  88. begin
  89. if Assigned(AValue) then
  90. FIgnoredParams.Assign(AValue);
  91. end;
  92. function TBrookConfigurator.GetProp(const AName: string): string;
  93. begin
  94. if Assigned(FTarget) then
  95. BrookObjectToString(FTarget, AName, Result)
  96. else
  97. Result := ES;
  98. end;
  99. procedure TBrookConfigurator.SetProp(const AName: string;
  100. const AValue: string);
  101. begin
  102. if Assigned(FTarget) then
  103. BrookStringToObject(FTarget, AName, AValue);
  104. end;
  105. function TBrookConfigurator.GetTarget: TObject;
  106. begin
  107. Result := FTarget;
  108. end;
  109. procedure TBrookConfigurator.SetParam(const AName: string; const AValue: string);
  110. begin
  111. FParams.Values[AName] := AValue;
  112. end;
  113. procedure TBrookConfigurator.SetParams(AValue: TStrings);
  114. begin
  115. if Assigned(AValue) then
  116. FParams.Assign(AValue);
  117. end;
  118. procedure TBrookConfigurator.SetTarget(AValue: TObject);
  119. begin
  120. if AValue = Self then
  121. FTarget := nil
  122. else
  123. FTarget := AValue;
  124. end;
  125. procedure TBrookConfigurator.Configure;
  126. var
  127. VOldDelim: Char;
  128. VOldStrictDelim: Boolean;
  129. VHandled: Boolean = False;
  130. begin
  131. if (csDesigning in ComponentState) then
  132. Exit;
  133. try
  134. if Assigned(FBeforeConfgure) then
  135. FBeforeConfgure(Self, VHandled);
  136. if (not Assigned(FTarget)) or VHandled then
  137. Exit;
  138. if (FParams.Count = 0) and (BrookSettings.Configuration <> ES) then
  139. begin
  140. VOldStrictDelim := FParams.StrictDelimiter;
  141. VOldDelim := FParams.Delimiter;
  142. try
  143. FParams.StrictDelimiter := True;
  144. FParams.Delimiter := SC;
  145. if (Pos(SC, BrookSettings.Configuration) <> 0) or
  146. (Pos(EQ, BrookSettings.Configuration) <> 0) then
  147. FParams.DelimitedText := BrookSettings.Configuration
  148. else
  149. begin
  150. if not FileExists(BrookSettings.Configuration) then
  151. raise EBrookConfigurator.CreateFmt(Self,
  152. SBrookCfgFileNotFoundError, [BrookSettings.Configuration]);
  153. FParams.LoadFromFile(BrookSettings.Configuration);
  154. end;
  155. finally
  156. FParams.StrictDelimiter := VOldStrictDelim;
  157. FParams.Delimiter := VOldDelim;
  158. end;
  159. end;
  160. BrookStringsToObject(FTarget, FParams, FIgnoredParams);
  161. finally
  162. if Assigned(FAfterConfigure) then
  163. FAfterConfigure(Self, VHandled);
  164. end;
  165. end;
  166. end.