From be8503664fc6c5a7e48a12c3e60ba3f559e4b102 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 19 Jan 2016 17:25:46 +0200 Subject: [PATCH] Fixed issue #325. Check pattern for inverse compatibility. --HG-- branch : develop --- ChangeLog.txt | 3 ++ src/libs/ifc/xml/vabstractconverter.cpp | 69 +++++++++++++++++++++---- src/libs/ifc/xml/vabstractconverter.h | 4 +- src/libs/ifc/xml/vpatternconverter.cpp | 18 ++++--- src/libs/ifc/xml/vpatternconverter.h | 1 + src/libs/ifc/xml/vvitconverter.cpp | 18 ++++--- src/libs/ifc/xml/vvitconverter.h | 1 + src/libs/ifc/xml/vvstconverter.cpp | 18 ++++--- src/libs/ifc/xml/vvstconverter.h | 1 + 9 files changed, 98 insertions(+), 35 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index d0b99aad1..1e6d8f90f 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,3 +1,6 @@ +# Version 0.5.0 +- [#325] Check pattern for inverse compatibility. + # Version 0.3.3 Released May 20, 2015 - [#297] Scaling Error - Print. - [#304] Layout appears different than my pattern. diff --git a/src/libs/ifc/xml/vabstractconverter.cpp b/src/libs/ifc/xml/vabstractconverter.cpp index 182bf6f18..2c89eda77 100644 --- a/src/libs/ifc/xml/vabstractconverter.cpp +++ b/src/libs/ifc/xml/vabstractconverter.cpp @@ -65,7 +65,14 @@ void VAbstractConverter::Convert() ReserveFile(); - ApplyPatches(); + if (ver <= MaxVer()) + { + ApplyPatches(); + } + else + { + DowngradeToCurrentMaxVersion(); + } QFile file(backupFileName); file.remove(); @@ -104,25 +111,25 @@ int VAbstractConverter::GetVersion(const QString &version) const { ValidateVersion(version); - QStringList ver = version.split("."); + const QStringList ver = version.split("."); bool ok = false; - int major = ver.at(0).toInt(&ok); - if (ok == false) + const int major = ver.at(0).toInt(&ok); + if (not ok) { return 0x0; } ok = false; - int minor = ver.at(1).toInt(&ok); - if (ok == false) + const int minor = ver.at(1).toInt(&ok); + if (not ok) { return 0x0; } ok = false; - int patch = ver.at(2).toInt(&ok); - if (ok == false) + const int patch = ver.at(2).toInt(&ok); + if (not ok) { return 0x0; } @@ -133,7 +140,7 @@ int VAbstractConverter::GetVersion(const QString &version) const //--------------------------------------------------------------------------------------------------------------------- void VAbstractConverter::ValidateVersion(const QString &version) const { - QRegExp rx(QStringLiteral("^(0|([1-9][0-9]*)).(0|([1-9][0-9]*)).(0|([1-9][0-9]*))$")); + const QRegExp rx(QStringLiteral("^(0|([1-9][0-9]*)).(0|([1-9][0-9]*)).(0|([1-9][0-9]*))$")); if (rx.exactMatch(version) == false) { @@ -207,7 +214,7 @@ void VAbstractConverter::BiasTokens(int position, int bias, QMap & } //--------------------------------------------------------------------------------------------------------------------- -void VAbstractConverter::CheckVersion(int ver) const +Q_NORETURN void VAbstractConverter::InvalidVersion(int ver) const { if (ver < MinVer()) { @@ -220,6 +227,9 @@ void VAbstractConverter::CheckVersion(int ver) const const QString errorMsg(tr("Invalid version. Maximum supported version is %1").arg(MaxVerStr())); throw VException(errorMsg); } + + const QString errorMsg(tr("Unexpected version \"%1\".").arg(ver, 0, 16)); + throw VException(errorMsg); } //--------------------------------------------------------------------------------------------------------------------- @@ -239,6 +249,43 @@ bool VAbstractConverter::SaveDocument(const QString &fileName, QString &error) c return VDomDocument::SaveDocument(fileName, error); } +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractConverter::ValidateInputFile(const QString ¤tSchema) const +{ + QString schema; + try + { + schema = XSDSchema(ver); + } + catch(const VException &e) + { + if (ver < MinVer()) + { // Version less than minimally supported version. Can't do anything. + throw e; + } + else if (ver > MaxVer()) + { // Version bigger than maximum supported version. We still have a chance to open the file. + try + { // Try to open like the current version. + ValidateXML(currentSchema, fileName); + } + catch(const VException &exp) + { // Nope, we can't. + Q_UNUSED(exp); + throw e; + } + } + else + { // Unexpected version. Most time mean that we do not catch all versions between min and max. + throw e; + } + + return; // All is fine and we can try to convert to current max version. + } + + ValidateXML(schema, fileName); +} + //--------------------------------------------------------------------------------------------------------------------- void VAbstractConverter::Save() const { @@ -253,6 +300,8 @@ void VAbstractConverter::Save() const //--------------------------------------------------------------------------------------------------------------------- void VAbstractConverter::SetVersion(const QString &version) { + ValidateVersion(version); + if (setTagText(TagVersion, version) == false) { VException e(tr("Could not change version.")); diff --git a/src/libs/ifc/xml/vabstractconverter.h b/src/libs/ifc/xml/vabstractconverter.h index 5f8adff7e..6fa3599ee 100644 --- a/src/libs/ifc/xml/vabstractconverter.h +++ b/src/libs/ifc/xml/vabstractconverter.h @@ -45,8 +45,9 @@ protected: int ver; QString fileName; + void ValidateInputFile(const QString ¤tSchema) const; int GetVersion(const QString &version) const; - void CheckVersion(int ver) const; + Q_NORETURN void InvalidVersion(int ver) const; void Save() const; void SetVersion(const QString &version); @@ -58,6 +59,7 @@ protected: virtual QString XSDSchema(int ver) const =0; virtual void ApplyPatches() =0; + virtual void DowngradeToCurrentMaxVersion() =0; void Replace(QString &formula, const QString &newName, int position, const QString &token, int &bias) const; void CorrectionsPositions(int position, int bias, QMap &tokens) const; diff --git a/src/libs/ifc/xml/vpatternconverter.cpp b/src/libs/ifc/xml/vpatternconverter.cpp index b88a4e561..4239e4fe0 100644 --- a/src/libs/ifc/xml/vpatternconverter.cpp +++ b/src/libs/ifc/xml/vpatternconverter.cpp @@ -50,8 +50,7 @@ const QString VPatternConverter::CurrentSchema = QStringLiteral("://schema/pa VPatternConverter::VPatternConverter(const QString &fileName) :VAbstractConverter(fileName) { - const QString schema = XSDSchema(ver); - ValidateXML(schema, fileName); + ValidateInputFile(CurrentSchema); } //--------------------------------------------------------------------------------------------------------------------- @@ -85,8 +84,6 @@ QString VPatternConverter::MaxVerStr() const //--------------------------------------------------------------------------------------------------------------------- QString VPatternConverter::XSDSchema(int ver) const { - CheckVersion(ver); - switch (ver) { case (0x000100): @@ -110,10 +107,8 @@ QString VPatternConverter::XSDSchema(int ver) const case (0x000204): return CurrentSchema; default: - { - const QString errorMsg(tr("Unexpected version \"%1\".").arg(ver, 0, 16)); - throw VException(errorMsg); - } + InvalidVersion(ver); + break; } } @@ -212,6 +207,13 @@ void VPatternConverter::ApplyPatches() } } +//--------------------------------------------------------------------------------------------------------------------- +void VPatternConverter::DowngradeToCurrentMaxVersion() +{ + SetVersion(PatternMaxVerStr); + Save(); +} + //--------------------------------------------------------------------------------------------------------------------- void VPatternConverter::ToV0_1_1() { diff --git a/src/libs/ifc/xml/vpatternconverter.h b/src/libs/ifc/xml/vpatternconverter.h index 28038c1d5..ee6ff32a1 100644 --- a/src/libs/ifc/xml/vpatternconverter.h +++ b/src/libs/ifc/xml/vpatternconverter.h @@ -50,6 +50,7 @@ protected: QString XSDSchema(int ver) const; virtual void ApplyPatches() Q_DECL_OVERRIDE; + virtual void DowngradeToCurrentMaxVersion() Q_DECL_OVERRIDE; private: Q_DISABLE_COPY(VPatternConverter) diff --git a/src/libs/ifc/xml/vvitconverter.cpp b/src/libs/ifc/xml/vvitconverter.cpp index e90bfcb90..75d9cdb57 100644 --- a/src/libs/ifc/xml/vvitconverter.cpp +++ b/src/libs/ifc/xml/vvitconverter.cpp @@ -47,8 +47,7 @@ const QString VVITConverter::CurrentSchema = QStringLiteral("://schema/in VVITConverter::VVITConverter(const QString &fileName) :VAbstractMConverter(fileName) { - const QString schema = XSDSchema(ver); - ValidateXML(schema, fileName); + ValidateInputFile(CurrentSchema); } //--------------------------------------------------------------------------------------------------------------------- @@ -82,8 +81,6 @@ QString VVITConverter::MaxVerStr() const //--------------------------------------------------------------------------------------------------------------------- QString VVITConverter::XSDSchema(int ver) const { - CheckVersion(ver); - switch (ver) { case (0x000200): @@ -97,10 +94,8 @@ QString VVITConverter::XSDSchema(int ver) const case (0x000303): return CurrentSchema; default: - { - const QString errorMsg(tr("Unexpected version \"%1\".").arg(ver, 0, 16)); - throw VException(errorMsg); - } + InvalidVersion(ver); + break; } } @@ -164,6 +159,13 @@ void VVITConverter::ApplyPatches() } } +//--------------------------------------------------------------------------------------------------------------------- +void VVITConverter::DowngradeToCurrentMaxVersion() +{ + SetVersion(MeasurementMaxVerStr); + Save(); +} + //--------------------------------------------------------------------------------------------------------------------- void VVITConverter::AddNewTagsForV0_3_0() { diff --git a/src/libs/ifc/xml/vvitconverter.h b/src/libs/ifc/xml/vvitconverter.h index b2a265d01..befde03e0 100644 --- a/src/libs/ifc/xml/vvitconverter.h +++ b/src/libs/ifc/xml/vvitconverter.h @@ -50,6 +50,7 @@ protected: QString XSDSchema(int ver) const; virtual void ApplyPatches() Q_DECL_OVERRIDE; + virtual void DowngradeToCurrentMaxVersion() Q_DECL_OVERRIDE; private: Q_DISABLE_COPY(VVITConverter) diff --git a/src/libs/ifc/xml/vvstconverter.cpp b/src/libs/ifc/xml/vvstconverter.cpp index 95a39dbe7..b65c80f94 100644 --- a/src/libs/ifc/xml/vvstconverter.cpp +++ b/src/libs/ifc/xml/vvstconverter.cpp @@ -47,8 +47,7 @@ const QString VVSTConverter::CurrentSchema = QStringLiteral("://schema/st VVSTConverter::VVSTConverter(const QString &fileName) :VAbstractMConverter(fileName) { - const QString schema = XSDSchema(ver); - ValidateXML(schema, fileName); + ValidateInputFile(CurrentSchema); } //--------------------------------------------------------------------------------------------------------------------- @@ -82,8 +81,6 @@ QString VVSTConverter::MaxVerStr() const //--------------------------------------------------------------------------------------------------------------------- QString VVSTConverter::XSDSchema(int ver) const { - CheckVersion(ver); - switch (ver) { case (0x000300): @@ -95,10 +92,8 @@ QString VVSTConverter::XSDSchema(int ver) const case (0x000402): return CurrentSchema; default: - { - const QString errorMsg(tr("Unexpected version \"%1\".").arg(ver, 0, 16)); - throw VException(errorMsg); - } + InvalidVersion(ver); + break; } } @@ -155,6 +150,13 @@ void VVSTConverter::ApplyPatches() } } +//--------------------------------------------------------------------------------------------------------------------- +void VVSTConverter::DowngradeToCurrentMaxVersion() +{ + SetVersion(MeasurementMaxVerStr); + Save(); +} + //--------------------------------------------------------------------------------------------------------------------- void VVSTConverter::AddNewTagsForV0_4_0() { diff --git a/src/libs/ifc/xml/vvstconverter.h b/src/libs/ifc/xml/vvstconverter.h index bdaa74d1b..e6ed806ee 100644 --- a/src/libs/ifc/xml/vvstconverter.h +++ b/src/libs/ifc/xml/vvstconverter.h @@ -50,6 +50,7 @@ protected: QString XSDSchema(int ver) const; virtual void ApplyPatches() Q_DECL_OVERRIDE; + virtual void DowngradeToCurrentMaxVersion() Q_DECL_OVERRIDE; private: Q_DISABLE_COPY(VVSTConverter)