Teuchos_XMLParameterListReader.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 // 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_XMLParameterListReader.hpp" 
00030 #include "Teuchos_TestForException.hpp"
00031 #include "Teuchos_StrUtils.hpp"
00032 
00033 using namespace Teuchos;
00034 
00035 XMLParameterListReader::XMLParameterListReader()
00036 {;}
00037 
00038 ParameterList XMLParameterListReader::toParameterList(const XMLObject& xml) const
00039 {
00040   TEST_FOR_EXCEPTION(xml.getTag() != "ParameterList", runtime_error,
00041                      "XMLParameterListReader expected tag ParameterList, found "
00042                      << xml.getTag());
00043 
00044   ParameterList rtn;
00045 
00046   for (int i=0; i<xml.numChildren(); i++)
00047     {
00048       XMLObject child = xml.getChild(i);
00049 
00050       TEST_FOR_EXCEPTION( (child.getTag() != "ParameterList" 
00051                            && child.getTag() != "Parameter"), 
00052                          runtime_error,
00053                          "XMLParameterListReader expected tag "
00054                          "ParameterList or Parameter, found "
00055                          << child.getTag());
00056 
00057       if (child.getTag()=="ParameterList")
00058         {
00059           const string& name = child.getRequired("name");
00060 
00061           ParameterList sublist = toParameterList(child);
00062 
00063           rtn.set(name, sublist);
00064         }
00065       else
00066         {
00067           const string& name = child.getRequired("name");
00068           const string& type = child.getRequired("type");
00069           
00070           bool isDefault = false;
00071           if (child.hasAttribute("isDefault")) 
00072             {
00073               isDefault = child.getRequiredBool("isDefault");
00074             }
00075           ParameterEntry entry;
00076           if (type=="double" || type=="float")
00077             {
00078               entry.setValue<double>(child.getRequiredDouble("value"), 
00079                                      isDefault);
00080             }
00081           else if (type=="int")
00082             {
00083               entry.setValue<int>(child.getRequiredInt("value"), 
00084                                   isDefault);
00085             }
00086           else if (type=="bool")
00087             {
00088               entry.setValue<bool>(child.getRequiredBool("value"), 
00089                                    isDefault);
00090             }
00091           else if (type=="string")
00092             {
00093               entry.setValue<string>(child.getRequired("value"), 
00094                                    isDefault);
00095             }
00096           else 
00097             {
00098               entry.setValue<string>(child.getRequired("value"), 
00099                                    isDefault);
00100             }
00101           rtn.setEntry(name, entry);
00102         }
00103                          
00104     }
00105 
00106   return rtn;
00107                      
00108 }

Generated on Thu Sep 18 12:41:18 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1