xml/qxmlstream.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5QXmlStreamEntityResolver::~QXmlStreamEntityResolver() -
6{ -
7} -
8 -
9 -
10 -
11 -
12 -
13 -
14QString QXmlStreamEntityResolver::resolveEntity(const QString& , const QString& ) -
15{ -
16 return QString();
-
17} -
18QString QXmlStreamEntityResolver::resolveUndeclaredEntity(const QString & ) -
19{ -
20 return QString();
-
21} -
22 -
23 -
24 -
25QString QXmlStreamReaderPrivate::resolveUndeclaredEntity(const QString &name) -
26{ -
27 if (entityResolver)
-
28 return entityResolver->resolveUndeclaredEntity(name);
-
29 return QString();
-
30} -
31void QXmlStreamReader::setEntityResolver(QXmlStreamEntityResolver *resolver) -
32{ -
33 QXmlStreamReaderPrivate * const d = d_func(); -
34 d->entityResolver = resolver; -
35}
-
36QXmlStreamEntityResolver *QXmlStreamReader::entityResolver() const -
37{ -
38 const QXmlStreamReaderPrivate * const d = d_func(); -
39 return d->entityResolver;
-
40} -
41QXmlStreamReader::QXmlStreamReader() -
42 : d_ptr(new QXmlStreamReaderPrivate(this)) -
43{ -
44}
-
45 -
46 -
47 -
48 -
49 -
50QXmlStreamReader::QXmlStreamReader(QIODevice *device) -
51 : d_ptr(new QXmlStreamReaderPrivate(this)) -
52{ -
53 setDevice(device); -
54}
-
55 -
56 -
57 -
58 -
59 -
60 -
61QXmlStreamReader::QXmlStreamReader(const QByteArray &data) -
62 : d_ptr(new QXmlStreamReaderPrivate(this)) -
63{ -
64 QXmlStreamReaderPrivate * const d = d_func(); -
65 d->dataBuffer = data; -
66}
-
67 -
68 -
69 -
70 -
71 -
72 -
73QXmlStreamReader::QXmlStreamReader(const QString &data) -
74 : d_ptr(new QXmlStreamReaderPrivate(this)) -
75{ -
76 QXmlStreamReaderPrivate * const d = d_func(); -
77 -
78 -
79 -
80 d->dataBuffer = d->codec->fromUnicode(data); -
81 d->decoder = d->codec->makeDecoder(); -
82 -
83 d->lockEncoding = true; -
84 -
85}
-
86 -
87 -
88 -
89 -
90 -
91 -
92QXmlStreamReader::QXmlStreamReader(const char *data) -
93 : d_ptr(new QXmlStreamReaderPrivate(this)) -
94{ -
95 QXmlStreamReaderPrivate * const d = d_func(); -
96 d->dataBuffer = QByteArray(data); -
97}
-
98 -
99 -
100 -
101 -
102QXmlStreamReader::~QXmlStreamReader() -
103{ -
104 QXmlStreamReaderPrivate * const d = d_func(); -
105 if (d->deleteDevice)
-
106 delete d->device;
-
107}
-
108void QXmlStreamReader::setDevice(QIODevice *device) -
109{ -
110 QXmlStreamReaderPrivate * const d = d_func(); -
111 if (d->deleteDevice) {
-
112 delete d->device; -
113 d->deleteDevice = false; -
114 }
-
115 d->device = device; -
116 d->init(); -
117 -
118}
-
119 -
120 -
121 -
122 -
123 -
124 -
125 -
126QIODevice *QXmlStreamReader::device() const -
127{ -
128 const QXmlStreamReaderPrivate * const d = d_func(); -
129 return d->device;
-
130} -
131void QXmlStreamReader::addData(const QByteArray &data) -
132{ -
133 QXmlStreamReaderPrivate * const d = d_func(); -
134 if (d->device) {
-
135 QMessageLogger("xml/qxmlstream.cpp", 504, __PRETTY_FUNCTION__).warning("QXmlStreamReader: addData() with device()"); -
136 return;
-
137 } -
138 d->dataBuffer += data; -
139}
-
140 -
141 -
142 -
143 -
144 -
145 -
146 -
147void QXmlStreamReader::addData(const QString &data) -
148{ -
149 QXmlStreamReaderPrivate * const d = d_func(); -
150 d->lockEncoding = true; -
151 -
152 -
153 -
154 addData(d->codec->fromUnicode(data)); -
155 -
156}
-
157 -
158 -
159 -
160 -
161 -
162 -
163 -
164void QXmlStreamReader::addData(const char *data) -
165{ -
166 addData(QByteArray(data)); -
167}
-
168 -
169 -
170 -
171 -
172 -
173 -
174 -
175void QXmlStreamReader::clear() -
176{ -
177 QXmlStreamReaderPrivate * const d = d_func(); -
178 d->init(); -
179 if (d->device) {
-
180 if (d->deleteDevice)
-
181 delete d->device;
-
182 d->device = 0; -
183 }
-
184}
-
185bool QXmlStreamReader::atEnd() const -
186{ -
187 const QXmlStreamReaderPrivate * const d = d_func(); -
188 if (d->atEnd
-
189 && ((d->type == QXmlStreamReader::Invalid && d->error == PrematureEndOfDocumentError)
-
190 || (d->type == QXmlStreamReader::EndDocument))) {
-
191 if (d->device)
-
192 return d->device->atEnd();
-
193 else -
194 return !d->dataBuffer.size();
-
195 } -
196 return (d->atEnd || d->type == QXmlStreamReader::Invalid);
-
197} -
198QXmlStreamReader::TokenType QXmlStreamReader::readNext() -
199{ -
200 QXmlStreamReaderPrivate * const d = d_func(); -
201 if (d->type != Invalid) {
-
202 if (!d->hasCheckedStartDocument)
-
203 if (!d->checkStartDocument())
-
204 return d->type;
-
205 d->parse(); -
206 if (d->atEnd && d->type != EndDocument && d->type != Invalid)
-
207 d->raiseError(PrematureEndOfDocumentError);
-
208 else if (!d->atEnd && d->type == EndDocument)
-
209 d->raiseWellFormedError(QXmlStream::tr("Extra content at end of document."));
-
210 } else if (d->error == PrematureEndOfDocumentError) {
-
211 -
212 d->type = NoToken; -
213 d->atEnd = false; -
214 d->token = -1; -
215 return readNext();
-
216 } -
217 return d->type;
-
218} -
219QXmlStreamReader::TokenType QXmlStreamReader::tokenType() const -
220{ -
221 const QXmlStreamReaderPrivate * const d = d_func(); -
222 return d->type;
-
223} -
224bool QXmlStreamReader::readNextStartElement() -
225{ -
226 while (readNext() != Invalid) {
-
227 if (isEndElement())
-
228 return false;
-
229 else if (isStartElement())
-
230 return true;
-
231 } -
232 return false;
-
233} -
234void QXmlStreamReader::skipCurrentElement() -
235{ -
236 int depth = 1; -
237 while (depth && readNext() != Invalid) {
-
238 if (isEndElement())
-
239 --depth;
-
240 else if (isStartElement())
-
241 ++depth;
-
242 } -
243}
-
244static const char QXmlStreamReader_tokenTypeString_string[] = -
245 "NoToken\0" -
246 "Invalid\0" -
247 "StartDocument\0" -
248 "EndDocument\0" -
249 "StartElement\0" -
250 "EndElement\0" -
251 "Characters\0" -
252 "Comment\0" -
253 "DTD\0" -
254 "EntityReference\0" -
255 "ProcessingInstruction\0"; -
256 -
257static const short QXmlStreamReader_tokenTypeString_indices[] = { -
258 0, 8, 16, 30, 42, 55, 66, 77, 85, 89, 105, 0 -
259}; -
260void QXmlStreamReader::setNamespaceProcessing(bool enable) -
261{ -
262 QXmlStreamReaderPrivate * const d = d_func(); -
263 d->namespaceProcessing = enable; -
264}
-
265 -
266bool QXmlStreamReader::namespaceProcessing() const -
267{ -
268 const QXmlStreamReaderPrivate * const d = d_func(); -
269 return d->namespaceProcessing;
-
270} -
271 -
272 -
273 -
274 -
275 -
276QString QXmlStreamReader::tokenString() const -
277{ -
278 const QXmlStreamReaderPrivate * const d = d_func(); -
279 return QLatin1String(QXmlStreamReader_tokenTypeString_string + -
280 QXmlStreamReader_tokenTypeString_indices[d->type]);
-
281} -
282 -
283 -
284 -
285QXmlStreamPrivateTagStack::QXmlStreamPrivateTagStack() -
286{ -
287 tagStack.reserve(16); -
288 tagStackStringStorage.reserve(32); -
289 tagStackStringStorageSize = 0; -
290 NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); -
291 namespaceDeclaration.prefix = addToStringStorage(QLatin1String("xml")); -
292 namespaceDeclaration.namespaceUri = addToStringStorage(QLatin1String("http://www.w3.org/XML/1998/namespace")); -
293}
-
294 -
295 -
296 -
297QXmlStreamReaderPrivate::QXmlStreamReaderPrivate(QXmlStreamReader *q) -
298 :q_ptr(q) -
299{ -
300 device = 0; -
301 deleteDevice = false; -
302 -
303 decoder = 0; -
304 -
305 stack_size = 64; -
306 sym_stack = 0; -
307 state_stack = 0; -
308 reallocateStack(); -
309 entityResolver = 0; -
310 init(); -
311 entityHash.insert(QLatin1String("lt"), Entity::createLiteral(QLatin1String("<"))); -
312 entityHash.insert(QLatin1String("gt"), Entity::createLiteral(QLatin1String(">"))); -
313 entityHash.insert(QLatin1String("amp"), Entity::createLiteral(QLatin1String("&"))); -
314 entityHash.insert(QLatin1String("apos"), Entity::createLiteral(QLatin1String("'"))); -
315 entityHash.insert(QLatin1String("quot"), Entity::createLiteral(QLatin1String("\""))); -
316}
-
317 -
318void QXmlStreamReaderPrivate::init() -
319{ -
320 scanDtd = false; -
321 token = -1; -
322 token_char = 0; -
323 isEmptyElement = false; -
324 isWhitespace = true; -
325 isCDATA = false; -
326 standalone = false; -
327 tos = 0; -
328 resumeReduction = 0; -
329 state_stack[tos++] = 0; -
330 state_stack[tos] = 0; -
331 putStack.clear(); -
332 putStack.reserve(32); -
333 textBuffer.clear(); -
334 textBuffer.reserve(256); -
335 tagStack.clear(); -
336 tagsDone = false; -
337 attributes.clear(); -
338 attributes.reserve(16); -
339 lineNumber = lastLineStart = characterOffset = 0; -
340 readBufferPos = 0; -
341 nbytesread = 0; -
342 -
343 codec = QTextCodec::codecForMib(106); -
344 delete decoder; -
345 decoder = 0; -
346 -
347 attributeStack.clear(); -
348 attributeStack.reserve(16); -
349 entityParser = 0; -
350 hasCheckedStartDocument = false; -
351 normalizeLiterals = false; -
352 hasSeenTag = false; -
353 atEnd = false; -
354 inParseEntity = false; -
355 referenceToUnparsedEntityDetected = false; -
356 referenceToParameterEntityDetected = false; -
357 hasExternalDtdSubset = false; -
358 lockEncoding = false; -
359 namespaceProcessing = true; -
360 rawReadBuffer.clear(); -
361 dataBuffer.clear(); -
362 readBuffer.clear(); -
363 -
364 type = QXmlStreamReader::NoToken; -
365 error = QXmlStreamReader::NoError; -
366}
-
367 -
368 -
369 -
370 -
371 -
372void QXmlStreamReaderPrivate::parseEntity(const QString &value) -
373{ -
374 QXmlStreamReader * const q = q_func(); -
375 -
376 if (value.isEmpty())
-
377 return;
-
378 -
379 -
380 if (!entityParser)
-
381 entityParser = new QXmlStreamReaderPrivate(q);
-
382 else -
383 entityParser->init();
-
384 entityParser->inParseEntity = true; -
385 entityParser->readBuffer = value; -
386 entityParser->injectToken(PARSE_ENTITY); -
387 while (!entityParser->atEnd && entityParser->type != QXmlStreamReader::Invalid)
-
388 entityParser->parse();
-
389 if (entityParser->type == QXmlStreamReader::Invalid || entityParser->tagStack.size())
-
390 raiseWellFormedError(QXmlStream::tr("Invalid entity value."));
-
391 -
392}
-
393 -
394inline void QXmlStreamReaderPrivate::reallocateStack() -
395{ -
396 stack_size <<= 1; -
397 sym_stack = reinterpret_cast<Value*> (realloc(sym_stack, stack_size * sizeof(Value))); -
398 do { if (!(sym_stack)) qBadAlloc(); } while (0);
-
399 state_stack = reinterpret_cast<int*> (realloc(state_stack, stack_size * sizeof(int))); -
400 do { if (!(sym_stack)) qBadAlloc(); } while (0);
-
401}
-
402 -
403 -
404QXmlStreamReaderPrivate::~QXmlStreamReaderPrivate() -
405{ -
406 -
407 delete decoder; -
408 -
409 free(sym_stack); -
410 free(state_stack); -
411 delete entityParser; -
412}
-
413 -
414 -
415inline uint QXmlStreamReaderPrivate::filterCarriageReturn() -
416{ -
417 uint peekc = peekChar(); -
418 if (peekc == '\n') {
-
419 if (putStack.size())
-
420 putStack.pop();
-
421 else -
422 ++readBufferPos;
-
423 return peekc;
-
424 } -
425 if (peekc == 0) {
-
426 putChar('\r'); -
427 return 0;
-
428 } -
429 return '\n';
-
430} -
431 -
432 -
433 -
434 -
435 -
436inline uint QXmlStreamReaderPrivate::getChar() -
437{ -
438 uint c; -
439 if (putStack.size()) {
-
440 c = atEnd ? 0 : putStack.pop();
-
441 } else {
-
442 if (readBufferPos < readBuffer.size())
-
443 c = readBuffer.at(readBufferPos++).unicode();
-
444 else -
445 c = getChar_helper();
-
446 } -
447 -
448 return c;
-
449} -
450 -
451inline uint QXmlStreamReaderPrivate::peekChar() -
452{ -
453 uint c; -
454 if (putStack.size()) {
-
455 c = putStack.top(); -
456 } else if (readBufferPos < readBuffer.size()) {
-
457 c = readBuffer.at(readBufferPos).unicode(); -
458 } else {
-
459 if ((c = getChar_helper()))
-
460 --readBufferPos;
-
461 }
-
462 -
463 return c;
-
464} -
465bool QXmlStreamReaderPrivate::scanUntil(const char *str, short tokenToInject) -
466{ -
467 int pos = textBuffer.size(); -
468 int oldLineNumber = lineNumber; -
469 -
470 while (uint c = getChar()) {
-
471 -
472 switch (c) { -
473 case '\r': -
474 if ((c = filterCarriageReturn()) == 0)
-
475 break;
-
476 -
477 case '\n':
-
478 ++lineNumber; -
479 lastLineStart = characterOffset + readBufferPos; -
480 -
481 case '\t':
-
482 textBuffer += QChar(c); -
483 continue;
-
484 default: -
485 if (c < 0x20 || (c > 0xFFFD && c < 0x10000) || c > QChar::LastValidCodePoint ) {
-
486 raiseWellFormedError(QXmlStream::tr("Invalid XML character.")); -
487 lineNumber = oldLineNumber; -
488 return false;
-
489 } -
490 textBuffer += QChar(c); -
491 }
-
492 -
493 -
494 -
495 if (c == uint(*str)) {
-
496 if (!*(str + 1)) {
-
497 if (tokenToInject >= 0)
-
498 injectToken(tokenToInject);
-
499 return true;
-
500 } else { -
501 if (scanString(str + 1, tokenToInject, false))
-
502 return true;
-
503 }
-
504 } -
505 }
-
506 putString(textBuffer, pos); -
507 textBuffer.resize(pos); -
508 lineNumber = oldLineNumber; -
509 return false;
-
510} -
511 -
512bool QXmlStreamReaderPrivate::scanString(const char *str, short tokenToInject, bool requireSpace) -
513{ -
514 int n = 0; -
515 while (str[n]) {
-
516 ushort c = getChar(); -
517 if (c != ushort(str[n])) {
-
518 if (c)
-
519 putChar(c);
-
520 while (n--) {
-
521 putChar(ushort(str[n])); -
522 }
-
523 return false;
-
524 } -
525 ++n; -
526 }
-
527 for (int i = 0; i < n; ++i)
-
528 textBuffer += QChar(ushort(str[i]));
-
529 if (requireSpace) {
-
530 int s = fastScanSpace(); -
531 if (!s || atEnd) {
-
532 int pos = textBuffer.size() - n - s; -
533 putString(textBuffer, pos); -
534 textBuffer.resize(pos); -
535 return false;
-
536 } -
537 }
-
538 if (tokenToInject >= 0)
-
539 injectToken(tokenToInject);
-
540 return true;
-
541} -
542 -
543bool QXmlStreamReaderPrivate::scanAfterLangleBang() -
544{ -
545 switch (peekChar()) { -
546 case '[': -
547 return scanString(spell[CDATA_START], CDATA_START, false);
-
548 case 'D': -
549 return scanString(spell[DOCTYPE], DOCTYPE);
-
550 case 'A': -
551 return scanString(spell[ATTLIST], ATTLIST);
-
552 case 'N': -
553 return scanString(spell[NOTATION], NOTATION);
-
554 case 'E': -
555 if (scanString(spell[ELEMENT], ELEMENT))
-
556 return true;
-
557 return scanString(spell[ENTITY], ENTITY);
-
558 -
559 default: -
560 ; -
561 };
-
562 return false;
-
563} -
564 -
565bool QXmlStreamReaderPrivate::scanPublicOrSystem() -
566{ -
567 switch (peekChar()) { -
568 case 'S': -
569 return scanString(spell[SYSTEM], SYSTEM);
-
570 case 'P': -
571 return scanString(spell[PUBLIC], PUBLIC);
-
572 default: -
573 ; -
574 }
-
575 return false;
-
576} -
577 -
578bool QXmlStreamReaderPrivate::scanNData() -
579{ -
580 if (fastScanSpace()) {
-
581 if (scanString(spell[NDATA], NDATA))
-
582 return true;
-
583 putChar(' '); -
584 }
-
585 return false;
-
586} -
587 -
588bool QXmlStreamReaderPrivate::scanAfterDefaultDecl() -
589{ -
590 switch (peekChar()) { -
591 case 'R': -
592 return scanString(spell[REQUIRED], REQUIRED, false);
-
593 case 'I': -
594 return scanString(spell[IMPLIED], IMPLIED, false);
-
595 case 'F': -
596 return scanString(spell[FIXED], FIXED, false);
-
597 default: -
598 ; -
599 }
-
600 return false;
-
601} -
602 -
603bool QXmlStreamReaderPrivate::scanAttType() -
604{ -
605 switch (peekChar()) { -
606 case 'C': -
607 return scanString(spell[CDATA], CDATA);
-
608 case 'I': -
609 if (scanString(spell[ID], ID))
-
610 return true;
-
611 if (scanString(spell[IDREF], IDREF))
-
612 return true;
-
613 return scanString(spell[IDREFS], IDREFS);
-
614 case 'E': -
615 if (scanString(spell[ENTITY], ENTITY))
-
616 return true;
-
617 return scanString(spell[ENTITIES], ENTITIES);
-
618 case 'N': -
619 if (scanString(spell[NOTATION], NOTATION))
-
620 return true;
-
621 if (scanString(spell[NMTOKEN], NMTOKEN))
-
622 return true;
-
623 return scanString(spell[NMTOKENS], NMTOKENS);
-
624 default: -
625 ; -
626 }
-
627 return false;
-
628} -
629inline int QXmlStreamReaderPrivate::fastScanLiteralContent() -
630{ -
631 int n = 0; -
632 uint c; -
633 while ((c = getChar())) {
-
634 switch (ushort(c)) { -
635 case 0xfffe: -
636 case 0xffff: -
637 case 0: -
638 -
639 -
640 putChar(c); -
641 return n;
-
642 case '\r': -
643 if (filterCarriageReturn() == 0)
-
644 return n;
-
645 -
646 case '\n':
-
647 ++lineNumber; -
648 lastLineStart = characterOffset + readBufferPos; -
649 -
650 case ' ':
-
651 case '\t': -
652 if (normalizeLiterals)
-
653 textBuffer += QLatin1Char(' ');
-
654 else -
655 textBuffer += QChar(c);
-
656 ++n; -
657 break;
-
658 case '&': -
659 case '<': -
660 case '\"': -
661 case '\'': -
662 if (!(c & 0xff0000)) {
-
663 putChar(c); -
664 return n;
-
665 } -
666 -
667 default:
-
668 textBuffer += QChar(c); -
669 ++n; -
670 }
-
671 }
-
672 return n;
-
673} -
674 -
675inline int QXmlStreamReaderPrivate::fastScanSpace() -
676{ -
677 int n = 0; -
678 ushort c; -
679 while ((c = getChar())) {
-
680 switch (c) { -
681 case '\r': -
682 if ((c = filterCarriageReturn()) == 0)
-
683 return n;
-
684 -
685 case '\n':
-
686 ++lineNumber; -
687 lastLineStart = characterOffset + readBufferPos; -
688 -
689 case ' ':
-
690 case '\t': -
691 textBuffer += QChar(c); -
692 ++n; -
693 break;
-
694 default: -
695 putChar(c); -
696 return n;
-
697 } -
698 }
-
699 return n;
-
700} -
701 -
702 -
703 -
704 -
705 -
706 -
707 -
708inline int QXmlStreamReaderPrivate::fastScanContentCharList() -
709{ -
710 int n = 0; -
711 uint c; -
712 while ((c = getChar())) {
-
713 switch (ushort(c)) { -
714 case 0xfffe: -
715 case 0xffff: -
716 case 0: -
717 putChar(c); -
718 return n;
-
719 case ']': { -
720 isWhitespace = false; -
721 int pos = textBuffer.size(); -
722 textBuffer += QChar(ushort(c)); -
723 ++n; -
724 while ((c = getChar()) == ']') {
-
725 textBuffer += QChar(ushort(c)); -
726 ++n; -
727 }
-
728 if (c == 0) {
-
729 putString(textBuffer, pos); -
730 textBuffer.resize(pos); -
731 } else if (c == '>' && textBuffer.at(textBuffer.size()-2) == QLatin1Char(']')) {
-
732 raiseWellFormedError(QXmlStream::tr("Sequence ']]>' not allowed in content.")); -
733 } else {
-
734 putChar(c); -
735 break;
-
736 } -
737 return n;
-
738 } break;
dead code: break;
-
739 case '\r': -
740 if ((c = filterCarriageReturn()) == 0)
-
741 return n;
-
742 -
743 case '\n':
-
744 ++lineNumber; -
745 lastLineStart = characterOffset + readBufferPos; -
746 -
747 case ' ':
-
748 case '\t': -
749 textBuffer += QChar(ushort(c)); -
750 ++n; -
751 break;
-
752 case '&': -
753 case '<': -
754 if (!(c & 0xff0000)) {
-
755 putChar(c); -
756 return n;
-
757 } -
758 -
759 default:
-
760 if (c < 0x20) {
-
761 putChar(c); -
762 return n;
-
763 } -
764 isWhitespace = false; -
765 textBuffer += QChar(ushort(c)); -
766 ++n; -
767 }
-
768 }
-
769 return n;
-
770} -
771 -
772inline int QXmlStreamReaderPrivate::fastScanName(int *prefix) -
773{ -
774 int n = 0; -
775 ushort c; -
776 while ((c = getChar())) {
-
777 switch (c) { -
778 case '\n': -
779 case ' ': -
780 case '\t': -
781 case '\r': -
782 case '&': -
783 case '#': -
784 case '\'': -
785 case '\"': -
786 case '<': -
787 case '>': -
788 case '[': -
789 case ']': -
790 case '=': -
791 case '%': -
792 case '/': -
793 case ';': -
794 case '?': -
795 case '!': -
796 case '^': -
797 case '|': -
798 case ',': -
799 case '(': -
800 case ')': -
801 case '+': -
802 case '*': -
803 putChar(c); -
804 if (prefix && *prefix == n+1) {
-
805 *prefix = 0; -
806 putChar(':'); -
807 --n; -
808 }
-
809 return n;
-
810 case ':': -
811 if (prefix) {
-
812 if (*prefix == 0) {
-
813 *prefix = n+2; -
814 } else {
-
815 putChar(c); -
816 return n;
-
817 } -
818 } else { -
819 putChar(c); -
820 return n;
-
821 } -
822 -
823 default:
-
824 textBuffer += QChar(c); -
825 ++n; -
826 }
-
827 }
-
828 -
829 if (prefix)
-
830 *prefix = 0;
-
831 int pos = textBuffer.size() - n; -
832 putString(textBuffer, pos); -
833 textBuffer.resize(pos); -
834 return 0;
-
835} -
836 -
837enum NameChar { NameBeginning, NameNotBeginning, NotName }; -
838 -
839static const char Begi = static_cast<char>(NameBeginning); -
840static const char NtBg = static_cast<char>(NameNotBeginning); -
841static const char NotN = static_cast<char>(NotName); -
842 -
843static const char nameCharTable[128] = -
844{ -
845 -
846 NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, -
847 NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, -
848 -
849 NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, -
850 NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, -
851 -
852 NotN, NotN, NotN, NotN, NotN, NotN, NotN, NotN, -
853 NotN, NotN, NotN, NotN, NotN, NtBg, NtBg, NotN, -
854 -
855 NtBg, NtBg, NtBg, NtBg, NtBg, NtBg, NtBg, NtBg, -
856 NtBg, NtBg, Begi, NotN, NotN, NotN, NotN, NotN, -
857 -
858 NotN, Begi, Begi, Begi, Begi, Begi, Begi, Begi, -
859 Begi, Begi, Begi, Begi, Begi, Begi, Begi, Begi, -
860 -
861 Begi, Begi, Begi, Begi, Begi, Begi, Begi, Begi, -
862 Begi, Begi, Begi, NotN, NotN, NotN, NotN, Begi, -
863 -
864 NotN, Begi, Begi, Begi, Begi, Begi, Begi, Begi, -
865 Begi, Begi, Begi, Begi, Begi, Begi, Begi, Begi, -
866 -
867 Begi, Begi, Begi, Begi, Begi, Begi, Begi, Begi, -
868 Begi, Begi, Begi, NotN, NotN, NotN, NotN, NotN -
869}; -
870 -
871static inline NameChar fastDetermineNameChar(QChar ch) -
872{ -
873 ushort uc = ch.unicode(); -
874 if (!(uc & ~0x7f))
-
875 return static_cast<NameChar>(nameCharTable[uc]);
-
876 -
877 QChar::Category cat = ch.category(); -
878 -
879 if ((cat >= QChar::Letter_Uppercase && cat <= QChar::Letter_Other)
-
880 || cat == QChar::Number_Letter)
-
881 return NameBeginning;
-
882 if ((cat >= QChar::Number_DecimalDigit && cat <= QChar::Number_Other)
-
883 || (cat >= QChar::Mark_NonSpacing && cat <= QChar::Mark_Enclosing))
-
884 return NameNotBeginning;
-
885 return NotName;
-
886} -
887 -
888inline int QXmlStreamReaderPrivate::fastScanNMTOKEN() -
889{ -
890 int n = 0; -
891 uint c; -
892 while ((c = getChar())) {
-
893 if (fastDetermineNameChar(c) == NotName) {
-
894 putChar(c); -
895 return n;
-
896 } else { -
897 ++n; -
898 textBuffer += QChar(c); -
899 }
-
900 } -
901 -
902 int pos = textBuffer.size() - n; -
903 putString(textBuffer, pos); -
904 textBuffer.resize(pos); -
905 -
906 return n;
-
907} -
908 -
909void QXmlStreamReaderPrivate::putString(const QString &s, int from) -
910{ -
911 putStack.reserve(s.size()); -
912 for (int i = s.size()-1; i >= from; --i)
-
913 putStack.rawPush() = s.at(i).unicode();
-
914}
-
915 -
916void QXmlStreamReaderPrivate::putStringLiteral(const QString &s) -
917{ -
918 putStack.reserve(s.size()); -
919 for (int i = s.size()-1; i >= 0; --i)
-
920 putStack.rawPush() = ((LETTER << 16) | s.at(i).unicode());
-
921}
-
922 -
923void QXmlStreamReaderPrivate::putReplacement(const QString &s) -
924{ -
925 putStack.reserve(s.size()); -
926 for (int i = s.size()-1; i >= 0; --i) {
-
927 ushort c = s.at(i).unicode(); -
928 if (c == '\n' || c == '\r')
-
929 putStack.rawPush() = ((LETTER << 16) | c);
-
930 else -
931 putStack.rawPush() = c;
-
932 } -
933}
-
934void QXmlStreamReaderPrivate::putReplacementInAttributeValue(const QString &s) -
935{ -
936 putStack.reserve(s.size()); -
937 for (int i = s.size()-1; i >= 0; --i) {
-
938 ushort c = s.at(i).unicode(); -
939 if (c == '&' || c == ';')
-
940 putStack.rawPush() = c;
-
941 else if (c == '\n' || c == '\r')
-
942 putStack.rawPush() = ' ';
-
943 else -
944 putStack.rawPush() = ((LETTER << 16) | c);
-
945 } -
946}
-
947 -
948ushort QXmlStreamReaderPrivate::getChar_helper() -
949{ -
950 const int BUFFER_SIZE = 8192; -
951 characterOffset += readBufferPos; -
952 readBufferPos = 0; -
953 readBuffer.resize(0); -
954 -
955 if (decoder)
-
956 -
957 nbytesread = 0;
-
958 if (device) {
-
959 rawReadBuffer.resize(BUFFER_SIZE); -
960 int nbytesreadOrMinus1 = device->read(rawReadBuffer.data() + nbytesread, BUFFER_SIZE - nbytesread); -
961 nbytesread += qMax(nbytesreadOrMinus1, 0); -
962 } else {
-
963 if (nbytesread)
-
964 rawReadBuffer += dataBuffer;
-
965 else -
966 rawReadBuffer = dataBuffer;
-
967 nbytesread = rawReadBuffer.size(); -
968 dataBuffer.clear(); -
969 }
-
970 if (!nbytesread) {
-
971 atEnd = true; -
972 return 0;
-
973 } -
974 -
975 -
976 if (!decoder) {
-
977 if (nbytesread < 4) {
-
978 -
979 atEnd = true; -
980 return 0;
-
981 } -
982 int mib = 106; -
983 -
984 -
985 uchar ch1 = rawReadBuffer.at(0); -
986 uchar ch2 = rawReadBuffer.at(1); -
987 uchar ch3 = rawReadBuffer.at(2); -
988 uchar ch4 = rawReadBuffer.at(3); -
989 -
990 if ((ch1 == 0 && ch2 == 0 && ch3 == 0xfe && ch4 == 0xff) ||
-
991 (ch1 == 0xff && ch2 == 0xfe && ch3 == 0 && ch4 == 0))
-
992 mib = 1017;
-
993 else if (ch1 == 0x3c && ch2 == 0x00 && ch3 == 0x00 && ch4 == 0x00)
-
994 mib = 1019;
-
995 else if (ch1 == 0x00 && ch2 == 0x00 && ch3 == 0x00 && ch4 == 0x3c)
-
996 mib = 1018;
-
997 else if ((ch1 == 0xfe && ch2 == 0xff) || (ch1 == 0xff && ch2 == 0xfe))
-
998 mib = 1015;
-
999 else if (ch1 == 0x3c && ch2 == 0x00)
-
1000 mib = 1014;
-
1001 else if (ch1 == 0x00 && ch2 == 0x3c)
-
1002 mib = 1013;
-
1003 codec = QTextCodec::codecForMib(mib); -
1004 qt_noop(); -
1005 decoder = codec->makeDecoder(); -
1006 }
-
1007 -
1008 decoder->toUnicode(&readBuffer, rawReadBuffer.constData(), nbytesread); -
1009 -
1010 if(lockEncoding && decoder->hasFailure()) {
-
1011 raiseWellFormedError(QXmlStream::tr("Encountered incorrectly encoded content.")); -
1012 readBuffer.clear(); -
1013 return 0;
-
1014 } -
1015 -
1016 -
1017 -
1018 -
1019 readBuffer.reserve(1); -
1020 -
1021 if (readBufferPos < readBuffer.size()) {
-
1022 ushort c = readBuffer.at(readBufferPos++).unicode(); -
1023 return c;
-
1024 } -
1025 -
1026 atEnd = true; -
1027 return 0;
-
1028} -
1029 -
1030QStringRef QXmlStreamReaderPrivate::namespaceForPrefix(const QStringRef &prefix) -
1031{ -
1032 for (int j = namespaceDeclarations.size() - 1; j >= 0; --j) {
-
1033 const NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(j); -
1034 if (namespaceDeclaration.prefix == prefix) {
-
1035 return namespaceDeclaration.namespaceUri;
-
1036 } -
1037 }
-
1038 -
1039 -
1040 if (namespaceProcessing && !prefix.isEmpty())
-
1041 raiseWellFormedError(QXmlStream::tr("Namespace prefix '%1' not declared").arg(prefix.toString()));
-
1042 -
1043 -
1044 return QStringRef();
-
1045} -
1046 -
1047 -
1048 -
1049 -
1050void QXmlStreamReaderPrivate::resolveTag() -
1051{ -
1052 int n = attributeStack.size(); -
1053 -
1054 if (namespaceProcessing) {
-
1055 for (int a = 0; a < dtdAttributes.size(); ++a) {
-
1056 DtdAttribute &dtdAttribute = dtdAttributes[a]; -
1057 if (!dtdAttribute.isNamespaceAttribute
-
1058 || dtdAttribute.defaultValue.isNull()
-
1059 || dtdAttribute.tagName != qualifiedName
-
1060 || dtdAttribute.attributeQualifiedName.isNull())
-
1061 continue;
-
1062 int i = 0; -
1063 while (i < n && symName(attributeStack[i].key) != dtdAttribute.attributeQualifiedName)
-
1064 ++i;
-
1065 if (i != n)
-
1066 continue;
-
1067 if (dtdAttribute.attributePrefix.isEmpty() && dtdAttribute.attributeName == QLatin1String("xmlns")) {
-
1068 NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); -
1069 namespaceDeclaration.prefix.clear(); -
1070 -
1071 const QStringRef ns(dtdAttribute.defaultValue); -
1072 if(ns == QLatin1String("http://www.w3.org/2000/xmlns/") ||
-
1073 ns == QLatin1String("http://www.w3.org/XML/1998/namespace"))
-
1074 raiseWellFormedError(QXmlStream::tr("Illegal namespace declaration."));
-
1075 else -
1076 namespaceDeclaration.namespaceUri = ns;
-
1077 } else if (dtdAttribute.attributePrefix == QLatin1String("xmlns")) {
-
1078 NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); -
1079 QStringRef namespacePrefix = dtdAttribute.attributeName; -
1080 QStringRef namespaceUri = dtdAttribute.defaultValue; -
1081 if (((namespacePrefix == QLatin1String("xml")) -
1082 ^ (namespaceUri == QLatin1String("http://www.w3.org/XML/1998/namespace")))
-
1083 || namespaceUri == QLatin1String("http://www.w3.org/2000/xmlns/")
-
1084 || namespaceUri.isEmpty()
-
1085 || namespacePrefix == QLatin1String("xmlns"))
-
1086 raiseWellFormedError(QXmlStream::tr("Illegal namespace declaration."));
-
1087 -
1088 namespaceDeclaration.prefix = namespacePrefix; -
1089 namespaceDeclaration.namespaceUri = namespaceUri; -
1090 }
-
1091 } -
1092 }
-
1093 -
1094 tagStack.top().namespaceDeclaration.namespaceUri = namespaceUri = namespaceForPrefix(prefix); -
1095 -
1096 attributes.resize(n); -
1097 -
1098 for (int i = 0; i < n; ++i) {
-
1099 QXmlStreamAttribute &attribute = attributes[i]; -
1100 Attribute &attrib = attributeStack[i]; -
1101 QStringRef prefix(symPrefix(attrib.key)); -
1102 QStringRef name(symString(attrib.key)); -
1103 QStringRef qualifiedName(symName(attrib.key)); -
1104 QStringRef value(symString(attrib.value)); -
1105 -
1106 attribute.m_name = QXmlStreamStringRef(name); -
1107 attribute.m_qualifiedName = QXmlStreamStringRef(qualifiedName); -
1108 attribute.m_value = QXmlStreamStringRef(value); -
1109 -
1110 if (!prefix.isEmpty()) {
-
1111 QStringRef attributeNamespaceUri = namespaceForPrefix(prefix); -
1112 attribute.m_namespaceUri = QXmlStreamStringRef(attributeNamespaceUri); -
1113 }
-
1114 -
1115 for (int j = 0; j < i; ++j) {
-
1116 if (attributes[j].name() == attribute.name()
-
1117 && attributes[j].namespaceUri() == attribute.namespaceUri()
-
1118 && (namespaceProcessing || attributes[j].qualifiedName() == attribute.qualifiedName()))
-
1119 raiseWellFormedError(QXmlStream::tr("Attribute redefined."));
-
1120 }
-
1121 }
-
1122 -
1123 for (int a = 0; a < dtdAttributes.size(); ++a) {
-
1124 DtdAttribute &dtdAttribute = dtdAttributes[a]; -
1125 if (dtdAttribute.isNamespaceAttribute
-
1126 || dtdAttribute.defaultValue.isNull()
-
1127 || dtdAttribute.tagName != qualifiedName
-
1128 || dtdAttribute.attributeQualifiedName.isNull())
-
1129 continue;
-
1130 int i = 0; -
1131 while (i < n && symName(attributeStack[i].key) != dtdAttribute.attributeQualifiedName)
-
1132 ++i;
-
1133 if (i != n)
-
1134 continue;
-
1135 -
1136 -
1137 -
1138 QXmlStreamAttribute attribute; -
1139 attribute.m_name = QXmlStreamStringRef(dtdAttribute.attributeName); -
1140 attribute.m_qualifiedName = QXmlStreamStringRef(dtdAttribute.attributeQualifiedName); -
1141 attribute.m_value = QXmlStreamStringRef(dtdAttribute.defaultValue); -
1142 -
1143 if (!dtdAttribute.attributePrefix.isEmpty()) {
-
1144 QStringRef attributeNamespaceUri = namespaceForPrefix(dtdAttribute.attributePrefix); -
1145 attribute.m_namespaceUri = QXmlStreamStringRef(attributeNamespaceUri); -
1146 }
-
1147 attribute.m_isDefault = true; -
1148 attributes.append(attribute); -
1149 }
-
1150 -
1151 attributeStack.clear(); -
1152}
-
1153 -
1154void QXmlStreamReaderPrivate::resolvePublicNamespaces() -
1155{ -
1156 const Tag &tag = tagStack.top(); -
1157 int n = namespaceDeclarations.size() - tag.namespaceDeclarationsSize; -
1158 publicNamespaceDeclarations.resize(n); -
1159 for (int i = 0; i < n; ++i) {
-
1160 const NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(tag.namespaceDeclarationsSize + i); -
1161 QXmlStreamNamespaceDeclaration &publicNamespaceDeclaration = publicNamespaceDeclarations[i]; -
1162 publicNamespaceDeclaration.m_prefix = QXmlStreamStringRef(namespaceDeclaration.prefix); -
1163 publicNamespaceDeclaration.m_namespaceUri = QXmlStreamStringRef(namespaceDeclaration.namespaceUri); -
1164 }
-
1165}
-
1166 -
1167void QXmlStreamReaderPrivate::resolveDtd() -
1168{ -
1169 publicNotationDeclarations.resize(notationDeclarations.size()); -
1170 for (int i = 0; i < notationDeclarations.size(); ++i) {
-
1171 const QXmlStreamReaderPrivate::NotationDeclaration &notationDeclaration = notationDeclarations.at(i); -
1172 QXmlStreamNotationDeclaration &publicNotationDeclaration = publicNotationDeclarations[i]; -
1173 publicNotationDeclaration.m_name = QXmlStreamStringRef(notationDeclaration.name); -
1174 publicNotationDeclaration.m_systemId = QXmlStreamStringRef(notationDeclaration.systemId); -
1175 publicNotationDeclaration.m_publicId = QXmlStreamStringRef(notationDeclaration.publicId); -
1176 -
1177 }
-
1178 notationDeclarations.clear(); -
1179 publicEntityDeclarations.resize(entityDeclarations.size()); -
1180 for (int i = 0; i < entityDeclarations.size(); ++i) {
-
1181 const QXmlStreamReaderPrivate::EntityDeclaration &entityDeclaration = entityDeclarations.at(i); -
1182 QXmlStreamEntityDeclaration &publicEntityDeclaration = publicEntityDeclarations[i]; -
1183 publicEntityDeclaration.m_name = QXmlStreamStringRef(entityDeclaration.name); -
1184 publicEntityDeclaration.m_notationName = QXmlStreamStringRef(entityDeclaration.notationName); -
1185 publicEntityDeclaration.m_systemId = QXmlStreamStringRef(entityDeclaration.systemId); -
1186 publicEntityDeclaration.m_publicId = QXmlStreamStringRef(entityDeclaration.publicId); -
1187 publicEntityDeclaration.m_value = QXmlStreamStringRef(entityDeclaration.value); -
1188 }
-
1189 entityDeclarations.clear(); -
1190 parameterEntityHash.clear(); -
1191}
-
1192 -
1193uint QXmlStreamReaderPrivate::resolveCharRef(int symbolIndex) -
1194{ -
1195 bool ok = true; -
1196 uint s; -
1197 -
1198 if (sym(symbolIndex).c == 'x')
-
1199 s = symString(symbolIndex, 1).toString().toUInt(&ok, 16);
-
1200 else -
1201 s = symString(symbolIndex).toString().toUInt(&ok, 10);
-
1202 -
1203 ok &= (s == 0x9 || s == 0xa || s == 0xd || (s >= 0x20 && s <= 0xd7ff)
-
1204 || (s >= 0xe000 && s <= 0xfffd) || (s >= 0x10000 && s <= QChar::LastValidCodePoint));
-
1205 -
1206 return ok ? s : 0;
-
1207} -
1208 -
1209 -
1210void QXmlStreamReaderPrivate::checkPublicLiteral(const QStringRef &publicId) -
1211{ -
1212 -
1213 -
1214 const ushort *data = reinterpret_cast<const ushort *>(publicId.constData()); -
1215 uchar c = 0; -
1216 int i; -
1217 for (i = publicId.size() - 1; i >= 0; --i) {
-
1218 if (data[i] < 256)
-
1219 switch ((c = data[i])) { -
1220 case ' ': case '\n': case '\r': case '-': case '(': case ')': -
1221 case '+': case ',': case '.': case '/': case ':': case '=': -
1222 case '?': case ';': case '!': case '*': case '#': case '@': -
1223 case '$': case '_': case '%': case '\'': case '\"': -
1224 continue;
-
1225 default: -
1226 if ((c >= 'a' && c <= 'z')
-
1227 || (c >= 'A' && c <= 'Z')
-
1228 || (c >= '0' && c <= '9'))
-
1229 continue;
-
1230 }
-
1231 break;
-
1232 } -
1233 if (i >= 0)
-
1234 raiseWellFormedError(QXmlStream::tr("Unexpected character '%1' in public id literal.").arg(QChar(QLatin1Char(c))));
-
1235}
-
1236 -
1237 -
1238 -
1239 -
1240 -
1241 -
1242bool QXmlStreamReaderPrivate::checkStartDocument() -
1243{ -
1244 hasCheckedStartDocument = true; -
1245 -
1246 if (scanString(spell[XML], XML))
-
1247 return true;
-
1248 -
1249 type = QXmlStreamReader::StartDocument; -
1250 if (atEnd) {
-
1251 hasCheckedStartDocument = false; -
1252 raiseError(QXmlStreamReader::PrematureEndOfDocumentError); -
1253 }
-
1254 return false;
-
1255} -
1256 -
1257void QXmlStreamReaderPrivate::startDocument() -
1258{ -
1259 QString err; -
1260 if (documentVersion != QLatin1String("1.0")) {
-
1261 if (documentVersion.toString().contains(QLatin1Char(' ')))
-
1262 err = QXmlStream::tr("Invalid XML version string.");
-
1263 else -
1264 err = QXmlStream::tr("Unsupported XML version.");
-
1265 } -
1266 int n = attributeStack.size(); -
1267 -
1268 -
1269 -
1270 -
1271 -
1272 bool hasStandalone = false; -
1273 -
1274 for (int i = 0; err.isNull() && i < n; ++i) {
-
1275 Attribute &attrib = attributeStack[i]; -
1276 QStringRef prefix(symPrefix(attrib.key)); -
1277 QStringRef key(symString(attrib.key)); -
1278 QStringRef value(symString(attrib.value)); -
1279 -
1280 if (prefix.isEmpty() && key == QLatin1String("encoding")) {
-
1281 const QString name(value.toString()); -
1282 documentEncoding = value; -
1283 -
1284 if(hasStandalone)
-
1285 err = QXmlStream::tr("The standalone pseudo attribute must appear after the encoding.");
-
1286 if(!QXmlUtils::isEncName(name))
-
1287 err = QXmlStream::tr("%1 is an invalid encoding name.").arg(name);
-
1288 else { -
1289 -
1290 -
1291 -
1292 QTextCodec *const newCodec = QTextCodec::codecForName(name.toLatin1()); -
1293 if (!newCodec)
-
1294 err = QXmlStream::tr("Encoding %1 is unsupported").arg(name);
-
1295 else if (newCodec != codec && !lockEncoding) {
-
1296 codec = newCodec; -
1297 delete decoder; -
1298 decoder = codec->makeDecoder(); -
1299 decoder->toUnicode(&readBuffer, rawReadBuffer.data(), nbytesread); -
1300 }
-
1301 -
1302 } -
1303 } else if (prefix.isEmpty() && key == QLatin1String("standalone")) {
-
1304 hasStandalone = true; -
1305 if (value == QLatin1String("yes"))
-
1306 standalone = true;
-
1307 else if (value == QLatin1String("no"))
-
1308 standalone = false;
-
1309 else -
1310 err = QXmlStream::tr("Standalone accepts only yes or no.");
-
1311 } else { -
1312 err = QXmlStream::tr("Invalid attribute in XML declaration."); -
1313 }
-
1314 } -
1315 -
1316 if (!err.isNull())
-
1317 raiseWellFormedError(err);
-
1318 attributeStack.clear(); -
1319}
-
1320 -
1321 -
1322void QXmlStreamReaderPrivate::raiseError(QXmlStreamReader::Error error, const QString& message) -
1323{ -
1324 this->error = error; -
1325 errorString = message; -
1326 if (errorString.isNull()) {
-
1327 if (error == QXmlStreamReader::PrematureEndOfDocumentError)
-
1328 errorString = QXmlStream::tr("Premature end of document.");
-
1329 else if (error == QXmlStreamReader::CustomError)
-
1330 errorString = QXmlStream::tr("Invalid document.");
-
1331 } -
1332 -
1333 type = QXmlStreamReader::Invalid; -
1334}
-
1335 -
1336void QXmlStreamReaderPrivate::raiseWellFormedError(const QString &message) -
1337{ -
1338 raiseError(QXmlStreamReader::NotWellFormedError, message); -
1339}
-
1340 -
1341void QXmlStreamReaderPrivate::parseError() -
1342{ -
1343 -
1344 if (token == EOF_SYMBOL) {
-
1345 raiseError(QXmlStreamReader::PrematureEndOfDocumentError); -
1346 return;
-
1347 } -
1348 const int nmax = 4; -
1349 QString error_message; -
1350 int ers = state_stack[tos]; -
1351 int nexpected = 0; -
1352 int expected[nmax]; -
1353 if (token != ERROR)
-
1354 for (int tk = 0; tk < TERMINAL_COUNT; ++tk) {
-
1355 int k = t_action(ers, tk); -
1356 if (k <= 0)
-
1357 continue;
-
1358 if (spell[tk]) {
-
1359 if (nexpected < nmax)
-
1360 expected[nexpected++] = tk;
-
1361 }
-
1362 }
-
1363 -
1364 error_message.clear (); -
1365 if (nexpected && nexpected < nmax) {
-
1366 bool first = true; -
1367 -
1368 for (int s = 0; s < nexpected; ++s) {
-
1369 if (first)
-
1370 error_message += QXmlStream::tr ("Expected ");
-
1371 else if (s == nexpected - 1)
-
1372 error_message += QLatin1String (nexpected > 2 ? ", or " : " or ");
-
1373 else -
1374 error_message += QLatin1String (", ");
-
1375 -
1376 first = false; -
1377 error_message += QLatin1String("\'"); -
1378 error_message += QLatin1String (spell [expected[s]]); -
1379 error_message += QLatin1String("\'"); -
1380 }
-
1381 error_message += QXmlStream::tr(", but got \'"); -
1382 error_message += QLatin1String(spell [token]); -
1383 error_message += QLatin1String("\'"); -
1384 } else {
-
1385 error_message += QXmlStream::tr("Unexpected \'"); -
1386 error_message += QLatin1String(spell [token]); -
1387 error_message += QLatin1String("\'"); -
1388 }
-
1389 error_message += QLatin1Char('.'); -
1390 -
1391 raiseWellFormedError(error_message); -
1392}
-
1393 -
1394void QXmlStreamReaderPrivate::resume(int rule) { -
1395 resumeReduction = rule; -
1396 if (error == QXmlStreamReader::NoError)
-
1397 raiseError(QXmlStreamReader::PrematureEndOfDocumentError);
-
1398}
-
1399 -
1400 -
1401 -
1402 -
1403 -
1404qint64 QXmlStreamReader::lineNumber() const -
1405{ -
1406 const QXmlStreamReaderPrivate * const d = d_func(); -
1407 return d->lineNumber + 1;
-
1408} -
1409 -
1410 -
1411 -
1412 -
1413 -
1414qint64 QXmlStreamReader::columnNumber() const -
1415{ -
1416 const QXmlStreamReaderPrivate * const d = d_func(); -
1417 return d->characterOffset - d->lastLineStart + d->readBufferPos;
-
1418} -
1419 -
1420 -
1421 -
1422 -
1423 -
1424qint64 QXmlStreamReader::characterOffset() const -
1425{ -
1426 const QXmlStreamReaderPrivate * const d = d_func(); -
1427 return d->characterOffset + d->readBufferPos;
-
1428} -
1429 -
1430 -
1431 -
1432 -
1433 -
1434QStringRef QXmlStreamReader::text() const -
1435{ -
1436 const QXmlStreamReaderPrivate * const d = d_func(); -
1437 return d->text;
-
1438} -
1439QXmlStreamNotationDeclarations QXmlStreamReader::notationDeclarations() const -
1440{ -
1441 const QXmlStreamReaderPrivate * const d = d_func(); -
1442 if (d->notationDeclarations.size())
-
1443 const_cast<QXmlStreamReaderPrivate *>(d)->resolveDtd();
-
1444 return d->publicNotationDeclarations;
-
1445} -
1446QXmlStreamEntityDeclarations QXmlStreamReader::entityDeclarations() const -
1447{ -
1448 const QXmlStreamReaderPrivate * const d = d_func(); -
1449 if (d->entityDeclarations.size())
-
1450 const_cast<QXmlStreamReaderPrivate *>(d)->resolveDtd();
-
1451 return d->publicEntityDeclarations;
-
1452} -
1453QStringRef QXmlStreamReader::dtdName() const -
1454{ -
1455 const QXmlStreamReaderPrivate * const d = d_func(); -
1456 if (d->type == QXmlStreamReader::DTD)
-
1457 return d->dtdName;
-
1458 return QStringRef();
-
1459} -
1460QStringRef QXmlStreamReader::dtdPublicId() const -
1461{ -
1462 const QXmlStreamReaderPrivate * const d = d_func(); -
1463 if (d->type == QXmlStreamReader::DTD)
-
1464 return d->dtdPublicId;
-
1465 return QStringRef();
-
1466} -
1467QStringRef QXmlStreamReader::dtdSystemId() const -
1468{ -
1469 const QXmlStreamReaderPrivate * const d = d_func(); -
1470 if (d->type == QXmlStreamReader::DTD)
-
1471 return d->dtdSystemId;
-
1472 return QStringRef();
-
1473} -
1474QXmlStreamNamespaceDeclarations QXmlStreamReader::namespaceDeclarations() const -
1475{ -
1476 const QXmlStreamReaderPrivate * const d = d_func(); -
1477 if (d->publicNamespaceDeclarations.isEmpty() && d->type == StartElement)
-
1478 const_cast<QXmlStreamReaderPrivate *>(d)->resolvePublicNamespaces();
-
1479 return d->publicNamespaceDeclarations;
-
1480} -
1481void QXmlStreamReader::addExtraNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &extraNamespaceDeclaration) -
1482{ -
1483 QXmlStreamReaderPrivate * const d = d_func(); -
1484 QXmlStreamReaderPrivate::NamespaceDeclaration &namespaceDeclaration = d->namespaceDeclarations.push(); -
1485 namespaceDeclaration.prefix = d->addToStringStorage(extraNamespaceDeclaration.prefix()); -
1486 namespaceDeclaration.namespaceUri = d->addToStringStorage(extraNamespaceDeclaration.namespaceUri()); -
1487}
-
1488void QXmlStreamReader::addExtraNamespaceDeclarations(const QXmlStreamNamespaceDeclarations &extraNamespaceDeclarations) -
1489{ -
1490 for (int i = 0; i < extraNamespaceDeclarations.size(); ++i)
-
1491 addExtraNamespaceDeclaration(extraNamespaceDeclarations.at(i));
-
1492}
-
1493QString QXmlStreamReader::readElementText(ReadElementTextBehaviour behaviour) -
1494{ -
1495 QXmlStreamReaderPrivate * const d = d_func(); -
1496 if (isStartElement()) {
-
1497 QString result; -
1498 for(;;) { -
1499 switch (readNext()) { -
1500 case Characters: -
1501 case EntityReference: -
1502 result.insert(result.size(), d->text.unicode(), d->text.size()); -
1503 break;
-
1504 case EndElement: -
1505 return result;
-
1506 case ProcessingInstruction: -
1507 case Comment: -
1508 break;
-
1509 case StartElement: -
1510 if (behaviour == SkipChildElements) {
-
1511 skipCurrentElement(); -
1512 break;
-
1513 } else if (behaviour == IncludeChildElements) {
-
1514 result += readElementText(behaviour); -
1515 break;
-
1516 } -
1517 -
1518 default:
-
1519 if (d->error || behaviour == ErrorOnUnexpectedElement) {
-
1520 if (!d->error)
-
1521 d->raiseError(UnexpectedElementError, QXmlStream::tr("Expected character data."));
-
1522 return result;
-
1523 } -
1524 }
-
1525 }
-
1526 }
-
1527 return QString();
-
1528} -
1529 -
1530 -
1531 -
1532 -
1533 -
1534void QXmlStreamReader::raiseError(const QString& message) -
1535{ -
1536 QXmlStreamReaderPrivate * const d = d_func(); -
1537 d->raiseError(CustomError, message); -
1538}
-
1539 -
1540 -
1541 -
1542 -
1543 -
1544 -
1545QString QXmlStreamReader::errorString() const -
1546{ -
1547 const QXmlStreamReaderPrivate * const d = d_func(); -
1548 if (d->type == QXmlStreamReader::Invalid)
-
1549 return d->errorString;
-
1550 return QString();
-
1551} -
1552 -
1553 -
1554 -
1555 -
1556 -
1557QXmlStreamReader::Error QXmlStreamReader::error() const -
1558{ -
1559 const QXmlStreamReaderPrivate * const d = d_func(); -
1560 if (d->type == QXmlStreamReader::Invalid)
-
1561 return d->error;
-
1562 return NoError;
-
1563} -
1564 -
1565 -
1566 -
1567 -
1568QStringRef QXmlStreamReader::processingInstructionTarget() const -
1569{ -
1570 const QXmlStreamReaderPrivate * const d = d_func(); -
1571 return d->processingInstructionTarget;
-
1572} -
1573 -
1574 -
1575 -
1576 -
1577QStringRef QXmlStreamReader::processingInstructionData() const -
1578{ -
1579 const QXmlStreamReaderPrivate * const d = d_func(); -
1580 return d->processingInstructionData;
-
1581} -
1582QStringRef QXmlStreamReader::name() const -
1583{ -
1584 const QXmlStreamReaderPrivate * const d = d_func(); -
1585 return d->name;
-
1586} -
1587 -
1588 -
1589 -
1590 -
1591 -
1592 -
1593QStringRef QXmlStreamReader::namespaceUri() const -
1594{ -
1595 const QXmlStreamReaderPrivate * const d = d_func(); -
1596 return d->namespaceUri;
-
1597} -
1598QStringRef QXmlStreamReader::qualifiedName() const -
1599{ -
1600 const QXmlStreamReaderPrivate * const d = d_func(); -
1601 return d->qualifiedName;
-
1602} -
1603QStringRef QXmlStreamReader::prefix() const -
1604{ -
1605 const QXmlStreamReaderPrivate * const d = d_func(); -
1606 return d->prefix;
-
1607} -
1608 -
1609 -
1610 -
1611 -
1612QXmlStreamAttributes QXmlStreamReader::attributes() const -
1613{ -
1614 const QXmlStreamReaderPrivate * const d = d_func(); -
1615 return d->attributes;
-
1616} -
1617QXmlStreamAttribute::QXmlStreamAttribute() -
1618{ -
1619 m_isDefault = false; -
1620}
-
1621 -
1622 -
1623 -
1624 -
1625QXmlStreamAttribute::~QXmlStreamAttribute() -
1626{ -
1627} -
1628 -
1629 -
1630 -
1631 -
1632QXmlStreamAttribute::QXmlStreamAttribute(const QString &namespaceUri, const QString &name, const QString &value) -
1633{ -
1634 m_namespaceUri = QXmlStreamStringRef(QStringRef(&namespaceUri)); -
1635 m_name = m_qualifiedName = QXmlStreamStringRef(QStringRef(&name)); -
1636 m_value = QXmlStreamStringRef(QStringRef(&value)); -
1637 m_namespaceUri = QXmlStreamStringRef(QStringRef(&namespaceUri)); -
1638}
-
1639 -
1640 -
1641 -
1642 -
1643QXmlStreamAttribute::QXmlStreamAttribute(const QString &qualifiedName, const QString &value) -
1644{ -
1645 int colon = qualifiedName.indexOf(QLatin1Char(':')); -
1646 m_name = QXmlStreamStringRef(QStringRef(&qualifiedName, -
1647 colon + 1, -
1648 qualifiedName.size() - (colon + 1))); -
1649 m_qualifiedName = QXmlStreamStringRef(QStringRef(&qualifiedName)); -
1650 m_value = QXmlStreamStringRef(QStringRef(&value)); -
1651}
-
1652QXmlStreamAttribute::QXmlStreamAttribute(const QXmlStreamAttribute &other) -
1653{ -
1654 *this = other; -
1655}
-
1656 -
1657 -
1658 -
1659 -
1660QXmlStreamAttribute& QXmlStreamAttribute::operator=(const QXmlStreamAttribute &other) -
1661{ -
1662 m_name = other.m_name; -
1663 m_namespaceUri = other.m_namespaceUri; -
1664 m_qualifiedName = other.m_qualifiedName; -
1665 m_value = other.m_value; -
1666 m_isDefault = other.m_isDefault; -
1667 return *this;
-
1668} -
1669QXmlStreamNotationDeclaration::QXmlStreamNotationDeclaration() -
1670{ -
1671} -
1672 -
1673 -
1674 -
1675QXmlStreamNotationDeclaration::QXmlStreamNotationDeclaration(const QXmlStreamNotationDeclaration &other) -
1676{ -
1677 *this = other; -
1678}
-
1679 -
1680 -
1681 -
1682 -
1683QXmlStreamNotationDeclaration& QXmlStreamNotationDeclaration::operator=(const QXmlStreamNotationDeclaration &other) -
1684{ -
1685 m_name = other.m_name; -
1686 m_systemId = other.m_systemId; -
1687 m_publicId = other.m_publicId; -
1688 return *this;
-
1689} -
1690 -
1691 -
1692 -
1693 -
1694QXmlStreamNotationDeclaration::~QXmlStreamNotationDeclaration() -
1695{ -
1696} -
1697QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration() -
1698{ -
1699} -
1700 -
1701 -
1702 -
1703 -
1704 -
1705 -
1706QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri) -
1707{ -
1708 m_prefix = prefix; -
1709 m_namespaceUri = namespaceUri; -
1710}
-
1711 -
1712 -
1713 -
1714 -
1715QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &other) -
1716{ -
1717 *this = other; -
1718}
-
1719 -
1720 -
1721 -
1722 -
1723QXmlStreamNamespaceDeclaration& QXmlStreamNamespaceDeclaration::operator=(const QXmlStreamNamespaceDeclaration &other) -
1724{ -
1725 m_prefix = other.m_prefix; -
1726 m_namespaceUri = other.m_namespaceUri; -
1727 return *this;
-
1728} -
1729 -
1730 -
1731 -
1732QXmlStreamNamespaceDeclaration::~QXmlStreamNamespaceDeclaration() -
1733{ -
1734} -
1735QXmlStreamEntityDeclaration::QXmlStreamEntityDeclaration() -
1736{ -
1737} -
1738 -
1739 -
1740 -
1741 -
1742QXmlStreamEntityDeclaration::QXmlStreamEntityDeclaration(const QXmlStreamEntityDeclaration &other) -
1743{ -
1744 *this = other; -
1745}
-
1746 -
1747 -
1748 -
1749 -
1750QXmlStreamEntityDeclaration& QXmlStreamEntityDeclaration::operator=(const QXmlStreamEntityDeclaration &other) -
1751{ -
1752 m_name = other.m_name; -
1753 m_notationName = other.m_notationName; -
1754 m_systemId = other.m_systemId; -
1755 m_publicId = other.m_publicId; -
1756 m_value = other.m_value; -
1757 return *this;
-
1758} -
1759 -
1760 -
1761 -
1762 -
1763QXmlStreamEntityDeclaration::~QXmlStreamEntityDeclaration() -
1764{ -
1765} -
1766QStringRef QXmlStreamAttributes::value(const QString &namespaceUri, const QString &name) const -
1767{ -
1768 for (int i = 0; i < size(); ++i) {
-
1769 const QXmlStreamAttribute &attribute = at(i); -
1770 if (attribute.name() == name && attribute.namespaceUri() == namespaceUri)
-
1771 return attribute.value();
-
1772 }
-
1773 return QStringRef();
-
1774} -
1775 -
1776 -
1777 -
1778 -
1779 -
1780 -
1781QStringRef QXmlStreamAttributes::value(const QString &namespaceUri, QLatin1String name) const -
1782{ -
1783 for (int i = 0; i < size(); ++i) {
-
1784 const QXmlStreamAttribute &attribute = at(i); -
1785 if (attribute.name() == name && attribute.namespaceUri() == namespaceUri)
-
1786 return attribute.value();
-
1787 }
-
1788 return QStringRef();
-
1789} -
1790 -
1791 -
1792 -
1793 -
1794 -
1795 -
1796QStringRef QXmlStreamAttributes::value(QLatin1String namespaceUri, QLatin1String name) const -
1797{ -
1798 for (int i = 0; i < size(); ++i) {
-
1799 const QXmlStreamAttribute &attribute = at(i); -
1800 if (attribute.name() == name && attribute.namespaceUri() == namespaceUri)
-
1801 return attribute.value();
-
1802 }
-
1803 return QStringRef();
-
1804} -
1805QStringRef QXmlStreamAttributes::value(const QString &qualifiedName) const -
1806{ -
1807 for (int i = 0; i < size(); ++i) {
-
1808 const QXmlStreamAttribute &attribute = at(i); -
1809 if (attribute.qualifiedName() == qualifiedName)
-
1810 return attribute.value();
-
1811 }
-
1812 return QStringRef();
-
1813} -
1814QStringRef QXmlStreamAttributes::value(QLatin1String qualifiedName) const -
1815{ -
1816 for (int i = 0; i < size(); ++i) {
-
1817 const QXmlStreamAttribute &attribute = at(i); -
1818 if (attribute.qualifiedName() == qualifiedName)
-
1819 return attribute.value();
-
1820 }
-
1821 return QStringRef();
-
1822} -
1823 -
1824 -
1825 -
1826 -
1827 -
1828void QXmlStreamAttributes::append(const QString &namespaceUri, const QString &name, const QString &value) -
1829{ -
1830 append(QXmlStreamAttribute(namespaceUri, name, value)); -
1831}
-
1832 -
1833 -
1834 -
1835 -
1836 -
1837void QXmlStreamAttributes::append(const QString &qualifiedName, const QString &value) -
1838{ -
1839 append(QXmlStreamAttribute(qualifiedName, value)); -
1840}
-
1841bool QXmlStreamReader::isWhitespace() const -
1842{ -
1843 const QXmlStreamReaderPrivate * const d = d_func(); -
1844 return d->type == QXmlStreamReader::Characters && d->isWhitespace;
-
1845} -
1846 -
1847 -
1848 -
1849 -
1850 -
1851 -
1852bool QXmlStreamReader::isCDATA() const -
1853{ -
1854 const QXmlStreamReaderPrivate * const d = d_func(); -
1855 return d->type == QXmlStreamReader::Characters && d->isCDATA;
-
1856} -
1857bool QXmlStreamReader::isStandaloneDocument() const -
1858{ -
1859 const QXmlStreamReaderPrivate * const d = d_func(); -
1860 return d->standalone;
-
1861} -
1862QStringRef QXmlStreamReader::documentVersion() const -
1863{ -
1864 const QXmlStreamReaderPrivate * const d = d_func(); -
1865 if (d->type == QXmlStreamReader::StartDocument)
-
1866 return d->documentVersion;
-
1867 return QStringRef();
-
1868} -
1869QStringRef QXmlStreamReader::documentEncoding() const -
1870{ -
1871 const QXmlStreamReaderPrivate * const d = d_func(); -
1872 if (d->type == QXmlStreamReader::StartDocument)
-
1873 return d->documentEncoding;
-
1874 return QStringRef();
-
1875} -
1876class QXmlStreamWriterPrivate : public QXmlStreamPrivateTagStack { -
1877 QXmlStreamWriter *q_ptr; -
1878 inline QXmlStreamWriter* q_func() { return static_cast<QXmlStreamWriter *>(q_ptr); } inline const QXmlStreamWriter* q_func() const { return static_cast<const QXmlStreamWriter *>(q_ptr); } friend class QXmlStreamWriter; -
1879public: -
1880 QXmlStreamWriterPrivate(QXmlStreamWriter *q); -
1881 ~QXmlStreamWriterPrivate() { -
1882 if (deleteDevice)
-
1883 delete device;
-
1884 -
1885 delete encoder; -
1886 -
1887 }
-
1888 -
1889 void write(const QStringRef &); -
1890 void write(const QString &); -
1891 void writeEscaped(const QString &, bool escapeWhitespace = false); -
1892 void write(const char *s, int len); -
1893 template <int N> void write(const char (&s)[N]) { write(s, N - 1); }
-
1894 bool finishStartElement(bool contents = true); -
1895 void writeStartElement(const QString &namespaceUri, const QString &name); -
1896 QIODevice *device; -
1897 QString *stringDevice; -
1898 uint deleteDevice :1; -
1899 uint inStartElement :1; -
1900 uint inEmptyElement :1; -
1901 uint lastWasStartElement :1; -
1902 uint wroteSomething :1; -
1903 uint hasError :1; -
1904 uint autoFormatting :1; -
1905 uint isCodecASCIICompatible :1; -
1906 QByteArray autoFormattingIndent; -
1907 NamespaceDeclaration emptyNamespace; -
1908 int lastNamespaceDeclaration; -
1909 -
1910 -
1911 QTextCodec *codec; -
1912 QTextEncoder *encoder; -
1913 -
1914 void checkIfASCIICompatibleCodec(); -
1915 -
1916 NamespaceDeclaration &findNamespace(const QString &namespaceUri, bool writeDeclaration = false, bool noDefault = false); -
1917 void writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration); -
1918 -
1919 int namespacePrefixCount; -
1920 -
1921 void indent(int level); -
1922}; -
1923 -
1924 -
1925QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q) -
1926 :autoFormattingIndent(4, ' ') -
1927{ -
1928 q_ptr = q; -
1929 device = 0; -
1930 stringDevice = 0; -
1931 deleteDevice = false; -
1932 -
1933 codec = QTextCodec::codecForMib(106); -
1934 encoder = codec->makeEncoder(QTextCodec::IgnoreHeader); -
1935 -
1936 checkIfASCIICompatibleCodec(); -
1937 inStartElement = inEmptyElement = false; -
1938 wroteSomething = false; -
1939 hasError = false; -
1940 lastWasStartElement = false; -
1941 lastNamespaceDeclaration = 1; -
1942 autoFormatting = false; -
1943 namespacePrefixCount = 0; -
1944}
-
1945 -
1946void QXmlStreamWriterPrivate::checkIfASCIICompatibleCodec() -
1947{ -
1948 -
1949 qt_noop(); -
1950 -
1951 const QByteArray bytes = encoder->fromUnicode(QString::fromUtf8("" " " "", sizeof(" ") - 1)); -
1952 isCodecASCIICompatible = (bytes.count() == 1); -
1953 -
1954 -
1955 -
1956}
-
1957 -
1958void QXmlStreamWriterPrivate::write(const QStringRef &s) -
1959{ -
1960 if (device) {
-
1961 if (hasError)
-
1962 return;
-
1963 -
1964 -
1965 -
1966 QByteArray bytes = encoder->fromUnicode(s.constData(), s.size()); -
1967 -
1968 if (device->write(bytes) != bytes.size())
-
1969 hasError = true;
-
1970 }
-
1971 else if (stringDevice)
-
1972 s.appendTo(stringDevice);
-
1973 else -
1974 QMessageLogger("xml/qxmlstream.cpp", 3052, __PRETTY_FUNCTION__).warning("QXmlStreamWriter: No device");
-
1975} -
1976 -
1977void QXmlStreamWriterPrivate::write(const QString &s) -
1978{ -
1979 if (device) {
-
1980 if (hasError)
-
1981 return;
-
1982 -
1983 -
1984 -
1985 QByteArray bytes = encoder->fromUnicode(s); -
1986 -
1987 if (device->write(bytes) != bytes.size())
-
1988 hasError = true;
-
1989 }
-
1990 else if (stringDevice)
-
1991 stringDevice->append(s);
-
1992 else -
1993 QMessageLogger("xml/qxmlstream.cpp", 3071, __PRETTY_FUNCTION__).warning("QXmlStreamWriter: No device");
-
1994} -
1995 -
1996void QXmlStreamWriterPrivate::writeEscaped(const QString &s, bool escapeWhitespace) -
1997{ -
1998 QString escaped; -
1999 escaped.reserve(s.size()); -
2000 for ( int i = 0; i < s.size(); ++i ) {
-
2001 QChar c = s.at(i); -
2002 if (c.unicode() == '<' )
-
2003 escaped.append(QLatin1String("&lt;"));
-
2004 else if (c.unicode() == '>' )
-
2005 escaped.append(QLatin1String("&gt;"));
-
2006 else if (c.unicode() == '&' )
-
2007 escaped.append(QLatin1String("&amp;"));
-
2008 else if (c.unicode() == '\"' )
-
2009 escaped.append(QLatin1String("&quot;"));
-
2010 else if (escapeWhitespace && c.isSpace()) {
-
2011 if (c.unicode() == '\n')
-
2012 escaped.append(QLatin1String("&#10;"));
-
2013 else if (c.unicode() == '\r')
-
2014 escaped.append(QLatin1String("&#13;"));
-
2015 else if (c.unicode() == '\t')
-
2016 escaped.append(QLatin1String("&#9;"));
-
2017 else -
2018 escaped += c;
-
2019 } else { -
2020 escaped += QChar(c); -
2021 }
-
2022 } -
2023 write(escaped); -
2024}
-
2025 -
2026 -
2027void QXmlStreamWriterPrivate::write(const char *s, int len) -
2028{ -
2029 if (device) {
-
2030 if (hasError)
-
2031 return;
-
2032 if (isCodecASCIICompatible) {
-
2033 if (device->write(s, len) != len)
-
2034 hasError = true;
-
2035 return;
-
2036 } -
2037 }
-
2038 -
2039 write(QString::fromLatin1(s, len)); -
2040}
-
2041 -
2042void QXmlStreamWriterPrivate::writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration) { -
2043 if (namespaceDeclaration.prefix.isEmpty()) {
-
2044 write(" xmlns=\""); -
2045 write(namespaceDeclaration.namespaceUri); -
2046 write("\""); -
2047 } else {
-
2048 write(" xmlns:"); -
2049 write(namespaceDeclaration.prefix); -
2050 write("=\""); -
2051 write(namespaceDeclaration.namespaceUri); -
2052 write("\""); -
2053 }
-
2054} -
2055 -
2056bool QXmlStreamWriterPrivate::finishStartElement(bool contents) -
2057{ -
2058 bool hadSomethingWritten = wroteSomething; -
2059 wroteSomething = contents; -
2060 if (!inStartElement)
-
2061 return hadSomethingWritten;
-
2062 -
2063 if (inEmptyElement) {
-
2064 write("/>"); -
2065 QXmlStreamWriterPrivate::Tag &tag = tagStack_pop(); -
2066 lastNamespaceDeclaration = tag.namespaceDeclarationsSize; -
2067 lastWasStartElement = false; -
2068 } else {
-
2069 write(">"); -
2070 }
-
2071 inStartElement = inEmptyElement = false; -
2072 lastNamespaceDeclaration = namespaceDeclarations.size(); -
2073 return hadSomethingWritten;
-
2074} -
2075 -
2076QXmlStreamPrivateTagStack::NamespaceDeclaration &QXmlStreamWriterPrivate::findNamespace(const QString &namespaceUri, bool writeDeclaration, bool noDefault) -
2077{ -
2078 for (int j = namespaceDeclarations.size() - 1; j >= 0; --j) {
-
2079 NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations[j]; -
2080 if (namespaceDeclaration.namespaceUri == namespaceUri) {
-
2081 if (!noDefault || !namespaceDeclaration.prefix.isEmpty())
-
2082 return namespaceDeclaration;
-
2083 }
-
2084 }
-
2085 if (namespaceUri.isEmpty())
-
2086 return emptyNamespace;
-
2087 NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push(); -
2088 if (namespaceUri.isEmpty()) {
-
2089 namespaceDeclaration.prefix.clear(); -
2090 } else {
-
2091 QString s; -
2092 int n = ++namespacePrefixCount; -
2093 for(;;) { -
2094 s = QLatin1Char('n') + QString::number(n++); -
2095 int j = namespaceDeclarations.size() - 2; -
2096 while (j >= 0 && namespaceDeclarations.at(j).prefix != s)
-
2097 --j;
-
2098 if (j < 0)
-
2099 break;
-
2100 }
-
2101 namespaceDeclaration.prefix = addToStringStorage(s); -
2102 }
-
2103 namespaceDeclaration.namespaceUri = addToStringStorage(namespaceUri); -
2104 if (writeDeclaration)
-
2105 writeNamespaceDeclaration(namespaceDeclaration);
-
2106 return namespaceDeclaration;
-
2107} -
2108 -
2109 -
2110 -
2111void QXmlStreamWriterPrivate::indent(int level) -
2112{ -
2113 write("\n"); -
2114 for (int i = level; i > 0; --i)
-
2115 write(autoFormattingIndent.constData(), autoFormattingIndent.length());
-
2116}
-
2117 -
2118 -
2119 -
2120 -
2121 -
2122 -
2123 -
2124QXmlStreamWriter::QXmlStreamWriter() -
2125 : d_ptr(new QXmlStreamWriterPrivate(this)) -
2126{ -
2127}
-
2128 -
2129 -
2130 -
2131 -
2132QXmlStreamWriter::QXmlStreamWriter(QIODevice *device) -
2133 : d_ptr(new QXmlStreamWriterPrivate(this)) -
2134{ -
2135 QXmlStreamWriterPrivate * const d = d_func(); -
2136 d->device = device; -
2137}
-
2138 -
2139 -
2140 -
2141 -
2142 -
2143QXmlStreamWriter::QXmlStreamWriter(QByteArray *array) -
2144 : d_ptr(new QXmlStreamWriterPrivate(this)) -
2145{ -
2146 QXmlStreamWriterPrivate * const d = d_func(); -
2147 d->device = new QBuffer(array); -
2148 d->device->open(QIODevice::WriteOnly); -
2149 d->deleteDevice = true; -
2150}
-
2151 -
2152 -
2153 -
2154 -
2155QXmlStreamWriter::QXmlStreamWriter(QString *string) -
2156 : d_ptr(new QXmlStreamWriterPrivate(this)) -
2157{ -
2158 QXmlStreamWriterPrivate * const d = d_func(); -
2159 d->stringDevice = string; -
2160}
-
2161 -
2162 -
2163 -
2164 -
2165QXmlStreamWriter::~QXmlStreamWriter() -
2166{ -
2167} -
2168void QXmlStreamWriter::setDevice(QIODevice *device) -
2169{ -
2170 QXmlStreamWriterPrivate * const d = d_func(); -
2171 if (device == d->device)
-
2172 return;
-
2173 d->stringDevice = 0; -
2174 if (d->deleteDevice) {
-
2175 delete d->device; -
2176 d->deleteDevice = false; -
2177 }
-
2178 d->device = device; -
2179}
-
2180 -
2181 -
2182 -
2183 -
2184 -
2185 -
2186 -
2187QIODevice *QXmlStreamWriter::device() const -
2188{ -
2189 const QXmlStreamWriterPrivate * const d = d_func(); -
2190 return d->device;
-
2191} -
2192void QXmlStreamWriter::setCodec(QTextCodec *codec) -
2193{ -
2194 QXmlStreamWriterPrivate * const d = d_func(); -
2195 if (codec) {
-
2196 d->codec = codec; -
2197 delete d->encoder; -
2198 d->encoder = codec->makeEncoder(QTextCodec::IgnoreHeader); -
2199 d->checkIfASCIICompatibleCodec(); -
2200 }
-
2201}
-
2202void QXmlStreamWriter::setCodec(const char *codecName) -
2203{ -
2204 setCodec(QTextCodec::codecForName(codecName)); -
2205}
-
2206 -
2207 -
2208 -
2209 -
2210 -
2211 -
2212QTextCodec *QXmlStreamWriter::codec() const -
2213{ -
2214 const QXmlStreamWriterPrivate * const d = d_func(); -
2215 return d->codec;
-
2216} -
2217void QXmlStreamWriter::setAutoFormatting(bool enable) -
2218{ -
2219 QXmlStreamWriterPrivate * const d = d_func(); -
2220 d->autoFormatting = enable; -
2221}
-
2222 -
2223 -
2224 -
2225 -
2226 -
2227 -
2228bool QXmlStreamWriter::autoFormatting() const -
2229{ -
2230 const QXmlStreamWriterPrivate * const d = d_func(); -
2231 return d->autoFormatting;
-
2232} -
2233void QXmlStreamWriter::setAutoFormattingIndent(int spacesOrTabs) -
2234{ -
2235 QXmlStreamWriterPrivate * const d = d_func(); -
2236 d->autoFormattingIndent = QByteArray(qAbs(spacesOrTabs), spacesOrTabs >= 0 ? ' ' : '\t'); -
2237}
-
2238 -
2239int QXmlStreamWriter::autoFormattingIndent() const -
2240{ -
2241 const QXmlStreamWriterPrivate * const d = d_func(); -
2242 return d->autoFormattingIndent.count(' ') - d->autoFormattingIndent.count('\t');
-
2243} -
2244 -
2245 -
2246 -
2247 -
2248 -
2249 -
2250 -
2251bool QXmlStreamWriter::hasError() const -
2252{ -
2253 const QXmlStreamWriterPrivate * const d = d_func(); -
2254 return d->hasError;
-
2255} -
2256void QXmlStreamWriter::writeAttribute(const QString &qualifiedName, const QString &value) -
2257{ -
2258 QXmlStreamWriterPrivate * const d = d_func(); -
2259 qt_noop(); -
2260 qt_noop(); -
2261 d->write(" "); -
2262 d->write(qualifiedName); -
2263 d->write("=\""); -
2264 d->writeEscaped(value, true); -
2265 d->write("\""); -
2266}
-
2267void QXmlStreamWriter::writeAttribute(const QString &namespaceUri, const QString &name, const QString &value) -
2268{ -
2269 QXmlStreamWriterPrivate * const d = d_func(); -
2270 qt_noop(); -
2271 qt_noop(); -
2272 QXmlStreamWriterPrivate::NamespaceDeclaration &namespaceDeclaration = d->findNamespace(namespaceUri, true, true); -
2273 d->write(" "); -
2274 if (!namespaceDeclaration.prefix.isEmpty()) {
-
2275 d->write(namespaceDeclaration.prefix); -
2276 d->write(":"); -
2277 }
-
2278 d->write(name); -
2279 d->write("=\""); -
2280 d->writeEscaped(value, true); -
2281 d->write("\""); -
2282}
-
2283void QXmlStreamWriter::writeAttribute(const QXmlStreamAttribute& attribute) -
2284{ -
2285 if (attribute.namespaceUri().isEmpty())
-
2286 writeAttribute(attribute.qualifiedName().toString(), -
2287 attribute.value().toString());
-
2288 else -
2289 writeAttribute(attribute.namespaceUri().toString(), -
2290 attribute.name().toString(), -
2291 attribute.value().toString());
-
2292} -
2293void QXmlStreamWriter::writeAttributes(const QXmlStreamAttributes& attributes) -
2294{ -
2295 QXmlStreamWriterPrivate * const d = d_func(); -
2296 qt_noop(); -
2297 (void)d;; -
2298 for (int i = 0; i < attributes.size(); ++i)
-
2299 writeAttribute(attributes.at(i));
-
2300}
-
2301void QXmlStreamWriter::writeCDATA(const QString &text) -
2302{ -
2303 QXmlStreamWriterPrivate * const d = d_func(); -
2304 d->finishStartElement(); -
2305 QString copy(text); -
2306 copy.replace(QLatin1String("]]>"), QLatin1String("]]]]><![CDATA[>")); -
2307 d->write("<![CDATA["); -
2308 d->write(copy); -
2309 d->write("]]>"); -
2310}
-
2311void QXmlStreamWriter::writeCharacters(const QString &text) -
2312{ -
2313 QXmlStreamWriterPrivate * const d = d_func(); -
2314 d->finishStartElement(); -
2315 d->writeEscaped(text); -
2316}
-
2317 -
2318 -
2319 -
2320 -
2321 -
2322 -
2323void QXmlStreamWriter::writeComment(const QString &text) -
2324{ -
2325 QXmlStreamWriterPrivate * const d = d_func(); -
2326 qt_noop(); -
2327 if (!d->finishStartElement(false) && d->autoFormatting)
-
2328 d->indent(d->tagStack.size());
-
2329 d->write("<!--"); -
2330 d->write(text); -
2331 d->write("-->"); -
2332 d->inStartElement = d->lastWasStartElement = false; -
2333}
-
2334 -
2335 -
2336 -
2337 -
2338 -
2339void QXmlStreamWriter::writeDTD(const QString &dtd) -
2340{ -
2341 QXmlStreamWriterPrivate * const d = d_func(); -
2342 d->finishStartElement(); -
2343 if (d->autoFormatting)
-
2344 d->write("\n");
-
2345 d->write(dtd); -
2346 if (d->autoFormatting)
-
2347 d->write("\n");
-
2348}
-
2349 -
2350 -
2351 -
2352 -
2353 -
2354 -
2355 -
2356void QXmlStreamWriter::writeEmptyElement(const QString &qualifiedName) -
2357{ -
2358 QXmlStreamWriterPrivate * const d = d_func(); -
2359 qt_noop(); -
2360 d->writeStartElement(QString(), qualifiedName); -
2361 d->inEmptyElement = true; -
2362}
-
2363void QXmlStreamWriter::writeEmptyElement(const QString &namespaceUri, const QString &name) -
2364{ -
2365 QXmlStreamWriterPrivate * const d = d_func(); -
2366 qt_noop(); -
2367 d->writeStartElement(namespaceUri, name); -
2368 d->inEmptyElement = true; -
2369}
-
2370void QXmlStreamWriter::writeTextElement(const QString &qualifiedName, const QString &text) -
2371{ -
2372 writeStartElement(qualifiedName); -
2373 writeCharacters(text); -
2374 writeEndElement(); -
2375}
-
2376void QXmlStreamWriter::writeTextElement(const QString &namespaceUri, const QString &name, const QString &text) -
2377{ -
2378 writeStartElement(namespaceUri, name); -
2379 writeCharacters(text); -
2380 writeEndElement(); -
2381}
-
2382 -
2383 -
2384 -
2385 -
2386 -
2387 -
2388 -
2389void QXmlStreamWriter::writeEndDocument() -
2390{ -
2391 QXmlStreamWriterPrivate * const d = d_func(); -
2392 while (d->tagStack.size())
-
2393 writeEndElement();
-
2394 d->write("\n"); -
2395}
-
2396 -
2397 -
2398 -
2399 -
2400 -
2401 -
2402void QXmlStreamWriter::writeEndElement() -
2403{ -
2404 QXmlStreamWriterPrivate * const d = d_func(); -
2405 if (d->tagStack.isEmpty())
-
2406 return;
-
2407 -
2408 -
2409 if (d->inStartElement && !d->inEmptyElement) {
-
2410 d->write("/>"); -
2411 d->lastWasStartElement = d->inStartElement = false; -
2412 QXmlStreamWriterPrivate::Tag &tag = d->tagStack_pop(); -
2413 d->lastNamespaceDeclaration = tag.namespaceDeclarationsSize; -
2414 return;
-
2415 } -
2416 -
2417 if (!d->finishStartElement(false) && !d->lastWasStartElement && d->autoFormatting)
-
2418 d->indent(d->tagStack.size()-1);
-
2419 if (d->tagStack.isEmpty())
-
2420 return;
-
2421 d->lastWasStartElement = false; -
2422 QXmlStreamWriterPrivate::Tag &tag = d->tagStack_pop(); -
2423 d->lastNamespaceDeclaration = tag.namespaceDeclarationsSize; -
2424 d->write("</"); -
2425 if (!tag.namespaceDeclaration.prefix.isEmpty()) {
-
2426 d->write(tag.namespaceDeclaration.prefix); -
2427 d->write(":"); -
2428 }
-
2429 d->write(tag.name); -
2430 d->write(">"); -
2431}
-
2432 -
2433 -
2434 -
2435 -
2436 -
2437 -
2438void QXmlStreamWriter::writeEntityReference(const QString &name) -
2439{ -
2440 QXmlStreamWriterPrivate * const d = d_func(); -
2441 d->finishStartElement(); -
2442 d->write("&"); -
2443 d->write(name); -
2444 d->write(";"); -
2445}
-
2446void QXmlStreamWriter::writeNamespace(const QString &namespaceUri, const QString &prefix) -
2447{ -
2448 QXmlStreamWriterPrivate * const d = d_func(); -
2449 qt_noop(); -
2450 qt_noop(); -
2451 if (prefix.isEmpty()) {
-
2452 d->findNamespace(namespaceUri, d->inStartElement); -
2453 } else {
-
2454 qt_noop(); -
2455 qt_noop(); -
2456 QXmlStreamWriterPrivate::NamespaceDeclaration &namespaceDeclaration = d->namespaceDeclarations.push(); -
2457 namespaceDeclaration.prefix = d->addToStringStorage(prefix); -
2458 namespaceDeclaration.namespaceUri = d->addToStringStorage(namespaceUri); -
2459 if (d->inStartElement)
-
2460 d->writeNamespaceDeclaration(namespaceDeclaration);
-
2461 }
-
2462} -
2463void QXmlStreamWriter::writeDefaultNamespace(const QString &namespaceUri) -
2464{ -
2465 QXmlStreamWriterPrivate * const d = d_func(); -
2466 qt_noop(); -
2467 qt_noop(); -
2468 QXmlStreamWriterPrivate::NamespaceDeclaration &namespaceDeclaration = d->namespaceDeclarations.push(); -
2469 namespaceDeclaration.prefix.clear(); -
2470 namespaceDeclaration.namespaceUri = d->addToStringStorage(namespaceUri); -
2471 if (d->inStartElement)
-
2472 d->writeNamespaceDeclaration(namespaceDeclaration);
-
2473}
-
2474 -
2475 -
2476 -
2477 -
2478 -
2479 -
2480void QXmlStreamWriter::writeProcessingInstruction(const QString &target, const QString &data) -
2481{ -
2482 QXmlStreamWriterPrivate * const d = d_func(); -
2483 qt_noop(); -
2484 if (!d->finishStartElement(false) && d->autoFormatting)
-
2485 d->indent(d->tagStack.size());
-
2486 d->write("<?"); -
2487 d->write(target); -
2488 if (!data.isNull()) {
-
2489 d->write(" "); -
2490 d->write(data); -
2491 }
-
2492 d->write("?>"); -
2493}
-
2494void QXmlStreamWriter::writeStartDocument() -
2495{ -
2496 writeStartDocument(QLatin1String("1.0")); -
2497}
-
2498 -
2499 -
2500 -
2501 -
2502 -
2503 -
2504 -
2505void QXmlStreamWriter::writeStartDocument(const QString &version) -
2506{ -
2507 QXmlStreamWriterPrivate * const d = d_func(); -
2508 d->finishStartElement(false); -
2509 d->write("<?xml version=\""); -
2510 d->write(version); -
2511 if (d->device) {
-
2512 d->write("\" encoding=\""); -
2513 -
2514 -
2515 -
2516 d->write(d->codec->name().constData(), d->codec->name().length()); -
2517 -
2518 }
-
2519 d->write("\"?>"); -
2520}
-
2521 -
2522 -
2523 -
2524 -
2525 -
2526 -
2527 -
2528void QXmlStreamWriter::writeStartDocument(const QString &version, bool standalone) -
2529{ -
2530 QXmlStreamWriterPrivate * const d = d_func(); -
2531 d->finishStartElement(false); -
2532 d->write("<?xml version=\""); -
2533 d->write(version); -
2534 if (d->device) {
-
2535 d->write("\" encoding=\""); -
2536 -
2537 -
2538 -
2539 d->write(d->codec->name().constData(), d->codec->name().length()); -
2540 -
2541 }
-
2542 if (standalone)
-
2543 d->write("\" standalone=\"yes\"?>");
-
2544 else -
2545 d->write("\" standalone=\"no\"?>");
-
2546} -
2547void QXmlStreamWriter::writeStartElement(const QString &qualifiedName) -
2548{ -
2549 QXmlStreamWriterPrivate * const d = d_func(); -
2550 qt_noop(); -
2551 d->writeStartElement(QString(), qualifiedName); -
2552}
-
2553void QXmlStreamWriter::writeStartElement(const QString &namespaceUri, const QString &name) -
2554{ -
2555 QXmlStreamWriterPrivate * const d = d_func(); -
2556 qt_noop(); -
2557 d->writeStartElement(namespaceUri, name); -
2558}
-
2559 -
2560void QXmlStreamWriterPrivate::writeStartElement(const QString &namespaceUri, const QString &name) -
2561{ -
2562 if (!finishStartElement(false) && autoFormatting)
-
2563 indent(tagStack.size());
-
2564 -
2565 Tag &tag = tagStack_push(); -
2566 tag.name = addToStringStorage(name); -
2567 tag.namespaceDeclaration = findNamespace(namespaceUri); -
2568 write("<"); -
2569 if (!tag.namespaceDeclaration.prefix.isEmpty()) {
-
2570 write(tag.namespaceDeclaration.prefix); -
2571 write(":"); -
2572 }
-
2573 write(tag.name); -
2574 inStartElement = lastWasStartElement = true; -
2575 -
2576 for (int i = lastNamespaceDeclaration; i < namespaceDeclarations.size(); ++i)
-
2577 writeNamespaceDeclaration(namespaceDeclarations[i]);
-
2578 tag.namespaceDeclarationsSize = lastNamespaceDeclaration; -
2579}
-
2580void QXmlStreamWriter::writeCurrentToken(const QXmlStreamReader &reader) -
2581{ -
2582 switch (reader.tokenType()) { -
2583 case QXmlStreamReader::NoToken: -
2584 break;
-
2585 case QXmlStreamReader::StartDocument: -
2586 writeStartDocument(); -
2587 break;
-
2588 case QXmlStreamReader::EndDocument: -
2589 writeEndDocument(); -
2590 break;
-
2591 case QXmlStreamReader::StartElement: { -
2592 QXmlStreamNamespaceDeclarations namespaceDeclarations = reader.namespaceDeclarations(); -
2593 for (int i = 0; i < namespaceDeclarations.size(); ++i) {
-
2594 const QXmlStreamNamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(i); -
2595 writeNamespace(namespaceDeclaration.namespaceUri().toString(), -
2596 namespaceDeclaration.prefix().toString()); -
2597 }
-
2598 writeStartElement(reader.namespaceUri().toString(), reader.name().toString()); -
2599 writeAttributes(reader.attributes()); -
2600 } break;
-
2601 case QXmlStreamReader::EndElement: -
2602 writeEndElement(); -
2603 break;
-
2604 case QXmlStreamReader::Characters: -
2605 if (reader.isCDATA())
-
2606 writeCDATA(reader.text().toString());
-
2607 else -
2608 writeCharacters(reader.text().toString());
-
2609 break;
-
2610 case QXmlStreamReader::Comment: -
2611 writeComment(reader.text().toString()); -
2612 break;
-
2613 case QXmlStreamReader::DTD: -
2614 writeDTD(reader.text().toString()); -
2615 break;
-
2616 case QXmlStreamReader::EntityReference: -
2617 writeEntityReference(reader.name().toString()); -
2618 break;
-
2619 case QXmlStreamReader::ProcessingInstruction: -
2620 writeProcessingInstruction(reader.processingInstructionTarget().toString(), -
2621 reader.processingInstructionData().toString()); -
2622 break;
-
2623 default: -
2624 qt_noop(); -
2625 QMessageLogger("xml/qxmlstream.cpp", 3929, __PRETTY_FUNCTION__).warning("QXmlStreamWriter: writeCurrentToken() with invalid state."); -
2626 break;
-
2627 } -
2628}
-
2629 -
2630 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial