]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_Validators.cpp
Salome HOME
Copyright update 2020
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Validators.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ExchangePlugin_Validators.h>
21
22 #include <ExchangePlugin_Tools.h>
23
24 #include <Events_InfoMessage.h>
25
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_AttributeString.h>
30 #include <ModelAPI_AttributeSelectionList.h>
31
32 #include <list>
33 #include <string>
34 #include <algorithm>
35
36 bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>& theArguments,
37                                                   std::list<std::string>& outFormats)
38 {
39   std::list<std::string>::const_iterator it = theArguments.begin();
40   bool result = true;
41   for (; it != theArguments.end(); ++it) {
42     std::string anArg = *it;
43     size_t aSepPos = anArg.find(":");
44     if (aSepPos == std::string::npos) {
45       result = false;
46       continue;
47     }
48     std::string aFormats = anArg.substr(0, aSepPos);
49     std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(), toupper);
50     std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
51     outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
52   }
53   return result;
54 }
55
56 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
57                                              const std::list<std::string>& theArguments,
58                                              Events_InfoMessage& theError) const
59 {
60   if (!theAttribute->isInitialized()) {
61 // LCOV_EXCL_START
62     theError = "%1 is not initialized.";
63     theError.arg(theAttribute->id());
64     return false;
65 // LCOV_EXCL_STOP
66   }
67
68   const AttributeStringPtr aStrAttr =
69       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
70   if (!aStrAttr) {
71 // LCOV_EXCL_START
72     theError = "%1 is not a string attribute.";
73     theError.arg(theAttribute->id());
74     return false;
75 // LCOV_EXCL_STOP
76   }
77
78   std::string aFileName = aStrAttr->value();
79   if (aFileName.empty()) {
80     theError = "File name is empty.";
81     return false;
82   }
83
84   std::list<std::string> aFormats;
85   ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
86   std::list<std::string>::const_iterator itFormats = aFormats.begin();
87   size_t aFileNameLen = aFileName.length();
88   std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
89   // Is file name ends with the format
90   for (; itFormats != aFormats.end(); ++itFormats) {
91     std::string aFormat = "." + *itFormats;
92     if (aFileNameLen > aFormat.length()) {
93       size_t aFormatBeginPos = aFileNameLen - aFormat.length();
94       if (aFileName.compare(aFormatBeginPos, std::string::npos, aFormat) == 0) {
95         return true;
96       }
97     }
98   }
99   theError = "File name does not end with any available format.";
100   return false;
101 }
102
103
104 bool ExchangePlugin_InHistoryValidator::isValid(const AttributePtr& theAttribute,
105                                                 const std::list<std::string>& theArguments,
106                                                 Events_InfoMessage& theError) const
107 {
108   std::string anAttributeType = theAttribute->attributeType();
109   if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
110     AttributeSelectionPtr anAttrSelection =
111       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
112     ResultPtr aContext = anAttrSelection->context();
113     if (!aContext.get()) {
114       theError = "Error: Context is empty.";
115       return false;
116     }
117
118     FeaturePtr aFeature = ModelAPI_Feature::feature(aContext);
119     if (!aFeature->isInHistory()) {
120       theError = "Error: Feature is not in history.";
121       return false;
122     }
123   } else if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
124     AttributeSelectionListPtr anAttrSelectionList =
125       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
126
127     // All objects should not be result constructions.
128     for(int anIndex = 0, aSize = anAttrSelectionList->size(); anIndex < aSize; ++anIndex) {
129       AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
130       if(!isValid(anAttrSelection, theArguments, theError)) {
131         return false;
132       }
133     }
134   } else {
135 // LCOV_EXCL_START
136     theError = "Error: Attribute \"%1\" does not supported by this validator.";
137     theError.arg(anAttributeType);
138     return false;
139 // LCOV_EXCL_STOP
140   }
141
142   return true;
143 }