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 | |
16 | QT_BEGIN_NAMESPACE |
17 | |
18 | namespace QtPrivate { |
19 | |
20 | class QXmlString { |
21 | QStringPrivate m_string; |
22 | public: |
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(other&: s.data_ptr()); return *this; } |
27 | inline constexpr QXmlString() {} |
28 | |
29 | void swap(QXmlString &other) noexcept |
30 | { |
31 | m_string.swap(other&: other.m_string); |
32 | } |
33 | |
34 | operator QStringView() const noexcept { return QStringView(m_string.data(), m_string.size); } |
35 | qsizetype size() const noexcept { return m_string.size; } |
36 | bool isNull() const noexcept { return m_string.isNull(); } |
37 | |
38 | private: |
39 | friend bool comparesEqual(const QXmlString &lhs, const QXmlString &rhs) noexcept |
40 | { |
41 | return QStringView(lhs) == QStringView(rhs); |
42 | } |
43 | Q_DECLARE_EQUALITY_COMPARABLE(QXmlString) |
44 | }; |
45 | |
46 | } |
47 | Q_DECLARE_SHARED_NS_EXT(QtPrivate, QXmlString) |
48 | |
49 | |
50 | class QXmlStreamReaderPrivate; |
51 | class QXmlStreamAttributes; |
52 | class Q_CORE_EXPORT QXmlStreamAttribute { |
53 | QtPrivate::QXmlString m_name, m_namespaceUri, m_qualifiedName, m_value; |
54 | uint m_isDefault : 1; |
55 | friend class QXmlStreamReaderPrivate; |
56 | friend class QXmlStreamAttributes; |
57 | public: |
58 | QXmlStreamAttribute(); |
59 | QXmlStreamAttribute(const QString &qualifiedName, const QString &value); |
60 | QXmlStreamAttribute(const QString &namespaceUri, const QString &name, const QString &value); |
61 | |
62 | inline QStringView namespaceUri() const { return m_namespaceUri; } |
63 | inline QStringView name() const { return m_name; } |
64 | inline QStringView qualifiedName() const { return m_qualifiedName; } |
65 | inline QStringView prefix() const { |
66 | return QStringView(m_qualifiedName).left(n: qMax(a: 0, b: m_qualifiedName.size() - m_name.size() - 1)); |
67 | } |
68 | inline QStringView value() const { return m_value; } |
69 | inline bool isDefault() const { return m_isDefault; } |
70 | #if QT_CORE_REMOVED_SINCE(6, 8) |
71 | inline bool operator==(const QXmlStreamAttribute &other) const |
72 | { return comparesEqual(*this, other); } |
73 | inline bool operator!=(const QXmlStreamAttribute &other) const |
74 | { return !operator==(other); } |
75 | #endif |
76 | |
77 | private: |
78 | friend bool comparesEqual(const QXmlStreamAttribute &lhs, |
79 | const QXmlStreamAttribute &rhs) noexcept |
80 | { |
81 | if (lhs.m_value != rhs.m_value) |
82 | return false; |
83 | if (lhs.m_namespaceUri.isNull()) |
84 | return lhs.m_qualifiedName == rhs.m_qualifiedName; |
85 | return lhs.m_namespaceUri == rhs.m_namespaceUri |
86 | && lhs.m_name == rhs.m_name; |
87 | } |
88 | Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamAttribute) |
89 | }; |
90 | |
91 | Q_DECLARE_TYPEINFO(QXmlStreamAttribute, Q_RELOCATABLE_TYPE); |
92 | |
93 | // We export each out-of-line method individually to prevent MSVC from |
94 | // exporting the whole QList class. |
95 | class QXmlStreamAttributes : public QList<QXmlStreamAttribute> |
96 | { |
97 | public: |
98 | inline QXmlStreamAttributes() {} |
99 | #if QT_CORE_REMOVED_SINCE(6, 6) |
100 | Q_CORE_EXPORT QStringView value(const QString &namespaceUri, const QString &name) const; |
101 | Q_CORE_EXPORT QStringView value(const QString &namespaceUri, QLatin1StringView name) const; |
102 | Q_CORE_EXPORT QStringView value(QLatin1StringView namespaceUri, QLatin1StringView name) const; |
103 | Q_CORE_EXPORT QStringView value(const QString &qualifiedName) const; |
104 | Q_CORE_EXPORT QStringView value(QLatin1StringView qualifiedName) const; |
105 | #endif |
106 | Q_CORE_EXPORT QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const noexcept; |
107 | Q_CORE_EXPORT QStringView value(QAnyStringView qualifiedName) const noexcept; |
108 | |
109 | Q_CORE_EXPORT void append(const QString &namespaceUri, const QString &name, const QString &value); |
110 | Q_CORE_EXPORT void append(const QString &qualifiedName, const QString &value); |
111 | |
112 | bool hasAttribute(QAnyStringView qualifiedName) const |
113 | { |
114 | return !value(qualifiedName).isNull(); |
115 | } |
116 | |
117 | bool hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const |
118 | { |
119 | return !value(namespaceUri, name).isNull(); |
120 | } |
121 | |
122 | using QList<QXmlStreamAttribute>::append; |
123 | }; |
124 | |
125 | class Q_CORE_EXPORT QXmlStreamNamespaceDeclaration { |
126 | QtPrivate::QXmlString m_prefix, m_namespaceUri; |
127 | |
128 | friend class QXmlStreamReaderPrivate; |
129 | public: |
130 | QXmlStreamNamespaceDeclaration(); |
131 | QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri); |
132 | |
133 | inline QStringView prefix() const { return m_prefix; } |
134 | inline QStringView namespaceUri() const { return m_namespaceUri; } |
135 | #if QT_CORE_REMOVED_SINCE(6, 8) |
136 | inline bool operator==(const QXmlStreamNamespaceDeclaration &other) const |
137 | { return comparesEqual(*this, other); } |
138 | inline bool operator!=(const QXmlStreamNamespaceDeclaration &other) const |
139 | { return !operator==(other); } |
140 | #endif |
141 | private: |
142 | friend bool comparesEqual(const QXmlStreamNamespaceDeclaration &lhs, |
143 | const QXmlStreamNamespaceDeclaration &rhs) noexcept |
144 | { |
145 | return lhs.m_prefix == rhs.m_prefix |
146 | && lhs.m_namespaceUri == rhs.m_namespaceUri; |
147 | } |
148 | Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamNamespaceDeclaration) |
149 | }; |
150 | |
151 | Q_DECLARE_TYPEINFO(QXmlStreamNamespaceDeclaration, Q_RELOCATABLE_TYPE); |
152 | typedef QList<QXmlStreamNamespaceDeclaration> QXmlStreamNamespaceDeclarations; |
153 | |
154 | class Q_CORE_EXPORT QXmlStreamNotationDeclaration { |
155 | QtPrivate::QXmlString m_name, m_systemId, m_publicId; |
156 | |
157 | friend class QXmlStreamReaderPrivate; |
158 | public: |
159 | QXmlStreamNotationDeclaration(); |
160 | |
161 | inline QStringView name() const { return m_name; } |
162 | inline QStringView systemId() const { return m_systemId; } |
163 | inline QStringView publicId() const { return m_publicId; } |
164 | #if QT_CORE_REMOVED_SINCE(6, 8) |
165 | inline bool operator==(const QXmlStreamNotationDeclaration &other) const |
166 | { return comparesEqual(*this, other); } |
167 | inline bool operator!=(const QXmlStreamNotationDeclaration &other) const |
168 | { return !operator==(other); } |
169 | #endif |
170 | private: |
171 | friend bool comparesEqual(const QXmlStreamNotationDeclaration &lhs, |
172 | const QXmlStreamNotationDeclaration &rhs) noexcept |
173 | { |
174 | return lhs.m_name == rhs.m_name && lhs.m_systemId == rhs.m_systemId |
175 | && lhs.m_publicId == rhs.m_publicId; |
176 | } |
177 | Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamNotationDeclaration) |
178 | }; |
179 | |
180 | Q_DECLARE_TYPEINFO(QXmlStreamNotationDeclaration, Q_RELOCATABLE_TYPE); |
181 | typedef QList<QXmlStreamNotationDeclaration> QXmlStreamNotationDeclarations; |
182 | |
183 | class Q_CORE_EXPORT QXmlStreamEntityDeclaration { |
184 | QtPrivate::QXmlString m_name, m_notationName, m_systemId, m_publicId, m_value; |
185 | |
186 | friend class QXmlStreamReaderPrivate; |
187 | public: |
188 | QXmlStreamEntityDeclaration(); |
189 | |
190 | inline QStringView name() const { return m_name; } |
191 | inline QStringView notationName() const { return m_notationName; } |
192 | inline QStringView systemId() const { return m_systemId; } |
193 | inline QStringView publicId() const { return m_publicId; } |
194 | inline QStringView value() const { return m_value; } |
195 | #if QT_CORE_REMOVED_SINCE(6, 8) |
196 | inline bool operator==(const QXmlStreamEntityDeclaration &other) const |
197 | { return comparesEqual(*this, other); } |
198 | inline bool operator!=(const QXmlStreamEntityDeclaration &other) const |
199 | { return !operator==(other); } |
200 | #endif |
201 | |
202 | private: |
203 | friend bool comparesEqual(const QXmlStreamEntityDeclaration &lhs, |
204 | const QXmlStreamEntityDeclaration &rhs) noexcept |
205 | { |
206 | return lhs.m_name == rhs.m_name |
207 | && lhs.m_notationName == rhs.m_notationName |
208 | && lhs.m_systemId == rhs.m_systemId |
209 | && lhs.m_publicId == rhs.m_publicId |
210 | && lhs.m_value == rhs.m_value; |
211 | } |
212 | Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamEntityDeclaration) |
213 | }; |
214 | |
215 | Q_DECLARE_TYPEINFO(QXmlStreamEntityDeclaration, Q_RELOCATABLE_TYPE); |
216 | typedef QList<QXmlStreamEntityDeclaration> QXmlStreamEntityDeclarations; |
217 | |
218 | class Q_CORE_EXPORT QXmlStreamEntityResolver |
219 | { |
220 | Q_DISABLE_COPY_MOVE(QXmlStreamEntityResolver) |
221 | public: |
222 | QXmlStreamEntityResolver() = default; |
223 | virtual ~QXmlStreamEntityResolver(); |
224 | virtual QString resolveEntity(const QString& publicId, const QString& systemId); |
225 | virtual QString resolveUndeclaredEntity(const QString &name); |
226 | }; |
227 | |
228 | #if QT_CONFIG(xmlstreamreader) |
229 | class Q_CORE_EXPORT QXmlStreamReader |
230 | { |
231 | QDOC_PROPERTY(bool namespaceProcessing READ namespaceProcessing WRITE setNamespaceProcessing) |
232 | public: |
233 | enum TokenType { |
234 | NoToken = 0, |
235 | Invalid, |
236 | StartDocument, |
237 | EndDocument, |
238 | StartElement, |
239 | EndElement, |
240 | Characters, |
241 | Comment, |
242 | DTD, |
243 | EntityReference, |
244 | ProcessingInstruction |
245 | }; |
246 | |
247 | |
248 | QXmlStreamReader(); |
249 | explicit QXmlStreamReader(QIODevice *device); |
250 | #if QT_CORE_REMOVED_SINCE(6, 5) |
251 | explicit QXmlStreamReader(const QByteArray &data); |
252 | explicit QXmlStreamReader(const QString &data); |
253 | explicit QXmlStreamReader(const char * data); |
254 | #endif // QT_CORE_REMOVED_SINCE(6, 5) |
255 | Q_WEAK_OVERLOAD |
256 | explicit QXmlStreamReader(const QByteArray &data) |
257 | : QXmlStreamReader(data, PrivateConstructorTag{}) { } |
258 | explicit QXmlStreamReader(QAnyStringView data); |
259 | ~QXmlStreamReader(); |
260 | |
261 | void setDevice(QIODevice *device); |
262 | QIODevice *device() const; |
263 | #if QT_CORE_REMOVED_SINCE(6, 5) |
264 | void addData(const QByteArray &data); |
265 | void addData(const QString &data); |
266 | void addData(const char *data); |
267 | #endif // QT_CORE_REMOVED_SINCE(6, 5) |
268 | Q_WEAK_OVERLOAD |
269 | void addData(const QByteArray &data) { addDataImpl(data); } |
270 | void addData(QAnyStringView data); |
271 | void clear(); |
272 | |
273 | |
274 | bool atEnd() const; |
275 | TokenType readNext(); |
276 | |
277 | bool readNextStartElement(); |
278 | void skipCurrentElement(); |
279 | |
280 | TokenType tokenType() const; |
281 | QString tokenString() const; |
282 | |
283 | void setNamespaceProcessing(bool); |
284 | bool namespaceProcessing() const; |
285 | |
286 | inline bool isStartDocument() const { return tokenType() == StartDocument; } |
287 | inline bool isEndDocument() const { return tokenType() == EndDocument; } |
288 | inline bool isStartElement() const { return tokenType() == StartElement; } |
289 | inline bool isEndElement() const { return tokenType() == EndElement; } |
290 | inline bool isCharacters() const { return tokenType() == Characters; } |
291 | bool isWhitespace() const; |
292 | bool isCDATA() const; |
293 | inline bool isComment() const { return tokenType() == Comment; } |
294 | inline bool isDTD() const { return tokenType() == DTD; } |
295 | inline bool isEntityReference() const { return tokenType() == EntityReference; } |
296 | inline bool isProcessingInstruction() const { return tokenType() == ProcessingInstruction; } |
297 | |
298 | bool isStandaloneDocument() const; |
299 | bool hasStandaloneDeclaration() const; |
300 | QStringView documentVersion() const; |
301 | QStringView documentEncoding() const; |
302 | |
303 | qint64 lineNumber() const; |
304 | qint64 columnNumber() const; |
305 | qint64 characterOffset() const; |
306 | |
307 | QXmlStreamAttributes attributes() const; |
308 | |
309 | enum ReadElementTextBehaviour { |
310 | ErrorOnUnexpectedElement, |
311 | IncludeChildElements, |
312 | SkipChildElements |
313 | }; |
314 | QString readElementText(ReadElementTextBehaviour behaviour = ErrorOnUnexpectedElement); |
315 | |
316 | QStringView name() const; |
317 | QStringView namespaceUri() const; |
318 | QStringView qualifiedName() const; |
319 | QStringView prefix() const; |
320 | |
321 | QStringView processingInstructionTarget() const; |
322 | QStringView processingInstructionData() const; |
323 | |
324 | QStringView text() const; |
325 | |
326 | QXmlStreamNamespaceDeclarations namespaceDeclarations() const; |
327 | void addExtraNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &extraNamespaceDeclaraction); |
328 | void addExtraNamespaceDeclarations(const QXmlStreamNamespaceDeclarations &extraNamespaceDeclaractions); |
329 | QXmlStreamNotationDeclarations notationDeclarations() const; |
330 | QXmlStreamEntityDeclarations entityDeclarations() const; |
331 | QStringView dtdName() const; |
332 | QStringView dtdPublicId() const; |
333 | QStringView dtdSystemId() const; |
334 | |
335 | int entityExpansionLimit() const; |
336 | void setEntityExpansionLimit(int limit); |
337 | |
338 | enum Error { |
339 | NoError, |
340 | UnexpectedElementError, |
341 | CustomError, |
342 | NotWellFormedError, |
343 | PrematureEndOfDocumentError |
344 | }; |
345 | void raiseError(const QString& message = QString()); |
346 | QString errorString() const; |
347 | Error error() const; |
348 | |
349 | inline bool hasError() const |
350 | { |
351 | return error() != NoError; |
352 | } |
353 | |
354 | void setEntityResolver(QXmlStreamEntityResolver *resolver); |
355 | QXmlStreamEntityResolver *entityResolver() const; |
356 | |
357 | private: |
358 | struct PrivateConstructorTag { }; |
359 | QXmlStreamReader(const QByteArray &data, PrivateConstructorTag); |
360 | void addDataImpl(const QByteArray &data); |
361 | |
362 | Q_DISABLE_COPY(QXmlStreamReader) |
363 | Q_DECLARE_PRIVATE(QXmlStreamReader) |
364 | QScopedPointer<QXmlStreamReaderPrivate> d_ptr; |
365 | |
366 | }; |
367 | #endif // feature xmlstreamreader |
368 | |
369 | #if QT_CONFIG(xmlstreamwriter) |
370 | |
371 | class QXmlStreamWriterPrivate; |
372 | |
373 | class Q_CORE_EXPORT QXmlStreamWriter |
374 | { |
375 | QDOC_PROPERTY(bool autoFormatting READ autoFormatting WRITE setAutoFormatting) |
376 | QDOC_PROPERTY(int autoFormattingIndent READ autoFormattingIndent WRITE setAutoFormattingIndent) |
377 | public: |
378 | QXmlStreamWriter(); |
379 | explicit QXmlStreamWriter(QIODevice *device); |
380 | explicit QXmlStreamWriter(QByteArray *array); |
381 | explicit QXmlStreamWriter(QString *string); |
382 | ~QXmlStreamWriter(); |
383 | |
384 | void setDevice(QIODevice *device); |
385 | QIODevice *device() const; |
386 | |
387 | void setAutoFormatting(bool); |
388 | bool autoFormatting() const; |
389 | |
390 | void setAutoFormattingIndent(int spacesOrTabs); |
391 | int autoFormattingIndent() const; |
392 | |
393 | #if QT_CORE_REMOVED_SINCE(6,5) |
394 | void writeAttribute(const QString &qualifiedName, const QString &value); |
395 | void writeAttribute(const QString &namespaceUri, const QString &name, const QString &value); |
396 | #endif |
397 | void writeAttribute(QAnyStringView qualifiedName, QAnyStringView value); |
398 | void writeAttribute(QAnyStringView namespaceUri, QAnyStringView name, QAnyStringView value); |
399 | |
400 | void writeAttribute(const QXmlStreamAttribute& attribute); |
401 | void writeAttributes(const QXmlStreamAttributes& attributes); |
402 | |
403 | #if QT_CORE_REMOVED_SINCE(6,5) |
404 | void writeCDATA(const QString &text); |
405 | void writeCharacters(const QString &text); |
406 | void writeComment(const QString &text); |
407 | |
408 | void writeDTD(const QString &dtd); |
409 | |
410 | void writeEmptyElement(const QString &qualifiedName); |
411 | void writeEmptyElement(const QString &namespaceUri, const QString &name); |
412 | |
413 | void writeTextElement(const QString &qualifiedName, const QString &text); |
414 | void writeTextElement(const QString &namespaceUri, const QString &name, const QString &text); |
415 | #endif |
416 | void writeCDATA(QAnyStringView text); |
417 | void writeCharacters(QAnyStringView text); |
418 | void writeComment(QAnyStringView text); |
419 | |
420 | void writeDTD(QAnyStringView dtd); |
421 | |
422 | void writeEmptyElement(QAnyStringView qualifiedName); |
423 | void writeEmptyElement(QAnyStringView namespaceUri, QAnyStringView name); |
424 | |
425 | void writeTextElement(QAnyStringView qualifiedName, QAnyStringView text); |
426 | void writeTextElement(QAnyStringView namespaceUri, QAnyStringView name, QAnyStringView text); |
427 | |
428 | |
429 | void writeEndDocument(); |
430 | void writeEndElement(); |
431 | |
432 | #if QT_CORE_REMOVED_SINCE(6,5) |
433 | void writeEntityReference(const QString &name); |
434 | void writeNamespace(const QString &namespaceUri, const QString &prefix); |
435 | void writeDefaultNamespace(const QString &namespaceUri); |
436 | void writeProcessingInstruction(const QString &target, const QString &data); |
437 | #endif |
438 | void writeEntityReference(QAnyStringView name); |
439 | void writeNamespace(QAnyStringView namespaceUri, QAnyStringView prefix = {}); |
440 | void writeDefaultNamespace(QAnyStringView namespaceUri); |
441 | void writeProcessingInstruction(QAnyStringView target, QAnyStringView data = {}); |
442 | |
443 | void writeStartDocument(); |
444 | #if QT_CORE_REMOVED_SINCE(6,5) |
445 | void writeStartDocument(const QString &version); |
446 | void writeStartDocument(const QString &version, bool standalone); |
447 | void writeStartElement(const QString &qualifiedName); |
448 | void writeStartElement(const QString &namespaceUri, const QString &name); |
449 | #endif |
450 | void writeStartDocument(QAnyStringView version); |
451 | void writeStartDocument(QAnyStringView version, bool standalone); |
452 | void writeStartElement(QAnyStringView qualifiedName); |
453 | void writeStartElement(QAnyStringView namespaceUri, QAnyStringView name); |
454 | |
455 | #if QT_CONFIG(xmlstreamreader) |
456 | void writeCurrentToken(const QXmlStreamReader &reader); |
457 | #endif |
458 | |
459 | bool hasError() const; |
460 | |
461 | private: |
462 | Q_DISABLE_COPY(QXmlStreamWriter) |
463 | Q_DECLARE_PRIVATE(QXmlStreamWriter) |
464 | QScopedPointer<QXmlStreamWriterPrivate> d_ptr; |
465 | }; |
466 | #endif // feature xmlstreamwriter |
467 | |
468 | QT_END_NAMESPACE |
469 | |
470 | #endif // feature xmlstream |
471 | |
472 | #endif // QXMLSTREAM_H |
473 |
Definitions
- QXmlString
- QXmlString
- QXmlString
- operator=
- operator=
- QXmlString
- swap
- operator QStringView
- size
- isNull
- comparesEqual
- QXmlStreamAttribute
- namespaceUri
- name
- qualifiedName
- prefix
- value
- isDefault
- comparesEqual
- QXmlStreamAttributes
- QXmlStreamAttributes
- hasAttribute
- hasAttribute
- QXmlStreamNamespaceDeclaration
- prefix
- namespaceUri
- comparesEqual
- QXmlStreamNotationDeclaration
- name
- systemId
- publicId
- comparesEqual
- QXmlStreamEntityDeclaration
- name
- notationName
- systemId
- publicId
- value
- comparesEqual
- QXmlStreamEntityResolver
- QXmlStreamEntityResolver
- QXmlStreamEntityResolver
- QXmlStreamReader
- TokenType
- QXmlStreamReader
- addData
- isStartDocument
- isEndDocument
- isStartElement
- isEndElement
- isCharacters
- isComment
- isDTD
- isEntityReference
- isProcessingInstruction
- ReadElementTextBehaviour
- Error
- hasError
- PrivateConstructorTag
- QXmlStreamReader
- QXmlStreamWriter
Start learning QML with our Intro Training
Find out more