|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 /** |
|
43 * @file |
|
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. |
|
46 */ |
|
47 |
|
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 |
|
52 { |
|
53 Q_ASSERT(sourceValue); |
|
54 Q_ASSERT(caster); |
|
55 Q_ASSERT(context); |
|
56 |
|
57 const Item retval(caster->castFrom(sourceValue, context)); |
|
58 |
|
59 if(issueError) |
|
60 { |
|
61 if(retval.template as<AtomicValue>()->hasError()) |
|
62 { |
|
63 issueCastError(retval, sourceValue, context); |
|
64 return Item(); |
|
65 } |
|
66 else |
|
67 return retval; |
|
68 } |
|
69 else |
|
70 return retval; |
|
71 } |
|
72 |
|
73 template <typename TSubClass, const bool issueError> |
|
74 Item CastingPlatform<TSubClass, issueError>::cast(const Item &sourceValue, |
|
75 const ReportContext::Ptr &context) const |
|
76 { |
|
77 Q_ASSERT(sourceValue); |
|
78 Q_ASSERT(context); |
|
79 Q_ASSERT(targetType()); |
|
80 |
|
81 if(m_caster) |
|
82 return castWithCaster(sourceValue, m_caster, context); |
|
83 else |
|
84 { |
|
85 bool castImpossible = false; |
|
86 const AtomicCaster::Ptr caster(locateCaster(sourceValue.type(), context, castImpossible, static_cast<const TSubClass *>(this), targetType())); |
|
87 |
|
88 if(!issueError && castImpossible) |
|
89 { |
|
90 /* If we're supposed to issue an error(issueError) then this |
|
91 * line will never be reached, because locateCaster() will in |
|
92 * that case throw. */ |
|
93 return ValidationError::createError(); |
|
94 } |
|
95 else |
|
96 return castWithCaster(sourceValue, caster, context); |
|
97 } |
|
98 } |
|
99 |
|
100 template <typename TSubClass, const bool issueError> |
|
101 bool CastingPlatform<TSubClass, issueError>::prepareCasting(const ReportContext::Ptr &context, |
|
102 const ItemType::Ptr &sourceType) |
|
103 { |
|
104 Q_ASSERT(sourceType); |
|
105 Q_ASSERT(context); |
|
106 |
|
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. */ |
|
113 |
|
114 bool castImpossible = false; |
|
115 m_caster = locateCaster(sourceType, context, castImpossible, static_cast<const TSubClass *>(this), targetType()); |
|
116 |
|
117 return !castImpossible; |
|
118 } |
|
119 |
|
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) |
|
126 { |
|
127 Q_ASSERT(sourceType); |
|
128 Q_ASSERT(targetType); |
|
129 |
|
130 const AtomicCasterLocator::Ptr locator(static_cast<AtomicType *>( |
|
131 targetType.data())->casterLocator()); |
|
132 if(!locator) |
|
133 { |
|
134 if(issueError) |
|
135 { |
|
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); |
|
139 } |
|
140 else |
|
141 castImpossible = true; |
|
142 |
|
143 return AtomicCaster::Ptr(); |
|
144 } |
|
145 |
|
146 const AtomicCaster::Ptr caster(static_cast<const AtomicType *>(sourceType.data())->accept(locator, location)); |
|
147 if(!caster) |
|
148 { |
|
149 if(issueError) |
|
150 { |
|
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); |
|
155 } |
|
156 else |
|
157 castImpossible = true; |
|
158 |
|
159 return AtomicCaster::Ptr(); |
|
160 } |
|
161 |
|
162 return caster; |
|
163 } |
|
164 |
|
165 template <typename TSubClass, const bool issueError> |
|
166 void CastingPlatform<TSubClass, issueError>::checkTargetType(const ReportContext::Ptr &context) const |
|
167 { |
|
168 Q_ASSERT(context); |
|
169 |
|
170 const ItemType::Ptr tType(targetType()); |
|
171 Q_ASSERT(tType); |
|
172 Q_ASSERT(tType->isAtomicType()); |
|
173 const AtomicType::Ptr asAtomic(tType); |
|
174 |
|
175 /* This catches casting to xs:NOTATION and xs:anyAtomicType. */ |
|
176 if(asAtomic->isAbstract()) |
|
177 { |
|
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)); |
|
183 } |
|
184 } |
|
185 |
|
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 |
|
190 { |
|
191 Q_ASSERT(validationError); |
|
192 Q_ASSERT(context); |
|
193 Q_ASSERT(validationError.isAtomicValue()); |
|
194 Q_ASSERT(validationError.template as<AtomicValue>()->hasError()); |
|
195 |
|
196 const ValidationError::Ptr err(validationError.template as<ValidationError>()); |
|
197 QString msg(err->message()); |
|
198 |
|
199 if(msg.isNull()) |
|
200 { |
|
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())); |
|
205 } |
|
206 else |
|
207 { |
|
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())) |
|
212 .arg(msg); |
|
213 } |
|
214 |
|
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. |
|
217 */ |
|
218 context->error(msg, m_errorCode == ReportContext::FORG0001 ? err->errorCode() : m_errorCode, |
|
219 static_cast<const TSubClass*>(this)); |
|
220 } |
|
221 |