Separatista
validator.h
1 /***************************************************************************
2 * Copyright (C) 2014 by Okkel Klaver *
3 * info@vanhetland.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifndef SEPARATISTA_VALIDATOR_H
21 #define SEPARATISTA_VALIDATOR_H
22 
23 #include <initializer_list>
24 #include <array>
25 
26 #include "separatista.h"
27 #include "element.h"
28 #include "exception.h"
29 
30 namespace Separatista
31 {
33  class SEPARATISTA_EXTERN InvalidValueException : public Exception
34  {
35  public:
36  InvalidValueException(const wchar_t *pMessage, Element *pSource, const wchar_t *pValue) : Exception(pMessage)
37  {
38  m_pSource = pSource;
39  m_pValue = pValue;
40  };
41 
42 #ifdef SEPARATISTA_DEBUG
43  InvalidValueException(const wchar_t *pMessage, const wchar_t *pPath, int nLine, Element *pSource, const wchar_t *pValue) :
44  Exception(pMessage, pPath, nLine)
45  {
46  m_pSource = pSource;
47  m_pValue = pValue;
48  };
49 #endif
50 
53  {
54  return m_pSource;
55  };
56 
58  const wchar_t* getValue() const
59  {
60  return m_pValue;
61  };
62 
63  private:
64  Element *m_pSource;
65  const wchar_t *m_pValue;
66  };
67 
69  class SEPARATISTA_EXTERN Validator
70  {
71  public:
72  static void validateDecimal(const wchar_t *pValue, Element *pElement);
73 
74  static void validateString(const wchar_t *pValue, Element *pElement);
75 
76  static void validateBoolean(const wchar_t *pValue, Element *pElement);
77 
78  static void validateMinInclusive(const wchar_t *pValue, const wchar_t *pArg, Element *pElement);
79 
80  static void validateFractionDigits(const wchar_t *pValue, const wchar_t *pArg, Element *pElement);
81 
82  static void validateTotalDigits(const wchar_t *pValue, const wchar_t *pArg, Element *pElement);
83 
84  static void validateNumeric(const wchar_t *pValue, Element *pElement);
85 
86  static void validateEnumeration(const wchar_t *pValue, std::initializer_list<const wchar_t*> pPossibleValues, Element *pElement);
87 
88  static void validatePattern(const wchar_t *pValue, const wchar_t *pArg, Element *pElement);
89 
90  static void validateMinLength(const wchar_t *pValue, const wchar_t *pArg, Element *pElement);
91 
92  static void validateMaxLength(const wchar_t *pValue, const wchar_t *pArg, Element *pElement);
93 
94  static void validateDateTime(const wchar_t *pValue, Element *pElement);
95 
96  static void validateDate(const wchar_t *pValue, Element *pElement);
97 
98  template <class T>
99  static void validate(const wchar_t* pSchemaSymbols, const wchar_t *pValue, const wchar_t *pArg, Element *pElement);
100 
101  };
102 
103  template<class T>
104  inline void Validator::validate(const wchar_t * pSchemaSymbols, const wchar_t * pValue, const wchar_t * pArg, Element *pElement)
105  {
106  xercesc::RefHashTableOf<xercesc::KVStringPair>* pFacets = new xercesc::RefHashTableOf<xercesc::KVStringPair>(1, true);
107  pFacets->put(
108  (void*)pSchemaSymbols,
109  new xercesc::KVStringPair(
110  pSchemaSymbols,
111  pArg));
112 
113  T *pValidator = new T(NULL, pFacets, NULL, 0);
114  if (pValidator)
115  {
116  try
117  {
118  pValidator->validate(pValue);
119  }
120  catch (const xercesc::InvalidDatatypeValueException &)
121  {
122  delete pValidator;
123  //delete pFacets;
124  SEPARATISTA_THROW_EXCEPTION(InvalidValueException, TEXT("Invalid value"), pElement, pValue);
125  }
126  }
127  delete pValidator;
128  //delete pFacets;
129  }
130 }
131 
132 #endif // !defined SEPARATISTA_VALIDATOR_H
Definition: exception.h:40
Invalid value exception, thrown when an element can&#39;t accept a value.
Definition: validator.h:33
Validator interface.
Definition: validator.h:69
Definition: mt940s.h:39
const wchar_t * getValue() const
Returns the erronous value.
Definition: validator.h:58
Element * getSourceElement() const
Returns the target element.
Definition: validator.h:52
Definition: element.h:55