source: NonGTP/Xerces/xerces-c_2_8_0/include/xercesc/validators/schema/SchemaGrammar.hpp @ 2674

Revision 2674, 20.2 KB checked in by mattausch, 16 years ago (diff)
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: SchemaGrammar.hpp 568078 2007-08-21 11:43:25Z amassari $
20 */
21
22
23
24#if !defined(SCHEMAGRAMMAR_HPP)
25#define SCHEMAGRAMMAR_HPP
26
27#include <xercesc/framework/XMLNotationDecl.hpp>
28#include <xercesc/util/RefHash3KeysIdPool.hpp>
29#include <xercesc/util/NameIdPool.hpp>
30#include <xercesc/util/StringPool.hpp>
31#include <xercesc/validators/common/Grammar.hpp>
32#include <xercesc/validators/schema/SchemaElementDecl.hpp>
33#include <xercesc/util/ValueVectorOf.hpp>
34#include <xercesc/validators/datatype/IDDatatypeValidator.hpp>
35#include <xercesc/validators/datatype/DatatypeValidatorFactory.hpp>
36#include <xercesc/framework/XMLSchemaDescription.hpp>
37#include <xercesc/framework/ValidationContext.hpp>
38
39XERCES_CPP_NAMESPACE_BEGIN
40
41//
42// This class stores the Schema information
43//  NOTE: Schemas are not namespace aware, so we just use regular NameIdPool
44//  data structures to store element and attribute decls. They are all set
45//  to be in the global namespace and the full QName is used as the base name
46//  of the decl. This means that all the URI parameters below are expected
47//  to be null pointers (and anything else will cause an exception.)
48//
49
50// ---------------------------------------------------------------------------
51//  Forward Declarations
52// ---------------------------------------------------------------------------
53class ComplexTypeInfo;
54class NamespaceScope;
55class XercesGroupInfo;
56class XercesAttGroupInfo;
57class XSAnnotation;
58
59// ---------------------------------------------------------------------------
60//  typedef declaration
61// ---------------------------------------------------------------------------
62typedef ValueVectorOf<SchemaElementDecl*> ElemVector;
63
64
65class VALIDATORS_EXPORT SchemaGrammar : public Grammar
66{
67public:
68    // -----------------------------------------------------------------------
69    //  Constructors and Destructor
70    // -----------------------------------------------------------------------
71    SchemaGrammar(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
72    virtual ~SchemaGrammar();
73
74    // -----------------------------------------------------------------------
75    //  Implementation of Virtual Interface
76    // -----------------------------------------------------------------------
77    virtual Grammar::GrammarType getGrammarType() const;
78    virtual const XMLCh* getTargetNamespace() const;
79
80    // this method should only be used while the grammar is being
81    // constructed, not while it is being used
82    // in a validation episode!
83    virtual XMLElementDecl* findOrAddElemDecl
84    (
85        const   unsigned int    uriId
86        , const XMLCh* const    baseName
87        , const XMLCh* const    prefixName
88        , const XMLCh* const    qName
89        , unsigned int          scope
90        ,       bool&           wasAdded
91    ) ;
92
93    virtual unsigned int getElemId
94    (
95        const   unsigned int    uriId
96        , const XMLCh* const    baseName
97        , const XMLCh* const    qName
98        , unsigned int          scope
99    )   const ;
100
101    virtual const XMLElementDecl* getElemDecl
102    (
103        const   unsigned int    uriId
104        , const XMLCh* const    baseName
105        , const XMLCh* const    qName
106        , unsigned int          scope
107    )   const ;
108
109    virtual XMLElementDecl* getElemDecl
110    (
111        const   unsigned int    uriId
112        , const XMLCh* const    baseName
113        , const XMLCh* const    qName
114        , unsigned int          scope
115    );
116
117    virtual const XMLElementDecl* getElemDecl
118    (
119        const   unsigned int    elemId
120    )   const;
121
122    virtual XMLElementDecl* getElemDecl
123    (
124        const   unsigned int    elemId
125    );
126
127    virtual const XMLNotationDecl* getNotationDecl
128    (
129        const   XMLCh* const    notName
130    )   const;
131
132    virtual XMLNotationDecl* getNotationDecl
133    (
134        const   XMLCh* const    notName
135    );
136
137    virtual bool getValidated() const;
138
139    virtual XMLElementDecl* putElemDecl
140    (
141        const   unsigned int    uriId
142        , const XMLCh* const    baseName
143        , const XMLCh* const    prefixName
144        , const XMLCh* const    qName
145        , unsigned int          scope
146        , const bool            notDeclared = false
147    );
148
149    virtual unsigned int putElemDecl
150    (
151        XMLElementDecl* const elemDecl
152        , const bool          notDeclared = false
153    )   ;
154
155    virtual unsigned int putNotationDecl
156    (
157        XMLNotationDecl* const notationDecl
158    )   const;
159
160    virtual void setValidated(const bool newState);
161
162    virtual void reset();
163
164    // -----------------------------------------------------------------------
165    //  Getter methods
166    // -----------------------------------------------------------------------
167    RefHash3KeysIdPoolEnumerator<SchemaElementDecl> getElemEnumerator() const;
168    NameIdPoolEnumerator<XMLNotationDecl> getNotationEnumerator() const;
169    RefHashTableOf<XMLAttDef>* getAttributeDeclRegistry() const;
170    RefHashTableOf<ComplexTypeInfo>* getComplexTypeRegistry() const;
171    RefHashTableOf<XercesGroupInfo>* getGroupInfoRegistry() const;
172    RefHashTableOf<XercesAttGroupInfo>* getAttGroupInfoRegistry() const;
173    DatatypeValidatorFactory* getDatatypeRegistry();
174    NamespaceScope* getNamespaceScope() const;
175    RefHash2KeysTableOf<ElemVector>* getValidSubstitutionGroups() const;
176
177    //deprecated
178    RefHashTableOf<XMLRefInfo>* getIDRefList() const;
179
180    ValidationContext*          getValidationContext() const;
181
182    // -----------------------------------------------------------------------
183    //  Setter methods
184    // -----------------------------------------------------------------------
185    void setTargetNamespace(const XMLCh* const targetNamespace);
186    void setAttributeDeclRegistry(RefHashTableOf<XMLAttDef>* const attReg);
187    void setComplexTypeRegistry(RefHashTableOf<ComplexTypeInfo>* const other);
188    void setGroupInfoRegistry(RefHashTableOf<XercesGroupInfo>* const other);
189    void setAttGroupInfoRegistry(RefHashTableOf<XercesAttGroupInfo>* const other);
190    void setNamespaceScope(NamespaceScope* const nsScope);
191    void setValidSubstitutionGroups(RefHash2KeysTableOf<ElemVector>* const);
192
193    virtual void                    setGrammarDescription( XMLGrammarDescription*);
194    virtual XMLGrammarDescription*  getGrammarDescription() const;
195
196    // -----------------------------------------------------------------------
197    //  Helper methods
198    // -----------------------------------------------------------------------
199    unsigned int putGroupElemDecl
200    (
201        XMLElementDecl* const elemDecl
202    )   const;
203
204    // -----------------------------------------------------------------------
205    // Annotation management methods
206    // -----------------------------------------------------------------------
207    /**
208      * Add annotation to the list of annotations for a given key
209      */
210    void putAnnotation(void* key, XSAnnotation* const annotation);
211
212    /**
213      * Add global annotation
214      *
215      * Note: XSAnnotation acts as a linked list
216      */
217    void addAnnotation(XSAnnotation* const annotation);
218
219    /**
220     * Retrieve the annotation that is associated with the specified key
221     *
222     * @param  key   represents a schema component object (i.e. SchemaGrammar)
223     * @return XSAnnotation associated with the key object
224     */
225    XSAnnotation* getAnnotation(const void* const key);
226
227    /**
228     * Retrieve the annotation that is associated with the specified key
229     *
230     * @param  key   represents a schema component object (i.e. SchemaGrammar)
231     * @return XSAnnotation associated with the key object
232     */
233    const XSAnnotation* getAnnotation(const void* const key) const;
234
235    /**
236      * Get global annotation
237      */
238    XSAnnotation* getAnnotation();
239    const XSAnnotation* getAnnotation() const;
240
241    /**
242      * Get annotation hash table, to enumerate through them
243      */
244    RefHashTableOf<XSAnnotation>*  getAnnotations();
245    const RefHashTableOf<XSAnnotation>*  getAnnotations() const;
246
247    /***
248     * Support for Serialization/De-serialization
249     ***/
250    DECL_XSERIALIZABLE(SchemaGrammar)
251
252private:
253    // -----------------------------------------------------------------------
254    //  Unimplemented constructors and operators
255    // -----------------------------------------------------------------------
256    SchemaGrammar(const SchemaGrammar&);
257    SchemaGrammar& operator=(const SchemaGrammar&);
258
259    // -----------------------------------------------------------------------
260    //  Helper methods
261    // -----------------------------------------------------------------------
262    void cleanUp();
263
264    // -----------------------------------------------------------------------
265    //  Private data members
266    //
267    //  fElemDeclPool
268    //      This is the element decl pool. It contains all of the elements
269    //      declared in the Schema (and their associated attributes.)
270    //
271    //  fElemNonDeclPool
272    //      This is the element decl pool that is is populated as new elements
273    //      are seen in the XML document (not declared in the Schema), and they
274    //      are given default characteristics.
275    //
276    //  fGroupElemDeclPool
277    //      This is the element decl pool for elements in a group that are
278    //      referenced in different scope. It contains all of the elements
279    //      declared in the Schema (and their associated attributes.)
280    //
281    //  fNotationDeclPool
282    //      This is a pool of NotationDecl objects, which contains all of the
283    //      notations declared in the Schema.
284    //
285    //  fTargetNamespace
286    //      Target name space for this grammar.
287    //
288    //  fAttributeDeclRegistry
289    //      Global attribute declarations
290    //
291    //  fComplexTypeRegistry
292    //      Stores complexType declaration info
293    //
294    //  fGroupInfoRegistry
295    //      Stores global <group> declaration info
296    //
297    //  fAttGroupInfoRegistry
298    //      Stores global <attributeGroup> declaration info
299    //
300    //  fDatatypeRegistry
301    //      Datatype validator factory
302    //
303    //  fNamespaceScope
304    //      Prefix to Namespace map
305    //
306    //  fValidSubstitutionGroups
307    //      Valid list of elements that can substitute a given element
308    //
309    //  fIDRefList
310    //      List of ids of schema declarations extracted during schema grammar
311    //      traversal
312    //
313    //  fValidated
314    //      Indicates if the content of the Grammar has been pre-validated
315    //      or not (UPA checking, etc.). When using a cached grammar, no need
316    //      for pre content validation.
317    //
318    //  fGramDesc: adopted
319    //
320    // -----------------------------------------------------------------------
321    XMLCh*                                 fTargetNamespace;
322    RefHash3KeysIdPool<SchemaElementDecl>* fElemDeclPool;
323    RefHash3KeysIdPool<SchemaElementDecl>* fElemNonDeclPool;
324    RefHash3KeysIdPool<SchemaElementDecl>* fGroupElemDeclPool;
325    NameIdPool<XMLNotationDecl>*           fNotationDeclPool;
326    RefHashTableOf<XMLAttDef>*             fAttributeDeclRegistry;
327    RefHashTableOf<ComplexTypeInfo>*       fComplexTypeRegistry;
328    RefHashTableOf<XercesGroupInfo>*       fGroupInfoRegistry;
329    RefHashTableOf<XercesAttGroupInfo>*    fAttGroupInfoRegistry;
330    NamespaceScope*                        fNamespaceScope;
331    RefHash2KeysTableOf<ElemVector>*       fValidSubstitutionGroups;
332    ValidationContext*                     fValidationContext;
333    MemoryManager*                         fMemoryManager;
334    XMLSchemaDescription*                  fGramDesc;
335    RefHashTableOf<XSAnnotation>*          fAnnotations;
336
337    bool                                   fValidated;
338    DatatypeValidatorFactory               fDatatypeRegistry;
339};
340
341
342// ---------------------------------------------------------------------------
343//  SchemaGrammar: Getter methods
344// ---------------------------------------------------------------------------
345inline RefHash3KeysIdPoolEnumerator<SchemaElementDecl>
346SchemaGrammar::getElemEnumerator() const
347{
348    return RefHash3KeysIdPoolEnumerator<SchemaElementDecl>(fElemDeclPool, false, fMemoryManager);
349}
350
351inline NameIdPoolEnumerator<XMLNotationDecl>
352SchemaGrammar::getNotationEnumerator() const
353{
354    return NameIdPoolEnumerator<XMLNotationDecl>(fNotationDeclPool, fMemoryManager);
355}
356
357inline RefHashTableOf<XMLAttDef>* SchemaGrammar::getAttributeDeclRegistry() const {
358
359    return fAttributeDeclRegistry;
360}
361
362inline RefHashTableOf<ComplexTypeInfo>*
363SchemaGrammar::getComplexTypeRegistry() const {
364
365    return fComplexTypeRegistry;
366}
367
368inline RefHashTableOf<XercesGroupInfo>*
369SchemaGrammar::getGroupInfoRegistry() const {
370
371    return fGroupInfoRegistry;
372}
373
374inline RefHashTableOf<XercesAttGroupInfo>*
375SchemaGrammar::getAttGroupInfoRegistry() const {
376
377    return fAttGroupInfoRegistry;
378}
379
380inline DatatypeValidatorFactory* SchemaGrammar::getDatatypeRegistry() {
381
382    return &fDatatypeRegistry;
383}
384
385inline NamespaceScope* SchemaGrammar::getNamespaceScope() const {
386
387    return fNamespaceScope;
388}
389
390inline RefHash2KeysTableOf<ElemVector>*
391SchemaGrammar::getValidSubstitutionGroups() const {
392
393    return fValidSubstitutionGroups;
394}
395
396inline RefHashTableOf<XMLRefInfo>* SchemaGrammar::getIDRefList() const {
397
398    return fValidationContext->getIdRefList();
399}
400
401inline ValidationContext* SchemaGrammar::getValidationContext() const {
402
403    return fValidationContext;
404}
405
406inline XMLGrammarDescription* SchemaGrammar::getGrammarDescription() const
407{
408    return fGramDesc;
409}
410
411inline XSAnnotation* SchemaGrammar::getAnnotation(const void* const key)
412{
413    return fAnnotations->get(key);
414}
415
416inline const XSAnnotation* SchemaGrammar::getAnnotation(const void* const key) const
417{
418    return fAnnotations->get(key);
419}
420
421inline XSAnnotation* SchemaGrammar::getAnnotation()
422{
423    return fAnnotations->get(this);
424}
425
426inline const XSAnnotation* SchemaGrammar::getAnnotation() const
427{
428    return fAnnotations->get(this);
429}
430
431inline RefHashTableOf<XSAnnotation>* SchemaGrammar::getAnnotations()
432{
433    return fAnnotations;
434}
435
436inline const RefHashTableOf<XSAnnotation>* SchemaGrammar::getAnnotations() const
437{
438    return fAnnotations;
439}
440// -----------------------------------------------------------------------
441//  Setter methods
442// -----------------------------------------------------------------------
443inline void SchemaGrammar::setTargetNamespace(const XMLCh* const targetNamespace)
444{
445    if (fTargetNamespace)
446        fMemoryManager->deallocate(fTargetNamespace);//delete [] fTargetNamespace;
447    fTargetNamespace = XMLString::replicate(targetNamespace, fMemoryManager);
448}
449
450inline void
451SchemaGrammar::setAttributeDeclRegistry(RefHashTableOf<XMLAttDef>* const attReg) {
452
453    fAttributeDeclRegistry = attReg;
454}
455
456inline void
457SchemaGrammar::setComplexTypeRegistry(RefHashTableOf<ComplexTypeInfo>* const other) {
458
459    fComplexTypeRegistry = other;
460}
461
462inline void
463SchemaGrammar::setGroupInfoRegistry(RefHashTableOf<XercesGroupInfo>* const other) {
464
465    fGroupInfoRegistry = other;
466}
467
468inline void
469SchemaGrammar::setAttGroupInfoRegistry(RefHashTableOf<XercesAttGroupInfo>* const other) {
470
471    fAttGroupInfoRegistry = other;
472}
473
474inline void SchemaGrammar::setNamespaceScope(NamespaceScope* const nsScope) {
475
476    fNamespaceScope = nsScope;
477}
478
479inline void
480SchemaGrammar::setValidSubstitutionGroups(RefHash2KeysTableOf<ElemVector>* const other) {
481
482    fValidSubstitutionGroups = other;
483}
484
485
486// ---------------------------------------------------------------------------
487//  SchemaGrammar: Virtual methods
488// ---------------------------------------------------------------------------
489inline Grammar::GrammarType SchemaGrammar::getGrammarType() const {
490    return Grammar::SchemaGrammarType;
491}
492
493inline const XMLCh* SchemaGrammar::getTargetNamespace() const {
494    return fTargetNamespace;
495}
496
497// Element Decl
498inline unsigned int SchemaGrammar::getElemId (const   unsigned int  uriId
499                                              , const XMLCh* const    baseName
500                                              , const XMLCh* const
501                                              , unsigned int          scope ) const
502{
503    //
504    //  In this case, we don't return zero to mean 'not found', so we have to
505    //  map it to the official not found value if we don't find it.
506    //
507    const SchemaElementDecl* decl = fElemDeclPool->getByKey(baseName, uriId, scope);
508    if (!decl) {
509
510        decl = fGroupElemDeclPool->getByKey(baseName, uriId, scope);
511
512        if (!decl)
513            return XMLElementDecl::fgInvalidElemId;
514    }
515    return decl->getId();
516}
517
518inline const XMLElementDecl* SchemaGrammar::getElemDecl( const   unsigned int  uriId
519                                              , const XMLCh* const    baseName
520                                              , const XMLCh* const
521                                              , unsigned int          scope )   const
522{
523    const SchemaElementDecl* decl = fElemDeclPool->getByKey(baseName, uriId, scope);
524
525    if (!decl) {
526
527        decl = fGroupElemDeclPool->getByKey(baseName, uriId, scope);
528
529        if (!decl && fElemNonDeclPool)
530            decl = fElemNonDeclPool->getByKey(baseName, uriId, scope);
531    }
532
533    return decl;
534}
535
536inline XMLElementDecl* SchemaGrammar::getElemDecl (const   unsigned int  uriId
537                                              , const XMLCh* const    baseName
538                                              , const XMLCh* const
539                                              , unsigned int          scope )
540{
541    SchemaElementDecl* decl = fElemDeclPool->getByKey(baseName, uriId, scope);
542
543    if (!decl) {
544
545        decl = fGroupElemDeclPool->getByKey(baseName, uriId, scope);
546
547        if (!decl && fElemNonDeclPool)
548            decl = fElemNonDeclPool->getByKey(baseName, uriId, scope);
549    }
550
551    return decl;
552}
553
554inline const XMLElementDecl* SchemaGrammar::getElemDecl(const unsigned int elemId) const
555{
556    // Look up this element decl by id
557    const SchemaElementDecl* decl = fElemDeclPool->getById(elemId);
558
559    if (!decl)
560        decl = fGroupElemDeclPool->getById(elemId);
561
562    return decl;
563}
564
565inline XMLElementDecl* SchemaGrammar::getElemDecl(const unsigned int elemId)
566{
567    // Look up this element decl by id
568    SchemaElementDecl* decl = fElemDeclPool->getById(elemId);
569
570    if (!decl)
571        decl = fGroupElemDeclPool->getById(elemId);
572
573    return decl;
574}
575
576inline unsigned int
577SchemaGrammar::putElemDecl(XMLElementDecl* const elemDecl,
578                           const bool notDeclared)
579{
580    if (notDeclared)
581    {
582        if(!fElemNonDeclPool)
583            fElemNonDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(29, true, 128, fMemoryManager);
584        return fElemNonDeclPool->put(elemDecl->getBaseName(), elemDecl->getURI(), ((SchemaElementDecl* )elemDecl)->getEnclosingScope(), (SchemaElementDecl*) elemDecl);
585    }
586
587    return fElemDeclPool->put(elemDecl->getBaseName(), elemDecl->getURI(), ((SchemaElementDecl* )elemDecl)->getEnclosingScope(), (SchemaElementDecl*) elemDecl);
588}
589
590inline unsigned int SchemaGrammar::putGroupElemDecl (XMLElementDecl* const elemDecl)   const
591{
592    return fGroupElemDeclPool->put(elemDecl->getBaseName(), elemDecl->getURI(), ((SchemaElementDecl* )elemDecl)->getEnclosingScope(), (SchemaElementDecl*) elemDecl);
593}
594
595// Notation Decl
596inline const XMLNotationDecl* SchemaGrammar::getNotationDecl(const XMLCh* const notName) const
597{
598    return fNotationDeclPool->getByKey(notName);
599}
600
601inline XMLNotationDecl* SchemaGrammar::getNotationDecl(const XMLCh* const notName)
602{
603    return fNotationDeclPool->getByKey(notName);
604}
605
606inline unsigned int SchemaGrammar::putNotationDecl(XMLNotationDecl* const notationDecl)   const
607{
608    return fNotationDeclPool->put(notationDecl);
609}
610
611inline bool SchemaGrammar::getValidated() const
612{
613    return fValidated;
614}
615
616inline void SchemaGrammar::setValidated(const bool newState)
617{
618    fValidated = newState;
619}
620
621XERCES_CPP_NAMESPACE_END
622
623#endif
Note: See TracBrowser for help on using the repository browser.