1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
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));
|
---|
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);
|
---|
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) const
|
---|
124 | {
|
---|
125 | Q_ASSERT(sourceType);
|
---|
126 | Q_ASSERT(targetType());
|
---|
127 |
|
---|
128 | const AtomicCasterLocator::Ptr locator(static_cast<AtomicType *>(
|
---|
129 | targetType().data())->casterLocator());
|
---|
130 | if(!locator)
|
---|
131 | {
|
---|
132 | if(issueError)
|
---|
133 | {
|
---|
134 | context->error(QtXmlPatterns::tr("No casting is possible with %1 as the target type.")
|
---|
135 | .arg(formatType(context->namePool(), targetType())),
|
---|
136 | ReportContext::XPTY0004, static_cast<const TSubClass *>(this));
|
---|
137 | }
|
---|
138 | else
|
---|
139 | castImpossible = true;
|
---|
140 |
|
---|
141 | return AtomicCaster::Ptr();
|
---|
142 | }
|
---|
143 |
|
---|
144 | const AtomicCaster::Ptr caster(static_cast<const AtomicType *>(sourceType.data())->accept(locator, static_cast<const TSubClass *>(this)));
|
---|
145 | if(!caster)
|
---|
146 | {
|
---|
147 | if(issueError)
|
---|
148 | {
|
---|
149 | context->error(QtXmlPatterns::tr("It is not possible to cast from %1 to %2.")
|
---|
150 | .arg(formatType(context->namePool(), sourceType))
|
---|
151 | .arg(formatType(context->namePool(), targetType())),
|
---|
152 | ReportContext::XPTY0004, static_cast<const TSubClass *>(this));
|
---|
153 | }
|
---|
154 | else
|
---|
155 | castImpossible = true;
|
---|
156 |
|
---|
157 | return AtomicCaster::Ptr();
|
---|
158 | }
|
---|
159 |
|
---|
160 | return caster;
|
---|
161 | }
|
---|
162 |
|
---|
163 | template <typename TSubClass, const bool issueError>
|
---|
164 | void CastingPlatform<TSubClass, issueError>::checkTargetType(const ReportContext::Ptr &context) const
|
---|
165 | {
|
---|
166 | Q_ASSERT(context);
|
---|
167 |
|
---|
168 | const ItemType::Ptr tType(targetType());
|
---|
169 | Q_ASSERT(tType);
|
---|
170 | Q_ASSERT(tType->isAtomicType());
|
---|
171 | const AtomicType::Ptr asAtomic(tType);
|
---|
172 |
|
---|
173 | /* This catches casting to xs:NOTATION and xs:anyAtomicType. */
|
---|
174 | if(asAtomic->isAbstract())
|
---|
175 | {
|
---|
176 | context->error(QtXmlPatterns::tr("Casting to %1 is not possible because it "
|
---|
177 | "is an abstract type, and can therefore never be instantiated.")
|
---|
178 | .arg(formatType(context->namePool(), tType)),
|
---|
179 | ReportContext::XPST0080,
|
---|
180 | static_cast<const TSubClass*>(this));
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | template <typename TSubClass, const bool issueError>
|
---|
185 | void CastingPlatform<TSubClass, issueError>::issueCastError(const Item &validationError,
|
---|
186 | const Item &sourceValue,
|
---|
187 | const ReportContext::Ptr &context) const
|
---|
188 | {
|
---|
189 | Q_ASSERT(validationError);
|
---|
190 | Q_ASSERT(context);
|
---|
191 | Q_ASSERT(validationError.isAtomicValue());
|
---|
192 | Q_ASSERT(validationError.template as<AtomicValue>()->hasError());
|
---|
193 |
|
---|
194 | const ValidationError::Ptr err(validationError.template as<ValidationError>());
|
---|
195 | QString msg(err->message());
|
---|
196 |
|
---|
197 | if(msg.isNull())
|
---|
198 | {
|
---|
199 | msg = QtXmlPatterns::tr("It's not possible to cast the value %1 of type %2 to %3")
|
---|
200 | .arg(formatData(sourceValue.stringValue()))
|
---|
201 | .arg(formatType(context->namePool(), sourceValue.type()))
|
---|
202 | .arg(formatType(context->namePool(), targetType()));
|
---|
203 | }
|
---|
204 | else
|
---|
205 | {
|
---|
206 | Q_ASSERT(!msg.isEmpty());
|
---|
207 | msg = QtXmlPatterns::tr("Failure when casting from %1 to %2: %3")
|
---|
208 | .arg(formatType(context->namePool(), sourceValue.type()))
|
---|
209 | .arg(formatType(context->namePool(), targetType()))
|
---|
210 | .arg(msg);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* If m_errorCode is FORG0001, we assume our sub-classer doesn't have a
|
---|
214 | * special wish about error code, so then we use the error object's code.
|
---|
215 | */
|
---|
216 | context->error(msg, m_errorCode == ReportContext::FORG0001 ? err->errorCode() : m_errorCode,
|
---|
217 | static_cast<const TSubClass*>(this));
|
---|
218 | }
|
---|
219 |
|
---|