| 1 | unit UXMLUtils;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | {$IFDEF WINDOWS}Windows,{$ENDIF}
|
|---|
| 9 | Classes, SysUtils, DateUtils, DOM, xmlread;
|
|---|
| 10 |
|
|---|
| 11 | function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
|
|---|
| 12 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
|
|---|
| 13 | procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
|
|---|
| 14 | procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
|
|---|
| 15 | procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
|
|---|
| 16 | procedure WriteString(Node: TDOMNode; Name: string; Value: string);
|
|---|
| 17 | procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
|
|---|
| 18 | function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
|
|---|
| 19 | function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
|
|---|
| 20 | function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
|
|---|
| 21 | function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
|
|---|
| 22 | function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime): TDateTime;
|
|---|
| 23 | procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | implementation
|
|---|
| 27 |
|
|---|
| 28 | procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
|
|---|
| 29 | var
|
|---|
| 30 | Parser: TDOMParser;
|
|---|
| 31 | Src: TXMLInputSource;
|
|---|
| 32 | InFile: TFileStream;
|
|---|
| 33 | begin
|
|---|
| 34 | try
|
|---|
| 35 | InFile := TFileStream.Create(FileName, fmOpenRead);
|
|---|
| 36 | Src := TXMLInputSource.Create(InFile);
|
|---|
| 37 | Parser := TDOMParser.Create;
|
|---|
| 38 | Parser.Options.PreserveWhitespace := True;
|
|---|
| 39 | Parser.Parse(Src, Doc);
|
|---|
| 40 | finally
|
|---|
| 41 | Src.Free;
|
|---|
| 42 | Parser.Free;
|
|---|
| 43 | InFile.Free;
|
|---|
| 44 | end;
|
|---|
| 45 | end;
|
|---|
| 46 |
|
|---|
| 47 | function GetTimeZoneBias: Integer;
|
|---|
| 48 | {$IFDEF WINDOWS}
|
|---|
| 49 | var
|
|---|
| 50 | TimeZoneInfo: TTimeZoneInformation;
|
|---|
| 51 | begin
|
|---|
| 52 | {$push}{$warn 5057 off}
|
|---|
| 53 | case GetTimeZoneInformation(TimeZoneInfo) of
|
|---|
| 54 | TIME_ZONE_ID_STANDARD: Result := TimeZoneInfo.Bias + TimeZoneInfo.StandardBias;
|
|---|
| 55 | TIME_ZONE_ID_DAYLIGHT: Result := TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias;
|
|---|
| 56 | else
|
|---|
| 57 | Result := 0;
|
|---|
| 58 | end;
|
|---|
| 59 | {$pop}
|
|---|
| 60 | end;
|
|---|
| 61 | {$ELSE}
|
|---|
| 62 | begin
|
|---|
| 63 | Result := 0;
|
|---|
| 64 | end;
|
|---|
| 65 | {$ENDIF}
|
|---|
| 66 |
|
|---|
| 67 | function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
|
|---|
| 68 | var
|
|---|
| 69 | I: Integer;
|
|---|
| 70 | Matched: Boolean;
|
|---|
| 71 | begin
|
|---|
| 72 | I := 1;
|
|---|
| 73 | Matched := True;
|
|---|
| 74 | while (I < Length(Source)) and Matched do begin
|
|---|
| 75 | Matched := True;
|
|---|
| 76 | if (Source[I] = Delimiter) then Matched := False;
|
|---|
| 77 | //for J := 1 to Length(Allowed) do
|
|---|
| 78 | // if Source[I] = Allowed[J] then Matched := True;
|
|---|
| 79 | if Matched then Inc(I);
|
|---|
| 80 | end;
|
|---|
| 81 | if (Delimiter = Copy(Source, I, Length(Delimiter))) or (I = Length(Source)) then begin
|
|---|
| 82 | Output := Copy(Source, 1, I - 1);
|
|---|
| 83 | Delete(Source, 1, Length(Output) + Length(Delimiter));
|
|---|
| 84 | Result := True;
|
|---|
| 85 | end else begin
|
|---|
| 86 | Output := '';
|
|---|
| 87 | Result := False;
|
|---|
| 88 | end;
|
|---|
| 89 | end;
|
|---|
| 90 |
|
|---|
| 91 | function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
|
|---|
| 92 | var
|
|---|
| 93 | Part: string;
|
|---|
| 94 | Year: Integer;
|
|---|
| 95 | Month: Integer;
|
|---|
| 96 | Day: Integer;
|
|---|
| 97 | Hour: Integer;
|
|---|
| 98 | Minute: Integer;
|
|---|
| 99 | Second: Integer;
|
|---|
| 100 | SecondFraction: Double;
|
|---|
| 101 | Millisecond: Integer;
|
|---|
| 102 | begin
|
|---|
| 103 | if LeftCutString(XMLDateTime, Part, '-') then
|
|---|
| 104 | Year := StrToInt(Part);
|
|---|
| 105 | if LeftCutString(XMLDateTime, Part, '-') then
|
|---|
| 106 | Month := StrToInt(Part);
|
|---|
| 107 | if Pos('T', XMLDateTime) > 0 then begin
|
|---|
| 108 | if LeftCutString(XMLDateTime, Part, 'T') then
|
|---|
| 109 | Day := StrToInt(Part);
|
|---|
| 110 | if LeftCutString(XMLDateTime, Part, ':') then
|
|---|
| 111 | Hour := StrToInt(Part);
|
|---|
| 112 | if LeftCutString(XMLDateTime, Part, ':') then
|
|---|
| 113 | Minute := StrToInt(Part);
|
|---|
| 114 | if Pos('.', XMLDateTime) > 0 then begin
|
|---|
| 115 | if LeftCutString(XMLDateTime, Part, '.') then
|
|---|
| 116 | Second := StrToInt(Part);
|
|---|
| 117 | if Pos('+', XMLDateTime) > 0 then
|
|---|
| 118 | LeftCutString(XMLDateTime, Part, '+') else
|
|---|
| 119 | if Pos('-', XMLDateTime) > 0 then
|
|---|
| 120 | LeftCutString(XMLDateTime, Part, '-') else
|
|---|
| 121 | if Pos('Z', XMLDateTime) > 0 then
|
|---|
| 122 | LeftCutString(XMLDateTime, Part, 'Z');
|
|---|
| 123 | SecondFraction := StrToFloat('0' + DefaultFormatSettings.DecimalSeparator + Part);
|
|---|
| 124 | Millisecond := Trunc(SecondFraction * 1000);
|
|---|
| 125 | end else begin
|
|---|
| 126 | if Pos('+', XMLDateTime) > 0 then
|
|---|
| 127 | LeftCutString(XMLDateTime, Part, '+') else
|
|---|
| 128 | if Pos('-', XMLDateTime) > 0 then
|
|---|
| 129 | LeftCutString(XMLDateTime, Part, '-') else
|
|---|
| 130 | if Pos('Z', XMLDateTime) > 0 then
|
|---|
| 131 | LeftCutString(XMLDateTime, Part, 'Z');
|
|---|
| 132 | Second := StrToInt(Part);
|
|---|
| 133 | Millisecond := 0;
|
|---|
| 134 | end;
|
|---|
| 135 | end else begin
|
|---|
| 136 | Day := StrToInt(XMLDateTime);
|
|---|
| 137 | end;
|
|---|
| 138 | Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
|
|---|
| 139 | // TODO: Correct time by zone bias
|
|---|
| 140 | end;
|
|---|
| 141 |
|
|---|
| 142 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
|
|---|
| 143 | const
|
|---|
| 144 | Neg: array[Boolean] of string = ('+', '-');
|
|---|
| 145 | var
|
|---|
| 146 | Bias: Integer;
|
|---|
| 147 | begin
|
|---|
| 148 | Result := FormatDateTime('yyyy''-''mm''-''dd''T''hh'':''nn'':''ss''.''zzz', Value); { Do not localize }
|
|---|
| 149 | Bias := GetTimeZoneBias;
|
|---|
| 150 | if (Bias <> 0) and ApplyLocalBias then
|
|---|
| 151 | begin
|
|---|
| 152 | Result := Format('%s%s%.2d:%.2d', [Result, Neg[Bias > 0], { Do not localize }
|
|---|
| 153 | Abs(Bias) div MinsPerHour,
|
|---|
| 154 | Abs(Bias) mod MinsPerHour]);
|
|---|
| 155 | end else
|
|---|
| 156 | Result := Result + 'Z'; { Do not localize }
|
|---|
| 157 | end;
|
|---|
| 158 |
|
|---|
| 159 | procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
|
|---|
| 160 | var
|
|---|
| 161 | NewNode: TDOMNode;
|
|---|
| 162 | begin
|
|---|
| 163 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 164 | NewNode.TextContent := DOMString(IntToStr(Value));
|
|---|
| 165 | Node.AppendChild(NewNode);
|
|---|
| 166 | end;
|
|---|
| 167 |
|
|---|
| 168 | procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
|
|---|
| 169 | var
|
|---|
| 170 | NewNode: TDOMNode;
|
|---|
| 171 | begin
|
|---|
| 172 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 173 | NewNode.TextContent := DOMString(IntToStr(Value));
|
|---|
| 174 | Node.AppendChild(NewNode);
|
|---|
| 175 | end;
|
|---|
| 176 |
|
|---|
| 177 | procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
|
|---|
| 178 | var
|
|---|
| 179 | NewNode: TDOMNode;
|
|---|
| 180 | begin
|
|---|
| 181 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 182 | NewNode.TextContent := DOMString(BoolToStr(Value));
|
|---|
| 183 | Node.AppendChild(NewNode);
|
|---|
| 184 | end;
|
|---|
| 185 |
|
|---|
| 186 | procedure WriteString(Node: TDOMNode; Name: string; Value: string);
|
|---|
| 187 | var
|
|---|
| 188 | NewNode: TDOMNode;
|
|---|
| 189 | begin
|
|---|
| 190 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 191 | NewNode.TextContent := DOMString(Value);
|
|---|
| 192 | Node.AppendChild(NewNode);
|
|---|
| 193 | end;
|
|---|
| 194 |
|
|---|
| 195 | procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
|
|---|
| 196 | var
|
|---|
| 197 | NewNode: TDOMNode;
|
|---|
| 198 | begin
|
|---|
| 199 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 200 | NewNode.TextContent := DOMString(DateTimeToXMLTime(Value));
|
|---|
| 201 | Node.AppendChild(NewNode);
|
|---|
| 202 | end;
|
|---|
| 203 |
|
|---|
| 204 | function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
|
|---|
| 205 | var
|
|---|
| 206 | NewNode: TDOMNode;
|
|---|
| 207 | begin
|
|---|
| 208 | Result := DefaultValue;
|
|---|
| 209 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 210 | if Assigned(NewNode) then
|
|---|
| 211 | Result := StrToInt(string(NewNode.TextContent));
|
|---|
| 212 | end;
|
|---|
| 213 |
|
|---|
| 214 | function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
|
|---|
| 215 | var
|
|---|
| 216 | NewNode: TDOMNode;
|
|---|
| 217 | begin
|
|---|
| 218 | Result := DefaultValue;
|
|---|
| 219 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 220 | if Assigned(NewNode) then
|
|---|
| 221 | Result := StrToInt64(string(NewNode.TextContent));
|
|---|
| 222 | end;
|
|---|
| 223 |
|
|---|
| 224 | function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
|
|---|
| 225 | var
|
|---|
| 226 | NewNode: TDOMNode;
|
|---|
| 227 | begin
|
|---|
| 228 | Result := DefaultValue;
|
|---|
| 229 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 230 | if Assigned(NewNode) then
|
|---|
| 231 | Result := StrToBool(string(NewNode.TextContent));
|
|---|
| 232 | end;
|
|---|
| 233 |
|
|---|
| 234 | function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
|
|---|
| 235 | var
|
|---|
| 236 | NewNode: TDOMNode;
|
|---|
| 237 | begin
|
|---|
| 238 | Result := DefaultValue;
|
|---|
| 239 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 240 | if Assigned(NewNode) then
|
|---|
| 241 | Result := string(NewNode.TextContent);
|
|---|
| 242 | end;
|
|---|
| 243 |
|
|---|
| 244 | function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime
|
|---|
| 245 | ): TDateTime;
|
|---|
| 246 | var
|
|---|
| 247 | NewNode: TDOMNode;
|
|---|
| 248 | begin
|
|---|
| 249 | Result := DefaultValue;
|
|---|
| 250 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 251 | if Assigned(NewNode) then
|
|---|
| 252 | Result := XMLTimeToDateTime(string(NewNode.TextContent));
|
|---|
| 253 | end;
|
|---|
| 254 |
|
|---|
| 255 | end.
|
|---|
| 256 |
|
|---|