# XMLDateTime.hpp

Go to the documentation of this file.

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