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

source code of qt5compat/src/core5/sax/qxml.h