Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Validators.cpp
1 // Copyright (C) 2014-2019  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
31 #include <list>
32 #include <string>
33 #include <algorithm>
34
35 bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>& theArguments,
36                                                   std::list<std::string>& outFormats)
37 {
38   std::list<std::string>::const_iterator it = theArguments.begin();
39   bool result = true;
40   for (; it != theArguments.end(); ++it) {
41     std::string anArg = *it;
42     size_t aSepPos = anArg.find(":");
43     if (aSepPos == std::string::npos) {
44       result = false;
45       continue;
46     }
47     std::string aFormats = anArg.substr(0, aSepPos);
48     std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(), toupper);
49     std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
50     outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
51   }
52   return result;
53 }
54
55 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
56                                              const std::list<std::string>& theArguments,
57                                              Events_InfoMessage& theError) const
58 {
59   if (!theAttribute->isInitialized()) {
60 // LCOV_EXCL_START
61     theError = "%1 is not initialized.";
62     theError.arg(theAttribute->id());
63     return false;
64 // LCOV_EXCL_STOP
65   }
66
67   const AttributeStringPtr aStrAttr =
68       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
69   if (!aStrAttr) {
70 // LCOV_EXCL_START
71     theError = "%1 is not a string attribute.";
72     theError.arg(theAttribute->id());
73     return false;
74 // LCOV_EXCL_STOP
75   }
76
77   std::string aFileName = aStrAttr->value();
78   if (aFileName.empty()) {
79     theError = "File name is empty.";
80     return false;
81   }
82
83   std::list<std::string> aFormats;
84   ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
85   std::list<std::string>::const_iterator itFormats = aFormats.begin();
86   size_t aFileNameLen = aFileName.length();
87   std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
88   // Is file name ends with the format
89   for (; itFormats != aFormats.end(); ++itFormats) {
90     if (aFileNameLen > (*itFormats).length()) {
91       size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
92       if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
93         return true;
94       }
95     }
96   }
97   theError = "File name does not end with any available format.";
98   return false;
99 }