Salome HOME
Variable validation without regexp
[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   std::string::const_iterator it = theString.begin();
33   if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
34     return false;
35   it++;
36   for ( ; it != theString.end(); ++it ) {
37     if(!(isalnum(*it) || (*it) == '_')) {
38       return false;
39     }
40   }
41   return true;
42 }
43
44 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
45 {
46
47 }
48
49 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
50 {
51
52 }
53
54 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
55                                                   const std::list<std::string>& theArguments) const
56 {
57   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
58   ResultParameterPtr aParam =
59       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
60
61   AttributeStringPtr aStrAttr =
62     std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
63   bool isEmptyExpr = aStrAttr->value().empty();
64   if(isEmptyExpr)
65     return false;
66
67   if(!aParam.get())
68     return false;
69
70   return aFeature->error().empty();
71 }