1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ExchangePlugin_Validators.cpp
4 // Created: Aug 01, 2014
5 // Author: Sergey BELASH
7 #include <ExchangePlugin_Validators.h>
9 #include <ExchangePlugin_Tools.h>
11 #include <Events_InfoMessage.h>
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_Object.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_AttributeString.h>
22 bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>& theArguments,
23 std::list<std::string>& outFormats)
25 std::list<std::string>::const_iterator it = theArguments.begin();
27 for (; it != theArguments.end(); ++it) {
28 std::string anArg = *it;
29 size_t aSepPos = anArg.find(":");
30 if (aSepPos == std::string::npos) {
34 std::string aFormats = anArg.substr(0, aSepPos);
35 std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(), toupper);
36 std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
37 outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
42 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
43 const std::list<std::string>& theArguments,
44 Events_InfoMessage& theError) const
46 if (!theAttribute->isInitialized()) {
47 theError = "Is not initialized.";
51 const AttributeStringPtr aStrAttr =
52 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
54 theError = "Is not a string attribute.";
58 std::string aFileName = aStrAttr->value();
59 if (aFileName.empty()) {
60 theError = "File name is empty.";
64 std::list<std::string> aFormats;
65 ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
66 std::list<std::string>::const_iterator itFormats = aFormats.begin();
67 size_t aFileNameLen = aFileName.length();
68 std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
69 // Is file name ends with the format
70 for (; itFormats != aFormats.end(); ++itFormats) {
71 if (aFileNameLen > (*itFormats).length()) {
72 size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
73 if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
78 theError = "File name does not end with any available format.";