|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008 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 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qxsdinstancereader_p.h" |
|
43 |
|
44 QT_BEGIN_NAMESPACE |
|
45 |
|
46 using namespace QPatternist; |
|
47 |
|
48 XsdInstanceReader::XsdInstanceReader(const QAbstractXmlNodeModel *model, const XsdSchemaContext::Ptr &context) |
|
49 : m_context(context) |
|
50 , m_model(model->iterate(model->root(QXmlNodeModelIndex()), QXmlNodeModelIndex::AxisChild)) |
|
51 { |
|
52 } |
|
53 |
|
54 bool XsdInstanceReader::atEnd() const |
|
55 { |
|
56 return (m_model.current() == AbstractXmlPullProvider::EndOfInput); |
|
57 } |
|
58 |
|
59 void XsdInstanceReader::readNext() |
|
60 { |
|
61 m_model.next(); |
|
62 |
|
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()); |
|
68 } |
|
69 } |
|
70 |
|
71 bool XsdInstanceReader::isStartElement() const |
|
72 { |
|
73 return (m_model.current() == AbstractXmlPullProvider::StartElement); |
|
74 } |
|
75 |
|
76 bool XsdInstanceReader::isEndElement() const |
|
77 { |
|
78 return (m_model.current() == AbstractXmlPullProvider::EndElement); |
|
79 } |
|
80 |
|
81 bool XsdInstanceReader::hasChildText() const |
|
82 { |
|
83 const QXmlNodeModelIndex index = m_model.index(); |
|
84 QXmlNodeModelIndex::Iterator::Ptr it = index.model()->iterate(index, QXmlNodeModelIndex::AxisChild); |
|
85 |
|
86 QXmlNodeModelIndex currentIndex = it->next(); |
|
87 while (!currentIndex.isNull()) { |
|
88 if (currentIndex.kind() == QXmlNodeModelIndex::Text) |
|
89 return true; |
|
90 |
|
91 currentIndex = it->next(); |
|
92 } |
|
93 |
|
94 return false; |
|
95 } |
|
96 |
|
97 bool XsdInstanceReader::hasChildElement() const |
|
98 { |
|
99 const QXmlNodeModelIndex index = m_model.index(); |
|
100 QXmlNodeModelIndex::Iterator::Ptr it = index.model()->iterate(index, QXmlNodeModelIndex::AxisChild); |
|
101 |
|
102 QXmlNodeModelIndex currentIndex = it->next(); |
|
103 while (!currentIndex.isNull()) { |
|
104 if (currentIndex.kind() == QXmlNodeModelIndex::Element) |
|
105 return true; |
|
106 |
|
107 currentIndex = it->next(); |
|
108 } |
|
109 |
|
110 return false; |
|
111 } |
|
112 |
|
113 QXmlName XsdInstanceReader::name() const |
|
114 { |
|
115 return m_model.name(); |
|
116 } |
|
117 |
|
118 QXmlName XsdInstanceReader::convertToQName(const QString &name) const |
|
119 { |
|
120 const int pos = name.indexOf(QLatin1Char(':')); |
|
121 |
|
122 QXmlName::PrefixCode prefixCode = 0; |
|
123 QXmlName::NamespaceCode namespaceCode; |
|
124 QXmlName::LocalNameCode localNameCode; |
|
125 if (pos != -1) { |
|
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)); |
|
129 } else { |
|
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); |
|
135 } |
|
136 |
|
137 return QXmlName(namespaceCode, localNameCode, prefixCode); |
|
138 } |
|
139 |
|
140 bool XsdInstanceReader::hasAttribute(const QXmlName &name) const |
|
141 { |
|
142 return m_cachedAttributes.contains(name); |
|
143 } |
|
144 |
|
145 QString XsdInstanceReader::attribute(const QXmlName &name) const |
|
146 { |
|
147 Q_ASSERT(m_cachedAttributes.contains(name)); |
|
148 |
|
149 return m_cachedAttributes.value(name); |
|
150 } |
|
151 |
|
152 QSet<QXmlName> XsdInstanceReader::attributeNames() const |
|
153 { |
|
154 return m_cachedAttributes.keys().toSet(); |
|
155 } |
|
156 |
|
157 QString XsdInstanceReader::text() const |
|
158 { |
|
159 const QXmlNodeModelIndex index = m_model.index(); |
|
160 QXmlNodeModelIndex::Iterator::Ptr it = index.model()->iterate(index, QXmlNodeModelIndex::AxisChild); |
|
161 |
|
162 QString result; |
|
163 |
|
164 QXmlNodeModelIndex currentIndex = it->next(); |
|
165 while (!currentIndex.isNull()) { |
|
166 if (currentIndex.kind() == QXmlNodeModelIndex::Text) { |
|
167 result.append(Item(currentIndex).stringValue()); |
|
168 } |
|
169 |
|
170 currentIndex = it->next(); |
|
171 } |
|
172 |
|
173 return result; |
|
174 } |
|
175 |
|
176 QXmlItem XsdInstanceReader::item() const |
|
177 { |
|
178 return m_cachedItem; |
|
179 } |
|
180 |
|
181 QXmlItem XsdInstanceReader::attributeItem(const QXmlName &name) const |
|
182 { |
|
183 return m_cachedAttributeItems.value(name); |
|
184 } |
|
185 |
|
186 QSourceLocation XsdInstanceReader::sourceLocation() const |
|
187 { |
|
188 return m_cachedSourceLocation; |
|
189 } |
|
190 |
|
191 QVector<QXmlName> XsdInstanceReader::namespaceBindings(const QXmlNodeModelIndex &index) const |
|
192 { |
|
193 return index.namespaceBindings(); |
|
194 } |
|
195 |
|
196 QT_END_NAMESPACE |