-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathintf.ZUGFeRDXmlUtils.pas
317 lines (261 loc) · 9.51 KB
/
intf.ZUGFeRDXmlUtils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
{* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.}
unit intf.ZUGFeRDXmlUtils;
interface
uses
System.Classes, System.SysUtils, System.IOUtils, System.DateUtils
,Xml.XMLDoc, Xml.xmldom, Xml.XMLIntf
,Xml.Win.msxmldom, Winapi.MSXMLIntf, Winapi.msxml
,intf.ZUGFeRDExceptions
,intf.ZUGFeRDHelper
;
type
TZUGFeRDXmlUtils = class(TObject)
public
class function NodeAsBool(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager = nil; } defaultValue: Boolean = False): Boolean;
class function NodeAsString(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager = nil; } defaultValue: string = ''): string;
class function NodeAsInt(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager = nil; } defaultValue: Integer = 0): Integer;
/// <summary>
/// reads the value from given xpath and interprets the value as decimal
/// </summary>
class function NodeAsDecimal(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager = nil; } defaultValue: IZUGFeRDNullableParam<Currency> = Nil): ZUGFeRDNullable<Currency>; overload;
class function NodeAsDecimal(node: IXmlDomNode; const xpath: string; defaultValue: Currency): ZUGFeRDNullable<Currency>; overload;
/// <summary>
/// reads the value from given xpath and interprets the value as date time
/// </summary>
class function NodeAsDouble(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager = nil; } defaultValue: IZUGFeRDNullableParam<Double> = Nil): ZUGFeRDNullable<Double>; overload;
class function NodeAsDouble(node: IXmlDomNode; const xpath: string; defaultValue: Double): ZUGFeRDNullable<Double>; overload;
class function NodeAsDateTime(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager = nil; } defaultValue: IZUGFeRDNullableParam<TDateTime> = Nil): ZUGFeRDNullable<TDateTime>;
class function SafeParseDateTime(const year: string = '0'; const month: string = '0'; const day: string = '0'; const hour: string = '0'; const minute: string = '0'; const second: string = '0'): TDateTime;
end;
implementation
class function TZUGFeRDXmlUtils.NodeAsBool(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager;}
defaultValue: Boolean): Boolean;
var
value: string;
begin
Result := defaultValue;
if node = nil then
exit;
value := TZUGFeRDXmlUtils.NodeAsString(node, xpath{, nsmgr});
if value.IsEmpty then
exit
else
begin
value := value.Trim.ToLower;
if (value = 'true') or (value = '1') then
Result := true
else
Result := false;
end;
end;
class function TZUGFeRDXmlUtils.NodeAsString(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager;}
defaultValue: string): string;
var
_node: IXmlDomNode;
begin
Result := defaultValue;
if node = nil then
exit;
try
_node := node.SelectSingleNode(xpath{, nsmgr});
if _node <> nil then
Result := _node.Text;
exit;
except
//on XPathException do
// Exit(defaultValue);
on ex: Exception do
raise ex;
end;
end;
class function TZUGFeRDXmlUtils.NodeAsInt(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager;}
defaultValue: Integer): Integer;
var
temp: string;
begin
Result := defaultValue;
if node = nil then
exit;
temp := TZUGFeRDXmlUtils.NodeAsString(node, xpath{, nsmgr});
TryStrToInt(temp, Result);
end;
class function TZUGFeRDXmlUtils.NodeAsDecimal(node: IXmlDomNode; const xpath: string; defaultValue: IZUGFeRDNullableParam<Currency> = Nil): ZUGFeRDNullable<Currency>;
var
temp: string;
ResCurrency: Currency;
begin
Result:= defaultValue;
if node = nil then
exit;
temp := TZUGFeRDXmlUtils.NodeAsString(node, xpath{, nsmgr});
if TryStrToCurr(temp, ResCurrency, FormatSettings.Invariant) then
Result:= ResCurrency;
end;
class function TZUGFeRDXmlUtils.NodeAsDecimal(node: IXmlDomNode; const xpath: string; defaultValue: Currency): ZUGFeRDNullable<Currency>;
begin
Result:= TZUGFeRDXmlUtils.NodeAsDecimal(node, xpath, IZUGFeRDNullableParam<Currency>(ZUGFeRDNullable<Currency>(defaultValue)))
end;
(*
var
temp: string;
ResCurrency: Currency;
begin
Result:= defaultValue;
if node = nil then
exit;
temp := TZUGFeRDXmlUtils.NodeAsString(node, xpath{, nsmgr});
if TryStrToCurr(temp, ResCurrency, FormatSettings.Invariant) then
Result:= ResCurrency;
end;
*)
class function TZUGFeRDXmlUtils.NodeAsDouble(node: IXmlDomNode; const xpath: string; defaultValue: IZUGFeRDNullableParam<Double> = Nil): ZUGFeRDNullable<Double>;
var
temp: string;
ResDouble: Double;
begin
Result := defaultValue;
if node = nil then
exit;
temp := TZUGFeRDXmlUtils.NodeAsString(node, xpath{, nsmgr});
if TryStrToFloat(temp, ResDouble, FormatSettings.Invariant) then
Result:= ResDouble
end;
class function TZUGFeRDXmlUtils.NodeAsDouble(node: IXmlDomNode; const xpath: string; defaultValue: Double): ZUGFeRDNullable<Double>;
begin
Result:= TZUGFeRDXmlUtils.NodeAsDouble(node, xpath, IZUGFeRDNullableParam<Double>(ZUGFeRDNullable<Double>(defaultValue)))
end;
(*
var
temp: string;
ResDouble: Double;
begin
Result := defaultValue;
if node = nil then
exit;
temp := TZUGFeRDXmlUtils.NodeAsString(node, xpath{, nsmgr});
if TryStrToFloat(temp, ResDouble, FormatSettings.Invariant) then
Result:= ResDouble
end;
*)
class function TZUGFeRDXmlUtils.NodeAsDateTime(node: IXmlDomNode; const xpath: string; {nsmgr: XmlNamespaceManager;}
defaultValue: IZUGFeRDNullableParam<TDateTime>): ZUGFeRDNullable<TDateTime>;
var
format, rawValue, year, month, day, hour, minute, second, week: string;
dateNode: IXmlDomNode;
aDayOfWeek: Integer;
jan4,aDay : TDateTime;
begin
Result := defaultValue;
if node = nil then
exit;
format := '';
dateNode := node.SelectSingleNode(xpath{, nsmgr});
if dateNode = nil then
exit;
if dateNode.Attributes.getNamedItem('format') <> nil then
if dateNode.Attributes.getNamedItem('format').text <> '' then
format := dateNode.Attributes.getNamedItem('format').text;
// to protect from space and /r /n characters
rawValue := Trim(dateNode.text);
if (rawValue = '') then // we have to deal with real-life ZUGFeRD files :(
exit;
if (format='102') then
begin
if Length(rawValue) <> 8 then
raise Exception.Create('Wrong length of datetime element (format 102)');
year := Copy(rawValue, 1, 4);
month := Copy(rawValue, 5, 2);
day := Copy(rawValue, 7, 2);
Result := SafeParseDateTime(year, month, day);
exit;
end else
if (format='610') then
begin
if Length(rawValue) <> 6 then
raise Exception.Create('Wrong length of datetime element (format 610)');
year := Copy(rawValue, 1, 4);
month := Copy(rawValue, 5, 2);
day := '1';
Result := SafeParseDateTime(year, month, day);
exit;
end else
if (format='616') then
begin
if Length(rawValue) <> 6 then
raise Exception.Create('Wrong length of datetime element (format 616)');
year := Copy(rawValue, 1, 4);
week := Copy(rawValue, 5, 2);
jan4 := EncodeDate(StrToInt(year), 1, 4);
aDay := IncWeek(jan4, StrToInt(week) - 1);
aDayOfWeek := DayOfWeek(aDay) - 1;
Result := aDay - aDayOfWeek;
exit;
end;
// if none of the codes above is present, use fallback approach
if Length(rawValue) = 8 then
begin
year := Copy(rawValue, 1, 4);
month := Copy(rawValue, 5, 2);
day := Copy(rawValue, 7, 2);
Result := SafeParseDateTime(year, month, day);
exit;
end
else if (Length(rawValue) = 10) and (rawValue[5] = '-') and (rawValue[8] = '-') then // yyyy-mm-dd
begin
year := Copy(rawValue, 1, 4);
month := Copy(rawValue, 6, 2);
day := Copy(rawValue, 9, 2);
Result := SafeParseDateTime(year, month, day);
exit;
end
else if Length(rawValue) = 19 then
begin
year := Copy(rawValue, 1, 4);
month := Copy(rawValue, 6, 2);
day := Copy(rawValue, 9, 2);
hour := Copy(rawValue, 12, 2);
minute := Copy(rawValue, 15, 2);
second := Copy(rawValue, 18, 2);
Result := SafeParseDateTime(year, month, day, hour, minute, second);
exit;
end
else
raise TZUGFeRDUnsupportedException.Create('Invalid length of datetime value');
end;
class function TZUGFeRDXmlUtils.SafeParseDateTime(const year: string = '0';
const month: string = '0'; const day: string = '0'; const hour: string = '0';
const minute: string = '0'; const second: string = '0'): TDateTime;
var
_year, _month, _day, _hour, _minute, _second: Integer;
begin
Result := 0;
if not TryStrToInt(year, _year) then
exit;
if not TryStrToInt(month, _month) then
exit;
if not TryStrToInt(day, _day) then
exit;
if not TryStrToInt(hour, _hour) then
exit;
if not TryStrToInt(minute, _minute) then
exit;
if not TryStrToInt(second, _second) then
exit;
Result := EncodeDateTime(_year, _month, _day, _hour, _minute, _second, 0);
end;
end.