qxmlstream.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QXMLSTREAM_H
00043 #define QXMLSTREAM_H
00044 
00045 #include <QtCore/qiodevice.h>
00046 
00047 #ifndef QT_NO_XMLSTREAM
00048 
00049 #include <QtCore/qstring.h>
00050 #include <QtCore/qvector.h>
00051 #include <QtCore/qscopedpointer.h>
00052 
00053 QT_BEGIN_HEADER
00054 
00055 QT_BEGIN_NAMESPACE
00056 
00057 QT_MODULE(Core)
00058 
00059 // QXmlStream* was originally in the QtXml module
00060 // since we've moved it to QtCore in Qt 4.4.0, we need to
00061 // keep binary compatibility
00062 //
00063 // The list of supported platforms is in:
00064 //   http://qt.nokia.com/doc/supported_platforms.html
00065 //
00066 // These platforms do not support symbol moving nor duplication
00067 // (because duplicate symbols cause warnings when linking):
00068 //   Apple MacOS X (Mach-O executable format)
00069 //       special case: 64-bit on Mac wasn't supported before 4.5.0
00070 //   IBM AIX (XCOFF executable format)
00071 //
00072 // These platforms do not support symbol moving but allow it to be duplicated:
00073 //   Microsoft Windows (COFF PE executable format)
00074 //      special case: Windows CE wasn't supported before 4.4.0
00075 //
00076 // These platforms support symbol moving:
00077 //   HP HP-UX (PA-RISC2.0 shared executables)
00078 //   HP HP-UXi (ELF executable format)
00079 //   FreeBSD (ELF executable format)
00080 //   Linux (ELF executable format)
00081 //   SGI IRIX (ELF executable format)
00082 //   Sun Solaris (ELF executable format)
00083 //
00084 // Other platforms are supported through community contributions only.
00085 // We are taking the optimist scenario here to avoid creating more
00086 // symbols to be supported.
00087 
00088 #if defined(Q_OS_MAC32) || defined(Q_OS_AIX)
00089 # if !defined QT_BUILD_XML_LIB
00090 #  define Q_XMLSTREAM_RENAME_SYMBOLS
00091 # endif
00092 #endif
00093 
00094 #if defined QT_BUILD_XML_LIB
00095 # define Q_XMLSTREAM_EXPORT     Q_XML_EXPORT
00096 #else
00097 # define Q_XMLSTREAM_EXPORT     Q_CORE_EXPORT
00098 #endif
00099 
00100 #if defined Q_XMLSTREAM_RENAME_SYMBOLS
00101 // don't worry, we'll undef and change to typedef at the bottom of the file
00102 # define QXmlStreamAttribute QCoreXmlStreamAttribute
00103 # define QXmlStreamAttributes QCoreXmlStreamAttributes
00104 # define QXmlStreamEntityDeclaration QCoreXmlStreamEntityDeclaration
00105 # define QXmlStreamEntityDeclarations QCoreXmlStreamEntityDeclarations
00106 # define QXmlStreamEntityResolver QCoreXmlStreamEntityResolver
00107 # define QXmlStreamNamespaceDeclaration QCoreXmlStreamNamespaceDeclaration
00108 # define QXmlStreamNamespaceDeclarations QCoreXmlStreamNamespaceDeclarations
00109 # define QXmlStreamNotationDeclaration QCoreXmlStreamNotationDeclaration
00110 # define QXmlStreamNotationDeclarations QCoreXmlStreamNotationDeclarations
00111 # define QXmlStreamReader QCoreXmlStreamReader
00112 # define QXmlStreamStringRef QCoreXmlStreamStringRef
00113 # define QXmlStreamWriter QCoreXmlStreamWriter
00114 #endif
00115 
00116 class Q_XMLSTREAM_EXPORT QXmlStreamStringRef {
00117     QString m_string;
00118     int m_position, m_size;
00119 public:
00120     inline QXmlStreamStringRef():m_position(0), m_size(0){}
00121     inline QXmlStreamStringRef(const QStringRef &aString)
00122         :m_string(aString.string()?*aString.string():QString()), m_position(aString.position()), m_size(aString.size()){}
00123     inline QXmlStreamStringRef(const QString &aString):m_string(aString), m_position(0), m_size(aString.size()){}
00124     inline ~QXmlStreamStringRef(){}
00125     inline void clear() { m_string.clear(); m_position = m_size = 0; }
00126     inline operator QStringRef() const { return QStringRef(&m_string, m_position, m_size); }
00127     inline const QString *string() const { return &m_string; }
00128     inline int position() const { return m_position; }
00129     inline int size() const { return m_size; }
00130 };
00131 
00132 
00133 class QXmlStreamReaderPrivate;
00134 class QXmlStreamAttributes;
00135 class Q_XMLSTREAM_EXPORT QXmlStreamAttribute {
00136     QXmlStreamStringRef m_name, m_namespaceUri, m_qualifiedName, m_value;
00137     void *reserved;
00138     uint m_isDefault : 1;
00139     friend class QXmlStreamReaderPrivate;
00140     friend class QXmlStreamAttributes;
00141 public:
00142     QXmlStreamAttribute();
00143     QXmlStreamAttribute(const QString &qualifiedName, const QString &value);
00144     QXmlStreamAttribute(const QString &namespaceUri, const QString &name, const QString &value);
00145     QXmlStreamAttribute(const QXmlStreamAttribute &);
00146     QXmlStreamAttribute& operator=(const QXmlStreamAttribute &);
00147     ~QXmlStreamAttribute();
00148     inline QStringRef namespaceUri() const { return m_namespaceUri; }
00149     inline QStringRef name() const { return m_name; }
00150     inline QStringRef qualifiedName() const { return m_qualifiedName; }
00151     inline QStringRef prefix() const {
00152         return QStringRef(m_qualifiedName.string(),
00153                           m_qualifiedName.position(),
00154                           qMax(0, m_qualifiedName.size() - m_name.size() - 1));
00155     }
00156     inline QStringRef value() const { return m_value; }
00157     inline bool isDefault() const { return m_isDefault; }
00158     inline bool operator==(const QXmlStreamAttribute &other) const {
00159         return (value() == other.value()
00160                 && (namespaceUri().isNull() ? (qualifiedName() == other.qualifiedName())
00161                     : (namespaceUri() == other.namespaceUri() && name() == other.name())));
00162     }
00163     inline bool operator!=(const QXmlStreamAttribute &other) const
00164         { return !operator==(other); }
00165 };
00166 
00167 Q_DECLARE_TYPEINFO(QXmlStreamAttribute, Q_MOVABLE_TYPE);
00168 
00169 class Q_XMLSTREAM_EXPORT QXmlStreamAttributes : public QVector<QXmlStreamAttribute>
00170 {
00171 public:
00172     QStringRef value(const QString &namespaceUri, const QString &name) const;
00173     QStringRef value(const QString &namespaceUri, const QLatin1String &name) const;
00174     QStringRef value(const QLatin1String &namespaceUri, const QLatin1String &name) const;
00175     QStringRef value(const QString &qualifiedName) const;
00176     QStringRef value(const QLatin1String &qualifiedName) const;
00177     void append(const QString &namespaceUri, const QString &name, const QString &value);
00178     void append(const QString &qualifiedName, const QString &value);
00179 
00180     inline bool hasAttribute(const QString &qualifiedName) const
00181     {
00182         return !value(qualifiedName).isNull();
00183     }
00184 
00185     inline bool hasAttribute(const QLatin1String &qualifiedName) const
00186     {
00187         return !value(qualifiedName).isNull();
00188     }
00189 
00190     inline bool hasAttribute(const QString &namespaceUri, const QString &name) const
00191     {
00192         return !value(namespaceUri, name).isNull();
00193     }
00194 
00195 #if !defined(Q_NO_USING_KEYWORD)
00196     using QVector<QXmlStreamAttribute>::append;
00197 #else
00198     inline void append(const QXmlStreamAttribute &attribute)
00199         { QVector<QXmlStreamAttribute>::append(attribute); }
00200 #endif
00201 };
00202 
00203 class Q_XMLSTREAM_EXPORT QXmlStreamNamespaceDeclaration {
00204     QXmlStreamStringRef m_prefix, m_namespaceUri;
00205     void *reserved;
00206 
00207     friend class QXmlStreamReaderPrivate;
00208 public:
00209     QXmlStreamNamespaceDeclaration();
00210     QXmlStreamNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &);
00211     QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri);
00212     ~QXmlStreamNamespaceDeclaration();
00213     QXmlStreamNamespaceDeclaration& operator=(const QXmlStreamNamespaceDeclaration &);
00214     inline QStringRef prefix() const { return m_prefix; }
00215     inline QStringRef namespaceUri() const { return m_namespaceUri; }
00216     inline bool operator==(const QXmlStreamNamespaceDeclaration &other) const {
00217         return (prefix() == other.prefix() && namespaceUri() == other.namespaceUri());
00218     }
00219     inline bool operator!=(const QXmlStreamNamespaceDeclaration &other) const
00220         { return !operator==(other); }
00221 };
00222 
00223 Q_DECLARE_TYPEINFO(QXmlStreamNamespaceDeclaration, Q_MOVABLE_TYPE);
00224 typedef QVector<QXmlStreamNamespaceDeclaration> QXmlStreamNamespaceDeclarations;
00225 
00226 class Q_XMLSTREAM_EXPORT QXmlStreamNotationDeclaration {
00227     QXmlStreamStringRef m_name, m_systemId, m_publicId;
00228     void *reserved;
00229 
00230     friend class QXmlStreamReaderPrivate;
00231 public:
00232     QXmlStreamNotationDeclaration();
00233     ~QXmlStreamNotationDeclaration();
00234     QXmlStreamNotationDeclaration(const QXmlStreamNotationDeclaration &);
00235     QXmlStreamNotationDeclaration& operator=(const QXmlStreamNotationDeclaration &);
00236     inline QStringRef name() const { return m_name; }
00237     inline QStringRef systemId() const { return m_systemId; }
00238     inline QStringRef publicId() const { return m_publicId; }
00239     inline bool operator==(const QXmlStreamNotationDeclaration &other) const {
00240         return (name() == other.name() && systemId() == other.systemId()
00241                 && publicId() == other.publicId());
00242     }
00243     inline bool operator!=(const QXmlStreamNotationDeclaration &other) const
00244         { return !operator==(other); }
00245 };
00246 
00247 Q_DECLARE_TYPEINFO(QXmlStreamNotationDeclaration, Q_MOVABLE_TYPE);
00248 typedef QVector<QXmlStreamNotationDeclaration> QXmlStreamNotationDeclarations;
00249 
00250 class Q_XMLSTREAM_EXPORT QXmlStreamEntityDeclaration {
00251     QXmlStreamStringRef m_name, m_notationName, m_systemId, m_publicId, m_value;
00252     void *reserved;
00253 
00254     friend class QXmlStreamReaderPrivate;
00255 public:
00256     QXmlStreamEntityDeclaration();
00257     ~QXmlStreamEntityDeclaration();
00258     QXmlStreamEntityDeclaration(const QXmlStreamEntityDeclaration &);
00259     QXmlStreamEntityDeclaration& operator=(const QXmlStreamEntityDeclaration &);
00260     inline QStringRef name() const { return m_name; }
00261     inline QStringRef notationName() const { return m_notationName; }
00262     inline QStringRef systemId() const { return m_systemId; }
00263     inline QStringRef publicId() const { return m_publicId; }
00264     inline QStringRef value() const { return m_value; }
00265     inline bool operator==(const QXmlStreamEntityDeclaration &other) const {
00266         return (name() == other.name()
00267                 && notationName() == other.notationName()
00268                 && systemId() == other.systemId()
00269                 && publicId() == other.publicId()
00270                 && value() == other.value());
00271     }
00272     inline bool operator!=(const QXmlStreamEntityDeclaration &other) const
00273         { return !operator==(other); }
00274 };
00275 
00276 Q_DECLARE_TYPEINFO(QXmlStreamEntityDeclaration, Q_MOVABLE_TYPE);
00277 typedef QVector<QXmlStreamEntityDeclaration> QXmlStreamEntityDeclarations;
00278 
00279 
00280 class Q_XMLSTREAM_EXPORT QXmlStreamEntityResolver
00281 {
00282 public:
00283     virtual ~QXmlStreamEntityResolver();
00284     virtual QString resolveEntity(const QString& publicId, const QString& systemId);
00285     virtual QString resolveUndeclaredEntity(const QString &name);
00286 };
00287 
00288 #ifndef QT_NO_XMLSTREAMREADER
00289 class Q_XMLSTREAM_EXPORT QXmlStreamReader {
00290     QDOC_PROPERTY(bool namespaceProcessing READ namespaceProcessing WRITE setNamespaceProcessing)
00291 public:
00292     enum TokenType {
00293         NoToken = 0,
00294         Invalid,
00295         StartDocument,
00296         EndDocument,
00297         StartElement,
00298         EndElement,
00299         Characters,
00300         Comment,
00301         DTD,
00302         EntityReference,
00303         ProcessingInstruction
00304     };
00305 
00306 
00307     QXmlStreamReader();
00308     QXmlStreamReader(QIODevice *device);
00309     QXmlStreamReader(const QByteArray &data);
00310     QXmlStreamReader(const QString &data);
00311     QXmlStreamReader(const char * data);
00312     ~QXmlStreamReader();
00313 
00314     void setDevice(QIODevice *device);
00315     QIODevice *device() const;
00316     void addData(const QByteArray &data);
00317     void addData(const QString &data);
00318     void addData(const char *data);
00319     void clear();
00320 
00321 
00322     bool atEnd() const;
00323     TokenType readNext();
00324 
00325     bool readNextStartElement();
00326     void skipCurrentElement();
00327 
00328     TokenType tokenType() const;
00329     QString tokenString() const;
00330 
00331     void setNamespaceProcessing(bool);
00332     bool namespaceProcessing() const;
00333 
00334     inline bool isStartDocument() const { return tokenType() == StartDocument; }
00335     inline bool isEndDocument() const { return tokenType() == EndDocument; }
00336     inline bool isStartElement() const { return tokenType() == StartElement; }
00337     inline bool isEndElement() const { return tokenType() == EndElement; }
00338     inline bool isCharacters() const { return tokenType() == Characters; }
00339     bool isWhitespace() const;
00340     bool isCDATA() const;
00341     inline bool isComment() const { return tokenType() == Comment; }
00342     inline bool isDTD() const { return tokenType() == DTD; }
00343     inline bool isEntityReference() const { return tokenType() == EntityReference; }
00344     inline bool isProcessingInstruction() const { return tokenType() == ProcessingInstruction; }
00345 
00346     bool isStandaloneDocument() const;
00347     QStringRef documentVersion() const;
00348     QStringRef documentEncoding() const;
00349 
00350     qint64 lineNumber() const;
00351     qint64 columnNumber() const;
00352     qint64 characterOffset() const;
00353 
00354     QXmlStreamAttributes attributes() const;
00355 
00356     enum ReadElementTextBehaviour {
00357         ErrorOnUnexpectedElement,
00358         IncludeChildElements,
00359         SkipChildElements
00360     };
00361     QString readElementText(ReadElementTextBehaviour behaviour);
00362     QString readElementText();
00363 
00364     QStringRef name() const;
00365     QStringRef namespaceUri() const;
00366     QStringRef qualifiedName() const;
00367     QStringRef prefix() const;
00368 
00369     QStringRef processingInstructionTarget() const;
00370     QStringRef processingInstructionData() const;
00371 
00372     QStringRef text() const;
00373 
00374     QXmlStreamNamespaceDeclarations namespaceDeclarations() const;
00375     void addExtraNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &extraNamespaceDeclaraction);
00376     void addExtraNamespaceDeclarations(const QXmlStreamNamespaceDeclarations &extraNamespaceDeclaractions);
00377     QXmlStreamNotationDeclarations notationDeclarations() const;
00378     QXmlStreamEntityDeclarations entityDeclarations() const;
00379     QStringRef dtdName() const;
00380     QStringRef dtdPublicId() const;
00381     QStringRef dtdSystemId() const;
00382 
00383 
00384     enum Error {
00385         NoError,
00386         UnexpectedElementError,
00387         CustomError,
00388         NotWellFormedError,
00389         PrematureEndOfDocumentError
00390     };
00391     void raiseError(const QString& message = QString());
00392     QString errorString() const;
00393     Error error() const;
00394 
00395     inline bool hasError() const
00396     {
00397         return error() != NoError;
00398     }
00399 
00400     void setEntityResolver(QXmlStreamEntityResolver *resolver);
00401     QXmlStreamEntityResolver *entityResolver() const;
00402 
00403 private:
00404     Q_DISABLE_COPY(QXmlStreamReader)
00405     Q_DECLARE_PRIVATE(QXmlStreamReader)
00406     QScopedPointer<QXmlStreamReaderPrivate> d_ptr;
00407 
00408 };
00409 #endif // QT_NO_XMLSTREAMREADER
00410 
00411 #ifndef QT_NO_XMLSTREAMWRITER
00412 
00413 class QXmlStreamWriterPrivate;
00414 
00415 class Q_XMLSTREAM_EXPORT QXmlStreamWriter
00416 {
00417     QDOC_PROPERTY(bool autoFormatting READ autoFormatting WRITE setAutoFormatting)
00418     QDOC_PROPERTY(int autoFormattingIndent READ autoFormattingIndent WRITE setAutoFormattingIndent)
00419 public:
00420     QXmlStreamWriter();
00421     QXmlStreamWriter(QIODevice *device);
00422     QXmlStreamWriter(QByteArray *array);
00423     QXmlStreamWriter(QString *string);
00424     ~QXmlStreamWriter();
00425 
00426     void setDevice(QIODevice *device);
00427     QIODevice *device() const;
00428 
00429 #ifndef QT_NO_TEXTCODEC
00430     void setCodec(QTextCodec *codec);
00431     void setCodec(const char *codecName);
00432     QTextCodec *codec() const;
00433 #endif
00434 
00435     void setAutoFormatting(bool);
00436     bool autoFormatting() const;
00437 
00438     void setAutoFormattingIndent(int spacesOrTabs);
00439     int autoFormattingIndent() const;
00440 
00441     void writeAttribute(const QString &qualifiedName, const QString &value);
00442     void writeAttribute(const QString &namespaceUri, const QString &name, const QString &value);
00443     void writeAttribute(const QXmlStreamAttribute& attribute);
00444     void writeAttributes(const QXmlStreamAttributes& attributes);
00445 
00446     void writeCDATA(const QString &text);
00447     void writeCharacters(const QString &text);
00448     void writeComment(const QString &text);
00449 
00450     void writeDTD(const QString &dtd);
00451 
00452     void writeEmptyElement(const QString &qualifiedName);
00453     void writeEmptyElement(const QString &namespaceUri, const QString &name);
00454 
00455     void writeTextElement(const QString &qualifiedName, const QString &text);
00456     void writeTextElement(const QString &namespaceUri, const QString &name, const QString &text);
00457 
00458     void writeEndDocument();
00459     void writeEndElement();
00460 
00461     void writeEntityReference(const QString &name);
00462     void writeNamespace(const QString &namespaceUri, const QString &prefix = QString());
00463     void writeDefaultNamespace(const QString &namespaceUri);
00464     void writeProcessingInstruction(const QString &target, const QString &data = QString());
00465 
00466     void writeStartDocument();
00467     void writeStartDocument(const QString &version);
00468     void writeStartDocument(const QString &version, bool standalone);
00469     void writeStartElement(const QString &qualifiedName);
00470     void writeStartElement(const QString &namespaceUri, const QString &name);
00471 
00472 #ifndef QT_NO_XMLSTREAMREADER
00473     void writeCurrentToken(const QXmlStreamReader &reader);
00474 #endif
00475 
00476 private:
00477     Q_DISABLE_COPY(QXmlStreamWriter)
00478     Q_DECLARE_PRIVATE(QXmlStreamWriter)
00479     QScopedPointer<QXmlStreamWriterPrivate> d_ptr;
00480 };
00481 #endif // QT_NO_XMLSTREAMWRITER
00482 
00483 QT_END_NAMESPACE
00484 
00485 QT_END_HEADER
00486 
00487 #endif // QT_NO_XMLSTREAM
00488 #endif // QXMLSTREAM_H