| API Docs for SAX and DOM |
| Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members File Members # 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,v 1.20 2004/09/08 13:56:24 peiyongz Exp $
    00019  * $Log: XMLDateTime.hpp,v $
    00020  * Revision 1.20  2004/09/08 13:56:24  peiyongz
    00021  * Apache License Version 2.0
    00022  *
    00023  * Revision 1.19  2004/08/31 20:50:50  peiyongz
    00024  * Parse/keep milisecond as double to retain precision.
    00025  *
    00026  * Revision 1.18  2004/08/11 16:48:55  peiyongz
    00027  * Allow XSValue access
    00028  *
    00029  * Revision 1.17  2004/01/29 11:48:47  cargilld
    00030  * Code cleanup changes to get rid of various compiler diagnostic messages.
    00031  *
    00032  * Revision 1.16  2004/01/13 19:50:56  peiyongz
    00033  * remove parseContent()
    00034  *
    00035  * Revision 1.14  2003/12/31 02:34:11  neilg
    00036  * enable production of canonical representations for dates with negative years, or years >9999
    00037  *
    00038  * Revision 1.13  2003/12/17 20:00:49  cargilld
    00039  * Update for memory management so that the static memory manager (one
    00040  * used to call Initialize) is only for static data.
    00041  *
    00042  * Revision 1.12  2003/12/17 00:18:35  cargilld
    00043  * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
    00044  *
    00045  * Revision 1.11  2003/12/11 21:38:12  peiyongz
    00046  * support for Canonical Representation for Datatype
    00047  *
    00048  * Revision 1.10  2003/09/23 18:16:07  peiyongz
    00049  * Inplementation for Serialization/Deserialization
    00050  *
    00051  * Revision 1.9  2003/08/14 02:57:27  knoaman
    00052  * Code refactoring to improve performance of validation.
    00053  *
    00054  * Revision 1.8  2003/05/18 14:02:05  knoaman
    00055  * Memory manager implementation: pass per instance manager.
    00056  *
    00057  * Revision 1.7  2003/05/15 19:07:46  knoaman
    00058  * Partial implementation of the configurable memory manager.
    00059  *
    00060  * Revision 1.6  2003/05/09 15:13:46  peiyongz
    00061  * Deprecated toString() in XMLNumber family
    00062  *
    00063  * Revision 1.5  2003/03/10 20:55:58  peiyongz
    00064  * Schema Errata E2-40 double/float
    00065  *
    00066  * Revision 1.4  2003/02/02 23:54:43  peiyongz
    00067  * getFormattedString() added to return original and converted value.
    00068  *
    00069  * Revision 1.3  2003/01/30 21:55:22  tng
    00070  * Performance: create getRawData which is similar to toString but return the internal data directly, user is not required to delete the returned memory.
    00071  *
    00072  * Revision 1.2  2002/11/04 15:22:05  tng
    00073  * C++ Namespace Support.
    00074  *
    00075  * Revision 1.1.1.1  2002/02/01 22:22:14  peiyongz
    00076  * sane_include
    00077  *
    00078  * Revision 1.4  2001/11/22 20:23:00  peiyongz
    00079  * _declspec(dllimport) and inline warning C4273
    00080  *
    00081  * Revision 1.3  2001/11/12 20:36:54  peiyongz
    00082  * SchemaDateTimeException defined
    00083  *
    00084  * Revision 1.2  2001/11/09 20:41:45  peiyongz
    00085  * Fix: compilation error on Solaris and AIX.
    00086  *
    00087  * Revision 1.1  2001/11/07 19:16:03  peiyongz
    00088  * DateTime Port
    00089  *
    00090  */
    00091
    00092 #ifndef XML_DATETIME_HPP
    00093 #define XML_DATETIME_HPP
    00094
    00095 #include <xercesc/util/XMLNumber.hpp>
    00096 #include <xercesc/util/PlatformUtils.hpp>
    00097 #include <xercesc/util/XMLString.hpp>
    00098 #include <xercesc/util/XMLUniDefs.hpp>
    00099 #include <xercesc/util/SchemaDateTimeException.hpp>
    00100
    00101 XERCES_CPP_NAMESPACE_BEGIN
    00102
    00103 class XSValue;
    00104
    00105 class  XMLDateTime : public XMLNumber
    00106 {
    00107 public:
    00108
    00109     enum valueIndex
    00110     {
    00111         CentYear   = 0,
    00112         Month      ,
    00113         Day        ,
    00114         Hour       ,
    00115         Minute     ,
    00116         Second     ,
    00117         MiliSecond ,  //not to be used directly
    00118         utc        ,
    00119         TOTAL_SIZE
    00120     };
    00121
    00122     enum utcType
    00123     {
    00124         UTC_UNKNOWN = 0,
    00125         UTC_STD        ,          // set in parse() or normalize()
    00126         UTC_POS        ,          // set in parse()
    00127         UTC_NEG                   // set in parse()
    00128     };
    00129
    00130     // -----------------------------------------------------------------------
    00131     // ctors and dtor
    00132     // -----------------------------------------------------------------------
    00133
    00134     XMLDateTime(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
    00135     XMLDateTime(const XMLCh* const,
    00136                 MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
    00137     ~XMLDateTime();
    00138
    00139     inline void           setBuffer(const XMLCh* const);
    00140
    00141     // -----------------------------------------------------------------------
    00142     // Copy ctor and Assignment operators
    00143     // -----------------------------------------------------------------------
    00144
    00145     XMLDateTime(const XMLDateTime&);
    00146
    00147     XMLDateTime&          operator=(const XMLDateTime&);
    00148
    00149     // -----------------------------------------------------------------------
    00150     // Implementation of Abstract Interface
    00151     // -----------------------------------------------------------------------
    00152
    00158     virtual XMLCh*        toString() const;
    00159
    00160     virtual XMLCh*        getRawData() const;
    00161
    00162     virtual const XMLCh*  getFormattedString() const;
    00163
    00164     virtual int           getSign() const;
    00165
    00166     // -----------------------------------------------------------------------
    00167     // Canonical Representation
    00168     // -----------------------------------------------------------------------
    00169
    00170     XMLCh*                getDateTimeCanonicalRepresentation(MemoryManager* const memMgr) const;
    00171
    00172     XMLCh*                getTimeCanonicalRepresentation(MemoryManager* const memMgr)     const;
    00173
    00174     // -----------------------------------------------------------------------
    00175     // parsers
    00176     // -----------------------------------------------------------------------
    00177
    00178     void                  parseDateTime();       //DateTime
    00179
    00180     void                  parseDate();           //Date
    00181
    00182     void                  parseTime();           //Time
    00183
    00184     void                  parseDay();            //gDay
    00185
    00186     void                  parseMonth();          //gMonth
    00187
    00188     void                  parseYear();           //gYear
    00189
    00190     void                  parseMonthDay();       //gMonthDay
    00191
    00192     void                  parseYearMonth();      //gYearMonth
    00193
    00194     void                  parseDuration();       //duration
    00195
    00196     // -----------------------------------------------------------------------
    00197     // Comparison
    00198     // -----------------------------------------------------------------------
    00199     static int            compare(const XMLDateTime* const
    00200                                 , const XMLDateTime* const);
    00201
    00202     static int            compare(const XMLDateTime* const
    00203                                 , const XMLDateTime* const
    00204                                 , bool                    );
    00205
    00206     static int            compareOrder(const XMLDateTime* const
    00207                                      , const XMLDateTime* const);
    00208
    00209     /***
    00210      * Support for Serialization/De-serialization
    00211      ***/
    00212     DECL_XSERIALIZABLE(XMLDateTime)
    00213
    00214 private:
    00215
    00216     // -----------------------------------------------------------------------
    00217     // Constant data
    00218     // -----------------------------------------------------------------------
    00219     //
    00220
    00221     enum timezoneIndex
    00222     {
    00223         hh = 0,
    00224         mm ,
    00225         TIMEZONE_ARRAYSIZE
    00226     };
    00227
    00228     // -----------------------------------------------------------------------
    00229     // Comparison
    00230     // -----------------------------------------------------------------------
    00231     static int            compareResult(int
    00232                                       , int
    00233                                       , bool);
    00234
    00235     static void           addDuration(XMLDateTime*             pDuration
    00236                                     , const XMLDateTime* const pBaseDate
    00237                                     , int                      index);
    00238
    00239
    00240     static int            compareResult(const XMLDateTime* const
    00241                                       , const XMLDateTime* const
    00242                                       , bool
    00243                                       , int);
    00244
    00245     static inline int     getRetVal(int, int);
    00246
    00247     // -----------------------------------------------------------------------
    00248     // helper
    00249     // -----------------------------------------------------------------------
    00250
    00251     inline  void          reset();
    00252
    00253     inline  void          assertBuffer()               const;
    00254
    00255     inline  void          copy(const XMLDateTime&);
    00256
    00257     // allow multiple parsing
    00258     inline  void          initParser();
    00259
    00260     inline  bool          isNormalized()               const;
    00261
    00262     // -----------------------------------------------------------------------
    00263     // scaners
    00264     // -----------------------------------------------------------------------
    00265
    00266     void                  getDate();
    00267
    00268     void                  getTime();
    00269
    00270     void                  getYearMonth();
    00271
    00272     void                  getTimeZone(const int);
    00273
    00274     void                  parseTimeZone();
    00275
    00276     // -----------------------------------------------------------------------
    00277     // locator and converter
    00278     // -----------------------------------------------------------------------
    00279
    00280     int                   findUTCSign(const int start);
    00281
    00282     int                   indexOf(const int start
    00283                                 , const int end
    00284                                 , const XMLCh ch)     const;
    00285
    00286     int                   parseInt(const int start
    00287                                  , const int end)     const;
    00288
    00289     int                   parseIntYear(const int end) const;
    00290
    00291     double                parseMiliSecond(const int start
    00292                                         , const int end) const;
    00293
    00294     // -----------------------------------------------------------------------
    00295     // validator and normalizer
    00296     // -----------------------------------------------------------------------
    00297
    00298     void                  validateDateTime()          const;
    00299
    00300     void                  normalize();
    00301
    00302     void                  fillString(XMLCh*& ptr, valueIndex ind, int expLen) const;
    00303
    00304     int                   fillYearString(XMLCh*& ptr, valueIndex ind) const;
    00305
    00306     void                  searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const;
    00307
    00308     // -----------------------------------------------------------------------
    00309     // Unimplemented operator ==
    00310     // -----------------------------------------------------------------------
    00311     bool operator==(const XMLDateTime& toCompare) const;
    00312
    00313
    00314     // -----------------------------------------------------------------------
    00315     //  Private data members
    00316     //
    00317     //     fValue[]
    00318     //          object representation of date time.
    00319     //
    00320     //     fTimeZone[]
    00321     //          temporary storage for normalization
    00322     //
    00323     //     fStart, fEnd
    00324     //          pointers to the portion of fBuffer being parsed
    00325     //
    00326     //     fBuffer
    00327     //          raw data to be parsed, own it.
    00328     //
    00329     // -----------------------------------------------------------------------
    00330
    00331     int          fValue[TOTAL_SIZE];
    00332     int          fTimeZone[TIMEZONE_ARRAYSIZE];
    00333     int          fStart;
    00334     int          fEnd;
    00335     int          fBufferMaxLen;
    00336     XMLCh*       fBuffer;
    00337
    00338     double       fMiliSecond;
    00339     bool         fHasTime;
    00340
    00341     MemoryManager* fMemoryManager;
    00342
    00343     friend class XSValue;
    00344 };
    00345
    00346 inline void XMLDateTime::setBuffer(const XMLCh* const aString)
    00347 {
    00348     reset();
    00349
    00350     fEnd = XMLString::stringLen(aString);
    00351     if (fEnd > 0) {
    00352
    00353         if (fEnd > fBufferMaxLen)
    00354         {
    00355             fMemoryManager->deallocate(fBuffer);
    00356             fBufferMaxLen = fEnd + 8;
    00357             fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
    00358         }
    00359
    00360         memcpy(fBuffer, aString, (fEnd+1) * sizeof(XMLCh));
    00361     }
    00362 }
    00363
    00364 inline void XMLDateTime::reset()
    00365 {
    00366     for ( int i=0; i < TOTAL_SIZE; i++ )
    00367         fValue[i] = 0;
    00368
    00369     fMiliSecond   = 0;
    00370     fHasTime      = false;
    00371     fTimeZone[hh] = fTimeZone[mm] = 0;
    00372     fStart = fEnd = 0;
    00373
    00374     if (fBuffer)
    00375         *fBuffer = 0;
    00376 }
    00377
    00378 inline void XMLDateTime::copy(const XMLDateTime& rhs)
    00379 {
    00380     for ( int i = 0; i < TOTAL_SIZE; i++ )
    00381         fValue[i] = rhs.fValue[i];
    00382
    00383     fMiliSecond   = rhs.fMiliSecond;
    00384     fHasTime      = rhs.fHasTime;
    00385     fTimeZone[hh] = rhs.fTimeZone[hh];
    00386     fTimeZone[mm] = rhs.fTimeZone[mm];
    00387     fStart = rhs.fStart;
    00388     fEnd   = rhs.fEnd;
    00389
    00390     if (fEnd > 0)
    00391     {
    00392         if (fEnd > fBufferMaxLen)
    00393         {
    00394             fMemoryManager->deallocate(fBuffer);//delete[] fBuffer;
    00395             fBufferMaxLen = rhs.fBufferMaxLen;
    00396             fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
    00397         }
    00398
    00399         memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh));
    00400     }
    00401 }
    00402
    00403 inline void XMLDateTime::assertBuffer() const
    00404 {
    00405     if ( ( !fBuffer )            ||
    00406          ( fBuffer[0] == chNull ) )
    00407     {
    00408         ThrowXMLwithMemMgr(SchemaDateTimeException
    00409                , XMLExcepts::DateTime_Assert_Buffer_Fail
    00410                , fMemoryManager);
    00411     }
    00412
    00413 }
    00414
    00415 inline void XMLDateTime::initParser()
    00416 {
    00417     assertBuffer();
    00418     fStart = 0;   // to ensure scan from the very first beginning
    00419                   // in case the pointer is updated accidentally by someone else.
    00420 }
    00421
    00422 inline bool XMLDateTime::isNormalized() const
    00423 {
    00424     return ( fValue[utc] == UTC_STD ? true : false );
    00425 }
    00426
    00427 inline int XMLDateTime::getRetVal(int c1, int c2)
    00428 {
    00429     if ((c1 == LESS_THAN    && c2 == GREATER_THAN) ||
    00430         (c1 == GREATER_THAN && c2 == LESS_THAN)      )
    00431     {
    00432         return INDETERMINATE;
    00433     }
    00434
    00435     return ( c1 != INDETERMINATE ) ? c1 : c2;
    00436 }
    00437
    00438 XERCES_CPP_NAMESPACE_END
    00439
    00440 #endif

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