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

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