1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qbuiltintypes_p.h"
43 #include "qcommonsequencetypes_p.h"
44 #include "qitemmappingiterator_p.h"
45 #include "qgenericsequencetype_p.h"
46 #include "qparentnodeaxis_p.h"
48 #include "qaxisstep_p.h"
52 using namespace QPatternist;
57 * This operator is needed for the s_whenAxisNodeKindEmpty array. The @c int constructors
58 * ensure we invoke another operator| such that we don't get an infinite loop.
60 static inline QXmlNodeModelIndex::NodeKind operator|(const QXmlNodeModelIndex::NodeKind &op1, const QXmlNodeModelIndex::NodeKind &op2)
62 return QXmlNodeModelIndex::NodeKind(int(op1) | int(op2));
67 * @note The order is significant. It is of the same order as the values in QXmlNodeModelIndex::Axis is declared.
69 const QXmlNodeModelIndex::NodeKind AxisStep::s_whenAxisNodeKindEmpty[] =
71 QXmlNodeModelIndex::Attribute|QXmlNodeModelIndex::Text|QXmlNodeModelIndex::ProcessingInstruction|QXmlNodeModelIndex::Comment|QXmlNodeModelIndex::Namespace, // child;
72 QXmlNodeModelIndex::Attribute|QXmlNodeModelIndex::Text|QXmlNodeModelIndex::ProcessingInstruction|QXmlNodeModelIndex::Comment|QXmlNodeModelIndex::Namespace, // descendant;
73 QXmlNodeModelIndex::Document|QXmlNodeModelIndex::Attribute|QXmlNodeModelIndex::Text|QXmlNodeModelIndex::ProcessingInstruction|QXmlNodeModelIndex::Comment|QXmlNodeModelIndex::Namespace,// attribute;
74 QXmlNodeModelIndex::NodeKind(0), // self;
75 QXmlNodeModelIndex::NodeKind(0), // descendant-or-self;
76 QXmlNodeModelIndex::Document|QXmlNodeModelIndex::Attribute|QXmlNodeModelIndex::Text|QXmlNodeModelIndex::ProcessingInstruction|QXmlNodeModelIndex::Comment|QXmlNodeModelIndex::Namespace, // namespace;
77 QXmlNodeModelIndex::Document, // following;
78 QXmlNodeModelIndex::Document, // parent;
79 QXmlNodeModelIndex::Document, // ancestor
80 QXmlNodeModelIndex::Document|QXmlNodeModelIndex::Attribute|QXmlNodeModelIndex::Namespace, // preceding-sibling;
81 QXmlNodeModelIndex::Document|QXmlNodeModelIndex::Attribute|QXmlNodeModelIndex::Namespace, // following-sibling;
82 QXmlNodeModelIndex::Document, // preceding;
83 QXmlNodeModelIndex::NodeKind(0) // ancestor-or-self;
86 bool AxisStep::isAlwaysEmpty(const QXmlNodeModelIndex::Axis axis, const QXmlNodeModelIndex::NodeKind nodeKind)
88 return (s_whenAxisNodeKindEmpty[(1 >> axis) - 1] & nodeKind) != 0;
91 AxisStep::AxisStep(const QXmlNodeModelIndex::Axis a,
92 const ItemType::Ptr &nt) : m_axis(a),
96 Q_ASSERT_X(BuiltinTypes::node->xdtTypeMatches(m_nodeTest), Q_FUNC_INFO,
97 "We assume we're a node type.");
100 Item AxisStep::mapToItem(const QXmlNodeModelIndex &node,
101 const DynamicContext::Ptr &context) const
103 Q_ASSERT(!node.isNull());
104 Q_ASSERT(Item(node).isNode());
105 Q_ASSERT(Item(node));
108 if(m_nodeTest->itemMatches(Item(node)))
114 Item::Iterator::Ptr AxisStep::evaluateSequence(const DynamicContext::Ptr &context) const
116 /* If we don't have a focus, it's either a bug or our parent isn't a Path
117 * that have advanced the focus iterator. Hence, attempt to advance the focus on our own. */
118 if(!context->contextItem())
119 context->focusIterator()->next();
121 Q_ASSERT(context->contextItem());
123 const QXmlNodeModelIndex::Iterator::Ptr source(context->contextItem().asNode().iterate(m_axis));
125 return makeItemMappingIterator<Item>(ConstPtr(this), source, context);
128 Item AxisStep::evaluateSingleton(const DynamicContext::Ptr &context) const
130 /* If we don't have a focus, it's either a bug or our parent isn't a Path
131 * that have advanced the focus iterator. Hence, attempt to advance the focus on our own. */
132 if(!context->contextItem())
133 context->focusIterator()->next();
135 Q_ASSERT(context->contextItem());
137 const QXmlNodeModelIndex::Iterator::Ptr it(context->contextItem().asNode().iterate(m_axis));
138 QXmlNodeModelIndex next(it->next());
140 while(!next.isNull())
142 const Item candidate(mapToItem(next, context));
153 Expression::Ptr AxisStep::typeCheck(const StaticContext::Ptr &context,
154 const SequenceType::Ptr &reqType)
156 if(m_axis == QXmlNodeModelIndex::AxisParent && *m_nodeTest == *BuiltinTypes::node)
158 /* We only rewrite parent::node() to ParentNodeAxis. */
159 return rewrite(Expression::Ptr(new ParentNodeAxis()), context)->typeCheck(context, reqType);
161 /* TODO temporarily disabled
162 else if(isAlwaysEmpty(m_axis, static_cast<const AnyNodeType *>(m_nodeTest.data())->nodeKind()))
163 return EmptySequence::create(this, context);
166 return EmptyContainer::typeCheck(context, reqType);
169 SequenceType::Ptr AxisStep::staticType() const
171 Cardinality cardinality;
173 if(m_axis == QXmlNodeModelIndex::AxisSelf || m_axis == QXmlNodeModelIndex::AxisParent)
174 cardinality = Cardinality::zeroOrOne();
176 cardinality = Cardinality::zeroOrMore();
178 return makeGenericSequenceType(m_nodeTest,
182 SequenceType::List AxisStep::expectedOperandTypes() const
184 SequenceType::List result;
185 result.append(CommonSequenceTypes::ZeroOrMoreNodes);
189 Expression::Properties AxisStep::properties() const
191 return RequiresContextItem | DisableElimination;
194 ItemType::Ptr AxisStep::expectedContextItemType() const
196 return BuiltinTypes::node;
199 ExpressionVisitorResult::Ptr AxisStep::accept(const ExpressionVisitor::Ptr &visitor) const
201 return visitor->visit(this);
204 QXmlNodeModelIndex::Axis AxisStep::axis() const
209 QString AxisStep::axisName(const QXmlNodeModelIndex::Axis axis)
211 const char *result = 0;
215 /* These must not be translated. */
216 case QXmlNodeModelIndex::AxisAncestorOrSelf: result = "ancestor-or-self"; break;
217 case QXmlNodeModelIndex::AxisAncestor: result = "ancestor"; break;
218 case QXmlNodeModelIndex::AxisAttributeOrTop: result = "attribute-or-top"; break;
219 case QXmlNodeModelIndex::AxisAttribute: result = "attribute"; break;
220 case QXmlNodeModelIndex::AxisChildOrTop: result = "child-or-top"; break;
221 case QXmlNodeModelIndex::AxisChild: result = "child"; break;
222 case QXmlNodeModelIndex::AxisDescendantOrSelf: result = "descendant-or-self"; break;
223 case QXmlNodeModelIndex::AxisDescendant: result = "descendant"; break;
224 case QXmlNodeModelIndex::AxisFollowing: result = "following"; break;
225 case QXmlNodeModelIndex::AxisFollowingSibling: result = "following-sibling"; break;
226 case QXmlNodeModelIndex::AxisNamespace: result = "namespace"; break;
227 case QXmlNodeModelIndex::AxisParent: result = "parent"; break;
228 case QXmlNodeModelIndex::AxisPreceding: result = "preceding"; break;
229 case QXmlNodeModelIndex::AxisPrecedingSibling: result = "preceding-sibling"; break;
230 case QXmlNodeModelIndex::AxisSelf: result = "self"; break;
233 Q_ASSERT_X(result, Q_FUNC_INFO, "An unknown axis type was apparently encountered.");
234 return QString::fromLatin1(result);
237 PatternPriority AxisStep::patternPriority() const
239 return static_cast<const AnyNodeType *>(m_nodeTest.data())->patternPriority();
242 Expression::ID AxisStep::id() const