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.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2013-11-15 13:41:26 +01:00
|
|
|
** <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"
|
2014-12-10 19:33:20 +01:00
|
|
|
#include "exception/vexceptionconversionerror.h"
|
|
|
|
#include "exception/vexceptionemptyparameter.h"
|
|
|
|
#include "exception/vexceptionbadid.h"
|
2014-12-16 08:42:24 +01:00
|
|
|
#include "exception/vexceptionwrongid.h"
|
2013-08-13 18:48:36 +02:00
|
|
|
|
2015-12-19 20:02:38 +01:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 1, 0)
|
|
|
|
# include "../vmisc/backport/qsavefile.h"
|
|
|
|
#else
|
|
|
|
# include <QSaveFile>
|
|
|
|
#endif
|
|
|
|
|
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,
|
2015-07-03 15:36:54 +02:00
|
|
|
const QUrl &identifier, const QSourceLocation &sourceLocation) Q_DECL_OVERRIDE;
|
2014-02-25 15:55:02 +01:00
|
|
|
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-06-02 09:43:27 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-16 19:08:23 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-06-02 09:43:27 +02:00
|
|
|
void MessageHandler::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;
|
|
|
|
}
|
|
|
|
|
2014-11-24 14:03:34 +01:00
|
|
|
Q_LOGGING_CATEGORY(vXML, "v.xml")
|
2014-11-20 18:52:51 +01:00
|
|
|
|
2014-05-16 16:11:31 +02:00
|
|
|
const QString VDomDocument::AttrId = QStringLiteral("id");
|
|
|
|
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");
|
2015-01-14 15:14:51 +01:00
|
|
|
const QString VDomDocument::UnitPX = QStringLiteral("px");
|
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-12-10 19:33:20 +01:00
|
|
|
VDomDocument::VDomDocument()
|
|
|
|
: QDomDocument(), map(QHash<QString, QDomElement>())
|
2014-05-02 13:11:30 +02:00
|
|
|
{}
|
2013-07-13 12:51:31 +02:00
|
|
|
|
2014-06-02 09:43:27 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VDomDocument::~VDomDocument()
|
|
|
|
{}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Finds an element by id.
|
|
|
|
* @param id value id attribute.
|
|
|
|
* @return dom element.
|
|
|
|
*/
|
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();
|
|
|
|
}
|
|
|
|
|
2015-02-11 11:43:50 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QDomElement VDomDocument::elementById(quint32 id)
|
|
|
|
{
|
|
|
|
return elementById(QString().setNum(id));
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Removes all children of a given element tag. RENAME: removeAllChildren
|
|
|
|
* @param element tag
|
|
|
|
*/
|
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
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Find element by id.
|
|
|
|
* @param node node
|
|
|
|
* @param id id value
|
|
|
|
* @return true if found
|
|
|
|
*/
|
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-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns the long long value of the given attribute. RENAME: GetParameterLongLong?
|
|
|
|
* @param domElement tag in xml tree
|
|
|
|
* @param name attribute name
|
|
|
|
* @return long long value
|
|
|
|
*/
|
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
|
|
|
{
|
2015-10-28 15:22:36 +01:00
|
|
|
Q_ASSERT_X(not name.isEmpty(), Q_FUNC_INFO, "name of parametr is empty");
|
|
|
|
Q_ASSERT_X(not domElement.isNull(), Q_FUNC_INFO, "domElement is null"); //-V591
|
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-08-07 13:17:24 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VDomDocument::GetParametrBool(const QDomElement &domElement, const QString &name, const QString &defValue) const
|
|
|
|
{
|
2015-10-28 15:22:36 +01:00
|
|
|
Q_ASSERT_X(not name.isEmpty(), Q_FUNC_INFO, "name of parametr is empty");
|
|
|
|
Q_ASSERT_X(not domElement.isNull(), Q_FUNC_INFO, "domElement is null");
|
2014-08-07 13:17:24 +02:00
|
|
|
|
|
|
|
QString parametr;
|
|
|
|
bool val = true;
|
|
|
|
|
|
|
|
QString message = tr("Can't convert toBool parameter");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
parametr = GetParametrString(domElement, name, defValue);
|
|
|
|
|
2014-10-17 22:26:30 +02:00
|
|
|
QStringList bools = QStringList() << QLatin1String("true") << QLatin1String("false");
|
2014-08-07 13:17:24 +02:00
|
|
|
switch (bools.indexOf(parametr))
|
|
|
|
{
|
|
|
|
case 0: // true
|
|
|
|
val = true;
|
|
|
|
break;
|
|
|
|
case 1: // false
|
|
|
|
val = false;
|
|
|
|
break;
|
|
|
|
default:// others
|
|
|
|
throw VExceptionConversionError(message, name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const VExceptionEmptyParameter &e)
|
|
|
|
{
|
|
|
|
VExceptionConversionError excep(message, name);
|
|
|
|
excep.AddMoreInformation(e.ErrorMessage());
|
|
|
|
throw excep;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns the string value of the given attribute. RENAME: see above
|
|
|
|
*
|
|
|
|
* if attribute empty return default value. If default value empty too throw exception.
|
|
|
|
* @return attribute value
|
|
|
|
* @throw VExceptionEmptyParameter when attribute is empty
|
|
|
|
*/
|
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
|
|
|
{
|
2015-10-28 15:22:36 +01:00
|
|
|
Q_ASSERT_X(not name.isEmpty(), Q_FUNC_INFO, "name of parametr is empty");
|
|
|
|
Q_ASSERT_X(not domElement.isNull(), 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
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns the double value of the given attribute.
|
|
|
|
* @param domElement tag in xml tree
|
|
|
|
* @param name attribute name
|
|
|
|
* @return double value
|
|
|
|
*/
|
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
|
|
|
{
|
2015-10-28 15:22:36 +01:00
|
|
|
Q_ASSERT_X(not name.isEmpty(), Q_FUNC_INFO, "name of parametr is empty");
|
|
|
|
Q_ASSERT_X(not domElement.isNull(), 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-12-16 08:42:24 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief GetParametrId return value id attribute.
|
|
|
|
* @param domElement tag in xml tree.
|
|
|
|
* @return id value.
|
|
|
|
*/
|
|
|
|
quint32 VDomDocument::GetParametrId(const QDomElement &domElement) const
|
|
|
|
{
|
2015-10-28 15:22:36 +01:00
|
|
|
Q_ASSERT_X(not domElement.isNull(), Q_FUNC_INFO, "domElement is null");
|
2014-12-16 08:42:24 +01:00
|
|
|
|
|
|
|
quint32 id = 0;
|
|
|
|
|
|
|
|
QString message = tr("Got wrong parameter id. Need only id > 0.");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
id = GetParametrUInt(domElement, VDomDocument::AttrId, NULL_ID_STR);
|
|
|
|
if (id <= 0)
|
|
|
|
{
|
|
|
|
throw VExceptionWrongId(message, domElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const VExceptionConversionError &e)
|
|
|
|
{
|
|
|
|
VExceptionWrongId excep(message, domElement);
|
|
|
|
excep.AddMoreInformation(e.ErrorMessage());
|
|
|
|
throw excep;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-07-18 13:08:44 +02:00
|
|
|
const QString text = domElement.text();
|
|
|
|
if (text.isEmpty())
|
|
|
|
{
|
|
|
|
return defVal;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
2014-02-19 14:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defVal;
|
|
|
|
}
|
2014-02-25 15:55:02 +01:00
|
|
|
|
2014-12-16 08:42:24 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief TestUniqueId test exist unique id in pattern file. Each id must be unique.
|
|
|
|
*/
|
|
|
|
void VDomDocument::TestUniqueId() const
|
|
|
|
{
|
|
|
|
QVector<quint32> vector;
|
|
|
|
CollectId(documentElement(), vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDomDocument::CollectId(const QDomElement &node, QVector<quint32> &vector) const
|
|
|
|
{
|
|
|
|
if (node.hasAttribute(VDomDocument::AttrId))
|
|
|
|
{
|
|
|
|
const quint32 id = GetParametrId(node);
|
|
|
|
if (vector.contains(id))
|
|
|
|
{
|
|
|
|
throw VExceptionWrongId(tr("This id is not unique."), node);
|
|
|
|
}
|
|
|
|
vector.append(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (qint32 i=0; i<node.childNodes().length(); ++i)
|
|
|
|
{
|
|
|
|
const QDomNode n = node.childNodes().at(i);
|
|
|
|
if (n.isElement())
|
|
|
|
{
|
|
|
|
CollectId(n.toElement(), vector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief ValidateXML validate xml file by xsd schema.
|
|
|
|
* @param schema path to schema file.
|
|
|
|
* @param fileName name of xml file.
|
|
|
|
*/
|
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
|
|
|
{
|
2015-10-01 17:34:03 +02:00
|
|
|
qCDebug(vXML, "Validation xml file %s.", qUtf8Printable(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);
|
2014-11-24 14:51:07 +01:00
|
|
|
if (sch.load(&fileSchema, QUrl::fromLocalFile(fileSchema.fileName()))==false)
|
|
|
|
{
|
|
|
|
pattern.close();
|
|
|
|
fileSchema.close();
|
2015-12-11 15:19:11 +01:00
|
|
|
const QString errorMsg(tr("Could not load schema file '%1'.").arg(fileSchema.fileName()));
|
2014-11-24 14:51:07 +01:00
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
2015-04-01 19:08:35 +02:00
|
|
|
qCDebug(vXML, "Schema loaded.");
|
2014-02-25 15:55:02 +01:00
|
|
|
|
|
|
|
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-09-16 21:08:48 +02:00
|
|
|
e.AddMoreInformation(tr("Validation error file %3 in line %1 column %2").arg(messageHandler.line())
|
|
|
|
.arg(messageHandler.column()).arg(fileName));
|
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-12-11 16:51:24 +01:00
|
|
|
void VDomDocument::setXMLContent(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);
|
2014-09-16 21:08:48 +02:00
|
|
|
e.AddMoreInformation(tr("Parsing error file %3 in line %1 column %2").arg(errorLine).arg(errorColumn)
|
|
|
|
.arg(fileName));
|
2014-03-24 16:02:57 +01:00
|
|
|
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-06-12 09:22:29 +02:00
|
|
|
Unit VDomDocument::StrToUnits(const QString &unit)
|
2014-03-14 15:12:53 +01:00
|
|
|
{
|
2015-01-14 15:14:51 +01:00
|
|
|
QStringList units = QStringList() << UnitMM << UnitCM << UnitINCH << UnitPX;
|
2014-06-12 09:22:29 +02:00
|
|
|
Unit result = Unit::Cm;
|
2014-03-14 15:12:53 +01:00
|
|
|
switch (units.indexOf(unit))
|
|
|
|
{
|
|
|
|
case 0:// mm
|
2014-06-12 09:22:29 +02:00
|
|
|
result = Unit::Mm;
|
2014-03-14 15:12:53 +01:00
|
|
|
break;
|
|
|
|
case 1:// cm
|
2014-06-12 09:22:29 +02:00
|
|
|
result = Unit::Cm;
|
2014-03-14 15:12:53 +01:00
|
|
|
break;
|
2014-03-24 16:02:57 +01:00
|
|
|
case 2:// inch
|
2014-06-12 09:22:29 +02:00
|
|
|
result = Unit::Inch;
|
2014-03-14 15:12:53 +01:00
|
|
|
break;
|
2015-01-14 15:14:51 +01:00
|
|
|
case 3:// px
|
|
|
|
result = Unit::Px;
|
|
|
|
break;
|
2014-03-14 15:12:53 +01:00
|
|
|
default:
|
2014-06-12 09:22:29 +02:00
|
|
|
result = Unit::Cm;
|
2014-03-14 15:12:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-03-24 16:02:57 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-12 09:49:45 +02:00
|
|
|
/**
|
|
|
|
* @brief UnitsToStr translate unit to string.
|
|
|
|
*
|
|
|
|
* This method used when need write unit in xml file and for showing unit in dialogs.
|
|
|
|
* @param unit curent unit
|
|
|
|
* @param translate true if need show translated name. Default value false.
|
|
|
|
* @return string reprezantation for unit.
|
|
|
|
*/
|
|
|
|
QString VDomDocument::UnitsToStr(const Unit &unit, const bool translate)
|
2014-03-24 16:02:57 +01:00
|
|
|
{
|
|
|
|
QString result;
|
2014-05-01 13:33:40 +02:00
|
|
|
switch (unit)
|
2014-03-24 16:02:57 +01:00
|
|
|
{
|
2014-06-12 09:22:29 +02:00
|
|
|
case Unit::Mm:
|
2014-06-12 09:49:45 +02:00
|
|
|
if (translate)
|
|
|
|
{
|
|
|
|
result = QObject::tr("mm");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-07 16:48:42 +02:00
|
|
|
result = UnitMM;
|
2014-06-12 09:49:45 +02:00
|
|
|
}
|
2014-03-24 16:02:57 +01:00
|
|
|
break;
|
2014-06-12 09:22:29 +02:00
|
|
|
case Unit::Cm:
|
2014-06-12 09:49:45 +02:00
|
|
|
if (translate)
|
|
|
|
{
|
|
|
|
result = QObject::tr("cm");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-07 16:48:42 +02:00
|
|
|
result = UnitCM;
|
2014-06-12 09:49:45 +02:00
|
|
|
}
|
2014-03-24 16:02:57 +01:00
|
|
|
break;
|
2014-06-12 09:22:29 +02:00
|
|
|
case Unit::Inch:
|
2014-06-12 09:49:45 +02:00
|
|
|
if (translate)
|
|
|
|
{
|
2014-08-07 16:48:42 +02:00
|
|
|
result = QObject::tr("inch");
|
2014-06-12 09:49:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-07 16:48:42 +02:00
|
|
|
result = UnitINCH;
|
2014-06-12 09:49:45 +02:00
|
|
|
}
|
2014-03-24 16:02:57 +01:00
|
|
|
break;
|
2015-01-14 15:14:51 +01:00
|
|
|
case Unit::Px:
|
|
|
|
if (translate)
|
|
|
|
{
|
|
|
|
result = QObject::tr("px");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = UnitPX;
|
|
|
|
}
|
|
|
|
break;
|
2014-03-24 16:02:57 +01:00
|
|
|
default:
|
2014-06-12 09:49:45 +02:00
|
|
|
if (translate)
|
|
|
|
{
|
|
|
|
result = QObject::tr("cm");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-07 16:48:42 +02:00
|
|
|
result = UnitCM;
|
2014-06-12 09:49:45 +02:00
|
|
|
}
|
2014-03-24 16:02:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-03-28 14:11:46 +01:00
|
|
|
|
2015-08-25 19:53:03 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VDomDocument::UnitsHelpString()
|
|
|
|
{
|
|
|
|
QString r;
|
|
|
|
for (auto i = static_cast<int>(Unit::Mm), last = static_cast<int>(Unit::LAST_UNIT_DO_NOT_USE); i < last;++i)
|
|
|
|
{
|
|
|
|
r += UnitsToStr(static_cast<Unit>(i));
|
|
|
|
if (i < last - 1)
|
|
|
|
{
|
|
|
|
r += ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-12-16 08:42:24 +01:00
|
|
|
bool VDomDocument::SaveDocument(const QString &fileName, QString &error) const
|
2014-03-28 14:11:46 +01:00
|
|
|
{
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
{
|
|
|
|
qDebug()<<"Got empty file name.";
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-19 20:02:38 +01:00
|
|
|
bool success = false;
|
|
|
|
QSaveFile file(fileName);
|
|
|
|
if (file.open(QIODevice::WriteOnly))
|
2014-03-28 14:11:46 +01:00
|
|
|
{
|
|
|
|
const int indent = 4;
|
2015-12-19 20:02:38 +01:00
|
|
|
QTextStream out(&file);
|
2014-03-28 14:11:46 +01:00
|
|
|
out.setCodec("UTF-8");
|
|
|
|
save(out, indent);
|
2015-12-19 20:02:38 +01:00
|
|
|
success = file.commit();
|
2014-03-28 14:11:46 +01:00
|
|
|
}
|
2014-10-08 10:51:17 +02:00
|
|
|
|
2015-12-19 20:02:38 +01:00
|
|
|
if (not success)
|
|
|
|
{
|
|
|
|
error = file.errorString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2014-03-28 14:11:46 +01:00
|
|
|
}
|
|
|
|
|
2014-05-16 16:11:31 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-05-16 16:11:31 +02:00
|
|
|
QString VDomDocument::Minor() const
|
|
|
|
{
|
|
|
|
QString version = UniqueTagText(TagVersion, "0.0.0");
|
|
|
|
QStringList v = version.split(".");
|
|
|
|
return v.at(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-05-16 16:11:31 +02:00
|
|
|
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-12-16 08:42:24 +01:00
|
|
|
bool VDomDocument::setTagText(const QString &tag, const QString &text)
|
2014-03-28 14:11:46 +01:00
|
|
|
{
|
|
|
|
const QDomNodeList nodeList = this->elementsByTagName(tag);
|
|
|
|
if (nodeList.isEmpty())
|
|
|
|
{
|
|
|
|
qDebug()<<"Can't save tag "<<tag<<Q_FUNC_INFO;
|
|
|
|
}
|
|
|
|
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);
|
2015-10-26 12:21:49 +01:00
|
|
|
if (not text.isEmpty())
|
|
|
|
{
|
|
|
|
const QDomText newTagText = createTextNode(text);
|
|
|
|
newTag.appendChild(newTagText);
|
|
|
|
}
|
2014-03-28 14:11:46 +01:00
|
|
|
|
|
|
|
parent.replaceChild(newTag, domElement);
|
2014-12-16 08:42:24 +01:00
|
|
|
return true;
|
2014-03-28 14:11:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 08:42:24 +01:00
|
|
|
return false;
|
2014-03-28 14:11:46 +01:00
|
|
|
}
|
2014-06-12 15:38:28 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief RemoveAllChild remove all child from file.
|
|
|
|
* @param domElement tag in xml tree.
|
|
|
|
*/
|
|
|
|
void VDomDocument::RemoveAllChild(QDomElement &domElement)
|
|
|
|
{
|
|
|
|
if ( domElement.hasChildNodes() )
|
|
|
|
{
|
|
|
|
while ( domElement.childNodes().length() >= 1 )
|
|
|
|
{
|
|
|
|
domElement.removeChild( domElement.firstChild() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-10 15:49:39 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QDomNode VDomDocument::ParentNodeById(const quint32 &nodeId)
|
|
|
|
{
|
|
|
|
QDomElement domElement = NodeById(nodeId);
|
|
|
|
return domElement.parentNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QDomElement VDomDocument::CloneNodeById(const quint32 &nodeId)
|
|
|
|
{
|
|
|
|
QDomElement domElement = NodeById(nodeId);
|
|
|
|
return domElement.cloneNode().toElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QDomElement VDomDocument::NodeById(const quint32 &nodeId)
|
|
|
|
{
|
|
|
|
QDomElement domElement = elementById(QString().setNum(nodeId));
|
|
|
|
if (domElement.isNull() || domElement.isElement() == false)
|
|
|
|
{
|
|
|
|
throw VExceptionBadId(tr("Couldn't get node"), nodeId);
|
|
|
|
}
|
|
|
|
return domElement;
|
|
|
|
}
|
2014-12-10 19:33:20 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VDomDocument::SafeCopy(const QString &source, const QString &destination, QString &error)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
qt_ntfs_permission_lookup++; // turn checking on
|
|
|
|
#endif /*Q_OS_WIN32*/
|
|
|
|
|
2015-12-19 21:15:42 +01:00
|
|
|
QTemporaryFile destFile(destination + QLatin1Literal(".XXXXXX"));
|
|
|
|
destFile.setAutoRemove(false);
|
|
|
|
if (not destFile.open())
|
2014-12-10 19:33:20 +01:00
|
|
|
{
|
2015-12-19 21:15:42 +01:00
|
|
|
error = destFile.errorString();
|
2015-12-19 20:02:38 +01:00
|
|
|
result = false;
|
2014-12-10 19:33:20 +01:00
|
|
|
}
|
2015-12-19 20:02:38 +01:00
|
|
|
else
|
2014-12-10 19:33:20 +01:00
|
|
|
{
|
|
|
|
QFile sourceFile(source);
|
2015-12-19 21:15:42 +01:00
|
|
|
if (sourceFile.open(QIODevice::ReadOnly))
|
2014-12-10 19:33:20 +01:00
|
|
|
{
|
2015-12-19 21:15:42 +01:00
|
|
|
result = true;
|
|
|
|
char block[4096];
|
|
|
|
qint64 bytes;
|
|
|
|
while ((bytes = sourceFile.read(block, sizeof(block))) > 0)
|
|
|
|
{
|
|
|
|
if (bytes != destFile.write(block, bytes))
|
|
|
|
{
|
|
|
|
error = destFile.errorString();
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes == -1)
|
2015-12-19 20:02:38 +01:00
|
|
|
{
|
2015-12-19 21:15:42 +01:00
|
|
|
error = sourceFile.errorString();
|
2015-12-19 20:02:38 +01:00
|
|
|
result = false;
|
|
|
|
}
|
2015-12-19 21:15:42 +01:00
|
|
|
|
|
|
|
if (result)
|
2015-12-19 20:02:38 +01:00
|
|
|
{
|
2015-12-19 21:15:42 +01:00
|
|
|
if (not destFile.rename(destination))
|
|
|
|
{
|
|
|
|
error = destFile.errorString();
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
}
|
2015-12-19 20:02:38 +01:00
|
|
|
}
|
2014-12-10 19:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
qt_ntfs_permission_lookup--; // turn off check permission again
|
|
|
|
#endif /*Q_OS_WIN32*/
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|