Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
| ** | |
| ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). | |
| ** Contact: http://www.qt-project.org/legal | |
| ** | |
| ** This file is part of the QtCore module of the Qt Toolkit. | |
| ** | |
| ** $QT_BEGIN_LICENSE:LGPL$ | |
| ** Commercial License Usage | |
| ** Licensees holding valid commercial Qt licenses may use this file in | |
| ** accordance with the commercial license agreement provided with the | |
| ** Software or, alternatively, in accordance with the terms contained in | |
| ** a written agreement between you and Digia. For licensing terms and | |
| ** conditions see http://qt.digia.com/licensing. For further information | |
| ** use the contact form at http://qt.digia.com/contact-us. | |
| ** | |
| ** GNU Lesser General Public License Usage | |
| ** Alternatively, this file may be used under the terms of the GNU Lesser | |
| ** General Public License version 2.1 as published by the Free Software | |
| ** Foundation and appearing in the file LICENSE.LGPL included in the | |
| ** packaging of this file. Please review the following information to | |
| ** ensure the GNU Lesser General Public License version 2.1 requirements | |
| ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | |
| ** | |
| ** In addition, as a special exception, Digia gives you certain additional | |
| ** rights. These rights are described in the Digia Qt LGPL Exception | |
| ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | |
| ** | |
| ** GNU General Public License Usage | |
| ** Alternatively, this file may be used under the terms of the GNU | |
| ** General Public License version 3.0 as published by the Free Software | |
| ** Foundation and appearing in the file LICENSE.GPL included in the | |
| ** packaging of this file. Please review the following information to | |
| ** ensure the GNU General Public License version 3.0 requirements will be | |
| ** met: http://www.gnu.org/copyleft/gpl.html. | |
| ** | |
| ** | |
| ** $QT_END_LICENSE$ | |
| ** | |
| ****************************************************************************/**************************************************************************** | |
2 | ** | - |
3 | ** Copyright (C) 2013 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 | #include "QtCore/qxmlstream.h" | - |
43 | | - |
44 | #if defined(QT_BUILD_XML_LIB) && defined(Q_OS_MAC64) | - |
45 | // No need to define this in the 64-bit Mac libraries. | - |
46 | // Since Qt 4.4 and previous weren't supported in 64-bit, there are | - |
47 | // no QXmlStream* symbols to keep compatibility with | - |
48 | # define QT_NO_XMLSTREAM | - |
49 | #endif | - |
50 | | - |
51 | #ifndef QT_NO_XMLSTREAM | - |
52 | | - |
53 | #include "qxmlutils_p.h" | - |
54 | #include <qdebug.h> | - |
55 | #include <qfile.h> | - |
56 | #include <stdio.h> | - |
57 | #include <qtextcodec.h> | - |
58 | #include <qstack.h> | - |
59 | #include <qbuffer.h> | - |
60 | #ifndef QT_BOOTSTRAPPED | - |
61 | #include <qcoreapplication.h> | - |
62 | #else | - |
63 | // This specialization of Q_DECLARE_TR_FUNCTIONS is not in qcoreapplication.h, | - |
64 | // because that header depends on QObject being available, which is not the | - |
65 | // case for most bootstrapped applications. | - |
66 | #define Q_DECLARE_TR_FUNCTIONS(context) \ | - |
67 | public: \ | - |
68 | static inline QString tr(const char *sourceText, const char *comment = 0) \ | - |
69 | { Q_UNUSED(comment); return QString::fromLatin1(sourceText); } \ | - |
70 | static inline QString trUtf8(const char *sourceText, const char *comment = 0) \ | - |
71 | { Q_UNUSED(comment); return QString::fromLatin1(sourceText); } \ | - |
72 | static inline QString tr(const char *sourceText, const char*, int) \ | - |
73 | { return QString::fromLatin1(sourceText); } \ | - |
74 | static inline QString trUtf8(const char *sourceText, const char*, int) \ | - |
75 | { return QString::fromLatin1(sourceText); } \ | - |
76 | private: | - |
77 | #endif | - |
78 | QT_BEGIN_NAMESPACE | - |
79 | | - |
80 | #include "qxmlstream_p.h" | - |
81 | | - |
82 | /*! | - |
83 | \enum QXmlStreamReader::TokenType | - |
84 | | - |
85 | This enum specifies the type of token the reader just read. | - |
86 | | - |
87 | \value NoToken The reader has not yet read anything. | - |
88 | | - |
89 | \value Invalid An error has occurred, reported in error() and | - |
90 | errorString(). | - |
91 | | - |
92 | \value StartDocument The reader reports the XML version number in | - |
93 | documentVersion(), and the encoding as specified in the XML | - |
94 | document in documentEncoding(). If the document is declared | - |
95 | standalone, isStandaloneDocument() returns true; otherwise it | - |
96 | returns false. | - |
97 | | - |
98 | \value EndDocument The reader reports the end of the document. | - |
99 | | - |
100 | \value StartElement The reader reports the start of an element | - |
101 | with namespaceUri() and name(). Empty elements are also reported | - |
102 | as StartElement, followed directly by EndElement. The convenience | - |
103 | function readElementText() can be called to concatenate all | - |
104 | content until the corresponding EndElement. Attributes are | - |
105 | reported in attributes(), namespace declarations in | - |
106 | namespaceDeclarations(). | - |
107 | | - |
108 | \value EndElement The reader reports the end of an element with | - |
109 | namespaceUri() and name(). | - |
110 | | - |
111 | \value Characters The reader reports characters in text(). If the | - |
112 | characters are all white-space, isWhitespace() returns true. If | - |
113 | the characters stem from a CDATA section, isCDATA() returns true. | - |
114 | | - |
115 | \value Comment The reader reports a comment in text(). | - |
116 | | - |
117 | \value DTD The reader reports a DTD in text(), notation | - |
118 | declarations in notationDeclarations(), and entity declarations in | - |
119 | entityDeclarations(). Details of the DTD declaration are reported | - |
120 | in in dtdName(), dtdPublicId(), and dtdSystemId(). | - |
121 | | - |
122 | \value EntityReference The reader reports an entity reference that | - |
123 | could not be resolved. The name of the reference is reported in | - |
124 | name(), the replacement text in text(). | - |
125 | | - |
126 | \value ProcessingInstruction The reader reports a processing | - |
127 | instruction in processingInstructionTarget() and | - |
128 | processingInstructionData(). | - |
129 | */ | - |
130 | | - |
131 | /*! | - |
132 | \enum QXmlStreamReader::ReadElementTextBehaviour | - |
133 | | - |
134 | This enum specifies the different behaviours of readElementText(). | - |
135 | | - |
136 | \value ErrorOnUnexpectedElement Raise an UnexpectedElementError and return | - |
137 | what was read so far when a child element is encountered. | - |
138 | | - |
139 | \value IncludeChildElements Recursively include the text from child elements. | - |
140 | | - |
141 | \value SkipChildElements Skip child elements. | - |
142 | | - |
143 | \since 4.6 | - |
144 | */ | - |
145 | | - |
146 | /*! | - |
147 | \enum QXmlStreamReader::Error | - |
148 | | - |
149 | This enum specifies different error cases | - |
150 | | - |
151 | \value NoError No error has occurred. | - |
152 | | - |
153 | \value CustomError A custom error has been raised with | - |
154 | raiseError() | - |
155 | | - |
156 | \value NotWellFormedError The parser internally raised an error | - |
157 | due to the read XML not being well-formed. | - |
158 | | - |
159 | \value PrematureEndOfDocumentError The input stream ended before a | - |
160 | well-formed XML document was parsed. Recovery from this error is | - |
161 | possible if more XML arrives in the stream, either by calling | - |
162 | addData() or by waiting for it to arrive on the device(). | - |
163 | | - |
164 | \value UnexpectedElementError The parser encountered an element | - |
165 | that was different to those it expected. | - |
166 | | - |
167 | */ | - |
168 | | - |
169 | /*! | - |
170 | \class QXmlStreamEntityResolver | - |
171 | \inmodule QtCore | - |
172 | \reentrant | - |
173 | \since 4.4 | - |
174 | | - |
175 | \brief The QXmlStreamEntityResolver class provides an entity | - |
176 | resolver for a QXmlStreamReader. | - |
177 | | - |
178 | \ingroup xml-tools | - |
179 | */ | - |
180 | | - |
181 | /*! | - |
182 | Destroys the entity resolver. | - |
183 | */ | - |
184 | QXmlStreamEntityResolver::~QXmlStreamEntityResolver() | - |
185 | { | - |
186 | } | - |
187 | | - |
188 | /*! | - |
189 | \internal | - |
190 | | - |
191 | This function is a stub for later functionality. | - |
192 | */ | - |
193 | QString QXmlStreamEntityResolver::resolveEntity(const QString& /*publicId*/, const QString& /*systemId*/) | - |
194 | { | - |
195 | return QString(); | - |
196 | } | - |
197 | | - |
198 | | - |
199 | /*! | - |
200 | Resolves the undeclared entity \a name and returns its replacement | - |
201 | text. If the entity is also unknown to the entity resolver, it | - |
202 | returns an empty string. | - |
203 | | - |
204 | The default implementation always returns an empty string. | - |
205 | */ | - |
206 | | - |
207 | QString QXmlStreamEntityResolver::resolveUndeclaredEntity(const QString &/*name*/) | - |
208 | { | - |
209 | return QString(); | - |
210 | } | - |
211 | | - |
212 | #ifndef QT_NO_XMLSTREAMREADER | - |
213 | | - |
214 | QString QXmlStreamReaderPrivate::resolveUndeclaredEntity(const QString &name) | - |
215 | { | - |
216 | if (entityResolver) | - |
217 | return entityResolver->resolveUndeclaredEntity(name); | - |
218 | return QString(); | - |
219 | } | - |
220 | | - |
221 | | - |
222 | | - |
223 | /*! | - |
224 | \since 4.4 | - |
225 | | - |
226 | Makes \a resolver the new entityResolver(). | - |
227 | | - |
228 | The stream reader does \e not take ownership of the resolver. It's | - |
229 | the callers responsibility to ensure that the resolver is valid | - |
230 | during the entire life-time of the stream reader object, or until | - |
231 | another resolver or 0 is set. | - |
232 | | - |
233 | \sa entityResolver() | - |
234 | */ | - |
235 | void QXmlStreamReader::setEntityResolver(QXmlStreamEntityResolver *resolver) | - |
236 | { | - |
237 | Q_D(QXmlStreamReader); | - |
238 | d->entityResolver = resolver; | - |
239 | } | - |
240 | | - |
241 | /*! | - |
242 | \since 4.4 | - |
243 | | - |
244 | Returns the entity resolver, or 0 if there is no entity resolver. | - |
245 | | - |
246 | \sa setEntityResolver() | - |
247 | */ | - |
248 | QXmlStreamEntityResolver *QXmlStreamReader::entityResolver() const | - |
249 | { | - |
250 | Q_D(const QXmlStreamReader); | - |
251 | return d->entityResolver; | - |
252 | } | - |
253 | | - |
254 | | - |
255 | | - |
256 | /*! | - |
257 | \class QXmlStreamReader | - |
258 | \inmodule QtCore | - |
259 | \reentrant | - |
260 | \since 4.3 | - |
261 | | - |
262 | \brief The QXmlStreamReader class provides a fast parser for reading | - |
263 | well-formed XML via a simple streaming API. | - |
264 | | - |
265 | | - |
266 | \ingroup xml-tools | - |
267 | | - |
268 | QXmlStreamReader is a faster and more convenient replacement for | - |
269 | Qt's own SAX parser (see QXmlSimpleReader). In some cases it might | - |
270 | also be a faster and more convenient alternative for use in | - |
271 | applications that would otherwise use a DOM tree (see QDomDocument). | - |
272 | QXmlStreamReader reads data either from a QIODevice (see | - |
273 | setDevice()), or from a raw QByteArray (see addData()). | - |
274 | | - |
275 | Qt provides QXmlStreamWriter for writing XML. | - |
276 | | - |
277 | The basic concept of a stream reader is to report an XML document as | - |
278 | a stream of tokens, similar to SAX. The main difference between | - |
279 | QXmlStreamReader and SAX is \e how these XML tokens are reported. | - |
280 | With SAX, the application must provide handlers (callback functions) | - |
281 | that receive so-called XML \e events from the parser at the parser's | - |
282 | convenience. With QXmlStreamReader, the application code itself | - |
283 | drives the loop and pulls \e tokens from the reader, one after | - |
284 | another, as it needs them. This is done by calling readNext(), where | - |
285 | the reader reads from the input stream until it completes the next | - |
286 | token, at which point it returns the tokenType(). A set of | - |
287 | convenient functions including isStartElement() and text() can then | - |
288 | be used to examine the token to obtain information about what has | - |
289 | been read. The big advantage of this \e pulling approach is the | - |
290 | possibility to build recursive descent parsers with it, meaning you | - |
291 | can split your XML parsing code easily into different methods or | - |
292 | classes. This makes it easy to keep track of the application's own | - |
293 | state when parsing XML. | - |
294 | | - |
295 | A typical loop with QXmlStreamReader looks like this: | - |
296 | | - |
297 | \snippet code/src_corelib_xml_qxmlstream.cpp 0 | - |
298 | | - |
299 | | - |
300 | QXmlStreamReader is a well-formed XML 1.0 parser that does \e not | - |
301 | include external parsed entities. As long as no error occurs, the | - |
302 | application code can thus be assured that the data provided by the | - |
303 | stream reader satisfies the W3C's criteria for well-formed XML. For | - |
304 | example, you can be certain that all tags are indeed nested and | - |
305 | closed properly, that references to internal entities have been | - |
306 | replaced with the correct replacement text, and that attributes have | - |
307 | been normalized or added according to the internal subset of the | - |
308 | DTD. | - |
309 | | - |
310 | If an error occurs while parsing, atEnd() and hasError() return | - |
311 | true, and error() returns the error that occurred. The functions | - |
312 | errorString(), lineNumber(), columnNumber(), and characterOffset() | - |
313 | are for constructing an appropriate error or warning message. To | - |
314 | simplify application code, QXmlStreamReader contains a raiseError() | - |
315 | mechanism that lets you raise custom errors that trigger the same | - |
316 | error handling described. | - |
317 | | - |
318 | The \l{QXmlStream Bookmarks Example} illustrates how to use the | - |
319 | recursive descent technique to read an XML bookmark file (XBEL) with | - |
320 | a stream reader. | - |
321 | | - |
322 | \section1 Namespaces | - |
323 | | - |
324 | QXmlStream understands and resolves XML namespaces. E.g. in case of | - |
325 | a StartElement, namespaceUri() returns the namespace the element is | - |
326 | in, and name() returns the element's \e local name. The combination | - |
327 | of namespaceUri and name uniquely identifies an element. If a | - |
328 | namespace prefix was not declared in the XML entities parsed by the | - |
329 | reader, the namespaceUri is empty. | - |
330 | | - |
331 | If you parse XML data that does not utilize namespaces according to | - |
332 | the XML specification or doesn't use namespaces at all, you can use | - |
333 | the element's qualifiedName() instead. A qualified name is the | - |
334 | element's prefix() followed by colon followed by the element's local | - |
335 | name() - exactly like the element appears in the raw XML data. Since | - |
336 | the mapping namespaceUri to prefix is neither unique nor universal, | - |
337 | qualifiedName() should be avoided for namespace-compliant XML data. | - |
338 | | - |
339 | In order to parse standalone documents that do use undeclared | - |
340 | namespace prefixes, you can turn off namespace processing completely | - |
341 | with the \l namespaceProcessing property. | - |
342 | | - |
343 | \section1 Incremental parsing | - |
344 | | - |
345 | QXmlStreamReader is an incremental parser. It can handle the case | - |
346 | where the document can't be parsed all at once because it arrives in | - |
347 | chunks (e.g. from multiple files, or over a network connection). | - |
348 | When the reader runs out of data before the complete document has | - |
349 | been parsed, it reports a PrematureEndOfDocumentError. When more | - |
350 | data arrives, either because of a call to addData() or because more | - |
351 | data is available through the network device(), the reader recovers | - |
352 | from the PrematureEndOfDocumentError error and continues parsing the | - |
353 | new data with the next call to readNext(). | - |
354 | | - |
355 | For example, if your application reads data from the network using a | - |
356 | \l{QNetworkAccessManager} {network access manager}, you would issue | - |
357 | a \l{QNetworkRequest} {network request} to the manager and receive a | - |
358 | \l{QNetworkReply} {network reply} in return. Since a QNetworkReply | - |
359 | is a QIODevice, you connect its \l{QNetworkReply::readyRead()} | - |
360 | {readyRead()} signal to a custom slot, e.g. \c{slotReadyRead()} in | - |
361 | the code snippet shown in the discussion for QNetworkAccessManager. | - |
362 | In this slot, you read all available data with | - |
363 | \l{QNetworkReply::readAll()} {readAll()} and pass it to the XML | - |
364 | stream reader using addData(). Then you call your custom parsing | - |
365 | function that reads the XML events from the reader. | - |
366 | | - |
367 | \section1 Performance and memory consumption | - |
368 | | - |
369 | QXmlStreamReader is memory-conservative by design, since it doesn't | - |
370 | store the entire XML document tree in memory, but only the current | - |
371 | token at the time it is reported. In addition, QXmlStreamReader | - |
372 | avoids the many small string allocations that it normally takes to | - |
373 | map an XML document to a convenient and Qt-ish API. It does this by | - |
374 | reporting all string data as QStringRef rather than real QString | - |
375 | objects. QStringRef is a thin wrapper around QString substrings that | - |
376 | provides a subset of the QString API without the memory allocation | - |
377 | and reference-counting overhead. Calling | - |
378 | \l{QStringRef::toString()}{toString()} on any of those objects | - |
379 | returns an equivalent real QString object. | - |
380 | | - |
381 | */ | - |
382 | | - |
383 | | - |
384 | /*! | - |
385 | Constructs a stream reader. | - |
386 | | - |
387 | \sa setDevice(), addData() | - |
388 | */ | - |
389 | QXmlStreamReader::QXmlStreamReader() | - |
390 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - |
391 | { | - |
392 | } | - |
393 | | - |
394 | /*! Creates a new stream reader that reads from \a device. | - |
395 | | - |
396 | \sa setDevice(), clear() | - |
397 | */ | - |
398 | QXmlStreamReader::QXmlStreamReader(QIODevice *device) | - |
399 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - |
400 | { | - |
401 | setDevice(device); | - |
402 | } | - |
403 | | - |
404 | /*! | - |
405 | Creates a new stream reader that reads from \a data. | - |
406 | | - |
407 | \sa addData(), clear(), setDevice() | - |
408 | */ | - |
409 | QXmlStreamReader::QXmlStreamReader(const QByteArray &data) | - |
410 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - |
411 | { | - |
412 | Q_D(QXmlStreamReader); | - |
413 | d->dataBuffer = data; | - |
414 | } | - |
415 | | - |
416 | /*! | - |
417 | Creates a new stream reader that reads from \a data. | - |
418 | | - |
419 | \sa addData(), clear(), setDevice() | - |
420 | */ | - |
421 | QXmlStreamReader::QXmlStreamReader(const QString &data) | - |
422 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - |
423 | { | - |
424 | Q_D(QXmlStreamReader); | - |
425 | #ifdef QT_NO_TEXTCODEC | - |
426 | d->dataBuffer = data.toLatin1(); | - |
427 | #else | - |
428 | d->dataBuffer = d->codec->fromUnicode(data); | - |
429 | d->decoder = d->codec->makeDecoder(); | - |
430 | #endif | - |
431 | d->lockEncoding = true; | - |
432 | | - |
433 | } | - |
434 | | - |
435 | /*! | - |
436 | Creates a new stream reader that reads from \a data. | - |
437 | | - |
438 | \sa addData(), clear(), setDevice() | - |
439 | */ | - |
440 | QXmlStreamReader::QXmlStreamReader(const char *data) | - |
441 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - |
442 | { | - |
443 | Q_D(QXmlStreamReader); | - |
444 | d->dataBuffer = QByteArray(data); | - |
445 | } | - |
446 | | - |
447 | /*! | - |
448 | Destructs the reader. | - |
449 | */ | - |
450 | QXmlStreamReader::~QXmlStreamReader() | - |
451 | { | - |
452 | Q_D(QXmlStreamReader); | - |
453 | if (d->deleteDevice) | - |
454 | delete d->device; | - |
455 | } | - |
456 | | - |
457 | /*! \fn bool QXmlStreamReader::hasError() const | - |
458 | Returns \c true if an error has occurred, otherwise \c false. | - |
459 | | - |
460 | \sa errorString(), error() | - |
461 | */ | - |
462 | | - |
463 | /*! | - |
464 | Sets the current device to \a device. Setting the device resets | - |
465 | the stream to its initial state. | - |
466 | | - |
467 | \sa device(), clear() | - |
468 | */ | - |
469 | void QXmlStreamReader::setDevice(QIODevice *device) | - |
470 | { | - |
471 | Q_D(QXmlStreamReader); | - |
472 | if (d->deleteDevice) { | - |
473 | delete d->device; | - |
474 | d->deleteDevice = false; | - |
475 | } | - |
476 | d->device = device; | - |
477 | d->init(); | - |
478 | | - |
479 | } | - |
480 | | - |
481 | /*! | - |
482 | Returns the current device associated with the QXmlStreamReader, | - |
483 | or 0 if no device has been assigned. | - |
484 | | - |
485 | \sa setDevice() | - |
486 | */ | - |
487 | QIODevice *QXmlStreamReader::device() const | - |
488 | { | - |
489 | Q_D(const QXmlStreamReader); | - |
490 | return d->device; | - |
491 | } | - |
492 | | - |
493 | | - |
494 | /*! | - |
495 | Adds more \a data for the reader to read. This function does | - |
496 | nothing if the reader has a device(). | - |
497 | | - |
498 | \sa readNext(), clear() | - |
499 | */ | - |
500 | void QXmlStreamReader::addData(const QByteArray &data) | - |
501 | { | - |
502 | Q_D(QXmlStreamReader); | - |
503 | if (d->device) { | - |
504 | qWarning("QXmlStreamReader: addData() with device()"); | - |
505 | return; | - |
506 | } | - |
507 | d->dataBuffer += data; | - |
508 | } | - |
509 | | - |
510 | /*! | - |
511 | Adds more \a data for the reader to read. This function does | - |
512 | nothing if the reader has a device(). | - |
513 | | - |
514 | \sa readNext(), clear() | - |
515 | */ | - |
516 | void QXmlStreamReader::addData(const QString &data) | - |
517 | { | - |
518 | Q_D(QXmlStreamReader); | - |
519 | d->lockEncoding = true; | - |
520 | #ifdef QT_NO_TEXTCODEC | - |
521 | addData(data.toLatin1()); | - |
522 | #else | - |
523 | addData(d->codec->fromUnicode(data)); | - |
524 | #endif | - |
525 | } | - |
526 | | - |
527 | /*! | - |
528 | Adds more \a data for the reader to read. This function does | - |
529 | nothing if the reader has a device(). | - |
530 | | - |
531 | \sa readNext(), clear() | - |
532 | */ | - |
533 | void QXmlStreamReader::addData(const char *data) | - |
534 | { | - |
535 | addData(QByteArray(data)); | - |
536 | } | - |
537 | | - |
538 | /*! | - |
539 | Removes any device() or data from the reader and resets its | - |
540 | internal state to the initial state. | - |
541 | | - |
542 | \sa addData() | - |
543 | */ | - |
544 | void QXmlStreamReader::clear() | - |
545 | { | - |
546 | Q_D(QXmlStreamReader); | - |
547 | d->init(); | - |
548 | if (d->device) { | - |
549 | if (d->deleteDevice) | - |
550 | delete d->device; | - |
551 | d->device = 0; | - |
552 | } | - |
553 | } | - |
554 | | - |
555 | /*! | - |
556 | Returns true if the reader has read until the end of the XML | - |
557 | document, or if an error() has occurred and reading has been | - |
558 | aborted. Otherwise, it returns false. | - |
559 | | - |
560 | When atEnd() and hasError() return true and error() returns | - |
561 | PrematureEndOfDocumentError, it means the XML has been well-formed | - |
562 | so far, but a complete XML document has not been parsed. The next | - |
563 | chunk of XML can be added with addData(), if the XML is being read | - |
564 | from a QByteArray, or by waiting for more data to arrive if the | - |
565 | XML is being read from a QIODevice. Either way, atEnd() will | - |
566 | return false once more data is available. | - |
567 | | - |
568 | \sa hasError(), error(), device(), QIODevice::atEnd() | - |
569 | */ | - |
570 | bool QXmlStreamReader::atEnd() const | - |
571 | { | - |
572 | Q_D(const QXmlStreamReader); | - |
573 | if (d->atEnd | - |
574 | && ((d->type == QXmlStreamReader::Invalid && d->error == PrematureEndOfDocumentError) | - |
575 | || (d->type == QXmlStreamReader::EndDocument))) { | - |
576 | if (d->device) | - |
577 | return d->device->atEnd(); | - |
578 | else | - |
579 | return !d->dataBuffer.size(); | - |
580 | } | - |
581 | return (d->atEnd || d->type == QXmlStreamReader::Invalid); | - |
582 | } | - |
583 | | - |
584 | | - |
585 | /*! | - |
586 | Reads the next token and returns its type. | - |
587 | | - |
588 | With one exception, once an error() is reported by readNext(), | - |
589 | further reading of the XML stream is not possible. Then atEnd() | - |
590 | returns true, hasError() returns true, and this function returns | - |
591 | QXmlStreamReader::Invalid. | - |
592 | | - |
593 | The exception is when error() returns PrematureEndOfDocumentError. | - |
594 | This error is reported when the end of an otherwise well-formed | - |
595 | chunk of XML is reached, but the chunk doesn't represent a complete | - |
596 | XML document. In that case, parsing \e can be resumed by calling | - |
597 | addData() to add the next chunk of XML, when the stream is being | - |
598 | read from a QByteArray, or by waiting for more data to arrive when | - |
599 | the stream is being read from a device(). | - |
600 | | - |
601 | \sa tokenType(), tokenString() | - |
602 | */ | - |
603 | QXmlStreamReader::TokenType QXmlStreamReader::readNext() | - |
604 | { | - |
605 | Q_D(QXmlStreamReader); | - |
606 | if (d->type != Invalid) { | - |
607 | if (!d->hasCheckedStartDocument) | - |
608 | if (!d->checkStartDocument()) | - |
609 | return d->type; // synthetic StartDocument or error | - |
610 | d->parse(); | - |
611 | if (d->atEnd && d->type != EndDocument && d->type != Invalid) | - |
612 | d->raiseError(PrematureEndOfDocumentError); | - |
613 | else if (!d->atEnd && d->type == EndDocument) | - |
614 | d->raiseWellFormedError(QXmlStream::tr("Extra content at end of document.")); | - |
615 | } else if (d->error == PrematureEndOfDocumentError) { | - |
616 | // resume error | - |
617 | d->type = NoToken; | - |
618 | d->atEnd = false; | - |
619 | d->token = -1; | - |
620 | return readNext(); | - |
621 | } | - |
622 | return d->type; | - |
623 | } | - |
624 | | - |
625 | | - |
626 | /*! | - |
627 | Returns the type of the current token. | - |
628 | | - |
629 | The current token can also be queried with the convenience functions | - |
630 | isStartDocument(), isEndDocument(), isStartElement(), | - |
631 | isEndElement(), isCharacters(), isComment(), isDTD(), | - |
632 | isEntityReference(), and isProcessingInstruction(). | - |
633 | | - |
634 | \sa tokenString() | - |
635 | */ | - |
636 | QXmlStreamReader::TokenType QXmlStreamReader::tokenType() const | - |
637 | { | - |
638 | Q_D(const QXmlStreamReader); | - |
639 | return d->type; | - |
640 | } | - |
641 | | - |
642 | /*! | - |
643 | Reads until the next start element within the current element. Returns true | - |
644 | when a start element was reached. When the end element was reached, or when | - |
645 | an error occurred, false is returned. | - |
646 | | - |
647 | The current element is the element matching the most recently parsed start | - |
648 | element of which a matching end element has not yet been reached. When the | - |
649 | parser has reached the end element, the current element becomes the parent | - |
650 | element. | - |
651 | | - |
652 | This is a convenience function for when you're only concerned with parsing | - |
653 | XML elements. The \l{QXmlStream Bookmarks Example} makes extensive use of | - |
654 | this function. | - |
655 | | - |
656 | \since 4.6 | - |
657 | \sa readNext() | - |
658 | */ | - |
659 | bool QXmlStreamReader::readNextStartElement() | - |
660 | { | - |
661 | while (readNext() != Invalid) { | - |
662 | if (isEndElement()) | - |
663 | return false; | - |
664 | else if (isStartElement()) | - |
665 | return true; | - |
666 | } | - |
667 | return false; | - |
668 | } | - |
669 | | - |
670 | /*! | - |
671 | Reads until the end of the current element, skipping any child nodes. | - |
672 | This function is useful for skipping unknown elements. | - |
673 | | - |
674 | The current element is the element matching the most recently parsed start | - |
675 | element of which a matching end element has not yet been reached. When the | - |
676 | parser has reached the end element, the current element becomes the parent | - |
677 | element. | - |
678 | | - |
679 | \since 4.6 | - |
680 | */ | - |
681 | void QXmlStreamReader::skipCurrentElement() | - |
682 | { | - |
683 | int depth = 1; | - |
684 | while (depth && readNext() != Invalid) { | - |
685 | if (isEndElement()) | - |
686 | --depth; | - |
687 | else if (isStartElement()) | - |
688 | ++depth; | - |
689 | } | - |
690 | } | - |
691 | | - |
692 | /* | - |
693 | * Use the following Perl script to generate the error string index list: | - |
694 | ===== PERL SCRIPT ==== | - |
695 | print "static const char QXmlStreamReader_tokenTypeString_string[] =\n"; | - |
696 | $counter = 0; | - |
697 | $i = 0; | - |
698 | while (<STDIN>) { | - |
699 | chomp; | - |
700 | print " \"$_\\0\"\n"; | - |
701 | $sizes[$i++] = $counter; | - |
702 | $counter += length 1 + $_; | - |
703 | } | - |
704 | print " \"\\0\";\n\nstatic const short QXmlStreamReader_tokenTypeString_indices[] = {\n "; | - |
705 | for ($j = 0; $j < $i; ++$j) { | - |
706 | printf "$sizes[$j], "; | - |
707 | } | - |
708 | print "0\n};\n"; | - |
709 | ===== PERL SCRIPT ==== | - |
710 | | - |
711 | * The input data is as follows (copied from qxmlstream.h): | - |
712 | NoToken | - |
713 | Invalid | - |
714 | StartDocument | - |
715 | EndDocument | - |
716 | StartElement | - |
717 | EndElement | - |
718 | Characters | - |
719 | Comment | - |
720 | DTD | - |
721 | EntityReference | - |
722 | ProcessingInstruction | - |
723 | */ | - |
724 | static const char QXmlStreamReader_tokenTypeString_string[] = | - |
725 | "NoToken\0" | - |
726 | "Invalid\0" | - |
727 | "StartDocument\0" | - |
728 | "EndDocument\0" | - |
729 | "StartElement\0" | - |
730 | "EndElement\0" | - |
731 | "Characters\0" | - |
732 | "Comment\0" | - |
733 | "DTD\0" | - |
734 | "EntityReference\0" | - |
735 | "ProcessingInstruction\0"; | - |
736 | | - |
737 | static const short QXmlStreamReader_tokenTypeString_indices[] = { | - |
738 | 0, 8, 16, 30, 42, 55, 66, 77, 85, 89, 105, 0 | - |
739 | }; | - |
740 | | - |
741 | | - |
742 | /*! | - |
743 | \property QXmlStreamReader::namespaceProcessing | - |
744 | the namespace-processing flag of the stream reader | - |
745 | | - |
746 | This property controls whether or not the stream reader processes | - |
747 | namespaces. If enabled, the reader processes namespaces, otherwise | - |
748 | it does not. | - |
749 | | - |
750 | By default, namespace-processing is enabled. | - |
751 | */ | - |
752 | | - |
753 | | - |
754 | void QXmlStreamReader::setNamespaceProcessing(bool enable) | - |
755 | { | - |
756 | Q_D(QXmlStreamReader); | - |
757 | d->namespaceProcessing = enable; | - |
758 | } | - |
759 | | - |
760 | bool QXmlStreamReader::namespaceProcessing() const | - |
761 | { | - |
762 | Q_D(const QXmlStreamReader); | - |
763 | return d->namespaceProcessing; | - |
764 | } | - |
765 | | - |
766 | /*! Returns the reader's current token as string. | - |
767 | | - |
768 | \sa tokenType() | - |
769 | */ | - |
770 | QString QXmlStreamReader::tokenString() const | - |
771 | { | - |
772 | Q_D(const QXmlStreamReader); | - |
773 | return QLatin1String(QXmlStreamReader_tokenTypeString_string + | - |
774 | QXmlStreamReader_tokenTypeString_indices[d->type]); | - |
775 | } | - |
776 | | - |
777 | #endif // QT_NO_XMLSTREAMREADER | - |
778 | | - |
779 | QXmlStreamPrivateTagStack::QXmlStreamPrivateTagStack() | - |
780 | { | - |
781 | tagStack.reserve(16); | - |
782 | tagStackStringStorage.reserve(32); | - |
783 | tagStackStringStorageSize = 0; | - |
784 | NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); | - |
785 | namespaceDeclaration.prefix = addToStringStorage(QLatin1String("xml")); | - |
786 | namespaceDeclaration.namespaceUri = addToStringStorage(QLatin1String("http://www.w3.org/XML/1998/namespace")); | - |
787 | } | - |
788 | | - |
789 | #ifndef QT_NO_XMLSTREAMREADER | - |
790 | | - |
791 | QXmlStreamReaderPrivate::QXmlStreamReaderPrivate(QXmlStreamReader *q) | - |
792 | :q_ptr(q) | - |
793 | { | - |
794 | device = 0; | - |
795 | deleteDevice = false; | - |
796 | #ifndef QT_NO_TEXTCODEC | - |
797 | decoder = 0; | - |
798 | #endif | - |
799 | stack_size = 64; | - |
800 | sym_stack = 0; | - |
801 | state_stack = 0; | - |
802 | reallocateStack(); | - |
803 | entityResolver = 0; | - |
804 | init(); | - |
805 | entityHash.insert(QLatin1String("lt"), Entity::createLiteral(QLatin1String("<"))); | - |
806 | entityHash.insert(QLatin1String("gt"), Entity::createLiteral(QLatin1String(">"))); | - |
807 | entityHash.insert(QLatin1String("amp"), Entity::createLiteral(QLatin1String("&"))); | - |
808 | entityHash.insert(QLatin1String("apos"), Entity::createLiteral(QLatin1String("'"))); | - |
809 | entityHash.insert(QLatin1String("quot"), Entity::createLiteral(QLatin1String("\""))); | - |
810 | } | - |
811 | | - |
812 | void QXmlStreamReaderPrivate::init() | - |
813 | { | - |
814 | scanDtd = false; | - |
815 | token = -1; | - |
816 | token_char = 0; | - |
817 | isEmptyElement = false; | - |
818 | isWhitespace = true; | - |
819 | isCDATA = false; | - |
820 | standalone = false; | - |
821 | tos = 0; | - |
822 | resumeReduction = 0; | - |
823 | state_stack[tos++] = 0; | - |
824 | state_stack[tos] = 0; | - |
825 | putStack.clear(); | - |
826 | putStack.reserve(32); | - |
827 | textBuffer.clear(); | - |
828 | textBuffer.reserve(256); | - |
829 | tagStack.clear(); | - |
830 | tagsDone = false; | - |
831 | attributes.clear(); | - |
832 | attributes.reserve(16); | - |
833 | lineNumber = lastLineStart = characterOffset = 0; | - |
834 | readBufferPos = 0; | - |
835 | nbytesread = 0; | - |
836 | #ifndef QT_NO_TEXTCODEC | - |
837 | codec = QTextCodec::codecForMib(106); // utf8 | - |
838 | delete decoder; | - |
839 | decoder = 0; | - |
840 | #endif | - |
841 | attributeStack.clear(); | - |
842 | attributeStack.reserve(16); | - |
843 | entityParser = 0; | - |
844 | hasCheckedStartDocument = false; | - |
845 | normalizeLiterals = false; | - |
846 | hasSeenTag = false; | - |
847 | atEnd = false; | - |
848 | inParseEntity = false; | - |
849 | referenceToUnparsedEntityDetected = false; | - |
850 | referenceToParameterEntityDetected = false; | - |
851 | hasExternalDtdSubset = false; | - |
852 | lockEncoding = false; | - |
853 | namespaceProcessing = true; | - |
854 | rawReadBuffer.clear(); | - |
855 | dataBuffer.clear(); | - |
856 | readBuffer.clear(); | - |
857 | | - |
858 | type = QXmlStreamReader::NoToken; | - |
859 | error = QXmlStreamReader::NoError; | - |
860 | } | - |
861 | | - |
862 | /* | - |
863 | Well-formed requires that we verify entity values. We do this with a | - |
864 | standard parser. | - |
865 | */ | - |
866 | void QXmlStreamReaderPrivate::parseEntity(const QString &value) | - |
867 | { | - |
868 | Q_Q(QXmlStreamReader); | - |
869 | | - |
870 | if (value.isEmpty()) | - |
871 | return; | - |
872 | | - |
873 | | - |
874 | if (!entityParser) | - |
875 | entityParser = new QXmlStreamReaderPrivate(q); | - |
876 | else | - |
877 | entityParser->init(); | - |
878 | entityParser->inParseEntity = true; | - |
879 | entityParser->readBuffer = value; | - |
880 | entityParser->injectToken(PARSE_ENTITY); | - |
881 | while (!entityParser->atEnd && entityParser->type != QXmlStreamReader::Invalid) | - |
882 | entityParser->parse(); | - |
883 | if (entityParser->type == QXmlStreamReader::Invalid || entityParser->tagStack.size()) | - |
884 | raiseWellFormedError(QXmlStream::tr("Invalid entity value.")); | - |
885 | | - |
886 | } | - |
887 | | - |
888 | inline void QXmlStreamReaderPrivate::reallocateStack() | - |
889 | { | - |
890 | stack_size <<= 1; | - |
891 | sym_stack = reinterpret_cast<Value*> (realloc(sym_stack, stack_size * sizeof(Value))); | - |
892 | Q_CHECK_PTR(sym_stack); | - |
893 | state_stack = reinterpret_cast<int*> (realloc(state_stack, stack_size * sizeof(int))); | - |
894 | Q_CHECK_PTR(sym_stack); | - |
895 | } | - |
896 | | - |
897 | | - |
898 | QXmlStreamReaderPrivate::~QXmlStreamReaderPrivate() | - |
899 | { | - |
900 | #ifndef QT_NO_TEXTCODEC | - |
901 | delete decoder; | - |
902 | #endif | - |
903 | free(sym_stack); | - |
904 | free(state_stack); | - |
905 | delete entityParser; | - |
906 | } | - |
907 | | - |
908 | | - |
909 | inline uint QXmlStreamReaderPrivate::filterCarriageReturn() | - |
910 | { | - |
911 | uint peekc = peekChar(); | - |
912 | if (peekc == '\n') { | - |
913 | if (putStack.size()) | - |
914 | putStack.pop(); | - |
915 | else | - |
916 | ++readBufferPos; | - |
917 | return peekc; | - |
918 | } | - |
919 | if (peekc == 0) { | - |
920 | putChar('\r'); | - |
921 | return 0; | - |
922 | } | - |
923 | return '\n'; | - |
924 | } | - |
925 | | - |
926 | /*! | - |
927 | \internal | - |
928 | If the end of the file is encountered, 0 is returned. | - |
929 | */ | - |
930 | inline uint QXmlStreamReaderPrivate::getChar() | - |
931 | { | - |
932 | uint c; | - |
933 | if (putStack.size()) { | - |
934 | c = atEnd ? 0 : putStack.pop(); | - |
935 | } else { | - |
936 | if (readBufferPos < readBuffer.size()) | - |
937 | c = readBuffer.at(readBufferPos++).unicode(); | - |
938 | else | - |
939 | c = getChar_helper(); | - |
940 | } | - |
941 | | - |
942 | return c; | - |
943 | } | - |
944 | | - |
945 | inline uint QXmlStreamReaderPrivate::peekChar() | - |
946 | { | - |
947 | uint c; | - |
948 | if (putStack.size()) { | - |
949 | c = putStack.top(); | - |
950 | } else if (readBufferPos < readBuffer.size()) { | - |
951 | c = readBuffer.at(readBufferPos).unicode(); | - |
952 | } else { | - |
953 | if ((c = getChar_helper())) | - |
954 | --readBufferPos; | - |
955 | } | - |
956 | | - |
957 | return c; | - |
958 | } | - |
959 | | - |
960 | /*! | - |
961 | \internal | - |
962 | | - |
963 | Scans characters until \a str is encountered, and validates the characters | - |
964 | as according to the Char[2] production and do the line-ending normalization. | - |
965 | If any character is invalid, false is returned, otherwise true upon success. | - |
966 | | - |
967 | If \a tokenToInject is not less than zero, injectToken() is called with | - |
968 | \a tokenToInject when \a str is found. | - |
969 | | - |
970 | If any error occurred, false is returned, otherwise true. | - |
971 | */ | - |
972 | bool QXmlStreamReaderPrivate::scanUntil(const char *str, short tokenToInject) | - |
973 | { | - |
974 | int pos = textBuffer.size(); | - |
975 | int oldLineNumber = lineNumber; | - |
976 | | - |
977 | while (uint c = getChar()) { | - |
978 | /* First, we do the validation & normalization. */ | - |
979 | switch (c) { | - |
980 | case '\r': | - |
981 | if ((c = filterCarriageReturn()) == 0) | - |
982 | break; | - |
983 | // fall through | - |
984 | case '\n': | - |
985 | ++lineNumber; | - |
986 | lastLineStart = characterOffset + readBufferPos; | - |
987 | // fall through | - |
988 | case '\t': | - |
989 | textBuffer += QChar(c); | - |
990 | continue; | - |
991 | default: | - |
992 | if (c < 0x20 || (c > 0xFFFD && c < 0x10000) || c > QChar::LastValidCodePoint ) { | - |
993 | raiseWellFormedError(QXmlStream::tr("Invalid XML character.")); | - |
994 | lineNumber = oldLineNumber; | - |
995 | return false; | - |
996 | } | - |
997 | textBuffer += QChar(c); | - |
998 | } | - |
999 | | - |
1000 | | - |
1001 | /* Second, attempt to lookup str. */ | - |
1002 | if (c == uint(*str)) { | - |
1003 | if (!*(str + 1)) { | - |
1004 | if (tokenToInject >= 0) | - |
1005 | injectToken(tokenToInject); | - |
1006 | return true; | - |
1007 | } else { | - |
1008 | if (scanString(str + 1, tokenToInject, false)) | - |
1009 | return true; | - |
1010 | } | - |
1011 | } | - |
1012 | } | - |
1013 | putString(textBuffer, pos); | - |
1014 | textBuffer.resize(pos); | - |
1015 | lineNumber = oldLineNumber; | - |
1016 | return false; | - |
1017 | } | - |
1018 | | - |
1019 | bool QXmlStreamReaderPrivate::scanString(const char *str, short tokenToInject, bool requireSpace) | - |
1020 | { | - |
1021 | int n = 0; | - |
1022 | while (str[n]) { | - |
1023 | ushort c = getChar(); | - |
1024 | if (c != ushort(str[n])) { | - |
1025 | if (c) | - |
1026 | putChar(c); | - |
1027 | while (n--) { | - |
1028 | putChar(ushort(str[n])); | - |
1029 | } | - |
1030 | return false; | - |
1031 | } | - |
1032 | ++n; | - |
1033 | } | - |
1034 | for (int i = 0; i < n; ++i) | - |
1035 | textBuffer += QChar(ushort(str[i])); | - |
1036 | if (requireSpace) { | - |
1037 | int s = fastScanSpace(); | - |
1038 | if (!s || atEnd) { | - |
1039 | int pos = textBuffer.size() - n - s; | - |
1040 | putString(textBuffer, pos); | - |
1041 | textBuffer.resize(pos); | - |
1042 | return false; | - |
1043 | } | - |
1044 | } | - |
1045 | if (tokenToInject >= 0) | - |
1046 | injectToken(tokenToInject); | - |
1047 | return true; | - |
1048 | } | - |
1049 | | - |
1050 | bool QXmlStreamReaderPrivate::scanAfterLangleBang() | - |
1051 | { | - |
1052 | switch (peekChar()) { | - |
1053 | case '[': | - |
1054 | return scanString(spell[CDATA_START], CDATA_START, false); | - |
1055 | case 'D': | - |
1056 | return scanString(spell[DOCTYPE], DOCTYPE); | - |
1057 | case 'A': | - |
1058 | return scanString(spell[ATTLIST], ATTLIST); | - |
1059 | case 'N': | - |
1060 | return scanString(spell[NOTATION], NOTATION); | - |
1061 | case 'E': | - |
1062 | if (scanString(spell[ELEMENT], ELEMENT)) | - |
1063 | return true; | - |
1064 | return scanString(spell[ENTITY], ENTITY); | - |
1065 | | - |
1066 | default: | - |
1067 | ; | - |
1068 | }; | - |
1069 | return false; | - |
1070 | } | - |
1071 | | - |
1072 | bool QXmlStreamReaderPrivate::scanPublicOrSystem() | - |
1073 | { | - |
1074 | switch (peekChar()) { | - |
1075 | case 'S': | - |
1076 | return scanString(spell[SYSTEM], SYSTEM); | - |
1077 | case 'P': | - |
1078 | return scanString(spell[PUBLIC], PUBLIC); | - |
1079 | default: | - |
1080 | ; | - |
1081 | } | - |
1082 | return false; | - |
1083 | } | - |
1084 | | - |
1085 | bool QXmlStreamReaderPrivate::scanNData() | - |
1086 | { | - |
1087 | if (fastScanSpace()) { | - |
1088 | if (scanString(spell[NDATA], NDATA)) | - |
1089 | return true; | - |
1090 | putChar(' '); | - |
1091 | } | - |
1092 | return false; | - |
1093 | } | - |
1094 | | - |
1095 | bool QXmlStreamReaderPrivate::scanAfterDefaultDecl() | - |
1096 | { | - |
1097 | switch (peekChar()) { | - |
1098 | case 'R': | - |
1099 | return scanString(spell[REQUIRED], REQUIRED, false); | - |
1100 | case 'I': | - |
1101 | return scanString(spell[IMPLIED], IMPLIED, false); | - |
1102 | case 'F': | - |
1103 | return scanString(spell[FIXED], FIXED, false); | - |
1104 | default: | - |
1105 | ; | - |
1106 | } | - |
1107 | return false; | - |
1108 | } | - |
1109 | | - |
1110 | bool QXmlStreamReaderPrivate::scanAttType() | - |
1111 | { | - |
1112 | switch (peekChar()) { | - |
1113 | case 'C': | - |
1114 | return scanString(spell[CDATA], CDATA); | - |
1115 | case 'I': | - |
1116 | if (scanString(spell[ID], ID)) | - |
1117 | return true; | - |
1118 | if (scanString(spell[IDREF], IDREF)) | - |
1119 | return true; | - |
1120 | return scanString(spell[IDREFS], IDREFS); | - |
1121 | case 'E': | - |
1122 | if (scanString(spell[ENTITY], ENTITY)) | - |
1123 | return true; | - |
1124 | return scanString(spell[ENTITIES], ENTITIES); | - |
1125 | case 'N': | - |
1126 | if (scanString(spell[NOTATION], NOTATION)) | - |
1127 | return true; | - |
1128 | if (scanString(spell[NMTOKEN], NMTOKEN)) | - |
1129 | return true; | - |
1130 | return scanString(spell[NMTOKENS], NMTOKENS); | - |
1131 | default: | - |
1132 | ; | - |
1133 | } | - |
1134 | return false; | - |
1135 | } | - |
1136 | | - |
1137 | /*! | - |
1138 | \internal | - |
1139 | | - |
1140 | Scan strings with quotes or apostrophes surround them. For instance, | - |
1141 | attributes, the version and encoding field in the XML prolog and | - |
1142 | entity declarations. | - |
1143 | | - |
1144 | If normalizeLiterals is set to true, the function also normalizes | - |
1145 | whitespace. It is set to true when the first start tag is | - |
1146 | encountered. | - |
1147 | | - |
1148 | */ | - |
1149 | inline int QXmlStreamReaderPrivate::fastScanLiteralContent() | - |
1150 | { | - |
1151 | int n = 0; | - |
1152 | uint c; | - |
1153 | while ((c = getChar())) { | - |
1154 | switch (ushort(c)) { | - |
1155 | case 0xfffe: | - |
1156 | case 0xffff: | - |
1157 | case 0: | - |
1158 | /* The putChar() call is necessary so the parser re-gets | - |
1159 | * the character from the input source, when raising an error. */ | - |
1160 | putChar(c); | - |
1161 | return n; | - |
1162 | case '\r': | - |
1163 | if (filterCarriageReturn() == 0) | - |
1164 | return n; | - |
1165 | // fall through | - |
1166 | case '\n': | - |
1167 | ++lineNumber; | - |
1168 | lastLineStart = characterOffset + readBufferPos; | - |
1169 | // fall through | - |
1170 | case ' ': | - |
1171 | case '\t': | - |
1172 | if (normalizeLiterals) | - |
1173 | textBuffer += QLatin1Char(' '); | - |
1174 | else | - |
1175 | textBuffer += QChar(c); | - |
1176 | ++n; | - |
1177 | break; | - |
1178 | case '&': | - |
1179 | case '<': | - |
1180 | case '\"': | - |
1181 | case '\'': | - |
1182 | if (!(c & 0xff0000)) { | - |
1183 | putChar(c); | - |
1184 | return n; | - |
1185 | } | - |
1186 | // fall through | - |
1187 | default: | - |
1188 | textBuffer += QChar(c); | - |
1189 | ++n; | - |
1190 | } | - |
1191 | } | - |
1192 | return n; | - |
1193 | } | - |
1194 | | - |
1195 | inline int QXmlStreamReaderPrivate::fastScanSpace() | - |
1196 | { | - |
1197 | int n = 0; | - |
1198 | ushort c; | - |
1199 | while ((c = getChar())) { | - |
1200 | switch (c) { | - |
1201 | case '\r': | - |
1202 | if ((c = filterCarriageReturn()) == 0) | - |
1203 | return n; | - |
1204 | // fall through | - |
1205 | case '\n': | - |
1206 | ++lineNumber; | - |
1207 | lastLineStart = characterOffset + readBufferPos; | - |
1208 | // fall through | - |
1209 | case ' ': | - |
1210 | case '\t': | - |
1211 | textBuffer += QChar(c); | - |
1212 | ++n; | - |
1213 | break; | - |
1214 | default: | - |
1215 | putChar(c); | - |
1216 | return n; | - |
1217 | } | - |
1218 | } | - |
1219 | return n; | - |
1220 | } | - |
1221 | | - |
1222 | /*! | - |
1223 | \internal | - |
1224 | | - |
1225 | Used for text nodes essentially. That is, characters appearing | - |
1226 | inside elements. | - |
1227 | */ | - |
1228 | inline int QXmlStreamReaderPrivate::fastScanContentCharList() | - |
1229 | { | - |
1230 | int n = 0; | - |
1231 | uint c; | - |
1232 | while ((c = getChar())) { | - |
1233 | switch (ushort(c)) { | - |
1234 | case 0xfffe: | - |
1235 | case 0xffff: | - |
1236 | case 0: | - |
1237 | putChar(c); | - |
1238 | return n; | - |
1239 | case ']': { | - |
1240 | isWhitespace = false; | - |
1241 | int pos = textBuffer.size(); | - |
1242 | textBuffer += QChar(ushort(c)); | - |
1243 | ++n; | - |
1244 | while ((c = getChar()) == ']') { | - |
1245 | textBuffer += QChar(ushort(c)); | - |
1246 | ++n; | - |
1247 | } | - |
1248 | if (c == 0) { | - |
1249 | putString(textBuffer, pos); | - |
1250 | textBuffer.resize(pos); | - |
1251 | } else if (c == '>' && textBuffer.at(textBuffer.size()-2) == QLatin1Char(']')) { | - |
1252 | raiseWellFormedError(QXmlStream::tr("Sequence ']]>' not allowed in content.")); | - |
1253 | } else { | - |
1254 | putChar(c); | - |
1255 | break; | - |
1256 | } | - |
1257 | return n; | - |
1258 | } break; | - |
1259 | case '\r': | - |
1260 | if ((c = filterCarriageReturn()) == 0) | - |
1261 | return n; | - |
1262 | // fall through | - |
1263 | case '\n': | - |
1264 | ++lineNumber; | - |
1265 | lastLineStart = characterOffset + readBufferPos; | - |
1266 | // fall through | - |
1267 | case ' ': | - |
1268 | case '\t': | - |
1269 | textBuffer += QChar(ushort(c)); | - |
1270 | ++n; | - |
1271 | break; | - |
1272 | case '&': | - |
1273 | case '<': | - |
1274 | if (!(c & 0xff0000)) { | - |
1275 | putChar(c); | - |
1276 | return n; | - |
1277 | } | - |
1278 | // fall through | - |
1279 | default: | - |
1280 | if (c < 0x20) { | - |
1281 | putChar(c); | - |
1282 | return n; | - |
1283 | } | - |
1284 | isWhitespace = false; | - |
1285 | textBuffer += QChar(ushort(c)); | - |
1286 | ++n; | - |
1287 | } | - |
1288 | } | - |
1289 | return n; | - |
1290 | } | - |
1291 | | - |
1292 | inline int QXmlStreamReaderPrivate::fastScanName(int *prefix) | - |
1293 | { | - |
1294 | int n = 0; | - |
1295 | ushort c; | - |
1296 | while ((c = getChar())) { | - |
1297 | switch (c) { | - |
1298 | case '\n': | - |
1299 | case ' ': | - |
1300 | case '\t': | - |
1301 | case '\r': | - |
1302 | case '&': | - |
1303 | case '#': | - |
1304 | case '\'': | - |
1305 | case '\"': | - |
1306 | case '<': | - |
1307 | case '>': | - |
1308 | case '[': | - |
1309 | case ']': | - |
1310 | case '=': | - |
1311 | case '%': | - |
1312 | case '/': | - |
1313 | case ';': | - |
1314 | case '?': | - |
1315 | case '!': | - |
1316 | case '^': | - |
1317 | case '|': | - |
1318 | case ',': | - |
1319 | case '(': | - |
1320 | case ')': | - |
1321 | case '+': | - |
1322 | case '*': | - |
1323 | putChar(c); | - |
1324 | if (prefix && *prefix == n+1) { | - |
1325 | *prefix = 0; | - |
1326 | putChar(':'); | - |
1327 | --n; | - |
1328 | } | - |
1329 | return n; | - |
1330 | case ':': | - |
1331 | if (prefix) { | - |
1332 | if (*prefix == 0) { | - |
1333 | *prefix = n+2; | - |
1334 | } else { // only one colon allowed according to the namespace spec. | - |
1335 | putChar(c); | - |
1336 | return n; | - |
1337 | } | - |
1338 | } else { | - |
1339 | putChar(c); | - |
1340 | return n; | - |
1341 | } | - |
1342 | // fall through | - |
1343 | default: | - |
1344 | textBuffer += QChar(c); | - |
1345 | ++n; | - |
1346 | } | - |
1347 | } | - |
1348 | | - |
1349 | if (prefix) | - |
1350 | *prefix = 0; | - |
1351 | int pos = textBuffer.size() - n; | - |
1352 | putString(textBuffer, pos); | - |
1353 | textBuffer.resize(pos); | - |
1354 | return 0; | - |
1355 | } | - |
1356 | | - |
1357 | enum NameChar { NameBeginning, NameNotBeginning, NotName }; | - |
1358 | | - |
1359 | static const char Begi = static_cast<char>(NameBeginning); | - |
1360 | static const char NtBg = static_cast<char>(NameNotBeginning); | - |
1361 | static const char NotN = static_cast<char>(NotName); | - |
1362 | | - |
1363 | static const char nameCharTable[128] = | - |
1364 | { | - |
1365 | // 0x00 | - |
1366 | NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, | - |
1367 | NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, | - |
1368 | // 0x10 | - |
1369 | NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, | - |
1370 | NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, | - |
1371 | // 0x20 (0x2D is '-', 0x2E is '.') | - |
1372 | NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, | - |
1373 | NotN, NotN, NotN, NotN, NotN, NtBg, NtBg, NotN, | - |
1374 | // 0x30 (0x30..0x39 are '0'..'9', 0x3A is ':') | - |
1375 | NtBg, NtBg, NtBg, NtBg, NtBg, NtBg, NtBg, NtBg, | - |
1376 | NtBg, NtBg, Begi, NotN, NotN, NotN, NotN, NotN, | - |
1377 | // 0x40 (0x41..0x5A are 'A'..'Z') | - |
1378 | NotN, Begi, Begi, Begi, Begi, Begi, Begi, Begi, | - |
1379 | Begi, Begi, Begi, Begi, Begi, Begi, Begi, Begi, | - |
1380 | // 0x50 (0x5F is '_') | - |
1381 | Begi, Begi, Begi, Begi, Begi, Begi, Begi, Begi, | - |
1382 | Begi, Begi, Begi, NotN, NotN, NotN, NotN, Begi, | - |
1383 | // 0x60 (0x61..0x7A are 'a'..'z') | - |
1384 | NotN, Begi, Begi, Begi, Begi, Begi, Begi, Begi, | - |
1385 | Begi, Begi, Begi, Begi, Begi, Begi, Begi, Begi, | - |
1386 | // 0x70 | - |
1387 | Begi, Begi, Begi, Begi, Begi, Begi, Begi, Begi, | - |
1388 | Begi, Begi, Begi, NotN, NotN, NotN, NotN, NotN | - |
1389 | }; | - |
1390 | | - |
1391 | static inline NameChar fastDetermineNameChar(QChar ch) | - |
1392 | { | - |
1393 | ushort uc = ch.unicode(); | - |
1394 | if (!(uc & ~0x7f)) // uc < 128 | - |
1395 | return static_cast<NameChar>(nameCharTable[uc]); | - |
1396 | | - |
1397 | QChar::Category cat = ch.category(); | - |
1398 | // ### some these categories might be slightly wrong | - |
1399 | if ((cat >= QChar::Letter_Uppercase && cat <= QChar::Letter_Other) | - |
1400 | || cat == QChar::Number_Letter) | - |
1401 | return NameBeginning; | - |
1402 | if ((cat >= QChar::Number_DecimalDigit && cat <= QChar::Number_Other) | - |
1403 | || (cat >= QChar::Mark_NonSpacing && cat <= QChar::Mark_Enclosing)) | - |
1404 | return NameNotBeginning; | - |
1405 | return NotName; | - |
1406 | } | - |
1407 | | - |
1408 | inline int QXmlStreamReaderPrivate::fastScanNMTOKEN() | - |
1409 | { | - |
1410 | int n = 0; | - |
1411 | uint c; | - |
1412 | while ((c = getChar())) { | - |
1413 | if (fastDetermineNameChar(c) == NotName) { | - |
1414 | putChar(c); | - |
1415 | return n; | - |
1416 | } else { | - |
1417 | ++n; | - |
1418 | textBuffer += QChar(c); | - |
1419 | } | - |
1420 | } | - |
1421 | | - |
1422 | int pos = textBuffer.size() - n; | - |
1423 | putString(textBuffer, pos); | - |
1424 | textBuffer.resize(pos); | - |
1425 | | - |
1426 | return n; | - |
1427 | } | - |
1428 | | - |
1429 | void QXmlStreamReaderPrivate::putString(const QString &s, int from) | - |
1430 | { | - |
1431 | putStack.reserve(s.size()); | - |
1432 | for (int i = s.size()-1; i >= from; --i) | - |
1433 | putStack.rawPush() = s.at(i).unicode(); | - |
1434 | } | - |
1435 | | - |
1436 | void QXmlStreamReaderPrivate::putStringLiteral(const QString &s) | - |
1437 | { | - |
1438 | putStack.reserve(s.size()); | - |
1439 | for (int i = s.size()-1; i >= 0; --i) | - |
1440 | putStack.rawPush() = ((LETTER << 16) | s.at(i).unicode()); | - |
1441 | } | - |
1442 | | - |
1443 | void QXmlStreamReaderPrivate::putReplacement(const QString &s) | - |
1444 | { | - |
1445 | putStack.reserve(s.size()); | - |
1446 | for (int i = s.size()-1; i >= 0; --i) { | - |
1447 | ushort c = s.at(i).unicode(); | - |
1448 | if (c == '\n' || c == '\r') | - |
1449 | putStack.rawPush() = ((LETTER << 16) | c); | - |
1450 | else | - |
1451 | putStack.rawPush() = c; | - |
1452 | } | - |
1453 | } | - |
1454 | void QXmlStreamReaderPrivate::putReplacementInAttributeValue(const QString &s) | - |
1455 | { | - |
1456 | putStack.reserve(s.size()); | - |
1457 | for (int i = s.size()-1; i >= 0; --i) { | - |
1458 | ushort c = s.at(i).unicode(); | - |
1459 | if (c == '&' || c == ';') | - |
1460 | putStack.rawPush() = c; | - |
1461 | else if (c == '\n' || c == '\r') | - |
1462 | putStack.rawPush() = ' '; | - |
1463 | else | - |
1464 | putStack.rawPush() = ((LETTER << 16) | c); | - |
1465 | } | - |
1466 | } | - |
1467 | | - |
1468 | ushort QXmlStreamReaderPrivate::getChar_helper() | - |
1469 | { | - |
1470 | const int BUFFER_SIZE = 8192; | - |
1471 | characterOffset += readBufferPos; | - |
1472 | readBufferPos = 0; | - |
1473 | readBuffer.resize(0); | - |
1474 | #ifndef QT_NO_TEXTCODEC | - |
1475 | if (decoder) | - |
1476 | #endif | - |
1477 | nbytesread = 0; | - |
1478 | if (device) { | - |
1479 | rawReadBuffer.resize(BUFFER_SIZE); | - |
1480 | int nbytesreadOrMinus1 = device->read(rawReadBuffer.data() + nbytesread, BUFFER_SIZE - nbytesread); | - |
1481 | nbytesread += qMax(nbytesreadOrMinus1, 0); | - |
1482 | } else { | - |
1483 | if (nbytesread) | - |
1484 | rawReadBuffer += dataBuffer; | - |
1485 | else | - |
1486 | rawReadBuffer = dataBuffer; | - |
1487 | nbytesread = rawReadBuffer.size(); | - |
1488 | dataBuffer.clear(); | - |
1489 | } | - |
1490 | if (!nbytesread) { | - |
1491 | atEnd = true; | - |
1492 | return 0; | - |
1493 | } | - |
1494 | | - |
1495 | #ifndef QT_NO_TEXTCODEC | - |
1496 | if (!decoder) { | - |
1497 | if (nbytesread < 4) { // the 4 is to cover 0xef 0xbb 0xbf plus | - |
1498 | // one extra for the utf8 codec | - |
1499 | atEnd = true; | - |
1500 | return 0; | - |
1501 | } | - |
1502 | int mib = 106; // UTF-8 | - |
1503 | | - |
1504 | // look for byte order mark | - |
1505 | uchar ch1 = rawReadBuffer.at(0); | - |
1506 | uchar ch2 = rawReadBuffer.at(1); | - |
1507 | uchar ch3 = rawReadBuffer.at(2); | - |
1508 | uchar ch4 = rawReadBuffer.at(3); | - |
1509 | | - |
1510 | if ((ch1 == 0 && ch2 == 0 && ch3 == 0xfe && ch4 == 0xff) || | - |
1511 | (ch1 == 0xff && ch2 == 0xfe && ch3 == 0 && ch4 == 0)) | - |
1512 | mib = 1017; // UTF-32 with byte order mark | - |
1513 | else if (ch1 == 0x3c && ch2 == 0x00 && ch3 == 0x00 && ch4 == 0x00) | - |
1514 | mib = 1019; // UTF-32LE | - |
1515 | else if (ch1 == 0x00 && ch2 == 0x00 && ch3 == 0x00 && ch4 == 0x3c) | - |
1516 | mib = 1018; // UTF-32BE | - |
1517 | else if ((ch1 == 0xfe && ch2 == 0xff) || (ch1 == 0xff && ch2 == 0xfe)) | - |
1518 | mib = 1015; // UTF-16 with byte order mark | - |
1519 | else if (ch1 == 0x3c && ch2 == 0x00) | - |
1520 | mib = 1014; // UTF-16LE | - |
1521 | else if (ch1 == 0x00 && ch2 == 0x3c) | - |
1522 | mib = 1013; // UTF-16BE | - |
1523 | codec = QTextCodec::codecForMib(mib); | - |
1524 | Q_ASSERT(codec); | - |
1525 | decoder = codec->makeDecoder(); | - |
1526 | } | - |
1527 | | - |
1528 | decoder->toUnicode(&readBuffer, rawReadBuffer.constData(), nbytesread); | - |
1529 | | - |
1530 | if(lockEncoding && decoder->hasFailure()) { | - |
1531 | raiseWellFormedError(QXmlStream::tr("Encountered incorrectly encoded content.")); | - |
1532 | readBuffer.clear(); | - |
1533 | return 0; | - |
1534 | } | - |
1535 | #else | - |
1536 | readBuffer = QString::fromLatin1(rawReadBuffer.data(), nbytesread); | - |
1537 | #endif // QT_NO_TEXTCODEC | - |
1538 | | - |
1539 | readBuffer.reserve(1); // keep capacity when calling resize() next time | - |
1540 | | - |
1541 | if (readBufferPos < readBuffer.size()) { | - |
1542 | ushort c = readBuffer.at(readBufferPos++).unicode(); | - |
1543 | return c; | - |
1544 | } | - |
1545 | | - |
1546 | atEnd = true; | - |
1547 | return 0; | - |
1548 | } | - |
1549 | | - |
1550 | QStringRef QXmlStreamReaderPrivate::namespaceForPrefix(const QStringRef &prefix) | - |
1551 | { | - |
1552 | for (int j = namespaceDeclarations.size() - 1; j >= 0; --j) { | - |
1553 | const NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(j); | - |
1554 | if (namespaceDeclaration.prefix == prefix) { | - |
1555 | return namespaceDeclaration.namespaceUri; | - |
1556 | } | - |
1557 | } | - |
1558 | | - |
1559 | #if 1 | - |
1560 | if (namespaceProcessing && !prefix.isEmpty()) | - |
1561 | raiseWellFormedError(QXmlStream::tr("Namespace prefix '%1' not declared").arg(prefix.toString())); | - |
1562 | #endif | - |
1563 | | - |
1564 | return QStringRef(); | - |
1565 | } | - |
1566 | | - |
1567 | /* | - |
1568 | uses namespaceForPrefix and builds the attribute vector | - |
1569 | */ | - |
1570 | void QXmlStreamReaderPrivate::resolveTag() | - |
1571 | { | - |
1572 | int n = attributeStack.size(); | - |
1573 | | - |
1574 | if (namespaceProcessing) { | - |
1575 | for (int a = 0; a < dtdAttributes.size(); ++a) { | - |
1576 | DtdAttribute &dtdAttribute = dtdAttributes[a]; | - |
1577 | if (!dtdAttribute.isNamespaceAttribute | - |
1578 | || dtdAttribute.defaultValue.isNull() | - |
1579 | || dtdAttribute.tagName != qualifiedName | - |
1580 | || dtdAttribute.attributeQualifiedName.isNull()) | - |
1581 | continue; | - |
1582 | int i = 0; | - |
1583 | while (i < n && symName(attributeStack[i].key) != dtdAttribute.attributeQualifiedName) | - |
1584 | ++i; | - |
1585 | if (i != n) | - |
1586 | continue; | - |
1587 | if (dtdAttribute.attributePrefix.isEmpty() && dtdAttribute.attributeName == QLatin1String("xmlns")) { | - |
1588 | NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); | - |
1589 | namespaceDeclaration.prefix.clear(); | - |
1590 | | - |
1591 | const QStringRef ns(dtdAttribute.defaultValue); | - |
1592 | if(ns == QLatin1String("http://www.w3.org/2000/xmlns/") || | - |
1593 | ns == QLatin1String("http://www.w3.org/XML/1998/namespace")) | - |
1594 | raiseWellFormedError(QXmlStream::tr("Illegal namespace declaration.")); | - |
1595 | else | - |
1596 | namespaceDeclaration.namespaceUri = ns; | - |
1597 | } else if (dtdAttribute.attributePrefix == QLatin1String("xmlns")) { | - |
1598 | NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); | - |
1599 | QStringRef namespacePrefix = dtdAttribute.attributeName; | - |
1600 | QStringRef namespaceUri = dtdAttribute.defaultValue; | - |
1601 | if (((namespacePrefix == QLatin1String("xml")) | - |
1602 | ^ (namespaceUri == QLatin1String("http://www.w3.org/XML/1998/namespace"))) | - |
1603 | || namespaceUri == QLatin1String("http://www.w3.org/2000/xmlns/") | - |
1604 | || namespaceUri.isEmpty() | - |
1605 | || namespacePrefix == QLatin1String("xmlns")) | - |
1606 | raiseWellFormedError(QXmlStream::tr("Illegal namespace declaration.")); | - |
1607 | | - |
1608 | namespaceDeclaration.prefix = namespacePrefix; | - |
1609 | namespaceDeclaration.namespaceUri = namespaceUri; | - |
1610 | } | - |
1611 | } | - |
1612 | } | - |
1613 | | - |
1614 | tagStack.top().namespaceDeclaration.namespaceUri = namespaceUri = namespaceForPrefix(prefix); | - |
1615 | | - |
1616 | attributes.resize(n); | - |
1617 | | - |
1618 | for (int i = 0; i < n; ++i) { | - |
1619 | QXmlStreamAttribute &attribute = attributes[i]; | - |
1620 | Attribute &attrib = attributeStack[i]; | - |
1621 | QStringRef prefix(symPrefix(attrib.key)); | - |
1622 | QStringRef name(symString(attrib.key)); | - |
1623 | QStringRef qualifiedName(symName(attrib.key)); | - |
1624 | QStringRef value(symString(attrib.value)); | - |
1625 | | - |
1626 | attribute.m_name = QXmlStreamStringRef(name); | - |
1627 | attribute.m_qualifiedName = QXmlStreamStringRef(qualifiedName); | - |
1628 | attribute.m_value = QXmlStreamStringRef(value); | - |
1629 | | - |
1630 | if (!prefix.isEmpty()) { | - |
1631 | QStringRef attributeNamespaceUri = namespaceForPrefix(prefix); | - |
1632 | attribute.m_namespaceUri = QXmlStreamStringRef(attributeNamespaceUri); | - |
1633 | } | - |
1634 | | - |
1635 | for (int j = 0; j < i; ++j) { | - |
1636 | if (attributes[j].name() == attribute.name() | - |
1637 | && attributes[j].namespaceUri() == attribute.namespaceUri() | - |
1638 | && (namespaceProcessing || attributes[j].qualifiedName() == attribute.qualifiedName())) | - |
1639 | raiseWellFormedError(QXmlStream::tr("Attribute redefined.")); | - |
1640 | } | - |
1641 | } | - |
1642 | | - |
1643 | for (int a = 0; a < dtdAttributes.size(); ++a) { | - |
1644 | DtdAttribute &dtdAttribute = dtdAttributes[a]; | - |
1645 | if (dtdAttribute.isNamespaceAttribute | - |
1646 | || dtdAttribute.defaultValue.isNull() | - |
1647 | || dtdAttribute.tagName != qualifiedName | - |
1648 | || dtdAttribute.attributeQualifiedName.isNull()) | - |
1649 | continue; | - |
1650 | int i = 0; | - |
1651 | while (i < n && symName(attributeStack[i].key) != dtdAttribute.attributeQualifiedName) | - |
1652 | ++i; | - |
1653 | if (i != n) | - |
1654 | continue; | - |
1655 | | - |
1656 | | - |
1657 | | - |
1658 | QXmlStreamAttribute attribute; | - |
1659 | attribute.m_name = QXmlStreamStringRef(dtdAttribute.attributeName); | - |
1660 | attribute.m_qualifiedName = QXmlStreamStringRef(dtdAttribute.attributeQualifiedName); | - |
1661 | attribute.m_value = QXmlStreamStringRef(dtdAttribute.defaultValue); | - |
1662 | | - |
1663 | if (!dtdAttribute.attributePrefix.isEmpty()) { | - |
1664 | QStringRef attributeNamespaceUri = namespaceForPrefix(dtdAttribute.attributePrefix); | - |
1665 | attribute.m_namespaceUri = QXmlStreamStringRef(attributeNamespaceUri); | - |
1666 | } | - |
1667 | attribute.m_isDefault = true; | - |
1668 | attributes.append(attribute); | - |
1669 | } | - |
1670 | | - |
1671 | attributeStack.clear(); | - |
1672 | } | - |
1673 | | - |
1674 | void QXmlStreamReaderPrivate::resolvePublicNamespaces() | - |
1675 | { | - |
1676 | const Tag &tag = tagStack.top(); | - |
1677 | int n = namespaceDeclarations.size() - tag.namespaceDeclarationsSize; | - |
1678 | publicNamespaceDeclarations.resize(n); | - |
1679 | for (int i = 0; i < n; ++i) { | - |
1680 | const NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(tag.namespaceDeclarationsSize + i); | - |
1681 | QXmlStreamNamespaceDeclaration &publicNamespaceDeclaration = publicNamespaceDeclarations[i]; | - |
1682 | publicNamespaceDeclaration.m_prefix = QXmlStreamStringRef(namespaceDeclaration.prefix); | - |
1683 | publicNamespaceDeclaration.m_namespaceUri = QXmlStreamStringRef(namespaceDeclaration.namespaceUri); | - |
1684 | } | - |
1685 | } | - |
1686 | | - |
1687 | void QXmlStreamReaderPrivate::resolveDtd() | - |
1688 | { | - |
1689 | publicNotationDeclarations.resize(notationDeclarations.size()); | - |
1690 | for (int i = 0; i < notationDeclarations.size(); ++i) { | - |
1691 | const QXmlStreamReaderPrivate::NotationDeclaration ¬ationDeclaration = notationDeclarations.at(i); | - |
1692 | QXmlStreamNotationDeclaration &publicNotationDeclaration = publicNotationDeclarations[i]; | - |
1693 | publicNotationDeclaration.m_name = QXmlStreamStringRef(notationDeclaration.name); | - |
1694 | publicNotationDeclaration.m_systemId = QXmlStreamStringRef(notationDeclaration.systemId); | - |
1695 | publicNotationDeclaration.m_publicId = QXmlStreamStringRef(notationDeclaration.publicId); | - |
1696 | | - |
1697 | } | - |
1698 | notationDeclarations.clear(); | - |
1699 | publicEntityDeclarations.resize(entityDeclarations.size()); | - |
1700 | for (int i = 0; i < entityDeclarations.size(); ++i) { | - |
1701 | const QXmlStreamReaderPrivate::EntityDeclaration &entityDeclaration = entityDeclarations.at(i); | - |
1702 | QXmlStreamEntityDeclaration &publicEntityDeclaration = publicEntityDeclarations[i]; | - |
1703 | publicEntityDeclaration.m_name = QXmlStreamStringRef(entityDeclaration.name); | - |
1704 | publicEntityDeclaration.m_notationName = QXmlStreamStringRef(entityDeclaration.notationName); | - |
1705 | publicEntityDeclaration.m_systemId = QXmlStreamStringRef(entityDeclaration.systemId); | - |
1706 | publicEntityDeclaration.m_publicId = QXmlStreamStringRef(entityDeclaration.publicId); | - |
1707 | publicEntityDeclaration.m_value = QXmlStreamStringRef(entityDeclaration.value); | - |
1708 | } | - |
1709 | entityDeclarations.clear(); | - |
1710 | parameterEntityHash.clear(); | - |
1711 | } | - |
1712 | | - |
1713 | uint QXmlStreamReaderPrivate::resolveCharRef(int symbolIndex) | - |
1714 | { | - |
1715 | bool ok = true; | - |
1716 | uint s; | - |
1717 | // ### add toXShort to QStringRef? | - |
1718 | if (sym(symbolIndex).c == 'x') | - |
1719 | s = symString(symbolIndex, 1).toString().toUInt(&ok, 16); | - |
1720 | else | - |
1721 | s = symString(symbolIndex).toString().toUInt(&ok, 10); | - |
1722 | | - |
1723 | ok &= (s == 0x9 || s == 0xa || s == 0xd || (s >= 0x20 && s <= 0xd7ff) | - |
1724 | || (s >= 0xe000 && s <= 0xfffd) || (s >= 0x10000 && s <= QChar::LastValidCodePoint)); | - |
1725 | | - |
1726 | return ok ? s : 0; | - |
1727 | } | - |
1728 | | - |
1729 | | - |
1730 | void QXmlStreamReaderPrivate::checkPublicLiteral(const QStringRef &publicId) | - |
1731 | { | - |
1732 | //#x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%] | - |
1733 | | - |
1734 | const ushort *data = reinterpret_cast<const ushort *>(publicId.constData()); | - |
1735 | uchar c = 0; | - |
1736 | int i; | - |
1737 | for (i = publicId.size() - 1; i >= 0; --i) { | - |
1738 | if (data[i] < 256) | - |
1739 | switch ((c = data[i])) { | - |
1740 | case ' ': case '\n': case '\r': case '-': case '(': case ')': | - |
1741 | case '+': case ',': case '.': case '/': case ':': case '=': | - |
1742 | case '?': case ';': case '!': case '*': case '#': case '@': | - |
1743 | case '$': case '_': case '%': case '\'': case '\"': | - |
1744 | continue; | - |
1745 | default: | - |
1746 | if ((c >= 'a' && c <= 'z') | - |
1747 | || (c >= 'A' && c <= 'Z') | - |
1748 | || (c >= '0' && c <= '9')) | - |
1749 | continue; | - |
1750 | } | - |
1751 | break; | - |
1752 | } | - |
1753 | if (i >= 0) | - |
1754 | raiseWellFormedError(QXmlStream::tr("Unexpected character '%1' in public id literal.").arg(QChar(QLatin1Char(c)))); | - |
1755 | } | - |
1756 | | - |
1757 | /* | - |
1758 | Checks whether the document starts with an xml declaration. If it | - |
1759 | does, this function returns true; otherwise it sets up everything | - |
1760 | for a synthetic start document event and returns false. | - |
1761 | */ | - |
1762 | bool QXmlStreamReaderPrivate::checkStartDocument() | - |
1763 | { | - |
1764 | hasCheckedStartDocument = true; | - |
1765 | | - |
1766 | if (scanString(spell[XML], XML)) | - |
1767 | return true; | - |
1768 | | - |
1769 | type = QXmlStreamReader::StartDocument; | - |
1770 | if (atEnd) { | - |
1771 | hasCheckedStartDocument = false; | - |
1772 | raiseError(QXmlStreamReader::PrematureEndOfDocumentError); | - |
1773 | } | - |
1774 | return false; | - |
1775 | } | - |
1776 | | - |
1777 | void QXmlStreamReaderPrivate::startDocument() | - |
1778 | { | - |
1779 | QString err; | - |
1780 | if (documentVersion != QLatin1String("1.0")) { | - |
1781 | if (documentVersion.toString().contains(QLatin1Char(' '))) | - |
1782 | err = QXmlStream::tr("Invalid XML version string."); | - |
1783 | else | - |
1784 | err = QXmlStream::tr("Unsupported XML version."); | - |
1785 | } | - |
1786 | int n = attributeStack.size(); | - |
1787 | | - |
1788 | /* We use this bool to ensure that the pesudo attributes are in the | - |
1789 | * proper order: | - |
1790 | * | - |
1791 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' */ | - |
1792 | bool hasStandalone = false; | - |
1793 | | - |
1794 | for (int i = 0; err.isNull() && i < n; ++i) { | - |
1795 | Attribute &attrib = attributeStack[i]; | - |
1796 | QStringRef prefix(symPrefix(attrib.key)); | - |
1797 | QStringRef key(symString(attrib.key)); | - |
1798 | QStringRef value(symString(attrib.value)); | - |
1799 | | - |
1800 | if (prefix.isEmpty() && key == QLatin1String("encoding")) { | - |
1801 | const QString name(value.toString()); | - |
1802 | documentEncoding = value; | - |
1803 | | - |
1804 | if(hasStandalone) | - |
1805 | err = QXmlStream::tr("The standalone pseudo attribute must appear after the encoding."); | - |
1806 | if(!QXmlUtils::isEncName(name)) | - |
1807 | err = QXmlStream::tr("%1 is an invalid encoding name.").arg(name); | - |
1808 | else { | - |
1809 | #ifdef QT_NO_TEXTCODEC | - |
1810 | readBuffer = QString::fromLatin1(rawReadBuffer.data(), nbytesread); | - |
1811 | #else | - |
1812 | QTextCodec *const newCodec = QTextCodec::codecForName(name.toLatin1()); | - |
1813 | if (!newCodec) | - |
1814 | err = QXmlStream::tr("Encoding %1 is unsupported").arg(name); | - |
1815 | else if (newCodec != codec && !lockEncoding) { | - |
1816 | codec = newCodec; | - |
1817 | delete decoder; | - |
1818 | decoder = codec->makeDecoder(); | - |
1819 | decoder->toUnicode(&readBuffer, rawReadBuffer.data(), nbytesread); | - |
1820 | } | - |
1821 | #endif // QT_NO_TEXTCODEC | - |
1822 | } | - |
1823 | } else if (prefix.isEmpty() && key == QLatin1String("standalone")) { | - |
1824 | hasStandalone = true; | - |
1825 | if (value == QLatin1String("yes")) | - |
1826 | standalone = true; | - |
1827 | else if (value == QLatin1String("no")) | - |
1828 | standalone = false; | - |
1829 | else | - |
1830 | err = QXmlStream::tr("Standalone accepts only yes or no."); | - |
1831 | } else { | - |
1832 | err = QXmlStream::tr("Invalid attribute in XML declaration."); | - |
1833 | } | - |
1834 | } | - |
1835 | | - |
1836 | if (!err.isNull()) | - |
1837 | raiseWellFormedError(err); | - |
1838 | attributeStack.clear(); | - |
1839 | } | - |
1840 | | - |
1841 | | - |
1842 | void QXmlStreamReaderPrivate::raiseError(QXmlStreamReader::Error error, const QString& message) | - |
1843 | { | - |
1844 | this->error = error; | - |
1845 | errorString = message; | - |
1846 | if (errorString.isNull()) { | - |
1847 | if (error == QXmlStreamReader::PrematureEndOfDocumentError) | - |
1848 | errorString = QXmlStream::tr("Premature end of document."); | - |
1849 | else if (error == QXmlStreamReader::CustomError) | - |
1850 | errorString = QXmlStream::tr("Invalid document."); | - |
1851 | } | - |
1852 | | - |
1853 | type = QXmlStreamReader::Invalid; | - |
1854 | } | - |
1855 | | - |
1856 | void QXmlStreamReaderPrivate::raiseWellFormedError(const QString &message) | - |
1857 | { | - |
1858 | raiseError(QXmlStreamReader::NotWellFormedError, message); | - |
1859 | } | - |
1860 | | - |
1861 | void QXmlStreamReaderPrivate::parseError() | - |
1862 | { | - |
1863 | | - |
1864 | if (token == EOF_SYMBOL) { | - |
1865 | raiseError(QXmlStreamReader::PrematureEndOfDocumentError); | - |
1866 | return; | - |
1867 | } | - |
1868 | const int nmax = 4; | - |
1869 | QString error_message; | - |
1870 | int ers = state_stack[tos]; | - |
1871 | int nexpected = 0; | - |
1872 | int expected[nmax]; | - |
1873 | if (token != ERROR) | - |
1874 | for (int tk = 0; tk < TERMINAL_COUNT; ++tk) { | - |
1875 | int k = t_action(ers, tk); | - |
1876 | if (k <= 0) | - |
1877 | continue; | - |
1878 | if (spell[tk]) { | - |
1879 | if (nexpected < nmax) | - |
1880 | expected[nexpected++] = tk; | - |
1881 | } | - |
1882 | } | - |
1883 | | - |
1884 | error_message.clear (); | - |
1885 | if (nexpected && nexpected < nmax) { | - |
1886 | bool first = true; | - |
1887 | | - |
1888 | for (int s = 0; s < nexpected; ++s) { | - |
1889 | if (first) | - |
1890 | error_message += QXmlStream::tr ("Expected "); | - |
1891 | else if (s == nexpected - 1) | - |
1892 | error_message += QLatin1String (nexpected > 2 ? ", or " : " or "); | - |
1893 | else | - |
1894 | error_message += QLatin1String (", "); | - |
1895 | | - |
1896 | first = false; | - |
1897 | error_message += QLatin1String("\'"); | - |
1898 | error_message += QLatin1String (spell [expected[s]]); | - |
1899 | error_message += QLatin1String("\'"); | - |
1900 | } | - |
1901 | error_message += QXmlStream::tr(", but got \'"); | - |
1902 | error_message += QLatin1String(spell [token]); | - |
1903 | error_message += QLatin1String("\'"); | - |
1904 | } else { | - |
1905 | error_message += QXmlStream::tr("Unexpected \'"); | - |
1906 | error_message += QLatin1String(spell [token]); | - |
1907 | error_message += QLatin1String("\'"); | - |
1908 | } | - |
1909 | error_message += QLatin1Char('.'); | - |
1910 | | - |
1911 | raiseWellFormedError(error_message); | - |
1912 | } | - |
1913 | | - |
1914 | void QXmlStreamReaderPrivate::resume(int rule) { | - |
1915 | resumeReduction = rule; | - |
1916 | if (error == QXmlStreamReader::NoError) | - |
1917 | raiseError(QXmlStreamReader::PrematureEndOfDocumentError); | - |
1918 | } | - |
1919 | | - |
1920 | /*! Returns the current line number, starting with 1. | - |
1921 | | - |
1922 | \sa columnNumber(), characterOffset() | - |
1923 | */ | - |
1924 | qint64 QXmlStreamReader::lineNumber() const | - |
1925 | { | - |
1926 | Q_D(const QXmlStreamReader); | - |
1927 | return d->lineNumber + 1; // in public we start with 1 | - |
1928 | } | - |
1929 | | - |
1930 | /*! Returns the current column number, starting with 0. | - |
1931 | | - |
1932 | \sa lineNumber(), characterOffset() | - |
1933 | */ | - |
1934 | qint64 QXmlStreamReader::columnNumber() const | - |
1935 | { | - |
1936 | Q_D(const QXmlStreamReader); | - |
1937 | return d->characterOffset - d->lastLineStart + d->readBufferPos; | - |
1938 | } | - |
1939 | | - |
1940 | /*! Returns the current character offset, starting with 0. | - |
1941 | | - |
1942 | \sa lineNumber(), columnNumber() | - |
1943 | */ | - |
1944 | qint64 QXmlStreamReader::characterOffset() const | - |
1945 | { | - |
1946 | Q_D(const QXmlStreamReader); | - |
1947 | return d->characterOffset + d->readBufferPos; | - |
1948 | } | - |
1949 | | - |
1950 | | - |
1951 | /*! Returns the text of \l Characters, \l Comment, \l DTD, or | - |
1952 | EntityReference. | - |
1953 | */ | - |
1954 | QStringRef QXmlStreamReader::text() const | - |
1955 | { | - |
1956 | Q_D(const QXmlStreamReader); | - |
1957 | return d->text; | - |
1958 | } | - |
1959 | | - |
1960 | | - |
1961 | /*! If the state() is \l DTD, this function returns the DTD's | - |
| notation declarations. Otherwise an empty vector is returned. | |
| | |
| The QXmlStreamNotationDeclarations class is defined to be a QVector | |
| of QXmlStreamNotationDeclaration. | |
| */*! If the tokenType() is \l DTD, this function returns the DTD's | |
1962 | notation declarations. Otherwise an empty vector is returned. | - |
1963 | | - |
1964 | The QXmlStreamNotationDeclarations class is defined to be a QVector | - |
1965 | of QXmlStreamNotationDeclaration. | - |
1966 | */ | - |
1967 | QXmlStreamNotationDeclarations QXmlStreamReader::notationDeclarations() const | - |
1968 | { | - |
1969 | Q_D(const QXmlStreamReader); | - |
1970 | if (d->notationDeclarations.size()) | - |
1971 | const_cast<QXmlStreamReaderPrivate *>(d)->resolveDtd(); | - |
1972 | return d->publicNotationDeclarations; | - |
1973 | } | - |
1974 | | - |
1975 | | - |
1976 | /*! If the state() is \l DTD, this function returns the DTD's | - |
| unparsed (external) entity declarations. Otherwise an empty vector is returned. | |
| | |
| The QXmlStreamEntityDeclarations class is defined to be a QVector | |
| of QXmlStreamEntityDeclaration. | |
| */*! If the tokenType() is \l DTD, this function returns the DTD's | |
1977 | unparsed (external) entity declarations. Otherwise an empty vector is returned. | - |
1978 | | - |
1979 | The QXmlStreamEntityDeclarations class is defined to be a QVector | - |
1980 | of QXmlStreamEntityDeclaration. | - |
1981 | */ | - |
1982 | QXmlStreamEntityDeclarations QXmlStreamReader::entityDeclarations() const | - |
1983 | { | - |
1984 | Q_D(const QXmlStreamReader); | - |
1985 | if (d->entityDeclarations.size()) | - |
1986 | const_cast<QXmlStreamReaderPrivate *>(d)->resolveDtd(); | - |
1987 | return d->publicEntityDeclarations; | - |
1988 | } | - |
1989 | | - |
1990 | /*! | - |
| \since 4.4 | |
| | |
| If the state() is \l DTD, this function returns the DTD's | |
| name. Otherwise an empty string is returned. | |
| | |
| */*! | |
1991 | \since 4.4 | - |
1992 | | - |
1993 | If the tokenType() is \l DTD, this function returns the DTD's | - |
1994 | name. Otherwise an empty string is returned. | - |
1995 | | - |
1996 | */ | - |
1997 | QStringRef QXmlStreamReader::dtdName() const | - |
1998 | { | - |
1999 | Q_D(const QXmlStreamReader); | - |
2000 | if (d->type == QXmlStreamReader::DTD) | - |
2001 | return d->dtdName; | - |
2002 | return QStringRef(); | - |
2003 | } | - |
2004 | | - |
2005 | /*! | - |
| \since 4.4 | |
| | |
| If the state() is \l DTD, this function returns the DTD's | |
| public identifier. Otherwise an empty string is returned. | |
| | |
| */*! | |
2006 | \since 4.4 | - |
2007 | | - |
2008 | If the tokenType() is \l DTD, this function returns the DTD's | - |
2009 | public identifier. Otherwise an empty string is returned. | - |
2010 | | - |
2011 | */ | - |
2012 | QStringRef QXmlStreamReader::dtdPublicId() const | - |
2013 | { | - |
2014 | Q_D(const QXmlStreamReader); | - |
2015 | if (d->type == QXmlStreamReader::DTD) | - |
2016 | return d->dtdPublicId; | - |
2017 | return QStringRef(); | - |
2018 | } | - |
2019 | | - |
2020 | /*! | - |
| \since 4.4 | |
| | |
| If the state() is \l DTD, this function returns the DTD's | |
| system identifier. Otherwise an empty string is returned. | |
| | |
| */*! | |
2021 | \since 4.4 | - |
2022 | | - |
2023 | If the tokenType() is \l DTD, this function returns the DTD's | - |
2024 | system identifier. Otherwise an empty string is returned. | - |
2025 | | - |
2026 | */ | - |
2027 | QStringRef QXmlStreamReader::dtdSystemId() const | - |
2028 | { | - |
2029 | Q_D(const QXmlStreamReader); | - |
2030 | if (d->type == QXmlStreamReader::DTD) | - |
2031 | return d->dtdSystemId; | - |
2032 | return QStringRef(); | - |
2033 | } | - |
2034 | | - |
2035 | /*! If the state() is \l StartElement, this function returns the | - |
| element's namespace declarations. Otherwise an empty vector is | |
| returned. | |
| | |
| The QXmlStreamNamespaceDeclaration class is defined to be a QVector | |
| of QXmlStreamNamespaceDeclaration. | |
| | |
| \sa addExtraNamespaceDeclaration(), addExtraNamespaceDeclarations() | |
| */*! If the tokenType() is \l StartElement, this function returns the | |
2036 | element's namespace declarations. Otherwise an empty vector is | - |
2037 | returned. | - |
2038 | | - |
2039 | The QXmlStreamNamespaceDeclaration class is defined to be a QVector | - |
2040 | of QXmlStreamNamespaceDeclaration. | - |
2041 | | - |
2042 | \sa addExtraNamespaceDeclaration(), addExtraNamespaceDeclarations() | - |
2043 | */ | - |
2044 | QXmlStreamNamespaceDeclarations QXmlStreamReader::namespaceDeclarations() const | - |
2045 | { | - |
2046 | Q_D(const QXmlStreamReader); | - |
2047 | if (d->publicNamespaceDeclarations.isEmpty() && d->type == StartElement) | - |
2048 | const_cast<QXmlStreamReaderPrivate *>(d)->resolvePublicNamespaces(); | - |
2049 | return d->publicNamespaceDeclarations; | - |
2050 | } | - |
2051 | | - |
2052 | | - |
2053 | /*! | - |
2054 | \since 4.4 | - |
2055 | | - |
2056 | Adds an \a extraNamespaceDeclaration. The declaration will be | - |
2057 | valid for children of the current element, or - should the function | - |
2058 | be called before any elements are read - for the entire XML | - |
2059 | document. | - |
2060 | | - |
2061 | \sa namespaceDeclarations(), addExtraNamespaceDeclarations(), setNamespaceProcessing() | - |
2062 | */ | - |
2063 | void QXmlStreamReader::addExtraNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &extraNamespaceDeclaration) | - |
2064 | { | - |
2065 | Q_D(QXmlStreamReader); | - |
2066 | QXmlStreamReaderPrivate::NamespaceDeclaration &namespaceDeclaration = d->namespaceDeclarations.push(); | - |
2067 | namespaceDeclaration.prefix = d->addToStringStorage(extraNamespaceDeclaration.prefix()); | - |
2068 | namespaceDeclaration.namespaceUri = d->addToStringStorage(extraNamespaceDeclaration.namespaceUri()); | - |
2069 | } | - |
2070 | | - |
2071 | /*! | - |
2072 | \since 4.4 | - |
2073 | | - |
2074 | Adds a vector of declarations specified by \a extraNamespaceDeclarations. | - |
2075 | | - |
2076 | \sa namespaceDeclarations(), addExtraNamespaceDeclaration() | - |
2077 | */ | - |
2078 | void QXmlStreamReader::addExtraNamespaceDeclarations(const QXmlStreamNamespaceDeclarations &extraNamespaceDeclarations) | - |
2079 | { | - |
2080 | for (int i = 0; i < extraNamespaceDeclarations.size(); ++i) | - |
2081 | addExtraNamespaceDeclaration(extraNamespaceDeclarations.at(i)); | - |
2082 | } | - |
2083 | | - |
2084 | | - |
2085 | /*! Convenience function to be called in case a StartElement was | - |
2086 | read. Reads until the corresponding EndElement and returns all text | - |
2087 | in-between. In case of no error, the current token (see tokenType()) | - |
2088 | after having called this function is EndElement. | - |
2089 | | - |
2090 | The function concatenates text() when it reads either \l Characters | - |
2091 | or EntityReference tokens, but skips ProcessingInstruction and \l | - |
2092 | Comment. If the current token is not StartElement, an empty string is | - |
2093 | returned. | - |
2094 | | - |
2095 | The \a behaviour defines what happens in case anything else is | - |
2096 | read before reaching EndElement. The function can include the text from | - |
2097 | child elements (useful for example for HTML), ignore child elements, or | - |
2098 | raise an UnexpectedElementError and return what was read so far (default). | - |
2099 | | - |
2100 | \since 4.6 | - |
2101 | */ | - |
2102 | QString QXmlStreamReader::readElementText(ReadElementTextBehaviour behaviour) | - |
2103 | { | - |
2104 | Q_D(QXmlStreamReader); | - |
2105 | if (isStartElement()) { | - |
2106 | QString result; | - |
2107 | forever { | - |
2108 | switch (readNext()) { | - |
2109 | case Characters: | - |
2110 | case EntityReference: | - |
2111 | result.insert(result.size(), d->text.unicode(), d->text.size()); | - |
2112 | break; | - |
2113 | case EndElement: | - |
2114 | return result; | - |
2115 | case ProcessingInstruction: | - |
2116 | case Comment: | - |
2117 | break; | - |
2118 | case StartElement: | - |
2119 | if (behaviour == SkipChildElements) { | - |
2120 | skipCurrentElement(); | - |
2121 | break; | - |
2122 | } else if (behaviour == IncludeChildElements) { | - |
2123 | result += readElementText(behaviour); | - |
2124 | break; | - |
2125 | } | - |
2126 | // Fall through (for ErrorOnUnexpectedElement) | - |
2127 | default: | - |
2128 | if (d->error || behaviour == ErrorOnUnexpectedElement) { | - |
2129 | if (!d->error) | - |
2130 | d->raiseError(UnexpectedElementError, QXmlStream::tr("Expected character data.")); | - |
2131 | return result; | - |
2132 | } | - |
2133 | } | - |
2134 | } | - |
2135 | } | - |
2136 | return QString(); | - |
2137 | } | - |
2138 | | - |
2139 | /*! Raises a custom error with an optional error \a message. | - |
2140 | | - |
2141 | \sa error(), errorString() | - |
2142 | */ | - |
2143 | void QXmlStreamReader::raiseError(const QString& message) | - |
2144 | { | - |
2145 | Q_D(QXmlStreamReader); | - |
2146 | d->raiseError(CustomError, message); | - |
2147 | } | - |
2148 | | - |
2149 | /*! | - |
2150 | Returns the error message that was set with raiseError(). | - |
2151 | | - |
2152 | \sa error(), lineNumber(), columnNumber(), characterOffset() | - |
2153 | */ | - |
2154 | QString QXmlStreamReader::errorString() const | - |
2155 | { | - |
2156 | Q_D(const QXmlStreamReader); | - |
2157 | if (d->type == QXmlStreamReader::Invalid) | - |
2158 | return d->errorString; | - |
2159 | return QString(); | - |
2160 | } | - |
2161 | | - |
2162 | /*! Returns the type of the current error, or NoError if no error occurred. | - |
2163 | | - |
2164 | \sa errorString(), raiseError() | - |
2165 | */ | - |
2166 | QXmlStreamReader::Error QXmlStreamReader::error() const | - |
2167 | { | - |
2168 | Q_D(const QXmlStreamReader); | - |
2169 | if (d->type == QXmlStreamReader::Invalid) | - |
2170 | return d->error; | - |
2171 | return NoError; | - |
2172 | } | - |
2173 | | - |
2174 | /*! | - |
2175 | Returns the target of a ProcessingInstruction. | - |
2176 | */ | - |
2177 | QStringRef QXmlStreamReader::processingInstructionTarget() const | - |
2178 | { | - |
2179 | Q_D(const QXmlStreamReader); | - |
2180 | return d->processingInstructionTarget; | - |
2181 | } | - |
2182 | | - |
2183 | /*! | - |
2184 | Returns the data of a ProcessingInstruction. | - |
2185 | */ | - |
2186 | QStringRef QXmlStreamReader::processingInstructionData() const | - |
2187 | { | - |
2188 | Q_D(const QXmlStreamReader); | - |
2189 | return d->processingInstructionData; | - |
2190 | } | - |
2191 | | - |
2192 | | - |
2193 | | - |
2194 | /*! | - |
2195 | Returns the local name of a StartElement, EndElement, or an EntityReference. | - |
2196 | | - |
2197 | \sa namespaceUri(), qualifiedName() | - |
2198 | */ | - |
2199 | QStringRef QXmlStreamReader::name() const | - |
2200 | { | - |
2201 | Q_D(const QXmlStreamReader); | - |
2202 | return d->name; | - |
2203 | } | - |
2204 | | - |
2205 | /*! | - |
2206 | Returns the namespaceUri of a StartElement or EndElement. | - |
2207 | | - |
2208 | \sa name(), qualifiedName() | - |
2209 | */ | - |
2210 | QStringRef QXmlStreamReader::namespaceUri() const | - |
2211 | { | - |
2212 | Q_D(const QXmlStreamReader); | - |
2213 | return d->namespaceUri; | - |
2214 | } | - |
2215 | | - |
2216 | /*! | - |
2217 | Returns the qualified name of a StartElement or EndElement; | - |
2218 | | - |
2219 | A qualified name is the raw name of an element in the XML data. It | - |
2220 | consists of the namespace prefix, followed by colon, followed by the | - |
2221 | element's local name. Since the namespace prefix is not unique (the | - |
2222 | same prefix can point to different namespaces and different prefixes | - |
2223 | can point to the same namespace), you shouldn't use qualifiedName(), | - |
2224 | but the resolved namespaceUri() and the attribute's local name(). | - |
2225 | | - |
2226 | \sa name(), prefix(), namespaceUri() | - |
2227 | */ | - |
2228 | QStringRef QXmlStreamReader::qualifiedName() const | - |
2229 | { | - |
2230 | Q_D(const QXmlStreamReader); | - |
2231 | return d->qualifiedName; | - |
2232 | } | - |
2233 | | - |
2234 | | - |
2235 | | - |
2236 | /*! | - |
2237 | \since 4.4 | - |
2238 | | - |
2239 | Returns the prefix of a StartElement or EndElement. | - |
2240 | | - |
2241 | \sa name(), qualifiedName() | - |
2242 | */ | - |
2243 | QStringRef QXmlStreamReader::prefix() const | - |
2244 | { | - |
2245 | Q_D(const QXmlStreamReader); | - |
2246 | return d->prefix; | - |
2247 | } | - |
2248 | | - |
2249 | /*! | - |
2250 | Returns the attributes of a StartElement. | - |
2251 | */ | - |
2252 | QXmlStreamAttributes QXmlStreamReader::attributes() const | - |
2253 | { | - |
2254 | Q_D(const QXmlStreamReader); | - |
2255 | return d->attributes; | - |
2256 | } | - |
2257 | | - |
2258 | #endif // QT_NO_XMLSTREAMREADER | - |
2259 | | - |
2260 | /*! | - |
2261 | \class QXmlStreamAttribute | - |
2262 | \inmodule QtCore | - |
2263 | \since 4.3 | - |
2264 | \reentrant | - |
2265 | \brief The QXmlStreamAttribute class represents a single XML attribute | - |
2266 | | - |
2267 | \ingroup xml-tools | - |
2268 | | - |
2269 | An attribute consists of an optionally empty namespaceUri(), a | - |
2270 | name(), a value(), and an isDefault() attribute. | - |
2271 | | - |
2272 | The raw XML attribute name is returned as qualifiedName(). | - |
2273 | */ | - |
2274 | | - |
2275 | /*! | - |
2276 | Creates an empty attribute. | - |
2277 | */ | - |
2278 | QXmlStreamAttribute::QXmlStreamAttribute() | - |
2279 | { | - |
2280 | m_isDefault = false; | - |
2281 | } | - |
2282 | | - |
2283 | /*! | - |
2284 | Destructs an attribute. | - |
2285 | */ | - |
2286 | QXmlStreamAttribute::~QXmlStreamAttribute() | - |
2287 | { | - |
2288 | } | - |
2289 | | - |
2290 | /*! Constructs an attribute in the namespace described with \a | - |
2291 | namespaceUri with \a name and value \a value. | - |
2292 | */ | - |
2293 | QXmlStreamAttribute::QXmlStreamAttribute(const QString &namespaceUri, const QString &name, const QString &value) | - |
2294 | { | - |
2295 | m_namespaceUri = QXmlStreamStringRef(QStringRef(&namespaceUri)); | - |
2296 | m_name = m_qualifiedName = QXmlStreamStringRef(QStringRef(&name)); | - |
2297 | m_value = QXmlStreamStringRef(QStringRef(&value)); | - |
2298 | m_namespaceUri = QXmlStreamStringRef(QStringRef(&namespaceUri)); | - |
2299 | } | - |
2300 | | - |
2301 | /*! | - |
2302 | Constructs an attribute with qualified name \a qualifiedName and value \a value. | - |
2303 | */ | - |
2304 | QXmlStreamAttribute::QXmlStreamAttribute(const QString &qualifiedName, const QString &value) | - |
2305 | { | - |
2306 | int colon = qualifiedName.indexOf(QLatin1Char(':')); | - |
2307 | m_name = QXmlStreamStringRef(QStringRef(&qualifiedName, | - |
2308 | colon + 1, | - |
2309 | qualifiedName.size() - (colon + 1))); | - |
2310 | m_qualifiedName = QXmlStreamStringRef(QStringRef(&qualifiedName)); | - |
2311 | m_value = QXmlStreamStringRef(QStringRef(&value)); | - |
2312 | } | - |
2313 | | - |
2314 | /*! \fn QStringRef QXmlStreamAttribute::namespaceUri() const | - |
2315 | | - |
2316 | Returns the attribute's resolved namespaceUri, or an empty string | - |
2317 | reference if the attribute does not have a defined namespace. | - |
2318 | */ | - |
2319 | /*! \fn QStringRef QXmlStreamAttribute::name() const | - |
2320 | Returns the attribute's local name. | - |
2321 | */ | - |
2322 | /*! \fn QStringRef QXmlStreamAttribute::qualifiedName() const | - |
2323 | Returns the attribute's qualified name. | - |
2324 | | - |
2325 | A qualified name is the raw name of an attribute in the XML | - |
2326 | data. It consists of the namespace prefix(), followed by colon, | - |
2327 | followed by the attribute's local name(). Since the namespace prefix | - |
2328 | is not unique (the same prefix can point to different namespaces | - |
2329 | and different prefixes can point to the same namespace), you | - |
2330 | shouldn't use qualifiedName(), but the resolved namespaceUri() and | - |
2331 | the attribute's local name(). | - |
2332 | */ | - |
2333 | /*! | - |
2334 | \fn QStringRef QXmlStreamAttribute::prefix() const | - |
2335 | \since 4.4 | - |
2336 | Returns the attribute's namespace prefix. | - |
2337 | | - |
2338 | \sa name(), qualifiedName() | - |
2339 | | - |
2340 | */ | - |
2341 | | - |
2342 | /*! \fn QStringRef QXmlStreamAttribute::value() const | - |
2343 | Returns the attribute's value. | - |
2344 | */ | - |
2345 | | - |
2346 | /*! \fn bool QXmlStreamAttribute::isDefault() const | - |
2347 | | - |
2348 | Returns true if the parser added this attribute with a default | - |
2349 | value following an ATTLIST declaration in the DTD; otherwise | - |
2350 | returns false. | - |
2351 | */ | - |
2352 | /*! \fn bool QXmlStreamAttribute::operator==(const QXmlStreamAttribute &other) const | - |
2353 | | - |
2354 | Compares this attribute with \a other and returns true if they are | - |
2355 | equal; otherwise returns false. | - |
2356 | */ | - |
2357 | /*! \fn bool QXmlStreamAttribute::operator!=(const QXmlStreamAttribute &other) const | - |
2358 | | - |
2359 | Compares this attribute with \a other and returns true if they are | - |
2360 | not equal; otherwise returns false. | - |
2361 | */ | - |
2362 | | - |
2363 | | - |
2364 | /*! | - |
2365 | Creates a copy of \a other. | - |
2366 | */ | - |
2367 | QXmlStreamAttribute::QXmlStreamAttribute(const QXmlStreamAttribute &other) | - |
2368 | { | - |
2369 | *this = other; | - |
2370 | } | - |
2371 | | - |
2372 | /*! | - |
2373 | Assigns \a other to this attribute. | - |
2374 | */ | - |
2375 | QXmlStreamAttribute& QXmlStreamAttribute::operator=(const QXmlStreamAttribute &other) | - |
2376 | { | - |
2377 | m_name = other.m_name; | - |
2378 | m_namespaceUri = other.m_namespaceUri; | - |
2379 | m_qualifiedName = other.m_qualifiedName; | - |
2380 | m_value = other.m_value; | - |
2381 | m_isDefault = other.m_isDefault; | - |
2382 | return *this; | - |
2383 | } | - |
2384 | | - |
2385 | | - |
2386 | /*! | - |
2387 | \class QXmlStreamAttributes | - |
2388 | \inmodule QtCore | - |
2389 | \since 4.3 | - |
2390 | \reentrant | - |
2391 | \brief The QXmlStreamAttributes class represents a vector of QXmlStreamAttribute. | - |
2392 | | - |
2393 | Attributes are returned by a QXmlStreamReader in | - |
2394 | \l{QXmlStreamReader::attributes()} {attributes()} when the reader | - |
2395 | reports a \l {QXmlStreamReader::StartElement}{start element}. The | - |
2396 | class can also be used with a QXmlStreamWriter as an argument to | - |
2397 | \l {QXmlStreamWriter::writeAttributes()}{writeAttributes()}. | - |
2398 | | - |
2399 | The convenience function value() loops over the vector and returns | - |
2400 | an attribute value for a given namespaceUri and an attribute's | - |
2401 | name. | - |
2402 | | - |
2403 | New attributes can be added with append(). | - |
2404 | | - |
2405 | \ingroup xml-tools | - |
2406 | */ | - |
2407 | | - |
2408 | /*! | - |
2409 | \fn void QXmlStreamAttributes::append(const QXmlStreamAttribute &attribute) | - |
2410 | | - |
2411 | Appends the given \a attribute to the end of the vector. | - |
2412 | | - |
2413 | \sa QVector::append() | - |
2414 | */ | - |
2415 | | - |
2416 | | - |
2417 | /*! | - |
2418 | \typedef QXmlStreamNotationDeclarations | - |
2419 | \relates QXmlStreamNotationDeclaration | - |
2420 | | - |
2421 | Synonym for QVector<QXmlStreamNotationDeclaration>. | - |
2422 | */ | - |
2423 | | - |
2424 | | - |
2425 | /*! | - |
2426 | \class QXmlStreamNotationDeclaration | - |
2427 | \inmodule QtCore | - |
2428 | \since 4.3 | - |
2429 | \reentrant | - |
2430 | \brief The QXmlStreamNotationDeclaration class represents a DTD notation declaration. | - |
2431 | | - |
2432 | \ingroup xml-tools | - |
2433 | | - |
2434 | An notation declaration consists of a name(), a systemId(), and a publicId(). | - |
2435 | */ | - |
2436 | | - |
2437 | /*! | - |
2438 | Creates an empty notation declaration. | - |
2439 | */ | - |
2440 | QXmlStreamNotationDeclaration::QXmlStreamNotationDeclaration() | - |
2441 | { | - |
2442 | } | - |
2443 | /*! | - |
2444 | Creates a copy of \a other. | - |
2445 | */ | - |
2446 | QXmlStreamNotationDeclaration::QXmlStreamNotationDeclaration(const QXmlStreamNotationDeclaration &other) | - |
2447 | { | - |
2448 | *this = other; | - |
2449 | } | - |
2450 | | - |
2451 | /*! | - |
2452 | Assigns \a other to this notation declaration. | - |
2453 | */ | - |
2454 | QXmlStreamNotationDeclaration& QXmlStreamNotationDeclaration::operator=(const QXmlStreamNotationDeclaration &other) | - |
2455 | { | - |
2456 | m_name = other.m_name; | - |
2457 | m_systemId = other.m_systemId; | - |
2458 | m_publicId = other.m_publicId; | - |
2459 | return *this; | - |
2460 | } | - |
2461 | | - |
2462 | /*! | - |
2463 | Destructs this notation declaration. | - |
2464 | */ | - |
2465 | QXmlStreamNotationDeclaration::~QXmlStreamNotationDeclaration() | - |
2466 | { | - |
2467 | } | - |
2468 | | - |
2469 | /*! \fn QStringRef QXmlStreamNotationDeclaration::name() const | - |
2470 | | - |
2471 | Returns the notation name. | - |
2472 | */ | - |
2473 | /*! \fn QStringRef QXmlStreamNotationDeclaration::systemId() const | - |
2474 | | - |
2475 | Returns the system identifier. | - |
2476 | */ | - |
2477 | /*! \fn QStringRef QXmlStreamNotationDeclaration::publicId() const | - |
2478 | | - |
2479 | Returns the public identifier. | - |
2480 | */ | - |
2481 | | - |
2482 | /*! \fn inline bool QXmlStreamNotationDeclaration::operator==(const QXmlStreamNotationDeclaration &other) const | - |
2483 | | - |
2484 | Compares this notation declaration with \a other and returns true | - |
2485 | if they are equal; otherwise returns false. | - |
2486 | */ | - |
2487 | /*! \fn inline bool QXmlStreamNotationDeclaration::operator!=(const QXmlStreamNotationDeclaration &other) const | - |
2488 | | - |
2489 | Compares this notation declaration with \a other and returns true | - |
2490 | if they are not equal; otherwise returns false. | - |
2491 | */ | - |
2492 | | - |
2493 | /*! | - |
2494 | \typedef QXmlStreamNamespaceDeclarations | - |
2495 | \relates QXmlStreamNamespaceDeclaration | - |
2496 | | - |
2497 | Synonym for QVector<QXmlStreamNamespaceDeclaration>. | - |
2498 | */ | - |
2499 | | - |
2500 | /*! | - |
2501 | \class QXmlStreamNamespaceDeclaration | - |
2502 | \inmodule QtCore | - |
2503 | \since 4.3 | - |
2504 | \reentrant | - |
2505 | \brief The QXmlStreamNamespaceDeclaration class represents a namespace declaration. | - |
2506 | | - |
2507 | \ingroup xml-tools | - |
2508 | | - |
2509 | An namespace declaration consists of a prefix() and a namespaceUri(). | - |
2510 | */ | - |
2511 | /*! \fn inline bool QXmlStreamNamespaceDeclaration::operator==(const QXmlStreamNamespaceDeclaration &other) const | - |
2512 | | - |
2513 | Compares this namespace declaration with \a other and returns true | - |
2514 | if they are equal; otherwise returns false. | - |
2515 | */ | - |
2516 | /*! \fn inline bool QXmlStreamNamespaceDeclaration::operator!=(const QXmlStreamNamespaceDeclaration &other) const | - |
2517 | | - |
2518 | Compares this namespace declaration with \a other and returns true | - |
2519 | if they are not equal; otherwise returns false. | - |
2520 | */ | - |
2521 | | - |
2522 | /*! | - |
2523 | Creates an empty namespace declaration. | - |
2524 | */ | - |
2525 | QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration() | - |
2526 | { | - |
2527 | } | - |
2528 | | - |
2529 | /*! | - |
2530 | \since 4.4 | - |
2531 | | - |
2532 | Creates a namespace declaration with \a prefix and \a namespaceUri. | - |
2533 | */ | - |
2534 | QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri) | - |
2535 | { | - |
2536 | m_prefix = prefix; | - |
2537 | m_namespaceUri = namespaceUri; | - |
2538 | } | - |
2539 | | - |
2540 | /*! | - |
2541 | Creates a copy of \a other. | - |
2542 | */ | - |
2543 | QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &other) | - |
2544 | { | - |
2545 | *this = other; | - |
2546 | } | - |
2547 | | - |
2548 | /*! | - |
2549 | Assigns \a other to this namespace declaration. | - |
2550 | */ | - |
2551 | QXmlStreamNamespaceDeclaration& QXmlStreamNamespaceDeclaration::operator=(const QXmlStreamNamespaceDeclaration &other) | - |
2552 | { | - |
2553 | m_prefix = other.m_prefix; | - |
2554 | m_namespaceUri = other.m_namespaceUri; | - |
2555 | return *this; | - |
2556 | } | - |
2557 | /*! | - |
2558 | Destructs this namespace declaration. | - |
2559 | */ | - |
2560 | QXmlStreamNamespaceDeclaration::~QXmlStreamNamespaceDeclaration() | - |
2561 | { | - |
2562 | } | - |
2563 | | - |
2564 | /*! \fn QStringRef QXmlStreamNamespaceDeclaration::prefix() const | - |
2565 | | - |
2566 | Returns the prefix. | - |
2567 | */ | - |
2568 | /*! \fn QStringRef QXmlStreamNamespaceDeclaration::namespaceUri() const | - |
2569 | | - |
2570 | Returns the namespaceUri. | - |
2571 | */ | - |
2572 | | - |
2573 | | - |
2574 | | - |
2575 | | - |
2576 | /*! | - |
2577 | \typedef QXmlStreamEntityDeclarations | - |
2578 | \relates QXmlStreamEntityDeclaration | - |
2579 | | - |
2580 | Synonym for QVector<QXmlStreamEntityDeclaration>. | - |
2581 | */ | - |
2582 | | - |
2583 | /*! | - |
2584 | \class QXmlStreamStringRef | - |
2585 | \inmodule QtCore | - |
2586 | \since 4.3 | - |
2587 | \internal | - |
2588 | */ | - |
2589 | | - |
2590 | /*! | - |
2591 | \class QXmlStreamEntityDeclaration | - |
2592 | \inmodule QtCore | - |
2593 | \since 4.3 | - |
2594 | \reentrant | - |
2595 | \brief The QXmlStreamEntityDeclaration class represents a DTD entity declaration. | - |
2596 | | - |
2597 | \ingroup xml-tools | - |
2598 | | - |
2599 | An entity declaration consists of a name(), a notationName(), a | - |
2600 | systemId(), a publicId(), and a value(). | - |
2601 | */ | - |
2602 | | - |
2603 | /*! | - |
2604 | Creates an empty entity declaration. | - |
2605 | */ | - |
2606 | QXmlStreamEntityDeclaration::QXmlStreamEntityDeclaration() | - |
2607 | { | - |
2608 | } | - |
2609 | | - |
2610 | /*! | - |
2611 | Creates a copy of \a other. | - |
2612 | */ | - |
2613 | QXmlStreamEntityDeclaration::QXmlStreamEntityDeclaration(const QXmlStreamEntityDeclaration &other) | - |
2614 | { | - |
2615 | *this = other; | - |
2616 | } | - |
2617 | | - |
2618 | /*! | - |
2619 | Assigns \a other to this entity declaration. | - |
2620 | */ | - |
2621 | QXmlStreamEntityDeclaration& QXmlStreamEntityDeclaration::operator=(const QXmlStreamEntityDeclaration &other) | - |
2622 | { | - |
2623 | m_name = other.m_name; | - |
2624 | m_notationName = other.m_notationName; | - |
2625 | m_systemId = other.m_systemId; | - |
2626 | m_publicId = other.m_publicId; | - |
2627 | m_value = other.m_value; | - |
2628 | return *this; | - |
2629 | } | - |
2630 | | - |
2631 | /*! | - |
2632 | Destructs this entity declaration. | - |
2633 | */ | - |
2634 | QXmlStreamEntityDeclaration::~QXmlStreamEntityDeclaration() | - |
2635 | { | - |
2636 | } | - |
2637 | | - |
2638 | /*! \fn QStringRef QXmlStreamEntityDeclaration::name() const | - |
2639 | | - |
2640 | Returns the entity name. | - |
2641 | */ | - |
2642 | /*! \fn QStringRef QXmlStreamEntityDeclaration::notationName() const | - |
2643 | | - |
2644 | Returns the notation name. | - |
2645 | */ | - |
2646 | /*! \fn QStringRef QXmlStreamEntityDeclaration::systemId() const | - |
2647 | | - |
2648 | Returns the system identifier. | - |
2649 | */ | - |
2650 | /*! \fn QStringRef QXmlStreamEntityDeclaration::publicId() const | - |
2651 | | - |
2652 | Returns the public identifier. | - |
2653 | */ | - |
2654 | /*! \fn QStringRef QXmlStreamEntityDeclaration::value() const | - |
2655 | | - |
2656 | Returns the entity's value. | - |
2657 | */ | - |
2658 | | - |
2659 | /*! \fn bool QXmlStreamEntityDeclaration::operator==(const QXmlStreamEntityDeclaration &other) const | - |
2660 | | - |
2661 | Compares this entity declaration with \a other and returns true if | - |
2662 | they are equal; otherwise returns false. | - |
2663 | */ | - |
2664 | /*! \fn bool QXmlStreamEntityDeclaration::operator!=(const QXmlStreamEntityDeclaration &other) const | - |
2665 | | - |
2666 | Compares this entity declaration with \a other and returns true if | - |
2667 | they are not equal; otherwise returns false. | - |
2668 | */ | - |
2669 | | - |
2670 | /*! Returns the value of the attribute \a name in the namespace | - |
2671 | described with \a namespaceUri, or an empty string reference if the | - |
2672 | attribute is not defined. The \a namespaceUri can be empty. | - |
2673 | */ | - |
2674 | QStringRef QXmlStreamAttributes::value(const QString &namespaceUri, const QString &name) const | - |
2675 | { | - |
2676 | for (int i = 0; i < size(); ++i) { | - |
2677 | const QXmlStreamAttribute &attribute = at(i); | - |
2678 | if (attribute.name() == name && attribute.namespaceUri() == namespaceUri) | - |
2679 | return attribute.value(); | - |
2680 | } | - |
2681 | return QStringRef(); | - |
2682 | } | - |
2683 | | - |
2684 | /*!\overload | - |
2685 | Returns the value of the attribute \a name in the namespace | - |
2686 | described with \a namespaceUri, or an empty string reference if the | - |
2687 | attribute is not defined. The \a namespaceUri can be empty. | - |
2688 | */ | - |
2689 | QStringRef QXmlStreamAttributes::value(const QString &namespaceUri, QLatin1String name) const | - |
2690 | { | - |
2691 | for (int i = 0; i < size(); ++i) { | - |
2692 | const QXmlStreamAttribute &attribute = at(i); | - |
2693 | if (attribute.name() == name && attribute.namespaceUri() == namespaceUri) | - |
2694 | return attribute.value(); | - |
2695 | } | - |
2696 | return QStringRef(); | - |
2697 | } | - |
2698 | | - |
2699 | /*!\overload | - |
2700 | Returns the value of the attribute \a name in the namespace | - |
2701 | described with \a namespaceUri, or an empty string reference if the | - |
2702 | attribute is not defined. The \a namespaceUri can be empty. | - |
2703 | */ | - |
2704 | QStringRef QXmlStreamAttributes::value(QLatin1String namespaceUri, QLatin1String name) const | - |
2705 | { | - |
2706 | for (int i = 0; i < size(); ++i) { | - |
2707 | const QXmlStreamAttribute &attribute = at(i); | - |
2708 | if (attribute.name() == name && attribute.namespaceUri() == namespaceUri) | - |
2709 | return attribute.value(); | - |
2710 | } | - |
2711 | return QStringRef(); | - |
2712 | } | - |
2713 | | - |
2714 | /*!\overload | - |
2715 | | - |
2716 | Returns the value of the attribute with qualified name \a | - |
2717 | qualifiedName , or an empty string reference if the attribute is not | - |
2718 | defined. A qualified name is the raw name of an attribute in the XML | - |
2719 | data. It consists of the namespace prefix, followed by colon, | - |
2720 | followed by the attribute's local name. Since the namespace prefix | - |
2721 | is not unique (the same prefix can point to different namespaces and | - |
2722 | different prefixes can point to the same namespace), you shouldn't | - |
2723 | use qualified names, but a resolved namespaceUri and the attribute's | - |
2724 | local name. | - |
2725 | */ | - |
2726 | QStringRef QXmlStreamAttributes::value(const QString &qualifiedName) const | - |
2727 | { | - |
2728 | for (int i = 0; i < size(); ++i) { | - |
2729 | const QXmlStreamAttribute &attribute = at(i); | - |
2730 | if (attribute.qualifiedName() == qualifiedName) | - |
2731 | return attribute.value(); | - |
2732 | } | - |
2733 | return QStringRef(); | - |
2734 | } | - |
2735 | | - |
2736 | /*!\overload | - |
2737 | | - |
2738 | Returns the value of the attribute with qualified name \a | - |
2739 | qualifiedName , or an empty string reference if the attribute is not | - |
2740 | defined. A qualified name is the raw name of an attribute in the XML | - |
2741 | data. It consists of the namespace prefix, followed by colon, | - |
2742 | followed by the attribute's local name. Since the namespace prefix | - |
2743 | is not unique (the same prefix can point to different namespaces and | - |
2744 | different prefixes can point to the same namespace), you shouldn't | - |
2745 | use qualified names, but a resolved namespaceUri and the attribute's | - |
2746 | local name. | - |
2747 | */ | - |
2748 | QStringRef QXmlStreamAttributes::value(QLatin1String qualifiedName) const | - |
2749 | { | - |
2750 | for (int i = 0; i < size(); ++i) { | - |
2751 | const QXmlStreamAttribute &attribute = at(i); | - |
2752 | if (attribute.qualifiedName() == qualifiedName) | - |
2753 | return attribute.value(); | - |
2754 | } | - |
2755 | return QStringRef(); | - |
2756 | } | - |
2757 | | - |
2758 | /*!Appends a new attribute with \a name in the namespace | - |
2759 | described with \a namespaceUri, and value \a value. The \a | - |
2760 | namespaceUri can be empty. | - |
2761 | */ | - |
2762 | void QXmlStreamAttributes::append(const QString &namespaceUri, const QString &name, const QString &value) | - |
2763 | { | - |
2764 | append(QXmlStreamAttribute(namespaceUri, name, value)); | - |
2765 | } | - |
2766 | | - |
2767 | /*!\overload | - |
2768 | Appends a new attribute with qualified name \a qualifiedName and | - |
2769 | value \a value. | - |
2770 | */ | - |
2771 | void QXmlStreamAttributes::append(const QString &qualifiedName, const QString &value) | - |
2772 | { | - |
2773 | append(QXmlStreamAttribute(qualifiedName, value)); | - |
2774 | } | - |
2775 | | - |
2776 | #ifndef QT_NO_XMLSTREAMREADER | - |
2777 | | - |
2778 | /*! \fn bool QXmlStreamReader::isStartDocument() const | - |
2779 | Returns true if tokenType() equals \l StartDocument; otherwise returns false. | - |
2780 | */ | - |
2781 | /*! \fn bool QXmlStreamReader::isEndDocument() const | - |
2782 | Returns true if tokenType() equals \l EndDocument; otherwise returns false. | - |
2783 | */ | - |
2784 | /*! \fn bool QXmlStreamReader::isStartElement() const | - |
2785 | Returns true if tokenType() equals \l StartElement; otherwise returns false. | - |
2786 | */ | - |
2787 | /*! \fn bool QXmlStreamReader::isEndElement() const | - |
2788 | Returns true if tokenType() equals \l EndElement; otherwise returns false. | - |
2789 | */ | - |
2790 | /*! \fn bool QXmlStreamReader::isCharacters() const | - |
2791 | Returns true if tokenType() equals \l Characters; otherwise returns false. | - |
2792 | | - |
2793 | \sa isWhitespace(), isCDATA() | - |
2794 | */ | - |
2795 | /*! \fn bool QXmlStreamReader::isComment() const | - |
2796 | Returns true if tokenType() equals \l Comment; otherwise returns false. | - |
2797 | */ | - |
2798 | /*! \fn bool QXmlStreamReader::isDTD() const | - |
2799 | Returns true if tokenType() equals \l DTD; otherwise returns false. | - |
2800 | */ | - |
2801 | /*! \fn bool QXmlStreamReader::isEntityReference() const | - |
2802 | Returns true if tokenType() equals \l EntityReference; otherwise returns false. | - |
2803 | */ | - |
2804 | /*! \fn bool QXmlStreamReader::isProcessingInstruction() const | - |
2805 | Returns true if tokenType() equals \l ProcessingInstruction; otherwise returns false. | - |
2806 | */ | - |
2807 | | - |
2808 | /*! Returns true if the reader reports characters that only consist | - |
2809 | of white-space; otherwise returns false. | - |
2810 | | - |
2811 | \sa isCharacters(), text() | - |
2812 | */ | - |
2813 | bool QXmlStreamReader::isWhitespace() const | - |
2814 | { | - |
2815 | Q_D(const QXmlStreamReader); | - |
2816 | return d->type == QXmlStreamReader::Characters && d->isWhitespace; | - |
2817 | } | - |
2818 | | - |
2819 | /*! Returns true if the reader reports characters that stem from a | - |
2820 | CDATA section; otherwise returns false. | - |
2821 | | - |
2822 | \sa isCharacters(), text() | - |
2823 | */ | - |
2824 | bool QXmlStreamReader::isCDATA() const | - |
2825 | { | - |
2826 | Q_D(const QXmlStreamReader); | - |
2827 | return d->type == QXmlStreamReader::Characters && d->isCDATA; | - |
2828 | } | - |
2829 | | - |
2830 | | - |
2831 | | - |
2832 | /*! | - |
2833 | Returns true if this document has been declared standalone in the | - |
2834 | XML declaration; otherwise returns false. | - |
2835 | | - |
2836 | If no XML declaration has been parsed, this function returns false. | - |
2837 | */ | - |
2838 | bool QXmlStreamReader::isStandaloneDocument() const | - |
2839 | { | - |
2840 | Q_D(const QXmlStreamReader); | - |
2841 | return d->standalone; | - |
2842 | } | - |
2843 | | - |
2844 | | - |
2845 | /*! | - |
| \since 4.4 | |
| | |
| If the state() is \l StartDocument, this function returns the | |
| version string as specified in the XML declaration. | |
| Otherwise an empty string is returned. | |
| */*! | |
2846 | \since 4.4 | - |
2847 | | - |
2848 | If the tokenType() is \l StartDocument, this function returns the | - |
2849 | version string as specified in the XML declaration. | - |
2850 | Otherwise an empty string is returned. | - |
2851 | */ | - |
2852 | QStringRef QXmlStreamReader::documentVersion() const | - |
2853 | { | - |
2854 | Q_D(const QXmlStreamReader); | - |
2855 | if (d->type == QXmlStreamReader::StartDocument) | - |
2856 | return d->documentVersion; | - |
2857 | return QStringRef(); | - |
2858 | } | - |
2859 | | - |
2860 | /*! | - |
| \since 4.4 | |
| | |
| If the state() is \l StartDocument, this function returns the | |
| encoding string as specified in the XML declaration. | |
| Otherwise an empty string is returned. | |
| */*! | |
2861 | \since 4.4 | - |
2862 | | - |
2863 | If the tokenType() is \l StartDocument, this function returns the | - |
2864 | encoding string as specified in the XML declaration. | - |
2865 | Otherwise an empty string is returned. | - |
2866 | */ | - |
2867 | QStringRef QXmlStreamReader::documentEncoding() const | - |
2868 | { | - |
2869 | Q_D(const QXmlStreamReader); | - |
2870 | if (d->type == QXmlStreamReader::StartDocument) | - |
2871 | return d->documentEncoding; | - |
2872 | return QStringRef(); | - |
2873 | } | - |
2874 | | - |
2875 | #endif // QT_NO_XMLSTREAMREADER | - |
2876 | | - |
2877 | /*! | - |
2878 | \class QXmlStreamWriter | - |
2879 | \inmodule QtCore | - |
2880 | \since 4.3 | - |
2881 | \reentrant | - |
2882 | | - |
2883 | \brief The QXmlStreamWriter class provides an XML writer with a | - |
2884 | simple streaming API. | - |
2885 | | - |
2886 | \ingroup xml-tools | - |
2887 | | - |
2888 | QXmlStreamWriter is the counterpart to QXmlStreamReader for writing | - |
2889 | XML. Like its related class, it operates on a QIODevice specified | - |
2890 | with setDevice(). The API is simple and straightforward: for every | - |
2891 | XML token or event you want to write, the writer provides a | - |
2892 | specialized function. | - |
2893 | | - |
2894 | You start a document with writeStartDocument() and end it with | - |
2895 | writeEndDocument(). This will implicitly close all remaining open | - |
2896 | tags. | - |
2897 | | - |
2898 | Element tags are opened with writeStartElement() followed by | - |
2899 | writeAttribute() or writeAttributes(), element content, and then | - |
2900 | writeEndElement(). A shorter form writeEmptyElement() can be used | - |
2901 | to write empty elements, followed by writeAttributes(). | - |
2902 | | - |
2903 | Element content consists of either characters, entity references or | - |
2904 | nested elements. It is written with writeCharacters(), which also | - |
2905 | takes care of escaping all forbidden characters and character | - |
2906 | sequences, writeEntityReference(), or subsequent calls to | - |
2907 | writeStartElement(). A convenience method writeTextElement() can be | - |
2908 | used for writing terminal elements that contain nothing but text. | - |
2909 | | - |
2910 | The following abridged code snippet shows the basic use of the class | - |
2911 | to write formatted XML with indentation: | - |
2912 | | - |
2913 | \snippet qxmlstreamwriter/main.cpp start stream | - |
2914 | \dots | - |
2915 | \snippet qxmlstreamwriter/main.cpp write element | - |
2916 | \dots | - |
2917 | \snippet qxmlstreamwriter/main.cpp finish stream | - |
2918 | | - |
2919 | QXmlStreamWriter takes care of prefixing namespaces, all you have to | - |
2920 | do is specify the \c namespaceUri when writing elements or | - |
2921 | attributes. If you must conform to certain prefixes, you can force | - |
2922 | the writer to use them by declaring the namespaces manually with | - |
2923 | either writeNamespace() or writeDefaultNamespace(). Alternatively, | - |
2924 | you can bypass the stream writer's namespace support and use | - |
2925 | overloaded methods that take a qualified name instead. The namespace | - |
2926 | \e http://www.w3.org/XML/1998/namespace is implicit and mapped to the | - |
2927 | prefix \e xml. | - |
2928 | | - |
2929 | The stream writer can automatically format the generated XML data by | - |
2930 | adding line-breaks and indentation to empty sections between | - |
2931 | elements, making the XML data more readable for humans and easier to | - |
2932 | work with for most source code management systems. The feature can | - |
2933 | be turned on with the \l autoFormatting property, and customized | - |
2934 | with the \l autoFormattingIndent property. | - |
2935 | | - |
2936 | Other functions are writeCDATA(), writeComment(), | - |
2937 | writeProcessingInstruction(), and writeDTD(). Chaining of XML | - |
2938 | streams is supported with writeCurrentToken(). | - |
2939 | | - |
2940 | By default, QXmlStreamWriter encodes XML in UTF-8. Different | - |
2941 | encodings can be enforced using setCodec(). | - |
2942 | | - |
2943 | If an error occurs while writing to the underlying device, hasError() | - |
2944 | starts returning true and subsequent writes are ignored. | - |
2945 | | - |
2946 | The \l{QXmlStream Bookmarks Example} illustrates how to use a | - |
2947 | stream writer to write an XML bookmark file (XBEL) that | - |
2948 | was previously read in by a QXmlStreamReader. | - |
2949 | | - |
2950 | */ | - |
2951 | | - |
2952 | #ifndef QT_NO_XMLSTREAMWRITER | - |
2953 | | - |
2954 | class QXmlStreamWriterPrivate : public QXmlStreamPrivateTagStack { | - |
2955 | QXmlStreamWriter *q_ptr; | - |
2956 | Q_DECLARE_PUBLIC(QXmlStreamWriter) | - |
2957 | public: | - |
2958 | QXmlStreamWriterPrivate(QXmlStreamWriter *q); | - |
2959 | ~QXmlStreamWriterPrivate() { | - |
2960 | if (deleteDevice) | - |
2961 | delete device; | - |
2962 | #ifndef QT_NO_TEXTCODEC | - |
2963 | delete encoder; | - |
2964 | #endif | - |
2965 | } | - |
2966 | | - |
2967 | void write(const QStringRef &); | - |
2968 | void write(const QString &); | - |
2969 | void writeEscaped(const QString &, bool escapeWhitespace = false); | - |
2970 | void write(const char *s, int len); | - |
2971 | template <int N> void write(const char (&s)[N]) { write(s, N - 1); } | - |
2972 | bool finishStartElement(bool contents = true); | - |
2973 | void writeStartElement(const QString &namespaceUri, const QString &name); | - |
2974 | QIODevice *device; | - |
2975 | QString *stringDevice; | - |
2976 | uint deleteDevice :1; | - |
2977 | uint inStartElement :1; | - |
2978 | uint inEmptyElement :1; | - |
2979 | uint lastWasStartElement :1; | - |
2980 | uint wroteSomething :1; | - |
2981 | uint hasError :1; | - |
2982 | uint autoFormatting :1; | - |
2983 | uint isCodecASCIICompatible :1; | - |
2984 | QByteArray autoFormattingIndent; | - |
2985 | NamespaceDeclaration emptyNamespace; | - |
2986 | int lastNamespaceDeclaration; | - |
2987 | | - |
2988 | #ifndef QT_NO_TEXTCODEC | - |
2989 | QTextCodec *codec; | - |
2990 | QTextEncoder *encoder; | - |
2991 | #endif | - |
2992 | void checkIfASCIICompatibleCodec(); | - |
2993 | | - |
2994 | NamespaceDeclaration &findNamespace(const QString &namespaceUri, bool writeDeclaration = false, bool noDefault = false); | - |
2995 | void writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration); | - |
2996 | | - |
2997 | int namespacePrefixCount; | - |
2998 | | - |
2999 | void indent(int level); | - |
3000 | }; | - |
3001 | | - |
3002 | | - |
3003 | QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q) | - |
3004 | :autoFormattingIndent(4, ' ') | - |
3005 | { | - |
3006 | q_ptr = q; | - |
3007 | device = 0; | - |
3008 | stringDevice = 0; | - |
3009 | deleteDevice = false; | - |
3010 | #ifndef QT_NO_TEXTCODEC | - |
3011 | codec = QTextCodec::codecForMib(106); // utf8 | - |
3012 | encoder = codec->makeEncoder(QTextCodec::IgnoreHeader); // no byte order mark for utf8 | - |
3013 | #endif | - |
3014 | checkIfASCIICompatibleCodec(); | - |
3015 | inStartElement = inEmptyElement = false; | - |
3016 | wroteSomething = false; | - |
3017 | hasError = false; | - |
3018 | lastWasStartElement = false; | - |
3019 | lastNamespaceDeclaration = 1; | - |
3020 | autoFormatting = false; | - |
3021 | namespacePrefixCount = 0; | - |
3022 | } | - |
3023 | | - |
3024 | void QXmlStreamWriterPrivate::checkIfASCIICompatibleCodec() | - |
3025 | { | - |
3026 | #ifndef QT_NO_TEXTCODEC | - |
3027 | Q_ASSERT(encoder); | - |
3028 | // assumes ASCII-compatibility for all 8-bit encodings | - |
3029 | const QByteArray bytes = encoder->fromUnicode(QStringLiteral(" ")); | - |
3030 | isCodecASCIICompatible = (bytes.count() == 1); | - |
3031 | #else | - |
3032 | isCodecASCIICompatible = true; | - |
3033 | #endif | - |
3034 | } | - |
3035 | | - |
3036 | void QXmlStreamWriterPrivate::write(const QStringRef &s) | - |
3037 | { | - |
3038 | if (device) { | - |
3039 | if (hasError) | - |
3040 | return; | - |
3041 | #ifdef QT_NO_TEXTCODEC | - |
3042 | QByteArray bytes = s.toLatin1(); | - |
3043 | #else | - |
3044 | QByteArray bytes = encoder->fromUnicode(s.constData(), s.size()); | - |
3045 | #endif | - |
3046 | if (device->write(bytes) != bytes.size()) | - |
3047 | hasError = true; | - |
3048 | } | - |
3049 | else if (stringDevice) | - |
3050 | s.appendTo(stringDevice); | - |
3051 | else | - |
3052 | qWarning("QXmlStreamWriter: No device"); | - |
3053 | } | - |
3054 | | - |
3055 | void QXmlStreamWriterPrivate::write(const QString &s) | - |
3056 | { | - |
3057 | if (device) { | - |
3058 | if (hasError) | - |
3059 | return; | - |
3060 | #ifdef QT_NO_TEXTCODEC | - |
3061 | QByteArray bytes = s.toLatin1(); | - |
3062 | #else | - |
3063 | QByteArray bytes = encoder->fromUnicode(s); | - |
3064 | #endif | - |
3065 | if (device->write(bytes) != bytes.size()) | - |
3066 | hasError = true; | - |
3067 | } | - |
3068 | else if (stringDevice) | - |
3069 | stringDevice->append(s); | - |
3070 | else | - |
3071 | qWarning("QXmlStreamWriter: No device"); | - |
3072 | } | - |
3073 | | - |
3074 | void QXmlStreamWriterPrivate::writeEscaped(const QString &s, bool escapeWhitespace) | - |
3075 | { | - |
3076 | QString escaped; | - |
3077 | escaped.reserve(s.size()); | - |
3078 | for ( int i = 0; i < s.size(); ++i ) { | - |
3079 | QChar c = s.at(i); | - |
3080 | if (c.unicode() == '<' ) | - |
3081 | escaped.append(QLatin1String("<")); | - |
3082 | else if (c.unicode() == '>' ) | - |
3083 | escaped.append(QLatin1String(">")); | - |
3084 | else if (c.unicode() == '&' ) | - |
3085 | escaped.append(QLatin1String("&")); | - |
3086 | else if (c.unicode() == '\"' ) | - |
3087 | escaped.append(QLatin1String(""")); | - |
3088 | else if (escapeWhitespace && c.isSpace()) { | - |
3089 | if (c.unicode() == '\n') | - |
3090 | escaped.append(QLatin1String(" ")); | - |
3091 | else if (c.unicode() == '\r') | - |
3092 | escaped.append(QLatin1String(" ")); | - |
3093 | else if (c.unicode() == '\t') | - |
3094 | escaped.append(QLatin1String("	")); | - |
3095 | else | - |
3096 | escaped += c; | - |
3097 | } else { | - |
3098 | escaped += QChar(c); | - |
3099 | } | - |
3100 | } | - |
3101 | write(escaped); | - |
3102 | } | - |
3103 | | - |
3104 | // Converts from ASCII to output encoding | - |
3105 | void QXmlStreamWriterPrivate::write(const char *s, int len) | - |
3106 | { | - |
3107 | if (device) { | - |
3108 | if (hasError) | - |
3109 | return; | - |
3110 | if (isCodecASCIICompatible) { | - |
3111 | if (device->write(s, len) != len) | - |
3112 | hasError = true; | - |
3113 | return; | - |
3114 | } | - |
3115 | } | - |
3116 | | - |
3117 | write(QString::fromLatin1(s, len)); | - |
3118 | } | - |
3119 | | - |
3120 | void QXmlStreamWriterPrivate::writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration) { | - |
3121 | if (namespaceDeclaration.prefix.isEmpty()) { | - |
3122 | write(" xmlns=\""); | - |
3123 | write(namespaceDeclaration.namespaceUri); | - |
3124 | write("\""); | - |
3125 | } else { | - |
3126 | write(" xmlns:"); | - |
3127 | write(namespaceDeclaration.prefix); | - |
3128 | write("=\""); | - |
3129 | write(namespaceDeclaration.namespaceUri); | - |
3130 | write("\""); | - |
3131 | } | - |
3132 | } | - |
3133 | | - |
3134 | bool QXmlStreamWriterPrivate::finishStartElement(bool contents) | - |
3135 | { | - |
3136 | bool hadSomethingWritten = wroteSomething; | - |
3137 | wroteSomething = contents; | - |
3138 | if (!inStartElement) | - |
3139 | return hadSomethingWritten; | - |
3140 | | - |
3141 | if (inEmptyElement) { | - |
3142 | write("/>"); | - |
3143 | QXmlStreamWriterPrivate::Tag &tag = tagStack_pop(); | - |
3144 | lastNamespaceDeclaration = tag.namespaceDeclarationsSize; | - |
3145 | lastWasStartElement = false; | - |
3146 | } else { | - |
3147 | write(">"); | - |
3148 | } | - |
3149 | inStartElement = inEmptyElement = false; | - |
3150 | lastNamespaceDeclaration = namespaceDeclarations.size(); | - |
3151 | return hadSomethingWritten; | - |
3152 | } | - |
3153 | | - |
3154 | QXmlStreamPrivateTagStack::NamespaceDeclaration &QXmlStreamWriterPrivate::findNamespace(const QString &namespaceUri, bool writeDeclaration, bool noDefault) | - |
3155 | { | - |
3156 | for (int j = namespaceDeclarations.size() - 1; j >= 0; --j) { | - |
3157 | NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations[j]; | - |
3158 | if (namespaceDeclaration.namespaceUri == namespaceUri) { | - |
3159 | if (!noDefault || !namespaceDeclaration.prefix.isEmpty()) | - |
3160 | return namespaceDeclaration; | - |
3161 | } | - |
3162 | } | - |
3163 | if (namespaceUri.isEmpty()) | - |
3164 | return emptyNamespace; | - |
3165 | NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); | - |
3166 | if (namespaceUri.isEmpty()) { | - |
3167 | namespaceDeclaration.prefix.clear(); | - |
3168 | } else { | - |
3169 | QString s; | - |
3170 | int n = ++namespacePrefixCount; | - |
3171 | forever { | - |
3172 | s = QLatin1Char('n') + QString::number(n++); | - |
3173 | int j = namespaceDeclarations.size() - 2; | - |
3174 | while (j >= 0 && namespaceDeclarations.at(j).prefix != s) | - |
3175 | --j; | - |
3176 | if (j < 0) | - |
3177 | break; | - |
3178 | } | - |
3179 | namespaceDeclaration.prefix = addToStringStorage(s); | - |
3180 | } | - |
3181 | namespaceDeclaration.namespaceUri = addToStringStorage(namespaceUri); | - |
3182 | if (writeDeclaration) | - |
3183 | writeNamespaceDeclaration(namespaceDeclaration); | - |
3184 | return namespaceDeclaration; | - |
3185 | } | - |
3186 | | - |
3187 | | - |
3188 | | - |
3189 | void QXmlStreamWriterPrivate::indent(int level) | - |
3190 | { | - |
3191 | write("\n"); | - |
3192 | for (int i = level; i > 0; --i) | - |
3193 | write(autoFormattingIndent.constData(), autoFormattingIndent.length()); | - |
3194 | } | - |
3195 | | - |
3196 | | - |
3197 | /*! | - |
3198 | Constructs a stream writer. | - |
3199 | | - |
3200 | \sa setDevice() | - |
3201 | */ | - |
3202 | QXmlStreamWriter::QXmlStreamWriter() | - |
3203 | : d_ptr(new QXmlStreamWriterPrivate(this)) | - |
3204 | { | - |
3205 | } | - |
3206 | | - |
3207 | /*! | - |
3208 | Constructs a stream writer that writes into \a device; | - |
3209 | */ | - |
3210 | QXmlStreamWriter::QXmlStreamWriter(QIODevice *device) | - |
3211 | : d_ptr(new QXmlStreamWriterPrivate(this)) | - |
3212 | { | - |
3213 | Q_D(QXmlStreamWriter); | - |
3214 | d->device = device; | - |
3215 | } | - |
3216 | | - |
3217 | /*! Constructs a stream writer that writes into \a array. This is the | - |
3218 | same as creating an xml writer that operates on a QBuffer device | - |
3219 | which in turn operates on \a array. | - |
3220 | */ | - |
3221 | QXmlStreamWriter::QXmlStreamWriter(QByteArray *array) | - |
3222 | : d_ptr(new QXmlStreamWriterPrivate(this)) | - |
3223 | { | - |
3224 | Q_D(QXmlStreamWriter); | - |
3225 | d->device = new QBuffer(array); | - |
3226 | d->device->open(QIODevice::WriteOnly); | - |
3227 | d->deleteDevice = true; | - |
3228 | } | - |
3229 | | - |
3230 | | - |
3231 | /*! Constructs a stream writer that writes into \a string. | - |
3232 | */ | - |
3233 | QXmlStreamWriter::QXmlStreamWriter(QString *string) | - |
3234 | : d_ptr(new QXmlStreamWriterPrivate(this)) | - |
3235 | { | - |
3236 | Q_D(QXmlStreamWriter); | - |
3237 | d->stringDevice = string; | - |
3238 | } | - |
3239 | | - |
3240 | /*! | - |
3241 | Destructor. | - |
3242 | */ | - |
3243 | QXmlStreamWriter::~QXmlStreamWriter() | - |
3244 | { | - |
3245 | } | - |
3246 | | - |
3247 | | - |
3248 | /*! | - |
3249 | Sets the current device to \a device. If you want the stream to | - |
3250 | write into a QByteArray, you can create a QBuffer device. | - |
3251 | | - |
3252 | \sa device() | - |
3253 | */ | - |
3254 | void QXmlStreamWriter::setDevice(QIODevice *device) | - |
3255 | { | - |
3256 | Q_D(QXmlStreamWriter); | - |
3257 | if (device == d->device) | - |
3258 | return; | - |
3259 | d->stringDevice = 0; | - |
3260 | if (d->deleteDevice) { | - |
3261 | delete d->device; | - |
3262 | d->deleteDevice = false; | - |
3263 | } | - |
3264 | d->device = device; | - |
3265 | } | - |
3266 | | - |
3267 | /*! | - |
3268 | Returns the current device associated with the QXmlStreamWriter, | - |
3269 | or 0 if no device has been assigned. | - |
3270 | | - |
3271 | \sa setDevice() | - |
3272 | */ | - |
3273 | QIODevice *QXmlStreamWriter::device() const | - |
3274 | { | - |
3275 | Q_D(const QXmlStreamWriter); | - |
3276 | return d->device; | - |
3277 | } | - |
3278 | | - |
3279 | | - |
3280 | #ifndef QT_NO_TEXTCODEC | - |
3281 | /*! | - |
3282 | Sets the codec for this stream to \a codec. The codec is used for | - |
3283 | encoding any data that is written. By default, QXmlStreamWriter | - |
3284 | uses UTF-8. | - |
3285 | | - |
3286 | The encoding information is stored in the initial xml tag which | - |
3287 | gets written when you call writeStartDocument(). Call this | - |
3288 | function before calling writeStartDocument(). | - |
3289 | | - |
3290 | \sa codec() | - |
3291 | */ | - |
3292 | void QXmlStreamWriter::setCodec(QTextCodec *codec) | - |
3293 | { | - |
3294 | Q_D(QXmlStreamWriter); | - |
3295 | if (codec) { | - |
3296 | d->codec = codec; | - |
3297 | delete d->encoder; | - |
3298 | d->encoder = codec->makeEncoder(QTextCodec::IgnoreHeader); // no byte order mark for utf8 | - |
3299 | d->checkIfASCIICompatibleCodec(); | - |
3300 | } | - |
3301 | } | - |
3302 | | - |
3303 | /*! | - |
3304 | Sets the codec for this stream to the QTextCodec for the encoding | - |
3305 | specified by \a codecName. Common values for \c codecName include | - |
3306 | "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't | - |
3307 | recognized, nothing happens. | - |
3308 | | - |
3309 | \sa QTextCodec::codecForName() | - |
3310 | */ | - |
3311 | void QXmlStreamWriter::setCodec(const char *codecName) | - |
3312 | { | - |
3313 | setCodec(QTextCodec::codecForName(codecName)); | - |
3314 | } | - |
3315 | | - |
3316 | /*! | - |
3317 | Returns the codec that is currently assigned to the stream. | - |
3318 | | - |
3319 | \sa setCodec() | - |
3320 | */ | - |
3321 | QTextCodec *QXmlStreamWriter::codec() const | - |
3322 | { | - |
3323 | Q_D(const QXmlStreamWriter); | - |
3324 | return d->codec; | - |
3325 | } | - |
3326 | #endif // QT_NO_TEXTCODEC | - |
3327 | | - |
3328 | /*! | - |
3329 | \property QXmlStreamWriter::autoFormatting | - |
3330 | \since 4.4 | - |
3331 | the auto-formatting flag of the stream writer | - |
3332 | | - |
3333 | This property controls whether or not the stream writer | - |
3334 | automatically formats the generated XML data. If enabled, the | - |
3335 | writer automatically adds line-breaks and indentation to empty | - |
3336 | sections between elements (ignorable whitespace). The main purpose | - |
3337 | of auto-formatting is to split the data into several lines, and to | - |
3338 | increase readability for a human reader. The indentation depth can | - |
3339 | be controlled through the \l autoFormattingIndent property. | - |
3340 | | - |
3341 | By default, auto-formatting is disabled. | - |
3342 | */ | - |
3343 | | - |
3344 | /*! | - |
3345 | \since 4.4 | - |
3346 | | - |
3347 | Enables auto formatting if \a enable is \c true, otherwise | - |
3348 | disables it. | - |
3349 | | - |
3350 | The default value is \c false. | - |
3351 | */ | - |
3352 | void QXmlStreamWriter::setAutoFormatting(bool enable) | - |
3353 | { | - |
3354 | Q_D(QXmlStreamWriter); | - |
3355 | d->autoFormatting = enable; | - |
3356 | } | - |
3357 | | - |
3358 | /*! | - |
3359 | \since 4.4 | - |
3360 | | - |
3361 | Returns \c true if auto formattting is enabled, otherwise \c false. | - |
3362 | */ | - |
3363 | bool QXmlStreamWriter::autoFormatting() const | - |
3364 | { | - |
3365 | Q_D(const QXmlStreamWriter); | - |
3366 | return d->autoFormatting; | - |
3367 | } | - |
3368 | | - |
3369 | /*! | - |
3370 | \property QXmlStreamWriter::autoFormattingIndent | - |
3371 | \since 4.4 | - |
3372 | | - |
3373 | \brief the number of spaces or tabs used for indentation when | - |
3374 | auto-formatting is enabled. Positive numbers indicate spaces, | - |
3375 | negative numbers tabs. | - |
3376 | | - |
3377 | The default indentation is 4. | - |
3378 | | - |
3379 | \sa autoFormatting | - |
3380 | */ | - |
3381 | | - |
3382 | | - |
3383 | void QXmlStreamWriter::setAutoFormattingIndent(int spacesOrTabs) | - |
3384 | { | - |
3385 | Q_D(QXmlStreamWriter); | - |
3386 | d->autoFormattingIndent = QByteArray(qAbs(spacesOrTabs), spacesOrTabs >= 0 ? ' ' : '\t'); | - |
3387 | } | - |
3388 | | - |
3389 | int QXmlStreamWriter::autoFormattingIndent() const | - |
3390 | { | - |
3391 | Q_D(const QXmlStreamWriter); | - |
3392 | return d->autoFormattingIndent.count(' ') - d->autoFormattingIndent.count('\t'); | - |
3393 | } | - |
3394 | | - |
3395 | /*! | - |
3396 | Returns \c true if the stream failed to write to the underlying device. | - |
3397 | | - |
3398 | The error status is never reset. Writes happening after the error | - |
3399 | occurred are ignored, even if the error condition is cleared. | - |
3400 | */ | - |
3401 | bool QXmlStreamWriter::hasError() const | - |
3402 | { | - |
3403 | Q_D(const QXmlStreamWriter); | - |
3404 | return d->hasError; | - |
3405 | } | - |
3406 | | - |
3407 | /*! | - |
3408 | \overload | - |
3409 | Writes an attribute with \a qualifiedName and \a value. | - |
3410 | | - |
3411 | | - |
3412 | This function can only be called after writeStartElement() before | - |
3413 | any content is written, or after writeEmptyElement(). | - |
3414 | */ | - |
3415 | void QXmlStreamWriter::writeAttribute(const QString &qualifiedName, const QString &value) | - |
3416 | { | - |
3417 | Q_D(QXmlStreamWriter); | - |
3418 | Q_ASSERT(d->inStartElement); | - |
3419 | Q_ASSERT(qualifiedName.count(QLatin1Char(':')) <= 1); | - |
3420 | d->write(" "); | - |
3421 | d->write(qualifiedName); | - |
3422 | d->write("=\""); | - |
3423 | d->writeEscaped(value, true); | - |
3424 | d->write("\""); | - |
3425 | } | - |
3426 | | - |
3427 | /*! Writes an attribute with \a name and \a value, prefixed for | - |
3428 | the specified \a namespaceUri. If the namespace has not been | - |
3429 | declared yet, QXmlStreamWriter will generate a namespace declaration | - |
3430 | for it. | - |
3431 | | - |
3432 | This function can only be called after writeStartElement() before | - |
3433 | any content is written, or after writeEmptyElement(). | - |
3434 | */ | - |
3435 | void QXmlStreamWriter::writeAttribute(const QString &namespaceUri, const QString &name, const QString &value) | - |
3436 | { | - |
3437 | Q_D(QXmlStreamWriter); | - |
3438 | Q_ASSERT(d->inStartElement); | - |
3439 | Q_ASSERT(!name.contains(QLatin1Char(':'))); | - |
3440 | QXmlStreamWriterPrivate::NamespaceDeclaration &namespaceDeclaration = d->findNamespace(namespaceUri, true, true); | - |
3441 | d->write(" "); | - |
3442 | if (!namespaceDeclaration.prefix.isEmpty()) { | - |
3443 | d->write(namespaceDeclaration.prefix); | - |
3444 | d->write(":"); | - |
3445 | } | - |
3446 | d->write(name); | - |
3447 | d->write("=\""); | - |
3448 | d->writeEscaped(value, true); | - |
3449 | d->write("\""); | - |
3450 | } | - |
3451 | | - |
3452 | /*! | - |
3453 | \overload | - |
3454 | | - |
3455 | Writes the \a attribute. | - |
3456 | | - |
3457 | This function can only be called after writeStartElement() before | - |
3458 | any content is written, or after writeEmptyElement(). | - |
3459 | */ | - |
3460 | void QXmlStreamWriter::writeAttribute(const QXmlStreamAttribute& attribute) | - |
3461 | { | - |
3462 | if (attribute.namespaceUri().isEmpty()) | - |
3463 | writeAttribute(attribute.qualifiedName().toString(), | - |
3464 | attribute.value().toString()); | - |
3465 | else | - |
3466 | writeAttribute(attribute.namespaceUri().toString(), | - |
3467 | attribute.name().toString(), | - |
3468 | attribute.value().toString()); | - |
3469 | } | - |
3470 | | - |
3471 | | - |
3472 | /*! Writes the attribute vector \a attributes. If a namespace | - |
3473 | referenced in an attribute not been declared yet, QXmlStreamWriter | - |
3474 | will generate a namespace declaration for it. | - |
3475 | | - |
3476 | This function can only be called after writeStartElement() before | - |
3477 | any content is written, or after writeEmptyElement(). | - |
3478 | | - |
3479 | \sa writeAttribute(), writeNamespace() | - |
3480 | */ | - |
3481 | void QXmlStreamWriter::writeAttributes(const QXmlStreamAttributes& attributes) | - |
3482 | { | - |
3483 | Q_D(QXmlStreamWriter); | - |
3484 | Q_ASSERT(d->inStartElement); | - |
3485 | Q_UNUSED(d); | - |
3486 | for (int i = 0; i < attributes.size(); ++i) | - |
3487 | writeAttribute(attributes.at(i)); | - |
3488 | } | - |
3489 | | - |
3490 | | - |
3491 | /*! Writes \a text as CDATA section. If \a text contains the | - |
3492 | forbidden character sequence "]]>", it is split into different CDATA | - |
3493 | sections. | - |
3494 | | - |
3495 | This function mainly exists for completeness. Normally you should | - |
3496 | not need use it, because writeCharacters() automatically escapes all | - |
3497 | non-content characters. | - |
3498 | */ | - |
3499 | void QXmlStreamWriter::writeCDATA(const QString &text) | - |
3500 | { | - |
3501 | Q_D(QXmlStreamWriter); | - |
3502 | d->finishStartElement(); | - |
3503 | QString copy(text); | - |
3504 | copy.replace(QLatin1String("]]>"), QLatin1String("]]]]><![CDATA[>")); | - |
3505 | d->write("<![CDATA["); | - |
3506 | d->write(copy); | - |
3507 | d->write("]]>"); | - |
3508 | } | - |
3509 | | - |
3510 | | - |
3511 | /*! Writes \a text. The characters "<", "&", and "\"" are escaped as entity | - |
3512 | references "<", "&, and """. To avoid the forbidden sequence | - |
3513 | "]]>", ">" is also escaped as ">". | - |
3514 | | - |
3515 | \sa writeEntityReference() | - |
3516 | */ | - |
3517 | void QXmlStreamWriter::writeCharacters(const QString &text) | - |
3518 | { | - |
3519 | Q_D(QXmlStreamWriter); | - |
3520 | d->finishStartElement(); | - |
3521 | d->writeEscaped(text); | - |
3522 | } | - |
3523 | | - |
3524 | | - |
3525 | /*! Writes \a text as XML comment, where \a text must not contain the | - |
3526 | forbidden sequence "--" or end with "-". Note that XML does not | - |
3527 | provide any way to escape "-" in a comment. | - |
3528 | */ | - |
3529 | void QXmlStreamWriter::writeComment(const QString &text) | - |
3530 | { | - |
3531 | Q_D(QXmlStreamWriter); | - |
3532 | Q_ASSERT(!text.contains(QLatin1String("--")) && !text.endsWith(QLatin1Char('-'))); | - |
3533 | if (!d->finishStartElement(false) && d->autoFormatting) | - |
3534 | d->indent(d->tagStack.size()); | - |
3535 | d->write("<!--"); | - |
3536 | d->write(text); | - |
3537 | d->write("-->"); | - |
3538 | d->inStartElement = d->lastWasStartElement = false; | - |
3539 | } | - |
3540 | | - |
3541 | | - |
3542 | /*! Writes a DTD section. The \a dtd represents the entire | - |
3543 | doctypedecl production from the XML 1.0 specification. | - |
3544 | */ | - |
3545 | void QXmlStreamWriter::writeDTD(const QString &dtd) | - |
3546 | { | - |
3547 | Q_D(QXmlStreamWriter); | - |
3548 | d->finishStartElement(); | - |
3549 | if (d->autoFormatting) | - |
3550 | d->write("\n"); | - |
3551 | d->write(dtd); | - |
3552 | if (d->autoFormatting) | - |
3553 | d->write("\n"); | - |
3554 | } | - |
3555 | | - |
3556 | | - |
3557 | | - |
3558 | /*! \overload | - |
3559 | Writes an empty element with qualified name \a qualifiedName. | - |
3560 | Subsequent calls to writeAttribute() will add attributes to this element. | - |
3561 | */ | - |
3562 | void QXmlStreamWriter::writeEmptyElement(const QString &qualifiedName) | - |
3563 | { | - |
3564 | Q_D(QXmlStreamWriter); | - |
3565 | Q_ASSERT(qualifiedName.count(QLatin1Char(':')) <= 1); | - |
3566 | d->writeStartElement(QString(), qualifiedName); | - |
3567 | d->inEmptyElement = true; | - |
3568 | } | - |
3569 | | - |
3570 | | - |
3571 | /*! Writes an empty element with \a name, prefixed for the specified | - |
3572 | \a namespaceUri. If the namespace has not been declared, | - |
3573 | QXmlStreamWriter will generate a namespace declaration for it. | - |
3574 | Subsequent calls to writeAttribute() will add attributes to this element. | - |
3575 | | - |
3576 | \sa writeNamespace() | - |
3577 | */ | - |
3578 | void QXmlStreamWriter::writeEmptyElement(const QString &namespaceUri, const QString &name) | - |
3579 | { | - |
3580 | Q_D(QXmlStreamWriter); | - |
3581 | Q_ASSERT(!name.contains(QLatin1Char(':'))); | - |
3582 | d->writeStartElement(namespaceUri, name); | - |
3583 | d->inEmptyElement = true; | - |
3584 | } | - |
3585 | | - |
3586 | | - |
3587 | /*!\overload | - |
3588 | Writes a text element with \a qualifiedName and \a text. | - |
3589 | | - |
3590 | | - |
3591 | This is a convenience function equivalent to: | - |
3592 | \snippet code/src_corelib_xml_qxmlstream.cpp 1 | - |
3593 | | - |
3594 | */ | - |
3595 | void QXmlStreamWriter::writeTextElement(const QString &qualifiedName, const QString &text) | - |
3596 | { | - |
3597 | writeStartElement(qualifiedName); | - |
3598 | writeCharacters(text); | - |
3599 | writeEndElement(); | - |
3600 | } | - |
3601 | | - |
3602 | /*! Writes a text element with \a name, prefixed for the specified \a | - |
3603 | namespaceUri, and \a text. If the namespace has not been | - |
3604 | declared, QXmlStreamWriter will generate a namespace declaration | - |
3605 | for it. | - |
3606 | | - |
3607 | | - |
3608 | This is a convenience function equivalent to: | - |
3609 | \snippet code/src_corelib_xml_qxmlstream.cpp 2 | - |
3610 | | - |
3611 | */ | - |
3612 | void QXmlStreamWriter::writeTextElement(const QString &namespaceUri, const QString &name, const QString &text) | - |
3613 | { | - |
3614 | writeStartElement(namespaceUri, name); | - |
3615 | writeCharacters(text); | - |
3616 | writeEndElement(); | - |
3617 | } | - |
3618 | | - |
3619 | | - |
3620 | /*! | - |
3621 | Closes all remaining open start elements and writes a newline. | - |
3622 | | - |
3623 | \sa writeStartDocument() | - |
3624 | */ | - |
3625 | void QXmlStreamWriter::writeEndDocument() | - |
3626 | { | - |
3627 | Q_D(QXmlStreamWriter); | - |
3628 | while (d->tagStack.size()) | - |
3629 | writeEndElement(); | - |
3630 | d->write("\n"); | - |
3631 | } | - |
3632 | | - |
3633 | /*! | - |
3634 | Closes the previous start element. | - |
3635 | | - |
3636 | \sa writeStartElement() | - |
3637 | */ | - |
3638 | void QXmlStreamWriter::writeEndElement() | - |
3639 | { | - |
3640 | Q_D(QXmlStreamWriter); | - |
3641 | if (d->tagStack.isEmpty()) | - |
3642 | return; | - |
3643 | | - |
3644 | // shortcut: if nothing was written, close as empty tag | - |
3645 | if (d->inStartElement && !d->inEmptyElement) { | - |
3646 | d->write("/>"); | - |
3647 | d->lastWasStartElement = d->inStartElement = false; | - |
3648 | QXmlStreamWriterPrivate::Tag &tag = d->tagStack_pop(); | - |
3649 | d->lastNamespaceDeclaration = tag.namespaceDeclarationsSize; | - |
3650 | return; | - |
3651 | } | - |
3652 | | - |
3653 | if (!d->finishStartElement(false) && !d->lastWasStartElement && d->autoFormatting) | - |
3654 | d->indent(d->tagStack.size()-1); | - |
3655 | if (d->tagStack.isEmpty()) | - |
3656 | return; | - |
3657 | d->lastWasStartElement = false; | - |
3658 | QXmlStreamWriterPrivate::Tag &tag = d->tagStack_pop(); | - |
3659 | d->lastNamespaceDeclaration = tag.namespaceDeclarationsSize; | - |
3660 | d->write("</"); | - |
3661 | if (!tag.namespaceDeclaration.prefix.isEmpty()) { | - |
3662 | d->write(tag.namespaceDeclaration.prefix); | - |
3663 | d->write(":"); | - |
3664 | } | - |
3665 | d->write(tag.name); | - |
3666 | d->write(">"); | - |
3667 | } | - |
3668 | | - |
3669 | | - |
3670 | | - |
3671 | /*! | - |
3672 | Writes the entity reference \a name to the stream, as "&\a{name};". | - |
3673 | */ | - |
3674 | void QXmlStreamWriter::writeEntityReference(const QString &name) | - |
3675 | { | - |
3676 | Q_D(QXmlStreamWriter); | - |
3677 | d->finishStartElement(); | - |
3678 | d->write("&"); | - |
3679 | d->write(name); | - |
3680 | d->write(";"); | - |
3681 | } | - |
3682 | | - |
3683 | | - |
3684 | /*! Writes a namespace declaration for \a namespaceUri with \a | - |
3685 | prefix. If \a prefix is empty, QXmlStreamWriter assigns a unique | - |
3686 | prefix consisting of the letter 'n' followed by a number. | - |
3687 | | - |
3688 | If writeStartElement() or writeEmptyElement() was called, the | - |
3689 | declaration applies to the current element; otherwise it applies to | - |
3690 | the next child element. | - |
3691 | | - |
3692 | Note that the prefix \e xml is both predefined and reserved for | - |
3693 | \e http://www.w3.org/XML/1998/namespace, which in turn cannot be | - |
3694 | bound to any other prefix. The prefix \e xmlns and its URI | - |
3695 | \e http://www.w3.org/2000/xmlns/ are used for the namespace mechanism | - |
3696 | itself and thus completely forbidden in declarations. | - |
3697 | | - |
3698 | */ | - |
3699 | void QXmlStreamWriter::writeNamespace(const QString &namespaceUri, const QString &prefix) | - |
3700 | { | - |
3701 | Q_D(QXmlStreamWriter); | - |
3702 | Q_ASSERT(!namespaceUri.isEmpty()); | - |
3703 | Q_ASSERT(prefix != QLatin1String("xmlns")); | - |
3704 | if (prefix.isEmpty()) { | - |
3705 | d->findNamespace(namespaceUri, d->inStartElement); | - |
3706 | } else { | - |
3707 | Q_ASSERT(!((prefix == QLatin1String("xml")) ^ (namespaceUri == QLatin1String("http://www.w3.org/XML/1998/namespace")))); | - |
3708 | Q_ASSERT(namespaceUri != QLatin1String("http://www.w3.org/2000/xmlns/")); | - |
3709 | QXmlStreamWriterPrivate::NamespaceDeclaration &namespaceDeclaration = d->namespaceDeclarations.push(); | - |
3710 | namespaceDeclaration.prefix = d->addToStringStorage(prefix); | - |
3711 | namespaceDeclaration.namespaceUri = d->addToStringStorage(namespaceUri); | - |
3712 | if (d->inStartElement) | - |
3713 | d->writeNamespaceDeclaration(namespaceDeclaration); | - |
3714 | } | - |
3715 | } | - |
3716 | | - |
3717 | | - |
3718 | /*! Writes a default namespace declaration for \a namespaceUri. | - |
3719 | | - |
3720 | If writeStartElement() or writeEmptyElement() was called, the | - |
3721 | declaration applies to the current element; otherwise it applies to | - |
3722 | the next child element. | - |
3723 | | - |
3724 | Note that the namespaces \e http://www.w3.org/XML/1998/namespace | - |
3725 | (bound to \e xmlns) and \e http://www.w3.org/2000/xmlns/ (bound to | - |
3726 | \e xml) by definition cannot be declared as default. | - |
3727 | */ | - |
3728 | void QXmlStreamWriter::writeDefaultNamespace(const QString &namespaceUri) | - |
3729 | { | - |
3730 | Q_D(QXmlStreamWriter); | - |
3731 | Q_ASSERT(namespaceUri != QLatin1String("http://www.w3.org/XML/1998/namespace")); | - |
3732 | Q_ASSERT(namespaceUri != QLatin1String("http://www.w3.org/2000/xmlns/")); | - |
3733 | QXmlStreamWriterPrivate::NamespaceDeclaration &namespaceDeclaration = d->namespaceDeclarations.push(); | - |
3734 | namespaceDeclaration.prefix.clear(); | - |
3735 | namespaceDeclaration.namespaceUri = d->addToStringStorage(namespaceUri); | - |
3736 | if (d->inStartElement) | - |
3737 | d->writeNamespaceDeclaration(namespaceDeclaration); | - |
3738 | } | - |
3739 | | - |
3740 | | - |
3741 | /*! | - |
3742 | Writes an XML processing instruction with \a target and \a data, | - |
3743 | where \a data must not contain the sequence "?>". | - |
3744 | */ | - |
3745 | void QXmlStreamWriter::writeProcessingInstruction(const QString &target, const QString &data) | - |
3746 | { | - |
3747 | Q_D(QXmlStreamWriter); | - |
3748 | Q_ASSERT(!data.contains(QLatin1String("?>"))); | - |
3749 | if (!d->finishStartElement(false) && d->autoFormatting) | - |
3750 | d->indent(d->tagStack.size()); | - |
3751 | d->write("<?"); | - |
3752 | d->write(target); | - |
3753 | if (!data.isNull()) { | - |
3754 | d->write(" "); | - |
3755 | d->write(data); | - |
3756 | } | - |
3757 | d->write("?>"); | - |
3758 | } | - |
3759 | | - |
3760 | | - |
3761 | | - |
3762 | /*!\overload | - |
3763 | | - |
3764 | Writes a document start with XML version number "1.0". This also | - |
3765 | writes the encoding information. | - |
3766 | | - |
3767 | \sa writeEndDocument(), setCodec() | - |
3768 | \since 4.5 | - |
3769 | */ | - |
3770 | void QXmlStreamWriter::writeStartDocument() | - |
3771 | { | - |
3772 | writeStartDocument(QLatin1String("1.0")); | - |
3773 | } | - |
3774 | | - |
3775 | | - |
3776 | /*! | - |
3777 | Writes a document start with the XML version number \a version. | - |
3778 | | - |
3779 | \sa writeEndDocument() | - |
3780 | */ | - |
3781 | void QXmlStreamWriter::writeStartDocument(const QString &version) | - |
3782 | { | - |
3783 | Q_D(QXmlStreamWriter); | - |
3784 | d->finishStartElement(false); | - |
3785 | d->write("<?xml version=\""); | - |
3786 | d->write(version); | - |
3787 | if (d->device) { // stringDevice does not get any encoding | - |
3788 | d->write("\" encoding=\""); | - |
3789 | #ifdef QT_NO_TEXTCODEC | - |
3790 | d->write("iso-8859-1"); | - |
3791 | #else | - |
3792 | d->write(d->codec->name().constData(), d->codec->name().length()); | - |
3793 | #endif | - |
3794 | } | - |
3795 | d->write("\"?>"); | - |
3796 | } | - |
3797 | | - |
3798 | /*! Writes a document start with the XML version number \a version | - |
3799 | and a standalone attribute \a standalone. | - |
3800 | | - |
3801 | \sa writeEndDocument() | - |
3802 | \since 4.5 | - |
3803 | */ | - |
3804 | void QXmlStreamWriter::writeStartDocument(const QString &version, bool standalone) | - |
3805 | { | - |
3806 | Q_D(QXmlStreamWriter); | - |
3807 | d->finishStartElement(false); | - |
3808 | d->write("<?xml version=\""); | - |
3809 | d->write(version); | - |
3810 | if (d->device) { // stringDevice does not get any encoding | - |
3811 | d->write("\" encoding=\""); | - |
3812 | #ifdef QT_NO_TEXTCODEC | - |
3813 | d->write("iso-8859-1"); | - |
3814 | #else | - |
3815 | d->write(d->codec->name().constData(), d->codec->name().length()); | - |
3816 | #endif | - |
3817 | } | - |
3818 | if (standalone) | - |
3819 | d->write("\" standalone=\"yes\"?>"); | - |
3820 | else | - |
3821 | d->write("\" standalone=\"no\"?>"); | - |
3822 | } | - |
3823 | | - |
3824 | | - |
3825 | /*!\overload | - |
3826 | | - |
3827 | Writes a start element with \a qualifiedName. Subsequent calls to | - |
3828 | writeAttribute() will add attributes to this element. | - |
3829 | | - |
3830 | \sa writeEndElement(), writeEmptyElement() | - |
3831 | */ | - |
3832 | void QXmlStreamWriter::writeStartElement(const QString &qualifiedName) | - |
3833 | { | - |
3834 | Q_D(QXmlStreamWriter); | - |
3835 | Q_ASSERT(qualifiedName.count(QLatin1Char(':')) <= 1); | - |
3836 | d->writeStartElement(QString(), qualifiedName); | - |
3837 | } | - |
3838 | | - |
3839 | | - |
3840 | /*! Writes a start element with \a name, prefixed for the specified | - |
3841 | \a namespaceUri. If the namespace has not been declared yet, | - |
3842 | QXmlStreamWriter will generate a namespace declaration for | - |
3843 | it. Subsequent calls to writeAttribute() will add attributes to this | - |
3844 | element. | - |
3845 | | - |
3846 | \sa writeNamespace(), writeEndElement(), writeEmptyElement() | - |
3847 | */ | - |
3848 | void QXmlStreamWriter::writeStartElement(const QString &namespaceUri, const QString &name) | - |
3849 | { | - |
3850 | Q_D(QXmlStreamWriter); | - |
3851 | Q_ASSERT(!name.contains(QLatin1Char(':'))); | - |
3852 | d->writeStartElement(namespaceUri, name); | - |
3853 | } | - |
3854 | | - |
3855 | void QXmlStreamWriterPrivate::writeStartElement(const QString &namespaceUri, const QString &name) | - |
3856 | { | - |
3857 | if (!finishStartElement(false) && autoFormatting) | - |
3858 | indent(tagStack.size()); | - |
3859 | | - |
3860 | Tag &tag = tagStack_push(); | - |
3861 | tag.name = addToStringStorage(name); | - |
3862 | tag.namespaceDeclaration = findNamespace(namespaceUri); | - |
3863 | write("<"); | - |
3864 | if (!tag.namespaceDeclaration.prefix.isEmpty()) { | - |
3865 | write(tag.namespaceDeclaration.prefix); | - |
3866 | write(":"); | - |
3867 | } | - |
3868 | write(tag.name); | - |
3869 | inStartElement = lastWasStartElement = true; | - |
3870 | | - |
3871 | for (int i = lastNamespaceDeclaration; i < namespaceDeclarations.size(); ++i) | - |
3872 | writeNamespaceDeclaration(namespaceDeclarations[i]); | - |
3873 | tag.namespaceDeclarationsSize = lastNamespaceDeclaration; | - |
3874 | } | - |
3875 | | - |
3876 | #ifndef QT_NO_XMLSTREAMREADER | - |
3877 | /*! Writes the current state of the \a reader. All possible valid | - |
3878 | states are supported. | - |
3879 | | - |
3880 | The purpose of this function is to support chained processing of XML data. | - |
3881 | | - |
3882 | \sa QXmlStreamReader::tokenType() | - |
3883 | */ | - |
3884 | void QXmlStreamWriter::writeCurrentToken(const QXmlStreamReader &reader) | - |
3885 | { | - |
3886 | switch (reader.tokenType()) { | - |
3887 | case QXmlStreamReader::NoToken: | - |
3888 | break; | - |
3889 | case QXmlStreamReader::StartDocument: | - |
3890 | writeStartDocument(); | - |
3891 | break; | - |
3892 | case QXmlStreamReader::EndDocument: | - |
3893 | writeEndDocument(); | - |
3894 | break; | - |
3895 | case QXmlStreamReader::StartElement: { | - |
3896 | QXmlStreamNamespaceDeclarations namespaceDeclarations = reader.namespaceDeclarations(); | - |
3897 | for (int i = 0; i < namespaceDeclarations.size(); ++i) { | - |
3898 | const QXmlStreamNamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(i); | - |
3899 | writeNamespace(namespaceDeclaration.namespaceUri().toString(), | - |
3900 | namespaceDeclaration.prefix().toString()); | - |
3901 | } | - |
3902 | writeStartElement(reader.namespaceUri().toString(), reader.name().toString()); | - |
3903 | writeAttributes(reader.attributes()); | - |
3904 | } break; | - |
3905 | case QXmlStreamReader::EndElement: | - |
3906 | writeEndElement(); | - |
3907 | break; | - |
3908 | case QXmlStreamReader::Characters: | - |
3909 | if (reader.isCDATA()) | - |
3910 | writeCDATA(reader.text().toString()); | - |
3911 | else | - |
3912 | writeCharacters(reader.text().toString()); | - |
3913 | break; | - |
3914 | case QXmlStreamReader::Comment: | - |
3915 | writeComment(reader.text().toString()); | - |
3916 | break; | - |
3917 | case QXmlStreamReader::DTD: | - |
3918 | writeDTD(reader.text().toString()); | - |
3919 | break; | - |
3920 | case QXmlStreamReader::EntityReference: | - |
3921 | writeEntityReference(reader.name().toString()); | - |
3922 | break; | - |
3923 | case QXmlStreamReader::ProcessingInstruction: | - |
3924 | writeProcessingInstruction(reader.processingInstructionTarget().toString(), | - |
3925 | reader.processingInstructionData().toString()); | - |
3926 | break; | - |
3927 | default: | - |
3928 | Q_ASSERT(reader.tokenType() != QXmlStreamReader::Invalid); | - |
3929 | qWarning("QXmlStreamWriter: writeCurrentToken() with invalid state."); | - |
3930 | break; | - |
3931 | } | - |
3932 | } | - |
3933 | | - |
3934 | /*! | - |
3935 | \fn bool QXmlStreamAttributes::hasAttribute(const QString &qualifiedName) const | - |
3936 | \since 4.5 | - |
3937 | | - |
3938 | Returns true if this QXmlStreamAttributes has an attribute whose | - |
3939 | qualified name is \a qualifiedName; otherwise returns false. | - |
3940 | | - |
3941 | Note that this is not namespace aware. For instance, if this | - |
3942 | QXmlStreamAttributes contains an attribute whose lexical name is "xlink:href" | - |
3943 | this doesn't tell that an attribute named \c href in the XLink namespace is | - |
3944 | present, since the \c xlink prefix can be bound to any namespace. Use the | - |
3945 | overload that takes a namespace URI and a local name as parameter, for | - |
3946 | namespace aware code. | - |
3947 | */ | - |
3948 | | - |
3949 | /*! | - |
3950 | \fn bool QXmlStreamAttributes::hasAttribute(QLatin1String qualifiedName) const | - |
3951 | \overload | - |
3952 | \since 4.5 | - |
3953 | */ | - |
3954 | | - |
3955 | /*! | - |
3956 | \fn bool QXmlStreamAttributes::hasAttribute(const QString &namespaceUri, | - |
3957 | const QString &name) const | - |
3958 | \overload | - |
3959 | \since 4.5 | - |
3960 | | - |
3961 | Returns true if this QXmlStreamAttributes has an attribute whose | - |
3962 | namespace URI and name correspond to \a namespaceUri and \a name; | - |
3963 | otherwise returns false. | - |
3964 | */ | - |
3965 | | - |
3966 | #endif // QT_NO_XMLSTREAMREADER | - |
3967 | #endif // QT_NO_XMLSTREAMWRITER | - |
3968 | | - |
3969 | QT_END_NAMESPACE | - |
3970 | | - |
3971 | #endif // QT_NO_XMLSTREAM | - |
3972 | | - |
| | |