source: icXML/icXML-devel/src/icxercesc/framework/XMLElementDecl.hpp @ 2721

Last change on this file since 2721 was 2721, checked in by cameron, 6 years ago

Fix imports in icXML modified Xerces files

File size: 19.8 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: XMLElementDecl.hpp 932887 2010-04-11 13:04:59Z borisk $
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_XMLELEMENTDECL_HPP)
23#define XERCESC_INCLUDE_GUARD_XMLELEMENTDECL_HPP
24
25#include <icxercesc/framework/XMLAttr.hpp>
26#include <xercesc/framework/XMLAttDefList.hpp>
27#include <icxercesc/util/XMLString.hpp>
28#include <icxercesc/util/PlatformUtils.hpp>
29#include <xercesc/internal/XSerializable.hpp>
30#include <icxmlc/XMLConfig.hpp>
31
32XERCES_CPP_NAMESPACE_BEGIN
33
34class ContentSpecNode;
35class XMLContentModel;
36struct XMLSymbol;
37template<class XMLScanner> class XMLParserImpl;
38
39/**
40 *  This class defines the core information of an element declaration. Each
41 *  validator (DTD, Schema, etc...) will have its own information that it
42 *  associations with the declaration of an element, but they must all share
43 *  at least this core information, i.e. they must all derive from this
44 *  class. The set of info enforced at this level is driven by the needs of
45 *  XML 1.0 spec validation and well formedness checks.
46 *
47 *  This class defines some special element id values for invalid elements
48 *  and PCDATA elements, as well as a string for the special PCDATA element
49 *  name. All validators must honor these special values in order to allow
50 *  content models to work generically (i.e. to let code know when its dealing
51 *  with invalid or PCDATA element ids without having to know what type of
52 *  validator its messing with.)
53 */
54class XMLPARSER_EXPORT XMLElementDecl : public XSerializable, public XMemory
55{
56        template<class XMLScanner> friend class XMLParserImpl;
57
58public:
59        // -----------------------------------------------------------------------
60        //  Class specific types
61        //
62        //  CreateReasons
63        //      This type is used to store how an element declaration got into
64        //      the grammar's element pool. They are faulted in for various
65        //      reasons.
66        //
67        //  LookupOpts
68        //      These are the values used by the attribute lookup methods.
69        //
70        //  CharDataOpts
71        //      This is used to indicate how this type of element reacts to
72        //      character data as content.
73        // -----------------------------------------------------------------------
74        enum CreateReasons
75        {
76                NoReason
77                , Declared
78                , AttList
79                , InContentModel
80                , AsRootElem
81                , JustFaultIn
82        };
83
84        enum CharDataOpts
85        {
86                NoCharData
87                , SpacesOk
88                , AllCharData
89        };
90
91        friend struct XMLSymbol;
92
93        // -----------------------------------------------------------------------
94        //  Public static data
95        //
96        //  fgInvalidElemId
97        //      A value to represent an invalid element node id.
98        //
99        //  fgPCDataElemId
100        //      This is the value to use to represent a PCDATA node when an
101        //      element id is required.
102        //
103        //  fgPCDataElemName
104        //      This is the value to use to represent a PCDATA node when an
105        //      element name is required.
106        // -----------------------------------------------------------------------
107        static const unsigned int   fgInvalidElemId;
108        static const unsigned int   fgPCDataElemId;
109        static const XMLCh          fgPCDataElemName[];
110
111
112
113        // -----------------------------------------------------------------------
114        //  Destructor
115        // -----------------------------------------------------------------------
116        /** @name Destructor */
117        //@{
118        virtual ~XMLElementDecl();
119        //@}
120
121
122        // -----------------------------------------------------------------------
123        //  The virtual element decl interface
124        // -----------------------------------------------------------------------
125
126        /** @name Virual ElementDecl interface */
127        //@{
128
129        /** Get a list of attributes defined for this element.
130          *
131          * The derived class should return a reference to some member object which
132          * implements the XMLAttDefList interface. This object gives the scanner the
133          * ability to look through the attributes defined for this element.
134          *
135          * It is done this way for efficiency, though of course this is not thread
136          * safe. The scanner guarantees that it won't ever call this method in any
137          * nested way, but the outside world must be careful about when it calls
138          * this method, and optimally never would.
139          */
140        virtual XMLAttDefList& getAttDefList() const = 0;
141
142        /** The character data options for this element type
143          *
144          * The derived class should return an appropriate character data opts value
145          * which correctly represents its tolerance towards whitespace or character
146          * data inside of its instances. This allows the scanner to do all of the
147          * validation of character data.
148          */
149        virtual CharDataOpts getCharDataOpts() const = 0;
150
151        /** Indicate whether this element type defined any attributes
152          *
153          * The derived class should return a boolean that indicates whether this
154          * element has any attributes defined for it or not. This is an optimization
155          * that allows the scanner to skip some work if no attributes exist.
156          */
157        virtual bool hasAttDefs() const = 0;
158
159        /** Get a pointer to the content spec node
160          *
161          * This method will return a const pointer to the content spec node object
162          * of this element.
163          *
164          * @return A const pointer to the element's content spec node
165          */
166        virtual const ContentSpecNode* getContentSpec() const = 0;
167
168        /** Get a pointer to the content spec node
169          *
170          * This method is identical to the previous one, except that it is non
171          * const.
172          */
173        virtual ContentSpecNode* getContentSpec() = 0;
174
175        /** Set the content spec node object for this element type
176          *
177          * This method will adopt the based content spec node object. This is called
178          * by the actual validator which is parsing its DTD or Schema or whatever
179          * and store it on the element decl object via this method.
180          *
181          * @param  toAdopt This method will adopt the passed content node spec
182          *         object. Any previous object is destroyed.
183          */
184        virtual void setContentSpec(ContentSpecNode* toAdopt) = 0;
185
186        /** Get a pointer to the abstract content model
187          *
188          * This method will return a const pointer to the content model object
189          * of this element. This class is a simple abstraction that allows an
190          * element to define and use multiple, specialized content model types
191          * internally but still allow the outside world to do simple stuff with
192          * them.
193          *
194          * @return A pointer to the element's content model, via the basic
195          * abstract content model type.
196          */
197        virtual XMLContentModel* getContentModel() = 0;
198
199        /** Set the content model object for this element type
200          *
201          * This method will adopt the based content model object. This is called
202          * by the actual validator which is parsing its DTD or Schema or whatever
203          * a creating an element decl. It will build what it feels is the correct
204          * content model type object and store it on the element decl object via
205          * this method.
206          *
207          * @param  newModelToAdopt This method will adopt the passed content model
208          *         object. Any previous object is destroyed.
209          */
210        virtual void setContentModel(XMLContentModel* const newModelToAdopt) = 0;
211
212        /** Geta formatted string of the content model
213          *
214          * This method is a convenience method which will create a formatted
215          * representation of the content model of the element. It will not always
216          * exactly recreate the original model, since some normalization or
217          * or reformatting may occur. But, it will be a technically accurate
218          * representation of the original content model.
219          *
220          * @return A pointer to an internal buffer which contains the formatted
221          *         content model. The caller does not own this buffer and should
222          *         copy it if it needs to be kept around.
223          */
224        virtual const XMLCh* getFormattedContentModel ()   const = 0;
225
226        //@}
227
228
229        // -----------------------------------------------------------------------
230        //  Getter methods
231        // -----------------------------------------------------------------------
232
233        /** @name Getter methods */
234        //@{
235
236        /** Get the base name of this element type.
237          *
238          * Return the base name part of the element's name. This is the
239          * same regardless of whether namespaces are enabled or not.
240          *
241          * @return A const pointer to the base name of the element decl.
242          */
243        const XMLCh* getBaseName() const;
244        XMLCh* getBaseName();
245        const XMLCh* getLocalPart() const;
246        XMLCh* getLocalPart();
247
248
249        /** Get the URI id of this element type.
250          *
251          * Return the URI Id of this element.
252          *
253          * @return The URI Id of the element decl, or the emptyNamespaceId if not applicable.
254          */
255        unsigned int getURI() const;
256
257        /** Get the QName of this element type.
258          *
259          * Return the QName part of the element's name.  This is the
260          * same regardless of whether namespaces are enabled or not.
261          *
262          * @return A const pointer to the QName of the element decl.
263          */
264        const QName* getElementName() const;
265        QName* getElementName();
266
267        /** Get the full name of this element type.
268          *
269          * Return the full name of the element. If namespaces
270          * are not enabled, then this is the qName. Else it is the {uri}baseName
271          * form. For those validators that always require namespace processing, it
272          * will always be in the latter form because namespace processing will always
273          * be on.
274          */
275        const XMLCh* getFullName() const;
276        const XMLCh* getRawName() const;
277
278        /** Get the create reason for this element type
279          *
280          * This method returns an enumeration which indicates why this element
281          * declaration exists. Elements can be used before they are actually
282          * declared, so they will often be faulted into the pool and marked as
283          * to why they are there.
284          *
285          * @return An enumerated value that indicates the reason why this element
286          * was added to the element decl pool.
287          */
288
289        CreateReasons getCreateReason() const;
290
291        /** Get the element decl pool id for this element type
292          *
293          * This method will return the element decl pool id of this element
294          * declaration. This uniquely identifies this element type within the
295          * parse event that it is declared within. This value is assigned by the
296          * grammar whose decl pool this object belongs to.
297          *
298          * @return The element decl id of this element declaration.
299          */
300        XMLSize_t getId() const;
301
302        /** Indicate whether this element type has been declared yet
303          *
304          * This method returns a boolean that indicates whether this element
305          * has been declared yet. There are a number of reasons why an element
306          * declaration can be faulted in, but eventually it must be declared or
307          * its an error. See the CreateReasons enumeration.
308          *
309          * @return true if this element has been declared, else false.
310          */
311        bool isDeclared() const;
312
313        /** Indicate whether this element type has been declared externally
314          *
315          * This method returns a boolean that indicates whether this element
316          * has been declared externally.
317          *
318          * @return true if this element has been declared externally, else false.
319          */
320
321        bool isExternal() const;
322
323        /** Get the memory manager
324          *
325          * This method returns the configurable memory manager used by the
326          * element declaration for dynamic allocation/deallocation.
327          *
328          * @return the memory manager
329          */
330        MemoryManager* getMemoryManager() const;
331
332        //@}
333
334
335        // -----------------------------------------------------------------------
336        //  Setter methods
337        // -----------------------------------------------------------------------
338
339        /** @name Setter methods */
340        //@{
341
342        /** Set the element name object for this element type
343          *
344          * This method will adopt the based content spec node object. This is called
345          * by the actual validator which is parsing its DTD or Schema or whatever
346          * and store it on the element decl object via this method.
347          *
348          * @param  prefix       Prefix of the element
349          * @param  localPart    Base Name of the element
350          * @param  uriId        The uriId of the element
351          */
352          void setElementName(const XMLCh* const       prefix
353                                                , const XMLCh* const       localPart
354                                                , const int                uriId );
355
356        /** Set the element name object for this element type
357          *
358          * This method will adopt the based content spec node object. This is called
359          * by the actual validator which is parsing its DTD or Schema or whatever
360          * and store it on the element decl object via this method.
361          *
362          * @param  rawName      Full Name of the element
363          * @param  uriId        The uriId of the element
364          */
365          void setElementName(const XMLCh* const    rawName
366                                                , const int             uriId );
367
368        /** Set the element name object for this element type
369          *
370          * This method will adopt the based content spec node object. This is called
371          * by the actual validator which is parsing its DTD or Schema or whatever
372          * and store it on the element decl object via this method.
373          *
374          * @param  elementName  QName of the element
375          */
376          void setElementName(const QName* const    elementName);
377
378        /** Update the create reason for this element type.
379          *
380          * This method will update the 'create reason' field for this element
381          * decl object. As the validator parses its DTD, Schema, etc... it will
382          * encounter various references to an element declaration, which will
383          * cause the element declaration to either be declared or to be faulted
384          * into the pool in preparation for some future declaration. As it does
385          * so,it will update this field to indicate the current status of the
386          * decl object.
387          */
388        void setCreateReason(const CreateReasons newReason);
389
390        /** Set the element decl pool id for this element type
391          *
392          * This method will set the pool id of this element decl. This is called
393          * by the grammar which created this object, and will provide this
394          * decl object with a unique id within the parse event that created it.
395          */
396        void setId(const XMLSize_t newId);
397
398
399        /** Set the element decl to indicate external declaration
400          *
401          */
402        void setExternalElemDeclaration(const bool aValue);
403
404        //@}
405
406
407        // -----------------------------------------------------------------------
408        //  Miscellaneous methods
409        // -----------------------------------------------------------------------
410
411        /** @name Miscellaneous methods */
412        //@{
413
414        //@}
415
416        /***
417         * Support for Serialization/De-serialization
418         ***/
419        DECL_XSERIALIZABLE(XMLElementDecl)
420
421        enum objectType
422        {
423                Schema
424          , DTD
425          , UnKnown
426        };
427
428        virtual XMLElementDecl::objectType  getObjectType() const = 0;
429
430        static void            storeElementDecl(XSerializeEngine&        serEng
431                                                                                  , XMLElementDecl*    const element);
432
433        static XMLElementDecl* loadElementDecl(XSerializeEngine& serEng);
434
435protected :
436        // -----------------------------------------------------------------------
437        //  Hidden constructors
438        // -----------------------------------------------------------------------
439        XMLElementDecl(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
440
441private :
442        // -----------------------------------------------------------------------
443        //  Unimplemented constructors and operators
444        // -----------------------------------------------------------------------
445        XMLElementDecl(const XMLElementDecl&);
446        XMLElementDecl& operator=(const XMLElementDecl&);
447
448
449        // -----------------------------------------------------------------------
450        //  Data members
451        //
452        //  fElementName
453        //      This is the name of the element decl.
454        //
455        //  fCreateReason
456        //      We sometimes have to put an element decl object into the elem
457        //      decl pool before the element's declaration is seen, such as when
458        //      its used in another element's content model or an att list is
459        //      seen for it. This flag tells us whether its been declared, and
460        //      if not why it had to be created.
461        //
462        //  fId
463        //      The unique id of this element. This is created by the derived
464        //      class, or more accurately the grammar that owns the objects
465        //      of the derived types. But, since they all have to have them, we
466        //      let them all store the id here. It is defaulted to have the
467        //      value fgInvalidElem until explicitly set.
468        //
469        //  fExternalElement
470        //      This flag indicates whether or the element was declared externally.
471        // -----------------------------------------------------------------------
472        MemoryManager*      fMemoryManager;
473        const QName *       fElementName;
474        CreateReasons       fCreateReason;
475        XMLSize_t           fId;
476        unsigned int            fUriId;
477        bool                fExternalElement;
478};
479
480// ---------------------------------------------------------------------------
481//  XMLElementDecl: Getter methods
482// ---------------------------------------------------------------------------
483inline const XMLCh* XMLElementDecl::getBaseName() const
484{
485        return fElementName->getLocalPart();
486}
487
488inline XMLCh* XMLElementDecl::getBaseName()
489{
490        return const_cast<QName*>(fElementName)->getLocalPart();
491}
492
493inline const XMLCh* XMLElementDecl::getLocalPart() const
494{
495        return fElementName->getLocalPart();
496}
497
498inline XMLCh* XMLElementDecl::getLocalPart()
499{
500        return const_cast<QName*>(fElementName)->getLocalPart();
501}
502
503inline unsigned int XMLElementDecl::getURI() const
504{
505        return fUriId;
506}
507
508inline const QName* XMLElementDecl::getElementName() const
509{
510        QName * elementName = const_cast<QName*>(fElementName);
511        elementName->setURI(getURI());
512        return elementName;
513}
514
515inline QName* XMLElementDecl::getElementName()
516{
517        QName * elementName = const_cast<QName*>(fElementName);
518        elementName->setURI(getURI());
519        return elementName;
520}
521
522inline const XMLCh* XMLElementDecl::getFullName() const
523{
524        return fElementName->getRawName();
525}
526
527inline const XMLCh* XMLElementDecl::getRawName() const
528{
529        return fElementName->getRawName();
530}
531
532inline XMLElementDecl::CreateReasons XMLElementDecl::getCreateReason() const
533{
534        return fCreateReason;
535}
536
537inline XMLSize_t XMLElementDecl::getId() const
538{
539        return fId;
540}
541
542inline bool XMLElementDecl::isDeclared() const
543{
544        return (fCreateReason == Declared);
545}
546
547
548inline bool XMLElementDecl::isExternal() const
549{
550        return fExternalElement;
551}
552
553inline MemoryManager* XMLElementDecl::getMemoryManager() const
554{
555        return fMemoryManager;
556}
557
558// ---------------------------------------------------------------------------
559//  XMLElementDecl: Setter methods
560// ---------------------------------------------------------------------------
561inline void
562XMLElementDecl::setCreateReason(const XMLElementDecl::CreateReasons newReason)
563{
564        fCreateReason = newReason;
565}
566
567inline void XMLElementDecl::setId(const XMLSize_t newId)
568{
569        fId = newId;
570}
571
572
573inline void XMLElementDecl::setExternalElemDeclaration(const bool aValue)
574{
575        fExternalElement = aValue;
576}
577
578#ifdef PRINT_DEBUG_MESSAGE
579static std::ostream & operator << (std::ostream & out, const XMLElementDecl & elemDecl)
580{
581    out << '('
582        << elemDecl.getId()
583        << ',' << elemDecl.getFullName()
584        << ',' << elemDecl.getURI()
585        //<< ',' << elemDecl.getContentModel()
586        //<< ',' << elemDecl.getContentSpec()
587        << ',' << elemDecl.getObjectType()
588        << ',' << elemDecl.getProtoType()
589        << ',';
590
591    switch (elemDecl.getCreateReason())
592    {
593        case XMLElementDecl::NoReason: out << "NoReason"; break;
594        case XMLElementDecl::Declared: out << "Declared"; break;
595        case XMLElementDecl::AttList: out << "AttList"; break;
596        case XMLElementDecl::InContentModel: out << "InContentModel"; break;
597        case XMLElementDecl::AsRootElem: out << "AsRootElem"; break;
598        case XMLElementDecl::JustFaultIn: out << "JustFaultIn"; break;
599    }
600
601    out << ')';
602    return out;
603}
604
605static std::ostream & operator << (std::ostream & out, const XMLElementDecl* elemDecl)
606{
607    if (likely(elemDecl != 0))
608        {
609        out << *elemDecl;
610        }
611        return out;
612}
613
614#endif
615
616XERCES_CPP_NAMESPACE_END
617
618#endif
Note: See TracBrowser for help on using the repository browser.