diff options
Diffstat (limited to 'sources/shiboken6/ApiExtractor/xmlutils_libxslt.cpp')
-rw-r--r-- | sources/shiboken6/ApiExtractor/xmlutils_libxslt.cpp | 79 |
1 files changed, 28 insertions, 51 deletions
diff --git a/sources/shiboken6/ApiExtractor/xmlutils_libxslt.cpp b/sources/shiboken6/ApiExtractor/xmlutils_libxslt.cpp index e1e185130..5a9a26913 100644 --- a/sources/shiboken6/ApiExtractor/xmlutils_libxslt.cpp +++ b/sources/shiboken6/ApiExtractor/xmlutils_libxslt.cpp @@ -1,34 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2019 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of Qt for Python. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "xmlutils_libxslt.h" #include "xmlutils.h" +#include "qtcompat.h" + #include <QtCore/QByteArray> #include <QtCore/QCoreApplication> #include <QtCore/QDir> @@ -44,6 +21,8 @@ #include <cstdlib> #include <memory> +using namespace Qt::StringLiterals; + static void cleanup() { xsltCleanupGlobals(); @@ -61,8 +40,6 @@ static void ensureInitialized() } } -namespace { - // RAI Helpers for cleaning up libxml2/libxslt data struct XmlDocDeleter // for std::unique_ptr<xmlDoc> @@ -85,8 +62,6 @@ struct XmlXPathContextDeleter void operator()(xmlXPathContextPtr xPathContext) { xmlXPathFreeContext(xPathContext); } }; -} // namespace - using XmlDocUniquePtr = std::unique_ptr<xmlDoc, XmlDocDeleter>; using XmlPathObjectUniquePtr = std::unique_ptr<xmlXPathObject, XmlXPathObjectDeleter>; using XmlStyleSheetUniquePtr = std::unique_ptr<xsltStylesheet, XmlStyleSheetDeleter>; @@ -109,13 +84,13 @@ static QByteArray formatNode(xmlNodePtr node, QString *errorMessage) xmlSaveToIO(qbXmlOutputWriteCallback, qbXmlOutputCloseCallback, &result, "UTF-8", 0); if (!saveContext) { - *errorMessage = QLatin1String("xmlSaveToIO() failed."); + *errorMessage = u"xmlSaveToIO() failed."_s; return result; } const long saveResult = xmlSaveTree(saveContext, node); xmlSaveClose(saveContext); if (saveResult < 0) - *errorMessage = QLatin1String("xmlSaveTree() failed."); + *errorMessage = u"xmlSaveTree() failed."_s; return result; } @@ -144,8 +119,8 @@ QString LibXmlXQuery::doEvaluate(const QString &xPathExpression, QString *errorM XmlPathObjectUniquePtr xPathObject(xmlXPathEvalExpression(xPathExpressionX, m_xpathContext.get())); if (!xPathObject) { - *errorMessage = QLatin1String("xmlXPathEvalExpression() failed for \"") + xPathExpression - + QLatin1Char('"'); + *errorMessage = u"xmlXPathEvalExpression() failed for \""_s + xPathExpression + + u'"'; return QString(); } QString result; @@ -162,39 +137,42 @@ QString LibXmlXQuery::doEvaluate(const QString &xPathExpression, QString *errorM return result; } -QSharedPointer<XQuery> libXml_createXQuery(const QString &focus, QString *errorMessage) +std::shared_ptr<XQuery> libXml_createXQuery(const QString &focus, QString *errorMessage) { - XmlDocUniquePtr doc(xmlParseFile(QFile::encodeName(focus).constData())); + XmlDocUniquePtr doc(xmlReadFile(QFile::encodeName(focus).constData(), + "utf-8", XML_PARSE_NOENT)); if (!doc) { - *errorMessage = QLatin1String("libxml2: Cannot set focus to ") + QDir::toNativeSeparators(focus); + *errorMessage = u"libxml2: Cannot set focus to "_s + QDir::toNativeSeparators(focus); return {}; } XmlXPathContextUniquePtr xpathContext(xmlXPathNewContext(doc.get())); if (!xpathContext) { - *errorMessage = QLatin1String("libxml2: xmlXPathNewContext() failed"); + *errorMessage = u"libxml2: xmlXPathNewContext() failed"_s; return {}; } - return QSharedPointer<XQuery>(new LibXmlXQuery(doc, xpathContext)); + return std::shared_ptr<XQuery>(new LibXmlXQuery(doc, xpathContext)); } // XSLT transformation -static const char xsltPrefix[] = R"(<?xml version="1.0" encoding="UTF-8" ?> +static constexpr auto xsltPrefix = R"(<?xml version="1.0" encoding="UTF-8" ?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> -)"; +)"_L1; QString libXslt_transform(const QString &xml, QString xsl, QString *errorMessage) { ensureInitialized(); // Read XML data - if (!xsl.startsWith(QLatin1String("<?xml"))) { - xsl.prepend(QLatin1String(xsltPrefix)); - xsl.append(QLatin1String("</xsl:transform>")); + if (!xsl.startsWith(u"<?xml")) { + xsl.prepend(xsltPrefix); + xsl.append(u"</xsl:transform>"_s); } const QByteArray xmlData = xml.toUtf8(); - XmlDocUniquePtr xmlDoc(xmlParseMemory(xmlData.constData(), xmlData.size())); + + XmlDocUniquePtr xmlDoc(xmlReadMemory(xmlData.constData(), int(xmlData.size()), + "", "utf-8", XML_PARSE_NOENT)); if (!xmlDoc) { - *errorMessage = QLatin1String("xmlParseMemory() failed for XML."); + *errorMessage = u"xmlParseMemory() failed for XML."_s; return xml; } @@ -203,15 +181,14 @@ QString libXslt_transform(const QString &xml, QString xsl, QString *errorMessage // xsltFreeStylesheet will delete this pointer xmlDocPtr xslDoc = xmlParseMemory(xslData.constData(), xslData.size()); if (!xslDoc) { - *errorMessage = QLatin1String("xmlParseMemory() failed for XSL \"") - + xsl + QLatin1String("\"."); + *errorMessage = u"xmlParseMemory() failed for XSL \""_s + xsl + u"\"."_s; return xml; }; // Parse XSL data XmlStyleSheetUniquePtr xslt(xsltParseStylesheetDoc(xslDoc)); if (!xslt) { - *errorMessage = QLatin1String("xsltParseStylesheetDoc() failed."); + *errorMessage = u"xsltParseStylesheetDoc() failed."_s; return xml; } @@ -224,7 +201,7 @@ QString libXslt_transform(const QString &xml, QString xsl, QString *errorMessage result = QString::fromUtf8(reinterpret_cast<char*>(buffer), bufferSize); std::free(buffer); } else { - *errorMessage = QLatin1String("xsltSaveResultToString() failed."); + *errorMessage = u"xsltSaveResultToString() failed."_s; result = xml; } return result.trimmed(); |