Salome HOME
6f89b49b83514e2a9c6bcc0298a9d0ce76d32aa7
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Validators.cpp
1 // Copyright (C) 2014-2023  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(),
50                    [](char c) { return static_cast<char>(::toupper(c)); });
51     std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
52     outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
53   }
54   return result;
55 }
56
57 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
58                                              const std::list<std::string>& theArguments,
59                                              Events_InfoMessage& theError) const
60 {
61   if (!theAttribute->isInitialized()) {
62 // LCOV_EXCL_START
63     theError = "%1 is not initialized.";
64     theError.arg(theAttribute->id());
65     return false;
66 // LCOV_EXCL_STOP
67   }
68
69   const AttributeStringPtr aStrAttr =
70       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
71   if (!aStrAttr) {
72 // LCOV_EXCL_START
73     theError = "%1 is not a string attribute.";
74     theError.arg(theAttribute->id());
75     return false;
76 // LCOV_EXCL_STOP
77   }
78
79   std::string aFileName = aStrAttr->value();
80   if (aFileName.empty()) {
81     theError = "File name is empty.";
82     return false;
83   }
84
85   std::list<std::string> aFormats;
86   ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
87   std::list<std::string>::const_iterator itFormats = aFormats.begin();
88   size_t aFileNameLen = aFileName.length();
89   std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(),
90                  [](char c) { return static_cast<char>(::toupper(c)); });
91   // Is file name ends with the format
92   for (; itFormats != aFormats.end(); ++itFormats) {
93     std::string aFormat = "." + *itFormats;
94     if (aFileNameLen > aFormat.length()) {
95       size_t aFormatBeginPos = aFileNameLen - aFormat.length();
96       if (aFileName.compare(aFormatBeginPos, std::string::npos, aFormat) == 0) {
97         return true;
98       }
99     }
100   }
101   theError = "File name does not end with any available format.";
102   return false;
103 }
104
105
106 bool ExchangePlugin_InHistoryValidator::isValid(const AttributePtr& theAttribute,
107                                                 const std::list<std::string>& theArguments,
108                                                 Events_InfoMessage& theError) const
109 {
110   std::string anAttributeType = theAttribute->attributeType();
111   if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
112     AttributeSelectionPtr anAttrSelection =
113       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
114     ResultPtr aContext = anAttrSelection->context();
115     if (!aContext.get()) {
116       theError = "Error: Context is empty.";
117       return false;
118     }
119
120     FeaturePtr aFeature = ModelAPI_Feature::feature(aContext);
121     if (!aFeature->isInHistory()) {
122       theError = "Error: Feature is not in history.";
123       return false;
124     }
125   } else if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
126     AttributeSelectionListPtr anAttrSelectionList =
127       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
128
129     // All objects should not be result constructions.
130     for(int anIndex = 0, aSize = anAttrSelectionList->size(); anIndex < aSize; ++anIndex) {
131       AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
132       if(!isValid(anAttrSelection, theArguments, theError)) {
133         return false;
134       }
135     }
136   } else {
137 // LCOV_EXCL_START
138     theError = "Error: Attribute \"%1\" does not supported by this validator.";
139     theError.arg(anAttributeType);
140     return false;
141 // LCOV_EXCL_STOP
142   }
143
144   return true;
145 }