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 "qxsdinstancereader_p.h" |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | using namespace QPatternist; |
45 | |
46 | XsdInstanceReader::XsdInstanceReader(const QAbstractXmlNodeModel *model, const XsdSchemaContext::Ptr &context) |
47 | : m_context(context) |
48 | , m_model(model->iterate(ni: model->root(n: QXmlNodeModelIndex()), axis: QXmlNodeModelIndex::AxisChild)) |
49 | { |
50 | } |
51 | |
52 | bool XsdInstanceReader::atEnd() const |
53 | { |
54 | return (m_model.current() == AbstractXmlPullProvider::EndOfInput); |
55 | } |
56 | |
57 | void XsdInstanceReader::readNext() |
58 | { |
59 | m_model.next(); |
60 | |
61 | if (m_model.current() == AbstractXmlPullProvider::StartElement) { |
62 | m_cachedAttributes = m_model.attributes(); |
63 | m_cachedAttributeItems = m_model.attributeItems(); |
64 | m_cachedSourceLocation = m_model.sourceLocation(); |
65 | m_cachedItem = QXmlItem(m_model.index()); |
66 | } |
67 | } |
68 | |
69 | bool XsdInstanceReader::isStartElement() const |
70 | { |
71 | return (m_model.current() == AbstractXmlPullProvider::StartElement); |
72 | } |
73 | |
74 | bool XsdInstanceReader::isEndElement() const |
75 | { |
76 | return (m_model.current() == AbstractXmlPullProvider::EndElement); |
77 | } |
78 | |
79 | bool XsdInstanceReader::hasChildText() const |
80 | { |
81 | const QXmlNodeModelIndex index = m_model.index(); |
82 | QXmlNodeModelIndex::Iterator::Ptr it = index.model()->iterate(ni: index, axis: QXmlNodeModelIndex::AxisChild); |
83 | |
84 | QXmlNodeModelIndex currentIndex = it->next(); |
85 | while (!currentIndex.isNull()) { |
86 | if (currentIndex.kind() == QXmlNodeModelIndex::Text) |
87 | return true; |
88 | |
89 | currentIndex = it->next(); |
90 | } |
91 | |
92 | return false; |
93 | } |
94 | |
95 | bool XsdInstanceReader::hasChildElement() const |
96 | { |
97 | const QXmlNodeModelIndex index = m_model.index(); |
98 | QXmlNodeModelIndex::Iterator::Ptr it = index.model()->iterate(ni: index, axis: QXmlNodeModelIndex::AxisChild); |
99 | |
100 | QXmlNodeModelIndex currentIndex = it->next(); |
101 | while (!currentIndex.isNull()) { |
102 | if (currentIndex.kind() == QXmlNodeModelIndex::Element) |
103 | return true; |
104 | |
105 | currentIndex = it->next(); |
106 | } |
107 | |
108 | return false; |
109 | } |
110 | |
111 | QXmlName XsdInstanceReader::name() const |
112 | { |
113 | return m_model.name(); |
114 | } |
115 | |
116 | QXmlName XsdInstanceReader::convertToQName(const QString &name) const |
117 | { |
118 | const int pos = name.indexOf(c: QLatin1Char(':')); |
119 | |
120 | QXmlName::PrefixCode prefixCode = 0; |
121 | QXmlName::NamespaceCode namespaceCode; |
122 | QXmlName::LocalNameCode localNameCode; |
123 | if (pos != -1) { |
124 | prefixCode = m_context->namePool()->allocatePrefix(prefix: name.left(n: pos)); |
125 | namespaceCode = m_cachedItem.toNodeModelIndex().namespaceForPrefix(prefix: prefixCode); |
126 | localNameCode = m_context->namePool()->allocateLocalName(ln: name.mid(position: pos + 1)); |
127 | } else { |
128 | prefixCode = StandardPrefixes::empty; |
129 | namespaceCode = m_cachedItem.toNodeModelIndex().namespaceForPrefix(prefix: prefixCode); |
130 | if (namespaceCode == -1) |
131 | namespaceCode = StandardNamespaces::empty; |
132 | localNameCode = m_context->namePool()->allocateLocalName(ln: name); |
133 | } |
134 | |
135 | return QXmlName(namespaceCode, localNameCode, prefixCode); |
136 | } |
137 | |
138 | bool XsdInstanceReader::hasAttribute(const QXmlName &name) const |
139 | { |
140 | return m_cachedAttributes.contains(akey: name); |
141 | } |
142 | |
143 | QString XsdInstanceReader::attribute(const QXmlName &name) const |
144 | { |
145 | Q_ASSERT(m_cachedAttributes.contains(name)); |
146 | |
147 | return m_cachedAttributes.value(akey: name); |
148 | } |
149 | |
150 | QSet<QXmlName> XsdInstanceReader::attributeNames() const |
151 | { |
152 | QSet<QXmlName> result; |
153 | for (auto it = m_cachedAttributes.cbegin(), end = m_cachedAttributes.cend(); it != end; ++it) |
154 | result.insert(value: it.key()); |
155 | return result; |
156 | } |
157 | |
158 | QString XsdInstanceReader::text() const |
159 | { |
160 | const QXmlNodeModelIndex index = m_model.index(); |
161 | QXmlNodeModelIndex::Iterator::Ptr it = index.model()->iterate(ni: index, axis: QXmlNodeModelIndex::AxisChild); |
162 | |
163 | QString result; |
164 | |
165 | QXmlNodeModelIndex currentIndex = it->next(); |
166 | while (!currentIndex.isNull()) { |
167 | if (currentIndex.kind() == QXmlNodeModelIndex::Text) { |
168 | result.append(s: Item(currentIndex).stringValue()); |
169 | } |
170 | |
171 | currentIndex = it->next(); |
172 | } |
173 | |
174 | return result; |
175 | } |
176 | |
177 | QXmlItem XsdInstanceReader::item() const |
178 | { |
179 | return m_cachedItem; |
180 | } |
181 | |
182 | QXmlItem XsdInstanceReader::attributeItem(const QXmlName &name) const |
183 | { |
184 | return m_cachedAttributeItems.value(akey: name); |
185 | } |
186 | |
187 | QSourceLocation XsdInstanceReader::sourceLocation() const |
188 | { |
189 | return m_cachedSourceLocation; |
190 | } |
191 | |
192 | QVector<QXmlName> XsdInstanceReader::namespaceBindings(const QXmlNodeModelIndex &index) const |
193 | { |
194 | return index.namespaceBindings(); |
195 | } |
196 | |
197 | QT_END_NAMESPACE |
198 | |