Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_XMLParameterListWriter.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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "Teuchos_XMLParameterListWriter.hpp"
00043 #include "Teuchos_ParameterEntryXMLConverterDB.hpp"
00044 #include "Teuchos_ValidatorXMLConverterDB.hpp"
00045 #include "Teuchos_XMLParameterListExceptions.hpp"
00046 #include "Teuchos_DependencyXMLConverterDB.hpp"
00047 
00048 
00049 namespace Teuchos{
00050 
00051 
00052 XMLParameterListWriter::XMLParameterListWriter()
00053 {;}
00054 
00055 
00056 XMLObject 
00057 XMLParameterListWriter::toXML(
00058   const ParameterList& p, 
00059   RCP<const DependencySheet> depSheet) const
00060 {
00061   EntryIDsMap entryIDsMap;
00062   ValidatortoIDMap validatorIDsMap;
00063   ParameterEntry::ParameterEntryID peIDCounter = 0;
00064 
00065   //We build an initial map full of validators that are located in the 
00066   //parameter list. That way we can convert the parameter entries.
00067   buildInitialValidatorMap(p, validatorIDsMap);
00068 
00069   XMLObject toReturn = 
00070     convertParameterList(p, peIDCounter, entryIDsMap, validatorIDsMap);
00071   toReturn.addAttribute(getNameAttributeName(), p.name());
00072 
00073   if(!depSheet.is_null()){
00074     XMLObject deps = 
00075       convertDependencies(depSheet, entryIDsMap, validatorIDsMap);
00076     toReturn.addChild(deps);
00077   }
00078 
00079   //Validators must be done after depencneies because dependencies might add
00080   //entries to the validator map. KLN 09/20/2010
00081   XMLObject validators = convertValidators(p, validatorIDsMap);
00082   toReturn.addChild(validators);
00083 
00084   return toReturn;
00085 }
00086 
00087 void XMLParameterListWriter::buildInitialValidatorMap(
00088   const ParameterList& p,
00089   ValidatortoIDMap& validatorIDsMap) const
00090 {
00091   for (ParameterList::ConstIterator i=p.begin(); i!=p.end(); ++i) {
00092     const ParameterEntry& entry = p.entry(i);
00093     if(entry.isList()){
00094       buildInitialValidatorMap(
00095         getValue<ParameterList>(entry),
00096         validatorIDsMap);
00097     }
00098     else if(nonnull(entry.validator())){
00099       validatorIDsMap.insert(entry.validator());
00100     }
00101   }
00102 }
00103 
00104 
00105 XMLObject XMLParameterListWriter::convertValidators(
00106   const ParameterList& p, ValidatortoIDMap& validatorIDsMap) const
00107 {
00108   XMLObject validators(getValidatorsTagName());
00109   for(
00110     ValidatortoIDMap::const_iterator it = validatorIDsMap.begin();
00111     it != validatorIDsMap.end();
00112     ++it)
00113   {
00114     validators.addChild(
00115       ValidatorXMLConverterDB::convertValidator(it->first, validatorIDsMap));
00116   }
00117   return validators;
00118 }
00119 
00120 
00121 XMLObject XMLParameterListWriter::convertParameterList(
00122   const ParameterList& p,
00123   ParameterEntry::ParameterEntryID& idCounter,
00124   EntryIDsMap& entryIDsMap,
00125   const ValidatortoIDMap& validatorIDsMap) const
00126 {
00127   XMLObject rtn(getParameterListTagName());
00128   
00129   for (ParameterList::ConstIterator i=p.begin(); i!=p.end(); ++i){
00130     RCP<const ParameterEntry> entry = p.getEntryRCP(i->first);
00131     if(entry->isList()){
00132       XMLObject newPL = convertParameterList(
00133         getValue<ParameterList>(entry), 
00134         idCounter, 
00135         entryIDsMap,
00136         validatorIDsMap);
00137       newPL.addAttribute(
00138         getNameAttributeName(), p.name(i));
00139       newPL.addAttribute(
00140         ParameterEntryXMLConverter::getIdAttributeName(), idCounter);
00141       entryIDsMap.insert(EntryIDsMap::value_type(entry, idCounter));
00142       rtn.addChild(newPL);
00143       ++idCounter;
00144     }
00145     else{
00146       rtn.addChild(ParameterEntryXMLConverterDB::convertEntry(
00147         entry, p.name(i), idCounter, validatorIDsMap));
00148       entryIDsMap.insert(EntryIDsMap::value_type(entry, idCounter));
00149       ++idCounter;
00150     }
00151   }
00152   return rtn;
00153 }
00154 
00155 XMLObject 
00156 XMLParameterListWriter::convertDependencies(
00157   RCP<const DependencySheet> depSheet,
00158   const EntryIDsMap& entryIDsMap,
00159   ValidatortoIDMap& validatorIDsMap) const
00160 {
00161   XMLObject toReturn(getDependenciesTagName());
00162   toReturn.addAttribute(
00163     DependencySheet::getNameAttributeName(), 
00164     depSheet->getName()
00165   );
00166 
00167   for(
00168     DependencySheet::DepSet::const_iterator it = depSheet->depBegin();
00169     it != depSheet->depEnd();
00170     ++it)
00171   {
00172     toReturn.addChild(DependencyXMLConverterDB::convertDependency(
00173       *it, entryIDsMap, validatorIDsMap));
00174   }
00175   return toReturn;
00176 }
00177 
00178 
00179 } // namespace Teuchos
00180 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines