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
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/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"
|
|
|
|
#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 <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>
|
2018-11-18 08:52:08 +01:00
|
|
|
#include <QRegularExpression>
|
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());
|
|
|
|
}
|
|
|
|
}
|
2020-01-09 17:56:37 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QList<QDomNode> GetChildElements(const QDomNode& e)
|
|
|
|
{
|
|
|
|
QDomNodeList children = e.childNodes();
|
|
|
|
QList<QDomNode> r;
|
|
|
|
r.reserve(children.size());
|
|
|
|
for (int k = 0; k < children.size(); ++k)
|
|
|
|
{
|
|
|
|
r << children.at(k);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QList<QDomNode> GetElementAttributes(const QDomNode& e)
|
|
|
|
{
|
|
|
|
QDomNamedNodeMap attributes = e.attributes();
|
|
|
|
QList<QDomNode> r;
|
|
|
|
r.reserve(attributes.size());
|
|
|
|
for (int k = 0; k < attributes.size(); ++k)
|
|
|
|
{
|
|
|
|
r << attributes.item(k);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool LessThen(const QDomNode &element1, const QDomNode &element2)
|
|
|
|
{
|
|
|
|
if (element1.nodeType() != element2.nodeType())
|
|
|
|
{
|
|
|
|
return element1.nodeType() < element2.nodeType();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString tag1 = element1.nodeName();
|
|
|
|
QString tag2 = element2.nodeName();
|
|
|
|
|
|
|
|
//qDebug() << tag1 <<tag2;
|
|
|
|
if (tag1 != tag2)
|
|
|
|
{
|
|
|
|
return tag1 < tag2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare attributes
|
|
|
|
QList<QDomNode> attributes1 = GetElementAttributes(element1);
|
|
|
|
QList<QDomNode> attributes2 = GetElementAttributes(element2);
|
|
|
|
|
|
|
|
if(attributes1.size() != attributes2.size())
|
|
|
|
{
|
|
|
|
return attributes1.size() < attributes2.size();
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:04:52 +01:00
|
|
|
auto CompareDomNodeLists = [](QList<QDomNode> list1, QList<QDomNode> list2, bool *stop)
|
2020-01-09 17:56:37 +01:00
|
|
|
{
|
2020-01-09 18:04:52 +01:00
|
|
|
*stop = false;
|
2020-01-09 17:56:37 +01:00
|
|
|
std::sort(list1.begin(), list1.end(), LessThen);
|
|
|
|
std::sort(list2.begin(), list2.end(), LessThen);
|
|
|
|
//qDebug() << "comparing sorted lists";
|
|
|
|
for(int k = 0; k < list1.size(); ++k)
|
|
|
|
{
|
|
|
|
if (!LessThen(list1[k], list2[k]))
|
|
|
|
{
|
|
|
|
if (LessThen(list2[k], list1[k]))
|
|
|
|
{
|
2020-01-09 18:04:52 +01:00
|
|
|
*stop = true;
|
2020-01-09 17:56:37 +01:00
|
|
|
//qDebug() << "false!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-09 18:04:52 +01:00
|
|
|
*stop = true;
|
2020-01-09 17:56:37 +01:00
|
|
|
//qDebug() << "true!";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2020-01-09 18:04:52 +01:00
|
|
|
bool stop = false;
|
|
|
|
bool result = CompareDomNodeLists(attributes1, attributes2, &stop);
|
2020-01-09 17:56:37 +01:00
|
|
|
if (stop)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare children
|
|
|
|
QList<QDomNode> elts1 = GetChildElements(element1);
|
|
|
|
QList<QDomNode> elts2 = GetChildElements(element2);
|
|
|
|
|
|
|
|
QString value1, value2;
|
|
|
|
|
|
|
|
if(elts1.size() != elts2.size())
|
|
|
|
{
|
|
|
|
return elts1.size() < elts2.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(elts1.isEmpty())
|
|
|
|
{
|
|
|
|
value1 = element1.nodeValue();
|
|
|
|
value2 = element2.nodeValue();
|
|
|
|
|
|
|
|
//qDebug() <<value1 << value2 << (value1 < value2);
|
|
|
|
return value1 < value2;
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:04:52 +01:00
|
|
|
result = CompareDomNodeLists(elts1, elts2, &stop);
|
2020-01-09 17:56:37 +01:00
|
|
|
if (stop)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-19 12:25:10 +01:00
|
|
|
} // namespace
|
2014-02-25 15:55:02 +01:00
|
|
|
|
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
|
|
|
|
2018-06-21 13:46:55 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VDomDocument::~VDomDocument()
|
|
|
|
{
|
|
|
|
m_watcher->cancel();
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-03-09 19:07:17 +01:00
|
|
|
QDomElement VDomDocument::elementById(quint32 id, const QString &tagName, bool updateCache)
|
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
|
|
|
{
|
2020-03-18 13:29:28 +01:00
|
|
|
const QDomElement e = m_elementIdCache.value(id);
|
|
|
|
if (e.parentNode().nodeType() != QDomNode::BaseNode)
|
|
|
|
{
|
|
|
|
if (not tagName.isEmpty())
|
|
|
|
{
|
|
|
|
if (e.tagName() == tagName)
|
|
|
|
{
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
2013-07-13 12:51:31 +02:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:07:17 +01:00
|
|
|
if (updateCache)
|
|
|
|
{ // Cached missed
|
|
|
|
RefreshElementIdCache();
|
|
|
|
}
|
2018-01-27 11:04:22 +01:00
|
|
|
|
2017-08-02 13:10:10 +02:00
|
|
|
if (tagName.isEmpty())
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2018-12-20 15:43:18 +01:00
|
|
|
// Because VDomDocument::find checks for unique id we must use temp cache
|
|
|
|
QHash<quint32, QDomElement> tmpCache;
|
|
|
|
if (VDomDocument::find(tmpCache, this->documentElement(), id))
|
2017-08-02 13:10:10 +02:00
|
|
|
{
|
2020-03-09 19:07:17 +01:00
|
|
|
return tmpCache.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
|
|
|
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-11-28 11:00:40 +01:00
|
|
|
if (cache.contains(elementId))
|
|
|
|
{
|
|
|
|
qWarning() << tr("Not unique id (%1)").arg(elementId);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-29 16:13:30 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VDomDocument::GetParametrInt(const QDomElement &domElement, const QString &name, const QString &defValue)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
QString parametr;
|
|
|
|
int value = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
parametr = GetParametrString(domElement, name, defValue);
|
|
|
|
value = parametr.toInt(&ok);
|
|
|
|
if (ok == false)
|
|
|
|
{
|
|
|
|
throw VExceptionConversionError(QObject::tr("Can't convert toInt parameter"), name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const VExceptionEmptyParameter &e)
|
|
|
|
{
|
|
|
|
VExceptionConversionError excep(QObject::tr("Can't convert toInt parameter"), name);
|
|
|
|
excep.AddMoreInformation(e.ErrorMessage());
|
|
|
|
throw excep;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2018-04-13 08:00:09 +02:00
|
|
|
const QStringList bools {trueStr, falseStr, QChar('1'), QChar('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;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
QString parametr = GetParametrString(domElement, name, defValue);
|
2018-04-13 08:14:55 +02:00
|
|
|
param = parametr.replace(QChar(','), QChar('.')).toDouble(&ok);
|
2014-03-14 16:59:28 +01:00
|
|
|
if (ok == false)
|
|
|
|
{
|
2021-01-19 12:25:10 +01:00
|
|
|
throw VExceptionConversionError(QObject::tr("Can't convert toDouble parameter"), name);
|
2014-03-14 16:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const VExceptionEmptyParameter &e)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2021-01-19 12:25:10 +01:00
|
|
|
VExceptionConversionError excep(QObject::tr("Can't convert toDouble parameter"), name);
|
2014-03-14 16:59:28 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
{
|
2018-11-28 11:00:40 +01:00
|
|
|
throw VExceptionWrongId(tr("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()
|
|
|
|
{
|
2018-06-21 13:46:55 +02:00
|
|
|
if (m_watcher->isFinished())
|
|
|
|
{
|
|
|
|
m_watcher->setFuture(QtConcurrent::run(this, &VDomDocument::RefreshCache, documentElement()));
|
|
|
|
}
|
2018-01-26 13:43:52 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 17:56:37 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VDomDocument::Compare(const QDomElement &element1, const QDomElement &element2)
|
|
|
|
{
|
|
|
|
QFuture<bool> lessThen2 = QtConcurrent::run(LessThen, element2, element1);
|
|
|
|
return !LessThen(element1, element2) && !lessThen2.result();
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:43:52 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDomDocument::CacheRefreshed()
|
|
|
|
{
|
|
|
|
m_elementIdCache = m_watcher->future().result();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-03-14 14:39:15 +01:00
|
|
|
const QString errorMsg(tr("Can't open file %1:\n%2.").arg(fileName, file.errorString()));
|
2014-03-24 16:02:57 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-10 11:42:37 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QDomElement VDomDocument::CreateElementWithText(const QString &tagName, const QString &text)
|
|
|
|
{
|
|
|
|
QDomElement tag = createElement(tagName);
|
|
|
|
tag.appendChild(createTextNode(text));
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
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");
|
2018-04-13 08:14:55 +02:00
|
|
|
QStringList v = version.split(QChar('.'));
|
2014-05-16 16:11:31 +02:00
|
|
|
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");
|
2018-04-13 08:14:55 +02:00
|
|
|
QStringList v = version.split(QChar('.'));
|
2014-05-16 16:11:31 +02:00
|
|
|
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");
|
2018-04-13 08:14:55 +02:00
|
|
|
QStringList v = version.split(QChar('.'));
|
2014-05-16 16:11:31 +02:00
|
|
|
return v.at(2);
|
|
|
|
}
|
|
|
|
|
2018-11-18 08:52:08 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VDomDocument::GetFormatVersionStr() const
|
|
|
|
{
|
|
|
|
const QDomNodeList nodeList = this->elementsByTagName(TagVersion);
|
|
|
|
if (nodeList.isEmpty())
|
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Couldn't get version information."));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nodeList.count() > 1)
|
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Too many tags <%1> in file.").arg(TagVersion));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QDomNode domNode = nodeList.at(0);
|
|
|
|
if (domNode.isNull() == false && domNode.isElement())
|
|
|
|
{
|
|
|
|
const QDomElement domElement = domNode.toElement();
|
|
|
|
if (domElement.isNull() == false)
|
|
|
|
{
|
|
|
|
return domElement.text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QString(QStringLiteral("0.0.0"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-09-24 13:24:10 +02:00
|
|
|
auto VDomDocument::GetFormatVersion(const QString &version) -> unsigned
|
2018-11-18 08:52:08 +01:00
|
|
|
{
|
|
|
|
ValidateVersion(version);
|
|
|
|
|
|
|
|
const QStringList ver = version.split(QChar('.'));
|
|
|
|
|
|
|
|
bool ok = false;
|
2021-09-24 13:24:10 +02:00
|
|
|
const unsigned major = ver.at(0).toUInt(&ok);
|
2018-11-18 08:52:08 +01:00
|
|
|
if (not ok)
|
|
|
|
{
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = false;
|
2021-09-24 13:24:10 +02:00
|
|
|
const unsigned minor = ver.at(1).toUInt(&ok);
|
2018-11-18 08:52:08 +01:00
|
|
|
if (not ok)
|
|
|
|
{
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = false;
|
2021-09-24 13:24:10 +02:00
|
|
|
const unsigned patch = ver.at(2).toUInt(&ok);
|
2018-11-18 08:52:08 +01:00
|
|
|
if (not ok)
|
|
|
|
{
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
2021-09-14 18:47:41 +02:00
|
|
|
return (major<<16u)|(minor<<8u)|(patch);
|
2018-11-18 08:52:08 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-01-11 16:24:16 +01:00
|
|
|
auto VDomDocument::setTagText(const QString &tag, const QString &text) -> bool
|
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
|
|
|
|
{
|
2022-01-11 16:24:16 +01:00
|
|
|
QDomNode domNode = nodeList.at(0);
|
|
|
|
if (not domNode.isNull() && domNode.isElement())
|
2014-03-28 14:11:46 +01:00
|
|
|
{
|
2022-01-11 16:24:16 +01:00
|
|
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-01-11 16:24:16 +01:00
|
|
|
auto VDomDocument::setTagText(QDomElement &domElement, const QString &text) -> bool
|
2016-04-06 04:34:08 +02:00
|
|
|
{
|
2022-01-11 16:24:16 +01:00
|
|
|
if (not domElement.isNull())
|
2016-04-06 04:34:08 +02:00
|
|
|
{
|
2022-01-11 16:24:16 +01:00
|
|
|
QDomNode oldText = domElement.firstChild();
|
|
|
|
const QDomText newText = createTextNode(text);
|
2016-04-07 11:35:08 +02:00
|
|
|
|
2022-01-11 16:24:16 +01:00
|
|
|
if (oldText.isNull())
|
|
|
|
{
|
|
|
|
domElement.appendChild(newText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (oldText.nodeType() == QDomNode::TextNode)
|
|
|
|
{
|
|
|
|
domElement.replaceChild(newText, oldText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RemoveAllChildren(domElement);
|
|
|
|
domElement.appendChild(newText);
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 11:35:08 +02:00
|
|
|
|
2016-04-06 04:34:08 +02: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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2019-10-30 14:25:25 +01:00
|
|
|
QDomElement VDomDocument::NodeById(const quint32 &nodeId, const QString &tagName)
|
2014-11-10 15:49:39 +01:00
|
|
|
{
|
2019-10-30 14:25:25 +01:00
|
|
|
QDomElement domElement = elementById(nodeId, tagName);
|
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;
|
|
|
|
|
2022-03-17 12:35:49 +01:00
|
|
|
//#ifdef Q_OS_WIN32
|
|
|
|
// qt_ntfs_permission_lookup++; // turn checking on
|
|
|
|
//#endif /*Q_OS_WIN32*/
|
2014-12-10 19:33:20 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-17 12:35:49 +01:00
|
|
|
//#ifdef Q_OS_WIN32
|
|
|
|
// qt_ntfs_permission_lookup--; // turn off check permission again
|
|
|
|
//#endif /*Q_OS_WIN32*/
|
2014-12-10 19:33:20 +01:00
|
|
|
|
|
|
|
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);
|
2018-04-12 12:13:19 +02:00
|
|
|
line.alignment = static_cast<int>(GetParametrUInt(tagLine, AttrAlignment, QChar('0')));
|
|
|
|
line.fontSizeIncrement = static_cast<int>(GetParametrUInt(tagLine, AttrFSIncrement, QChar('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())
|
|
|
|
{
|
2018-04-03 13:36:38 +02:00
|
|
|
for (auto &line : lines)
|
2017-08-15 09:44:00 +02:00
|
|
|
{
|
|
|
|
QDomElement tagLine = createElement(TagLine);
|
|
|
|
|
2018-04-03 13:36:38 +02:00
|
|
|
SetAttribute(tagLine, AttrText, line.line);
|
|
|
|
SetAttribute(tagLine, AttrBold, line.bold);
|
|
|
|
SetAttribute(tagLine, AttrItalic, line.italic);
|
|
|
|
SetAttribute(tagLine, AttrAlignment, line.alignment);
|
|
|
|
SetAttribute(tagLine, AttrFSIncrement, line.fontSizeIncrement);
|
2017-08-15 09:44:00 +02:00
|
|
|
|
|
|
|
element.appendChild(tagLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-18 08:52:08 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDomDocument::ValidateVersion(const QString &version)
|
|
|
|
{
|
|
|
|
const QRegularExpression rx(QStringLiteral("^([0-9]|[1-9][0-9]|[1-2][0-5][0-5]).([0-9]|[1-9][0-9]|[1-2][0-5][0-5])"
|
|
|
|
".([0-9]|[1-9][0-9]|[1-2][0-5][0-5])$"));
|
|
|
|
|
|
|
|
if (rx.match(version).hasMatch() == false)
|
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Version \"%1\" invalid.").arg(version));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version == QLatin1String("0.0.0"))
|
|
|
|
{
|
|
|
|
const QString errorMsg(tr("Version \"0.0.0\" invalid."));
|
|
|
|
throw VException(errorMsg);
|
|
|
|
}
|
|
|
|
}
|