http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Home

Readme
Release Info

Installation
Download
Build

FAQs
Samples
API Docs

DOM C++ Binding
Programming
Migration Guide

Feedback
Bug-Reporting
PDF Document

CVS Repository
Mail Archive

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  * The Apache Software License, Version 1.1
00003  *
00004  * Copyright (c) 2001 The Apache Software Foundation.  All rights
00005  * reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in
00016  *    the documentation and/or other materials provided with the
00017  *    distribution.
00018  *
00019  * 3. The end-user documentation included with the redistribution,
00020  *    if any, must include the following acknowledgment:
00021  *       "This product includes software developed by the
00022  *        Apache Software Foundation (http://www.apache.org/)."
00023  *    Alternately, this acknowledgment may appear in the software itself,
00024  *    if and wherever such third-party acknowledgments normally appear.
00025  *
00026  * 4. The names "Xerces" and "Apache Software Foundation" must
00027  *    not be used to endorse or promote products derived from this
00028  *    software without prior written permission. For written
00029  *    permission, please contact apache\@apache.org.
00030  *
00031  * 5. Products derived from this software may not be called "Apache",
00032  *    nor may "Apache" appear in their name, without prior written
00033  *    permission of the Apache Software Foundation.
00034  *
00035  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00036  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00037  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00038  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00039  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00040  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00041  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00042  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00043  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00044  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00045  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00046  * SUCH DAMAGE.
00047  * ====================================================================
00048  *
00049  * This software consists of voluntary contributions made by many
00050  * individuals on behalf of the Apache Software Foundation, and was
00051  * originally based on software copyright (c) 2001, International
00052  * Business Machines, Inc., http://www.ibm.com .  For more information
00053  * on the Apache Software Foundation, please see
00054  * <http://www.apache.org/>.
00055  */
00056 
00057 /*
00058  * $Id: XMLDateTime.hpp,v 1.17 2004/01/29 11:48:47 cargilld Exp $
00059  * $Log: XMLDateTime.hpp,v $
00060  * Revision 1.17  2004/01/29 11:48:47  cargilld
00061  * Code cleanup changes to get rid of various compiler diagnostic messages.
00062  *
00063  * Revision 1.16  2004/01/13 19:50:56  peiyongz
00064  * remove parseContent()
00065  *
00066  * Revision 1.14  2003/12/31 02:34:11  neilg
00067  * enable production of canonical representations for dates with negative years, or years >9999
00068  *
00069  * Revision 1.13  2003/12/17 20:00:49  cargilld
00070  * Update for memory management so that the static memory manager (one
00071  * used to call Initialize) is only for static data.
00072  *
00073  * Revision 1.12  2003/12/17 00:18:35  cargilld
00074  * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
00075  *
00076  * Revision 1.11  2003/12/11 21:38:12  peiyongz
00077  * support for Canonical Representation for Datatype
00078  *
00079  * Revision 1.10  2003/09/23 18:16:07  peiyongz
00080  * Inplementation for Serialization/Deserialization
00081  *
00082  * Revision 1.9  2003/08/14 02:57:27  knoaman
00083  * Code refactoring to improve performance of validation.
00084  *
00085  * Revision 1.8  2003/05/18 14:02:05  knoaman
00086  * Memory manager implementation: pass per instance manager.
00087  *
00088  * Revision 1.7  2003/05/15 19:07:46  knoaman
00089  * Partial implementation of the configurable memory manager.
00090  *
00091  * Revision 1.6  2003/05/09 15:13:46  peiyongz
00092  * Deprecated toString() in XMLNumber family
00093  *
00094  * Revision 1.5  2003/03/10 20:55:58  peiyongz
00095  * Schema Errata E2-40 double/float
00096  *
00097  * Revision 1.4  2003/02/02 23:54:43  peiyongz
00098  * getFormattedString() added to return original and converted value.
00099  *
00100  * Revision 1.3  2003/01/30 21:55:22  tng
00101  * Performance: create getRawData which is similar to toString but return the internal data directly, user is not required to delete the returned memory.
00102  *
00103  * Revision 1.2  2002/11/04 15:22:05  tng
00104  * C++ Namespace Support.
00105  *
00106  * Revision 1.1.1.1  2002/02/01 22:22:14  peiyongz
00107  * sane_include
00108  *
00109  * Revision 1.4  2001/11/22 20:23:00  peiyongz
00110  * _declspec(dllimport) and inline warning C4273
00111  *
00112  * Revision 1.3  2001/11/12 20:36:54  peiyongz
00113  * SchemaDateTimeException defined
00114  *
00115  * Revision 1.2  2001/11/09 20:41:45  peiyongz
00116  * Fix: compilation error on Solaris and AIX.
00117  *
00118  * Revision 1.1  2001/11/07 19:16:03  peiyongz
00119  * DateTime Port
00120  *
00121  */
00122 
00123 #ifndef XML_DATETIME_HPP
00124 #define XML_DATETIME_HPP
00125 
00126 #include <xercesc/util/XMLNumber.hpp>
00127 #include <xercesc/util/PlatformUtils.hpp>
00128 #include <xercesc/util/XMLString.hpp>
00129 #include <xercesc/util/XMLUniDefs.hpp>
00130 #include <xercesc/util/SchemaDateTimeException.hpp>
00131 
00132 XERCES_CPP_NAMESPACE_BEGIN
00133 
00134 class  XMLDateTime : public XMLNumber
00135 {
00136 public:
00137 
00138     enum valueIndex
00139     {
00140         CentYear   = 0,
00141         Month      ,
00142         Day        ,
00143         Hour       ,
00144         Minute     ,
00145         Second     ,
00146         MiliSecond ,
00147         utc        ,
00148         TOTAL_SIZE
00149     };
00150 
00151     enum utcType
00152     {
00153         UTC_UNKNOWN = 0,
00154         UTC_STD        ,          // set in parse() or normalize()
00155         UTC_POS        ,          // set in parse()
00156         UTC_NEG                   // set in parse()
00157     };
00158 
00159     // -----------------------------------------------------------------------
00160     // ctors and dtor
00161     // -----------------------------------------------------------------------
00162 
00163     XMLDateTime(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
00164     XMLDateTime(const XMLCh* const,
00165                 MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
00166     ~XMLDateTime();
00167 
00168     inline void           setBuffer(const XMLCh* const);
00169 
00170     // -----------------------------------------------------------------------
00171     // Copy ctor and Assignment operators
00172     // -----------------------------------------------------------------------
00173 
00174     XMLDateTime(const XMLDateTime&);
00175 
00176     XMLDateTime&          operator=(const XMLDateTime&);
00177 
00178     // -----------------------------------------------------------------------
00179     // Implementation of Abstract Interface
00180     // -----------------------------------------------------------------------
00181 
00187     virtual XMLCh*        toString() const;
00188     
00189     virtual XMLCh*        getRawData() const;
00190 
00191     virtual const XMLCh*  getFormattedString() const;
00192 
00193     virtual int           getSign() const;
00194 
00195     // -----------------------------------------------------------------------
00196     // Canonical Representation
00197     // -----------------------------------------------------------------------
00198 
00199     XMLCh*                getDateTimeCanonicalRepresentation(MemoryManager* const memMgr) const;
00200 
00201     XMLCh*                getTimeCanonicalRepresentation(MemoryManager* const memMgr)     const;
00202 
00203     // -----------------------------------------------------------------------
00204     // parsers
00205     // -----------------------------------------------------------------------
00206 
00207     void                  parseDateTime();       //DateTime
00208 
00209     void                  parseDate();           //Date
00210 
00211     void                  parseTime();           //Time
00212 
00213     void                  parseDay();            //gDay
00214 
00215     void                  parseMonth();          //gMonth
00216 
00217     void                  parseYear();           //gYear
00218 
00219     void                  parseMonthDay();       //gMonthDay
00220 
00221     void                  parseYearMonth();      //gYearMonth
00222 
00223     void                  parseDuration();       //duration
00224 
00225     // -----------------------------------------------------------------------
00226     // Comparison
00227     // -----------------------------------------------------------------------
00228     static int            compare(const XMLDateTime* const
00229                                 , const XMLDateTime* const);
00230 
00231     static int            compare(const XMLDateTime* const
00232                                 , const XMLDateTime* const
00233                                 , bool                    );
00234 
00235     static int            compareOrder(const XMLDateTime* const
00236                                      , const XMLDateTime* const);                                    
00237 
00238     /***
00239      * Support for Serialization/De-serialization
00240      ***/
00241     DECL_XSERIALIZABLE(XMLDateTime)
00242 
00243 private:
00244 
00245     // -----------------------------------------------------------------------
00246     // Constant data
00247     // -----------------------------------------------------------------------
00248     //
00249     enum timezoneIndex
00250     {
00251         hh = 0,
00252         mm ,
00253         TIMEZONE_ARRAYSIZE
00254     };
00255 
00256     // -----------------------------------------------------------------------
00257     // Comparison
00258     // -----------------------------------------------------------------------
00259     static int            compareResult(int
00260                                       , int
00261                                       , bool);
00262 
00263     static void           addDuration(XMLDateTime*             pDuration
00264                                     , const XMLDateTime* const pBaseDate
00265                                     , int                      index);
00266 
00267 
00268     static int            compareResult(const XMLDateTime* const
00269                                       , const XMLDateTime* const
00270                                       , bool
00271                                       , int);
00272 
00273     static inline int     getRetVal(int, int);
00274 
00275     // -----------------------------------------------------------------------
00276     // helper
00277     // -----------------------------------------------------------------------
00278 
00279     inline  void          reset();
00280 
00281     inline  void          assertBuffer()               const;
00282 
00283     inline  void          copy(const XMLDateTime&);
00284 
00285     // allow multiple parsing
00286     inline  void          initParser();
00287 
00288     inline  bool          isNormalized()               const;
00289 
00290     // -----------------------------------------------------------------------
00291     // scaners
00292     // -----------------------------------------------------------------------
00293 
00294     void                  getDate();
00295 
00296     void                  getTime();
00297 
00298     void                  getYearMonth();
00299 
00300     void                  getTimeZone(const int);
00301 
00302     void                  parseTimeZone();
00303 
00304     // -----------------------------------------------------------------------
00305     // locator and converter
00306     // -----------------------------------------------------------------------
00307 
00308     int                   findUTCSign(const int start);
00309 
00310     int                   indexOf(const int start
00311                                 , const int end
00312                                 , const XMLCh ch)     const;
00313 
00314     int                   parseInt(const int start
00315                                  , const int end)     const;
00316 
00317     int                   parseIntYear(const int end) const;
00318 
00319     // -----------------------------------------------------------------------
00320     // validator and normalizer
00321     // -----------------------------------------------------------------------
00322 
00323     void                  validateDateTime()          const;
00324 
00325     void                  normalize();
00326 
00327     void                  fillString(XMLCh*& ptr, valueIndex ind, int expLen) const;
00328 
00329     int                   fillYearString(XMLCh*& ptr, valueIndex ind) const;
00330 
00331     void                  searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const;
00332 
00333     // -----------------------------------------------------------------------
00334     // Unimplemented operator ==
00335     // -----------------------------------------------------------------------
00336     bool operator==(const XMLDateTime& toCompare) const;
00337 
00338 
00339     // -----------------------------------------------------------------------
00340     //  Private data members
00341     //
00342     //     fValue[]
00343     //          object representation of date time.
00344     //
00345     //     fTimeZone[]
00346     //          temporary storage for normalization
00347     //
00348     //     fStart, fEnd
00349     //          pointers to the portion of fBuffer being parsed
00350     //
00351     //     fBuffer
00352     //          raw data to be parsed, own it.
00353     //
00354     // -----------------------------------------------------------------------
00355 
00356     int          fValue[TOTAL_SIZE];
00357     int          fTimeZone[TIMEZONE_ARRAYSIZE];
00358     int          fStart;
00359     int          fEnd;
00360     int          fBufferMaxLen;
00361     XMLCh*       fBuffer;
00362     MemoryManager* fMemoryManager;
00363 };
00364 
00365 inline void XMLDateTime::setBuffer(const XMLCh* const aString)
00366 {
00367     reset();
00368 
00369     fEnd = XMLString::stringLen(aString);
00370     if (fEnd > 0) {
00371     
00372         if (fEnd > fBufferMaxLen)
00373         {
00374             fMemoryManager->deallocate(fBuffer);
00375             fBufferMaxLen = fEnd + 8;
00376             fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
00377         }
00378 
00379         memcpy(fBuffer, aString, (fEnd+1) * sizeof(XMLCh));
00380     }
00381 }
00382 
00383 inline void XMLDateTime::reset()
00384 {
00385     for ( int i=0; i < TOTAL_SIZE; i++ )
00386         fValue[i] = 0;
00387 
00388     fTimeZone[hh] = fTimeZone[mm] = 0;
00389     fStart = fEnd = 0;
00390 
00391     if (fBuffer)
00392         *fBuffer = 0;
00393 }
00394 
00395 inline void XMLDateTime::copy(const XMLDateTime& rhs)
00396 {
00397     for ( int i = 0; i < TOTAL_SIZE; i++ )
00398         fValue[i] = rhs.fValue[i];
00399 
00400     fTimeZone[hh] = rhs.fTimeZone[hh];
00401     fTimeZone[mm] = rhs.fTimeZone[mm];
00402     fStart = rhs.fStart;
00403     fEnd   = rhs.fEnd;
00404 
00405     if (fEnd > 0)
00406     {
00407         if (fEnd > fBufferMaxLen)
00408         {
00409             fMemoryManager->deallocate(fBuffer);//delete[] fBuffer;
00410             fBufferMaxLen = rhs.fBufferMaxLen;
00411             fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
00412         }
00413 
00414         memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh));
00415     }
00416 }
00417 
00418 inline void XMLDateTime::assertBuffer() const
00419 {
00420     if ( ( !fBuffer )            ||
00421          ( fBuffer[0] == chNull ) )
00422     {
00423         ThrowXMLwithMemMgr(SchemaDateTimeException
00424                , XMLExcepts::DateTime_Assert_Buffer_Fail
00425                , fMemoryManager);
00426     }
00427 
00428 }
00429 
00430 inline void XMLDateTime::initParser()
00431 {
00432     assertBuffer();
00433     fStart = 0;   // to ensure scan from the very first beginning
00434                   // in case the pointer is updated accidentally by someone else.
00435 }
00436 
00437 inline bool XMLDateTime::isNormalized() const
00438 {
00439     return ( fValue[utc] == UTC_STD ? true : false );
00440 }
00441 
00442 inline int XMLDateTime::getRetVal(int c1, int c2)
00443 {
00444     if ((c1 == LESS_THAN    && c2 == GREATER_THAN) ||
00445         (c1 == GREATER_THAN && c2 == LESS_THAN)      )
00446     {
00447         return INDETERMINATE;
00448     }
00449 
00450     return ( c1 != INDETERMINATE ) ? c1 : c2;
00451 }
00452 
00453 XERCES_CPP_NAMESPACE_END
00454 
00455 #endif


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