1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include "qabstractfloat_p.h"
45 #include "qanyuri_p.h"
46 #include "qbase64binary_p.h"
47 #include "qboolean_p.h"
48 #include "qcommonvalues_p.h"
50 #include "qschemadatetime_p.h"
51 #include "qdaytimeduration_p.h"
52 #include "qdecimal_p.h"
53 #include "qduration_p.h"
55 #include "qgmonth_p.h"
56 #include "qgmonthday_p.h"
58 #include "qgyearmonth_p.h"
59 #include "qhexbinary_p.h"
60 #include "qinteger_p.h"
61 #include "qatomicstring_p.h"
62 #include "qschematime_p.h"
63 #include "quntypedatomic_p.h"
64 #include "qyearmonthduration_p.h"
66 #include "qatomiccasters_p.h"
70 using namespace QPatternist;
72 Item ToUntypedAtomicCaster::castFrom(const Item &from,
73 const QExplicitlySharedDataPointer<DynamicContext> &) const
75 return UntypedAtomic::fromValue(from.stringValue());
78 Item ToAnyURICaster::castFrom(const Item &from,
79 const QExplicitlySharedDataPointer<DynamicContext> &) const
81 return toItem(AnyURI::fromLexical(from.stringValue()));
84 Item Base64BinaryToHexBinaryCaster::castFrom(const Item &from,
85 const QExplicitlySharedDataPointer<DynamicContext> &) const
87 return HexBinary::fromValue(from.as<Base64Binary>()->asByteArray());
90 Item StringToHexBinaryCaster::castFrom(const Item &from,
91 const QExplicitlySharedDataPointer<DynamicContext> &context) const
93 return HexBinary::fromLexical(context->namePool(), from.stringValue());
96 Item HexBinaryToBase64BinaryCaster::castFrom(const Item &from,
97 const QExplicitlySharedDataPointer<DynamicContext> &) const
99 return Base64Binary::fromValue(from.as<Base64Binary>()->asByteArray());
102 Item StringToBase64BinaryCaster::castFrom(const Item &from,
103 const QExplicitlySharedDataPointer<DynamicContext> &) const
105 return Base64Binary::fromLexical(from.stringValue());
108 Item NumericToBooleanCaster::castFrom(const Item &from,
109 const QExplicitlySharedDataPointer<DynamicContext> &) const
111 const xsDouble val = from.as<Numeric>()->toDouble();
112 if(Double::isEqual(val, 0.0) || qIsNaN(val))
113 return CommonValues::BooleanFalse;
115 return CommonValues::BooleanTrue;
118 Item StringToBooleanCaster::castFrom(const Item &from,
119 const QExplicitlySharedDataPointer<DynamicContext> &) const
121 return Boolean::fromLexical(from.stringValue());
124 Item StringToDecimalCaster::castFrom(const Item &from,
125 const QExplicitlySharedDataPointer<DynamicContext> &) const
127 return Decimal::fromLexical(from.stringValue());
130 Item StringToIntegerCaster::castFrom(const Item &from,
131 const QExplicitlySharedDataPointer<DynamicContext> &) const
133 return Integer::fromLexical(from.stringValue());
136 Item BooleanToDecimalCaster::castFrom(const Item &from,
137 const QExplicitlySharedDataPointer<DynamicContext> &context) const
139 if(from.as<AtomicValue>()->evaluateEBV(context))
140 return CommonValues::DecimalOne;
142 return CommonValues::DecimalZero;
145 Item BooleanToIntegerCaster::castFrom(const Item &from,
146 const QExplicitlySharedDataPointer<DynamicContext> &context) const
148 if(from.as<AtomicValue>()->evaluateEBV(context))
149 return CommonValues::IntegerOne;
151 return CommonValues::IntegerZero;
154 Item SelfToSelfCaster::castFrom(const Item &from,
155 const QExplicitlySharedDataPointer<DynamicContext> &) const
160 Item StringToGYearCaster::castFrom(const Item &from,
161 const QExplicitlySharedDataPointer<DynamicContext> &) const
163 return GYear::fromLexical(from.stringValue());
166 Item StringToGDayCaster::castFrom(const Item &from,
167 const QExplicitlySharedDataPointer<DynamicContext> &) const
169 return GDay::fromLexical(from.stringValue());
172 Item StringToGMonthCaster::castFrom(const Item &from,
173 const QExplicitlySharedDataPointer<DynamicContext> &) const
175 return GMonth::fromLexical(from.stringValue());
178 Item StringToGYearMonthCaster::castFrom(const Item &from,
179 const QExplicitlySharedDataPointer<DynamicContext> &) const
181 return GYearMonth::fromLexical(from.stringValue());
184 Item StringToGMonthDayCaster::castFrom(const Item &from,
185 const QExplicitlySharedDataPointer<DynamicContext> &) const
187 return GMonthDay::fromLexical(from.stringValue());
190 Item StringToDateTimeCaster::castFrom(const Item &from,
191 const QExplicitlySharedDataPointer<DynamicContext> &) const
193 return DateTime::fromLexical(from.stringValue());
196 Item StringToTimeCaster::castFrom(const Item &from,
197 const QExplicitlySharedDataPointer<DynamicContext> &) const
199 return SchemaTime::fromLexical(from.stringValue());
202 Item StringToDateCaster::castFrom(const Item &from,
203 const QExplicitlySharedDataPointer<DynamicContext> &) const
205 return Date::fromLexical(from.stringValue());
208 Item StringToDurationCaster::castFrom(const Item &from,
209 const QExplicitlySharedDataPointer<DynamicContext> &) const
211 return Duration::fromLexical(from.stringValue());
214 Item StringToDayTimeDurationCaster::castFrom(const Item &from,
215 const QExplicitlySharedDataPointer<DynamicContext> &) const
217 return toItem(DayTimeDuration::fromLexical(from.stringValue()));
220 Item AbstractDurationToDayTimeDurationCaster::castFrom(const Item &from,
221 const QExplicitlySharedDataPointer<DynamicContext> &) const
223 const AbstractDuration *const val = from.as<AbstractDuration>();
225 return toItem(DayTimeDuration::fromComponents(val->isPositive(),
233 Item AbstractDurationToYearMonthDurationCaster::castFrom(const Item &from,
234 const QExplicitlySharedDataPointer<DynamicContext> &) const
236 const AbstractDuration *const val = from.as<AbstractDuration>();
238 return toItem(YearMonthDuration::fromComponents(val->isPositive(),
243 Item AbstractDurationToDurationCaster::castFrom(const Item &from,
244 const QExplicitlySharedDataPointer<DynamicContext> &) const
246 const AbstractDuration *const val = from.as<AbstractDuration>();
248 return Duration::fromComponents(val->isPositive(),
258 Item StringToYearMonthDurationCaster::castFrom(const Item &from,
259 const QExplicitlySharedDataPointer<DynamicContext> &) const
261 return YearMonthDuration::fromLexical(from.stringValue());
264 Item AbstractDateTimeToGYearCaster::castFrom(const Item &from,
265 const QExplicitlySharedDataPointer<DynamicContext> &) const
267 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
268 // TODO DT dt.setDateOnly(true);
270 return GYear::fromDateTime(dt);
273 Item AbstractDateTimeToGYearMonthCaster::castFrom(const Item &from,
274 const QExplicitlySharedDataPointer<DynamicContext> &) const
276 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
277 // TODO DT dt.setDateOnly(true);
279 return GYearMonth::fromDateTime(dt);
282 Item AbstractDateTimeToGMonthCaster::castFrom(const Item &from,
283 const QExplicitlySharedDataPointer<DynamicContext> &) const
285 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
286 // TODO DT dt.setDateOnly(true);
288 return GMonth::fromDateTime(dt);
291 Item AbstractDateTimeToGMonthDayCaster::castFrom(const Item &from,
292 const QExplicitlySharedDataPointer<DynamicContext> &) const
294 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
295 // TODO DT dt.setDateOnly(true);
297 return GMonthDay::fromDateTime(dt);
300 Item AbstractDateTimeToGDayCaster::castFrom(const Item &from,
301 const QExplicitlySharedDataPointer<DynamicContext> &) const
303 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
304 // TODO DT dt.setDateOnly(true);
306 return GDay::fromDateTime(dt);
309 Item AbstractDateTimeToDateTimeCaster::castFrom(const Item &from,
310 const QExplicitlySharedDataPointer<DynamicContext> &) const
312 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
313 // TODO DT dt.setDateOnly(false);
315 return DateTime::fromDateTime(dt);
318 Item AbstractDateTimeToDateCaster::castFrom(const Item &from,
319 const QExplicitlySharedDataPointer<DynamicContext> &) const
321 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
322 // TODO DT dt.setDateOnly(true);
324 return Date::fromDateTime(dt);
327 Item AbstractDateTimeToTimeCaster::castFrom(const Item &from,
328 const QExplicitlySharedDataPointer<DynamicContext> &) const
330 QDateTime dt(from.as<AbstractDateTime>()->toDateTime());
331 // TODO DT dt.setDateOnly(false);
333 return SchemaTime::fromDateTime(dt);