Xerces-C++: XMLDateTime.hpp Source File

| API Docs for SAX and DOM |
| Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages # XMLDateTime.hpp Go to the documentation of this file.
    00001 /*
    00002  * Copyright 2001,2004 The Apache Software Foundation.
    00003  *
    00004  * Licensed under the Apache License, Version 2.0 (the "License");
    00005  * you may not use this file except in compliance with the License.
    00006  * You may obtain a copy of the License at
    00007  *
    00008  *      http://www.apache.org/licenses/LICENSE-2.0
    00009  *
    00010  * Unless required by applicable law or agreed to in writing, software
    00011  * distributed under the License is distributed on an "AS IS" BASIS,
    00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    00013  * See the License for the specific language governing permissions and
    00014  * limitations under the License.
    00015  */
    00016
    00017 /*
    00018  * $Id: XMLDateTime.hpp 225121 2005-07-25 13:41:47Z cargilld $
    00019  */
    00020
    00021 #ifndef XML_DATETIME_HPP
    00022 #define XML_DATETIME_HPP
    00023
    00024 #include <xercesc/util/XMLNumber.hpp>
    00025 #include <xercesc/util/PlatformUtils.hpp>
    00026 #include <xercesc/util/XMLString.hpp>
    00027 #include <xercesc/util/XMLUniDefs.hpp>
    00028 #include <xercesc/util/SchemaDateTimeException.hpp>
    00029
    00030 XERCES_CPP_NAMESPACE_BEGIN
    00031
    00032 class XSValue;
    00033
    00034 class  XMLDateTime : public XMLNumber
    00035 {
    00036 public:
    00037
    00038     enum valueIndex
    00039     {
    00040         CentYear   = 0,
    00041         Month      ,
    00042         Day        ,
    00043         Hour       ,
    00044         Minute     ,
    00045         Second     ,
    00046         MiliSecond ,  //not to be used directly
    00047         utc        ,
    00048         TOTAL_SIZE
    00049     };
    00050
    00051     enum utcType
    00052     {
    00053         UTC_UNKNOWN = 0,
    00054         UTC_STD        ,          // set in parse() or normalize()
    00055         UTC_POS        ,          // set in parse()
    00056         UTC_NEG                   // set in parse()
    00057     };
    00058
    00059     // -----------------------------------------------------------------------
    00060     // ctors and dtor
    00061     // -----------------------------------------------------------------------
    00062
    00063     XMLDateTime(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
    00064     XMLDateTime(const XMLCh* const,
    00065                 MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
    00066     ~XMLDateTime();
    00067
    00068     inline void           setBuffer(const XMLCh* const);
    00069
    00070     // -----------------------------------------------------------------------
    00071     // Copy ctor and Assignment operators
    00072     // -----------------------------------------------------------------------
    00073
    00074     XMLDateTime(const XMLDateTime&);
    00075
    00076     XMLDateTime&          operator=(const XMLDateTime&);
    00077
    00078     // -----------------------------------------------------------------------
    00079     // Implementation of Abstract Interface
    00080     // -----------------------------------------------------------------------
    00081
    00087     virtual XMLCh*        toString() const;
    00088
    00089     virtual XMLCh*        getRawData() const;
    00090
    00091     virtual const XMLCh*  getFormattedString() const;
    00092
    00093     virtual int           getSign() const;
    00094
    00095     // -----------------------------------------------------------------------
    00096     // Canonical Representation
    00097     // -----------------------------------------------------------------------
    00098
    00099     XMLCh*                getDateTimeCanonicalRepresentation(MemoryManager* const memMgr) const;
    00100
    00101     XMLCh*                getTimeCanonicalRepresentation(MemoryManager* const memMgr)     const;
    00102
    00103     XMLCh*                getDateCanonicalRepresentation(MemoryManager* const memMgr)     const;
    00104
    00105     // -----------------------------------------------------------------------
    00106     // parsers
    00107     // -----------------------------------------------------------------------
    00108
    00109     void                  parseDateTime();       //DateTime
    00110
    00111     void                  parseDate();           //Date
    00112
    00113     void                  parseTime();           //Time
    00114
    00115     void                  parseDay();            //gDay
    00116
    00117     void                  parseMonth();          //gMonth
    00118
    00119     void                  parseYear();           //gYear
    00120
    00121     void                  parseMonthDay();       //gMonthDay
    00122
    00123     void                  parseYearMonth();      //gYearMonth
    00124
    00125     void                  parseDuration();       //duration
    00126
    00127     // -----------------------------------------------------------------------
    00128     // Comparison
    00129     // -----------------------------------------------------------------------
    00130     static int            compare(const XMLDateTime* const
    00131                                 , const XMLDateTime* const);
    00132
    00133     static int            compare(const XMLDateTime* const
    00134                                 , const XMLDateTime* const
    00135                                 , bool                    );
    00136
    00137     static int            compareOrder(const XMLDateTime* const
    00138                                      , const XMLDateTime* const);
    00139
    00140     /***
    00141      * Support for Serialization/De-serialization
    00142      ***/
    00143     DECL_XSERIALIZABLE(XMLDateTime)
    00144
    00145 private:
    00146
    00147     // -----------------------------------------------------------------------
    00148     // Constant data
    00149     // -----------------------------------------------------------------------
    00150     //
    00151
    00152     enum timezoneIndex
    00153     {
    00154         hh = 0,
    00155         mm ,
    00156         TIMEZONE_ARRAYSIZE
    00157     };
    00158
    00159     // -----------------------------------------------------------------------
    00160     // Comparison
    00161     // -----------------------------------------------------------------------
    00162     static int            compareResult(int
    00163                                       , int
    00164                                       , bool);
    00165
    00166     static void           addDuration(XMLDateTime*             pDuration
    00167                                     , const XMLDateTime* const pBaseDate
    00168                                     , int                      index);
    00169
    00170
    00171     static int            compareResult(const XMLDateTime* const
    00172                                       , const XMLDateTime* const
    00173                                       , bool
    00174                                       , int);
    00175
    00176     static inline int     getRetVal(int, int);
    00177
    00178     // -----------------------------------------------------------------------
    00179     // helper
    00180     // -----------------------------------------------------------------------
    00181
    00182     inline  void          reset();
    00183
    00184     inline  void          assertBuffer()               const;
    00185
    00186     inline  void          copy(const XMLDateTime&);
    00187
    00188     // allow multiple parsing
    00189     inline  void          initParser();
    00190
    00191     inline  bool          isNormalized()               const;
    00192
    00193     // -----------------------------------------------------------------------
    00194     // scaners
    00195     // -----------------------------------------------------------------------
    00196
    00197     void                  getDate();
    00198
    00199     void                  getTime();
    00200
    00201     void                  getYearMonth();
    00202
    00203     void                  getTimeZone(const int);
    00204
    00205     void                  parseTimeZone();
    00206
    00207     // -----------------------------------------------------------------------
    00208     // locator and converter
    00209     // -----------------------------------------------------------------------
    00210
    00211     int                   findUTCSign(const int start);
    00212
    00213     int                   indexOf(const int start
    00214                                 , const int end
    00215                                 , const XMLCh ch)     const;
    00216
    00217     int                   parseInt(const int start
    00218                                  , const int end)     const;
    00219
    00220     int                   parseIntYear(const int end) const;
    00221
    00222     double                parseMiliSecond(const int start
    00223                                         , const int end) const;
    00224
    00225     // -----------------------------------------------------------------------
    00226     // validator and normalizer
    00227     // -----------------------------------------------------------------------
    00228
    00229     void                  validateDateTime()          const;
    00230
    00231     void                  normalize();
    00232
    00233     void                  fillString(XMLCh*& ptr, int value, int expLen) const;
    00234
    00235     int                   fillYearString(XMLCh*& ptr, int value) const;
    00236
    00237     void                  searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const;
    00238
    00239     // -----------------------------------------------------------------------
    00240     // Unimplemented operator ==
    00241     // -----------------------------------------------------------------------
    00242     bool operator==(const XMLDateTime& toCompare) const;
    00243
    00244
    00245     // -----------------------------------------------------------------------
    00246     //  Private data members
    00247     //
    00248     //     fValue[]
    00249     //          object representation of date time.
    00250     //
    00251     //     fTimeZone[]
    00252     //          temporary storage for normalization
    00253     //
    00254     //     fStart, fEnd
    00255     //          pointers to the portion of fBuffer being parsed
    00256     //
    00257     //     fBuffer
    00258     //          raw data to be parsed, own it.
    00259     //
    00260     // -----------------------------------------------------------------------
    00261
    00262     int          fValue[TOTAL_SIZE];
    00263     int          fTimeZone[TIMEZONE_ARRAYSIZE];
    00264     int          fStart;
    00265     int          fEnd;
    00266     int          fBufferMaxLen;
    00267
    00268     double       fMiliSecond;
    00269     bool         fHasTime;
    00270
    00271     XMLCh*       fBuffer;
    00272     MemoryManager* fMemoryManager;
    00273
    00274     friend class XSValue;
    00275 };
    00276
    00277 inline void XMLDateTime::setBuffer(const XMLCh* const aString)
    00278 {
    00279     reset();
    00280
    00281     fEnd = XMLString::stringLen(aString);
    00282     if (fEnd > 0) {
    00283
    00284         if (fEnd > fBufferMaxLen)
    00285         {
    00286             fMemoryManager->deallocate(fBuffer);
    00287             fBufferMaxLen = fEnd + 8;
    00288             fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
    00289         }
    00290
    00291         memcpy(fBuffer, aString, (fEnd+1) * sizeof(XMLCh));
    00292     }
    00293 }
    00294
    00295 inline void XMLDateTime::reset()
    00296 {
    00297     for ( int i=0; i < TOTAL_SIZE; i++ )
    00298         fValue[i] = 0;
    00299
    00300     fMiliSecond   = 0;
    00301     fHasTime      = false;
    00302     fTimeZone[hh] = fTimeZone[mm] = 0;
    00303     fStart = fEnd = 0;
    00304
    00305     if (fBuffer)
    00306         *fBuffer = 0;
    00307 }
    00308
    00309 inline void XMLDateTime::copy(const XMLDateTime& rhs)
    00310 {
    00311     for ( int i = 0; i < TOTAL_SIZE; i++ )
    00312         fValue[i] = rhs.fValue[i];
    00313
    00314     fMiliSecond   = rhs.fMiliSecond;
    00315     fHasTime      = rhs.fHasTime;
    00316     fTimeZone[hh] = rhs.fTimeZone[hh];
    00317     fTimeZone[mm] = rhs.fTimeZone[mm];
    00318     fStart = rhs.fStart;
    00319     fEnd   = rhs.fEnd;
    00320
    00321     if (fEnd > 0)
    00322     {
    00323         if (fEnd > fBufferMaxLen)
    00324         {
    00325             fMemoryManager->deallocate(fBuffer);//delete[] fBuffer;
    00326             fBufferMaxLen = rhs.fBufferMaxLen;
    00327             fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
    00328         }
    00329
    00330         memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh));
    00331     }
    00332 }
    00333
    00334 inline void XMLDateTime::assertBuffer() const
    00335 {
    00336     if ( ( !fBuffer )            ||
    00337          ( fBuffer[0] == chNull ) )
    00338     {
    00339         ThrowXMLwithMemMgr(SchemaDateTimeException
    00340                , XMLExcepts::DateTime_Assert_Buffer_Fail
    00341                , fMemoryManager);
    00342     }
    00343
    00344 }
    00345
    00346 inline void XMLDateTime::initParser()
    00347 {
    00348     assertBuffer();
    00349     fStart = 0;   // to ensure scan from the very first beginning
    00350                   // in case the pointer is updated accidentally by someone else.
    00351 }
    00352
    00353 inline bool XMLDateTime::isNormalized() const
    00354 {
    00355     return ( fValue[utc] == UTC_STD ? true : false );
    00356 }
    00357
    00358 inline int XMLDateTime::getRetVal(int c1, int c2)
    00359 {
    00360     if ((c1 == LESS_THAN    && c2 == GREATER_THAN) ||
    00361         (c1 == GREATER_THAN && c2 == LESS_THAN)      )
    00362     {
    00363         return INDETERMINATE;
    00364     }
    00365
    00366     return ( c1 != INDETERMINATE ) ? c1 : c2;
    00367 }
    00368
    00369 XERCES_CPP_NAMESPACE_END
    00370
    00371 #endif

Copyright © 1994-2004 The Apache Software Foundation. All Rights Reserved.