[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtXml module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at qt-info@nokia.com.
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #ifndef QXML_H
|
---|
| 43 | #define QXML_H
|
---|
| 44 |
|
---|
| 45 | #include <QtCore/qtextstream.h>
|
---|
| 46 | #include <QtCore/qfile.h>
|
---|
| 47 | #include <QtCore/qstring.h>
|
---|
| 48 | #include <QtCore/qstringlist.h>
|
---|
| 49 | #include <QtCore/qlist.h>
|
---|
[561] | 50 | #include <QtCore/qscopedpointer.h>
|
---|
[2] | 51 |
|
---|
| 52 | QT_BEGIN_HEADER
|
---|
| 53 |
|
---|
| 54 | QT_BEGIN_NAMESPACE
|
---|
| 55 |
|
---|
| 56 | QT_MODULE(Xml)
|
---|
| 57 |
|
---|
| 58 | class QXmlNamespaceSupport;
|
---|
| 59 | class QXmlAttributes;
|
---|
| 60 | class QXmlContentHandler;
|
---|
| 61 | class QXmlDefaultHandler;
|
---|
| 62 | class QXmlDTDHandler;
|
---|
| 63 | class QXmlEntityResolver;
|
---|
| 64 | class QXmlErrorHandler;
|
---|
| 65 | class QXmlLexicalHandler;
|
---|
| 66 | class QXmlDeclHandler;
|
---|
| 67 | class QXmlInputSource;
|
---|
| 68 | class QXmlLocator;
|
---|
| 69 | class QXmlNamespaceSupport;
|
---|
| 70 | class QXmlParseException;
|
---|
| 71 |
|
---|
| 72 | class QXmlReader;
|
---|
| 73 | class QXmlSimpleReader;
|
---|
| 74 |
|
---|
| 75 | class QXmlSimpleReaderPrivate;
|
---|
| 76 | class QXmlNamespaceSupportPrivate;
|
---|
| 77 | class QXmlAttributesPrivate;
|
---|
| 78 | class QXmlInputSourcePrivate;
|
---|
| 79 | class QXmlParseExceptionPrivate;
|
---|
| 80 | class QXmlLocatorPrivate;
|
---|
| 81 | class QXmlDefaultHandlerPrivate;
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | //
|
---|
| 85 | // SAX Namespace Support
|
---|
| 86 | //
|
---|
| 87 |
|
---|
| 88 | class Q_XML_EXPORT QXmlNamespaceSupport
|
---|
| 89 | {
|
---|
| 90 | public:
|
---|
| 91 | QXmlNamespaceSupport();
|
---|
| 92 | ~QXmlNamespaceSupport();
|
---|
| 93 |
|
---|
| 94 | void setPrefix(const QString&, const QString&);
|
---|
| 95 |
|
---|
| 96 | QString prefix(const QString&) const;
|
---|
| 97 | QString uri(const QString&) const;
|
---|
| 98 | void splitName(const QString&, QString&, QString&) const;
|
---|
| 99 | void processName(const QString&, bool, QString&, QString&) const;
|
---|
| 100 | QStringList prefixes() const;
|
---|
| 101 | QStringList prefixes(const QString&) const;
|
---|
| 102 |
|
---|
| 103 | void pushContext();
|
---|
| 104 | void popContext();
|
---|
| 105 | void reset();
|
---|
| 106 |
|
---|
| 107 | private:
|
---|
| 108 | QXmlNamespaceSupportPrivate *d;
|
---|
| 109 |
|
---|
| 110 | friend class QXmlSimpleReaderPrivate;
|
---|
| 111 | Q_DISABLE_COPY(QXmlNamespaceSupport)
|
---|
| 112 | };
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | //
|
---|
| 116 | // SAX Attributes
|
---|
| 117 | //
|
---|
| 118 |
|
---|
| 119 | class Q_XML_EXPORT QXmlAttributes
|
---|
| 120 | {
|
---|
| 121 | public:
|
---|
| 122 | QXmlAttributes() {}
|
---|
| 123 | virtual ~QXmlAttributes() {}
|
---|
| 124 |
|
---|
| 125 | int index(const QString& qName) const;
|
---|
| 126 | int index(const QLatin1String& qName) const;
|
---|
| 127 | int index(const QString& uri, const QString& localPart) const;
|
---|
| 128 | int length() const;
|
---|
| 129 | int count() const;
|
---|
| 130 | QString localName(int index) const;
|
---|
| 131 | QString qName(int index) const;
|
---|
| 132 | QString uri(int index) const;
|
---|
| 133 | QString type(int index) const;
|
---|
| 134 | QString type(const QString& qName) const;
|
---|
| 135 | QString type(const QString& uri, const QString& localName) const;
|
---|
| 136 | QString value(int index) const;
|
---|
| 137 | QString value(const QString& qName) const;
|
---|
| 138 | QString value(const QLatin1String& qName) const;
|
---|
| 139 | QString value(const QString& uri, const QString& localName) const;
|
---|
| 140 |
|
---|
| 141 | void clear();
|
---|
| 142 | void append(const QString &qName, const QString &uri, const QString &localPart, const QString &value);
|
---|
| 143 |
|
---|
| 144 | private:
|
---|
| 145 | struct Attribute {
|
---|
| 146 | QString qname, uri, localname, value;
|
---|
| 147 | };
|
---|
| 148 | typedef QList<Attribute> AttributeList;
|
---|
| 149 | AttributeList attList;
|
---|
| 150 |
|
---|
| 151 | QXmlAttributesPrivate *d;
|
---|
| 152 | };
|
---|
| 153 |
|
---|
| 154 | //
|
---|
| 155 | // SAX Input Source
|
---|
| 156 | //
|
---|
| 157 |
|
---|
| 158 | class Q_XML_EXPORT QXmlInputSource
|
---|
| 159 | {
|
---|
| 160 | public:
|
---|
| 161 | QXmlInputSource();
|
---|
| 162 | QXmlInputSource(QIODevice *dev);
|
---|
| 163 | virtual ~QXmlInputSource();
|
---|
| 164 |
|
---|
| 165 | virtual void setData(const QString& dat);
|
---|
| 166 | virtual void setData(const QByteArray& dat);
|
---|
| 167 | virtual void fetchData();
|
---|
| 168 | virtual QString data() const;
|
---|
| 169 | virtual QChar next();
|
---|
| 170 | virtual void reset();
|
---|
| 171 |
|
---|
| 172 | static const ushort EndOfData;
|
---|
| 173 | static const ushort EndOfDocument;
|
---|
| 174 |
|
---|
| 175 | #ifdef QT3_SUPPORT
|
---|
| 176 | QT3_SUPPORT_CONSTRUCTOR QXmlInputSource(QFile& file);
|
---|
| 177 | QT3_SUPPORT_CONSTRUCTOR QXmlInputSource(QTextStream& stream);
|
---|
| 178 | #endif
|
---|
| 179 |
|
---|
| 180 | protected:
|
---|
| 181 | virtual QString fromRawData(const QByteArray &data, bool beginning = false);
|
---|
| 182 |
|
---|
| 183 | private:
|
---|
| 184 | void init();
|
---|
| 185 | QXmlInputSourcePrivate *d;
|
---|
| 186 | };
|
---|
| 187 |
|
---|
| 188 | //
|
---|
| 189 | // SAX Exception Classes
|
---|
| 190 | //
|
---|
| 191 |
|
---|
| 192 | class Q_XML_EXPORT QXmlParseException
|
---|
| 193 | {
|
---|
| 194 | public:
|
---|
| 195 | explicit QXmlParseException(const QString &name = QString(), int c = -1, int l = -1,
|
---|
| 196 | const QString &p = QString(), const QString &s = QString());
|
---|
[561] | 197 | QXmlParseException(const QXmlParseException &other);
|
---|
[2] | 198 | ~QXmlParseException();
|
---|
| 199 |
|
---|
| 200 | int columnNumber() const;
|
---|
| 201 | int lineNumber() const;
|
---|
| 202 | QString publicId() const;
|
---|
| 203 | QString systemId() const;
|
---|
| 204 | QString message() const;
|
---|
| 205 |
|
---|
| 206 | private:
|
---|
[561] | 207 | QScopedPointer<QXmlParseExceptionPrivate> d;
|
---|
[2] | 208 | };
|
---|
| 209 |
|
---|
| 210 |
|
---|
| 211 | //
|
---|
| 212 | // XML Reader
|
---|
| 213 | //
|
---|
| 214 |
|
---|
| 215 | class Q_XML_EXPORT QXmlReader
|
---|
| 216 | {
|
---|
| 217 | public:
|
---|
| 218 | virtual ~QXmlReader() {}
|
---|
| 219 | virtual bool feature(const QString& name, bool *ok = 0) const = 0;
|
---|
| 220 | virtual void setFeature(const QString& name, bool value) = 0;
|
---|
| 221 | virtual bool hasFeature(const QString& name) const = 0;
|
---|
| 222 | virtual void* property(const QString& name, bool *ok = 0) const = 0;
|
---|
| 223 | virtual void setProperty(const QString& name, void* value) = 0;
|
---|
| 224 | virtual bool hasProperty(const QString& name) const = 0;
|
---|
| 225 | virtual void setEntityResolver(QXmlEntityResolver* handler) = 0;
|
---|
| 226 | virtual QXmlEntityResolver* entityResolver() const = 0;
|
---|
| 227 | virtual void setDTDHandler(QXmlDTDHandler* handler) = 0;
|
---|
| 228 | virtual QXmlDTDHandler* DTDHandler() const = 0;
|
---|
| 229 | virtual void setContentHandler(QXmlContentHandler* handler) = 0;
|
---|
| 230 | virtual QXmlContentHandler* contentHandler() const = 0;
|
---|
| 231 | virtual void setErrorHandler(QXmlErrorHandler* handler) = 0;
|
---|
| 232 | virtual QXmlErrorHandler* errorHandler() const = 0;
|
---|
| 233 | virtual void setLexicalHandler(QXmlLexicalHandler* handler) = 0;
|
---|
| 234 | virtual QXmlLexicalHandler* lexicalHandler() const = 0;
|
---|
| 235 | virtual void setDeclHandler(QXmlDeclHandler* handler) = 0;
|
---|
| 236 | virtual QXmlDeclHandler* declHandler() const = 0;
|
---|
| 237 | virtual bool parse(const QXmlInputSource& input) = 0;
|
---|
| 238 | virtual bool parse(const QXmlInputSource* input) = 0;
|
---|
| 239 | };
|
---|
| 240 |
|
---|
| 241 | class Q_XML_EXPORT QXmlSimpleReader : public QXmlReader
|
---|
| 242 | {
|
---|
| 243 | public:
|
---|
| 244 | QXmlSimpleReader();
|
---|
| 245 | virtual ~QXmlSimpleReader();
|
---|
| 246 |
|
---|
| 247 | bool feature(const QString& name, bool *ok = 0) const;
|
---|
| 248 | void setFeature(const QString& name, bool value);
|
---|
| 249 | bool hasFeature(const QString& name) const;
|
---|
| 250 |
|
---|
| 251 | void* property(const QString& name, bool *ok = 0) const;
|
---|
| 252 | void setProperty(const QString& name, void* value);
|
---|
| 253 | bool hasProperty(const QString& name) const;
|
---|
| 254 |
|
---|
| 255 | void setEntityResolver(QXmlEntityResolver* handler);
|
---|
| 256 | QXmlEntityResolver* entityResolver() const;
|
---|
| 257 | void setDTDHandler(QXmlDTDHandler* handler);
|
---|
| 258 | QXmlDTDHandler* DTDHandler() const;
|
---|
| 259 | void setContentHandler(QXmlContentHandler* handler);
|
---|
| 260 | QXmlContentHandler* contentHandler() const;
|
---|
| 261 | void setErrorHandler(QXmlErrorHandler* handler);
|
---|
| 262 | QXmlErrorHandler* errorHandler() const;
|
---|
| 263 | void setLexicalHandler(QXmlLexicalHandler* handler);
|
---|
| 264 | QXmlLexicalHandler* lexicalHandler() const;
|
---|
| 265 | void setDeclHandler(QXmlDeclHandler* handler);
|
---|
| 266 | QXmlDeclHandler* declHandler() const;
|
---|
| 267 |
|
---|
| 268 | bool parse(const QXmlInputSource& input);
|
---|
| 269 | bool parse(const QXmlInputSource* input);
|
---|
| 270 | virtual bool parse(const QXmlInputSource* input, bool incremental);
|
---|
| 271 | virtual bool parseContinue();
|
---|
| 272 |
|
---|
| 273 | private:
|
---|
| 274 | Q_DISABLE_COPY(QXmlSimpleReader)
|
---|
| 275 | Q_DECLARE_PRIVATE(QXmlSimpleReader)
|
---|
[561] | 276 | QScopedPointer<QXmlSimpleReaderPrivate> d_ptr;
|
---|
[2] | 277 |
|
---|
| 278 | friend class QXmlSimpleReaderLocator;
|
---|
| 279 | };
|
---|
| 280 |
|
---|
| 281 | //
|
---|
| 282 | // SAX Locator
|
---|
| 283 | //
|
---|
| 284 |
|
---|
| 285 | class Q_XML_EXPORT QXmlLocator
|
---|
| 286 | {
|
---|
| 287 | public:
|
---|
| 288 | QXmlLocator();
|
---|
| 289 | virtual ~QXmlLocator();
|
---|
| 290 |
|
---|
| 291 | virtual int columnNumber() const = 0;
|
---|
| 292 | virtual int lineNumber() const = 0;
|
---|
| 293 | // QString getPublicId() const
|
---|
| 294 | // QString getSystemId() const
|
---|
| 295 | };
|
---|
| 296 |
|
---|
| 297 | //
|
---|
| 298 | // SAX handler classes
|
---|
| 299 | //
|
---|
| 300 |
|
---|
| 301 | class Q_XML_EXPORT QXmlContentHandler
|
---|
| 302 | {
|
---|
| 303 | public:
|
---|
| 304 | virtual ~QXmlContentHandler() {}
|
---|
| 305 | virtual void setDocumentLocator(QXmlLocator* locator) = 0;
|
---|
| 306 | virtual bool startDocument() = 0;
|
---|
| 307 | virtual bool endDocument() = 0;
|
---|
| 308 | virtual bool startPrefixMapping(const QString& prefix, const QString& uri) = 0;
|
---|
| 309 | virtual bool endPrefixMapping(const QString& prefix) = 0;
|
---|
| 310 | virtual bool startElement(const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts) = 0;
|
---|
| 311 | virtual bool endElement(const QString& namespaceURI, const QString& localName, const QString& qName) = 0;
|
---|
| 312 | virtual bool characters(const QString& ch) = 0;
|
---|
| 313 | virtual bool ignorableWhitespace(const QString& ch) = 0;
|
---|
| 314 | virtual bool processingInstruction(const QString& target, const QString& data) = 0;
|
---|
| 315 | virtual bool skippedEntity(const QString& name) = 0;
|
---|
| 316 | virtual QString errorString() const = 0;
|
---|
| 317 | };
|
---|
| 318 |
|
---|
| 319 | class Q_XML_EXPORT QXmlErrorHandler
|
---|
| 320 | {
|
---|
| 321 | public:
|
---|
| 322 | virtual ~QXmlErrorHandler() {}
|
---|
| 323 | virtual bool warning(const QXmlParseException& exception) = 0;
|
---|
| 324 | virtual bool error(const QXmlParseException& exception) = 0;
|
---|
| 325 | virtual bool fatalError(const QXmlParseException& exception) = 0;
|
---|
| 326 | virtual QString errorString() const = 0;
|
---|
| 327 | };
|
---|
| 328 |
|
---|
| 329 | class Q_XML_EXPORT QXmlDTDHandler
|
---|
| 330 | {
|
---|
| 331 | public:
|
---|
| 332 | virtual ~QXmlDTDHandler() {}
|
---|
| 333 | virtual bool notationDecl(const QString& name, const QString& publicId, const QString& systemId) = 0;
|
---|
| 334 | virtual bool unparsedEntityDecl(const QString& name, const QString& publicId, const QString& systemId, const QString& notationName) = 0;
|
---|
| 335 | virtual QString errorString() const = 0;
|
---|
| 336 | };
|
---|
| 337 |
|
---|
| 338 | class Q_XML_EXPORT QXmlEntityResolver
|
---|
| 339 | {
|
---|
| 340 | public:
|
---|
| 341 | virtual ~QXmlEntityResolver() {}
|
---|
| 342 | virtual bool resolveEntity(const QString& publicId, const QString& systemId, QXmlInputSource*& ret) = 0;
|
---|
| 343 | virtual QString errorString() const = 0;
|
---|
| 344 | };
|
---|
| 345 |
|
---|
| 346 | class Q_XML_EXPORT QXmlLexicalHandler
|
---|
| 347 | {
|
---|
| 348 | public:
|
---|
| 349 | virtual ~QXmlLexicalHandler() {}
|
---|
| 350 | virtual bool startDTD(const QString& name, const QString& publicId, const QString& systemId) = 0;
|
---|
| 351 | virtual bool endDTD() = 0;
|
---|
| 352 | virtual bool startEntity(const QString& name) = 0;
|
---|
| 353 | virtual bool endEntity(const QString& name) = 0;
|
---|
| 354 | virtual bool startCDATA() = 0;
|
---|
| 355 | virtual bool endCDATA() = 0;
|
---|
| 356 | virtual bool comment(const QString& ch) = 0;
|
---|
| 357 | virtual QString errorString() const = 0;
|
---|
| 358 | };
|
---|
| 359 |
|
---|
| 360 | class Q_XML_EXPORT QXmlDeclHandler
|
---|
| 361 | {
|
---|
| 362 | public:
|
---|
| 363 | virtual ~QXmlDeclHandler() {}
|
---|
| 364 | virtual bool attributeDecl(const QString& eName, const QString& aName, const QString& type, const QString& valueDefault, const QString& value) = 0;
|
---|
| 365 | virtual bool internalEntityDecl(const QString& name, const QString& value) = 0;
|
---|
| 366 | virtual bool externalEntityDecl(const QString& name, const QString& publicId, const QString& systemId) = 0;
|
---|
| 367 | virtual QString errorString() const = 0;
|
---|
| 368 | // ### Qt 5: Conform to SAX by adding elementDecl
|
---|
| 369 | };
|
---|
| 370 |
|
---|
| 371 |
|
---|
| 372 | class Q_XML_EXPORT QXmlDefaultHandler : public QXmlContentHandler, public QXmlErrorHandler, public QXmlDTDHandler, public QXmlEntityResolver, public QXmlLexicalHandler, public QXmlDeclHandler
|
---|
| 373 | {
|
---|
| 374 | public:
|
---|
| 375 | QXmlDefaultHandler() { }
|
---|
| 376 | virtual ~QXmlDefaultHandler() { }
|
---|
| 377 |
|
---|
| 378 | void setDocumentLocator(QXmlLocator* locator);
|
---|
| 379 | bool startDocument();
|
---|
| 380 | bool endDocument();
|
---|
| 381 | bool startPrefixMapping(const QString& prefix, const QString& uri);
|
---|
| 382 | bool endPrefixMapping(const QString& prefix);
|
---|
| 383 | bool startElement(const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts);
|
---|
| 384 | bool endElement(const QString& namespaceURI, const QString& localName, const QString& qName);
|
---|
| 385 | bool characters(const QString& ch);
|
---|
| 386 | bool ignorableWhitespace(const QString& ch);
|
---|
| 387 | bool processingInstruction(const QString& target, const QString& data);
|
---|
| 388 | bool skippedEntity(const QString& name);
|
---|
| 389 |
|
---|
| 390 | bool warning(const QXmlParseException& exception);
|
---|
| 391 | bool error(const QXmlParseException& exception);
|
---|
| 392 | bool fatalError(const QXmlParseException& exception);
|
---|
| 393 |
|
---|
| 394 | bool notationDecl(const QString& name, const QString& publicId, const QString& systemId);
|
---|
| 395 | bool unparsedEntityDecl(const QString& name, const QString& publicId, const QString& systemId, const QString& notationName);
|
---|
| 396 |
|
---|
| 397 | bool resolveEntity(const QString& publicId, const QString& systemId, QXmlInputSource*& ret);
|
---|
| 398 |
|
---|
| 399 | bool startDTD(const QString& name, const QString& publicId, const QString& systemId);
|
---|
| 400 | bool endDTD();
|
---|
| 401 | bool startEntity(const QString& name);
|
---|
| 402 | bool endEntity(const QString& name);
|
---|
| 403 | bool startCDATA();
|
---|
| 404 | bool endCDATA();
|
---|
| 405 | bool comment(const QString& ch);
|
---|
| 406 |
|
---|
| 407 | bool attributeDecl(const QString& eName, const QString& aName, const QString& type, const QString& valueDefault, const QString& value);
|
---|
| 408 | bool internalEntityDecl(const QString& name, const QString& value);
|
---|
| 409 | bool externalEntityDecl(const QString& name, const QString& publicId, const QString& systemId);
|
---|
| 410 |
|
---|
| 411 | QString errorString() const;
|
---|
| 412 |
|
---|
| 413 | private:
|
---|
| 414 | QXmlDefaultHandlerPrivate *d;
|
---|
| 415 | Q_DISABLE_COPY(QXmlDefaultHandler)
|
---|
| 416 | };
|
---|
| 417 |
|
---|
| 418 | // inlines
|
---|
| 419 |
|
---|
| 420 | inline int QXmlAttributes::count() const
|
---|
| 421 | { return length(); }
|
---|
| 422 |
|
---|
| 423 | QT_END_NAMESPACE
|
---|
| 424 |
|
---|
| 425 | QT_END_HEADER
|
---|
| 426 |
|
---|
| 427 | #endif // QXML_H
|
---|