56 #include <QDomElement>
58 #include <QDomNodeList>
61 #include <QLatin1String>
63 #include <QRegularExpression>
64 #include <QRegularExpressionMatch>
65 #include <QStaticStringData>
66 #include <QStringData>
67 #include <QStringDataPtr>
68 #include <QStringList>
70 #include "../exception/vexception.h"
71 #include "../exception/vexceptionwrongid.h"
78 m_convertedFileName(fileName),
84 qDebug() <<
"VAbstractConverter::GetVersion() = " <<
m_ver;
106 const QString errorMsg(tr(
"Error Opening a temp file: %1.").arg(
m_tmpFile.errorString()));
110 qDebug() <<
" m_ver = " <<
m_ver;
111 qDebug() <<
" MaxVer = " <<
MaxVer();
127 const QDomNodeList nodeList = this->elementsByTagName(
TagVersion);
128 if (nodeList.isEmpty())
130 const QString errorMsg(tr(
"Couldn't get version information."));
134 if (nodeList.count() > 1)
136 const QString errorMsg(tr(
"Too many tags <%1> in file.").arg(
TagVersion));
140 const QDomNode domNode = nodeList.at(0);
141 if (domNode.isNull() ==
false && domNode.isElement())
143 const QDomElement domElement = domNode.toElement();
144 if (domElement.isNull() ==
false)
146 qDebug() <<
" VAbstractConverter::GetVersionStr()" << domElement.text();
147 return domElement.text();
150 return QString(QStringLiteral(
"0.0.0"));
158 const QStringList ver = version.split(
".");
161 const int major = ver.at(0).toInt(&ok);
168 const int minor = ver.at(1).toInt(&ok);
175 const int patch = ver.at(2).toInt(&ok);
180 return (major<<16)|(minor<<8)|(patch);
192 const QRegularExpression newVersionRx(QStringLiteral(
"_v\\d\\d\\d"));
193 QString fileNameWithoutVersion;
194 int dotPos = fileName.lastIndexOf(QLatin1Char(
'.'));
195 int versionPos = fileName.indexOf(QLatin1String(
"(v"));
200 int lastParenthesisPos = fileName.lastIndexOf(QLatin1Char(
')'));
201 if (lastParenthesisPos > versionPos && lastParenthesisPos < dotPos)
203 dotPos = lastParenthesisPos + 1;
206 fileNameWithoutVersion = fileName.left(versionPos);
208 else if ((versionPos = fileName.indexOf(newVersionRx)) > -1)
211 dotPos = fileName.indexOf(QLatin1Char(
'.'), versionPos);
212 fileNameWithoutVersion = fileName.left(versionPos);
216 fileNameWithoutVersion = fileName;
219 if (versionPos > -1 && dotPos > versionPos)
221 fileNameWithoutVersion += fileName.mid(dotPos);
224 return fileNameWithoutVersion;
230 QString newFileName = fileName;
231 int dotPos = newFileName.lastIndexOf(QLatin1Char(
'.'));
233 while (dotPos > -1 && newFileName.mid(dotPos) == QLatin1String(
".bak"))
235 newFileName = newFileName.left(dotPos);
236 dotPos = newFileName.lastIndexOf(QLatin1Char(
'.'));
245 const QRegularExpression rx(QStringLiteral(
"^(0|([1-9][0-9]*)).(0|([1-9][0-9]*)).(0|([1-9][0-9]*))$"));
247 if (rx.match(version).hasMatch() ==
false)
249 const QString errorMsg(tr(
"Version \"%1\" invalid.").arg(version));
253 if (version == QLatin1String(
"0.0.0"))
255 const QString errorMsg(tr(
"Version \"0.0.0\" invalid."));
267 const QString baseNameWithoutVersion = info.baseName();
268 const QString versionWithoutDots =
GetVersionStr().remove(QLatin1Char(
'.'));
269 const QString baseFileName = QString(
"%1_v%2")
270 .arg(baseNameWithoutVersion)
271 .arg(versionWithoutDots);
272 QString sequencePart;
273 int sequenceNumber = 1;
274 QString reserveFileName;
277 reserveFileName = QString(
"%1/%2%3.%4")
278 .arg(info.absoluteDir().absolutePath())
281 .arg(info.completeSuffix());
282 sequencePart = QString(
"_(%1)").arg(++sequenceNumber);
283 }
while (QFileInfo(reserveFileName).exists());
288 qt_ntfs_permission_lookup++;
290 const bool isFileWritable = info.isWritable();
292 qt_ntfs_permission_lookup--;
297 const QString errorMsg(tr(
"Error creating a reserv copy: %1.").arg(error));
307 formula.replace(position, token.length(), newName);
308 bias = token.length() - newName.length();
327 while (i != tokens.constEnd())
329 if (i.key()<= position)
331 newTokens.insert(i.key(), i.value());
335 newTokens.insert(i.key()-bias, i.value());
347 const QString errorMsg(tr(
"Invalid version. Minimum supported version is %1").arg(
MinVerStr()));
353 const QString errorMsg(tr(
"Invalid version. Maximum supported version is %1").arg(
MaxVerStr()));
357 const QString errorMsg(tr(
"Unexpected version \"%1\".").arg(ver, 0, 16));
413 const int indent = 4;
415 out.setCodec(
"UTF-8");
432 VException e(tr(
"Could not change version."));
static int GetVersion(const QString &version)
static QString removeVersionNumber(const QString &fileName)
Removes version number from.
virtual void DowngradeToCurrentMaxVersion()=0
virtual bool IsReadOnly() const =0
void SetVersion(const QString &version)
void Replace(QString &formula, const QString &newName, int position, const QString &token, int &bias) const
virtual QString MaxVerStr() const =0
int GetCurrentFormatVarsion() const
static void BiasTokens(int position, int bias, QMap< int, QString > &tokens)
static void ValidateVersion(const QString &version)
Q_NORETURN void InvalidVersion(int ver) const
virtual QString XSDSchema(int ver) const =0
virtual int MaxVer() const =0
static QString removeBakExtension(const QString &fileName)
Removes single or repeated '.bak' extension (as long as it is at the end of.
void CorrectionsPositions(int position, int bias, QMap< int, QString > &tokens) const
virtual int MinVer() const =0
void ValidateInputFile(const QString ¤tSchema) const
QString GetVersionStr() const
VAbstractConverter(const QString &fileName)
virtual QString MinVerStr() const =0
QString m_convertedFileName
virtual void ApplyPatches()=0
The VDomDocument class represents a Seamly2D document (.val file).
static void ValidateXML(const QString &schema, const QString &fileName)
ValidateXML validate xml file by xsd schema.
void TestUniqueId() const
TestUniqueId test exist unique id in pattern file. Each id must be unique.
bool setTagText(const QString &tag, const QString &text)
static bool SafeCopy(const QString &source, const QString &destination, QString &error)
static const QString TagVersion
virtual void setXMLContent(const QString &fileName)
The VExceptionWrongId class for exception wrong id.
The VException class parent for all exception. Could be use for abstract exception.