source: NonGTP/Xerces/xerces-c_2_8_0/include/xercesc/framework/XMLElementDecl.hpp @ 2674

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