Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_StandardDependencyXMLConverters.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_StandardDependencyXMLConverters.hpp"
00043 #include "Teuchos_ConditionXMLConverterDB.hpp"
00044 
00045 
00046 namespace Teuchos{
00047 
00048 
00049 RCP<Dependency> VisualDependencyXMLConverter::convertXML(
00050   const XMLObject& xmlObj, 
00051   const Dependency::ConstParameterEntryList dependees,
00052   const Dependency::ParameterEntryList dependents,
00053   const XMLParameterListReader::EntryIDsMap& entryIDsMap,
00054   const IDtoValidatorMap& /*validatorIDsMap*/) const
00055 {
00056   bool showIf = xmlObj.getWithDefault(
00057     getShowIfAttributeName(), VisualDependency::getShowIfDefaultValue());
00058   return convertSpecialVisualAttributes(
00059     xmlObj,
00060     dependees,
00061     dependents,
00062     showIf,
00063     entryIDsMap);
00064 }
00065 
00066 void VisualDependencyXMLConverter::convertDependency(
00067   const RCP<const Dependency> dependency, 
00068   XMLObject& xmlObj,
00069   const XMLParameterListWriter::EntryIDsMap& entryIDsMap,
00070   ValidatortoIDMap& /*validatorIDsMap*/) const
00071 {
00072   RCP<const VisualDependency> castedDep = 
00073     rcp_dynamic_cast<const VisualDependency>(dependency, true);
00074 
00075   xmlObj.addBool(getShowIfAttributeName(), castedDep->getShowIf());
00076   convertSpecialVisualAttributes(castedDep, xmlObj, entryIDsMap);
00077 }
00078 
00079 RCP<Dependency> ValidatorDependencyXMLConverter::convertXML(
00080     const XMLObject& xmlObj, 
00081     const Dependency::ConstParameterEntryList dependees,
00082     const Dependency::ParameterEntryList dependents,
00083     const XMLParameterListReader::EntryIDsMap& /*entryIDsMap*/,
00084     const IDtoValidatorMap& validatorIDsMap) const
00085 {
00086   TEST_FOR_EXCEPTION(dependees.size() > 1,
00087     TooManyDependeesException,
00088     "A Validator Dependency can only have 1 dependee!" << 
00089     std::endl << std::endl);
00090   return convertSpecialValidatorAttributes(
00091     xmlObj, *(dependees.begin()), dependents, validatorIDsMap);
00092 }
00093 
00094 void ValidatorDependencyXMLConverter::convertDependency(
00095   const RCP<const Dependency> dependency, 
00096     XMLObject& xmlObj,
00097     const XMLParameterListWriter::EntryIDsMap& /*entryIDsMap*/,
00098     ValidatortoIDMap& validatorIDsMap) const
00099 {
00100   RCP<const ValidatorDependency> castedDep = 
00101     rcp_dynamic_cast<const ValidatorDependency>(dependency, true);
00102   convertSpecialValidatorAttributes(castedDep, xmlObj, validatorIDsMap);
00103 }
00104   
00105  
00106 void StringVisualDependencyXMLConverter::convertSpecialVisualAttributes(
00107     RCP<const VisualDependency> dependency,
00108     XMLObject& xmlObj,
00109     const XMLParameterListWriter::EntryIDsMap& /*entryIDsMap*/) const
00110 {
00111   RCP<const StringVisualDependency> castedDependency = 
00112     rcp_dynamic_cast<const StringVisualDependency>(dependency, true);
00113   StringVisualDependency::ValueList valueList = castedDependency->getValues();
00114   XMLObject valuesTag(getStringValuesTagName());
00115   for(
00116     StringVisualDependency::ValueList::const_iterator it = valueList.begin(); 
00117     it != valueList.end(); 
00118     ++it)
00119   {
00120     XMLObject stringValue(getStringTagName());
00121     stringValue.addAttribute(getValueAttributeName(), *it);
00122     valuesTag.addChild(stringValue);
00123   }
00124   xmlObj.addChild(valuesTag);
00125 }
00126   
00127 RCP<VisualDependency> 
00128 StringVisualDependencyXMLConverter::convertSpecialVisualAttributes(
00129   const XMLObject& xmlObj,
00130   const Dependency::ConstParameterEntryList dependees,
00131   const Dependency::ParameterEntryList dependents,
00132   bool showIf,
00133   const XMLParameterListReader::EntryIDsMap& /*entryIDsMap*/) const
00134 {
00135   TEST_FOR_EXCEPTION(dependees.size() > 1,
00136     TooManyDependeesException,
00137     "A StringVisualDependency can only have 1 dependee!" << 
00138     std::endl << std::endl);
00139 
00140   StringVisualDependency::ValueList valueList;
00141   int valuesTagIndex = xmlObj.findFirstChild(getStringValuesTagName());
00142   
00143   TEST_FOR_EXCEPTION(valuesTagIndex < 0,
00144     ValuesTagMissingException,
00145     "Couldn't find " << getStringValuesTagName() << " tag for a " <<
00146     "StringVisualDependency!" << std::endl <<std::endl);
00147 
00148   XMLObject valuesTag = xmlObj.getChild(valuesTagIndex);
00149 
00150   for(int i=0; i<valuesTag.numChildren(); ++i){
00151     XMLObject child = valuesTag.getChild(i);
00152     valueList.push_back(child.getRequired(getValueAttributeName()));
00153   }
00154 
00155   return rcp(
00156     new StringVisualDependency(
00157       *(dependees.begin()), 
00158       dependents, 
00159       valueList, 
00160       showIf));
00161 }
00162   
00163 void BoolVisualDependencyXMLConverter::convertSpecialVisualAttributes(
00164   RCP<const VisualDependency> dependency,
00165   XMLObject& xmlObj,
00166   const XMLParameterListWriter::EntryIDsMap& /*entryIDsMap*/) const
00167 {}
00168   
00169 RCP<VisualDependency> 
00170 BoolVisualDependencyXMLConverter::convertSpecialVisualAttributes(
00171   const XMLObject& xmlObj,
00172   const Dependency::ConstParameterEntryList dependees,
00173   const Dependency::ParameterEntryList dependents,
00174   bool showIf,
00175   const XMLParameterListReader::EntryIDsMap& /*entryIDsMap*/) const
00176 {
00177   TEST_FOR_EXCEPTION(dependees.size() > 1,
00178     TooManyDependeesException,
00179     "A BoolVisualDependency can only have 1 dependee!" <<
00180     std::endl << std::endl);
00181   return rcp(new BoolVisualDependency(
00182     *(dependees.begin()), dependents, showIf));
00183 }
00184 
00185 void ConditionVisualDependencyXMLConverter::convertSpecialVisualAttributes(
00186   RCP<const VisualDependency> dependency,
00187   XMLObject& xmlObj,
00188   const XMLParameterListWriter::EntryIDsMap& entryIDsMap) const
00189 {
00190   RCP<const ConditionVisualDependency> castedDependency = 
00191     rcp_dynamic_cast<const ConditionVisualDependency>(dependency, true);
00192   xmlObj.addChild(
00193     ConditionXMLConverterDB::convertCondition(
00194       castedDependency->getCondition(), entryIDsMap));
00195 }
00196   
00197 RCP<VisualDependency> 
00198 ConditionVisualDependencyXMLConverter::convertSpecialVisualAttributes(
00199   const XMLObject& xmlObj,
00200   const Dependency::ConstParameterEntryList dependees,
00201   const Dependency::ParameterEntryList dependents,
00202   bool showIf,
00203   const XMLParameterListReader::EntryIDsMap& entryIDsMap) const
00204 {
00205   int conditionIndex = xmlObj.findFirstChild(Condition::getXMLTagName());
00206   TEST_FOR_EXCEPTION(conditionIndex < 0,
00207     MissingConditionTagException,
00208     "ConditionVisualDependencies must have a Condition tag!"
00209   );
00210   XMLObject conditionObj = xmlObj.getChild(conditionIndex);
00211   Teuchos::RCP<Condition> condition = 
00212     ConditionXMLConverterDB::convertXML(conditionObj, entryIDsMap);
00213   return rcp(new ConditionVisualDependency(condition, dependents, showIf));
00214 }
00215  
00216 void
00217 StringValidatorDependencyXMLConverter::convertSpecialValidatorAttributes(
00218   RCP<const ValidatorDependency> dependency,
00219   XMLObject& xmlObj,
00220   ValidatortoIDMap& validatorIDsMap) const
00221 {
00222   RCP<const StringValidatorDependency> castedDependency = 
00223     rcp_dynamic_cast<const StringValidatorDependency>(dependency, true);  
00224   XMLObject valueMapTag(getValuesAndValidatorsTag()); 
00225   const StringValidatorDependency::ValueToValidatorMap valuesAndValidators = 
00226     castedDependency->getValuesAndValidators();
00227   for(
00228     StringValidatorDependency::ValueToValidatorMap::const_iterator it =
00229       valuesAndValidators.begin();
00230     it != valuesAndValidators.end();
00231     ++it)
00232   {
00233     XMLObject pairTag(getPairTag());
00234     pairTag.addAttribute(getValueAttributeName(), it->first);
00235     if(validatorIDsMap.find(it->second) == validatorIDsMap.end()){
00236       validatorIDsMap.insert(it->second);
00237     }
00238     pairTag.addAttribute(getValidatorIdAttributeName(), 
00239       validatorIDsMap.find(it->second)->second);
00240     valueMapTag.addChild(pairTag);
00241   }  
00242   xmlObj.addChild(valueMapTag);
00243   RCP<const ParameterEntryValidator> defaultVali = 
00244     castedDependency->getDefaultValidator();
00245   if(nonnull(defaultVali)){
00246     if(validatorIDsMap.find(defaultVali) == validatorIDsMap.end()){
00247       validatorIDsMap.insert(defaultVali);
00248     }
00249     xmlObj.addAttribute(
00250       getDefaultValidatorIdAttributeName(),
00251       validatorIDsMap.find(defaultVali)->second);
00252   }
00253 }
00254 
00255 RCP<ValidatorDependency> 
00256 StringValidatorDependencyXMLConverter::convertSpecialValidatorAttributes(
00257     const XMLObject& xmlObj,
00258     RCP<const ParameterEntry> dependee,
00259     const Dependency::ParameterEntryList dependents,
00260     const IDtoValidatorMap& validatorIDsMap) const
00261 {
00262   StringValidatorDependency::ValueToValidatorMap valueValidatorMap;
00263   int valuesAndValidatorIndex = 
00264     xmlObj.findFirstChild(getValuesAndValidatorsTag()); 
00265     
00266   TEST_FOR_EXCEPTION(valuesAndValidatorIndex < 0,
00267     MissingValuesAndValidatorsTagException,
00268     "Error: All StringValidatorDependencies must have a " << 
00269     getValuesAndValidatorsTag() << "tag!" << std::endl << std::endl);
00270 
00271   XMLObject valuesAndValidatorTag = xmlObj.getChild(valuesAndValidatorIndex);
00272   for(int i=0; i < valuesAndValidatorTag.numChildren(); ++i){
00273     XMLObject child = valuesAndValidatorTag.getChild(i);
00274     std::string value = child.getRequired(getValueAttributeName());
00275     ParameterEntryValidator::ValidatorID valiID = 
00276       child.getRequired<ParameterEntryValidator::ValidatorID>(
00277         getValidatorIdAttributeName());
00278     TEST_FOR_EXCEPTION(validatorIDsMap.find(valiID) == validatorIDsMap.end(),
00279       MissingValidatorException,
00280       "Could not find a validator corresponding to the ID " << valiID <<
00281       " in the given validatorIDsMap!" << std::endl << std::endl);
00282     RCP<ParameterEntryValidator> validator = 
00283       validatorIDsMap.find(valiID)->second;
00284     valueValidatorMap.insert(
00285       StringValidatorDependency::ValueToValidatorPair(value, validator));
00286   }
00287 
00288   RCP<ParameterEntryValidator> defaultValidator = null;
00289   if(xmlObj.hasAttribute(getDefaultValidatorIdAttributeName())){
00290     ParameterEntryValidator::ValidatorID defaultValiID = 
00291       xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
00292         getDefaultValidatorIdAttributeName());
00293     TEST_FOR_EXCEPTION(
00294       validatorIDsMap.find(defaultValiID) == validatorIDsMap.end(),
00295       MissingValidatorException,
00296       "Could not find a validator (for the default validator) " <<
00297       "corresponding to the ID " << defaultValiID << 
00298       " in the given validatorIDsMap!" << std::endl << std::endl);
00299     defaultValidator = validatorIDsMap.find(defaultValiID)->second;
00300   }
00301 
00302   return rcp(new StringValidatorDependency(
00303     dependee, dependents, valueValidatorMap, defaultValidator));
00304 }
00305 
00306 void
00307 BoolValidatorDependencyXMLConverter::convertSpecialValidatorAttributes(
00308   RCP<const ValidatorDependency> dependency,
00309   XMLObject& xmlObj,
00310   ValidatortoIDMap& validatorIDsMap) const
00311 {
00312   RCP<const BoolValidatorDependency> castedDependency = 
00313     rcp_dynamic_cast<const BoolValidatorDependency>(dependency, true);  
00314 
00315   RCP<const ParameterEntryValidator> trueVali = 
00316     castedDependency->getTrueValidator();
00317   RCP<const ParameterEntryValidator> falseVali = 
00318     castedDependency->getFalseValidator();
00319 
00320   if(nonnull(trueVali)){
00321     if(validatorIDsMap.find(castedDependency->getTrueValidator()) == 
00322       validatorIDsMap.end()){
00323       validatorIDsMap.insert(castedDependency->getTrueValidator());
00324     }
00325     xmlObj.addAttribute(
00326       getTrueValidatorIdAttributeName(),
00327       validatorIDsMap.find(castedDependency->getTrueValidator())->second);
00328   }
00329 
00330   if(nonnull(falseVali)){
00331     if(validatorIDsMap.find(falseVali) == 
00332       validatorIDsMap.end()){
00333       validatorIDsMap.insert(falseVali);
00334     }
00335     xmlObj.addAttribute(
00336       getFalseValidatorIdAttributeName(),
00337       validatorIDsMap.find(falseVali)->second);
00338   }
00339 
00340 }
00341 
00342 RCP<ValidatorDependency> 
00343 BoolValidatorDependencyXMLConverter::convertSpecialValidatorAttributes(
00344   const XMLObject& xmlObj,
00345   RCP<const ParameterEntry> dependee,
00346   const Dependency::ParameterEntryList dependents,
00347   const IDtoValidatorMap& validatorIDsMap) const
00348 {
00349 
00350   RCP<ParameterEntryValidator> trueValidator = null;
00351   RCP<ParameterEntryValidator> falseValidator = null;
00352 
00353   if(xmlObj.hasAttribute(getTrueValidatorIdAttributeName())){
00354 
00355     ParameterEntryValidator::ValidatorID trueID = 
00356       xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
00357         getTrueValidatorIdAttributeName());
00358   
00359     TEST_FOR_EXCEPTION(
00360       validatorIDsMap.find(trueID)
00361       == 
00362       validatorIDsMap.end(),
00363       MissingValidatorException,
00364       "Could not find a Validator for the True validator " <<
00365       "with ID " << trueID <<
00366       " in the given validatorIDsMap!" << std::endl << std::endl);
00367   
00368     trueValidator = 
00369       validatorIDsMap.find(trueID)->second;
00370   }
00371 
00372 
00373   if(xmlObj.hasAttribute(getFalseValidatorIdAttributeName())){
00374     ParameterEntryValidator::ValidatorID falseID = 
00375       xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
00376         getFalseValidatorIdAttributeName());
00377   
00378     TEST_FOR_EXCEPTION(
00379       validatorIDsMap.find(falseID)
00380       == 
00381       validatorIDsMap.end(),
00382       MissingValidatorException,
00383       "Could not find a Validator for the False validator " <<
00384       "with ID " << falseID <<
00385       " in the given validatorIDsMap!" << std::endl << std::endl);
00386   
00387     falseValidator = 
00388       validatorIDsMap.find(falseID)->second;
00389   }
00390 
00391   return rcp(new BoolValidatorDependency(
00392     dependee, dependents, trueValidator, falseValidator));
00393 }
00394 
00395 
00396 
00397 } //namespace Teuchos
00398 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines