Salome HOME
Replace error construction with + with using %1 placeholders
[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 <ParametersPlugin_Parameter.h>
11
12 #include <Events_InfoMessage.h>
13
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_ResultParameter.h>
17 #include <ModelAPI_Tools.h>
18
19 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
20 {
21 }
22
23 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
24 {
25 }
26
27 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
28                                                  const std::list<std::string>& theArguments,
29                                                  Events_InfoMessage& theError) const
30 {
31   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
32   if (!aStrAttr->isInitialized()) {
33     theError = "Attribute \"%1\" is not initialized.";
34     theError.arg(aStrAttr->id());
35     return false;
36   }
37   bool isEmptyExpr = aStrAttr->value().empty();
38   if (isEmptyExpr) {
39     theError = "Attribute \"%1\" value is empty.";
40     theError.arg(aStrAttr->id());
41     return false;
42   }
43   if (!isVariable(aStrAttr->value())) {
44     theError = "Incorrect variable name.";
45     return false;
46   } 
47   if (!isUnique(theAttribute, aStrAttr->value())) {
48     theError = "Variable name is not unique.";
49     return false;
50   }
51   return true;
52 }
53
54 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
55 {
56   if (theString.empty())
57     return false;
58   std::string::const_iterator it = theString.begin();
59   if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
60     return false;
61   it++;
62   for ( ; it != theString.end(); ++it ) {
63     if(!(isalnum(*it) || (*it) == '_')) {
64       return false;
65     }
66   }
67   return true;
68 }
69
70 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
71                                                   const std::string& theString) const
72 {
73   DocumentPtr aDocument = theAttribute->owner()->document();
74   for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
75        anIndex < aSize; ++anIndex) {
76     ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
77     if (aParamObj->data()->name() != theString)
78       continue;
79     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
80     if (!aParam.get())
81       continue;
82     FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
83     if (aFeature == theAttribute->owner())
84       continue;
85     return false;
86   }
87   return true;
88 }
89
90 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
91 {
92
93 }
94
95 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
96 {
97
98 }
99
100 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
101                                                    const std::list<std::string>& theArguments,
102                                                    Events_InfoMessage& theError) const
103 {
104   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
105   ResultParameterPtr aParam =
106       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
107
108   AttributeStringPtr aStrAttr =
109       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
110   if (!aStrAttr->isInitialized()) {
111     theError = "Attribute \"%1\" is not initialized.";
112     theError.arg(aStrAttr->id());
113     return false;
114   }
115   bool isEmptyExpr = aStrAttr->value().empty();
116   if (isEmptyExpr) {
117     theError = "Expression is empty.";
118     return false;
119   }
120
121   if (!aParam.get()) {
122     theError = "Result is empty.";
123     return false;
124   }
125
126   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
127   return theError.empty();
128 }