source: icXML/icXML-devel/src/xercesc/validators/DTD/DTDGrammar.cpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 8.4 KB
Line 
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: DTDGrammar.cpp 676911 2008-07-15 13:27:32Z amassari $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include <xercesc/util/XMLUniDefs.hpp>
27#include <xercesc/util/XMLUni.hpp>
28#include <xercesc/util/XMLInitializer.hpp>
29#include <xercesc/validators/DTD/DTDGrammar.hpp>
30#include <xercesc/validators/DTD/XMLDTDDescriptionImpl.hpp>
31
32#include <xercesc/internal/XTemplateSerializer.hpp>
33
34XERCES_CPP_NAMESPACE_BEGIN
35
36// ---------------------------------------------------------------------------
37//  DTDGrammar: Static member data
38// ---------------------------------------------------------------------------
39NameIdPool<DTDEntityDecl>* DTDGrammar::fDefaultEntities = 0;
40
41void XMLInitializer::initializeDTDGrammar()
42{
43    DTDGrammar::fDefaultEntities = new NameIdPool<DTDEntityDecl>(11, 12);
44
45    // Add the default entity entries for the character refs that must
46    // always be present. We indicate that they are from the internal
47    // subset. They aren't really, but they have to look that way so
48    // that they are still valid for use within a standalone document.
49    //
50    // We also mark them as special char entities, which allows them
51    // to be used in places whether other non-numeric general entities
52    // cannot.
53    //
54    if (DTDGrammar::fDefaultEntities)
55    {
56        DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgAmp, chAmpersand, true, true));
57        DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgLT, chOpenAngle, true, true));
58        DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgGT, chCloseAngle, true, true));
59        DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgQuot, chDoubleQuote, true, true));
60        DTDGrammar::fDefaultEntities->put(new DTDEntityDecl(XMLUni::fgApos, chSingleQuote, true, true));
61    }
62}
63
64void XMLInitializer::terminateDTDGrammar()
65{
66  delete DTDGrammar::fDefaultEntities;
67  DTDGrammar::fDefaultEntities = 0;
68}
69
70//---------------------------------------------------------------------------
71//  DTDGrammar: Constructors and Destructor
72// ---------------------------------------------------------------------------
73DTDGrammar::DTDGrammar(MemoryManager* const manager) :
74    fMemoryManager(manager)
75    , fElemDeclPool(0)
76    , fElemNonDeclPool(0)
77    , fEntityDeclPool(0)
78    , fNotationDeclPool(0)
79    , fGramDesc(0)
80    , fValidated(false)
81{
82    //
83    //  Init all the pool members.
84    //
85    //  <TBD> Investigate what the optimum values would be for the various
86    //  pools.
87    //
88    fElemDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(109, 128, fMemoryManager);
89    // should not need this in the common situation where grammars
90    // are built once and then read - NG
91    //fElemNonDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(29, 128, fMemoryManager);
92    fEntityDeclPool = new (fMemoryManager) NameIdPool<DTDEntityDecl>(109, 128, fMemoryManager);
93    fNotationDeclPool = new (fMemoryManager) NameIdPool<XMLNotationDecl>(109, 128, fMemoryManager);
94
95    //REVISIT: use grammarPool to create
96    fGramDesc = new (fMemoryManager) XMLDTDDescriptionImpl(XMLUni::fgDTDEntityString, fMemoryManager);
97}
98
99DTDGrammar::~DTDGrammar()
100{
101    delete fElemDeclPool;
102    if(fElemNonDeclPool)
103    {
104        delete fElemNonDeclPool;
105    }
106    delete fEntityDeclPool;
107    delete fNotationDeclPool;
108    delete fGramDesc;
109}
110
111// -----------------------------------------------------------------------
112//  Virtual methods
113// -----------------------------------------------------------------------
114XMLElementDecl* DTDGrammar::findOrAddElemDecl (const   unsigned int    uriId
115        , const XMLCh* const    baseName
116        , const XMLCh* const
117        , const XMLCh* const    qName
118        , unsigned int          scope
119        ,       bool&           wasAdded )
120{
121    // See it it exists
122    DTDElementDecl* retVal = (DTDElementDecl*) getElemDecl(uriId, baseName, qName, scope);
123
124    // if not, then add this in
125    if (!retVal)
126    {
127        retVal = new (fMemoryManager) DTDElementDecl
128        (
129            qName
130            , uriId
131            , DTDElementDecl::Any
132            , fMemoryManager
133        );
134        if(!fElemNonDeclPool)
135            fElemNonDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(29, 128, fMemoryManager);
136        const XMLSize_t elemId = fElemNonDeclPool->put(retVal);
137        retVal->setId(elemId);
138        wasAdded = true;
139    }
140     else
141    {
142        wasAdded = false;
143    }
144    return retVal;
145}
146
147XMLElementDecl* DTDGrammar::putElemDecl (const   unsigned int    uriId
148        , const XMLCh* const
149        , const XMLCh* const
150        , const XMLCh* const    qName
151        , unsigned int
152        , const bool            notDeclared)
153{
154    DTDElementDecl* retVal = new (fMemoryManager) DTDElementDecl
155    (
156        qName
157        , uriId
158        , DTDElementDecl::Any
159        , fMemoryManager
160    );
161    if(notDeclared)
162    {
163        if(!fElemNonDeclPool)
164            fElemNonDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(29, 128, fMemoryManager);
165        retVal->setId(fElemNonDeclPool->put(retVal));
166    } else
167    {
168        retVal->setId(fElemDeclPool->put(retVal));
169    }
170    return retVal;
171}
172
173void DTDGrammar::reset()
174{
175    //
176    //  We need to reset all of the pools.
177    //
178    fElemDeclPool->removeAll();
179    // now that we have this, no point in deleting it...
180    if(fElemNonDeclPool)
181        fElemNonDeclPool->removeAll();
182    fNotationDeclPool->removeAll();
183    fEntityDeclPool->removeAll();
184    fValidated = false;
185}
186
187void DTDGrammar::setGrammarDescription( XMLGrammarDescription* gramDesc)
188{
189    if ((!gramDesc) ||
190        (gramDesc->getGrammarType() != Grammar::DTDGrammarType))
191        return;
192
193    if (fGramDesc)
194        delete fGramDesc;
195
196    //adopt the grammar Description
197    fGramDesc = (XMLDTDDescription*) gramDesc;
198}
199
200XMLGrammarDescription*  DTDGrammar::getGrammarDescription() const
201{
202    return fGramDesc;
203}
204
205/***
206 * Support for Serialization/De-serialization
207 ***/
208
209IMPL_XSERIALIZABLE_TOCREATE(DTDGrammar)
210
211void DTDGrammar::serialize(XSerializeEngine& serEng)
212{
213
214    Grammar::serialize(serEng);
215
216    //don't serialize fDefaultEntities
217
218    if (serEng.isStoring())
219    {
220        /***
221         *
222         * Serialize NameIdPool<DTDElementDecl>*       fElemDeclPool;
223         * Serialize NameIdPool<DTDEntityDecl>*        fEntityDeclPool;
224         * Serialize NameIdPool<XMLNotationDecl>*      fNotationDeclPool;
225         ***/
226        XTemplateSerializer::storeObject(fElemDeclPool, serEng);
227        XTemplateSerializer::storeObject(fEntityDeclPool, serEng);
228        XTemplateSerializer::storeObject(fNotationDeclPool, serEng);
229
230        /***
231         * serialize() method shall be used to store object
232         * which has been created in ctor
233         ***/
234        fGramDesc->serialize(serEng);
235
236        serEng<<fValidated;
237    }
238    else
239    {
240
241       /***
242         *
243         * Deserialize NameIdPool<DTDElementDecl>*       fElemDeclPool;
244         * Deserialize NameIdPool<DTDEntityDecl>*        fEntityDeclPool;
245         * Deerialize NameIdPool<XMLNotationDecl>*       fNotationDeclPool;
246         ***/
247        XTemplateSerializer::loadObject(&fElemDeclPool, 109, 128, serEng);
248        fElemNonDeclPool = 0;
249        XTemplateSerializer::loadObject(&fEntityDeclPool, 109, 128, serEng);
250        XTemplateSerializer::loadObject(&fNotationDeclPool, 109, 128, serEng);
251
252        /***
253         * serialize() method shall be used to load object
254         * which has been created in ctor
255         ***/
256        fGramDesc->serialize(serEng);
257
258        serEng>>fValidated;
259    }
260
261}
262
263XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.