xmlcfg.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999 Sebastian Guenther, [email protected]
  5. Implementation of TXMLConfig class
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. TXMLConfig enables applications to use XML files for storing their
  14. configuration data
  15. }
  16. {$MODE objfpc}
  17. unit xmlcfg;
  18. interface
  19. uses DOM, xmlread, xmlwrite;
  20. type
  21. {"APath" is the path and name of a value: A XML configuration file is
  22. hierarchical. "/" is the path delimiter, the part after the last "/"
  23. is the name of the value. The path components will be mapped to XML
  24. elements, the name will be an element attribute.}
  25. TXMLConfig = class
  26. protected
  27. doc: TXMLDocument;
  28. FileName: String;
  29. public
  30. constructor Create(AFileName: String);
  31. destructor Destroy; override;
  32. procedure Flush; // Writes the XML file
  33. function GetValue(APath, ADefault: String): String;
  34. function GetValue(APath: String; ADefault: Integer): Integer;
  35. function GetValue(APath: String; ADefault: Boolean): Boolean;
  36. procedure SetValue(APath, AValue: String);
  37. procedure SetValue(APath: String; AValue: Integer);
  38. procedure SetValue(APath: String; AValue: Boolean);
  39. end;
  40. // =======================================================
  41. implementation
  42. uses sysutils;
  43. constructor TXMLConfig.Create(AFileName: String);
  44. var
  45. f: File;
  46. cfg: TDOMElement;
  47. begin
  48. FileName := AFileName;
  49. Assign(f, AFileName);
  50. {$I-}
  51. Reset(f, 1);
  52. {$I+}
  53. if IOResult = 0 then begin
  54. try
  55. ReadXMLFile(doc, f);
  56. except
  57. on e: EXMLReadError do
  58. WriteLn(StdErr, 'Warning: XML config parsing error: ', e.Message);
  59. end;
  60. Close(f);
  61. end;
  62. if doc = nil then
  63. doc := TXMLDocument.Create;
  64. cfg :=TDOMElement(doc.FindNode('CONFIG'));
  65. if cfg = nil then begin
  66. cfg := doc.CreateElement('CONFIG');
  67. doc.AppendChild(cfg);
  68. end;
  69. doc.SetDocumentElement(cfg);
  70. end;
  71. destructor TXMLConfig.Destroy;
  72. begin
  73. Flush;
  74. inherited Destroy;
  75. end;
  76. procedure TXMLConfig.Flush;
  77. var
  78. f: Text;
  79. begin
  80. Assign(f, FileName);
  81. Rewrite(f);
  82. WriteXMLFile(doc, f);
  83. Close(f);
  84. end;
  85. function TXMLConfig.GetValue(APath, ADefault: String): String;
  86. var
  87. node, subnode, attr: TDOMNode;
  88. i: Integer;
  89. name: String;
  90. begin
  91. node := doc.DocumentElement;
  92. while True do begin
  93. i := Pos('/', APath);
  94. if i = 0 then break;
  95. name := Copy(APath, 1, i - 1);
  96. APath := Copy(APath, i + 1, Length(APath));
  97. subnode := node.FindNode(name);
  98. if subnode = nil then begin
  99. Result := ADefault;
  100. exit;
  101. end;
  102. node := subnode;
  103. end;
  104. attr := node.Attributes.GetNamedItem(APath);
  105. if attr = nil then
  106. Result := ADefault
  107. else
  108. Result := attr.NodeValue;
  109. end;
  110. function TXMLConfig.GetValue(APath: String; ADefault: Integer): Integer;
  111. begin
  112. Result := StrToInt(GetValue(APath, IntToStr(ADefault)));
  113. end;
  114. function TXMLConfig.GetValue(APath: String; ADefault: Boolean): Boolean;
  115. var
  116. s: String;
  117. begin
  118. if ADefault then s := 'True'
  119. else s := 'False';
  120. s := GetValue(APath, s);
  121. if UpperCase(s) = 'TRUE' then Result := True
  122. else if UpperCase(s) = 'FALSE' then Result := False
  123. else Result := ADefault;
  124. end;
  125. procedure TXMLConfig.SetValue(APath, AValue: String);
  126. var
  127. node, subnode, attr: TDOMNode;
  128. i: Integer;
  129. name: String;
  130. begin
  131. node := doc.DocumentElement;
  132. while True do begin
  133. i := Pos('/', APath);
  134. if i = 0 then break;
  135. name := Copy(APath, 1, i - 1);
  136. APath := Copy(APath, i + 1, Length(APath));
  137. subnode := node.FindNode(name);
  138. if subnode = nil then begin
  139. subnode := doc.CreateElement(name);
  140. node.AppendChild(subnode);
  141. end;
  142. node := subnode;
  143. end;
  144. attr := node.Attributes.GetNamedItem(APath);
  145. if attr = nil then begin
  146. attr := doc.CreateAttribute(APath);
  147. node.Attributes.SetNamedItem(attr);
  148. end;
  149. attr.NodeValue := AValue;
  150. end;
  151. procedure TXMLConfig.SetValue(APath: String; AValue: Integer);
  152. begin
  153. SetValue(APath, IntToStr(AValue));
  154. end;
  155. procedure TXMLConfig.SetValue(APath: String; AValue: Boolean);
  156. begin
  157. if AValue then SetValue(APath, 'True')
  158. else SetValue(APath, 'False');
  159. end;
  160. end.
  161. {
  162. $Log$
  163. Revision 1.3 1999-07-25 16:24:13 michael
  164. + Fixes from Sebastiam Guenther - more error-proof
  165. Revision 1.2 1999/07/09 21:05:50 michael
  166. + fixes from Guenther Sebastian
  167. Revision 1.1 1999/07/09 08:35:09 michael
  168. + Initial implementation by Sebastian Guenther
  169. }