1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtCore 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 Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QXMLSTREAM_H
43#define QXMLSTREAM_H
44
45#include <QtCore/qiodevice.h>
46
47#ifndef QT_NO_XMLSTREAM
48
49#include <QtCore/qstring.h>
50#include <QtCore/qvector.h>
51#include <QtCore/qscopedpointer.h>
52
53QT_BEGIN_HEADER
54
55QT_BEGIN_NAMESPACE
56
57QT_MODULE(Core)
58
59// QXmlStream* was originally in the QtXml module
60// since we've moved it to QtCore in Qt 4.4.0, we need to
61// keep binary compatibility
62//
63// The list of supported platforms is in:
64// http://qt.nokia.com/doc/supported_platforms.html
65//
66// These platforms do not support symbol moving nor duplication
67// (because duplicate symbols cause warnings when linking):
68// Apple MacOS X (Mach-O executable format)
69// special case: 64-bit on Mac wasn't supported before 4.5.0
70// IBM AIX (XCOFF executable format)
71//
72// These platforms do not support symbol moving but allow it to be duplicated:
73// Microsoft Windows (COFF PE executable format)
74// special case: Windows CE wasn't supported before 4.4.0
75//
76// These platforms support symbol moving:
77// HP HP-UX (PA-RISC2.0 shared executables)
78// HP HP-UXi (ELF executable format)
79// FreeBSD (ELF executable format)
80// Linux (ELF executable format)
81// SGI IRIX (ELF executable format)
82// Sun Solaris (ELF executable format)
83//
84// Other platforms are supported through community contributions only.
85// We are taking the optimist scenario here to avoid creating more
86// symbols to be supported.
87
88#if defined(Q_OS_MAC32) || defined(Q_OS_AIX)
89# if !defined QT_BUILD_XML_LIB
90# define Q_XMLSTREAM_RENAME_SYMBOLS
91# endif
92#endif
93
94#if defined QT_BUILD_XML_LIB
95# define Q_XMLSTREAM_EXPORT Q_XML_EXPORT
96#else
97# define Q_XMLSTREAM_EXPORT Q_CORE_EXPORT
98#endif
99
100#if defined Q_XMLSTREAM_RENAME_SYMBOLS
101// don't worry, we'll undef and change to typedef at the bottom of the file
102# define QXmlStreamAttribute QCoreXmlStreamAttribute
103# define QXmlStreamAttributes QCoreXmlStreamAttributes
104# define QXmlStreamEntityDeclaration QCoreXmlStreamEntityDeclaration
105# define QXmlStreamEntityDeclarations QCoreXmlStreamEntityDeclarations
106# define QXmlStreamEntityResolver QCoreXmlStreamEntityResolver
107# define QXmlStreamNamespaceDeclaration QCoreXmlStreamNamespaceDeclaration
108# define QXmlStreamNamespaceDeclarations QCoreXmlStreamNamespaceDeclarations
109# define QXmlStreamNotationDeclaration QCoreXmlStreamNotationDeclaration
110# define QXmlStreamNotationDeclarations QCoreXmlStreamNotationDeclarations
111# define QXmlStreamReader QCoreXmlStreamReader
112# define QXmlStreamStringRef QCoreXmlStreamStringRef
113# define QXmlStreamWriter QCoreXmlStreamWriter
114#endif
115
116class Q_XMLSTREAM_EXPORT QXmlStreamStringRef {
117 QString m_string;
118 int m_position, m_size;
119public:
120 inline QXmlStreamStringRef():m_position(0), m_size(0){}
121 inline QXmlStreamStringRef(const QStringRef &aString)
122 :m_string(aString.string()?*aString.string():QString()), m_position(aString.position()), m_size(aString.size()){}
123 inline QXmlStreamStringRef(const QString &aString):m_string(aString), m_position(0), m_size(aString.size()){}
124 inline ~QXmlStreamStringRef(){}
125 inline void clear() { m_string.clear(); m_position = m_size = 0; }
126 inline operator QStringRef() const { return QStringRef(&m_string, m_position, m_size); }
127 inline const QString *string() const { return &m_string; }
128 inline int position() const { return m_position; }
129 inline int size() const { return m_size; }
130};
131
132
133class QXmlStreamReaderPrivate;
134class QXmlStreamAttributes;
135class Q_XMLSTREAM_EXPORT QXmlStreamAttribute {
136 QXmlStreamStringRef m_name, m_namespaceUri, m_qualifiedName, m_value;
137 void *reserved;
138 uint m_isDefault : 1;
139 friend class QXmlStreamReaderPrivate;
140 friend class QXmlStreamAttributes;
141public:
142 QXmlStreamAttribute();
143 QXmlStreamAttribute(const QString &qualifiedName, const QString &value);
144 QXmlStreamAttribute(const QString &namespaceUri, const QString &name, const QString &value);
145 QXmlStreamAttribute(const QXmlStreamAttribute &);
146 QXmlStreamAttribute& operator=(const QXmlStreamAttribute &);
147 ~QXmlStreamAttribute();
148 inline QStringRef namespaceUri() const { return m_namespaceUri; }
149 inline QStringRef name() const { return m_name; }
150 inline QStringRef qualifiedName() const { return m_qualifiedName; }
151 inline QStringRef prefix() const {
152 return QStringRef(m_qualifiedName.string(),
153 m_qualifiedName.position(),
154 qMax(0, m_qualifiedName.size() - m_name.size() - 1));
155 }
156 inline QStringRef value() const { return m_value; }
157 inline bool isDefault() const { return m_isDefault; }
158 inline bool operator==(const QXmlStreamAttribute &other) const {
159 return (value() == other.value()
160 && (namespaceUri().isNull() ? (qualifiedName() == other.qualifiedName())
161 : (namespaceUri() == other.namespaceUri() && name() == other.name())));
162 }
163 inline bool operator!=(const QXmlStreamAttribute &other) const
164 { return !operator==(other); }
165};
166
167Q_DECLARE_TYPEINFO(QXmlStreamAttribute, Q_MOVABLE_TYPE);
168
169class Q_XMLSTREAM_EXPORT QXmlStreamAttributes : public QVector<QXmlStreamAttribute>
170{
171public:
172 inline QXmlStreamAttributes() {}
173 QStringRef value(const QString &namespaceUri, const QString &name) const;
174 QStringRef value(const QString &namespaceUri, const QLatin1String &name) const;
175 QStringRef value(const QLatin1String &namespaceUri, const QLatin1String &name) const;
176 QStringRef value(const QString &qualifiedName) const;
177 QStringRef value(const QLatin1String &qualifiedName) const;
178 void append(const QString &namespaceUri, const QString &name, const QString &value);
179 void append(const QString &qualifiedName, const QString &value);
180
181 inline bool hasAttribute(const QString &qualifiedName) const
182 {
183 return !value(qualifiedName).isNull();
184 }
185
186 inline bool hasAttribute(const QLatin1String &qualifiedName) const
187 {
188 return !value(qualifiedName).isNull();
189 }
190
191 inline bool hasAttribute(const QString &namespaceUri, const QString &name) const
192 {
193 return !value(namespaceUri, name).isNull();
194 }
195
196#if !defined(Q_NO_USING_KEYWORD)
197 using QVector<QXmlStreamAttribute>::append;
198#else
199 inline void append(const QXmlStreamAttribute &attribute)
200 { QVector<QXmlStreamAttribute>::append(attribute); }
201#endif
202};
203
204class Q_XMLSTREAM_EXPORT QXmlStreamNamespaceDeclaration {
205 QXmlStreamStringRef m_prefix, m_namespaceUri;
206 void *reserved;
207
208 friend class QXmlStreamReaderPrivate;
209public:
210 QXmlStreamNamespaceDeclaration();
211 QXmlStreamNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &);
212 QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri);
213 ~QXmlStreamNamespaceDeclaration();
214 QXmlStreamNamespaceDeclaration& operator=(const QXmlStreamNamespaceDeclaration &);
215 inline QStringRef prefix() const { return m_prefix; }
216 inline QStringRef namespaceUri() const { return m_namespaceUri; }
217 inline bool operator==(const QXmlStreamNamespaceDeclaration &other) const {
218 return (prefix() == other.prefix() && namespaceUri() == other.namespaceUri());
219 }
220 inline bool operator!=(const QXmlStreamNamespaceDeclaration &other) const
221 { return !operator==(other); }
222};
223
224Q_DECLARE_TYPEINFO(QXmlStreamNamespaceDeclaration, Q_MOVABLE_TYPE);
225typedef QVector<QXmlStreamNamespaceDeclaration> QXmlStreamNamespaceDeclarations;
226
227class Q_XMLSTREAM_EXPORT QXmlStreamNotationDeclaration {
228 QXmlStreamStringRef m_name, m_systemId, m_publicId;
229 void *reserved;
230
231 friend class QXmlStreamReaderPrivate;
232public:
233 QXmlStreamNotationDeclaration();
234 ~QXmlStreamNotationDeclaration();
235 QXmlStreamNotationDeclaration(const QXmlStreamNotationDeclaration &);
236 QXmlStreamNotationDeclaration& operator=(const QXmlStreamNotationDeclaration &);
237 inline QStringRef name() const { return m_name; }
238 inline QStringRef systemId() const { return m_systemId; }
239 inline QStringRef publicId() const { return m_publicId; }
240 inline bool operator==(const QXmlStreamNotationDeclaration &other) const {
241 return (name() == other.name() && systemId() == other.systemId()
242 && publicId() == other.publicId());
243 }
244 inline bool operator!=(const QXmlStreamNotationDeclaration &other) const
245 { return !operator==(other); }
246};
247
248Q_DECLARE_TYPEINFO(QXmlStreamNotationDeclaration, Q_MOVABLE_TYPE);
249typedef QVector<QXmlStreamNotationDeclaration> QXmlStreamNotationDeclarations;
250
251class Q_XMLSTREAM_EXPORT QXmlStreamEntityDeclaration {
252 QXmlStreamStringRef m_name, m_notationName, m_systemId, m_publicId, m_value;
253 void *reserved;
254
255 friend class QXmlStreamReaderPrivate;
256public:
257 QXmlStreamEntityDeclaration();
258 ~QXmlStreamEntityDeclaration();
259 QXmlStreamEntityDeclaration(const QXmlStreamEntityDeclaration &);
260 QXmlStreamEntityDeclaration& operator=(const QXmlStreamEntityDeclaration &);
261 inline QStringRef name() const { return m_name; }
262 inline QStringRef notationName() const { return m_notationName; }
263 inline QStringRef systemId() const { return m_systemId; }
264 inline QStringRef publicId() const { return m_publicId; }
265 inline QStringRef value() const { return m_value; }
266 inline bool operator==(const QXmlStreamEntityDeclaration &other) const {
267 return (name() == other.name()
268 && notationName() == other.notationName()
269 && systemId() == other.systemId()
270 && publicId() == other.publicId()
271 && value() == other.value());
272 }
273 inline bool operator!=(const QXmlStreamEntityDeclaration &other) const
274 { return !operator==(other); }
275};
276
277Q_DECLARE_TYPEINFO(QXmlStreamEntityDeclaration, Q_MOVABLE_TYPE);
278typedef QVector<QXmlStreamEntityDeclaration> QXmlStreamEntityDeclarations;
279
280
281class Q_XMLSTREAM_EXPORT QXmlStreamEntityResolver
282{
283public:
284 virtual ~QXmlStreamEntityResolver();
285 virtual QString resolveEntity(const QString& publicId, const QString& systemId);
286 virtual QString resolveUndeclaredEntity(const QString &name);
287};
288
289#ifndef QT_NO_XMLSTREAMREADER
290class Q_XMLSTREAM_EXPORT QXmlStreamReader {
291 QDOC_PROPERTY(bool namespaceProcessing READ namespaceProcessing WRITE setNamespaceProcessing)
292public:
293 enum TokenType {
294 NoToken = 0,
295 Invalid,
296 StartDocument,
297 EndDocument,
298 StartElement,
299 EndElement,
300 Characters,
301 Comment,
302 DTD,
303 EntityReference,
304 ProcessingInstruction
305 };
306
307
308 QXmlStreamReader();
309 QXmlStreamReader(QIODevice *device);
310 QXmlStreamReader(const QByteArray &data);
311 QXmlStreamReader(const QString &data);
312 QXmlStreamReader(const char * data);
313 ~QXmlStreamReader();
314
315 void setDevice(QIODevice *device);
316 QIODevice *device() const;
317 void addData(const QByteArray &data);
318 void addData(const QString &data);
319 void addData(const char *data);
320 void clear();
321
322
323 bool atEnd() const;
324 TokenType readNext();
325
326 bool readNextStartElement();
327 void skipCurrentElement();
328
329 TokenType tokenType() const;
330 QString tokenString() const;
331
332 void setNamespaceProcessing(bool);
333 bool namespaceProcessing() const;
334
335 inline bool isStartDocument() const { return tokenType() == StartDocument; }
336 inline bool isEndDocument() const { return tokenType() == EndDocument; }
337 inline bool isStartElement() const { return tokenType() == StartElement; }
338 inline bool isEndElement() const { return tokenType() == EndElement; }
339 inline bool isCharacters() const { return tokenType() == Characters; }
340 bool isWhitespace() const;
341 bool isCDATA() const;
342 inline bool isComment() const { return tokenType() == Comment; }
343 inline bool isDTD() const { return tokenType() == DTD; }
344 inline bool isEntityReference() const { return tokenType() == EntityReference; }
345 inline bool isProcessingInstruction() const { return tokenType() == ProcessingInstruction; }
346
347 bool isStandaloneDocument() const;
348 QStringRef documentVersion() const;
349 QStringRef documentEncoding() const;
350
351 qint64 lineNumber() const;
352 qint64 columnNumber() const;
353 qint64 characterOffset() const;
354
355 QXmlStreamAttributes attributes() const;
356
357 enum ReadElementTextBehaviour {
358 ErrorOnUnexpectedElement,
359 IncludeChildElements,
360 SkipChildElements
361 };
362 QString readElementText(ReadElementTextBehaviour behaviour);
363 QString readElementText();
364
365 QStringRef name() const;
366 QStringRef namespaceUri() const;
367 QStringRef qualifiedName() const;
368 QStringRef prefix() const;
369
370 QStringRef processingInstructionTarget() const;
371 QStringRef processingInstructionData() const;
372
373 QStringRef text() const;
374
375 QXmlStreamNamespaceDeclarations namespaceDeclarations() const;
376 void addExtraNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &extraNamespaceDeclaraction);
377 void addExtraNamespaceDeclarations(const QXmlStreamNamespaceDeclarations &extraNamespaceDeclaractions);
378 QXmlStreamNotationDeclarations notationDeclarations() const;
379 QXmlStreamEntityDeclarations entityDeclarations() const;
380 QStringRef dtdName() const;
381 QStringRef dtdPublicId() const;
382 QStringRef dtdSystemId() const;
383
384
385 enum Error {
386 NoError,
387 UnexpectedElementError,
388 CustomError,
389 NotWellFormedError,
390 PrematureEndOfDocumentError
391 };
392 void raiseError(const QString& message = QString());
393 QString errorString() const;
394 Error error() const;
395
396 inline bool hasError() const
397 {
398 return error() != NoError;
399 }
400
401 void setEntityResolver(QXmlStreamEntityResolver *resolver);
402 QXmlStreamEntityResolver *entityResolver() const;
403
404private:
405 Q_DISABLE_COPY(QXmlStreamReader)
406 Q_DECLARE_PRIVATE(QXmlStreamReader)
407 QScopedPointer<QXmlStreamReaderPrivate> d_ptr;
408
409};
410#endif // QT_NO_XMLSTREAMREADER
411
412#ifndef QT_NO_XMLSTREAMWRITER
413
414class QXmlStreamWriterPrivate;
415
416class Q_XMLSTREAM_EXPORT QXmlStreamWriter
417{
418 QDOC_PROPERTY(bool autoFormatting READ autoFormatting WRITE setAutoFormatting)
419 QDOC_PROPERTY(int autoFormattingIndent READ autoFormattingIndent WRITE setAutoFormattingIndent)
420public:
421 QXmlStreamWriter();
422 QXmlStreamWriter(QIODevice *device);
423 QXmlStreamWriter(QByteArray *array);
424 QXmlStreamWriter(QString *string);
425 ~QXmlStreamWriter();
426
427 void setDevice(QIODevice *device);
428 QIODevice *device() const;
429
430#ifndef QT_NO_TEXTCODEC
431 void setCodec(QTextCodec *codec);
432 void setCodec(const char *codecName);
433 QTextCodec *codec() const;
434#endif
435
436 void setAutoFormatting(bool);
437 bool autoFormatting() const;
438
439 void setAutoFormattingIndent(int spacesOrTabs);
440 int autoFormattingIndent() const;
441
442 void writeAttribute(const QString &qualifiedName, const QString &value);
443 void writeAttribute(const QString &namespaceUri, const QString &name, const QString &value);
444 void writeAttribute(const QXmlStreamAttribute& attribute);
445 void writeAttributes(const QXmlStreamAttributes& attributes);
446
447 void writeCDATA(const QString &text);
448 void writeCharacters(const QString &text);
449 void writeComment(const QString &text);
450
451 void writeDTD(const QString &dtd);
452
453 void writeEmptyElement(const QString &qualifiedName);
454 void writeEmptyElement(const QString &namespaceUri, const QString &name);
455
456 void writeTextElement(const QString &qualifiedName, const QString &text);
457 void writeTextElement(const QString &namespaceUri, const QString &name, const QString &text);
458
459 void writeEndDocument();
460 void writeEndElement();
461
462 void writeEntityReference(const QString &name);
463 void writeNamespace(const QString &namespaceUri, const QString &prefix = QString());
464 void writeDefaultNamespace(const QString &namespaceUri);
465 void writeProcessingInstruction(const QString &target, const QString &data = QString());
466
467 void writeStartDocument();
468 void writeStartDocument(const QString &version);
469 void writeStartDocument(const QString &version, bool standalone);
470 void writeStartElement(const QString &qualifiedName);
471 void writeStartElement(const QString &namespaceUri, const QString &name);
472
473#ifndef QT_NO_XMLSTREAMREADER
474 void writeCurrentToken(const QXmlStreamReader &reader);
475#endif
476
477 bool hasError() const;
478
479private:
480 Q_DISABLE_COPY(QXmlStreamWriter)
481 Q_DECLARE_PRIVATE(QXmlStreamWriter)
482 QScopedPointer<QXmlStreamWriterPrivate> d_ptr;
483};
484#endif // QT_NO_XMLSTREAMWRITER
485
486QT_END_NAMESPACE
487
488QT_END_HEADER
489
490#endif // QT_NO_XMLSTREAM
491#endif // QXMLSTREAM_H
492