Optika GUI Toolik Version of the Day
Optika_SpecificParameterEntryValidators.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //         Optika: A Tool For Developing Parameter Obtaining GUIs
00005 //                Copyright (2009) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, with Sandia Corporation, the 
00008 // U.S. Government retains certain rights in this software.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Kurtis Nusbaum (klnusbaum@gmail.com) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 #include "Optika_SpecificParameterEntryValidators.hpp"
00029 
00030 namespace Optika{
00031 
00032 FileNameValidator::FileNameValidator(bool mustAlreadyExist):ParameterEntryValidator(),mustAlreadyExist(mustAlreadyExist){}
00033 
00034 bool FileNameValidator::fileMustExist() const{
00035   return mustAlreadyExist;
00036 }
00037 
00038 bool FileNameValidator::setFileMustExist(bool shouldFileExist){
00039   this->mustAlreadyExist = shouldFileExist;
00040   return mustAlreadyExist;
00041 }
00042 
00043 Teuchos::RCP<const Teuchos::Array<std::string> > FileNameValidator::validStringValues() const{
00044   return Teuchos::null;
00045 }
00046 
00047 void FileNameValidator::validate(Teuchos::ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const{
00048   Teuchos::any anyValue = entry.getAny(true);
00049   if(!(anyValue.type() == typeid(std::string) )){
00050     const std::string &entryName = entry.getAny(false).typeName();
00051     std::stringstream oss;
00052     std::string msg;
00053     oss << "Aww shoot! Sorry bud, but it looks like the \"" << paramName << "\"" <<
00054     " parameter in the \"" << sublistName << "\" sublist didn't quite work out.\n" <<
00055     "No need to fret though. I'm sure it's just a small mistake. Maybe the information below "<<
00056     "can help you figure out what went wrong.\n\n"
00057     "Error: The value that you entered was the wrong type.\n" <<
00058     "Parameter: " << paramName << "\n" << 
00059     "Type specified: " << entryName << "\n" <<
00060     "Type accepted: " << typeid(std::string).name() << "\n";
00061     msg = oss.str();
00062     throw Teuchos::Exceptions::InvalidParameterType(msg);
00063   }
00064   if(mustAlreadyExist){
00065     std::string fileName = Teuchos::getValue<std::string>(entry);
00066     struct stat fileInfo;
00067     int intStat= stat(fileName.c_str(),&fileInfo);
00068     if(intStat !=0){
00069       std::stringstream oss;
00070       std::string msg;
00071       oss << "Aww shoot! Sorry bud, but it looks like the \"" << paramName << "\"" <<
00072       " parameter in the \"" << sublistName << "\" sublist didn't quite work out.\n" <<
00073       "No need to fret though. I'm sure it's just a small mistake. Maybe the information below "<<
00074       "can help you figure out what went wrong.\n\n"
00075       "Error: The file must already exists. The value you entered does not corresspond to an existing file name.\n" <<
00076       "Parameter: " << paramName << "\n" << 
00077       "File name specified: " << fileName << "\n";
00078       msg = oss.str();
00079       throw Teuchos::Exceptions::InvalidParameterValue(msg);
00080     }
00081   }
00082 }
00083 
00084 void FileNameValidator::printDoc(std::string const &docString, std::ostream &out) const{
00085   Teuchos::StrUtils::printLines(out,"# ",docString);
00086   out << "#  Validator Used: \n";
00087   out << "# FileName Validator\n";
00088 }
00089 
00090 StringValidator::StringValidator(ValueList validStrings):
00091   ParameterEntryValidator(),
00092   validStrings(validStrings){}
00093 
00094 const Teuchos::Array<std::string> StringValidator::setValidStrings(ValueList validStrings){
00095   this->validStrings = validStrings;
00096   return this->validStrings;
00097 }
00098 
00099 Teuchos::RCP<const Teuchos::Array<std::string> > StringValidator::validStringValues() const{
00100   Teuchos::RCP<const Teuchos::Array<std::string> > toReturn = Teuchos::RCP<const Teuchos::Array<std::string> >(new Teuchos::Array<std::string>(validStrings));
00101   return toReturn;
00102 }
00103 
00104 void StringValidator::validate(Teuchos::ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const{
00105   Teuchos::any anyValue = entry.getAny(true);
00106   if(!(anyValue.type() == typeid(std::string) )){
00107     const std::string &entryName = entry.getAny(false).typeName();
00108     std::stringstream oss;
00109     std::string msg;
00110     oss << "Aww shoot! Sorry bud, but it looks like the \"" << paramName << "\"" <<
00111     " parameter in the \"" << sublistName << "\" sublist didn't quite work out.\n" <<
00112     "No need to fret though. I'm sure it's just a small mistake. Maybe the information below "<<
00113     "can help you figure out what went wrong.\n\n"
00114     "Error: The value that you entered was the wrong type." <<
00115     "Parameter: " << paramName << "\n" << 
00116     "Type specified: " << entryName << "\n" <<
00117     "Type accepted: " << typeid(std::string).name() << "\n";
00118     msg = oss.str();
00119     throw Teuchos::Exceptions::InvalidParameterType(msg);
00120   }
00121   else{
00122     Teuchos::Array<std::string>::const_iterator it = std::find(validStrings.begin(), validStrings.end(), Teuchos::getValue<std::string>(entry));
00123     if(it == validStrings.end()){
00124       std::stringstream oss;
00125       std::string msg;
00126       oss << "Aww shoot! Sorry bud, but it looks like the \"" << paramName << "\"" <<
00127       " parameter in the \"" << sublistName << "\" sublist didn't quite work out.\n" <<
00128       "No need to fret though. I'm sure it's just a small mistake. Maybe the information below "<<
00129       "can help you figure out what went wrong.\n\n"
00130       "Error: The value that was entered doesn't fall with in " <<
00131       "the range set by the validator." <<
00132       "Parameter: " << paramName << "\n" <<
00133       "Acceptable Values: " << validStrings << "\n" <<
00134       "Value entered: " << Teuchos::getValue<std::string>(entry) << "\n";
00135       msg = oss.str();
00136       throw Teuchos::Exceptions::InvalidParameterValue(msg);
00137     }
00138 
00139   }
00140 }
00141 
00142 void StringValidator::printDoc(std::string const &docString, std::ostream &out) const{
00143   Teuchos::StrUtils::printLines(out,"# ",docString);
00144   out << "#  Validator Used: \n";
00145   out << "# FileName Validator\n";
00146 }
00147 
00148 }
00149 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends