1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ExchangePlugin_Validators.h>
23 #include <ExchangePlugin_Tools.h>
25 #include <Events_InfoMessage.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Object.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_AttributeString.h>
36 bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>& theArguments,
37 std::list<std::string>& outFormats)
39 std::list<std::string>::const_iterator it = theArguments.begin();
41 for (; it != theArguments.end(); ++it) {
42 std::string anArg = *it;
43 size_t aSepPos = anArg.find(":");
44 if (aSepPos == std::string::npos) {
48 std::string aFormats = anArg.substr(0, aSepPos);
49 std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(), toupper);
50 std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
51 outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
56 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
57 const std::list<std::string>& theArguments,
58 Events_InfoMessage& theError) const
60 if (!theAttribute->isInitialized()) {
61 theError = "%1 is not initialized.";
62 theError.arg(theAttribute->id());
66 const AttributeStringPtr aStrAttr =
67 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
69 theError = "%1 is not a string attribute.";
70 theError.arg(theAttribute->id());
74 std::string aFileName = aStrAttr->value();
75 if (aFileName.empty()) {
76 theError = "File name is empty.";
80 std::list<std::string> aFormats;
81 ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
82 std::list<std::string>::const_iterator itFormats = aFormats.begin();
83 size_t aFileNameLen = aFileName.length();
84 std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
85 // Is file name ends with the format
86 for (; itFormats != aFormats.end(); ++itFormats) {
87 if (aFileNameLen > (*itFormats).length()) {
88 size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
89 if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
94 theError = "File name does not end with any available format.";