1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchPlugin_Validators.cpp
4 // Created: 01 Aug 2014
5 // Author: Vitaly SMETANNIKOV
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>
18 bool ExchangePlugin_ImportFormatValidator::parseFormats(const std::list<std::string>& theArguments,
19 std::list<std::string>& outFormats)
21 std::list<std::string>::const_iterator it = theArguments.begin();
23 for (; it != theArguments.end(); ++it) {
24 std::string anArg = *it;
25 int aSepPos = anArg.find(":");
26 if (aSepPos == std::string::npos) {
30 std::string aFormatList = anArg.substr(0, aSepPos);
31 std::transform(aFormatList.begin(), aFormatList.end(), aFormatList.begin(), toupper);
32 std::istringstream aStream(aFormatList);
34 while (std::getline(aStream, aFormat, '|'))
35 outFormats.push_back(aFormat);
40 bool ExchangePlugin_ImportFormatValidator::isValid(const AttributePtr& theAttribute,
41 const std::list<std::string>& theArguments) const
43 SessionPtr aMgr = ModelAPI_Session::get();
44 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
45 if (theAttribute->isInitialized()) {
46 const AttributeStringPtr aStrAttr =
47 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
50 std::string aFileName = aStrAttr->value();
51 if (!aFileName.empty()) {
52 std::list<std::string> aFormats;
53 ExchangePlugin_ImportFormatValidator::parseFormats(theArguments, aFormats);
54 std::list<std::string>::const_iterator itFormats = aFormats.begin();
55 size_t aFileNameLen = aFileName.length();
56 std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
57 // Is file name ends with the format
58 for (; itFormats != aFormats.end(); ++itFormats) {
59 size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
60 if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {