Salome HOME
CEA - Lot3 : Add LOFT Feature
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Validators.cpp
index d751c36bae268a1369436ca1c95d3eacb03e4cfe..3bd228d2a666805c60f71b3d962946054b8293c7 100644 (file)
@@ -1,18 +1,35 @@
-/*
- * Parameters_VariableValidator.cpp
- *
- *  Created on: Apr 9, 2015
- *      Author: sbh
- */
+// Copyright (C) 2014-2022  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include <ParametersPlugin_Validators.h>
 
 #include <ParametersPlugin_Parameter.h>
 
+#include <Events_InfoMessage.h>
+
+#include <Locale_Convert.h>
+
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_ResultParameter.h>
 #include <ModelAPI_Tools.h>
+#include <ModelAPI_Expression.h>
 
 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
 {
@@ -23,23 +40,25 @@ ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
 }
 
 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
-                                                 const std::list<std::string>& theArguments,
-                                                 std::string& theError) const
+                                                 const std::list<std::string>& /*theArguments*/,
+                                                 Events_InfoMessage& theError) const
 {
   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
   if (!aStrAttr->isInitialized()) {
-    theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
+    theError = "Attribute \"%1\" is not initialized.";
+    theError.arg(aStrAttr->id());
     return false;
   }
   bool isEmptyExpr = aStrAttr->value().empty();
   if (isEmptyExpr) {
-    theError = "Attribute \"" + aStrAttr->id() + "\" value is empty.";
+    theError = "Attribute \"%1\" value is empty.";
+    theError.arg(aStrAttr->id());
     return false;
   }
-  if (!isVariable(aStrAttr->value())) {
+  if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
     theError = "Incorrect variable name.";
     return false;
-  } 
+  }
   if (!isUnique(theAttribute, aStrAttr->value())) {
     theError = "Variable name is not unique.";
     return false;
@@ -47,22 +66,6 @@ bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribut
   return true;
 }
 
-bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
-{
-  if (theString.empty())
-    return false;
-  std::string::const_iterator it = theString.begin();
-  if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
-    return false;
-  it++;
-  for ( ; it != theString.end(); ++it ) {
-    if(!(isalnum(*it) || (*it) == '_')) {
-      return false;
-    }
-  }
-  return true;
-}
-
 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
                                                   const std::string& theString) const
 {
@@ -70,7 +73,7 @@ bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribu
   for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
        anIndex < aSize; ++anIndex) {
     ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
-    if (aParamObj->data()->name() != theString)
+    if (Locale::Convert::toString(aParamObj->data()->name()) != theString)
       continue;
     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
     if (!aParam.get())
@@ -94,17 +97,16 @@ ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
 }
 
 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
-                                                   const std::list<std::string>& theArguments,
-                                                   std::string& theError) const
+                                                   const std::list<std::string>& /*theArguments*/,
+                                                   Events_InfoMessage& theError) const
 {
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
-  ResultParameterPtr aParam =
-      std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
 
   AttributeStringPtr aStrAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
   if (!aStrAttr->isInitialized()) {
-    theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
+    theError = "Attribute \"%1\" is not initialized.";
+    theError.arg(aStrAttr->id());
     return false;
   }
   bool isEmptyExpr = aStrAttr->value().empty();
@@ -113,11 +115,6 @@ bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttrib
     return false;
   }
 
-  if (!aParam.get()) {
-    theError = "Result is empty.";
-    return false;
-  }
-
   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
   return theError.empty();
 }