1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qcommonsequencetypes_p.h"
41 #include "qcommonvalues_p.h"
42 #include "qemptysequence_p.h"
43 #include "qinteger_p.h"
44 #include "qschemanumeric_p.h"
45 #include "qrangeiterator_p.h"
46
47 #include "qnodecomparison_p.h"
48
49 QT_BEGIN_NAMESPACE
50
51 using namespace QPatternist;
52
NodeComparison(const Expression::Ptr & operand1,const QXmlNodeModelIndex::DocumentOrder op,const Expression::Ptr & operand2)53 NodeComparison::NodeComparison(const Expression::Ptr &operand1,
54 const QXmlNodeModelIndex::DocumentOrder op,
55 const Expression::Ptr &operand2)
56 : PairContainer(operand1, operand2)
57 , m_op(op)
58 {
59 Q_ASSERT(op == QXmlNodeModelIndex::Precedes ||
60 op == QXmlNodeModelIndex::Follows ||
61 op == QXmlNodeModelIndex::Is);
62 }
63
evaluateSingleton(const DynamicContext::Ptr & context) const64 Item NodeComparison::evaluateSingleton(const DynamicContext::Ptr &context) const
65 {
66 switch(evaluate(context))
67 {
68 case True:
69 return CommonValues::BooleanTrue;
70 case False:
71 return CommonValues::BooleanFalse;
72 default:
73 return Item();
74 }
75 }
76
evaluateEBV(const DynamicContext::Ptr & context) const77 bool NodeComparison::evaluateEBV(const DynamicContext::Ptr &context) const
78 {
79 switch(evaluate(context))
80 {
81 case True:
82 return true;
83 default:
84 /* We include the empty sequence here. */
85 return false;
86 }
87 }
88
evaluate(const DynamicContext::Ptr & context) const89 NodeComparison::Result NodeComparison::evaluate(const DynamicContext::Ptr &context) const
90 {
91 const Item op1(m_operand1->evaluateSingleton(context));
92 if(!op1)
93 return Empty;
94
95 const Item op2(m_operand2->evaluateSingleton(context));
96 if(!op2)
97 return Empty;
98
99 /* We just returns an arbitrary value, since there's no order defined for nodes from different
100 * models, except for that the return value must be stable. */
101 if(op1.asNode().model() != op2.asNode().model())
102 return False;
103
104 switch(m_op)
105 {
106 case QXmlNodeModelIndex::Is:
107 return op1.asNode().is(op2.asNode()) ? True : False;
108 case QXmlNodeModelIndex::Precedes:
109 return op1.asNode().compareOrder(op2.asNode()) == QXmlNodeModelIndex::Precedes ? True : False;
110 default:
111 {
112 Q_ASSERT(m_op == QXmlNodeModelIndex::Follows);
113 return op1.asNode().compareOrder(op2.asNode()) == QXmlNodeModelIndex::Follows ? True : False;
114 }
115 }
116 }
117
118
expectedOperandTypes() const119 SequenceType::List NodeComparison::expectedOperandTypes() const
120 {
121 SequenceType::List result;
122 result.append(CommonSequenceTypes::ZeroOrOneNode);
123 result.append(CommonSequenceTypes::ZeroOrOneNode);
124 return result;
125 }
126
compress(const StaticContext::Ptr & context)127 Expression::Ptr NodeComparison::compress(const StaticContext::Ptr &context)
128 {
129 const Expression::Ptr me(PairContainer::compress(context));
130
131 if(me != this)
132 /* We're already rewritten. */
133 return me;
134
135 if(m_operand1->staticType()->cardinality().isEmpty() ||
136 m_operand2->staticType()->cardinality().isEmpty())
137 {
138 // TODO issue a warning in the @p context saying that one of the operands
139 // were empty, and that the expression always result in the empty sequence
140 // (which never is the intent, right?).
141 return EmptySequence::create(this, context);
142 }
143
144 return Expression::Ptr(this);
145 }
146
displayName(const QXmlNodeModelIndex::DocumentOrder op)147 QString NodeComparison::displayName(const QXmlNodeModelIndex::DocumentOrder op)
148 {
149 switch(op)
150 {
151 case QXmlNodeModelIndex::Is:
152 return QLatin1String("is");
153 case QXmlNodeModelIndex::Precedes:
154 return QLatin1String("<<");
155 default:
156 {
157 Q_ASSERT(op == QXmlNodeModelIndex::Follows);
158 return QLatin1String(">>");
159 }
160 }
161 }
162
staticType() const163 SequenceType::Ptr NodeComparison::staticType() const
164 {
165 if(m_operand1->staticType()->cardinality().allowsEmpty() ||
166 m_operand2->staticType()->cardinality().allowsEmpty())
167 return CommonSequenceTypes::ZeroOrOneBoolean;
168 else
169 return CommonSequenceTypes::ExactlyOneBoolean;
170 }
171
operatorID() const172 QXmlNodeModelIndex::DocumentOrder NodeComparison::operatorID() const
173 {
174 return m_op;
175 }
176
accept(const ExpressionVisitor::Ptr & visitor) const177 ExpressionVisitorResult::Ptr NodeComparison::accept(const ExpressionVisitor::Ptr &visitor) const
178 {
179 return visitor->visit(this);
180 }
181
182 QT_END_NAMESPACE
183