Salome HOME
Fix warning
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    ExchangePlugin_Validators.cpp
4 // Created: Aug 01, 2014
5 // Author:  Sergey BELASH
6
7 #include <ExchangePlugin_Validators.h>
8
9 #include <ExchangePlugin_Tools.h>
10
11 #include <Events_InfoMessage.h>
12
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_Object.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_AttributeString.h>
17
18 #include <list>
19 #include <string>
20 #include <algorithm>
21
22 bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>& theArguments,
23                                                   std::list<std::string>& outFormats)
24 {
25   std::list<std::string>::const_iterator it = theArguments.begin();
26   bool result = true;
27   for (; it != theArguments.end(); ++it) {
28     std::string anArg = *it;
29     size_t aSepPos = anArg.find(":");
30     if (aSepPos == std::string::npos) {
31       result = false;
32       continue;
33     }
34     std::string aFormats = anArg.substr(0, aSepPos);
35     std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(), toupper);
36     std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
37     outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
38   }
39   return result;
40 }
41
42 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
43                                              const std::list<std::string>& theArguments,
44                                              Events_InfoMessage& theError) const
45 {
46   if (!theAttribute->isInitialized()) {
47     theError = "Is not initialized.";
48     return false;
49   }
50
51   const AttributeStringPtr aStrAttr =
52       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
53   if (!aStrAttr) {
54     theError = "Is not a string attribute.";
55     return false;
56   }
57
58   std::string aFileName = aStrAttr->value();
59   if (aFileName.empty()) {
60     theError = "File name is empty.";
61     return false;
62   }
63
64   std::list<std::string> aFormats;
65   ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
66   std::list<std::string>::const_iterator itFormats = aFormats.begin();
67   size_t aFileNameLen = aFileName.length();
68   std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
69   // Is file name ends with the format
70   for (; itFormats != aFormats.end(); ++itFormats) {
71     if (aFileNameLen > (*itFormats).length()) {
72       size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
73       if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
74         return true;
75       }
76     }
77   }
78   theError = "File name does not end with any available format.";
79   return false;
80 }