1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * $Id: AbstractNumericValidator.cpp 471747 2006-11-06 14:31:56Z amassari $
20  */
21 
22 // ---------------------------------------------------------------------------
23 //  Includes
24 // ---------------------------------------------------------------------------
25 #include <xercesc/validators/datatype/AbstractNumericValidator.hpp>
26 #include <xercesc/validators/datatype/InvalidDatatypeValueException.hpp>
27 #include <xercesc/util/XMLAbstractDoubleFloat.hpp>
28 
29 XERCES_CPP_NAMESPACE_BEGIN
30 
31 #define  REPORT_VALUE_ERROR(val1, val2, except_code, manager)    \
32   ThrowXMLwithMemMgr2(InvalidDatatypeValueException               \
33           , except_code                                 \
34           , val1->getFormattedString()                  \
35           , val2->getFormattedString()                  \
36           , manager);
37 
38 // ---------------------------------------------------------------------------
39 //  Constructors and Destructor
40 // ---------------------------------------------------------------------------
~AbstractNumericValidator()41 AbstractNumericValidator::~AbstractNumericValidator()
42 {}
43 
AbstractNumericValidator(DatatypeValidator * const baseValidator,RefHashTableOf<KVStringPair> * const facets,const int finalSet,const ValidatorType type,MemoryManager * const manager)44 AbstractNumericValidator::AbstractNumericValidator(
45                           DatatypeValidator*            const baseValidator
46                         , RefHashTableOf<KVStringPair>* const facets
47                         , const int                           finalSet
48                         , const ValidatorType                 type
49                         , MemoryManager* const                manager)
50 :AbstractNumericFacetValidator(baseValidator, facets, finalSet, type, manager)
51 {
52     //do not invoke init() here !!!
53 }
54 
validate(const XMLCh * const content,ValidationContext * const context,MemoryManager * const manager)55 void AbstractNumericValidator::validate(const XMLCh*             const content
56                                        ,      ValidationContext* const context
57                                        ,      MemoryManager*     const manager)
58 {
59     checkContent(content, context, false, manager);
60 }
61 
boundsCheck(const XMLNumber * const theData,MemoryManager * const manager)62 void AbstractNumericValidator::boundsCheck(const XMLNumber*         const theData
63                                           ,      MemoryManager*     const manager)
64 {
65     int thisFacetsDefined = getFacetsDefined();
66     int result;
67 
68     if (thisFacetsDefined == 0)
69         return;
70 
71     // must be < MaxExclusive
72     if ( (thisFacetsDefined & DatatypeValidator::FACET_MAXEXCLUSIVE) != 0 )
73     {
74         result = compareValues(theData, getMaxExclusive());
75         if ( result != -1)
76         {
77             REPORT_VALUE_ERROR(theData
78                                  , getMaxExclusive()
79                                  , XMLExcepts::VALUE_exceed_maxExcl
80                                  , manager)
81         }
82     }
83 
84     // must be <= MaxInclusive
85     if ( (thisFacetsDefined & DatatypeValidator::FACET_MAXINCLUSIVE) != 0 )
86     {
87         result = compareValues(theData, getMaxInclusive());
88         if (result == 1)
89         {
90             REPORT_VALUE_ERROR(theData
91                              , getMaxInclusive()
92                              , XMLExcepts::VALUE_exceed_maxIncl
93                              , manager)
94         }
95     }
96 
97     // must be >= MinInclusive
98     if ( (thisFacetsDefined & DatatypeValidator::FACET_MININCLUSIVE) != 0 )
99     {
100         result = compareValues(theData, getMinInclusive());
101         if (result == -1)
102         {
103             REPORT_VALUE_ERROR(theData
104                              , getMinInclusive()
105                              , XMLExcepts::VALUE_exceed_minIncl
106                              , manager)
107         }
108     }
109 
110     // must be > MinExclusive
111     if ( (thisFacetsDefined & DatatypeValidator::FACET_MINEXCLUSIVE) != 0 )
112     {
113         result = compareValues(theData, getMinExclusive());
114         if (result != 1)
115         {
116             REPORT_VALUE_ERROR(theData
117                              , getMinExclusive()
118                              , XMLExcepts::VALUE_exceed_minExcl
119                              , manager)
120         }
121     }
122 }
123 
getCanonicalRepresentation(const XMLCh * const rawData,MemoryManager * const memMgr,bool toValidate) const124 const XMLCh* AbstractNumericValidator::getCanonicalRepresentation(const XMLCh*         const rawData
125                                                                  ,      MemoryManager* const memMgr
126                                                                  ,      bool                 toValidate) const
127 {
128     MemoryManager* toUse = memMgr? memMgr : fMemoryManager;
129 
130     if (toValidate)
131     {
132         AbstractNumericValidator* temp = (AbstractNumericValidator*) this;
133 
134         try
135         {
136             temp->checkContent(rawData, 0, false, toUse);
137         }
138         catch (...)
139         {
140             return 0;
141         }
142     }
143 
144     // XMLAbstractDoubleFloat::getCanonicalRepresentation handles
145     // exceptional cases
146     return XMLAbstractDoubleFloat::getCanonicalRepresentation(rawData, toUse);
147 
148 }
149 
150 /***
151  * Support for Serialization/De-serialization
152  ***/
153 
IMPL_XSERIALIZABLE_NOCREATE(AbstractNumericValidator)154 IMPL_XSERIALIZABLE_NOCREATE(AbstractNumericValidator)
155 
156 void AbstractNumericValidator::serialize(XSerializeEngine& serEng)
157 {
158     AbstractNumericFacetValidator::serialize(serEng);
159 
160     /***
161      * Need not to do anything else here
162      *
163      * Note: its derivatives, Doubledv, Floatdv and Decimaldv writes
164      *       number type info into the binary data stream to be read
165      *       by AbstractNumericFacetVaildator during loading, therefore
166      *       this class can NOT write/read anything into/from the binary
167      *       data stream.
168      *
169      *       Later on, if this class has to write/read something into/from
170      *       the binary data stream, we need to add a numberType data
171      *       to XMLNumber and let AbstractNumericFacetValidator to write/read
172      *       this number type info.
173      ***/
174 }
175 
176 XERCES_CPP_NAMESPACE_END
177 
178 /**
179   * End of file AbstractNumericValidator::cpp
180   */
181 
182