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