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: SchemaElementDecl.hpp 932887 2010-04-11 13:04:59Z borisk $
20  */
21 
22 #if !defined(XERCESC_INCLUDE_GUARD_SCHEMAELEMENTDECL_HPP)
23 #define XERCESC_INCLUDE_GUARD_SCHEMAELEMENTDECL_HPP
24 
25 #include <xercesc/util/QName.hpp>
26 #include <xercesc/validators/common/Grammar.hpp>
27 #include <xercesc/validators/schema/ComplexTypeInfo.hpp>
28 #include <xercesc/validators/schema/identity/IdentityConstraint.hpp>
29 #include <xercesc/validators/datatype/DatatypeValidator.hpp>
30 #include <xercesc/validators/schema/PSVIDefs.hpp>
31 
32 XERCES_CPP_NAMESPACE_BEGIN
33 
34 class ContentSpecNode;
35 class SchemaAttDefList;
36 
37 //
38 //  This class is a derivative of the basic element decl. This one implements
39 //  the virtuals so that they work for a Schema.
40 //
41 class VALIDATORS_EXPORT SchemaElementDecl : public XMLElementDecl
42 {
43 public :
44 
45     // -----------------------------------------------------------------------
46     //  Class specific types
47     //
48     //  ModelTypes
49     //      Indicates the type of content model that an element has. This
50     //      indicates how the content model is represented and validated.
51     // -----------------------------------------------------------------------
52     enum ModelTypes
53     {
54         Empty
55         , Any
56         , Mixed_Simple
57         , Mixed_Complex
58         , Children
59         , Simple
60         , ElementOnlyEmpty
61         , ModelTypes_Count
62     };
63 
64     // -----------------------------------------------------------------------
65     //  Constructors and Destructor
66     // -----------------------------------------------------------------------
67     SchemaElementDecl(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
68     SchemaElementDecl
69     (
70           const XMLCh* const   prefix
71         , const XMLCh* const   localPart
72         , const int            uriId
73         , const ModelTypes     modelType = Any
74         , const unsigned int   enclosingScope = Grammar::TOP_LEVEL_SCOPE
75         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
76     );
77 
78     SchemaElementDecl
79     (
80           const QName* const   elementName
81         , const ModelTypes     modelType = Any
82         , const unsigned int   enclosingScope = Grammar::TOP_LEVEL_SCOPE
83         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
84     );
85 
86     ~SchemaElementDecl();
87 
88 
89     // -----------------------------------------------------------------------
90     //  The virtual element decl interface
91     // -----------------------------------------------------------------------
92     virtual XMLAttDefList& getAttDefList() const;
93     virtual CharDataOpts getCharDataOpts() const;
94     virtual bool hasAttDefs() const;
95     virtual const ContentSpecNode* getContentSpec() const;
96     virtual ContentSpecNode* getContentSpec();
97     virtual void setContentSpec(ContentSpecNode* toAdopt);
98     virtual XMLContentModel* getContentModel();
99     virtual void setContentModel(XMLContentModel* const newModelToAdopt);
100     virtual const XMLCh* getFormattedContentModel ()   const;
101 
102 
103     // -----------------------------------------------------------------------
104     //  Getter methods
105     // -----------------------------------------------------------------------
106     const SchemaAttDef* getAttDef(const XMLCh* const baseName, const int uriId) const;
107     SchemaAttDef* getAttDef(const XMLCh* const baseName, const int uriId);
108     const SchemaAttDef* getAttWildCard() const;
109     SchemaAttDef* getAttWildCard();
110     ModelTypes getModelType() const;
111     PSVIDefs::PSVIScope getPSVIScope() const;
112     DatatypeValidator* getDatatypeValidator() const;
113     unsigned int getEnclosingScope() const;
114     int getFinalSet() const;
115     int getBlockSet() const;
116     int getMiscFlags() const;
117     XMLCh* getDefaultValue() const;
118     ComplexTypeInfo* getComplexTypeInfo() const;
119     virtual bool isGlobalDecl() const;
120     SchemaElementDecl* getSubstitutionGroupElem() const;
121 
122     // -----------------------------------------------------------------------
123     //  Setter methods
124     // -----------------------------------------------------------------------
125     void setModelType(const SchemaElementDecl::ModelTypes toSet);
126     void setPSVIScope(const PSVIDefs::PSVIScope toSet);
127     void setDatatypeValidator(DatatypeValidator* newDatatypeValidator);
128     void setEnclosingScope(const unsigned int enclosingScope);
129     void setFinalSet(const int finalSet);
130     void setBlockSet(const int blockSet);
131     void setMiscFlags(const int flags);
132     void setDefaultValue(const XMLCh* const value);
133     void setComplexTypeInfo(ComplexTypeInfo* const typeInfo);
134     void setAttWildCard(SchemaAttDef* const attWildCard);
135     void setSubstitutionGroupElem(SchemaElementDecl* const elemDecl);
136 
137     // -----------------------------------------------------------------------
138     //  IC methods
139     // -----------------------------------------------------------------------
140     void addIdentityConstraint(IdentityConstraint* const ic);
141     XMLSize_t getIdentityConstraintCount() const;
142     IdentityConstraint* getIdentityConstraintAt(XMLSize_t index) const;
143 
144     /***
145      * Support for Serialization/De-serialization
146      ***/
147     DECL_XSERIALIZABLE(SchemaElementDecl)
148 
149     virtual XMLElementDecl::objectType  getObjectType() const;
150 
151 private :
152     // -----------------------------------------------------------------------
153     //  Unimplemented constructors and operators
154     // -----------------------------------------------------------------------
155     SchemaElementDecl(const SchemaElementDecl&);
156     SchemaElementDecl& operator=(const SchemaElementDecl&);
157 
158     // -----------------------------------------------------------------------
159     //  Private data members
160     //
161     //  fModelType
162     //      The content model type of this element. This tells us what kind
163     //      of content model to create.
164     //
165     //  fDatatypeValidator
166     //      The DatatypeValidator used to validate this element type.
167     //
168     //  fEnclosingScope
169     //      The enclosing scope where this element is declared.
170     //
171     //  fFinalSet
172     //      The value set of the 'final' attribute.
173     //
174     //  fBlockSet
175     //      The value set of the 'block' attribute.
176     //
177     //  fMiscFlags
178     //      Stores 'abstract/nullable' values
179     //
180     //  fDefaultValue
181     //      The default/fixed value
182     //
183     //  fComplexTypeInfo
184     //      Stores complex type information
185     //      (no need to delete - handled by schema grammar)
186     //
187     //  fAttDefs
188     //      The list of attributes that are faulted in for this element
189     //      when ComplexTypeInfo does not exist.  We want to keep track
190     //      of these faulted in attributes to avoid duplicate redundant
191     //      error.
192     //
193     //  fIdentityConstraints
194     //      Store information about an element identity constraints.
195     //
196     //  fAttWildCard
197     //      Store wildcard attribute in the case of an element with a type of
198     //      'anyType'.
199     //
200     //  fSubstitutionGroupElem
201     //      The substitution group element declaration.
202     // -----------------------------------------------------------------------
203 
204     // -----------------------------------------------------------------------
205     ModelTypes                         fModelType;
206     PSVIDefs::PSVIScope                fPSVIScope;
207 
208     unsigned int                       fEnclosingScope;
209     int                                fFinalSet;
210     int                                fBlockSet;
211     int                                fMiscFlags;
212     XMLCh*                             fDefaultValue;
213     ComplexTypeInfo*                   fComplexTypeInfo;
214     RefHash2KeysTableOf<SchemaAttDef>* fAttDefs;
215     RefVectorOf<IdentityConstraint>*   fIdentityConstraints;
216     SchemaAttDef*                      fAttWildCard;
217     SchemaElementDecl*                 fSubstitutionGroupElem;
218     DatatypeValidator*                 fDatatypeValidator;
219 };
220 
221 // ---------------------------------------------------------------------------
222 //  SchemaElementDecl: XMLElementDecl virtual interface implementation
223 // ---------------------------------------------------------------------------
getContentSpec()224 inline ContentSpecNode* SchemaElementDecl::getContentSpec()
225 {
226     if (fComplexTypeInfo != 0) {
227         return fComplexTypeInfo->getContentSpec();
228     }
229 
230     return 0;
231 }
232 
getContentSpec() const233 inline const ContentSpecNode* SchemaElementDecl::getContentSpec() const
234 {
235     if (fComplexTypeInfo != 0) {
236         return fComplexTypeInfo->getContentSpec();
237     }
238 
239     return 0;
240 }
241 
242 inline void
setContentSpec(ContentSpecNode *)243 SchemaElementDecl::setContentSpec(ContentSpecNode*)
244 {
245     //Handled by complexType
246 }
247 
getContentModel()248 inline XMLContentModel* SchemaElementDecl::getContentModel()
249 {
250     if (fComplexTypeInfo != 0) {
251         return fComplexTypeInfo->getContentModel();
252     }
253     return 0;
254 }
255 
256 inline void
setContentModel(XMLContentModel * const)257 SchemaElementDecl::setContentModel(XMLContentModel* const)
258 {
259     //Handled by complexType
260 }
261 
262 
263 // ---------------------------------------------------------------------------
264 //  SchemaElementDecl: Getter methods
265 // ---------------------------------------------------------------------------
getModelType() const266 inline SchemaElementDecl::ModelTypes SchemaElementDecl::getModelType() const
267 {
268     if (fComplexTypeInfo) {
269         return (SchemaElementDecl::ModelTypes) fComplexTypeInfo->getContentType();
270     }
271 
272     return fModelType;
273 }
274 
getPSVIScope() const275 inline PSVIDefs::PSVIScope SchemaElementDecl::getPSVIScope() const
276 {
277     return fPSVIScope;
278 }
279 
getDatatypeValidator() const280 inline DatatypeValidator* SchemaElementDecl::getDatatypeValidator() const
281 {
282     return fDatatypeValidator;
283 }
284 
getEnclosingScope() const285 inline unsigned int SchemaElementDecl::getEnclosingScope() const
286 {
287     return fEnclosingScope;
288 }
289 
getFinalSet() const290 inline int SchemaElementDecl::getFinalSet() const
291 {
292     return fFinalSet;
293 }
294 
getBlockSet() const295 inline int SchemaElementDecl::getBlockSet() const
296 {
297     return fBlockSet;
298 }
299 
getMiscFlags() const300 inline int SchemaElementDecl::getMiscFlags() const
301 {
302     return fMiscFlags;
303 }
304 
getDefaultValue() const305 inline XMLCh* SchemaElementDecl::getDefaultValue() const
306 {
307     return fDefaultValue;
308 }
309 
getComplexTypeInfo() const310 inline ComplexTypeInfo* SchemaElementDecl::getComplexTypeInfo() const
311 {
312     return fComplexTypeInfo;
313 }
314 
getAttWildCard() const315 inline const SchemaAttDef* SchemaElementDecl::getAttWildCard() const {
316     return fAttWildCard;
317 }
318 
getAttWildCard()319 inline SchemaAttDef* SchemaElementDecl::getAttWildCard() {
320     return fAttWildCard;
321 }
322 
isGlobalDecl() const323 inline bool SchemaElementDecl::isGlobalDecl() const {
324 
325     return (fEnclosingScope == Grammar::TOP_LEVEL_SCOPE);
326 }
327 
328 inline SchemaElementDecl*
getSubstitutionGroupElem() const329 SchemaElementDecl::getSubstitutionGroupElem() const {
330 
331     return fSubstitutionGroupElem;
332 }
333 
334 // ---------------------------------------------------------------------------
335 //  SchemaElementDecl: Setter methods
336 // ---------------------------------------------------------------------------
337 inline void
setModelType(const SchemaElementDecl::ModelTypes toSet)338 SchemaElementDecl::setModelType(const SchemaElementDecl::ModelTypes toSet)
339 {
340     fModelType = toSet;
341 }
342 
343 inline void
setPSVIScope(const PSVIDefs::PSVIScope toSet)344 SchemaElementDecl::setPSVIScope(const PSVIDefs::PSVIScope toSet)
345 {
346     fPSVIScope = toSet;
347 }
348 
setDatatypeValidator(DatatypeValidator * newDatatypeValidator)349 inline void SchemaElementDecl::setDatatypeValidator(DatatypeValidator* newDatatypeValidator)
350 {
351     fDatatypeValidator = newDatatypeValidator;
352 }
353 
setEnclosingScope(const unsigned int newEnclosingScope)354 inline void SchemaElementDecl::setEnclosingScope(const unsigned int newEnclosingScope)
355 {
356     fEnclosingScope = newEnclosingScope;
357 }
358 
setFinalSet(const int finalSet)359 inline void SchemaElementDecl::setFinalSet(const int finalSet)
360 {
361     fFinalSet = finalSet;
362 }
363 
setBlockSet(const int blockSet)364 inline void SchemaElementDecl::setBlockSet(const int blockSet)
365 {
366     fBlockSet = blockSet;
367 }
368 
setMiscFlags(const int flags)369 inline void SchemaElementDecl::setMiscFlags(const int flags)
370 {
371     fMiscFlags = flags;
372 }
373 
setDefaultValue(const XMLCh * const value)374 inline void SchemaElementDecl::setDefaultValue(const XMLCh* const value)
375 {
376     if (fDefaultValue) {
377         getMemoryManager()->deallocate(fDefaultValue);//delete[] fDefaultValue;
378     }
379 
380     fDefaultValue = XMLString::replicate(value, getMemoryManager());
381 }
382 
383 inline void
setComplexTypeInfo(ComplexTypeInfo * const typeInfo)384 SchemaElementDecl::setComplexTypeInfo(ComplexTypeInfo* const typeInfo)
385 {
386     fComplexTypeInfo = typeInfo;
387 }
388 
389 inline void
setAttWildCard(SchemaAttDef * const attWildCard)390 SchemaElementDecl::setAttWildCard(SchemaAttDef* const attWildCard) {
391 
392     if (fAttWildCard)
393         delete fAttWildCard;
394 
395     fAttWildCard = attWildCard;
396 }
397 
398 inline void
setSubstitutionGroupElem(SchemaElementDecl * const elemDecl)399 SchemaElementDecl::setSubstitutionGroupElem(SchemaElementDecl* const elemDecl) {
400 
401     fSubstitutionGroupElem = elemDecl;
402 }
403 
404 // ---------------------------------------------------------------------------
405 //  SchemaElementDecl: IC methods
406 // ---------------------------------------------------------------------------
407 inline void
addIdentityConstraint(IdentityConstraint * const ic)408 SchemaElementDecl::addIdentityConstraint(IdentityConstraint* const ic) {
409 
410     if (!fIdentityConstraints) {
411         fIdentityConstraints = new (getMemoryManager()) RefVectorOf<IdentityConstraint>(16, true, getMemoryManager());
412     }
413 
414     fIdentityConstraints->addElement(ic);
415 }
416 
getIdentityConstraintCount() const417 inline XMLSize_t SchemaElementDecl::getIdentityConstraintCount() const {
418 
419     if (fIdentityConstraints) {
420         return fIdentityConstraints->size();
421     }
422 
423     return 0;
424 }
425 
426 inline IdentityConstraint*
getIdentityConstraintAt(XMLSize_t index) const427 SchemaElementDecl::getIdentityConstraintAt(XMLSize_t index) const {
428 
429     if (fIdentityConstraints) {
430         return fIdentityConstraints->elementAt(index);
431     }
432 
433     return 0;
434 }
435 
436 XERCES_CPP_NAMESPACE_END
437 
438 #endif
439