Xerces-C++ 3.3.0

XMLDateTime.hpp
Go to the documentation of this file.
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$
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_XML_DATETIME_HPP)
23#define XERCESC_INCLUDE_GUARD_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#include <xercesc/util/XMLChar.hpp>
31
32#include <time.h>
33
34 XERCES_CPP_NAMESPACE_BEGIN
35
36 class XSValue;
37
38 class XMLUTIL_EXPORT XMLDateTime : public XMLNumber
39{
40 public:
41
42 enum valueIndex
43 {
44 CentYear = 0,
45 Month ,
46 Day ,
47 Hour ,
48 Minute ,
49 Second ,
50 MiliSecond , //not to be used directly
51 utc ,
52 TOTAL_SIZE
53 };
54
55 enum utcType
56 {
57 UTC_UNKNOWN = 0,
58 UTC_STD , // set in parse() or normalize()
59 UTC_POS , // set in parse()
60 UTC_NEG // set in parse()
61 };
62
63 // -----------------------------------------------------------------------
64 // ctors and dtor
65 // -----------------------------------------------------------------------
66
67 XMLDateTime(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
68 XMLDateTime(const XMLCh* const,
69 MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
70 XMLDateTime(time_t epoch, bool duration,
71 MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
72 ~XMLDateTime();
73
74 inline void setBuffer(const XMLCh* const);
75
76 // -----------------------------------------------------------------------
77 // Copy ctor and Assignment operators
78 // -----------------------------------------------------------------------
79
80 XMLDateTime(const XMLDateTime&);
81
82 XMLDateTime& operator=(const XMLDateTime&);
83
84 // -----------------------------------------------------------------------
85 // Implementation of Abstract Interface
86 // -----------------------------------------------------------------------
87
88 virtual XMLCh* getRawData() const;
89
90 virtual const XMLCh* getFormattedString() const;
91
92 virtual int getSign() const;
93
94 // -----------------------------------------------------------------------
95 // Canonical Representation
96 // -----------------------------------------------------------------------
97
98 XMLCh* getDateTimeCanonicalRepresentation(MemoryManager* const memMgr) const;
99
100 XMLCh* getTimeCanonicalRepresentation(MemoryManager* const memMgr) const;
101
102 XMLCh* getDateCanonicalRepresentation(MemoryManager* const memMgr) const;
103
104 // -----------------------------------------------------------------------
105 // parsers
106 // -----------------------------------------------------------------------
107
108 void parseDateTime(); //DateTime
109
110 void parseDate(); //Date
111
112 void parseTime(); //Time
113
114 void parseDay(); //gDay
115
116 void parseMonth(); //gMonth
117
118 void parseYear(); //gYear
119
120 void parseMonthDay(); //gMonthDay
121
122 void parseYearMonth(); //gYearMonth
123
124 void parseDuration(); //duration
125
126 // -----------------------------------------------------------------------
127 // Comparison
128 // -----------------------------------------------------------------------
129 static int compare(const XMLDateTime* const
130 , const XMLDateTime* const);
131
132 static int compare(const XMLDateTime* const
133 , const XMLDateTime* const
134 , bool );
135
136 static int compareOrder(const XMLDateTime* const
137 , const XMLDateTime* const);
138
139 int getYear() const {return fValue[CentYear];}
140 int getMonth() const {return fValue[Month];}
141 int getDay() const {return fValue[Day];}
142 int getHour() const {return fValue[Hour];}
143 int getMinute() const {return fValue[Minute];}
144 int getSecond() const {return fValue[Second];}
145 time_t getEpoch(bool duration=false) const;
146
147 /***
148 * Support for Serialization/De-serialization
149 ***/
150 DECL_XSERIALIZABLE(XMLDateTime)
151
152 private:
153
154 // -----------------------------------------------------------------------
155 // Constant data
156 // -----------------------------------------------------------------------
157 //
158
159 enum timezoneIndex
160 {
161 hh = 0,
162 mm ,
163 TIMEZONE_ARRAYSIZE
164 };
165
166 // -----------------------------------------------------------------------
167 // Comparison
168 // -----------------------------------------------------------------------
169 static int compareResult(int
170 , int
171 , bool);
172
173 static void addDuration(XMLDateTime* pDuration
174 , const XMLDateTime* const pBaseDate
175 , int index);
176
177
178 static int compareResult(const XMLDateTime* const
179 , const XMLDateTime* const
180 , bool
181 , int);
182
183 static inline int getRetVal(int, int);
184
185 // -----------------------------------------------------------------------
186 // helper
187 // -----------------------------------------------------------------------
188
189 inline void reset();
190
191 inline void assertBuffer() const;
192
193 inline void copy(const XMLDateTime&);
194
195 // allow multiple parsing
196 inline bool initParser();
197
198 inline bool isNormalized() const;
199
200 // -----------------------------------------------------------------------
201 // scaners
202 // -----------------------------------------------------------------------
203
204 void getDate();
205
206 void getTime();
207
208 void getYearMonth();
209
210 void getTimeZone(const XMLSize_t);
211
212 void parseTimeZone();
213
214 // -----------------------------------------------------------------------
215 // locator and converter
216 // -----------------------------------------------------------------------
217
218 int findUTCSign(const XMLSize_t start);
219
220 int indexOf(const XMLSize_t start
221 , const XMLSize_t end
222 , const XMLCh ch) const;
223
224 int parseInt(const XMLSize_t start
225 , const XMLSize_t end) const;
226
227 int parseIntYear(const XMLSize_t end) const;
228
229 double parseMiliSecond(const XMLSize_t start
230 , const XMLSize_t end) const;
231
232 // -----------------------------------------------------------------------
233 // validator and normalizer
234 // -----------------------------------------------------------------------
235
236 void validateDateTime() const;
237
238 void normalize();
239
240 void fillString(XMLCh*& ptr, int value, XMLSize_t expLen) const;
241
242 int fillYearString(XMLCh*& ptr, int value) const;
243
244 void searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const;
245
246 // -----------------------------------------------------------------------
247 // Unimplemented operator ==
248 // -----------------------------------------------------------------------
249 bool operator==(const XMLDateTime& toCompare) const;
250
251
252 // -----------------------------------------------------------------------
253 // Private data members
254 //
255 // fValue[]
256 // object representation of date time.
257 //
258 // fTimeZone[]
259 // temporary storage for normalization
260 //
261 // fStart, fEnd
262 // pointers to the portion of fBuffer being parsed
263 //
264 // fBuffer
265 // raw data to be parsed, own it.
266 //
267 // -----------------------------------------------------------------------
268
269 int fValue[TOTAL_SIZE];
270 int fTimeZone[TIMEZONE_ARRAYSIZE];
271 XMLSize_t fStart;
272 XMLSize_t fEnd;
273 XMLSize_t fBufferMaxLen;
274
275 double fMilliSecond;
276 bool fHasTime;
277
278 XMLCh* fBuffer;
279 MemoryManager* fMemoryManager;
280
281 friend class XSValue;
282};
283
284 inline void XMLDateTime::setBuffer(const XMLCh* const aString)
285{
286 reset();
287
288 fEnd = XMLString::stringLen(aString);
289
290 for (; fEnd > 0; fEnd--)
291 {
292 if (!XMLChar1_0::isWhitespace(aString[fEnd - 1]))
293 break;
294 }
295
296 if (fEnd > 0) {
297
298 if (fEnd > fBufferMaxLen)
299 {
300 fMemoryManager->deallocate(fBuffer);
301 fBufferMaxLen = fEnd + 8;
302 fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
303 }
304
305 memcpy(fBuffer, aString, (fEnd) * sizeof(XMLCh));
306 fBuffer[fEnd] = '\0';
307 }
308}
309
310 inline void XMLDateTime::reset()
311{
312 for ( int i=0; i < TOTAL_SIZE; i++ )
313 fValue[i] = 0;
314
315 fMilliSecond = 0;
316 fHasTime = false;
317 fTimeZone[hh] = fTimeZone[mm] = 0;
318 fStart = fEnd = 0;
319
320 if (fBuffer)
321 *fBuffer = 0;
322}
323
324 inline void XMLDateTime::copy(const XMLDateTime& rhs)
325{
326 for ( int i = 0; i < TOTAL_SIZE; i++ )
327 fValue[i] = rhs.fValue[i];
328
329 fMilliSecond = rhs.fMilliSecond;
330 fHasTime = rhs.fHasTime;
331 fTimeZone[hh] = rhs.fTimeZone[hh];
332 fTimeZone[mm] = rhs.fTimeZone[mm];
333 fStart = rhs.fStart;
334 fEnd = rhs.fEnd;
335
336 if (fEnd > 0)
337 {
338 if (fEnd > fBufferMaxLen)
339 {
340 fMemoryManager->deallocate(fBuffer);//delete[] fBuffer;
341 fBufferMaxLen = rhs.fBufferMaxLen;
342 fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
343 }
344
345 memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh));
346 }
347}
348
349 inline bool XMLDateTime::initParser()
350{
351 if (!fBuffer || fBuffer[0] == chNull)
352 return false;
353
354 fStart = 0; // to ensure scan from the very first beginning
355 // in case the pointer is updated accidentally by
356 // someone else.
357 return true;
358}
359
360 inline bool XMLDateTime::isNormalized() const
361{
362 return ( fValue[utc] == UTC_STD ? true : false );
363}
364
365 inline int XMLDateTime::getRetVal(int c1, int c2)
366{
367 if ((c1 == LESS_THAN && c2 == GREATER_THAN) ||
368 (c1 == GREATER_THAN && c2 == LESS_THAN) )
369 {
370 return INDETERMINATE;
371 }
372
373 return ( c1 != INDETERMINATE ) ? c1 : c2;
374}
375
376 XERCES_CPP_NAMESPACE_END
377
378#endif
PlatformUtils.hpp
SchemaDateTimeException.hpp
XMLChar.hpp
XMLNumber.hpp
XMLString.hpp
XMLUniDefs.hpp
chNull
XERCES_CPP_NAMESPACE_BEGIN const XMLCh chNull
Definition XMLUniDefs.hpp:37
XERCES_CPP_NAMESPACE_BEGIN
#define XERCES_CPP_NAMESPACE_BEGIN
Definition XercesDefs.hpp:112
XMLUTIL_EXPORT
#define XMLUTIL_EXPORT
Definition XercesDefs.hpp:162
XERCES_CPP_NAMESPACE_END
#define XERCES_CPP_NAMESPACE_END
Definition XercesDefs.hpp:113
XMLSize_t
size_t XMLSize_t
Definition Xerces_autoconf_config.hpp:112
XMLCh
char16_t XMLCh
Definition Xerces_autoconf_config.hpp:120
MemoryManager
Configurable memory manager.
Definition MemoryManager.hpp:40
MemoryManager::allocate
virtual void * allocate(XMLSize_t size)=0
This method allocates requested memory.
MemoryManager::deallocate
virtual void deallocate(void *p)=0
This method deallocates memory.
XMLChar1_0::isWhitespace
static bool isWhitespace(const XMLCh toCheck)
Definition XMLChar.hpp:242
XMLDateTime
Definition XMLDateTime.hpp:39
XMLDateTime::getHour
int getHour() const
Definition XMLDateTime.hpp:142
XMLDateTime::parseDay
void parseDay()
XMLDateTime::parseYear
void parseYear()
XMLDateTime::parseDate
void parseDate()
XMLDateTime::parseMonth
void parseMonth()
XMLDateTime::XMLDateTime
XMLDateTime(MemoryManager *const manager=XMLPlatformUtils::fgMemoryManager)
XMLDateTime::getDateTimeCanonicalRepresentation
XMLCh * getDateTimeCanonicalRepresentation(MemoryManager *const memMgr) const
XMLDateTime::compare
static int compare(const XMLDateTime *const, const XMLDateTime *const, bool)
XMLDateTime::utcType
utcType
Definition XMLDateTime.hpp:56
XMLDateTime::UTC_POS
@ UTC_POS
Definition XMLDateTime.hpp:59
XMLDateTime::UTC_STD
@ UTC_STD
Definition XMLDateTime.hpp:58
XMLDateTime::getMinute
int getMinute() const
Definition XMLDateTime.hpp:143
XMLDateTime::operator=
XMLDateTime & operator=(const XMLDateTime &)
XMLDateTime::getDay
int getDay() const
Definition XMLDateTime.hpp:141
XMLDateTime::parseDateTime
void parseDateTime()
XMLDateTime::getMonth
int getMonth() const
Definition XMLDateTime.hpp:140
XMLDateTime::parseDuration
void parseDuration()
XMLDateTime::compare
static int compare(const XMLDateTime *const, const XMLDateTime *const)
XMLDateTime::getSign
virtual int getSign() const
Returns the sign of this number.
XMLDateTime::setBuffer
void setBuffer(const XMLCh *const)
Definition XMLDateTime.hpp:284
XMLDateTime::getRawData
virtual XMLCh * getRawData() const
Return string representation of the decimal value.
XMLDateTime::parseTime
void parseTime()
XMLDateTime::getDateCanonicalRepresentation
XMLCh * getDateCanonicalRepresentation(MemoryManager *const memMgr) const
XMLDateTime::getYear
int getYear() const
Definition XMLDateTime.hpp:139
XMLDateTime::getEpoch
time_t getEpoch(bool duration=false) const
XMLDateTime::XMLDateTime
XMLDateTime(const XMLDateTime &)
XMLDateTime::valueIndex
valueIndex
Definition XMLDateTime.hpp:43
XMLDateTime::MiliSecond
@ MiliSecond
Definition XMLDateTime.hpp:50
XMLDateTime::Hour
@ Hour
Definition XMLDateTime.hpp:47
XMLDateTime::Day
@ Day
Definition XMLDateTime.hpp:46
XMLDateTime::Minute
@ Minute
Definition XMLDateTime.hpp:48
XMLDateTime::utc
@ utc
Definition XMLDateTime.hpp:51
XMLDateTime::Second
@ Second
Definition XMLDateTime.hpp:49
XMLDateTime::TOTAL_SIZE
@ TOTAL_SIZE
Definition XMLDateTime.hpp:52
XMLDateTime::Month
@ Month
Definition XMLDateTime.hpp:45
XMLDateTime::getSecond
int getSecond() const
Definition XMLDateTime.hpp:144
XMLDateTime::getTimeCanonicalRepresentation
XMLCh * getTimeCanonicalRepresentation(MemoryManager *const memMgr) const
XMLDateTime::~XMLDateTime
~XMLDateTime()
XMLDateTime::compareOrder
static int compareOrder(const XMLDateTime *const, const XMLDateTime *const)
XMLDateTime::parseYearMonth
void parseYearMonth()
XMLDateTime::parseMonthDay
void parseMonthDay()
XMLDateTime::XMLDateTime
XMLDateTime(const XMLCh *const, MemoryManager *const manager=XMLPlatformUtils::fgMemoryManager)
XMLDateTime::XMLDateTime
XMLDateTime(time_t epoch, bool duration, MemoryManager *const manager=XMLPlatformUtils::fgMemoryManager)
XMLDateTime::getFormattedString
virtual const XMLCh * getFormattedString() const
Return the original and converted value of the original data.
XMLNumber
Definition XMLNumber.hpp:31
XMLNumber::GREATER_THAN
@ GREATER_THAN
Definition XMLNumber.hpp:38
XMLNumber::INDETERMINATE
@ INDETERMINATE
Definition XMLNumber.hpp:39
XMLNumber::LESS_THAN
@ LESS_THAN
Definition XMLNumber.hpp:36
XMLPlatformUtils::fgMemoryManager
static MemoryManager * fgMemoryManager
The configurable memory manager.
Definition PlatformUtils.hpp:121
XMLString::stringLen
static XMLSize_t stringLen(const char *const src)
Get the length of the string.
XSValue
Definition XSValue.hpp:33