Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_StandardValidatorXMLConverters.cpp
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 #include "Teuchos_StandardValidatorXMLConverters.hpp"
00043 #include "Teuchos_RCP.hpp"
00044 
00049 namespace Teuchos {
00050 
00051 
00052 RCP<ParameterEntryValidator> AnyNumberValidatorXMLConverter::convertXML(
00053   const XMLObject& xmlObj,
00054   const IDtoValidatorMap& /*validatorIDsMap*/) const
00055 {
00056 
00057   AnyNumberParameterEntryValidator::AcceptedTypes acceptedTypes;
00058   acceptedTypes.allowInt(xmlObj.getRequiredBool(getAllowIntAttributeName()));
00059   acceptedTypes.allowDouble(
00060     xmlObj.getRequiredBool(getAllowDoubleAttributeName()));
00061   acceptedTypes.allowString(
00062     xmlObj.getRequiredBool(getAllowStringAttributeName()));
00063   return anyNumberParameterEntryValidator(
00064       AnyNumberParameterEntryValidator::getPrefferedTypeStringEnum(
00065         xmlObj.getRequired(getPrefferedTypeAttributeName())), 
00066       acceptedTypes);
00067 }
00068 
00069 
00070 void AnyNumberValidatorXMLConverter::convertValidator(
00071   const RCP<const ParameterEntryValidator> validator,
00072   XMLObject& xmlObj,
00073   const ValidatortoIDMap& /*validatorIDsMap*/) const
00074 {
00075   RCP<const AnyNumberParameterEntryValidator> castedValidator = 
00076     rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(validator, true);
00077   xmlObj.addBool(
00078     getAllowIntAttributeName(), castedValidator->isIntAllowed());
00079   xmlObj.addBool(
00080     getAllowDoubleAttributeName(), castedValidator->isDoubleAllowed());
00081   xmlObj.addBool(
00082     getAllowStringAttributeName(), castedValidator->isStringAllowed());
00083   xmlObj.addAttribute(getPrefferedTypeAttributeName(),
00084     castedValidator->getPrefferedTypeString(
00085       castedValidator->getPreferredType()));
00086 }
00087 
00088 #ifdef HAVE_TEUCHOS_DEBUG
00089 RCP<const ParameterEntryValidator> 
00090 AnyNumberValidatorXMLConverter::getDummyValidator() const{
00091   return DummyObjectGetter<AnyNumberParameterEntryValidator>::getDummyObject();
00092 }
00093 #endif
00094 
00095 RCP<ParameterEntryValidator> FileNameValidatorXMLConverter::convertXML(
00096   const XMLObject& xmlObj,
00097   const IDtoValidatorMap& /*validatorIDsMap*/) const
00098 {
00099   return rcp(
00100     new FileNameValidator(
00101       xmlObj.getWithDefault<bool>(
00102         getFileMustExistAttributeName(),
00103         FileNameValidator::mustAlreadyExistDefault()
00104         )
00105       )
00106     );
00107 }
00108 
00109 
00110 void FileNameValidatorXMLConverter::convertValidator(
00111   const RCP<const ParameterEntryValidator> validator,
00112   XMLObject& xmlObj,
00113   const ValidatortoIDMap& /*validatorIDsMap*/) const
00114 {
00115   RCP<const FileNameValidator> castedValidator =
00116     rcp_dynamic_cast<const FileNameValidator>(validator);
00117   xmlObj.addBool(
00118     getFileMustExistAttributeName(), castedValidator->fileMustExist());
00119 }
00120 
00121 
00122 #ifdef HAVE_TEUCHOS_DEBUG
00123 RCP<const ParameterEntryValidator> 
00124 FileNameValidatorXMLConverter::getDummyValidator() const{
00125   return DummyObjectGetter<FileNameValidator>::getDummyObject();
00126 }
00127 #endif
00128 
00129 
00130 RCP<ParameterEntryValidator> StringValidatorXMLConverter::convertXML(
00131   const XMLObject& xmlObj,
00132   const IDtoValidatorMap& /*validatorIDsMap*/) const
00133 {
00134   Array<std::string> strings(xmlObj.numChildren());
00135   if(xmlObj.numChildren()!=0){
00136     for(int i=0; i<xmlObj.numChildren(); ++i){
00137       XMLObject currentChild = xmlObj.getChild(i);
00138       TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(), 
00139         BadTagException,  
00140         "Error converting xmlObject to StringValidator." << std::endl << 
00141         "Unrecognized tag: " << currentChild.getTag());
00142         strings[i] = (currentChild.getRequired(getStringValueAttributeName()));
00143     }
00144   }
00145   return rcp(new StringValidator(strings));
00146 }
00147 
00148 
00149 void StringValidatorXMLConverter::convertValidator(
00150   const RCP<const ParameterEntryValidator> validator,
00151   XMLObject& xmlObj,
00152   const ValidatortoIDMap& /*validatorIDsMap*/) const
00153 {
00154   RCP<const StringValidator> castedValidator = 
00155     rcp_dynamic_cast<const StringValidator>(validator);
00156 
00157   if(!is_null(validator->validStringValues())){
00158     Array<std::string>::const_iterator it = 
00159      validator->validStringValues()->begin();
00160     for(; it != validator->validStringValues()->end(); ++it){
00161       XMLObject stringTag(getStringTagName());
00162       stringTag.addAttribute(getStringValueAttributeName(), *it);
00163       xmlObj.addChild(stringTag);
00164     }
00165   }
00166 }
00167 
00168 
00169 #ifdef HAVE_TEUCHOS_DEBUG
00170 RCP<const ParameterEntryValidator> 
00171 StringValidatorXMLConverter::getDummyValidator() const{
00172   return DummyObjectGetter<StringValidator>::getDummyObject();
00173 }
00174 #endif
00175 
00176 } // namespace Teuchos
00177 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines