Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_StandardConditionXMLConverters.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_StandardConditionXMLConverters.hpp"
00043 #include "Teuchos_ConditionXMLConverterDB.hpp"
00044 #include "Teuchos_XMLConditionExceptions.hpp"
00045 #include "Teuchos_XMLParameterListExceptions.hpp"
00046 
00047 namespace Teuchos{
00048 
00049 RCP<Condition> BoolLogicConditionConverter::convertXML(
00050     const XMLObject& xmlObj,
00051     const XMLParameterListReader::EntryIDsMap& entryIDsMap) const
00052 {
00053   Condition::ConstConditionList conditions;
00054   for(int i = 0; i < xmlObj.numChildren(); ++i){
00055     conditions.push_back(
00056       ConditionXMLConverterDB::convertXML(xmlObj.getChild(i), entryIDsMap));
00057   }
00058   return getSpecificBoolLogicCondition(conditions); 
00059 }
00060 
00061 void BoolLogicConditionConverter::convertCondition(
00062   const RCP<const Condition> condition,
00063   XMLObject& xmlObj,
00064   const XMLParameterListWriter::EntryIDsMap& entryIDsMap) const
00065 {
00066   RCP<const BoolLogicCondition> castedCon = 
00067     rcp_dynamic_cast<const BoolLogicCondition>(condition, true);
00068 
00069   const Condition::ConstConditionList conditions = castedCon->getConditions();
00070   for(
00071     Condition::ConstConditionList::const_iterator it = conditions.begin();
00072     it != conditions.end();
00073     ++it)
00074   {
00075     xmlObj.addChild(ConditionXMLConverterDB::convertCondition(*it, entryIDsMap));    
00076   }
00077 }
00078 
00079 RCP<BoolLogicCondition> 
00080 OrConditionConverter::getSpecificBoolLogicCondition(
00081   Condition::ConstConditionList& conditions) const
00082 {
00083   return rcp( new OrCondition(conditions));    
00084 }
00085 
00086 RCP<BoolLogicCondition> 
00087 AndConditionConverter::getSpecificBoolLogicCondition(
00088   Condition::ConstConditionList& conditions) const
00089 {
00090   return rcp( new AndCondition(conditions));    
00091 }
00092 
00093 RCP<BoolLogicCondition> 
00094 EqualsConditionConverter::getSpecificBoolLogicCondition(
00095   Condition::ConstConditionList& conditions) const
00096 {
00097   return rcp( new EqualsCondition(conditions));    
00098 }
00099 
00100 RCP<Condition> NotConditionConverter::convertXML(
00101     const XMLObject& xmlObj,
00102     const XMLParameterListReader::EntryIDsMap& entryIDsMap) const
00103 {
00104   return rcp(new NotCondition(
00105     ConditionXMLConverterDB::convertXML(xmlObj.getChild(0), entryIDsMap)));
00106 }
00107 
00108 void NotConditionConverter::convertCondition(
00109   const RCP<const Condition> condition,
00110   XMLObject& xmlObj,
00111   const XMLParameterListWriter::EntryIDsMap& entryIDsMap) const
00112 {
00113   RCP<const NotCondition> castedCondition =
00114     rcp_dynamic_cast<const NotCondition>(condition);
00115   xmlObj.addChild(ConditionXMLConverterDB::convertCondition(
00116     castedCondition->getChildCondition(), entryIDsMap));
00117 }
00118 
00119 RCP<Condition> ParameterConditionConverter::convertXML(
00120     const XMLObject& xmlObj,
00121     const XMLParameterListReader::EntryIDsMap& entryIDsMap) const
00122 {
00123   ParameterEntry::ParameterEntryID paramID = 
00124     xmlObj.getRequired<ParameterEntry::ParameterEntryID>(
00125       getParameterEntryIdAttributeName());
00126   TEUCHOS_TEST_FOR_EXCEPTION(
00127     entryIDsMap.find(paramID) == entryIDsMap.end(),
00128     MissingParameterEntryDefinitionException,
00129     "Can't find a parameter entry with id " << paramID << " in the "
00130     "given entryIDsMap!" << std::endl << std::endl);
00131   return getSpecificParameterCondition(
00132     xmlObj, entryIDsMap.find(paramID)->second);
00133 }
00134 
00135 void ParameterConditionConverter::convertCondition(
00136   const RCP<const Condition> condition, 
00137   XMLObject& xmlObj, 
00138   const XMLParameterListWriter::EntryIDsMap& entryIDsMap) const
00139 {
00140   RCP<const ParameterCondition> castedCondition = 
00141     rcp_dynamic_cast<const ParameterCondition>(condition, true);
00142 
00143   TEUCHOS_TEST_FOR_EXCEPTION(
00144     entryIDsMap.find(castedCondition->getParameter()) == entryIDsMap.end(),
00145     MissingParameterEntryDefinitionException,
00146     "Couldn't find an id for the parameter in the given entryIDsMap!" <<
00147     std::endl << std::endl);
00148 
00149   xmlObj.addAttribute(
00150     getParameterEntryIdAttributeName(),
00151     entryIDsMap.find(castedCondition->getParameter())->second);
00152   
00153   addSpecificXMLTraits(castedCondition, xmlObj);
00154 }
00155 
00156 RCP<ParameterCondition> 
00157 StringConditionConverter::getSpecificParameterCondition(
00158   const XMLObject& xmlObj,
00159   RCP<ParameterEntry> parameterEntry) const
00160 {
00161   StringCondition::ValueList values;
00162   int result = xmlObj.findFirstChild(getValuesTagName());
00163   TEUCHOS_TEST_FOR_EXCEPTION(result == -1,
00164     MissingValuesTagException,
00165     "A StringCondtion must have a tag with the name " <<
00166     getValuesTagName() << " as one of it's children!");
00167 
00168   XMLObject valuesTag = xmlObj.getChild(result);
00169   for(int i=0; i< valuesTag.numChildren(); ++i){
00170     XMLObject child = valuesTag.getChild(i);
00171     if(child.getTag() == getStringTagName()){
00172       values.append(child.getRequired(getStringValueAttributeName()));
00173     }
00174   }
00175   return rcp(new StringCondition(parameterEntry, values));
00176 }
00177 
00178 void StringConditionConverter::addSpecificXMLTraits(
00179   RCP<const ParameterCondition> condition, XMLObject& xmlObj) const             
00180 {
00181   RCP<const StringCondition> castedCon =
00182     rcp_dynamic_cast<const StringCondition>(condition, true);
00183   XMLObject valueTag(getValuesTagName());
00184   for(
00185     StringCondition::ValueList::const_iterator it =
00186       castedCon->getValueList().begin();
00187     it != castedCon->getValueList().end();
00188     ++it)
00189   {
00190     XMLObject stringTag(getStringTagName());
00191     stringTag.addAttribute(getStringValueAttributeName(), *it);
00192     valueTag.addChild(stringTag);
00193   }
00194   xmlObj.addChild(valueTag);
00195 }
00196  
00197 RCP<ParameterCondition> 
00198 BoolConditionConverter::getSpecificParameterCondition(
00199   const XMLObject& xmlObj,
00200   RCP<ParameterEntry> parameterEntry) const
00201 {
00202   return rcp(new BoolCondition(parameterEntry));
00203 }
00204 
00205 void BoolConditionConverter::addSpecificXMLTraits(
00206   RCP<const ParameterCondition> condition, XMLObject& xmlObj) const             
00207 {}
00208  
00209 
00210 } //namespace Teuchos
00211 
00212 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines