source: trunk/src/xmlpatterns/expr/qaxisstep.cpp@ 477

Last change on this file since 477 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
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"
47
48#include "qaxisstep_p.h"
49
50QT_BEGIN_NAMESPACE
51
52using namespace QPatternist;
53
54namespace QPatternist
55{
56 /**
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.
59 */
60 static inline QXmlNodeModelIndex::NodeKind operator|(const QXmlNodeModelIndex::NodeKind &op1, const QXmlNodeModelIndex::NodeKind &op2)
61 {
62 return QXmlNodeModelIndex::NodeKind(int(op1) | int(op2));
63 }
64}
65
66/**
67 * @note The order is significant. It is of the same order as the values in QXmlNodeModelIndex::Axis is declared.
68 */
69const QXmlNodeModelIndex::NodeKind AxisStep::s_whenAxisNodeKindEmpty[] =
70{
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;
84};
85
86bool AxisStep::isAlwaysEmpty(const QXmlNodeModelIndex::Axis axis, const QXmlNodeModelIndex::NodeKind nodeKind)
87{
88 return (s_whenAxisNodeKindEmpty[(1 >> axis) - 1] & nodeKind) != 0;
89}
90
91AxisStep::AxisStep(const QXmlNodeModelIndex::Axis a,
92 const ItemType::Ptr &nt) : m_axis(a),
93 m_nodeTest(nt)
94{
95 Q_ASSERT(m_nodeTest);
96 Q_ASSERT_X(BuiltinTypes::node->xdtTypeMatches(m_nodeTest), Q_FUNC_INFO,
97 "We assume we're a node type.");
98}
99
100Item AxisStep::mapToItem(const QXmlNodeModelIndex &node,
101 const DynamicContext::Ptr &context) const
102{
103 Q_ASSERT(!node.isNull());
104 Q_ASSERT(Item(node).isNode());
105 Q_ASSERT(Item(node));
106 Q_UNUSED(context);
107
108 if(m_nodeTest->itemMatches(Item(node)))
109 return Item(node);
110 else
111 return Item();
112}
113
114Item::Iterator::Ptr AxisStep::evaluateSequence(const DynamicContext::Ptr &context) const
115{
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();
120
121 Q_ASSERT(context->contextItem());
122
123 const QXmlNodeModelIndex::Iterator::Ptr source(context->contextItem().asNode().iterate(m_axis));
124
125 return makeItemMappingIterator<Item>(ConstPtr(this), source, context);
126}
127
128Item AxisStep::evaluateSingleton(const DynamicContext::Ptr &context) const
129{
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();
134
135 Q_ASSERT(context->contextItem());
136
137 const QXmlNodeModelIndex::Iterator::Ptr it(context->contextItem().asNode().iterate(m_axis));
138 QXmlNodeModelIndex next(it->next());
139
140 while(!next.isNull())
141 {
142 const Item candidate(mapToItem(next, context));
143
144 if(candidate)
145 return candidate;
146 else
147 next = it->next();
148 };
149
150 return Item();
151}
152
153Expression::Ptr AxisStep::typeCheck(const StaticContext::Ptr &context,
154 const SequenceType::Ptr &reqType)
155{
156 if(m_axis == QXmlNodeModelIndex::AxisParent && *m_nodeTest == *BuiltinTypes::node)
157 {