File indexing completed on 2026-05-08 08:49:33
0001
0002
0003
0004
0005 #ifndef QXMLSTREAM_H
0006 #define QXMLSTREAM_H
0007
0008 #include <QtCore/qiodevice.h>
0009
0010 #if QT_CONFIG(xmlstream)
0011
0012 #include <QtCore/qcompare.h>
0013 #include <QtCore/qlist.h>
0014 #include <QtCore/qscopedpointer.h>
0015 #include <QtCore/qstring.h>
0016
0017 #include <memory>
0018
0019 QT_BEGIN_NAMESPACE
0020
0021 namespace QtPrivate {
0022
0023 class QXmlString {
0024 QStringPrivate m_string;
0025 public:
0026 QXmlString(QStringPrivate &&d) : m_string(std::move(d)) {}
0027 QXmlString(const QString &s) : m_string(s.data_ptr()) {}
0028 QXmlString & operator=(const QString &s) { m_string = s.data_ptr(); return *this; }
0029 QXmlString & operator=(QString &&s) { m_string.swap(s.data_ptr()); return *this; }
0030 inline constexpr QXmlString() {}
0031
0032 void swap(QXmlString &other) noexcept
0033 {
0034 m_string.swap(other.m_string);
0035 }
0036
0037 operator QStringView() const noexcept { return QStringView(m_string.data(), m_string.size); }
0038 qsizetype size() const noexcept { return m_string.size; }
0039 bool isNull() const noexcept { return m_string.isNull(); }
0040
0041 private:
0042 friend bool comparesEqual(const QXmlString &lhs, const QXmlString &rhs) noexcept
0043 {
0044 return QStringView(lhs) == QStringView(rhs);
0045 }
0046 Q_DECLARE_EQUALITY_COMPARABLE(QXmlString)
0047 };
0048
0049 }
0050 Q_DECLARE_SHARED_NS_EXT(QtPrivate, QXmlString)
0051
0052
0053 class QXmlStreamReaderPrivate;
0054 class QXmlStreamAttributes;
0055 class Q_CORE_EXPORT QXmlStreamAttribute {
0056 QtPrivate::QXmlString m_name, m_namespaceUri, m_qualifiedName, m_value;
0057 uint m_isDefault : 1;
0058 friend class QXmlStreamReaderPrivate;
0059 friend class QXmlStreamAttributes;
0060 public:
0061 QXmlStreamAttribute();
0062 QXmlStreamAttribute(const QString &qualifiedName, const QString &value);
0063 QXmlStreamAttribute(const QString &namespaceUri, const QString &name, const QString &value);
0064
0065 inline QStringView namespaceUri() const { return m_namespaceUri; }
0066 inline QStringView name() const { return m_name; }
0067 inline QStringView qualifiedName() const { return m_qualifiedName; }
0068 inline QStringView prefix() const {
0069 return QStringView(m_qualifiedName).left(qMax(0, m_qualifiedName.size() - m_name.size() - 1));
0070 }
0071 inline QStringView value() const { return m_value; }
0072 inline bool isDefault() const { return m_isDefault; }
0073 #if QT_CORE_REMOVED_SINCE(6, 8)
0074 inline bool operator==(const QXmlStreamAttribute &other) const
0075 { return comparesEqual(*this, other); }
0076 inline bool operator!=(const QXmlStreamAttribute &other) const
0077 { return !operator==(other); }
0078 #endif
0079
0080 private:
0081 friend bool comparesEqual(const QXmlStreamAttribute &lhs,
0082 const QXmlStreamAttribute &rhs) noexcept
0083 {
0084 if (lhs.m_value != rhs.m_value)
0085 return false;
0086 if (lhs.m_namespaceUri.isNull())
0087 return lhs.m_qualifiedName == rhs.m_qualifiedName;
0088 return lhs.m_namespaceUri == rhs.m_namespaceUri
0089 && lhs.m_name == rhs.m_name;
0090 }
0091 Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamAttribute)
0092 };
0093
0094 Q_DECLARE_TYPEINFO(QXmlStreamAttribute, Q_RELOCATABLE_TYPE);
0095
0096
0097
0098 class QXmlStreamAttributes : public QList<QXmlStreamAttribute>
0099 {
0100 public:
0101 inline QXmlStreamAttributes() {}
0102 #if QT_CORE_REMOVED_SINCE(6, 6)
0103 Q_CORE_EXPORT QStringView value(const QString &namespaceUri, const QString &name) const;
0104 Q_CORE_EXPORT QStringView value(const QString &namespaceUri, QLatin1StringView name) const;
0105 Q_CORE_EXPORT QStringView value(QLatin1StringView namespaceUri, QLatin1StringView name) const;
0106 Q_CORE_EXPORT QStringView value(const QString &qualifiedName) const;
0107 Q_CORE_EXPORT QStringView value(QLatin1StringView qualifiedName) const;
0108 #endif
0109 Q_CORE_EXPORT QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const noexcept;
0110 Q_CORE_EXPORT QStringView value(QAnyStringView qualifiedName) const noexcept;
0111
0112 Q_CORE_EXPORT void append(const QString &namespaceUri, const QString &name, const QString &value);
0113 Q_CORE_EXPORT void append(const QString &qualifiedName, const QString &value);
0114
0115 bool hasAttribute(QAnyStringView qualifiedName) const
0116 {
0117 return !value(qualifiedName).isNull();
0118 }
0119
0120 bool hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const
0121 {
0122 return !value(namespaceUri, name).isNull();
0123 }
0124
0125 using QList<QXmlStreamAttribute>::append;
0126 };
0127
0128 class Q_CORE_EXPORT QXmlStreamNamespaceDeclaration {
0129 QtPrivate::QXmlString m_prefix, m_namespaceUri;
0130
0131 friend class QXmlStreamReaderPrivate;
0132 public:
0133 QXmlStreamNamespaceDeclaration();
0134 QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri);
0135
0136 inline QStringView prefix() const { return m_prefix; }
0137 inline QStringView namespaceUri() const { return m_namespaceUri; }
0138 #if QT_CORE_REMOVED_SINCE(6, 8)
0139 inline bool operator==(const QXmlStreamNamespaceDeclaration &other) const
0140 { return comparesEqual(*this, other); }
0141 inline bool operator!=(const QXmlStreamNamespaceDeclaration &other) const
0142 { return !operator==(other); }
0143 #endif
0144 private:
0145 friend bool comparesEqual(const QXmlStreamNamespaceDeclaration &lhs,
0146 const QXmlStreamNamespaceDeclaration &rhs) noexcept
0147 {
0148 return lhs.m_prefix == rhs.m_prefix
0149 && lhs.m_namespaceUri == rhs.m_namespaceUri;
0150 }
0151 Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamNamespaceDeclaration)
0152 };
0153
0154 Q_DECLARE_TYPEINFO(QXmlStreamNamespaceDeclaration, Q_RELOCATABLE_TYPE);
0155 typedef QList<QXmlStreamNamespaceDeclaration> QXmlStreamNamespaceDeclarations;
0156
0157 class Q_CORE_EXPORT QXmlStreamNotationDeclaration {
0158 QtPrivate::QXmlString m_name, m_systemId, m_publicId;
0159
0160 friend class QXmlStreamReaderPrivate;
0161 public:
0162 QXmlStreamNotationDeclaration();
0163
0164 inline QStringView name() const { return m_name; }
0165 inline QStringView systemId() const { return m_systemId; }
0166 inline QStringView publicId() const { return m_publicId; }
0167 #if QT_CORE_REMOVED_SINCE(6, 8)
0168 inline bool operator==(const QXmlStreamNotationDeclaration &other) const
0169 { return comparesEqual(*this, other); }
0170 inline bool operator!=(const QXmlStreamNotationDeclaration &other) const
0171 { return !operator==(other); }
0172 #endif
0173 private:
0174 friend bool comparesEqual(const QXmlStreamNotationDeclaration &lhs,
0175 const QXmlStreamNotationDeclaration &rhs) noexcept
0176 {
0177 return lhs.m_name == rhs.m_name && lhs.m_systemId == rhs.m_systemId
0178 && lhs.m_publicId == rhs.m_publicId;
0179 }
0180 Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamNotationDeclaration)
0181 };
0182
0183 Q_DECLARE_TYPEINFO(QXmlStreamNotationDeclaration, Q_RELOCATABLE_TYPE);
0184 typedef QList<QXmlStreamNotationDeclaration> QXmlStreamNotationDeclarations;
0185
0186 class Q_CORE_EXPORT QXmlStreamEntityDeclaration {
0187 QtPrivate::QXmlString m_name, m_notationName, m_systemId, m_publicId, m_value;
0188
0189 friend class QXmlStreamReaderPrivate;
0190 public:
0191 QXmlStreamEntityDeclaration();
0192
0193 inline QStringView name() const { return m_name; }
0194 inline QStringView notationName() const { return m_notationName; }
0195 inline QStringView systemId() const { return m_systemId; }
0196 inline QStringView publicId() const { return m_publicId; }
0197 inline QStringView value() const { return m_value; }
0198 #if QT_CORE_REMOVED_SINCE(6, 8)
0199 inline bool operator==(const QXmlStreamEntityDeclaration &other) const
0200 { return comparesEqual(*this, other); }
0201 inline bool operator!=(const QXmlStreamEntityDeclaration &other) const
0202 { return !operator==(other); }
0203 #endif
0204
0205 private:
0206 friend bool comparesEqual(const QXmlStreamEntityDeclaration &lhs,
0207 const QXmlStreamEntityDeclaration &rhs) noexcept
0208 {
0209 return lhs.m_name == rhs.m_name
0210 && lhs.m_notationName == rhs.m_notationName
0211 && lhs.m_systemId == rhs.m_systemId
0212 && lhs.m_publicId == rhs.m_publicId
0213 && lhs.m_value == rhs.m_value;
0214 }
0215 Q_DECLARE_EQUALITY_COMPARABLE(QXmlStreamEntityDeclaration)
0216 };
0217
0218 Q_DECLARE_TYPEINFO(QXmlStreamEntityDeclaration, Q_RELOCATABLE_TYPE);
0219 typedef QList<QXmlStreamEntityDeclaration> QXmlStreamEntityDeclarations;
0220
0221 class Q_CORE_EXPORT QXmlStreamEntityResolver
0222 {
0223 Q_DISABLE_COPY_MOVE(QXmlStreamEntityResolver)
0224 public:
0225 QXmlStreamEntityResolver() = default;
0226 virtual ~QXmlStreamEntityResolver();
0227 virtual QString resolveEntity(const QString& publicId, const QString& systemId);
0228 virtual QString resolveUndeclaredEntity(const QString &name);
0229 };
0230
0231 #if QT_CONFIG(xmlstreamreader)
0232 class Q_CORE_EXPORT QXmlStreamReader
0233 {
0234 QDOC_PROPERTY(bool namespaceProcessing READ namespaceProcessing WRITE setNamespaceProcessing)
0235 public:
0236 enum TokenType {
0237 NoToken = 0,
0238 Invalid,
0239 StartDocument,
0240 EndDocument,
0241 StartElement,
0242 EndElement,
0243 Characters,
0244 Comment,
0245 DTD,
0246 EntityReference,
0247 ProcessingInstruction
0248 };
0249
0250
0251 QXmlStreamReader();
0252 explicit QXmlStreamReader(QIODevice *device);
0253 #if QT_CORE_REMOVED_SINCE(6, 5)
0254 explicit QXmlStreamReader(const QByteArray &data);
0255 explicit QXmlStreamReader(const QString &data);
0256 explicit QXmlStreamReader(const char * data);
0257 #endif
0258 Q_WEAK_OVERLOAD
0259 explicit QXmlStreamReader(const QByteArray &data)
0260 : QXmlStreamReader(data, PrivateConstructorTag{}) { }
0261 explicit QXmlStreamReader(QAnyStringView data);
0262 ~QXmlStreamReader();
0263
0264 void setDevice(QIODevice *device);
0265 QIODevice *device() const;
0266 #if QT_CORE_REMOVED_SINCE(6, 5)
0267 void addData(const QByteArray &data);
0268 void addData(const QString &data);
0269 void addData(const char *data);
0270 #endif
0271 Q_WEAK_OVERLOAD
0272 void addData(const QByteArray &data) { addDataImpl(data); }
0273 void addData(QAnyStringView data);
0274 void clear();
0275
0276
0277 bool atEnd() const;
0278 TokenType readNext();
0279
0280 bool readNextStartElement();
0281 void skipCurrentElement();
0282 QString readRawInnerData();
0283
0284 TokenType tokenType() const;
0285 QString tokenString() const;
0286
0287 void setNamespaceProcessing(bool);
0288 bool namespaceProcessing() const;
0289
0290 inline bool isStartDocument() const { return tokenType() == StartDocument; }
0291 inline bool isEndDocument() const { return tokenType() == EndDocument; }
0292 inline bool isStartElement() const { return tokenType() == StartElement; }
0293 inline bool isEndElement() const { return tokenType() == EndElement; }
0294 inline bool isCharacters() const { return tokenType() == Characters; }
0295 bool isWhitespace() const;
0296 bool isCDATA() const;
0297 inline bool isComment() const { return tokenType() == Comment; }
0298 inline bool isDTD() const { return tokenType() == DTD; }
0299 inline bool isEntityReference() const { return tokenType() == EntityReference; }
0300 inline bool isProcessingInstruction() const { return tokenType() == ProcessingInstruction; }
0301
0302 bool isStandaloneDocument() const;
0303 bool hasStandaloneDeclaration() const;
0304 QStringView documentVersion() const;
0305 QStringView documentEncoding() const;
0306
0307 qint64 lineNumber() const;
0308 qint64 columnNumber() const;
0309 qint64 characterOffset() const;
0310
0311 QXmlStreamAttributes attributes() const;
0312
0313 enum ReadElementTextBehaviour {
0314 ErrorOnUnexpectedElement,
0315 IncludeChildElements,
0316 SkipChildElements
0317 };
0318 QString readElementText(ReadElementTextBehaviour behaviour = ErrorOnUnexpectedElement);
0319
0320 QStringView name() const;
0321 QStringView namespaceUri() const;
0322 QStringView qualifiedName() const;
0323 QStringView prefix() const;
0324
0325 QStringView processingInstructionTarget() const;
0326 QStringView processingInstructionData() const;
0327
0328 QStringView text() const;
0329
0330 QXmlStreamNamespaceDeclarations namespaceDeclarations() const;
0331 void addExtraNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &extraNamespaceDeclaraction);
0332 void addExtraNamespaceDeclarations(const QXmlStreamNamespaceDeclarations &extraNamespaceDeclaractions);
0333 QXmlStreamNotationDeclarations notationDeclarations() const;
0334 QXmlStreamEntityDeclarations entityDeclarations() const;
0335 QStringView dtdName() const;
0336 QStringView dtdPublicId() const;
0337 QStringView dtdSystemId() const;
0338
0339 int entityExpansionLimit() const;
0340 void setEntityExpansionLimit(int limit);
0341
0342 enum Error {
0343 NoError,
0344 UnexpectedElementError,
0345 CustomError,
0346 NotWellFormedError,
0347 PrematureEndOfDocumentError
0348 };
0349 void raiseError(const QString& message = QString());
0350 QString errorString() const;
0351 Error error() const;
0352
0353 inline bool hasError() const
0354 {
0355 return error() != NoError;
0356 }
0357
0358 void setEntityResolver(QXmlStreamEntityResolver *resolver);
0359 QXmlStreamEntityResolver *entityResolver() const;
0360
0361 private:
0362 struct PrivateConstructorTag { };
0363 QXmlStreamReader(const QByteArray &data, PrivateConstructorTag);
0364 void addDataImpl(const QByteArray &data);
0365
0366 Q_DISABLE_COPY(QXmlStreamReader)
0367 Q_DECLARE_PRIVATE(QXmlStreamReader)
0368 std::unique_ptr<QXmlStreamReaderPrivate> d_ptr;
0369
0370 };
0371 #endif
0372
0373 #if QT_CONFIG(xmlstreamwriter)
0374
0375 class QXmlStreamWriterPrivate;
0376
0377 class Q_CORE_EXPORT QXmlStreamWriter
0378 {
0379 QDOC_PROPERTY(bool autoFormatting READ autoFormatting WRITE setAutoFormatting)
0380 QDOC_PROPERTY(int autoFormattingIndent READ autoFormattingIndent WRITE setAutoFormattingIndent)
0381 QDOC_PROPERTY(bool stopWritingOnError READ stopWritingOnError WRITE setStopWritingOnError)
0382 public:
0383 QXmlStreamWriter();
0384 explicit QXmlStreamWriter(QIODevice *device);
0385 explicit QXmlStreamWriter(QByteArray *array);
0386 explicit QXmlStreamWriter(QString *string);
0387 ~QXmlStreamWriter();
0388
0389 void setDevice(QIODevice *device);
0390 QIODevice *device() const;
0391
0392 void setAutoFormatting(bool);
0393 bool autoFormatting() const;
0394
0395 void setAutoFormattingIndent(int spacesOrTabs);
0396 int autoFormattingIndent() const;
0397
0398 void setStopWritingOnError(bool stop);
0399 bool stopWritingOnError() const;
0400
0401 #if QT_CORE_REMOVED_SINCE(6,5)
0402 void writeAttribute(const QString &qualifiedName, const QString &value);
0403 void writeAttribute(const QString &namespaceUri, const QString &name, const QString &value);
0404 #endif
0405 void writeAttribute(QAnyStringView qualifiedName, QAnyStringView value);
0406 void writeAttribute(QAnyStringView namespaceUri, QAnyStringView name, QAnyStringView value);
0407
0408 void writeAttribute(const QXmlStreamAttribute& attribute);
0409 void writeAttributes(const QXmlStreamAttributes& attributes);
0410
0411 #if QT_CORE_REMOVED_SINCE(6,5)
0412 void writeCDATA(const QString &text);
0413 void writeCharacters(const QString &text);
0414 void writeComment(const QString &text);
0415
0416 void writeDTD(const QString &dtd);
0417
0418 void writeEmptyElement(const QString &qualifiedName);
0419 void writeEmptyElement(const QString &namespaceUri, const QString &name);
0420
0421 void writeTextElement(const QString &qualifiedName, const QString &text);
0422 void writeTextElement(const QString &namespaceUri, const QString &name, const QString &text);
0423 #endif
0424 void writeCDATA(QAnyStringView text);
0425 void writeCharacters(QAnyStringView text);
0426 void writeComment(QAnyStringView text);
0427
0428 void writeDTD(QAnyStringView dtd);
0429
0430 void writeEmptyElement(QAnyStringView qualifiedName);
0431 void writeEmptyElement(QAnyStringView namespaceUri, QAnyStringView name);
0432
0433 void writeTextElement(QAnyStringView qualifiedName, QAnyStringView text);
0434 void writeTextElement(QAnyStringView namespaceUri, QAnyStringView name, QAnyStringView text);
0435
0436
0437 void writeEndDocument();
0438 void writeEndElement();
0439
0440 #if QT_CORE_REMOVED_SINCE(6,5)
0441 void writeEntityReference(const QString &name);
0442 void writeNamespace(const QString &namespaceUri, const QString &prefix);
0443 void writeDefaultNamespace(const QString &namespaceUri);
0444 void writeProcessingInstruction(const QString &target, const QString &data);
0445 #endif
0446 void writeEntityReference(QAnyStringView name);
0447 void writeNamespace(QAnyStringView namespaceUri, QAnyStringView prefix = {});
0448 void writeDefaultNamespace(QAnyStringView namespaceUri);
0449 void writeProcessingInstruction(QAnyStringView target, QAnyStringView data = {});
0450
0451 void writeStartDocument();
0452 #if QT_CORE_REMOVED_SINCE(6,5)
0453 void writeStartDocument(const QString &version);
0454 void writeStartDocument(const QString &version, bool standalone);
0455 void writeStartElement(const QString &qualifiedName);
0456 void writeStartElement(const QString &namespaceUri, const QString &name);
0457 #endif
0458 void writeStartDocument(QAnyStringView version);
0459 void writeStartDocument(QAnyStringView version, bool standalone);
0460 void writeStartElement(QAnyStringView qualifiedName);
0461 void writeStartElement(QAnyStringView namespaceUri, QAnyStringView name);
0462
0463 #if QT_CONFIG(xmlstreamreader)
0464 void writeCurrentToken(const QXmlStreamReader &reader);
0465 #endif
0466
0467 enum class Error {
0468 None,
0469 IO,
0470 Encoding,
0471 InvalidCharacter,
0472 Custom,
0473 };
0474
0475 void raiseError(QAnyStringView message);
0476 QString errorString() const;
0477 Error error() const;
0478 bool hasError() const;
0479
0480 private:
0481 Q_DISABLE_COPY(QXmlStreamWriter)
0482 Q_DECLARE_PRIVATE(QXmlStreamWriter)
0483 std::unique_ptr<QXmlStreamWriterPrivate> d_ptr;
0484 };
0485 #endif
0486
0487 QT_END_NAMESPACE
0488
0489 #endif
0490
0491 #endif