Update copyright year in license headers.
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / api / qpullbridge.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QVariant>
43
44 #include "qabstractxmlnodemodel_p.h"
45 #include "qitemmappingiterator_p.h"
46 #include "qitem_p.h"
47 #include "qxmlname.h"
48 #include "qxmlquery_p.h"
49
50 #include "qpullbridge_p.h"
51
52 QT_BEGIN_NAMESPACE
53
54 using namespace QPatternist;
55
56 /*!
57   \brief Bridges a QPatternist::SequenceIterator to QAbstractXmlPullProvider.
58   \class QPatternist::PullBridge
59   \internal
60   \reentrant
61   \ingroup xml-tools
62
63   The approach of this class is rather straight forward since QPatternist::SequenceIterator
64   and QAbstractXmlPullProvider are conceptually similar. While QPatternist::SequenceIterator only
65   delivers top level items(since it's not an event stream, it's a list of items), PullBridge
66   needs to recursively iterate the children of nodes too, which is achieved through the
67   stack m_iterators.
68  */
69
70 AbstractXmlPullProvider::Event PullBridge::next()
71 {
72     m_index = m_iterators.top().second->next();
73
74     if(!m_index.isNull())
75     {
76         Item item(m_index);
77
78         if(item && item.isAtomicValue())
79             m_current = AtomicValue;
80         else
81         {
82             Q_ASSERT(item.isNode());
83
84             switch(m_index.kind())
85             {
86                 case QXmlNodeModelIndex::Attribute:
87                 {
88                     m_current = Attribute;
89                     break;
90                 }
91                 case QXmlNodeModelIndex::Comment:
92                 {
93                     m_current = Comment;
94                     break;
95                 }
96                 case QXmlNodeModelIndex::Element:
97                 {
98                     m_iterators.push(qMakePair(StartElement, m_index.iterate(QXmlNodeModelIndex::AxisChild)));
99                     m_current = StartElement;
100                     break;
101                 }
102                 case QXmlNodeModelIndex::Document:
103                 {
104                     m_iterators.push(qMakePair(StartDocument, m_index.iterate(QXmlNodeModelIndex::AxisChild)));
105                     m_current = StartDocument;
106                     break;
107                 }
108                 case QXmlNodeModelIndex::Namespace:
109                 {
110                     m_current = Namespace;
111                     break;
112                 }
113                 case QXmlNodeModelIndex::ProcessingInstruction:
114                 {
115                     m_current = ProcessingInstruction;
116                     break;
117                 }
118                 case QXmlNodeModelIndex::Text:
119                 {
120                     m_current = Text;
121                     break;
122                 }
123             }
124         }
125     }
126     else
127     {
128         if(m_iterators.isEmpty())
129             m_current = EndOfInput;
130         else
131         {
132             switch(m_iterators.top().first)
133             {
134                 case StartOfInput:
135                 {
136                     m_current = EndOfInput;
137                     break;
138                 }
139                 case StartElement:
140                 {
141                     m_current = EndElement;
142                     m_iterators.pop();
143                     break;
144                 }
145                 case StartDocument:
146                 {
147                     m_current = EndDocument;
148                     m_iterators.pop();
149                     break;
150                 }
151                 default:
152                 {
153                     Q_ASSERT_X(false, Q_FUNC_INFO,
154                                "Invalid value.");
155                     m_current = EndOfInput;
156                 }
157             }
158         }
159
160     }
161
162     return m_current;
163 }
164
165 AbstractXmlPullProvider::Event PullBridge::current() const
166 {
167     return m_current;
168 }
169
170 QXmlNodeModelIndex PullBridge::index() const
171 {
172     return m_index;
173 }
174
175 QSourceLocation PullBridge::sourceLocation() const
176 {
177     return m_index.model()->sourceLocation(m_index);
178 }
179
180 QXmlName PullBridge::name() const
181 {
182     return m_index.name();
183 }
184
185 QVariant PullBridge::atomicValue() const
186 {
187     return QVariant();
188 }
189
190 QString PullBridge::stringValue() const
191 {
192     return QString();
193 }
194
195 QHash<QXmlName, QString> PullBridge::attributes()
196 {
197     Q_ASSERT(m_current == StartElement);
198
199     QHash<QXmlName, QString> attributes;
200
201     QXmlNodeModelIndex::Iterator::Ptr it = m_index.iterate(QXmlNodeModelIndex::AxisAttribute);
202     QXmlNodeModelIndex index = it->next();
203     while (!index.isNull()) {
204         const Item attribute(index);
205         attributes.insert(index.name(), index.stringValue());
206
207         index = it->next();
208     }
209
210     return attributes;
211 }
212
213 QHash<QXmlName, QXmlItem> PullBridge::attributeItems()
214 {
215     Q_ASSERT(m_current == StartElement);
216
217     QHash<QXmlName, QXmlItem> attributes;
218
219     QXmlNodeModelIndex::Iterator::Ptr it = m_index.iterate(QXmlNodeModelIndex::AxisAttribute);
220     QXmlNodeModelIndex index = it->next();
221     while (!index.isNull()) {
222         const Item attribute(index);
223         attributes.insert(index.name(), QXmlItem(index));
224
225         index = it->next();
226     }
227
228     return attributes;
229 }
230
231 QT_END_NAMESPACE
232