source: NonGTP/Xerces/xerces/include/xercesc/validators/DTD/DTDGrammar.hpp @ 358

Revision 358, 16.6 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2001,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * $Log: DTDGrammar.hpp,v $
19 * Revision 1.15  2004/09/08 13:56:50  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.14  2004/01/29 11:52:30  cargilld
23 * Code cleanup changes to get rid of various compiler diagnostic messages.
24 *
25 * Revision 1.13  2003/12/22 15:22:58  gareth
26 * made getRootElemID const. Bug #25699
27 *
28 * Revision 1.12  2003/12/17 00:18:40  cargilld
29 * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
30 *
31 * Revision 1.11  2003/10/14 15:20:42  peiyongz
32 * Implementation of Serialization/Deserialization
33 *
34 * Revision 1.10  2003/09/22 19:49:02  neilg
35 * implement change to Grammar::putElem(XMLElementDecl, bool).  If Grammars are used only to hold declared objects, there will be no need for the fElemNonDeclPool tables; make Grammar implementations lazily create them only if the application requires them (which good cpplications should not.)
36 *
37 * Revision 1.9  2003/08/14 03:00:46  knoaman
38 * Code refactoring to improve performance of validation.
39 *
40 * Revision 1.8  2003/07/31 17:09:59  peiyongz
41 * Grammar embed grammar description
42 *
43 * Revision 1.7  2003/05/16 21:43:19  knoaman
44 * Memory manager implementation: Modify constructors to pass in the memory manager.
45 *
46 * Revision 1.6  2003/05/15 18:54:50  knoaman
47 * Partial implementation of the configurable memory manager.
48 *
49 * Revision 1.5  2002/11/04 14:50:40  tng
50 * C++ Namespace Support.
51 *
52 * Revision 1.4  2002/08/22 15:05:39  tng
53 * Remove unused parameter variables in inline functions.
54 *
55 * Revision 1.3  2002/07/11 18:19:28  knoaman
56 * Grammar caching/preparsing - initial implementation.
57 *
58 * Revision 1.2  2002/07/05 17:08:10  tng
59 * [Bug 10119] Grammar::getGrammarType need a const modifier
60 *
61 * Revision 1.1.1.1  2002/02/01 22:22:43  peiyongz
62 * sane_include
63 *
64 * Revision 1.5  2001/09/14 14:50:22  tng
65 * Schema: Fix some wildcard bugs, and some retrieving qualified/unqualified element decl problems.
66 *
67 * Revision 1.4  2001/05/28 20:56:17  tng
68 * Schema: Move getTargetNamespace as virtual function in base class Grammar
69 *
70 * Revision 1.3  2001/05/11 13:27:09  tng
71 * Copyright update.
72 *
73 * Revision 1.2  2001/04/19 18:17:21  tng
74 * Schema: SchemaValidator update, and use QName in Content Model
75 *
76 * Revision 1.1  2001/03/21 21:56:20  tng
77 * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
78 *
79 */
80
81
82
83#if !defined(DTDGRAMMAR_HPP)
84#define DTDGRAMMAR_HPP
85
86#include <xercesc/util/RefHashTableOf.hpp>
87#include <xercesc/util/NameIdPool.hpp>
88#include <xercesc/util/StringPool.hpp>
89#include <xercesc/validators/common/Grammar.hpp>
90#include <xercesc/validators/DTD/DTDElementDecl.hpp>
91#include <xercesc/validators/DTD/DTDEntityDecl.hpp>
92#include <xercesc/framework/XMLDTDDescription.hpp>
93
94
95XERCES_CPP_NAMESPACE_BEGIN
96
97//
98// This class stores the DTD information
99//  NOTE: DTDs are not namespace aware, so we just use regular NameIdPool
100//  data structures to store element and attribute decls. They are all set
101//  to be in the global namespace and the full QName is used as the base name
102//  of the decl. This means that all the URI parameters below are expected
103//  to be null pointers (and anything else will cause an exception.)
104//
105
106class VALIDATORS_EXPORT DTDGrammar : public Grammar
107{
108public:
109    // -----------------------------------------------------------------------
110    //  Constructors and Destructor
111    // -----------------------------------------------------------------------
112    DTDGrammar(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
113    virtual ~DTDGrammar();
114
115    // -----------------------------------------------------------------------
116    //  Implementation of Virtual Interface
117    // -----------------------------------------------------------------------
118    virtual Grammar::GrammarType getGrammarType() const;
119    virtual const XMLCh* getTargetNamespace() const;
120
121    // this method should only be used while the grammar is being
122    // constructed, not while it is being used
123    // in a validation episode!
124    virtual XMLElementDecl* findOrAddElemDecl
125    (
126        const   unsigned int    uriId
127        , const XMLCh* const    baseName
128        , const XMLCh* const    prefixName
129        , const XMLCh* const    qName
130        , unsigned int          scope
131        ,       bool&           wasAdded
132    ) ;
133
134    virtual unsigned int getElemId
135    (
136        const   unsigned int    uriId
137        , const XMLCh* const    baseName
138        , const XMLCh* const    qName
139        , unsigned int          scope
140    )   const ;
141
142    virtual const XMLElementDecl* getElemDecl
143    (
144        const   unsigned int    uriId
145        , const XMLCh* const    baseName
146        , const XMLCh* const    qName
147        , unsigned int          scope
148    )   const ;
149
150    virtual XMLElementDecl* getElemDecl
151    (
152        const   unsigned int    uriId
153        , const XMLCh* const    baseName
154        , const XMLCh* const    qName
155        , unsigned int          scope
156    );
157
158    virtual const XMLElementDecl* getElemDecl
159    (
160        const   unsigned int    elemId
161    )   const;
162
163    virtual XMLElementDecl* getElemDecl
164    (
165        const   unsigned int    elemId
166    );
167
168    virtual const XMLNotationDecl* getNotationDecl
169    (
170        const   XMLCh* const    notName
171    )   const;
172
173    virtual XMLNotationDecl* getNotationDecl
174    (
175        const   XMLCh* const    notName
176    );
177
178    virtual bool getValidated() const;
179
180    virtual XMLElementDecl* putElemDecl
181    (
182        const   unsigned int    uriId
183        , const XMLCh* const    baseName
184        , const XMLCh* const    prefixName
185        , const XMLCh* const    qName
186        , unsigned int          scope
187        , const bool            notDeclared = false
188    );
189
190    virtual unsigned int putElemDecl
191    (
192        XMLElementDecl* const elemDecl
193        , const bool          notDeclared = false
194    )   ;
195
196    virtual unsigned int putNotationDecl
197    (
198        XMLNotationDecl* const notationDecl
199    )   const;
200
201    virtual void setValidated(const bool newState);
202
203    virtual void reset();
204
205    // -----------------------------------------------------------------------
206    //  Getter methods
207    // -----------------------------------------------------------------------
208   
209    // deprecated.  returns the ID of the root element; not
210    // useable in multithreaded environments!
211    unsigned int getRootElemId() const;
212    const DTDEntityDecl* getEntityDecl(const XMLCh* const entName) const;
213    DTDEntityDecl* getEntityDecl(const XMLCh* const entName);
214    NameIdPool<DTDEntityDecl>* getEntityDeclPool();
215    const NameIdPool<DTDEntityDecl>* getEntityDeclPool() const;
216    NameIdPoolEnumerator<DTDElementDecl> getElemEnumerator() const;
217    NameIdPoolEnumerator<DTDEntityDecl> getEntityEnumerator() const;
218    NameIdPoolEnumerator<XMLNotationDecl> getNotationEnumerator() const;
219
220    // -----------------------------------------------------------------------
221    //  Setter methods
222    // -----------------------------------------------------------------------
223
224    // deprecated.  Not usable in multithreaded environments
225    void setRootElemId(unsigned int rootElemId);
226
227    virtual void                    setGrammarDescription( XMLGrammarDescription*);
228    virtual XMLGrammarDescription*  getGrammarDescription() const;
229
230    // -----------------------------------------------------------------------
231    //  Content management methods
232    // -----------------------------------------------------------------------
233    unsigned int putEntityDecl(DTDEntityDecl* const entityDecl) const;
234
235
236    // -----------------------------------------------------------------------
237    //  Notification that lazy data has been deleted
238    // -----------------------------------------------------------------------
239    static void reinitDfltEntities();
240
241    /***
242     * Support for Serialization/De-serialization
243     ***/
244    DECL_XSERIALIZABLE(DTDGrammar)
245
246private:
247    // -----------------------------------------------------------------------
248    //  Private helper methods
249    // -----------------------------------------------------------------------
250    void resetEntityDeclPool();
251
252    // -----------------------------------------------------------------------
253    // Unimplemented constructors and operators
254    // -----------------------------------------------------------------------
255    DTDGrammar(const DTDGrammar &);
256    DTDGrammar& operator = (const  DTDGrammar&);
257
258    // -----------------------------------------------------------------------
259    //  Private data members
260    //
261    //  fElemDeclPool
262    //      This is the element decl pool. It contains all of the elements
263    //      declared in the DTD (and their associated attributes.)
264    //
265    //  fElemNonDeclPool
266    //      This is the element decl pool that is is populated as new elements
267    //      are seen in the XML document (not declared in the DTD), and they
268    //      are given default characteristics.
269    //
270    //  fEntityDeclPool
271    //      This is a pool of EntityDecl objects, which contains all of the
272    //      general entities that are declared in the DTD subsets, plus the
273    //      default entities (such as &gt; &lt; ...) defined by the XML Standard.
274    //
275    //  fNotationDeclPool
276    //      This is a pool of NotationDecl objects, which contains all of the
277    //      notations declared in the DTD subsets.
278    //
279    //  fRootElemId
280    //      The id of the root element that we found in the DOCTYPE statement.
281    //      Its initialized to ContentModel::fgInvalidElemId, so that its
282    //      invalid unless we have a DOCTYPE.
283    //
284    //  fValidated
285    //      Indicates if the content of the Grammar has been pre-validated
286    //      or not. When using a cached grammar, no need for pre content
287    //      validation.
288    //
289    //  fGramDesc: adopted
290    //
291    // -----------------------------------------------------------------------
292    static NameIdPool<DTDEntityDecl>* fDefaultEntities;
293    MemoryManager*                    fMemoryManager;
294    NameIdPool<DTDElementDecl>*       fElemDeclPool;
295    NameIdPool<DTDElementDecl>*       fElemNonDeclPool;
296    NameIdPool<DTDEntityDecl>*        fEntityDeclPool;
297    NameIdPool<XMLNotationDecl>*      fNotationDeclPool;
298    unsigned int                      fRootElemId;
299    bool                              fValidated;
300    XMLDTDDescription*                fGramDesc;
301};
302
303
304// ---------------------------------------------------------------------------
305//  DTDGrammar: Getter methods
306// ---------------------------------------------------------------------------
307inline unsigned int DTDGrammar::getRootElemId() const
308{
309    return fRootElemId;
310}
311
312// ---------------------------------------------------------------------------
313//  DTDGrammar: Getter methods
314// ---------------------------------------------------------------------------
315inline NameIdPoolEnumerator<DTDElementDecl>
316DTDGrammar::getElemEnumerator() const
317{
318    return NameIdPoolEnumerator<DTDElementDecl>(fElemDeclPool, fMemoryManager);
319}
320
321inline NameIdPoolEnumerator<DTDEntityDecl>
322DTDGrammar::getEntityEnumerator() const
323{
324    return NameIdPoolEnumerator<DTDEntityDecl>(fEntityDeclPool, fMemoryManager);
325}
326
327inline NameIdPoolEnumerator<XMLNotationDecl>
328DTDGrammar::getNotationEnumerator() const
329{
330    return NameIdPoolEnumerator<XMLNotationDecl>(fNotationDeclPool, fMemoryManager);
331}
332
333inline const DTDEntityDecl*
334DTDGrammar::getEntityDecl(const XMLCh* const entName) const
335{
336    DTDEntityDecl* decl = fDefaultEntities->getByKey(entName);
337
338    if (!decl)
339        return fEntityDeclPool->getByKey(entName);
340
341    return decl;
342}
343
344inline DTDEntityDecl* DTDGrammar::getEntityDecl(const XMLCh* const entName)
345{
346    DTDEntityDecl* decl = fDefaultEntities->getByKey(entName);
347
348    if (!decl)
349        return fEntityDeclPool->getByKey(entName);
350
351    return decl;
352}
353
354
355inline NameIdPool<DTDEntityDecl>* DTDGrammar::getEntityDeclPool()
356{
357    return fEntityDeclPool;
358}
359
360inline const NameIdPool<DTDEntityDecl>* DTDGrammar::getEntityDeclPool() const
361{
362    return fEntityDeclPool;
363}
364
365// -----------------------------------------------------------------------
366//  Setter methods
367// -----------------------------------------------------------------------
368inline void DTDGrammar::setRootElemId(unsigned int rootElemId) {
369    fRootElemId = rootElemId;
370}
371
372inline unsigned int DTDGrammar::putEntityDecl(DTDEntityDecl* const entityDecl)   const
373{
374    return fEntityDeclPool->put(entityDecl);
375}
376
377
378// ---------------------------------------------------------------------------
379//  DTDGrammar: Virtual methods
380// ---------------------------------------------------------------------------
381inline Grammar::GrammarType DTDGrammar::getGrammarType() const {
382    return Grammar::DTDGrammarType;
383}
384
385inline const XMLCh* DTDGrammar::getTargetNamespace() const {
386    return XMLUni::fgZeroLenString;
387}
388
389// Element Decl
390inline unsigned int DTDGrammar::getElemId (const   unsigned int
391                                              , const XMLCh* const
392                                              , const XMLCh* const    qName
393                                              , unsigned int) const
394{
395    //
396    //  In this case, we don't return zero to mean 'not found', so we have to
397    //  map it to the official not found value if we don't find it.
398    //
399    const DTDElementDecl* decl = fElemDeclPool->getByKey(qName);
400    if (!decl)
401        return XMLElementDecl::fgInvalidElemId;
402    return decl->getId();
403}
404
405inline const XMLElementDecl* DTDGrammar::getElemDecl( const   unsigned int
406                                              , const XMLCh* const
407                                              , const XMLCh* const    qName
408                                              , unsigned int)   const
409{
410    const XMLElementDecl* elemDecl = fElemDeclPool->getByKey(qName);
411
412    if (!elemDecl && fElemNonDeclPool)
413        elemDecl = fElemNonDeclPool->getByKey(qName);
414
415    return elemDecl;
416}
417
418inline XMLElementDecl* DTDGrammar::getElemDecl (const   unsigned int
419                                              , const XMLCh* const
420                                              , const XMLCh* const    qName
421                                              , unsigned int)
422{
423    XMLElementDecl* elemDecl = fElemDeclPool->getByKey(qName);
424
425    if (!elemDecl && fElemNonDeclPool)
426        elemDecl = fElemNonDeclPool->getByKey(qName);
427
428    return elemDecl;
429}
430
431inline const XMLElementDecl* DTDGrammar::getElemDecl(const unsigned int elemId) const
432{
433    // Look up this element decl by id
434    return fElemDeclPool->getById(elemId);
435}
436
437inline XMLElementDecl* DTDGrammar::getElemDecl(const unsigned int elemId)
438{
439    // Look up this element decl by id
440    return fElemDeclPool->getById(elemId);
441}
442
443inline unsigned int
444DTDGrammar::putElemDecl(XMLElementDecl* const elemDecl,
445                        const bool notDeclared)
446{
447    if (notDeclared)
448    {
449        if(!fElemNonDeclPool)
450            fElemNonDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(29, 128, fMemoryManager);
451        return fElemNonDeclPool->put((DTDElementDecl*) elemDecl);
452    }
453
454    return fElemDeclPool->put((DTDElementDecl*) elemDecl);
455}
456
457// Notation Decl
458inline const XMLNotationDecl* DTDGrammar::getNotationDecl(const XMLCh* const notName) const
459{
460    return fNotationDeclPool->getByKey(notName);
461}
462
463inline XMLNotationDecl* DTDGrammar::getNotationDecl(const XMLCh* const notName)
464{
465    return fNotationDeclPool->getByKey(notName);
466}
467
468inline unsigned int DTDGrammar::putNotationDecl(XMLNotationDecl* const notationDecl)   const
469{
470    return fNotationDeclPool->put(notationDecl);
471}
472
473inline bool DTDGrammar::getValidated() const
474{
475    return fValidated;
476}
477
478inline void DTDGrammar::setValidated(const bool newState)
479{
480    fValidated = newState;
481}
482
483XERCES_CPP_NAMESPACE_END
484
485#endif
Note: See TracBrowser for help on using the repository browser.