QDomDocument: deprecate old setContent() overloads in favor of new ones

And use the new overloads in examples and tests.

[ChangeLog][QtXml][QDomDocument] Deprecated the old setContent()
overloads in favor of the new ones that take ParseOptions and
ParseError.

Task-number: QTBUG-104507
Change-Id: I61b37eba2fe3002c03bddc90f6877676d539f7ec
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Sona Kurazyan 2022-06-30 13:34:44 +02:00
parent 2f7a78cd7b
commit e684c25b85
4 changed files with 69 additions and 49 deletions

View File

@ -53,17 +53,14 @@ void XbelTree::contextMenuEvent(QContextMenuEvent *event)
bool XbelTree::read(QIODevice *device) bool XbelTree::read(QIODevice *device)
{ {
QString errorStr; QDomDocument::ParseResult result =
int errorLine; domDocument.setContent(device, QDomDocument::ParseOption::UseNamespaceProcessing);
int errorColumn; if (!result) {
if (!domDocument.setContent(device, true, &errorStr, &errorLine,
&errorColumn)) {
QMessageBox::information(window(), tr("DOM Bookmarks"), QMessageBox::information(window(), tr("DOM Bookmarks"),
tr("Parse error at line %1, column %2:\n%3") tr("Parse error at line %1, column %2:\n%3")
.arg(errorLine) .arg(result.errorLine)
.arg(errorColumn) .arg(result.errorColumn)
.arg(errorStr)); .arg(result.errorMessage));
return false; return false;
} }

View File

@ -6031,8 +6031,12 @@ QDomDocument::~QDomDocument()
{ {
} }
#if QT_DEPRECATED_SINCE(6, 8)
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
/*! /*!
\overload \overload
\deprecated [6.8] Use the overloads taking ParseOptions instead.
This function reads the XML document from the string \a text, returning This function reads the XML document from the string \a text, returning
true if the content was successfully parsed; otherwise returns \c false. true if the content was successfully parsed; otherwise returns \c false.
@ -6048,6 +6052,8 @@ bool QDomDocument::setContent(const QString& text, bool namespaceProcessing,
} }
/*! /*!
\deprecated [6.8] Use the overload taking ParseOptions instead.
This function parses the XML document from the byte array \a This function parses the XML document from the byte array \a
data and sets it as the content of the document. It tries to data and sets it as the content of the document. It tries to
detect the encoding of the document as required by the XML detect the encoding of the document as required by the XML
@ -6125,6 +6131,7 @@ static inline void unpackParseResult(const QDomDocument::ParseResult &parseResul
/*! /*!
\overload \overload
\deprecated [6.8] Use the overload taking ParseOptions instead.
This function reads the XML document from the IO device \a dev, returning This function reads the XML document from the IO device \a dev, returning
true if the content was successfully parsed; otherwise returns \c false. true if the content was successfully parsed; otherwise returns \c false.
@ -6144,6 +6151,7 @@ bool QDomDocument::setContent(QIODevice* dev, bool namespaceProcessing,
/*! /*!
\overload \overload
\deprecated [6.8] Use the overload returning ParseResult instead.
This function reads the XML document from the string \a text, returning This function reads the XML document from the string \a text, returning
true if the content was successfully parsed; otherwise returns \c false. true if the content was successfully parsed; otherwise returns \c false.
@ -6159,6 +6167,7 @@ bool QDomDocument::setContent(const QString& text, QString *errorMsg, int *error
/*! /*!
\overload \overload
\deprecated [6.8] Use the overload returning ParseResult instead.
This function reads the XML document from the byte array \a buffer, This function reads the XML document from the byte array \a buffer,
returning true if the content was successfully parsed; otherwise returns returning true if the content was successfully parsed; otherwise returns
@ -6173,7 +6182,7 @@ bool QDomDocument::setContent(const QByteArray& buffer, QString *errorMsg, int *
/*! /*!
\overload \overload
\deprecated \deprecated [6.8] Use the overload returning ParseResult instead.
This function reads the XML document from the IO device \a dev, returning This function reads the XML document from the IO device \a dev, returning
true if the content was successfully parsed; otherwise returns \c false. true if the content was successfully parsed; otherwise returns \c false.
@ -6188,6 +6197,7 @@ bool QDomDocument::setContent(QIODevice* dev, QString *errorMsg, int *errorLine,
/*! /*!
\overload \overload
\since 5.15 \since 5.15
\deprecated [6.8] Use the overload taking ParseOptions instead.
This function reads the XML document from the QXmlStreamReader \a reader This function reads the XML document from the QXmlStreamReader \a reader
and parses it. Returns \c true if the content was successfully parsed; and parses it. Returns \c true if the content was successfully parsed;
@ -6211,6 +6221,8 @@ bool QDomDocument::setContent(QXmlStreamReader *reader, bool namespaceProcessing
unpackParseResult(result, errorMsg, errorLine, errorColumn); unpackParseResult(result, errorMsg, errorLine, errorColumn);
return bool(result); return bool(result);
} }
QT_WARNING_POP
#endif // QT_DEPRECATED_SINCE(6, 8)
/*! /*!
\enum QDomDocument::ParseOption \enum QDomDocument::ParseOption

View File

@ -313,14 +313,23 @@ public:
inline QDomNode::NodeType nodeType() const { return DocumentNode; } inline QDomNode::NodeType nodeType() const { return DocumentNode; }
// Qt extensions // Qt extensions
#if QT_DEPRECATED_SINCE(6, 8)
QT_DEPRECATED_VERSION_X_6_8("Use the overload taking ParseOptions instead.")
bool setContent(const QByteArray &text, bool namespaceProcessing, QString *errorMsg = nullptr, int *errorLine = nullptr, int *errorColumn = nullptr); bool setContent(const QByteArray &text, bool namespaceProcessing, QString *errorMsg = nullptr, int *errorLine = nullptr, int *errorColumn = nullptr);
QT_DEPRECATED_VERSION_X_6_8("Use the overload taking ParseOptions instead.")
bool setContent(const QString &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);
QT_DEPRECATED_VERSION_X_6_8("Use the overload taking ParseOptions instead.")
bool setContent(QIODevice *dev, 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);
QT_DEPRECATED_VERSION_X_6_8("Use the overload returning ParseResult instead.")
bool setContent(const QByteArray &text, QString *errorMsg, int *errorLine = nullptr, int *errorColumn = nullptr); bool setContent(const QByteArray &text, QString *errorMsg, int *errorLine = nullptr, int *errorColumn = nullptr);
QT_DEPRECATED_VERSION_X_6_8("Use the overload returning ParseResult instead.")
bool setContent(const QString &text, QString *errorMsg, int *errorLine = nullptr, int *errorColumn = nullptr); bool setContent(const QString &text, QString *errorMsg, int *errorLine = nullptr, int *errorColumn = nullptr);
QT_DEPRECATED_VERSION_X_6_8("Use the overload returning ParseResult instead.")
bool setContent(QIODevice *dev, QString *errorMsg, int *errorLine = nullptr, int *errorColumn = nullptr); bool setContent(QIODevice *dev, QString *errorMsg, int *errorLine = nullptr, int *errorColumn = nullptr);
QT_DEPRECATED_VERSION_X_6_8("Use the overload taking ParseOptions instead.")
bool setContent(QXmlStreamReader *reader, bool namespaceProcessing, QString *errorMsg = nullptr, bool setContent(QXmlStreamReader *reader, bool namespaceProcessing, QString *errorMsg = nullptr,
int *errorLine = nullptr, int *errorColumn = nullptr); int *errorLine = nullptr, int *errorColumn = nullptr);
#endif // QT_DEPRECATED_SINCE(6, 8)
Q_WEAK_OVERLOAD Q_WEAK_OVERLOAD
ParseResult setContent(const QByteArray &data, ParseOptions options = ParseOption::Default) ParseResult setContent(const QByteArray &data, ParseOptions options = ParseOption::Default)

View File

@ -178,7 +178,11 @@ void tst_QDom::setContent()
QDomDocument domDoc; QDomDocument domDoc;
QXmlStreamReader reader(doc); QXmlStreamReader reader(doc);
QVERIFY(domDoc.setContent(&reader, true)); #if QT_DEPRECATED_SINCE(6, 8)
QT_IGNORE_DEPRECATIONS(domDoc.setContent(&reader, true);)
#else
QVERIFY(domDoc.setContent(&reader, QDomDocument::ParseOption::UseNamespaceProcessing));
#endif // QT_DEPRECATED_SINCE(6, 8)
QString eRes; QString eRes;
QTextStream ts( &eRes, QIODevice::WriteOnly ); QTextStream ts( &eRes, QIODevice::WriteOnly );
@ -240,6 +244,9 @@ void tst_QDom::setContentOverloads()
QVERIFY(doc.setContent(&buffer, QDomDocument::ParseOption::Default)); QVERIFY(doc.setContent(&buffer, QDomDocument::ParseOption::Default));
buffer.reset(); buffer.reset();
#if QT_DEPRECATED_SINCE(6, 8)
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
// With output param // With output param
QVERIFY(doc.setContent(data, &errorMessage)); QVERIFY(doc.setContent(data, &errorMessage));
QVERIFY(doc.setContent(text, &errorMessage)); QVERIFY(doc.setContent(text, &errorMessage));
@ -261,6 +268,8 @@ void tst_QDom::setContentOverloads()
QVERIFY(doc.setContent(&reader, false, &errorMessage)); QVERIFY(doc.setContent(&reader, false, &errorMessage));
QVERIFY(doc.setContent(&buffer, false, &errorMessage)); QVERIFY(doc.setContent(&buffer, false, &errorMessage));
buffer.reset(); buffer.reset();
QT_WARNING_POP
#endif // QT_DEPRECATED_SINCE(6, 8)
} }
void tst_QDom::parseOptions() void tst_QDom::parseOptions()
@ -409,13 +418,11 @@ void tst_QDom::toString_01()
QVERIFY2(f.open(QIODevice::ReadOnly), qPrintable(QString::fromLatin1("Failed to open file %1, file error: %2").arg(fileName).arg(f.error()))); QVERIFY2(f.open(QIODevice::ReadOnly), qPrintable(QString::fromLatin1("Failed to open file %1, file error: %2").arg(fileName).arg(f.error())));
QDomDocument doc; QDomDocument doc;
QString errorMsg; QDomDocument::ParseResult result = doc.setContent(&f, QDomDocument::ParseOption::Default);
int errorLine; QVERIFY2(result,
int errorCol; qPrintable(u"QDomDocument::setContent() failed (%1:%2): %3"_s.arg(result.errorLine)
.arg(result.errorColumn)
QVERIFY(doc.setContent( &f, &errorMsg, &errorLine, &errorCol )); /*, .arg(result.errorMessage)));
QString("QDomDocument::setContent() failed: %1 in line %2, column %3")
.arg( errorMsg ).arg( errorLine ).arg( errorCol )); */
// test toString()'s invariant with different indenting depths // test toString()'s invariant with different indenting depths
for ( int i=0; i<5; i++ ) { for ( int i=0; i<5; i++ ) {
QString toStr = doc.toString( i ); QString toStr = doc.toString( i );
@ -675,17 +682,14 @@ void tst_QDom::saveWithSerialization() const
QVERIFY(readDevice.open(QIODevice::ReadOnly)); QVERIFY(readDevice.open(QIODevice::ReadOnly));
QDomDocument result; QDomDocument result;
QDomDocument::ParseResult parseResult =
result.setContent(&readDevice, QDomDocument::ParseOption::Default);
QString msg; QVERIFY2(parseResult,
int line = 0;
int column = 0;
QVERIFY2(result.setContent(&readDevice, &msg, &line, &column),
qPrintable(QString::fromLatin1("Failed: line %2, column %3: %4, content: %5") qPrintable(QString::fromLatin1("Failed: line %2, column %3: %4, content: %5")
.arg(QString::number(line), .arg(QString::number(parseResult.errorLine),
QString::number(column), QString::number(parseResult.errorColumn),
msg, parseResult.errorMessage, QString::fromUtf8(storage))));
QString::fromUtf8(storage))));
if (!compareDocuments(doc, result)) if (!compareDocuments(doc, result))
{ {
QCOMPARE(doc.toString(), result.toString()); QCOMPARE(doc.toString(), result.toString());
@ -1586,10 +1590,8 @@ void tst_QDom::nonBMPCharacters()
const QString input = u"<text>Supplementary Plane: 𝄞 😂 🀄 🀶 🃪 🃋</text>"_qs; const QString input = u"<text>Supplementary Plane: 𝄞 😂 🀄 🀶 🃪 🃋</text>"_qs;
QString errorMsg;
QDomDocument doc; QDomDocument doc;
doc.setContent(input, &errorMsg); QVERIFY(doc.setContent(input, QDomDocument::ParseOption::Default));
QVERIFY(errorMsg.isEmpty());
QCOMPARE(doc.toString(-1), input); QCOMPARE(doc.toString(-1), input);
} }
@ -1631,10 +1633,8 @@ void tst_QDom::roundTripCDATA() const
{ {
const QString input = u"<?xml version='1.0' encoding='UTF-8'?>\n" const QString input = u"<?xml version='1.0' encoding='UTF-8'?>\n"
"<content><![CDATA[]]></content>\n"_s; "<content><![CDATA[]]></content>\n"_s;
QString errorMsg;
QDomDocument doc; QDomDocument doc;
QVERIFY(doc.setContent(input, false, &errorMsg)); QVERIFY(doc.setContent(input, QDomDocument::ParseOption::Default));
QVERIFY(errorMsg.isEmpty());
QCOMPARE(doc.toString(), input); QCOMPARE(doc.toString(), input);
} }
@ -1646,7 +1646,7 @@ void tst_QDom::normalizeEndOfLine() const
QVERIFY(buffer.open(QIODevice::ReadOnly)); QVERIFY(buffer.open(QIODevice::ReadOnly));
QDomDocument doc; QDomDocument doc;
QVERIFY(doc.setContent(&buffer, true)); QVERIFY(doc.setContent(&buffer, QDomDocument::ParseOption::UseNamespaceProcessing));
const QString expected(QLatin1String("<a>\nc\nc\na\na</a>")); const QString expected(QLatin1String("<a>\nc\nc\na\na</a>"));
@ -1663,7 +1663,7 @@ void tst_QDom::normalizeAttributes() const
QVERIFY(buffer.open(QIODevice::ReadOnly)); QVERIFY(buffer.open(QIODevice::ReadOnly));
QDomDocument doc; QDomDocument doc;
QVERIFY(doc.setContent(&buffer, true)); QVERIFY(doc.setContent(&buffer, QDomDocument::ParseOption::UseNamespaceProcessing));
QCOMPARE(doc.documentElement().attribute(QLatin1String("attribute")), QString::fromLatin1("a a")); QCOMPARE(doc.documentElement().attribute(QLatin1String("attribute")), QString::fromLatin1("a a"));
} }
@ -1707,17 +1707,18 @@ void tst_QDom::serializeNamespaces() const
QDomDocument doc; QDomDocument doc;
QByteArray ba(input); QByteArray ba(input);
QXmlStreamReader streamReader(input); QXmlStreamReader streamReader(input);
QVERIFY(doc.setContent(&streamReader, true)); QVERIFY(doc.setContent(&streamReader, QDomDocument::ParseOption::UseNamespaceProcessing));
const QByteArray serialized(doc.toByteArray()); const QByteArray serialized(doc.toByteArray());
QDomDocument doc2; QDomDocument doc2;
QVERIFY(doc2.setContent(doc.toString(), true)); QVERIFY(doc2.setContent(doc.toString(), QDomDocument::ParseOption::UseNamespaceProcessing));
/* Here we test that it roundtrips. */ /* Here we test that it roundtrips. */
QVERIFY(isDeepEqual(doc2, doc)); QVERIFY(isDeepEqual(doc2, doc));
QDomDocument doc3; QDomDocument doc3;
QVERIFY(doc3.setContent(QString::fromLatin1(serialized.constData()), true)); QVERIFY(doc3.setContent(QString::fromLatin1(serialized.constData()),
QDomDocument::ParseOption::UseNamespaceProcessing));
QVERIFY(isDeepEqual(doc3, doc)); QVERIFY(isDeepEqual(doc3, doc));
} }
@ -1743,7 +1744,7 @@ void tst_QDom::flagUndeclaredNamespace() const
QDomDocument doc; QDomDocument doc;
QByteArray ba(input); QByteArray ba(input);
QXmlStreamReader streamReader(ba); QXmlStreamReader streamReader(ba);
QVERIFY(!doc.setContent(&streamReader, true)); QVERIFY(!doc.setContent(&streamReader, QDomDocument::ParseOption::UseNamespaceProcessing));
} }
void tst_QDom::indentComments() const void tst_QDom::indentComments() const
@ -1825,12 +1826,12 @@ void tst_QDom::namespacedAttributes() const
"</xan:td>\n"; "</xan:td>\n";
QDomDocument one("document"); QDomDocument one("document");
QString error; QDomDocument::ParseResult result =
bool docParsed = one.setContent(QByteArray(xml), true, &error); one.setContent(QByteArray(xml), QDomDocument::ParseOption::UseNamespaceProcessing);
QVERIFY2(docParsed, qPrintable(error)); QVERIFY2(result, qPrintable(result.errorMessage));
QDomDocument two("document2"); QDomDocument two("document2");
docParsed = two.setContent(one.toByteArray(2), true, &error); result = two.setContent(one.toByteArray(2), QDomDocument::ParseOption::UseNamespaceProcessing);
QVERIFY2(docParsed, qPrintable(error)); QVERIFY2(result, qPrintable(result.errorMessage));
QVERIFY(isDeepEqual(one, two)); QVERIFY(isDeepEqual(one, two));
} }
@ -1992,7 +1993,8 @@ void tst_QDom::crashInSetContent() const
QDomImplementation::setInvalidDataPolicy(QDomImplementation::ReturnNullNode); QDomImplementation::setInvalidDataPolicy(QDomImplementation::ReturnNullNode);
QDomDocument docImport; QDomDocument docImport;
QCOMPARE(docImport.setContent(QLatin1String("<a:>text</a:>"), true), false); QVERIFY(!docImport.setContent(QLatin1String("<a:>text</a:>"),
QDomDocument::ParseOption::UseNamespaceProcessing));
QVERIFY(docImport.setContent(QLatin1String("<?xml version=\"1.0\"?><e/>"))); QVERIFY(docImport.setContent(QLatin1String("<?xml version=\"1.0\"?><e/>")));
} }
@ -2007,7 +2009,7 @@ void tst_QDom::doubleNamespaceDeclarations() const
QVERIFY(file.open(QIODevice::ReadOnly)); QVERIFY(file.open(QIODevice::ReadOnly));
QXmlStreamReader streamReader(&file); QXmlStreamReader streamReader(&file);
QVERIFY(doc.setContent(&streamReader, true)); QVERIFY(doc.setContent(&streamReader, QDomDocument::ParseOption::UseNamespaceProcessing));
// tst_QDom relies on a specific QHash ordering, see QTBUG-25071 // tst_QDom relies on a specific QHash ordering, see QTBUG-25071
QString docAsString = doc.toString(0); QString docAsString = doc.toString(0);
@ -2024,7 +2026,7 @@ void tst_QDom::setContentQXmlReaderOverload() const
{ {
QDomDocument doc; QDomDocument doc;
QXmlStreamReader streamReader(QByteArray("<e/>")); QXmlStreamReader streamReader(QByteArray("<e/>"));
doc.setContent(&streamReader, true); doc.setContent(&streamReader, QDomDocument::ParseOption::UseNamespaceProcessing);
QCOMPARE(doc.documentElement().nodeName(), QString::fromLatin1("e")); QCOMPARE(doc.documentElement().nodeName(), QString::fromLatin1("e"));
} }
@ -2130,7 +2132,7 @@ void tst_QDom::setContentUnopenedQIODevice() const
// Note: the check below is expected to fail in Qt 7. // Note: the check below is expected to fail in Qt 7.
// Fix the test and remove the obsolete code from setContent(). // Fix the test and remove the obsolete code from setContent().
QVERIFY(doc.setContent(&buffer, true)); QVERIFY(doc.setContent(&buffer, QDomDocument::ParseOption::UseNamespaceProcessing));
QCOMPARE(doc.toString().trimmed(), data); QCOMPARE(doc.toString().trimmed(), data);
} }
@ -2236,7 +2238,7 @@ void tst_QDom::DTDInternalSubset() const
QFETCH( QString, internalSubset ); QFETCH( QString, internalSubset );
QXmlStreamReader reader(doc); QXmlStreamReader reader(doc);
QDomDocument document; QDomDocument document;
QVERIFY(document.setContent(&reader, true)); QVERIFY(document.setContent(&reader, QDomDocument::ParseOption::UseNamespaceProcessing));
QCOMPARE(document.doctype().internalSubset(), internalSubset); QCOMPARE(document.doctype().internalSubset(), internalSubset);
} }