diff options
author | Leander Beernaert <[email protected]> | 2020-01-22 13:47:08 +0100 |
---|---|---|
committer | Leander Beernaert <[email protected]> | 2020-01-24 13:17:33 +0100 |
commit | 502d3d6744913899da87acfda5ebdab42c40329e (patch) | |
tree | 16658a328503bfd5a62b4fd5d69ffb66e9854b18 /src/xml/dom | |
parent | d1be8b9ceb2c7b20bbe53a07154c79699540ea3d (diff) | |
parent | 06bb315beb6c2c398223cfe52cbc7f66e14a8557 (diff) |
Merge remote-tracking branch 'origin/dev' into merge-dev
Change-Id: I31b761cfd5ea01373c60d02a5da8c33398d34739
Diffstat (limited to 'src/xml/dom')
-rw-r--r-- | src/xml/dom/qdom.cpp | 159 | ||||
-rw-r--r-- | src/xml/dom/qdom.h | 15 | ||||
-rw-r--r-- | src/xml/dom/qdom_p.h | 9 | ||||
-rw-r--r-- | src/xml/dom/qdomhelpers.cpp | 520 | ||||
-rw-r--r-- | src/xml/dom/qdomhelpers_p.h | 155 |
5 files changed, 768 insertions, 90 deletions
diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp index 7dbf864a7c1..90e97f512c9 100644 --- a/src/xml/dom/qdom.cpp +++ b/src/xml/dom/qdom.cpp @@ -59,6 +59,7 @@ #include <qvariant.h> #include <qshareddata.h> #include <qdebug.h> +#include <qxmlstream.h> #include <stdio.h> QT_BEGIN_NAMESPACE @@ -2572,8 +2573,8 @@ void QDomNamedNodeMapPrivate::clearMap() QDomNodePrivate* QDomNamedNodeMapPrivate::namedItem(const QString& name) const { - QDomNodePrivate* p = map[name]; - return p; + auto it = map.constFind(name); + return it == map.cend() ? nullptr : *it; } QDomNodePrivate* QDomNamedNodeMapPrivate::namedItemNS(const QString& nsURI, const QString& localName) const @@ -2602,7 +2603,7 @@ QDomNodePrivate* QDomNamedNodeMapPrivate::setNamedItem(QDomNodePrivate* arg) QDomNodePrivate *n = map.value(arg->nodeName()); // We take a reference arg->ref.ref(); - map.insertMulti(arg->nodeName(), arg); + map.insert(arg->nodeName(), arg); return n; } @@ -2619,7 +2620,7 @@ QDomNodePrivate* QDomNamedNodeMapPrivate::setNamedItemNS(QDomNodePrivate* arg) QDomNodePrivate *n = namedItemNS(arg->namespaceURI, arg->name); // We take a reference arg->ref.ref(); - map.insertMulti(arg->nodeName(), arg); + map.insert(arg->nodeName(), arg); return n; } else { // ### check the following code if it is ok @@ -2648,7 +2649,7 @@ QDomNodePrivate* QDomNamedNodeMapPrivate::item(int index) const { if (index >= length() || index < 0) return nullptr; - return *(map.constBegin() + index); + return *std::next(map.cbegin(), index); } int QDomNamedNodeMapPrivate::length() const @@ -2962,10 +2963,10 @@ QDomDocumentTypePrivate::QDomDocumentTypePrivate(QDomDocumentTypePrivate* n, boo while (p) { if (p->isEntity()) // Don't use normal insert function since we would create infinite recursion - entities->map.insertMulti(p->nodeName(), p); + entities->map.insert(p->nodeName(), p); if (p->isNotation()) // Don't use normal insert function since we would create infinite recursion - notations->map.insertMulti(p->nodeName(), p); + notations->map.insert(p->nodeName(), p); p = p->next; } } @@ -3009,9 +3010,9 @@ QDomNodePrivate* QDomDocumentTypePrivate::insertBefore(QDomNodePrivate* newChild QDomNodePrivate* p = QDomNodePrivate::insertBefore(newChild, refChild); // Update the maps if (p && p->isEntity()) - entities->map.insertMulti(p->nodeName(), p); + entities->map.insert(p->nodeName(), p); else if (p && p->isNotation()) - notations->map.insertMulti(p->nodeName(), p); + notations->map.insert(p->nodeName(), p); return p; } @@ -3022,9 +3023,9 @@ QDomNodePrivate* QDomDocumentTypePrivate::insertAfter(QDomNodePrivate* newChild, QDomNodePrivate* p = QDomNodePrivate::insertAfter(newChild, refChild); // Update the maps if (p && p->isEntity()) - entities->map.insertMulti(p->nodeName(), p); + entities->map.insert(p->nodeName(), p); else if (p && p->isNotation()) - notations->map.insertMulti(p->nodeName(), p); + notations->map.insert(p->nodeName(), p); return p; } @@ -3041,9 +3042,9 @@ QDomNodePrivate* QDomDocumentTypePrivate::replaceChild(QDomNodePrivate* newChild notations->map.remove(oldChild->nodeName()); if (p->isEntity()) - entities->map.insertMulti(p->nodeName(), p); + entities->map.insert(p->nodeName(), p); else if (p->isNotation()) - notations->map.insertMulti(p->nodeName(), p); + notations->map.insert(p->nodeName(), p); } return p; @@ -4312,20 +4313,20 @@ void QDomElement::setAttribute(const QString& name, const QString& value) \fn void QDomElement::setAttribute(const QString& name, int value) \overload - The number is formatted according to the current locale. + The formatting always uses QLocale::C. */ /*! \fn void QDomElement::setAttribute(const QString& name, uint value) \overload - The number is formatted according to the current locale. + The formatting always uses QLocale::C. */ /*! \overload - The number is formatted according to the current locale. + The formatting always uses QLocale::C. */ void QDomElement::setAttribute(const QString& name, qlonglong value) { @@ -4339,7 +4340,7 @@ void QDomElement::setAttribute(const QString& name, qlonglong value) /*! \overload - The number is formatted according to the current locale. + The formatting always uses QLocale::C. */ void QDomElement::setAttribute(const QString& name, qulonglong value) { @@ -4353,7 +4354,7 @@ void QDomElement::setAttribute(const QString& name, qulonglong value) /*! \overload - The number is formatted according to the current locale. + The formatting always uses QLocale::C. */ void QDomElement::setAttribute(const QString& name, float value) { @@ -4367,19 +4368,14 @@ void QDomElement::setAttribute(const QString& name, float value) /*! \overload - The number is formatted according to the current locale. + The formatting always uses QLocale::C. */ void QDomElement::setAttribute(const QString& name, double value) { if (!impl) return; QString x; - char buf[256]; - int count = qsnprintf(buf, sizeof(buf), "%.16g", value); - if (count > 0) - x = QString::fromLatin1(buf, count); - else - x.setNum(value); // Fallback + x.setNum(value); IMPL->setAttribute(name, x); } @@ -5695,6 +5691,10 @@ void QDomDocumentPrivate::clear() QDomNodePrivate::clear(); } +#if QT_DEPRECATED_SINCE(5, 15) + +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED static void initializeReader(QXmlSimpleReader &reader, bool namespaceProcessing) { reader.setFeature(QLatin1String("http://xml.org/sax/features/namespaces"), namespaceProcessing); @@ -5728,11 +5728,42 @@ bool QDomDocumentPrivate::setContent(QXmlInputSource *source, QXmlReader *reader if (!reader->parse(source)) { if (errorMsg) - *errorMsg = hnd.errorMsg; + *errorMsg = std::get<0>(hnd.errorInfo()); if (errorLine) - *errorLine = hnd.errorLine; + *errorLine = std::get<1>(hnd.errorInfo()); if (errorColumn) - *errorColumn = hnd.errorColumn; + *errorColumn = std::get<2>(hnd.errorInfo()); + return false; + } + + return true; +} +QT_WARNING_POP + +#endif // QT_DEPRECATED_SINCE(5, 15) + +bool QDomDocumentPrivate::setContent(QXmlStreamReader *reader, bool namespaceProcessing, + QString *errorMsg, int *errorLine, int *errorColumn) +{ + clear(); + impl = new QDomImplementationPrivate; + type = new QDomDocumentTypePrivate(this, this); + type->ref.deref(); + + if (!reader) { + qWarning("Failed to set content, XML reader is not initialized"); + return false; + } + + QDomParser domParser(this, reader, namespaceProcessing); + + if (!domParser.parse()) { + if (errorMsg) + *errorMsg = std::get<0>(domParser.errorInfo()); + if (errorLine) + *errorLine = std::get<1>(domParser.errorInfo()); + if (errorColumn) + *errorColumn = std::get<2>(domParser.errorInfo()); return false; } @@ -6158,9 +6189,19 @@ bool QDomDocument::setContent(const QString& text, bool namespaceProcessing, QSt { if (!impl) impl = new QDomDocumentPrivate(); + +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && QT_DEPRECATED_SINCE(5, 15) +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED QXmlInputSource source; +QT_WARNING_POP source.setData(text); return IMPL->setContent(&source, namespaceProcessing, errorMsg, errorLine, errorColumn); +#else + QXmlStreamReader streamReader(text); + streamReader.setNamespaceProcessing(namespaceProcessing); + return IMPL->setContent(&streamReader, namespaceProcessing, errorMsg, errorLine, errorColumn); +#endif } /*! @@ -6220,10 +6261,20 @@ bool QDomDocument::setContent(const QByteArray &data, bool namespaceProcessing, { if (!impl) impl = new QDomDocumentPrivate(); + +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && QT_DEPRECATED_SINCE(5, 15) QBuffer buf; buf.setData(data); +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED QXmlInputSource source(&buf); +QT_WARNING_POP return IMPL->setContent(&source, namespaceProcessing, errorMsg, errorLine, errorColumn); +#else + QXmlStreamReader streamReader(data); + streamReader.setNamespaceProcessing(namespaceProcessing); + return IMPL->setContent(&streamReader, namespaceProcessing, errorMsg, errorLine, errorColumn); +#endif } /*! @@ -6236,18 +6287,32 @@ bool QDomDocument::setContent(QIODevice* dev, bool namespaceProcessing, QString { if (!impl) impl = new QDomDocumentPrivate(); + +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && QT_DEPRECATED_SINCE(5, 15) +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED QXmlInputSource source(dev); +QT_WARNING_POP return IMPL->setContent(&source, namespaceProcessing, errorMsg, errorLine, errorColumn); +#else + QXmlStreamReader streamReader(dev); + streamReader.setNamespaceProcessing(namespaceProcessing); + return IMPL->setContent(&streamReader, namespaceProcessing, errorMsg, errorLine, errorColumn); +#endif } +#if QT_DEPRECATED_SINCE(5, 15) /*! \overload + \obsolete \since 4.5 This function reads the XML document from the QXmlInputSource \a source, returning true if the content was successfully parsed; otherwise returns \c false. */ +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED bool QDomDocument::setContent(QXmlInputSource *source, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn ) { if (!impl) @@ -6256,6 +6321,9 @@ bool QDomDocument::setContent(QXmlInputSource *source, bool namespaceProcessing, initializeReader(reader, namespaceProcessing); return IMPL->setContent(source, &reader, &reader, errorMsg, errorLine, errorColumn); } +QT_WARNING_POP + +#endif /*! \overload @@ -6288,6 +6356,7 @@ bool QDomDocument::setContent(const QByteArray& buffer, QString *errorMsg, int * /*! \overload + \obsolete This function reads the XML document from the IO device \a dev, returning true if the content was successfully parsed; otherwise returns \c false. @@ -6299,8 +6368,10 @@ bool QDomDocument::setContent(QIODevice* dev, QString *errorMsg, int *errorLine, return setContent(dev, false, errorMsg, errorLine, errorColumn); } +#if QT_DEPRECATED_SINCE(5, 15) /*! \overload + \obsolete This function reads the XML document from the QXmlInputSource \a source and parses it with the QXmlReader \a reader, returning true if the content was @@ -6312,12 +6383,44 @@ bool QDomDocument::setContent(QIODevice* dev, QString *errorMsg, int *errorLine, \sa QXmlSimpleReader */ +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED bool QDomDocument::setContent(QXmlInputSource *source, QXmlReader *reader, QString *errorMsg, int *errorLine, int *errorColumn ) { if (!impl) impl = new QDomDocumentPrivate(); return IMPL->setContent(source, reader, nullptr, errorMsg, errorLine, errorColumn); } +QT_WARNING_POP + +#endif + +/*! + \overload + \since 5.15 + + This function reads the XML document from the QXmlStreamReader \a reader + and parses it. Returns \c true if the content was successfully parsed; + otherwise returns \c false. + + If \a namespaceProcessing is \c true, the parser recognizes namespaces in the XML + file and sets the prefix name, local name and namespace URI to appropriate values. + If \a namespaceProcessing is \c false, the parser does no namespace processing when + it reads the XML file. + + If a parse error occurs, the error message is placed in \c{*}\a{errorMsg}, the line + number in \c{*}\a{errorLine} and the column number in \c{*}\a{errorColumn} (unless + the associated pointer is set to 0). + + \sa QXmlStreamReader +*/ +bool QDomDocument::setContent(QXmlStreamReader *reader, bool namespaceProcessing, QString *errorMsg, + int *errorLine, int *errorColumn) +{ + if (!impl) + impl = new QDomDocumentPrivate(); + return IMPL->setContent(reader, namespaceProcessing, errorMsg, errorLine, errorColumn); +} /*! Converts the parsed document back to its textual representation. diff --git a/src/xml/dom/qdom.h b/src/xml/dom/qdom.h index 2d07e34f3b4..9f34290121d 100644 --- a/src/xml/dom/qdom.h +++ b/src/xml/dom/qdom.h @@ -91,6 +91,7 @@ class QDomNode; class QDomEntity; class QDomNotation; class QDomCharacterData; +class QXmlStreamReader; class Q_XML_EXPORT QDomImplementation { @@ -338,11 +339,25 @@ public: bool setContent(const QByteArray& text, bool namespaceProcessing, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr ); bool setContent(const QString& text, bool namespaceProcessing, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr ); bool setContent(QIODevice* dev, bool namespaceProcessing, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr ); +#if QT_DEPRECATED_SINCE(5, 15) +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED + QT_DEPRECATED_X("Use other overloads instead") bool setContent(QXmlInputSource *source, bool namespaceProcessing, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr ); +QT_WARNING_POP +#endif bool setContent(const QByteArray& text, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr ); bool setContent(const QString& text, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr ); bool setContent(QIODevice* dev, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr ); +#if QT_DEPRECATED_SINCE(5, 15) +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED + QT_DEPRECATED_X("Use other overloads instead") bool setContent(QXmlInputSource *source, QXmlReader *reader, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr ); +QT_WARNING_POP +#endif + bool setContent(QXmlStreamReader *reader, bool namespaceProcessing, QString *errorMsg = nullptr, + int *errorLine = nullptr, int *errorColumn = nullptr); // Qt extensions QString toString(int = 1) const; diff --git a/src/xml/dom/qdom_p.h b/src/xml/dom/qdom_p.h index 06ed0d5f0e7..a9399d99012 100644 --- a/src/xml/dom/qdom_p.h +++ b/src/xml/dom/qdom_p.h @@ -235,7 +235,7 @@ public: // Variables QAtomicInt ref; - QHash<QString, QDomNodePrivate *> map; + QMultiHash<QString, QDomNodePrivate *> map; QDomNodePrivate *parent; bool readonly; bool appendToParent; @@ -461,10 +461,17 @@ public: QDomDocumentPrivate(QDomDocumentPrivate *n, bool deep); ~QDomDocumentPrivate(); +#if QT_DEPRECATED_SINCE(5, 15) +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED bool setContent(QXmlInputSource *source, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn); bool setContent(QXmlInputSource *source, QXmlReader *reader, QXmlSimpleReader *simpleReader, QString *errorMsg, int *errorLine, int *errorColumn); +QT_WARNING_POP +#endif + bool setContent(QXmlStreamReader *reader, bool namespaceProcessing, QString *errorMsg, + int *errorLine, int *errorColumn); // Attributes QDomDocumentTypePrivate *doctype() { return type.data(); } diff --git a/src/xml/dom/qdomhelpers.cpp b/src/xml/dom/qdomhelpers.cpp index 74d2c8c124e..10e37f7c0fe 100644 --- a/src/xml/dom/qdomhelpers.cpp +++ b/src/xml/dom/qdomhelpers.cpp @@ -39,26 +39,23 @@ #include "qdomhelpers_p.h" #include "qdom_p.h" +#include "qxmlstream.h" #include "private/qxml_p.h" QT_BEGIN_NAMESPACE +#if QT_DEPRECATED_SINCE(5, 15) + /************************************************************** * * QDomHandler * **************************************************************/ - +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED QDomHandler::QDomHandler(QDomDocumentPrivate *adoc, QXmlSimpleReader *areader, bool namespaceProcessing) - : errorLine(0), - errorColumn(0), - doc(adoc), - node(adoc), - cdata(false), - nsProcessing(namespaceProcessing), - locator(nullptr), - reader(areader) + : cdata(false), reader(areader), domBuilder(adoc, &locator, namespaceProcessing) { } @@ -66,13 +63,183 @@ QDomHandler::~QDomHandler() {} bool QDomHandler::endDocument() { + return domBuilder.endDocument(); +} + +bool QDomHandler::startDTD(const QString &name, const QString &publicId, const QString &systemId) +{ + return domBuilder.startDTD(name, publicId, systemId); +} + +bool QDomHandler::startElement(const QString &nsURI, const QString &, const QString &qName, + const QXmlAttributes &atts) +{ + return domBuilder.startElement(nsURI, qName, atts); +} + +bool QDomHandler::endElement(const QString &, const QString &, const QString &) +{ + return domBuilder.endElement(); +} + +bool QDomHandler::characters(const QString &ch) +{ + return domBuilder.characters(ch, cdata); +} + +bool QDomHandler::processingInstruction(const QString &target, const QString &data) +{ + return domBuilder.processingInstruction(target, data); +} + +bool QDomHandler::skippedEntity(const QString &name) +{ + // we can only handle inserting entity references into content + if (reader && !reader->d_ptr->skipped_entity_in_content) + return true; + + return domBuilder.skippedEntity(name); +} + +bool QDomHandler::fatalError(const QXmlParseException &exception) +{ + domBuilder.errorMsg = exception.message(); + domBuilder.errorLine = exception.lineNumber(); + domBuilder.errorColumn = exception.columnNumber(); + return QXmlDefaultHandler::fatalError(exception); +} + +bool QDomHandler::startCDATA() +{ + cdata = true; + return true; +} + +bool QDomHandler::endCDATA() +{ + cdata = false; + return true; +} + +bool QDomHandler::startEntity(const QString &name) +{ + return domBuilder.startEntity(name); +} + +bool QDomHandler::endEntity(const QString &) +{ + return domBuilder.endEntity(); +} + +bool QDomHandler::comment(const QString &ch) +{ + return domBuilder.comment(ch); +} + +bool QDomHandler::unparsedEntityDecl(const QString &name, const QString &publicId, + const QString &systemId, const QString ¬ationName) +{ + return domBuilder.unparsedEntityDecl(name, publicId, systemId, notationName); +} + +bool QDomHandler::externalEntityDecl(const QString &name, const QString &publicId, + const QString &systemId) +{ + return unparsedEntityDecl(name, publicId, systemId, QString()); +} + +bool QDomHandler::notationDecl(const QString &name, const QString &publicId, + const QString &systemId) +{ + return domBuilder.notationDecl(name, publicId, systemId); +} + +void QDomHandler::setDocumentLocator(QXmlLocator *locator) +{ + this->locator.setLocator(locator); +} + +QDomBuilder::ErrorInfo QDomHandler::errorInfo() const +{ + return domBuilder.error(); +} +QT_WARNING_POP + +#endif // QT_DEPRECATED_SINCE(5, 15) + +/************************************************************** + * + * QXmlDocumentLocators + * + **************************************************************/ + +int QDomDocumentLocator::column() const +{ + Q_ASSERT(reader); + return static_cast<int>(reader->columnNumber()); +} + +int QDomDocumentLocator::line() const +{ + Q_ASSERT(reader); + return static_cast<int>(reader->lineNumber()); +} + +#if QT_DEPRECATED_SINCE(5, 15) + +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED +void QSAXDocumentLocator::setLocator(QXmlLocator *l) +{ + locator = l; +} +QT_WARNING_POP + +int QSAXDocumentLocator::column() const +{ + if (!locator) + return 0; + + return static_cast<int>(locator->columnNumber()); +} + +int QSAXDocumentLocator::line() const +{ + if (!locator) + return 0; + + return static_cast<int>(locator->lineNumber()); +} + +#endif // QT_DEPRECATED_SINCE(5, 15) + +/************************************************************** + * + * QDomBuilder + * + **************************************************************/ + +QDomBuilder::QDomBuilder(QDomDocumentPrivate *d, QXmlDocumentLocator *l, bool namespaceProcessing) + : errorLine(0), + errorColumn(0), + doc(d), + node(d), + locator(l), + nsProcessing(namespaceProcessing) +{ +} + +QDomBuilder::~QDomBuilder() {} + +bool QDomBuilder::endDocument() +{ // ### is this really necessary? (rms) if (node != doc) return false; return true; } -bool QDomHandler::startDTD(const QString &name, const QString &publicId, const QString &systemId) +bool QDomBuilder::startDTD(const QString &name, const QString &publicId, const QString &systemId) { doc->doctype()->name = name; doc->doctype()->publicId = publicId; @@ -80,7 +247,11 @@ bool QDomHandler::startDTD(const QString &name, const QString &publicId, const Q return true; } -bool QDomHandler::startElement(const QString &nsURI, const QString &, const QString &qName, +#if QT_DEPRECATED_SINCE(5, 15) + +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED +bool QDomBuilder::startElement(const QString &nsURI, const QString &qName, const QXmlAttributes &atts) { // tag name @@ -94,24 +265,65 @@ bool QDomHandler::startElement(const QString &nsURI, const QString &, const QStr if (!n) return false; - n->setLocation(locator->lineNumber(), locator->columnNumber()); + n->setLocation(locator->line(), locator->column()); node->appendChild(n); node = n; // attributes for (int i = 0; i < atts.length(); i++) { + auto domElement = static_cast<QDomElementPrivate *>(node); + if (nsProcessing) + domElement->setAttributeNS(atts.uri(i), atts.qName(i), atts.value(i)); + else + domElement->setAttribute(atts.qName(i), atts.value(i)); + } + + return true; +} +QT_WARNING_POP + +#endif // QT_DEPRECATED_SINCE(5, 15) + +inline QString stringRefToString(const QStringRef &stringRef) +{ + // Calling QStringRef::toString() on a NULL QStringRef in some cases returns + // an empty string (i.e. QString("")) instead of a NULL string (i.e. QString()). + // QDom implementation differentiates between NULL and empty strings, so + // we need this as workaround to keep the current behavior unchanged. + return stringRef.isNull() ? QString() : stringRef.toString(); +} + +bool QDomBuilder::startElement(const QString &nsURI, const QString &qName, + const QXmlStreamAttributes &atts) +{ + QDomNodePrivate *n = + nsProcessing ? doc->createElementNS(nsURI, qName) : doc->createElement(qName); + if (!n) + return false; + + n->setLocation(locator->line(), locator->column()); + + node->appendChild(n); + node = n; + + // attributes + for (const auto &attr : atts) { + auto domElement = static_cast<QDomElementPrivate *>(node); if (nsProcessing) { - ((QDomElementPrivate *)node)->setAttributeNS(atts.uri(i), atts.qName(i), atts.value(i)); + domElement->setAttributeNS(stringRefToString(attr.namespaceUri()), + stringRefToString(attr.qualifiedName()), + stringRefToString(attr.value())); } else { - ((QDomElementPrivate *)node)->setAttribute(atts.qName(i), atts.value(i)); + domElement->setAttribute(stringRefToString(attr.qualifiedName()), + stringRefToString(attr.value())); } } return true; } -bool QDomHandler::endElement(const QString &, const QString &, const QString &) +bool QDomBuilder::endElement() { if (!node || node == doc) return false; @@ -120,7 +332,7 @@ bool QDomHandler::endElement(const QString &, const QString &, const QString &) return true; } -bool QDomHandler::characters(const QString &ch) +bool QDomBuilder::characters(const QString &characters, bool cdata) { // No text as child of some document if (node == doc) @@ -128,91 +340,79 @@ bool QDomHandler::characters(const QString &ch) QScopedPointer<QDomNodePrivate> n; if (cdata) { - n.reset(doc->createCDATASection(ch)); + n.reset(doc->createCDATASection(characters)); } else if (!entityName.isEmpty()) { QScopedPointer<QDomEntityPrivate> e( new QDomEntityPrivate(doc, nullptr, entityName, QString(), QString(), QString())); - e->value = ch; + e->value = characters; e->ref.deref(); doc->doctype()->appendChild(e.data()); e.take(); n.reset(doc->createEntityReference(entityName)); } else { - n.reset(doc->createTextNode(ch)); + n.reset(doc->createTextNode(characters)); } - n->setLocation(locator->lineNumber(), locator->columnNumber()); + n->setLocation(locator->line(), locator->column()); node->appendChild(n.data()); n.take(); return true; } -bool QDomHandler::processingInstruction(const QString &target, const QString &data) +bool QDomBuilder::processingInstruction(const QString &target, const QString &data) { QDomNodePrivate *n; n = doc->createProcessingInstruction(target, data); if (n) { - n->setLocation(locator->lineNumber(), locator->columnNumber()); + n->setLocation(locator->line(), locator->column()); node->appendChild(n); return true; } else return false; } -bool QDomHandler::skippedEntity(const QString &name) +bool QDomBuilder::skippedEntity(const QString &name) { - // we can only handle inserting entity references into content - if (reader && !reader->d_ptr->skipped_entity_in_content) - return true; - QDomNodePrivate *n = doc->createEntityReference(name); - n->setLocation(locator->lineNumber(), locator->columnNumber()); + n->setLocation(locator->line(), locator->column()); node->appendChild(n); return true; } -bool QDomHandler::fatalError(const QXmlParseException &exception) +void QDomBuilder::fatalError(const QString &message) { - errorMsg = exception.message(); - errorLine = exception.lineNumber(); - errorColumn = exception.columnNumber(); - return QXmlDefaultHandler::fatalError(exception); + errorMsg = message; + errorLine = static_cast<int>(locator->line()); + errorColumn = static_cast<int>(locator->column()); } -bool QDomHandler::startCDATA() +QDomBuilder::ErrorInfo QDomBuilder::error() const { - cdata = true; - return true; + return ErrorInfo(errorMsg, errorLine, errorColumn); } -bool QDomHandler::endCDATA() -{ - cdata = false; - return true; -} - -bool QDomHandler::startEntity(const QString &name) +bool QDomBuilder::startEntity(const QString &name) { entityName = name; return true; } -bool QDomHandler::endEntity(const QString &) +bool QDomBuilder::endEntity() { entityName.clear(); return true; } -bool QDomHandler::comment(const QString &ch) +bool QDomBuilder::comment(const QString &characters) { QDomNodePrivate *n; - n = doc->createComment(ch); - n->setLocation(locator->lineNumber(), locator->columnNumber()); + n = doc->createComment(characters); + n->setLocation(locator->line(), locator->column()); node->appendChild(n); return true; } -bool QDomHandler::unparsedEntityDecl(const QString &name, const QString &publicId, +bool QDomBuilder::unparsedEntityDecl(const QString &name, const QString &publicId, const QString &systemId, const QString ¬ationName) { QDomEntityPrivate *e = @@ -223,13 +423,13 @@ bool QDomHandler::unparsedEntityDecl(const QString &name, const QString &publicI return true; } -bool QDomHandler::externalEntityDecl(const QString &name, const QString &publicId, +bool QDomBuilder::externalEntityDecl(const QString &name, const QString &publicId, const QString &systemId) { return unparsedEntityDecl(name, publicId, systemId, QString()); } -bool QDomHandler::notationDecl(const QString &name, const QString &publicId, +bool QDomBuilder::notationDecl(const QString &name, const QString &publicId, const QString &systemId) { QDomNotationPrivate *n = new QDomNotationPrivate(doc, nullptr, name, publicId, systemId); @@ -239,9 +439,225 @@ bool QDomHandler::notationDecl(const QString &name, const QString &publicId, return true; } -void QDomHandler::setDocumentLocator(QXmlLocator *locator) +/************************************************************** + * + * QDomParser + * + **************************************************************/ + +QDomParser::QDomParser(QDomDocumentPrivate *d, QXmlStreamReader *r, bool namespaceProcessing) + : reader(r), locator(r), domBuilder(d, &locator, namespaceProcessing) +{ +} + +bool QDomParser::parse() +{ + return parseProlog() && parseBody(); +} + +QDomBuilder::ErrorInfo QDomParser::errorInfo() const +{ + return domBuilder.error(); +} + +bool QDomParser::parseProlog() +{ + Q_ASSERT(reader); + + bool foundDtd = false; + + while (!reader->atEnd()) { + reader->readNext(); + + if (reader->hasError()) { + domBuilder.fatalError(reader->errorString()); + return false; + } + + switch (reader->tokenType()) { + case QXmlStreamReader::StartDocument: + if (!reader->documentVersion().isEmpty()) { + QString value(QLatin1String("version='")); + value += reader->documentVersion(); + value += QLatin1Char('\''); + if (!reader->documentEncoding().isEmpty()) { + value += QLatin1String(" encoding='"); + value += reader->documentEncoding(); + value += QLatin1Char('\''); + } + if (reader->isStandaloneDocument()) { + value += QLatin1String(" standalone='yes'"); + } else { + // TODO: Add standalone='no', if 'standalone' is specified. With the current + // QXmlStreamReader there is no way to figure out if it was specified or not. + // QXmlStreamReader needs to be modified for handling that case correctly. + } + + if (!domBuilder.processingInstruction(QLatin1String("xml"), value)) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing XML declaration")); + return false; + } + } + break; + case QXmlStreamReader::DTD: + if (foundDtd) { + domBuilder.fatalError(QDomParser::tr("Multiple DTD sections are not allowed")); + return false; + } + foundDtd = true; + + if (!domBuilder.startDTD(stringRefToString(reader->dtdName()), + stringRefToString(reader->dtdPublicId()), + stringRefToString(reader->dtdSystemId()))) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing document type declaration")); + return false; + } + if (!parseMarkupDecl()) + return false; + break; + case QXmlStreamReader::Comment: + if (!domBuilder.comment(reader->text().toString())) { + domBuilder.fatalError(QDomParser::tr("Error occurred while processing comment")); + return false; + } + break; + case QXmlStreamReader::ProcessingInstruction: + if (!domBuilder.processingInstruction(reader->processingInstructionTarget().toString(), + reader->processingInstructionData().toString())) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing a processing instruction")); + return false; + } + break; + default: + // If the token is none of the above, prolog processing is done. + return true; + } + } + + return true; +} + +bool QDomParser::parseBody() { - this->locator = locator; + Q_ASSERT(reader); + + std::stack<QStringRef> tagStack; + while (!reader->atEnd() && !reader->hasError()) { + switch (reader->tokenType()) { + case QXmlStreamReader::StartElement: + tagStack.push(reader->qualifiedName()); + if (!domBuilder.startElement(stringRefToString(reader->namespaceUri()), + stringRefToString(reader->qualifiedName()), + reader->attributes())) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing a start element")); + return false; + } + break; + case QXmlStreamReader::EndElement: + if (tagStack.empty() || reader->qualifiedName() != tagStack.top()) { + domBuilder.fatalError( + QDomParser::tr("Unexpected end element '%1'").arg(reader->name())); + return false; + } + tagStack.pop(); + if (!domBuilder.endElement()) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing an end element")); + return false; + } + break; + case QXmlStreamReader::Characters: + if (!reader->isWhitespace()) { // Skip the content consisting of only whitespaces + if (!reader->text().toString().trimmed().isEmpty()) { + if (!domBuilder.characters(reader->text().toString(), reader->isCDATA())) { + domBuilder.fatalError(QDomParser::tr( + "Error occurred while processing the element content")); + return false; + } + } + } + break; + case QXmlStreamReader::Comment: + if (!domBuilder.comment(reader->text().toString())) { + domBuilder.fatalError(QDomParser::tr("Error occurred while processing comments")); + return false; + } + break; + case QXmlStreamReader::ProcessingInstruction: + if (!domBuilder.processingInstruction(reader->processingInstructionTarget().toString(), + reader->processingInstructionData().toString())) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing a processing instruction")); + return false; + } + break; + case QXmlStreamReader::EntityReference: + if (!domBuilder.skippedEntity(reader->name().toString())) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing an entity reference")); + return false; + } + break; + default: + domBuilder.fatalError(QDomParser::tr("Unexpected token")); + return false; + } + + reader->readNext(); + } + + if (reader->hasError()) { + domBuilder.fatalError(reader->errorString()); + reader->readNext(); + return false; + } + + if (!tagStack.empty()) { + domBuilder.fatalError(QDomParser::tr("Tag mismatch")); + return false; + } + + return true; +} + +bool QDomParser::parseMarkupDecl() +{ + Q_ASSERT(reader); + + const auto entities = reader->entityDeclarations(); + for (const auto &entityDecl : entities) { + // Entity declarations are created only for Extrenal Entities. Internal Entities + // are parsed, and QXmlStreamReader handles the parsing itself and returns the + // parsed result. So we don't need to do anything for the Internal Entities. + if (!entityDecl.publicId().isEmpty() || !entityDecl.systemId().isEmpty()) { + // External Entity + if (!domBuilder.unparsedEntityDecl(stringRefToString(entityDecl.name()), + stringRefToString(entityDecl.publicId()), + stringRefToString(entityDecl.systemId()), + stringRefToString(entityDecl.notationName()))) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing entity declaration")); + return false; + } + } + } + + const auto notations = reader->notationDeclarations(); + for (const auto ¬ationDecl : notations) { + if (!domBuilder.notationDecl(stringRefToString(notationDecl.name()), + stringRefToString(notationDecl.publicId()), + stringRefToString(notationDecl.systemId()))) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing notation declaration")); + return false; + } + } + + return true; } QT_END_NAMESPACE diff --git a/src/xml/dom/qdomhelpers_p.h b/src/xml/dom/qdomhelpers_p.h index ee81f1f9ce8..4de18f7d4dd 100644 --- a/src/xml/dom/qdomhelpers_p.h +++ b/src/xml/dom/qdomhelpers_p.h @@ -39,6 +39,7 @@ #ifndef QDOMHELPERS_P_H #define QDOMHELPERS_P_H +#include <qcoreapplication.h> #include <qglobal.h> #include <qxml.h> @@ -57,6 +58,115 @@ QT_BEGIN_NAMESPACE class QDomDocumentPrivate; class QDomNodePrivate; +class QXmlStreamReader; +class QXmlStreamAttributes; + +/************************************************************** + * + * QXmlDocumentLocators + * + **************************************************************/ + +/* TODO: QXmlDocumentLocator can be removed when the SAX-based + * implementation is removed. Right now it is needed for QDomBuilder + * to work with both QXmlStreamReader and QXmlInputSource (SAX) + * based implementations. + */ +class QXmlDocumentLocator +{ +public: + virtual ~QXmlDocumentLocator() = default; + virtual int column() const = 0; + virtual int line() const = 0; +}; + +class QDomDocumentLocator : public QXmlDocumentLocator +{ +public: + QDomDocumentLocator(QXmlStreamReader *r) : reader(r) {} + ~QDomDocumentLocator() override = default; + + int column() const override; + int line() const override; + +private: + QXmlStreamReader *reader; +}; + +#if QT_DEPRECATED_SINCE(5, 15) + +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED + +class QSAXDocumentLocator : public QXmlDocumentLocator +{ +public: + ~QSAXDocumentLocator() override = default; + + int column() const override; + int line() const override; + + void setLocator(QXmlLocator *l); + +private: + QXmlLocator *locator = nullptr; +}; + +QT_WARNING_POP + +#endif + +/************************************************************** + * + * QDomBuilder + * + **************************************************************/ + +class QDomBuilder +{ +public: + QDomBuilder(QDomDocumentPrivate *d, QXmlDocumentLocator *l, bool namespaceProcessing); + ~QDomBuilder(); + + bool endDocument(); +#if QT_DEPRECATED_SINCE(5, 15) +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED + bool startElement(const QString &nsURI, const QString &qName, const QXmlAttributes &atts); +QT_WARNING_POP +#endif + bool startElement(const QString &nsURI, const QString &qName, const QXmlStreamAttributes &atts); + bool endElement(); + bool characters(const QString &characters, bool cdata = false); + bool processingInstruction(const QString &target, const QString &data); + bool skippedEntity(const QString &name); + bool startEntity(const QString &name); + bool endEntity(); + bool startDTD(const QString &name, const QString &publicId, const QString &systemId); + bool comment(const QString &characters); + bool externalEntityDecl(const QString &name, const QString &publicId, const QString &systemId); + bool notationDecl(const QString &name, const QString &publicId, const QString &systemId); + bool unparsedEntityDecl(const QString &name, const QString &publicId, const QString &systemId, + const QString ¬ationName); + + void fatalError(const QString &message); + + using ErrorInfo = std::tuple<QString, int, int>; + ErrorInfo error() const; + + QString errorMsg; + int errorLine; + int errorColumn; + +private: + QDomDocumentPrivate *doc; + QDomNodePrivate *node; + QXmlDocumentLocator *locator; + QString entityName; + bool nsProcessing; +}; + +#if QT_DEPRECATED_SINCE(5, 15) /************************************************************** * @@ -64,11 +174,14 @@ class QDomNodePrivate; * **************************************************************/ +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED + class QDomHandler : public QXmlDefaultHandler { public: QDomHandler(QDomDocumentPrivate *d, QXmlSimpleReader *reader, bool namespaceProcessing); - ~QDomHandler(); + ~QDomHandler() override; // content handler bool endDocument() override; @@ -102,18 +215,42 @@ public: void setDocumentLocator(QXmlLocator *locator) override; - QString errorMsg; - int errorLine; - int errorColumn; + QDomBuilder::ErrorInfo errorInfo() const; private: - QDomDocumentPrivate *doc; - QDomNodePrivate *node; - QString entityName; bool cdata; - bool nsProcessing; - QXmlLocator *locator; QXmlSimpleReader *reader; + QSAXDocumentLocator locator; + QDomBuilder domBuilder; +}; + +QT_WARNING_POP + +#endif // QT_DEPRECATED_SINCE(5, 15) + +/************************************************************** + * + * QDomParser + * + **************************************************************/ + +class QDomParser +{ + Q_DECLARE_TR_FUNCTIONS(QDomParser) +public: + QDomParser(QDomDocumentPrivate *d, QXmlStreamReader *r, bool namespaceProcessing); + + bool parse(); + QDomBuilder::ErrorInfo errorInfo() const; + +private: + bool parseProlog(); + bool parseBody(); + bool parseMarkupDecl(); + + QXmlStreamReader *reader; + QDomDocumentLocator locator; + QDomBuilder domBuilder; }; QT_END_NAMESPACE |