1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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));