Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_StandardValidatorXMLConverters.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 
00043 #ifndef TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
00044 #define TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
00045 
00050 #include "Teuchos_ValidatorXMLConverter.hpp"
00051 #include "Teuchos_StandardParameterEntryValidators.hpp"
00052 #include "Teuchos_ValidatorXMLConverterDB.hpp"
00053 #include "Teuchos_XMLParameterListReader.hpp"
00054 #include "Teuchos_DummyObjectGetter.hpp"
00055 
00056 
00057 namespace Teuchos {
00058 
00059 
00088 template<class IntegralType>
00089 class StringToIntegralValidatorXMLConverter : 
00090   public ValidatorXMLConverter
00091 {
00092 
00093 public:
00094 
00097 
00099   RCP<ParameterEntryValidator> convertXML(
00100     const XMLObject& xmlObj,
00101     const IDtoValidatorMap& validatorIDsMap) const;
00102 
00104   void convertValidator(
00105     const RCP<const ParameterEntryValidator> validator,
00106     XMLObject& xmlObj,
00107     const ValidatortoIDMap& validatorIDsMap) const;
00108 
00109   #ifdef HAVE_TEUCHOS_DEBUG
00110 
00111   RCP<const ParameterEntryValidator > 
00112   getDummyValidator() const{
00113     return DummyObjectGetter<
00114     StringToIntegralParameterEntryValidator<IntegralType> >::getDummyObject();
00115   }
00116   #endif
00117   
00119 
00120 private:
00121 
00124   
00126   static const std::string& getIntegralValueAttributeName() {
00127     static const std::string integralValueAttributeName_ = "integralValue";
00128     return integralValueAttributeName_;
00129   }
00130 
00132   static const std::string& getStringTagName() {
00133     static const std::string stringTagName_ = "String";
00134     return stringTagName_;
00135   }
00136 
00138   static const std::string& getStringValueAttributeName() {
00139     static const std::string stringValueAttributeName_ = "stringValue";
00140     return stringValueAttributeName_;
00141   }
00142 
00144   static const std::string& getStringDocAttributeName() {
00145     static const std::string stringDocAttributeName_ = "stringDoc";
00146     return stringDocAttributeName_;
00147   }
00148 
00150   static const std::string& getDefaultParameterAttributeName() {
00151     static const std::string defaultParameterAttributeName_ =
00152       "defaultParameterName";
00153     return defaultParameterAttributeName_;
00154   }
00155   
00157 
00158 };
00159 
00160 
00161 //
00162 // Implementations
00163 //
00164 
00165 
00166 template<class IntegralType>
00167 RCP<ParameterEntryValidator>
00168 StringToIntegralValidatorXMLConverter<IntegralType>::convertXML(
00169   const XMLObject& xmlObj,
00170   const IDtoValidatorMap& /*validatorIDsMap*/) const
00171 {
00172   Array<std::string> strings;
00173   Array<std::string> stringDocs;
00174   Array<IntegralType> integralValues;
00175   for (int i=0; i<xmlObj.numChildren(); ++i) {
00176     XMLObject currentChild = xmlObj.getChild(i);
00177     TEUCHOS_TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(), 
00178       BadTagException,  
00179       "Error converting xmlObject to "
00180       "StringToIntegralParameterEntryValidator." << std::endl << 
00181       "Unrecognized tag: " << currentChild.getTag());
00182     strings.append(currentChild.getRequired(getStringValueAttributeName()));
00183     if (currentChild.hasAttribute(getIntegralValueAttributeName())) {
00184       integralValues.append(
00185         currentChild.getRequired<IntegralType>(
00186           getIntegralValueAttributeName()));
00187     }
00188     if (currentChild.hasAttribute(getStringDocAttributeName())) {
00189       stringDocs.append(
00190         currentChild.getRequired<std::string>(getStringDocAttributeName()));
00191     }
00192   }
00193   std::string defaultParameterName = 
00194     xmlObj.getRequired(getDefaultParameterAttributeName());
00195 
00196   if(stringDocs.size() != 0 && integralValues.size() != 0){
00197     return stringToIntegralParameterEntryValidator<IntegralType>(
00198       strings, stringDocs, integralValues(), defaultParameterName);
00199   }
00200   else if(integralValues.size() != 0){
00201     return stringToIntegralParameterEntryValidator<IntegralType>(
00202       strings, integralValues(), defaultParameterName);
00203   }
00204   else{
00205     return stringToIntegralParameterEntryValidator<IntegralType>(
00206       strings, defaultParameterName);
00207   }
00208 }
00209 
00210 
00211 template<class IntegralType>
00212 void StringToIntegralValidatorXMLConverter<IntegralType>::convertValidator(
00213   const RCP<const ParameterEntryValidator> validator,
00214   XMLObject& xmlObj,
00215   const ValidatortoIDMap& /*validatorIDsMap*/) const
00216 {
00217   RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 
00218     castedValidator =
00219     rcp_dynamic_cast<
00220       const StringToIntegralParameterEntryValidator<IntegralType> >(
00221         validator, true);
00222 
00223   RCP<const Array<std::string> > stringValues =
00224     castedValidator->validStringValues();
00225   RCP<const Array<std::string> > stringDocValues = 
00226     castedValidator->getStringDocs();
00227 
00228   bool hasStringDocs = 
00229     !(stringDocValues.is_null()) && (stringDocValues->size() != 0);
00230   for (int i =0; i<stringValues->size(); ++i) {
00231     XMLObject stringTag(getStringTagName());
00232     stringTag.addAttribute(getStringValueAttributeName(), (*stringValues)[i]);
00233     stringTag.addAttribute(getIntegralValueAttributeName(),
00234       castedValidator->getIntegralValue((*stringValues)[i]));
00235     if (hasStringDocs) {
00236       stringTag.addAttribute(
00237         getStringDocAttributeName(), (*stringDocValues)[i]);
00238     }
00239     xmlObj.addChild(stringTag);
00240   }
00241   xmlObj.addAttribute(getDefaultParameterAttributeName(),
00242     castedValidator->getDefaultParameterName());
00243   xmlObj.addAttribute(getIntegralValueAttributeName(),
00244     TypeNameTraits<IntegralType>::name());
00245 }
00246 
00260 class TEUCHOS_LIB_DLL_EXPORT AnyNumberValidatorXMLConverter : public ValidatorXMLConverter
00261 {
00262 
00263 public:
00264 
00267 
00269   RCP<ParameterEntryValidator> convertXML(
00270     const XMLObject& xmlObj,
00271     const IDtoValidatorMap& validatorIDsMap) const;
00272 
00274   void convertValidator(
00275     const RCP<const ParameterEntryValidator> validator,
00276     XMLObject& xmlObj,
00277     const ValidatortoIDMap& validatorIDsMap) const;
00278 
00279   #ifdef HAVE_TEUCHOS_DEBUG
00280 
00281   RCP<const ParameterEntryValidator> getDummyValidator() const;
00282   #endif
00283   
00285 
00286 private:
00287 
00290   
00292   static const std::string& getAllowIntAttributeName() {
00293     static const std::string allowIntAttributeName_ = "allowInt";
00294     return allowIntAttributeName_;
00295   }
00296 
00298   static const std::string& getAllowDoubleAttributeName() {
00299     static const std::string allowDoubleAttributeName_ = "allowDouble";
00300     return allowDoubleAttributeName_;
00301   }
00302 
00304   static const std::string& getAllowStringAttributeName() {
00305     static const std::string allowStringAttributeName_ = "allowString";
00306     return allowStringAttributeName_;
00307   }
00308   
00310   static const std::string& getPrefferedTypeAttributeName() {
00311     static const std::string prefferedTypeAttributeName_ = "prefferedType";
00312     return prefferedTypeAttributeName_;
00313   }
00314   
00316  
00317 };
00318 
00319 
00334 template<class T>
00335 class EnhancedNumberValidatorXMLConverter : public ValidatorXMLConverter
00336 {
00337 
00338 public:
00339 
00342 
00344   RCP<ParameterEntryValidator> convertXML(
00345     const XMLObject& xmlObj,
00346     const IDtoValidatorMap& validatorIDsMap) const;
00347 
00349   void convertValidator(
00350     const RCP<const ParameterEntryValidator> validator,
00351     XMLObject& xmlObj,
00352     const ValidatortoIDMap& validatorIDsMap) const;
00353 
00354 #ifdef HAVE_TEUCHOS_DEBUG
00355 
00356   RCP<const ParameterEntryValidator> getDummyValidator() const{
00357     return DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject();
00358   }
00359 #endif
00360   
00362 
00363 private:
00364 
00367   
00369   static const std::string& getMinAttributeName() {
00370     static const std::string minAttributeName = "min";
00371     return minAttributeName;
00372   }
00373 
00375   static const std::string& getMaxAttributeName() {
00376     static const std::string maxAttributeName = "max";
00377     return maxAttributeName;
00378   }
00379 
00381   static const std::string& getStepAttributeName() {
00382     static const std::string stepAttributeName = "step";
00383     return stepAttributeName;
00384   }
00385 
00387   static const std::string& getPrecisionAttributeName() {
00388     static const std::string precisionAttributeName = "precision";
00389     return precisionAttributeName;
00390   }
00391   
00393 
00394 };
00395 
00396 
00397 template<class T>
00398 RCP<ParameterEntryValidator> 
00399 EnhancedNumberValidatorXMLConverter<T>::convertXML(
00400   const XMLObject& xmlObj,
00401   const IDtoValidatorMap& /*validatorIDsMap*/) const
00402 {
00403   RCP<EnhancedNumberValidator<T> > toReturn = 
00404     rcp(new EnhancedNumberValidator<T>);
00405   T step = xmlObj.getWithDefault(
00406     getStepAttributeName(), EnhancedNumberTraits<T>::defaultStep());
00407   toReturn->setStep(step);
00408   unsigned short int precision = xmlObj.getWithDefault(
00409    getPrecisionAttributeName(),
00410    EnhancedNumberTraits<T>::defaultPrecision());
00411   toReturn->setPrecision(precision);
00412   if (xmlObj.hasAttribute(getMinAttributeName())) {
00413     toReturn->setMin(xmlObj.getRequired<T>(getMinAttributeName()));
00414   }
00415   if (xmlObj.hasAttribute(getMaxAttributeName())) {
00416     toReturn->setMax(xmlObj.getRequired<T>(getMaxAttributeName()));
00417   }
00418   return toReturn;
00419 }
00420 
00421 
00422 template<class T>
00423 void EnhancedNumberValidatorXMLConverter<T>::convertValidator(
00424   const RCP<const ParameterEntryValidator > validator,
00425   XMLObject& xmlObj,
00426   const ValidatortoIDMap& /*validatorIDsMap*/) const
00427 {
00428   RCP<const EnhancedNumberValidator<T> > castedValidator =
00429     rcp_dynamic_cast<const EnhancedNumberValidator<T> >(validator, true);
00430   if (castedValidator->hasMin()) {
00431     xmlObj.addAttribute<T>(getMinAttributeName(), castedValidator->getMin());
00432   }
00433   if (castedValidator->hasMax()) {
00434     xmlObj.addAttribute<T>(getMaxAttributeName(), castedValidator->getMax());
00435   }
00436   xmlObj.addAttribute<T>(getStepAttributeName(), castedValidator->getStep());
00437   xmlObj.addAttribute<short unsigned int>(
00438     getPrecisionAttributeName(), castedValidator->getPrecision());
00439 }
00440 
00441 
00456 class TEUCHOS_LIB_DLL_EXPORT FileNameValidatorXMLConverter : public ValidatorXMLConverter
00457 {
00458 
00459 public:
00460 
00463 
00465   RCP<ParameterEntryValidator> convertXML(
00466     const XMLObject& xmlObj,
00467     const IDtoValidatorMap& validatorIDsMap) const;
00468 
00470   void convertValidator(
00471     const RCP<const ParameterEntryValidator> validator,
00472     XMLObject& xmlObj,
00473     const ValidatortoIDMap& validatorIDsMap) const;
00474 
00475   #ifdef HAVE_TEUCHOS_DEBUG
00476 
00477   RCP<const ParameterEntryValidator> getDummyValidator() const;
00478   #endif
00479   
00481 
00482 private:
00483 
00486   
00488   static const std::string& getFileMustExistAttributeName() {
00489     static const std::string fileMustExistAttributeName = "fileMustExist";
00490     return fileMustExistAttributeName;
00491   }
00492   
00494   
00495 };
00496 
00497 
00512 class TEUCHOS_LIB_DLL_EXPORT StringValidatorXMLConverter : public ValidatorXMLConverter
00513 {
00514 
00515 public:
00516 
00519 
00521   RCP<ParameterEntryValidator> convertXML(
00522     const XMLObject& xmlObj,
00523     const IDtoValidatorMap& validatorIDsMap) const;
00524 
00526   void convertValidator(
00527     const RCP<const ParameterEntryValidator> validator,
00528     XMLObject& xmlObj,
00529     const ValidatortoIDMap& validatorIDsMap) const;
00530 
00531   #ifdef HAVE_TEUCHOS_DEBUG
00532 
00533   RCP<const ParameterEntryValidator> getDummyValidator() const;
00534   #endif
00535   
00537 
00538 private:
00539   
00542   
00544   static const std::string& getStringTagName() {
00545     static const std::string stringTagName = "String";
00546     return stringTagName;
00547   }
00548 
00550   static const std::string& getStringValueAttributeName() {
00551     static const std::string stringValueAttributeName = "value";
00552     return stringValueAttributeName;
00553   }
00554   
00556 
00557 };
00558 
00559 template<class ValidatorType, class EntryType>
00560 class AbstractArrayValidatorXMLConverter : public ValidatorXMLConverter{
00561 public:
00562 
00565 
00567   RCP<ParameterEntryValidator> convertXML(
00568     const XMLObject& xmlObj,
00569     const IDtoValidatorMap& validatorIDsMap) const;
00570 
00572   void convertValidator(
00573     const RCP<const ParameterEntryValidator> validator,
00574     XMLObject& xmlObj,
00575     const ValidatortoIDMap& validatorIDsMap) const;
00576 
00578   
00581   
00585   virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > 
00586     getConcreteValidator(RCP<ValidatorType> prototypeValidator) const = 0;
00587 
00589 };
00590 
00591 
00592 template<class ValidatorType, class EntryType>
00593 RCP<ParameterEntryValidator>
00594 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertXML(
00595     const XMLObject& xmlObj,
00596     const IDtoValidatorMap& validatorIDsMap) const
00597 {
00598   RCP<ValidatorType> prototypeValidator;
00599   if(xmlObj.hasAttribute(
00600     ValidatorXMLConverter::getPrototypeIdAttributeName()))
00601   {
00602     IDtoValidatorMap::const_iterator result =
00603       validatorIDsMap.find(
00604         xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
00605           getPrototypeIdAttributeName()));
00606     if (result != validatorIDsMap.end() ) {
00607       prototypeValidator = 
00608         rcp_dynamic_cast<ValidatorType>(result->second, true);
00609     }
00610     else {
00611       TEUCHOS_TEST_FOR_EXCEPTION(true,
00612         MissingValidatorDefinitionException,
00613         "Could not find prototype validator with id: "
00614         << xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
00615           getPrototypeIdAttributeName()) << std::endl<< std::endl);
00616     }
00617   }
00618   else {
00619     prototypeValidator = rcp_dynamic_cast<ValidatorType>(
00620       ValidatorXMLConverterDB::convertXML(
00621         xmlObj.getChild(0), validatorIDsMap), true);
00622   }
00623   return getConcreteValidator(prototypeValidator);
00624 }
00625 
00626 template<class ValidatorType, class EntryType>
00627 void 
00628 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertValidator(
00629   const RCP<const ParameterEntryValidator> validator,
00630   XMLObject& xmlObj,
00631   const ValidatortoIDMap& validatorIDsMap) const
00632 {
00633   RCP<const AbstractArrayValidator<ValidatorType, EntryType> > castedValidator = 
00634     rcp_dynamic_cast<const AbstractArrayValidator<ValidatorType, EntryType> >(
00635       validator, true);
00636   if(validatorIDsMap.find(castedValidator->getPrototype()) 
00637     == validatorIDsMap.end())
00638   {
00639     xmlObj.addChild(ValidatorXMLConverterDB::convertValidator(
00640       castedValidator->getPrototype(), validatorIDsMap, false));
00641   }
00642   else{
00643     ParameterEntryValidator::ValidatorID prototypeID = 
00644       validatorIDsMap.find(castedValidator->getPrototype())->second;
00645   
00646     xmlObj.addAttribute<ParameterEntryValidator::ValidatorID>(
00647       getPrototypeIdAttributeName(), prototypeID);
00648   }
00649 }
00650 
00678 template<class ValidatorType, class EntryType>
00679 class ArrayValidatorXMLConverter : 
00680   public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
00681 {
00684 
00685   virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
00686     RCP<ValidatorType> prototypeValidator) const
00687   {
00688     return rcp(new ArrayValidator<ValidatorType, EntryType>(prototypeValidator));
00689   }
00690 
00691 #ifdef HAVE_TEUCHOS_DEBUG
00692 
00695   RCP<const ParameterEntryValidator> getDummyValidator() const{
00696     return DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >::
00697       getDummyObject();
00698   }
00700 #endif
00701 };
00702 
00730 template<class ValidatorType, class EntryType>
00731 class TwoDArrayValidatorXMLConverter : 
00732   public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
00733 {
00736 
00737   virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
00738     RCP<ValidatorType> prototypeValidator) const
00739   {
00740     return rcp(new TwoDArrayValidator<ValidatorType, EntryType>(prototypeValidator));
00741   }
00742 
00744   
00745 #ifdef HAVE_TEUCHOS_DEBUG
00746 
00749   RCP<const ParameterEntryValidator> getDummyValidator() const{
00750     return DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >::
00751       getDummyObject();
00752   }
00754 #endif
00755   
00756 };
00757 
00758 
00759 
00760 } // namespace Teuchos
00761 
00762 
00763 #endif  // TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
00764 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines