2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file vdomdocument.cpp
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2013 Valentina project
|
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-09-18 21:16:19 +02:00
|
|
|
** 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.
|
|
|
|
**
|
2013-10-27 13:36:29 +01:00
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
2013-09-18 21:16:19 +02:00
|
|
|
** 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/>.
|
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-09-18 21:16:19 +02:00
|
|
|
|
2013-07-13 12:51:31 +02:00
|
|
|
#include "vdomdocument.h"
|
2013-11-06 16:49:07 +01:00
|
|
|
#include "../exception/vexceptionconversionerror.h"
|
|
|
|
#include "../exception/vexceptionemptyparameter.h"
|
|
|
|
#include "../exception/vexceptionbadid.h"
|
2013-08-13 18:48:36 +02:00
|
|
|
|
2014-02-25 15:55:02 +01:00
|
|
|
#include <QAbstractMessageHandler>
|
|
|
|
#include <QXmlSchema>
|
|
|
|
#include <QXmlSchemaValidator>
|
|
|
|
#include <QFile>
|
2014-04-17 19:18:26 +02:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QTemporaryFile>
|
2014-02-25 15:55:02 +01:00
|
|
|
|
|
|
|
//This class need for validation pattern file using XSD shema
|
|
|
|
class MessageHandler : public QAbstractMessageHandler
|
|
|
|
{
|
|
|
|
public:
|
2014-02-26 10:22:05 +01:00
|
|
|
MessageHandler() : QAbstractMessageHandler(), m_messageType(QtMsgType()), m_description(QString()),
|
2014-02-25 15:55:02 +01:00
|
|
|
m_sourceLocation(QSourceLocation()){}
|
2014-02-26 09:18:59 +01:00
|
|
|
QString statusMessage() const;
|
|
|
|
qint64 line() const;
|
|
|
|
qint64 column() const;
|
2014-02-25 15:55:02 +01:00
|
|
|
protected:
|
2014-05-11 20:15:32 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-02-25 15:55:02 +01:00
|
|
|
virtual void handleMessage(QtMsgType type, const QString &description,
|
|
|
|
const QUrl &identifier, const QSourceLocation &sourceLocation)
|
|
|
|
{
|
|
|
|
Q_UNUSED(type);
|
|
|
|
Q_UNUSED(identifier);
|
|
|
|
|
|
|
|
m_messageType = type;
|
|
|
|
m_description = description;
|
|
|
|
m_sourceLocation = sourceLocation;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
QtMsgType m_messageType;
|
|
|
|
QString m_description;
|
|
|
|
QSourceLocation m_sourceLocation;
|
|
|
|
};
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-02-26 09:18:59 +01:00
|
|
|
inline QString MessageHandler::statusMessage() const
|
|
|
|
{
|
|
|
|
return m_description;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-02-26 09:18:59 +01:00
|
|
|
inline qint64 MessageHandler::line() const
|
|
|
|
{
|
|
|
|
return m_sourceLocation.line();
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-02-26 09:18:59 +01:00
|
|
|
inline qint64 MessageHandler::column() const
|
|
|
|
{
|
|
|
|
return m_sourceLocation.column();
|
|
|
|
}
|
|
|
|
|
2014-05-16 16:11:31 +02:00
|
|
|
const QString VDomDocument::AttrId = QStringLiteral("id");
|
|
|
|
const QString VDomDocument::AttrUnit = QStringLiteral("unit");
|
|
|
|
const QString VDomDocument::UnitMM = QStringLiteral("mm");
|
|
|
|
const QString VDomDocument::UnitCM = QStringLiteral("cm");
|
2014-03-24 16:02:57 +01:00
|
|
|
const QString VDomDocument::UnitINCH = QStringLiteral("inch");
|
2014-05-16 16:11:31 +02:00
|
|
|
const QString VDomDocument::TagVersion = QStringLiteral("version");
|
2014-03-21 11:08:29 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-02-25 15:02:09 +01:00
|
|
|
VDomDocument::VDomDocument(VContainer *data)
|
|
|
|
: QDomDocument(), data(data), map(QHash<QString, QDomElement>())
|
2014-05-02 13:11:30 +02:00
|
|
|
{}
|
2013-07-13 12:51:31 +02:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-04 21:35:15 +01:00
|
|
|
QDomElement VDomDocument::elementById(const QString& id)
|
|
|
|
{
|
|
|
|
if (map.contains(id))
|
|
|
|
{
|
2013-07-13 12:51:31 +02:00
|
|
|
QDomElement e = map[id];
|
2013-11-04 21:35:15 +01:00
|
|
|
if (e.parentNode().nodeType() != QDomNode::BaseNode)
|
|
|
|
{
|
2013-07-13 12:51:31 +02:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
map.remove(id);
|
|
|
|
}
|
|
|
|
|
2014-02-25 15:02:09 +01:00
|
|
|
if (this->find(this->documentElement(), id))
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-07-13 12:51:31 +02:00
|
|
|
return map[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return QDomElement();
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-01-02 16:50:01 +01:00
|
|
|
void VDomDocument::removeAllChilds(QDomElement &element)
|
|
|
|
{
|
|
|
|
QDomNode domNode = element.firstChild();
|
|
|
|
while (domNode.isNull() == false)
|
|
|
|
{
|
|
|
|
if (domNode.isElement())
|
|
|
|
{
|
|
|
|
QDomElement domElement = domNode.toElement();
|
|
|
|
if (domElement.isNull() == false)
|
|
|
|
{
|
|
|
|
element.removeChild(domElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
domNode = element.firstChild();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-06 22:11:12 +01:00
|
|
|
bool VDomDocument::find(const QDomElement &node, const QString& id)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-03-21 11:08:29 +01:00
|
|
|
if (node.hasAttribute(AttrId))
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-03-21 11:08:29 +01:00
|
|
|
const QString value = node.attribute(AttrId);
|
2013-07-13 12:51:31 +02:00
|
|
|
this->map[value] = node;
|
2013-11-04 21:35:15 +01:00
|
|
|
if (value == id)
|
|
|
|
{
|
2013-07-13 12:51:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 21:35:15 +01:00
|
|
|
for (qint32 i=0; i<node.childNodes().length(); ++i)
|
|
|
|
{
|
2013-07-13 12:51:31 +02:00
|
|
|
QDomNode n = node.childNodes().at(i);
|
2013-11-04 21:35:15 +01:00
|
|
|
if (n.isElement())
|
|
|
|
{
|
2014-02-25 15:02:09 +01:00
|
|
|
if (this->find(n.toElement(), id))
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-07-13 12:51:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-02-25 15:40:24 +01:00
|
|
|
quint32 VDomDocument::GetParametrUInt(const QDomElement &domElement, const QString &name, const QString &defValue) const
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
|
|
|
Q_ASSERT_X(name.isEmpty() == false, Q_FUNC_INFO, "name of parametr is empty");
|
|
|
|
Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null");
|
2014-03-14 16:59:28 +01:00
|
|
|
|
2013-09-11 16:14:21 +02:00
|
|
|
bool ok = false;
|
2014-03-14 16:59:28 +01:00
|
|
|
QString parametr;
|
|
|
|
quint32 id = 0;
|
|
|
|
|
|
|
|
QString message = tr("Can't convert toUInt parameter");
|
|
|
|
try
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-03-14 16:59:28 +01:00
|
|
|
parametr = GetParametrString(domElement, name, defValue);
|
|
|
|
id = parametr.toUInt(&ok);
|
|
|
|
if (ok == false)
|
|
|
|
{
|
|
|
|
throw VExceptionConversionError(message, name);
|
|
|
|
}
|
2013-09-23 14:08:06 +02:00
|
|
|
}
|
2014-03-14 16:59:28 +01:00
|
|
|
catch (const VExceptionEmptyParameter &e)
|
|
|
|
{
|
|
|
|
VExceptionConversionError excep(message, name);
|
|
|
|
excep.AddMoreInformation(e.ErrorMessage());
|
|
|
|
throw excep;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:14:21 +02:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-12-23 20:50:30 +01:00
|
|
|
QString VDomDocument::GetParametrString(const QDomElement &domElement, const QString &name,
|
|
|
|
const QString &defValue) const
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
|
|
|
Q_ASSERT_X(name.isEmpty() == false, Q_FUNC_INFO, "name of parametr is empty");
|
|
|
|
Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null");
|
2014-02-25 15:02:09 +01:00
|
|
|
const QString parameter = domElement.attribute(name, defValue);
|
2013-11-04 21:35:15 +01:00
|
|
|
if (parameter.isEmpty())
|
|
|
|
{
|
2014-03-14 15:12:53 +01:00
|
|
|
if (defValue.isEmpty())
|
|
|
|
{
|
|
|
|
throw VExceptionEmptyParameter(tr("Got empty parameter"), name, domElement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return defValue;
|
|
|
|
}
|
2013-09-23 14:08:06 +02:00
|
|
|
}
|
|
|
|
return parameter;
|
2013-09-11 16:14:21 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-12-23 20:50:30 +01:00
|
|
|
qreal VDomDocument::GetParametrDouble(const QDomElement &domElement, const QString &name, const QString &defValue) const
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
|
|
|
Q_ASSERT_X(name.isEmpty() == false, Q_FUNC_INFO, "name of parametr is empty");
|
|
|
|
Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null");
|
2014-03-14 16:59:28 +01:00
|
|
|
|
2013-09-11 16:14:21 +02:00
|
|
|
bool ok = false;
|
2014-03-14 16:59:28 +01:00
|
|
|
qreal param = 0;
|
|
|
|
|
|
|
|
QString message = tr("Can't convert toDouble parameter");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
QString parametr = GetParametrString(domElement, name, defValue);
|
|
|
|
param = parametr.replace(",", ".").toDouble(&ok);
|
|
|
|
if (ok == false)
|
|
|
|
{
|
|
|
|
throw VExceptionConversionError(message, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const VExceptionEmptyParameter &e)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-03-14 16:59:28 +01:00
|
|
|
VExceptionConversionError excep(message, name);
|
|
|
|
excep.AddMoreInformation(e.ErrorMessage());
|
|
|
|
throw excep;
|
2013-09-23 14:08:06 +02:00
|
|
|
}
|
2013-09-11 16:14:21 +02:00
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-02-25 15:02:09 +01:00
|
|
|
QString VDomDocument::UniqueTagText(const QString &tagName, const QString &defVal) const
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-02-25 15:02:09 +01:00
|
|
|
const QDomNodeList nodeList = this->elementsByTagName(tagName);
|
2014-02-19 14:32:02 +01:00
|
|
|
if (nodeList.isEmpty())
|
|
|
|
{
|
|
|
|
return defVal;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-25 15:02:09 +01:00
|
|
|
const QDomNode domNode = nodeList.at(0);
|
2014-02-19 14:32:02 +01:00
|
|
|
if (domNode.isNull() == false && domNode.isElement())
|
|
|
|
{
|
2014-02-25 15:02:09 +01:00
|
|
|
const QDomElement domElement = domNode.toElement();
|
2014-02-19 14:32:02 +01:00
|
|
|
if (domElement.isNull() == false)
|
|
|
|
{
|
|
|
|
return domElement.text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defVal;
|
|
|
|
}
|
2014-02-25 15:55:02 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-03-24 16:02:57 +01:00
|
|
|
void VDomDocument::ValidateXML(const QString &schema, const QString &fileName)
|
2014-02-25 15:55:02 +01:00
|
|
|
{
|
|
|
|
QFile pattern(fileName);
|
|
|
|
if (pattern.open(QIODevice::ReadOnly) == false)
|
|
|
|
{
|
2014-03-24 16:02:57 +01:00
|
|
|
const QString errorMsg(tr("Can't open file %1:\n%2.").arg(fileName).arg(pattern.errorString()));
|
|
|
|
throw VException(errorMsg);
|
2014-02-25 15:55:02 +01:00
|
|
|
}
|
2014-03-24 16:02:57 +01:00
|
|
|
|
2014-02-25 15:55:02 +01:00
|
|
|
QFile fileSchema(schema);
|
|
|
|
if (fileSchema.open(QIODevice::ReadOnly) == false)
|
|
|
|
{
|
2014-03-24 16:02:57 +01:00
|
|
|
pattern.close();
|
|
|
|
const QString errorMsg(tr("Can't open schema file %1:\n%2.").arg(schema).arg(fileSchema.errorString()));
|
|
|
|
throw VException(errorMsg);
|
2014-02-25 15:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageHandler messageHandler;
|
|
|
|
QXmlSchema sch;
|
|
|
|
sch.setMessageHandler(&messageHandler);
|
|
|
|
sch.load(&fileSchema, QUrl::fromLocalFile(fileSchema.fileName()));
|
|
|
|
|
|
|
|
bool errorOccurred = false;
|
|
|
|
if (sch.isValid() == false)
|
|
|
|
{
|
|
|
|
errorOccurred = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QXmlSchemaValidator validator(sch);
|
|
|
|
if (validator.validate(&pattern, QUrl::fromLocalFile(pattern.fileName())) == false)
|
|
|
|
{
|
|
|
|
errorOccurred = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorOccurred)
|
|
|
|
{
|
2014-03-24 16:02:57 +01:00
|
|
|
pattern.close();
|
|
|
|
fileSchema.close();
|
2014-03-14 18:54:20 +01:00
|
|
|
VException e(messageHandler.statusMessage());
|
2014-05-01 13:33:40 +02:00
|
|
|
e.AddMoreInformation(tr("Validation error in line %1 column %2").arg(messageHandler.line())
|
|
|
|
.arg(messageHandler.column()));
|
2014-03-14 18:54:20 +01:00
|
|
|
throw e;
|
2014-02-25 15:55:02 +01:00
|
|
|
}
|
2014-03-24 16:02:57 +01:00
|
|
|
pattern.close();
|
|
|
|
fileSchema.close();
|
2014-03-14 18:54:20 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-03-24 16:02:57 +01:00
|
|
|
void VDomDocument::setContent(const QString &fileName)
|
2014-03-14 18:54:20 +01:00
|
|
|
{
|
2014-03-24 16:02:57 +01:00
|
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QIODevice::ReadOnly) == false)
|
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Can't open file %1:\n%2.").arg(fileName).arg(file.errorString()));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
2014-03-14 18:54:20 +01:00
|
|
|
QString errorMsg;
|
2014-03-24 16:02:57 +01:00
|
|
|
int errorLine = -1;
|
|
|
|
int errorColumn = -1;
|
|
|
|
if (QDomDocument::setContent(&file, &errorMsg, &errorLine, &errorColumn) == false)
|
2014-02-25 15:55:02 +01:00
|
|
|
{
|
2014-03-24 16:02:57 +01:00
|
|
|
file.close();
|
|
|
|
VException e(errorMsg);
|
|
|
|
e.AddMoreInformation(tr("Parcing error in line %1 column %2").arg(errorLine).arg(errorColumn));
|
|
|
|
throw e;
|
2014-02-25 15:55:02 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-14 15:12:53 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-03-24 16:02:57 +01:00
|
|
|
Valentina::Units VDomDocument::StrToUnits(const QString &unit)
|
2014-03-14 15:12:53 +01:00
|
|
|
{
|
|
|
|
QStringList units;
|
2014-03-24 16:02:57 +01:00
|
|
|
units << UnitMM << UnitCM << UnitINCH;
|
2014-03-14 15:12:53 +01:00
|
|
|
Valentina::Units result = Valentina::Cm;
|
|
|
|
switch (units.indexOf(unit))
|
|
|
|
{
|
|
|
|
case 0:// mm
|
|
|
|
result = Valentina::Mm;
|
|
|
|
break;
|
|
|
|
case 1:// cm
|
|
|
|
result = Valentina::Cm;
|
|
|
|
break;
|
2014-03-24 16:02:57 +01:00
|
|
|
case 2:// inch
|
|
|
|
result = Valentina::Inch;
|
2014-03-14 15:12:53 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = Valentina::Cm;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-03-24 16:02:57 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-03-24 16:02:57 +01:00
|
|
|
QString VDomDocument::UnitsToStr(const Valentina::Units &unit)
|
|
|
|
{
|
|
|
|
QString result;
|
2014-05-01 13:33:40 +02:00
|
|
|
switch (unit)
|
2014-03-24 16:02:57 +01:00
|
|
|
{
|
|
|
|
case Valentina::Mm:
|
|
|
|
result = "mm";
|
|
|
|
break;
|
|
|
|
case Valentina::Cm:
|
|
|
|
result = "cm";
|
|
|
|
break;
|
|
|
|
case Valentina::Inch:
|
|
|
|
result = "inch";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = "cm";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-03-28 14:11:46 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-03-28 14:11:46 +01:00
|
|
|
bool VDomDocument::SaveDocument(const QString &fileName)
|
|
|
|
{
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
{
|
|
|
|
qDebug()<<"Got empty file name.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//Writing in temporary file
|
|
|
|
QFileInfo tempInfo(fileName);
|
|
|
|
QString temp = tempInfo.absolutePath() + "/" + tempInfo.baseName() + ".tmp";
|
|
|
|
QFile tempFile(temp);
|
|
|
|
if (tempFile.open(QIODevice::WriteOnly| QIODevice::Truncate))
|
|
|
|
{
|
|
|
|
const int indent = 4;
|
|
|
|
QTextStream out(&tempFile);
|
|
|
|
out.setCodec("UTF-8");
|
|
|
|
save(out, indent);
|
|
|
|
tempFile.close();
|
|
|
|
}
|
|
|
|
//Replace temp file our
|
|
|
|
bool result = false;
|
|
|
|
QFile patternFile(fileName);
|
|
|
|
// We need here temporary file because we need restore document after error of copying temp file.
|
|
|
|
QTemporaryFile tempOfPattern;
|
|
|
|
if (tempOfPattern.open())
|
|
|
|
{
|
|
|
|
patternFile.copy(tempOfPattern.fileName());
|
|
|
|
}
|
|
|
|
if ( patternFile.exists() == false || patternFile.remove() )
|
|
|
|
{
|
|
|
|
if ( tempFile.copy(patternFile.fileName()) == false )
|
|
|
|
{
|
|
|
|
qDebug()<<"Could not copy temp file to document file"<<Q_FUNC_INFO;
|
|
|
|
tempOfPattern.copy(fileName);
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tempFile.remove();
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug()<<"Could not remove document file"<<Q_FUNC_INFO;
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-05-16 16:11:31 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VDomDocument::Major() const
|
|
|
|
{
|
|
|
|
QString version = UniqueTagText(TagVersion, "0.0.0");
|
|
|
|
QStringList v = version.split(".");
|
|
|
|
return v.at(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VDomDocument::Minor() const
|
|
|
|
{
|
|
|
|
QString version = UniqueTagText(TagVersion, "0.0.0");
|
|
|
|
QStringList v = version.split(".");
|
|
|
|
return v.at(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VDomDocument::Patch() const
|
|
|
|
{
|
|
|
|
QString version = UniqueTagText(TagVersion, "0.0.0");
|
|
|
|
QStringList v = version.split(".");
|
|
|
|
return v.at(2);
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-03-28 14:11:46 +01:00
|
|
|
void VDomDocument::setTagText(const QString &tag, const QString &text)
|
|
|
|
{
|
|
|
|
const QDomNodeList nodeList = this->elementsByTagName(tag);
|
|
|
|
if (nodeList.isEmpty())
|
|
|
|
{
|
|
|
|
qDebug()<<"Can't save tag "<<tag<<Q_FUNC_INFO;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const QDomNode domNode = nodeList.at(0);
|
|
|
|
if (domNode.isNull() == false && domNode.isElement())
|
|
|
|
{
|
|
|
|
const QDomElement domElement = domNode.toElement();
|
|
|
|
if (domElement.isNull() == false)
|
|
|
|
{
|
|
|
|
QDomElement parent = domElement.parentNode().toElement();
|
|
|
|
QDomElement newTag = createElement(tag);
|
|
|
|
const QDomText newTagText = createTextNode(text);
|
|
|
|
newTag.appendChild(newTagText);
|
|
|
|
|
|
|
|
parent.replaceChild(newTag, domElement);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|