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 //
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 
280 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT BoolValidatorXMLConverter : public ValidatorXMLConverter
281 {
282 
283 public:
284 
287 
289  RCP<ParameterEntryValidator> convertXML(
290  const XMLObject& xmlObj,
291  const IDtoValidatorMap& validatorIDsMap) const;
292 
294  void convertValidator(
295  const RCP<const ParameterEntryValidator> validator,
296  XMLObject& xmlObj,
297  const ValidatortoIDMap& validatorIDsMap) const;
298 
299  #ifdef HAVE_TEUCHOS_DEBUG
300 
301  RCP<const ParameterEntryValidator> getDummyValidator() const;
302  #endif
303 
305 
306 private:
307 
310 
311  // currently empty
312 
314 
315 };
316 
330 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT AnyNumberValidatorXMLConverter : public ValidatorXMLConverter
331 {
332 
333 public:
334 
337 
339  RCP<ParameterEntryValidator> convertXML(
340  const XMLObject& xmlObj,
341  const IDtoValidatorMap& validatorIDsMap) const;
342 
344  void convertValidator(
345  const RCP<const ParameterEntryValidator> validator,
346  XMLObject& xmlObj,
347  const ValidatortoIDMap& validatorIDsMap) const;
348 
349  #ifdef HAVE_TEUCHOS_DEBUG
350 
351  RCP<const ParameterEntryValidator> getDummyValidator() const;
352  #endif
353 
355 
356 private:
357 
360 
362  static const std::string& getAllowIntAttributeName() {
363  static const std::string allowIntAttributeName_ = "allowInt";
364  return allowIntAttributeName_;
365  }
366 
368  static const std::string& getAllowDoubleAttributeName() {
369  static const std::string allowDoubleAttributeName_ = "allowDouble";
370  return allowDoubleAttributeName_;
371  }
372 
374  static const std::string& getAllowStringAttributeName() {
375  static const std::string allowStringAttributeName_ = "allowString";
376  return allowStringAttributeName_;
377  }
378 
380  static const std::string& getPrefferedTypeAttributeName() {
381  static const std::string prefferedTypeAttributeName_ = "prefferedType";
382  return prefferedTypeAttributeName_;
383  }
384 
386 
387 };
388 
389 
404 template<class T>
406 {
407 
408 public:
409 
412 
415  const XMLObject& xmlObj,
416  const IDtoValidatorMap& validatorIDsMap) const;
417 
419  void convertValidator(
420  const RCP<const ParameterEntryValidator> validator,
421  XMLObject& xmlObj,
422  const ValidatortoIDMap& validatorIDsMap) const;
423 
424 #ifdef HAVE_TEUCHOS_DEBUG
425 
426  RCP<const ParameterEntryValidator> getDummyValidator() const{
427  return DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject();
428  }
429 #endif
430 
432 
433 private:
434 
437 
439  static const std::string& getMinAttributeName() {
440  static const std::string minAttributeName = "min";
441  return minAttributeName;
442  }
443 
445  static const std::string& getMaxAttributeName() {
446  static const std::string maxAttributeName = "max";
447  return maxAttributeName;
448  }
449 
451  static const std::string& getStepAttributeName() {
452  static const std::string stepAttributeName = "step";
453  return stepAttributeName;
454  }
455 
457  static const std::string& getPrecisionAttributeName() {
458  static const std::string precisionAttributeName = "precision";
459  return precisionAttributeName;
460  }
461 
463 
464 };
465 
466 
467 template<class T>
470  const XMLObject& xmlObj,
471  const IDtoValidatorMap& /*validatorIDsMap*/) const
472 {
473  RCP<EnhancedNumberValidator<T> > toReturn =
475  T step = xmlObj.getWithDefault(
476  getStepAttributeName(), EnhancedNumberTraits<T>::defaultStep());
477  toReturn->setStep(step);
478  unsigned short int precision = xmlObj.getWithDefault(
479  getPrecisionAttributeName(),
481  toReturn->setPrecision(precision);
482  if (xmlObj.hasAttribute(getMinAttributeName())) {
483  toReturn->setMin(xmlObj.getRequired<T>(getMinAttributeName()));
484  }
485  if (xmlObj.hasAttribute(getMaxAttributeName())) {
486  toReturn->setMax(xmlObj.getRequired<T>(getMaxAttributeName()));
487  }
488  return toReturn;
489 }
490 
491 
492 template<class T>
494  const RCP<const ParameterEntryValidator > validator,
495  XMLObject& xmlObj,
496  const ValidatortoIDMap& /*validatorIDsMap*/) const
497 {
498  RCP<const EnhancedNumberValidator<T> > castedValidator =
499  rcp_dynamic_cast<const EnhancedNumberValidator<T> >(validator, true);
500  if (castedValidator->hasMin()) {
501  xmlObj.addAttribute<T>(getMinAttributeName(), castedValidator->getMin());
502  }
503  if (castedValidator->hasMax()) {
504  xmlObj.addAttribute<T>(getMaxAttributeName(), castedValidator->getMax());
505  }
506  xmlObj.addAttribute<T>(getStepAttributeName(), castedValidator->getStep());
507  xmlObj.addAttribute<short unsigned int>(
508  getPrecisionAttributeName(), castedValidator->getPrecision());
509 }
510 
511 
526 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidatorXMLConverter : public ValidatorXMLConverter
527 {
528 
529 public:
530 
533 
535  RCP<ParameterEntryValidator> convertXML(
536  const XMLObject& xmlObj,
537  const IDtoValidatorMap& validatorIDsMap) const;
538 
540  void convertValidator(
541  const RCP<const ParameterEntryValidator> validator,
542  XMLObject& xmlObj,
543  const ValidatortoIDMap& validatorIDsMap) const;
544 
545  #ifdef HAVE_TEUCHOS_DEBUG
546 
547  RCP<const ParameterEntryValidator> getDummyValidator() const;
548  #endif
549 
551 
552 private:
553 
556 
558  static const std::string& getFileMustExistAttributeName() {
559  static const std::string fileMustExistAttributeName = "fileMustExist";
560  return fileMustExistAttributeName;
561  }
562 
564 
565 };
566 
567 
582 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidatorXMLConverter : public ValidatorXMLConverter
583 {
584 
585 public:
586 
589 
591  RCP<ParameterEntryValidator> convertXML(
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 
601  #ifdef HAVE_TEUCHOS_DEBUG
602 
603  RCP<const ParameterEntryValidator> getDummyValidator() const;
604  #endif
605 
607 
608 private:
609 
612 
614  static const std::string& getStringTagName() {
615  static const std::string stringTagName = "String";
616  return stringTagName;
617  }
618 
620  static const std::string& getStringValueAttributeName() {
621  static const std::string stringValueAttributeName = "value";
622  return stringValueAttributeName;
623  }
624 
626 
627 };
628 
629 template<class ValidatorType, class EntryType>
630 class AbstractArrayValidatorXMLConverter : public ValidatorXMLConverter{
631 public:
632 
635 
637  RCP<ParameterEntryValidator> convertXML(
638  const XMLObject& xmlObj,
639  const IDtoValidatorMap& validatorIDsMap) const;
640 
642  void convertValidator(
643  const RCP<const ParameterEntryValidator> validator,
644  XMLObject& xmlObj,
645  const ValidatortoIDMap& validatorIDsMap) const;
646 
648 
651 
656  getConcreteValidator(RCP<ValidatorType> prototypeValidator) const = 0;
657 
659 };
660 
661 
662 template<class ValidatorType, class EntryType>
663 RCP<ParameterEntryValidator>
664 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertXML(
665  const XMLObject& xmlObj,
666  const IDtoValidatorMap& validatorIDsMap) const
667 {
668  RCP<ValidatorType> prototypeValidator;
669  if(xmlObj.hasAttribute(
671  {
673  validatorIDsMap.find(
674  xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
675  getPrototypeIdAttributeName()));
676  if (result != validatorIDsMap.end() ) {
677  prototypeValidator =
678  rcp_dynamic_cast<ValidatorType>(result->second, true);
679  }
680  else {
682  MissingValidatorDefinitionException,
683  "Could not find prototype validator with id: "
684  << xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
685  getPrototypeIdAttributeName()) << std::endl<< std::endl);
686  }
687  }
688  else {
689  prototypeValidator = rcp_dynamic_cast<ValidatorType>(
691  xmlObj.getChild(0), validatorIDsMap), true);
692  }
693  return getConcreteValidator(prototypeValidator);
694 }
695 
696 template<class ValidatorType, class EntryType>
697 void
698 AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertValidator(
699  const RCP<const ParameterEntryValidator> validator,
700  XMLObject& xmlObj,
701  const ValidatortoIDMap& validatorIDsMap) const
702 {
703  RCP<const AbstractArrayValidator<ValidatorType, EntryType> > castedValidator =
704  rcp_dynamic_cast<const AbstractArrayValidator<ValidatorType, EntryType> >(
705  validator, true);
706  if(validatorIDsMap.find(castedValidator->getPrototype())
707  == validatorIDsMap.end())
708  {
710  castedValidator->getPrototype(), validatorIDsMap, false));
711  }
712  else{
714  validatorIDsMap.find(castedValidator->getPrototype())->second;
715 
716  xmlObj.addAttribute<ParameterEntryValidator::ValidatorID>(
717  getPrototypeIdAttributeName(), prototypeID);
718  }
719 }
720 
748 template<class ValidatorType, class EntryType>
750  public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
751 {
754 
755  virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
756  RCP<ValidatorType> prototypeValidator) const
757  {
758  return rcp(new ArrayValidator<ValidatorType, EntryType>(prototypeValidator));
759  }
760 
761 #ifdef HAVE_TEUCHOS_DEBUG
762 
765  RCP<const ParameterEntryValidator> getDummyValidator() const{
767  getDummyObject();
768  }
770 #endif
771 };
772 
800 template<class ValidatorType, class EntryType>
802  public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
803 {
806 
807  virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
808  RCP<ValidatorType> prototypeValidator) const
809  {
810  return rcp(new TwoDArrayValidator<ValidatorType, EntryType>(prototypeValidator));
811  }
812 
814 
815 #ifdef HAVE_TEUCHOS_DEBUG
816 
819  RCP<const ParameterEntryValidator> getDummyValidator() const{
821  getDummyObject();
822  }
824 #endif
825 
826 };
827 
828 
829 
830 } // namespace Teuchos
831 
832 
833 #endif // TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
834 
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.