Fixed issue #325. Check pattern for inverse compatibility.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2016-01-19 17:25:46 +02:00
parent c59efe15d3
commit be8503664f
9 changed files with 98 additions and 35 deletions

View File

@ -1,3 +1,6 @@
# Version 0.5.0
- [#325] Check pattern for inverse compatibility.
# Version 0.3.3 Released May 20, 2015 # Version 0.3.3 Released May 20, 2015
- [#297] Scaling Error - Print. - [#297] Scaling Error - Print.
- [#304] Layout appears different than my pattern. - [#304] Layout appears different than my pattern.

View File

@ -65,7 +65,14 @@ void VAbstractConverter::Convert()
ReserveFile(); ReserveFile();
if (ver <= MaxVer())
{
ApplyPatches(); ApplyPatches();
}
else
{
DowngradeToCurrentMaxVersion();
}
QFile file(backupFileName); QFile file(backupFileName);
file.remove(); file.remove();
@ -104,25 +111,25 @@ int VAbstractConverter::GetVersion(const QString &version) const
{ {
ValidateVersion(version); ValidateVersion(version);
QStringList ver = version.split("."); const QStringList ver = version.split(".");
bool ok = false; bool ok = false;
int major = ver.at(0).toInt(&ok); const int major = ver.at(0).toInt(&ok);
if (ok == false) if (not ok)
{ {
return 0x0; return 0x0;
} }
ok = false; ok = false;
int minor = ver.at(1).toInt(&ok); const int minor = ver.at(1).toInt(&ok);
if (ok == false) if (not ok)
{ {
return 0x0; return 0x0;
} }
ok = false; ok = false;
int patch = ver.at(2).toInt(&ok); const int patch = ver.at(2).toInt(&ok);
if (ok == false) if (not ok)
{ {
return 0x0; return 0x0;
} }
@ -133,7 +140,7 @@ int VAbstractConverter::GetVersion(const QString &version) const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VAbstractConverter::ValidateVersion(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) if (rx.exactMatch(version) == false)
{ {
@ -207,7 +214,7 @@ void VAbstractConverter::BiasTokens(int position, int bias, QMap<int, QString> &
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VAbstractConverter::CheckVersion(int ver) const Q_NORETURN void VAbstractConverter::InvalidVersion(int ver) const
{ {
if (ver < MinVer()) 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())); const QString errorMsg(tr("Invalid version. Maximum supported version is %1").arg(MaxVerStr()));
throw VException(errorMsg); 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); return VDomDocument::SaveDocument(fileName, error);
} }
//---------------------------------------------------------------------------------------------------------------------
void VAbstractConverter::ValidateInputFile(const QString &currentSchema) 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 void VAbstractConverter::Save() const
{ {
@ -253,6 +300,8 @@ void VAbstractConverter::Save() const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VAbstractConverter::SetVersion(const QString &version) void VAbstractConverter::SetVersion(const QString &version)
{ {
ValidateVersion(version);
if (setTagText(TagVersion, version) == false) if (setTagText(TagVersion, version) == false)
{ {
VException e(tr("Could not change version.")); VException e(tr("Could not change version."));

View File

@ -45,8 +45,9 @@ protected:
int ver; int ver;
QString fileName; QString fileName;
void ValidateInputFile(const QString &currentSchema) const;
int GetVersion(const QString &version) const; int GetVersion(const QString &version) const;
void CheckVersion(int ver) const; Q_NORETURN void InvalidVersion(int ver) const;
void Save() const; void Save() const;
void SetVersion(const QString &version); void SetVersion(const QString &version);
@ -58,6 +59,7 @@ protected:
virtual QString XSDSchema(int ver) const =0; virtual QString XSDSchema(int ver) const =0;
virtual void ApplyPatches() =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 Replace(QString &formula, const QString &newName, int position, const QString &token, int &bias) const;
void CorrectionsPositions(int position, int bias, QMap<int, QString> &tokens) const; void CorrectionsPositions(int position, int bias, QMap<int, QString> &tokens) const;

View File

@ -50,8 +50,7 @@ const QString VPatternConverter::CurrentSchema = QStringLiteral("://schema/pa
VPatternConverter::VPatternConverter(const QString &fileName) VPatternConverter::VPatternConverter(const QString &fileName)
:VAbstractConverter(fileName) :VAbstractConverter(fileName)
{ {
const QString schema = XSDSchema(ver); ValidateInputFile(CurrentSchema);
ValidateXML(schema, fileName);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -85,8 +84,6 @@ QString VPatternConverter::MaxVerStr() const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VPatternConverter::XSDSchema(int ver) const QString VPatternConverter::XSDSchema(int ver) const
{ {
CheckVersion(ver);
switch (ver) switch (ver)
{ {
case (0x000100): case (0x000100):
@ -110,10 +107,8 @@ QString VPatternConverter::XSDSchema(int ver) const
case (0x000204): case (0x000204):
return CurrentSchema; return CurrentSchema;
default: default:
{ InvalidVersion(ver);
const QString errorMsg(tr("Unexpected version \"%1\".").arg(ver, 0, 16)); break;
throw VException(errorMsg);
}
} }
} }
@ -212,6 +207,13 @@ void VPatternConverter::ApplyPatches()
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void VPatternConverter::DowngradeToCurrentMaxVersion()
{
SetVersion(PatternMaxVerStr);
Save();
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPatternConverter::ToV0_1_1() void VPatternConverter::ToV0_1_1()
{ {

View File

@ -50,6 +50,7 @@ protected:
QString XSDSchema(int ver) const; QString XSDSchema(int ver) const;
virtual void ApplyPatches() Q_DECL_OVERRIDE; virtual void ApplyPatches() Q_DECL_OVERRIDE;
virtual void DowngradeToCurrentMaxVersion() Q_DECL_OVERRIDE;
private: private:
Q_DISABLE_COPY(VPatternConverter) Q_DISABLE_COPY(VPatternConverter)

View File

@ -47,8 +47,7 @@ const QString VVITConverter::CurrentSchema = QStringLiteral("://schema/in
VVITConverter::VVITConverter(const QString &fileName) VVITConverter::VVITConverter(const QString &fileName)
:VAbstractMConverter(fileName) :VAbstractMConverter(fileName)
{ {
const QString schema = XSDSchema(ver); ValidateInputFile(CurrentSchema);
ValidateXML(schema, fileName);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -82,8 +81,6 @@ QString VVITConverter::MaxVerStr() const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VVITConverter::XSDSchema(int ver) const QString VVITConverter::XSDSchema(int ver) const
{ {
CheckVersion(ver);
switch (ver) switch (ver)
{ {
case (0x000200): case (0x000200):
@ -97,10 +94,8 @@ QString VVITConverter::XSDSchema(int ver) const
case (0x000303): case (0x000303):
return CurrentSchema; return CurrentSchema;
default: default:
{ InvalidVersion(ver);
const QString errorMsg(tr("Unexpected version \"%1\".").arg(ver, 0, 16)); break;
throw VException(errorMsg);
}
} }
} }
@ -164,6 +159,13 @@ void VVITConverter::ApplyPatches()
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void VVITConverter::DowngradeToCurrentMaxVersion()
{
SetVersion(MeasurementMaxVerStr);
Save();
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VVITConverter::AddNewTagsForV0_3_0() void VVITConverter::AddNewTagsForV0_3_0()
{ {

View File

@ -50,6 +50,7 @@ protected:
QString XSDSchema(int ver) const; QString XSDSchema(int ver) const;
virtual void ApplyPatches() Q_DECL_OVERRIDE; virtual void ApplyPatches() Q_DECL_OVERRIDE;
virtual void DowngradeToCurrentMaxVersion() Q_DECL_OVERRIDE;
private: private:
Q_DISABLE_COPY(VVITConverter) Q_DISABLE_COPY(VVITConverter)

View File

@ -47,8 +47,7 @@ const QString VVSTConverter::CurrentSchema = QStringLiteral("://schema/st
VVSTConverter::VVSTConverter(const QString &fileName) VVSTConverter::VVSTConverter(const QString &fileName)
:VAbstractMConverter(fileName) :VAbstractMConverter(fileName)
{ {
const QString schema = XSDSchema(ver); ValidateInputFile(CurrentSchema);
ValidateXML(schema, fileName);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -82,8 +81,6 @@ QString VVSTConverter::MaxVerStr() const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VVSTConverter::XSDSchema(int ver) const QString VVSTConverter::XSDSchema(int ver) const
{ {
CheckVersion(ver);
switch (ver) switch (ver)
{ {
case (0x000300): case (0x000300):
@ -95,10 +92,8 @@ QString VVSTConverter::XSDSchema(int ver) const
case (0x000402): case (0x000402):
return CurrentSchema; return CurrentSchema;
default: default:
{ InvalidVersion(ver);
const QString errorMsg(tr("Unexpected version \"%1\".").arg(ver, 0, 16)); break;
throw VException(errorMsg);
}
} }
} }
@ -155,6 +150,13 @@ void VVSTConverter::ApplyPatches()
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void VVSTConverter::DowngradeToCurrentMaxVersion()
{
SetVersion(MeasurementMaxVerStr);
Save();
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VVSTConverter::AddNewTagsForV0_4_0() void VVSTConverter::AddNewTagsForV0_4_0()
{ {

View File

@ -50,6 +50,7 @@ protected:
QString XSDSchema(int ver) const; QString XSDSchema(int ver) const;
virtual void ApplyPatches() Q_DECL_OVERRIDE; virtual void ApplyPatches() Q_DECL_OVERRIDE;
virtual void DowngradeToCurrentMaxVersion() Q_DECL_OVERRIDE;
private: private:
Q_DISABLE_COPY(VVSTConverter) Q_DISABLE_COPY(VVSTConverter)