Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_StandardValidatorXMLConverters.hpp
Go to the documentation of this file.
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 
43 #ifndef TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
44 #define TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
45 
51 #include "Teuchos_StandardParameterEntryValidators.hpp"
55 
56 
57 namespace Teuchos {
58 
59 
88 template<class IntegralType>
91 {
92 
93 public:
94 
97 
100  const XMLObject& xmlObj,
101  const IDtoValidatorMap& validatorIDsMap) const;
102 
104  void convertValidator(
105  const RCP<const ParameterEntryValidator> validator,
106  XMLObject& xmlObj,
107  const ValidatortoIDMap& validatorIDsMap) const;
108 
109  #ifdef HAVE_TEUCHOS_DEBUG
110 
112  getDummyValidator() const{
113  return DummyObjectGetter<
115  }
116  #endif
117 
119 
120 private:
121 
124 
126  static const std::string& getIntegralValueAttributeName() {
127  static const std::string integralValueAttributeName_ = "integralValue";
128  return integralValueAttributeName_;
129  }
130 
132  static const std::string& getStringTagName() {
133  static const std::string stringTagName_ = "String";
134  return stringTagName_;
135  }
136 
138  static const std::string& getStringValueAttributeName() {
139  static const std::string stringValueAttributeName_ = "stringValue";
140  return stringValueAttributeName_;
141  }
142 
144  static const std::string& getStringDocAttributeName() {
145  static const std::string stringDocAttributeName_ = "stringDoc";
146  return stringDocAttributeName_;
147  }
148 
150  static const std::string& getDefaultParameterAttributeName() {
151  static const std::string defaultParameterAttributeName_ =
152  "defaultParameterName";
153  return defaultParameterAttributeName_;
154  }
155 
157 
158 };
159 
160 
161 //
162 // Implementations
163 //
164 
165 
166 template<class IntegralType>
169  const XMLObject& xmlObj,
170  const IDtoValidatorMap& /*validatorIDsMap*/) const
171 {
172  Array<std::string> strings;
173  Array<std::string> stringDocs;
174  Array<IntegralType> integralValues;
175  for (int i=0; i<xmlObj.numChildren(); ++i) {
176  XMLObject currentChild = xmlObj.getChild(i);
177  TEUCHOS_TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(),
179  "Error converting xmlObject to "
180  "StringToIntegralParameterEntryValidator." << std::endl <<
181  "Unrecognized tag: " << currentChild.getTag());
182  strings.append(currentChild.getRequired(getStringValueAttributeName()));
183  if (currentChild.hasAttribute(getIntegralValueAttributeName())) {
184  integralValues.append(
185  currentChild.getRequired<IntegralType>(
186  getIntegralValueAttributeName()));
187  }
188  if (currentChild.hasAttribute(getStringDocAttributeName())) {
189  stringDocs.append(
190  currentChild.getRequired<std::string>(getStringDocAttributeName()));
191  }
192  }
193  std::string defaultParameterName =
194  xmlObj.getRequired(getDefaultParameterAttributeName());
195 
196  if(stringDocs.size() != 0 && integralValues.size() != 0){
197  return stringToIntegralParameterEntryValidator<IntegralType>(
198  strings, stringDocs, integralValues(), defaultParameterName);
199  }
200  else if(integralValues.size() != 0){
201  return stringToIntegralParameterEntryValidator<IntegralType>(
202  strings, integralValues(), defaultParameterName);
203  }
204  else{
205  return stringToIntegralParameterEntryValidator<IntegralType>(
206  strings, defaultParameterName);
207  }
208 }
209 
210 
211 template<class IntegralType>
213  const RCP<const ParameterEntryValidator> validator,
214  XMLObject& xmlObj,
215  const ValidatortoIDMap& /*validatorIDsMap*/) const
216 {
218  castedValidator =
219  rcp_dynamic_cast<
221  validator, true);
222 
223  RCP<const Array<std::string> > stringValues =
224  castedValidator->validStringValues();
225  RCP<const Array<std::string> > stringDocValues =
226  castedValidator->getStringDocs();
227 
228  bool hasStringDocs =
229  !(stringDocValues.is_null()) && (stringDocValues->size() != 0);
230  for (int i =0; i<stringValues->size(); ++i) {
231  XMLObject stringTag(getStringTagName());
232  stringTag.addAttribute(getStringValueAttributeName(), (*stringValues)[i]);
233  stringTag.addAttribute(getIntegralValueAttributeName(),
234  castedValidator->getIntegralValue((*stringValues)[i]));
235  if (hasStringDocs) {
236  stringTag.addAttribute(
237  getStringDocAttributeName(), (*stringDocValues)[i]);
238  }
239  xmlObj.addChild(stringTag);
240  }
241  xmlObj.addAttribute(getDefaultParameterAttributeName(),
242  castedValidator->getDefaultParameterName());
243  xmlObj.addAttribute(getIntegralValueAttributeName(),
245 }
246 
260 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT AnyNumberValidatorXMLConverter : public ValidatorXMLConverter
261 {
262 
263 public:
264 
267 
270  const XMLObject& xmlObj,
271  const IDtoValidatorMap& validatorIDsMap) const;
272 
274  void convertValidator(
275  const RCP<const ParameterEntryValidator> validator,
276  XMLObject& xmlObj,
277  const ValidatortoIDMap& validatorIDsMap) const;
278 
279  #ifdef HAVE_TEUCHOS_DEBUG
280 
281  RCP<const ParameterEntryValidator> getDummyValidator() const;
282  #endif
283 
285 
286 private:
287 
290 
292  static const std::string& getAllowIntAttributeName() {
293  static const std::string allowIntAttributeName_ = "allowInt";
294  return allowIntAttributeName_;
295  }
296 
298  static const std::string& getAllowDoubleAttributeName() {
299  static const std::string allowDoubleAttributeName_ = "allowDouble";
300  return allowDoubleAttributeName_;
301  }
302 
304  static const std::string& getAllowStringAttributeName() {
305  static const std::string allowStringAttributeName_ = "allowString";
306  return allowStringAttributeName_;
307  }
308 
310  static const std::string& getPrefferedTypeAttributeName() {
311  static const std::string prefferedTypeAttributeName_ = "prefferedType";
312  return prefferedTypeAttributeName_;
313  }
314 
316 
317 };
318 
319 
334 template<class T>
336 {
337 
338 public:
339 
342 
345  const XMLObject& xmlObj,
346  const IDtoValidatorMap& validatorIDsMap) const;
347 
349  void convertValidator(
350  const RCP<const ParameterEntryValidator> validator,
351  XMLObject& xmlObj,
352  const ValidatortoIDMap& validatorIDsMap) const;
353 
354 #ifdef HAVE_TEUCHOS_DEBUG
355 
356  RCP<const ParameterEntryValidator> getDummyValidator() const{
357  return DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject();
358  }
359 #endif
360 
362 
363 private:
364 
367 
369  static const std::string& getMinAttributeName() {
370  static const std::string minAttributeName = "min";
371  return minAttributeName;
372  }
373 
375  static const std::string& getMaxAttributeName() {
376  static const std::string maxAttributeName = "max";
377  return maxAttributeName;
378  }
379 
381  static const std::string& getStepAttributeName() {
382  static const std::string stepAttributeName = "step";
383  return stepAttributeName;
384  }
385 
387  static const std::string& getPrecisionAttributeName() {
388  static const std::string precisionAttributeName = "precision";
389  return precisionAttributeName;
390  }
391 
393 
394 };
395 
396 
397 template<class T>
400  const XMLObject& xmlObj,
401  const IDtoValidatorMap& /*validatorIDsMap*/) const
402 {
403  RCP<EnhancedNumberValidator<T> > toReturn =
405  T step = xmlObj.getWithDefault(
406  getStepAttributeName(), EnhancedNumberTraits<T>::defaultStep());
407  toReturn->setStep(step);
408  unsigned short int precision = xmlObj.getWithDefault(
409  getPrecisionAttributeName(),
411  toReturn->setPrecision(precision);
412  if (xmlObj.hasAttribute(getMinAttributeName())) {
413  toReturn->setMin(xmlObj.getRequired<T>(getMinAttributeName()));
414  }
415  if (xmlObj.hasAttribute(getMaxAttributeName())) {
416  toReturn->setMax(xmlObj.getRequired<T>(getMaxAttributeName()));
417  }
418  return toReturn;
419 }
420 
421 
422 template<class T>
424  const RCP<const ParameterEntryValidator > validator,
425  XMLObject& xmlObj,
426  const ValidatortoIDMap& /*validatorIDsMap*/) const
427 {
428  RCP<const EnhancedNumberValidator<T> > castedValidator =
429  rcp_dynamic_cast<const EnhancedNumberValidator<T> >(validator, true);
430  if (castedValidator->hasMin()) {
431  xmlObj.addAttribute<T>(getMinAttributeName(), castedValidator->getMin());
432  }
433  if (castedValidator->hasMax()) {
434  xmlObj.addAttribute<T>(getMaxAttributeName(), castedValidator->getMax());
435  }
436  xmlObj.addAttribute<T>(getStepAttributeName(), castedValidator->getStep());
437  xmlObj.addAttribute<short unsigned int>(
438  getPrecisionAttributeName(), castedValidator->getPrecision());
439 }
440 
441 
456 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidatorXMLConverter : public ValidatorXMLConverter
457 {
458 
459 public:
460 
463 
466  const XMLObject& xmlObj,
467  const IDtoValidatorMap& validatorIDsMap) const;
468 
470  void convertValidator(
471  const RCP<const ParameterEntryValidator> validator,
472  XMLObject& xmlObj,
473  const ValidatortoIDMap& validatorIDsMap) const;
474 
475  #ifdef HAVE_TEUCHOS_DEBUG
476 
477  RCP<const ParameterEntryValidator> getDummyValidator() const;
478  #endif
479 
481 
482 private:
483 
486 
488  static const std::string& getFileMustExistAttributeName() {
489  static const std::string fileMustExistAttributeName = "fileMustExist";
490  return fileMustExistAttributeName;
491  }
492 
494 
495 };
496 
497 
512 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidatorXMLConverter : public ValidatorXMLConverter
513 {
514 
515 public:
516 
519 
522  const XMLObject& xmlObj,
523  const IDtoValidatorMap& validatorIDsMap) const;
524 
526  void convertValidator(
527  const RCP<const ParameterEntryValidator> validator,
528  XMLObject& xmlObj,
529  const ValidatortoIDMap& validatorIDsMap) const;
530 
531  #ifdef HAVE_TEUCHOS_DEBUG
532 
533  RCP<const ParameterEntryValidator> getDummyValidator() const;
534  #endif
535 
537 
538 private:
539 
542 
544  static const std::string& getStringTagName() {
545  static const std::string stringTagName = "String";
546  return stringTagName;
547  }
548 
550  static const std::string& getStringValueAttributeName() {
551  static const std::string stringValueAttributeName = "value";
552  return stringValueAttributeName;
553  }
554 
556 
557 };
558 
559 template<class ValidatorType, class EntryType>
560 class AbstractArrayValidatorXMLConverter : public ValidatorXMLConverter{
561 public:
562 
565 
568  const XMLObject& xmlObj,
569  const IDtoValidatorMap& validatorIDsMap) const;
570 
572  void convertValidator(
573  const RCP<const ParameterEntryValidator> validator,
574  XMLObject& xmlObj,
575  const ValidatortoIDMap& validatorIDsMap) const;
576 
578 
581 
586  getConcreteValidator(RCP<ValidatorType> prototypeValidator) const = 0;
587 
589 };
590 
591 
592 template<class ValidatorType, class EntryType>
594 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertXML(
595  const XMLObject& xmlObj,
596  const IDtoValidatorMap& validatorIDsMap) const
597 {
598  RCP<ValidatorType> prototypeValidator;
599  if(xmlObj.hasAttribute(
601  {
603  validatorIDsMap.find(
606  if (result != validatorIDsMap.end() ) {
607  prototypeValidator =
608  rcp_dynamic_cast<ValidatorType>(result->second, true);
609  }
610  else {
613  "Could not find prototype validator with id: "
615  getPrototypeIdAttributeName()) << std::endl<< std::endl);
616  }
617  }
618  else {
619  prototypeValidator = rcp_dynamic_cast<ValidatorType>(
621  xmlObj.getChild(0), validatorIDsMap), true);
622  }
623  return getConcreteValidator(prototypeValidator);
624 }
625 
626 template<class ValidatorType, class EntryType>
627 void
628 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertValidator(
629  const RCP<const ParameterEntryValidator> validator,
630  XMLObject& xmlObj,
631  const ValidatortoIDMap& validatorIDsMap) const
632 {
634  rcp_dynamic_cast<const AbstractArrayValidator<ValidatorType, EntryType> >(
635  validator, true);
636  if(validatorIDsMap.find(castedValidator->getPrototype())
637  == validatorIDsMap.end())
638  {
640  castedValidator->getPrototype(), validatorIDsMap, false));
641  }
642  else{
644  validatorIDsMap.find(castedValidator->getPrototype())->second;
645 
647  getPrototypeIdAttributeName(), prototypeID);
648  }
649 }
650 
678 template<class ValidatorType, class EntryType>
680  public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
681 {
684 
685  virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
686  RCP<ValidatorType> prototypeValidator) const
687  {
688  return rcp(new ArrayValidator<ValidatorType, EntryType>(prototypeValidator));
689  }
690 
691 #ifdef HAVE_TEUCHOS_DEBUG
692 
695  RCP<const ParameterEntryValidator> getDummyValidator() const{
697  getDummyObject();
698  }
700 #endif
701 };
702 
730 template<class ValidatorType, class EntryType>
732  public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
733 {
736 
737  virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
738  RCP<ValidatorType> prototypeValidator) const
739  {
740  return rcp(new TwoDArrayValidator<ValidatorType, EntryType>(prototypeValidator));
741  }
742 
744 
745 #ifdef HAVE_TEUCHOS_DEBUG
746 
749  RCP<const ParameterEntryValidator> getDummyValidator() const{
751  getDummyObject();
752  }
754 #endif
755 
756 };
757 
758 
759 
760 } // namespace Teuchos
761 
762 
763 #endif // TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
764 
const_iterator find(int id) const
Retrieves and iterator to a validator and id based on the id given.
Writes an XML object to a parameter list.
const std::string & getTag() const
Return the tag of the current node.
Array< T > & append(const T &x)
Add a new entry at the end of the array.
Maps Validators to integers.
Converts StringToIntegralParameterEntryValidators to and from XML.
Converts StringValidators to and from XML.
Converts AnyNumberParameterEntryValidators to and from XML.
Converts FileNameValidators to and from XML.
ValidatorMap::const_iterator const_iterator
#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.
Standard implementation of a ParameterEntryValidator that maps from a list of strings to some integra...
Converts EnhancedNumberValidators to and from XML.
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.
Converts TwoDArrayValidators to and from XML.
Takes a validator, wraps it, and applies it to a TwoDArray.
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
Converts ArrayValidators to and from XML.
static XMLObject convertValidator(RCP< const ParameterEntryValidator > validator, const ValidatortoIDMap &validatorIDsMap, bool assignedID=true)
Given a validator converts the validator to XML.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
static const std::string & getPrototypeIdAttributeName()
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...
Converts back and forth between XML and ParameterEntryValidators.
A database for ValidatorXMLConverters.
const_iterator end() const
Returns a const_reference to the end of the map.
An abstract base class for all ArrayValidators.
A class for mapping validators to integers.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
Takes a validator, wraps it, and applies it to an array.
const_iterator end() const
Returns a const_reference to the end of the map.
static RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObject, const IDtoValidatorMap &validatorIDsMap)
Given an XMLObject converts the XMLObject to a ParameterEntryValidator and inserts the validator into...
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
const_iterator find(const RCP< const ParameterEntryValidator > validator) const
Returns an iterator to the validator and id specified by the validator.
Default traits class that just returns typeid(T).name().
Class for retrieving a dummy object of type T.
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.
Class uesd to validate a particular type of number.
size_type size() const
Class defining the traits of the number type being used in an EnhancedNumberValidator.
Smart reference counting pointer class for automatic garbage collection.
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.
bool hasAttribute(const std::string &name) const
Find out if the current node has an attribute of the specified name.
T getWithDefault(const std::string &name, const T &defaultValue) const
Get an attribute, assigning a default value if the requested attribute does not exist.
An abstract base class for converting ParameterEntryValidators to and from XML.
Thrown when a referenced validator can&#39;t be found.
bool is_null() const
Returns true if the underlying pointer is null.