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