source: trunk/src/xmlpatterns/acceltree/qacceliterators.cpp@ 317

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

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

File size: 5.6 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 <QtDebug>
43
44#include "qacceliterators_p.h"
45
46QT_BEGIN_NAMESPACE
47
48using namespace QPatternist;
49
50xsInteger AccelIterator::position() const
51{
52 return m_position;
53}
54
55QXmlNodeModelIndex AccelIterator::current() const
56{
57 return m_current;
58}
59
60QXmlNodeModelIndex FollowingIterator::next()
61{
62 /* "the following axis contains all nodes that are descendants
63 * of the root of the tree in which the context node is found,
64 * are not descendants of the context node, and occur after
65 * the context node in document order." */
66
67 if(m_position == 0)
68 {
69 /* Skip the descendants. */
70 m_currentPre += m_document->size(m_preNumber) + 1;
71 }
72
73 if(m_currentPre > m_document->maximumPreNumber())
74 return closedExit();
75
76 while(m_document->kind(m_currentPre) == QXmlNodeModelIndex::Attribute)
77 {
78 ++m_currentPre;
79 if(m_currentPre > m_document->maximumPreNumber())
80 return closedExit();
81 }
82
83 m_current = m_document->createIndex(m_currentPre);
84 ++m_position;
85 ++m_currentPre;
86 return m_current;
87}
88
89QXmlNodeModelIndex PrecedingIterator::next()
90{
91 if(m_currentPre == -1)
92 return closedExit();
93
94 /* We skip ancestors and attributes and take into account that they can be intermixed. If one
95 * skips them in two separate loops, one can end up with skipping all the attributes to then
96 * be positioned at an ancestor(which will be accepted because the ancestor loop was before the
97 * attributes loop). */
98 while(m_document->kind(m_currentPre) == QXmlNodeModelIndex::Attribute ||
99 m_document->postNumber(m_currentPre) > m_postNumber)
100 {
101 --m_currentPre;
102 if(m_currentPre == -1)
103 return closedExit();
104 }
105
106 if(m_currentPre == -1)
107 {
108 m_currentPre = -1;
109 return closedExit();
110 }
111
112 /* Phew, m_currentPre is now 1) not an ancestor; and
113 * 2) not an attribute; and 3) preceds the context node. */
114
115 m_current = m_document->createIndex(m_currentPre);
116 ++m_position;
117 --m_currentPre;
118
119 return m_current;
120}
121
122QXmlNodeModelIndex::Iterator::Ptr PrecedingIterator::copy() const
123{
124 return QXmlNodeModelIndex::Iterator::Ptr(new PrecedingIterator(m_document, m_preNumber));
125}
126
127QXmlNodeModelIndex::Iterator::Ptr FollowingIterator::copy() const
128{
129 return QXmlNodeModelIndex::Iterator::Ptr(new FollowingIterator(m_document, m_preNumber));
130}
131
132QXmlNodeModelIndex ChildIterator::next()
133{
134 if(m_currentPre == -1)
135 return closedExit();
136
137 ++m_position;
138 m_current = m_document->createIndex(m_currentPre);
139
140 /* We get the count of the descendants, and increment m_currentPre. After
141 * this, m_currentPre is the node after the descendants. */
142 m_currentPre += m_document->size(m_currentPre);
143 ++m_currentPre;
144
145 if(m_currentPre > m_document->maximumPreNumber() || m_document->depth(m_currentPre) != m_depth)
146 m_currentPre = -1;
147
148 return m_current;
149}
150
151QXmlNodeModelIndex::Iterator::Ptr ChildIterator::copy() const
152{
153 return QXmlNodeModelIndex::Iterator::Ptr(new ChildIterator(m_document, m_preNumber));
154}
155
156QXmlNodeModelIndex AttributeIterator::next()
157{
158 if(m_currentPre == -1)
159 return closedExit();
160 else
161 {
162 m_current = m_document->createIndex(m_currentPre);
163 ++m_position;
164
165 ++m_currentPre;
166
167 if(m_currentPre > m_document->maximumPreNumber() ||
168 m_document->kind(m_currentPre) != QXmlNodeModelIndex::Attribute)
169 m_currentPre = -1;
170
171 return m_current;
172 }
173}
174
175QXmlNodeModelIndex::Iterator::Ptr AttributeIterator::copy() const
176{
177 return QXmlNodeModelIndex::Iterator::Ptr(new AttributeIterator(m_document, m_preNumber));
178}
179
180QT_END_NAMESPACE
181
Note: See TracBrowser for help on using the repository browser.