source: trunk/src/corelib/xml/qxmlstream.h@ 1023

Last change on this file since 1023 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 18.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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 QStringRef value(const QString &namespaceUri, const QString &name) const;
173 QStringRef value(const QString &namespaceUri, const QLatin1String &name) const;
174 QStringRef value(const QLatin1String &namespaceUri, const QLatin1String &name) const;
175 QStringRef value(const QString &qualifiedName) const;
176 QStringRef value(const QLatin1String &qualifiedName) const;
177 void append(const QString &namespaceUri, const QString &name, const QString &value);
178 void append(const QString &qualifiedName, const QString &value);
179
180 inline bool hasAttribute(const QString &qualifiedName) const
181 {
182 return !value(qualifiedName).isNull();
183 }
184
185 inline bool hasAttribute(const QLatin1String &qualifiedName) const
186 {
187 return !value(qualifiedName).isNull();
188 }
189
190 inline bool hasAttribute(const QString &namespaceUri, const QString &name) const
191 {
192 return !value(namespaceUri, name).isNull();
193 }
194
195#if !defined(Q_NO_USING_KEYWORD)
196 using QVector<QXmlStreamAttribute>::append;
197#else
198 inline void append(const QXmlStreamAttribute &attribute)
199 { QVector<QXmlStreamAttribute>::append(attribute); }
200#endif
201};
202
203class Q_XMLSTREAM_EXPORT QXmlStreamNamespaceDeclaration {
204 QXmlStreamStringRef m_prefix, m_namespaceUri;
205 void *reserved;
206
207 friend class QXmlStreamReaderPrivate;
208public:
209 QXmlStreamNamespaceDeclaration();
210 QXmlStreamNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &);
211 QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri);
212 ~QXmlStreamNamespaceDeclaration();
213 QXmlStreamNamespaceDeclaration& operator=(const QXmlStreamNamespaceDeclaration &);
214 inline QStringRef prefix() const { return m_prefix; }
215 inline QStringRef namespaceUri() const { return m_namespaceUri; }
216 inline bool operator==(const QXmlStreamNamespaceDeclaration &other) const {
217 return (prefix() == other.prefix() && namespaceUri() == other.namespaceUri());
218 }
219 inline bool operator!=(const QXmlStreamNamespaceDeclaration &other) const
220 { return !operator==(other); }
221};
222
223Q_DECLARE_TYPEINFO(QXmlStreamNamespaceDeclaration, Q_MOVABLE_TYPE);
224typedef QVector<QXmlStreamNamespaceDeclaration> QXmlStreamNamespaceDeclarations;
225
226class Q_XMLSTREAM_EXPORT QXmlStreamNotationDeclaration {
227 QXmlStreamStringRef m_name, m_systemId, m_publicId;
228 void *reserved;
229
230 friend class QXmlStreamReaderPrivate;
231public:
232 QXmlStreamNotationDeclaration();
233 ~QXmlStreamNotationDeclaration();
234 QXmlStreamNotationDeclaration(const QXmlStreamNotationDeclaration &);
235 QXmlStreamNotationDeclaration& operator=(const QXmlStreamNotationDeclaration &);
236 inline QStringRef name() const { return m_name; }
237 inline QStringRef systemId() const { return m_systemId; }
238 inline QStringRef publicId() const { return m_publicId; }
239 inline bool operator==(const QXmlStreamNotationDeclaration &other) const {
240 return (name() == other.name() && systemId() == other.systemId()
241 && publicId() == other.publicId());
242 }
243 inline bool operator!=(const QXmlStreamNotationDeclaration &other) const
244 { return !operator==(other); }
245};
246
247Q_DECLARE_TYPEINFO(QXmlStreamNotationDeclaration, Q_MOVABLE_TYPE);
248typedef QVector<QXmlStreamNotationDeclaration> QXmlStreamNotationDeclarations;
249
250class Q_XMLSTREAM_EXPORT QXmlStreamEntityDeclaration {
251 QXmlStreamStringRef m_name, m_notationName, m_systemId, m_publicId, m_value;
252 void *reserved;
253
254 friend class QXmlStreamReaderPrivate;
255public:
256 QXmlStreamEntityDeclaration();
257 ~QXmlStreamEntityDeclaration();
258 QXmlStreamEntityDeclaration(const QXmlStreamEntityDeclaration &);
259 QXmlStreamEntityDeclaration& operator=(const QXmlStreamEntityDeclaration &);
260 inline QStringRef name() const { return m_name; }
261 inline QStringRef notationName() const { return m_notationName; }
262 inline QStringRef systemId() const { return m_systemId; }
263 inline QStringRef publicId() const { return m_publicId; }
264 inline QStringRef value() const { return m_value; }
265 inline bool operator==(const QXmlStreamEntityDeclaration &other) const {
266 return (name() == other.name()
267 && notationName() == other.notationName()
268 && systemId() == other.systemId()
269 && publicId() == other.publicId()
270 && value() == other.value());
271 }
272 inline bool operator!=(const QXmlStreamEntityDeclaration &other) const
273 { return !operator==(other); }
274};
275
276Q_DECLARE_TYPEINFO(QXmlStreamEntityDeclaration, Q_MOVABLE_TYPE);
277typedef QVector<QXmlStreamEntityDeclaration> QXmlStreamEntityDeclarations;
278
279
280class Q_XMLSTREAM_EXPORT QXmlStreamEntityResolver
281{
282public:
283 virtual ~QXmlStreamEntityResolver();
284 virtual QString resolveEntity(const QString& publicId, const QString& systemId);
285 virtual QString resolveUndeclaredEntity(const QString &name);
286};
287
288#ifndef QT_NO_XMLSTREAMREADER
289class Q_XMLSTREAM_EXPORT QXmlStreamReader {
290 QDOC_PROPERTY(bool namespaceProcessing READ namespaceProcessing WRITE setNamespaceProcessing)
291public:
292 enum TokenType {
293 NoToken = 0,
294 Invalid,
295 StartDocument,
296 EndDocument,
297 StartElement,
298 EndElement,
299 Characters,
300 Comment,
301 DTD,
302 EntityReference,
303 ProcessingInstruction
304 };
305
306
307 QXmlStreamReader();
308 QXmlStreamReader(QIODevice *device);
309 QXmlStreamReader(const QByteArray &data);
310 QXmlStreamReader(const QString &data);
311 QXmlStreamReader(const char * data);
312 ~QXmlStreamReader();
313
314 void setDevice(QIODevice *device);
315 QIODevice *device() const;
316 void addData(const QByteArray &data);
317 void addData(const QString &data);
318 void addData(const char *data);
319 void clear();
320
321
322 bool atEnd() const;
323 TokenType readNext();
324
325 bool readNextStartElement();
326 void skipCurrentElement();
327
328 TokenType tokenType() const;
329 QString tokenString() const;
330
331 void setNamespaceProcessing(bool);
332 bool namespaceProcessing() const;
333
334 inline bool isStartDocument() const { return tokenType() == StartDocument; }
335 inline bool isEndDocument() const { return tokenType() == EndDocument; }
336 inline bool isStartElement() const { return tokenType() == StartElement; }
337 inline bool isEndElement() const { return tokenType() == EndElement; }
338 inline bool isCharacters() const { return tokenType() == Characters; }
339 bool isWhitespace() const;
340 bool isCDATA() const;
341 inline bool isComment() const { return tokenType() == Comment; }
342 inline bool isDTD() const { return tokenType() == DTD; }
343 inline bool isEntityReference() const { return tokenType() == EntityReference; }
344 inline bool isProcessingInstruction() const { return tokenType() == ProcessingInstruction; }
345
346 bool isStandaloneDocument() const;
347 QStringRef documentVersion() const;
348 QStringRef documentEncoding() const;
349
350 qint64 lineNumber() const;
351 qint64 columnNumber() const;
352 qint64 characterOffset() const;
353
354 QXmlStreamAttributes attributes() const;
355
356 enum ReadElementTextBehaviour {
357 ErrorOnUnexpectedElement,
358 IncludeChildElements,
359 SkipChildElements
360 };
361 QString readElementText(ReadElementTextBehaviour behaviour);
362 QString readElementText();
363
364 QStringRef name() const;
365 QStringRef namespaceUri() const;
366 QStringRef qualifiedName() const;
367 QStringRef prefix() const;
368
369 QStringRef processingInstructionTarget() const;
370 QStringRef processingInstructionData() const;
371
372 QStringRef text() const;
373
374 QXmlStreamNamespaceDeclarations namespaceDeclarations() const;
375 void addExtraNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &extraNamespaceDeclaraction);
376 void addExtraNamespaceDeclarations(const QXmlStreamNamespaceDeclarations &extraNamespaceDeclaractions);
377 QXmlStreamNotationDeclarations notationDeclarations() const;
378 QXmlStreamEntityDeclarations entityDeclarations() const;
379 QStringRef dtdName() const;
380 QStringRef dtdPublicId() const;
381 QStringRef dtdSystemId() const;
382
383
384 enum Error {
385 NoError,
386 UnexpectedElementError,
387 CustomError,
388 NotWellFormedError,
389 PrematureEndOfDocumentError
390 };
391 void raiseError(const QString& message = QString());
392 QString errorString() const;
393 Error error() const;
394
395 inline bool hasError() const
396 {
397 return error() != NoError;
398 }
399
400 void setEntityResolver(QXmlStreamEntityResolver *resolver);
401 QXmlStreamEntityResolver *entityResolver() const;
402
403private:
404 Q_DISABLE_COPY(QXmlStreamReader)
405 Q_DECLARE_PRIVATE(QXmlStreamReader)
406 QScopedPointer<QXmlStreamReaderPrivate> d_ptr;
407
408};
409#endif // QT_NO_XMLSTREAMREADER
410
411#ifndef QT_NO_XMLSTREAMWRITER
412
413class QXmlStreamWriterPrivate;
414
415class Q_XMLSTREAM_EXPORT QXmlStreamWriter
416{
417 QDOC_PROPERTY(bool autoFormatting READ autoFormatting WRITE setAutoFormatting)
418 QDOC_PROPERTY(int autoFormattingIndent READ autoFormattingIndent WRITE setAutoFormattingIndent)
419public:
420 QXmlStreamWriter();
421 QXmlStreamWriter(QIODevice *device);
422 QXmlStreamWriter(QByteArray *array);
423 QXmlStreamWriter(QString *string);
424 ~QXmlStreamWriter();
425
426 void setDevice(QIODevice *device);
427 QIODevice *device() const;
428
429#ifndef QT_NO_TEXTCODEC
430 void setCodec(QTextCodec *codec);
431 void setCodec(const char *codecName);
432 QTextCodec *codec() const;
433#endif
434
435 void setAutoFormatting(bool);
436 bool autoFormatting() const;
437
438 void setAutoFormattingIndent(int spacesOrTabs);
439 int autoFormattingIndent() const;
440
441 void writeAttribute(const QString &qualifiedName, const QString &value);
442 void writeAttribute(const QString &namespaceUri, const QString &name, const QString &value);
443 void writeAttribute(const QXmlStreamAttribute& attribute);
444 void writeAttributes(const QXmlStreamAttributes& attributes);
445
446 void writeCDATA(const QString &text);
447 void writeCharacters(const QString &text);
448 void writeComment(const QString &text);
449
450 void writeDTD(const QString &dtd);
451
452 void writeEmptyElement(const QString &qualifiedName);
453 void writeEmptyElement(const QString &namespaceUri, const QString &name);
454
455 void writeTextElement(const QString &qualifiedName, const QString &text);
456 void writeTextElement(const QString &namespaceUri, const QString &name, const QString &text);
457
458 void writeEndDocument();
459 void writeEndElement();
460
461 void writeEntityReference(const QString &name);
462 void writeNamespace(const QString &namespaceUri, const QString &prefix = QString());
463 void writeDefaultNamespace(const QString &namespaceUri);
464 void writeProcessingInstruction(const QString &target, const QString &data = QString());
465
466 void writeStartDocument();
467 void writeStartDocument(const QString &version);
468 void writeStartDocument(const QString &version, bool standalone);
469 void writeStartElement(const QString &qualifiedName);
470 void writeStartElement(const QString &namespaceUri, const QString &name);
471
472#ifndef QT_NO_XMLSTREAMREADER
473 void writeCurrentToken(const QXmlStreamReader &reader);
474#endif
475
476private:
477 Q_DISABLE_COPY(QXmlStreamWriter)
478 Q_DECLARE_PRIVATE(QXmlStreamWriter)
479 QScopedPointer<QXmlStreamWriterPrivate> d_ptr;
480};
481#endif // QT_NO_XMLSTREAMWRITER
482
483QT_END_NAMESPACE
484
485QT_END_HEADER
486
487#endif // QT_NO_XMLSTREAM
488#endif // QXMLSTREAM_H
Note: See TracBrowser for help on using the repository browser.