1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 * @short This file is included by qcastingplatform_p.h.
45 * If you need includes in this file, put them in CasttingPlatform.h, outside of the namespace.
48 template <typename TSubClass, const bool issueError>
49 Item CastingPlatform<TSubClass, issueError>::castWithCaster(const Item &sourceValue,
50 const AtomicCaster::Ptr &caster,
51 const ReportContext::Ptr &context) const
53 Q_ASSERT(sourceValue);
57 const Item retval(caster->castFrom(sourceValue, context));
61 if(retval.template as<AtomicValue>()->hasError())
63 issueCastError(retval, sourceValue, context);
73 template <typename TSubClass, const bool issueError>
74 Item CastingPlatform<TSubClass, issueError>::cast(const Item &sourceValue,
75 const ReportContext::Ptr &context) const
77 Q_ASSERT(sourceValue);
79 Q_ASSERT(targetType());
82 return castWithCaster(sourceValue, m_caster, context);
85 bool castImpossible = false;
86 const AtomicCaster::Ptr caster(locateCaster(sourceValue.type(), context, castImpossible, static_cast<const TSubClass *>(this), targetType()));
88 if(!issueError && castImpossible)
90 /* If we're supposed to issue an error(issueError) then this
91 * line will never be reached, because locateCaster() will in
93 return ValidationError::createError();
96 return castWithCaster(sourceValue, caster, context);
100 template <typename TSubClass, const bool issueError>
101 bool CastingPlatform<TSubClass, issueError>::prepareCasting(const ReportContext::Ptr &context,
102 const ItemType::Ptr &sourceType)
104 Q_ASSERT(sourceType);
107 if(*sourceType == *BuiltinTypes::xsAnyAtomicType ||
108 *sourceType == *BuiltinTypes::item ||
109 *sourceType == *CommonSequenceTypes::Empty ||
110 *sourceType == *BuiltinTypes::numeric)
111 return true; /* The type could not be narrowed better than xs:anyAtomicType
112 or numeric at compile time. We'll do lookup at runtime instead. */
114 bool castImpossible = false;
115 m_caster = locateCaster(sourceType, context, castImpossible, static_cast<const TSubClass *>(this), targetType());
117 return !castImpossible;
120 template <typename TSubClass, const bool issueError>
121 AtomicCaster::Ptr CastingPlatform<TSubClass, issueError>::locateCaster(const ItemType::Ptr &sourceType,
122 const ReportContext::Ptr &context,
123 bool &castImpossible,
124 const SourceLocationReflection *const location,
125 const ItemType::Ptr &targetType)
127 Q_ASSERT(sourceType);
128 Q_ASSERT(targetType);
130 const AtomicCasterLocator::Ptr locator(static_cast<AtomicType *>(
131 targetType.data())->casterLocator());
136 context->error(QtXmlPatterns::tr("No casting is possible with %1 as the target type.")
137 .arg(formatType(context->namePool(), targetType)),
138 ReportContext::XPTY0004, location);
141 castImpossible = true;
143 return AtomicCaster::Ptr();
146 const AtomicCaster::Ptr caster(static_cast<const AtomicType *>(sourceType.data())->accept(locator, location));
151 context->error(QtXmlPatterns::tr("It is not possible to cast from %1 to %2.")
152 .arg(formatType(context->namePool(), sourceType))
153 .arg(formatType(context->namePool(), targetType)),
154 ReportContext::XPTY0004, location);
157 castImpossible = true;
159 return AtomicCaster::Ptr();
165 template <typename TSubClass, const bool issueError>
166 void CastingPlatform<TSubClass, issueError>::checkTargetType(const ReportContext::Ptr &context) const
170 const ItemType::Ptr tType(targetType());
172 Q_ASSERT(tType->isAtomicType());
173 const AtomicType::Ptr asAtomic(tType);
175 /* This catches casting to xs:NOTATION and xs:anyAtomicType. */
176 if(asAtomic->isAbstract())
178 context->error(QtXmlPatterns::tr("Casting to %1 is not possible because it "
179 "is an abstract type, and can therefore never be instantiated.")
180 .arg(formatType(context->namePool(), tType)),
181 ReportContext::XPST0080,
182 static_cast<const TSubClass*>(this));
186 template <typename TSubClass, const bool issueError>
187 void CastingPlatform<TSubClass, issueError>::issueCastError(const Item &validationError,
188 const Item &sourceValue,
189 const ReportContext::Ptr &context) const
191 Q_ASSERT(validationError);
193 Q_ASSERT(validationError.isAtomicValue());
194 Q_ASSERT(validationError.template as<AtomicValue>()->hasError());
196 const ValidationError::Ptr err(validationError.template as<ValidationError>());
197 QString msg(err->message());
201 msg = QtXmlPatterns::tr("It's not possible to cast the value %1 of type %2 to %3")
202 .arg(formatData(sourceValue.stringValue()))
203 .arg(formatType(context->namePool(), sourceValue.type()))
204 .arg(formatType(context->namePool(), targetType()));
208 Q_ASSERT(!msg.isEmpty());
209 msg = QtXmlPatterns::tr("Failure when casting from %1 to %2: %3")
210 .arg(formatType(context->namePool(), sourceValue.type()))
211 .arg(formatType(context->namePool(), targetType()))
215 /* If m_errorCode is FORG0001, we assume our sub-classer doesn't have a
216 * special wish about error code, so then we use the error object's code.
218 context->error(msg, m_errorCode == ReportContext::FORG0001 ? err->errorCode() : m_errorCode,
219 static_cast<const TSubClass*>(this));