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

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