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 <QUrl> |
41 | #include <QVector> |
42 | #include <QXmlNamePool> |
43 | |
44 | #include "qabstractxmlnodemodel_p.h" |
45 | #include "qemptyiterator_p.h" |
46 | #include "qitemmappingiterator_p.h" |
47 | #include "qsequencemappingiterator_p.h" |
48 | #include "qsimplexmlnodemodel.h" |
49 | #include "qsingletoniterator_p.h" |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | using namespace QPatternist; |
54 | |
55 | class QSimpleXmlNodeModelPrivate : public QAbstractXmlNodeModelPrivate |
56 | { |
57 | public: |
58 | QSimpleXmlNodeModelPrivate(const QXmlNamePool &np) : namePool(np) |
59 | { |
60 | } |
61 | |
62 | mutable QXmlNamePool namePool; |
63 | }; |
64 | |
65 | /*! |
66 | \class QSimpleXmlNodeModel |
67 | \brief The QSimpleXmlNodeModel class is an implementation of QAbstractXmlNodeModel sufficient for many common cases. |
68 | \reentrant |
69 | \since 4.4 |
70 | \ingroup xml-tools |
71 | \inmodule QtXmlPatterns |
72 | |
73 | Subclassing QAbstractXmlNodeModel can be a significant task, because it |
74 | requires implementing several, complex member functions. QSimpleXmlNodeModel |
75 | provides default implementations of these member functions that are suitable |
76 | for a wide range of node models. |
77 | |
78 | Subclasses of QSimpleXmlNodeModel must be thread-safe. |
79 | */ |
80 | |
81 | /*! |
82 | Constructs a QSimpleXmlNodeModel for use with with the specified |
83 | \a namePool. |
84 | */ |
85 | QSimpleXmlNodeModel::QSimpleXmlNodeModel(const QXmlNamePool &namePool) |
86 | : QAbstractXmlNodeModel(new QSimpleXmlNodeModelPrivate(namePool)) |
87 | { |
88 | } |
89 | |
90 | /*! |
91 | Destructor. |
92 | */ |
93 | QSimpleXmlNodeModel::~QSimpleXmlNodeModel() |
94 | { |
95 | } |
96 | |
97 | /*! |
98 | If \a node is an element or attribute, typedValue() is called, and |
99 | the return value converted to a string, as per XQuery's rules. |
100 | |
101 | If \a node is another type of node, the empty string is returned. |
102 | |
103 | If this function is overridden for comments or processing |
104 | instructions, it is important to remember to call it (for elements |
105 | and attributes having values not of type \c xs:string) to ensure that |
106 | the values are formatted according to XQuery. |
107 | |
108 | */ |
109 | QString QSimpleXmlNodeModel::stringValue(const QXmlNodeModelIndex &node) const |
110 | { |
111 | const QXmlNodeModelIndex::NodeKind k= kind(ni: node); |
112 | if(k == QXmlNodeModelIndex::Element || k == QXmlNodeModelIndex::Attribute) |
113 | { |
114 | const QVariant &candidate = typedValue(n: node); |
115 | if(candidate.isNull()) |
116 | return QString(); |
117 | else |
118 | return AtomicValue::toXDM(value: candidate).stringValue(); |
119 | } |
120 | else |
121 | return QString(); |
122 | } |
123 | |
124 | /*! |
125 | Returns the base URI for \a node. This is always the document |
126 | URI. |
127 | |
128 | \sa documentUri() |
129 | */ |
130 | QUrl QSimpleXmlNodeModel::baseUri(const QXmlNodeModelIndex &node) const |
131 | { |
132 | return documentUri(ni: node); |
133 | } |
134 | |
135 | /*! |
136 | Returns the name pool associated with this model. The |
137 | implementation of name() will use this name pool to create |
138 | names. |
139 | */ |
140 | QXmlNamePool &QSimpleXmlNodeModel::namePool() const |
141 | { |
142 | Q_D(const QSimpleXmlNodeModel); |
143 | |
144 | return d->namePool; |
145 | } |
146 | |
147 | /*! |
148 | Always returns an empty QVector. This signals that no namespace |
149 | bindings are in scope for \a node. |
150 | */ |
151 | QVector<QXmlName> QSimpleXmlNodeModel::namespaceBindings(const QXmlNodeModelIndex &node) const |
152 | { |
153 | Q_UNUSED(node); |
154 | return QVector<QXmlName>(); |
155 | } |
156 | |
157 | /*! |
158 | Always returns a default constructed QXmlNodeModelIndex instance, |
159 | regardless of \a id. |
160 | |
161 | This effectively means the model has no elements that have an id. |
162 | */ |
163 | QXmlNodeModelIndex QSimpleXmlNodeModel::elementById(const QXmlName &id) const |
164 | { |
165 | Q_UNUSED(id); |
166 | return QXmlNodeModelIndex(); |
167 | } |
168 | |
169 | /*! |
170 | Always returns an empty vector, regardless of \a idref. |
171 | |
172 | This effectively means the model has no elements or attributes of |
173 | type \c IDREF. |
174 | */ |
175 | QVector<QXmlNodeModelIndex> QSimpleXmlNodeModel::nodesByIdref(const QXmlName &idref) const |
176 | { |
177 | Q_UNUSED(idref); |
178 | return QVector<QXmlNodeModelIndex>(); |
179 | } |
180 | |
181 | QT_END_NAMESPACE |
182 | |
183 | |
184 | |