Salome HOME
updated copyright message
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Validators.cpp
index 3f52e2cf1023ccd86dc3161e219e459e8555a57e..4af768c74a04605af38fcac9888089401413b311 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -27,6 +27,7 @@
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeSelectionList.h>
 
 #include <list>
 #include <string>
@@ -45,7 +46,8 @@ bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>&
       continue;
     }
     std::string aFormats = anArg.substr(0, aSepPos);
-    std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(), toupper);
+    std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(),
+                   [](char c) { return static_cast<char>(::toupper(c)); });
     std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
     outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
   }
@@ -84,12 +86,14 @@ bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
   ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
   std::list<std::string>::const_iterator itFormats = aFormats.begin();
   size_t aFileNameLen = aFileName.length();
-  std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
+  std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(),
+                 [](char c) { return static_cast<char>(::toupper(c)); });
   // Is file name ends with the format
   for (; itFormats != aFormats.end(); ++itFormats) {
-    if (aFileNameLen > (*itFormats).length()) {
-      size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
-      if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
+    std::string aFormat = "." + *itFormats;
+    if (aFileNameLen > aFormat.length()) {
+      size_t aFormatBeginPos = aFileNameLen - aFormat.length();
+      if (aFileName.compare(aFormatBeginPos, std::string::npos, aFormat) == 0) {
         return true;
       }
     }
@@ -97,3 +101,45 @@ bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
   theError = "File name does not end with any available format.";
   return false;
 }
+
+
+bool ExchangePlugin_InHistoryValidator::isValid(const AttributePtr& theAttribute,
+                                                const std::list<std::string>& theArguments,
+                                                Events_InfoMessage& theError) const
+{
+  std::string anAttributeType = theAttribute->attributeType();
+  if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
+    AttributeSelectionPtr anAttrSelection =
+      std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+    ResultPtr aContext = anAttrSelection->context();
+    if (!aContext.get()) {
+      theError = "Error: Context is empty.";
+      return false;
+    }
+
+    FeaturePtr aFeature = ModelAPI_Feature::feature(aContext);
+    if (!aFeature->isInHistory()) {
+      theError = "Error: Feature is not in history.";
+      return false;
+    }
+  } else if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
+    AttributeSelectionListPtr anAttrSelectionList =
+      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+
+    // All objects should not be result constructions.
+    for(int anIndex = 0, aSize = anAttrSelectionList->size(); anIndex < aSize; ++anIndex) {
+      AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
+      if(!isValid(anAttrSelection, theArguments, theError)) {
+        return false;
+      }
+    }
+  } else {
+// LCOV_EXCL_START
+    theError = "Error: Attribute \"%1\" does not supported by this validator.";
+    theError.arg(anAttributeType);
+    return false;
+// LCOV_EXCL_STOP
+  }
+
+  return true;
+}