1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qxsdinstancereader_p.h"
46 using namespace QPatternist;
48 XsdInstanceReader::XsdInstanceReader(const QAbstractXmlNodeModel *model, const XsdSchemaContext::Ptr &context)
50 , m_model(model->iterate(model->root(QXmlNodeModelIndex()), QXmlNodeModelIndex::AxisChild))
54 bool XsdInstanceReader::atEnd() const
56 return (m_model.current() == AbstractXmlPullProvider::EndOfInput);
59 void XsdInstanceReader::readNext()
63 if (m_model.current() == AbstractXmlPullProvider::StartElement) {
64 m_cachedAttributes = m_model.attributes();
65 m_cachedAttributeItems = m_model.attributeItems();
66 m_cachedSourceLocation = m_model.sourceLocation();
67 m_cachedItem = QXmlItem(m_model.index());
71 bool XsdInstanceReader::isStartElement() const
73 return (m_model.current() == AbstractXmlPullProvider::StartElement);
76 bool XsdInstanceReader::isEndElement() const
78 return (m_model.current() == AbstractXmlPullProvider::EndElement);
81 bool XsdInstanceReader::hasChildText() const
83 const QXmlNodeModelIndex index = m_model.index();
84 QXmlNodeModelIndex::Iterator::Ptr it = index.model()->iterate(index, QXmlNodeModelIndex::AxisChild);
86 QXmlNodeModelIndex currentIndex = it->next();
87 while (!currentIndex.isNull()) {
88 if (currentIndex.kind() == QXmlNodeModelIndex::Text)
91 currentIndex = it->next();
97 bool XsdInstanceReader::hasChildElement() const
99 const QXmlNodeModelIndex index = m_model.index();
100 QXmlNodeModelIndex::Iterator::Ptr it = index.model()->iterate(index, QXmlNodeModelIndex::AxisChild);
102 QXmlNodeModelIndex currentIndex = it->next();
103 while (!currentIndex.isNull()) {
104 if (currentIndex.kind() == QXmlNodeModelIndex::Element)
107 currentIndex = it->next();
113 QXmlName XsdInstanceReader::name() const
115 return m_model.name();
118 QXmlName XsdInstanceReader::convertToQName(const QString &name) const
120 const int pos = name.indexOf(QLatin1Char(':'));
122 QXmlName::PrefixCode prefixCode = 0;
123 QXmlName::NamespaceCode namespaceCode;
124 QXmlName::LocalNameCode localNameCode;
126 prefixCode = m_context->namePool()->allocatePrefix(name.left(pos));
127 namespaceCode = m_cachedItem.toNodeModelIndex().namespaceForPrefix(prefixCode);
128 localNameCode = m_context->namePool()->allocateLocalName(name.mid(pos + 1));
130 prefixCode = StandardPrefixes::empty;
131 namespaceCode = m_cachedItem.toNodeModelIndex().namespaceForPrefix(prefixCode);
132 if (namespaceCode == -1)
133 namespaceCode = StandardNamespaces::empty;
134 localNameCode = m_context->namePool()->allocateLocalName(name);
137 return QXmlName(namespaceCode, localNameCode, prefixCode);
140 bool XsdInstanceReader::hasAttribute(const QXmlName &name) const
142 return m_cachedAttributes.contains(name);
145 QString XsdInstanceReader::attribute(const QXmlName &name) const
147 Q_ASSERT(m_cachedAttributes.contains(name));
149 return m_cachedAttributes.value(name);
152 QSet<QXmlName> XsdInstanceReader::attributeNames() const
154 return m_cachedAttributes.keys().toSet();
157 QString XsdInstanceReader::text() const
159 const QXmlNodeModelIndex index = m_model.index();
160 QXmlNodeModelIndex::Iterator::Ptr it = index.model()->iterate(index, QXmlNodeModelIndex::AxisChild);
164 QXmlNodeModelIndex currentIndex = it->next();
165 while (!currentIndex.isNull()) {
166 if (currentIndex.kind() == QXmlNodeModelIndex::Text) {
167 result.append(Item(currentIndex).stringValue());
170 currentIndex = it->next();
176 QXmlItem XsdInstanceReader::item() const
181 QXmlItem XsdInstanceReader::attributeItem(const QXmlName &name) const
183 return m_cachedAttributeItems.value(name);
186 QSourceLocation XsdInstanceReader::sourceLocation() const
188 return m_cachedSourceLocation;
191 QVector<QXmlName> XsdInstanceReader::namespaceBindings(const QXmlNodeModelIndex &index) const
193 return index.namespaceBindings();