Salome HOME
Return original message if translation string is empty
[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 <ModelAPI_Feature.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_AttributeString.h>
15
16 #include <list>
17 #include <string>
18 #include <algorithm>
19
20 bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>& theArguments,
21                                                   std::list<std::string>& outFormats)
22 {
23   std::list<std::string>::const_iterator it = theArguments.begin();
24   bool result = true;
25   for (; it != theArguments.end(); ++it) {
26     std::string anArg = *it;
27     size_t aSepPos = anArg.find(":");
28     if (aSepPos == std::string::npos) {
29       result = false;
30       continue;
31     }
32     std::string aFormats = anArg.substr(0, aSepPos);
33     std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(), toupper);
34     std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
35     outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
36   }
37   return result;
38 }
39
40 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
41                                              const std::list<std::string>& theArguments,
42                                              std::string& theError) const
43 {
44   if (!theAttribute->isInitialized()) {
45     theError = "Is not initialized.";
46     return false;
47   }
48
49   const AttributeStringPtr aStrAttr =
50       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
51   if (!aStrAttr) {
52     theError = "Is not a string attribute.";
53     return false;
54   }
55
56   std::string aFileName = aStrAttr->value();
57   if (aFileName.empty()) {
58     theError = "File name is empty.";
59     return false;
60   }
61
62   std::list<std::string> aFormats;
63   ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
64   std::list<std::string>::const_iterator itFormats = aFormats.begin();
65   size_t aFileNameLen = aFileName.length();
66   std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
67   // Is file name ends with the format
68   for (; itFormats != aFormats.end(); ++itFormats) {
69     if (aFileNameLen > (*itFormats).length()) {
70       size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
71       if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
72         return true;
73       }
74     }
75   }
76   theError = "File name does not end with any available format.";
77   return false;
78 }