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: ComplexTypeInfo.hpp 932887 2010-04-11 13:04:59Z borisk $
20  */
21 
22 #if !defined(XERCESC_INCLUDE_GUARD_COMPLEXTYPEINFO_HPP)
23 #define XERCESC_INCLUDE_GUARD_COMPLEXTYPEINFO_HPP
24 
25 
26 /**
27   * The class act as a place holder to store complex type information.
28   *
29   * The class is intended for internal use.
30   */
31 
32 // ---------------------------------------------------------------------------
33 //  Includes
34 // ---------------------------------------------------------------------------
35 #include <xercesc/util/XMLString.hpp>
36 #include <xercesc/util/RefHash2KeysTableOf.hpp>
37 #include <xercesc/util/RefVectorOf.hpp>
38 #include <xercesc/framework/XMLElementDecl.hpp>
39 #include <xercesc/framework/XMLContentModel.hpp>
40 #include <xercesc/validators/schema/SchemaAttDef.hpp>
41 #include <xercesc/internal/XSerializable.hpp>
42 
43 XERCES_CPP_NAMESPACE_BEGIN
44 
45 
46 // ---------------------------------------------------------------------------
47 //  Forward Declarations
48 // ---------------------------------------------------------------------------
49 class DatatypeValidator;
50 class ContentSpecNode;
51 class SchemaAttDefList;
52 class SchemaElementDecl;
53 class XSDLocator;
54 
55 
56 class VALIDATORS_EXPORT ComplexTypeInfo : public XSerializable, public XMemory
57 {
58 public:
59     // -----------------------------------------------------------------------
60     //  Public Constructors/Destructor
61     // -----------------------------------------------------------------------
62     ComplexTypeInfo(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
63     ~ComplexTypeInfo();
64 
65     // -----------------------------------------------------------------------
66     //  Getter methods
67     // -----------------------------------------------------------------------
68     bool                     getAbstract() const;
69     bool                     getAdoptContentSpec() const;
70     bool                     containsAttWithTypeId() const;
71     bool                     getPreprocessed() const;
72     int                      getDerivedBy() const;
73     int                      getBlockSet() const;
74     int                      getFinalSet() const;
75     unsigned int             getScopeDefined() const;
76     unsigned int             getElementId() const;
77     int                      getContentType() const;
78     XMLSize_t                elementCount() const;
79     XMLCh*                   getTypeName() const;
80     DatatypeValidator*       getBaseDatatypeValidator() const;
81     DatatypeValidator*       getDatatypeValidator() const;
82     ComplexTypeInfo*         getBaseComplexTypeInfo() const;
83     ContentSpecNode*         getContentSpec() const;
84     const SchemaAttDef*      getAttWildCard() const;
85     SchemaAttDef*            getAttWildCard();
86     const SchemaAttDef*      getAttDef(const XMLCh* const baseName,
87                                        const int uriId) const;
88     SchemaAttDef*            getAttDef(const XMLCh* const baseName,
89                                        const int uriId);
90     XMLAttDefList&           getAttDefList() const;
91     const SchemaElementDecl* elementAt(const XMLSize_t index) const;
92     SchemaElementDecl*       elementAt(const XMLSize_t index);
93     XMLContentModel*         getContentModel(const bool checkUPA = false);
94     const XMLCh*             getFormattedContentModel ()   const;
95     XSDLocator*              getLocator() const;
96     const XMLCh*             getTypeLocalName() const;
97     const XMLCh*             getTypeUri() const;
98 
99     /**
100      * returns true if this type is anonymous
101      **/
102     bool getAnonymous() const;
103 
104     // -----------------------------------------------------------------------
105     //  Setter methods
106     // -----------------------------------------------------------------------
107     void setAbstract(const bool isAbstract);
108     void setAdoptContentSpec(const bool toAdopt);
109     void setAttWithTypeId(const bool value);
110     void setPreprocessed(const bool aValue = true);
111     void setDerivedBy(const int derivedBy);
112     void setBlockSet(const int blockSet);
113     void setFinalSet(const int finalSet);
114     void setScopeDefined(const unsigned int scopeDefined);
115     void setElementId(const unsigned int elemId);
116     void setTypeName(const XMLCh* const typeName);
117     void setContentType(const int contentType);
118     void setBaseDatatypeValidator(DatatypeValidator* const baseValidator);
119     void setDatatypeValidator(DatatypeValidator* const validator);
120     void setBaseComplexTypeInfo(ComplexTypeInfo* const typeInfo);
121     void setContentSpec(ContentSpecNode* const toAdopt);
122     void setAttWildCard(SchemaAttDef* const toAdopt);
123     void addAttDef(SchemaAttDef* const toAdd);
124     void addElement(SchemaElementDecl* const toAdd);
125     void setLocator(XSDLocator* const aLocator);
126 
127     /**
128      * sets this type to be anonymous
129      **/
130     void setAnonymous();
131 
132     // -----------------------------------------------------------------------
133     //  Helper methods
134     // -----------------------------------------------------------------------
135     bool hasAttDefs() const;
136     bool contains(const XMLCh* const attName);
137     void checkUniqueParticleAttribution
138     (
139         SchemaGrammar*    const pGrammar
140       , GrammarResolver*  const pGrammarResolver
141       , XMLStringPool*    const pStringPool
142       , XMLValidator*     const pValidator
143     ) ;
144 
145     /**
146       * Return a singleton that represents 'anyType'
147       *
148       * @param emptyNSId the uri id of the empty namespace
149       */
150     static ComplexTypeInfo* getAnyType(unsigned int emptyNSId);
151 
152     /**
153       *  Notification that lazy data has been deleted
154       */
155     static void reinitAnyType();
156 
157     /***
158      * Support for Serialization/De-serialization
159      ***/
160     DECL_XSERIALIZABLE(ComplexTypeInfo)
161 
162 private:
163     // -----------------------------------------------------------------------
164     //  Unimplemented constructors and operators
165     // -----------------------------------------------------------------------
166     ComplexTypeInfo(const ComplexTypeInfo& elemInfo);
167     ComplexTypeInfo& operator= (const ComplexTypeInfo& other);
168 
169     // -----------------------------------------------------------------------
170     //  Private helper methods
171     // -----------------------------------------------------------------------
172     void faultInAttDefList() const;
173     bool useRepeatingLeafNodes(ContentSpecNode* particle);
174     XMLContentModel* makeContentModel(bool checkUPA = false);
175     XMLCh* formatContentModel () const ;
176     ContentSpecNode* expandContentModel(ContentSpecNode* const curNode, int minOccurs, int maxOccurs, bool bAllowCompactSyntax);
177     ContentSpecNode* convertContentSpecTree(ContentSpecNode* const curNode, bool checkUPA, bool bAllowCompactSyntax);
178     void resizeContentSpecOrgURI();
179 
180     // -----------------------------------------------------------------------
181     //  Private data members
182     // -----------------------------------------------------------------------
183     bool                               fAnonymous;
184     bool                               fAbstract;
185     bool                               fAdoptContentSpec;
186     bool                               fAttWithTypeId;
187     bool                               fPreprocessed;
188     int                                fDerivedBy;
189     int                                fBlockSet;
190     int                                fFinalSet;
191     unsigned int                       fScopeDefined;
192     int                                fContentType;
193 
194     unsigned int                       fElementId;
195     unsigned int                       fUniqueURI;
196     unsigned int                       fContentSpecOrgURISize;
197 
198     XMLCh*                             fTypeName;
199     XMLCh*                             fTypeLocalName;
200     XMLCh*                             fTypeUri;
201     DatatypeValidator*                 fBaseDatatypeValidator;
202     DatatypeValidator*                 fDatatypeValidator;
203     ComplexTypeInfo*                   fBaseComplexTypeInfo;
204     ContentSpecNode*                   fContentSpec;
205     SchemaAttDef*                      fAttWildCard;
206     SchemaAttDefList*                  fAttList;
207     RefVectorOf<SchemaElementDecl>*    fElements;
208     RefHash2KeysTableOf<SchemaAttDef>* fAttDefs;
209     XMLContentModel*                   fContentModel;
210     XMLCh*                             fFormattedModel;
211     unsigned int*                      fContentSpecOrgURI;
212     XSDLocator*                        fLocator;
213     MemoryManager*                     fMemoryManager;
214 
215     static ComplexTypeInfo*            fAnyType;
216 
217     friend class XMLInitializer;
218 };
219 
220 // ---------------------------------------------------------------------------
221 //  ComplexTypeInfo: Getter methods
222 // ---------------------------------------------------------------------------
getAbstract() const223 inline bool ComplexTypeInfo::getAbstract() const {
224 
225     return fAbstract;
226 }
227 
getAdoptContentSpec() const228 inline bool ComplexTypeInfo::getAdoptContentSpec() const {
229 
230     return fAdoptContentSpec;
231 }
232 
containsAttWithTypeId() const233 inline bool ComplexTypeInfo::containsAttWithTypeId() const {
234 
235     return fAttWithTypeId;
236 }
237 
getPreprocessed() const238 inline bool ComplexTypeInfo::getPreprocessed() const {
239 
240     return fPreprocessed;
241 }
242 
getDerivedBy() const243 inline int ComplexTypeInfo::getDerivedBy() const {
244 
245     return fDerivedBy;
246 }
247 
getBlockSet() const248 inline int ComplexTypeInfo::getBlockSet() const {
249 
250     return fBlockSet;
251 }
252 
getFinalSet() const253 inline int ComplexTypeInfo::getFinalSet() const {
254 
255     return fFinalSet;
256 }
257 
getScopeDefined() const258 inline unsigned int ComplexTypeInfo::getScopeDefined() const {
259 
260     return fScopeDefined;
261 }
262 
getElementId() const263 inline unsigned int ComplexTypeInfo::getElementId() const {
264 
265     return fElementId;
266 }
267 
getContentType() const268 inline int ComplexTypeInfo::getContentType() const {
269 
270     return fContentType;
271 }
272 
elementCount() const273 inline XMLSize_t ComplexTypeInfo::elementCount() const {
274 
275     if (fElements) {
276         return fElements->size();
277     }
278 
279     return 0;
280 }
281 
getTypeName() const282 inline XMLCh* ComplexTypeInfo::getTypeName() const {
283     return fTypeName;
284 }
285 
getBaseDatatypeValidator() const286 inline DatatypeValidator* ComplexTypeInfo::getBaseDatatypeValidator() const {
287 
288     return fBaseDatatypeValidator;
289 }
290 
getDatatypeValidator() const291 inline DatatypeValidator* ComplexTypeInfo::getDatatypeValidator() const {
292 
293     return fDatatypeValidator;
294 }
295 
getBaseComplexTypeInfo() const296 inline ComplexTypeInfo* ComplexTypeInfo::getBaseComplexTypeInfo() const {
297 
298     return fBaseComplexTypeInfo;
299 }
300 
getContentSpec() const301 inline ContentSpecNode* ComplexTypeInfo::getContentSpec() const {
302 
303     return fContentSpec;
304 }
305 
getAttWildCard() const306 inline const SchemaAttDef* ComplexTypeInfo::getAttWildCard() const {
307 
308     return fAttWildCard;
309 }
310 
getAttWildCard()311 inline SchemaAttDef* ComplexTypeInfo::getAttWildCard() {
312 
313     return fAttWildCard;
314 }
315 
getAttDef(const XMLCh * const baseName,const int uriId) const316 inline const SchemaAttDef* ComplexTypeInfo::getAttDef(const XMLCh* const baseName,
317                                                       const int uriId) const {
318 
319     return fAttDefs->get(baseName, uriId);
320 }
321 
getAttDef(const XMLCh * const baseName,const int uriId)322 inline SchemaAttDef* ComplexTypeInfo::getAttDef(const XMLCh* const baseName,
323                                                 const int uriId)
324 {
325     return fAttDefs->get(baseName, uriId);
326 }
327 
328 inline SchemaElementDecl*
elementAt(const XMLSize_t index)329 ComplexTypeInfo::elementAt(const XMLSize_t index) {
330 
331     if (!fElements) {
332         return 0; // REVISIT - need to throw an exception
333     }
334 
335     return fElements->elementAt(index);
336 }
337 
338 inline const SchemaElementDecl*
elementAt(const XMLSize_t index) const339 ComplexTypeInfo::elementAt(const XMLSize_t index) const {
340 
341     if (!fElements) {
342         return 0; // REVISIT - need to throw an exception
343     }
344 
345     return fElements->elementAt(index);
346 }
347 
getContentModel(const bool checkUPA)348 inline XMLContentModel* ComplexTypeInfo::getContentModel(const bool checkUPA)
349 {
350     if (!fContentModel && fContentSpec)
351         fContentModel = makeContentModel(checkUPA);
352 
353     return fContentModel;
354 }
355 
getLocator() const356 inline XSDLocator* ComplexTypeInfo::getLocator() const
357 {
358     return fLocator;
359 }
360 
getAnonymous() const361 inline bool ComplexTypeInfo::getAnonymous() const {
362     return fAnonymous;
363 }
364 
getTypeLocalName() const365 inline const XMLCh* ComplexTypeInfo::getTypeLocalName() const
366 {
367     return fTypeLocalName;
368 }
369 
getTypeUri() const370 inline const XMLCh* ComplexTypeInfo::getTypeUri() const
371 {
372    return fTypeUri;
373 }
374 
375 // ---------------------------------------------------------------------------
376 //  ComplexTypeInfo: Setter methods
377 // ---------------------------------------------------------------------------
setAbstract(const bool isAbstract)378 inline void ComplexTypeInfo::setAbstract(const bool isAbstract) {
379 
380     fAbstract = isAbstract;
381 }
382 
setAdoptContentSpec(const bool toAdopt)383 inline void ComplexTypeInfo::setAdoptContentSpec(const bool toAdopt) {
384 
385     fAdoptContentSpec = toAdopt;
386 }
387 
setAttWithTypeId(const bool value)388 inline void ComplexTypeInfo::setAttWithTypeId(const bool value) {
389 
390     fAttWithTypeId = value;
391 }
392 
setPreprocessed(const bool aValue)393 inline void ComplexTypeInfo::setPreprocessed(const bool aValue) {
394 
395     fPreprocessed = aValue;
396 }
397 
setDerivedBy(const int derivedBy)398 inline void ComplexTypeInfo::setDerivedBy(const int derivedBy) {
399 
400     fDerivedBy = derivedBy;
401 }
402 
setBlockSet(const int blockSet)403 inline void ComplexTypeInfo::setBlockSet(const int blockSet) {
404 
405     fBlockSet = blockSet;
406 }
407 
setFinalSet(const int finalSet)408 inline void ComplexTypeInfo::setFinalSet(const int finalSet) {
409 
410     fFinalSet = finalSet;
411 }
412 
setScopeDefined(const unsigned int scopeDefined)413 inline void ComplexTypeInfo::setScopeDefined(const unsigned int scopeDefined) {
414 
415     fScopeDefined = scopeDefined;
416 }
417 
setElementId(const unsigned int elemId)418 inline void ComplexTypeInfo::setElementId(const unsigned int elemId) {
419 
420     fElementId = elemId;
421 }
422 
423 inline void
setContentType(const int contentType)424 ComplexTypeInfo::setContentType(const int contentType) {
425 
426     fContentType = contentType;
427 }
428 
setTypeName(const XMLCh * const typeName)429 inline void ComplexTypeInfo::setTypeName(const XMLCh* const typeName) {
430 
431     fMemoryManager->deallocate(fTypeName);//delete [] fTypeName;
432     fMemoryManager->deallocate(fTypeLocalName);//delete [] fTypeLocalName;
433     fMemoryManager->deallocate(fTypeUri);//delete [] fTypeUri;
434 
435     if (typeName)
436     {
437         fTypeName = XMLString::replicate(typeName, fMemoryManager);
438 
439         int index = XMLString::indexOf(fTypeName, chComma);
440         XMLSize_t length = XMLString::stringLen(fTypeName);
441         fTypeLocalName = (XMLCh*) fMemoryManager->allocate
442         (
443             (length - index + 1) * sizeof(XMLCh)
444         ); //new XMLCh[length - index + 1];
445         XMLString::subString(fTypeLocalName, fTypeName, index + 1, length, fMemoryManager);
446 
447         fTypeUri = (XMLCh*) fMemoryManager->allocate
448         (
449             (index + 1) * sizeof(XMLCh)
450         ); //new XMLCh[index + 1];
451         XMLString::subString(fTypeUri, fTypeName, 0, index, fMemoryManager);
452     }
453     else
454     {
455         fTypeName = fTypeLocalName = fTypeUri = 0;
456     }
457 }
458 
459 inline void
setBaseDatatypeValidator(DatatypeValidator * const validator)460 ComplexTypeInfo::setBaseDatatypeValidator(DatatypeValidator* const validator) {
461 
462     fBaseDatatypeValidator = validator;
463 }
464 
465 inline void
setDatatypeValidator(DatatypeValidator * const validator)466 ComplexTypeInfo::setDatatypeValidator(DatatypeValidator* const validator) {
467 
468     fDatatypeValidator = validator;
469 }
470 
471 inline void
setBaseComplexTypeInfo(ComplexTypeInfo * const typeInfo)472 ComplexTypeInfo::setBaseComplexTypeInfo(ComplexTypeInfo* const typeInfo) {
473 
474     fBaseComplexTypeInfo = typeInfo;
475 }
476 
addElement(SchemaElementDecl * const elem)477 inline void ComplexTypeInfo::addElement(SchemaElementDecl* const elem) {
478 
479     if (!fElements) {
480         fElements = new (fMemoryManager) RefVectorOf<SchemaElementDecl>(8, false, fMemoryManager);
481     }
482     else if (fElements->containsElement(elem)) {
483         return;
484     }
485 
486     fElements->addElement(elem);
487 }
488 
setAttWildCard(SchemaAttDef * const toAdopt)489 inline void ComplexTypeInfo::setAttWildCard(SchemaAttDef* const toAdopt) {
490 
491     if (fAttWildCard) {
492        delete fAttWildCard;
493     }
494 
495     fAttWildCard = toAdopt;
496 }
497 
setAnonymous()498 inline void ComplexTypeInfo::setAnonymous() {
499     fAnonymous = true;
500 }
501 
502 // ---------------------------------------------------------------------------
503 //  ComplexTypeInfo: Helper methods
504 // ---------------------------------------------------------------------------
hasAttDefs() const505 inline bool ComplexTypeInfo::hasAttDefs() const
506 {
507     return !fAttDefs->isEmpty();
508 }
509 
contains(const XMLCh * const attName)510 inline bool ComplexTypeInfo::contains(const XMLCh* const attName) {
511 
512     RefHash2KeysTableOfEnumerator<SchemaAttDef> enumDefs(fAttDefs, false, fMemoryManager);
513 
514     while (enumDefs.hasMoreElements()) {
515 
516         if (XMLString::equals(attName, enumDefs.nextElement().getAttName()->getLocalPart())) {
517             return true;
518         }
519     }
520 
521     return false;
522 }
523 
524 XERCES_CPP_NAMESPACE_END
525 
526 #endif
527 
528 /**
529   * End of file ComplexTypeInfo.hpp
530   */
531 
532