[2674] | 1 | /* |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 4 | * this work for additional information regarding copyright ownership. |
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 6 | * (the "License"); you may not use this file except in compliance with |
---|
| 7 | * the License. You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | /* |
---|
| 19 | * $Id: XMLDateTime.hpp 568078 2007-08-21 11:43:25Z amassari $ |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #ifndef XML_DATETIME_HPP |
---|
| 23 | #define XML_DATETIME_HPP |
---|
| 24 | |
---|
| 25 | #include <xercesc/util/XMLNumber.hpp> |
---|
| 26 | #include <xercesc/util/PlatformUtils.hpp> |
---|
| 27 | #include <xercesc/util/XMLString.hpp> |
---|
| 28 | #include <xercesc/util/XMLUniDefs.hpp> |
---|
| 29 | #include <xercesc/util/SchemaDateTimeException.hpp> |
---|
| 30 | |
---|
| 31 | XERCES_CPP_NAMESPACE_BEGIN |
---|
| 32 | |
---|
| 33 | class XSValue; |
---|
| 34 | |
---|
| 35 | class XMLUTIL_EXPORT XMLDateTime : public XMLNumber |
---|
| 36 | { |
---|
| 37 | public: |
---|
| 38 | |
---|
| 39 | enum valueIndex |
---|
| 40 | { |
---|
| 41 | CentYear = 0, |
---|
| 42 | Month , |
---|
| 43 | Day , |
---|
| 44 | Hour , |
---|
| 45 | Minute , |
---|
| 46 | Second , |
---|
| 47 | MiliSecond , //not to be used directly |
---|
| 48 | utc , |
---|
| 49 | TOTAL_SIZE |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | enum utcType |
---|
| 53 | { |
---|
| 54 | UTC_UNKNOWN = 0, |
---|
| 55 | UTC_STD , // set in parse() or normalize() |
---|
| 56 | UTC_POS , // set in parse() |
---|
| 57 | UTC_NEG // set in parse() |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | // ----------------------------------------------------------------------- |
---|
| 61 | // ctors and dtor |
---|
| 62 | // ----------------------------------------------------------------------- |
---|
| 63 | |
---|
| 64 | XMLDateTime(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
| 65 | XMLDateTime(const XMLCh* const, |
---|
| 66 | MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
| 67 | ~XMLDateTime(); |
---|
| 68 | |
---|
| 69 | inline void setBuffer(const XMLCh* const); |
---|
| 70 | |
---|
| 71 | // ----------------------------------------------------------------------- |
---|
| 72 | // Copy ctor and Assignment operators |
---|
| 73 | // ----------------------------------------------------------------------- |
---|
| 74 | |
---|
| 75 | XMLDateTime(const XMLDateTime&); |
---|
| 76 | |
---|
| 77 | XMLDateTime& operator=(const XMLDateTime&); |
---|
| 78 | |
---|
| 79 | // ----------------------------------------------------------------------- |
---|
| 80 | // Implementation of Abstract Interface |
---|
| 81 | // ----------------------------------------------------------------------- |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * |
---|
| 85 | * Deprecated: please use getRawData |
---|
| 86 | * |
---|
| 87 | */ |
---|
| 88 | virtual XMLCh* toString() const; |
---|
| 89 | |
---|
| 90 | virtual XMLCh* getRawData() const; |
---|
| 91 | |
---|
| 92 | virtual const XMLCh* getFormattedString() const; |
---|
| 93 | |
---|
| 94 | virtual int getSign() const; |
---|
| 95 | |
---|
| 96 | // ----------------------------------------------------------------------- |
---|
| 97 | // Canonical Representation |
---|
| 98 | // ----------------------------------------------------------------------- |
---|
| 99 | |
---|
| 100 | XMLCh* getDateTimeCanonicalRepresentation(MemoryManager* const memMgr) const; |
---|
| 101 | |
---|
| 102 | XMLCh* getTimeCanonicalRepresentation(MemoryManager* const memMgr) const; |
---|
| 103 | |
---|
| 104 | XMLCh* getDateCanonicalRepresentation(MemoryManager* const memMgr) const; |
---|
| 105 | |
---|
| 106 | // ----------------------------------------------------------------------- |
---|
| 107 | // parsers |
---|
| 108 | // ----------------------------------------------------------------------- |
---|
| 109 | |
---|
| 110 | void parseDateTime(); //DateTime |
---|
| 111 | |
---|
| 112 | void parseDate(); //Date |
---|
| 113 | |
---|
| 114 | void parseTime(); //Time |
---|
| 115 | |
---|
| 116 | void parseDay(); //gDay |
---|
| 117 | |
---|
| 118 | void parseMonth(); //gMonth |
---|
| 119 | |
---|
| 120 | void parseYear(); //gYear |
---|
| 121 | |
---|
| 122 | void parseMonthDay(); //gMonthDay |
---|
| 123 | |
---|
| 124 | void parseYearMonth(); //gYearMonth |
---|
| 125 | |
---|
| 126 | void parseDuration(); //duration |
---|
| 127 | |
---|
| 128 | // ----------------------------------------------------------------------- |
---|
| 129 | // Comparison |
---|
| 130 | // ----------------------------------------------------------------------- |
---|
| 131 | static int compare(const XMLDateTime* const |
---|
| 132 | , const XMLDateTime* const); |
---|
| 133 | |
---|
| 134 | static int compare(const XMLDateTime* const |
---|
| 135 | , const XMLDateTime* const |
---|
| 136 | , bool ); |
---|
| 137 | |
---|
| 138 | static int compareOrder(const XMLDateTime* const |
---|
| 139 | , const XMLDateTime* const); |
---|
| 140 | |
---|
| 141 | /*** |
---|
| 142 | * Support for Serialization/De-serialization |
---|
| 143 | ***/ |
---|
| 144 | DECL_XSERIALIZABLE(XMLDateTime) |
---|
| 145 | |
---|
| 146 | private: |
---|
| 147 | |
---|
| 148 | // ----------------------------------------------------------------------- |
---|
| 149 | // Constant data |
---|
| 150 | // ----------------------------------------------------------------------- |
---|
| 151 | // |
---|
| 152 | |
---|
| 153 | enum timezoneIndex |
---|
| 154 | { |
---|
| 155 | hh = 0, |
---|
| 156 | mm , |
---|
| 157 | TIMEZONE_ARRAYSIZE |
---|
| 158 | }; |
---|
| 159 | |
---|
| 160 | // ----------------------------------------------------------------------- |
---|
| 161 | // Comparison |
---|
| 162 | // ----------------------------------------------------------------------- |
---|
| 163 | static int compareResult(int |
---|
| 164 | , int |
---|
| 165 | , bool); |
---|
| 166 | |
---|
| 167 | static void addDuration(XMLDateTime* pDuration |
---|
| 168 | , const XMLDateTime* const pBaseDate |
---|
| 169 | , int index); |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | static int compareResult(const XMLDateTime* const |
---|
| 173 | , const XMLDateTime* const |
---|
| 174 | , bool |
---|
| 175 | , int); |
---|
| 176 | |
---|
| 177 | static inline int getRetVal(int, int); |
---|
| 178 | |
---|
| 179 | // ----------------------------------------------------------------------- |
---|
| 180 | // helper |
---|
| 181 | // ----------------------------------------------------------------------- |
---|
| 182 | |
---|
| 183 | inline void reset(); |
---|
| 184 | |
---|
| 185 | inline void assertBuffer() const; |
---|
| 186 | |
---|
| 187 | inline void copy(const XMLDateTime&); |
---|
| 188 | |
---|
| 189 | // allow multiple parsing |
---|
| 190 | inline void initParser(); |
---|
| 191 | |
---|
| 192 | inline bool isNormalized() const; |
---|
| 193 | |
---|
| 194 | // ----------------------------------------------------------------------- |
---|
| 195 | // scaners |
---|
| 196 | // ----------------------------------------------------------------------- |
---|
| 197 | |
---|
| 198 | void getDate(); |
---|
| 199 | |
---|
| 200 | void getTime(); |
---|
| 201 | |
---|
| 202 | void getYearMonth(); |
---|
| 203 | |
---|
| 204 | void getTimeZone(const int); |
---|
| 205 | |
---|
| 206 | void parseTimeZone(); |
---|
| 207 | |
---|
| 208 | // ----------------------------------------------------------------------- |
---|
| 209 | // locator and converter |
---|
| 210 | // ----------------------------------------------------------------------- |
---|
| 211 | |
---|
| 212 | int findUTCSign(const int start); |
---|
| 213 | |
---|
| 214 | int indexOf(const int start |
---|
| 215 | , const int end |
---|
| 216 | , const XMLCh ch) const; |
---|
| 217 | |
---|
| 218 | int parseInt(const int start |
---|
| 219 | , const int end) const; |
---|
| 220 | |
---|
| 221 | int parseIntYear(const int end) const; |
---|
| 222 | |
---|
| 223 | double parseMiliSecond(const int start |
---|
| 224 | , const int end) const; |
---|
| 225 | |
---|
| 226 | // ----------------------------------------------------------------------- |
---|
| 227 | // validator and normalizer |
---|
| 228 | // ----------------------------------------------------------------------- |
---|
| 229 | |
---|
| 230 | void validateDateTime() const; |
---|
| 231 | |
---|
| 232 | void normalize(); |
---|
| 233 | |
---|
| 234 | void fillString(XMLCh*& ptr, int value, int expLen) const; |
---|
| 235 | |
---|
| 236 | int fillYearString(XMLCh*& ptr, int value) const; |
---|
| 237 | |
---|
| 238 | void searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const; |
---|
| 239 | |
---|
| 240 | // ----------------------------------------------------------------------- |
---|
| 241 | // Unimplemented operator == |
---|
| 242 | // ----------------------------------------------------------------------- |
---|
| 243 | bool operator==(const XMLDateTime& toCompare) const; |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | // ----------------------------------------------------------------------- |
---|
| 247 | // Private data members |
---|
| 248 | // |
---|
| 249 | // fValue[] |
---|
| 250 | // object representation of date time. |
---|
| 251 | // |
---|
| 252 | // fTimeZone[] |
---|
| 253 | // temporary storage for normalization |
---|
| 254 | // |
---|
| 255 | // fStart, fEnd |
---|
| 256 | // pointers to the portion of fBuffer being parsed |
---|
| 257 | // |
---|
| 258 | // fBuffer |
---|
| 259 | // raw data to be parsed, own it. |
---|
| 260 | // |
---|
| 261 | // ----------------------------------------------------------------------- |
---|
| 262 | |
---|
| 263 | int fValue[TOTAL_SIZE]; |
---|
| 264 | int fTimeZone[TIMEZONE_ARRAYSIZE]; |
---|
| 265 | int fStart; |
---|
| 266 | int fEnd; |
---|
| 267 | int fBufferMaxLen; |
---|
| 268 | |
---|
| 269 | double fMiliSecond; |
---|
| 270 | bool fHasTime; |
---|
| 271 | |
---|
| 272 | XMLCh* fBuffer; |
---|
| 273 | MemoryManager* fMemoryManager; |
---|
| 274 | |
---|
| 275 | friend class XSValue; |
---|
| 276 | }; |
---|
| 277 | |
---|
| 278 | inline void XMLDateTime::setBuffer(const XMLCh* const aString) |
---|
| 279 | { |
---|
| 280 | reset(); |
---|
| 281 | |
---|
| 282 | fEnd = XMLString::stringLen(aString); |
---|
| 283 | if (fEnd > 0) { |
---|
| 284 | |
---|
| 285 | if (fEnd > fBufferMaxLen) |
---|
| 286 | { |
---|
| 287 | fMemoryManager->deallocate(fBuffer); |
---|
| 288 | fBufferMaxLen = fEnd + 8; |
---|
| 289 | fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh)); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | memcpy(fBuffer, aString, (fEnd+1) * sizeof(XMLCh)); |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | inline void XMLDateTime::reset() |
---|
| 297 | { |
---|
| 298 | for ( int i=0; i < TOTAL_SIZE; i++ ) |
---|
| 299 | fValue[i] = 0; |
---|
| 300 | |
---|
| 301 | fMiliSecond = 0; |
---|
| 302 | fHasTime = false; |
---|
| 303 | fTimeZone[hh] = fTimeZone[mm] = 0; |
---|
| 304 | fStart = fEnd = 0; |
---|
| 305 | |
---|
| 306 | if (fBuffer) |
---|
| 307 | *fBuffer = 0; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | inline void XMLDateTime::copy(const XMLDateTime& rhs) |
---|
| 311 | { |
---|
| 312 | for ( int i = 0; i < TOTAL_SIZE; i++ ) |
---|
| 313 | fValue[i] = rhs.fValue[i]; |
---|
| 314 | |
---|
| 315 | fMiliSecond = rhs.fMiliSecond; |
---|
| 316 | fHasTime = rhs.fHasTime; |
---|
| 317 | fTimeZone[hh] = rhs.fTimeZone[hh]; |
---|
| 318 | fTimeZone[mm] = rhs.fTimeZone[mm]; |
---|
| 319 | fStart = rhs.fStart; |
---|
| 320 | fEnd = rhs.fEnd; |
---|
| 321 | |
---|
| 322 | if (fEnd > 0) |
---|
| 323 | { |
---|
| 324 | if (fEnd > fBufferMaxLen) |
---|
| 325 | { |
---|
| 326 | fMemoryManager->deallocate(fBuffer);//delete[] fBuffer; |
---|
| 327 | fBufferMaxLen = rhs.fBufferMaxLen; |
---|
| 328 | fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh)); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh)); |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | inline void XMLDateTime::assertBuffer() const |
---|
| 336 | { |
---|
| 337 | if ( ( !fBuffer ) || |
---|
| 338 | ( fBuffer[0] == chNull ) ) |
---|
| 339 | { |
---|
| 340 | ThrowXMLwithMemMgr(SchemaDateTimeException |
---|
| 341 | , XMLExcepts::DateTime_Assert_Buffer_Fail |
---|
| 342 | , fMemoryManager); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | inline void XMLDateTime::initParser() |
---|
| 348 | { |
---|
| 349 | assertBuffer(); |
---|
| 350 | fStart = 0; // to ensure scan from the very first beginning |
---|
| 351 | // in case the pointer is updated accidentally by someone else. |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | inline bool XMLDateTime::isNormalized() const |
---|
| 355 | { |
---|
| 356 | return ( fValue[utc] == UTC_STD ? true : false ); |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | inline int XMLDateTime::getRetVal(int c1, int c2) |
---|
| 360 | { |
---|
| 361 | if ((c1 == LESS_THAN && c2 == GREATER_THAN) || |
---|
| 362 | (c1 == GREATER_THAN && c2 == LESS_THAN) ) |
---|
| 363 | { |
---|
| 364 | return INDETERMINATE; |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | return ( c1 != INDETERMINATE ) ? c1 : c2; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | XERCES_CPP_NAMESPACE_END |
---|
| 371 | |
---|
| 372 | #endif |
---|