GME  13
BooleanDatatypeValidator.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: BooleanDatatypeValidator.cpp 471747 2006-11-06 14:31:56Z amassari $
00020  */
00021 
00022 // ---------------------------------------------------------------------------
00023 //  Includes
00024 // ---------------------------------------------------------------------------
00025 #include <xercesc/validators/datatype/BooleanDatatypeValidator.hpp>
00026 #include <xercesc/validators/schema/SchemaSymbols.hpp>
00027 #include <xercesc/validators/datatype/InvalidDatatypeFacetException.hpp>
00028 #include <xercesc/validators/datatype/InvalidDatatypeValueException.hpp>
00029 
00030 XERCES_CPP_NAMESPACE_BEGIN
00031 
00032 // ---------------------------------------------------------------------------
00033 //  Constructors and Destructor
00034 // ---------------------------------------------------------------------------
00035 BooleanDatatypeValidator::BooleanDatatypeValidator(
00036                           DatatypeValidator*            const baseValidator
00037                         , RefHashTableOf<KVStringPair>* const facets
00038                         , RefArrayVectorOf<XMLCh>*      const enums
00039                         , const int                           finalSet
00040                         , MemoryManager* const                manager)
00041 :DatatypeValidator(baseValidator, facets, finalSet, DatatypeValidator::Boolean, manager)
00042 {
00043 
00044     // Set Facets if any defined
00045     if ( facets )
00046     {
00047 
00048         // Boolean shall NOT have enumeration
00049         if (enums) {
00050             delete enums;
00051             ThrowXMLwithMemMgr1(InvalidDatatypeFacetException
00052                     , XMLExcepts::FACET_Invalid_Tag
00053                     , "enumeration"
00054                     , manager);
00055         }
00056 
00057         XMLCh* key;
00058         XMLCh* value;
00059         RefHashTableOfEnumerator<KVStringPair> e(facets, false, manager);
00060 
00061         while (e.hasMoreElements())
00062         {
00063             KVStringPair pair = e.nextElement();
00064             key = pair.getKey();
00065             value = pair.getValue();
00066 
00067             if (XMLString::equals(key, SchemaSymbols::fgELT_PATTERN))
00068             {
00069                 setPattern(value);
00070                 setFacetsDefined(DatatypeValidator::FACET_PATTERN);
00071             }
00072             else
00073             {
00074                 ThrowXMLwithMemMgr1(InvalidDatatypeFacetException
00075                         , XMLExcepts::FACET_Invalid_Tag
00076                         , key
00077                         , manager);
00078             }
00079 
00080         }
00081 
00082     }// End of facet setting
00083 }
00084 
00085 void BooleanDatatypeValidator::checkContent( const XMLCh*             const content
00086                                            ,       ValidationContext* const context
00087                                            ,       bool                     asBase
00088                                            ,       MemoryManager*     const manager)
00089 {
00090 
00091     //validate against base validator if any
00092     BooleanDatatypeValidator *pBaseValidator = (BooleanDatatypeValidator*) this->getBaseValidator();
00093     if (pBaseValidator !=0)
00094         pBaseValidator->checkContent(content, context, true, manager);
00095 
00096     // we check pattern first
00097     if ( (getFacetsDefined() & DatatypeValidator::FACET_PATTERN ) != 0 )
00098     {
00099         if (getRegex()->matches(content, manager) ==false)
00100         {
00101             ThrowXMLwithMemMgr2(InvalidDatatypeValueException
00102                     , XMLExcepts::VALUE_NotMatch_Pattern
00103                     , content
00104                     , getPattern()
00105                     , manager);
00106         }
00107     }
00108 
00109     // if this is a base validator, we only need to check pattern facet
00110     // all other facet were inherited by the derived type
00111     if (asBase)
00112         return;
00113 
00114     unsigned int   i = 0;
00115     for ( ; i < XMLUni::fgBooleanValueSpaceArraySize; i++ )
00116     {
00117         if ( XMLString::equals(content, XMLUni::fgBooleanValueSpace[i]))
00118             break;
00119     }
00120 
00121     if (i == XMLUni::fgBooleanValueSpaceArraySize)
00122         ThrowXMLwithMemMgr2(InvalidDatatypeValueException
00123                            , XMLExcepts::VALUE_Invalid_Name
00124                            , content
00125                            , SchemaSymbols::fgDT_BOOLEAN
00126                            , manager);
00127         //Not valid boolean type
00128 
00129 }
00130 
00131 int BooleanDatatypeValidator::compare(const XMLCh* const lValue
00132                                     , const XMLCh* const rValue
00133                                     , MemoryManager* const)
00134 {
00135     // need to check by bool semantics
00136     // 1 == true
00137     // 0 == false
00138 
00139     if (XMLString::equals(lValue, XMLUni::fgBooleanValueSpace[0])||
00140         XMLString::equals(lValue, XMLUni::fgBooleanValueSpace[2]))
00141     {
00142         if (XMLString::equals(rValue, XMLUni::fgBooleanValueSpace[0]) ||
00143             XMLString::equals(rValue, XMLUni::fgBooleanValueSpace[2]))
00144             return 0;
00145     }
00146     else
00147     if (XMLString::equals(lValue, XMLUni::fgBooleanValueSpace[1]) ||
00148         XMLString::equals(lValue, XMLUni::fgBooleanValueSpace[3]))
00149     {
00150         if (XMLString::equals(rValue, XMLUni::fgBooleanValueSpace[1]) ||
00151             XMLString::equals(rValue, XMLUni::fgBooleanValueSpace[3]))
00152             return 0;
00153     }
00154 
00155     return 1;
00156 }
00157 
00158 const RefArrayVectorOf<XMLCh>* BooleanDatatypeValidator::getEnumString() const
00159 {
00160         return 0;
00161 }
00162 
00163 /***
00164  * 3.2.2.2 Canonical representation
00165  *
00166  * The canonical representation for boolean is the set of literals {true, false}.
00167  ***/
00168 const XMLCh* BooleanDatatypeValidator::getCanonicalRepresentation(const XMLCh*         const rawData
00169                                                                 ,       MemoryManager* const memMgr
00170                                                                 ,       bool           toValidate) const
00171 {
00172 
00173     MemoryManager* toUse = memMgr? memMgr : getMemoryManager();
00174     
00175     if (toValidate)
00176     {
00177         BooleanDatatypeValidator *temp = (BooleanDatatypeValidator*) this;
00178 
00179         try
00180         {
00181             temp->checkContent(rawData, 0, false, toUse);   
00182         }
00183         catch (...)
00184         {
00185             return 0;
00186         }
00187     }
00188 
00189     return ( XMLString::equals(rawData, XMLUni::fgBooleanValueSpace[0]) ||
00190              XMLString::equals(rawData, XMLUni::fgBooleanValueSpace[2])  ) ?
00191              XMLString::replicate(XMLUni::fgBooleanValueSpace[0], toUse) :
00192              XMLString::replicate(XMLUni::fgBooleanValueSpace[1], toUse) ;
00193 
00194 }
00195 
00196 /***
00197  * Support for Serialization/De-serialization
00198  ***/
00199 
00200 IMPL_XSERIALIZABLE_TOCREATE(BooleanDatatypeValidator)
00201 
00202 void BooleanDatatypeValidator::serialize(XSerializeEngine& serEng)
00203 {
00204     DatatypeValidator::serialize(serEng);
00205 }
00206 
00207 XERCES_CPP_NAMESPACE_END
00208