Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/xml/qxmlstream.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
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 The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "QtCore/qxmlstream.h" | - | ||||||
41 | - | |||||||
42 | #ifndef QT_NO_XMLSTREAM | - | ||||||
43 | - | |||||||
44 | #include "qxmlutils_p.h" | - | ||||||
45 | #include <qdebug.h> | - | ||||||
46 | #include <qfile.h> | - | ||||||
47 | #include <stdio.h> | - | ||||||
48 | #include <qtextcodec.h> | - | ||||||
49 | #include <qstack.h> | - | ||||||
50 | #include <qbuffer.h> | - | ||||||
51 | #ifndef QT_BOOTSTRAPPED | - | ||||||
52 | #include <qcoreapplication.h> | - | ||||||
53 | #else | - | ||||||
54 | // This specialization of Q_DECLARE_TR_FUNCTIONS is not in qcoreapplication.h, | - | ||||||
55 | // because that header depends on QObject being available, which is not the | - | ||||||
56 | // case for most bootstrapped applications. | - | ||||||
57 | #define Q_DECLARE_TR_FUNCTIONS(context) \ | - | ||||||
58 | public: \ | - | ||||||
59 | static inline QString tr(const char *sourceText, const char *comment = 0) \ | - | ||||||
60 | { Q_UNUSED(comment); return QString::fromLatin1(sourceText); } \ | - | ||||||
61 | static inline QString trUtf8(const char *sourceText, const char *comment = 0) \ | - | ||||||
62 | { Q_UNUSED(comment); return QString::fromLatin1(sourceText); } \ | - | ||||||
63 | static inline QString tr(const char *sourceText, const char*, int) \ | - | ||||||
64 | { return QString::fromLatin1(sourceText); } \ | - | ||||||
65 | static inline QString trUtf8(const char *sourceText, const char*, int) \ | - | ||||||
66 | { return QString::fromLatin1(sourceText); } \ | - | ||||||
67 | private: | - | ||||||
68 | #endif | - | ||||||
69 | QT_BEGIN_NAMESPACE | - | ||||||
70 | - | |||||||
71 | #include "qxmlstream_p.h" | - | ||||||
72 | - | |||||||
73 | enum { StreamEOF = ~0U }; | - | ||||||
74 | - | |||||||
75 | /*! | - | ||||||
76 | \enum QXmlStreamReader::TokenType | - | ||||||
77 | - | |||||||
78 | This enum specifies the type of token the reader just read. | - | ||||||
79 | - | |||||||
80 | \value NoToken The reader has not yet read anything. | - | ||||||
81 | - | |||||||
82 | \value Invalid An error has occurred, reported in error() and | - | ||||||
83 | errorString(). | - | ||||||
84 | - | |||||||
85 | \value StartDocument The reader reports the XML version number in | - | ||||||
86 | documentVersion(), and the encoding as specified in the XML | - | ||||||
87 | document in documentEncoding(). If the document is declared | - | ||||||
88 | standalone, isStandaloneDocument() returns \c true; otherwise it | - | ||||||
89 | returns \c false. | - | ||||||
90 | - | |||||||
91 | \value EndDocument The reader reports the end of the document. | - | ||||||
92 | - | |||||||
93 | \value StartElement The reader reports the start of an element | - | ||||||
94 | with namespaceUri() and name(). Empty elements are also reported | - | ||||||
95 | as StartElement, followed directly by EndElement. The convenience | - | ||||||
96 | function readElementText() can be called to concatenate all | - | ||||||
97 | content until the corresponding EndElement. Attributes are | - | ||||||
98 | reported in attributes(), namespace declarations in | - | ||||||
99 | namespaceDeclarations(). | - | ||||||
100 | - | |||||||
101 | \value EndElement The reader reports the end of an element with | - | ||||||
102 | namespaceUri() and name(). | - | ||||||
103 | - | |||||||
104 | \value Characters The reader reports characters in text(). If the | - | ||||||
105 | characters are all white-space, isWhitespace() returns \c true. If | - | ||||||
106 | the characters stem from a CDATA section, isCDATA() returns \c true. | - | ||||||
107 | - | |||||||
108 | \value Comment The reader reports a comment in text(). | - | ||||||
109 | - | |||||||
110 | \value DTD The reader reports a DTD in text(), notation | - | ||||||
111 | declarations in notationDeclarations(), and entity declarations in | - | ||||||
112 | entityDeclarations(). Details of the DTD declaration are reported | - | ||||||
113 | in in dtdName(), dtdPublicId(), and dtdSystemId(). | - | ||||||
114 | - | |||||||
115 | \value EntityReference The reader reports an entity reference that | - | ||||||
116 | could not be resolved. The name of the reference is reported in | - | ||||||
117 | name(), the replacement text in text(). | - | ||||||
118 | - | |||||||
119 | \value ProcessingInstruction The reader reports a processing | - | ||||||
120 | instruction in processingInstructionTarget() and | - | ||||||
121 | processingInstructionData(). | - | ||||||
122 | */ | - | ||||||
123 | - | |||||||
124 | /*! | - | ||||||
125 | \enum QXmlStreamReader::ReadElementTextBehaviour | - | ||||||
126 | - | |||||||
127 | This enum specifies the different behaviours of readElementText(). | - | ||||||
128 | - | |||||||
129 | \value ErrorOnUnexpectedElement Raise an UnexpectedElementError and return | - | ||||||
130 | what was read so far when a child element is encountered. | - | ||||||
131 | - | |||||||
132 | \value IncludeChildElements Recursively include the text from child elements. | - | ||||||
133 | - | |||||||
134 | \value SkipChildElements Skip child elements. | - | ||||||
135 | - | |||||||
136 | \since 4.6 | - | ||||||
137 | */ | - | ||||||
138 | - | |||||||
139 | /*! | - | ||||||
140 | \enum QXmlStreamReader::Error | - | ||||||
141 | - | |||||||
142 | This enum specifies different error cases | - | ||||||
143 | - | |||||||
144 | \value NoError No error has occurred. | - | ||||||
145 | - | |||||||
146 | \value CustomError A custom error has been raised with | - | ||||||
147 | raiseError() | - | ||||||
148 | - | |||||||
149 | \value NotWellFormedError The parser internally raised an error | - | ||||||
150 | due to the read XML not being well-formed. | - | ||||||
151 | - | |||||||
152 | \value PrematureEndOfDocumentError The input stream ended before a | - | ||||||
153 | well-formed XML document was parsed. Recovery from this error is | - | ||||||
154 | possible if more XML arrives in the stream, either by calling | - | ||||||
155 | addData() or by waiting for it to arrive on the device(). | - | ||||||
156 | - | |||||||
157 | \value UnexpectedElementError The parser encountered an element | - | ||||||
158 | that was different to those it expected. | - | ||||||
159 | - | |||||||
160 | */ | - | ||||||
161 | - | |||||||
162 | /*! | - | ||||||
163 | \class QXmlStreamEntityResolver | - | ||||||
164 | \inmodule QtCore | - | ||||||
165 | \reentrant | - | ||||||
166 | \since 4.4 | - | ||||||
167 | - | |||||||
168 | \brief The QXmlStreamEntityResolver class provides an entity | - | ||||||
169 | resolver for a QXmlStreamReader. | - | ||||||
170 | - | |||||||
171 | \ingroup xml-tools | - | ||||||
172 | */ | - | ||||||
173 | - | |||||||
174 | /*! | - | ||||||
175 | Destroys the entity resolver. | - | ||||||
176 | */ | - | ||||||
177 | QXmlStreamEntityResolver::~QXmlStreamEntityResolver() | - | ||||||
178 | { | - | ||||||
179 | } | - | ||||||
180 | - | |||||||
181 | /*! | - | ||||||
182 | \internal | - | ||||||
183 | - | |||||||
184 | This function is a stub for later functionality. | - | ||||||
185 | */ | - | ||||||
186 | QString QXmlStreamEntityResolver::resolveEntity(const QString& /*publicId*/, const QString& /*systemId*/) | - | ||||||
187 | { | - | ||||||
188 | return QString(); | - | ||||||
189 | } | - | ||||||
190 | - | |||||||
191 | - | |||||||
192 | /*! | - | ||||||
193 | Resolves the undeclared entity \a name and returns its replacement | - | ||||||
194 | text. If the entity is also unknown to the entity resolver, it | - | ||||||
195 | returns an empty string. | - | ||||||
196 | - | |||||||
197 | The default implementation always returns an empty string. | - | ||||||
198 | */ | - | ||||||
199 | - | |||||||
200 | QString QXmlStreamEntityResolver::resolveUndeclaredEntity(const QString &/*name*/) | - | ||||||
201 | { | - | ||||||
202 | return QString(); | - | ||||||
203 | } | - | ||||||
204 | - | |||||||
205 | #ifndef QT_NO_XMLSTREAMREADER | - | ||||||
206 | - | |||||||
207 | QString QXmlStreamReaderPrivate::resolveUndeclaredEntity(const QString &name) | - | ||||||
208 | { | - | ||||||
209 | if (entityResolver) | - | ||||||
210 | return entityResolver->resolveUndeclaredEntity(name); | - | ||||||
211 | return QString(); | - | ||||||
212 | } | - | ||||||
213 | - | |||||||
214 | - | |||||||
215 | - | |||||||
216 | /*! | - | ||||||
217 | \since 4.4 | - | ||||||
218 | - | |||||||
219 | Makes \a resolver the new entityResolver(). | - | ||||||
220 | - | |||||||
221 | The stream reader does \e not take ownership of the resolver. It's | - | ||||||
222 | the callers responsibility to ensure that the resolver is valid | - | ||||||
223 | during the entire life-time of the stream reader object, or until | - | ||||||
224 | another resolver or 0 is set. | - | ||||||
225 | - | |||||||
226 | \sa entityResolver() | - | ||||||
227 | */ | - | ||||||
228 | void QXmlStreamReader::setEntityResolver(QXmlStreamEntityResolver *resolver) | - | ||||||
229 | { | - | ||||||
230 | Q_D(QXmlStreamReader); | - | ||||||
231 | d->entityResolver = resolver; | - | ||||||
232 | } | - | ||||||
233 | - | |||||||
234 | /*! | - | ||||||
235 | \since 4.4 | - | ||||||
236 | - | |||||||
237 | Returns the entity resolver, or 0 if there is no entity resolver. | - | ||||||
238 | - | |||||||
239 | \sa setEntityResolver() | - | ||||||
240 | */ | - | ||||||
241 | QXmlStreamEntityResolver *QXmlStreamReader::entityResolver() const | - | ||||||
242 | { | - | ||||||
243 | Q_D(const QXmlStreamReader); | - | ||||||
244 | return d->entityResolver; | - | ||||||
245 | } | - | ||||||
246 | - | |||||||
247 | - | |||||||
248 | - | |||||||
249 | /*! | - | ||||||
250 | \class QXmlStreamReader | - | ||||||
251 | \inmodule QtCore | - | ||||||
252 | \reentrant | - | ||||||
253 | \since 4.3 | - | ||||||
254 | - | |||||||
255 | \brief The QXmlStreamReader class provides a fast parser for reading | - | ||||||
256 | well-formed XML via a simple streaming API. | - | ||||||
257 | - | |||||||
258 | - | |||||||
259 | \ingroup xml-tools | - | ||||||
260 | - | |||||||
261 | QXmlStreamReader is a faster and more convenient replacement for | - | ||||||
262 | Qt's own SAX parser (see QXmlSimpleReader). In some cases it might | - | ||||||
263 | also be a faster and more convenient alternative for use in | - | ||||||
264 | applications that would otherwise use a DOM tree (see QDomDocument). | - | ||||||
265 | QXmlStreamReader reads data either from a QIODevice (see | - | ||||||
266 | setDevice()), or from a raw QByteArray (see addData()). | - | ||||||
267 | - | |||||||
268 | Qt provides QXmlStreamWriter for writing XML. | - | ||||||
269 | - | |||||||
270 | The basic concept of a stream reader is to report an XML document as | - | ||||||
271 | a stream of tokens, similar to SAX. The main difference between | - | ||||||
272 | QXmlStreamReader and SAX is \e how these XML tokens are reported. | - | ||||||
273 | With SAX, the application must provide handlers (callback functions) | - | ||||||
274 | that receive so-called XML \e events from the parser at the parser's | - | ||||||
275 | convenience. With QXmlStreamReader, the application code itself | - | ||||||
276 | drives the loop and pulls \e tokens from the reader, one after | - | ||||||
277 | another, as it needs them. This is done by calling readNext(), where | - | ||||||
278 | the reader reads from the input stream until it completes the next | - | ||||||
279 | token, at which point it returns the tokenType(). A set of | - | ||||||
280 | convenient functions including isStartElement() and text() can then | - | ||||||
281 | be used to examine the token to obtain information about what has | - | ||||||
282 | been read. The big advantage of this \e pulling approach is the | - | ||||||
283 | possibility to build recursive descent parsers with it, meaning you | - | ||||||
284 | can split your XML parsing code easily into different methods or | - | ||||||
285 | classes. This makes it easy to keep track of the application's own | - | ||||||
286 | state when parsing XML. | - | ||||||
287 | - | |||||||
288 | A typical loop with QXmlStreamReader looks like this: | - | ||||||
289 | - | |||||||
290 | \snippet code/src_corelib_xml_qxmlstream.cpp 0 | - | ||||||
291 | - | |||||||
292 | - | |||||||
293 | QXmlStreamReader is a well-formed XML 1.0 parser that does \e not | - | ||||||
294 | include external parsed entities. As long as no error occurs, the | - | ||||||
295 | application code can thus be assured that the data provided by the | - | ||||||
296 | stream reader satisfies the W3C's criteria for well-formed XML. For | - | ||||||
297 | example, you can be certain that all tags are indeed nested and | - | ||||||
298 | closed properly, that references to internal entities have been | - | ||||||
299 | replaced with the correct replacement text, and that attributes have | - | ||||||
300 | been normalized or added according to the internal subset of the | - | ||||||
301 | DTD. | - | ||||||
302 | - | |||||||
303 | If an error occurs while parsing, atEnd() and hasError() return | - | ||||||
304 | true, and error() returns the error that occurred. The functions | - | ||||||
305 | errorString(), lineNumber(), columnNumber(), and characterOffset() | - | ||||||
306 | are for constructing an appropriate error or warning message. To | - | ||||||
307 | simplify application code, QXmlStreamReader contains a raiseError() | - | ||||||
308 | mechanism that lets you raise custom errors that trigger the same | - | ||||||
309 | error handling described. | - | ||||||
310 | - | |||||||
311 | The \l{QXmlStream Bookmarks Example} illustrates how to use the | - | ||||||
312 | recursive descent technique to read an XML bookmark file (XBEL) with | - | ||||||
313 | a stream reader. | - | ||||||
314 | - | |||||||
315 | \section1 Namespaces | - | ||||||
316 | - | |||||||
317 | QXmlStream understands and resolves XML namespaces. E.g. in case of | - | ||||||
318 | a StartElement, namespaceUri() returns the namespace the element is | - | ||||||
319 | in, and name() returns the element's \e local name. The combination | - | ||||||
320 | of namespaceUri and name uniquely identifies an element. If a | - | ||||||
321 | namespace prefix was not declared in the XML entities parsed by the | - | ||||||
322 | reader, the namespaceUri is empty. | - | ||||||
323 | - | |||||||
324 | If you parse XML data that does not utilize namespaces according to | - | ||||||
325 | the XML specification or doesn't use namespaces at all, you can use | - | ||||||
326 | the element's qualifiedName() instead. A qualified name is the | - | ||||||
327 | element's prefix() followed by colon followed by the element's local | - | ||||||
328 | name() - exactly like the element appears in the raw XML data. Since | - | ||||||
329 | the mapping namespaceUri to prefix is neither unique nor universal, | - | ||||||
330 | qualifiedName() should be avoided for namespace-compliant XML data. | - | ||||||
331 | - | |||||||
332 | In order to parse standalone documents that do use undeclared | - | ||||||
333 | namespace prefixes, you can turn off namespace processing completely | - | ||||||
334 | with the \l namespaceProcessing property. | - | ||||||
335 | - | |||||||
336 | \section1 Incremental Parsing | - | ||||||
337 | - | |||||||
338 | QXmlStreamReader is an incremental parser. It can handle the case | - | ||||||
339 | where the document can't be parsed all at once because it arrives in | - | ||||||
340 | chunks (e.g. from multiple files, or over a network connection). | - | ||||||
341 | When the reader runs out of data before the complete document has | - | ||||||
342 | been parsed, it reports a PrematureEndOfDocumentError. When more | - | ||||||
343 | data arrives, either because of a call to addData() or because more | - | ||||||
344 | data is available through the network device(), the reader recovers | - | ||||||
345 | from the PrematureEndOfDocumentError error and continues parsing the | - | ||||||
346 | new data with the next call to readNext(). | - | ||||||
347 | - | |||||||
348 | For example, if your application reads data from the network using a | - | ||||||
349 | \l{QNetworkAccessManager} {network access manager}, you would issue | - | ||||||
350 | a \l{QNetworkRequest} {network request} to the manager and receive a | - | ||||||
351 | \l{QNetworkReply} {network reply} in return. Since a QNetworkReply | - | ||||||
352 | is a QIODevice, you connect its \l{QIODevice::readyRead()} | - | ||||||
353 | {readyRead()} signal to a custom slot, e.g. \c{slotReadyRead()} in | - | ||||||
354 | the code snippet shown in the discussion for QNetworkAccessManager. | - | ||||||
355 | In this slot, you read all available data with | - | ||||||
356 | \l{QIODevice::readAll()} {readAll()} and pass it to the XML | - | ||||||
357 | stream reader using addData(). Then you call your custom parsing | - | ||||||
358 | function that reads the XML events from the reader. | - | ||||||
359 | - | |||||||
360 | \section1 Performance and Memory Consumption | - | ||||||
361 | - | |||||||
362 | QXmlStreamReader is memory-conservative by design, since it doesn't | - | ||||||
363 | store the entire XML document tree in memory, but only the current | - | ||||||
364 | token at the time it is reported. In addition, QXmlStreamReader | - | ||||||
365 | avoids the many small string allocations that it normally takes to | - | ||||||
366 | map an XML document to a convenient and Qt-ish API. It does this by | - | ||||||
367 | reporting all string data as QStringRef rather than real QString | - | ||||||
368 | objects. QStringRef is a thin wrapper around QString substrings that | - | ||||||
369 | provides a subset of the QString API without the memory allocation | - | ||||||
370 | and reference-counting overhead. Calling | - | ||||||
371 | \l{QStringRef::toString()}{toString()} on any of those objects | - | ||||||
372 | returns an equivalent real QString object. | - | ||||||
373 | - | |||||||
374 | */ | - | ||||||
375 | - | |||||||
376 | - | |||||||
377 | /*! | - | ||||||
378 | Constructs a stream reader. | - | ||||||
379 | - | |||||||
380 | \sa setDevice(), addData() | - | ||||||
381 | */ | - | ||||||
382 | QXmlStreamReader::QXmlStreamReader() | - | ||||||
383 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - | ||||||
384 | { | - | ||||||
385 | } | - | ||||||
386 | - | |||||||
387 | /*! Creates a new stream reader that reads from \a device. | - | ||||||
388 | - | |||||||
389 | \sa setDevice(), clear() | - | ||||||
390 | */ | - | ||||||
391 | QXmlStreamReader::QXmlStreamReader(QIODevice *device) | - | ||||||
392 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - | ||||||
393 | { | - | ||||||
394 | setDevice(device); | - | ||||||
395 | } | - | ||||||
396 | - | |||||||
397 | /*! | - | ||||||
398 | Creates a new stream reader that reads from \a data. | - | ||||||
399 | - | |||||||
400 | \sa addData(), clear(), setDevice() | - | ||||||
401 | */ | - | ||||||
402 | QXmlStreamReader::QXmlStreamReader(const QByteArray &data) | - | ||||||
403 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - | ||||||
404 | { | - | ||||||
405 | Q_D(QXmlStreamReader); | - | ||||||
406 | d->dataBuffer = data; | - | ||||||
407 | } | - | ||||||
408 | - | |||||||
409 | /*! | - | ||||||
410 | Creates a new stream reader that reads from \a data. | - | ||||||
411 | - | |||||||
412 | \sa addData(), clear(), setDevice() | - | ||||||
413 | */ | - | ||||||
414 | QXmlStreamReader::QXmlStreamReader(const QString &data) | - | ||||||
415 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - | ||||||
416 | { | - | ||||||
417 | Q_D(QXmlStreamReader); | - | ||||||
418 | #ifdef QT_NO_TEXTCODEC | - | ||||||
419 | d->dataBuffer = data.toLatin1(); | - | ||||||
420 | #else | - | ||||||
421 | d->dataBuffer = d->codec->fromUnicode(data); | - | ||||||
422 | d->decoder = d->codec->makeDecoder(); | - | ||||||
423 | #endif | - | ||||||
424 | d->lockEncoding = true; | - | ||||||
425 | - | |||||||
426 | } | - | ||||||
427 | - | |||||||
428 | /*! | - | ||||||
429 | Creates a new stream reader that reads from \a data. | - | ||||||
430 | - | |||||||
431 | \sa addData(), clear(), setDevice() | - | ||||||
432 | */ | - | ||||||
433 | QXmlStreamReader::QXmlStreamReader(const char *data) | - | ||||||
434 | : d_ptr(new QXmlStreamReaderPrivate(this)) | - | ||||||
435 | { | - | ||||||
436 | Q_D(QXmlStreamReader); | - | ||||||
437 | d->dataBuffer = QByteArray(data); | - | ||||||
438 | } | - | ||||||
439 | - | |||||||
440 | /*! | - | ||||||
441 | Destructs the reader. | - | ||||||
442 | */ | - | ||||||
443 | QXmlStreamReader::~QXmlStreamReader() | - | ||||||
444 | { | - | ||||||
445 | Q_D(QXmlStreamReader); | - | ||||||
446 | if (d->deleteDevice) | - | ||||||
447 | delete d->device; | - | ||||||
448 | } | - | ||||||
449 | - | |||||||
450 | /*! \fn bool QXmlStreamReader::hasError() const | - | ||||||
451 | Returns \c true if an error has occurred, otherwise \c false. | - | ||||||
452 | - | |||||||
453 | \sa errorString(), error() | - | ||||||
454 | */ | - | ||||||
455 | - | |||||||
456 | /*! | - | ||||||
457 | Sets the current device to \a device. Setting the device resets | - | ||||||
458 | the stream to its initial state. | - | ||||||
459 | - | |||||||
460 | \sa device(), clear() | - | ||||||
461 | */ | - | ||||||
462 | void QXmlStreamReader::setDevice(QIODevice *device) | - | ||||||
463 | { | - | ||||||
464 | Q_D(QXmlStreamReader); | - | ||||||
465 | if (d->deleteDevice) { | - | ||||||
466 | delete d->device; | - | ||||||
467 | d->deleteDevice = false; | - | ||||||
468 | } | - | ||||||
469 | d->device = device; | - | ||||||
470 | d->init(); | - | ||||||
471 | - | |||||||
472 | } | - | ||||||
473 | - | |||||||
474 | /*! | - | ||||||
475 | Returns the current device associated with the QXmlStreamReader, | - | ||||||
476 | or 0 if no device has been assigned. | - | ||||||
477 | - | |||||||
478 | \sa setDevice() | - | ||||||
479 | */ | - | ||||||
480 | QIODevice *QXmlStreamReader::device() const | - | ||||||
481 | { | - | ||||||
482 | Q_D(const QXmlStreamReader); | - | ||||||
483 | return d->device; | - | ||||||
484 | } | - | ||||||
485 | - | |||||||
486 | - | |||||||
487 | /*! | - | ||||||
488 | Adds more \a data for the reader to read. This function does | - | ||||||
489 | nothing if the reader has a device(). | - | ||||||
490 | - | |||||||
491 | \sa readNext(), clear() | - | ||||||
492 | */ | - | ||||||
493 | void QXmlStreamReader::addData(const QByteArray &data) | - | ||||||
494 | { | - | ||||||
495 | Q_D(QXmlStreamReader); | - | ||||||
496 | if (d->device) { | - | ||||||
497 | qWarning("QXmlStreamReader: addData() with device()"); | - | ||||||
498 | return; | - | ||||||
499 | } | - | ||||||
500 | d->dataBuffer += data; | - | ||||||
501 | } | - | ||||||
502 | - | |||||||
503 | /*! | - | ||||||
504 | Adds more \a data for the reader to read. This function does | - | ||||||
505 | nothing if the reader has a device(). | - | ||||||
506 | - | |||||||
507 | \sa readNext(), clear() | - | ||||||
508 | */ | - | ||||||
509 | void QXmlStreamReader::addData(const QString &data) | - | ||||||
510 | { | - | ||||||
511 | Q_D(QXmlStreamReader); | - | ||||||
512 | d->lockEncoding = true; | - | ||||||
513 | #ifdef QT_NO_TEXTCODEC | - | ||||||
514 | addData(data.toLatin1()); | - | ||||||
515 | #else | - | ||||||
516 | addData(d->codec->fromUnicode(data)); | - | ||||||
517 | #endif | - | ||||||
518 | } | - | ||||||
519 | - | |||||||
520 | /*! | - | ||||||
521 | Adds more \a data for the reader to read. This function does | - | ||||||
522 | nothing if the reader has a device(). | - | ||||||
523 | - | |||||||
524 | \sa readNext(), clear() | - | ||||||
525 | */ | - | ||||||
526 | void QXmlStreamReader::addData(const char *data) | - | ||||||
527 | { | - | ||||||
528 | addData(QByteArray(data)); | - | ||||||
529 | } | - | ||||||
530 | - | |||||||
531 | /*! | - | ||||||
532 | Removes any device() or data from the reader and resets its | - | ||||||
533 | internal state to the initial state. | - | ||||||
534 | - | |||||||
535 | \sa addData() | - | ||||||
536 | */ | - | ||||||
537 | void QXmlStreamReader::clear() | - | ||||||
538 | { | - | ||||||
539 | Q_D(QXmlStreamReader); | - | ||||||
540 | d->init(); | - | ||||||
541 | if (d->device) { | - | ||||||
542 | if (d->deleteDevice) | - | ||||||
543 | delete d->device; | - | ||||||
544 | d->device = 0; | - | ||||||
545 | } | - | ||||||
546 | } | - | ||||||
547 | - | |||||||
548 | /*! | - | ||||||
549 | Returns \c true if the reader has read until the end of the XML | - | ||||||
550 | document, or if an error() has occurred and reading has been | - | ||||||
551 | aborted. Otherwise, it returns \c false. | - | ||||||
552 | - | |||||||
553 | When atEnd() and hasError() return true and error() returns | - | ||||||
554 | PrematureEndOfDocumentError, it means the XML has been well-formed | - | ||||||
555 | so far, but a complete XML document has not been parsed. The next | - | ||||||
556 | chunk of XML can be added with addData(), if the XML is being read | - | ||||||
557 | from a QByteArray, or by waiting for more data to arrive if the | - | ||||||
558 | XML is being read from a QIODevice. Either way, atEnd() will | - | ||||||
559 | return false once more data is available. | - | ||||||
560 | - | |||||||
561 | \sa hasError(), error(), device(), QIODevice::atEnd() | - | ||||||
562 | */ | - | ||||||
563 | bool QXmlStreamReader::atEnd() const | - | ||||||
564 | { | - | ||||||
565 | Q_D(const QXmlStreamReader); | - | ||||||
566 | if (d->atEnd | - | ||||||
567 | && ((d->type == QXmlStreamReader::Invalid && d->error == PrematureEndOfDocumentError) | - | ||||||
568 | || (d->type == QXmlStreamReader::EndDocument))) { | - | ||||||
569 | if (d->device) | - | ||||||
570 | return d->device->atEnd(); | - | ||||||
571 | else | - | ||||||
572 | return !d->dataBuffer.size(); | - | ||||||
573 | } | - | ||||||
574 | return (d->atEnd || d->type == QXmlStreamReader::Invalid); | - | ||||||
575 | } | - | ||||||
576 | - | |||||||
577 | - | |||||||
578 | /*! | - | ||||||
579 | Reads the next token and returns its type. | - | ||||||
580 | - | |||||||
581 | With one exception, once an error() is reported by readNext(), | - | ||||||
582 | further reading of the XML stream is not possible. Then atEnd() | - | ||||||
583 | returns \c true, hasError() returns \c true, and this function returns | - | ||||||
584 | QXmlStreamReader::Invalid. | - | ||||||
585 | - | |||||||
586 | The exception is when error() returns PrematureEndOfDocumentError. | - | ||||||
587 | This error is reported when the end of an otherwise well-formed | - | ||||||
588 | chunk of XML is reached, but the chunk doesn't represent a complete | - | ||||||
589 | XML document. In that case, parsing \e can be resumed by calling | - | ||||||
590 | addData() to add the next chunk of XML, when the stream is being | - | ||||||
591 | read from a QByteArray, or by waiting for more data to arrive when | - | ||||||
592 | the stream is being read from a device(). | - | ||||||
593 | - | |||||||
594 | \sa tokenType(), tokenString() | - | ||||||
595 | */ | - | ||||||
596 | QXmlStreamReader::TokenType QXmlStreamReader::readNext() | - | ||||||
597 | { | - | ||||||
598 | Q_D(QXmlStreamReader); | - | ||||||
599 | if (d->type != Invalid) { | - | ||||||
600 | if (!d->hasCheckedStartDocument) | - | ||||||
601 | if (!d->checkStartDocument()) | - | ||||||
602 | return d->type; // synthetic StartDocument or error | - | ||||||
603 | d->parse(); | - | ||||||
604 | if (d->atEnd && d->type != EndDocument && d->type != Invalid) | - | ||||||
605 | d->raiseError(PrematureEndOfDocumentError); | - | ||||||
606 | else if (!d->atEnd && d->type == EndDocument) | - | ||||||
607 | d->raiseWellFormedError(QXmlStream::tr("Extra content at end of document.")); | - | ||||||
608 | } else if (d->error == PrematureEndOfDocumentError) { | - | ||||||
609 | // resume error | - | ||||||
610 | d->type = NoToken; | - | ||||||
611 | d->atEnd = false; | - | ||||||
612 | d->token = -1; | - | ||||||
613 | return readNext(); | - | ||||||
614 | } | - | ||||||
615 | return d->type; | - | ||||||
616 | } | - | ||||||
617 | - | |||||||
618 | - | |||||||
619 | /*! | - | ||||||
620 | Returns the type of the current token. | - | ||||||
621 | - | |||||||
622 | The current token can also be queried with the convenience functions | - | ||||||
623 | isStartDocument(), isEndDocument(), isStartElement(), | - | ||||||
624 | isEndElement(), isCharacters(), isComment(), isDTD(), | - | ||||||
625 | isEntityReference(), and isProcessingInstruction(). | - | ||||||
626 | - | |||||||
627 | \sa tokenString() | - | ||||||
628 | */ | - | ||||||
629 | QXmlStreamReader::TokenType QXmlStreamReader::tokenType() const | - | ||||||
630 | { | - | ||||||
631 | Q_D(const QXmlStreamReader); | - | ||||||
632 | return d->type; | - | ||||||
633 | } | - | ||||||
634 | - | |||||||
635 | /*! | - | ||||||
636 | Reads until the next start element within the current element. Returns \c true | - | ||||||
637 | when a start element was reached. When the end element was reached, or when | - | ||||||
638 | an error occurred, false is returned. | - | ||||||
639 | - | |||||||
640 | The current element is the element matching the most recently parsed start | - | ||||||
641 | element of which a matching end element has not yet been reached. When the | - | ||||||
642 | parser has reached the end element, the current element becomes the parent | - | ||||||
643 | element. | - | ||||||
644 | - | |||||||
645 | This is a convenience function for when you're only concerned with parsing | - | ||||||
646 | XML elements. The \l{QXmlStream Bookmarks Example} makes extensive use of | - | ||||||
647 | this function. | - | ||||||
648 | - | |||||||
649 | \since 4.6 | - | ||||||
650 | \sa readNext() | - | ||||||
651 | */ | - | ||||||
652 | bool QXmlStreamReader::readNextStartElement() | - | ||||||
653 | { | - | ||||||
654 | while (readNext() != Invalid) { | - | ||||||
655 | if (isEndElement()) | - | ||||||
656 | return false; | - | ||||||
657 | else if (isStartElement()) | - | ||||||
658 | return true; | - | ||||||
659 | } | - | ||||||
660 | return false; | - | ||||||
661 | } | - | ||||||
662 | - | |||||||
663 | /*! | - | ||||||
664 | Reads until the end of the current element, skipping any child nodes. | - | ||||||
665 | This function is useful for skipping unknown elements. | - | ||||||
666 | - | |||||||
667 | The current element is the element matching the most recently parsed start | - | ||||||
668 | element of which a matching end element has not yet been reached. When the | - | ||||||
669 | parser has reached the end element, the current element becomes the parent | - | ||||||
670 | element. | - | ||||||
671 | - | |||||||
672 | \since 4.6 | - | ||||||
673 | */ | - | ||||||
674 | void QXmlStreamReader::skipCurrentElement() | - | ||||||
675 | { | - | ||||||
676 | int depth = 1; | - | ||||||
677 | while (depth && readNext() != Invalid) { | - | ||||||
678 | if (isEndElement()) | - | ||||||
679 | --depth; | - | ||||||
680 | else if (isStartElement()) | - | ||||||
681 | ++depth; | - | ||||||
682 | } | - | ||||||
683 | } | - | ||||||
684 | - | |||||||
685 | /* | - | ||||||
686 | * Use the following Perl script to generate the error string index list: | - | ||||||
687 | ===== PERL SCRIPT ==== | - | ||||||
688 | print "static const char QXmlStreamReader_tokenTypeString_string[] =\n"; | - | ||||||
689 | $counter = 0; | - | ||||||
690 | $i = 0; | - | ||||||
691 | while (<STDIN>) { | - | ||||||
692 | chomp; | - | ||||||
693 | print " \"$_\\0\"\n"; | - | ||||||
694 | $sizes[$i++] = $counter; | - | ||||||
695 | $counter += length 1 + $_; | - | ||||||
696 | } | - | ||||||
697 | print " \"\\0\";\n\nstatic const short QXmlStreamReader_tokenTypeString_indices[] = {\n "; | - | ||||||
698 | for ($j = 0; $j < $i; ++$j) { | - | ||||||
699 | printf "$sizes[$j], "; | - | ||||||
700 | } | - | ||||||
701 | print "0\n};\n"; | - | ||||||
702 | ===== PERL SCRIPT ==== | - | ||||||
703 | - | |||||||
704 | * The input data is as follows (copied from qxmlstream.h): | - | ||||||
705 | NoToken | - | ||||||
706 | Invalid | - | ||||||
707 | StartDocument | - | ||||||
708 | EndDocument | - | ||||||
709 | StartElement | - | ||||||
710 | EndElement | - | ||||||
711 | Characters | - | ||||||
712 | Comment | - | ||||||
713 | DTD | - | ||||||
714 | EntityReference | - | ||||||
715 | ProcessingInstruction | - | ||||||
716 | */ | - | ||||||
717 | static const char QXmlStreamReader_tokenTypeString_string[] = | - | ||||||
718 | "NoToken\0" | - | ||||||
719 | "Invalid\0" | - | ||||||
720 | "StartDocument\0" | - | ||||||
721 | "EndDocument\0" | - | ||||||
722 | "StartElement\0" | - | ||||||
723 | "EndElement\0" | - | ||||||
724 | "Characters\0" | - | ||||||
725 | "Comment\0" | - | ||||||
726 | "DTD\0" | - | ||||||
727 | "EntityReference\0" | - | ||||||
728 | "ProcessingInstruction\0"; | - | ||||||
729 | - | |||||||
730 | static const short QXmlStreamReader_tokenTypeString_indices[] = { | - | ||||||
731 | 0, 8, 16, 30, 42, 55, 66, 77, 85, 89, 105, 0 | - | ||||||
732 | }; | - | ||||||
733 | - | |||||||
734 | - | |||||||
735 | /*! | - | ||||||
736 | \property QXmlStreamReader::namespaceProcessing | - | ||||||
737 | The namespace-processing flag of the stream reader | - | ||||||
738 | - | |||||||
739 | This property controls whether or not the stream reader processes | - | ||||||
740 | namespaces. If enabled, the reader processes namespaces, otherwise | - | ||||||
741 | it does not. | - | ||||||
742 | - | |||||||
743 | By default, namespace-processing is enabled. | - | ||||||
744 | */ | - | ||||||
745 | - | |||||||
746 | - | |||||||
747 | void QXmlStreamReader::setNamespaceProcessing(bool enable) | - | ||||||
748 | { | - | ||||||
749 | Q_D(QXmlStreamReader); | - | ||||||
750 | d->namespaceProcessing = enable; | - | ||||||
751 | } | - | ||||||
752 | - | |||||||
753 | bool QXmlStreamReader::namespaceProcessing() const | - | ||||||
754 | { | - | ||||||
755 | Q_D(const QXmlStreamReader); | - | ||||||
756 | return d->namespaceProcessing; | - | ||||||
757 | } | - | ||||||
758 | - | |||||||
759 | /*! Returns the reader's current token as string. | - | ||||||
760 | - | |||||||
761 | \sa tokenType() | - | ||||||
762 | */ | - | ||||||
763 | QString QXmlStreamReader::tokenString() const | - | ||||||
764 | { | - | ||||||
765 | Q_D(const QXmlStreamReader); | - | ||||||
766 | return QLatin1String(QXmlStreamReader_tokenTypeString_string + | - | ||||||
767 | QXmlStreamReader_tokenTypeString_indices[d->type]); | - | ||||||
768 | } | - | ||||||
769 | - | |||||||
770 | #endif // QT_NO_XMLSTREAMREADER | - | ||||||
771 | - | |||||||
772 | QXmlStreamPrivateTagStack::QXmlStreamPrivateTagStack() | - | ||||||
773 | { | - | ||||||
774 | tagStack.reserve(16); | - | ||||||
775 | tagStackStringStorage.reserve(32); | - | ||||||
776 | tagStackStringStorageSize = 0; | - | ||||||
777 | NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); | - | ||||||
778 | namespaceDeclaration.prefix = addToStringStorage(QLatin1String("xml")); | - | ||||||
779 | namespaceDeclaration.namespaceUri = addToStringStorage(QLatin1String("http://www.w3.org/XML/1998/namespace")); | - | ||||||
780 | initialTagStackStringStorageSize = tagStackStringStorageSize; | - | ||||||
781 | } | - | ||||||
782 | - | |||||||
783 | #ifndef QT_NO_XMLSTREAMREADER | - | ||||||
784 | - | |||||||
785 | QXmlStreamReaderPrivate::QXmlStreamReaderPrivate(QXmlStreamReader *q) | - | ||||||
786 | :q_ptr(q) | - | ||||||
787 | { | - | ||||||
788 | device = 0; | - | ||||||
789 | deleteDevice = false; | - | ||||||
790 | #ifndef QT_NO_TEXTCODEC | - | ||||||
791 | decoder = 0; | - | ||||||
792 | #endif | - | ||||||
793 | stack_size = 64; | - | ||||||
794 | sym_stack = 0; | - | ||||||
795 | state_stack = 0; | - | ||||||
796 | reallocateStack(); | - | ||||||
797 | entityResolver = 0; | - | ||||||
798 | init(); | - | ||||||
799 | entityHash.insert(QLatin1String("lt"), Entity::createLiteral(QLatin1String("<"))); | - | ||||||
800 | entityHash.insert(QLatin1String("gt"), Entity::createLiteral(QLatin1String(">"))); | - | ||||||
801 | entityHash.insert(QLatin1String("amp"), Entity::createLiteral(QLatin1String("&"))); | - | ||||||
802 | entityHash.insert(QLatin1String("apos"), Entity::createLiteral(QLatin1String("'"))); | - | ||||||
803 | entityHash.insert(QLatin1String("quot"), Entity::createLiteral(QLatin1String("\""))); | - | ||||||
804 | } | - | ||||||
805 | - | |||||||
806 | void QXmlStreamReaderPrivate::init() | - | ||||||
807 | { | - | ||||||
808 | scanDtd = false; | - | ||||||
809 | token = -1; | - | ||||||
810 | token_char = 0; | - | ||||||
811 | isEmptyElement = false; | - | ||||||
812 | isWhitespace = true; | - | ||||||
813 | isCDATA = false; | - | ||||||
814 | standalone = false; | - | ||||||
815 | tos = 0; | - | ||||||
816 | resumeReduction = 0; | - | ||||||
817 | state_stack[tos++] = 0; | - | ||||||
818 | state_stack[tos] = 0; | - | ||||||
819 | putStack.clear(); | - | ||||||
820 | putStack.reserve(32); | - | ||||||
821 | textBuffer.clear(); | - | ||||||
822 | textBuffer.reserve(256); | - | ||||||
823 | tagStack.clear(); | - | ||||||
824 | tagsDone = false; | - | ||||||
825 | attributes.clear(); | - | ||||||
826 | attributes.reserve(16); | - | ||||||
827 | lineNumber = lastLineStart = characterOffset = 0; | - | ||||||
828 | readBufferPos = 0; | - | ||||||
829 | nbytesread = 0; | - | ||||||
830 | #ifndef QT_NO_TEXTCODEC | - | ||||||
831 | codec = QTextCodec::codecForMib(106); // utf8 | - | ||||||
832 | delete decoder; | - | ||||||
833 | decoder = 0; | - | ||||||
834 | #endif | - | ||||||
835 | attributeStack.clear(); | - | ||||||
836 | attributeStack.reserve(16); | - | ||||||
837 | entityParser = 0; | - | ||||||
838 | hasCheckedStartDocument = false; | - | ||||||
839 | normalizeLiterals = false; | - | ||||||
840 | hasSeenTag = false; | - | ||||||
841 | atEnd = false; | - | ||||||
842 | inParseEntity = false; | - | ||||||
843 | referenceToUnparsedEntityDetected = false; | - | ||||||
844 | referenceToParameterEntityDetected = false; | - | ||||||
845 | hasExternalDtdSubset = false; | - | ||||||
846 | lockEncoding = false; | - | ||||||
847 | namespaceProcessing = true; | - | ||||||
848 | rawReadBuffer.clear(); | - | ||||||
849 | dataBuffer.clear(); | - | ||||||
850 | readBuffer.clear(); | - | ||||||
851 | tagStackStringStorageSize = initialTagStackStringStorageSize; | - | ||||||
852 | - | |||||||
853 | type = QXmlStreamReader::NoToken; | - | ||||||
854 | error = QXmlStreamReader::NoError; | - | ||||||
855 | } | - | ||||||
856 | - | |||||||
857 | /* | - | ||||||
858 | Well-formed requires that we verify entity values. We do this with a | - | ||||||
859 | standard parser. | - | ||||||
860 | */ | - | ||||||
861 | void QXmlStreamReaderPrivate::parseEntity(const QString &value) | - | ||||||
862 | { | - | ||||||
863 | Q_Q(QXmlStreamReader); | - | ||||||
864 | - | |||||||
865 | if (value.isEmpty()) | - | ||||||
866 | return; | - | ||||||
867 | - | |||||||
868 | - | |||||||
869 | if (!entityParser) | - | ||||||
870 | entityParser = new QXmlStreamReaderPrivate(q); | - | ||||||
871 | else | - | ||||||
872 | entityParser->init(); | - | ||||||
873 | entityParser->inParseEntity = true; | - | ||||||
874 | entityParser->readBuffer = value; | - | ||||||
875 | entityParser->injectToken(PARSE_ENTITY); | - | ||||||
876 | while (!entityParser->atEnd && entityParser->type != QXmlStreamReader::Invalid) | - | ||||||
877 | entityParser->parse(); | - | ||||||
878 | if (entityParser->type == QXmlStreamReader::Invalid || entityParser->tagStack.size()) | - | ||||||
879 | raiseWellFormedError(QXmlStream::tr("Invalid entity value.")); | - | ||||||
880 | - | |||||||
881 | } | - | ||||||
882 | - | |||||||
883 | inline void QXmlStreamReaderPrivate::reallocateStack() | - | ||||||
884 | { | - | ||||||
885 | stack_size <<= 1; | - | ||||||
886 | sym_stack = reinterpret_cast<Value*> (realloc(sym_stack, stack_size * sizeof(Value))); | - | ||||||
887 | Q_CHECK_PTR(sym_stack); | - | ||||||
888 | state_stack = reinterpret_cast<int*> (realloc(state_stack, stack_size * sizeof(int))); | - | ||||||
889 | Q_CHECK_PTR(state_stack); | - | ||||||
890 | } | - | ||||||
891 | - | |||||||
892 | - | |||||||
893 | QXmlStreamReaderPrivate::~QXmlStreamReaderPrivate() | - | ||||||
894 | { | - | ||||||
895 | #ifndef QT_NO_TEXTCODEC | - | ||||||
896 | delete decoder; | - | ||||||
897 | #endif | - | ||||||
898 | free(sym_stack); | - | ||||||
899 | free(state_stack); | - | ||||||
900 | delete entityParser; | - | ||||||
901 | } | - | ||||||
902 | - | |||||||
903 | - | |||||||
904 | inline uint QXmlStreamReaderPrivate::filterCarriageReturn() | - | ||||||
905 | { | - | ||||||
906 | uint peekc = peekChar(); | - | ||||||
907 | if (peekc == '\n') { | - | ||||||
908 | if (putStack.size()) | - | ||||||
909 | putStack.pop(); | - | ||||||
910 | else | - | ||||||
911 | ++readBufferPos; | - | ||||||
912 | return peekc; | - | ||||||
913 | } | - | ||||||
914 | if (peekc == StreamEOF) { | - | ||||||
915 | putChar('\r'); | - | ||||||
916 | return 0; | - | ||||||
917 | } | - | ||||||
918 | return '\n'; | - | ||||||
919 | } | - | ||||||
920 | - | |||||||
921 | /*! | - | ||||||
922 | \internal | - | ||||||
923 | If the end of the file is encountered, ~0 is returned. | - | ||||||
924 | */ | - | ||||||
925 | inline uint QXmlStreamReaderPrivate::getChar() | - | ||||||
926 | { | - | ||||||
927 | uint c; | - | ||||||
928 | if (putStack.size()) { | - | ||||||
929 | c = atEnd ? StreamEOF : putStack.pop(); | - | ||||||
930 | } else { | - | ||||||
931 | if (readBufferPos < readBuffer.size()) | - | ||||||
932 | c = readBuffer.at(readBufferPos++).unicode(); | - | ||||||
933 | else | - | ||||||
934 | c = getChar_helper(); | - | ||||||
935 | } | - | ||||||
936 | - | |||||||
937 | return c; | - | ||||||
938 | } | - | ||||||
939 | - | |||||||
940 | inline uint QXmlStreamReaderPrivate::peekChar() | - | ||||||
941 | { | - | ||||||
942 | uint c; | - | ||||||
943 | if (putStack.size()) { | - | ||||||
944 | c = putStack.top(); | - | ||||||
945 | } else if (readBufferPos < readBuffer.size()) { | - | ||||||
946 | c = readBuffer.at(readBufferPos).unicode(); | - | ||||||
947 | } else { | - | ||||||
948 | if ((c = getChar_helper()) != StreamEOF) | - | ||||||
949 | --readBufferPos; | - | ||||||
950 | } | - | ||||||
951 | - | |||||||
952 | return c; | - | ||||||
953 | } | - | ||||||
954 | - | |||||||
955 | /*! | - | ||||||
956 | \internal | - | ||||||
957 | - | |||||||
958 | Scans characters until \a str is encountered, and validates the characters | - | ||||||
959 | as according to the Char[2] production and do the line-ending normalization. | - | ||||||
960 | If any character is invalid, false is returned, otherwise true upon success. | - | ||||||
961 | - | |||||||
962 | If \a tokenToInject is not less than zero, injectToken() is called with | - | ||||||
963 | \a tokenToInject when \a str is found. | - | ||||||
964 | - | |||||||
965 | If any error occurred, false is returned, otherwise true. | - | ||||||
966 | */ | - | ||||||
967 | bool QXmlStreamReaderPrivate::scanUntil(const char *str, short tokenToInject) | - | ||||||
968 | { | - | ||||||
969 | int pos = textBuffer.size(); | - | ||||||
970 | int oldLineNumber = lineNumber; | - | ||||||
971 | - | |||||||
972 | uint c; | - | ||||||
973 | while ((c = getChar()) != StreamEOF) { | - | ||||||
974 | /* First, we do the validation & normalization. */ | - | ||||||
975 | switch (c) { | - | ||||||
976 | case '\r': | - | ||||||
977 | if ((c = filterCarriageReturn()) == 0) | - | ||||||
978 | break; | - | ||||||
979 | // fall through | - | ||||||
980 | case '\n': | - | ||||||
981 | ++lineNumber; | - | ||||||
982 | lastLineStart = characterOffset + readBufferPos; | - | ||||||
983 | // fall through | - | ||||||
984 | case '\t': | - | ||||||
985 | textBuffer += QChar(c); | - | ||||||
986 | continue; | - | ||||||
987 | default: | - | ||||||
988 | if (c < 0x20 || (c > 0xFFFD && c < 0x10000) || c > QChar::LastValidCodePoint ) { | - | ||||||
989 | raiseWellFormedError(QXmlStream::tr("Invalid XML character.")); | - | ||||||
990 | lineNumber = oldLineNumber; | - | ||||||
991 | return false; | - | ||||||
992 | } | - | ||||||
993 | textBuffer += QChar(c); | - | ||||||
994 | } | - | ||||||
995 | - | |||||||
996 | - | |||||||
997 | /* Second, attempt to lookup str. */ | - | ||||||
998 | if (c == uint(*str)) { | - | ||||||
999 | if (!*(str + 1)) { | - | ||||||
1000 | if (tokenToInject >= 0) | - | ||||||
1001 | injectToken(tokenToInject); | - | ||||||
1002 | return true; | - | ||||||
1003 | } else { | - | ||||||
1004 | if (scanString(str + 1, tokenToInject, false)) | - | ||||||
1005 | return true; | - | ||||||
1006 | } | - | ||||||
1007 | } | - | ||||||
1008 | } | - | ||||||
1009 | putString(textBuffer, pos); | - | ||||||
1010 | textBuffer.resize(pos); | - | ||||||
1011 | lineNumber = oldLineNumber; | - | ||||||
1012 | return false; | - | ||||||
1013 | } | - | ||||||
1014 | - | |||||||
1015 | bool QXmlStreamReaderPrivate::scanString(const char *str, short tokenToInject, bool requireSpace) | - | ||||||
1016 | { | - | ||||||
1017 | int n = 0; | - | ||||||
1018 | while (str[n]) { | - | ||||||
1019 | uint c = getChar(); | - | ||||||
1020 | if (c != ushort(str[n])) { | - | ||||||
1021 | if (c != StreamEOF) | - | ||||||
1022 | putChar(c); | - | ||||||
1023 | while (n--) { | - | ||||||
1024 | putChar(ushort(str[n])); | - | ||||||
1025 | } | - | ||||||
1026 | return false; | - | ||||||
1027 | } | - | ||||||
1028 | ++n; | - | ||||||
1029 | } | - | ||||||
1030 | for (int i = 0; i < n; ++i) | - | ||||||
1031 | textBuffer += QChar(ushort(str[i])); | - | ||||||
1032 | if (requireSpace) { | - | ||||||
1033 | int s = fastScanSpace(); | - | ||||||
1034 | if (!s || atEnd) { | - | ||||||
1035 | int pos = textBuffer.size() - n - s; | - | ||||||
1036 | putString(textBuffer, pos); | - | ||||||
1037 | textBuffer.resize(pos); | - | ||||||
1038 | return false; | - | ||||||
1039 | } | - | ||||||
1040 | } | - | ||||||
1041 | if (tokenToInject >= 0) | - | ||||||
1042 | injectToken(tokenToInject); | - | ||||||
1043 | return true; | - | ||||||
1044 | } | - | ||||||
1045 | - | |||||||
1046 | bool QXmlStreamReaderPrivate::scanAfterLangleBang() | - | ||||||
1047 | { | - | ||||||
1048 | switch (peekChar()) { | - | ||||||
1049 | case '[': | - | ||||||
1050 | return scanString(spell[CDATA_START], CDATA_START, false); | - | ||||||
1051 | case 'D': | - | ||||||
1052 | return scanString(spell[DOCTYPE], DOCTYPE); | - | ||||||
1053 | case 'A': | - | ||||||
1054 | return scanString(spell[ATTLIST], ATTLIST); | - | ||||||
1055 | case 'N': | - | ||||||
1056 | return scanString(spell[NOTATION], NOTATION); | - | ||||||
1057 | case 'E': | - | ||||||
1058 | if (scanString(spell[ELEMENT], ELEMENT)) | - | ||||||
1059 | return true; | - | ||||||
1060 | return scanString(spell[ENTITY], ENTITY); | - | ||||||
1061 | - | |||||||
1062 | default: | - | ||||||
1063 | ; | - | ||||||
1064 | }; | - | ||||||
1065 | return false; | - | ||||||
1066 | } | - | ||||||
1067 | - | |||||||
1068 | bool QXmlStreamReaderPrivate::scanPublicOrSystem() | - | ||||||
1069 | { | - | ||||||
1070 | switch (peekChar()) { | - | ||||||
1071 | case 'S': | - | ||||||
1072 | return scanString(spell[SYSTEM], SYSTEM); | - | ||||||
1073 | case 'P': | - | ||||||
1074 | return scanString(spell[PUBLIC], PUBLIC); | - | ||||||
1075 | default: | - | ||||||
1076 | ; | - | ||||||
1077 | } | - | ||||||
1078 | return false; | - | ||||||
1079 | } | - | ||||||
1080 | - | |||||||
1081 | bool QXmlStreamReaderPrivate::scanNData() | - | ||||||
1082 | { | - | ||||||
1083 | if (fastScanSpace()) { | - | ||||||
1084 | if (scanString(spell[NDATA], NDATA)) | - | ||||||
1085 | return true; | - | ||||||
1086 | putChar(' '); | - | ||||||
1087 | } | - | ||||||
1088 | return false; | - | ||||||
1089 | } | - | ||||||
1090 | - | |||||||
1091 | bool QXmlStreamReaderPrivate::scanAfterDefaultDecl() | - | ||||||
1092 | { | - | ||||||
1093 | switch (peekChar()) { | - | ||||||
1094 | case 'R': | - | ||||||
1095 | return scanString(spell[REQUIRED], REQUIRED, false); | - | ||||||
1096 | case 'I': | - | ||||||
1097 | return scanString(spell[IMPLIED], IMPLIED, false); | - | ||||||
1098 | case 'F': | - | ||||||
1099 | return scanString(spell[FIXED], FIXED, false); | - | ||||||
1100 | default: | - | ||||||
1101 | ; | - | ||||||
1102 | } | - | ||||||
1103 | return false; | - | ||||||
1104 | } | - | ||||||
1105 | - | |||||||
1106 | bool QXmlStreamReaderPrivate::scanAttType() | - | ||||||
1107 | { | - | ||||||
1108 | switch (peekChar()) { | - | ||||||
1109 | case 'C': | - | ||||||
1110 | return scanString(spell[CDATA], CDATA); | - | ||||||
1111 | case 'I': | - | ||||||
1112 | if (scanString(spell[ID], ID)) | - | ||||||
1113 | return true; | - | ||||||
1114 | if (scanString(spell[IDREF], IDREF)) | - | ||||||
1115 | return true; | - | ||||||
1116 | return scanString(spell[IDREFS], IDREFS); | - | ||||||
1117 | case 'E': | - | ||||||
1118 | if (scanString(spell[ENTITY], ENTITY)) | - | ||||||
1119 | return true; | - | ||||||
1120 | return scanString(spell[ENTITIES], ENTITIES); | - | ||||||
1121 | case 'N': | - | ||||||
1122 | if (scanString(spell[NOTATION], NOTATION)) | - | ||||||
1123 | return true; | - | ||||||
1124 | if (scanString(spell[NMTOKEN], NMTOKEN)) | - | ||||||
1125 | return true; | - | ||||||
1126 | return scanString(spell[NMTOKENS], NMTOKENS); | - | ||||||
1127 | default: | - | ||||||
1128 | ; | - | ||||||
1129 | } | - | ||||||
1130 | return false; | - | ||||||
1131 | } | - | ||||||
1132 | - | |||||||
1133 | /*! | - | ||||||
1134 | \internal | - | ||||||
1135 | - | |||||||
1136 | Scan strings with quotes or apostrophes surround them. For instance, | - | ||||||
1137 | attributes, the version and encoding field in the XML prolog and | - | ||||||
1138 | entity declarations. | - | ||||||
1139 | - | |||||||
1140 | If normalizeLiterals is set to true, the function also normalizes | - | ||||||
1141 | whitespace. It is set to true when the first start tag is | - | ||||||
1142 | encountered. | - | ||||||
1143 | - | |||||||
1144 | */ | - | ||||||
1145 | inline int QXmlStreamReaderPrivate::fastScanLiteralContent() | - | ||||||
1146 | { | - | ||||||
1147 | int n = 0; | - | ||||||
1148 | uint c; | - | ||||||
1149 | while ((c = getChar()) != StreamEOF) { | - | ||||||
1150 | switch (ushort(c)) { | - | ||||||
1151 | case 0xfffe: | - | ||||||
1152 | case 0xffff: | - | ||||||
1153 | case 0: | - | ||||||
1154 | /* The putChar() call is necessary so the parser re-gets | - | ||||||
1155 | * the character from the input source, when raising an error. */ | - | ||||||
1156 | putChar(c); | - | ||||||
1157 | return n; | - | ||||||
1158 | case '\r': | - | ||||||
1159 | if (filterCarriageReturn() == 0) | - | ||||||
1160 | return n; | - | ||||||
1161 | // fall through | - | ||||||
1162 | case '\n': | - | ||||||
1163 | ++lineNumber; | - | ||||||
1164 | lastLineStart = characterOffset + readBufferPos; | - | ||||||
1165 | // fall through | - | ||||||
1166 | case ' ': | - | ||||||
1167 | case '\t': | - | ||||||
1168 | if (normalizeLiterals) | - | ||||||
1169 | textBuffer += QLatin1Char(' '); | - | ||||||
1170 | else | - | ||||||
1171 | textBuffer += QChar(c); | - | ||||||
1172 | ++n; | - | ||||||
1173 | break; | - | ||||||
1174 | case '&': | - | ||||||
1175 | case '<': | - | ||||||
1176 | case '\"': | - | ||||||
1177 | case '\'': | - | ||||||
1178 | if (!(c & 0xff0000)) { | - | ||||||
1179 | putChar(c); | - | ||||||
1180 | return n; | - | ||||||
1181 | } | - | ||||||
1182 | // fall through | - | ||||||
1183 | default: | - | ||||||
1184 | if (c < 0x20) { | - | ||||||
1185 | putChar(c); | - | ||||||
1186 | return n; | - | ||||||
1187 | } | - | ||||||
1188 | textBuffer += QChar(c); | - | ||||||
1189 | ++n; | - | ||||||
1190 | } | - | ||||||
1191 | } | - | ||||||
1192 | return n; | - | ||||||
1193 | } | - | ||||||
1194 | - | |||||||
1195 | inline int QXmlStreamReaderPrivate::fastScanSpace() | - | ||||||
1196 | { | - | ||||||
1197 | int n = 0; | - | ||||||
1198 | uint c; | - | ||||||
1199 | while ((c = getChar()) != StreamEOF) { | - | ||||||
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()) != StreamEOF) { | - | ||||||
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; dead code: 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 | uint c; | - | ||||||
1296 | while ((c = getChar()) != StreamEOF) { | - | ||||||
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()) != StreamEOF) { | - | ||||||
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 | uint 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 StreamEOF; | - | ||||||
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 StreamEOF; | - | ||||||
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 StreamEOF; | - | ||||||
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 StreamEOF; | - | ||||||
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 '%1' redefined.").arg(attribute.qualifiedName().toString())); | - | ||||||
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).toUInt(&ok, 16); | - | ||||||
1720 | else | - | ||||||
1721 | s = symString(symbolIndex).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 \c true; otherwise it sets up everything | - | ||||||
1760 | for a synthetic start document event and returns \c 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.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 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 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 | /*! | - | ||||||
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 | /*! | - | ||||||
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 | /*! | - | ||||||
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 tokenType() is \l StartElement, this function returns the | - | ||||||
2036 | element's namespace declarations. Otherwise an empty vector is | - | ||||||
2037 | returned. | - | ||||||
2038 | - | |||||||
2039 | The QXmlStreamNamespaceDeclarations 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 \c true if the parser added this attribute with a default | - | ||||||
2349 | value following an ATTLIST declaration in the DTD; otherwise | - | ||||||
2350 | returns \c false. | - | ||||||
2351 | */ | - | ||||||
2352 | /*! \fn bool QXmlStreamAttribute::operator==(const QXmlStreamAttribute &other) const | - | ||||||
2353 | - | |||||||
2354 | Compares this attribute with \a other and returns \c true if they are | - | ||||||
2355 | equal; otherwise returns \c false. | - | ||||||
2356 | */ | - | ||||||
2357 | /*! \fn bool QXmlStreamAttribute::operator!=(const QXmlStreamAttribute &other) const | - | ||||||
2358 | - | |||||||
2359 | Compares this attribute with \a other and returns \c true if they are | - | ||||||
2360 | not equal; otherwise returns \c 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 QXmlStreamAttributes::QXmlStreamAttributes() | - | ||||||
2410 | - | |||||||
2411 | A constructor for QXmlStreamAttributes. | - | ||||||
2412 | */ | - | ||||||
2413 | - | |||||||
2414 | /*! | - | ||||||
2415 | \typedef QXmlStreamNotationDeclarations | - | ||||||
2416 | \relates QXmlStreamNotationDeclaration | - | ||||||
2417 | - | |||||||
2418 | Synonym for QVector<QXmlStreamNotationDeclaration>. | - | ||||||
2419 | */ | - | ||||||
2420 | - | |||||||
2421 | - | |||||||
2422 | /*! | - | ||||||
2423 | \class QXmlStreamNotationDeclaration | - | ||||||
2424 | \inmodule QtCore | - | ||||||
2425 | \since 4.3 | - | ||||||
2426 | \reentrant | - | ||||||
2427 | \brief The QXmlStreamNotationDeclaration class represents a DTD notation declaration. | - | ||||||
2428 | - | |||||||
2429 | \ingroup xml-tools | - | ||||||
2430 | - | |||||||
2431 | An notation declaration consists of a name(), a systemId(), and a publicId(). | - | ||||||
2432 | */ | - | ||||||
2433 | - | |||||||
2434 | /*! | - | ||||||
2435 | Creates an empty notation declaration. | - | ||||||
2436 | */ | - | ||||||
2437 | QXmlStreamNotationDeclaration::QXmlStreamNotationDeclaration() | - | ||||||
2438 | { | - | ||||||
2439 | } | - | ||||||
2440 | /*! | - | ||||||
2441 | Creates a copy of \a other. | - | ||||||
2442 | */ | - | ||||||
2443 | QXmlStreamNotationDeclaration::QXmlStreamNotationDeclaration(const QXmlStreamNotationDeclaration &other) | - | ||||||
2444 | { | - | ||||||
2445 | *this = other; | - | ||||||
2446 | } | - | ||||||
2447 | - | |||||||
2448 | /*! | - | ||||||
2449 | Assigns \a other to this notation declaration. | - | ||||||
2450 | */ | - | ||||||
2451 | QXmlStreamNotationDeclaration& QXmlStreamNotationDeclaration::operator=(const QXmlStreamNotationDeclaration &other) | - | ||||||
2452 | { | - | ||||||
2453 | m_name = other.m_name; | - | ||||||
2454 | m_systemId = other.m_systemId; | - | ||||||
2455 | m_publicId = other.m_publicId; | - | ||||||
2456 | return *this; | - | ||||||
2457 | } | - | ||||||
2458 | - | |||||||
2459 | /*! | - | ||||||
2460 | Destructs this notation declaration. | - | ||||||
2461 | */ | - | ||||||
2462 | QXmlStreamNotationDeclaration::~QXmlStreamNotationDeclaration() | - | ||||||
2463 | { | - | ||||||
2464 | } | - | ||||||
2465 | - | |||||||
2466 | /*! \fn QStringRef QXmlStreamNotationDeclaration::name() const | - | ||||||
2467 | - | |||||||
2468 | Returns the notation name. | - | ||||||
2469 | */ | - | ||||||
2470 | /*! \fn QStringRef QXmlStreamNotationDeclaration::systemId() const | - | ||||||
2471 | - | |||||||
2472 | Returns the system identifier. | - | ||||||
2473 | */ | - | ||||||
2474 | /*! \fn QStringRef QXmlStreamNotationDeclaration::publicId() const | - | ||||||
2475 | - | |||||||
2476 | Returns the public identifier. | - | ||||||
2477 | */ | - | ||||||
2478 | - | |||||||
2479 | /*! \fn inline bool QXmlStreamNotationDeclaration::operator==(const QXmlStreamNotationDeclaration &other) const | - | ||||||
2480 | - | |||||||
2481 | Compares this notation declaration with \a other and returns \c true | - | ||||||
2482 | if they are equal; otherwise returns \c false. | - | ||||||
2483 | */ | - | ||||||
2484 | /*! \fn inline bool QXmlStreamNotationDeclaration::operator!=(const QXmlStreamNotationDeclaration &other) const | - | ||||||
2485 | - | |||||||
2486 | Compares this notation declaration with \a other and returns \c true | - | ||||||
2487 | if they are not equal; otherwise returns \c false. | - | ||||||
2488 | */ | - | ||||||
2489 | - | |||||||
2490 | /*! | - | ||||||
2491 | \typedef QXmlStreamNamespaceDeclarations | - | ||||||
2492 | \relates QXmlStreamNamespaceDeclaration | - | ||||||
2493 | - | |||||||
2494 | Synonym for QVector<QXmlStreamNamespaceDeclaration>. | - | ||||||
2495 | */ | - | ||||||
2496 | - | |||||||
2497 | /*! | - | ||||||
2498 | \class QXmlStreamNamespaceDeclaration | - | ||||||
2499 | \inmodule QtCore | - | ||||||
2500 | \since 4.3 | - | ||||||
2501 | \reentrant | - | ||||||
2502 | \brief The QXmlStreamNamespaceDeclaration class represents a namespace declaration. | - | ||||||
2503 | - | |||||||
2504 | \ingroup xml-tools | - | ||||||
2505 | - | |||||||
2506 | An namespace declaration consists of a prefix() and a namespaceUri(). | - | ||||||
2507 | */ | - | ||||||
2508 | /*! \fn inline bool QXmlStreamNamespaceDeclaration::operator==(const QXmlStreamNamespaceDeclaration &other) const | - | ||||||
2509 | - | |||||||
2510 | Compares this namespace declaration with \a other and returns \c true | - | ||||||
2511 | if they are equal; otherwise returns \c false. | - | ||||||
2512 | */ | - | ||||||
2513 | /*! \fn inline bool QXmlStreamNamespaceDeclaration::operator!=(const QXmlStreamNamespaceDeclaration &other) const | - | ||||||
2514 | - | |||||||
2515 | Compares this namespace declaration with \a other and returns \c true | - | ||||||
2516 | if they are not equal; otherwise returns \c false. | - | ||||||
2517 | */ | - | ||||||
2518 | - | |||||||
2519 | /*! | - | ||||||
2520 | Creates an empty namespace declaration. | - | ||||||
2521 | */ | - | ||||||
2522 | QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration() | - | ||||||
2523 | { | - | ||||||
2524 | } | - | ||||||
2525 | - | |||||||
2526 | /*! | - | ||||||
2527 | \since 4.4 | - | ||||||
2528 | - | |||||||
2529 | Creates a namespace declaration with \a prefix and \a namespaceUri. | - | ||||||
2530 | */ | - | ||||||
2531 | QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri) | - | ||||||
2532 | { | - | ||||||
2533 | m_prefix = prefix; | - | ||||||
2534 | m_namespaceUri = namespaceUri; | - | ||||||
2535 | } | - | ||||||
2536 | - | |||||||
2537 | /*! | - | ||||||
2538 | Creates a copy of \a other. | - | ||||||
2539 | */ | - | ||||||
2540 | QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &other) | - | ||||||
2541 | { | - | ||||||
2542 | *this = other; | - | ||||||
2543 | } | - | ||||||
2544 | - | |||||||
2545 | /*! | - | ||||||
2546 | Assigns \a other to this namespace declaration. | - | ||||||
2547 | */ | - | ||||||
2548 | QXmlStreamNamespaceDeclaration& QXmlStreamNamespaceDeclaration::operator=(const QXmlStreamNamespaceDeclaration &other) | - | ||||||
2549 | { | - | ||||||
2550 | m_prefix = other.m_prefix; | - | ||||||
2551 | m_namespaceUri = other.m_namespaceUri; | - | ||||||
2552 | return *this; | - | ||||||
2553 | } | - | ||||||
2554 | /*! | - | ||||||
2555 | Destructs this namespace declaration. | - | ||||||
2556 | */ | - | ||||||
2557 | QXmlStreamNamespaceDeclaration::~QXmlStreamNamespaceDeclaration() | - | ||||||
2558 | { | - | ||||||
2559 | } | - | ||||||
2560 | - | |||||||
2561 | /*! \fn QStringRef QXmlStreamNamespaceDeclaration::prefix() const | - | ||||||
2562 | - | |||||||
2563 | Returns the prefix. | - | ||||||
2564 | */ | - | ||||||
2565 | /*! \fn QStringRef QXmlStreamNamespaceDeclaration::namespaceUri() const | - | ||||||
2566 | - | |||||||
2567 | Returns the namespaceUri. | - | ||||||
2568 | */ | - | ||||||
2569 | - | |||||||
2570 | - | |||||||
2571 | - | |||||||
2572 | - | |||||||
2573 | /*! | - | ||||||
2574 | \typedef QXmlStreamEntityDeclarations | - | ||||||
2575 | \relates QXmlStreamEntityDeclaration | - | ||||||
2576 | - | |||||||
2577 | Synonym for QVector<QXmlStreamEntityDeclaration>. | - | ||||||
2578 | */ | - | ||||||
2579 | - | |||||||
2580 | /*! | - | ||||||
2581 | \class QXmlStreamStringRef | - | ||||||
2582 | \inmodule QtCore | - | ||||||
2583 | \since 4.3 | - | ||||||
2584 | \internal | - | ||||||
2585 | */ | - | ||||||
2586 | - | |||||||
2587 | /*! | - | ||||||
2588 | \class QXmlStreamEntityDeclaration | - | ||||||
2589 | \inmodule QtCore | - | ||||||
2590 | \since 4.3 | - | ||||||
2591 | \reentrant | - | ||||||
2592 | \brief The QXmlStreamEntityDeclaration class represents a DTD entity declaration. | - | ||||||
2593 | - | |||||||
2594 | \ingroup xml-tools | - | ||||||
2595 | - | |||||||
2596 | An entity declaration consists of a name(), a notationName(), a | - | ||||||
2597 | systemId(), a publicId(), and a value(). | - | ||||||
2598 | */ | - | ||||||
2599 | - | |||||||
2600 | /*! | - | ||||||
2601 | Creates an empty entity declaration. | - | ||||||
2602 | */ | - | ||||||
2603 | QXmlStreamEntityDeclaration::QXmlStreamEntityDeclaration() | - | ||||||
2604 | { | - | ||||||
2605 | } | - | ||||||
2606 | - | |||||||
2607 | /*! | - | ||||||
2608 | Creates a copy of \a other. | - | ||||||
2609 | */ | - | ||||||
2610 | QXmlStreamEntityDeclaration::QXmlStreamEntityDeclaration(const QXmlStreamEntityDeclaration &other) | - | ||||||
2611 | { | - | ||||||
2612 | *this = other; | - | ||||||
2613 | } | - | ||||||
2614 | - | |||||||
2615 | /*! | - | ||||||
2616 | Assigns \a other to this entity declaration. | - | ||||||
2617 | */ | - | ||||||
2618 | QXmlStreamEntityDeclaration& QXmlStreamEntityDeclaration::operator=(const QXmlStreamEntityDeclaration &other) | - | ||||||
2619 | { | - | ||||||
2620 | m_name = other.m_name; | - | ||||||
2621 | m_notationName = other.m_notationName; | - | ||||||
2622 | m_systemId = other.m_systemId; | - | ||||||
2623 | m_publicId = other.m_publicId; | - | ||||||
2624 | m_value = other.m_value; | - | ||||||
2625 | return *this; | - | ||||||
2626 | } | - | ||||||
2627 | - | |||||||
2628 | /*! | - | ||||||
2629 | Destructs this entity declaration. | - | ||||||
2630 | */ | - | ||||||
2631 | QXmlStreamEntityDeclaration::~QXmlStreamEntityDeclaration() | - | ||||||
2632 | { | - | ||||||
2633 | } | - | ||||||
2634 | - | |||||||
2635 | /*! \fn QXmlStreamStringRef::swap(QXmlStreamStringRef &other) | - | ||||||
2636 | \since 5.6 | - | ||||||
2637 | - | |||||||
2638 | Swaps this string reference's contents with \a other. | - | ||||||
2639 | This function is very fast and never fails. | - | ||||||
2640 | */ | - | ||||||
2641 | - | |||||||
2642 | /*! \fn QStringRef QXmlStreamEntityDeclaration::name() const | - | ||||||
2643 | - | |||||||
2644 | Returns the entity name. | - | ||||||
2645 | */ | - | ||||||
2646 | /*! \fn QStringRef QXmlStreamEntityDeclaration::notationName() const | - | ||||||
2647 | - | |||||||
2648 | Returns the notation name. | - | ||||||
2649 | */ | - | ||||||
2650 | /*! \fn QStringRef QXmlStreamEntityDeclaration::systemId() const | - | ||||||
2651 | - | |||||||
2652 | Returns the system identifier. | - | ||||||
2653 | */ | - | ||||||
2654 | /*! \fn QStringRef QXmlStreamEntityDeclaration::publicId() const | - | ||||||
2655 | - | |||||||
2656 | Returns the public identifier. | - | ||||||
2657 | */ | - | ||||||
2658 | /*! \fn QStringRef QXmlStreamEntityDeclaration::value() const | - | ||||||
2659 | - | |||||||
2660 | Returns the entity's value. | - | ||||||
2661 | */ | - | ||||||
2662 | - | |||||||
2663 | /*! \fn bool QXmlStreamEntityDeclaration::operator==(const QXmlStreamEntityDeclaration &other) const | - | ||||||
2664 | - | |||||||
2665 | Compares this entity declaration with \a other and returns \c true if | - | ||||||
2666 | they are equal; otherwise returns \c false. | - | ||||||
2667 | */ | - | ||||||
2668 | /*! \fn bool QXmlStreamEntityDeclaration::operator!=(const QXmlStreamEntityDeclaration &other) const | - | ||||||
2669 | - | |||||||
2670 | Compares this entity declaration with \a other and returns \c true if | - | ||||||
2671 | they are not equal; otherwise returns \c false. | - | ||||||
2672 | */ | - | ||||||
2673 | - | |||||||
2674 | /*! Returns the value of the attribute \a name in the namespace | - | ||||||
2675 | described with \a namespaceUri, or an empty string reference if the | - | ||||||
2676 | attribute is not defined. The \a namespaceUri can be empty. | - | ||||||
2677 | */ | - | ||||||
2678 | QStringRef QXmlStreamAttributes::value(const QString &namespaceUri, const QString &name) const | - | ||||||
2679 | { | - | ||||||
2680 | for (int i = 0; i < size(); ++i) { | - | ||||||
2681 | const QXmlStreamAttribute &attribute = at(i); | - | ||||||
2682 | if (attribute.name() == name && attribute.namespaceUri() == namespaceUri) | - | ||||||
2683 | return attribute.value(); | - | ||||||
2684 | } | - | ||||||
2685 | return QStringRef(); | - | ||||||
2686 | } | - | ||||||
2687 | - | |||||||
2688 | /*!\overload | - | ||||||
2689 | Returns the value of the attribute \a name in the namespace | - | ||||||
2690 | described with \a namespaceUri, or an empty string reference if the | - | ||||||
2691 | attribute is not defined. The \a namespaceUri can be empty. | - | ||||||
2692 | */ | - | ||||||
2693 | QStringRef QXmlStreamAttributes::value(const QString &namespaceUri, QLatin1String name) const | - | ||||||
2694 | { | - | ||||||
2695 | for (int i = 0; i < size(); ++i) { | - | ||||||
2696 | const QXmlStreamAttribute &attribute = at(i); | - | ||||||
2697 | if (attribute.name() == name && attribute.namespaceUri() == namespaceUri) | - | ||||||
2698 | return attribute.value(); | - | ||||||
2699 | } | - | ||||||
2700 | return QStringRef(); | - | ||||||
2701 | } | - | ||||||
2702 | - | |||||||
2703 | /*!\overload | - | ||||||
2704 | Returns the value of the attribute \a name in the namespace | - | ||||||
2705 | described with \a namespaceUri, or an empty string reference if the | - | ||||||
2706 | attribute is not defined. The \a namespaceUri can be empty. | - | ||||||
2707 | */ | - | ||||||
2708 | QStringRef QXmlStreamAttributes::value(QLatin1String namespaceUri, QLatin1String name) const | - | ||||||
2709 | { | - | ||||||
2710 | for (int i = 0; i < size(); ++i) { | - | ||||||
2711 | const QXmlStreamAttribute &attribute = at(i); | - | ||||||
2712 | if (attribute.name() == name && attribute.namespaceUri() == namespaceUri) | - | ||||||
2713 | return attribute.value(); | - | ||||||
2714 | } | - | ||||||
2715 | return QStringRef(); | - | ||||||
2716 | } | - | ||||||
2717 | - | |||||||
2718 | /*!\overload | - | ||||||
2719 | - | |||||||
2720 | Returns the value of the attribute with qualified name \a | - | ||||||
2721 | qualifiedName , or an empty string reference if the attribute is not | - | ||||||
2722 | defined. A qualified name is the raw name of an attribute in the XML | - | ||||||
2723 | data. It consists of the namespace prefix, followed by colon, | - | ||||||
2724 | followed by the attribute's local name. Since the namespace prefix | - | ||||||
2725 | is not unique (the same prefix can point to different namespaces and | - | ||||||
2726 | different prefixes can point to the same namespace), you shouldn't | - | ||||||
2727 | use qualified names, but a resolved namespaceUri and the attribute's | - | ||||||
2728 | local name. | - | ||||||
2729 | */ | - | ||||||
2730 | QStringRef QXmlStreamAttributes::value(const QString &qualifiedName) const | - | ||||||
2731 | { | - | ||||||
2732 | for (int i = 0; i < size(); ++i) { | - | ||||||
2733 | const QXmlStreamAttribute &attribute = at(i); | - | ||||||
2734 | if (attribute.qualifiedName() == qualifiedName) | - | ||||||
2735 | return attribute.value(); | - | ||||||
2736 | } | - | ||||||
2737 | return QStringRef(); | - | ||||||
2738 | } | - | ||||||
2739 | - | |||||||
2740 | /*!\overload | - | ||||||
2741 | - | |||||||
2742 | Returns the value of the attribute with qualified name \a | - | ||||||
2743 | qualifiedName , or an empty string reference if the attribute is not | - | ||||||
2744 | defined. A qualified name is the raw name of an attribute in the XML | - | ||||||
2745 | data. It consists of the namespace prefix, followed by colon, | - | ||||||
2746 | followed by the attribute's local name. Since the namespace prefix | - | ||||||
2747 | is not unique (the same prefix can point to different namespaces and | - | ||||||
2748 | different prefixes can point to the same namespace), you shouldn't | - | ||||||
2749 | use qualified names, but a resolved namespaceUri and the attribute's | - | ||||||
2750 | local name. | - | ||||||
2751 | */ | - | ||||||
2752 | QStringRef QXmlStreamAttributes::value(QLatin1String qualifiedName) const | - | ||||||
2753 | { | - | ||||||
2754 | for (int i = 0; i < size(); ++i) { | - | ||||||
2755 | const QXmlStreamAttribute &attribute = at(i); | - | ||||||
2756 | if (attribute.qualifiedName() == qualifiedName) | - | ||||||
2757 | return attribute.value(); | - | ||||||
2758 | } | - | ||||||
2759 | return QStringRef(); | - | ||||||
2760 | } | - | ||||||
2761 | - | |||||||
2762 | /*!Appends a new attribute with \a name in the namespace | - | ||||||
2763 | described with \a namespaceUri, and value \a value. The \a | - | ||||||
2764 | namespaceUri can be empty. | - | ||||||
2765 | */ | - | ||||||
2766 | void QXmlStreamAttributes::append(const QString &namespaceUri, const QString &name, const QString &value) | - | ||||||
2767 | { | - | ||||||
2768 | append(QXmlStreamAttribute(namespaceUri, name, value)); | - | ||||||
2769 | } | - | ||||||
2770 | - | |||||||
2771 | /*!\overload | - | ||||||
2772 | Appends a new attribute with qualified name \a qualifiedName and | - | ||||||
2773 | value \a value. | - | ||||||
2774 | */ | - | ||||||
2775 | void QXmlStreamAttributes::append(const QString &qualifiedName, const QString &value) | - | ||||||
2776 | { | - | ||||||
2777 | append(QXmlStreamAttribute(qualifiedName, value)); | - | ||||||
2778 | } | - | ||||||
2779 | - | |||||||
2780 | #ifndef QT_NO_XMLSTREAMREADER | - | ||||||
2781 | - | |||||||
2782 | /*! \fn bool QXmlStreamReader::isStartDocument() const | - | ||||||
2783 | Returns \c true if tokenType() equals \l StartDocument; otherwise returns \c false. | - | ||||||
2784 | */ | - | ||||||
2785 | /*! \fn bool QXmlStreamReader::isEndDocument() const | - | ||||||
2786 | Returns \c true if tokenType() equals \l EndDocument; otherwise returns \c false. | - | ||||||
2787 | */ | - | ||||||
2788 | /*! \fn bool QXmlStreamReader::isStartElement() const | - | ||||||
2789 | Returns \c true if tokenType() equals \l StartElement; otherwise returns \c false. | - | ||||||
2790 | */ | - | ||||||
2791 | /*! \fn bool QXmlStreamReader::isEndElement() const | - | ||||||
2792 | Returns \c true if tokenType() equals \l EndElement; otherwise returns \c false. | - | ||||||
2793 | */ | - | ||||||
2794 | /*! \fn bool QXmlStreamReader::isCharacters() const | - | ||||||
2795 | Returns \c true if tokenType() equals \l Characters; otherwise returns \c false. | - | ||||||
2796 | - | |||||||
2797 | \sa isWhitespace(), isCDATA() | - | ||||||
2798 | */ | - | ||||||
2799 | /*! \fn bool QXmlStreamReader::isComment() const | - | ||||||
2800 | Returns \c true if tokenType() equals \l Comment; otherwise returns \c false. | - | ||||||
2801 | */ | - | ||||||
2802 | /*! \fn bool QXmlStreamReader::isDTD() const | - | ||||||
2803 | Returns \c true if tokenType() equals \l DTD; otherwise returns \c false. | - | ||||||
2804 | */ | - | ||||||
2805 | /*! \fn bool QXmlStreamReader::isEntityReference() const | - | ||||||
2806 | Returns \c true if tokenType() equals \l EntityReference; otherwise returns \c false. | - | ||||||
2807 | */ | - | ||||||
2808 | /*! \fn bool QXmlStreamReader::isProcessingInstruction() const | - | ||||||
2809 | Returns \c true if tokenType() equals \l ProcessingInstruction; otherwise returns \c false. | - | ||||||
2810 | */ | - | ||||||
2811 | - | |||||||
2812 | /*! Returns \c true if the reader reports characters that only consist | - | ||||||
2813 | of white-space; otherwise returns \c false. | - | ||||||
2814 | - | |||||||
2815 | \sa isCharacters(), text() | - | ||||||
2816 | */ | - | ||||||
2817 | bool QXmlStreamReader::isWhitespace() const | - | ||||||
2818 | { | - | ||||||
2819 | Q_D(const QXmlStreamReader); | - | ||||||
2820 | return d->type == QXmlStreamReader::Characters && d->isWhitespace; | - | ||||||
2821 | } | - | ||||||
2822 | - | |||||||
2823 | /*! Returns \c true if the reader reports characters that stem from a | - | ||||||
2824 | CDATA section; otherwise returns \c false. | - | ||||||
2825 | - | |||||||
2826 | \sa isCharacters(), text() | - | ||||||
2827 | */ | - | ||||||
2828 | bool QXmlStreamReader::isCDATA() const | - | ||||||
2829 | { | - | ||||||
2830 | Q_D(const QXmlStreamReader); | - | ||||||
2831 | return d->type == QXmlStreamReader::Characters && d->isCDATA; | - | ||||||
2832 | } | - | ||||||
2833 | - | |||||||
2834 | - | |||||||
2835 | - | |||||||
2836 | /*! | - | ||||||
2837 | Returns \c true if this document has been declared standalone in the | - | ||||||
2838 | XML declaration; otherwise returns \c false. | - | ||||||
2839 | - | |||||||
2840 | If no XML declaration has been parsed, this function returns \c false. | - | ||||||
2841 | */ | - | ||||||
2842 | bool QXmlStreamReader::isStandaloneDocument() const | - | ||||||
2843 | { | - | ||||||
2844 | Q_D(const QXmlStreamReader); | - | ||||||
2845 | return d->standalone; | - | ||||||
2846 | } | - | ||||||
2847 | - | |||||||
2848 | - | |||||||
2849 | /*! | - | ||||||
2850 | \since 4.4 | - | ||||||
2851 | - | |||||||
2852 | If the tokenType() is \l StartDocument, this function returns the | - | ||||||
2853 | version string as specified in the XML declaration. | - | ||||||
2854 | Otherwise an empty string is returned. | - | ||||||
2855 | */ | - | ||||||
2856 | QStringRef QXmlStreamReader::documentVersion() const | - | ||||||
2857 | { | - | ||||||
2858 | Q_D(const QXmlStreamReader); | - | ||||||
2859 | if (d->type == QXmlStreamReader::StartDocument) | - | ||||||
2860 | return d->documentVersion; | - | ||||||
2861 | return QStringRef(); | - | ||||||
2862 | } | - | ||||||
2863 | - | |||||||
2864 | /*! | - | ||||||
2865 | \since 4.4 | - | ||||||
2866 | - | |||||||
2867 | If the tokenType() is \l StartDocument, this function returns the | - | ||||||
2868 | encoding string as specified in the XML declaration. | - | ||||||
2869 | Otherwise an empty string is returned. | - | ||||||
2870 | */ | - | ||||||
2871 | QStringRef QXmlStreamReader::documentEncoding() const | - | ||||||
2872 | { | - | ||||||
2873 | Q_D(const QXmlStreamReader); | - | ||||||
2874 | if (d->type == QXmlStreamReader::StartDocument) | - | ||||||
2875 | return d->documentEncoding; | - | ||||||
2876 | return QStringRef(); | - | ||||||
2877 | } | - | ||||||
2878 | - | |||||||
2879 | #endif // QT_NO_XMLSTREAMREADER | - | ||||||
2880 | - | |||||||
2881 | /*! | - | ||||||
2882 | \class QXmlStreamWriter | - | ||||||
2883 | \inmodule QtCore | - | ||||||
2884 | \since 4.3 | - | ||||||
2885 | \reentrant | - | ||||||
2886 | - | |||||||
2887 | \brief The QXmlStreamWriter class provides an XML writer with a | - | ||||||
2888 | simple streaming API. | - | ||||||
2889 | - | |||||||
2890 | \ingroup xml-tools | - | ||||||
2891 | - | |||||||
2892 | QXmlStreamWriter is the counterpart to QXmlStreamReader for writing | - | ||||||
2893 | XML. Like its related class, it operates on a QIODevice specified | - | ||||||
2894 | with setDevice(). The API is simple and straightforward: for every | - | ||||||
2895 | XML token or event you want to write, the writer provides a | - | ||||||
2896 | specialized function. | - | ||||||
2897 | - | |||||||
2898 | You start a document with writeStartDocument() and end it with | - | ||||||
2899 | writeEndDocument(). This will implicitly close all remaining open | - | ||||||
2900 | tags. | - | ||||||
2901 | - | |||||||
2902 | Element tags are opened with writeStartElement() followed by | - | ||||||
2903 | writeAttribute() or writeAttributes(), element content, and then | - | ||||||
2904 | writeEndElement(). A shorter form writeEmptyElement() can be used | - | ||||||
2905 | to write empty elements, followed by writeAttributes(). | - | ||||||
2906 | - | |||||||
2907 | Element content consists of either characters, entity references or | - | ||||||
2908 | nested elements. It is written with writeCharacters(), which also | - | ||||||
2909 | takes care of escaping all forbidden characters and character | - | ||||||
2910 | sequences, writeEntityReference(), or subsequent calls to | - | ||||||
2911 | writeStartElement(). A convenience method writeTextElement() can be | - | ||||||
2912 | used for writing terminal elements that contain nothing but text. | - | ||||||
2913 | - | |||||||
2914 | The following abridged code snippet shows the basic use of the class | - | ||||||
2915 | to write formatted XML with indentation: | - | ||||||
2916 | - | |||||||
2917 | \snippet qxmlstreamwriter/main.cpp start stream | - | ||||||
2918 | \dots | - | ||||||
2919 | \snippet qxmlstreamwriter/main.cpp write element | - | ||||||
2920 | \dots | - | ||||||
2921 | \snippet qxmlstreamwriter/main.cpp finish stream | - | ||||||
2922 | - | |||||||
2923 | QXmlStreamWriter takes care of prefixing namespaces, all you have to | - | ||||||
2924 | do is specify the \c namespaceUri when writing elements or | - | ||||||
2925 | attributes. If you must conform to certain prefixes, you can force | - | ||||||
2926 | the writer to use them by declaring the namespaces manually with | - | ||||||
2927 | either writeNamespace() or writeDefaultNamespace(). Alternatively, | - | ||||||
2928 | you can bypass the stream writer's namespace support and use | - | ||||||
2929 | overloaded methods that take a qualified name instead. The namespace | - | ||||||
2930 | \e http://www.w3.org/XML/1998/namespace is implicit and mapped to the | - | ||||||
2931 | prefix \e xml. | - | ||||||
2932 | - | |||||||
2933 | The stream writer can automatically format the generated XML data by | - | ||||||
2934 | adding line-breaks and indentation to empty sections between | - | ||||||
2935 | elements, making the XML data more readable for humans and easier to | - | ||||||
2936 | work with for most source code management systems. The feature can | - | ||||||
2937 | be turned on with the \l autoFormatting property, and customized | - | ||||||
2938 | with the \l autoFormattingIndent property. | - | ||||||
2939 | - | |||||||
2940 | Other functions are writeCDATA(), writeComment(), | - | ||||||
2941 | writeProcessingInstruction(), and writeDTD(). Chaining of XML | - | ||||||
2942 | streams is supported with writeCurrentToken(). | - | ||||||
2943 | - | |||||||
2944 | By default, QXmlStreamWriter encodes XML in UTF-8. Different | - | ||||||
2945 | encodings can be enforced using setCodec(). | - | ||||||
2946 | - | |||||||
2947 | If an error occurs while writing to the underlying device, hasError() | - | ||||||
2948 | starts returning true and subsequent writes are ignored. | - | ||||||
2949 | - | |||||||
2950 | The \l{QXmlStream Bookmarks Example} illustrates how to use a | - | ||||||
2951 | stream writer to write an XML bookmark file (XBEL) that | - | ||||||
2952 | was previously read in by a QXmlStreamReader. | - | ||||||
2953 | - | |||||||
2954 | */ | - | ||||||
2955 | - | |||||||
2956 | #ifndef QT_NO_XMLSTREAMWRITER | - | ||||||
2957 | - | |||||||
2958 | class QXmlStreamWriterPrivate : public QXmlStreamPrivateTagStack { | - | ||||||
2959 | QXmlStreamWriter *q_ptr; | - | ||||||
2960 | Q_DECLARE_PUBLIC(QXmlStreamWriter) | - | ||||||
2961 | public: | - | ||||||
2962 | QXmlStreamWriterPrivate(QXmlStreamWriter *q); | - | ||||||
2963 | ~QXmlStreamWriterPrivate() { | - | ||||||
2964 | if (deleteDevice) | - | ||||||
2965 | delete device; | - | ||||||
2966 | #ifndef QT_NO_TEXTCODEC | - | ||||||
2967 | delete encoder; | - | ||||||
2968 | #endif | - | ||||||
2969 | } | - | ||||||
2970 | - | |||||||
2971 | void write(const QStringRef &); | - | ||||||
2972 | void write(const QString &); | - | ||||||
2973 | void writeEscaped(const QString &, bool escapeWhitespace = false); | - | ||||||
2974 | void write(const char *s, int len); | - | ||||||
2975 | template <int N> void write(const char (&s)[N]) { write(s, N - 1); } | - | ||||||
2976 | bool finishStartElement(bool contents = true); | - | ||||||
2977 | void writeStartElement(const QString &namespaceUri, const QString &name); | - | ||||||
2978 | QIODevice *device; | - | ||||||
2979 | QString *stringDevice; | - | ||||||
2980 | uint deleteDevice :1; | - | ||||||
2981 | uint inStartElement :1; | - | ||||||
2982 | uint inEmptyElement :1; | - | ||||||
2983 | uint lastWasStartElement :1; | - | ||||||
2984 | uint wroteSomething :1; | - | ||||||
2985 | uint hasError :1; | - | ||||||
2986 | uint autoFormatting :1; | - | ||||||
2987 | uint isCodecASCIICompatible :1; | - | ||||||
2988 | QByteArray autoFormattingIndent; | - | ||||||
2989 | NamespaceDeclaration emptyNamespace; | - | ||||||
2990 | int lastNamespaceDeclaration; | - | ||||||
2991 | - | |||||||
2992 | #ifndef QT_NO_TEXTCODEC | - | ||||||
2993 | QTextCodec *codec; | - | ||||||
2994 | QTextEncoder *encoder; | - | ||||||
2995 | #endif | - | ||||||
2996 | void checkIfASCIICompatibleCodec(); | - | ||||||
2997 | - | |||||||
2998 | NamespaceDeclaration &findNamespace(const QString &namespaceUri, bool writeDeclaration = false, bool noDefault = false); | - | ||||||
2999 | void writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration); | - | ||||||
3000 | - | |||||||
3001 | int namespacePrefixCount; | - | ||||||
3002 | - | |||||||
3003 | void indent(int level); | - | ||||||
3004 | }; | - | ||||||
3005 | - | |||||||
3006 | - | |||||||
3007 | QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q) | - | ||||||
3008 | :autoFormattingIndent(4, ' ') | - | ||||||
3009 | { | - | ||||||
3010 | q_ptr = q; | - | ||||||
3011 | device = 0; | - | ||||||
3012 | stringDevice = 0; | - | ||||||
3013 | deleteDevice = false; | - | ||||||
3014 | #ifndef QT_NO_TEXTCODEC | - | ||||||
3015 | codec = QTextCodec::codecForMib(106); // utf8 | - | ||||||
3016 | encoder = codec->makeEncoder(QTextCodec::IgnoreHeader); // no byte order mark for utf8 | - | ||||||
3017 | #endif | - | ||||||
3018 | checkIfASCIICompatibleCodec(); | - | ||||||
3019 | inStartElement = inEmptyElement = false; | - | ||||||
3020 | wroteSomething = false; | - | ||||||
3021 | hasError = false; | - | ||||||
3022 | lastWasStartElement = false; | - | ||||||
3023 | lastNamespaceDeclaration = 1; | - | ||||||
3024 | autoFormatting = false; | - | ||||||
3025 | namespacePrefixCount = 0; | - | ||||||
3026 | } | - | ||||||
3027 | - | |||||||
3028 | void QXmlStreamWriterPrivate::checkIfASCIICompatibleCodec() | - | ||||||
3029 | { | - | ||||||
3030 | #ifndef QT_NO_TEXTCODEC | - | ||||||
3031 | Q_ASSERT(encoder); | - | ||||||
3032 | // test ASCII-compatibility using the letter 'a' | - | ||||||
3033 | QChar letterA = QLatin1Char('a'); | - | ||||||
3034 | const QByteArray bytesA = encoder->fromUnicode(&letterA, 1); | - | ||||||
3035 | const bool isCodecASCIICompatibleA = (bytesA.count() == 1) && (bytesA[0] == 0x61) ; | - | ||||||
3036 | QChar letterLess = QLatin1Char('<'); | - | ||||||
3037 | const QByteArray bytesLess = encoder->fromUnicode(&letterLess, 1); | - | ||||||
3038 | const bool isCodecASCIICompatibleLess = (bytesLess.count() == 1) && (bytesLess[0] == 0x3C) ; | - | ||||||
3039 | isCodecASCIICompatible = isCodecASCIICompatibleA && isCodecASCIICompatibleLess ; | - | ||||||
3040 | #else | - | ||||||
3041 | isCodecASCIICompatible = true; | - | ||||||
3042 | #endif | - | ||||||
3043 | } | - | ||||||
3044 | - | |||||||
3045 | void QXmlStreamWriterPrivate::write(const QStringRef &s) | - | ||||||
3046 | { | - | ||||||
3047 | if (device) { | - | ||||||
3048 | if (hasError) | - | ||||||
3049 | return; | - | ||||||
3050 | #ifdef QT_NO_TEXTCODEC | - | ||||||
3051 | QByteArray bytes = s.toLatin1(); | - | ||||||
3052 | #else | - | ||||||
3053 | QByteArray bytes = encoder->fromUnicode(s.constData(), s.size()); | - | ||||||
3054 | #endif | - | ||||||
3055 | if (device->write(bytes) != bytes.size()) | - | ||||||
3056 | hasError = true; | - | ||||||
3057 | } | - | ||||||
3058 | else if (stringDevice) | - | ||||||
3059 | s.appendTo(stringDevice); | - | ||||||
3060 | else | - | ||||||
3061 | qWarning("QXmlStreamWriter: No device"); | - | ||||||
3062 | } | - | ||||||
3063 | - | |||||||
3064 | void QXmlStreamWriterPrivate::write(const QString &s) | - | ||||||
3065 | { | - | ||||||
3066 | if (device) { | - | ||||||
3067 | if (hasError) | - | ||||||
3068 | return; | - | ||||||
3069 | #ifdef QT_NO_TEXTCODEC | - | ||||||
3070 | QByteArray bytes = s.toLatin1(); | - | ||||||
3071 | #else | - | ||||||
3072 | QByteArray bytes = encoder->fromUnicode(s); | - | ||||||
3073 | #endif | - | ||||||
3074 | if (device->write(bytes) != bytes.size()) | - | ||||||
3075 | hasError = true; | - | ||||||
3076 | } | - | ||||||
3077 | else if (stringDevice) | - | ||||||
3078 | stringDevice->append(s); | - | ||||||
3079 | else | - | ||||||
3080 | qWarning("QXmlStreamWriter: No device"); | - | ||||||
3081 | } | - | ||||||
3082 | - | |||||||
3083 | void QXmlStreamWriterPrivate::writeEscaped(const QString &s, bool escapeWhitespace) | - | ||||||
3084 | { | - | ||||||
3085 | QString escaped; | - | ||||||
3086 | escaped.reserve(s.size()); | - | ||||||
3087 | for ( int i = 0; i < s.size(); ++i ) { | - | ||||||
3088 | QChar c = s.at(i); | - | ||||||
3089 | if (c.unicode() == '<' ) | - | ||||||
3090 | escaped.append(QLatin1String("<")); | - | ||||||
3091 | else if (c.unicode() == '>' ) | - | ||||||
3092 | escaped.append(QLatin1String(">")); | - | ||||||
3093 | else if (c.unicode() == '&' ) | - | ||||||
3094 | escaped.append(QLatin1String("&")); | - | ||||||
3095 | else if (c.unicode() == '\"' ) | - | ||||||
3096 | escaped.append(QLatin1String(""")); | - | ||||||
3097 | else if (escapeWhitespace && c.isSpace()) { | - | ||||||
3098 | if (c.unicode() == '\n') | - | ||||||
3099 | escaped.append(QLatin1String(" ")); | - | ||||||
3100 | else if (c.unicode() == '\r') | - | ||||||
3101 | escaped.append(QLatin1String(" ")); | - | ||||||
3102 | else if (c.unicode() == '\t') | - | ||||||
3103 | escaped.append(QLatin1String("	")); | - | ||||||
3104 | else | - | ||||||
3105 | escaped += c; | - | ||||||
3106 | } else { | - | ||||||
3107 | escaped += QChar(c); | - | ||||||
3108 | } | - | ||||||
3109 | } | - | ||||||
3110 | write(escaped); | - | ||||||
3111 | } | - | ||||||
3112 | - | |||||||
3113 | // Converts from ASCII to output encoding | - | ||||||
3114 | void QXmlStreamWriterPrivate::write(const char *s, int len) | - | ||||||
3115 | { | - | ||||||
3116 | if (device) { | - | ||||||
3117 | if (hasError) | - | ||||||
3118 | return; | - | ||||||
3119 | if (isCodecASCIICompatible) { | - | ||||||
3120 | if (device->write(s, len) != len) | - | ||||||
3121 | hasError = true; | - | ||||||
3122 | return; | - | ||||||
3123 | } | - | ||||||
3124 | } | - | ||||||
3125 | - | |||||||
3126 | write(QString::fromLatin1(s, len)); | - | ||||||
3127 | } | - | ||||||
3128 | - | |||||||
3129 | void QXmlStreamWriterPrivate::writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration) { | - | ||||||
3130 | if (namespaceDeclaration.prefix.isEmpty()) { | - | ||||||
3131 | write(" xmlns=\""); | - | ||||||
3132 | write(namespaceDeclaration.namespaceUri); | - | ||||||
3133 | write("\""); | - | ||||||
3134 | } else { | - | ||||||
3135 | write(" xmlns:"); | - | ||||||
3136 | write(namespaceDeclaration.prefix); | - | ||||||
3137 | write("=\""); | - | ||||||
3138 | write(namespaceDeclaration.namespaceUri); | - | ||||||
3139 | write("\""); | - | ||||||
3140 | } | - | ||||||
3141 | } | - | ||||||
3142 | - | |||||||
3143 | bool QXmlStreamWriterPrivate::finishStartElement(bool contents) | - | ||||||
3144 | { | - | ||||||
3145 | bool hadSomethingWritten = wroteSomething; | - | ||||||
3146 | wroteSomething = contents; | - | ||||||
3147 | if (!inStartElement) | - | ||||||
3148 | return hadSomethingWritten; | - | ||||||
3149 | - | |||||||
3150 | if (inEmptyElement) { | - | ||||||
3151 | write("/>"); | - | ||||||
3152 | QXmlStreamWriterPrivate::Tag &tag = tagStack_pop(); | - | ||||||
3153 | lastNamespaceDeclaration = tag.namespaceDeclarationsSize; | - | ||||||
3154 | lastWasStartElement = false; | - | ||||||
3155 | } else { | - | ||||||
3156 | write(">"); | - | ||||||
3157 | } | - | ||||||
3158 | inStartElement = inEmptyElement = false; | - | ||||||
3159 | lastNamespaceDeclaration = namespaceDeclarations.size(); | - | ||||||
3160 | return hadSomethingWritten; | - | ||||||
3161 | } | - | ||||||
3162 | - | |||||||
3163 | QXmlStreamPrivateTagStack::NamespaceDeclaration &QXmlStreamWriterPrivate::findNamespace(const QString &namespaceUri, bool writeDeclaration, bool noDefault) | - | ||||||
3164 | { | - | ||||||
3165 | for (int j = namespaceDeclarations.size() - 1; j >= 0; --j) { | - | ||||||
3166 | NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations[j]; | - | ||||||
3167 | if (namespaceDeclaration.namespaceUri == namespaceUri) { | - | ||||||
3168 | if (!noDefault || !namespaceDeclaration.prefix.isEmpty()) | - | ||||||
3169 | return namespaceDeclaration; | - | ||||||
3170 | } | - | ||||||
3171 | } | - | ||||||
3172 | if (namespaceUri.isEmpty()) | - | ||||||
3173 | return emptyNamespace; | - | ||||||
3174 | NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); | - | ||||||
3175 | if (namespaceUri.isEmpty()) { | - | ||||||
3176 | namespaceDeclaration.prefix.clear(); | - | ||||||
3177 | } else { | - | ||||||
3178 | QString s; | - | ||||||
3179 | int n = ++namespacePrefixCount; | - | ||||||
3180 | forever { | - | ||||||
3181 | s = QLatin1Char('n') + QString::number(n++); | - | ||||||
3182 | int j = namespaceDeclarations.size() - 2; | - | ||||||
3183 | while (j >= 0 && namespaceDeclarations.at(j).prefix != s) | - | ||||||
3184 | --j; | - | ||||||
3185 | if (j < 0) | - | ||||||
3186 | break; | - | ||||||
3187 | } | - | ||||||
3188 | namespaceDeclaration.prefix = addToStringStorage(s); | - | ||||||
3189 | } | - | ||||||
3190 | namespaceDeclaration.namespaceUri = addToStringStorage(namespaceUri); | - | ||||||
3191 | if (writeDeclaration) | - | ||||||
3192 | writeNamespaceDeclaration(namespaceDeclaration); | - | ||||||
3193 | return namespaceDeclaration; | - | ||||||
3194 | } | - | ||||||
3195 | - | |||||||
3196 | - | |||||||
3197 | - | |||||||
3198 | void QXmlStreamWriterPrivate::indent(int level) | - | ||||||
3199 | { | - | ||||||
3200 | write("\n"); | - | ||||||
3201 | for (int i = level; i > 0; --i) | - | ||||||
3202 | write(autoFormattingIndent.constData(), autoFormattingIndent.length()); | - | ||||||
3203 | } | - | ||||||
3204 | - | |||||||
3205 | - | |||||||
3206 | /*! | - | ||||||
3207 | Constructs a stream writer. | - | ||||||
3208 | - | |||||||
3209 | \sa setDevice() | - | ||||||
3210 | */ | - | ||||||
3211 | QXmlStreamWriter::QXmlStreamWriter() | - | ||||||
3212 | : d_ptr(new QXmlStreamWriterPrivate(this)) | - | ||||||
3213 | { | - | ||||||
3214 | } | - | ||||||
3215 | - | |||||||
3216 | /*! | - | ||||||
3217 | Constructs a stream writer that writes into \a device; | - | ||||||
3218 | */ | - | ||||||
3219 | QXmlStreamWriter::QXmlStreamWriter(QIODevice *device) | - | ||||||
3220 | : d_ptr(new QXmlStreamWriterPrivate(this)) | - | ||||||
3221 | { | - | ||||||
3222 | Q_D(QXmlStreamWriter); | - | ||||||
3223 | d->device = device; | - | ||||||
3224 | } | - | ||||||
3225 | - | |||||||
3226 | /*! Constructs a stream writer that writes into \a array. This is the | - | ||||||
3227 | same as creating an xml writer that operates on a QBuffer device | - | ||||||
3228 | which in turn operates on \a array. | - | ||||||
3229 | */ | - | ||||||
3230 | QXmlStreamWriter::QXmlStreamWriter(QByteArray *array) | - | ||||||
3231 | : d_ptr(new QXmlStreamWriterPrivate(this)) | - | ||||||
3232 | { | - | ||||||
3233 | Q_D(QXmlStreamWriter); | - | ||||||
3234 | d->device = new QBuffer(array); | - | ||||||
3235 | d->device->open(QIODevice::WriteOnly); | - | ||||||
3236 | d->deleteDevice = true; | - | ||||||
3237 | } | - | ||||||
3238 | - | |||||||
3239 | - | |||||||
3240 | /*! Constructs a stream writer that writes into \a string. | - | ||||||
3241 | */ | - | ||||||
3242 | QXmlStreamWriter::QXmlStreamWriter(QString *string) | - | ||||||
3243 | : d_ptr(new QXmlStreamWriterPrivate(this)) | - | ||||||
3244 | { | - | ||||||
3245 | Q_D(QXmlStreamWriter); | - | ||||||
3246 | d->stringDevice = string; | - | ||||||
3247 | } | - | ||||||
3248 | - | |||||||
3249 | /*! | - | ||||||
3250 | Destructor. | - | ||||||
3251 | */ | - | ||||||
3252 | QXmlStreamWriter::~QXmlStreamWriter() | - | ||||||
3253 | { | - | ||||||
3254 | } | - | ||||||
3255 | - | |||||||
3256 | - | |||||||
3257 | /*! | - | ||||||
3258 | Sets the current device to \a device. If you want the stream to | - | ||||||
3259 | write into a QByteArray, you can create a QBuffer device. | - | ||||||
3260 | - | |||||||
3261 | \sa device() | - | ||||||
3262 | */ | - | ||||||
3263 | void QXmlStreamWriter::setDevice(QIODevice *device) | - | ||||||
3264 | { | - | ||||||
3265 | Q_D(QXmlStreamWriter); | - | ||||||
3266 | if (device == d->device) | - | ||||||
3267 | return; | - | ||||||
3268 | d->stringDevice = 0; | - | ||||||
3269 | if (d->deleteDevice) { | - | ||||||
3270 | delete d->device; | - | ||||||
3271 | d->deleteDevice = false; | - | ||||||
3272 | } | - | ||||||
3273 | d->device = device; | - | ||||||
3274 | } | - | ||||||
3275 | - | |||||||
3276 | /*! | - | ||||||
3277 | Returns the current device associated with the QXmlStreamWriter, | - | ||||||
3278 | or 0 if no device has been assigned. | - | ||||||
3279 | - | |||||||
3280 | \sa setDevice() | - | ||||||
3281 | */ | - | ||||||
3282 | QIODevice *QXmlStreamWriter::device() const | - | ||||||
3283 | { | - | ||||||
3284 | Q_D(const QXmlStreamWriter); | - | ||||||
3285 | return d->device; | - | ||||||
3286 | } | - | ||||||
3287 | - | |||||||
3288 | - | |||||||
3289 | #ifndef QT_NO_TEXTCODEC | - | ||||||
3290 | /*! | - | ||||||
3291 | Sets the codec for this stream to \a codec. The codec is used for | - | ||||||
3292 | encoding any data that is written. By default, QXmlStreamWriter | - | ||||||
3293 | uses UTF-8. | - | ||||||
3294 | - | |||||||
3295 | The encoding information is stored in the initial xml tag which | - | ||||||
3296 | gets written when you call writeStartDocument(). Call this | - | ||||||
3297 | function before calling writeStartDocument(). | - | ||||||
3298 | - | |||||||
3299 | \sa codec() | - | ||||||
3300 | */ | - | ||||||
3301 | void QXmlStreamWriter::setCodec(QTextCodec *codec) | - | ||||||
3302 | { | - | ||||||
3303 | Q_D(QXmlStreamWriter); | - | ||||||
3304 | if (codec) { | - | ||||||
3305 | d->codec = codec; | - | ||||||
3306 | delete d->encoder; | - | ||||||
3307 | d->encoder = codec->makeEncoder(QTextCodec::IgnoreHeader); // no byte order mark for utf8 | - | ||||||
3308 | d->checkIfASCIICompatibleCodec(); | - | ||||||
3309 | } | - | ||||||
3310 | } | - | ||||||
3311 | - | |||||||
3312 | /*! | - | ||||||
3313 | Sets the codec for this stream to the QTextCodec for the encoding | - | ||||||
3314 | specified by \a codecName. Common values for \c codecName include | - | ||||||
3315 | "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't | - | ||||||
3316 | recognized, nothing happens. | - | ||||||
3317 | - | |||||||
3318 | \sa QTextCodec::codecForName() | - | ||||||
3319 | */ | - | ||||||
3320 | void QXmlStreamWriter::setCodec(const char *codecName) | - | ||||||
3321 | { | - | ||||||
3322 | setCodec(QTextCodec::codecForName(codecName)); | - | ||||||
3323 | } | - | ||||||
3324 | - | |||||||
3325 | /*! | - | ||||||
3326 | Returns the codec that is currently assigned to the stream. | - | ||||||
3327 | - | |||||||
3328 | \sa setCodec() | - | ||||||
3329 | */ | - | ||||||
3330 | QTextCodec *QXmlStreamWriter::codec() const | - | ||||||
3331 | { | - | ||||||
3332 | Q_D(const QXmlStreamWriter); | - | ||||||
3333 | return d->codec; | - | ||||||
3334 | } | - | ||||||
3335 | #endif // QT_NO_TEXTCODEC | - | ||||||
3336 | - | |||||||
3337 | /*! | - | ||||||
3338 | \property QXmlStreamWriter::autoFormatting | - | ||||||
3339 | \since 4.4 | - | ||||||
3340 | The auto-formatting flag of the stream writer | - | ||||||
3341 | - | |||||||
3342 | This property controls whether or not the stream writer | - | ||||||
3343 | automatically formats the generated XML data. If enabled, the | - | ||||||
3344 | writer automatically adds line-breaks and indentation to empty | - | ||||||
3345 | sections between elements (ignorable whitespace). The main purpose | - | ||||||
3346 | of auto-formatting is to split the data into several lines, and to | - | ||||||
3347 | increase readability for a human reader. The indentation depth can | - | ||||||
3348 | be controlled through the \l autoFormattingIndent property. | - | ||||||
3349 | - | |||||||
3350 | By default, auto-formatting is disabled. | - | ||||||
3351 | */ | - | ||||||
3352 | - | |||||||
3353 | /*! | - | ||||||
3354 | \since 4.4 | - | ||||||
3355 | - | |||||||
3356 | Enables auto formatting if \a enable is \c true, otherwise | - | ||||||
3357 | disables it. | - | ||||||
3358 | - | |||||||
3359 | The default value is \c false. | - | ||||||
3360 | */ | - | ||||||
3361 | void QXmlStreamWriter::setAutoFormatting(bool enable) | - | ||||||
3362 | { | - | ||||||
3363 | Q_D(QXmlStreamWriter); | - | ||||||
3364 | d->autoFormatting = enable; | - | ||||||
3365 | } | - | ||||||
3366 | - | |||||||
3367 | /*! | - | ||||||
3368 | \since 4.4 | - | ||||||
3369 | - | |||||||
3370 | Returns \c true if auto formattting is enabled, otherwise \c false. | - | ||||||
3371 | */ | - | ||||||
3372 | bool QXmlStreamWriter::autoFormatting() const | - | ||||||
3373 | { | - | ||||||
3374 | Q_D(const QXmlStreamWriter); | - | ||||||
3375 | return d->autoFormatting; | - | ||||||
3376 | } | - | ||||||
3377 | - | |||||||
3378 | /*! | - | ||||||
3379 | \property QXmlStreamWriter::autoFormattingIndent | - | ||||||
3380 | \since 4.4 | - | ||||||
3381 | - | |||||||
3382 | \brief the number of spaces or tabs used for indentation when | - | ||||||
3383 | auto-formatting is enabled. Positive numbers indicate spaces, | - | ||||||
3384 | negative numbers tabs. | - | ||||||
3385 | - | |||||||
3386 | The default indentation is 4. | - | ||||||
3387 | - | |||||||
3388 | \sa autoFormatting | - | ||||||
3389 | */ | - | ||||||
3390 | - | |||||||
3391 | - | |||||||
3392 | void QXmlStreamWriter::setAutoFormattingIndent(int spacesOrTabs) | - | ||||||
3393 | { | - | ||||||
3394 | Q_D(QXmlStreamWriter); | - | ||||||
3395 | d->autoFormattingIndent = QByteArray(qAbs(spacesOrTabs), spacesOrTabs >= 0 ? ' ' : '\t'); | - | ||||||
3396 | } | - | ||||||
3397 | - | |||||||
3398 | int QXmlStreamWriter::autoFormattingIndent() const | - | ||||||
3399 | { | - | ||||||
3400 | Q_D(const QXmlStreamWriter); | - | ||||||
3401 | return d->autoFormattingIndent.count(' ') - d->autoFormattingIndent.count('\t'); | - | ||||||
3402 | } | - | ||||||
3403 | - | |||||||
3404 | /*! | - | ||||||
3405 | Returns \c true if the stream failed to write to the underlying device. | - | ||||||
3406 | - | |||||||
3407 | The error status is never reset. Writes happening after the error | - | ||||||
3408 | occurred are ignored, even if the error condition is cleared. | - | ||||||
3409 | */ | - | ||||||
3410 | bool QXmlStreamWriter::hasError() const | - | ||||||
3411 | { | - | ||||||
3412 | Q_D(const QXmlStreamWriter); | - | ||||||
3413 | return d->hasError; | - | ||||||
3414 | } | - | ||||||
3415 | - | |||||||
3416 | /*! | - | ||||||
3417 | \overload | - | ||||||
3418 | Writes an attribute with \a qualifiedName and \a value. | - | ||||||
3419 | - | |||||||
3420 | - | |||||||
3421 | This function can only be called after writeStartElement() before | - | ||||||
3422 | any content is written, or after writeEmptyElement(). | - | ||||||
3423 | */ | - | ||||||
3424 | void QXmlStreamWriter::writeAttribute(const QString &qualifiedName, const QString &value) | - | ||||||
3425 | { | - | ||||||
3426 | Q_D(QXmlStreamWriter); | - | ||||||
3427 | Q_ASSERT(d->inStartElement); | - | ||||||
3428 | Q_ASSERT(qualifiedName.count(QLatin1Char(':')) <= 1); | - | ||||||
3429 | d->write(" "); | - | ||||||
3430 | d->write(qualifiedName); | - | ||||||
3431 | d->write("=\""); | - | ||||||
3432 | d->writeEscaped(value, true); | - | ||||||
3433 | d->write("\""); | - | ||||||
3434 | } | - | ||||||
3435 | - | |||||||
3436 | /*! Writes an attribute with \a name and \a value, prefixed for | - | ||||||
3437 | the specified \a namespaceUri. If the namespace has not been | - | ||||||
3438 | declared yet, QXmlStreamWriter will generate a namespace declaration | - | ||||||
3439 | for it. | - | ||||||
3440 | - | |||||||
3441 | This function can only be called after writeStartElement() before | - | ||||||
3442 | any content is written, or after writeEmptyElement(). | - | ||||||
3443 | */ | - | ||||||
3444 | void QXmlStreamWriter::writeAttribute(const QString &namespaceUri, const QString &name, const QString &value) | - | ||||||
3445 | { | - | ||||||
3446 | Q_D(QXmlStreamWriter); | - | ||||||
3447 | Q_ASSERT(d->inStartElement); | - | ||||||
3448 | Q_ASSERT(!name.contains(QLatin1Char(':'))); | - | ||||||
3449 | QXmlStreamWriterPrivate::NamespaceDeclaration &namespaceDeclaration = d->findNamespace(namespaceUri, true, true); | - | ||||||
3450 | d->write(" "); | - | ||||||
3451 | if (!namespaceDeclaration.prefix.isEmpty()) { | - | ||||||
3452 | d->write(namespaceDeclaration.prefix); | - | ||||||
3453 | d->write(":"); | - | ||||||
3454 | } | - | ||||||
3455 | d->write(name); | - | ||||||
3456 | d->write("=\""); | - | ||||||
3457 | d->writeEscaped(value, true); | - | ||||||
3458 | d->write("\""); | - | ||||||
3459 | } | - | ||||||
3460 | - | |||||||
3461 | /*! | - | ||||||
3462 | \overload | - | ||||||
3463 | - | |||||||
3464 | Writes the \a attribute. | - | ||||||
3465 | - | |||||||
3466 | This function can only be called after writeStartElement() before | - | ||||||
3467 | any content is written, or after writeEmptyElement(). | - | ||||||
3468 | */ | - | ||||||
3469 | void QXmlStreamWriter::writeAttribute(const QXmlStreamAttribute& attribute) | - | ||||||
3470 | { | - | ||||||
3471 | if (attribute.namespaceUri().isEmpty()) | - | ||||||
3472 | writeAttribute(attribute.qualifiedName().toString(), | - | ||||||
3473 | attribute.value().toString()); | - | ||||||
3474 | else | - | ||||||
3475 | writeAttribute(attribute.namespaceUri().toString(), | - | ||||||
3476 | attribute.name().toString(), | - | ||||||
3477 | attribute.value().toString()); | - | ||||||
3478 | } | - | ||||||
3479 | - | |||||||
3480 | - | |||||||
3481 | /*! Writes the attribute vector \a attributes. If a namespace | - | ||||||
3482 | referenced in an attribute not been declared yet, QXmlStreamWriter | - | ||||||
3483 | will generate a namespace declaration for it. | - | ||||||
3484 | - | |||||||
3485 | This function can only be called after writeStartElement() before | - | ||||||
3486 | any content is written, or after writeEmptyElement(). | - | ||||||
3487 | - | |||||||
3488 | \sa writeAttribute(), writeNamespace() | - | ||||||
3489 | */ | - | ||||||
3490 | void QXmlStreamWriter::writeAttributes(const QXmlStreamAttributes& attributes) | - | ||||||
3491 | { | - | ||||||
3492 | Q_D(QXmlStreamWriter); | - | ||||||
3493 | Q_ASSERT(d->inStartElement); | - | ||||||
3494 | Q_UNUSED(d); | - | ||||||
3495 | for (int i = 0; i < attributes.size(); ++i) | - | ||||||
3496 | writeAttribute(attributes.at(i)); | - | ||||||
3497 | } | - | ||||||
3498 | - | |||||||
3499 | - | |||||||
3500 | /*! Writes \a text as CDATA section. If \a text contains the | - | ||||||
3501 | forbidden character sequence "]]>", it is split into different CDATA | - | ||||||
3502 | sections. | - | ||||||
3503 | - | |||||||
3504 | This function mainly exists for completeness. Normally you should | - | ||||||
3505 | not need use it, because writeCharacters() automatically escapes all | - | ||||||
3506 | non-content characters. | - | ||||||
3507 | */ | - | ||||||
3508 | void QXmlStreamWriter::writeCDATA(const QString &text) | - | ||||||
3509 | { | - | ||||||
3510 | Q_D(QXmlStreamWriter); | - | ||||||
3511 | d->finishStartElement(); | - | ||||||
3512 | QString copy(text); | - | ||||||
3513 | copy.replace(QLatin1String("]]>"), QLatin1String("]]]]><![CDATA[>")); | - | ||||||
3514 | d->write("<![CDATA["); | - | ||||||
3515 | d->write(copy); | - | ||||||
3516 | d->write("]]>"); | - | ||||||
3517 | } | - | ||||||
3518 | - | |||||||
3519 | - | |||||||
3520 | /*! Writes \a text. The characters "<", "&", and "\"" are escaped as entity | - | ||||||
3521 | references "<", "&, and """. To avoid the forbidden sequence | - | ||||||
3522 | "]]>", ">" is also escaped as ">". | - | ||||||
3523 | - | |||||||
3524 | \sa writeEntityReference() | - | ||||||
3525 | */ | - | ||||||
3526 | void QXmlStreamWriter::writeCharacters(const QString &text) | - | ||||||
3527 | { | - | ||||||
3528 | Q_D(QXmlStreamWriter); | - | ||||||
3529 | d->finishStartElement(); | - | ||||||
3530 | d->writeEscaped(text); | - | ||||||
3531 | } | - | ||||||
3532 | - | |||||||
3533 | - | |||||||
3534 | /*! Writes \a text as XML comment, where \a text must not contain the | - | ||||||
3535 | forbidden sequence "--" or end with "-". Note that XML does not | - | ||||||
3536 | provide any way to escape "-" in a comment. | - | ||||||
3537 | */ | - | ||||||
3538 | void QXmlStreamWriter::writeComment(const QString &text) | - | ||||||
3539 | { | - | ||||||
3540 | Q_D(QXmlStreamWriter); | - | ||||||
3541 | Q_ASSERT(!text.contains(QLatin1String("--")) && !text.endsWith(QLatin1Char('-'))); | - | ||||||
3542 | if (!d->finishStartElement(false) && d->autoFormatting) | - | ||||||
3543 | d->indent(d->tagStack.size()); | - | ||||||
3544 | d->write("<!--"); | - | ||||||
3545 | d->write(text); | - | ||||||
3546 | d->write("-->"); | - | ||||||
3547 | d->inStartElement = d->lastWasStartElement = false; | - | ||||||
3548 | } | - | ||||||
3549 | - | |||||||
3550 | - | |||||||
3551 | /*! Writes a DTD section. The \a dtd represents the entire | - | ||||||
3552 | doctypedecl production from the XML 1.0 specification. | - | ||||||
3553 | */ | - | ||||||
3554 | void QXmlStreamWriter::writeDTD(const QString &dtd) | - | ||||||
3555 | { | - | ||||||
3556 | Q_D(QXmlStreamWriter); | - | ||||||
3557 | d->finishStartElement(); | - | ||||||
3558 | if (d->autoFormatting) | - | ||||||
3559 | d->write("\n"); | - | ||||||
3560 | d->write(dtd); | - | ||||||
3561 | if (d->autoFormatting) | - | ||||||
3562 | d->write("\n"); | - | ||||||
3563 | } | - | ||||||
3564 | - | |||||||
3565 | - | |||||||
3566 | - | |||||||
3567 | /*! \overload | - | ||||||
3568 | Writes an empty element with qualified name \a qualifiedName. | - | ||||||
3569 | Subsequent calls to writeAttribute() will add attributes to this element. | - | ||||||
3570 | */ | - | ||||||
3571 | void QXmlStreamWriter::writeEmptyElement(const QString &qualifiedName) | - | ||||||
3572 | { | - | ||||||
3573 | Q_D(QXmlStreamWriter); | - | ||||||
3574 | Q_ASSERT(qualifiedName.count(QLatin1Char(':')) <= 1); | - | ||||||
3575 | d->writeStartElement(QString(), qualifiedName); | - | ||||||
3576 | d->inEmptyElement = true; | - | ||||||
3577 | } | - | ||||||
3578 | - | |||||||
3579 | - | |||||||
3580 | /*! Writes an empty element with \a name, prefixed for the specified | - | ||||||
3581 | \a namespaceUri. If the namespace has not been declared, | - | ||||||
3582 | QXmlStreamWriter will generate a namespace declaration for it. | - | ||||||
3583 | Subsequent calls to writeAttribute() will add attributes to this element. | - | ||||||
3584 | - | |||||||
3585 | \sa writeNamespace() | - | ||||||
3586 | */ | - | ||||||
3587 | void QXmlStreamWriter::writeEmptyElement(const QString &namespaceUri, const QString &name) | - | ||||||
3588 | { | - | ||||||
3589 | Q_D(QXmlStreamWriter); | - | ||||||
3590 | Q_ASSERT(!name.contains(QLatin1Char(':'))); | - | ||||||
3591 | d->writeStartElement(namespaceUri, name); | - | ||||||
3592 | d->inEmptyElement = true; | - | ||||||
3593 | } | - | ||||||
3594 | - | |||||||
3595 | - | |||||||
3596 | /*!\overload | - | ||||||
3597 | Writes a text element with \a qualifiedName and \a text. | - | ||||||
3598 | - | |||||||
3599 | - | |||||||
3600 | This is a convenience function equivalent to: | - | ||||||
3601 | \snippet code/src_corelib_xml_qxmlstream.cpp 1 | - | ||||||
3602 | - | |||||||
3603 | */ | - | ||||||
3604 | void QXmlStreamWriter::writeTextElement(const QString &qualifiedName, const QString &text) | - | ||||||
3605 | { | - | ||||||
3606 | writeStartElement(qualifiedName); | - | ||||||
3607 | writeCharacters(text); | - | ||||||
3608 | writeEndElement(); | - | ||||||
3609 | } | - | ||||||
3610 | - | |||||||
3611 | /*! Writes a text element with \a name, prefixed for the specified \a | - | ||||||
3612 | namespaceUri, and \a text. If the namespace has not been | - | ||||||
3613 | declared, QXmlStreamWriter will generate a namespace declaration | - | ||||||
3614 | for it. | - | ||||||
3615 | - | |||||||
3616 | - | |||||||
3617 | This is a convenience function equivalent to: | - | ||||||
3618 | \snippet code/src_corelib_xml_qxmlstream.cpp 2 | - | ||||||
3619 | - | |||||||
3620 | */ | - | ||||||
3621 | void QXmlStreamWriter::writeTextElement(const QString &namespaceUri, const QString &name, const QString &text) | - | ||||||
3622 | { | - | ||||||
3623 | writeStartElement(namespaceUri, name); | - | ||||||
3624 | writeCharacters(text); | - | ||||||
3625 | writeEndElement(); | - | ||||||
3626 | } | - | ||||||
3627 | - | |||||||
3628 | - | |||||||
3629 | /*! | - | ||||||
3630 | Closes all remaining open start elements and writes a newline. | - | ||||||
3631 | - | |||||||
3632 | \sa writeStartDocument() | - | ||||||
3633 | */ | - | ||||||
3634 | void QXmlStreamWriter::writeEndDocument() | - | ||||||
3635 | { | - | ||||||
3636 | Q_D(QXmlStreamWriter); | - | ||||||
3637 | while (d->tagStack.size()) | - | ||||||
3638 | writeEndElement(); | - | ||||||
3639 | d->write("\n"); | - | ||||||
3640 | } | - | ||||||
3641 | - | |||||||
3642 | /*! | - | ||||||
3643 | Closes the previous start element. | - | ||||||
3644 | - | |||||||
3645 | \sa writeStartElement() | - | ||||||
3646 | */ | - | ||||||
3647 | void QXmlStreamWriter::writeEndElement() | - | ||||||
3648 | { | - | ||||||
3649 | Q_D(QXmlStreamWriter); | - | ||||||
3650 | if (d->tagStack.isEmpty()) | - | ||||||
3651 | return; | - | ||||||
3652 | - | |||||||
3653 | // shortcut: if nothing was written, close as empty tag | - | ||||||
3654 | if (d->inStartElement && !d->inEmptyElement) { | - | ||||||
3655 | d->write("/>"); | - | ||||||
3656 | d->lastWasStartElement = d->inStartElement = false; | - | ||||||
3657 | QXmlStreamWriterPrivate::Tag &tag = d->tagStack_pop(); | - | ||||||
3658 | d->lastNamespaceDeclaration = tag.namespaceDeclarationsSize; | - | ||||||
3659 | return; | - | ||||||
3660 | } | - | ||||||
3661 | - | |||||||
3662 | if (!d->finishStartElement(false) && !d->lastWasStartElement && d->autoFormatting) | - | ||||||
3663 | d->indent(d->tagStack.size()-1); | - | ||||||
3664 | if (d->tagStack.isEmpty()) | - | ||||||
3665 | return; | - | ||||||
3666 | d->lastWasStartElement = false; | - | ||||||
3667 | QXmlStreamWriterPrivate::Tag &tag = d->tagStack_pop(); | - | ||||||
3668 | d->lastNamespaceDeclaration = tag.namespaceDeclarationsSize; | - | ||||||
3669 | d->write("</"); | - | ||||||
3670 | if (!tag.namespaceDeclaration.prefix.isEmpty()) { | - | ||||||
3671 | d->write(tag.namespaceDeclaration.prefix); | - | ||||||
3672 | d->write(":"); | - | ||||||
3673 | } | - | ||||||
3674 | d->write(tag.name); | - | ||||||
3675 | d->write(">"); | - | ||||||
3676 | } | - | ||||||
3677 | - | |||||||
3678 | - | |||||||
3679 | - | |||||||
3680 | /*! | - | ||||||
3681 | Writes the entity reference \a name to the stream, as "&\a{name};". | - | ||||||
3682 | */ | - | ||||||
3683 | void QXmlStreamWriter::writeEntityReference(const QString &name) | - | ||||||
3684 | { | - | ||||||
3685 | Q_D(QXmlStreamWriter); | - | ||||||
3686 | d->finishStartElement(); | - | ||||||
3687 | d->write("&"); | - | ||||||
3688 | d->write(name); | - | ||||||
3689 | d->write(";"); | - | ||||||
3690 | } | - | ||||||
3691 | - | |||||||
3692 | - | |||||||
3693 | /*! Writes a namespace declaration for \a namespaceUri with \a | - | ||||||
3694 | prefix. If \a prefix is empty, QXmlStreamWriter assigns a unique | - | ||||||
3695 | prefix consisting of the letter 'n' followed by a number. | - | ||||||
3696 | - | |||||||
3697 | If writeStartElement() or writeEmptyElement() was called, the | - | ||||||
3698 | declaration applies to the current element; otherwise it applies to | - | ||||||
3699 | the next child element. | - | ||||||
3700 | - | |||||||
3701 | Note that the prefix \e xml is both predefined and reserved for | - | ||||||
3702 | \e http://www.w3.org/XML/1998/namespace, which in turn cannot be | - | ||||||
3703 | bound to any other prefix. The prefix \e xmlns and its URI | - | ||||||
3704 | \e http://www.w3.org/2000/xmlns/ are used for the namespace mechanism | - | ||||||
3705 | itself and thus completely forbidden in declarations. | - | ||||||
3706 | - | |||||||
3707 | */ | - | ||||||
3708 | void QXmlStreamWriter::writeNamespace(const QString &namespaceUri, const QString &prefix) | - | ||||||
3709 | { | - | ||||||
3710 | Q_D(QXmlStreamWriter); | - | ||||||
3711 | Q_ASSERT(!namespaceUri.isEmpty()); | - | ||||||
3712 | Q_ASSERT(prefix != QLatin1String("xmlns")); | - | ||||||
3713 | if (prefix.isEmpty()) { | - | ||||||
3714 | d->findNamespace(namespaceUri, d->inStartElement); | - | ||||||
3715 | } else { | - | ||||||
3716 | Q_ASSERT(!((prefix == QLatin1String("xml")) ^ (namespaceUri == QLatin1String("http://www.w3.org/XML/1998/namespace")))); | - | ||||||
3717 | Q_ASSERT(namespaceUri != QLatin1String("http://www.w3.org/2000/xmlns/")); | - | ||||||
3718 | QXmlStreamWriterPrivate::NamespaceDeclaration &namespaceDeclaration = d->namespaceDeclarations.push(); | - | ||||||
3719 | namespaceDeclaration.prefix = d->addToStringStorage(prefix); | - | ||||||
3720 | namespaceDeclaration.namespaceUri = d->addToStringStorage(namespaceUri); | - | ||||||
3721 | if (d->inStartElement) | - | ||||||
3722 | d->writeNamespaceDeclaration(namespaceDeclaration); | - | ||||||
3723 | } | - | ||||||
3724 | } | - | ||||||
3725 | - | |||||||
3726 | - | |||||||
3727 | /*! Writes a default namespace declaration for \a namespaceUri. | - | ||||||
3728 | - | |||||||
3729 | If writeStartElement() or writeEmptyElement() was called, the | - | ||||||
3730 | declaration applies to the current element; otherwise it applies to | - | ||||||
3731 | the next child element. | - | ||||||
3732 | - | |||||||
3733 | Note that the namespaces \e http://www.w3.org/XML/1998/namespace | - | ||||||
3734 | (bound to \e xmlns) and \e http://www.w3.org/2000/xmlns/ (bound to | - | ||||||
3735 | \e xml) by definition cannot be declared as default. | - | ||||||
3736 | */ | - | ||||||
3737 | void QXmlStreamWriter::writeDefaultNamespace(const QString &namespaceUri) | - | ||||||
3738 | { | - | ||||||
3739 | Q_D(QXmlStreamWriter); | - | ||||||
3740 | Q_ASSERT(namespaceUri != QLatin1String("http://www.w3.org/XML/1998/namespace")); | - | ||||||
3741 | Q_ASSERT(namespaceUri != QLatin1String("http://www.w3.org/2000/xmlns/")); | - | ||||||
3742 | QXmlStreamWriterPrivate::NamespaceDeclaration &namespaceDeclaration = d->namespaceDeclarations.push(); | - | ||||||
3743 | namespaceDeclaration.prefix.clear(); | - | ||||||
3744 | namespaceDeclaration.namespaceUri = d->addToStringStorage(namespaceUri); | - | ||||||
3745 | if (d->inStartElement) | - | ||||||
3746 | d->writeNamespaceDeclaration(namespaceDeclaration); | - | ||||||
3747 | } | - | ||||||
3748 | - | |||||||
3749 | - | |||||||
3750 | /*! | - | ||||||
3751 | Writes an XML processing instruction with \a target and \a data, | - | ||||||
3752 | where \a data must not contain the sequence "?>". | - | ||||||
3753 | */ | - | ||||||
3754 | void QXmlStreamWriter::writeProcessingInstruction(const QString &target, const QString &data) | - | ||||||
3755 | { | - | ||||||
3756 | Q_D(QXmlStreamWriter); | - | ||||||
3757 | Q_ASSERT(!data.contains(QLatin1String("?>"))); | - | ||||||
3758 | if (!d->finishStartElement(false) && d->autoFormatting) | - | ||||||
3759 | d->indent(d->tagStack.size()); | - | ||||||
3760 | d->write("<?"); | - | ||||||
3761 | d->write(target); | - | ||||||
3762 | if (!data.isNull()) { | - | ||||||
3763 | d->write(" "); | - | ||||||
3764 | d->write(data); | - | ||||||
3765 | } | - | ||||||
3766 | d->write("?>"); | - | ||||||
3767 | } | - | ||||||
3768 | - | |||||||
3769 | - | |||||||
3770 | - | |||||||
3771 | /*!\overload | - | ||||||
3772 | - | |||||||
3773 | Writes a document start with XML version number "1.0". This also | - | ||||||
3774 | writes the encoding information. | - | ||||||
3775 | - | |||||||
3776 | \sa writeEndDocument(), setCodec() | - | ||||||
3777 | \since 4.5 | - | ||||||
3778 | */ | - | ||||||
3779 | void QXmlStreamWriter::writeStartDocument() | - | ||||||
3780 | { | - | ||||||
3781 | writeStartDocument(QLatin1String("1.0")); | - | ||||||
3782 | } | - | ||||||
3783 | - | |||||||
3784 | - | |||||||
3785 | /*! | - | ||||||
3786 | Writes a document start with the XML version number \a version. | - | ||||||
3787 | - | |||||||
3788 | \sa writeEndDocument() | - | ||||||
3789 | */ | - | ||||||
3790 | void QXmlStreamWriter::writeStartDocument(const QString &version) | - | ||||||
3791 | { | - | ||||||
3792 | Q_D(QXmlStreamWriter); | - | ||||||
3793 | d->finishStartElement(false); | - | ||||||
3794 | d->write("<?xml version=\""); | - | ||||||
3795 | d->write(version); | - | ||||||
3796 | if (d->device) { // stringDevice does not get any encoding
| 2-35 | ||||||
3797 | d->write("\" encoding=\""); | - | ||||||
3798 | #ifdef QT_NO_TEXTCODEC | - | ||||||
3799 | d->write("iso-8859-1"); | - | ||||||
3800 | #else | - | ||||||
3801 | d->write(const QByteArray name = d->codec->name().constData(),(); | - | ||||||
3802 | d->codec->write(name()..constData(), name.length()); | - | ||||||
3803 | #endif | - | ||||||
3804 | } executed 35 times by 3 tests: end of block Executed by:
| 35 | ||||||
3805 | d->write("\"?>"); | - | ||||||
3806 | } executed 37 times by 3 tests: end of block Executed by:
| 37 | ||||||
3807 | - | |||||||
3808 | /*! Writes a document start with the XML version number \a version | - | ||||||
3809 | and a standalone attribute \a standalone. | - | ||||||
3810 | - | |||||||
3811 | \sa writeEndDocument() | - | ||||||
3812 | \since 4.5 | - | ||||||
3813 | */ | - | ||||||
3814 | void QXmlStreamWriter::writeStartDocument(const QString &version, bool standalone) | - | ||||||
3815 | { | - | ||||||
3816 | Q_D(QXmlStreamWriter); | - | ||||||
3817 | d->finishStartElement(false); | - | ||||||
3818 | d->write("<?xml version=\""); | - | ||||||
3819 | d->write(version); | - | ||||||
3820 | if (d->device) { // stringDevice does not get any encoding
| 0-2 | ||||||
3821 | d->write("\" encoding=\""); | - | ||||||
3822 | #ifdef QT_NO_TEXTCODEC | - | ||||||
3823 | d->write("iso-8859-1"); | - | ||||||
3824 | #else | - | ||||||
3825 | d->write(const QByteArray name = d->codec->name().constData(),(); | - | ||||||
3826 | d->codec->write(name()..constData(), name.length()); | - | ||||||
3827 | #endif | - | ||||||
3828 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
3829 | if (standalone)
| 1 | ||||||
3830 | d->write("\" standalone=\"yes\"?>"); executed 1 time by 1 test: d->write("\" standalone=\"yes\"?>"); Executed by:
| 1 | ||||||
3831 | else | - | ||||||
3832 | d->write("\" standalone=\"no\"?>"); executed 1 time by 1 test: d->write("\" standalone=\"no\"?>"); Executed by:
| 1 | ||||||
3833 | } | - | ||||||
3834 | - | |||||||
3835 | - | |||||||
3836 | /*!\overload | - | ||||||
3837 | - | |||||||
3838 | Writes a start element with \a qualifiedName. Subsequent calls to | - | ||||||
3839 | writeAttribute() will add attributes to this element. | - | ||||||
3840 | - | |||||||
3841 | \sa writeEndElement(), writeEmptyElement() | - | ||||||
3842 | */ | - | ||||||
3843 | void QXmlStreamWriter::writeStartElement(const QString &qualifiedName) | - | ||||||
3844 | { | - | ||||||
3845 | Q_D(QXmlStreamWriter); | - | ||||||
3846 | Q_ASSERT(qualifiedName.count(QLatin1Char(':')) <= 1); | - | ||||||
3847 | d->writeStartElement(QString(), qualifiedName); | - | ||||||
3848 | } | - | ||||||
3849 | - | |||||||
3850 | - | |||||||
3851 | /*! Writes a start element with \a name, prefixed for the specified | - | ||||||
3852 | \a namespaceUri. If the namespace has not been declared yet, | - | ||||||
3853 | QXmlStreamWriter will generate a namespace declaration for | - | ||||||
3854 | it. Subsequent calls to writeAttribute() will add attributes to this | - | ||||||
3855 | element. | - | ||||||
3856 | - | |||||||
3857 | \sa writeNamespace(), writeEndElement(), writeEmptyElement() | - | ||||||
3858 | */ | - | ||||||
3859 | void QXmlStreamWriter::writeStartElement(const QString &namespaceUri, const QString &name) | - | ||||||
3860 | { | - | ||||||
3861 | Q_D(QXmlStreamWriter); | - | ||||||
3862 | Q_ASSERT(!name.contains(QLatin1Char(':'))); | - | ||||||
3863 | d->writeStartElement(namespaceUri, name); | - | ||||||
3864 | } | - | ||||||
3865 | - | |||||||
3866 | void QXmlStreamWriterPrivate::writeStartElement(const QString &namespaceUri, const QString &name) | - | ||||||
3867 | { | - | ||||||
3868 | if (!finishStartElement(false) && autoFormatting) | - | ||||||
3869 | indent(tagStack.size()); | - | ||||||
3870 | - | |||||||
3871 | Tag &tag = tagStack_push(); | - | ||||||
3872 | tag.name = addToStringStorage(name); | - | ||||||
3873 | tag.namespaceDeclaration = findNamespace(namespaceUri); | - | ||||||
3874 | write("<"); | - | ||||||
3875 | if (!tag.namespaceDeclaration.prefix.isEmpty()) { | - | ||||||
3876 | write(tag.namespaceDeclaration.prefix); | - | ||||||
3877 | write(":"); | - | ||||||
3878 | } | - | ||||||
3879 | write(tag.name); | - | ||||||
3880 | inStartElement = lastWasStartElement = true; | - | ||||||
3881 | - | |||||||
3882 | for (int i = lastNamespaceDeclaration; i < namespaceDeclarations.size(); ++i) | - | ||||||
3883 | writeNamespaceDeclaration(namespaceDeclarations[i]); | - | ||||||
3884 | tag.namespaceDeclarationsSize = lastNamespaceDeclaration; | - | ||||||
3885 | } | - | ||||||
3886 | - | |||||||
3887 | #ifndef QT_NO_XMLSTREAMREADER | - | ||||||
3888 | /*! Writes the current state of the \a reader. All possible valid | - | ||||||
3889 | states are supported. | - | ||||||
3890 | - | |||||||
3891 | The purpose of this function is to support chained processing of XML data. | - | ||||||
3892 | - | |||||||
3893 | \sa QXmlStreamReader::tokenType() | - | ||||||
3894 | */ | - | ||||||
3895 | void QXmlStreamWriter::writeCurrentToken(const QXmlStreamReader &reader) | - | ||||||
3896 | { | - | ||||||
3897 | switch (reader.tokenType()) { | - | ||||||
3898 | case QXmlStreamReader::NoToken: | - | ||||||
3899 | break; | - | ||||||
3900 | case QXmlStreamReader::StartDocument: | - | ||||||
3901 | writeStartDocument(); | - | ||||||
3902 | break; | - | ||||||
3903 | case QXmlStreamReader::EndDocument: | - | ||||||
3904 | writeEndDocument(); | - | ||||||
3905 | break; | - | ||||||
3906 | case QXmlStreamReader::StartElement: { | - | ||||||
3907 | QXmlStreamNamespaceDeclarations namespaceDeclarations = reader.namespaceDeclarations(); | - | ||||||
3908 | for (int i = 0; i < namespaceDeclarations.size(); ++i) { | - | ||||||
3909 | const QXmlStreamNamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(i); | - | ||||||
3910 | writeNamespace(namespaceDeclaration.namespaceUri().toString(), | - | ||||||
3911 | namespaceDeclaration.prefix().toString()); | - | ||||||
3912 | } | - | ||||||
3913 | writeStartElement(reader.namespaceUri().toString(), reader.name().toString()); | - | ||||||
3914 | writeAttributes(reader.attributes()); | - | ||||||
3915 | } break; | - | ||||||
3916 | case QXmlStreamReader::EndElement: | - | ||||||
3917 | writeEndElement(); | - | ||||||
3918 | break; | - | ||||||
3919 | case QXmlStreamReader::Characters: | - | ||||||
3920 | if (reader.isCDATA()) | - | ||||||
3921 | writeCDATA(reader.text().toString()); | - | ||||||
3922 | else | - | ||||||
3923 | writeCharacters(reader.text().toString()); | - | ||||||
3924 | break; | - | ||||||
3925 | case QXmlStreamReader::Comment: | - | ||||||
3926 | writeComment(reader.text().toString()); | - | ||||||
3927 | break; | - | ||||||
3928 | case QXmlStreamReader::DTD: | - | ||||||
3929 | writeDTD(reader.text().toString()); | - | ||||||
3930 | break; | - | ||||||
3931 | case QXmlStreamReader::EntityReference: | - | ||||||
3932 | writeEntityReference(reader.name().toString()); | - | ||||||
3933 | break; | - | ||||||
3934 | case QXmlStreamReader::ProcessingInstruction: | - | ||||||
3935 | writeProcessingInstruction(reader.processingInstructionTarget().toString(), | - | ||||||
3936 | reader.processingInstructionData().toString()); | - | ||||||
3937 | break; | - | ||||||
3938 | default: | - | ||||||
3939 | Q_ASSERT(reader.tokenType() != QXmlStreamReader::Invalid); | - | ||||||
3940 | qWarning("QXmlStreamWriter: writeCurrentToken() with invalid state."); | - | ||||||
3941 | break; | - | ||||||
3942 | } | - | ||||||
3943 | } | - | ||||||
3944 | - | |||||||
3945 | /*! | - | ||||||
3946 | \fn bool QXmlStreamAttributes::hasAttribute(const QString &qualifiedName) const | - | ||||||
3947 | \since 4.5 | - | ||||||
3948 | - | |||||||
3949 | Returns \c true if this QXmlStreamAttributes has an attribute whose | - | ||||||
3950 | qualified name is \a qualifiedName; otherwise returns \c false. | - | ||||||
3951 | - | |||||||
3952 | Note that this is not namespace aware. For instance, if this | - | ||||||
3953 | QXmlStreamAttributes contains an attribute whose lexical name is "xlink:href" | - | ||||||
3954 | this doesn't tell that an attribute named \c href in the XLink namespace is | - | ||||||
3955 | present, since the \c xlink prefix can be bound to any namespace. Use the | - | ||||||
3956 | overload that takes a namespace URI and a local name as parameter, for | - | ||||||
3957 | namespace aware code. | - | ||||||
3958 | */ | - | ||||||
3959 | - | |||||||
3960 | /*! | - | ||||||
3961 | \fn bool QXmlStreamAttributes::hasAttribute(QLatin1String qualifiedName) const | - | ||||||
3962 | \overload | - | ||||||
3963 | \since 4.5 | - | ||||||
3964 | */ | - | ||||||
3965 | - | |||||||
3966 | /*! | - | ||||||
3967 | \fn bool QXmlStreamAttributes::hasAttribute(const QString &namespaceUri, | - | ||||||
3968 | const QString &name) const | - | ||||||
3969 | \overload | - | ||||||
3970 | \since 4.5 | - | ||||||
3971 | - | |||||||
3972 | Returns \c true if this QXmlStreamAttributes has an attribute whose | - | ||||||
3973 | namespace URI and name correspond to \a namespaceUri and \a name; | - | ||||||
3974 | otherwise returns \c false. | - | ||||||
3975 | */ | - | ||||||
3976 | - | |||||||
3977 | #endif // QT_NO_XMLSTREAMREADER | - | ||||||
3978 | #endif // QT_NO_XMLSTREAMWRITER | - | ||||||
3979 | - | |||||||
3980 | QT_END_NAMESPACE | - | ||||||
3981 | - | |||||||
3982 | #endif // QT_NO_XMLSTREAM | - | ||||||
Source code | Switch to Preprocessed file |