Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:19

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_SCHEMAGRAMMAR_HPP)
0023 #define XERCESC_INCLUDE_GUARD_SCHEMAGRAMMAR_HPP
0024 
0025 #include <xercesc/framework/XMLNotationDecl.hpp>
0026 #include <xercesc/util/RefHash3KeysIdPool.hpp>
0027 #include <xercesc/util/NameIdPool.hpp>
0028 #include <xercesc/util/StringPool.hpp>
0029 #include <xercesc/validators/common/Grammar.hpp>
0030 #include <xercesc/validators/schema/SchemaElementDecl.hpp>
0031 #include <xercesc/util/ValueVectorOf.hpp>
0032 #include <xercesc/validators/datatype/IDDatatypeValidator.hpp>
0033 #include <xercesc/validators/datatype/DatatypeValidatorFactory.hpp>
0034 #include <xercesc/framework/XMLSchemaDescription.hpp>
0035 
0036 XERCES_CPP_NAMESPACE_BEGIN
0037 
0038 //
0039 // This class stores the Schema information
0040 //  NOTE: Schemas are not namespace aware, so we just use regular NameIdPool
0041 //  data structures to store element and attribute decls. They are all set
0042 //  to be in the global namespace and the full QName is used as the base name
0043 //  of the decl. This means that all the URI parameters below are expected
0044 //  to be null pointers (and anything else will cause an exception.)
0045 //
0046 
0047 // ---------------------------------------------------------------------------
0048 //  Forward Declarations
0049 // ---------------------------------------------------------------------------
0050 class ComplexTypeInfo;
0051 class XercesGroupInfo;
0052 class XercesAttGroupInfo;
0053 class XSAnnotation;
0054 
0055 // ---------------------------------------------------------------------------
0056 //  typedef declaration
0057 // ---------------------------------------------------------------------------
0058 typedef ValueVectorOf<SchemaElementDecl*> ElemVector;
0059 
0060 
0061 class VALIDATORS_EXPORT SchemaGrammar : public Grammar
0062 {
0063 public:
0064     // -----------------------------------------------------------------------
0065     //  Constructors and Destructor
0066     // -----------------------------------------------------------------------
0067     SchemaGrammar(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0068     virtual ~SchemaGrammar();
0069 
0070     // -----------------------------------------------------------------------
0071     //  Implementation of Virtual Interface
0072     // -----------------------------------------------------------------------
0073     virtual Grammar::GrammarType getGrammarType() const;
0074     virtual const XMLCh* getTargetNamespace() const;
0075 
0076     // this method should only be used while the grammar is being
0077     // constructed, not while it is being used
0078     // in a validation episode!
0079     virtual XMLElementDecl* findOrAddElemDecl
0080     (
0081         const   unsigned int    uriId
0082         , const XMLCh* const    baseName
0083         , const XMLCh* const    prefixName
0084         , const XMLCh* const    qName
0085         , unsigned int          scope
0086         ,       bool&           wasAdded
0087     ) ;
0088 
0089     virtual XMLSize_t getElemId
0090     (
0091         const   unsigned int    uriId
0092         , const XMLCh* const    baseName
0093         , const XMLCh* const    qName
0094         , unsigned int          scope
0095     )   const ;
0096 
0097     virtual const XMLElementDecl* getElemDecl
0098     (
0099         const   unsigned int    uriId
0100         , const XMLCh* const    baseName
0101         , const XMLCh* const    qName
0102         , unsigned int          scope
0103     )   const ;
0104 
0105     virtual XMLElementDecl* getElemDecl
0106     (
0107         const   unsigned int    uriId
0108         , const XMLCh* const    baseName
0109         , const XMLCh* const    qName
0110         , unsigned int          scope
0111     );
0112 
0113     virtual const XMLElementDecl* getElemDecl
0114     (
0115         const   unsigned int    elemId
0116     )   const;
0117 
0118     virtual XMLElementDecl* getElemDecl
0119     (
0120         const   unsigned int    elemId
0121     );
0122 
0123     virtual const XMLNotationDecl* getNotationDecl
0124     (
0125         const   XMLCh* const    notName
0126     )   const;
0127 
0128     virtual XMLNotationDecl* getNotationDecl
0129     (
0130         const   XMLCh* const    notName
0131     );
0132 
0133     virtual bool getValidated() const;
0134 
0135     virtual XMLElementDecl* putElemDecl
0136     (
0137         const   unsigned int    uriId
0138         , const XMLCh* const    baseName
0139         , const XMLCh* const    prefixName
0140         , const XMLCh* const    qName
0141         , unsigned int          scope
0142         , const bool            notDeclared = false
0143     );
0144 
0145     virtual XMLSize_t putElemDecl
0146     (
0147         XMLElementDecl* const elemDecl
0148         , const bool          notDeclared = false
0149     )   ;
0150 
0151     virtual XMLSize_t putNotationDecl
0152     (
0153         XMLNotationDecl* const notationDecl
0154     )   const;
0155 
0156     virtual void setValidated(const bool newState);
0157 
0158     virtual void reset();
0159 
0160     // -----------------------------------------------------------------------
0161     //  Getter methods
0162     // -----------------------------------------------------------------------
0163     RefHash3KeysIdPoolEnumerator<SchemaElementDecl> getElemEnumerator() const;
0164     NameIdPoolEnumerator<XMLNotationDecl> getNotationEnumerator() const;
0165     RefHashTableOf<XMLAttDef>* getAttributeDeclRegistry() const;
0166     RefHashTableOf<ComplexTypeInfo>* getComplexTypeRegistry() const;
0167     RefHashTableOf<XercesGroupInfo>* getGroupInfoRegistry() const;
0168     RefHashTableOf<XercesAttGroupInfo>* getAttGroupInfoRegistry() const;
0169     DatatypeValidatorFactory* getDatatypeRegistry();
0170     RefHash2KeysTableOf<ElemVector>* getValidSubstitutionGroups() const;
0171 
0172     // -----------------------------------------------------------------------
0173     //  Setter methods
0174     // -----------------------------------------------------------------------
0175     void setTargetNamespace(const XMLCh* const targetNamespace);
0176     void setAttributeDeclRegistry(RefHashTableOf<XMLAttDef>* const attReg);
0177     void setComplexTypeRegistry(RefHashTableOf<ComplexTypeInfo>* const other);
0178     void setGroupInfoRegistry(RefHashTableOf<XercesGroupInfo>* const other);
0179     void setAttGroupInfoRegistry(RefHashTableOf<XercesAttGroupInfo>* const other);
0180     void setValidSubstitutionGroups(RefHash2KeysTableOf<ElemVector>* const);
0181 
0182     virtual void                    setGrammarDescription( XMLGrammarDescription*);
0183     virtual XMLGrammarDescription*  getGrammarDescription() const;
0184 
0185     // -----------------------------------------------------------------------
0186     //  Helper methods
0187     // -----------------------------------------------------------------------
0188     XMLSize_t putGroupElemDecl
0189     (
0190         XMLElementDecl* const elemDecl
0191     )   const;
0192 
0193     // -----------------------------------------------------------------------
0194     // Annotation management methods
0195     // -----------------------------------------------------------------------
0196     /**
0197       * Add annotation to the list of annotations for a given key
0198       */
0199     void putAnnotation(void* key, XSAnnotation* const annotation);
0200 
0201     /**
0202       * Add global annotation
0203       *
0204       * Note: XSAnnotation acts as a linked list
0205       */
0206     void addAnnotation(XSAnnotation* const annotation);
0207 
0208     /**
0209      * Retrieve the annotation that is associated with the specified key
0210      *
0211      * @param  key   represents a schema component object (i.e. SchemaGrammar)
0212      * @return XSAnnotation associated with the key object
0213      */
0214     XSAnnotation* getAnnotation(const void* const key);
0215 
0216     /**
0217      * Retrieve the annotation that is associated with the specified key
0218      *
0219      * @param  key   represents a schema component object (i.e. SchemaGrammar)
0220      * @return XSAnnotation associated with the key object
0221      */
0222     const XSAnnotation* getAnnotation(const void* const key) const;
0223 
0224     /**
0225       * Get global annotation
0226       */
0227     XSAnnotation* getAnnotation();
0228     const XSAnnotation* getAnnotation() const;
0229 
0230     /**
0231       * Get annotation hash table, to enumerate through them
0232       */
0233     RefHashTableOf<XSAnnotation, PtrHasher>*  getAnnotations();
0234     const RefHashTableOf<XSAnnotation, PtrHasher>*  getAnnotations() const;
0235 
0236     /**
0237      * Get/set scope count.
0238      */
0239     unsigned int getScopeCount () const;
0240     void setScopeCount (unsigned int);
0241 
0242     /**
0243      * Get/set anonymous type count.
0244      */
0245     unsigned int getAnonTypeCount () const;
0246     void setAnonTypeCount (unsigned int);
0247 
0248     /***
0249      * Support for Serialization/De-serialization
0250      ***/
0251     DECL_XSERIALIZABLE(SchemaGrammar)
0252 
0253 private:
0254     // -----------------------------------------------------------------------
0255     //  Unimplemented constructors and operators
0256     // -----------------------------------------------------------------------
0257     SchemaGrammar(const SchemaGrammar&);
0258     SchemaGrammar& operator=(const SchemaGrammar&);
0259 
0260     // -----------------------------------------------------------------------
0261     //  Helper methods
0262     // -----------------------------------------------------------------------
0263     void cleanUp();
0264 
0265     // -----------------------------------------------------------------------
0266     //  Private data members
0267     //
0268     //  fElemDeclPool
0269     //      This is the element decl pool. It contains all of the elements
0270     //      declared in the Schema (and their associated attributes.)
0271     //
0272     //  fElemNonDeclPool
0273     //      This is the element decl pool that is is populated as new elements
0274     //      are seen in the XML document (not declared in the Schema), and they
0275     //      are given default characteristics.
0276     //
0277     //  fGroupElemDeclPool
0278     //      This is the element decl pool for elements in a group that are
0279     //      referenced in different scope. It contains all of the elements
0280     //      declared in the Schema (and their associated attributes.)
0281     //
0282     //  fNotationDeclPool
0283     //      This is a pool of NotationDecl objects, which contains all of the
0284     //      notations declared in the Schema.
0285     //
0286     //  fTargetNamespace
0287     //      Target name space for this grammar.
0288     //
0289     //  fAttributeDeclRegistry
0290     //      Global attribute declarations
0291     //
0292     //  fComplexTypeRegistry
0293     //      Stores complexType declaration info
0294     //
0295     //  fGroupInfoRegistry
0296     //      Stores global <group> declaration info
0297     //
0298     //  fAttGroupInfoRegistry
0299     //      Stores global <attributeGroup> declaration info
0300     //
0301     //  fDatatypeRegistry
0302     //      Datatype validator factory
0303     //
0304     //  fValidSubstitutionGroups
0305     //      Valid list of elements that can substitute a given element
0306     //
0307     //  fIDRefList
0308     //      List of ids of schema declarations extracted during schema grammar
0309     //      traversal
0310     //
0311     //  fValidated
0312     //      Indicates if the content of the Grammar has been pre-validated
0313     //      or not (UPA checking, etc.). When using a cached grammar, no need
0314     //      for pre content validation.
0315     //
0316     //  fGramDesc: adopted
0317     //
0318     // -----------------------------------------------------------------------
0319     XMLCh*                                   fTargetNamespace;
0320     RefHash3KeysIdPool<SchemaElementDecl>*   fElemDeclPool;
0321     RefHash3KeysIdPool<SchemaElementDecl>*   fElemNonDeclPool;
0322     RefHash3KeysIdPool<SchemaElementDecl>*   fGroupElemDeclPool;
0323     NameIdPool<XMLNotationDecl>*             fNotationDeclPool;
0324     RefHashTableOf<XMLAttDef>*               fAttributeDeclRegistry;
0325     RefHashTableOf<ComplexTypeInfo>*         fComplexTypeRegistry;
0326     RefHashTableOf<XercesGroupInfo>*         fGroupInfoRegistry;
0327     RefHashTableOf<XercesAttGroupInfo>*      fAttGroupInfoRegistry;
0328     RefHash2KeysTableOf<ElemVector>*         fValidSubstitutionGroups;
0329     MemoryManager*                           fMemoryManager;
0330     XMLSchemaDescription*                    fGramDesc;
0331     RefHashTableOf<XSAnnotation, PtrHasher>* fAnnotations;
0332 
0333     bool                                   fValidated;
0334     DatatypeValidatorFactory               fDatatypeRegistry;
0335 
0336     unsigned int                             fScopeCount;
0337     unsigned int                             fAnonTypeCount;
0338 };
0339 
0340 
0341 // ---------------------------------------------------------------------------
0342 //  SchemaGrammar: Getter methods
0343 // ---------------------------------------------------------------------------
0344 inline RefHash3KeysIdPoolEnumerator<SchemaElementDecl>
0345 SchemaGrammar::getElemEnumerator() const
0346 {
0347     return RefHash3KeysIdPoolEnumerator<SchemaElementDecl>(fElemDeclPool, false, fMemoryManager);
0348 }
0349 
0350 inline NameIdPoolEnumerator<XMLNotationDecl>
0351 SchemaGrammar::getNotationEnumerator() const
0352 {
0353     return NameIdPoolEnumerator<XMLNotationDecl>(fNotationDeclPool, fMemoryManager);
0354 }
0355 
0356 inline RefHashTableOf<XMLAttDef>* SchemaGrammar::getAttributeDeclRegistry() const {
0357 
0358     return fAttributeDeclRegistry;
0359 }
0360 
0361 inline RefHashTableOf<ComplexTypeInfo>*
0362 SchemaGrammar::getComplexTypeRegistry() const {
0363 
0364     return fComplexTypeRegistry;
0365 }
0366 
0367 inline RefHashTableOf<XercesGroupInfo>*
0368 SchemaGrammar::getGroupInfoRegistry() const {
0369 
0370     return fGroupInfoRegistry;
0371 }
0372 
0373 inline RefHashTableOf<XercesAttGroupInfo>*
0374 SchemaGrammar::getAttGroupInfoRegistry() const {
0375 
0376     return fAttGroupInfoRegistry;
0377 }
0378 
0379 inline DatatypeValidatorFactory* SchemaGrammar::getDatatypeRegistry() {
0380 
0381     return &fDatatypeRegistry;
0382 }
0383 
0384 inline RefHash2KeysTableOf<ElemVector>*
0385 SchemaGrammar::getValidSubstitutionGroups() const {
0386 
0387     return fValidSubstitutionGroups;
0388 }
0389 
0390 inline XMLGrammarDescription* SchemaGrammar::getGrammarDescription() const
0391 {
0392     return fGramDesc;
0393 }
0394 
0395 inline XSAnnotation* SchemaGrammar::getAnnotation(const void* const key)
0396 {
0397     return fAnnotations->get(key);
0398 }
0399 
0400 inline const XSAnnotation* SchemaGrammar::getAnnotation(const void* const key) const
0401 {
0402     return fAnnotations->get(key);
0403 }
0404 
0405 inline XSAnnotation* SchemaGrammar::getAnnotation()
0406 {
0407     return fAnnotations->get(this);
0408 }
0409 
0410 inline const XSAnnotation* SchemaGrammar::getAnnotation() const
0411 {
0412     return fAnnotations->get(this);
0413 }
0414 
0415 inline RefHashTableOf<XSAnnotation, PtrHasher>* SchemaGrammar::getAnnotations()
0416 {
0417     return fAnnotations;
0418 }
0419 
0420 inline const RefHashTableOf<XSAnnotation, PtrHasher>* SchemaGrammar::getAnnotations() const
0421 {
0422     return fAnnotations;
0423 }
0424 // -----------------------------------------------------------------------
0425 //  Setter methods
0426 // -----------------------------------------------------------------------
0427 inline void SchemaGrammar::setTargetNamespace(const XMLCh* const targetNamespace)
0428 {
0429     if (fTargetNamespace)
0430         fMemoryManager->deallocate(fTargetNamespace);//delete [] fTargetNamespace;
0431     fTargetNamespace = XMLString::replicate(targetNamespace, fMemoryManager);
0432 }
0433 
0434 inline void
0435 SchemaGrammar::setAttributeDeclRegistry(RefHashTableOf<XMLAttDef>* const attReg) {
0436 
0437     fAttributeDeclRegistry = attReg;
0438 }
0439 
0440 inline void
0441 SchemaGrammar::setComplexTypeRegistry(RefHashTableOf<ComplexTypeInfo>* const other) {
0442 
0443     fComplexTypeRegistry = other;
0444 }
0445 
0446 inline void
0447 SchemaGrammar::setGroupInfoRegistry(RefHashTableOf<XercesGroupInfo>* const other) {
0448 
0449     fGroupInfoRegistry = other;
0450 }
0451 
0452 inline void
0453 SchemaGrammar::setAttGroupInfoRegistry(RefHashTableOf<XercesAttGroupInfo>* const other) {
0454 
0455     fAttGroupInfoRegistry = other;
0456 }
0457 
0458 inline void
0459 SchemaGrammar::setValidSubstitutionGroups(RefHash2KeysTableOf<ElemVector>* const other) {
0460 
0461     fValidSubstitutionGroups = other;
0462 }
0463 
0464 
0465 // ---------------------------------------------------------------------------
0466 //  SchemaGrammar: Virtual methods
0467 // ---------------------------------------------------------------------------
0468 inline Grammar::GrammarType SchemaGrammar::getGrammarType() const {
0469     return Grammar::SchemaGrammarType;
0470 }
0471 
0472 inline const XMLCh* SchemaGrammar::getTargetNamespace() const {
0473     return fTargetNamespace;
0474 }
0475 
0476 // Element Decl
0477 inline XMLSize_t SchemaGrammar::getElemId (const   unsigned int  uriId
0478                                               , const XMLCh* const    baseName
0479                                               , const XMLCh* const
0480                                               , unsigned int          scope ) const
0481 {
0482     //
0483     //  In this case, we don't return zero to mean 'not found', so we have to
0484     //  map it to the official not found value if we don't find it.
0485     //
0486     const SchemaElementDecl* decl = fElemDeclPool->getByKey(baseName, uriId, scope);
0487     if (!decl) {
0488 
0489         decl = fGroupElemDeclPool->getByKey(baseName, uriId, scope);
0490 
0491         if (!decl)
0492             return XMLElementDecl::fgInvalidElemId;
0493     }
0494     return decl->getId();
0495 }
0496 
0497 inline const XMLElementDecl* SchemaGrammar::getElemDecl( const   unsigned int  uriId
0498                                               , const XMLCh* const    baseName
0499                                               , const XMLCh* const
0500                                               , unsigned int          scope )   const
0501 {
0502     const SchemaElementDecl* decl = fElemDeclPool->getByKey(baseName, uriId, scope);
0503 
0504     if (!decl) {
0505 
0506         decl = fGroupElemDeclPool->getByKey(baseName, uriId, scope);
0507 
0508         if (!decl && fElemNonDeclPool)
0509             decl = fElemNonDeclPool->getByKey(baseName, uriId, scope);
0510     }
0511 
0512     return decl;
0513 }
0514 
0515 inline XMLElementDecl* SchemaGrammar::getElemDecl (const   unsigned int  uriId
0516                                               , const XMLCh* const    baseName
0517                                               , const XMLCh* const
0518                                               , unsigned int          scope )
0519 {
0520     SchemaElementDecl* decl = fElemDeclPool->getByKey(baseName, uriId, scope);
0521 
0522     if (!decl) {
0523 
0524         decl = fGroupElemDeclPool->getByKey(baseName, uriId, scope);
0525 
0526         if (!decl && fElemNonDeclPool)
0527             decl = fElemNonDeclPool->getByKey(baseName, uriId, scope);
0528     }
0529 
0530     return decl;
0531 }
0532 
0533 inline const XMLElementDecl* SchemaGrammar::getElemDecl(const unsigned int elemId) const
0534 {
0535     // Look up this element decl by id
0536     const SchemaElementDecl* decl = fElemDeclPool->getById(elemId);
0537 
0538     if (!decl)
0539         decl = fGroupElemDeclPool->getById(elemId);
0540 
0541     return decl;
0542 }
0543 
0544 inline XMLElementDecl* SchemaGrammar::getElemDecl(const unsigned int elemId)
0545 {
0546     // Look up this element decl by id
0547     SchemaElementDecl* decl = fElemDeclPool->getById(elemId);
0548 
0549     if (!decl)
0550         decl = fGroupElemDeclPool->getById(elemId);
0551 
0552     return decl;
0553 }
0554 
0555 inline XMLSize_t
0556 SchemaGrammar::putElemDecl(XMLElementDecl* const elemDecl,
0557                            const bool notDeclared)
0558 {
0559     if (notDeclared)
0560     {
0561         if(!fElemNonDeclPool)
0562             fElemNonDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(29, true, 128, fMemoryManager);
0563         return fElemNonDeclPool->put(elemDecl->getBaseName(), elemDecl->getURI(), ((SchemaElementDecl* )elemDecl)->getEnclosingScope(), (SchemaElementDecl*) elemDecl);
0564     }
0565 
0566     return fElemDeclPool->put(elemDecl->getBaseName(), elemDecl->getURI(), ((SchemaElementDecl* )elemDecl)->getEnclosingScope(), (SchemaElementDecl*) elemDecl);
0567 }
0568 
0569 inline XMLSize_t SchemaGrammar::putGroupElemDecl (XMLElementDecl* const elemDecl)   const
0570 {
0571     return fGroupElemDeclPool->put(elemDecl->getBaseName(), elemDecl->getURI(), ((SchemaElementDecl* )elemDecl)->getEnclosingScope(), (SchemaElementDecl*) elemDecl);
0572 }
0573 
0574 // Notation Decl
0575 inline const XMLNotationDecl* SchemaGrammar::getNotationDecl(const XMLCh* const notName) const
0576 {
0577     return fNotationDeclPool->getByKey(notName);
0578 }
0579 
0580 inline XMLNotationDecl* SchemaGrammar::getNotationDecl(const XMLCh* const notName)
0581 {
0582     return fNotationDeclPool->getByKey(notName);
0583 }
0584 
0585 inline XMLSize_t SchemaGrammar::putNotationDecl(XMLNotationDecl* const notationDecl)   const
0586 {
0587     return fNotationDeclPool->put(notationDecl);
0588 }
0589 
0590 inline bool SchemaGrammar::getValidated() const
0591 {
0592     return fValidated;
0593 }
0594 
0595 inline void SchemaGrammar::setValidated(const bool newState)
0596 {
0597     fValidated = newState;
0598 }
0599 
0600 inline unsigned int
0601 SchemaGrammar::getScopeCount () const
0602 {
0603   return fScopeCount;
0604 }
0605 
0606 inline void
0607 SchemaGrammar::setScopeCount (unsigned int scopeCount)
0608 {
0609   fScopeCount = scopeCount;
0610 }
0611 
0612 inline unsigned int
0613 SchemaGrammar::getAnonTypeCount () const
0614 {
0615   return fAnonTypeCount;
0616 }
0617 
0618 inline void
0619 SchemaGrammar::setAnonTypeCount (unsigned int count)
0620 {
0621   fAnonTypeCount = count;
0622 }
0623 
0624 XERCES_CPP_NAMESPACE_END
0625 
0626 #endif