Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_StandardValidatorXMLConverters.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 
43 #include "Teuchos_RCP.hpp"
44 
49 namespace Teuchos {
50 
51 
53  const XMLObject& xmlObj,
54  const IDtoValidatorMap& /*validatorIDsMap*/) const
55 {
56 
58  acceptedTypes.allowInt(xmlObj.getRequiredBool(getAllowIntAttributeName()));
59  acceptedTypes.allowDouble(
60  xmlObj.getRequiredBool(getAllowDoubleAttributeName()));
61  acceptedTypes.allowString(
62  xmlObj.getRequiredBool(getAllowStringAttributeName()));
63  return anyNumberParameterEntryValidator(
65  xmlObj.getRequired(getPrefferedTypeAttributeName())),
66  acceptedTypes);
67 }
68 
69 
71  const RCP<const ParameterEntryValidator> validator,
72  XMLObject& xmlObj,
73  const ValidatortoIDMap& /*validatorIDsMap*/) const
74 {
76  rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(validator, true);
77  xmlObj.addBool(
78  getAllowIntAttributeName(), castedValidator->isIntAllowed());
79  xmlObj.addBool(
80  getAllowDoubleAttributeName(), castedValidator->isDoubleAllowed());
81  xmlObj.addBool(
82  getAllowStringAttributeName(), castedValidator->isStringAllowed());
83  xmlObj.addAttribute(getPrefferedTypeAttributeName(),
84  castedValidator->getPrefferedTypeString(
85  castedValidator->getPreferredType()));
86 }
87 
88 #ifdef HAVE_TEUCHOS_DEBUG
90 AnyNumberValidatorXMLConverter::getDummyValidator() const{
92 }
93 #endif
94 
96  const XMLObject& xmlObj,
97  const IDtoValidatorMap& /*validatorIDsMap*/) const
98 {
99  return rcp(
100  new FileNameValidator(
101  xmlObj.getWithDefault<bool>(
102  getFileMustExistAttributeName(),
104  )
105  )
106  );
107 }
108 
109 
111  const RCP<const ParameterEntryValidator> validator,
112  XMLObject& xmlObj,
113  const ValidatortoIDMap& /*validatorIDsMap*/) const
114 {
115  RCP<const FileNameValidator> castedValidator =
116  rcp_dynamic_cast<const FileNameValidator>(validator);
117  xmlObj.addBool(
118  getFileMustExistAttributeName(), castedValidator->fileMustExist());
119 }
120 
121 
122 #ifdef HAVE_TEUCHOS_DEBUG
124 FileNameValidatorXMLConverter::getDummyValidator() const{
126 }
127 #endif
128 
129 
131  const XMLObject& xmlObj,
132  const IDtoValidatorMap& /*validatorIDsMap*/) const
133 {
134  Array<std::string> strings(xmlObj.numChildren());
135  if(xmlObj.numChildren()!=0){
136  for(int i=0; i<xmlObj.numChildren(); ++i){
137  XMLObject currentChild = xmlObj.getChild(i);
138  TEUCHOS_TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(),
140  "Error converting xmlObject to StringValidator." << std::endl <<
141  "Unrecognized tag: " << currentChild.getTag());
142  strings[i] = (currentChild.getRequired(getStringValueAttributeName()));
143  }
144  }
145  return rcp(new StringValidator(strings));
146 }
147 
148 
150  const RCP<const ParameterEntryValidator> validator,
151  XMLObject& xmlObj,
152  const ValidatortoIDMap& /*validatorIDsMap*/) const
153 {
154  RCP<const StringValidator> castedValidator =
155  rcp_dynamic_cast<const StringValidator>(validator);
156 
157  if(!is_null(validator->validStringValues())){
159  validator->validStringValues()->begin();
160  for(; it != validator->validStringValues()->end(); ++it){
161  XMLObject stringTag(getStringTagName());
162  stringTag.addAttribute(getStringValueAttributeName(), *it);
163  xmlObj.addChild(stringTag);
164  }
165  }
166 }
167 
168 
169 #ifdef HAVE_TEUCHOS_DEBUG
171 StringValidatorXMLConverter::getDummyValidator() const{
173 }
174 #endif
175 
176 } // namespace Teuchos
177 
static bool mustAlreadyExistDefault()
The default value of the mustAlreadyExist parameter in the constructor.
A collection of standard ValidatorXMLConverters.
const std::string & getTag() const
Return the tag of the current node.
Maps Validators to integers.
static RCP< T > getDummyObject()
Retrieves a dummy object of type T.
void addBool(const std::string &name, bool val)
Add a bool as an attribute.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
void addChild(const XMLObject &child)
Add a child node to the node.
AcceptedTypes & allowString(bool _allowString)
Set allow an std::string value or not.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.
static EPreferredType getPrefferedTypeStringEnum(const std::string &enumString)
Gets the preferred type enum associated with a give string.
AcceptedTypes & allowInt(bool _allowInt)
Set allow an int value or not.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
A class for mapping validators to integers.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
A simple validator that only allows certain string values to be choosen or simply enforces that a par...
bool getRequiredBool(const std::string &name) const
Get a required attribute, returning it as a bool.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
int numChildren() const
Return the number of child nodes owned by this node.
AcceptedTypes & allowDouble(bool _allowDouble)
Set allow a double value or not.
Smart reference counting pointer class for automatic garbage collection.
bool fileMustExist() const
Gets the variable describing whether or not this validator wants the file that is specified to alread...
Thrown when xml tag is encountered that is either unrecognized or inappropriate for a given context...
const XMLObject & getChild(int i) const
Return the i-th child node.
Standard implementation of a ParameterEntryValidator that accepts numbers from a number of different ...
T getWithDefault(const std::string &name, const T &defaultValue) const
Get an attribute, assigning a default value if the requested attribute does not exist.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
Reference-counted pointer class and non-member templated function implementations.