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 "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);