1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtXml 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 #ifndef QDOMHELPERS_P_H
40 #define QDOMHELPERS_P_H
41 
42 #include <qcoreapplication.h>
43 #include <qglobal.h>
44 #include <qxml.h>
45 
46 QT_BEGIN_NAMESPACE
47 
48 //
49 //  W A R N I N G
50 //  -------------
51 //
52 // This file is not part of the Qt API. It exists for the convenience of
53 // qxml.cpp and qdom.cpp. This header file may change from version to version without
54 // notice, or even be removed.
55 //
56 // We mean it.
57 //
58 
59 class QDomDocumentPrivate;
60 class QDomNodePrivate;
61 class QXmlStreamReader;
62 class QXmlStreamAttributes;
63 
64 /**************************************************************
65  *
66  * QXmlDocumentLocators
67  *
68  **************************************************************/
69 
70 /* TODO: QXmlDocumentLocator can be removed when the SAX-based
71  * implementation is removed. Right now it is needed for QDomBuilder
72  * to work with both QXmlStreamReader and QXmlInputSource (SAX)
73  * based implementations.
74  */
75 class QXmlDocumentLocator
76 {
77 public:
78     virtual ~QXmlDocumentLocator() = default;
79     virtual int column() const = 0;
80     virtual int line() const = 0;
81 };
82 
83 class QDomDocumentLocator : public QXmlDocumentLocator
84 {
85 public:
QDomDocumentLocator(QXmlStreamReader * r)86     QDomDocumentLocator(QXmlStreamReader *r) : reader(r) {}
87     ~QDomDocumentLocator() override = default;
88 
89     int column() const override;
90     int line() const override;
91 
92 private:
93     QXmlStreamReader *reader;
94 };
95 
96 #if QT_DEPRECATED_SINCE(5, 15)
97 
98 QT_WARNING_PUSH
99 QT_WARNING_DISABLE_DEPRECATED
100 
101 class QSAXDocumentLocator : public QXmlDocumentLocator
102 {
103 public:
104     ~QSAXDocumentLocator() override = default;
105 
106     int column() const override;
107     int line() const override;
108 
109     void setLocator(QXmlLocator *l);
110 
111 private:
112     QXmlLocator *locator = nullptr;
113 };
114 
115 QT_WARNING_POP
116 
117 #endif
118 
119 /**************************************************************
120  *
121  * QDomBuilder
122  *
123  **************************************************************/
124 
125 class QDomBuilder
126 {
127 public:
128     QDomBuilder(QDomDocumentPrivate *d, QXmlDocumentLocator *l, bool namespaceProcessing);
129     ~QDomBuilder();
130 
131     bool endDocument();
132 #if QT_DEPRECATED_SINCE(5, 15)
133 QT_WARNING_PUSH
134 QT_WARNING_DISABLE_DEPRECATED
135     bool startElement(const QString &nsURI, const QString &qName, const QXmlAttributes &atts);
136 QT_WARNING_POP
137 #endif
138     bool startElement(const QString &nsURI, const QString &qName, const QXmlStreamAttributes &atts);
139     bool endElement();
140     bool characters(const QString &characters, bool cdata = false);
141     bool processingInstruction(const QString &target, const QString &data);
142     bool skippedEntity(const QString &name);
143     bool startEntity(const QString &name);
144     bool endEntity();
145     bool startDTD(const QString &name, const QString &publicId, const QString &systemId);
146     bool comment(const QString &characters);
147     bool externalEntityDecl(const QString &name, const QString &publicId, const QString &systemId);
148     bool notationDecl(const QString &name, const QString &publicId, const QString &systemId);
149     bool unparsedEntityDecl(const QString &name, const QString &publicId, const QString &systemId,
150                             const QString &notationName);
151 
152     void fatalError(const QString &message);
153 
154     using ErrorInfo = std::tuple<QString, int, int>;
155     ErrorInfo error() const;
156 
157     QString errorMsg;
158     int errorLine;
159     int errorColumn;
160 
161 private:
162     QDomDocumentPrivate *doc;
163     QDomNodePrivate *node;
164     QXmlDocumentLocator *locator;
165     QString entityName;
166     bool nsProcessing;
167 };
168 
169 #if QT_DEPRECATED_SINCE(5, 15)
170 
171 /**************************************************************
172  *
173  * QDomHandler
174  *
175  **************************************************************/
176 
177 QT_WARNING_PUSH
178 QT_WARNING_DISABLE_DEPRECATED
179 
180 class QDomHandler : public QXmlDefaultHandler
181 {
182 public:
183     QDomHandler(QDomDocumentPrivate *d, QXmlSimpleReader *reader, bool namespaceProcessing);
184     ~QDomHandler() override;
185 
186     // content handler
187     bool endDocument() override;
188     bool startElement(const QString &nsURI, const QString &localName, const QString &qName,
189                       const QXmlAttributes &atts) override;
190     bool endElement(const QString &nsURI, const QString &localName, const QString &qName) override;
191     bool characters(const QString &ch) override;
192     bool processingInstruction(const QString &target, const QString &data) override;
193     bool skippedEntity(const QString &name) override;
194 
195     // error handler
196     bool fatalError(const QXmlParseException &exception) override;
197 
198     // lexical handler
199     bool startCDATA() override;
200     bool endCDATA() override;
201     bool startEntity(const QString &) override;
202     bool endEntity(const QString &) override;
203     bool startDTD(const QString &name, const QString &publicId, const QString &systemId) override;
204     bool comment(const QString &ch) override;
205 
206     // decl handler
207     bool externalEntityDecl(const QString &name, const QString &publicId,
208                             const QString &systemId) override;
209 
210     // DTD handler
211     bool notationDecl(const QString &name, const QString &publicId,
212                       const QString &systemId) override;
213     bool unparsedEntityDecl(const QString &name, const QString &publicId, const QString &systemId,
214                             const QString &notationName) override;
215 
216     void setDocumentLocator(QXmlLocator *locator) override;
217 
218     QDomBuilder::ErrorInfo errorInfo() const;
219 
220 private:
221     bool cdata;
222     QXmlSimpleReader *reader;
223     QSAXDocumentLocator locator;
224     QDomBuilder domBuilder;
225 };
226 
227 QT_WARNING_POP
228 
229 #endif // QT_DEPRECATED_SINCE(5, 15)
230 
231 /**************************************************************
232  *
233  * QDomParser
234  *
235  **************************************************************/
236 
237 class QDomParser
238 {
239     Q_DECLARE_TR_FUNCTIONS(QDomParser)
240 public:
241     QDomParser(QDomDocumentPrivate *d, QXmlStreamReader *r, bool namespaceProcessing);
242 
243     bool parse();
244     QDomBuilder::ErrorInfo errorInfo() const;
245 
246 private:
247     bool parseProlog();
248     bool parseBody();
249     bool parseMarkupDecl();
250 
251     QXmlStreamReader *reader;
252     QDomDocumentLocator locator;
253     QDomBuilder domBuilder;
254 };
255 
256 QT_END_NAMESPACE
257 
258 #endif // QDOMHELPERS_P_H
259