From 296a9e5e93e4d4be67b7509cefa76920e05e97aa Mon Sep 17 00:00:00 2001 From: dismine Date: Mon, 7 Oct 2013 12:13:24 +0300 Subject: [PATCH] Test unique id. --HG-- branch : develop --- Valentina.pro | 6 ++++-- exception/vexceptionemptyparameter.cpp | 6 ++---- exception/vexceptionobjecterror.cpp | 6 ++---- exception/vexceptionuniqueid.cpp | 21 ++++++++++++++++++++ exception/vexceptionuniqueid.h | 24 +++++++++++++++++++++++ exception/vexceptionwrongparameterid.cpp | 6 ++---- mainwindow.cpp | 17 +++++++++++++++- xml/vdomdocument.cpp | 25 +++++++++++++++++++++++- xml/vdomdocument.h | 7 ++++--- 9 files changed, 99 insertions(+), 19 deletions(-) create mode 100644 exception/vexceptionuniqueid.cpp create mode 100644 exception/vexceptionuniqueid.h diff --git a/Valentina.pro b/Valentina.pro index 14e0c70e0..f94c2e337 100644 --- a/Valentina.pro +++ b/Valentina.pro @@ -95,7 +95,8 @@ SOURCES += main.cpp\ exception/vexceptionconversionerror.cpp \ exception/vexceptionemptyparameter.cpp \ exception/vexceptionobjecterror.cpp \ - widgets/vapplication.cpp + widgets/vapplication.cpp \ + exception/vexceptionuniqueid.cpp HEADERS += mainwindow.h \ widgets/vmaingraphicsscene.h \ @@ -185,7 +186,8 @@ HEADERS += mainwindow.h \ exception/vexceptionconversionerror.h \ exception/vexceptionemptyparameter.h \ exception/vexceptionobjecterror.h \ - widgets/vapplication.h + widgets/vapplication.h \ + exception/vexceptionuniqueid.h FORMS += mainwindow.ui \ dialogs/dialogsinglepoint.ui \ diff --git a/exception/vexceptionemptyparameter.cpp b/exception/vexceptionemptyparameter.cpp index 72c645724..fb7ee565f 100644 --- a/exception/vexceptionemptyparameter.cpp +++ b/exception/vexceptionemptyparameter.cpp @@ -27,10 +27,8 @@ VExceptionEmptyParameter::VExceptionEmptyParameter(const QString &what, const QS name(name), tagText(QString()), tagName(QString()), lineNumber(-1){ Q_ASSERT_X(!domElement.isNull(), Q_FUNC_INFO, "domElement is null"); Q_ASSERT_X(!name.isEmpty(), Q_FUNC_INFO, "Parameter name is empty"); - if(domElement.isText()){ - QDomText text = domElement.toText(); - tagText = text.data(); - } + QTextStream stream(&tagText); + domElement.save(stream, 4); tagName = domElement.tagName(); lineNumber = domElement.lineNumber(); } diff --git a/exception/vexceptionobjecterror.cpp b/exception/vexceptionobjecterror.cpp index 9c4f36c83..d1a424eff 100644 --- a/exception/vexceptionobjecterror.cpp +++ b/exception/vexceptionobjecterror.cpp @@ -25,10 +25,8 @@ VExceptionObjectError::VExceptionObjectError(const QString &what, const QDomElement &domElement): VException(what), tagText(QString()), tagName(QString()), lineNumber(-1), moreInfo(QString()){ Q_ASSERT_X(!domElement.isNull(), Q_FUNC_INFO, "domElement is null"); - if(domElement.isText()){ - QDomText text = domElement.toText(); - tagText = text.data(); - } + QTextStream stream(&tagText); + domElement.save(stream, 4); tagName = domElement.tagName(); lineNumber = domElement.lineNumber(); } diff --git a/exception/vexceptionuniqueid.cpp b/exception/vexceptionuniqueid.cpp new file mode 100644 index 000000000..51ddf4c82 --- /dev/null +++ b/exception/vexceptionuniqueid.cpp @@ -0,0 +1,21 @@ +#include "vexceptionuniqueid.h" +#include + +VExceptionUniqueId::VExceptionUniqueId(const QString &what, const QDomElement &domElement) + :VException(what), tagText(QString()), tagName(QString()), lineNumber(-1){ + Q_ASSERT_X(!domElement.isNull(), Q_FUNC_INFO, "domElement is null"); + QTextStream stream(&tagText); + domElement.save(stream, 4); + tagName = domElement.tagName(); + lineNumber = domElement.lineNumber(); +} + +QString VExceptionUniqueId::ErrorMessage() const{ + QString error = QString("ExceptionUniqueId: %1").arg(what); + return error; +} + +QString VExceptionUniqueId::DetailedInformation() const{ + QString detail = QString("tag: %1 in line %2\nFull tag:\n%3").arg(tagName).arg(lineNumber).arg(tagText); + return detail; +} diff --git a/exception/vexceptionuniqueid.h b/exception/vexceptionuniqueid.h new file mode 100644 index 000000000..1bf1760d4 --- /dev/null +++ b/exception/vexceptionuniqueid.h @@ -0,0 +1,24 @@ +#ifndef VEXCEPTIONUNIQUEID_H +#define VEXCEPTIONUNIQUEID_H + +#include "vexception.h" +#include + +class VExceptionUniqueId : public VException{ +public: + VExceptionUniqueId(const QString &what, const QDomElement &domElement); + VExceptionUniqueId(const VExceptionUniqueId &e):VException(e), tagText(e.TagText()), + tagName(e.TagName()), lineNumber(e.LineNumber()){} + virtual ~VExceptionUniqueId() noexcept(true){} + virtual QString ErrorMessage() const; + virtual QString DetailedInformation() const; + QString TagText() const {return tagText;} + QString TagName() const {return tagName;} + qint32 LineNumber() const {return lineNumber;} +protected: + QString tagText; + QString tagName; + qint32 lineNumber; +}; + +#endif // VEXCEPTIONUNIQUEID_H diff --git a/exception/vexceptionwrongparameterid.cpp b/exception/vexceptionwrongparameterid.cpp index 31b3d95e4..0da7ea21b 100644 --- a/exception/vexceptionwrongparameterid.cpp +++ b/exception/vexceptionwrongparameterid.cpp @@ -25,10 +25,8 @@ VExceptionWrongParameterId::VExceptionWrongParameterId(const QString &what, const QDomElement &domElement): VException(what), tagText(QString()), tagName(QString()), lineNumber(-1){ Q_ASSERT_X(!domElement.isNull(), Q_FUNC_INFO, "domElement is null"); - if(domElement.isText()){ - QDomText text = domElement.toText(); - tagText = text.data(); - } + QTextStream stream(&tagText); + domElement.save(stream, 4); tagName = domElement.tagName(); lineNumber = domElement.lineNumber(); } diff --git a/mainwindow.cpp b/mainwindow.cpp index e4a612332..5a023eff3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -35,6 +35,7 @@ #include "exception/vexceptionconversionerror.h" #include "exception/vexceptionemptyparameter.h" #include "exception/vexceptionwrongparameterid.h" +#include "exception/vexceptionuniqueid.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), tool(Tool::ArrowTool), currentScene(0), sceneDraw(0), @@ -1011,7 +1012,7 @@ void MainWindow::OpenPattern(const QString &fileName){ if(file.open(QIODevice::ReadOnly)){ if(doc->setContent(&file, &errorMsg, &errorLine, &errorColumn)){ disconnect(comboBoxDraws, static_cast(&QComboBox::currentIndexChanged), - this, &MainWindow::currentDrawChanged); + this, &MainWindow::currentDrawChanged); try{ doc->Parse(Document::FullParse, sceneDraw, sceneDetails); } @@ -1070,6 +1071,20 @@ void MainWindow::OpenPattern(const QString &fileName){ Clear(); return; } + catch(const VExceptionUniqueId &e){ + QMessageBox msgBox; + msgBox.setWindowTitle(tr("Error!")); + msgBox.setText(tr("Error don't unique id.")); + msgBox.setInformativeText(e.ErrorMessage()); + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.setDefaultButton(QMessageBox::Ok); + msgBox.setDetailedText(e.DetailedInformation()); + msgBox.setIcon(QMessageBox::Critical); + msgBox.exec(); + file.close(); + Clear(); + return; + } connect(comboBoxDraws, static_cast(&QComboBox::currentIndexChanged), this, &MainWindow::currentDrawChanged); QString nameDraw = doc->GetNameActivDraw(); diff --git a/xml/vdomdocument.cpp b/xml/vdomdocument.cpp index 161a272c6..10f601a82 100644 --- a/xml/vdomdocument.cpp +++ b/xml/vdomdocument.cpp @@ -33,7 +33,7 @@ #include "exception/vexceptionemptyparameter.h" #include "exception/vexceptionbadid.h" #include "exception/vexceptionobjecterror.h" - +#include "exception/vexceptionuniqueid.h" VDomDocument::VDomDocument(VContainer *data, QComboBox *comboBoxDraws, Draw::Draws *mode) : QDomDocument(), map(QHash()), nameActivDraw(QString()), data(data), @@ -255,6 +255,7 @@ void VDomDocument::Parse(Document::Documents parse, VMainGraphicsScene *sceneDra Q_CHECK_PTR(sceneDraw); Q_CHECK_PTR(sceneDetail); if(parse == Document::FullParse){ + TestUniqueId(); data->Clear(); nameActivDraw.clear(); sceneDraw->clear(); @@ -370,6 +371,28 @@ qreal VDomDocument::GetParametrDouble(const QDomElement &domElement, const QStri return param; } +void VDomDocument::TestUniqueId() const{ + QVector vector; + CollectId(this->documentElement(), vector); +} + +void VDomDocument::CollectId(QDomElement node, QVector &vector) const{ + if (node.hasAttribute("id")) { + qint64 id = GetParametrId(node); + if(vector.contains(id)){ + throw VExceptionUniqueId(tr("This id is not unique."), node); + } + vector.append(id); + } + + for (qint32 i=0; i &vector)const; }; #pragma GCC diagnostic pop