2014-12-10 18:18:22 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vabstractconverter.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 10 12, 2014
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2014-12-10 18:18:22 +01:00
|
|
|
** 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)
|
2017-02-04 12:28:47 +01:00
|
|
|
: VDomDocument(),
|
|
|
|
m_ver(0x0),
|
|
|
|
m_convertedFileName(fileName),
|
|
|
|
m_tmpFile()
|
2014-12-10 18:18:22 +01:00
|
|
|
{
|
2017-02-04 12:28:47 +01:00
|
|
|
setXMLContent(m_convertedFileName);// Throw an exception on error
|
|
|
|
m_ver = GetVersion(GetVersionStr());
|
2014-12-11 16:51:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-11 21:54:33 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-02-04 12:28:47 +01:00
|
|
|
QString VAbstractConverter::Convert()
|
2014-12-11 21:54:33 +01:00
|
|
|
{
|
2017-02-04 12:28:47 +01:00
|
|
|
if (m_ver == MaxVer())
|
2014-12-11 21:54:33 +01:00
|
|
|
{
|
2017-02-04 12:28:47 +01:00
|
|
|
return m_convertedFileName;
|
2014-12-11 21:54:33 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 10:27:30 +01:00
|
|
|
if (not IsReadOnly())
|
|
|
|
{
|
|
|
|
ReserveFile();
|
|
|
|
}
|
2015-08-27 10:45:31 +02:00
|
|
|
|
2017-02-04 12:28:47 +01:00
|
|
|
if (m_tmpFile.open())
|
2016-01-19 16:25:46 +01:00
|
|
|
{
|
2017-02-04 12:28:47 +01:00
|
|
|
m_convertedFileName = m_tmpFile.fileName();
|
2016-01-19 16:25:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-21 16:28:25 +02:00
|
|
|
const QString errorMsg(tr("Error openning a temp file: %1.").arg(m_tmpFile.errorString()));
|
2017-02-04 12:28:47 +01:00
|
|
|
throw VException(errorMsg);
|
2016-01-19 16:25:46 +01:00
|
|
|
}
|
2014-12-11 21:54:33 +01:00
|
|
|
|
2017-02-04 12:28:47 +01:00
|
|
|
m_ver < MaxVer() ? ApplyPatches() : DowngradeToCurrentMaxVersion();
|
|
|
|
|
|
|
|
return m_convertedFileName;
|
2014-12-11 21:54:33 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 08:49:05 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VAbstractConverter::GetCurrentFormatVarsion() const
|
|
|
|
{
|
|
|
|
return m_ver;
|
|
|
|
}
|
|
|
|
|
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;
|
2017-02-04 12:28:47 +01:00
|
|
|
QFileInfo info(m_convertedFileName);
|
|
|
|
const QString reserveFileName = QString("%1/%2(v%3).%4.bak")
|
2015-08-27 10:45:31 +02:00
|
|
|
.arg(info.absoluteDir().absolutePath())
|
|
|
|
.arg(info.baseName())
|
|
|
|
.arg(GetVersionStr())
|
|
|
|
.arg(info.completeSuffix());
|
2017-02-04 12:28:47 +01:00
|
|
|
if (not SafeCopy(m_convertedFileName, reserveFileName, error))
|
2016-10-30 13:34:55 +01:00
|
|
|
{
|
2017-02-04 12:28:47 +01:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
qt_ntfs_permission_lookup++; // turn checking on
|
|
|
|
#endif /*Q_OS_WIN32*/
|
|
|
|
const bool isFileWritable = info.isWritable();
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
qt_ntfs_permission_lookup--; // turn it off again
|
|
|
|
#endif /*Q_OS_WIN32*/
|
|
|
|
|
|
|
|
if (not IsReadOnly() && isFileWritable)
|
2016-10-30 13:34:55 +01:00
|
|
|
{
|
2017-02-04 12:28:47 +01:00
|
|
|
const QString errorMsg(tr("Error creating a reserv copy: %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())
|
|
|
|
{
|
2018-01-13 12:42:12 +01:00
|
|
|
const QString errorMsg(tr("Invalid version. Minimum supported format version is %1").arg(MinVerStr()));
|
2014-12-11 20:44:48 +01:00
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ver > MaxVer())
|
|
|
|
{
|
2018-01-13 12:42:12 +01:00
|
|
|
const QString errorMsg(tr("Invalid version. Maximum supported format version is %1").arg(MaxVerStr()));
|
2014-12-11 20:44:48 +01:00
|
|
|
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
|
|
|
|
2016-01-19 16:25:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractConverter::ValidateInputFile(const QString ¤tSchema) const
|
|
|
|
{
|
|
|
|
QString schema;
|
|
|
|
try
|
|
|
|
{
|
2017-02-04 12:28:47 +01:00
|
|
|
schema = XSDSchema(m_ver);
|
2016-01-19 16:25:46 +01:00
|
|
|
}
|
|
|
|
catch(const VException &e)
|
|
|
|
{
|
2017-02-04 12:28:47 +01:00
|
|
|
if (m_ver < MinVer())
|
2016-01-19 16:25:46 +01:00
|
|
|
{ // 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
|
|
|
}
|
2017-02-04 12:28:47 +01:00
|
|
|
else if (m_ver > MaxVer())
|
2016-01-19 16:25:46 +01:00
|
|
|
{ // Version bigger than maximum supported version. We still have a chance to open the file.
|
|
|
|
try
|
|
|
|
{ // Try to open like the current version.
|
2017-02-04 12:28:47 +01:00
|
|
|
ValidateXML(currentSchema, m_convertedFileName);
|
2016-01-19 16:25:46 +01:00
|
|
|
}
|
|
|
|
catch(const VException &exp)
|
|
|
|
{ // Nope, we can't.
|
2016-12-20 20:19:21 +01:00
|
|
|
Q_UNUSED(exp)
|
2016-01-19 16:25:46 +01:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
2017-02-04 12:28:47 +01:00
|
|
|
ValidateXML(schema, m_convertedFileName);
|
2016-01-19 16:25:46 +01:00
|
|
|
}
|
|
|
|
|
2014-12-16 08:42:24 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-02-07 12:44:33 +01:00
|
|
|
void VAbstractConverter::Save()
|
2014-12-16 08:42:24 +01:00
|
|
|
{
|
2017-02-07 12:44:33 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
TestUniqueId();
|
|
|
|
}
|
|
|
|
catch (const VExceptionWrongId &e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e)
|
|
|
|
VException ex(tr("Error no unique id."));
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_tmpFile.resize(0);//clear previous content
|
|
|
|
const int indent = 4;
|
|
|
|
QTextStream out(&m_tmpFile);
|
|
|
|
out.setCodec("UTF-8");
|
|
|
|
save(out, indent);
|
|
|
|
|
|
|
|
if (not m_tmpFile.flush())
|
2014-12-16 08:42:24 +01:00
|
|
|
{
|
2017-02-07 12:44:33 +01:00
|
|
|
VException e(m_tmpFile.errorString());
|
2014-12-16 08:42:24 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|