1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <qnumeric.h>
41
42#include "qabstractfloat_p.h"
43#include "qanyuri_p.h"
44#include "qbase64binary_p.h"
45#include "qboolean_p.h"
46#include "qcommonvalues_p.h"
47#include "qdate_p.h"
48#include "qschemadatetime_p.h"
49#include "qdaytimeduration_p.h"
50#include "qdecimal_p.h"
51#include "qduration_p.h"
52#include "qgday_p.h"
53#include "qgmonth_p.h"
54#include "qgmonthday_p.h"
55#include "qgyear_p.h"
56#include "qgyearmonth_p.h"
57#include "qhexbinary_p.h"
58#include "qinteger_p.h"
59#include "qatomicstring_p.h"
60#include "qschematime_p.h"
61#include "quntypedatomic_p.h"
62#include "qyearmonthduration_p.h"
63
64#include "qatomiccasters_p.h"
65
66QT_BEGIN_NAMESPACE
67
68using namespace QPatternist;
69
70Item ToUntypedAtomicCaster::castFrom(const Item &from,
71 const QExplicitlySharedDataPointer<DynamicContext> &) const
72{
73 return UntypedAtomic::fromValue(value: from.stringValue());
74}
75
76Item ToAnyURICaster::castFrom(const Item &from,
77 const QExplicitlySharedDataPointer<DynamicContext> &) const
78{
79 return toItem(atomicValue: AnyURI::fromLexical(value: from.stringValue()));
80}
81
82Item Base64BinaryToHexBinaryCaster::castFrom(const Item &from,
83 const QExplicitlySharedDataPointer<DynamicContext> &) const
84{
85 return HexBinary::fromValue(data: from.as<Base64Binary>()->asByteArray());
86}
87
88Item StringToHexBinaryCaster::castFrom(const Item &from,
89 const QExplicitlySharedDataPointer<DynamicContext> &context) const
90{
91 return HexBinary::fromLexical(np: context->namePool(), value: from.stringValue());
92}
93
94Item HexBinaryToBase64BinaryCaster::castFrom(const Item &from,
95 const QExplicitlySharedDataPointer<DynamicContext> &) const
96{
97 return Base64Binary::fromValue(data: from.as<Base64Binary>()->asByteArray());
98}
99
100Item StringToBase64BinaryCaster::castFrom(const Item &from,
101 const QExplicitlySharedDataPointer<DynamicContext> &) const
102{
103 return Base64Binary::fromLexical(value: from.stringValue());
104}
105
106Item NumericToBooleanCaster::castFrom(const Item &from,
107 const QExplicitlySharedDataPointer<DynamicContext> &) const
108{
109 const xsDouble val = from.as<Numeric>()->toDouble();
110 if(Double::isEqual(a: val, b: 0.0) || qIsNaN(d: val))
111 return CommonValues::BooleanFalse;
112 else
113 return CommonValues::BooleanTrue;
114}
115
116Item StringToBooleanCaster::castFrom(const Item &from,
117 const QExplicitlySharedDataPointer<DynamicContext> &) const
118{
119 return Boolean::fromLexical(val: from.stringValue());
120}
121
122Item StringToDecimalCaster::castFrom(const Item &from,
123 const QExplicitlySharedDataPointer<DynamicContext> &) const
124{
125 return Decimal::fromLexical(strNumeric: from.stringValue());
126}
127
128Item StringToIntegerCaster::castFrom(const Item &from,
129 const QExplicitlySharedDataPointer<DynamicContext> &) const
130{
131 return Integer::fromLexical(strNumeric: from.stringValue());
132}
133
134Item BooleanToDecimalCaster::castFrom(const Item &from,
135 const QExplicitlySharedDataPointer<DynamicContext> &context) const
136{
137 if(from.as<AtomicValue>()->evaluateEBV(context))
138 return CommonValues::DecimalOne;
139 else
140 return CommonValues::DecimalZero;
141}
142
143Item BooleanToIntegerCaster::castFrom(const Item &from,
144 const QExplicitlySharedDataPointer<DynamicContext> &context) const
145{
146 if(from.as<AtomicValue>()->evaluateEBV(context))
147 return CommonValues::IntegerOne;
148 else
149 return CommonValues::IntegerZero;
150}
151
152Item SelfToSelfCaster::castFrom(const Item &from,
153 const QExplicitlySharedDataPointer<DynamicContext> &) const
154{
155 return from;
156}
157
158Item StringToGYearCaster::castFrom(const Item &from,
159 const QExplicitlySharedDataPointer<DynamicContext> &) const
160{
161 return GYear::fromLexical(string: from.stringValue());
162}
163
164Item StringToGDayCaster::castFrom(const Item &from,
165 const QExplicitlySharedDataPointer<DynamicContext> &) const
166{
167 return GDay::fromLexical(string: from.stringValue());
168}
169
170Item StringToGMonthCaster::castFrom(const Item &from,
171 const QExplicitlySharedDataPointer<DynamicContext> &) const
172{
173 return GMonth::fromLexical(string: from.stringValue());
174}
175
176Item StringToGYearMonthCaster::castFrom(const Item &from,
177 const QExplicitlySharedDataPointer<DynamicContext> &) const
178{
179 return GYearMonth::fromLexical(string: from.stringValue());
180}
181
182Item StringToGMonthDayCaster::castFrom(const Item &from,
183 const QExplicitlySharedDataPointer<DynamicContext> &) const
184{
185 return GMonthDay::fromLexical(string: from.stringValue());
186}
187
188Item StringToDateTimeCaster::castFrom(const Item &from,
189 const QExplicitlySharedDataPointer<DynamicContext> &) const
190{
191 return DateTime::fromLexical(string: from.stringValue());
192}
193
194Item StringToTimeCaster::castFrom(const Item &from,
195 const QExplicitlySharedDataPointer<DynamicContext> &) const
196{
197 return SchemaTime::fromLexical(string: from.stringValue());
198}
199
200Item StringToDateCaster::castFrom(const Item &from,
201 const QExplicitlySharedDataPointer<DynamicContext> &) const
202{
203 return Date::fromLexical(string: from.stringValue());
204}
205
206Item StringToDurationCaster::castFrom(const Item &from,
207 const QExplicitlySharedDataPointer<DynamicContext> &) const
208{
209 return Duration::fromLexical(string: from.stringValue());
210}
211
212Item StringToDayTimeDurationCaster::castFrom(const Item &from,
213 const QExplicitlySharedDataPointer<DynamicContext> &) const
214{
215 return toItem(atomicValue: DayTimeDuration::fromLexical(string: from.stringValue()));
216}
217
218Item AbstractDurationToDayTimeDurationCaster::castFrom(const Item &from,
219 const QExplicitlySharedDataPointer<DynamicContext> &) const
220{
221 const AbstractDuration *const val = from.as<AbstractDuration>();
222
223 return toItem(atomicValue: DayTimeDuration::fromComponents(isPositive: val->isPositive(),
224 days: val->days(),
225 hours: val->hours(),
226 minutes: val->minutes(),
227 seconds: val->seconds(),
228 mseconds: val->mseconds()));
229}
230
231Item AbstractDurationToYearMonthDurationCaster::castFrom(const Item &from,
232 const QExplicitlySharedDataPointer<DynamicContext> &) const
233{
234 const AbstractDuration *const val = from.as<AbstractDuration>();
235
236 return toItem(atomicValue: YearMonthDuration::fromComponents(isPositive: val->isPositive(),
237 years: val->years(),
238 months: val->months()));
239}
240
241Item AbstractDurationToDurationCaster::castFrom(const Item &from,
242 const QExplicitlySharedDataPointer<DynamicContext> &) const
243{
244 const AbstractDuration *const val = from.as<AbstractDuration>();
245
246 return Duration::fromComponents(isPositive: val->isPositive(),
247 years: val->years(),
248 months: val->months(),
249 days: val->days(),
250 hours: val->hours(),
251 minutes: val->minutes(),
252 seconds: val->seconds(),
253 mseconds: val->mseconds());
254}
255
256Item StringToYearMonthDurationCaster::castFrom(const Item &from,
257 const QExplicitlySharedDataPointer<DynamicContext> &) const
258{
259 return YearMonthDuration::fromLexical(string: from.stringValue());
260}
261
262Item AbstractDateTimeToGYearCaster::castFrom(const Item &from,
263 const QExplicitlySharedDataPointer<DynamicContext> &) const
264{
265 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
266 // TODO DT dt.setDateOnly(true);
267
268 return GYear::fromDateTime(dt);
269}
270
271Item AbstractDateTimeToGYearMonthCaster::castFrom(const Item &from,
272 const QExplicitlySharedDataPointer<DynamicContext> &) const
273{
274 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
275 // TODO DT dt.setDateOnly(true);
276
277 return GYearMonth::fromDateTime(dt);
278}
279
280Item AbstractDateTimeToGMonthCaster::castFrom(const Item &from,
281 const QExplicitlySharedDataPointer<DynamicContext> &) const
282{
283 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
284 // TODO DT dt.setDateOnly(true);
285
286 return GMonth::fromDateTime(dt);
287}
288
289Item AbstractDateTimeToGMonthDayCaster::castFrom(const Item &from,
290 const QExplicitlySharedDataPointer<DynamicContext> &) const
291{
292 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
293 // TODO DT dt.setDateOnly(true);
294
295 return GMonthDay::fromDateTime(dt);
296}
297
298Item AbstractDateTimeToGDayCaster::castFrom(const Item &from,
299 const QExplicitlySharedDataPointer<DynamicContext> &) const
300{
301 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
302 // TODO DT dt.setDateOnly(true);
303
304 return GDay::fromDateTime(dt);
305}
306
307Item AbstractDateTimeToDateTimeCaster::castFrom(const Item &from,
308 const QExplicitlySharedDataPointer<DynamicContext> &) const
309{
310 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
311 // TODO DT dt.setDateOnly(false);
312
313 return DateTime::fromDateTime(dt);
314}
315
316Item AbstractDateTimeToDateCaster::castFrom(const Item &from,
317 const QExplicitlySharedDataPointer<DynamicContext> &) const
318{
319 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
320 // TODO DT dt.setDateOnly(true);
321
322 return Date::fromDateTime(date: dt);
323}
324
325Item AbstractDateTimeToTimeCaster::castFrom(const Item &from,
326 const QExplicitlySharedDataPointer<DynamicContext> &) const
327{
328 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
329 // TODO DT dt.setDateOnly(false);
330
331 return SchemaTime::fromDateTime(dt);
332}
333
334QT_END_NAMESPACE
335

source code of qtxmlpatterns/src/xmlpatterns/data/qatomiccasters.cpp