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 
98 template<class IntegralType>
100  public ValidatorXMLConverter
101 {
102 
103 public:
104 
107 
110  const XMLObject& xmlObj,
111  const IDtoValidatorMap& validatorIDsMap) const;
112 
114  void convertValidator(
115  const RCP<const ParameterEntryValidator> validator,
116  XMLObject& xmlObj,
117  const ValidatortoIDMap& validatorIDsMap) const;
118 
119  #ifdef HAVE_TEUCHOS_DEBUG
120 
122  getDummyValidator() const{
123  return DummyObjectGetter<
125  }
126  #endif
127 
129 
130 private:
131 
134 
136  static const std::string& getIntegralValueAttributeName() {
137  static const std::string integralValueAttributeName_ = "integralValue";
138  return integralValueAttributeName_;
139  }
140 
142  static const std::string& getStringTagName() {
143  static const std::string stringTagName_ = "String";
144  return stringTagName_;
145  }
146 
148  static const std::string& getStringValueAttributeName() {
149  static const std::string stringValueAttributeName_ = "stringValue";
150  return stringValueAttributeName_;
151  }
152 
154  static const std::string& getStringDocAttributeName() {
155  static const std::string stringDocAttributeName_ = "stringDoc";
156  return stringDocAttributeName_;
157  }
158 
160  static const std::string& getDefaultParameterAttributeName() {
161  static const std::string defaultParameterAttributeName_ =
162  "defaultParameterName";
163  return defaultParameterAttributeName_;
164  }
165 
167  static const std::string& getCaseSensitiveAttributeName() {
168  static const std::string caseSensitiveAttributeName_ =
169  "caseSensitive";
170  return caseSensitiveAttributeName_;
171  }
172 
174 
175 };
176 
177 
178 //
179 // Implementations
180 //
181 
182 
183 template<class IntegralType>
186  const XMLObject& xmlObj,
187  const IDtoValidatorMap& /*validatorIDsMap*/) const
188 {
189  Array<std::string> strings;
190  Array<std::string> stringDocs;
191  Array<IntegralType> integralValues;
192  for (int i=0; i<xmlObj.numChildren(); ++i) {
193  XMLObject currentChild = xmlObj.getChild(i);
194  TEUCHOS_TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(),
196  "Error converting xmlObject to "
197  "StringToIntegralParameterEntryValidator." << std::endl <<
198  "Unrecognized tag: " << currentChild.getTag());
199  strings.append(currentChild.getRequired(getStringValueAttributeName()));
200  if (currentChild.hasAttribute(getIntegralValueAttributeName())) {
201  integralValues.append(
202  currentChild.getRequired<IntegralType>(
203  getIntegralValueAttributeName()));
204  }
205  if (currentChild.hasAttribute(getStringDocAttributeName())) {
206  stringDocs.append(
207  currentChild.getRequired<std::string>(getStringDocAttributeName()));
208  }
209  }
210  std::string defaultParameterName =
211  xmlObj.getRequired(getDefaultParameterAttributeName());
212 
213  // The "caseSensitive" attribute is not required. It is true by default.
214  const bool caseSensitive =
215  xmlObj.getWithDefault<bool> (getCaseSensitiveAttributeName (), true);
216 
218  if (stringDocs.size() != 0 && integralValues.size() != 0) {
219  return rcp (new ret_type (strings, stringDocs, integralValues (), defaultParameterName, caseSensitive));
220  }
221  else if (integralValues.size() != 0) {
222  return rcp (new ret_type (strings, integralValues(), defaultParameterName, caseSensitive));
223  }
224  else {
225  return rcp (new ret_type (strings, defaultParameterName, caseSensitive));
226  }
227 }
228 
229 
230 template<class IntegralType>
232  const RCP<const ParameterEntryValidator> validator,
233  XMLObject& xmlObj,
234  const ValidatortoIDMap& /*validatorIDsMap*/) const
235 {
237  castedValidator =
238  rcp_dynamic_cast<
240  validator, true);
241 
242  RCP<const Array<std::string> > stringValues =
243  castedValidator->validStringValues();
244  RCP<const Array<std::string> > stringDocValues =
245  castedValidator->getStringDocs();
246 
247  bool hasStringDocs =
248  !(stringDocValues.is_null()) && (stringDocValues->size() != 0);
249  for (int i =0; i<stringValues->size(); ++i) {
250  XMLObject stringTag(getStringTagName());
251  stringTag.addAttribute(getStringValueAttributeName(), (*stringValues)[i]);
252  stringTag.addAttribute(getIntegralValueAttributeName(),
253  castedValidator->getIntegralValue((*stringValues)[i]));
254  if (hasStringDocs) {
255  stringTag.addAttribute(
256  getStringDocAttributeName(), (*stringDocValues)[i]);
257  }
258  xmlObj.addChild(stringTag);
259  }
260  xmlObj.addAttribute(getDefaultParameterAttributeName(),
261  castedValidator->getDefaultParameterName());
262 
263  // Add "caseSensitive" bool attribute here.
264  const bool caseSensitive = castedValidator->isCaseSensitive ();
265  xmlObj.addBool (getCaseSensitiveAttributeName (), caseSensitive);
266 
267  xmlObj.addAttribute(getIntegralValueAttributeName(),
269 }
270 
284 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT AnyNumberValidatorXMLConverter : public ValidatorXMLConverter
285 {
286 
287 public:
288 
291 
294  const XMLObject& xmlObj,
295  const IDtoValidatorMap& validatorIDsMap) const;
296 
298  void convertValidator(
299  const RCP<const ParameterEntryValidator> validator,
300  XMLObject& xmlObj,
301  const ValidatortoIDMap& validatorIDsMap) const;
302 
303  #ifdef HAVE_TEUCHOS_DEBUG
304 
305  RCP<const ParameterEntryValidator> getDummyValidator() const;
306  #endif
307 
309 
310 private:
311 
314 
316  static const std::string& getAllowIntAttributeName() {
317  static const std::string allowIntAttributeName_ = "allowInt";
318  return allowIntAttributeName_;
319  }
320 
322  static const std::string& getAllowDoubleAttributeName() {
323  static const std::string allowDoubleAttributeName_ = "allowDouble";
324  return allowDoubleAttributeName_;
325  }
326 
328  static const std::string& getAllowStringAttributeName() {
329  static const std::string allowStringAttributeName_ = "allowString";
330  return allowStringAttributeName_;
331  }
332 
334  static const std::string& getPrefferedTypeAttributeName() {
335  static const std::string prefferedTypeAttributeName_ = "prefferedType";
336  return prefferedTypeAttributeName_;
337  }
338 
340 
341 };
342 
343 
358 template<class T>
360 {
361 
362 public:
363 
366 
369  const XMLObject& xmlObj,
370  const IDtoValidatorMap& validatorIDsMap) const;
371 
373  void convertValidator(
374  const RCP<const ParameterEntryValidator> validator,
375  XMLObject& xmlObj,
376  const ValidatortoIDMap& validatorIDsMap) const;
377 
378 #ifdef HAVE_TEUCHOS_DEBUG
379 
380  RCP<const ParameterEntryValidator> getDummyValidator() const{
381  return DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject();
382  }
383 #endif
384 
386 
387 private:
388 
391 
393  static const std::string& getMinAttributeName() {
394  static const std::string minAttributeName = "min";
395  return minAttributeName;
396  }
397 
399  static const std::string& getMaxAttributeName() {
400  static const std::string maxAttributeName = "max";
401  return maxAttributeName;
402  }
403 
405  static const std::string& getStepAttributeName() {
406  static const std::string stepAttributeName = "step";
407  return stepAttributeName;
408  }
409 
411  static const std::string& getPrecisionAttributeName() {
412  static const std::string precisionAttributeName = "precision";
413  return precisionAttributeName;
414  }
415 
417 
418 };
419 
420 
421 template<class T>
424  const XMLObject& xmlObj,
425  const IDtoValidatorMap& /*validatorIDsMap*/) const
426 {
427  RCP<EnhancedNumberValidator<T> > toReturn =
429  T step = xmlObj.getWithDefault(
430  getStepAttributeName(), EnhancedNumberTraits<T>::defaultStep());
431  toReturn->setStep(step);
432  unsigned short int precision = xmlObj.getWithDefault(
433  getPrecisionAttributeName(),
435  toReturn->setPrecision(precision);
436  if (xmlObj.hasAttribute(getMinAttributeName())) {
437  toReturn->setMin(xmlObj.getRequired<T>(getMinAttributeName()));
438  }
439  if (xmlObj.hasAttribute(getMaxAttributeName())) {
440  toReturn->setMax(xmlObj.getRequired<T>(getMaxAttributeName()));
441  }
442  return toReturn;
443 }
444 
445 
446 template<class T>
448  const RCP<const ParameterEntryValidator > validator,
449  XMLObject& xmlObj,
450  const ValidatortoIDMap& /*validatorIDsMap*/) const
451 {
452  RCP<const EnhancedNumberValidator<T> > castedValidator =
453  rcp_dynamic_cast<const EnhancedNumberValidator<T> >(validator, true);
454  if (castedValidator->hasMin()) {
455  xmlObj.addAttribute<T>(getMinAttributeName(), castedValidator->getMin());
456  }
457  if (castedValidator->hasMax()) {
458  xmlObj.addAttribute<T>(getMaxAttributeName(), castedValidator->getMax());
459  }
460  xmlObj.addAttribute<T>(getStepAttributeName(), castedValidator->getStep());
461  xmlObj.addAttribute<short unsigned int>(
462  getPrecisionAttributeName(), castedValidator->getPrecision());
463 }
464 
465 
480 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidatorXMLConverter : public ValidatorXMLConverter
481 {
482 
483 public:
484 
487 
490  const XMLObject& xmlObj,
491  const IDtoValidatorMap& validatorIDsMap) const;
492 
494  void convertValidator(
495  const RCP<const ParameterEntryValidator> validator,
496  XMLObject& xmlObj,
497  const ValidatortoIDMap& validatorIDsMap) const;
498 
499  #ifdef HAVE_TEUCHOS_DEBUG
500 
501  RCP<const ParameterEntryValidator> getDummyValidator() const;
502  #endif
503 
505 
506 private:
507 
510 
512  static const std::string& getFileMustExistAttributeName() {
513  static const std::string fileMustExistAttributeName = "fileMustExist";
514  return fileMustExistAttributeName;
515  }
516 
518 
519 };
520 
521 
536 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidatorXMLConverter : public ValidatorXMLConverter
537 {
538 
539 public:
540 
543 
546  const XMLObject& xmlObj,
547  const IDtoValidatorMap& validatorIDsMap) const;
548 
550  void convertValidator(
551  const RCP<const ParameterEntryValidator> validator,
552  XMLObject& xmlObj,
553  const ValidatortoIDMap& validatorIDsMap) const;
554 
555  #ifdef HAVE_TEUCHOS_DEBUG
556 
557  RCP<const ParameterEntryValidator> getDummyValidator() const;
558  #endif
559 
561 
562 private:
563 
566 
568  static const std::string& getStringTagName() {
569  static const std::string stringTagName = "String";
570  return stringTagName;
571  }
572 
574  static const std::string& getStringValueAttributeName() {
575  static const std::string stringValueAttributeName = "value";
576  return stringValueAttributeName;
577  }
578 
580 
581 };
582 
583 template<class ValidatorType, class EntryType>
584 class AbstractArrayValidatorXMLConverter : public ValidatorXMLConverter{
585 public:
586 
589 
592  const XMLObject& xmlObj,
593  const IDtoValidatorMap& validatorIDsMap) const;
594 
596  void convertValidator(
597  const RCP<const ParameterEntryValidator> validator,
598  XMLObject& xmlObj,
599  const ValidatortoIDMap& validatorIDsMap) const;
600 
602 
605 
610  getConcreteValidator(RCP<ValidatorType> prototypeValidator) const = 0;
611 
613 };
614 
615 
616 template<class ValidatorType, class EntryType>
618 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertXML(
619  const XMLObject& xmlObj,
620  const IDtoValidatorMap& validatorIDsMap) const
621 {
622  RCP<ValidatorType> prototypeValidator;
623  if(xmlObj.hasAttribute(
625  {
627  validatorIDsMap.find(
630  if (result != validatorIDsMap.end() ) {
631  prototypeValidator =
632  rcp_dynamic_cast<ValidatorType>(result->second, true);
633  }
634  else {
637  "Could not find prototype validator with id: "
639  getPrototypeIdAttributeName()) << std::endl<< std::endl);
640  }
641  }
642  else {
643  prototypeValidator = rcp_dynamic_cast<ValidatorType>(
645  xmlObj.getChild(0), validatorIDsMap), true);
646  }
647  return getConcreteValidator(prototypeValidator);
648 }
649 
650 template<class ValidatorType, class EntryType>
651 void
652 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertValidator(
653  const RCP<const ParameterEntryValidator> validator,
654  XMLObject& xmlObj,
655  const ValidatortoIDMap& validatorIDsMap) const
656 {
658  rcp_dynamic_cast<const AbstractArrayValidator<ValidatorType, EntryType> >(
659  validator, true);
660  if(validatorIDsMap.find(castedValidator->getPrototype())
661  == validatorIDsMap.end())
662  {
664  castedValidator->getPrototype(), validatorIDsMap, false));
665  }
666  else{
668  validatorIDsMap.find(castedValidator->getPrototype())->second;
669 
671  getPrototypeIdAttributeName(), prototypeID);
672  }
673 }
674 
702 template<class ValidatorType, class EntryType>
704  public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
705 {
708 
709  virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
710  RCP<ValidatorType> prototypeValidator) const
711  {
712  return rcp(new ArrayValidator<ValidatorType, EntryType>(prototypeValidator));
713  }
714 
715 #ifdef HAVE_TEUCHOS_DEBUG
716 
719  RCP<const ParameterEntryValidator> getDummyValidator() const{
721  getDummyObject();
722  }
724 #endif
725 };
726 
754 template<class ValidatorType, class EntryType>
756  public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
757 {
760 
761  virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
762  RCP<ValidatorType> prototypeValidator) const
763  {
764  return rcp(new TwoDArrayValidator<ValidatorType, EntryType>(prototypeValidator));
765  }
766 
768 
769 #ifdef HAVE_TEUCHOS_DEBUG
770 
773  RCP<const ParameterEntryValidator> getDummyValidator() const{
775  getDummyObject();
776  }
778 #endif
779 
780 };
781 
782 
783 
784 } // namespace Teuchos
785 
786 
787 #endif // TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
788 
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.
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.
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.