GME  13
SchemaGrammar.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: SchemaGrammar.cpp 883376 2009-11-23 15:45:23Z borisk $
00020  */
00021 
00022 // ---------------------------------------------------------------------------
00023 //  Includes
00024 // ---------------------------------------------------------------------------
00025 #include <xercesc/validators/schema/SchemaGrammar.hpp>
00026 #include <xercesc/validators/schema/ComplexTypeInfo.hpp>
00027 #include <xercesc/validators/schema/SchemaSymbols.hpp>
00028 #include <xercesc/validators/schema/XercesGroupInfo.hpp>
00029 #include <xercesc/validators/schema/XercesAttGroupInfo.hpp>
00030 #include <xercesc/validators/schema/XMLSchemaDescriptionImpl.hpp>
00031 #include <xercesc/util/OutOfMemoryException.hpp>
00032 #include <xercesc/framework/psvi/XSAnnotation.hpp>
00033 
00034 #include <xercesc/internal/XTemplateSerializer.hpp>
00035 #include <xercesc/internal/ValidationContextImpl.hpp>
00036 
00037 XERCES_CPP_NAMESPACE_BEGIN
00038 
00039 typedef JanitorMemFunCall<SchemaGrammar>    CleanupType;
00040 
00041 // ---------------------------------------------------------------------------
00042 //  SchemaGrammar: Constructors and Destructor
00043 // ---------------------------------------------------------------------------
00044 SchemaGrammar::SchemaGrammar(MemoryManager* const manager) :
00045     fTargetNamespace(0)
00046     , fElemDeclPool(0)
00047     , fElemNonDeclPool(0)
00048     , fGroupElemDeclPool(0)
00049     , fNotationDeclPool(0)
00050     , fAttributeDeclRegistry(0)
00051     , fComplexTypeRegistry(0)
00052     , fGroupInfoRegistry(0)
00053     , fAttGroupInfoRegistry(0)
00054     , fValidSubstitutionGroups(0)
00055     , fValidationContext(0)
00056     , fMemoryManager(manager)
00057     , fGramDesc(0)
00058     , fAnnotations(0)
00059     , fValidated(false)
00060     , fDatatypeRegistry(manager)
00061     , fScopeCount (0)
00062     , fAnonTypeCount (0)
00063 {
00064     CleanupType cleanup(this, &SchemaGrammar::cleanUp);
00065 
00066     //
00067     //  Init all the pool members.
00068     //
00069     //  <TBD> Investigate what the optimum values would be for the various
00070     //  pools.
00071     //
00072     fElemDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(109, true, 128, fMemoryManager);
00073 
00074     try {
00075         // should not be necessary now that grammars, once built,
00076         // are read-only
00077         // fElemNonDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(29, true, 128, fMemoryManager);
00078         fGroupElemDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(109, false, 128, fMemoryManager);
00079         fNotationDeclPool = new (fMemoryManager) NameIdPool<XMLNotationDecl>(109, 128, fMemoryManager);
00080         fValidationContext = new (fMemoryManager) ValidationContextImpl(fMemoryManager);
00081 
00082         //REVISIT: use grammarPool to create
00083         fGramDesc = new (fMemoryManager) XMLSchemaDescriptionImpl(XMLUni::fgXMLNSURIName, fMemoryManager);
00084 
00085         // Create annotation table
00086         fAnnotations = new (fMemoryManager) RefHashTableOf<XSAnnotation, PtrHasher>
00087         (
00088             29, true, fMemoryManager
00089         );
00090 
00091         //
00092         //  Call our own reset method. This lets us have the pool setup stuff
00093         //  done in just one place (because this stame setup stuff has to be
00094         //  done every time we are reset.)
00095         //
00096         reset();
00097     }
00098     catch(const OutOfMemoryException&)
00099     {
00100         cleanup.release();
00101 
00102         throw;
00103     }
00104 
00105     cleanup.release();
00106 }
00107 
00108 SchemaGrammar::~SchemaGrammar()
00109 {
00110     cleanUp();
00111 }
00112 
00113 
00114 // -----------------------------------------------------------------------
00115 //  Virtual methods
00116 // -----------------------------------------------------------------------
00117 XMLElementDecl* SchemaGrammar::findOrAddElemDecl (const   unsigned int    uriId
00118         , const XMLCh* const    baseName
00119         , const XMLCh* const    prefixName
00120         , const XMLCh* const    qName
00121         , unsigned int          scope
00122         ,       bool&           wasAdded )
00123 {
00124     // See it it exists
00125     SchemaElementDecl* retVal = (SchemaElementDecl*) getElemDecl(uriId, baseName, qName, scope);
00126 
00127     // if not, then add this in
00128     if (!retVal)
00129     {
00130         retVal = new (fMemoryManager) SchemaElementDecl
00131         (
00132             prefixName
00133             , baseName
00134             , uriId
00135             , SchemaElementDecl::Any
00136             , Grammar::TOP_LEVEL_SCOPE
00137             , fMemoryManager
00138         );
00139         if(!fElemNonDeclPool)
00140             fElemNonDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(29, true, 128, fMemoryManager);
00141         const XMLSize_t elemId = fElemNonDeclPool->put((void*)retVal->getBaseName(), uriId, scope, retVal);
00142         retVal->setId(elemId);
00143         wasAdded = true;
00144     }
00145      else
00146     {
00147         wasAdded = false;
00148     }
00149     return retVal;
00150 }
00151 
00152 XMLElementDecl* SchemaGrammar::putElemDecl (const   unsigned int    uriId
00153         , const XMLCh* const    baseName
00154         , const XMLCh* const    prefixName
00155         , const XMLCh* const
00156         , unsigned int          scope
00157         , const bool            notDeclared)
00158 {
00159     SchemaElementDecl* retVal = new (fMemoryManager) SchemaElementDecl
00160     (
00161         prefixName
00162         , baseName
00163         , uriId
00164         , SchemaElementDecl::Any
00165         , Grammar::TOP_LEVEL_SCOPE
00166         , fMemoryManager
00167     );
00168     if(notDeclared)
00169     {
00170         if(!fElemNonDeclPool)
00171             fElemNonDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(29, true, 128, fMemoryManager);
00172         retVal->setId(fElemNonDeclPool->put((void*)retVal->getBaseName(), uriId, scope, retVal));
00173     } else
00174     {
00175         retVal->setId(fElemDeclPool->put((void*)retVal->getBaseName(), uriId, scope, retVal));
00176     }
00177     return retVal;
00178 }
00179 
00180 void SchemaGrammar::reset()
00181 {
00182     //
00183     //  We need to reset all of the pools.
00184     //
00185     fElemDeclPool->removeAll();
00186     if(fElemNonDeclPool)
00187         fElemNonDeclPool->removeAll();
00188     fGroupElemDeclPool->removeAll();
00189     fNotationDeclPool->removeAll();
00190     fAnnotations->removeAll();
00191     fValidated = false;
00192 }
00193 
00194 
00195 void SchemaGrammar::cleanUp()
00196 {
00197     delete fElemDeclPool;
00198     if(fElemNonDeclPool)
00199         delete fElemNonDeclPool;
00200     delete fGroupElemDeclPool;
00201     delete fNotationDeclPool;
00202     fMemoryManager->deallocate(fTargetNamespace);//delete [] fTargetNamespace;
00203     delete fAttributeDeclRegistry;
00204     delete fComplexTypeRegistry;
00205     delete fGroupInfoRegistry;
00206     delete fAttGroupInfoRegistry;
00207     delete fValidSubstitutionGroups;
00208     delete fValidationContext;
00209     delete fGramDesc;
00210     delete fAnnotations;
00211 }
00212 
00213 void SchemaGrammar::setGrammarDescription(XMLGrammarDescription* gramDesc)
00214 {
00215     if ((!gramDesc) ||
00216         (gramDesc->getGrammarType() != Grammar::SchemaGrammarType))
00217         return;
00218 
00219     if (fGramDesc)
00220         delete fGramDesc;
00221 
00222     //adopt the grammar Description
00223     fGramDesc = (XMLSchemaDescription*) gramDesc;
00224 }
00225 
00226 // ---------------------------------------------------------------------------
00227 //  SchemaGrammar: Helper methods
00228 // ---------------------------------------------------------------------------
00229 void SchemaGrammar::putAnnotation(void* key, XSAnnotation* const annotation)
00230 {
00231     fAnnotations->put(key, annotation);
00232 }
00233 
00234 void SchemaGrammar::addAnnotation(XSAnnotation* const annotation)
00235 {
00236     XSAnnotation* lAnnot = fAnnotations->get(this);
00237 
00238     if (lAnnot)
00239         lAnnot->setNext(annotation);
00240     else
00241         fAnnotations->put(this, annotation);
00242 }
00243 
00244 /***
00245  * Support for Serialization/De-serialization
00246  ***/
00247 
00248 IMPL_XSERIALIZABLE_TOCREATE(SchemaGrammar)
00249 
00250 void SchemaGrammar::serialize(XSerializeEngine& serEng)
00251 {
00252 
00253     /***
00254      * don't serialize ValidationContext* fValidationContext;
00255      *                                    fElemNonDeclPool
00256      ***/
00257 
00258     Grammar::serialize(serEng);
00259 
00260     if (serEng.isStoring())
00261     {
00262         //serialize DatatypeValidatorFactory first
00263         fDatatypeRegistry.serialize(serEng);
00264 
00265         /***
00266          *
00267          * Serialize RefHash3KeysIdPool<SchemaElementDecl>* fElemDeclPool;
00268          * Serialize RefHash3KeysIdPool<SchemaElementDecl>* fGroupElemDeclPool;
00269          *
00270         ***/
00271         XTemplateSerializer::storeObject(fElemDeclPool, serEng);
00272         XTemplateSerializer::storeObject(fGroupElemDeclPool, serEng);
00273 
00274         /***
00275          * Serialize NameIdPool<XMLNotationDecl>*           fNotationDeclPool;
00276          ***/
00277         XTemplateSerializer::storeObject(fNotationDeclPool, serEng);
00278 
00279         /***
00280          *
00281          * Serialize RefHashTableOf<XMLAttDef>*             fAttributeDeclRegistry;
00282          * Serialize RefHashTableOf<ComplexTypeInfo>*       fComplexTypeRegistry;
00283          * Serialize RefHashTableOf<XercesGroupInfo>*       fGroupInfoRegistry;
00284          * Serialize RefHashTableOf<XercesAttGroupInfo>*    fAttGroupInfoRegistry;
00285          * Serialize RefHashTableOf<XMLRefInfo>*            fIDRefList;
00286          *
00287          ***/
00288 
00289         XTemplateSerializer::storeObject(fAttributeDeclRegistry, serEng);
00290         XTemplateSerializer::storeObject(fComplexTypeRegistry, serEng);
00291         XTemplateSerializer::storeObject(fGroupInfoRegistry, serEng);
00292         XTemplateSerializer::storeObject(fAttGroupInfoRegistry, serEng);
00293 
00294         /***
00295          * Serialize RefHash2KeysTableOf<ElemVector>*       fValidSubstitutionGroups;
00296          ***/
00297         XTemplateSerializer::storeObject(fValidSubstitutionGroups, serEng);
00298 
00299         /***
00300          * Serialize RefHashTableOf<XSAnnotation>*       fAnnotations;
00301          ***/
00302         XTemplateSerializer::storeObject(fAnnotations, serEng);
00303 
00304         serEng.writeString(fTargetNamespace);
00305         serEng<<fValidated;
00306 
00307         /***
00308          * serialize() method shall be used to store object
00309          * which has been created in ctor
00310          ***/
00311         fGramDesc->serialize(serEng);
00312 
00313     }
00314     else
00315     {
00316         fDatatypeRegistry.serialize(serEng);
00317 
00318         /***
00319          *
00320          * Deserialize RefHash3KeysIdPool<SchemaElementDecl>* fElemDeclPool;
00321          * Deserialize RefHash3KeysIdPool<SchemaElementDecl>* fGroupElemDeclPool;
00322          *
00323         ***/
00324         XTemplateSerializer::loadObject(&fElemDeclPool, 109, true, 128, serEng);
00325         XTemplateSerializer::loadObject(&fGroupElemDeclPool, 109, true, 128, serEng);
00326 
00327         /***
00328          * Deserialize NameIdPool<XMLNotationDecl>*           fNotationDeclPool;
00329          ***/
00330         XTemplateSerializer::loadObject(&fNotationDeclPool, 109, 128, serEng);
00331 
00332         /***
00333          *
00334          * Deserialize RefHashTableOf<XMLAttDef>*             fAttributeDeclRegistry;
00335          * Deserialize RefHashTableOf<ComplexTypeInfo>*       fComplexTypeRegistry;
00336          * Deserialize RefHashTableOf<XercesGroupInfo>*       fGroupInfoRegistry;
00337          * Deserialize RefHashTableOf<XercesAttGroupInfo>*    fAttGroupInfoRegistry;
00338          * Deserialize RefHashTableOf<XMLRefInfo>*            fIDRefList;
00339          *
00340          ***/
00341 
00342         XTemplateSerializer::loadObject(&fAttributeDeclRegistry, 29, true, serEng);
00343         XTemplateSerializer::loadObject(&fComplexTypeRegistry, 29, true, serEng);
00344         XTemplateSerializer::loadObject(&fGroupInfoRegistry, 13, true, serEng);
00345         XTemplateSerializer::loadObject(&fAttGroupInfoRegistry, 13, true, serEng);
00346 
00347         /***
00348          * Deserialize RefHash2KeysTableOf<ElemVector>*       fValidSubstitutionGroups;
00349          ***/
00350         XTemplateSerializer::loadObject(&fValidSubstitutionGroups, 29, true, serEng);
00351 
00352         /***
00353          * Deserialize RefHashTableOf<XSAnnotation>*       fAnnotations;
00354          ***/
00355         XTemplateSerializer::loadObject(&fAnnotations, 29, true, serEng);
00356 
00357         serEng.readString(fTargetNamespace);
00358         serEng>>fValidated;
00359 
00360         /***
00361          * serialize() method shall be used to load object
00362          * which has been created in ctor
00363          ***/
00364         fGramDesc->serialize(serEng);
00365 
00366     }
00367 }
00368 
00369 XERCES_CPP_NAMESPACE_END