source: tools/Image resize/UXMLUtils.pas

Last change on this file was 276, checked in by chronos, 4 years ago
  • Added: Action to convert magenta transparency png images to images with alpha channel.
File size: 7.8 KB
Line 
1unit UXMLUtils;
2
3{$mode delphi}
4
5interface
6
7uses
8 {$IFDEF WINDOWS}Windows,{$ENDIF}
9 Classes, SysUtils, DateUtils, DOM, xmlread;
10
11function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
12function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
13procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
14procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
15procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
16procedure WriteString(Node: TDOMNode; Name: string; Value: string);
17procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
18function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
19function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
20function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
21function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
22function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime): TDateTime;
23procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
24
25
26implementation
27
28procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
29var
30 Parser: TDOMParser;
31 Src: TXMLInputSource;
32 InFile: TFileStream;
33begin
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;
45end;
46
47function GetTimeZoneBias: Integer;
48{$IFDEF WINDOWS}
49var
50 TimeZoneInfo: TTimeZoneInformation;
51begin
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}
60end;
61{$ELSE}
62begin
63 Result := 0;
64end;
65{$ENDIF}
66
67function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
68var
69 I: Integer;
70 Matched: Boolean;
71begin
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;
89end;
90
91function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
92var
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;
102begin
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
140end;
141
142function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
143const
144 Neg: array[Boolean] of string = ('+', '-');
145var
146 Bias: Integer;
147begin
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 }
157end;
158
159procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
160var
161 NewNode: TDOMNode;
162begin
163 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
164 NewNode.TextContent := DOMString(IntToStr(Value));
165 Node.AppendChild(NewNode);
166end;
167
168procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
169var
170 NewNode: TDOMNode;
171begin
172 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
173 NewNode.TextContent := DOMString(IntToStr(Value));
174 Node.AppendChild(NewNode);
175end;
176
177procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
178var
179 NewNode: TDOMNode;
180begin
181 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
182 NewNode.TextContent := DOMString(BoolToStr(Value));
183 Node.AppendChild(NewNode);
184end;
185
186procedure WriteString(Node: TDOMNode; Name: string; Value: string);
187var
188 NewNode: TDOMNode;
189begin
190 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
191 NewNode.TextContent := DOMString(Value);
192 Node.AppendChild(NewNode);
193end;
194
195procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
196var
197 NewNode: TDOMNode;
198begin
199 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
200 NewNode.TextContent := DOMString(DateTimeToXMLTime(Value));
201 Node.AppendChild(NewNode);
202end;
203
204function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
205var
206 NewNode: TDOMNode;
207begin
208 Result := DefaultValue;
209 NewNode := Node.FindNode(DOMString(Name));
210 if Assigned(NewNode) then
211 Result := StrToInt(string(NewNode.TextContent));
212end;
213
214function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
215var
216 NewNode: TDOMNode;
217begin
218 Result := DefaultValue;
219 NewNode := Node.FindNode(DOMString(Name));
220 if Assigned(NewNode) then
221 Result := StrToInt64(string(NewNode.TextContent));
222end;
223
224function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
225var
226 NewNode: TDOMNode;
227begin
228 Result := DefaultValue;
229 NewNode := Node.FindNode(DOMString(Name));
230 if Assigned(NewNode) then
231 Result := StrToBool(string(NewNode.TextContent));
232end;
233
234function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
235var
236 NewNode: TDOMNode;
237begin
238 Result := DefaultValue;
239 NewNode := Node.FindNode(DOMString(Name));
240 if Assigned(NewNode) then
241 Result := string(NewNode.TextContent);
242end;
243
244function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime
245 ): TDateTime;
246var
247 NewNode: TDOMNode;
248begin
249 Result := DefaultValue;
250 NewNode := Node.FindNode(DOMString(Name));
251 if Assigned(NewNode) then
252 Result := XMLTimeToDateTime(string(NewNode.TextContent));
253end;
254
255end.
256
Note: See TracBrowser for help on using the repository browser.