From a791fc0173aa1bc38727506c9bee2b64cbd9386c Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 4 Mar 2023 09:17:31 +0200 Subject: [PATCH] Reading from disc doesn't work from on Windows. Read from memory instead. --- src/libs/ifc/xml/vabstractconverter.cpp | 101 ++++++++++++----------- src/test/ValentinaTest/tst_xsdschema.cpp | 49 +++++------ 2 files changed, 76 insertions(+), 74 deletions(-) diff --git a/src/libs/ifc/xml/vabstractconverter.cpp b/src/libs/ifc/xml/vabstractconverter.cpp index 0fbb1eea8..becc8e29a 100644 --- a/src/libs/ifc/xml/vabstractconverter.cpp +++ b/src/libs/ifc/xml/vabstractconverter.cpp @@ -31,6 +31,7 @@ #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #include +#include #else #include #include @@ -180,58 +181,64 @@ void VAbstractConverter::ValidateXML(const QString &schema) const VParserErrorHandler parserErrorHandler; #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - QScopedPointer tempSchema(QTemporaryFile::createNativeFile(fileSchema)); - if (tempSchema == nullptr) + XERCES_CPP_NAMESPACE::XercesDOMParser domParser; + domParser.setErrorHandler(&parserErrorHandler); + + QByteArray data = fileSchema.readAll(); + const char* schemaData = data.constData(); + + QScopedPointer grammarSource( + new XERCES_CPP_NAMESPACE::MemBufInputSource(reinterpret_cast(schemaData), + strlen(schemaData), "schema")); + + if (domParser.loadGrammar( + *grammarSource, + XERCES_CPP_NAMESPACE::Grammar::SchemaGrammarType, true) == nullptr) { - const QString errorMsg(tr("Can't create native file for schema file %1:\n%2.") - .arg(schema, fileSchema.errorString())); + VException e(parserErrorHandler.StatusMessage()); + e.AddMoreInformation(tr("Could not load schema file '%1'.").arg(fileSchema.fileName())); + throw e; + } + + qCDebug(vXML, "Schema loaded."); + + if (parserErrorHandler.HasError()) + { + VException e(parserErrorHandler.StatusMessage()); + e.AddMoreInformation(tr("Schema file %3 invalid in line %1 column %2").arg(parserErrorHandler.Line()) + .arg(parserErrorHandler.Column()).arg(fileSchema.fileName())); + throw e; + } + + domParser.setValidationScheme(XERCES_CPP_NAMESPACE::XercesDOMParser::Val_Always); + domParser.setDoNamespaces(true); + domParser.setDoSchema(true); + domParser.setValidationConstraintFatal(true); + domParser.setValidationSchemaFullChecking(true); + domParser.useCachedGrammarInParse(true); + + QFile pattern(m_convertedFileName); + if (not pattern.open(QIODevice::ReadOnly)) + { + const QString errorMsg(tr("Can't open file %1:\n%2.").arg(m_convertedFileName, pattern.errorString())); throw VException(errorMsg); } - if (tempSchema->open()) + QByteArray patternFileData = pattern.readAll(); + const char* patternData = patternFileData.constData(); + + QScopedPointer patternSource( + new XERCES_CPP_NAMESPACE::MemBufInputSource(reinterpret_cast(patternData), + strlen(patternData), "pattern")); + + domParser.parse(*patternSource); + + if (domParser.getErrorCount() > 0) { - XERCES_CPP_NAMESPACE::XercesDOMParser domParser; - domParser.setErrorHandler(&parserErrorHandler); - - if (domParser.loadGrammar( - tempSchema->fileName().toUtf8().constData(), - XERCES_CPP_NAMESPACE::Grammar::SchemaGrammarType, true) == nullptr) - { - VException e(parserErrorHandler.StatusMessage()); - e.AddMoreInformation(tr("Could not load schema file '%1'.").arg(fileSchema.fileName())); - throw e; - } - - qCDebug(vXML, "Schema loaded."); - - if (parserErrorHandler.HasError()) - { - VException e(parserErrorHandler.StatusMessage()); - e.AddMoreInformation(tr("Schema file %3 invalid in line %1 column %2").arg(parserErrorHandler.Line()) - .arg(parserErrorHandler.Column()).arg(fileSchema.fileName())); - throw e; - } - - domParser.setValidationScheme(XERCES_CPP_NAMESPACE::XercesDOMParser::Val_Always); - domParser.setDoNamespaces(true); - domParser.setDoSchema(true); - domParser.setValidationConstraintFatal(true); - domParser.setValidationSchemaFullChecking(true); - domParser.useCachedGrammarInParse(true); - - domParser.parse(m_convertedFileName.toUtf8().constData()); - - if (domParser.getErrorCount() > 0) - { - VException e(parserErrorHandler.StatusMessage()); - e.AddMoreInformation(tr("Validation error file %3 in line %1 column %2").arg(parserErrorHandler.Line()) - .arg(parserErrorHandler.Column()).arg(m_originalFileName)); - throw e; - } - } - else - { - qCritical() << tr("Unable to open native file for schema"); + VException e(parserErrorHandler.StatusMessage()); + e.AddMoreInformation(tr("Validation error file %3 in line %1 column %2").arg(parserErrorHandler.Line()) + .arg(parserErrorHandler.Column()).arg(m_originalFileName)); + throw e; } #else QFile pattern(m_convertedFileName); diff --git a/src/test/ValentinaTest/tst_xsdschema.cpp b/src/test/ValentinaTest/tst_xsdschema.cpp index 2ebf00283..bf5805728 100644 --- a/src/test/ValentinaTest/tst_xsdschema.cpp +++ b/src/test/ValentinaTest/tst_xsdschema.cpp @@ -39,6 +39,7 @@ #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #include +#include #include #else @@ -48,6 +49,7 @@ namespace { + //--------------------------------------------------------------------------------------------------------------------- void ValidateSchema(const QString &schema) { @@ -60,36 +62,29 @@ void ValidateSchema(const QString &schema) VParserErrorHandler parserErrorHandler; #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - QScopedPointer tempSchema(QTemporaryFile::createNativeFile(fileSchema)); - if (tempSchema == nullptr) + XERCES_CPP_NAMESPACE::XercesDOMParser domParser; + domParser.setErrorHandler(&parserErrorHandler); + + QByteArray data = fileSchema.readAll(); + const char* schemaData = data.constData(); + + QScopedPointer grammarSource( + new XERCES_CPP_NAMESPACE::MemBufInputSource(reinterpret_cast(schemaData), + strlen(schemaData), "schema")); + + if (domParser.loadGrammar( + *grammarSource, + XERCES_CPP_NAMESPACE::Grammar::SchemaGrammarType, true) == nullptr) { - QFAIL(qUtf8Printable(QStringLiteral("Can't create native file for schema file %1:\n%2.") - .arg(schema, fileSchema.errorString()))); + QFAIL(qUtf8Printable(QStringLiteral("%1 Could not load schema file '%2'.") + .arg(parserErrorHandler.StatusMessage(), fileSchema.fileName()))); } - if (tempSchema->open()) - { - XERCES_CPP_NAMESPACE::XercesDOMParser domParser; - domParser.setErrorHandler(&parserErrorHandler); - - if (domParser.loadGrammar( - tempSchema->fileName().toUtf8().constData(), - XERCES_CPP_NAMESPACE::Grammar::SchemaGrammarType, true) == nullptr) - { - QFAIL(qUtf8Printable(QStringLiteral("%1 Could not load schema file '%2'.") - .arg(parserErrorHandler.StatusMessage(), fileSchema.fileName()))); - } - - QVERIFY2(not parserErrorHandler.HasError(), - qUtf8Printable(QStringLiteral("%1 Schema file %2 invalid in line %3 column %4.") - .arg(parserErrorHandler.StatusMessage(), fileSchema.fileName()) - .arg(parserErrorHandler.Line()) - .arg(parserErrorHandler.Column()))); - } - else - { - QFAIL("Unable to open native file for schema"); - } + QVERIFY2(not parserErrorHandler.HasError(), + qUtf8Printable(QStringLiteral("%1 Schema file %2 invalid in line %3 column %4.") + .arg(parserErrorHandler.StatusMessage(), fileSchema.fileName()) + .arg(parserErrorHandler.Line()) + .arg(parserErrorHandler.Column()))); #else QXmlSchema sch; sch.setMessageHandler(&parserErrorHandler);