1 /****************************************************************************
3 ** Copyright (C) 2012 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 "qcommonnamespaces_p.h"
44 #include "qcommonsequencetypes_p.h"
45 #include "qfunctionfactory_p.h"
46 #include "qgeneralcomparison_p.h"
47 #include "qliteral_p.h"
48 #include "qschemanumeric_p.h"
49 #include "qvaluecomparison_p.h"
51 #include "qoptimizerblocks_p.h"
55 using namespace QPatternist;
57 ByIDIdentifier::ByIDIdentifier(const Expression::ID id) : m_id(id)
61 bool ByIDIdentifier::matches(const Expression::Ptr &expr) const
63 return expr->is(m_id);
66 ComparisonIdentifier::ComparisonIdentifier(const QVector<Expression::ID> hosts,
67 const AtomicComparator::Operator op) : m_hosts(hosts),
72 bool ComparisonIdentifier::matches(const Expression::Ptr &e) const
74 const Expression::ID eID = e->id();
76 if(eID == Expression::IDGeneralComparison)
78 if(m_hosts.contains(Expression::IDGeneralComparison))
79 return e->as<GeneralComparison>()->operatorID() == m_op;
83 else if(eID == Expression::IDValueComparison)
85 if(m_hosts.contains(Expression::IDValueComparison))
86 return e->as<ValueComparison>()->operatorID() == m_op;
94 BySequenceTypeIdentifier::BySequenceTypeIdentifier(const SequenceType::Ptr &seqType) : m_seqType(seqType)
99 bool BySequenceTypeIdentifier::matches(const Expression::Ptr &expr) const
101 const SequenceType::Ptr t(expr->staticType());
103 return m_seqType->itemType()->xdtTypeMatches(t->itemType())
105 m_seqType->cardinality().isMatch(t->cardinality());
108 IntegerIdentifier::IntegerIdentifier(const xsInteger num) : m_num(num)
112 bool IntegerIdentifier::matches(const Expression::Ptr &expr) const
114 return expr->is(Expression::IDIntegerValue) &&
115 expr->as<Literal>()->item().as<Numeric>()->toInteger() == m_num;
118 BooleanIdentifier::BooleanIdentifier(const bool value) : m_value(value)
122 bool BooleanIdentifier::matches(const Expression::Ptr &expr) const
124 return expr->is(Expression::IDBooleanValue) &&
125 expr->evaluateEBV(DynamicContext::Ptr()) == m_value;
128 ByIDCreator::ByIDCreator(const Expression::ID id) : m_id(id)
130 Q_ASSERT(id != Expression::IDIgnorableExpression);
133 Expression::Ptr ByIDCreator::create(const Expression::List &operands,
134 const StaticContext::Ptr &context,
135 const SourceLocationReflection *const r) const
137 return create(m_id, operands, context, r);
140 Expression::Ptr ByIDCreator::create(const Expression::ID id,
141 const Expression::List &operands,
142 const StaticContext::Ptr &context,
143 const SourceLocationReflection *const r)
147 QXmlName::LocalNameCode fnName;
151 case Expression::IDExistsFN:
153 fnName = StandardLocalNames::exists;
156 case Expression::IDEmptyFN:
158 fnName = StandardLocalNames::empty;
163 Q_ASSERT_X(false, Q_FUNC_INFO,
164 "Cannot create an expression of requested type; m_id is wrong.");
165 return Expression::Ptr();
169 /* The reason we don't simply do 'new ExistsFN()' ourselves, is that all FunctionCall
170 * instances needs their FunctionSignature in order to function, and the FunctionFactories
172 const QXmlName qName(StandardNamespaces::fn, fnName);
174 const Expression::Ptr result(context->functionSignatures()->createFunctionCall(qName, operands, context, r));
175 context->wrapExpressionWith(r, result);