Coverage Report

Created: 2026-07-25 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xerces-c/src/xercesc/framework/XMLAttDef.hpp
Line
Count
Source
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: XMLAttDef.hpp 932889 2010-04-11 13:10:10Z borisk $
20
 */
21
22
#if !defined(XERCESC_INCLUDE_GUARD_XMLATTDEF_HPP)
23
#define XERCESC_INCLUDE_GUARD_XMLATTDEF_HPP
24
25
#include <xercesc/util/PlatformUtils.hpp>
26
#include <xercesc/util/XMLString.hpp>
27
#include <xercesc/util/XMemory.hpp>
28
#include <xercesc/internal/XSerializable.hpp>
29
30
XERCES_CPP_NAMESPACE_BEGIN
31
32
class XMLAttr;
33
34
/** Represents the core information of an attribute definition
35
 *
36
 *  This class defines the basic characteristics of an attribute, no matter
37
 *  what type of validator is used. If a particular schema associates more
38
 *  information with an attribute it will create a derivative of this class.
39
 *  So this class provides an abstract way to get basic information on
40
 *  attributes from any type of validator.
41
 *
42
 *  This class supports keyed collection semantics on the fully qualified
43
 *  attribute name, by providing a getKey() method to extract the key string.
44
 *  getKey(), in this case, just calls the virtual method getFullName() to
45
 *  get the fully qualified name, as defined by the derived class.
46
 *
47
 *  Note that the 'value' of an attribute type definition is the default or
48
 *  of fixed value given to it in its definition. If the attribute is of the
49
 *  enumerated or notation type, it will have an 'enumeration value' as well
50
 *  which is a space separated list of its possible vlaues.
51
 */
52
class XMLPARSER_EXPORT XMLAttDef : public XSerializable, public XMemory
53
{
54
public:
55
    // -----------------------------------------------------------------------
56
    //  Class specific types
57
    //
58
    //  AttTypes
59
    //      The list of possible types that an attribute can have, according
60
    //      to the XML 1.0 spec and schema.
61
    //
62
    //  DefAttTypes
63
    //      The modifiers that an attribute decl can have, which indicates
64
    //      whether instances of that attributes are required, implied, etc..
65
    //
66
    //  CreateReasons
67
    //      This type is used to store how an attribute declaration got into
68
    //      the elementdecl's attribute pool.
69
    //
70
    // -----------------------------------------------------------------------
71
  enum AttTypes
72
    {
73
        CData               = 0
74
        , ID                = 1
75
        , IDRef             = 2
76
        , IDRefs            = 3
77
        , Entity            = 4
78
        , Entities          = 5
79
        , NmToken           = 6
80
        , NmTokens          = 7
81
        , Notation          = 8
82
        , Enumeration       = 9
83
        , Simple            = 10
84
        , Any_Any           = 11
85
        , Any_Other         = 12
86
        , Any_List          = 13
87
88
        , AttTypes_Count
89
        , AttTypes_Min      = 0
90
        , AttTypes_Max      = 13
91
        , AttTypes_Unknown  = -1
92
  };
93
94
    enum DefAttTypes
95
    {
96
        Default                  = 0
97
        , Fixed                  = 1
98
        , Required               = 2
99
        , Required_And_Fixed     = 3
100
        , Implied                = 4
101
        , ProcessContents_Skip   = 5
102
        , ProcessContents_Lax    = 6
103
        , ProcessContents_Strict = 7
104
        , Prohibited             = 8
105
106
        , DefAttTypes_Count
107
        , DefAttTypes_Min   = 0
108
        , DefAttTypes_Max   = 8
109
        , DefAttTypes_Unknown = -1
110
  };
111
112
    enum CreateReasons
113
    {
114
        NoReason
115
        , JustFaultIn
116
    };
117
118
    // -----------------------------------------------------------------------
119
    //  Public static data members
120
    // -----------------------------------------------------------------------
121
    static const unsigned int fgInvalidAttrId;
122
123
124
    // -----------------------------------------------------------------------
125
    //  Public, static methods
126
    // -----------------------------------------------------------------------
127
128
    /** @name Public, static methods */
129
    //@{
130
131
    /** Get a string representation of the passed attribute type enum
132
      *
133
      * This method allows you to get a textual representation of an attribute
134
      * type, mostly for debug or display.
135
      *
136
      * @param attrType The attribute type value to get the string for.
137
      * @param manager The MemoryManager to use to allocate objects
138
      * @return A const pointer to the static string that holds the text
139
      *         description of the passed type.
140
      */
141
    static const XMLCh* getAttTypeString(const AttTypes attrType
142
        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
143
144
    /** Get a string representation of the passed def attribute type enum
145
      *
146
      * This method allows you to get a textual representation of an default
147
      * attributetype, mostly for debug or display.
148
      *
149
      * @param attrType The default attribute type value to get the string for.
150
      * @param manager The MemoryManager to use to allocate objects
151
      * @return A const pointer to the static string that holds the text
152
      *         description of the passed default type.
153
      */
154
    static const XMLCh* getDefAttTypeString(const DefAttTypes attrType
155
        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
156
157
    //@}
158
159
160
    // -----------------------------------------------------------------------
161
    //  Destructor
162
    // -----------------------------------------------------------------------
163
164
    /** @name Destructor */
165
    //@{
166
167
    /**
168
      *  Destructor
169
      */
170
    virtual ~XMLAttDef();
171
    //@}
172
173
174
    // -----------------------------------------------------------------------
175
    //  The virtual attribute def interface
176
    // -----------------------------------------------------------------------
177
178
    /** @name Virtual interface */
179
    //@{
180
181
    /** Get the full name of this attribute type
182
      *
183
      * The derived class should return a const pointer to the full name of
184
      * this attribute. This will vary depending on the type of validator in
185
      * use.
186
      *
187
      * @return A const pointer to the full name of this attribute type.
188
      */
189
    virtual const XMLCh* getFullName() const = 0;
190
191
    /**
192
     * The derived class should implement any cleaning up required between
193
     * each use of an instance of this class for validation
194
     */
195
    virtual void reset() = 0;
196
197
    //@}
198
199
200
    // -----------------------------------------------------------------------
201
    //  Getter methods
202
    // -----------------------------------------------------------------------
203
204
    /** @name Getter methods */
205
    //@{
206
207
    /** Get the default type of this attribute type
208
      *
209
      * This method returns the 'default type' of the attribute. Default
210
      * type in this case refers to the XML concept of a default type for
211
      * an attribute, i.e. \#FIXED, \#IMPLIED, etc...
212
      *
213
      * @return The default type enum for this attribute type.
214
      */
215
    DefAttTypes getDefaultType() const;
216
217
    /** Get the enumeration value (if any) of this attribute type
218
      *
219
      * If the attribute is of an enumeration or notation type, then this
220
      * method will return a const reference to a string that contains the
221
      * space separated values that can the attribute can have.
222
      *
223
      * @return A const pointer to a string that contains the space separated
224
      *         legal values for this attribute.
225
      */
226
    const XMLCh* getEnumeration() const;
227
228
    /** Get the pool id of this attribute type
229
      *
230
      * This method will return the id of this attribute in the validator's
231
      * attribute pool. It was set by the validator when this attribute was
232
      * created.
233
      *
234
      * @return The pool id of this attribute type.
235
      */
236
    XMLSize_t getId() const;
237
238
    /** Get the type of this attribute
239
      *
240
      * Gets the type of this attribute. This type is represented by an enum
241
      * that converts the types of attributes allowed by XML, e.g. CDATA, NMTOKEN,
242
      * NOTATION, etc...
243
      *
244
      * @return The attribute type enumeration value for this type of
245
      *         attribute.
246
      */
247
    AttTypes getType() const;
248
249
    /** Get the default/fixed value of this attribute (if any.)
250
      *
251
      * If the attribute defined a default/fixed value, then it is stored
252
      * and this method will retrieve it. If it has non, then a null pointer
253
      * is returned.
254
      *
255
      * @return A const pointer to the default/fixed value for this attribute
256
      *         type.
257
      */
258
    const XMLCh* getValue() const;
259
260
    /** Get the create reason for this attribute
261
      *
262
      * This method returns an enumeration which indicates why this attribute
263
      * declaration exists.
264
      *
265
      * @return An enumerated value that indicates the reason why this attribute
266
      * was added to the attribute table.
267
      */
268
    CreateReasons getCreateReason() const;
269
270
    /** Indicate whether this attribute has been declared externally
271
      *
272
      * This method returns a boolean that indicates whether this attribute
273
      * has been declared externally.
274
      *
275
      * @return true if this attribute has been declared externally, else false.
276
      */
277
    bool isExternal() const;
278
279
    /** Get the plugged-in memory manager
280
      *
281
      * This method returns the plugged-in memory manager user for dynamic
282
      * memory allocation/deallocation.
283
      *
284
      * @return the plugged-in memory manager
285
      */
286
    MemoryManager* getMemoryManager() const;
287
288
    //@}
289
290
291
    // -----------------------------------------------------------------------
292
    //  Setter methods
293
    // -----------------------------------------------------------------------
294
295
    /** @name Setter methods */
296
    //@{
297
298
    /** Set the default attribute type
299
      *
300
      * This method sets the default attribute type for this attribute.
301
      * This setting controls whether the attribute is required, fixed,
302
      * implied, etc...
303
      *
304
      * @param  newValue The new default attribute to set
305
      */
306
    void setDefaultType(const XMLAttDef::DefAttTypes newValue);
307
308
    /** Set the pool id for this attribute type.
309
      *
310
      * This method sets the pool id of this attribute type. This is usually
311
      * called by the validator that creates the actual instance (which is of
312
      * a derived type known only by the validator.)
313
      *
314
      * @param  newId The new pool id to set.
315
      */
316
    void setId(const XMLSize_t newId);
317
318
    /** Set the type of this attribute type.
319
      *
320
      * This method will set the type of the attribute. The type of an attribute
321
      * controls how it is normalized and what kinds of characters it can hold.
322
      *
323
      * @param  newValue The new attribute type to set
324
      */
325
    void setType(const XMLAttDef::AttTypes newValue);
326
327
    /** Set the default/fixed value of this attribute type.
328
      *
329
      * This method set the fixed/default value for the attribute. This value
330
      * will be used when instances of this attribute type are faulted in. It
331
      * <b>must</b> be a valid value for the type set by setType(). If the
332
      * type is enumeration or notation, this must be one of the valid values
333
      * set in the setEnumeration() call.
334
      *
335
      * @param  newValue The new fixed/default value to set.
336
      */
337
    void setValue(const XMLCh* const newValue);
338
339
    /** Set the enumerated value of this attribute type.
340
      *
341
      * This method sets the enumerated/notation value list for this attribute
342
      * type. It is a space separated set of possible values. These values must
343
      * meet the constrains of the XML spec for such values of this type of
344
      * attribute. This should only be set if the setType() method is used to
345
      * set the type to the enumeration or notation types.
346
      *
347
      * @param  newValue The new enumerated/notation value list to set.
348
      */
349
    void setEnumeration(const XMLCh* const newValue);
350
351
    /** Update the create reason for this attribute type.
352
      *
353
      * This method will update the 'create reason' field for this attribute
354
      * decl object.
355
      *
356
      * @param  newReason The new create reason.
357
      */
358
    void setCreateReason(const CreateReasons newReason);
359
360
    /**
361
      * Set the attribute decl to indicate external declaration
362
      *
363
      * @param  aValue The new value to indicate external declaration.
364
      */
365
    void setExternalAttDeclaration(const bool aValue);
366
367
    //@}
368
369
    /***
370
     * Support for Serialization/De-serialization
371
     ***/
372
    DECL_XSERIALIZABLE(XMLAttDef)
373
374
protected :
375
    // -----------------------------------------------------------------------
376
    //  Hidden constructors
377
    // -----------------------------------------------------------------------
378
    XMLAttDef
379
    (
380
        const   AttTypes       type = CData
381
        , const DefAttTypes    defType= Implied
382
        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
383
    );
384
    XMLAttDef
385
    (
386
        const   XMLCh* const        attValue
387
        , const AttTypes            type
388
        , const DefAttTypes         defType
389
        , const XMLCh* const        enumValues = 0
390
        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
391
    );
392
393
394
private :
395
    // -----------------------------------------------------------------------
396
    //  Unimplemented constructors and operators
397
    // -----------------------------------------------------------------------
398
    XMLAttDef(const XMLAttDef&);
399
    XMLAttDef& operator=(const XMLAttDef&);
400
401
402
    // -----------------------------------------------------------------------
403
    //  Private helper methods
404
    // -----------------------------------------------------------------------
405
    void cleanUp();
406
407
408
    // -----------------------------------------------------------------------
409
    //  Private data members
410
    //
411
    //  fDefaultType
412
    //      Indicates what, if any, default stuff this attribute has.
413
    //
414
    //  fEnumeration
415
    //      If its an enumeration, this is the list of values as space
416
    //      separated values.
417
    //
418
    //  fId
419
    //      This is the unique id of this attribute, given to it when its put
420
    //      into the validator's attribute decl pool. It defaults to the
421
    //      special value XMLAttrDef::fgInvalidAttrId.
422
    //
423
    //  fType
424
    //      The type of attribute, which is one of the AttTypes values.
425
    //
426
    //  fValue
427
    //      This is the value of the attribute, which is the default value
428
    //      given in the attribute declaration.
429
    //
430
    //  fCreateReason
431
    //      This flag tells us how this attribute got created.  Sometimes even
432
    //      the attribute was not declared for the element, we want to fault
433
    //      fault it into the pool to avoid lots of redundant errors.
434
    //
435
    //  fExternalAttribute
436
    //      This flag indicates whether or not the attribute was declared externally.
437
    // -----------------------------------------------------------------------
438
    DefAttTypes     fDefaultType;
439
    AttTypes        fType;
440
    CreateReasons   fCreateReason;   
441
    bool            fExternalAttribute;
442
    XMLSize_t       fId;
443
    XMLCh*          fValue;
444
    XMLCh*          fEnumeration;
445
    MemoryManager*  fMemoryManager;
446
};
447
448
449
// ---------------------------------------------------------------------------
450
//  Getter methods
451
// ---------------------------------------------------------------------------
452
inline XMLAttDef::DefAttTypes XMLAttDef::getDefaultType() const
453
12.7M
{
454
12.7M
    return fDefaultType;
455
12.7M
}
456
457
inline const XMLCh* XMLAttDef::getEnumeration() const
458
6.37M
{
459
6.37M
    return fEnumeration;
460
6.37M
}
461
462
inline XMLSize_t XMLAttDef::getId() const
463
0
{
464
0
    return fId;
465
0
}
466
467
inline XMLAttDef::AttTypes XMLAttDef::getType() const
468
17.6M
{
469
17.6M
    return fType;
470
17.6M
}
471
472
inline const XMLCh* XMLAttDef::getValue() const
473
9.31M
{
474
9.31M
    return fValue;
475
9.31M
}
476
477
inline XMLAttDef::CreateReasons XMLAttDef::getCreateReason() const
478
0
{
479
0
    return fCreateReason;
480
0
}
481
482
inline bool XMLAttDef::isExternal() const
483
11.2M
{
484
11.2M
    return fExternalAttribute;
485
11.2M
}
486
487
inline MemoryManager* XMLAttDef::getMemoryManager() const
488
74.1k
{
489
74.1k
    return fMemoryManager;
490
74.1k
}
491
492
// ---------------------------------------------------------------------------
493
//  XMLAttDef: Setter methods
494
// ---------------------------------------------------------------------------
495
inline void XMLAttDef::setDefaultType(const XMLAttDef::DefAttTypes newValue)
496
36.5k
{
497
36.5k
    fDefaultType = newValue;
498
36.5k
}
499
500
inline void XMLAttDef::setEnumeration(const XMLCh* const newValue)
501
12.6k
{
502
12.6k
    if (fEnumeration)
503
1.74k
        fMemoryManager->deallocate(fEnumeration);
504
505
12.6k
    fEnumeration = XMLString::replicate(newValue, fMemoryManager);
506
12.6k
}
507
508
inline void XMLAttDef::setId(const XMLSize_t newId)
509
24.9k
{
510
24.9k
    fId = newId;
511
24.9k
}
512
513
inline void XMLAttDef::setType(const XMLAttDef::AttTypes newValue)
514
36.6k
{
515
36.6k
    fType = newValue;
516
36.6k
}
517
518
inline void XMLAttDef::setValue(const XMLCh* const newValue)
519
20.2k
{
520
20.2k
    if (fValue)
521
4.86k
       fMemoryManager->deallocate(fValue);
522
523
20.2k
    fValue = XMLString::replicate(newValue, fMemoryManager);
524
20.2k
}
525
526
inline void
527
XMLAttDef::setCreateReason(const XMLAttDef::CreateReasons newReason)
528
0
{
529
0
    fCreateReason = newReason;
530
0
}
531
532
inline void XMLAttDef::setExternalAttDeclaration(const bool aValue)
533
24.4k
{
534
24.4k
    fExternalAttribute = aValue;
535
24.4k
}
536
537
XERCES_CPP_NAMESPACE_END
538
539
#endif