|
Teuchos - Trilinos Tools Package
Version of the Day
|
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 00098 template<class IntegralType> 00099 class StringToIntegralValidatorXMLConverter : 00100 public ValidatorXMLConverter 00101 { 00102 00103 public: 00104 00107 00109 RCP<ParameterEntryValidator> convertXML( 00110 const XMLObject& xmlObj, 00111 const IDtoValidatorMap& validatorIDsMap) const; 00112 00114 void convertValidator( 00115 const RCP<const ParameterEntryValidator> validator, 00116 XMLObject& xmlObj, 00117 const ValidatortoIDMap& validatorIDsMap) const; 00118 00119 #ifdef HAVE_TEUCHOS_DEBUG 00120 00121 RCP<const ParameterEntryValidator > 00122 getDummyValidator() const{ 00123 return DummyObjectGetter< 00124 StringToIntegralParameterEntryValidator<IntegralType> >::getDummyObject(); 00125 } 00126 #endif 00127 00129 00130 private: 00131 00134 00136 static const std::string& getIntegralValueAttributeName() { 00137 static const std::string integralValueAttributeName_ = "integralValue"; 00138 return integralValueAttributeName_; 00139 } 00140 00142 static const std::string& getStringTagName() { 00143 static const std::string stringTagName_ = "String"; 00144 return stringTagName_; 00145 } 00146 00148 static const std::string& getStringValueAttributeName() { 00149 static const std::string stringValueAttributeName_ = "stringValue"; 00150 return stringValueAttributeName_; 00151 } 00152 00154 static const std::string& getStringDocAttributeName() { 00155 static const std::string stringDocAttributeName_ = "stringDoc"; 00156 return stringDocAttributeName_; 00157 } 00158 00160 static const std::string& getDefaultParameterAttributeName() { 00161 static const std::string defaultParameterAttributeName_ = 00162 "defaultParameterName"; 00163 return defaultParameterAttributeName_; 00164 } 00165 00167 static const std::string& getCaseSensitiveAttributeName() { 00168 static const std::string caseSensitiveAttributeName_ = 00169 "caseSensitive"; 00170 return caseSensitiveAttributeName_; 00171 } 00172 00174 00175 }; 00176 00177 00178 // 00179 // Implementations 00180 // 00181 00182 00183 template<class IntegralType> 00184 RCP<ParameterEntryValidator> 00185 StringToIntegralValidatorXMLConverter<IntegralType>::convertXML( 00186 const XMLObject& xmlObj, 00187 const IDtoValidatorMap& /*validatorIDsMap*/) const 00188 { 00189 Array<std::string> strings; 00190 Array<std::string> stringDocs; 00191 Array<IntegralType> integralValues; 00192 for (int i=0; i<xmlObj.numChildren(); ++i) { 00193 XMLObject currentChild = xmlObj.getChild(i); 00194 TEUCHOS_TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(), 00195 BadTagException, 00196 "Error converting xmlObject to " 00197 "StringToIntegralParameterEntryValidator." << std::endl << 00198 "Unrecognized tag: " << currentChild.getTag()); 00199 strings.append(currentChild.getRequired(getStringValueAttributeName())); 00200 if (currentChild.hasAttribute(getIntegralValueAttributeName())) { 00201 integralValues.append( 00202 currentChild.getRequired<IntegralType>( 00203 getIntegralValueAttributeName())); 00204 } 00205 if (currentChild.hasAttribute(getStringDocAttributeName())) { 00206 stringDocs.append( 00207 currentChild.getRequired<std::string>(getStringDocAttributeName())); 00208 } 00209 } 00210 std::string defaultParameterName = 00211 xmlObj.getRequired(getDefaultParameterAttributeName()); 00212 00213 // The "caseSensitive" attribute is not required. It is true by default. 00214 const bool caseSensitive = 00215 xmlObj.getWithDefault<bool> (getCaseSensitiveAttributeName (), true); 00216 00217 typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type; 00218 if (stringDocs.size() != 0 && integralValues.size() != 0) { 00219 return rcp (new ret_type (strings, stringDocs, integralValues (), defaultParameterName, caseSensitive)); 00220 } 00221 else if (integralValues.size() != 0) { 00222 return rcp (new ret_type (strings, integralValues(), defaultParameterName, caseSensitive)); 00223 } 00224 else { 00225 return rcp (new ret_type (strings, defaultParameterName, caseSensitive)); 00226 } 00227 } 00228 00229 00230 template<class IntegralType> 00231 void StringToIntegralValidatorXMLConverter<IntegralType>::convertValidator( 00232 const RCP<const ParameterEntryValidator> validator, 00233 XMLObject& xmlObj, 00234 const ValidatortoIDMap& /*validatorIDsMap*/) const 00235 { 00236 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 00237 castedValidator = 00238 rcp_dynamic_cast< 00239 const StringToIntegralParameterEntryValidator<IntegralType> >( 00240 validator, true); 00241 00242 RCP<const Array<std::string> > stringValues = 00243 castedValidator->validStringValues(); 00244 RCP<const Array<std::string> > stringDocValues = 00245 castedValidator->getStringDocs(); 00246 00247 bool hasStringDocs = 00248 !(stringDocValues.is_null()) && (stringDocValues->size() != 0); 00249 for (int i =0; i<stringValues->size(); ++i) { 00250 XMLObject stringTag(getStringTagName()); 00251 stringTag.addAttribute(getStringValueAttributeName(), (*stringValues)[i]); 00252 stringTag.addAttribute(getIntegralValueAttributeName(), 00253 castedValidator->getIntegralValue((*stringValues)[i])); 00254 if (hasStringDocs) { 00255 stringTag.addAttribute( 00256 getStringDocAttributeName(), (*stringDocValues)[i]); 00257 } 00258 xmlObj.addChild(stringTag); 00259 } 00260 xmlObj.addAttribute(getDefaultParameterAttributeName(), 00261 castedValidator->getDefaultParameterName()); 00262 00263 // Add "caseSensitive" bool attribute here. 00264 const bool caseSensitive = castedValidator->isCaseSensitive (); 00265 xmlObj.addBool (getCaseSensitiveAttributeName (), caseSensitive); 00266 00267 xmlObj.addAttribute(getIntegralValueAttributeName(), 00268 TypeNameTraits<IntegralType>::name()); 00269 } 00270 00284 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT AnyNumberValidatorXMLConverter : public ValidatorXMLConverter 00285 { 00286 00287 public: 00288 00291 00293 RCP<ParameterEntryValidator> convertXML( 00294 const XMLObject& xmlObj, 00295 const IDtoValidatorMap& validatorIDsMap) const; 00296 00298 void convertValidator( 00299 const RCP<const ParameterEntryValidator> validator, 00300 XMLObject& xmlObj, 00301 const ValidatortoIDMap& validatorIDsMap) const; 00302 00303 #ifdef HAVE_TEUCHOS_DEBUG 00304 00305 RCP<const ParameterEntryValidator> getDummyValidator() const; 00306 #endif 00307 00309 00310 private: 00311 00314 00316 static const std::string& getAllowIntAttributeName() { 00317 static const std::string allowIntAttributeName_ = "allowInt"; 00318 return allowIntAttributeName_; 00319 } 00320 00322 static const std::string& getAllowDoubleAttributeName() { 00323 static const std::string allowDoubleAttributeName_ = "allowDouble"; 00324 return allowDoubleAttributeName_; 00325 } 00326 00328 static const std::string& getAllowStringAttributeName() { 00329 static const std::string allowStringAttributeName_ = "allowString"; 00330 return allowStringAttributeName_; 00331 } 00332 00334 static const std::string& getPrefferedTypeAttributeName() { 00335 static const std::string prefferedTypeAttributeName_ = "prefferedType"; 00336 return prefferedTypeAttributeName_; 00337 } 00338 00340 00341 }; 00342 00343 00358 template<class T> 00359 class EnhancedNumberValidatorXMLConverter : public ValidatorXMLConverter 00360 { 00361 00362 public: 00363 00366 00368 RCP<ParameterEntryValidator> convertXML( 00369 const XMLObject& xmlObj, 00370 const IDtoValidatorMap& validatorIDsMap) const; 00371 00373 void convertValidator( 00374 const RCP<const ParameterEntryValidator> validator, 00375 XMLObject& xmlObj, 00376 const ValidatortoIDMap& validatorIDsMap) const; 00377 00378 #ifdef HAVE_TEUCHOS_DEBUG 00379 00380 RCP<const ParameterEntryValidator> getDummyValidator() const{ 00381 return DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject(); 00382 } 00383 #endif 00384 00386 00387 private: 00388 00391 00393 static const std::string& getMinAttributeName() { 00394 static const std::string minAttributeName = "min"; 00395 return minAttributeName; 00396 } 00397 00399 static const std::string& getMaxAttributeName() { 00400 static const std::string maxAttributeName = "max"; 00401 return maxAttributeName; 00402 } 00403 00405 static const std::string& getStepAttributeName() { 00406 static const std::string stepAttributeName = "step"; 00407 return stepAttributeName; 00408 } 00409 00411 static const std::string& getPrecisionAttributeName() { 00412 static const std::string precisionAttributeName = "precision"; 00413 return precisionAttributeName; 00414 } 00415 00417 00418 }; 00419 00420 00421 template<class T> 00422 RCP<ParameterEntryValidator> 00423 EnhancedNumberValidatorXMLConverter<T>::convertXML( 00424 const XMLObject& xmlObj, 00425 const IDtoValidatorMap& /*validatorIDsMap*/) const 00426 { 00427 RCP<EnhancedNumberValidator<T> > toReturn = 00428 rcp(new EnhancedNumberValidator<T>); 00429 T step = xmlObj.getWithDefault( 00430 getStepAttributeName(), EnhancedNumberTraits<T>::defaultStep()); 00431 toReturn->setStep(step); 00432 unsigned short int precision = xmlObj.getWithDefault( 00433 getPrecisionAttributeName(), 00434 EnhancedNumberTraits<T>::defaultPrecision()); 00435 toReturn->setPrecision(precision); 00436 if (xmlObj.hasAttribute(getMinAttributeName())) { 00437 toReturn->setMin(xmlObj.getRequired<T>(getMinAttributeName())); 00438 } 00439 if (xmlObj.hasAttribute(getMaxAttributeName())) { 00440 toReturn->setMax(xmlObj.getRequired<T>(getMaxAttributeName())); 00441 } 00442 return toReturn; 00443 } 00444 00445 00446 template<class T> 00447 void EnhancedNumberValidatorXMLConverter<T>::convertValidator( 00448 const RCP<const ParameterEntryValidator > validator, 00449 XMLObject& xmlObj, 00450 const ValidatortoIDMap& /*validatorIDsMap*/) const 00451 { 00452 RCP<const EnhancedNumberValidator<T> > castedValidator = 00453 rcp_dynamic_cast<const EnhancedNumberValidator<T> >(validator, true); 00454 if (castedValidator->hasMin()) { 00455 xmlObj.addAttribute<T>(getMinAttributeName(), castedValidator->getMin()); 00456 } 00457 if (castedValidator->hasMax()) { 00458 xmlObj.addAttribute<T>(getMaxAttributeName(), castedValidator->getMax()); 00459 } 00460 xmlObj.addAttribute<T>(getStepAttributeName(), castedValidator->getStep()); 00461 xmlObj.addAttribute<short unsigned int>( 00462 getPrecisionAttributeName(), castedValidator->getPrecision()); 00463 } 00464 00465 00480 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidatorXMLConverter : public ValidatorXMLConverter 00481 { 00482 00483 public: 00484 00487 00489 RCP<ParameterEntryValidator> convertXML( 00490 const XMLObject& xmlObj, 00491 const IDtoValidatorMap& validatorIDsMap) const; 00492 00494 void convertValidator( 00495 const RCP<const ParameterEntryValidator> validator, 00496 XMLObject& xmlObj, 00497 const ValidatortoIDMap& validatorIDsMap) const; 00498 00499 #ifdef HAVE_TEUCHOS_DEBUG 00500 00501 RCP<const ParameterEntryValidator> getDummyValidator() const; 00502 #endif 00503 00505 00506 private: 00507 00510 00512 static const std::string& getFileMustExistAttributeName() { 00513 static const std::string fileMustExistAttributeName = "fileMustExist"; 00514 return fileMustExistAttributeName; 00515 } 00516 00518 00519 }; 00520 00521 00536 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidatorXMLConverter : public ValidatorXMLConverter 00537 { 00538 00539 public: 00540 00543 00545 RCP<ParameterEntryValidator> convertXML( 00546 const XMLObject& xmlObj, 00547 const IDtoValidatorMap& validatorIDsMap) const; 00548 00550 void convertValidator( 00551 const RCP<const ParameterEntryValidator> validator, 00552 XMLObject& xmlObj, 00553 const ValidatortoIDMap& validatorIDsMap) const; 00554 00555 #ifdef HAVE_TEUCHOS_DEBUG 00556 00557 RCP<const ParameterEntryValidator> getDummyValidator() const; 00558 #endif 00559 00561 00562 private: 00563 00566 00568 static const std::string& getStringTagName() { 00569 static const std::string stringTagName = "String"; 00570 return stringTagName; 00571 } 00572 00574 static const std::string& getStringValueAttributeName() { 00575 static const std::string stringValueAttributeName = "value"; 00576 return stringValueAttributeName; 00577 } 00578 00580 00581 }; 00582 00583 template<class ValidatorType, class EntryType> 00584 class AbstractArrayValidatorXMLConverter : public ValidatorXMLConverter{ 00585 public: 00586 00589 00591 RCP<ParameterEntryValidator> convertXML( 00592 const XMLObject& xmlObj, 00593 const IDtoValidatorMap& validatorIDsMap) const; 00594 00596 void convertValidator( 00597 const RCP<const ParameterEntryValidator> validator, 00598 XMLObject& xmlObj, 00599 const ValidatortoIDMap& validatorIDsMap) const; 00600 00602 00605 00609 virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > 00610 getConcreteValidator(RCP<ValidatorType> prototypeValidator) const = 0; 00611 00613 }; 00614 00615 00616 template<class ValidatorType, class EntryType> 00617 RCP<ParameterEntryValidator> 00618 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertXML( 00619 const XMLObject& xmlObj, 00620 const IDtoValidatorMap& validatorIDsMap) const 00621 { 00622 RCP<ValidatorType> prototypeValidator; 00623 if(xmlObj.hasAttribute( 00624 ValidatorXMLConverter::getPrototypeIdAttributeName())) 00625 { 00626 IDtoValidatorMap::const_iterator result = 00627 validatorIDsMap.find( 00628 xmlObj.getRequired<ParameterEntryValidator::ValidatorID>( 00629 getPrototypeIdAttributeName())); 00630 if (result != validatorIDsMap.end() ) { 00631 prototypeValidator = 00632 rcp_dynamic_cast<ValidatorType>(result->second, true); 00633 } 00634 else { 00635 TEUCHOS_TEST_FOR_EXCEPTION(true, 00636 MissingValidatorDefinitionException, 00637 "Could not find prototype validator with id: " 00638 << xmlObj.getRequired<ParameterEntryValidator::ValidatorID>( 00639 getPrototypeIdAttributeName()) << std::endl<< std::endl); 00640 } 00641 } 00642 else { 00643 prototypeValidator = rcp_dynamic_cast<ValidatorType>( 00644 ValidatorXMLConverterDB::convertXML( 00645 xmlObj.getChild(0), validatorIDsMap), true); 00646 } 00647 return getConcreteValidator(prototypeValidator); 00648 } 00649 00650 template<class ValidatorType, class EntryType> 00651 void 00652 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertValidator( 00653 const RCP<const ParameterEntryValidator> validator, 00654 XMLObject& xmlObj, 00655 const ValidatortoIDMap& validatorIDsMap) const 00656 { 00657 RCP<const AbstractArrayValidator<ValidatorType, EntryType> > castedValidator = 00658 rcp_dynamic_cast<const AbstractArrayValidator<ValidatorType, EntryType> >( 00659 validator, true); 00660 if(validatorIDsMap.find(castedValidator->getPrototype()) 00661 == validatorIDsMap.end()) 00662 { 00663 xmlObj.addChild(ValidatorXMLConverterDB::convertValidator( 00664 castedValidator->getPrototype(), validatorIDsMap, false)); 00665 } 00666 else{ 00667 ParameterEntryValidator::ValidatorID prototypeID = 00668 validatorIDsMap.find(castedValidator->getPrototype())->second; 00669 00670 xmlObj.addAttribute<ParameterEntryValidator::ValidatorID>( 00671 getPrototypeIdAttributeName(), prototypeID); 00672 } 00673 } 00674 00702 template<class ValidatorType, class EntryType> 00703 class ArrayValidatorXMLConverter : 00704 public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType> 00705 { 00708 00709 virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator( 00710 RCP<ValidatorType> prototypeValidator) const 00711 { 00712 return rcp(new ArrayValidator<ValidatorType, EntryType>(prototypeValidator)); 00713 } 00714 00715 #ifdef HAVE_TEUCHOS_DEBUG 00716 00719 RCP<const ParameterEntryValidator> getDummyValidator() const{ 00720 return DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >:: 00721 getDummyObject(); 00722 } 00724 #endif 00725 }; 00726 00754 template<class ValidatorType, class EntryType> 00755 class TwoDArrayValidatorXMLConverter : 00756 public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType> 00757 { 00760 00761 virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator( 00762 RCP<ValidatorType> prototypeValidator) const 00763 { 00764 return rcp(new TwoDArrayValidator<ValidatorType, EntryType>(prototypeValidator)); 00765 } 00766 00768 00769 #ifdef HAVE_TEUCHOS_DEBUG 00770 00773 RCP<const ParameterEntryValidator> getDummyValidator() const{ 00774 return DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >:: 00775 getDummyObject(); 00776 } 00778 #endif 00779 00780 }; 00781 00782 00783 00784 } // namespace Teuchos 00785 00786 00787 #endif // TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP 00788
1.7.6.1