2014-12-10 18:18:22 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vabstractconverter.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 10 12, 2014
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2014-12-10 18:18:22 +01:00
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vabstractconverter.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QDir>
|
|
|
|
#include <QDomElement>
|
|
|
|
#include <QDomNode>
|
|
|
|
#include <QDomNodeList>
|
2014-12-11 21:54:33 +01:00
|
|
|
#include <QFile>
|
2015-08-27 10:45:31 +02:00
|
|
|
#include <QFileInfo>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QLatin1String>
|
|
|
|
#include <QMap>
|
2016-07-15 11:00:00 +02:00
|
|
|
#include <QRegularExpression>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QRegularExpressionMatch>
|
|
|
|
#include <QStaticStringData>
|
|
|
|
#include <QStringData>
|
|
|
|
#include <QStringDataPtr>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
#include "../exception/vexception.h"
|
|
|
|
#include "../exception/vexceptionwrongid.h"
|
2016-08-09 15:55:46 +02:00
|
|
|
#include "vdomdocument.h"
|
2014-12-11 21:54:33 +01:00
|
|
|
|
2014-12-11 16:12:16 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-12-11 16:51:24 +01:00
|
|
|
VAbstractConverter::VAbstractConverter(const QString &fileName)
|
2014-12-11 21:16:03 +01:00
|
|
|
:VDomDocument(), ver(0x0), fileName(fileName)
|
2014-12-10 18:18:22 +01:00
|
|
|
{
|
2016-10-30 13:34:55 +01:00
|
|
|
QFileInfo info(fileName);
|
|
|
|
if (info.isSymLink() && not info.isWritable())
|
|
|
|
{
|
|
|
|
ReplaceSymLink();
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:51:24 +01:00
|
|
|
this->setXMLContent(fileName);
|
2014-12-11 20:44:48 +01:00
|
|
|
const QString version = GetVersionStr();
|
2014-12-11 21:16:03 +01:00
|
|
|
ver = GetVersion(version);
|
2014-12-11 16:51:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-11 20:44:48 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VAbstractConverter::~VAbstractConverter()
|
|
|
|
{}
|
|
|
|
|
2014-12-11 21:54:33 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-12-16 08:42:24 +01:00
|
|
|
void VAbstractConverter::Convert()
|
2014-12-11 21:54:33 +01:00
|
|
|
{
|
|
|
|
if (ver == MaxVer())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString error;
|
2016-07-20 10:39:15 +02:00
|
|
|
const QString backupFileName = fileName + QLatin1String(".backup");
|
2014-12-11 21:54:33 +01:00
|
|
|
if (SafeCopy(fileName, backupFileName, error) == false)
|
|
|
|
{
|
2015-08-27 10:45:31 +02:00
|
|
|
const QString errorMsg(tr("Error creating a backup file: %1.").arg(error));
|
2014-12-11 21:54:33 +01:00
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:45:31 +02:00
|
|
|
ReserveFile();
|
|
|
|
|
2016-01-19 16:25:46 +01:00
|
|
|
if (ver <= MaxVer())
|
|
|
|
{
|
|
|
|
ApplyPatches();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DowngradeToCurrentMaxVersion();
|
|
|
|
}
|
2014-12-11 21:54:33 +01:00
|
|
|
|
|
|
|
QFile file(backupFileName);
|
|
|
|
file.remove();
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:51:24 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VAbstractConverter::GetVersionStr() const
|
|
|
|
{
|
|
|
|
const QDomNodeList nodeList = this->elementsByTagName(TagVersion);
|
|
|
|
if (nodeList.isEmpty())
|
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Couldn't get version information."));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nodeList.count() > 1)
|
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Too many tags <%1> in file.").arg(TagVersion));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QDomNode domNode = nodeList.at(0);
|
|
|
|
if (domNode.isNull() == false && domNode.isElement())
|
|
|
|
{
|
|
|
|
const QDomElement domElement = domNode.toElement();
|
|
|
|
if (domElement.isNull() == false)
|
|
|
|
{
|
|
|
|
return domElement.text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QString(QStringLiteral("0.0.0"));
|
2014-12-10 18:18:22 +01:00
|
|
|
}
|
2014-12-11 17:14:02 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-15 11:00:00 +02:00
|
|
|
int VAbstractConverter::GetVersion(const QString &version)
|
2014-12-11 19:40:26 +01:00
|
|
|
{
|
|
|
|
ValidateVersion(version);
|
|
|
|
|
2016-01-19 16:25:46 +01:00
|
|
|
const QStringList ver = version.split(".");
|
2014-12-11 19:40:26 +01:00
|
|
|
|
|
|
|
bool ok = false;
|
2016-01-19 16:25:46 +01:00
|
|
|
const int major = ver.at(0).toInt(&ok);
|
|
|
|
if (not ok)
|
2014-12-11 19:40:26 +01:00
|
|
|
{
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = false;
|
2016-01-19 16:25:46 +01:00
|
|
|
const int minor = ver.at(1).toInt(&ok);
|
|
|
|
if (not ok)
|
2014-12-11 19:40:26 +01:00
|
|
|
{
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = false;
|
2016-01-19 16:25:46 +01:00
|
|
|
const int patch = ver.at(2).toInt(&ok);
|
|
|
|
if (not ok)
|
2014-12-11 19:40:26 +01:00
|
|
|
{
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (major<<16)|(minor<<8)|(patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-15 11:00:00 +02:00
|
|
|
void VAbstractConverter::ValidateVersion(const QString &version)
|
2014-12-11 17:14:02 +01:00
|
|
|
{
|
2016-07-15 11:00:00 +02:00
|
|
|
const QRegularExpression rx(QStringLiteral("^(0|([1-9][0-9]*)).(0|([1-9][0-9]*)).(0|([1-9][0-9]*))$"));
|
2014-12-11 17:14:02 +01:00
|
|
|
|
2016-07-15 11:00:00 +02:00
|
|
|
if (rx.match(version).hasMatch() == false)
|
2014-12-11 17:14:02 +01:00
|
|
|
{
|
2014-12-11 19:40:26 +01:00
|
|
|
const QString errorMsg(tr("Version \"%1\" invalid.").arg(version));
|
2014-12-11 17:14:02 +01:00
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
2014-12-11 19:40:26 +01:00
|
|
|
if (version == QLatin1String("0.0.0"))
|
2014-12-11 17:14:02 +01:00
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Version \"0.0.0\" invalid."));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
}
|
2014-12-11 20:44:48 +01:00
|
|
|
|
2015-08-27 10:45:31 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractConverter::ReserveFile() const
|
|
|
|
{
|
|
|
|
//It's not possible in all cases make conversion without lose data.
|
|
|
|
//For such cases we will store old version in a reserve file.
|
|
|
|
QString error;
|
|
|
|
QFileInfo info(fileName);
|
|
|
|
const QString reserveFileName = QString("%1/%2(v%3).%4")
|
|
|
|
.arg(info.absoluteDir().absolutePath())
|
|
|
|
.arg(info.baseName())
|
|
|
|
.arg(GetVersionStr())
|
|
|
|
.arg(info.completeSuffix());
|
2015-12-19 20:02:38 +01:00
|
|
|
if (not SafeCopy(fileName, reserveFileName, error))
|
2015-08-27 10:45:31 +02:00
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Error creating a reserv copy: %1.").arg(error));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 13:34:55 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractConverter::ReplaceSymLink() const
|
|
|
|
{
|
|
|
|
// See issue #582. Issue with standard path to shared data on Linux
|
|
|
|
// https://bitbucket.org/dismine/valentina/issues/582/issue-with-standard-path-to-shared-data-on
|
|
|
|
QFileInfo info(fileName);
|
|
|
|
if (info.isSymLink() && not info.isWritable())
|
|
|
|
{
|
|
|
|
QString error;
|
|
|
|
if (not SafeCopy(info.symLinkTarget(), fileName, error))
|
|
|
|
{
|
2016-10-31 08:30:10 +01:00
|
|
|
const QString errorMsg(tr("Error replacing a symlink by real file: %1.").arg(error));
|
2016-10-30 13:34:55 +01:00
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:25:11 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractConverter::Replace(QString &formula, const QString &newName, int position, const QString &token,
|
|
|
|
int &bias) const
|
|
|
|
{
|
|
|
|
formula.replace(position, token.length(), newName);
|
|
|
|
bias = token.length() - newName.length();
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:30:51 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-08-27 10:25:11 +02:00
|
|
|
void VAbstractConverter::CorrectionsPositions(int position, int bias, QMap<int, QString> &tokens) const
|
|
|
|
{
|
|
|
|
if (bias == 0)
|
|
|
|
{
|
|
|
|
return;// Nothing to correct;
|
|
|
|
}
|
|
|
|
|
|
|
|
BiasTokens(position, bias, tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractConverter::BiasTokens(int position, int bias, QMap<int, QString> &tokens)
|
|
|
|
{
|
|
|
|
QMap<int, QString> newTokens;
|
|
|
|
QMap<int, QString>::const_iterator i = tokens.constBegin();
|
|
|
|
while (i != tokens.constEnd())
|
|
|
|
{
|
|
|
|
if (i.key()<= position)
|
|
|
|
{ // Tokens before position "position" did not change his positions.
|
|
|
|
newTokens.insert(i.key(), i.value());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newTokens.insert(i.key()-bias, i.value());
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
tokens = newTokens;
|
|
|
|
}
|
|
|
|
|
2014-12-11 20:44:48 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-01-19 16:25:46 +01:00
|
|
|
Q_NORETURN void VAbstractConverter::InvalidVersion(int ver) const
|
2014-12-11 20:44:48 +01:00
|
|
|
{
|
|
|
|
if (ver < MinVer())
|
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Invalid version. Minimum supported version is %1").arg(MinVerStr()));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ver > MaxVer())
|
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Invalid version. Maximum supported version is %1").arg(MaxVerStr()));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
2016-01-19 16:25:46 +01:00
|
|
|
|
|
|
|
const QString errorMsg(tr("Unexpected version \"%1\".").arg(ver, 0, 16));
|
|
|
|
throw VException(errorMsg);
|
2014-12-11 20:44:48 +01:00
|
|
|
}
|
2014-12-16 08:42:24 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VAbstractConverter::SaveDocument(const QString &fileName, QString &error) const
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
TestUniqueId();
|
|
|
|
}
|
|
|
|
catch (const VExceptionWrongId &e)
|
|
|
|
{
|
2015-03-16 13:23:02 +01:00
|
|
|
Q_UNUSED(e)
|
2014-12-16 08:42:24 +01:00
|
|
|
error = tr("Error no unique id.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return VDomDocument::SaveDocument(fileName, error);
|
|
|
|
}
|
|
|
|
|
2016-01-19 16:25:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
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.
|
2016-08-02 14:12:13 +02:00
|
|
|
throw;
|
2016-01-19 16:25:46 +01:00
|
|
|
}
|
|
|
|
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.
|
2016-08-02 14:12:13 +02:00
|
|
|
throw;
|
2016-01-19 16:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return; // All is fine and we can try to convert to current max version.
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidateXML(schema, fileName);
|
|
|
|
}
|
|
|
|
|
2014-12-16 08:42:24 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractConverter::Save() const
|
|
|
|
{
|
|
|
|
QString error;
|
|
|
|
if (SaveDocument(fileName, error) == false)
|
|
|
|
{
|
|
|
|
VException e(error);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractConverter::SetVersion(const QString &version)
|
|
|
|
{
|
2016-01-19 16:25:46 +01:00
|
|
|
ValidateVersion(version);
|
|
|
|
|
2014-12-16 08:42:24 +01:00
|
|
|
if (setTagText(TagVersion, version) == false)
|
|
|
|
{
|
|
|
|
VException e(tr("Could not change version."));
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|