Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qxmlstream.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QXMLSTREAM_H
5#define QXMLSTREAM_H
6
7#include <QtCore/qiodevice.h>
8
9#if QT_CONFIG(xmlstream)
10
11#include <QtCore/qcompare.h>
12#include <QtCore/qlist.h>
13#include <QtCore/qscopedpointer.h>
14#include <QtCore/qstring.h>
15
17
18namespace QtPrivate {
19
20class QXmlString {
21 QStringPrivate m_string;
22public:
23 QXmlString(QStringPrivate &&d) : m_string(std::move(d)) {}
24 QXmlString(const QString &s) : m_string(s.data_ptr()) {}
25 QXmlString & operator=(const QString &s) { m_string = s.data_ptr(); return *this; }
26 QXmlString & operator=(QString &&s) { m_string.swap(s.data_ptr()); return *this; }
27 inline constexpr QXmlString() {}
28
29 void swap(QXmlString &other) noexcept
30 {
31 m_string.swap(other.m_string);
32 }
33
34 inline operator QStringView() const { return QStringView(m_string.data(), m_string.size); }
35 inline qsizetype size() const { return m_string.size; }
36};
37
38}
39Q_DECLARE_SHARED(QtPrivate::QXmlString)
40
41
43class QXmlStreamAttributes;
44class Q_CORE_EXPORT QXmlStreamAttribute {
45 QtPrivate::QXmlString m_name, m_namespaceUri, m_qualifiedName, m_value;
46 uint m_isDefault : 1;
47 friend class QXmlStreamReaderPrivate;
48 friend class QXmlStreamAttributes;
49public:
50 QXmlStreamAttribute();
51 QXmlStreamAttribute(const QString &qualifiedName, const QString &value);
52 QXmlStreamAttribute(const QString &namespaceUri, const QString &name, const QString &value);
53
54 inline QStringView namespaceUri() const { return m_namespaceUri; }
55 inline QStringView name() const { return m_name; }
56 inline QStringView qualifiedName() const { return m_qualifiedName; }
57 inline QStringView prefix() const {
58 return QStringView(m_qualifiedName).left(qMax(0, m_qualifiedName.size() - m_name.size() - 1));
59 }
60 inline QStringView value() const { return m_value; }
61 inline bool isDefault() const { return m_isDefault; }
62#if QT_CORE_REMOVED_SINCE(6, 8)
63 inline bool operator==(const QXmlStreamAttribute &other) const
64 { return comparesEqual(*this, other); }
65 inline bool operator!=(const QXmlStreamAttribute &other) const
66 { return !operator==(other); }
67#endif
68
69private:
70 friend bool comparesEqual(const QXmlStreamAttribute &lhs,
71 const QXmlStreamAttribute &rhs) noexcept
72 {
73 return (lhs.value() == rhs.value()
74 && (lhs.namespaceUri().isNull() ? (lhs.qualifiedName() == rhs.qualifiedName())
75 : (lhs.namespaceUri() == rhs.namespaceUri()
76 && lhs.name() == rhs.name())));
77 }
78 Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamAttribute)
79};
80
81Q_DECLARE_TYPEINFO(QXmlStreamAttribute, Q_RELOCATABLE_TYPE);
82
83// We export each out-of-line method individually to prevent MSVC from
84// exporting the whole QList class.
85class QXmlStreamAttributes : public QList<QXmlStreamAttribute>
86{
87public:
88 inline QXmlStreamAttributes() {}
89#if QT_CORE_REMOVED_SINCE(6, 6)
90 Q_CORE_EXPORT QStringView value(const QString &namespaceUri, const QString &name) const;
91 Q_CORE_EXPORT QStringView value(const QString &namespaceUri, QLatin1StringView name) const;
92 Q_CORE_EXPORT QStringView value(QLatin1StringView namespaceUri, QLatin1StringView name) const;
93 Q_CORE_EXPORT QStringView value(const QString &qualifiedName) const;
94 Q_CORE_EXPORT QStringView value(QLatin1StringView qualifiedName) const;
95#endif
96 Q_CORE_EXPORT QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const noexcept;
97 Q_CORE_EXPORT QStringView value(QAnyStringView qualifiedName) const noexcept;
98
99 Q_CORE_EXPORT void append(const QString &namespaceUri, const QString &name, const QString &value);
100 Q_CORE_EXPORT void append(const QString &qualifiedName, const QString &value);
101
102 bool hasAttribute(QAnyStringView qualifiedName) const
103 {
104 return !value(qualifiedName).isNull();
105 }
106
107 bool hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const
108 {
109 return !value(namespaceUri, name).isNull();
110 }
111
112 using QList<QXmlStreamAttribute>::append;
113};
114
115class Q_CORE_EXPORT QXmlStreamNamespaceDeclaration {
116 QtPrivate::QXmlString m_prefix, m_namespaceUri;
117
118 friend class QXmlStreamReaderPrivate;
119public:
120 QXmlStreamNamespaceDeclaration();
121 QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri);
122
123 inline QStringView prefix() const { return m_prefix; }
124 inline QStringView namespaceUri() const { return m_namespaceUri; }
125#if QT_CORE_REMOVED_SINCE(6, 8)
126 inline bool operator==(const QXmlStreamNamespaceDeclaration &other) const
127 { return comparesEqual(*this, other); }
128 inline bool operator!=(const QXmlStreamNamespaceDeclaration &other) const
129 { return !operator==(other); }
130#endif
131private:
132 friend bool comparesEqual(const QXmlStreamNamespaceDeclaration &lhs,
133 const QXmlStreamNamespaceDeclaration &rhs) noexcept
134 {
135 return (lhs.prefix() == rhs.prefix() && lhs.namespaceUri() == rhs.namespaceUri());
136 }
137 Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamNamespaceDeclaration)
138};
139
140Q_DECLARE_TYPEINFO(QXmlStreamNamespaceDeclaration, Q_RELOCATABLE_TYPE);
141typedef QList<QXmlStreamNamespaceDeclaration> QXmlStreamNamespaceDeclarations;
142
143class Q_CORE_EXPORT QXmlStreamNotationDeclaration {
144 QtPrivate::QXmlString m_name, m_systemId, m_publicId;
145
146 friend class QXmlStreamReaderPrivate;
147public:
148 QXmlStreamNotationDeclaration();
149
150 inline QStringView name() const { return m_name; }
151 inline QStringView systemId() const { return m_systemId; }
152 inline QStringView publicId() const { return m_publicId; }
153#if QT_CORE_REMOVED_SINCE(6, 8)
154 inline bool operator==(const QXmlStreamNotationDeclaration &other) const
155 { return comparesEqual(*this, other); }
156 inline bool operator!=(const QXmlStreamNotationDeclaration &other) const
157 { return !operator==(other); }
158#endif
159private:
160 friend bool comparesEqual(const QXmlStreamNotationDeclaration &lhs,
161 const QXmlStreamNotationDeclaration &rhs) noexcept
162 {
163 return (lhs.name() == rhs.name() && lhs.systemId() == rhs.systemId()
164 && lhs.publicId() == rhs.publicId());
165 }
166 Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamNotationDeclaration)
167};
168
169Q_DECLARE_TYPEINFO(QXmlStreamNotationDeclaration, Q_RELOCATABLE_TYPE);
170typedef QList<QXmlStreamNotationDeclaration> QXmlStreamNotationDeclarations;
171
172class Q_CORE_EXPORT QXmlStreamEntityDeclaration {
173 QtPrivate::QXmlString m_name, m_notationName, m_systemId, m_publicId, m_value;
174
175 friend class QXmlStreamReaderPrivate;
176public:
177 QXmlStreamEntityDeclaration();
178
179 inline QStringView name() const { return m_name; }
180 inline QStringView notationName() const { return m_notationName; }
181 inline QStringView systemId() const { return m_systemId; }
182 inline QStringView publicId() const { return m_publicId; }
183 inline QStringView value() const { return m_value; }
184#if QT_CORE_REMOVED_SINCE(6, 8)
185 inline bool operator==(const QXmlStreamEntityDeclaration &other) const
186 { return comparesEqual(*this, other); }
187 inline bool operator!=(const QXmlStreamEntityDeclaration &other) const
188 { return !operator==(other); }
189#endif
190
191private:
192 friend bool comparesEqual(const QXmlStreamEntityDeclaration &lhs,
193 const QXmlStreamEntityDeclaration &rhs) noexcept
194 {
195 return (lhs.name() == rhs.name()
196 && lhs.notationName() == rhs.notationName()
197 && lhs.systemId() == rhs.systemId()
198 && lhs.publicId() == rhs.publicId()
199 && lhs.value() == rhs.value());
200 }
201 Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamEntityDeclaration)
202};
203
204Q_DECLARE_TYPEINFO(QXmlStreamEntityDeclaration, Q_RELOCATABLE_TYPE);
205typedef QList<QXmlStreamEntityDeclaration> QXmlStreamEntityDeclarations;
206
207class Q_CORE_EXPORT QXmlStreamEntityResolver
208{
209 Q_DISABLE_COPY_MOVE(QXmlStreamEntityResolver)
210public:
211 QXmlStreamEntityResolver() = default;
212 virtual ~QXmlStreamEntityResolver();
213 virtual QString resolveEntity(const QString& publicId, const QString& systemId);
214 virtual QString resolveUndeclaredEntity(const QString &name);
215};
216
217#if QT_CONFIG(xmlstreamreader)
218class Q_CORE_EXPORT QXmlStreamReader
219{
220 QDOC_PROPERTY(bool namespaceProcessing READ namespaceProcessing WRITE setNamespaceProcessing)
221public:
222 enum TokenType {
223 NoToken = 0,
224 Invalid,
225 StartDocument,
226 EndDocument,
227 StartElement,
228 EndElement,
229 Characters,
230 Comment,
231 DTD,
232 EntityReference,
233 ProcessingInstruction
234 };
235
236
237 QXmlStreamReader();
238 explicit QXmlStreamReader(QIODevice *device);
239#if QT_CORE_REMOVED_SINCE(6, 5)
240 explicit QXmlStreamReader(const QByteArray &data);
241 explicit QXmlStreamReader(const QString &data);
242 explicit QXmlStreamReader(const char * data);
243#endif // QT_CORE_REMOVED_SINCE(6, 5)
245 explicit QXmlStreamReader(const QByteArray &data)
246 : QXmlStreamReader(data, PrivateConstructorTag{}) { }
247 explicit QXmlStreamReader(QAnyStringView data);
248 ~QXmlStreamReader();
249
250 void setDevice(QIODevice *device);
251 QIODevice *device() const;
252#if QT_CORE_REMOVED_SINCE(6, 5)
253 void addData(const QByteArray &data);
254 void addData(const QString &data);
255 void addData(const char *data);
256#endif // QT_CORE_REMOVED_SINCE(6, 5)
258 void addData(const QByteArray &data) { addDataImpl(data); }
259 void addData(QAnyStringView data);
260 void clear();
261
262
263 bool atEnd() const;
264 TokenType readNext();
265
266 bool readNextStartElement();
267 void skipCurrentElement();
268
269 TokenType tokenType() const;
270 QString tokenString() const;
271
272 void setNamespaceProcessing(bool);
273 bool namespaceProcessing() const;
274
275 inline bool isStartDocument() const { return tokenType() == StartDocument; }
276 inline bool isEndDocument() const { return tokenType() == EndDocument; }
277 inline bool isStartElement() const { return tokenType() == StartElement; }
278 inline bool isEndElement() const { return tokenType() == EndElement; }
279 inline bool isCharacters() const { return tokenType() == Characters; }
280 bool isWhitespace() const;
281 bool isCDATA() const;
282 inline bool isComment() const { return tokenType() == Comment; }
283 inline bool isDTD() const { return tokenType() == DTD; }
284 inline bool isEntityReference() const { return tokenType() == EntityReference; }
285 inline bool isProcessingInstruction() const { return tokenType() == ProcessingInstruction; }
286
287 bool isStandaloneDocument() const;
288 bool hasStandaloneDeclaration() const;
289 QStringView documentVersion() const;
290 QStringView documentEncoding() const;
291
292 qint64 lineNumber() const;
293 qint64 columnNumber() const;
294 qint64 characterOffset() const;
295
296 QXmlStreamAttributes attributes() const;
297
298 enum ReadElementTextBehaviour {
299 ErrorOnUnexpectedElement,
300 IncludeChildElements,
301 SkipChildElements
302 };
303 QString readElementText(ReadElementTextBehaviour behaviour = ErrorOnUnexpectedElement);
304
305 QStringView name() const;
306 QStringView namespaceUri() const;
307 QStringView qualifiedName() const;
308 QStringView prefix() const;
309
310 QStringView processingInstructionTarget() const;
311 QStringView processingInstructionData() const;
312
313 QStringView text() const;
314
315 QXmlStreamNamespaceDeclarations namespaceDeclarations() const;
316 void addExtraNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &extraNamespaceDeclaraction);
317 void addExtraNamespaceDeclarations(const QXmlStreamNamespaceDeclarations &extraNamespaceDeclaractions);
318 QXmlStreamNotationDeclarations notationDeclarations() const;
319 QXmlStreamEntityDeclarations entityDeclarations() const;
320 QStringView dtdName() const;
321 QStringView dtdPublicId() const;
322 QStringView dtdSystemId() const;
323
324 int entityExpansionLimit() const;
325 void setEntityExpansionLimit(int limit);
326
327 enum Error {
328 NoError,
329 UnexpectedElementError,
330 CustomError,
331 NotWellFormedError,
332 PrematureEndOfDocumentError
333 };
334 void raiseError(const QString& message = QString());
335 QString errorString() const;
336 Error error() const;
337
338 inline bool hasError() const
339 {
340 return error() != NoError;
341 }
342
343 void setEntityResolver(QXmlStreamEntityResolver *resolver);
344 QXmlStreamEntityResolver *entityResolver() const;
345
346private:
347 struct PrivateConstructorTag { };
348 QXmlStreamReader(const QByteArray &data, PrivateConstructorTag);
349 void addDataImpl(const QByteArray &data);
350
351 Q_DISABLE_COPY(QXmlStreamReader)
352 Q_DECLARE_PRIVATE(QXmlStreamReader)
353 QScopedPointer<QXmlStreamReaderPrivate> d_ptr;
354
355};
356#endif // feature xmlstreamreader
357
358#if QT_CONFIG(xmlstreamwriter)
359
360class QXmlStreamWriterPrivate;
361
362class Q_CORE_EXPORT QXmlStreamWriter
363{
364 QDOC_PROPERTY(bool autoFormatting READ autoFormatting WRITE setAutoFormatting)
365 QDOC_PROPERTY(int autoFormattingIndent READ autoFormattingIndent WRITE setAutoFormattingIndent)
366public:
367 QXmlStreamWriter();
368 explicit QXmlStreamWriter(QIODevice *device);
369 explicit QXmlStreamWriter(QByteArray *array);
370 explicit QXmlStreamWriter(QString *string);
371 ~QXmlStreamWriter();
372
373 void setDevice(QIODevice *device);
374 QIODevice *device() const;
375
376 void setAutoFormatting(bool);
377 bool autoFormatting() const;
378
379 void setAutoFormattingIndent(int spacesOrTabs);
380 int autoFormattingIndent() const;
381
382#if QT_CORE_REMOVED_SINCE(6,5)
383 void writeAttribute(const QString &qualifiedName, const QString &value);
384 void writeAttribute(const QString &namespaceUri, const QString &name, const QString &value);
385#endif
388
389 void writeAttribute(const QXmlStreamAttribute& attribute);
390 void writeAttributes(const QXmlStreamAttributes& attributes);
391
392#if QT_CORE_REMOVED_SINCE(6,5)
393 void writeCDATA(const QString &text);
394 void writeCharacters(const QString &text);
395 void writeComment(const QString &text);
396
397 void writeDTD(const QString &dtd);
398
399 void writeEmptyElement(const QString &qualifiedName);
400 void writeEmptyElement(const QString &namespaceUri, const QString &name);
401
402 void writeTextElement(const QString &qualifiedName, const QString &text);
403 void writeTextElement(const QString &namespaceUri, const QString &name, const QString &text);
404#endif
405 void writeCDATA(QAnyStringView text);
407 void writeComment(QAnyStringView text);
408
409 void writeDTD(QAnyStringView dtd);
410
411 void writeEmptyElement(QAnyStringView qualifiedName);
412 void writeEmptyElement(QAnyStringView namespaceUri, QAnyStringView name);
413
414 void writeTextElement(QAnyStringView qualifiedName, QAnyStringView text);
415 void writeTextElement(QAnyStringView namespaceUri, QAnyStringView name, QAnyStringView text);
416
417
418 void writeEndDocument();
419 void writeEndElement();
420
421#if QT_CORE_REMOVED_SINCE(6,5)
422 void writeEntityReference(const QString &name);
423 void writeNamespace(const QString &namespaceUri, const QString &prefix);
424 void writeDefaultNamespace(const QString &namespaceUri);
425 void writeProcessingInstruction(const QString &target, const QString &data);
426#endif
427 void writeEntityReference(QAnyStringView name);
428 void writeNamespace(QAnyStringView namespaceUri, QAnyStringView prefix = {});
429 void writeDefaultNamespace(QAnyStringView namespaceUri);
430 void writeProcessingInstruction(QAnyStringView target, QAnyStringView data = {});
431
432 void writeStartDocument();
433#if QT_CORE_REMOVED_SINCE(6,5)
434 void writeStartDocument(const QString &version);
435 void writeStartDocument(const QString &version, bool standalone);
436 void writeStartElement(const QString &qualifiedName);
437 void writeStartElement(const QString &namespaceUri, const QString &name);
438#endif
439 void writeStartDocument(QAnyStringView version);
440 void writeStartDocument(QAnyStringView version, bool standalone);
441 void writeStartElement(QAnyStringView qualifiedName);
443
444#if QT_CONFIG(xmlstreamreader)
445 void writeCurrentToken(const QXmlStreamReader &reader);
446#endif
447
448 bool hasError() const;
449
450private:
451 Q_DISABLE_COPY(QXmlStreamWriter)
452 Q_DECLARE_PRIVATE(QXmlStreamWriter)
453 QScopedPointer<QXmlStreamWriterPrivate> d_ptr;
454};
455#endif // feature xmlstreamwriter
456
458
459#endif // feature xmlstream
460
461#endif // QXMLSTREAM_H
IOBluetoothDevice * device
\inmodule QtCore
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore \reentrant
Definition qiodevice.h:34
Definition qlist.h:75
\inmodule QtCore
Definition qstringview.h:78
constexpr QStringView left(qsizetype n) const noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
b clear()
QString text
list append(new Employee("Blackpool", "Stephen"))
Combined button and popup list for selecting options.
Q_MULTIMEDIA_EXPORT QString errorString(HRESULT hr)
\macro QT_NO_KEYWORDS >
static void writeAttribute(QXmlStreamWriter *stream, const QVariant &attribute)
#define Q_DECLARE_EQUALITY_COMPARABLE(...)
#define Q_WEAK_OVERLOAD
constexpr bool operator!=(const timespec &t1, const timespec &t2)
DBusConnection const char DBusError * error
bool comparesEqual(const QDir &lhs, const QDir &rhs)
Definition qdir.cpp:1819
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
EGLOutputLayerEXT EGLint attribute
@ Invalid
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
static bool isWhitespace(char c)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum target
GLuint GLsizei const GLchar * message
GLuint name
GLdouble s
[6]
Definition qopenglext.h:235
GLenum array
GLint limit
const void * data_ptr(const QTransform &t)
Definition qpainter_p.h:48
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
@ NoError
Definition main.cpp:34
static QChar resolveEntity(QStringView entity)
#define QDOC_PROPERTY(text)
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:158
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
long long qint64
Definition qtypes.h:60
QSharedPointer< T > other(t)
[5]
this swap(other)
writeEndElement()
writeCharacters(text)
writeStartElement(qualifiedName)
[0]