From ad96323e29005b048635299cf64d58b0a3e07fec Mon Sep 17 00:00:00 2001 From: dismine Date: Tue, 16 Dec 2014 09:42:24 +0200 Subject: [PATCH] Preparation for new changes in pattern format. --HG-- branch : feature --- src/app/xml/vpattern.cpp | 70 +----------------------- src/app/xml/vpattern.h | 5 +- src/libs/ifc/xml/vabstractconverter.cpp | 40 +++++++++++++- src/libs/ifc/xml/vabstractconverter.h | 7 ++- src/libs/ifc/xml/vdomdocument.cpp | 72 +++++++++++++++++++++++-- src/libs/ifc/xml/vdomdocument.h | 14 ++--- src/libs/ifc/xml/vpatternconverter.cpp | 18 ++++++- src/libs/ifc/xml/vpatternconverter.h | 4 +- 8 files changed, 141 insertions(+), 89 deletions(-) diff --git a/src/app/xml/vpattern.cpp b/src/app/xml/vpattern.cpp index ed8efa99e..7673dbd44 100644 --- a/src/app/xml/vpattern.cpp +++ b/src/app/xml/vpattern.cpp @@ -514,16 +514,6 @@ void VPattern::DecrementReferens(quint32 id) const tool->decrementReferens(); } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief TestUniqueId test exist unique id in pattern file. Each id must be unique. - */ -void VPattern::TestUniqueId() const -{ - QVector vector; - CollectId(documentElement(), vector); -} - //--------------------------------------------------------------------------------------------------------------------- /** * @brief SPointActiveDraw return id base point current pattern peace. @@ -717,7 +707,7 @@ MeasurementsType VPattern::MType() const } //--------------------------------------------------------------------------------------------------------------------- -bool VPattern::SaveDocument(const QString &fileName, QString &error) +bool VPattern::SaveDocument(const QString &fileName, QString &error) const { try { @@ -2075,36 +2065,6 @@ void VPattern::ParseIncrementsElement(const QDomNode &node) } } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetParametrId return value id attribute. - * @param domElement tag in xml tree. - * @return id value. - */ -quint32 VPattern::GetParametrId(const QDomElement &domElement) const -{ - Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); - - quint32 id = 0; - - QString message = tr("Got wrong parameter id. Need only id > 0."); - try - { - id = GetParametrUInt(domElement, VDomDocument::AttrId, NULL_ID_STR); - if (id <= 0) - { - throw VExceptionWrongId(message, domElement); - } - } - catch (const VExceptionConversionError &e) - { - VExceptionWrongId excep(message, domElement); - excep.AddMoreInformation(e.ErrorMessage()); - throw excep; - } - return id; -} - //--------------------------------------------------------------------------------------------------------------------- QMap VPattern::GetGradationHeights() const { @@ -2507,34 +2467,6 @@ QString VPattern::GenerateLabel(const LabelType &type) const return QString(); } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief CollectId recursive function, try find id attribute in file. Throw exclusion if find not unique. - * @param node tag in xml tree. - * @param vector list with ids. - */ -void VPattern::CollectId(const QDomElement &node, QVector &vector) const -{ - if (node.hasAttribute(VDomDocument::AttrId)) - { - const quint32 id = GetParametrId(node); - if (vector.contains(id)) - { - throw VExceptionWrongId(tr("This id is not unique."), node); - } - vector.append(id); - } - - for (qint32 i=0; i GetGradationHeights() const; void SetGradationHeights(const QMap &options); @@ -265,7 +263,6 @@ private: void ParseToolsElement(VMainGraphicsScene *scene, const QDomElement& domElement, const Document &parse, const QString& type); void ParseIncrementsElement(const QDomNode& node); - void CollectId(const QDomElement &node, QVector &vector)const; void PrepareForParse(const Document &parse); void UpdateMeasurements(); void ToolsCommonAttributes(const QDomElement &domElement, quint32 &id); diff --git a/src/libs/ifc/xml/vabstractconverter.cpp b/src/libs/ifc/xml/vabstractconverter.cpp index a5fec8d05..f148d3fac 100644 --- a/src/libs/ifc/xml/vabstractconverter.cpp +++ b/src/libs/ifc/xml/vabstractconverter.cpp @@ -28,6 +28,7 @@ #include "vabstractconverter.h" #include "exception/vexception.h" +#include "exception/vexceptionwrongid.h" #include @@ -45,7 +46,7 @@ VAbstractConverter::~VAbstractConverter() {} //--------------------------------------------------------------------------------------------------------------------- -void VAbstractConverter::Convert() const +void VAbstractConverter::Convert() { if (ver == MaxVer()) { @@ -158,3 +159,40 @@ void VAbstractConverter::CheckVersion(int ver) const throw VException(errorMsg); } } + +//--------------------------------------------------------------------------------------------------------------------- +bool VAbstractConverter::SaveDocument(const QString &fileName, QString &error) const +{ + try + { + TestUniqueId(); + } + catch (const VExceptionWrongId &e) + { + error = tr("Error no unique id."); + return false; + } + + return VDomDocument::SaveDocument(fileName, error); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractConverter::Save() const +{ + QString error; + if (SaveDocument(fileName, error) == false) + { + VException e(error); + throw e; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractConverter::SetVersion(const QString &version) +{ + if (setTagText(TagVersion, version) == false) + { + VException e(tr("Could not change version.")); + throw e; + } +} diff --git a/src/libs/ifc/xml/vabstractconverter.h b/src/libs/ifc/xml/vabstractconverter.h index a4cc94ecd..4f7746380 100644 --- a/src/libs/ifc/xml/vabstractconverter.h +++ b/src/libs/ifc/xml/vabstractconverter.h @@ -38,7 +38,8 @@ public: VAbstractConverter(const QString &fileName); virtual ~VAbstractConverter(); - void Convert() const; + void Convert(); + virtual bool SaveDocument(const QString &fileName, QString &error) const; protected: int ver; @@ -46,6 +47,8 @@ protected: int GetVersion(const QString &version) const; void CheckVersion(int ver) const; + void Save() const; + void SetVersion(const QString &version); virtual int MinVer() const =0; virtual int MaxVer() const =0; @@ -54,7 +57,7 @@ protected: virtual QString MaxVerStr() const =0; virtual QString XSDSchema(int ver) const =0; - virtual void ApplyPatches() const =0; + virtual void ApplyPatches() =0; private: Q_DISABLE_COPY(VAbstractConverter) diff --git a/src/libs/ifc/xml/vdomdocument.cpp b/src/libs/ifc/xml/vdomdocument.cpp index 5dcf8d698..0f29fb26f 100644 --- a/src/libs/ifc/xml/vdomdocument.cpp +++ b/src/libs/ifc/xml/vdomdocument.cpp @@ -30,6 +30,7 @@ #include "exception/vexceptionconversionerror.h" #include "exception/vexceptionemptyparameter.h" #include "exception/vexceptionbadid.h" +#include "exception/vexceptionwrongid.h" #include #include @@ -323,6 +324,36 @@ qreal VDomDocument::GetParametrDouble(const QDomElement &domElement, const QStri return param; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetParametrId return value id attribute. + * @param domElement tag in xml tree. + * @return id value. + */ +quint32 VDomDocument::GetParametrId(const QDomElement &domElement) const +{ + Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); + + quint32 id = 0; + + QString message = tr("Got wrong parameter id. Need only id > 0."); + try + { + id = GetParametrUInt(domElement, VDomDocument::AttrId, NULL_ID_STR); + if (id <= 0) + { + throw VExceptionWrongId(message, domElement); + } + } + catch (const VExceptionConversionError &e) + { + VExceptionWrongId excep(message, domElement); + excep.AddMoreInformation(e.ErrorMessage()); + throw excep; + } + return id; +} + //--------------------------------------------------------------------------------------------------------------------- QString VDomDocument::UniqueTagText(const QString &tagName, const QString &defVal) const { @@ -346,6 +377,39 @@ QString VDomDocument::UniqueTagText(const QString &tagName, const QString &defVa return defVal; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief TestUniqueId test exist unique id in pattern file. Each id must be unique. + */ +void VDomDocument::TestUniqueId() const +{ + QVector vector; + CollectId(documentElement(), vector); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VDomDocument::CollectId(const QDomElement &node, QVector &vector) const +{ + if (node.hasAttribute(VDomDocument::AttrId)) + { + const quint32 id = GetParametrId(node); + if (vector.contains(id)) + { + throw VExceptionWrongId(tr("This id is not unique."), node); + } + vector.append(id); + } + + for (qint32 i=0; ielementsByTagName(tag); if (nodeList.isEmpty()) { qDebug()<<"Can't save tag "<= V_FORMAT_VERSION_CHECK(4, 4, 0)) -*/ -#define V_FORMAT_VERSION_CHECK(major, minor, patch) ((major<<16)|(minor<<8)|(patch)) - #ifdef Q_CC_GNU #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" @@ -104,12 +99,13 @@ public: QString GetParametrString(const QDomElement& domElement, const QString &name, const QString &defValue = QString()) const; qreal GetParametrDouble(const QDomElement& domElement, const QString &name, const QString &defValue) const; + quint32 GetParametrId(const QDomElement& domElement) const; static void ValidateXML(const QString &schema, const QString &fileName); void setXMLContent(const QString &fileName); static Unit StrToUnits(const QString &unit); static QString UnitsToStr(const Unit &unit, const bool translate = false); - virtual bool SaveDocument(const QString &fileName, QString &error); + virtual bool SaveDocument(const QString &fileName, QString &error) const; QString Major() const; QString Minor() const; QString Patch() const; @@ -118,12 +114,16 @@ public: QDomNode ParentNodeById(const quint32 &nodeId); QDomElement CloneNodeById(const quint32 &nodeId); QDomElement NodeById(const quint32 &nodeId); + static bool SafeCopy(const QString &source, const QString &destination, QString &error); protected: - void setTagText(const QString &tag, const QString &text); + bool setTagText(const QString &tag, const QString &text); QString UniqueTagText(const QString &tagName, const QString &defVal = QString()) const; + void TestUniqueId() const; + void CollectId(const QDomElement &node, QVector &vector)const; + private: Q_DISABLE_COPY(VDomDocument) /** @brief Map used for finding element by id. */ diff --git a/src/libs/ifc/xml/vpatternconverter.cpp b/src/libs/ifc/xml/vpatternconverter.cpp index 59aba9b57..8352cbc1c 100644 --- a/src/libs/ifc/xml/vpatternconverter.cpp +++ b/src/libs/ifc/xml/vpatternconverter.cpp @@ -87,6 +87,8 @@ QString VPatternConverter::XSDSchema(int ver) const switch(ver) { case (0x000101): +// return QStringLiteral("://schema/pattern/v0.1.2.xsd"); +// case (0x000102): return CurrentSchema; default: { @@ -98,13 +100,20 @@ QString VPatternConverter::XSDSchema(int ver) const } //--------------------------------------------------------------------------------------------------------------------- -void VPatternConverter::ApplyPatches() const +void VPatternConverter::ApplyPatches() { try { switch(ver) { case (0x000101): +// { +// ToV0_1_2(); +// const QString schema = XSDSchema(0x000102); +// ValidateXML(schema, fileName); +// break; +// } +// case (0x000102): break; default: break; @@ -128,3 +137,10 @@ void VPatternConverter::ApplyPatches() const throw e; } } + +//--------------------------------------------------------------------------------------------------------------------- +//void VPatternConverter::ToV0_1_2() +//{ +// SetVersion("0.1.2"); +// Save(); +//} diff --git a/src/libs/ifc/xml/vpatternconverter.h b/src/libs/ifc/xml/vpatternconverter.h index e4d3db2b3..5f1629976 100644 --- a/src/libs/ifc/xml/vpatternconverter.h +++ b/src/libs/ifc/xml/vpatternconverter.h @@ -48,11 +48,13 @@ protected: virtual QString MaxVerStr() const; QString XSDSchema(int ver) const; - virtual void ApplyPatches() const; + virtual void ApplyPatches(); private: Q_DISABLE_COPY(VPatternConverter) static const QString PatternMinVerStr; + +// void ToV0_1_2(); }; #endif // VPATTERNCONVERTER_H