Salome HOME
Merge branch 'master' into cgt/devCEA
[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 = "%1 is not initialized.";
48     theError.arg(theAttribute->id());
49     return false;
50   }
51
52   const AttributeStringPtr aStrAttr =
53       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
54   if (!aStrAttr) {
55     theError = "%1 is not a string attribute.";
56     theError.arg(theAttribute->id());
57     return false;
58   }
59
60   std::string aFileName = aStrAttr->value();
61   if (aFileName.empty()) {
62     theError = "File name is empty.";
63     return false;
64   }
65
66   std::list<std::string> aFormats;
67   ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
68   std::list<std::string>::const_iterator itFormats = aFormats.begin();
69   size_t aFileNameLen = aFileName.length();
70   std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
71   // Is file name ends with the format
72   for (; itFormats != aFormats.end(); ++itFormats) {
73     if (aFileNameLen > (*itFormats).length()) {
74       size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
75       if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
76         return true;
77       }
78     }
79   }
80   theError = "File name does not end with any available format.";
81   return false;
82 }