Salome HOME
Fix for import/export features
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Validators.cpp
1 /*
2  * Parameters_VariableValidator.cpp
3  *
4  *  Created on: Apr 9, 2015
5  *      Author: sbh
6  */
7
8 #include <ParametersPlugin_Validators.h>
9
10 #include <ModelAPI_AttributeString.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_ResultParameter.h>
13
14 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
15 {
16 }
17
18 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
19 {
20 }
21
22 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
23                                                  const std::list<std::string>& theArguments) const
24 {
25   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
26   bool result = isVariable(aStrAttr->value());
27   return result;
28 }
29
30 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
31 {
32   if (theString.empty())
33     return false;
34   std::string::const_iterator it = theString.begin();
35   if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
36     return false;
37   it++;
38   for ( ; it != theString.end(); ++it ) {
39     if(!(isalnum(*it) || (*it) == '_')) {
40       return false;
41     }
42   }
43   return true;
44 }
45
46 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
47 {
48
49 }
50
51 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
52 {
53
54 }
55
56 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
57                                                   const std::list<std::string>& theArguments) const
58 {
59   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
60   ResultParameterPtr aParam =
61       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
62
63   AttributeStringPtr aStrAttr =
64     std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
65   bool isEmptyExpr = aStrAttr->value().empty();
66   if(isEmptyExpr)
67     return false;
68
69   if(!aParam.get())
70     return false;
71
72   return aFeature->error().empty();
73 }