GME  13
DatatypeValidator.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: DatatypeValidator.cpp 555320 2007-07-11 16:05:13Z amassari $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/validators/datatype/DatatypeValidator.hpp>
00027 #include <xercesc/framework/MemoryManager.hpp>
00028 
00029 //since we need to dynamically created each and every derivatives 
00030 //during deserialization by XSerializeEngine>>Derivative, we got
00031 //to include all hpp
00032 
00033 #include <xercesc/validators/datatype/StringDatatypeValidator.hpp>
00034 #include <xercesc/validators/datatype/AnyURIDatatypeValidator.hpp>
00035 #include <xercesc/validators/datatype/QNameDatatypeValidator.hpp>
00036 #include <xercesc/validators/datatype/NameDatatypeValidator.hpp>
00037 #include <xercesc/validators/datatype/NCNameDatatypeValidator.hpp>
00038 #include <xercesc/validators/datatype/BooleanDatatypeValidator.hpp>
00039 #include <xercesc/validators/datatype/FloatDatatypeValidator.hpp>
00040 #include <xercesc/validators/datatype/DoubleDatatypeValidator.hpp>
00041 #include <xercesc/validators/datatype/DecimalDatatypeValidator.hpp>
00042 #include <xercesc/validators/datatype/HexBinaryDatatypeValidator.hpp>
00043 #include <xercesc/validators/datatype/Base64BinaryDatatypeValidator.hpp>
00044 #include <xercesc/validators/datatype/DurationDatatypeValidator.hpp>
00045 #include <xercesc/validators/datatype/DateTimeDatatypeValidator.hpp>
00046 #include <xercesc/validators/datatype/DateDatatypeValidator.hpp>
00047 #include <xercesc/validators/datatype/TimeDatatypeValidator.hpp>
00048 #include <xercesc/validators/datatype/MonthDayDatatypeValidator.hpp>
00049 #include <xercesc/validators/datatype/YearMonthDatatypeValidator.hpp>
00050 #include <xercesc/validators/datatype/YearDatatypeValidator.hpp>
00051 #include <xercesc/validators/datatype/MonthDatatypeValidator.hpp>
00052 #include <xercesc/validators/datatype/DayDatatypeValidator.hpp>
00053 #include <xercesc/validators/datatype/IDDatatypeValidator.hpp>
00054 #include <xercesc/validators/datatype/IDREFDatatypeValidator.hpp>
00055 #include <xercesc/validators/datatype/ENTITYDatatypeValidator.hpp>
00056 #include <xercesc/validators/datatype/NOTATIONDatatypeValidator.hpp>
00057 #include <xercesc/validators/datatype/ListDatatypeValidator.hpp>
00058 #include <xercesc/validators/datatype/UnionDatatypeValidator.hpp>
00059 #include <xercesc/validators/datatype/AnySimpleTypeDatatypeValidator.hpp>
00060 
00061 #include <xercesc/internal/XTemplateSerializer.hpp>
00062 
00063 XERCES_CPP_NAMESPACE_BEGIN
00064 
00065 static const int DV_BUILTIN = -1;
00066 static const int DV_NORMAL  = -2;
00067 static const int DV_ZERO    = -3;
00068 
00069 static const int TYPENAME_ZERO    = -1;
00070 static const int TYPENAME_S4S     = -2;
00071 static const int TYPENAME_NORMAL  = -3;
00072 
00073 // ---------------------------------------------------------------------------
00074 //  DatatypeValidator: Constructors and Destructor
00075 // ---------------------------------------------------------------------------
00076 DatatypeValidator::DatatypeValidator(DatatypeValidator* const baseValidator,
00077                                      RefHashTableOf<KVStringPair>* const facets,
00078                                      const int finalSet,
00079                                      const ValidatorType type,
00080                                      MemoryManager* const manager)
00081     : fAnonymous(false)
00082     , fFinite(false)
00083     , fBounded(false)
00084     , fNumeric(false)
00085     , fWhiteSpace(COLLAPSE)
00086     , fFinalSet(finalSet)
00087     , fFacetsDefined(0)
00088     , fFixed(0)
00089     , fType(type)
00090     , fOrdered(XSSimpleTypeDefinition::ORDERED_FALSE)
00091     , fBaseValidator(baseValidator)
00092     , fFacets(facets)
00093     , fPattern(0)
00094     , fRegex(0)
00095     , fTypeName(0)
00096     , fTypeLocalName(XMLUni::fgZeroLenString)
00097     , fTypeUri(XMLUni::fgZeroLenString)
00098     , fMemoryManager(manager)
00099 {
00100 }
00101 
00102 DatatypeValidator::~DatatypeValidator()
00103 {
00104         cleanUp();
00105 }
00106 
00107 const XMLCh* DatatypeValidator::getWSstring(const short theType) const
00108 {
00109     switch (theType)
00110     {
00111     case PRESERVE:
00112          return SchemaSymbols::fgWS_PRESERVE;
00113     case REPLACE:
00114          return SchemaSymbols::fgWS_REPLACE;
00115     case COLLAPSE:
00116          return SchemaSymbols::fgWS_COLLAPSE;
00117     default:
00118          return SchemaSymbols::fgWS_PRESERVE;
00119     }
00120 
00121 }
00122 
00123 void DatatypeValidator::setTypeName(const XMLCh* const name, const XMLCh* const uri)
00124 {
00125     if (fTypeName) {
00126 
00127         fMemoryManager->deallocate(fTypeName);
00128         fTypeName = 0;
00129     }
00130 
00131     if (name || uri) {
00132 
00133         XMLSize_t nameLen = XMLString::stringLen(name);
00134         XMLSize_t uriLen = XMLString::stringLen(uri);
00135 
00136         fTypeName = (XMLCh*) fMemoryManager->allocate
00137         (
00138             (nameLen + uriLen + 2)*sizeof(XMLCh)
00139         );
00140         fTypeUri = fTypeName;
00141         fTypeLocalName = &fTypeName[uriLen+1];
00142 
00143         if (uri)
00144                         XMLString::moveChars(fTypeName, uri, uriLen+1);
00145         else
00146                         fTypeName[0] = chNull;
00147 
00148         if (name)
00149             XMLString::moveChars(&fTypeName[uriLen+1], name, nameLen+1);
00150         else
00151             fTypeName[uriLen+1] = chNull;
00152     }
00153     else
00154     {
00155         fTypeUri = fTypeLocalName = XMLUni::fgZeroLenString;
00156     }
00157 }
00158 
00159 void DatatypeValidator::setTypeName(const XMLCh* const typeName)
00160 {
00161     if (fTypeName)
00162     {
00163         fMemoryManager->deallocate(fTypeName);
00164         fTypeName = 0;
00165     }
00166 
00167     if (typeName)
00168     {
00169         XMLSize_t nameLen = XMLString::stringLen(typeName);
00170         int commaOffset = XMLString::indexOf(typeName, chComma);
00171 
00172         fTypeName = (XMLCh*) fMemoryManager->allocate
00173         (
00174             (nameLen + 1) * sizeof(XMLCh)
00175         );
00176             XMLString::moveChars(fTypeName, typeName, nameLen+1);
00177 
00178         if ( commaOffset == -1) {
00179             fTypeUri = SchemaSymbols::fgURI_SCHEMAFORSCHEMA;
00180             fTypeLocalName = fTypeName;
00181         }
00182         else {
00183             fTypeUri = fTypeName;
00184             fTypeLocalName = &fTypeName[commaOffset+1];
00185             fTypeName[commaOffset] = chNull;
00186         }
00187     }
00188     else
00189     {
00190         fTypeUri = fTypeLocalName = XMLUni::fgZeroLenString;
00191     }
00192 }
00193 
00194 // ---------------------------------------------------------------------------
00195 //  DatatypeValidator: CleanUp methods
00196 // ---------------------------------------------------------------------------
00197 void DatatypeValidator::cleanUp() {
00198 
00199         delete fFacets;
00200     delete fRegex;
00201     if (fPattern)
00202         fMemoryManager->deallocate(fPattern);//delete [] fPattern;
00203     if (fTypeName)
00204         fMemoryManager->deallocate(fTypeName);
00205 }
00206 
00207 /***
00208  * Support for Serialization/De-serialization
00209  ***/
00210 
00211 IMPL_XSERIALIZABLE_NOCREATE(DatatypeValidator)
00212 
00213 void DatatypeValidator::serialize(XSerializeEngine& serEng)
00214 {
00215 
00216     if (serEng.isStoring())
00217     {
00218         serEng<<fAnonymous;
00219         serEng<<fFinite;
00220         serEng<<fBounded;
00221         serEng<<fNumeric;
00222 
00223         serEng<<fWhiteSpace;
00224         serEng<<fFinalSet;
00225         serEng<<fFacetsDefined;
00226         serEng<<fFixed;
00227 
00228         serEng<<(int)fType;
00229         serEng<<(int)fOrdered;
00230 
00231         storeDV(serEng, fBaseValidator);
00232 
00233         /***
00234          *  Serialize RefHashTableOf<KVStringPair>
00235          ***/
00236         XTemplateSerializer::storeObject(fFacets, serEng);
00237 
00238         serEng.writeString(fPattern);
00239 
00240         if (fTypeUri==XMLUni::fgZeroLenString)
00241         {
00242             serEng<<TYPENAME_ZERO;
00243         }
00244         else if (fTypeUri == SchemaSymbols::fgURI_SCHEMAFORSCHEMA)
00245         {
00246             serEng<<TYPENAME_S4S;
00247             serEng.writeString(fTypeLocalName);
00248         }
00249         else
00250         {        
00251             serEng<<TYPENAME_NORMAL;
00252             serEng.writeString(fTypeLocalName);
00253             serEng.writeString(fTypeUri);
00254         }
00255 
00256         /***
00257          * don't serialize 
00258          *       fRegex
00259          ***/    
00260     }
00261     else
00262     {
00263         serEng>>fAnonymous;
00264         serEng>>fFinite;
00265         serEng>>fBounded;
00266         serEng>>fNumeric;
00267 
00268         serEng>>fWhiteSpace;
00269         serEng>>fFinalSet;
00270         serEng>>fFacetsDefined;
00271         serEng>>fFixed;
00272 
00273         int type;
00274         serEng>>type;
00275         fType=(ValidatorType)type;
00276 
00277         serEng>>type;
00278         fOrdered = (XSSimpleTypeDefinition::ORDERING)type;
00279 
00280 
00281         fBaseValidator = loadDV(serEng);
00282 
00283         /***
00284          *
00285          *  Deserialize RefHashTableOf<KVStringPair>
00286          *
00287          ***/
00288         XTemplateSerializer::loadObject(&fFacets, 29, true, serEng);
00289         serEng.readString(fPattern);       
00290 
00291         /***
00292          *   Recreate through setTypeName()
00293          *       fTypeName
00294          ***/
00295 
00296         int flag;
00297         serEng>>flag;
00298 
00299         if ( TYPENAME_ZERO == flag )
00300         {
00301             setTypeName(0);
00302         }
00303         else if ( TYPENAME_S4S == flag )
00304         {
00305             XMLCh* typeLocalName;
00306             serEng.readString(typeLocalName);
00307             ArrayJanitor<XMLCh> janName(typeLocalName, fMemoryManager);
00308 
00309             setTypeName(typeLocalName);
00310         }
00311         else // TYPENAME_NORMAL
00312         {
00313             XMLCh* typeLocalName;
00314             serEng.readString(typeLocalName);
00315             ArrayJanitor<XMLCh> janName(typeLocalName, fMemoryManager);
00316 
00317             XMLCh* typeUri;
00318             serEng.readString(typeUri);
00319             ArrayJanitor<XMLCh> janUri(typeUri, fMemoryManager);
00320 
00321             setTypeName(typeLocalName, typeUri);
00322         }
00323 
00324         /***
00325          * don't serialize fRegex
00326          ***/
00327         fRegex = new (fMemoryManager) RegularExpression(fPattern, SchemaSymbols::fgRegEx_XOption, fMemoryManager);
00328 
00329     }
00330 
00331 }
00332 
00333 /***
00334  *
00335  *  When deserialized, we need to know, exactly what
00336  *  validator was serialized here.
00337  *
00338  *  Design Issue:
00339  *    
00340  *    This extra type information is only necessary when
00341  *  we need to create and deserialize an DatatypeValidator 
00342  *  derivative by operator >>, but not by object.serialize().
00343  *  Therefore it is appropriate to save this type info by
00344  *  hosting object rather than by derivative.serialize().
00345  *
00346  *
00347  ***/
00348 void DatatypeValidator::storeDV(XSerializeEngine&        serEng
00349                               , DatatypeValidator* const dv)
00350 {
00351     if (dv)
00352     {
00353         //builtIndv
00354         if (dv == DatatypeValidatorFactory::getBuiltInRegistry()->get(dv->getTypeLocalName()))
00355         {
00356             serEng<<DV_BUILTIN;
00357             serEng.writeString(dv->getTypeLocalName());
00358         }
00359         else
00360         {
00361             serEng<<DV_NORMAL;
00362             serEng<<(int) dv->getType();
00363             serEng<<dv;
00364         }
00365     }
00366     else
00367     {
00368         serEng<<DV_ZERO;
00369     }
00370 
00371 }
00372 
00373 DatatypeValidator* DatatypeValidator::loadDV(XSerializeEngine& serEng)
00374 {
00375 
00376     int flag;
00377     serEng>>flag;
00378 
00379     if (DV_BUILTIN == flag)
00380     {
00381         XMLCh* dvName;
00382         serEng.readString(dvName);
00383         ArrayJanitor<XMLCh> janName(dvName, serEng.getMemoryManager());
00384 
00385         return DatatypeValidatorFactory::getBuiltInRegistry()->get(dvName);
00386     }
00387     else if (DV_ZERO == flag)
00388     {
00389         return 0;
00390     }
00391 
00392     int type;
00393     serEng>>type;
00394 
00395     switch((ValidatorType)type)
00396     {
00397     case String: 
00398         StringDatatypeValidator* stringdv;
00399         serEng>>stringdv;
00400         return stringdv;        
00401     case AnyURI:
00402         AnyURIDatatypeValidator* anyuridv;
00403         serEng>>anyuridv;
00404         return anyuridv;        
00405     case QName: 
00406         QNameDatatypeValidator* qnamedv;
00407         serEng>>qnamedv;
00408         return qnamedv;        
00409     case Name: 
00410         NameDatatypeValidator* namedv;
00411         serEng>>namedv;
00412         return namedv;        
00413     case NCName:  
00414         NCNameDatatypeValidator* ncnamedv;
00415         serEng>>ncnamedv;
00416         return ncnamedv;        
00417     case Boolean: 
00418         BooleanDatatypeValidator* booleandv;
00419         serEng>>booleandv;
00420         return booleandv;        
00421     case Float: 
00422         FloatDatatypeValidator* floatdv;
00423         serEng>>floatdv;
00424         return floatdv;        
00425     case Double: 
00426         DoubleDatatypeValidator* doubledv;
00427         serEng>>doubledv;
00428         return doubledv;        
00429     case Decimal: 
00430         DecimalDatatypeValidator* decimaldv;
00431         serEng>>decimaldv;
00432         return decimaldv;        
00433     case HexBinary:  
00434         HexBinaryDatatypeValidator* hexbinarydv;
00435         serEng>>hexbinarydv;
00436         return hexbinarydv;       
00437     case Base64Binary: 
00438         Base64BinaryDatatypeValidator* base64binarydv;
00439         serEng>>base64binarydv;
00440         return base64binarydv;      
00441     case Duration:     
00442         DurationDatatypeValidator* durationdv;
00443         serEng>>durationdv;
00444         return durationdv;
00445     case DateTime:       
00446         DateTimeDatatypeValidator* datetimedv;
00447         serEng>>datetimedv;
00448         return datetimedv; 
00449     case Date:          
00450         DateDatatypeValidator* datedv;
00451         serEng>>datedv;
00452         return datedv;
00453     case Time:         
00454         TimeDatatypeValidator* timedv;
00455         serEng>>timedv;
00456         return timedv;
00457     case MonthDay:      
00458         MonthDayDatatypeValidator* monthdaydv;
00459         serEng>>monthdaydv;
00460         return monthdaydv;
00461     case YearMonth:     
00462         YearMonthDatatypeValidator* yearmonthdv;
00463         serEng>>yearmonthdv;
00464         return yearmonthdv;
00465     case Year:          
00466         YearDatatypeValidator* yeardv;
00467         serEng>>yeardv;
00468         return yeardv;
00469     case Month:        
00470         MonthDatatypeValidator* monthdv;
00471         serEng>>monthdv;
00472         return monthdv;
00473     case Day:           
00474         DayDatatypeValidator* daydv;
00475         serEng>>daydv;
00476         return daydv;
00477     case ID:           
00478         IDDatatypeValidator* iddv;
00479         serEng>>iddv;
00480         return iddv;
00481     case IDREF:         
00482         IDREFDatatypeValidator* idrefdv;
00483         serEng>>idrefdv;
00484         return idrefdv;
00485     case ENTITY:       
00486         ENTITYDatatypeValidator* entitydv;
00487         serEng>>entitydv;
00488         return entitydv;
00489     case NOTATION:     
00490         NOTATIONDatatypeValidator* notationdv;
00491         serEng>>notationdv;
00492         return notationdv;
00493     case List:          
00494         ListDatatypeValidator* listdv;
00495         serEng>>listdv;
00496         return listdv;
00497     case Union:         
00498         UnionDatatypeValidator* uniondv;
00499         serEng>>uniondv;
00500         return uniondv;
00501     case AnySimpleType:  
00502         AnySimpleTypeDatatypeValidator* anysimpletypedv;
00503         serEng>>anysimpletypedv;
00504         return anysimpletypedv;
00505     case UnKnown:
00506         return 0;
00507     default: //we treat this same as UnKnown
00508         return 0;
00509     }
00510 
00511 }
00512 
00517 const XMLCh* DatatypeValidator::getCanonicalRepresentation(const XMLCh*         const rawData
00518                                                           ,      MemoryManager* const memMgr
00519                                                           ,      bool                 toValidate) const
00520 {
00521     MemoryManager* toUse = memMgr? memMgr : fMemoryManager;
00522 
00523     if (toValidate)
00524     {
00525         DatatypeValidator *temp = (DatatypeValidator*) this;
00526 
00527         try
00528         {
00529             temp->validate(rawData, 0, toUse);    
00530         }
00531         catch (...)
00532         {
00533             return 0;
00534         }
00535     }
00536 
00537     return XMLString::replicate(rawData, toUse);
00538 }
00539 
00540 
00541 XERCES_CPP_NAMESPACE_END
00542