1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtXml 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 <QtXml/qtxmlglobal.h>
41
42#ifndef QT_NO_DOM
43
44#include "qdomhelpers_p.h"
45#include "qdom_p.h"
46#include "qxmlstream.h"
47#include "private/qxml_p.h"
48
49QT_BEGIN_NAMESPACE
50
51#if QT_DEPRECATED_SINCE(5, 15)
52
53/**************************************************************
54 *
55 * QDomHandler
56 *
57 **************************************************************/
58QT_WARNING_PUSH
59QT_WARNING_DISABLE_DEPRECATED
60QDomHandler::QDomHandler(QDomDocumentPrivate *adoc, QXmlSimpleReader *areader,
61 bool namespaceProcessing)
62 : cdata(false), reader(areader), domBuilder(adoc, &locator, namespaceProcessing)
63{
64}
65
66QDomHandler::~QDomHandler() {}
67
68bool QDomHandler::endDocument()
69{
70 return domBuilder.endDocument();
71}
72
73bool QDomHandler::startDTD(const QString &name, const QString &publicId, const QString &systemId)
74{
75 return domBuilder.startDTD(name, publicId, systemId);
76}
77
78bool QDomHandler::startElement(const QString &nsURI, const QString &, const QString &qName,
79 const QXmlAttributes &atts)
80{
81 return domBuilder.startElement(nsURI, qName, atts);
82}
83
84bool QDomHandler::endElement(const QString &, const QString &, const QString &)
85{
86 return domBuilder.endElement();
87}
88
89bool QDomHandler::characters(const QString &ch)
90{
91 return domBuilder.characters(characters: ch, cdata);
92}
93
94bool QDomHandler::processingInstruction(const QString &target, const QString &data)
95{
96 return domBuilder.processingInstruction(target, data);
97}
98
99bool QDomHandler::skippedEntity(const QString &name)
100{
101 // we can only handle inserting entity references into content
102 if (reader && !reader->d_ptr->skipped_entity_in_content)
103 return true;
104
105 return domBuilder.skippedEntity(name);
106}
107
108bool QDomHandler::fatalError(const QXmlParseException &exception)
109{
110 domBuilder.errorMsg = exception.message();
111 domBuilder.errorLine = exception.lineNumber();
112 domBuilder.errorColumn = exception.columnNumber();
113 return QXmlDefaultHandler::fatalError(exception);
114}
115
116bool QDomHandler::startCDATA()
117{
118 cdata = true;
119 return true;
120}
121
122bool QDomHandler::endCDATA()
123{
124 cdata = false;
125 return true;
126}
127
128bool QDomHandler::startEntity(const QString &name)
129{
130 return domBuilder.startEntity(name);
131}
132
133bool QDomHandler::endEntity(const QString &)
134{
135 return domBuilder.endEntity();
136}
137
138bool QDomHandler::comment(const QString &ch)
139{
140 return domBuilder.comment(characters: ch);
141}
142
143bool QDomHandler::unparsedEntityDecl(const QString &name, const QString &publicId,
144 const QString &systemId, const QString &notationName)
145{
146 return domBuilder.unparsedEntityDecl(name, publicId, systemId, notationName);
147}
148
149bool QDomHandler::externalEntityDecl(const QString &name, const QString &publicId,
150 const QString &systemId)
151{
152 return unparsedEntityDecl(name, publicId, systemId, notationName: QString());
153}
154
155bool QDomHandler::notationDecl(const QString &name, const QString &publicId,
156 const QString &systemId)
157{
158 return domBuilder.notationDecl(name, publicId, systemId);
159}
160
161void QDomHandler::setDocumentLocator(QXmlLocator *locator)
162{
163 this->locator.setLocator(locator);
164}
165
166QDomBuilder::ErrorInfo QDomHandler::errorInfo() const
167{
168 return domBuilder.error();
169}
170QT_WARNING_POP
171
172#endif // QT_DEPRECATED_SINCE(5, 15)
173
174/**************************************************************
175 *
176 * QXmlDocumentLocators
177 *
178 **************************************************************/
179
180int QDomDocumentLocator::column() const
181{
182 Q_ASSERT(reader);
183 return static_cast<int>(reader->columnNumber());
184}
185
186int QDomDocumentLocator::line() const
187{
188 Q_ASSERT(reader);
189 return static_cast<int>(reader->lineNumber());
190}
191
192#if QT_DEPRECATED_SINCE(5, 15)
193
194QT_WARNING_PUSH
195QT_WARNING_DISABLE_DEPRECATED
196
197void QSAXDocumentLocator::setLocator(QXmlLocator *l)
198{
199 locator = l;
200}
201
202int QSAXDocumentLocator::column() const
203{
204 if (!locator)
205 return 0;
206
207 return static_cast<int>(locator->columnNumber());
208}
209
210int QSAXDocumentLocator::line() const
211{
212 if (!locator)
213 return 0;
214
215 return static_cast<int>(locator->lineNumber());
216}
217
218QT_WARNING_POP
219
220#endif // QT_DEPRECATED_SINCE(5, 15)
221
222/**************************************************************
223 *
224 * QDomBuilder
225 *
226 **************************************************************/
227
228QDomBuilder::QDomBuilder(QDomDocumentPrivate *d, QXmlDocumentLocator *l, bool namespaceProcessing)
229 : errorLine(0),
230 errorColumn(0),
231 doc(d),
232 node(d),
233 locator(l),
234 nsProcessing(namespaceProcessing)
235{
236}
237
238QDomBuilder::~QDomBuilder() {}
239
240bool QDomBuilder::endDocument()
241{
242 // ### is this really necessary? (rms)
243 if (node != doc)
244 return false;
245 return true;
246}
247
248bool QDomBuilder::startDTD(const QString &name, const QString &publicId, const QString &systemId)
249{
250 doc->doctype()->name = name;
251 doc->doctype()->publicId = publicId;
252 doc->doctype()->systemId = systemId;
253 return true;
254}
255
256#if QT_DEPRECATED_SINCE(5, 15)
257
258QT_WARNING_PUSH
259QT_WARNING_DISABLE_DEPRECATED
260bool QDomBuilder::startElement(const QString &nsURI, const QString &qName,
261 const QXmlAttributes &atts)
262{
263 // tag name
264 QDomNodePrivate *n;
265 if (nsProcessing) {
266 n = doc->createElementNS(nsURI, qName);
267 } else {
268 n = doc->createElement(tagName: qName);
269 }
270
271 if (!n)
272 return false;
273
274 n->setLocation(lineNumber: locator->line(), columnNumber: locator->column());
275
276 node->appendChild(newChild: n);
277 node = n;
278
279 // attributes
280 for (int i = 0; i < atts.length(); i++) {
281 auto domElement = static_cast<QDomElementPrivate *>(node);
282 if (nsProcessing)
283 domElement->setAttributeNS(nsURI: atts.uri(index: i), qName: atts.qName(index: i), newValue: atts.value(index: i));
284 else
285 domElement->setAttribute(name: atts.qName(index: i), value: atts.value(index: i));
286 }
287
288 return true;
289}
290QT_WARNING_POP
291
292#endif // QT_DEPRECATED_SINCE(5, 15)
293
294inline QString stringRefToString(const QStringRef &stringRef)
295{
296 // Calling QStringRef::toString() on a NULL QStringRef in some cases returns
297 // an empty string (i.e. QString("")) instead of a NULL string (i.e. QString()).
298 // QDom implementation differentiates between NULL and empty strings, so
299 // we need this as workaround to keep the current behavior unchanged.
300 return stringRef.isNull() ? QString() : stringRef.toString();
301}
302
303bool QDomBuilder::startElement(const QString &nsURI, const QString &qName,
304 const QXmlStreamAttributes &atts)
305{
306 QDomNodePrivate *n =
307 nsProcessing ? doc->createElementNS(nsURI, qName) : doc->createElement(tagName: qName);
308 if (!n)
309 return false;
310
311 n->setLocation(lineNumber: locator->line(), columnNumber: locator->column());
312
313 node->appendChild(newChild: n);
314 node = n;
315
316 // attributes
317 for (const auto &attr : atts) {
318 auto domElement = static_cast<QDomElementPrivate *>(node);
319 if (nsProcessing) {
320 domElement->setAttributeNS(nsURI: stringRefToString(stringRef: attr.namespaceUri()),
321 qName: stringRefToString(stringRef: attr.qualifiedName()),
322 newValue: stringRefToString(stringRef: attr.value()));
323 } else {
324 domElement->setAttribute(name: stringRefToString(stringRef: attr.qualifiedName()),
325 value: stringRefToString(stringRef: attr.value()));
326 }
327 }
328
329 return true;
330}
331
332bool QDomBuilder::endElement()
333{
334 if (!node || node == doc)
335 return false;
336 node = node->parent();
337
338 return true;
339}
340
341bool QDomBuilder::characters(const QString &characters, bool cdata)
342{
343 // No text as child of some document
344 if (node == doc)
345 return false;
346
347 QScopedPointer<QDomNodePrivate> n;
348 if (cdata) {
349 n.reset(other: doc->createCDATASection(data: characters));
350 } else if (!entityName.isEmpty()) {
351 QScopedPointer<QDomEntityPrivate> e(
352 new QDomEntityPrivate(doc, nullptr, entityName, QString(), QString(), QString()));
353 e->value = characters;
354 e->ref.deref();
355 doc->doctype()->appendChild(newChild: e.data());
356 e.take();
357 n.reset(other: doc->createEntityReference(name: entityName));
358 } else {
359 n.reset(other: doc->createTextNode(data: characters));
360 }
361 n->setLocation(lineNumber: locator->line(), columnNumber: locator->column());
362 node->appendChild(newChild: n.data());
363 n.take();
364
365 return true;
366}
367
368bool QDomBuilder::processingInstruction(const QString &target, const QString &data)
369{
370 QDomNodePrivate *n;
371 n = doc->createProcessingInstruction(target, data);
372 if (n) {
373 n->setLocation(lineNumber: locator->line(), columnNumber: locator->column());
374 node->appendChild(newChild: n);
375 return true;
376 } else
377 return false;
378}
379
380bool QDomBuilder::skippedEntity(const QString &name)
381{
382 QDomNodePrivate *n = doc->createEntityReference(name);
383 n->setLocation(lineNumber: locator->line(), columnNumber: locator->column());
384 node->appendChild(newChild: n);
385 return true;
386}
387
388void QDomBuilder::fatalError(const QString &message)
389{
390 errorMsg = message;
391 errorLine = static_cast<int>(locator->line());
392 errorColumn = static_cast<int>(locator->column());
393}
394
395QDomBuilder::ErrorInfo QDomBuilder::error() const
396{
397 return ErrorInfo(errorMsg, errorLine, errorColumn);
398}
399
400bool QDomBuilder::startEntity(const QString &name)
401{
402 entityName = name;
403 return true;
404}
405
406bool QDomBuilder::endEntity()
407{
408 entityName.clear();
409 return true;
410}
411
412bool QDomBuilder::comment(const QString &characters)
413{
414 QDomNodePrivate *n;
415 n = doc->createComment(data: characters);
416 n->setLocation(lineNumber: locator->line(), columnNumber: locator->column());
417 node->appendChild(newChild: n);
418 return true;
419}
420
421bool QDomBuilder::unparsedEntityDecl(const QString &name, const QString &publicId,
422 const QString &systemId, const QString &notationName)
423{
424 QDomEntityPrivate *e =
425 new QDomEntityPrivate(doc, nullptr, name, publicId, systemId, notationName);
426 // keep the refcount balanced: appendChild() does a ref anyway.
427 e->ref.deref();
428 doc->doctype()->appendChild(newChild: e);
429 return true;
430}
431
432bool QDomBuilder::externalEntityDecl(const QString &name, const QString &publicId,
433 const QString &systemId)
434{
435 return unparsedEntityDecl(name, publicId, systemId, notationName: QString());
436}
437
438bool QDomBuilder::notationDecl(const QString &name, const QString &publicId,
439 const QString &systemId)
440{
441 QDomNotationPrivate *n = new QDomNotationPrivate(doc, nullptr, name, publicId, systemId);
442 // keep the refcount balanced: appendChild() does a ref anyway.
443 n->ref.deref();
444 doc->doctype()->appendChild(newChild: n);
445 return true;
446}
447
448/**************************************************************
449 *
450 * QDomParser
451 *
452 **************************************************************/
453
454QDomParser::QDomParser(QDomDocumentPrivate *d, QXmlStreamReader *r, bool namespaceProcessing)
455 : reader(r), locator(r), domBuilder(d, &locator, namespaceProcessing)
456{
457}
458
459bool QDomParser::parse()
460{
461 return parseProlog() && parseBody();
462}
463
464QDomBuilder::ErrorInfo QDomParser::errorInfo() const
465{
466 return domBuilder.error();
467}
468
469bool QDomParser::parseProlog()
470{
471 Q_ASSERT(reader);
472
473 bool foundDtd = false;
474
475 while (!reader->atEnd()) {
476 reader->readNext();
477
478 if (reader->hasError()) {
479 domBuilder.fatalError(message: reader->errorString());
480 return false;
481 }
482
483 switch (reader->tokenType()) {
484 case QXmlStreamReader::StartDocument:
485 if (!reader->documentVersion().isEmpty()) {
486 QString value(QLatin1String("version='"));
487 value += reader->documentVersion();
488 value += QLatin1Char('\'');
489 if (!reader->documentEncoding().isEmpty()) {
490 value += QLatin1String(" encoding='");
491 value += reader->documentEncoding();
492 value += QLatin1Char('\'');
493 }
494 if (reader->isStandaloneDocument()) {
495 value += QLatin1String(" standalone='yes'");
496 } else {
497 // TODO: Add standalone='no', if 'standalone' is specified. With the current
498 // QXmlStreamReader there is no way to figure out if it was specified or not.
499 // QXmlStreamReader needs to be modified for handling that case correctly.
500 }
501
502 if (!domBuilder.processingInstruction(target: QLatin1String("xml"), data: value)) {
503 domBuilder.fatalError(
504 message: QDomParser::tr(sourceText: "Error occurred while processing XML declaration"));
505 return false;
506 }
507 }
508 break;
509 case QXmlStreamReader::DTD:
510 if (foundDtd) {
511 domBuilder.fatalError(message: QDomParser::tr(sourceText: "Multiple DTD sections are not allowed"));
512 return false;
513 }
514 foundDtd = true;
515
516 if (!domBuilder.startDTD(name: stringRefToString(stringRef: reader->dtdName()),
517 publicId: stringRefToString(stringRef: reader->dtdPublicId()),
518 systemId: stringRefToString(stringRef: reader->dtdSystemId()))) {
519 domBuilder.fatalError(
520 message: QDomParser::tr(sourceText: "Error occurred while processing document type declaration"));
521 return false;
522 }
523 if (!parseMarkupDecl())
524 return false;
525 break;
526 case QXmlStreamReader::Comment:
527 if (!domBuilder.comment(characters: reader->text().toString())) {
528 domBuilder.fatalError(message: QDomParser::tr(sourceText: "Error occurred while processing comment"));
529 return false;
530 }
531 break;
532 case QXmlStreamReader::ProcessingInstruction:
533 if (!domBuilder.processingInstruction(target: reader->processingInstructionTarget().toString(),
534 data: reader->processingInstructionData().toString())) {
535 domBuilder.fatalError(
536 message: QDomParser::tr(sourceText: "Error occurred while processing a processing instruction"));
537 return false;
538 }
539 break;
540 default:
541 // If the token is none of the above, prolog processing is done.
542 return true;
543 }
544 }
545
546 return true;
547}
548
549bool QDomParser::parseBody()
550{
551 Q_ASSERT(reader);
552
553 std::stack<QStringRef> tagStack;
554 while (!reader->atEnd() && !reader->hasError()) {
555 switch (reader->tokenType()) {
556 case QXmlStreamReader::StartElement:
557 tagStack.push(x: reader->qualifiedName());
558 if (!domBuilder.startElement(nsURI: stringRefToString(stringRef: reader->namespaceUri()),
559 qName: stringRefToString(stringRef: reader->qualifiedName()),
560 atts: reader->attributes())) {
561 domBuilder.fatalError(
562 message: QDomParser::tr(sourceText: "Error occurred while processing a start element"));
563 return false;
564 }
565 break;
566 case QXmlStreamReader::EndElement:
567 if (tagStack.empty() || reader->qualifiedName() != tagStack.top()) {
568 domBuilder.fatalError(
569 message: QDomParser::tr(sourceText: "Unexpected end element '%1'").arg(a: reader->name()));
570 return false;
571 }
572 tagStack.pop();
573 if (!domBuilder.endElement()) {
574 domBuilder.fatalError(
575 message: QDomParser::tr(sourceText: "Error occurred while processing an end element"));
576 return false;
577 }
578 break;
579 case QXmlStreamReader::Characters:
580 if (!reader->isWhitespace()) { // Skip the content consisting of only whitespaces
581 if (!reader->text().toString().trimmed().isEmpty()) {
582 if (!domBuilder.characters(characters: reader->text().toString(), cdata: reader->isCDATA())) {
583 domBuilder.fatalError(message: QDomParser::tr(
584 sourceText: "Error occurred while processing the element content"));
585 return false;
586 }
587 }
588 }
589 break;
590 case QXmlStreamReader::Comment:
591 if (!domBuilder.comment(characters: reader->text().toString())) {
592 domBuilder.fatalError(message: QDomParser::tr(sourceText: "Error occurred while processing comments"));
593 return false;
594 }
595 break;
596 case QXmlStreamReader::ProcessingInstruction:
597 if (!domBuilder.processingInstruction(target: reader->processingInstructionTarget().toString(),
598 data: reader->processingInstructionData().toString())) {
599 domBuilder.fatalError(
600 message: QDomParser::tr(sourceText: "Error occurred while processing a processing instruction"));
601 return false;
602 }
603 break;
604 case QXmlStreamReader::EntityReference:
605 if (!domBuilder.skippedEntity(name: reader->name().toString())) {
606 domBuilder.fatalError(
607 message: QDomParser::tr(sourceText: "Error occurred while processing an entity reference"));
608 return false;
609 }
610 break;
611 default:
612 domBuilder.fatalError(message: QDomParser::tr(sourceText: "Unexpected token"));
613 return false;
614 }
615
616 reader->readNext();
617 }
618
619 if (reader->hasError()) {
620 domBuilder.fatalError(message: reader->errorString());
621 reader->readNext();
622 return false;
623 }
624
625 if (!tagStack.empty()) {
626 domBuilder.fatalError(message: QDomParser::tr(sourceText: "Tag mismatch"));
627 return false;
628 }
629
630 return true;
631}
632
633bool QDomParser::parseMarkupDecl()
634{
635 Q_ASSERT(reader);
636
637 const auto entities = reader->entityDeclarations();
638 for (const auto &entityDecl : entities) {
639 // Entity declarations are created only for Extrenal Entities. Internal Entities
640 // are parsed, and QXmlStreamReader handles the parsing itself and returns the
641 // parsed result. So we don't need to do anything for the Internal Entities.
642 if (!entityDecl.publicId().isEmpty() || !entityDecl.systemId().isEmpty()) {
643 // External Entity
644 if (!domBuilder.unparsedEntityDecl(name: stringRefToString(stringRef: entityDecl.name()),
645 publicId: stringRefToString(stringRef: entityDecl.publicId()),
646 systemId: stringRefToString(stringRef: entityDecl.systemId()),
647 notationName: stringRefToString(stringRef: entityDecl.notationName()))) {
648 domBuilder.fatalError(
649 message: QDomParser::tr(sourceText: "Error occurred while processing entity declaration"));
650 return false;
651 }
652 }
653 }
654
655 const auto notations = reader->notationDeclarations();
656 for (const auto &notationDecl : notations) {
657 if (!domBuilder.notationDecl(name: stringRefToString(stringRef: notationDecl.name()),
658 publicId: stringRefToString(stringRef: notationDecl.publicId()),
659 systemId: stringRefToString(stringRef: notationDecl.systemId()))) {
660 domBuilder.fatalError(
661 message: QDomParser::tr(sourceText: "Error occurred while processing notation declaration"));
662 return false;
663 }
664 }
665
666 return true;
667}
668
669QT_END_NAMESPACE
670
671#endif // QT_NO_DOM
672

source code of qtbase/src/xml/dom/qdomhelpers.cpp