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 ****************************************************************************/
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"
49 #include "qcardinalityverifier_p.h"
53 using namespace QPatternist;
55 QString CardinalityVerifier::wrongCardinality(const Cardinality &req,
56 const Cardinality &got)
58 return QtXmlPatterns::tr("Required cardinality is %1; got cardinality %2.")
59 .arg(formatType(req), formatType(got));
62 Expression::Ptr CardinalityVerifier::verifyCardinality(const Expression::Ptr &operand,
63 const Cardinality &requiredCard,
64 const StaticContext::Ptr &context,
65 const ReportContext::ErrorCode code)
67 const Cardinality opCard(operand->staticType()->cardinality());
69 if(requiredCard.isMatch(opCard))
71 else if(requiredCard.canMatch(opCard))
72 return Expression::Ptr(new CardinalityVerifier(operand, requiredCard, code));
73 else if(context->compatModeEnabled() &&
76 return GenericPredicate::createFirstItem(operand);
80 /* Sequences within this cardinality can never match. */
81 context->error(wrongCardinality(requiredCard, opCard), code, operand.data());
86 CardinalityVerifier::CardinalityVerifier(const Expression::Ptr &operand,
87 const Cardinality &card,
88 const ReportContext::ErrorCode code)
89 : SingleContainer(operand),
91 m_allowsMany(operand->staticType()->cardinality().allowsMany()),
94 Q_ASSERT_X(m_reqCard != Cardinality::zeroOrMore(), Q_FUNC_INFO,
95 "It makes no sense to use CardinalityVerifier for cardinality zero-or-more.");
98 Item::Iterator::Ptr CardinalityVerifier::evaluateSequence(const DynamicContext::Ptr &context) const
100 const Item::Iterator::Ptr it(m_operand->evaluateSequence(context));
101 const Item next(it->next());
105 const Item next2(it->next());
109 if(m_reqCard.allowsMany())
115 return Item::Iterator::Ptr(new InsertionIterator(it, 1, makeListIterator(start)));
119 context->error(wrongCardinality(m_reqCard, Cardinality::twoOrMore()), m_errorCode, this);
120 return CommonValues::emptyIterator;
125 /* We might be instantiated for the empty sequence. */
126 if(m_reqCard.isEmpty())
128 context->error(wrongCardinality(m_reqCard, Cardinality::twoOrMore()), m_errorCode, this);
129 return CommonValues::emptyIterator;
132 return makeSingletonIterator(next);
137 if(m_reqCard.allowsEmpty())
138 return CommonValues::emptyIterator;
141 context->error(wrongCardinality(m_reqCard, Cardinality::twoOrMore()), m_errorCode, this);
142 return CommonValues::emptyIterator;
147 Item CardinalityVerifier::evaluateSingleton(const DynamicContext::Ptr &context) const
151 const Item::Iterator::Ptr it(m_operand->evaluateSequence(context));
152 const Item item(it->next());
158 context->error(wrongCardinality(m_reqCard, Cardinality::twoOrMore()),
165 else if(m_reqCard.allowsEmpty())
169 context->error(wrongCardinality(m_reqCard), m_errorCode, this);
175 const Item item(m_operand->evaluateSingleton(context));
179 else if(m_reqCard.allowsEmpty())
183 context->error(wrongCardinality(m_reqCard), m_errorCode, this);
189 const SourceLocationReflection *CardinalityVerifier::actualReflection() const
191 return m_operand->actualReflection();
194 Expression::Ptr CardinalityVerifier::compress(const StaticContext::Ptr &context)
196 if(m_reqCard.isMatch(m_operand->staticType()->cardinality()))
197 return m_operand->compress(context);
199 return SingleContainer::compress(context);
202 SequenceType::List CardinalityVerifier::expectedOperandTypes() const
204 SequenceType::List result;
205 result.append(CommonSequenceTypes::ZeroOrMoreItems);
209 SequenceType::Ptr CardinalityVerifier::staticType() const
211 return makeGenericSequenceType(m_operand->staticType()->itemType(), m_reqCard);
214 ExpressionVisitorResult::Ptr CardinalityVerifier::accept(const ExpressionVisitor::Ptr &visitor) const
216 return visitor->visit(this);
219 Expression::ID CardinalityVerifier::id() const
221 return IDCardinalityVerifier;