GME  13
DTDGrammar.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: DTDGrammar.cpp 676911 2008-07-15 13:27:32Z amassari $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/util/XMLUniDefs.hpp>
00027 #include <xercesc/util/XMLUni.hpp>
00028 #include <xercesc/util/XMLInitializer.hpp>
00029 #include <xercesc/validators/DTD/DTDGrammar.hpp>
00030 #include <xercesc/validators/DTD/XMLDTDDescriptionImpl.hpp>
00031 
00032 #include <xercesc/internal/XTemplateSerializer.hpp>
00033 
00034 XERCES_CPP_NAMESPACE_BEGIN
00035 
00036 // ---------------------------------------------------------------------------
00037 //  DTDGrammar: Static member data
00038 // ---------------------------------------------------------------------------
00039 NameIdPool<DTDEntityDecl>* DTDGrammar::fDefaultEntities = 0;
00040 
00041 void XMLInitializer::initializeDTDGrammar()
00042 {
00043     DTDGrammar::fDefaultEntities = new NameIdPool<DTDEntityDecl>(11, 12);
00044 
00045     // Add the default entity entries for the character refs that must
00046     // always be present. We indicate that they are from the internal
00047     // subset. They aren't really, but they have to look that way so
00048     // that they are still valid for use within a standalone document.
00049     //
00050     // We also mark them as special char entities, which allows them
00051     // to be used in places whether other non-numeric general entities
00052     // cannot.
00053     //
00054     if (DTDGrammar::fDefaultEntities)
00055     {
00056         DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgAmp, chAmpersand, true, true));
00057         DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgLT, chOpenAngle, true, true));
00058         DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgGT, chCloseAngle, true, true));
00059         DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgQuot, chDoubleQuote, true, true));
00060         DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgApos, chSingleQuote, true, true));
00061     }
00062 }
00063 
00064 void XMLInitializer::terminateDTDGrammar()
00065 {
00066   delete DTDGrammar::fDefaultEntities;
00067   DTDGrammar::fDefaultEntities = 0;
00068 }
00069 
00070 //---------------------------------------------------------------------------
00071 //  DTDGrammar: Constructors and Destructor
00072 // ---------------------------------------------------------------------------
00073 DTDGrammar::DTDGrammar(MemoryManager* const manager) :
00074     fMemoryManager(manager)
00075     , fElemDeclPool(0)
00076     , fElemNonDeclPool(0)
00077     , fEntityDeclPool(0)
00078     , fNotationDeclPool(0)
00079     , fGramDesc(0)
00080     , fValidated(false)
00081 {
00082     //
00083     //  Init all the pool members.
00084     //
00085     //  <TBD> Investigate what the optimum values would be for the various
00086     //  pools.
00087     //
00088     fElemDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(109, 128, fMemoryManager);
00089     // should not need this in the common situation where grammars
00090     // are built once and then read - NG
00091     //fElemNonDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(29, 128, fMemoryManager);
00092     fEntityDeclPool = new (fMemoryManager) NameIdPool<DTDEntityDecl>(109, 128, fMemoryManager);
00093     fNotationDeclPool = new (fMemoryManager) NameIdPool<XMLNotationDecl>(109, 128, fMemoryManager);
00094 
00095     //REVISIT: use grammarPool to create
00096     fGramDesc = new (fMemoryManager) XMLDTDDescriptionImpl(XMLUni::fgDTDEntityString, fMemoryManager);
00097 }
00098 
00099 DTDGrammar::~DTDGrammar()
00100 {
00101     delete fElemDeclPool;
00102     if(fElemNonDeclPool)
00103     {
00104         delete fElemNonDeclPool;
00105     }
00106     delete fEntityDeclPool;
00107     delete fNotationDeclPool;
00108     delete fGramDesc;
00109 }
00110 
00111 // -----------------------------------------------------------------------
00112 //  Virtual methods
00113 // -----------------------------------------------------------------------
00114 XMLElementDecl* DTDGrammar::findOrAddElemDecl (const   unsigned int    uriId
00115         , const XMLCh* const    baseName
00116         , const XMLCh* const
00117         , const XMLCh* const    qName
00118         , unsigned int          scope
00119         ,       bool&           wasAdded )
00120 {
00121     // See it it exists
00122     DTDElementDecl* retVal = (DTDElementDecl*) getElemDecl(uriId, baseName, qName, scope);
00123 
00124     // if not, then add this in
00125     if (!retVal)
00126     {
00127         retVal = new (fMemoryManager) DTDElementDecl
00128         (
00129             qName
00130             , uriId
00131             , DTDElementDecl::Any
00132             , fMemoryManager
00133         );
00134         if(!fElemNonDeclPool)
00135             fElemNonDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(29, 128, fMemoryManager);
00136         const XMLSize_t elemId = fElemNonDeclPool->put(retVal);
00137         retVal->setId(elemId);
00138         wasAdded = true;
00139     }
00140      else
00141     {
00142         wasAdded = false;
00143     }
00144     return retVal;
00145 }
00146 
00147 XMLElementDecl* DTDGrammar::putElemDecl (const   unsigned int    uriId
00148         , const XMLCh* const
00149         , const XMLCh* const
00150         , const XMLCh* const    qName
00151         , unsigned int
00152         , const bool            notDeclared)
00153 {
00154     DTDElementDecl* retVal = new (fMemoryManager) DTDElementDecl
00155     (
00156         qName
00157         , uriId
00158         , DTDElementDecl::Any
00159         , fMemoryManager
00160     );
00161     if(notDeclared)
00162     {
00163         if(!fElemNonDeclPool)
00164             fElemNonDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(29, 128, fMemoryManager);
00165         retVal->setId(fElemNonDeclPool->put(retVal));
00166     } else
00167     {
00168         retVal->setId(fElemDeclPool->put(retVal));
00169     }
00170     return retVal;
00171 }
00172 
00173 void DTDGrammar::reset()
00174 {
00175     //
00176     //  We need to reset all of the pools.
00177     //
00178     fElemDeclPool->removeAll();
00179     // now that we have this, no point in deleting it...
00180     if(fElemNonDeclPool)
00181         fElemNonDeclPool->removeAll();
00182     fNotationDeclPool->removeAll();
00183     fEntityDeclPool->removeAll();
00184     fValidated = false;
00185 }
00186 
00187 void DTDGrammar::setGrammarDescription( XMLGrammarDescription* gramDesc)
00188 {
00189     if ((!gramDesc) ||
00190         (gramDesc->getGrammarType() != Grammar::DTDGrammarType))
00191         return;
00192 
00193     if (fGramDesc)
00194         delete fGramDesc;
00195 
00196     //adopt the grammar Description
00197     fGramDesc = (XMLDTDDescription*) gramDesc;
00198 }
00199 
00200 XMLGrammarDescription*  DTDGrammar::getGrammarDescription() const
00201 {
00202     return fGramDesc;
00203 }
00204 
00205 /***
00206  * Support for Serialization/De-serialization
00207  ***/
00208 
00209 IMPL_XSERIALIZABLE_TOCREATE(DTDGrammar)
00210 
00211 void DTDGrammar::serialize(XSerializeEngine& serEng)
00212 {
00213 
00214     Grammar::serialize(serEng);
00215 
00216     //don't serialize fDefaultEntities
00217 
00218     if (serEng.isStoring())
00219     {
00220         /***
00221          *
00222          * Serialize NameIdPool<DTDElementDecl>*       fElemDeclPool;
00223          * Serialize NameIdPool<DTDEntityDecl>*        fEntityDeclPool;
00224          * Serialize NameIdPool<XMLNotationDecl>*      fNotationDeclPool;
00225          ***/
00226         XTemplateSerializer::storeObject(fElemDeclPool, serEng);
00227         XTemplateSerializer::storeObject(fEntityDeclPool, serEng);
00228         XTemplateSerializer::storeObject(fNotationDeclPool, serEng);
00229 
00230         /***
00231          * serialize() method shall be used to store object
00232          * which has been created in ctor
00233          ***/
00234         fGramDesc->serialize(serEng);
00235 
00236         serEng<<fValidated;
00237     }
00238     else
00239     {
00240 
00241        /***
00242          *
00243          * Deserialize NameIdPool<DTDElementDecl>*       fElemDeclPool;
00244          * Deserialize NameIdPool<DTDEntityDecl>*        fEntityDeclPool;
00245          * Deerialize NameIdPool<XMLNotationDecl>*       fNotationDeclPool;
00246          ***/
00247         XTemplateSerializer::loadObject(&fElemDeclPool, 109, 128, serEng);
00248         fElemNonDeclPool = 0;
00249         XTemplateSerializer::loadObject(&fEntityDeclPool, 109, 128, serEng);
00250         XTemplateSerializer::loadObject(&fNotationDeclPool, 109, 128, serEng);
00251 
00252         /***
00253          * serialize() method shall be used to load object
00254          * which has been created in ctor
00255          ***/
00256         fGramDesc->serialize(serEng);
00257 
00258         serEng>>fValidated;
00259     }
00260 
00261 }
00262 
00263 XERCES_CPP_NAMESPACE_END