Salome HOME
2582ca76e115063bcb941e10ab4d0e586e9e69b4
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Validators.cpp
1 // File:        SketchPlugin_Validators.cpp
2 // Created:     01 Aug 2014
3 // Author:      Vitaly SMETANNIKOV
4
5 #include <ExchangePlugin_Validators.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Object.h>
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_AttributeString.h>
10
11 #include <list>
12 #include <string>
13 #include <algorithm>
14
15 bool ExchangePlugin_ImportFormatValidator::parseFormats(const std::list<std::string>& theArguments,
16                                                         std::list<std::string>& outFormats)
17 {
18   std::list<std::string>::const_iterator it = theArguments.begin();
19   bool result = true;
20   for (; it != theArguments.end(); ++it) {
21     std::string anArg = *it;
22     int aSepPos = anArg.find(":");
23     if (aSepPos == std::string::npos) {
24       result = false;
25       continue;
26     }
27     std::string aFormat = anArg.substr(0, aSepPos);
28     std::transform(aFormat.begin(), aFormat.end(), aFormat.begin(), toupper);
29     outFormats.push_back(aFormat);
30   }
31   return result;
32 }
33
34 bool ExchangePlugin_ImportFormatValidator::parsePlugins(const std::list<std::string>& theArguments,
35                                                         std::list<std::string>& outPlugins)
36 {
37   std::list<std::string>::const_iterator it = theArguments.begin();
38   bool result = true;
39   for (; it != theArguments.end(); ++it) {
40     std::string anArg = *it;
41     int aSepPos = anArg.find(":");
42     if (aSepPos == std::string::npos) {
43       result = false;
44       continue;
45     }
46     outPlugins.push_back(anArg.substr(aSepPos + 1));
47   }
48   return result;
49 }
50
51 bool ExchangePlugin_ImportFormatValidator::isValid(const AttributePtr& theAttribute,
52                                                    const std::list<std::string>& theArguments) const
53 {
54   SessionPtr aMgr = ModelAPI_Session::get();
55   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
56   if (theAttribute->isInitialized()) {
57     const AttributeStringPtr aStrAttr =
58         std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
59     if(!aStrAttr)
60       return false;
61     std::string aFileName = aStrAttr->value();
62     if (!aFileName.empty()) {
63       std::list<std::string> aFormats;
64       ExchangePlugin_ImportFormatValidator::parseFormats(theArguments, aFormats);
65       std::list<std::string>::const_iterator itFormats = aFormats.begin();
66       size_t aFileNameLen = aFileName.length();
67       std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
68       // Is file name ends with the format
69       for (; itFormats != aFormats.end(); ++itFormats) {
70         size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
71         if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
72           return true;
73         }
74       }
75     }
76   }
77   return false;
78 }
79