Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_ParameterEntryXMLConverter.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "Teuchos_ParameterEntryXMLConverter.hpp"
46 #include "Teuchos_ParameterEntryXMLConverterDB.hpp"
47 
48 namespace Teuchos{
49 
50 
51 ParameterEntry
53  const XMLObject &xmlObj) const
54 {
55  #ifdef HAVE_TEUCHOS_DEBUG
59  "Error: this Parameter Entry XML tag has a type different than "
60  "the XMLConverter being used to convert it." <<std::endl <<
61  "Parameter name: " << xmlObj.getRequired(
63  "XML Parameter Entry type: " <<
64  xmlObj.getRequired(getTypeAttributeName()) << std::endl <<
65  "XMLConverter type: " << getTypeAttributeValue() <<
66  std::endl <<std::endl);
67  #endif
68 
72  ParameterEntry::getTagName() <<" tags must "
73  "have a " << getValueAttributeName() << " attribute" << std::endl <<
74  "Bad Parameter: " <<
76  std::endl << std::endl);
77 
78  ParameterEntry toReturn;
79  bool isDefault = false;
80  bool isUsed = false;
81  std::string docString = "";
82 
83 
84  if(xmlObj.hasAttribute(getDefaultAttributeName())){
85  isDefault = xmlObj.getRequiredBool(getDefaultAttributeName());
86  }
87 
88  if(xmlObj.hasAttribute(getUsedAttributeName())){
89  isUsed = xmlObj.getRequiredBool(getUsedAttributeName());
90  }
91 
92  if(xmlObj.hasAttribute(getDocStringAttributeName())){
93  docString = xmlObj.getRequired(getDocStringAttributeName());
94  }
95 
96  toReturn.setAnyValue(getAny(xmlObj), isDefault);
97  toReturn.setDocString(docString);
98 
99  if(isUsed){
100  toReturn.getAny();
101  }
102 
103  return toReturn;
104 }
105 
106 
107 XMLObject
110  const std::string &name,
112  const ValidatortoIDMap& validatorIDsMap) const
113 {
114  #ifdef HAVE_TEUCHOS_DEBUG
116  (entry->getAny().typeName() != getTypeAttributeValue())
117  &&
118  (
120  ParameterEntryXMLConverterDB::getDefaultConverter()->getTypeAttributeValue()
121  ),
123  "Error: This converter can't convert the given ParameterEntry to XML "
124  "because their types don't match." << std::endl <<
125  "Parameter name: " << name << std::endl <<
126  "Parameter type: " << entry->getAny().typeName() << std::endl <<
127  "Converter type: " << getTypeAttributeValue() << std::endl << std::endl);
128  #endif
129 
131  toReturn.addAttribute(
133  toReturn.addAttribute(getTypeAttributeName(), getTypeAttributeValue());
134  toReturn.addAttribute(getDocStringAttributeName(), entry->docString());
135  toReturn.addAttribute(getIdAttributeName(), id);
136  toReturn.addAttribute(
138  toReturn.addBool(getDefaultAttributeName(), entry->isDefault());
139  toReturn.addBool(getUsedAttributeName(), entry->isUsed());
140  if(nonnull(entry->validator())){
142  validatorIDsMap.find(entry->validator()) == validatorIDsMap.end(),
144  "Could not find validator in given ValidatorIDsMap! " <<
145  std::endl << std::endl);
146  toReturn.addAttribute(
148  validatorIDsMap.find(entry->validator())->second);
149  }
150  return toReturn;
151 }
152 
153 
154 } // namespace Teuchos
155 
static const std::string & getIdAttributeName()
XMLObject fromParameterEntrytoXML(RCP< const ParameterEntry > entry, const std::string &name, const ParameterEntry::ParameterEntryID &id, const ValidatortoIDMap &validatorIDsMap) const
Converts the given parameter entry to xml.
Thrown when a parameter entry tag is missing it&#39;s value attribute.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
This object is held as the "value" in the Teuchos::ParameterList std::map.
static const std::string & getTagName()
Get the string that should be used as the tag name for all parameters when they are serialized to xml...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.
Thrown when a converter is being used to convert either and XML tag or ParameterEntry with an innappr...
virtual const std::string getTypeAttributeValue() const =0
Gets a string representing the value that should be assigned to the "type" attribute when converting ...
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
Converts back and forth between XML and ParameterEntryValidators.
Writes a ParameterList to an XML object.
ParameterEntry fromXMLtoParameterEntry(const XMLObject &xmlObj) const
Converts the given xml into a parameter entry.
const_iterator end() const
Returns a const_reference to the end of the map.
A class for mapping validators to integers.
bool getRequiredBool(const std::string &name) const
Get a required attribute, returning it as a bool.
const_iterator find(const RCP< const ParameterEntryValidator > validator) const
Returns an iterator to the validator and id specified by the validator.
virtual const std::string getValueAttributeValue(RCP< const ParameterEntry > entry) const =0
Gets the value to be assigned to the "value" attribute when converting the paramter entry to xml...
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
A collection of Exceptions that can be potentially thrown when converting a ParameterList to and from...
const std::string & getAttribute(const std::string &name) const
Return the value of the attribute with the specified name.
static RCP< const ParameterEntryXMLConverter > getDefaultConverter()
Gets the default converter to be used on Parameter Entries.
Smart reference counting pointer class for automatic garbage collection.
bool hasAttribute(const std::string &name) const
Find out if the current node has an attribute of the specified name.
static const std::string & getNameAttributeName()
Thrown when a referenced validator can&#39;t be found.