Teuchos_XMLParameterListReader.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_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", std::runtime_error,
00041                      "XMLParameterListReader expected tag ParameterList, found "
00042                      << xml.getTag());
00043 
00044   ParameterList rtn;
00045   
00046   if (xml.hasAttribute("name"))
00047     {
00048       rtn.setName(xml.getAttribute("name"));
00049     }
00050 
00051   for (int i=0; i<xml.numChildren(); i++)
00052     {
00053       XMLObject child = xml.getChild(i);
00054 
00055       TEST_FOR_EXCEPTION( (child.getTag() != "ParameterList" 
00056                            && child.getTag() != "Parameter"), 
00057                          std::runtime_error,
00058                          "XMLParameterListReader expected tag "
00059                          "ParameterList or Parameter, found "
00060                          << child.getTag());
00061 
00062       if (child.getTag()=="ParameterList")
00063         {
00064           const std::string& name = child.getRequired("name");
00065 
00066           ParameterList sublist = toParameterList(child);
00067           sublist.setName(name);
00068 
00069           rtn.set(name, sublist);
00070         }
00071       else
00072         {
00073           const std::string& name = child.getRequired("name");
00074           const std::string& type = child.getRequired("type");
00075           
00076           bool isDefault = false;
00077           bool isUsed = false;
00078           if (child.hasAttribute("isDefault")) 
00079           {
00080             isDefault = child.getRequiredBool("isDefault");
00081           }
00082           if (child.hasAttribute("isUsed")) 
00083           {
00084             isUsed = child.getRequiredBool("isUsed");
00085           }
00086 
00087           // setValue assigns isUsed to false
00088           // getValue assigns isUsed to true
00089 
00090           ParameterEntry entry;
00091           if (type=="double" || type=="float")
00092             {
00093               entry.setValue<double>(child.getRequiredDouble("value"), 
00094                                      isDefault);
00095               if (isUsed) {
00096                 double tmp = entry.getValue<double>(&tmp);
00097               }
00098             }
00099           else if (type=="int")
00100             {
00101               entry.setValue<int>(child.getRequiredInt("value"), 
00102                                   isDefault);
00103               if (isUsed) {
00104                 int tmp = entry.getValue<int>(&tmp);
00105               }
00106             }
00107           else if (type=="bool")
00108             {
00109               entry.setValue<bool>(child.getRequiredBool("value"), 
00110                                    isDefault);
00111               if (isUsed) {
00112                 bool tmp = entry.getValue<bool>(&tmp);
00113               }
00114             }
00115           else if (type=="string")
00116             {
00117               entry.setValue<std::string>(child.getRequired("value"), 
00118                                      isDefault);
00119               if (isUsed) {
00120                 std::string tmp = entry.getValue<std::string>(&tmp);
00121               }
00122             }
00123           else 
00124             {
00125               entry.setValue<std::string>(child.getRequired("value"), 
00126                                    isDefault);
00127               if (isUsed) {
00128                 std::string tmp = entry.getValue<std::string>(&tmp);
00129               }
00130             }
00131           rtn.setEntry(name, entry);
00132         }
00133                          
00134     }
00135 
00136   return rtn;
00137                      
00138 }

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