Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:15

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_XML_DATETIME_HPP)
0023 #define XERCESC_INCLUDE_GUARD_XML_DATETIME_HPP
0024 
0025 #include <xercesc/util/XMLNumber.hpp>
0026 #include <xercesc/util/PlatformUtils.hpp>
0027 #include <xercesc/util/XMLString.hpp>
0028 #include <xercesc/util/XMLUniDefs.hpp>
0029 #include <xercesc/util/SchemaDateTimeException.hpp>
0030 #include <xercesc/util/XMLChar.hpp>
0031 
0032 #include <time.h>
0033 
0034 XERCES_CPP_NAMESPACE_BEGIN
0035 
0036 class XSValue;
0037 
0038 class XMLUTIL_EXPORT XMLDateTime : public XMLNumber
0039 {
0040 public:
0041 
0042     enum valueIndex
0043     {
0044         CentYear   = 0,
0045         Month      ,
0046         Day        ,
0047         Hour       ,
0048         Minute     ,
0049         Second     ,
0050         MiliSecond ,  //not to be used directly
0051         utc        ,
0052         TOTAL_SIZE
0053     };
0054 
0055     enum utcType
0056     {
0057         UTC_UNKNOWN = 0,
0058         UTC_STD        ,          // set in parse() or normalize()
0059         UTC_POS        ,          // set in parse()
0060         UTC_NEG                   // set in parse()
0061     };
0062 
0063     // -----------------------------------------------------------------------
0064     // ctors and dtor
0065     // -----------------------------------------------------------------------
0066 
0067     XMLDateTime(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0068     XMLDateTime(const XMLCh* const,
0069                 MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0070     XMLDateTime(time_t epoch, bool duration,
0071                     MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0072     ~XMLDateTime();
0073 
0074     inline void           setBuffer(const XMLCh* const);
0075 
0076     // -----------------------------------------------------------------------
0077     // Copy ctor and Assignment operators
0078     // -----------------------------------------------------------------------
0079 
0080     XMLDateTime(const XMLDateTime&);
0081 
0082     XMLDateTime&          operator=(const XMLDateTime&);
0083 
0084     // -----------------------------------------------------------------------
0085     // Implementation of Abstract Interface
0086     // -----------------------------------------------------------------------
0087 
0088     virtual XMLCh*        getRawData() const;
0089 
0090     virtual const XMLCh*  getFormattedString() const;
0091 
0092     virtual int           getSign() const;
0093 
0094     // -----------------------------------------------------------------------
0095     // Canonical Representation
0096     // -----------------------------------------------------------------------
0097 
0098     XMLCh*                getDateTimeCanonicalRepresentation(MemoryManager* const memMgr) const;
0099 
0100     XMLCh*                getTimeCanonicalRepresentation(MemoryManager* const memMgr)     const;
0101 
0102     XMLCh*                getDateCanonicalRepresentation(MemoryManager* const memMgr)     const;
0103 
0104     // -----------------------------------------------------------------------
0105     // parsers
0106     // -----------------------------------------------------------------------
0107 
0108     void                  parseDateTime();       //DateTime
0109 
0110     void                  parseDate();           //Date
0111 
0112     void                  parseTime();           //Time
0113 
0114     void                  parseDay();            //gDay
0115 
0116     void                  parseMonth();          //gMonth
0117 
0118     void                  parseYear();           //gYear
0119 
0120     void                  parseMonthDay();       //gMonthDay
0121 
0122     void                  parseYearMonth();      //gYearMonth
0123 
0124     void                  parseDuration();       //duration
0125 
0126     // -----------------------------------------------------------------------
0127     // Comparison
0128     // -----------------------------------------------------------------------
0129     static int            compare(const XMLDateTime* const
0130                                 , const XMLDateTime* const);
0131 
0132     static int            compare(const XMLDateTime* const
0133                                 , const XMLDateTime* const
0134                                 , bool                    );
0135 
0136     static int            compareOrder(const XMLDateTime* const
0137                                      , const XMLDateTime* const);
0138 
0139     int getYear() const {return fValue[CentYear];}
0140     int getMonth() const {return fValue[Month];}
0141     int getDay() const {return fValue[Day];}
0142     int getHour() const {return fValue[Hour];}
0143     int getMinute() const {return fValue[Minute];}
0144     int getSecond() const {return fValue[Second];}
0145     time_t getEpoch(bool duration=false) const;
0146 
0147     /***
0148      * Support for Serialization/De-serialization
0149      ***/
0150     DECL_XSERIALIZABLE(XMLDateTime)
0151 
0152 private:
0153 
0154     // -----------------------------------------------------------------------
0155     // Constant data
0156     // -----------------------------------------------------------------------
0157     //
0158 
0159     enum timezoneIndex
0160     {
0161         hh = 0,
0162         mm ,
0163         TIMEZONE_ARRAYSIZE
0164     };
0165 
0166     // -----------------------------------------------------------------------
0167     // Comparison
0168     // -----------------------------------------------------------------------
0169     static int            compareResult(int
0170                                       , int
0171                                       , bool);
0172 
0173     static void           addDuration(XMLDateTime*             pDuration
0174                                     , const XMLDateTime* const pBaseDate
0175                                     , int                      index);
0176 
0177 
0178     static int            compareResult(const XMLDateTime* const
0179                                       , const XMLDateTime* const
0180                                       , bool
0181                                       , int);
0182 
0183     static inline int     getRetVal(int, int);
0184 
0185     // -----------------------------------------------------------------------
0186     // helper
0187     // -----------------------------------------------------------------------
0188 
0189     inline  void          reset();
0190 
0191     inline  void          assertBuffer()               const;
0192 
0193     inline  void          copy(const XMLDateTime&);
0194 
0195     // allow multiple parsing
0196     inline  bool          initParser();
0197 
0198     inline  bool          isNormalized()               const;
0199 
0200     // -----------------------------------------------------------------------
0201     // scaners
0202     // -----------------------------------------------------------------------
0203 
0204     void                  getDate();
0205 
0206     void                  getTime();
0207 
0208     void                  getYearMonth();
0209 
0210     void                  getTimeZone(const XMLSize_t);
0211 
0212     void                  parseTimeZone();
0213 
0214     // -----------------------------------------------------------------------
0215     // locator and converter
0216     // -----------------------------------------------------------------------
0217 
0218     int                   findUTCSign(const XMLSize_t start);
0219 
0220     int                   indexOf(const XMLSize_t start
0221                                 , const XMLSize_t end
0222                                 , const XMLCh ch)     const;
0223 
0224     int                   parseInt(const XMLSize_t start
0225                                  , const XMLSize_t end)     const;
0226 
0227     int                   parseIntYear(const XMLSize_t end) const;
0228 
0229     double                parseMiliSecond(const XMLSize_t start
0230                                         , const XMLSize_t end) const;
0231 
0232     // -----------------------------------------------------------------------
0233     // validator and normalizer
0234     // -----------------------------------------------------------------------
0235 
0236     void                  validateDateTime()          const;
0237 
0238     void                  normalize();
0239 
0240     void                  fillString(XMLCh*& ptr, int value, XMLSize_t expLen) const;
0241 
0242     int                   fillYearString(XMLCh*& ptr, int value) const;
0243 
0244     void                  searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const;
0245 
0246     // -----------------------------------------------------------------------
0247     // Unimplemented operator ==
0248     // -----------------------------------------------------------------------
0249     bool operator==(const XMLDateTime& toCompare) const;
0250 
0251 
0252     // -----------------------------------------------------------------------
0253     //  Private data members
0254     //
0255     //     fValue[]
0256     //          object representation of date time.
0257     //
0258     //     fTimeZone[]
0259     //          temporary storage for normalization
0260     //
0261     //     fStart, fEnd
0262     //          pointers to the portion of fBuffer being parsed
0263     //
0264     //     fBuffer
0265     //          raw data to be parsed, own it.
0266     //
0267     // -----------------------------------------------------------------------
0268 
0269     int          fValue[TOTAL_SIZE];
0270     int          fTimeZone[TIMEZONE_ARRAYSIZE];
0271     XMLSize_t    fStart;
0272     XMLSize_t    fEnd;
0273     XMLSize_t    fBufferMaxLen;
0274 
0275     double       fMilliSecond;
0276     bool         fHasTime;
0277 
0278     XMLCh*       fBuffer;
0279     MemoryManager* fMemoryManager;
0280 
0281     friend class XSValue;
0282 };
0283 
0284 inline void XMLDateTime::setBuffer(const XMLCh* const aString)
0285 {
0286     reset();
0287 
0288     fEnd = XMLString::stringLen(aString);
0289 
0290     for (; fEnd > 0; fEnd--)
0291     {
0292         if (!XMLChar1_0::isWhitespace(aString[fEnd - 1]))
0293             break;
0294     }
0295 
0296     if (fEnd > 0) {
0297 
0298         if (fEnd > fBufferMaxLen)
0299         {
0300             fMemoryManager->deallocate(fBuffer);
0301             fBufferMaxLen = fEnd + 8;
0302             fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
0303         }
0304 
0305         memcpy(fBuffer, aString, (fEnd) * sizeof(XMLCh));
0306         fBuffer[fEnd] = '\0';
0307     }
0308 }
0309 
0310 inline void XMLDateTime::reset()
0311 {
0312     for ( int i=0; i < TOTAL_SIZE; i++ )
0313         fValue[i] = 0;
0314 
0315     fMilliSecond   = 0;
0316     fHasTime      = false;
0317     fTimeZone[hh] = fTimeZone[mm] = 0;
0318     fStart = fEnd = 0;
0319 
0320     if (fBuffer)
0321         *fBuffer = 0;
0322 }
0323 
0324 inline void XMLDateTime::copy(const XMLDateTime& rhs)
0325 {
0326     for ( int i = 0; i < TOTAL_SIZE; i++ )
0327         fValue[i] = rhs.fValue[i];
0328 
0329     fMilliSecond   = rhs.fMilliSecond;
0330     fHasTime      = rhs.fHasTime;
0331     fTimeZone[hh] = rhs.fTimeZone[hh];
0332     fTimeZone[mm] = rhs.fTimeZone[mm];
0333     fStart = rhs.fStart;
0334     fEnd   = rhs.fEnd;
0335 
0336     if (fEnd > 0)
0337     {
0338         if (fEnd > fBufferMaxLen)
0339         {
0340             fMemoryManager->deallocate(fBuffer);//delete[] fBuffer;
0341             fBufferMaxLen = rhs.fBufferMaxLen;
0342             fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
0343         }
0344 
0345         memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh));
0346     }
0347 }
0348 
0349 inline bool XMLDateTime::initParser()
0350 {
0351     if (!fBuffer || fBuffer[0] == chNull)
0352         return false;
0353 
0354     fStart = 0;   // to ensure scan from the very first beginning
0355                   // in case the pointer is updated accidentally by
0356                   // someone else.
0357     return true;
0358 }
0359 
0360 inline bool XMLDateTime::isNormalized() const
0361 {
0362     return ( fValue[utc] == UTC_STD ? true : false );
0363 }
0364 
0365 inline int XMLDateTime::getRetVal(int c1, int c2)
0366 {
0367     if ((c1 == LESS_THAN    && c2 == GREATER_THAN) ||
0368         (c1 == GREATER_THAN && c2 == LESS_THAN)      )
0369     {
0370         return INDETERMINATE;
0371     }
0372 
0373     return ( c1 != INDETERMINATE ) ? c1 : c2;
0374 }
0375 
0376 XERCES_CPP_NAMESPACE_END
0377 
0378 #endif