Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom into Dev_0.7.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::isValid(const AttributePtr& theAttribute,
37                                                    const std::list<std::string>& theArguments) const
38 {
39   SessionPtr aMgr = ModelAPI_Session::get();
40   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
41   if (theAttribute->isInitialized()) {
42     const AttributeStringPtr aStrAttr =
43         std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
44     if(!aStrAttr)
45       return false;
46     std::string aFileName = aStrAttr->value();
47     if (!aFileName.empty()) {
48       std::list<std::string> aFormats;
49       ExchangePlugin_ImportFormatValidator::parseFormats(theArguments, aFormats);
50       std::list<std::string>::const_iterator itFormats = aFormats.begin();
51       size_t aFileNameLen = aFileName.length();
52       std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
53       // Is file name ends with the format
54       for (; itFormats != aFormats.end(); ++itFormats) {
55         size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
56         if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
57           return true;
58         }
59       }
60     }
61   }
62   return false;
63 }
64