Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
52  const XMLObject& /* xmlObj */,
53  const IDtoValidatorMap& /*validatorIDsMap*/) const
54 {
55  return boolParameterEntryValidator();
56 }
57 
58 
60  const RCP<const ParameterEntryValidator> /* validator */,
61  XMLObject& /* xmlObj */,
62  const ValidatortoIDMap& /*validatorIDsMap*/) const
63 {
64  //RCP<const AnyNumberParameterEntryValidator> castedValidator =
65  // rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(validator, true);
66 
67  // currently no action
68 }
69 
70 #ifdef HAVE_TEUCHOS_DEBUG
72 BoolValidatorXMLConverter::getDummyValidator() const{
74 }
75 #endif
76 
78  const XMLObject& xmlObj,
79  const IDtoValidatorMap& /*validatorIDsMap*/) const
80 {
81 
83  acceptedTypes.allowInt(xmlObj.getRequiredBool(getAllowIntAttributeName()));
84  acceptedTypes.allowDouble(
85  xmlObj.getRequiredBool(getAllowDoubleAttributeName()));
86  acceptedTypes.allowString(
87  xmlObj.getRequiredBool(getAllowStringAttributeName()));
88  return anyNumberParameterEntryValidator(
90  xmlObj.getRequired(getPrefferedTypeAttributeName())),
91  acceptedTypes);
92 }
93 
94 
96  const RCP<const ParameterEntryValidator> validator,
97  XMLObject& xmlObj,
98  const ValidatortoIDMap& /*validatorIDsMap*/) const
99 {
101  rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(validator, true);
102  xmlObj.addBool(
103  getAllowIntAttributeName(), castedValidator->isIntAllowed());
104  xmlObj.addBool(
105  getAllowDoubleAttributeName(), castedValidator->isDoubleAllowed());
106  xmlObj.addBool(
107  getAllowStringAttributeName(), castedValidator->isStringAllowed());
108  xmlObj.addAttribute(getPrefferedTypeAttributeName(),
109  castedValidator->getPrefferedTypeString(
110  castedValidator->getPreferredType()));
111 }
112 
113 #ifdef HAVE_TEUCHOS_DEBUG
115 AnyNumberValidatorXMLConverter::getDummyValidator() const{
117 }
118 #endif
119 
121  const XMLObject& xmlObj,
122  const IDtoValidatorMap& /*validatorIDsMap*/) const
123 {
124  return rcp(
125  new FileNameValidator(
126  xmlObj.getWithDefault<bool>(
127  getFileMustExistAttributeName(),
129  )
130  )
131  );
132 }
133 
134 
136  const RCP<const ParameterEntryValidator> validator,
137  XMLObject& xmlObj,
138  const ValidatortoIDMap& /*validatorIDsMap*/) const
139 {
140  RCP<const FileNameValidator> castedValidator =
141  rcp_dynamic_cast<const FileNameValidator>(validator);
142  xmlObj.addBool(
143  getFileMustExistAttributeName(), castedValidator->fileMustExist());
144 }
145 
146 
147 #ifdef HAVE_TEUCHOS_DEBUG
149 FileNameValidatorXMLConverter::getDummyValidator() const{
151 }
152 #endif
153 
154 
156  const XMLObject& xmlObj,
157  const IDtoValidatorMap& /*validatorIDsMap*/) const
158 {
159  Array<std::string> strings(xmlObj.numChildren());
160  if(xmlObj.numChildren()!=0){
161  for(int i=0; i<xmlObj.numChildren(); ++i){
162  XMLObject currentChild = xmlObj.getChild(i);
163  TEUCHOS_TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(),
165  "Error converting xmlObject to StringValidator." << std::endl <<
166  "Unrecognized tag: " << currentChild.getTag());
167  strings[i] = (currentChild.getRequired(getStringValueAttributeName()));
168  }
169  }
170  return rcp(new StringValidator(strings));
171 }
172 
173 
175  const RCP<const ParameterEntryValidator> validator,
176  XMLObject& xmlObj,
177  const ValidatortoIDMap& /*validatorIDsMap*/) const
178 {
179  RCP<const StringValidator> castedValidator =
180  rcp_dynamic_cast<const StringValidator>(validator);
181 
182  if(!is_null(validator->validStringValues())){
184  validator->validStringValues()->begin();
185  for(; it != validator->validStringValues()->end(); ++it){
186  XMLObject stringTag(getStringTagName());
187  stringTag.addAttribute(getStringValueAttributeName(), *it);
188  xmlObj.addChild(stringTag);
189  }
190  }
191 }
192 
193 
194 #ifdef HAVE_TEUCHOS_DEBUG
196 StringValidatorXMLConverter::getDummyValidator() const{
198 }
199 #endif
200 
201 } // namespace Teuchos
202 
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
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.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
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.
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
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.
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.
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.