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