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