Qt 4.8
qcardinalityverifier.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 
42 #include "qcommonsequencetypes_p.h"
43 #include "qcommonvalues_p.h"
44 #include "qgenericpredicate_p.h"
45 #include "qgenericsequencetype_p.h"
46 #include "qinsertioniterator_p.h"
47 #include "qpatternistlocale_p.h"
48 
49 #include "qcardinalityverifier_p.h"
50 
52 
53 using namespace QPatternist;
54 
56  const Cardinality &got)
57 {
58  return QtXmlPatterns::tr("Required cardinality is %1; got cardinality %2.")
59  .arg(formatType(req), formatType(got));
60 }
61 
63  const Cardinality &requiredCard,
64  const StaticContext::Ptr &context,
65  const ReportContext::ErrorCode code)
66 {
67  const Cardinality opCard(operand->staticType()->cardinality());
68 
69  if(requiredCard.isMatch(opCard))
70  return operand;
71  else if(requiredCard.canMatch(opCard))
72  return Expression::Ptr(new CardinalityVerifier(operand, requiredCard, code));
73  else if(context->compatModeEnabled() &&
74  !opCard.isEmpty())
75  {
76  return GenericPredicate::createFirstItem(operand);
77  }
78  else
79  {
80  /* Sequences within this cardinality can never match. */
81  context->error(wrongCardinality(requiredCard, opCard), code, operand.data());
82  return operand;
83  }
84 }
85 
87  const Cardinality &card,
88  const ReportContext::ErrorCode code)
89  : SingleContainer(operand),
90  m_reqCard(card),
91  m_allowsMany(operand->staticType()->cardinality().allowsMany()),
92  m_errorCode(code)
93 {
95  "It makes no sense to use CardinalityVerifier for cardinality zero-or-more.");
96 }
97 
99 {
101  const Item next(it->next());
102 
103  if(next)
104  {
105  const Item next2(it->next());
106 
107  if(next2)
108  {
109  if(m_reqCard.allowsMany())
110  {
111  Item::List start;
112  start.append(next);
113  start.append(next2);
114 
116  }
117  else
118  {
121  }
122  }
123  else
124  {
125  /* We might be instantiated for the empty sequence. */
126  if(m_reqCard.isEmpty())
127  {
130  }
131  else
132  return makeSingletonIterator(next);
133  }
134  }
135  else
136  {
137  if(m_reqCard.allowsEmpty())
139  else
140  {
143  }
144  }
145 }
146 
148 {
149  if(m_allowsMany)
150  {
152  const Item item(it->next());
153 
154  if(item)
155  {
156  if(it->next())
157  {
159  m_errorCode, this);
160  return Item();
161  }
162  else
163  return item;
164  }
165  else if(m_reqCard.allowsEmpty())
166  return Item();
167  else
168  {
169  context->error(wrongCardinality(m_reqCard), m_errorCode, this);
170  return Item();
171  }
172  }
173  else
174  {
175  const Item item(m_operand->evaluateSingleton(context));
176 
177  if(item)
178  return item;
179  else if(m_reqCard.allowsEmpty())
180  return Item();
181  else
182  {
183  context->error(wrongCardinality(m_reqCard), m_errorCode, this);
184  return Item();
185  }
186  }
187 }
188 
190 {
191  return m_operand->actualReflection();
192 }
193 
195 {
197  return m_operand->compress(context);
198  else
199  return SingleContainer::compress(context);
200 }
201 
203 {
204  SequenceType::List result;
206  return result;
207 }
208 
210 {
211  return makeGenericSequenceType(m_operand->staticType()->itemType(), m_reqCard);
212 }
213 
215 {
216  return visitor->visit(this);
217 }
218 
220 {
221  return IDCardinalityVerifier;
222 }
223 
static Cardinality zeroOrMore()
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
virtual bool compatModeEnabled() const =0
virtual SequenceType::Ptr staticType() const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static const SequenceType::Ptr ZeroOrMoreItems
#define it(className, varName)
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
QAbstractXmlForwardIterator< T >::Ptr makeListIterator(const QList< T > &list)
The QString class provides a Unicode character string.
Definition: qstring.h:83
static const EmptyIterator< Item >::Ptr emptyIterator
static QString wrongCardinality(const Cardinality &req, const Cardinality &got=Cardinality::empty())
static Cardinality twoOrMore()
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< Item > > Ptr
A smart pointer wrapping an instance of a QAbstractXmlForwardIterator subclass.
static Expression::Ptr verifyCardinality(const Expression::Ptr &operand, const Cardinality &card, const StaticContext::Ptr &context, const ReportContext::ErrorCode code=ReportContext::XPTY0004)
virtual const SourceLocationReflection * actualReflection() const
Returns this.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
virtual ExpressionVisitorResult::Ptr accept(const ExpressionVisitor::Ptr &visitor) const
The namespace for the internal API of QtXmlPatterns.
virtual Cardinality cardinality() const =0
Conceptually inserts one QAbstractXmlForwardIterator into another, make two QAbstractXmlForwardIterat...
virtual SequenceType::Ptr staticType() const =0
virtual SequenceType::List expectedOperandTypes() const
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
bool canMatch(const Cardinality &other) const
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
CardinalityVerifier(const Expression::Ptr &operand, const Cardinality &card, const ReportContext::ErrorCode code)
virtual Item evaluateSingleton(const DynamicContext::Ptr &) const
Represents a cardinality, a possible , often represented by occurrence indicators.
const ReportContext::ErrorCode m_errorCode
virtual ItemType::Ptr itemType() const =0
bool isMatch(const Cardinality &other) const
virtual const SourceLocationReflection * actualReflection() const
Returns this.
static Expression::Ptr createFirstItem(const Expression::Ptr &sourceExpression)
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Base class for all instances that represents something at a certain location.
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
Base class for expressions that has exactly one operand.
#define Q_FUNC_INFO
Definition: qglobal.h:1871