source: icXML/icXML-devel/src/icxercesc/framework/XMLAttr.hpp @ 2774

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

Various fixes

File size: 24.7 KB
Line 
1/*
2 * Unless required by applicable law or agreed to in writing, software
3 * distributed under the License is distributed on an "AS IS" BASIS,
4 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5 * See the License for the specific language governing permissions and
6 * limitations under the License.
7 */
8
9/*
10 * $Id: XMLAttr.hpp 932887 2010-04-11 13:04:59Z borisk $
11 */
12
13#if !defined(XERCESC_INCLUDE_GUARD_XMLATTR_HPP)
14#define XERCESC_INCLUDE_GUARD_XMLATTR_HPP
15
16#include <icxercesc/util/PlatformUtils.hpp>
17#include <icxercesc/util/QName.hpp>
18#include <icxercesc/framework/XMLAttDef.hpp>
19#include <xercesc/validators/datatype/DatatypeValidator.hpp>
20#include <icxmlc/XMLConfig.hpp>
21
22XERCES_CPP_NAMESPACE_BEGIN
23
24class XMLScanner;
25class IGXMLScanner;
26class DOMXPathExpressionImpl;
27
28/**
29 *  This class defines the information about an attribute that will come out
30 *  of the scanner during parsing. This information does not depend upon the
31 *  type of validator because it is not tied to any scheme/DTD type info. Its
32 *  just the raw XML 1.0 information that will be reported about an attribute
33 *  in the startElement() callback method of the XMLDocumentHandler class.
34 *  Hence it is not intended to be extended or derived from. Its designed to
35 *  be used as is.
36 *
37 *  The 'specified' field of this class indicates whether the attribute was
38 *  actually present or whether it was faulted in because it had a fixed or
39 *  default value.
40 *
41 *  The code receiving this information can ask its validator for more info
42 *  about the attribute, i.e. get its declaration from the DTD/Schema info.
43 *
44 *  Because of the heavy use (and reuse) of instances of this class, and the
45 *  number of string members it has, this class takes pains to not reallocate
46 *  string members unless it has to. It keeps up with how long each buffer
47 *  is and only reallocates if the new value won't fit.
48 */
49class XMLPARSER_EXPORT XMLAttr : public XMemory
50{
51    friend class XMLScanner;
52    friend class IGXMLScanner;
53    friend class DOMXPathExpressionImpl;
54
55public:
56        // -----------------------------------------------------------------------
57        //  Constructors and Destructor
58        // -----------------------------------------------------------------------
59        /** @name Constructors */
60        //@{
61
62        /**
63          * The default constructor just setsup an empty attribute to be filled
64          * in the later. Though the initial state is a reasonable one, it is
65          * not documented because it should not be depended on.
66          *
67          * @param  manager     The configurable memory manager
68          */
69        XMLAttr(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
70
71        /**
72          * This is the primary constructor which takes all of the information
73          * required to construct a complete attribute object.
74          *
75          * @param  uriId       The id into the validator's URI pool of the URI
76          *                     that the prefix mapped to. Only used if namespaces
77          *                     are enabled/supported.
78          *
79          * @param  attrName    The base name of the attribute, i.e. the part
80          *                     after any prefix.
81          *
82          * @param  attrPrefix  The prefix, if any, of this attribute's name. If
83          *                     this is empty, then uriID is meaningless as well.
84          *
85          * @param  attrValue   The value string of the attribute, which should
86          *                     be fully normalized by XML rules!
87          *
88          * @param  type        The type of the attribute. This will indicate
89          *                     the type of normalization done and constrains
90          *                     the value content. Make sure that the value
91          *                     set meets the constraints!
92          *
93          * @param  specified   Indicates whether the attribute was explicitly
94          *                     specified or not. If not, then it was faulted
95          *                     in from a FIXED or DEFAULT value.
96          *
97          * @param  manager     The configurable memory manager
98          * @param datatypeValidator type used to validate the attribute,
99          *                         if it was validated by an XML Schema
100          * @param isSchema         true if and only if this attribute was validated
101          *                         by an XML Schema
102          */
103        XMLAttr
104        (
105                  const unsigned int        uriId
106                , const XMLCh* const        attrName
107                , const XMLCh* const        attrPrefix
108                , const XMLCh* const        attrValue
109                , const XMLAttDef::AttTypes type = XMLAttDef::CData
110                , const bool                specified = true
111                , MemoryManager* const      manager = XMLPlatformUtils::fgMemoryManager
112                , DatatypeValidator * datatypeValidator = 0
113                , const bool isSchema = false
114        );
115
116        /**
117          * This is the primary constructor which takes all of the information
118          * required to construct a complete attribute object.
119          *
120          * @param  uriId       The id into the validator's URI pool of the URI
121          *                     that the prefix mapped to. Only used if namespaces
122          *                     are enabled/supported.
123          *
124          * @param  rawName     The raw name of the attribute.
125          *
126          * @param  attrValue   The value string of the attribute, which should
127          *                     be fully normalized by XML rules!
128          *
129          * @param  type        The type of the attribute. This will indicate
130          *                     the type of normalization done and constrains
131          *                     the value content. Make sure that the value
132          *                     set meets the constraints!
133          *
134          * @param  specified   Indicates whether the attribute was explicitly
135          *                     specified or not. If not, then it was faulted
136          *                     in from a FIXED or DEFAULT value.
137          *
138          * @param  manager     The configurable memory manager
139          * @param datatypeValidator type used to validate the attribute,
140          *                         if it was validated by an XML Schema
141          * @param isSchema         true if and only if this attribute was validated
142          *                         by an XML Schema
143          */
144        XMLAttr
145        (
146                const unsigned int uriId
147                , const XMLCh* const rawName
148                , const XMLCh* const attrValue
149                , const XMLAttDef::AttTypes type = XMLAttDef::CData
150                , const bool specified = true
151                , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
152                , DatatypeValidator * datatypeValidator = 0
153                , const bool isSchema = false
154        );
155
156
157        /**
158          * This is the primary constructor which takes all of the information
159          * required to construct a complete attribute object.
160          *
161          * @param  qName       The precomputed qName that is stored in the symbol table
162          *
163          * @param  attrValue   The value string of the attribute, which should
164          *                     be fully normalized by XML rules!
165          *
166          * @param  type        The type of the attribute. This will indicate
167          *                     the type of normalization done and constrains
168          *                     the value content. Make sure that the value
169          *                     set meets the constraints!
170          *
171          * @param  specified   Indicates whether the attribute was explicitly
172          *                     specified or not. If not, then it was faulted
173          *                     in from a FIXED or DEFAULT value.
174          *
175          * @param  manager     The configurable memory manager
176          * @param datatypeValidator type used to validate the attribute,
177          *                         if it was validated by an XML Schema
178          * @param isSchema         true if and only if this attribute was validated
179          *                         by an XML Schema
180          */
181        XMLAttr
182        (
183                const QName * qName
184                , const XMLCh* const attrValue
185                , const XMLAttDef::AttTypes type = XMLAttDef::CData
186                , const bool                specified = true
187                , MemoryManager* const      manager = XMLPlatformUtils::fgMemoryManager
188                , DatatypeValidator * datatypeValidator = 0
189                , const bool isSchema = false
190        );
191
192    /**
193      * This is the primary constructor which takes all of the information
194      * required to construct a complete attribute object.
195      *
196      * @param  qName       The precomputed qName that is stored in the symbol table
197      *
198      * @param  attrValue   The value string of the attribute, which should
199      *                     be fully normalized by XML rules!
200      *
201      * @param  attrLength  The length of the attribute value string
202      *
203      * @param  type        The type of the attribute. This will indicate
204      *                     the type of normalization done and constrains
205      *                     the value content. Make sure that the value
206      *                     set meets the constraints!
207      *
208      * @param  specified   Indicates whether the attribute was explicitly
209      *                     specified or not. If not, then it was faulted
210      *                     in from a FIXED or DEFAULT value.
211      *
212      * @param  manager     The configurable memory manager
213      * @param datatypeValidator type used to validate the attribute,
214      *                         if it was validated by an XML Schema
215      * @param isSchema         true if and only if this attribute was validated
216      *                         by an XML Schema
217      */
218    XMLAttr
219    (
220        const QName *               qName
221        , const XMLCh* const        attrValue
222        , const XMLSize_t           attrLength
223        , const XMLAttDef::AttTypes type = XMLAttDef::CData
224        , const bool                specified = true
225        , MemoryManager* const      manager = XMLPlatformUtils::fgMemoryManager
226        , DatatypeValidator *       datatypeValidator = 0
227        , const bool                isSchema = false
228    );
229
230
231        //@}
232
233        /** @name Destructor */
234        //@{
235        ~XMLAttr();
236        //@}
237
238
239        // -----------------------------------------------------------------------
240        //  Getter methods
241        // -----------------------------------------------------------------------
242
243        /** @name Getter methods */
244        //@{
245
246        /**
247          * This method returns the attribute name in a QName format.
248          */
249        QName* getAttName() const;
250
251        /**
252          * This method gets a const pointer to the name of the attribute. The
253          * form of this name is defined by the validator in use.
254          */
255        const XMLCh* getName() const;
256
257        /**
258          * This method will get a const pointer to the prefix string of this
259          * attribute. Since prefixes are optional, it may be zero.
260          */
261        const XMLCh* getPrefix() const;
262
263        /**
264          * This method will get the QName of this attribute, which will be the
265          * prefix if any, then a colon, then the base name. If there was no
266          * prefix, its the same as the getName() method.
267          */
268        const XMLCh* getQName() const;
269
270        /**
271          * This method will get the specified flag, which indicates whether
272          * the attribute was explicitly specified or just faulted in.
273          */
274        bool getSpecified() const;
275
276        /**
277          * This method will get the type of the attribute. The available types
278          * are defined by the XML specification.
279          */
280        XMLAttDef::AttTypes getType() const;
281
282        /**
283          * This method will get the value of the attribute. The value can be
284          * be an empty string, but never null if the object is correctly
285          * set up.
286          */
287        const XMLCh* getValue() const;
288
289        /**
290          * This method will return the length of this attribute's value.
291          */
292        const XMLSize_t getValueLen() const;
293
294        /**
295          * This method will get the id of the URI that this attribute's prefix
296          * mapped to. If namespaces are not on, then its value is meaningless.
297          */
298        unsigned int getURIId() const;
299
300        //@}
301
302
303        // -----------------------------------------------------------------------
304        //  Setter methods
305        // -----------------------------------------------------------------------
306
307        /** @name Setter methods */
308        //@{
309
310        /**
311          * This method is called to set up a default constructed object after
312          * the fact, or to reuse a previously used object.
313          *
314          * @param  uriId       The id into the validator's URI pool of the URI
315          *                     that the prefix mapped to. Only used if namespaces
316          *                     are enabled/supported.
317          *
318          * @param  attrName    The base name of the attribute, i.e. the part
319          *                     after any prefix.
320          *
321          * @param  attrPrefix  The prefix, if any, of this attribute's name. If
322          *                     this is empty, then uriID is meaningless as well.
323          *
324          * @param  attrValue   The value string of the attribute, which should
325          *                     be fully normalized by XML rules according to the
326          *                     attribute type.
327          *
328          * @param  type        The type of the attribute. This will indicate
329          *                     the type of normalization done and constrains
330          *                     the value content. Make sure that the value
331          *                     set meets the constraints!
332          * @param datatypeValidator type used to validate the attribute,
333          *                         if it was validated by an XML Schema
334          * @param isSchema         true if and only if this attribute was validated
335          *                         by an XML Schema
336          *
337          */
338        void set
339        (
340                const   unsigned int        uriId
341                , const XMLCh* const        attrName
342                , const XMLCh* const        attrPrefix
343                , const XMLCh* const        attrValue
344                , const XMLAttDef::AttTypes type = XMLAttDef::CData
345                , DatatypeValidator * datatypeValidator = 0
346                , const bool isSchema = false
347        );
348
349        /**
350          * This method is called to set up a default constructed object after
351          * the fact, or to reuse a previously used object.
352          *
353          * @param  uriId       The id into the validator's URI pool of the URI
354          *                     that the prefix mapped to. Only used if namespaces
355          *                     are enabled/supported.
356          *
357          * @param  attrRawName The raw name of the attribute.
358          *
359          * @param  attrValue   The value string of the attribute, which should
360          *                     be fully normalized by XML rules according to the
361          *                     attribute type.
362          *
363          * @param  type        The type of the attribute. This will indicate
364          *                     the type of normalization done and constrains
365          *                     the value content. Make sure that the value
366          *                     set meets the constraints!
367          * @param datatypeValidator type used to validate the attribute,
368          *                         if it was validated by an XML Schema
369          * @param isSchema         true if and only if this attribute was validated
370          *                         by an XML Schema
371          */
372        void set
373        (
374                const   unsigned int        uriId
375                , const XMLCh* const        attrRawName
376                , const XMLCh* const        attrValue
377                , const XMLAttDef::AttTypes type = XMLAttDef::CData
378                , DatatypeValidator * datatypeValidator = 0
379                , const bool isSchema = false
380        );
381
382        /**
383          * This method is called to set up a default constructed object after
384          * the fact, or to reuse a previously used object.
385          *
386          * @param  qName       The precomputed qName that is stored in the symbol table
387          * @param  attrValue   The value string of the attribute, which should
388          *                     be fully normalized by XML rules according to the
389          *                     attribute type.
390          *
391          * @param  type        The type of the attribute. This will indicate
392          *                     the type of normalization done and constrains
393          *                     the value content. Make sure that the value
394          *                     set meets the constraints!
395          * @param datatypeValidator type used to validate the attribute,
396          *                         if it was validated by an XML Schema
397          * @param isSchema         true if and only if this attribute was validated
398          *                         by an XML Schema
399          *
400          */
401        void set
402        (
403        const QName *               qName
404        , const XMLCh * const       attrValue
405                , const XMLAttDef::AttTypes type = XMLAttDef::CData
406        , DatatypeValidator *       datatypeValidator = 0
407        , const bool                isSchema = false
408        );
409
410    /**
411      * This method is called to set up a default constructed object after
412      * the fact, or to reuse a previously used object.
413      *
414      * @param  qName       The precomputed qName that is stored in the symbol table
415      *
416      * @param  attrValue   The value string of the attribute, which should
417      *                     be fully normalized by XML rules according to the
418      *                     attribute type.
419      *
420      * @param  attrLength  The length of the attribute string
421      *
422      * @param  type        The type of the attribute. This will indicate
423      *                     the type of normalization done and constrains
424      *                     the value content. Make sure that the value
425      *                     set meets the constraints!
426      * @param datatypeValidator type used to validate the attribute,
427      *                         if it was validated by an XML Schema
428      * @param isSchema         true if and only if this attribute was validated
429      *                         by an XML Schema
430      *
431      */
432    void set
433    (
434        const QName *               qName
435        , const XMLCh * const       attrValue
436        , const XMLSize_t           attrLength
437        , const XMLAttDef::AttTypes type = XMLAttDef::CData
438        , DatatypeValidator *       datatypeValidator = 0
439        , const bool                isSchema = false
440    );
441
442        /**
443          * This method will update just the name related fields of the
444          * attribute object. The other fields are left as is.
445          *
446          * @param  uriId       The id into the validator's URI pool of the URI
447          *                     that the prefix mapped to. Only used if namespaces
448          *                     are enabled/supported.
449          *
450          * @param  attrName    The base name of the attribute, i.e. the part
451          *                     after any prefix.
452          *
453          * @param  attrPrefix  The prefix, if any, of this attribute's name. If
454          *                     this is empty, then uriID is meaningless as well.
455          */
456        void setName
457        (
458                const   unsigned int        uriId
459                , const XMLCh* const        attrName
460                , const XMLCh* const        attrPrefix
461        );
462
463        /**
464          * This method will update the specified state of the object.
465          *
466          * @param  newValue    Indicates whether the attribute was explicitly
467          *                     specified or not. If not, then it was faulted
468          *                     in from a FIXED or DEFAULT value.
469          */
470        void setSpecified(const bool newValue);
471
472        /**
473          * This method will update the attribute type of the object.
474          *
475          * @param  newType     The type of the attribute. This will indicate
476          *                     the type of normalization done and constrains
477          *                     the value content. Make sure that the value
478          *                     set meets the constraints!
479          */
480        void setType(const XMLAttDef::AttTypes newType);
481
482        /**
483          * This method will update the value field of the attribute.
484          *
485          * @param  newValue    The value string of the attribute, which should
486          *                     be fully normalized by XML rules according to the
487          *                     attribute type.
488          */
489        void setValue(const XMLCh* const newValue);
490
491        /**
492          * This method will update the value field of the attribute.
493          *
494          * @param  newValue    The value string of the attribute, which should
495          *                     be fully normalized by XML rules according to the
496          *                     attribute type.
497          */
498        void setValue(const XMLCh* const newValue, const XMLSize_t length);
499
500#if 0
501        /**
502          * This method will update the value field of the attribute.
503          *
504          * @param  newValue    The value string of the attribute, which should
505          *                     be fully normalized by XML rules according to the
506          *                     attribute type.
507          */
508        void setValue(const XMLBuffer & newValue);
509#endif
510
511        /**
512          * This method will set the URI id field of this attribute. This is
513          * generally only ever called internally by the parser itself during
514          * the parsing process.
515          *
516          * @param  uriId       The uriId of the attribute.
517          */
518        void setURIId(const unsigned int uriId);
519
520        //@}
521
522
523    /**
524     * Returns whether this is an xmlns or xmlns: attribute.
525     */
526    bool isXMLNS() const;
527
528private:
529        // -----------------------------------------------------------------------
530        //  Unimplemented constructors and operators
531        // -----------------------------------------------------------------------
532        XMLAttr(const XMLAttr&);
533        XMLAttr& operator=(const XMLAttr&);
534
535
536        // -----------------------------------------------------------------------
537        //  Private, helper methods
538        // -----------------------------------------------------------------------
539        void cleanUp();
540
541
542        // -----------------------------------------------------------------------
543        //  Private instance variables
544        //
545        //  fAttName
546        //      The Attribute Name;
547        //
548        //  fSpecified
549        //      True if this attribute appeared in the element; else, false if
550        //      it was defaulted from an AttDef.
551        //
552        //  fType
553        //      The attribute type enum value for this attribute. Indicates what
554        //      type of attribute it was.
555        //
556        //  fValue
557        //  fValueBufSz
558        //      The attribute value that was given in the attribute instance, and
559        //      its current buffer size (minus one, where the null is.)
560        //
561        //  fMemoryManager
562        //      The memory manager used for dynamic memory allocation/deallocation
563        // -----------------------------------------------------------------------
564        bool                fSpecified;
565        XMLAttDef::AttTypes fType;
566        XMLSize_t           fValueLen;
567        const XMLCh*        fValue;
568        QName*              fAttName;
569        MemoryManager*      fMemoryManager;
570};
571
572// ---------------------------------------------------------------------------
573//  XMLAttr: Constructors and Destructor
574// ---------------------------------------------------------------------------
575inline XMLAttr::~XMLAttr()
576{
577        cleanUp();
578}
579
580
581// ---------------------------------------------------------------------------
582//  XMLAttr: Getter methods
583// ---------------------------------------------------------------------------
584inline QName* XMLAttr::getAttName() const
585{
586        return fAttName;
587}
588
589inline const XMLCh* XMLAttr::getName() const
590{
591        return fAttName->getLocalPart();
592}
593
594inline const XMLCh* XMLAttr::getPrefix() const
595{
596        return fAttName->getPrefix();
597}
598
599inline bool XMLAttr::getSpecified() const
600{
601        return fSpecified;
602}
603
604inline XMLAttDef::AttTypes XMLAttr::getType() const
605{
606        return fType;
607}
608
609inline const XMLCh* XMLAttr::getValue() const
610{
611        return fValue;
612}
613
614inline const XMLSize_t XMLAttr::getValueLen() const
615{
616        return fValueLen;
617}
618
619inline unsigned int XMLAttr::getURIId() const
620{
621        return fAttName->getURI();
622}
623
624// ---------------------------------------------------------------------------
625//  XMLAttr: Setter methods
626// ---------------------------------------------------------------------------
627
628inline void XMLAttr::set( const QName *             qName
629                                                , const XMLCh* const        attrValue
630                                                , const XMLAttDef::AttTypes type
631                                                , DatatypeValidator * /*datatypeValidator */
632                                                , const bool /*isSchema*/ )
633{
634        fAttName = const_cast<QName *>(qName);
635        setValue(attrValue);
636        fType = type;
637}
638
639inline void XMLAttr::set( const QName *             qName
640                        , const XMLCh* const        attrValue
641                        , const XMLSize_t           attrLength
642                        , const XMLAttDef::AttTypes type
643                        , DatatypeValidator * /*datatypeValidator */
644                        , const bool /*isSchema*/ )
645{
646    fAttName = const_cast<QName *>(qName);
647    setValue(attrValue, attrLength);
648    fType = type;
649}
650
651inline void XMLAttr::setType(const XMLAttDef::AttTypes newValue)
652{
653        fType = newValue;
654}
655
656inline void XMLAttr::setSpecified(const bool newValue)
657{
658        fSpecified = newValue;
659}
660
661// ---------------------------------------------------------------------------
662//  ICXML DEPRECATED FUNCTIONS!
663// ---------------------------------------------------------------------------
664
665inline void XMLAttr::set( const unsigned int
666                                                , const XMLCh* const
667                                                , const XMLCh* const
668                                                , const XMLCh* const
669                                                , const XMLAttDef::AttTypes
670                                                , DatatypeValidator *
671                                                , const bool  )
672{
673        DEPRECATED_FEATURE_IN_ICXML;
674}
675
676inline void XMLAttr::set(const  unsigned int
677                                                , const XMLCh* const
678                                                , const XMLCh* const
679                                                , const XMLAttDef::AttTypes
680                                                , DatatypeValidator *
681                                                , const bool  )
682{
683        DEPRECATED_FEATURE_IN_ICXML;
684}
685
686#ifdef PRINT_DEBUG_MESSAGE
687static std::ostream & operator << (std::ostream & out, const XMLAttr & attr)
688{
689    out << '('
690        << attr.getAttName() << ','
691        << attr.getValue() << ','
692        << attr.getURIId() << ','
693        << attr.getSpecified() << ','
694        << attr.getType() << ','
695        << ')';
696    return out;
697}
698
699static std::ostream & operator << (std::ostream & out, const XMLAttr * attr)
700{
701    if (likely(attr != 0))
702    {
703        out << *attr;
704    }
705    return out;
706}
707
708
709XERCES_CPP_NAMESPACE_END
710
711#include <xercesc/util/RefVectorOf.hpp>
712
713XERCES_CPP_NAMESPACE_BEGIN
714
715static std::ostream & operator << (std::ostream & out, const RefVectorOf<XMLAttr> & attrs)
716{
717    char leadingChar = '{';
718    for (XMLSize_t index = 0; index < attrs.size(); index++)
719    {
720        out << leadingChar << attrs.elementAt(index);
721        leadingChar = ',';
722    }
723    out << ((leadingChar == '{') ? '0' : '}');
724
725    return out;
726}
727#endif
728
729XERCES_CPP_NAMESPACE_END
730
731#endif
Note: See TracBrowser for help on using the repository browser.