1 // File: Model_Validator.cpp
3 // Author: Mikhail PONIKAROV
5 #include <Model_Validator.h>
6 #include <ModelAPI_Feature.h>
7 #include <Events_Error.h>
11 void Model_ValidatorsFactory::registerValidator(
12 const string& theID, ModelAPI_Validator* theValidator)
14 if (myIDs.find(theID) != myIDs.end()) {
15 Events_Error::send(string("Validator ") + theID + " is already registered");
17 myIDs[theID] = theValidator;
21 void Model_ValidatorsFactory::assignValidator(const string& theID, const string& theFeatureID)
24 map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
25 if (aVal == myIDs.end()) {
27 string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
30 if (myFeatures.find(theFeatureID) != myFeatures.end()) {
31 // TODO: it is called many times now because of not-optimized XML reader
33 // string("Validator for feature ") + theFeatureID + " is already registered");
37 myFeatures[theFeatureID] = aVal->second;
40 void Model_ValidatorsFactory::assignValidator(const string& theID,
41 const string& theFeatureID, const string& theAttrID, const list<string>& theArguments)
44 map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
45 if (aVal == myIDs.end()) {
47 string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
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);
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()) {
62 aFeature->second[theAttrID] =
63 pair<ModelAPI_Validator*, list<string> >(aVal->second, theArguments);
66 // TODO: it is called many times now because of not-optimized XML reader
68 // string("Validator ") + theID + " for feature " + theFeatureID +
69 // "attribute " + theAttrID + " is already registered");
74 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const string& theFeatureID) const
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
82 const ModelAPI_Validator* Model_ValidatorsFactory::validator(
83 const std::string& theFeatureID, const std::string& theAttrID) const
85 map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::const_iterator
86 aFeature = myAttrs.find(theFeatureID);
87 if (aFeature == myAttrs.cend()) return NULL; // 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 NULL; // attribute is not found
91 return anAttr->second.first;
94 /*bool Model_ValidatorsFactory::validate(
95 const boost::shared_ptr<ModelAPI_Feature>& theFeature, const string& theAttrID ) const
97 map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::const_iterator
98 aFeature = myAttrs.find(theFeature->getKind());
99 if (aFeature == myAttrs.cend()) return true; // feature is not found
100 map<string, pair<ModelAPI_Validator*, list<string> > >::const_iterator
101 anAttr = aFeature->second.find(theAttrID);
102 if (anAttr == aFeature->second.cend()) return true; // attribute is not found
103 return anAttr->second.first->validate(theFeature, theAttrID, anAttr->second.second);
106 Model_ValidatorsFactory::Model_ValidatorsFactory() : ModelAPI_ValidatorsFactory()