Teuchos - Trilinos Tools Package Version of the Day
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=="short")
00100             {
00101               entry.setValue<short>(child.getRequiredInt("value"), 
00102                                   isDefault);
00103               if (isUsed) {
00104                 short tmp = entry.getValue<short>(&tmp);
00105               }
00106             }
00107           else if (type=="int")
00108             {
00109               entry.setValue<int>(child.getRequiredInt("value"), 
00110                                   isDefault);
00111               if (isUsed) {
00112                 int tmp = entry.getValue<int>(&tmp);
00113               }
00114             }
00115           else if (type=="bool")
00116             {
00117               entry.setValue<bool>(child.getRequiredBool("value"), 
00118                                    isDefault);
00119               if (isUsed) {
00120                 bool tmp = entry.getValue<bool>(&tmp);
00121               }
00122             }
00123           else if (type=="string")
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           else if (type=="Array int")
00132             {
00133               entry.setValue<Array<int> >(Teuchos::fromStringToArray<int>(child.getRequired("value")),
00134                                       isDefault);
00135               if (isUsed) {
00136                 Array<int> tmp = entry.getValue<Array<int> >(&tmp);
00137               }
00138             }
00139           else if (type=="Array short")
00140             {
00141               entry.setValue<Array<short> >(Teuchos::fromStringToArray<short>(child.getRequired("value")),
00142                                       isDefault);
00143               if (isUsed) {
00144                 Array<short> tmp = entry.getValue<Array<short> >(&tmp);
00145               }
00146             }
00147           else if (type=="Array float")
00148             {
00149               entry.setValue<Array<float> >(Teuchos::fromStringToArray<float>(child.getRequired("value")),
00150                                       isDefault);
00151               if (isUsed) {
00152                 Array<float> tmp = entry.getValue<Array<float> >(&tmp);
00153               }
00154             }
00155           else if (type=="Array double")
00156             {
00157               entry.setValue<Array<double> >(Teuchos::fromStringToArray<double>(child.getRequired("value")),
00158                                       isDefault);
00159               if (isUsed) {
00160                 Array<double> tmp = entry.getValue<Array<double> >(&tmp);
00161               }
00162             }
00163           else if (type=="Array string")
00164             {
00165               entry.setValue<Array<std::string> >(Teuchos::fromStringToArray<std::string>(child.getRequired("value")),
00166                                       isDefault);
00167               if (isUsed) {
00168                 Array<std::string> tmp = entry.getValue<Array<std::string> >(&tmp);
00169               }
00170             }
00171           else 
00172             {
00173               entry.setValue<std::string>(child.getRequired("value"), 
00174                                    isDefault);
00175               if (isUsed) {
00176                 std::string tmp = entry.getValue<std::string>(&tmp);
00177               }
00178             }
00179           rtn.setEntry(name, entry);
00180         }
00181                          
00182     }
00183 
00184   return rtn;
00185                      
00186 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines