Teuchos_XMLObject.cpp

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 // 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 Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_XMLObject.hpp"
00030 #include "Teuchos_StrUtils.hpp"
00031 
00032 using namespace Teuchos;
00033 
00034 
00035 
00036 XMLObject::XMLObject(const std::string& tag)
00037   : ptr_(rcp(new XMLObjectImplem(tag)))
00038 {}
00039 
00040 XMLObject::XMLObject(XMLObjectImplem* ptr)
00041   : ptr_(rcp(ptr))
00042 {}
00043 
00044 XMLObject XMLObject::deepCopy() const
00045 {
00046   return XMLObject(ptr_->deepCopy());
00047 }
00048 
00049 const std::string& XMLObject::getRequired(const std::string& name) const 
00050 {
00051   TEST_FOR_EXCEPTION(!hasAttribute(name), std::runtime_error,
00052                      "XMLObject::getRequired: key " 
00053                      << name << " not found");
00054   return getAttribute(name);
00055 }
00056 
00057 std::string XMLObject::getWithDefault(const std::string& name, 
00058                                  const std::string& defaultValue) const
00059 {
00060   if (hasAttribute(name)) return getRequired(name);
00061   else return defaultValue;
00062 }
00063 
00064 bool XMLObject::getRequiredBool(const std::string& name) const
00065 {
00066   if (hasAttribute(name))
00067     {
00068       std::string val = StrUtils::allCaps(getRequired(name));
00069       if (val=="TRUE" || val=="YES" || val=="1")
00070         {
00071           return true;
00072         }
00073       else if (val=="FALSE" || val=="NO" || val=="0")
00074         {
00075           return false;
00076         }
00077       else
00078         {
00079           TEST_FOR_EXCEPTION(true, std::runtime_error, 
00080                              "XMLObject::getRequiredBool value [" << val 
00081                              << "] should have been {TRUE|FALSE|YES|NO|0|1}");
00082         }
00083     }
00084   return false; // -Wall
00085 }
00086 
00087 
00088 void XMLObject::checkTag(const std::string& expected) const 
00089 {
00090   TEST_FOR_EXCEPTION(getTag() != expected, std::runtime_error,
00091                      "XMLObject::checkTag error: expected <"
00092                      << expected << ">, found <" 
00093                      << getTag() << ">");
00094 }
00095 
00096 
00097 

Generated on Tue Oct 20 12:45:26 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7