Salome HOME
771ea6c34bd461fdcf5899b88d8b9aece6736d89
[modules/shaper.git] / src / Model / Model_Validator.cpp
1 // File:        Model_Validator.cpp
2 // Created:     2 Jul 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Validator.h>
6 #include <ModelAPI_Feature.h>
7 #include <Events_Error.h>
8
9 using namespace std;
10
11 void Model_ValidatorsFactory::registerValidator(
12   const string& theID, ModelAPI_Validator* theValidator)
13 {
14   if (myIDs.find(theID) != myIDs.end()) {
15     Events_Error::send(string("Validator ") + theID + " is already registered");
16   } else {
17     myIDs[theID] = theValidator;
18   }
19 }
20
21 void Model_ValidatorsFactory::assignValidator(const string& theID, const string& theFeatureID)
22 {
23   bool isError = false;
24   map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
25   if (aVal == myIDs.end()) {
26     Events_Error::send(
27       string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
28     isError = true;
29   }
30   if (myFeatures.find(theFeatureID) != myFeatures.end()) {
31     // TODO: it is called many times now because of not-optimized XML reader
32       //Events_Error::send(
33       //  string("Validator for feature ") + theFeatureID + " is already registered");
34     isError = true;
35   }
36   if (!isError)
37     myFeatures[theFeatureID] = aVal->second;
38 }
39
40 void Model_ValidatorsFactory::assignValidator(const string& theID, 
41   const string& theFeatureID, const string& theAttrID, const list<string>& theArguments)
42 {
43   bool isError = false;
44   map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
45   if (aVal == myIDs.end()) {
46     Events_Error::send(
47       string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
48     isError = true;
49   }
50   // create feature-structures if not exist
51   map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::iterator
52     aFeature = myAttrs.find(theFeatureID);
53   if (aFeature == myAttrs.end()) {
54     myAttrs[theFeatureID] = map<string, pair<ModelAPI_Validator*, list<string> > >();
55     aFeature = myAttrs.find(theFeatureID);
56   }
57   // add attr-structure if not exist, or generate error if already exist
58   map<string, pair<ModelAPI_Validator*, list<string> > >::iterator 
59     anAttr = aFeature->second.find(theAttrID);
60   if (anAttr == aFeature->second.end()) {
61     if (!isError) {
62       aFeature->second[theAttrID] = 
63         pair<ModelAPI_Validator*, list<string> >(aVal->second, theArguments);
64     }
65   } else {
66     // TODO: it is called many times now because of not-optimized XML reader
67     //Events_Error::send(
68     //  string("Validator ") + theID + " for feature " + theFeatureID +
69     //          "attribute " + theAttrID + " is already registered");
70     isError = true;
71   }
72 }
73
74 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const string& theFeatureID) const
75 {
76   map<string, ModelAPI_Validator*>::const_iterator aFeature = myFeatures.find(theFeatureID);
77   if (aFeature != myFeatures.cend())
78     return aFeature->second;
79   return NULL; // not found
80 }
81
82 /*bool Model_ValidatorsFactory::validate(
83   const boost::shared_ptr<ModelAPI_Feature>& theFeature, const string& theAttrID ) const
84 {
85   map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::const_iterator
86     aFeature = myAttrs.find(theFeature->getKind());
87   if (aFeature == myAttrs.cend()) return true; // feature is not found
88   map<string, pair<ModelAPI_Validator*, list<string> > >::const_iterator 
89     anAttr = aFeature->second.find(theAttrID);
90   if (anAttr == aFeature->second.cend()) return true; // attribute is not found
91   return anAttr->second.first->validate(theFeature, theAttrID, anAttr->second.second);
92 }*/
93
94 Model_ValidatorsFactory::Model_ValidatorsFactory() : ModelAPI_ValidatorsFactory()
95 {
96 }