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
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2013-11-15 13:41:26 +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
|
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"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include <qcompilerdetection.h>
|
|
|
|
#include <qdom.h>
|
2017-05-30 18:52:38 +02:00
|
|
|
#include <QSaveFile>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include "../exception/vexceptionbadid.h"
|
2016-08-05 12:41:02 +02:00
|
|
|
#include "../exception/vexceptionconversionerror.h"
|
|
|
|
#include "../exception/vexceptionemptyparameter.h"
|
|
|
|
#include "../exception/vexceptionwrongid.h"
|
2016-08-09 15:55:46 +02:00
|
|
|
#include "../exception/vexception.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "../vmisc/logging.h"
|
2016-08-09 15:55:46 +02:00
|
|
|
#include "../ifcdef.h"
|
2013-08-13 18:48:36 +02:00
|
|
|
|
2014-02-25 15:55:02 +01:00
|
|
|
#include <QAbstractMessageHandler>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QDomNodeList>
|
|
|
|
#include <QDomText>
|
2014-02-25 15:55:02 +01:00
|
|
|
#include <QFile>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QIODevice>
|
|
|
|
#include <QMessageLogger>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QSourceLocation>
|
|
|
|
#include <QStringList>
|
2014-04-17 19:18:26 +02:00
|
|
|
#include <QTemporaryFile>
|
2017-03-18 17:07:05 +01:00
|
|
|
#include <QTextDocument>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QTextStream>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QVector>
|
|
|
|
#include <QXmlSchema>
|
|
|
|
#include <QXmlSchemaValidator>
|
|
|
|
#include <QtDebug>
|
2017-04-20 18:01:08 +02:00
|
|
|
#include <QXmlStreamWriter>
|
2018-01-26 13:43:52 +01:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QtConcurrentRun>
|
|
|
|
#include <QFutureWatcher>
|
2017-04-20 18:01:08 +02:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void SaveNodeCanonically(QXmlStreamWriter &stream, const QDomNode &domNode)
|
|
|
|
{
|
|
|
|
if (stream.hasError())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domNode.isElement())
|
|
|
|
{
|
|
|
|
const QDomElement domElement = domNode.toElement();
|
|
|
|
if (not domElement.isNull())
|
|
|
|
{
|
|
|
|
stream.writeStartElement(domElement.tagName());
|
|
|
|
|
|
|
|
if (domElement.hasAttributes())
|
|
|
|
{
|
|
|
|
QMap<QString, QString> attributes;
|
|
|
|
const QDomNamedNodeMap attributeMap = domElement.attributes();
|
|
|
|
for (int i = 0; i < attributeMap.count(); ++i)
|
|
|
|
{
|
|
|
|
const QDomNode attribute = attributeMap.item(i);
|
|
|
|
attributes.insert(attribute.nodeName(), attribute.nodeValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QString, QString>::const_iterator i = attributes.constBegin();
|
|
|
|
while (i != attributes.constEnd())
|
|
|
|
{
|
|
|
|
stream.writeAttribute(i.key(), i.value());
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domElement.hasChildNodes())
|
|
|
|
{
|
|
|
|
QDomNode elementChild = domElement.firstChild();
|
|
|
|
while (not elementChild.isNull())
|
|
|
|
{
|
|
|
|
SaveNodeCanonically(stream, elementChild);
|
|
|
|
elementChild = elementChild.nextSibling();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.writeEndElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (domNode.isComment())
|
|
|
|
{
|
|
|
|
stream.writeComment(domNode.nodeValue());
|
|
|
|
}
|
|
|
|
else if (domNode.isText())
|
|
|
|
{
|
|
|
|
stream.writeCharacters(domNode.nodeValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 15:55:02 +01:00
|
|
|
|
|
|
|
//This class need for validation pattern file using XSD shema
|
|
|
|
class MessageHandler : public QAbstractMessageHandler
|
|
|
|
{
|
|
|
|
public:
|
2017-03-18 17:07:05 +01:00
|
|
|
MessageHandler()
|
|
|
|
: QAbstractMessageHandler(),
|
|
|
|
m_messageType(QtMsgType()),
|
|
|
|
m_description(),
|
|
|
|
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
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-03-18 17:07:05 +01:00
|
|
|
QString MessageHandler::statusMessage() const
|
2014-02-26 09:18:59 +01:00
|
|
|
{
|
2017-03-18 17:07:05 +01:00
|
|
|
QTextDocument doc;
|
|
|
|
doc.setHtml(m_description);
|
|
|
|
return doc.toPlainText();
|
2014-02-26 09:18:59 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-12-20 20:19:21 +01:00
|
|
|
Q_UNUSED(type)
|
|
|
|
Q_UNUSED(identifier)
|
2014-06-02 09:43:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-08-15 17:10:15 +02:00
|
|
|
const QString VDomDocument::AttrId = QStringLiteral("id");
|
|
|
|
const QString VDomDocument::AttrText = QStringLiteral("text");
|
|
|
|
const QString VDomDocument::AttrBold = QStringLiteral("bold");
|
|
|
|
const QString VDomDocument::AttrItalic = QStringLiteral("italic");
|
|
|
|
const QString VDomDocument::AttrAlignment = QStringLiteral("alignment");
|
|
|
|
const QString VDomDocument::AttrFSIncrement = QStringLiteral("sfIncrement");
|
2017-08-15 09:44:00 +02:00
|
|
|
|
2014-05-16 16:11:31 +02:00
|
|
|
const QString VDomDocument::TagVersion = QStringLiteral("version");
|
2017-04-18 10:46:53 +02:00
|
|
|
const QString VDomDocument::TagUnit = QStringLiteral("unit");
|
2017-08-15 09:44:00 +02:00
|
|
|
const QString VDomDocument::TagLine = QStringLiteral("line");
|
2014-03-21 11:08:29 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-01-26 13:43:52 +01:00
|
|
|
VDomDocument::VDomDocument(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
QDomDocument(),
|
|
|
|
m_elementIdCache(),
|
|
|
|
m_watcher(new QFutureWatcher<QHash<quint32, QDomElement>>(this))
|
|
|
|
{
|
|
|
|
connect(m_watcher, &QFutureWatcher<QHash<quint32, QDomElement>>::finished, this, &VDomDocument::CacheRefreshed);
|
|
|
|
}
|
2013-07-13 12:51:31 +02:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-08-02 13:40:17 +02:00
|
|
|
QDomElement VDomDocument::elementById(quint32 id, const QString &tagName)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2017-08-02 13:40:17 +02:00
|
|
|
if (id == 0)
|
|
|
|
{
|
|
|
|
return QDomElement();
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:43:52 +01:00
|
|
|
if (m_elementIdCache.contains(id))
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2018-01-26 13:43:52 +01:00
|
|
|
const QDomElement e = m_elementIdCache.value(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;
|
|
|
|
}
|
2018-01-26 13:43:52 +01:00
|
|
|
m_elementIdCache.remove(id);
|
2013-07-13 12:51:31 +02:00
|
|
|
}
|
|
|
|
|
2018-01-27 11:04:22 +01:00
|
|
|
// Cached missed
|
|
|
|
RefreshElementIdCache();
|
|
|
|
|
2017-08-02 13:10:10 +02:00
|
|
|
if (tagName.isEmpty())
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2018-01-26 13:43:52 +01:00
|
|
|
if (VDomDocument::find(m_elementIdCache, this->documentElement(), id))
|
2017-08-02 13:10:10 +02:00
|
|
|
{
|
2018-01-26 13:43:52 +01:00
|
|
|
return m_elementIdCache.value(id);
|
2017-08-02 13:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const QDomNodeList list = elementsByTagName(tagName);
|
|
|
|
for (int i=0; i < list.size(); ++i)
|
|
|
|
{
|
|
|
|
const QDomElement domElement = list.at(i).toElement();
|
|
|
|
if (not domElement.isNull() && domElement.hasAttribute(AttrId))
|
|
|
|
{
|
2018-01-26 13:43:52 +01:00
|
|
|
const quint32 elementId = GetParametrUInt(domElement, AttrId, NULL_ID_STR);
|
2017-08-02 13:40:17 +02:00
|
|
|
|
2018-01-26 13:43:52 +01:00
|
|
|
m_elementIdCache.insert(elementId, domElement);
|
|
|
|
if (elementId == id)
|
2017-08-02 13:40:17 +02:00
|
|
|
{
|
2018-01-26 13:43:52 +01:00
|
|
|
return domElement;
|
2017-08-02 13:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-13 12:51:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return QDomElement();
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Find element by id.
|
2018-01-26 13:43:52 +01:00
|
|
|
* @param cache cache with element ids
|
2014-06-13 19:02:41 +02:00
|
|
|
* @param node node
|
|
|
|
* @param id id value
|
|
|
|
* @return true if found
|
|
|
|
*/
|
2018-01-26 13:43:52 +01:00
|
|
|
bool VDomDocument::find(QHash<quint32, QDomElement> &cache, const QDomElement &node, quint32 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
|
|
|
{
|
2018-01-26 13:43:52 +01:00
|
|
|
const quint32 elementId = GetParametrUInt(node, AttrId, NULL_ID_STR);
|
2017-08-02 13:40:17 +02:00
|
|
|
|
2018-01-26 13:43:52 +01:00
|
|
|
cache.insert(elementId, node);
|
|
|
|
if (elementId == id)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2018-01-26 13:43:52 +01:00
|
|
|
return true;
|
2013-07-13 12:51:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 21:35:15 +01:00
|
|
|
for (qint32 i=0; i<node.childNodes().length(); ++i)
|
|
|
|
{
|
2017-08-02 10:34:44 +02:00
|
|
|
const QDomNode n = node.childNodes().at(i);
|
2013-11-04 21:35:15 +01:00
|
|
|
if (n.isElement())
|
|
|
|
{
|
2018-01-26 13:43:52 +01:00
|
|
|
if (VDomDocument::find(cache, n.toElement(), id))
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-07-13 12:51:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:43:52 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QHash<quint32, QDomElement> VDomDocument::RefreshCache(const QDomElement &root) const
|
|
|
|
{
|
|
|
|
QHash<quint32, QDomElement> cache;
|
|
|
|
VDomDocument::find(cache, root, NULL_ID);
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:01:08 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VDomDocument::SaveCanonicalXML(QIODevice *file, int indent, QString &error) const
|
|
|
|
{
|
|
|
|
SCASSERT(file != nullptr)
|
|
|
|
|
|
|
|
QXmlStreamWriter stream(file);
|
|
|
|
stream.setAutoFormatting(true);
|
|
|
|
stream.setAutoFormattingIndent(indent);
|
|
|
|
stream.writeStartDocument();
|
|
|
|
|
|
|
|
QDomNode root = documentElement();
|
|
|
|
while (not root.isNull())
|
|
|
|
{
|
|
|
|
SaveNodeCanonically(stream, root);
|
|
|
|
if (stream.hasError())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
root = root.nextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.writeEndDocument();
|
|
|
|
|
|
|
|
if (stream.hasError())
|
|
|
|
{
|
|
|
|
error = tr("Fail to write Canonical XML.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2017-01-16 10:59:02 +01:00
|
|
|
quint32 VDomDocument::GetParametrUInt(const QDomElement &domElement, const QString &name, const QString &defValue)
|
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;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-08-05 10:03:19 +02:00
|
|
|
throw VExceptionConversionError(QObject::tr("Can't convert toUInt parameter"), name);
|
2014-03-14 16:59:28 +01:00
|
|
|
}
|
2013-09-23 14:08:06 +02:00
|
|
|
}
|
2014-03-14 16:59:28 +01:00
|
|
|
catch (const VExceptionEmptyParameter &e)
|
|
|
|
{
|
2017-08-05 10:03:19 +02:00
|
|
|
VExceptionConversionError excep(QObject::tr("Can't convert toUInt parameter"), name);
|
2014-03-14 16:59:28 +01:00
|
|
|
excep.AddMoreInformation(e.ErrorMessage());
|
|
|
|
throw excep;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:14:21 +02:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2014-08-07 13:17:24 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-16 10:59:02 +01:00
|
|
|
bool VDomDocument::GetParametrBool(const QDomElement &domElement, const QString &name, const QString &defValue)
|
2014-08-07 13:17:24 +02: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-08-07 13:17:24 +02:00
|
|
|
|
|
|
|
QString parametr;
|
|
|
|
bool val = true;
|
|
|
|
|
2017-01-16 10:59:02 +01:00
|
|
|
const QString message = QObject::tr("Can't convert toBool parameter");
|
2014-08-07 13:17:24 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
parametr = GetParametrString(domElement, name, defValue);
|
|
|
|
|
2016-11-10 11:51:06 +01:00
|
|
|
const QStringList bools = QStringList() << QLatin1String("true")
|
|
|
|
<< QLatin1String("false")
|
|
|
|
<< QLatin1String("1")
|
|
|
|
<< QLatin1String("0");
|
2014-08-07 13:17:24 +02:00
|
|
|
switch (bools.indexOf(parametr))
|
|
|
|
{
|
|
|
|
case 0: // true
|
2016-11-10 11:51:06 +01:00
|
|
|
case 2: // 1
|
|
|
|
val = true;
|
|
|
|
break;
|
2017-03-18 10:04:12 +01:00
|
|
|
case 1: // false
|
2016-11-10 11:51:06 +01:00
|
|
|
case 3: // 0
|
|
|
|
val = false;
|
|
|
|
break;
|
2014-08-07 13:17:24 +02:00
|
|
|
default:// others
|
|
|
|
throw VExceptionConversionError(message, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const VExceptionEmptyParameter &e)
|
|
|
|
{
|
|
|
|
VExceptionConversionError excep(message, name);
|
|
|
|
excep.AddMoreInformation(e.ErrorMessage());
|
|
|
|
throw excep;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-12-26 14:08:41 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-16 10:59:02 +01:00
|
|
|
NodeUsage VDomDocument::GetParametrUsage(const QDomElement &domElement, const QString &name)
|
2015-12-26 14:08:41 +01:00
|
|
|
{
|
2016-04-05 19:53:49 +02:00
|
|
|
const bool value = GetParametrBool(domElement, name, trueStr);
|
2015-12-26 14:08:41 +01:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
return NodeUsage::InUse;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NodeUsage::NotInUse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDomDocument::SetParametrUsage(QDomElement &domElement, const QString &name, const NodeUsage &value)
|
|
|
|
{
|
|
|
|
if (value == NodeUsage::InUse)
|
|
|
|
{
|
2016-04-05 19:53:49 +02:00
|
|
|
domElement.setAttribute(name, trueStr);
|
2015-12-26 14:08:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-05 19:53:49 +02:00
|
|
|
domElement.setAttribute(name, falseStr);
|
2015-12-26 14:08:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2017-01-16 10:59:02 +01:00
|
|
|
const QString &defValue)
|
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())
|
|
|
|
{
|
2017-01-16 10:59:02 +01:00
|
|
|
throw VExceptionEmptyParameter(QObject::tr("Got empty parameter"), name, domElement);
|
2014-03-14 15:12:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return defValue;
|
|
|
|
}
|
2013-09-23 14:08:06 +02:00
|
|
|
}
|
|
|
|
return parameter;
|
2013-09-11 16:14:21 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 12:16:43 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VDomDocument::GetParametrEmptyString(const QDomElement &domElement, const QString &name)
|
|
|
|
{
|
2018-01-20 11:31:12 +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");
|
|
|
|
return domElement.attribute(name);
|
2017-08-21 12:16:43 +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
|
|
|
|
*/
|
2017-01-16 10:59:02 +01:00
|
|
|
qreal VDomDocument::GetParametrDouble(const QDomElement &domElement, const QString &name, const QString &defValue)
|
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;
|
|
|
|
|
2017-01-16 10:59:02 +01:00
|
|
|
const QString message = QObject::tr("Can't convert toDouble parameter");
|
2014-03-14 16:59:28 +01:00
|
|
|
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.
|
|
|
|
*/
|
2017-01-16 10:59:02 +01:00
|
|
|
quint32 VDomDocument::GetParametrId(const QDomElement &domElement)
|
2014-12-16 08:42:24 +01:00
|
|
|
{
|
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
|
|
|
|
2016-08-02 14:12:13 +02:00
|
|
|
quint32 id = NULL_ID;
|
2014-12-16 08:42:24 +01:00
|
|
|
|
2017-01-16 10:59:02 +01:00
|
|
|
const QString message = QObject::tr("Got wrong parameter id. Need only id > 0.");
|
2014-12-16 08:42:24 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
id = GetParametrUInt(domElement, VDomDocument::AttrId, NULL_ID_STR);
|
2016-08-02 14:12:13 +02:00
|
|
|
if (id == NULL_ID)
|
2014-12-16 08:42:24 +01:00
|
|
|
{
|
|
|
|
throw VExceptionWrongId(message, domElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const VExceptionConversionError &e)
|
|
|
|
{
|
|
|
|
VExceptionWrongId excep(message, domElement);
|
|
|
|
excep.AddMoreInformation(e.ErrorMessage());
|
|
|
|
throw excep;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2017-04-18 10:46:53 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
Unit VDomDocument::MUnit() const
|
|
|
|
{
|
2017-07-02 13:34:36 +02:00
|
|
|
Unit unit = StrToUnits(UniqueTagText(TagUnit, unitCM));
|
2017-04-18 10:46:53 +02:00
|
|
|
|
|
|
|
if (unit == Unit::Px)
|
|
|
|
{
|
|
|
|
unit = Unit::Cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
{
|
2017-09-11 10:45:38 +02:00
|
|
|
throw VExceptionWrongId(QString("This id (%1) is not unique.").arg(id), node);
|
2014-12-16 08:42:24 +01:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:43:52 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDomDocument::RefreshElementIdCache()
|
|
|
|
{
|
|
|
|
m_watcher->setFuture(QtConcurrent::run(this, &VDomDocument::RefreshCache, documentElement()));
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDomDocument::CacheRefreshed()
|
|
|
|
{
|
|
|
|
m_elementIdCache = m_watcher->future().result();
|
|
|
|
}
|
|
|
|
|
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);
|
2016-08-09 13:18:49 +02:00
|
|
|
// cppcheck-suppress ConfigurationNotChecked
|
2014-02-25 15:55:02 +01:00
|
|
|
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);
|
2016-08-09 13:18:49 +02:00
|
|
|
// cppcheck-suppress ConfigurationNotChecked
|
2014-02-25 15:55:02 +01:00
|
|
|
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();
|
2017-01-17 11:26:25 +01:00
|
|
|
VException e(messageHandler.statusMessage());
|
|
|
|
e.AddMoreInformation(tr("Could not load schema file '%1'.").arg(fileSchema.fileName()));
|
|
|
|
throw e;
|
2014-11-24 14:51:07 +01:00
|
|
|
}
|
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);
|
2016-08-09 13:18:49 +02:00
|
|
|
// cppcheck-suppress ConfigurationNotChecked
|
2014-03-24 16:02:57 +01:00
|
|
|
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
|
|
|
}
|
2018-01-26 13:43:52 +01:00
|
|
|
|
|
|
|
RefreshElementIdCache();
|
2014-02-25 15:55:02 +01:00
|
|
|
}
|
2014-03-14 15:12:53 +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
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-05-29 15:33:36 +02:00
|
|
|
bool VDomDocument::SaveDocument(const QString &fileName, QString &error)
|
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);
|
2016-08-09 13:18:49 +02:00
|
|
|
// cppcheck-suppress ConfigurationNotChecked
|
2015-12-19 20:02:38 +01:00
|
|
|
if (file.open(QIODevice::WriteOnly))
|
2014-03-28 14:11:46 +01:00
|
|
|
{
|
2017-04-20 18:01:08 +02:00
|
|
|
// See issue #666. QDomDocument produces random attribute order.
|
2014-03-28 14:11:46 +01:00
|
|
|
const int indent = 4;
|
2017-04-20 18:01:08 +02:00
|
|
|
if (not SaveCanonicalXML(&file, indent, error))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Left these strings in case we will need them for testing purposes
|
|
|
|
// QTextStream out(&file);
|
|
|
|
// 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();
|
2016-04-07 11:35:08 +02:00
|
|
|
return setTagText(domElement, text);
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-04-06 04:34:08 +02:00
|
|
|
bool VDomDocument::setTagText(const QDomElement &domElement, const QString &text)
|
|
|
|
{
|
|
|
|
if (domElement.isNull() == false)
|
|
|
|
{
|
|
|
|
QDomElement parent = domElement.parentNode().toElement();
|
|
|
|
QDomElement newTag = createElement(domElement.tagName());
|
2016-04-07 11:35:08 +02:00
|
|
|
|
|
|
|
const QDomText newTagText = createTextNode(text);
|
|
|
|
newTag.appendChild(newTagText);
|
|
|
|
|
2016-04-06 04:34:08 +02:00
|
|
|
parent.replaceChild(newTag, domElement);
|
|
|
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
2016-03-04 17:08:29 +01:00
|
|
|
* @brief RemoveAllChildren remove all children from file.
|
2014-06-12 15:38:28 +02:00
|
|
|
* @param domElement tag in xml tree.
|
|
|
|
*/
|
2016-03-04 17:08:29 +01:00
|
|
|
void VDomDocument::RemoveAllChildren(QDomElement &domElement)
|
2014-06-12 15:38:28 +02:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2017-08-02 13:40:17 +02:00
|
|
|
QDomElement domElement = elementById(nodeId);
|
2014-11-10 15:49:39 +01:00
|
|
|
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*/
|
|
|
|
|
2016-07-20 10:39:15 +02:00
|
|
|
QTemporaryFile destFile(destination + QLatin1String(".XXXXXX"));
|
2017-02-04 12:28:47 +01:00
|
|
|
destFile.setAutoRemove(false);// Will be renamed to be destination file
|
2016-08-09 13:18:49 +02:00
|
|
|
// cppcheck-suppress ConfigurationNotChecked
|
2015-12-19 21:15:42 +01:00
|
|
|
if (not destFile.open())
|
2014-12-10 19:33:20 +01:00
|
|
|
{
|
2015-12-19 21:15:42 +01:00
|
|
|
error = destFile.errorString();
|
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);
|
2016-08-09 13:18:49 +02:00
|
|
|
// cppcheck-suppress ConfigurationNotChecked
|
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-26 14:08:41 +01:00
|
|
|
QFile::remove(destination);
|
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
|
|
|
}
|
2017-02-04 12:28:47 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
error = sourceFile.errorString();
|
|
|
|
}
|
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;
|
|
|
|
}
|
2017-08-15 09:44:00 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<VLabelTemplateLine> VDomDocument::GetLabelTemplate(const QDomElement &element) const
|
|
|
|
{
|
2017-08-15 10:23:48 +02:00
|
|
|
// We use implicit conversion. That's why check if values are still the same as excpected.
|
|
|
|
Q_STATIC_ASSERT(Qt::AlignLeft == 1);
|
|
|
|
Q_STATIC_ASSERT(Qt::AlignRight == 2);
|
|
|
|
Q_STATIC_ASSERT(Qt::AlignHCenter == 4);
|
|
|
|
|
2017-08-15 09:44:00 +02:00
|
|
|
QVector<VLabelTemplateLine> lines;
|
|
|
|
|
|
|
|
if (not element.isNull())
|
|
|
|
{
|
|
|
|
QDomElement tagLine = element.firstChildElement();
|
|
|
|
while (tagLine.isNull() == false)
|
|
|
|
{
|
|
|
|
if (tagLine.tagName() == TagLine)
|
|
|
|
{
|
|
|
|
VLabelTemplateLine line;
|
|
|
|
line.line = GetParametrString(tagLine, AttrText, tr("<empty>"));
|
|
|
|
line.bold = GetParametrBool(tagLine, AttrBold, falseStr);
|
|
|
|
line.italic = GetParametrBool(tagLine, AttrItalic, falseStr);
|
2017-08-21 15:03:43 +02:00
|
|
|
line.alignment = static_cast<int>(GetParametrUInt(tagLine, AttrAlignment, "0"));
|
|
|
|
line.fontSizeIncrement = static_cast<int>(GetParametrUInt(tagLine, AttrFSIncrement, "0"));
|
2017-08-15 09:44:00 +02:00
|
|
|
lines.append(line);
|
|
|
|
}
|
|
|
|
tagLine = tagLine.nextSiblingElement(TagLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDomDocument::SetLabelTemplate(QDomElement &element, const QVector<VLabelTemplateLine> &lines)
|
|
|
|
{
|
|
|
|
if (not element.isNull())
|
|
|
|
{
|
|
|
|
for (int i=0; i < lines.size(); ++i)
|
|
|
|
{
|
|
|
|
QDomElement tagLine = createElement(TagLine);
|
|
|
|
|
|
|
|
SetAttribute(tagLine, AttrText, lines.at(i).line);
|
|
|
|
SetAttribute(tagLine, AttrBold, lines.at(i).bold);
|
|
|
|
SetAttribute(tagLine, AttrItalic, lines.at(i).italic);
|
|
|
|
SetAttribute(tagLine, AttrAlignment, lines.at(i).alignment);
|
2017-08-15 17:10:15 +02:00
|
|
|
SetAttribute(tagLine, AttrFSIncrement, lines.at(i).fontSizeIncrement);
|
2017-08-15 09:44:00 +02:00
|
|
|
|
|
|
|
element.appendChild(tagLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|