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