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

Last change on this file since 33 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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