root / UXmlHelper.pas @ 13:c78b5eafa10e
History | View | Annotate | Download (4.6 kB)
| 1 | (*
|
|---|---|
| 2 | * CDDL HEADER START |
| 3 | * |
| 4 | * The contents of this file are subject to the terms of the |
| 5 | * Common Development and Distribution License, Version 1.0 only |
| 6 | * (the "License"). You may not use this file except in compliance |
| 7 | * with the License. |
| 8 | * |
| 9 | * You can obtain a copy of the license at |
| 10 | * http://www.opensource.org/licenses/cddl1.php. |
| 11 | * See the License for the specific language governing permissions |
| 12 | * and limitations under the License. |
| 13 | * |
| 14 | * When distributing Covered Code, include this CDDL HEADER in each |
| 15 | * file and include the License file at |
| 16 | * http://www.opensource.org/licenses/cddl1.php. If applicable, |
| 17 | * add the following below this CDDL HEADER, with the fields enclosed |
| 18 | * by brackets "[]" replaced with your own identifying * information: |
| 19 | * Portions Copyright [yyyy] [name of copyright owner] |
| 20 | * |
| 21 | * CDDL HEADER END |
| 22 | * |
| 23 | * |
| 24 | * Portions Copyright 2008 Andreas Schneider |
| 25 | *) |
| 26 | unit UXmlHelper;
|
| 27 | |
| 28 | {$mode delphi}{$H+} |
| 29 | |
| 30 | interface
|
| 31 | |
| 32 | uses
|
| 33 | Classes, SysUtils, dom; |
| 34 | |
| 35 | type
|
| 36 | |
| 37 | { TXmlHelper }
|
| 38 | |
| 39 | TXmlHelper = class(TObject)
|
| 40 | class function FindChild(AParent: TDOMElement; AName: string): TDOMElement; |
| 41 | class function AssureElement(AParent: TDOMElement; AName: string): TDOMElement; |
| 42 | class procedure WriteString(AParent: TDOMElement; AName, AValue: string); |
| 43 | class function ReadString(AParent: TDOMElement; AName, ADefault: string): string; |
| 44 | class procedure WriteInteger(AParent: TDOMElement; AName: string; AValue: Integer); |
| 45 | class function ReadInteger(AParent: TDOMElement; AName: string; ADefault: Integer): Integer; |
| 46 | class procedure WriteBoolean(AParent: TDOMElement; AName: string; AValue: Boolean); |
| 47 | class function ReadBoolean(AParent: TDOMElement; AName: string; ADefault: Boolean): Boolean; |
| 48 | class procedure WriteCoords(AParent: TDOMElement; AName: string; AX, AY: Integer); |
| 49 | class function ReadCoords(AParent: TDOMElement; AName: string; out X, Y: Integer): Boolean; |
| 50 | end;
|
| 51 | |
| 52 | implementation
|
| 53 | |
| 54 | { TXmlHelper }
|
| 55 | |
| 56 | class function TXmlHelper.FindChild(AParent: TDOMElement; AName: string): TDOMElement; |
| 57 | var
|
| 58 | i: LongWord; |
| 59 | nodeList: TDOMNodeList; |
| 60 | begin
|
| 61 | Result := nil;
|
| 62 | nodeList := AParent.GetChildNodes; |
| 63 | i := 0;
|
| 64 | while (Result = nil) and (i < nodeList.Count) do |
| 65 | begin
|
| 66 | if nodeList.Item[i].NodeName = AName then |
| 67 | Result := TDOMElement(nodeList[i]); |
| 68 | inc(i); |
| 69 | end;
|
| 70 | nodeList.Free; |
| 71 | end;
|
| 72 | |
| 73 | class function TXmlHelper.AssureElement(AParent: TDOMElement; AName: string): TDOMElement; |
| 74 | begin
|
| 75 | Result := FindChild(AParent, AName); |
| 76 | if Result = nil then |
| 77 | begin
|
| 78 | Result := AParent.OwnerDocument.CreateElement(AName); |
| 79 | AParent.AppendChild(Result); |
| 80 | end;
|
| 81 | end;
|
| 82 | |
| 83 | class procedure TXmlHelper.WriteString(AParent: TDOMElement; AName, AValue: string); |
| 84 | var
|
| 85 | element: TDOMElement; |
| 86 | begin
|
| 87 | element := AssureElement(AParent, AName); |
| 88 | if assigned(element.FirstChild) then |
| 89 | TDOMText(element.FirstChild).NodeValue := AValue |
| 90 | else
|
| 91 | element.AppendChild(AParent.OwnerDocument.CreateTextNode(AValue)); |
| 92 | end;
|
| 93 | |
| 94 | class function TXmlHelper.ReadString(AParent: TDOMElement; AName, ADefault: string): string; |
| 95 | var
|
| 96 | element: TDOMElement; |
| 97 | begin
|
| 98 | element := FindChild(AParent, AName); |
| 99 | if assigned(element) and assigned(element.FirstChild) then |
| 100 | Result := TDOMText(element.FirstChild).Data |
| 101 | else
|
| 102 | Result := ADefault; |
| 103 | end;
|
| 104 | |
| 105 | class procedure TXmlHelper.WriteInteger(AParent: TDOMElement; AName: string; |
| 106 | AValue: Integer); |
| 107 | begin
|
| 108 | WriteString(AParent, AName, IntToStr(AValue)); |
| 109 | end;
|
| 110 | |
| 111 | class function TXmlHelper.ReadInteger(AParent: TDOMElement; AName: string; |
| 112 | ADefault: Integer): Integer; |
| 113 | begin
|
| 114 | if not TryStrToInt(ReadString(AParent, AName, ''), Result) then |
| 115 | Result := ADefault; |
| 116 | end;
|
| 117 | |
| 118 | class procedure TXmlHelper.WriteBoolean(AParent: TDOMElement; AName: string; |
| 119 | AValue: Boolean); |
| 120 | begin
|
| 121 | WriteString(AParent, AName, BoolToStr(AValue)); |
| 122 | end;
|
| 123 | |
| 124 | class function TXmlHelper.ReadBoolean(AParent: TDOMElement; AName: string; |
| 125 | ADefault: Boolean): Boolean; |
| 126 | begin
|
| 127 | Result := StrToBool(ReadString(AParent, AName, BoolToStr(ADefault))); |
| 128 | end;
|
| 129 | |
| 130 | class procedure TXmlHelper.WriteCoords(AParent: TDOMElement; AName: string; AX, |
| 131 | AY: Integer); |
| 132 | var
|
| 133 | element: TDOMElement; |
| 134 | begin
|
| 135 | element := AssureElement(AParent, AName); |
| 136 | element.AttribStrings['x'] := IntToStr(AX);
|
| 137 | element.AttribStrings['y'] := IntToStr(AY);
|
| 138 | end;
|
| 139 | |
| 140 | class function TXmlHelper.ReadCoords(AParent: TDOMElement; AName: string; out |
| 141 | X, Y: Integer): Boolean; |
| 142 | var
|
| 143 | element: TDOMElement; |
| 144 | tempX, tempY: Integer; |
| 145 | begin
|
| 146 | element := FindChild(AParent, AName); |
| 147 | Result := assigned(element) and TryStrToInt(element.AttribStrings['x'], tempX) |
| 148 | and TryStrToInt(element.AttribStrings['y'], tempY); |
| 149 | |
| 150 | if Result then |
| 151 | begin
|
| 152 | X := tempX; |
| 153 | Y := tempY; |
| 154 | end;
|
| 155 | end;
|
| 156 | |
| 157 | end.
|
| 158 |