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: BooleanDatatypeValidator.cpp 471747 2006-11-06 14:31:56Z amassari $
20 */
21
22 // ---------------------------------------------------------------------------
23 // Includes
24 // ---------------------------------------------------------------------------
25 #include <xercesc/validators/datatype/BooleanDatatypeValidator.hpp>
26 #include <xercesc/validators/schema/SchemaSymbols.hpp>
27 #include <xercesc/validators/datatype/InvalidDatatypeFacetException.hpp>
28 #include <xercesc/validators/datatype/InvalidDatatypeValueException.hpp>
29
30 XERCES_CPP_NAMESPACE_BEGIN
31
32 // ---------------------------------------------------------------------------
33 // Constructors and Destructor
34 // ---------------------------------------------------------------------------
BooleanDatatypeValidator(DatatypeValidator * const baseValidator,RefHashTableOf<KVStringPair> * const facets,RefArrayVectorOf<XMLCh> * const enums,const int finalSet,MemoryManager * const manager)35 BooleanDatatypeValidator::BooleanDatatypeValidator(
36 DatatypeValidator* const baseValidator
37 , RefHashTableOf<KVStringPair>* const facets
38 , RefArrayVectorOf<XMLCh>* const enums
39 , const int finalSet
40 , MemoryManager* const manager)
41 :DatatypeValidator(baseValidator, facets, finalSet, DatatypeValidator::Boolean, manager)
42 {
43
44 // Set Facets if any defined
45 if ( facets )
46 {
47
48 // Boolean shall NOT have enumeration
49 if (enums) {
50 delete enums;
51 ThrowXMLwithMemMgr1(InvalidDatatypeFacetException
52 , XMLExcepts::FACET_Invalid_Tag
53 , "enumeration"
54 , manager);
55 }
56
57 XMLCh* key;
58 XMLCh* value;
59 RefHashTableOfEnumerator<KVStringPair> e(facets, false, manager);
60
61 while (e.hasMoreElements())
62 {
63 KVStringPair pair = e.nextElement();
64 key = pair.getKey();
65 value = pair.getValue();
66
67 if (XMLString::equals(key, SchemaSymbols::fgELT_PATTERN))
68 {
69 setPattern(value);
70 setFacetsDefined(DatatypeValidator::FACET_PATTERN);
71 }
72 else
73 {
74 ThrowXMLwithMemMgr1(InvalidDatatypeFacetException
75 , XMLExcepts::FACET_Invalid_Tag
76 , key
77 , manager);
78 }
79
80 }
81
82 }// End of facet setting
83 }
84
checkContent(const XMLCh * const content,ValidationContext * const context,bool asBase,MemoryManager * const manager)85 void BooleanDatatypeValidator::checkContent( const XMLCh* const content
86 , ValidationContext* const context
87 , bool asBase
88 , MemoryManager* const manager)
89 {
90
91 //validate against base validator if any
92 BooleanDatatypeValidator *pBaseValidator = (BooleanDatatypeValidator*) this->getBaseValidator();
93 if (pBaseValidator !=0)
94 pBaseValidator->checkContent(content, context, true, manager);
95
96 // we check pattern first
97 if ( (getFacetsDefined() & DatatypeValidator::FACET_PATTERN ) != 0 )
98 {
99 if (getRegex()->matches(content, manager) ==false)
100 {
101 ThrowXMLwithMemMgr2(InvalidDatatypeValueException
102 , XMLExcepts::VALUE_NotMatch_Pattern
103 , content
104 , getPattern()
105 , manager);
106 }
107 }
108
109 // if this is a base validator, we only need to check pattern facet
110 // all other facet were inherited by the derived type
111 if (asBase)
112 return;
113
114 unsigned int i = 0;
115 for ( ; i < XMLUni::fgBooleanValueSpaceArraySize; i++ )
116 {
117 if ( XMLString::equals(content, XMLUni::fgBooleanValueSpace[i]))
118 break;
119 }
120
121 if (i == XMLUni::fgBooleanValueSpaceArraySize)
122 ThrowXMLwithMemMgr2(InvalidDatatypeValueException
123 , XMLExcepts::VALUE_Invalid_Name
124 , content
125 , SchemaSymbols::fgDT_BOOLEAN
126 , manager);
127 //Not valid boolean type
128
129 }
130
compare(const XMLCh * const lValue,const XMLCh * const rValue,MemoryManager * const)131 int BooleanDatatypeValidator::compare(const XMLCh* const lValue
132 , const XMLCh* const rValue
133 , MemoryManager* const)
134 {
135 // need to check by bool semantics
136 // 1 == true
137 // 0 == false
138
139 if (XMLString::equals(lValue, XMLUni::fgBooleanValueSpace[0])||
140 XMLString::equals(lValue, XMLUni::fgBooleanValueSpace[2]))
141 {
142 if (XMLString::equals(rValue, XMLUni::fgBooleanValueSpace[0]) ||
143 XMLString::equals(rValue, XMLUni::fgBooleanValueSpace[2]))
144 return 0;
145 }
146 else
147 if (XMLString::equals(lValue, XMLUni::fgBooleanValueSpace[1]) ||
148 XMLString::equals(lValue, XMLUni::fgBooleanValueSpace[3]))
149 {
150 if (XMLString::equals(rValue, XMLUni::fgBooleanValueSpace[1]) ||
151 XMLString::equals(rValue, XMLUni::fgBooleanValueSpace[3]))
152 return 0;
153 }
154
155 return 1;
156 }
157
getEnumString() const158 const RefArrayVectorOf<XMLCh>* BooleanDatatypeValidator::getEnumString() const
159 {
160 return 0;
161 }
162
163 /***
164 * 3.2.2.2 Canonical representation
165 *
166 * The canonical representation for boolean is the set of literals {true, false}.
167 ***/
getCanonicalRepresentation(const XMLCh * const rawData,MemoryManager * const memMgr,bool toValidate) const168 const XMLCh* BooleanDatatypeValidator::getCanonicalRepresentation(const XMLCh* const rawData
169 , MemoryManager* const memMgr
170 , bool toValidate) const
171 {
172
173 MemoryManager* toUse = memMgr? memMgr : getMemoryManager();
174
175 if (toValidate)
176 {
177 BooleanDatatypeValidator *temp = (BooleanDatatypeValidator*) this;
178
179 try
180 {
181 temp->checkContent(rawData, 0, false, toUse);
182 }
183 catch (...)
184 {
185 return 0;
186 }
187 }
188
189 return ( XMLString::equals(rawData, XMLUni::fgBooleanValueSpace[0]) ||
190 XMLString::equals(rawData, XMLUni::fgBooleanValueSpace[2]) ) ?
191 XMLString::replicate(XMLUni::fgBooleanValueSpace[0], toUse) :
192 XMLString::replicate(XMLUni::fgBooleanValueSpace[1], toUse) ;
193
194 }
195
196 /***
197 * Support for Serialization/De-serialization
198 ***/
199
IMPL_XSERIALIZABLE_TOCREATE(BooleanDatatypeValidator)200 IMPL_XSERIALIZABLE_TOCREATE(BooleanDatatypeValidator)
201
202 void BooleanDatatypeValidator::serialize(XSerializeEngine& serEng)
203 {
204 DatatypeValidator::serialize(serEng);
205 }
206
207 XERCES_CPP_NAMESPACE_END
208
209 /**
210 * End of file BooleanDatatypeValidator.cpp
211 */
212
213