| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QXMLSERIALIZER_H |
| 41 | #define QXMLSERIALIZER_H |
| 42 | |
| 43 | #include <QtXmlPatterns/QAbstractXmlReceiver> |
| 44 | |
| 45 | QT_BEGIN_NAMESPACE |
| 46 | |
| 47 | |
| 48 | class QIODevice; |
| 49 | class QTextCodec; |
| 50 | class QXmlQuery; |
| 51 | class QXmlSerializerPrivate; |
| 52 | |
| 53 | class Q_XMLPATTERNS_EXPORT QXmlSerializer : public QAbstractXmlReceiver |
| 54 | { |
| 55 | public: |
| 56 | QXmlSerializer(const QXmlQuery &query, |
| 57 | QIODevice *outputDevice); |
| 58 | |
| 59 | void namespaceBinding(const QXmlName &nb) override; |
| 60 | |
| 61 | void characters(const QStringRef &value) override; |
| 62 | void (const QString &value) override; |
| 63 | |
| 64 | void startElement(const QXmlName &name) override; |
| 65 | |
| 66 | void endElement() override; |
| 67 | |
| 68 | void attribute(const QXmlName &name, |
| 69 | const QStringRef &value) override; |
| 70 | |
| 71 | void processingInstruction(const QXmlName &name, |
| 72 | const QString &value) override; |
| 73 | |
| 74 | void atomicValue(const QVariant &value) override; |
| 75 | |
| 76 | void startDocument() override; |
| 77 | void endDocument() override; |
| 78 | void startOfSequence() override; |
| 79 | void endOfSequence() override; |
| 80 | |
| 81 | QIODevice *outputDevice() const; |
| 82 | |
| 83 | void setCodec(const QTextCodec *codec); |
| 84 | const QTextCodec *codec() const; |
| 85 | |
| 86 | /* The members below are internal, not part of the public API, and |
| 87 | * unsupported. Using them leads to undefined behavior. */ |
| 88 | void item(const QPatternist::Item &item) override; |
| 89 | protected: |
| 90 | QXmlSerializer(QAbstractXmlReceiverPrivate *d); |
| 91 | |
| 92 | private: |
| 93 | inline bool isBindingInScope(const QXmlName nb) const; |
| 94 | |
| 95 | /** |
| 96 | * Where in the document the QXmlSerializer is currently working. |
| 97 | */ |
| 98 | enum State |
| 99 | { |
| 100 | /** |
| 101 | * Before the document element. This is the XML prolog where the |
| 102 | * XML declaration, and possibly comments and processing |
| 103 | * instructions are found. |
| 104 | */ |
| 105 | BeforeDocumentElement, |
| 106 | |
| 107 | /** |
| 108 | * This is inside the document element, at any level. |
| 109 | */ |
| 110 | InsideDocumentElement |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * If the current state is neither BeforeDocumentElement or |
| 115 | * AfterDocumentElement. |
| 116 | */ |
| 117 | inline bool atDocumentRoot() const; |
| 118 | |
| 119 | /** |
| 120 | * Closes any open element start tag. Must be called before outputting |
| 121 | * any element content. |
| 122 | */ |
| 123 | inline void startContent(); |
| 124 | |
| 125 | /** |
| 126 | * Escapes content intended as text nodes for elements. |
| 127 | */ |
| 128 | void writeEscaped(const QString &toEscape); |
| 129 | |
| 130 | /** |
| 131 | * Identical to writeEscaped(), but also escapes quotes. |
| 132 | */ |
| 133 | inline void writeEscapedAttribute(const QString &toEscape); |
| 134 | |
| 135 | /** |
| 136 | * Writes out @p name. |
| 137 | */ |
| 138 | inline void write(const QXmlName &name); |
| 139 | |
| 140 | inline void write(const char *const chars); |
| 141 | /** |
| 142 | * Encodes and writes out @p content. |
| 143 | */ |
| 144 | inline void write(const QString &content); |
| 145 | |
| 146 | Q_DECLARE_PRIVATE(QXmlSerializer) |
| 147 | }; |
| 148 | |
| 149 | QT_END_NAMESPACE |
| 150 | |
| 151 | #endif |
| 152 | |