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
|
{ $Id: xmlpropstorage.pas 51954 2016-03-15 21:16:43Z juha $ }
{
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit XMLPropStorage;
{$mode objfpc}{$H+}
interface
uses
// RTL, FCL
Classes, SysUtils, XMLConf, DOM, XMLRead, XMLWrite,
// LCL
LCLProc, Forms,
// LazUtils
LazConfigStorage, LazUTF8, LazUTF8Classes;
type
{ TPropStorageXMLConfig }
TPropStorageXMLConfig = class(TXMLConfig)
Public
procedure DeleteSubNodes(const ARootNode: String);
procedure LoadFromStream(s: TStream); virtual;
procedure SaveToStream(s: TStream); virtual;
property XMLDoc: TXMLDocument read Doc;
end;
{ TCustomXMLPropStorage }
TCustomXMLPropStorage = class(TFormPropertyStorage)
private
FCount: Integer;
FFileName: String;
FXML: TPropStorageXMLConfig;
FRootNodePath: String;
protected
function GetXMLFileName: string; virtual;
function RootSection: String; Override;
function FixPath(const APath: String): String; virtual;
Property XMLConfig: TPropStorageXMLConfig Read FXML;
public
procedure StorageNeeded(ReadOnly: Boolean);override;
procedure FreeStorage; override;
function DoReadString(const Section, Ident, TheDefault: string): string; override;
procedure DoWriteString(const Section, Ident, Value: string); override;
procedure DoEraseSections(const ARootSection: String);override;
public
property FileName: String Read FFileName Write FFileName;
property RootNodePath: String Read FRootNodePath Write FRootNodePath;
end;
{ TXMLPropStorage }
TXMLPropStorage = class(TCustomXMLPropStorage)
Published
property StoredValues;
property FileName;
property RootNodePath;
property Active;
property OnSavingProperties;
property OnSaveProperties;
property OnRestoringProperties;
property OnRestoreProperties;
end;
{ TXMLConfigStorage }
TXMLConfigStorage = class(TConfigStorage)
private
FFilename: string;
FFreeXMLConfig: boolean;
FXMLConfig: TXMLConfig;
protected
function GetFullPathValue(const APath, ADefault: String): String; override;
function GetFullPathValue(const APath: String; ADefault: Integer): Integer; override;
function GetFullPathValue(const APath: String; ADefault: Boolean): Boolean; override;
procedure SetFullPathValue(const APath, AValue: String); override;
procedure SetDeleteFullPathValue(const APath, AValue, DefValue: String); override;
procedure SetFullPathValue(const APath: String; AValue: Integer); override;
procedure SetDeleteFullPathValue(const APath: String; AValue, DefValue: Integer); override;
procedure SetFullPathValue(const APath: String; AValue: Boolean); override;
procedure SetDeleteFullPathValue(const APath: String; AValue, DefValue: Boolean); override;
procedure DeleteFullPath(const APath: string); override;
procedure DeleteFullPathValue(const APath: string); override;
public
procedure Clear; override;
constructor Create(const Filename: string; LoadFromDisk: Boolean); override;
constructor Create(TheXMLConfig: TXMLConfig);
constructor Create(TheXMLConfig: TXMLConfig; const StartPath: string);
constructor Create(s: TStream; const StartPath: string = '');
destructor Destroy; override;
property XMLConfig: TXMLConfig read FXMLConfig;
property FreeXMLConfig: boolean read FFreeXMLConfig write FFreeXMLConfig;
procedure WriteToDisk; override;
function GetFilename: string; override;
procedure SaveToStream(s: TStream); virtual;
end;
procedure Register;
implementation
{$IFDEF FPC_HAS_CPSTRING}
{$WARN IMPLICIT_STRING_CAST OFF}
{$WARN IMPLICIT_STRING_CAST_LOSS OFF}
{$ENDIF}
procedure Register;
begin
RegisterComponents('Misc',[TXMLPropStorage]);
end;
{ TCustomXMLPropStorage }
procedure TCustomXMLPropStorage.StorageNeeded(ReadOnly: Boolean);
begin
If (FXML=Nil) and not (csDesigning in ComponentState) then
begin
FXML:=TPropStorageXMLConfig.Create(nil);
FXML.FileName := GetXMLFileName;
end;
Inc(FCount);
//debugln('TCustomXMLPropStorage.StorageNeeded ',dbgsname(FXML),' ',dbgs(FXML),' FCount=',dbgs(FCount));
end;
procedure TCustomXMLPropStorage.FreeStorage;
begin
Dec(FCount);
//debugln('TCustomXMLPropStorage.FreeStorage ',dbgsname(FXML),' ',dbgs(FXML),' FCount=',dbgs(FCount));
If (FCount<=0) then
begin
FCount:=0;
FreeAndNil(FXML);
end;
end;
function TCustomXMLPropStorage.GetXMLFileName: string;
begin
if (FFileName<>'') then
Result:=FFileName
else if csDesigning in ComponentState then
raise Exception.Create('TCustomXMLPropStorage.GetXMLFileName: missing Filename')
else
{$ifdef unix}
Result:=IncludeTrailingPathDelimiter(GetEnvironmentVariableUTF8('HOME'))
+'.'+ExtractFileName(Application.ExeName);
{$else}
Result:=ChangeFileExt(Application.ExeName,'.xml');
{$endif}
//debugln('TCustomXMLPropStorage.GetXMLFileName "',Result,'"');
end;
function TCustomXMLPropStorage.FixPath(const APath: String): String;
begin
Result:=StringReplace(APath,'.','/',[rfReplaceAll]);
end;
function TCustomXMLPropStorage.RootSection: String;
begin
If (FRootNodePath<>'') then
Result:=FRootNodePath
else
Result:=inherited RootSection;
Result:=FixPath(Result);
end;
function TCustomXMLPropStorage.DoReadString(const Section, Ident,
TheDefault: string): string;
var
Res: UnicodeString;
begin
Res:=FXML.GetValue(Utf8Decode(FixPath(Section)+'/'+Ident), Utf8Decode(TheDefault));
Result := Utf8Encode(Res);
//debugln('TCustomXMLPropStorage.DoReadString Section="',Section,'" Ident="',Ident,'" Result=',Result);
end;
procedure TCustomXMLPropStorage.DoWriteString(const Section, Ident,
Value: string);
begin
//debugln('TCustomXMLPropStorage.DoWriteString Section="',Section,'" Ident="',Ident,'" Value="',Value,'"');
FXML.SetValue(Utf8Decode(FixPath(Section)+'/'+Ident), Utf8Decode(Value));
end;
procedure TCustomXMLPropStorage.DoEraseSections(const ARootSection: String);
begin
//debugln('TCustomXMLPropStorage.DoEraseSections ARootSection="',ARootSection,'"');
FXML.DeleteSubNodes(FixPath(ARootSection));
end;
{ TPropStorageXMLConfig }
procedure TPropStorageXMLConfig.DeleteSubNodes(const ARootNode: String);
var
Node, Child: TDOMNode;
i: Integer;
NodePath: String;
begin
Node := doc.DocumentElement;
NodePath := ARootNode;
while (Length(NodePath)>0) and (Node<>Nil) do
begin
i := Pos('/', NodePath);
if i = 0 then
I:=Length(NodePath)+1;
Child := Node.FindNode(UTF8Decode(Copy(NodePath,1,i - 1)));
System.Delete(NodePath,1,I);
Node := Child;
end;
If Assigned(Node) then begin
//debugln('TPropStorageXMLConfig.DeleteSubNodes ',ARootNode);
Node.Free;
end;
end;
procedure TPropStorageXMLConfig.LoadFromStream(s: TStream);
begin
FreeAndNil(Doc);
ReadXMLFile(Doc,s);
end;
procedure TPropStorageXMLConfig.SaveToStream(s: TStream);
begin
WriteXMLFile(Doc,s);
end;
{ TXMLConfigStorage }
function TXMLConfigStorage.GetFullPathValue(const APath, ADefault: String
): String;
begin
Result:=XMLConfig.GetValue(APath, ADefault);
end;
function TXMLConfigStorage.GetFullPathValue(const APath: String;
ADefault: Integer): Integer;
begin
Result:=XMLConfig.GetValue(APath, ADefault);
end;
function TXMLConfigStorage.GetFullPathValue(const APath: String;
ADefault: Boolean): Boolean;
begin
Result:=XMLConfig.GetValue(APath, ADefault);
end;
procedure TXMLConfigStorage.SetFullPathValue(const APath, AValue: String);
begin
XMLConfig.SetValue(APath, AValue);
end;
procedure TXMLConfigStorage.SetDeleteFullPathValue(const APath, AValue,
DefValue: String);
begin
XMLConfig.SetDeleteValue(APath, AValue, DefValue);
end;
procedure TXMLConfigStorage.SetFullPathValue(const APath: String;
AValue: Integer);
begin
XMLConfig.SetValue(APath, AValue);
end;
procedure TXMLConfigStorage.SetDeleteFullPathValue(const APath: String;
AValue, DefValue: Integer);
begin
XMLConfig.SetDeleteValue(APath, AValue, DefValue);
end;
procedure TXMLConfigStorage.SetFullPathValue(const APath: String;
AValue: Boolean);
begin
XMLConfig.SetValue(APath, AValue);
end;
procedure TXMLConfigStorage.SetDeleteFullPathValue(const APath: String;
AValue, DefValue: Boolean);
begin
XMLConfig.SetDeleteValue(APath, AValue, DefValue);
end;
procedure TXMLConfigStorage.DeleteFullPath(const APath: string);
begin
XMLConfig.DeletePath(APath);
end;
procedure TXMLConfigStorage.DeleteFullPathValue(const APath: string);
begin
XMLConfig.DeleteValue(APath);
end;
procedure TXMLConfigStorage.Clear;
begin
FXMLConfig.Clear;
end;
constructor TXMLConfigStorage.Create(const Filename: string;
LoadFromDisk: Boolean);
var
ms: TMemoryStream;
fs: TFileStreamUTF8;
begin
FXMLConfig:=TPropStorageXMLConfig.Create(nil);
FFilename:=Filename;
FFreeXMLConfig:=true;
if LoadFromDisk then
begin
fs:=TFileStreamUTF8.Create(Filename,fmOpenRead+fmShareDenyWrite);
try
ms:=TMemoryStream.Create;
try
ms.CopyFrom(fs,fs.Size);
ms.Position:=0;
TPropStorageXMLConfig(FXMLConfig).LoadFromStream(ms);
finally
ms.Free;
end;
finally
fs.Free;
end;
end;
end;
constructor TXMLConfigStorage.Create(TheXMLConfig: TXMLConfig);
begin
FXMLConfig:=TheXMLConfig;
FFilename:=FXMLConfig.Filename;
if FXMLConfig=nil then
raise Exception.Create('');
end;
constructor TXMLConfigStorage.Create(TheXMLConfig: TXMLConfig;
const StartPath: string);
begin
Create(TheXMLConfig);
AppendBasePath(StartPath);
end;
constructor TXMLConfigStorage.Create(s: TStream; const StartPath: string);
begin
FXMLConfig:=TPropStorageXMLConfig.Create(nil);
FFreeXMLConfig:=true;
TPropStorageXMLConfig(FXMLConfig).LoadFromStream(s);
if StartPath<>'' then
AppendBasePath(StartPath);
end;
destructor TXMLConfigStorage.Destroy;
begin
if FreeXMLConfig then FreeAndNil(FXMLConfig);
inherited Destroy;
end;
procedure TXMLConfigStorage.WriteToDisk;
var
ms: TMemoryStream;
fs: TFileStreamUTF8;
begin
if FXMLConfig is TPropStorageXMLConfig then
begin
ms:=TMemoryStream.Create;
try
TPropStorageXMLConfig(FXMLConfig).SaveToStream(ms);
ms.Position:=0;
fs:=TFileStreamUTF8.Create(GetFilename,fmCreate);
try
fs.CopyFrom(ms,ms.Size);
finally
fs.Free;
end;
finally
ms.Free;
end;
end else
FXMLConfig.Flush;
end;
function TXMLConfigStorage.GetFilename: string;
begin
Result:=FFilename;
end;
procedure TXMLConfigStorage.SaveToStream(s: TStream);
begin
if FXMLConfig is TPropStorageXMLConfig then begin
TPropStorageXMLConfig(FXMLConfig).SaveToStream(s);
end else
raise Exception.Create('TXMLConfigStorage.SaveToStream not supported for '+DbgSName(FXMLConfig));
end;
end.
|