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
|
{ $Id: xmlpropstorage.pas 25922 2010-06-05 15:35:41Z mattias $ }
{
*****************************************************************************
* *
* This file is part of the Lazarus Component Library (LCL) *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program 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. *
* *
*****************************************************************************
}
unit XMLPropStorage;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LCLProc, Forms, PropertyStorage, XMLCfg, DOM,
LazConfigStorage;
type
{ TPropStorageXMLConfig }
TPropStorageXMLConfig = class(TXMLConfig)
Public
procedure DeleteSubNodes (const ARootNode: String);
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
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);
destructor Destroy; override;
property XMLConfig: TXMLConfig read FXMLConfig;
property FreeXMLConfig: boolean read FFreeXMLConfig write FFreeXMLConfig;
procedure WriteToDisk; override;
function GetFilename: string; override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Misc',[TXMLPropStorage]);
end;
{ TCustomXMLPropStorage }
procedure TCustomXMLPropStorage.StorageNeeded(ReadOnly: Boolean);
begin
If (FXML=Nil) 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
{$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;
begin
Result:=FXML.GetValue(FixPath(Section)+'/'+Ident, TheDefault);
//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(FixPath(Section)+'/'+Ident, 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(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;
{ 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);
begin
FXMLConfig:=TXMLConfig.Create(nil);
FXMLConfig.StartEmpty:=not LoadFromDisk;
FXMLConfig.Filename:=Filename;
FFreeXMLConfig:=true;
end;
constructor TXMLConfigStorage.Create(TheXMLConfig: TXMLConfig);
begin
FXMLConfig:=TheXMLConfig;
if FXMLConfig=nil then
raise Exception.Create('');
end;
constructor TXMLConfigStorage.Create(TheXMLConfig: TXMLConfig;
const StartPath: string);
begin
Create(TheXMLConfig);
AppendBasePath(StartPath);
end;
destructor TXMLConfigStorage.Destroy;
begin
if FreeXMLConfig then FreeAndNil(FXMLConfig);
inherited Destroy;
end;
procedure TXMLConfigStorage.WriteToDisk;
begin
FXMLConfig.Flush;
end;
function TXMLConfigStorage.GetFilename: string;
begin
Result:=FXMLConfig.Filename;
end;
end.
|