Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_StandardValidatorXMLConverters.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
11 #define TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
12 
18 #include "Teuchos_StandardParameterEntryValidators.hpp"
22 
23 
24 namespace Teuchos {
25 
26 
65 template<class IntegralType>
68 {
69 
70 public:
71 
74 
77  const XMLObject& xmlObj,
78  const IDtoValidatorMap& validatorIDsMap) const;
79 
81  void convertValidator(
82  const RCP<const ParameterEntryValidator> validator,
83  XMLObject& xmlObj,
84  const ValidatortoIDMap& validatorIDsMap) const;
85 
86  #ifdef HAVE_TEUCHOS_DEBUG
87 
89  getDummyValidator() const{
90  return DummyObjectGetter<
92  }
93  #endif
94 
96 
97 private:
98 
101 
103  static const std::string& getIntegralValueAttributeName() {
104  static const std::string integralValueAttributeName_ = "integralValue";
105  return integralValueAttributeName_;
106  }
107 
109  static const std::string& getStringTagName() {
110  static const std::string stringTagName_ = "String";
111  return stringTagName_;
112  }
113 
115  static const std::string& getStringValueAttributeName() {
116  static const std::string stringValueAttributeName_ = "stringValue";
117  return stringValueAttributeName_;
118  }
119 
121  static const std::string& getStringDocAttributeName() {
122  static const std::string stringDocAttributeName_ = "stringDoc";
123  return stringDocAttributeName_;
124  }
125 
127  static const std::string& getDefaultParameterAttributeName() {
128  static const std::string defaultParameterAttributeName_ =
129  "defaultParameterName";
130  return defaultParameterAttributeName_;
131  }
132 
134  static const std::string& getCaseSensitiveAttributeName() {
135  static const std::string caseSensitiveAttributeName_ =
136  "caseSensitive";
137  return caseSensitiveAttributeName_;
138  }
139 
141 
142 };
143 
144 
145 //
146 // Implementations
147 //
148 
149 
150 template<class IntegralType>
153  const XMLObject& xmlObj,
154  const IDtoValidatorMap& /*validatorIDsMap*/) const
155 {
156  Array<std::string> strings;
157  Array<std::string> stringDocs;
158  Array<IntegralType> integralValues;
159  for (int i=0; i<xmlObj.numChildren(); ++i) {
160  XMLObject currentChild = xmlObj.getChild(i);
161  TEUCHOS_TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(),
163  "Error converting xmlObject to "
164  "StringToIntegralParameterEntryValidator." << std::endl <<
165  "Unrecognized tag: " << currentChild.getTag());
166  strings.append(currentChild.getRequired(getStringValueAttributeName()));
167  if (currentChild.hasAttribute(getIntegralValueAttributeName())) {
168  integralValues.append(
169  currentChild.getRequired<IntegralType>(
170  getIntegralValueAttributeName()));
171  }
172  if (currentChild.hasAttribute(getStringDocAttributeName())) {
173  stringDocs.append(
174  currentChild.getRequired<std::string>(getStringDocAttributeName()));
175  }
176  }
177  std::string defaultParameterName =
178  xmlObj.getRequired(getDefaultParameterAttributeName());
179 
180  // The "caseSensitive" attribute is not required. It is true by default.
181  const bool caseSensitive =
182  xmlObj.getWithDefault<bool> (getCaseSensitiveAttributeName (), true);
183 
185  if (stringDocs.size() != 0 && integralValues.size() != 0) {
186  return rcp (new ret_type (strings, stringDocs, integralValues (), defaultParameterName, caseSensitive));
187  }
188  else if (integralValues.size() != 0) {
189  return rcp (new ret_type (strings, integralValues(), defaultParameterName, caseSensitive));
190  }
191  else {
192  return rcp (new ret_type (strings, defaultParameterName, caseSensitive));
193  }
194 }
195 
196 
197 template<class IntegralType>
199  const RCP<const ParameterEntryValidator> validator,
200  XMLObject& xmlObj,
201  const ValidatortoIDMap& /*validatorIDsMap*/) const
202 {
204  castedValidator =
205  rcp_dynamic_cast<
207  validator, true);
208 
209  RCP<const Array<std::string> > stringValues =
210  castedValidator->validStringValues();
211  RCP<const Array<std::string> > stringDocValues =
212  castedValidator->getStringDocs();
213 
214  bool hasStringDocs =
215  !(stringDocValues.is_null()) && (stringDocValues->size() != 0);
216  for (int i =0; i<stringValues->size(); ++i) {
217  XMLObject stringTag(getStringTagName());
218  stringTag.addAttribute(getStringValueAttributeName(), (*stringValues)[i]);
219  stringTag.addAttribute(getIntegralValueAttributeName(),
220  castedValidator->getIntegralValue((*stringValues)[i]));
221  if (hasStringDocs) {
222  stringTag.addAttribute(
223  getStringDocAttributeName(), (*stringDocValues)[i]);
224  }
225  xmlObj.addChild(stringTag);
226  }
227  xmlObj.addAttribute(getDefaultParameterAttributeName(),
228  castedValidator->getDefaultParameterName());
229 
230  // Add "caseSensitive" bool attribute here.
231  const bool caseSensitive = castedValidator->isCaseSensitive ();
232  xmlObj.addBool (getCaseSensitiveAttributeName (), caseSensitive);
233 
234  xmlObj.addAttribute(getIntegralValueAttributeName(),
236 }
237 
247 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT BoolValidatorXMLConverter : public ValidatorXMLConverter
248 {
249 
250 public:
251 
254 
256  RCP<ParameterEntryValidator> convertXML(
257  const XMLObject& xmlObj,
258  const IDtoValidatorMap& validatorIDsMap) const;
259 
261  void convertValidator(
262  const RCP<const ParameterEntryValidator> validator,
263  XMLObject& xmlObj,
264  const ValidatortoIDMap& validatorIDsMap) const;
265 
266  #ifdef HAVE_TEUCHOS_DEBUG
267 
268  RCP<const ParameterEntryValidator> getDummyValidator() const;
269  #endif
270 
272 
273 private:
274 
277 
278  // currently empty
279 
281 
282 };
283 
297 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT AnyNumberValidatorXMLConverter : public ValidatorXMLConverter
298 {
299 
300 public:
301 
304 
306  RCP<ParameterEntryValidator> convertXML(
307  const XMLObject& xmlObj,
308  const IDtoValidatorMap& validatorIDsMap) const;
309 
311  void convertValidator(
312  const RCP<const ParameterEntryValidator> validator,
313  XMLObject& xmlObj,
314  const ValidatortoIDMap& validatorIDsMap) const;
315 
316  #ifdef HAVE_TEUCHOS_DEBUG
317 
318  RCP<const ParameterEntryValidator> getDummyValidator() const;
319  #endif
320 
322 
323 private:
324 
327 
329  static const std::string& getAllowIntAttributeName() {
330  static const std::string allowIntAttributeName_ = "allowInt";
331  return allowIntAttributeName_;
332  }
333 
335  static const std::string& getAllowDoubleAttributeName() {
336  static const std::string allowDoubleAttributeName_ = "allowDouble";
337  return allowDoubleAttributeName_;
338  }
339 
341  static const std::string& getAllowStringAttributeName() {
342  static const std::string allowStringAttributeName_ = "allowString";
343  return allowStringAttributeName_;
344  }
345 
347  static const std::string& getPrefferedTypeAttributeName() {
348  static const std::string prefferedTypeAttributeName_ = "prefferedType";
349  return prefferedTypeAttributeName_;
350  }
351 
353 
354 };
355 
356 
371 template<class T>
373 {
374 
375 public:
376 
379 
382  const XMLObject& xmlObj,
383  const IDtoValidatorMap& validatorIDsMap) const;
384 
386  void convertValidator(
387  const RCP<const ParameterEntryValidator> validator,
388  XMLObject& xmlObj,
389  const ValidatortoIDMap& validatorIDsMap) const;
390 
391 #ifdef HAVE_TEUCHOS_DEBUG
392 
393  RCP<const ParameterEntryValidator> getDummyValidator() const{
394  return DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject();
395  }
396 #endif
397 
399 
400 private:
401 
404 
406  static const std::string& getMinAttributeName() {
407  static const std::string minAttributeName = "min";
408  return minAttributeName;
409  }
410 
412  static const std::string& getMaxAttributeName() {
413  static const std::string maxAttributeName = "max";
414  return maxAttributeName;
415  }
416 
418  static const std::string& getStepAttributeName() {
419  static const std::string stepAttributeName = "step";
420  return stepAttributeName;
421  }
422 
424  static const std::string& getPrecisionAttributeName() {
425  static const std::string precisionAttributeName = "precision";
426  return precisionAttributeName;
427  }
428 
430 
431 };
432 
433 
434 template<class T>
437  const XMLObject& xmlObj,
438  const IDtoValidatorMap& /*validatorIDsMap*/) const
439 {
440  RCP<EnhancedNumberValidator<T> > toReturn =
442  T step = xmlObj.getWithDefault(
443  getStepAttributeName(), EnhancedNumberTraits<T>::defaultStep());
444  toReturn->setStep(step);
445  unsigned short int precision = xmlObj.getWithDefault(
446  getPrecisionAttributeName(),
448  toReturn->setPrecision(precision);
449  if (xmlObj.hasAttribute(getMinAttributeName())) {
450  toReturn->setMin(xmlObj.getRequired<T>(getMinAttributeName()));
451  }
452  if (xmlObj.hasAttribute(getMaxAttributeName())) {
453  toReturn->setMax(xmlObj.getRequired<T>(getMaxAttributeName()));
454  }
455  return toReturn;
456 }
457 
458 
459 template<class T>
461  const RCP<const ParameterEntryValidator > validator,
462  XMLObject& xmlObj,
463  const ValidatortoIDMap& /*validatorIDsMap*/) const
464 {
465  RCP<const EnhancedNumberValidator<T> > castedValidator =
466  rcp_dynamic_cast<const EnhancedNumberValidator<T> >(validator, true);
467  if (castedValidator->hasMin()) {
468  xmlObj.addAttribute<T>(getMinAttributeName(), castedValidator->getMin());
469  }
470  if (castedValidator->hasMax()) {
471  xmlObj.addAttribute<T>(getMaxAttributeName(), castedValidator->getMax());
472  }
473  xmlObj.addAttribute<T>(getStepAttributeName(), castedValidator->getStep());
474  xmlObj.addAttribute<short unsigned int>(
475  getPrecisionAttributeName(), castedValidator->getPrecision());
476 }
477 
478 
493 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidatorXMLConverter : public ValidatorXMLConverter
494 {
495 
496 public:
497 
500 
502  RCP<ParameterEntryValidator> convertXML(
503  const XMLObject& xmlObj,
504  const IDtoValidatorMap& validatorIDsMap) const;
505 
507  void convertValidator(
508  const RCP<const ParameterEntryValidator> validator,
509  XMLObject& xmlObj,
510  const ValidatortoIDMap& validatorIDsMap) const;
511 
512  #ifdef HAVE_TEUCHOS_DEBUG
513 
514  RCP<const ParameterEntryValidator> getDummyValidator() const;
515  #endif
516 
518 
519 private:
520 
523 
525  static const std::string& getFileMustExistAttributeName() {
526  static const std::string fileMustExistAttributeName = "fileMustExist";
527  return fileMustExistAttributeName;
528  }
529 
531 
532 };
533 
534 
549 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidatorXMLConverter : public ValidatorXMLConverter
550 {
551 
552 public:
553 
556 
558  RCP<ParameterEntryValidator> convertXML(
559  const XMLObject& xmlObj,
560  const IDtoValidatorMap& validatorIDsMap) const;
561 
563  void convertValidator(
564  const RCP<const ParameterEntryValidator> validator,
565  XMLObject& xmlObj,
566  const ValidatortoIDMap& validatorIDsMap) const;
567 
568  #ifdef HAVE_TEUCHOS_DEBUG
569 
570  RCP<const ParameterEntryValidator> getDummyValidator() const;
571  #endif
572 
574 
575 private:
576 
579 
581  static const std::string& getStringTagName() {
582  static const std::string stringTagName = "String";
583  return stringTagName;
584  }
585 
587  static const std::string& getStringValueAttributeName() {
588  static const std::string stringValueAttributeName = "value";
589  return stringValueAttributeName;
590  }
591 
593 
594 };
595 
596 template<class ValidatorType, class EntryType>
597 class AbstractArrayValidatorXMLConverter : public ValidatorXMLConverter{
598 public:
599 
602 
604  RCP<ParameterEntryValidator> convertXML(
605  const XMLObject& xmlObj,
606  const IDtoValidatorMap& validatorIDsMap) const;
607 
609  void convertValidator(
610  const RCP<const ParameterEntryValidator> validator,
611  XMLObject& xmlObj,
612  const ValidatortoIDMap& validatorIDsMap) const;
613 
615 
618 
623  getConcreteValidator(RCP<ValidatorType> prototypeValidator) const = 0;
624 
626 };
627 
628 
629 template<class ValidatorType, class EntryType>
630 RCP<ParameterEntryValidator>
631 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertXML(
632  const XMLObject& xmlObj,
633  const IDtoValidatorMap& validatorIDsMap) const
634 {
635  RCP<ValidatorType> prototypeValidator;
636  if(xmlObj.hasAttribute(
638  {
640  validatorIDsMap.find(
641  xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
642  getPrototypeIdAttributeName()));
643  if (result != validatorIDsMap.end() ) {
644  prototypeValidator =
645  rcp_dynamic_cast<ValidatorType>(result->second, true);
646  }
647  else {
649  MissingValidatorDefinitionException,
650  "Could not find prototype validator with id: "
651  << xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
652  getPrototypeIdAttributeName()) << std::endl<< std::endl);
653  }
654  }
655  else {
656  prototypeValidator = rcp_dynamic_cast<ValidatorType>(
658  xmlObj.getChild(0), validatorIDsMap), true);
659  }
660  return getConcreteValidator(prototypeValidator);
661 }
662 
663 template<class ValidatorType, class EntryType>
664 void
665 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertValidator(
666  const RCP<const ParameterEntryValidator> validator,
667  XMLObject& xmlObj,
668  const ValidatortoIDMap& validatorIDsMap) const
669 {
670  RCP<const AbstractArrayValidator<ValidatorType, EntryType> > castedValidator =
671  rcp_dynamic_cast<const AbstractArrayValidator<ValidatorType, EntryType> >(
672  validator, true);
673  if(validatorIDsMap.find(castedValidator->getPrototype())
674  == validatorIDsMap.end())
675  {
677  castedValidator->getPrototype(), validatorIDsMap, false));
678  }
679  else{
681  validatorIDsMap.find(castedValidator->getPrototype())->second;
682 
683  xmlObj.addAttribute<ParameterEntryValidator::ValidatorID>(
684  getPrototypeIdAttributeName(), prototypeID);
685  }
686 }
687 
715 template<class ValidatorType, class EntryType>
717  public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
718 {
721 
722  virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
723  RCP<ValidatorType> prototypeValidator) const
724  {
725  return rcp(new ArrayValidator<ValidatorType, EntryType>(prototypeValidator));
726  }
727 
728 #ifdef HAVE_TEUCHOS_DEBUG
729 
732  RCP<const ParameterEntryValidator> getDummyValidator() const{
734  getDummyObject();
735  }
737 #endif
738 };
739 
767 template<class ValidatorType, class EntryType>
769  public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
770 {
773 
774  virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
775  RCP<ValidatorType> prototypeValidator) const
776  {
777  return rcp(new TwoDArrayValidator<ValidatorType, EntryType>(prototypeValidator));
778  }
779 
781 
782 #ifdef HAVE_TEUCHOS_DEBUG
783 
786  RCP<const ParameterEntryValidator> getDummyValidator() const{
788  getDummyObject();
789  }
791 #endif
792 
793 };
794 
795 
796 
797 } // namespace Teuchos
798 
799 
800 #endif // TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
801 
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.
void addBool(const std::string &name, bool val)
Add a bool as an attribute.
Convert a StringToIntegralParameterEntryValidator 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 an enum or i...
Converts EnhancedNumberValidators to and from XML.
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.
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.
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
Default traits class that just returns typeid(T).name().
Class for retrieving a dummy object of type T.
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.
Converts BoolParameterEntryValidators to and from XML.
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.
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.
An abstract base class for converting ParameterEntryValidators to and from XML.
bool is_null() const
Returns true if the underlying pointer is null.