Qt 4.8
qcastingplatform.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
48 template <typename TSubClass, const bool issueError>
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>
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>
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>
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>
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>
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 
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
QString stringValue() const
Returns the string value of this Item.
Definition: qitem_p.h:302
QExplicitlySharedDataPointer< ItemType > type() const
Returns the ItemType this Item is of.
Definition: qitem_p.h:365
Provides casting functionality for classes, such as CastAs or NumberFN, which needs to perform castin...
bool isAtomicValue() const
Determines whether this item is an atomic value, or a node.
Definition: qitem_p.h:335
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
static QTestResult::TestLocation location
Definition: qtestresult.cpp:63
Base class for all instances that represents something at a certain location.
static QString formatData(const QXmlNodeModelIndex node)
Definition: qitem_p.h:440