| 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 QXML_H | 
| 5 | #define QXML_H | 
| 6 |  | 
| 7 | #include <QtCore5Compat/qcore5global.h> | 
| 8 |  | 
| 9 | #include <QtCore/qtextstream.h> | 
| 10 | #include <QtCore/qfile.h> | 
| 11 | #include <QtCore/qstring.h> | 
| 12 | #include <QtCore/qstringlist.h> | 
| 13 | #include <QtCore/qlist.h> | 
| 14 |  | 
| 15 | #include <memory> | 
| 16 |  | 
| 17 | QT_BEGIN_NAMESPACE | 
| 18 |  | 
| 19 | class QXmlNamespaceSupport; | 
| 20 | class QXmlAttributes; | 
| 21 | class QXmlContentHandler; | 
| 22 | class QXmlDefaultHandler; | 
| 23 | class QXmlDTDHandler; | 
| 24 | class QXmlEntityResolver; | 
| 25 | class QXmlErrorHandler; | 
| 26 | class QXmlLexicalHandler; | 
| 27 | class QXmlDeclHandler; | 
| 28 | class QXmlInputSource; | 
| 29 | class QXmlLocator; | 
| 30 | class QXmlNamespaceSupport; | 
| 31 | class QXmlParseException; | 
| 32 |  | 
| 33 | class QXmlReader; | 
| 34 | class QXmlSimpleReader; | 
| 35 |  | 
| 36 | class QXmlSimpleReaderPrivate; | 
| 37 | class QXmlNamespaceSupportPrivate; | 
| 38 | class QXmlAttributesPrivate; | 
| 39 | class QXmlInputSourcePrivate; | 
| 40 | class QXmlParseExceptionPrivate; | 
| 41 | class QXmlLocatorPrivate; | 
| 42 | class QXmlDefaultHandlerPrivate; | 
| 43 |  | 
| 44 | // | 
| 45 | // SAX Namespace Support | 
| 46 | // | 
| 47 |  | 
| 48 | class Q_CORE5COMPAT_EXPORT QXmlNamespaceSupport | 
| 49 | { | 
| 50 | public: | 
| 51 |     QXmlNamespaceSupport(); | 
| 52 |     ~QXmlNamespaceSupport(); | 
| 53 |  | 
| 54 |     void setPrefix(const QString&, const QString&); | 
| 55 |  | 
| 56 |     QString prefix(const QString&) const; | 
| 57 |     QString uri(const QString&) const; | 
| 58 |     void splitName(const QString&, QString&, QString&) const; | 
| 59 |     void processName(const QString&, bool, QString&, QString&) const; | 
| 60 |     QStringList prefixes() const; | 
| 61 |     QStringList prefixes(const QString&) const; | 
| 62 |  | 
| 63 |     void pushContext(); | 
| 64 |     void popContext(); | 
| 65 |     void reset(); | 
| 66 |  | 
| 67 | private: | 
| 68 |     QXmlNamespaceSupportPrivate *d; | 
| 69 |  | 
| 70 |     friend class QXmlSimpleReaderPrivate; | 
| 71 |     Q_DISABLE_COPY(QXmlNamespaceSupport) | 
| 72 | }; | 
| 73 |  | 
| 74 | // | 
| 75 | // SAX Attributes | 
| 76 | // | 
| 77 |  | 
| 78 | class Q_CORE5COMPAT_EXPORT QXmlAttributes | 
| 79 | { | 
| 80 | public: | 
| 81 |     QXmlAttributes(); | 
| 82 |     QXmlAttributes(const QXmlAttributes &) = default; | 
| 83 |     QXmlAttributes(QXmlAttributes &&) noexcept = default; | 
| 84 |     QXmlAttributes &operator=(const QXmlAttributes &) = default; | 
| 85 |     QXmlAttributes &operator=(QXmlAttributes &&) noexcept = default; | 
| 86 |  | 
| 87 |     virtual ~QXmlAttributes(); | 
| 88 |  | 
| 89 |     void swap(QXmlAttributes &other) noexcept | 
| 90 |     { | 
| 91 |         attList.swap(other&: other.attList); | 
| 92 |         qt_ptr_swap(lhs&: d, rhs&: other.d); | 
| 93 |     } | 
| 94 |  | 
| 95 |     int index(const QString& qName) const; | 
| 96 |     int index(QLatin1String qName) const; | 
| 97 |     int index(const QString& uri, const QString& localPart) const; | 
| 98 |     int length() const; | 
| 99 |     int count() const; | 
| 100 |     QString localName(int index) const; | 
| 101 |     QString qName(int index) const; | 
| 102 |     QString uri(int index) const; | 
| 103 |     QString type(int index) const; | 
| 104 |     QString type(const QString& qName) const; | 
| 105 |     QString type(const QString& uri, const QString& localName) const; | 
| 106 |     QString value(int index) const; | 
| 107 |     QString value(const QString& qName) const; | 
| 108 |     QString value(QLatin1String qName) const; | 
| 109 |     QString value(const QString& uri, const QString& localName) const; | 
| 110 |  | 
| 111 |     void clear(); | 
| 112 |     void append(const QString &qName, const QString &uri, const QString &localPart, const QString &value); | 
| 113 |  | 
| 114 | private: | 
| 115 |     struct Attribute { | 
| 116 |         QString qname, uri, localname, value; | 
| 117 |     }; | 
| 118 |     friend class QTypeInfo<Attribute>; | 
| 119 |     typedef QList<Attribute> AttributeList; | 
| 120 |     AttributeList attList; | 
| 121 |  | 
| 122 |     QXmlAttributesPrivate *d; | 
| 123 | }; | 
| 124 |  | 
| 125 | Q_DECLARE_TYPEINFO(QXmlAttributes::Attribute, Q_RELOCATABLE_TYPE); | 
| 126 | Q_DECLARE_SHARED(QXmlAttributes) | 
| 127 |  | 
| 128 | // | 
| 129 | // SAX Input Source | 
| 130 | // | 
| 131 |  | 
| 132 | class Q_CORE5COMPAT_EXPORT QXmlInputSource | 
| 133 | { | 
| 134 | public: | 
| 135 |     QXmlInputSource(); | 
| 136 |     explicit QXmlInputSource(QIODevice *dev); | 
| 137 |     virtual ~QXmlInputSource(); | 
| 138 |  | 
| 139 |     virtual void setData(const QString& dat); | 
| 140 |     virtual void setData(const QByteArray& dat); | 
| 141 |     virtual void fetchData(); | 
| 142 |     virtual QString data() const; | 
| 143 |     virtual QChar next(); | 
| 144 |     virtual void reset(); | 
| 145 |  | 
| 146 |     static const char16_t EndOfData; | 
| 147 |     static const char16_t EndOfDocument; | 
| 148 |  | 
| 149 | protected: | 
| 150 |     virtual QString fromRawData(const QByteArray &data, bool beginning = false); | 
| 151 |  | 
| 152 | private: | 
| 153 |     void init(); | 
| 154 |     QXmlInputSourcePrivate *d; | 
| 155 | }; | 
| 156 |  | 
| 157 | // | 
| 158 | // SAX Exception Classes | 
| 159 | // | 
| 160 |  | 
| 161 | class Q_CORE5COMPAT_EXPORT QXmlParseException | 
| 162 | { | 
| 163 | public: | 
| 164 |     explicit QXmlParseException(const QString &name = QString(), int c = -1, int l = -1, | 
| 165 |                                 const QString &p = QString(), const QString &s = QString()); | 
| 166 |     QXmlParseException(const QXmlParseException &other); | 
| 167 |     ~QXmlParseException(); | 
| 168 |  | 
| 169 |     int columnNumber() const; | 
| 170 |     int lineNumber() const; | 
| 171 |     QString publicId() const; | 
| 172 |     QString systemId() const; | 
| 173 |     QString message() const; | 
| 174 |  | 
| 175 | private: | 
| 176 |     std::unique_ptr<QXmlParseExceptionPrivate> d; | 
| 177 | }; | 
| 178 |  | 
| 179 | // | 
| 180 | // XML Reader | 
| 181 | // | 
| 182 |  | 
| 183 | class Q_CORE5COMPAT_EXPORT QXmlReader | 
| 184 | { | 
| 185 | public: | 
| 186 |     virtual ~QXmlReader(); | 
| 187 |     virtual bool feature(const QString& name, bool *ok = nullptr) const = 0; | 
| 188 |     virtual void setFeature(const QString& name, bool value) = 0; | 
| 189 |     virtual bool hasFeature(const QString& name) const = 0; | 
| 190 |     virtual void* property(const QString& name, bool *ok = nullptr) const = 0; | 
| 191 |     virtual void setProperty(const QString& name, void* value) = 0; | 
| 192 |     virtual bool hasProperty(const QString& name) const = 0; | 
| 193 |     virtual void setEntityResolver(QXmlEntityResolver* handler) = 0; | 
| 194 |     virtual QXmlEntityResolver* entityResolver() const = 0; | 
| 195 |     virtual void setDTDHandler(QXmlDTDHandler* handler) = 0; | 
| 196 |     virtual QXmlDTDHandler* DTDHandler() const = 0; | 
| 197 |     virtual void setContentHandler(QXmlContentHandler* handler) = 0; | 
| 198 |     virtual QXmlContentHandler* contentHandler() const = 0; | 
| 199 |     virtual void setErrorHandler(QXmlErrorHandler* handler) = 0; | 
| 200 |     virtual QXmlErrorHandler* errorHandler() const = 0; | 
| 201 |     virtual void setLexicalHandler(QXmlLexicalHandler* handler) = 0; | 
| 202 |     virtual QXmlLexicalHandler* lexicalHandler() const = 0; | 
| 203 |     virtual void setDeclHandler(QXmlDeclHandler* handler) = 0; | 
| 204 |     virtual QXmlDeclHandler* declHandler() const = 0; | 
| 205 |     virtual bool parse(const QXmlInputSource& input) = 0; | 
| 206 |     virtual bool parse(const QXmlInputSource* input) = 0; | 
| 207 | }; | 
| 208 |  | 
| 209 | class Q_CORE5COMPAT_EXPORT QXmlSimpleReader | 
| 210 |     : public QXmlReader | 
| 211 | { | 
| 212 | public: | 
| 213 |     QXmlSimpleReader(); | 
| 214 |     virtual ~QXmlSimpleReader(); | 
| 215 |  | 
| 216 |     bool feature(const QString& name, bool *ok = nullptr) const override; | 
| 217 |     void setFeature(const QString& name, bool value) override; | 
| 218 |     bool hasFeature(const QString& name) const override; | 
| 219 |  | 
| 220 |     void* property(const QString& name, bool *ok = nullptr) const override; | 
| 221 |     void setProperty(const QString& name, void* value) override; | 
| 222 |     bool hasProperty(const QString& name) const override; | 
| 223 |  | 
| 224 |     void setEntityResolver(QXmlEntityResolver* handler) override; | 
| 225 |     QXmlEntityResolver* entityResolver() const override; | 
| 226 |     void setDTDHandler(QXmlDTDHandler* handler) override; | 
| 227 |     QXmlDTDHandler* DTDHandler() const override; | 
| 228 |     void setContentHandler(QXmlContentHandler* handler) override; | 
| 229 |     QXmlContentHandler* contentHandler() const override; | 
| 230 |     void setErrorHandler(QXmlErrorHandler* handler) override; | 
| 231 |     QXmlErrorHandler* errorHandler() const override; | 
| 232 |     void setLexicalHandler(QXmlLexicalHandler* handler) override; | 
| 233 |     QXmlLexicalHandler* lexicalHandler() const override; | 
| 234 |     void setDeclHandler(QXmlDeclHandler* handler) override; | 
| 235 |     QXmlDeclHandler* declHandler() const override; | 
| 236 |  | 
| 237 |     bool parse(const QXmlInputSource& input) override; | 
| 238 |     bool parse(const QXmlInputSource* input) override; | 
| 239 |     virtual bool parse(const QXmlInputSource* input, bool incremental); | 
| 240 |     virtual bool parseContinue(); | 
| 241 |  | 
| 242 | private: | 
| 243 |     Q_DISABLE_COPY(QXmlSimpleReader) | 
| 244 |     Q_DECLARE_PRIVATE(QXmlSimpleReader) | 
| 245 |     std::unique_ptr<QXmlSimpleReaderPrivate> d_ptr; | 
| 246 |  | 
| 247 |     friend class QXmlSimpleReaderLocator; | 
| 248 | }; | 
| 249 |  | 
| 250 | // | 
| 251 | // SAX Locator | 
| 252 | // | 
| 253 |  | 
| 254 | class Q_CORE5COMPAT_EXPORT QXmlLocator | 
| 255 | { | 
| 256 | public: | 
| 257 |     QXmlLocator(); | 
| 258 |     virtual ~QXmlLocator(); | 
| 259 |  | 
| 260 |     virtual int columnNumber() const = 0; | 
| 261 |     virtual int lineNumber() const = 0; | 
| 262 | }; | 
| 263 |  | 
| 264 | // | 
| 265 | // SAX handler classes | 
| 266 | // | 
| 267 |  | 
| 268 | class Q_CORE5COMPAT_EXPORT QXmlContentHandler | 
| 269 | { | 
| 270 | public: | 
| 271 |     virtual ~QXmlContentHandler(); | 
| 272 |     virtual void setDocumentLocator(QXmlLocator* locator) = 0; | 
| 273 |     virtual bool startDocument() = 0; | 
| 274 |     virtual bool endDocument() = 0; | 
| 275 |     virtual bool startPrefixMapping(const QString& prefix, const QString& uri) = 0; | 
| 276 |     virtual bool endPrefixMapping(const QString& prefix) = 0; | 
| 277 |     virtual bool startElement(const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts) = 0; | 
| 278 |     virtual bool endElement(const QString& namespaceURI, const QString& localName, const QString& qName) = 0; | 
| 279 |     virtual bool characters(const QString& ch) = 0; | 
| 280 |     virtual bool ignorableWhitespace(const QString& ch) = 0; | 
| 281 |     virtual bool processingInstruction(const QString& target, const QString& data) = 0; | 
| 282 |     virtual bool skippedEntity(const QString& name) = 0; | 
| 283 |     virtual QString errorString() const = 0; | 
| 284 | }; | 
| 285 |  | 
| 286 | class Q_CORE5COMPAT_EXPORT QXmlErrorHandler | 
| 287 | { | 
| 288 | public: | 
| 289 |     virtual ~QXmlErrorHandler(); | 
| 290 |     virtual bool warning(const QXmlParseException& exception) = 0; | 
| 291 |     virtual bool error(const QXmlParseException& exception) = 0; | 
| 292 |     virtual bool fatalError(const QXmlParseException& exception) = 0; | 
| 293 |     virtual QString errorString() const = 0; | 
| 294 | }; | 
| 295 |  | 
| 296 | class Q_CORE5COMPAT_EXPORT QXmlDTDHandler | 
| 297 | { | 
| 298 | public: | 
| 299 |     virtual ~QXmlDTDHandler(); | 
| 300 |     virtual bool notationDecl(const QString& name, const QString& publicId, const QString& systemId) = 0; | 
| 301 |     virtual bool unparsedEntityDecl(const QString& name, const QString& publicId, const QString& systemId, const QString& notationName) = 0; | 
| 302 |     virtual QString errorString() const = 0; | 
| 303 | }; | 
| 304 |  | 
| 305 | class Q_CORE5COMPAT_EXPORT QXmlEntityResolver | 
| 306 | { | 
| 307 | public: | 
| 308 |     virtual ~QXmlEntityResolver(); | 
| 309 |     virtual bool resolveEntity(const QString& publicId, const QString& systemId, QXmlInputSource*& ret) = 0; | 
| 310 |     virtual QString errorString() const = 0; | 
| 311 | }; | 
| 312 |  | 
| 313 | class Q_CORE5COMPAT_EXPORT QXmlLexicalHandler | 
| 314 | { | 
| 315 | public: | 
| 316 |     virtual ~QXmlLexicalHandler(); | 
| 317 |     virtual bool startDTD(const QString& name, const QString& publicId, const QString& systemId) = 0; | 
| 318 |     virtual bool endDTD() = 0; | 
| 319 |     virtual bool startEntity(const QString& name) = 0; | 
| 320 |     virtual bool endEntity(const QString& name) = 0; | 
| 321 |     virtual bool startCDATA() = 0; | 
| 322 |     virtual bool endCDATA() = 0; | 
| 323 |     virtual bool comment(const QString& ch) = 0; | 
| 324 |     virtual QString errorString() const = 0; | 
| 325 | }; | 
| 326 |  | 
| 327 | class Q_CORE5COMPAT_EXPORT QXmlDeclHandler | 
| 328 | { | 
| 329 | public: | 
| 330 |     virtual ~QXmlDeclHandler(); | 
| 331 |     virtual bool attributeDecl(const QString& eName, const QString& aName, const QString& type, const QString& valueDefault, const QString& value) = 0; | 
| 332 |     virtual bool internalEntityDecl(const QString& name, const QString& value) = 0; | 
| 333 |     virtual bool externalEntityDecl(const QString& name, const QString& publicId, const QString& systemId) = 0; | 
| 334 |     virtual QString errorString() const = 0; | 
| 335 |     // ### Conform to SAX by adding elementDecl | 
| 336 | }; | 
| 337 |  | 
| 338 | class Q_CORE5COMPAT_EXPORT QXmlDefaultHandler : public QXmlContentHandler, | 
| 339 |                                                 public QXmlErrorHandler, | 
| 340 |                                                 public QXmlDTDHandler, | 
| 341 |                                                 public QXmlEntityResolver, | 
| 342 |                                                 public QXmlLexicalHandler, | 
| 343 |                                                 public QXmlDeclHandler | 
| 344 | { | 
| 345 | public: | 
| 346 |     QXmlDefaultHandler(); | 
| 347 |     virtual ~QXmlDefaultHandler(); | 
| 348 |  | 
| 349 |     void setDocumentLocator(QXmlLocator* locator) override; | 
| 350 |     bool startDocument() override; | 
| 351 |     bool endDocument() override; | 
| 352 |     bool startPrefixMapping(const QString& prefix, const QString& uri) override; | 
| 353 |     bool endPrefixMapping(const QString& prefix) override; | 
| 354 |     bool startElement(const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts) override; | 
| 355 |     bool endElement(const QString& namespaceURI, const QString& localName, const QString& qName) override; | 
| 356 |     bool characters(const QString& ch) override; | 
| 357 |     bool ignorableWhitespace(const QString& ch) override; | 
| 358 |     bool processingInstruction(const QString& target, const QString& data) override; | 
| 359 |     bool skippedEntity(const QString& name) override; | 
| 360 |  | 
| 361 |     bool warning(const QXmlParseException& exception) override; | 
| 362 |     bool error(const QXmlParseException& exception) override; | 
| 363 |     bool fatalError(const QXmlParseException& exception) override; | 
| 364 |  | 
| 365 |     bool notationDecl(const QString& name, const QString& publicId, const QString& systemId) override; | 
| 366 |     bool unparsedEntityDecl(const QString& name, const QString& publicId, const QString& systemId, const QString& notationName) override; | 
| 367 |  | 
| 368 |     bool resolveEntity(const QString& publicId, const QString& systemId, QXmlInputSource*& ret) override; | 
| 369 |  | 
| 370 |     bool startDTD(const QString& name, const QString& publicId, const QString& systemId) override; | 
| 371 |     bool endDTD() override; | 
| 372 |     bool startEntity(const QString& name) override; | 
| 373 |     bool endEntity(const QString& name) override; | 
| 374 |     bool startCDATA() override; | 
| 375 |     bool endCDATA() override; | 
| 376 |     bool comment(const QString& ch) override; | 
| 377 |  | 
| 378 |     bool attributeDecl(const QString& eName, const QString& aName, const QString& type, const QString& valueDefault, const QString& value) override; | 
| 379 |     bool internalEntityDecl(const QString& name, const QString& value) override; | 
| 380 |     bool externalEntityDecl(const QString& name, const QString& publicId, const QString& systemId) override; | 
| 381 |  | 
| 382 |     QString errorString() const override; | 
| 383 |  | 
| 384 | private: | 
| 385 |     QXmlDefaultHandlerPrivate *d; | 
| 386 |     Q_DISABLE_COPY(QXmlDefaultHandler) | 
| 387 | }; | 
| 388 |  | 
| 389 | // inlines | 
| 390 |  | 
| 391 | inline int QXmlAttributes::count() const | 
| 392 | { return length(); } | 
| 393 |  | 
| 394 | QT_END_NAMESPACE | 
| 395 |  | 
| 396 | #endif // QXML_H | 
| 397 |  |