123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- {
- $Id$
- This file is part of the Free Component Library
- Implementation of TXMLConfig class
- Copyright (c) 1999-2000 by Sebastian Guenther, [email protected]
- See the file COPYING.FPC, 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.
- **********************************************************************}
- {
- TXMLConfig enables applications to use XML files for storing their
- configuration data
- }
- {$MODE objfpc}
- {$H+}
- unit XMLCfg;
- interface
- uses DOM, XMLRead, XMLWrite;
- type
- {"APath" is the path and name of a value: A XML configuration file is
- hierarchical. "/" is the path delimiter, the part after the last "/"
- is the name of the value. The path components will be mapped to XML
- elements, the name will be an element attribute.}
- TXMLConfig = class
- protected
- doc: TXMLDocument;
- FileName: String;
- public
- constructor Create(const AFileName: String);
- destructor Destroy; override;
- procedure Flush; // Writes the XML file
- function GetValue(const APath, ADefault: String): String;
- function GetValue(const APath: String; ADefault: Integer): Integer;
- function GetValue(const APath: String; ADefault: Boolean): Boolean;
- procedure SetValue(const APath, AValue: String);
- procedure SetValue(const APath: String; AValue: Integer);
- procedure SetValue(const APath: String; AValue: Boolean);
- end;
- // ===================================================================
- implementation
- uses SysUtils;
- constructor TXMLConfig.Create(const AFileName: String);
- var
- f: File;
- cfg: TDOMElement;
- begin
- FileName := AFileName;
- Assign(f, AFileName);
- {$I-}
- Reset(f, 1);
- {$I+}
- if IOResult = 0 then begin
- try
- ReadXMLFile(doc, f);
- except
- on e: EXMLReadError do
- WriteLn(StdErr, 'Warning: XML config parsing error: ', e.Message);
- end;
- Close(f);
- end;
- if not Assigned(doc) then
- doc := TXMLDocument.Create;
- cfg :=TDOMElement(doc.FindNode('CONFIG'));
- if not Assigned(cfg) then begin
- cfg := doc.CreateElement('CONFIG');
- doc.AppendChild(cfg);
- end;
- end;
- destructor TXMLConfig.Destroy;
- begin
- Flush;
- if Assigned(doc) then
- doc.Free;
- inherited Destroy;
- end;
- procedure TXMLConfig.Flush;
- var
- f: Text;
- begin
- Assign(f, FileName);
- Rewrite(f);
- WriteXMLFile(doc, f);
- Close(f);
- end;
- function TXMLConfig.GetValue(const APath, ADefault: String): String;
- var
- node, subnode, attr: TDOMNode;
- i: Integer;
- name, path: String;
- begin
- node := doc.DocumentElement;
- path := APath;
- while True do begin
- i := Pos('/', path);
- if i = 0 then break;
- name := Copy(path, 1, i - 1);
- path := Copy(path, i + 1, Length(path));
- subnode := node.FindNode(name);
- if not Assigned(subnode) then begin
- Result := ADefault;
- exit;
- end;
- node := subnode;
- end;
- attr := node.Attributes.GetNamedItem(path);
- if Assigned(attr) then
- Result := attr.NodeValue
- else
- Result := ADefault;
- end;
- function TXMLConfig.GetValue(const APath: String; ADefault: Integer): Integer;
- begin
- Result := StrToInt(GetValue(APath, IntToStr(ADefault)));
- end;
- function TXMLConfig.GetValue(const APath: String; ADefault: Boolean): Boolean;
- var
- s: String;
- begin
- if ADefault then
- s := 'True'
- else
- s := 'False';
- s := GetValue(APath, s);
- if UpperCase(s) = 'TRUE' then
- Result := True
- else if UpperCase(s) = 'FALSE' then
- Result := False
- else
- Result := ADefault;
- end;
- procedure TXMLConfig.SetValue(const APath, AValue: String);
- var
- node, subnode, attr: TDOMNode;
- i: Integer;
- name, path: String;
- begin
- node := doc.DocumentElement;
- path := APath;
- while True do
- begin
- i := Pos('/', path);
- if i = 0 then
- break;
- name := Copy(path, 1, i - 1);
- path := Copy(path, i + 1, Length(path));
- subnode := node.FindNode(name);
- if not Assigned(subnode) then
- begin
- subnode := doc.CreateElement(name);
- node.AppendChild(subnode);
- end;
- node := subnode;
- end;
- TDOMElement(node).SetAttribute(path, AValue);
- { attr := node.Attributes.GetNamedItem(path);
- if not Assigned(attr) then begin
- attr := doc.CreateAttribute(path);
- node.Attributes.SetNamedItem(attr);
- end;
- attr.NodeValue := AValue;}
- end;
- procedure TXMLConfig.SetValue(const APath: String; AValue: Integer);
- begin
- SetValue(APath, IntToStr(AValue));
- end;
- procedure TXMLConfig.SetValue(const APath: String; AValue: Boolean);
- begin
- if AValue then
- SetValue(APath, 'True')
- else
- SetValue(APath, 'False');
- end;
- end.
- {
- $Log$
- Revision 1.3 2000-07-29 14:52:24 sg
- * Modified the copyright notice to remove ambiguities
- Revision 1.2 2000/07/13 11:33:07 michael
- + removed logs
-
- }
|