1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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;