1 | /*
|
---|
2 | * Copyright 2001,2004 The Apache Software Foundation.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * $Id: XMLDateTime.hpp,v 1.20 2004/09/08 13:56:24 peiyongz Exp $
|
---|
19 | * $Log: XMLDateTime.hpp,v $
|
---|
20 | * Revision 1.20 2004/09/08 13:56:24 peiyongz
|
---|
21 | * Apache License Version 2.0
|
---|
22 | *
|
---|
23 | * Revision 1.19 2004/08/31 20:50:50 peiyongz
|
---|
24 | * Parse/keep milisecond as double to retain precision.
|
---|
25 | *
|
---|
26 | * Revision 1.18 2004/08/11 16:48:55 peiyongz
|
---|
27 | * Allow XSValue access
|
---|
28 | *
|
---|
29 | * Revision 1.17 2004/01/29 11:48:47 cargilld
|
---|
30 | * Code cleanup changes to get rid of various compiler diagnostic messages.
|
---|
31 | *
|
---|
32 | * Revision 1.16 2004/01/13 19:50:56 peiyongz
|
---|
33 | * remove parseContent()
|
---|
34 | *
|
---|
35 | * Revision 1.14 2003/12/31 02:34:11 neilg
|
---|
36 | * enable production of canonical representations for dates with negative years, or years >9999
|
---|
37 | *
|
---|
38 | * Revision 1.13 2003/12/17 20:00:49 cargilld
|
---|
39 | * Update for memory management so that the static memory manager (one
|
---|
40 | * used to call Initialize) is only for static data.
|
---|
41 | *
|
---|
42 | * Revision 1.12 2003/12/17 00:18:35 cargilld
|
---|
43 | * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
|
---|
44 | *
|
---|
45 | * Revision 1.11 2003/12/11 21:38:12 peiyongz
|
---|
46 | * support for Canonical Representation for Datatype
|
---|
47 | *
|
---|
48 | * Revision 1.10 2003/09/23 18:16:07 peiyongz
|
---|
49 | * Inplementation for Serialization/Deserialization
|
---|
50 | *
|
---|
51 | * Revision 1.9 2003/08/14 02:57:27 knoaman
|
---|
52 | * Code refactoring to improve performance of validation.
|
---|
53 | *
|
---|
54 | * Revision 1.8 2003/05/18 14:02:05 knoaman
|
---|
55 | * Memory manager implementation: pass per instance manager.
|
---|
56 | *
|
---|
57 | * Revision 1.7 2003/05/15 19:07:46 knoaman
|
---|
58 | * Partial implementation of the configurable memory manager.
|
---|
59 | *
|
---|
60 | * Revision 1.6 2003/05/09 15:13:46 peiyongz
|
---|
61 | * Deprecated toString() in XMLNumber family
|
---|
62 | *
|
---|
63 | * Revision 1.5 2003/03/10 20:55:58 peiyongz
|
---|
64 | * Schema Errata E2-40 double/float
|
---|
65 | *
|
---|
66 | * Revision 1.4 2003/02/02 23:54:43 peiyongz
|
---|
67 | * getFormattedString() added to return original and converted value.
|
---|
68 | *
|
---|
69 | * Revision 1.3 2003/01/30 21:55:22 tng
|
---|
70 | * Performance: create getRawData which is similar to toString but return the internal data directly, user is not required to delete the returned memory.
|
---|
71 | *
|
---|
72 | * Revision 1.2 2002/11/04 15:22:05 tng
|
---|
73 | * C++ Namespace Support.
|
---|
74 | *
|
---|
75 | * Revision 1.1.1.1 2002/02/01 22:22:14 peiyongz
|
---|
76 | * sane_include
|
---|
77 | *
|
---|
78 | * Revision 1.4 2001/11/22 20:23:00 peiyongz
|
---|
79 | * _declspec(dllimport) and inline warning C4273
|
---|
80 | *
|
---|
81 | * Revision 1.3 2001/11/12 20:36:54 peiyongz
|
---|
82 | * SchemaDateTimeException defined
|
---|
83 | *
|
---|
84 | * Revision 1.2 2001/11/09 20:41:45 peiyongz
|
---|
85 | * Fix: compilation error on Solaris and AIX.
|
---|
86 | *
|
---|
87 | * Revision 1.1 2001/11/07 19:16:03 peiyongz
|
---|
88 | * DateTime Port
|
---|
89 | *
|
---|
90 | */
|
---|
91 |
|
---|
92 | #ifndef XML_DATETIME_HPP
|
---|
93 | #define XML_DATETIME_HPP
|
---|
94 |
|
---|
95 | #include <xercesc/util/XMLNumber.hpp>
|
---|
96 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
97 | #include <xercesc/util/XMLString.hpp>
|
---|
98 | #include <xercesc/util/XMLUniDefs.hpp>
|
---|
99 | #include <xercesc/util/SchemaDateTimeException.hpp>
|
---|
100 |
|
---|
101 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
102 |
|
---|
103 | class XSValue;
|
---|
104 |
|
---|
105 | class XMLUTIL_EXPORT XMLDateTime : public XMLNumber
|
---|
106 | {
|
---|
107 | public:
|
---|
108 |
|
---|
109 | enum valueIndex
|
---|
110 | {
|
---|
111 | CentYear = 0,
|
---|
112 | Month ,
|
---|
113 | Day ,
|
---|
114 | Hour ,
|
---|
115 | Minute ,
|
---|
116 | Second ,
|
---|
117 | MiliSecond , //not to be used directly
|
---|
118 | utc ,
|
---|
119 | TOTAL_SIZE
|
---|
120 | };
|
---|
121 |
|
---|
122 | enum utcType
|
---|
123 | {
|
---|
124 | UTC_UNKNOWN = 0,
|
---|
125 | UTC_STD , // set in parse() or normalize()
|
---|
126 | UTC_POS , // set in parse()
|
---|
127 | UTC_NEG // set in parse()
|
---|
128 | };
|
---|
129 |
|
---|
130 | // -----------------------------------------------------------------------
|
---|
131 | // ctors and dtor
|
---|
132 | // -----------------------------------------------------------------------
|
---|
133 |
|
---|
134 | XMLDateTime(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
|
---|
135 | XMLDateTime(const XMLCh* const,
|
---|
136 | MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
|
---|
137 | ~XMLDateTime();
|
---|
138 |
|
---|
139 | inline void setBuffer(const XMLCh* const);
|
---|
140 |
|
---|
141 | // -----------------------------------------------------------------------
|
---|
142 | // Copy ctor and Assignment operators
|
---|
143 | // -----------------------------------------------------------------------
|
---|
144 |
|
---|
145 | XMLDateTime(const XMLDateTime&);
|
---|
146 |
|
---|
147 | XMLDateTime& operator=(const XMLDateTime&);
|
---|
148 |
|
---|
149 | // -----------------------------------------------------------------------
|
---|
150 | // Implementation of Abstract Interface
|
---|
151 | // -----------------------------------------------------------------------
|
---|
152 |
|
---|
153 | /**
|
---|
154 | *
|
---|
155 | * Deprecated: please use getRawData
|
---|
156 | *
|
---|
157 | */
|
---|
158 | virtual XMLCh* toString() const;
|
---|
159 |
|
---|
160 | virtual XMLCh* getRawData() const;
|
---|
161 |
|
---|
162 | virtual const XMLCh* getFormattedString() const;
|
---|
163 |
|
---|
164 | virtual int getSign() const;
|
---|
165 |
|
---|
166 | // -----------------------------------------------------------------------
|
---|
167 | // Canonical Representation
|
---|
168 | // -----------------------------------------------------------------------
|
---|
169 |
|
---|
170 | XMLCh* getDateTimeCanonicalRepresentation(MemoryManager* const memMgr) const;
|
---|
171 |
|
---|
172 | XMLCh* getTimeCanonicalRepresentation(MemoryManager* const memMgr) const;
|
---|
173 |
|
---|
174 | // -----------------------------------------------------------------------
|
---|
175 | // parsers
|
---|
176 | // -----------------------------------------------------------------------
|
---|
177 |
|
---|
178 | void parseDateTime(); //DateTime
|
---|
179 |
|
---|
180 | void parseDate(); //Date
|
---|
181 |
|
---|
182 | void parseTime(); //Time
|
---|
183 |
|
---|
184 | void parseDay(); //gDay
|
---|
185 |
|
---|
186 | void parseMonth(); //gMonth
|
---|
187 |
|
---|
188 | void parseYear(); //gYear
|
---|
189 |
|
---|
190 | void parseMonthDay(); //gMonthDay
|
---|
191 |
|
---|
192 | void parseYearMonth(); //gYearMonth
|
---|
193 |
|
---|
194 | void parseDuration(); //duration
|
---|
195 |
|
---|
196 | // -----------------------------------------------------------------------
|
---|
197 | // Comparison
|
---|
198 | // -----------------------------------------------------------------------
|
---|
199 | static int compare(const XMLDateTime* const
|
---|
200 | , const XMLDateTime* const);
|
---|
201 |
|
---|
202 | static int compare(const XMLDateTime* const
|
---|
203 | , const XMLDateTime* const
|
---|
204 | , bool );
|
---|
205 |
|
---|
206 | static int compareOrder(const XMLDateTime* const
|
---|
207 | , const XMLDateTime* const);
|
---|
208 |
|
---|
209 | /***
|
---|
210 | * Support for Serialization/De-serialization
|
---|
211 | ***/
|
---|
212 | DECL_XSERIALIZABLE(XMLDateTime)
|
---|
213 |
|
---|
214 | private:
|
---|
215 |
|
---|
216 | // -----------------------------------------------------------------------
|
---|
217 | // Constant data
|
---|
218 | // -----------------------------------------------------------------------
|
---|
219 | //
|
---|
220 |
|
---|
221 | enum timezoneIndex
|
---|
222 | {
|
---|
223 | hh = 0,
|
---|
224 | mm ,
|
---|
225 | TIMEZONE_ARRAYSIZE
|
---|
226 | };
|
---|
227 |
|
---|
228 | // -----------------------------------------------------------------------
|
---|
229 | // Comparison
|
---|
230 | // -----------------------------------------------------------------------
|
---|
231 | static int compareResult(int
|
---|
232 | , int
|
---|
233 | , bool);
|
---|
234 |
|
---|
235 | static void addDuration(XMLDateTime* pDuration
|
---|
236 | , const XMLDateTime* const pBaseDate
|
---|
237 | , int index);
|
---|
238 |
|
---|
239 |
|
---|
240 | static int compareResult(const XMLDateTime* const
|
---|
241 | , const XMLDateTime* const
|
---|
242 | , bool
|
---|
243 | , int);
|
---|
244 |
|
---|
245 | static inline int getRetVal(int, int);
|
---|
246 |
|
---|
247 | // -----------------------------------------------------------------------
|
---|
248 | // helper
|
---|
249 | // -----------------------------------------------------------------------
|
---|
250 |
|
---|
251 | inline void reset();
|
---|
252 |
|
---|
253 | inline void assertBuffer() const;
|
---|
254 |
|
---|
255 | inline void copy(const XMLDateTime&);
|
---|
256 |
|
---|
257 | // allow multiple parsing
|
---|
258 | inline void initParser();
|
---|
259 |
|
---|
260 | inline bool isNormalized() const;
|
---|
261 |
|
---|
262 | // -----------------------------------------------------------------------
|
---|
263 | // scaners
|
---|
264 | // -----------------------------------------------------------------------
|
---|
265 |
|
---|
266 | void getDate();
|
---|
267 |
|
---|
268 | void getTime();
|
---|
269 |
|
---|
270 | void getYearMonth();
|
---|
271 |
|
---|
272 | void getTimeZone(const int);
|
---|
273 |
|
---|
274 | void parseTimeZone();
|
---|
275 |
|
---|
276 | // -----------------------------------------------------------------------
|
---|
277 | // locator and converter
|
---|
278 | // -----------------------------------------------------------------------
|
---|
279 |
|
---|
280 | int findUTCSign(const int start);
|
---|
281 |
|
---|
282 | int indexOf(const int start
|
---|
283 | , const int end
|
---|
284 | , const XMLCh ch) const;
|
---|
285 |
|
---|
286 | int parseInt(const int start
|
---|
287 | , const int end) const;
|
---|
288 |
|
---|
289 | int parseIntYear(const int end) const;
|
---|
290 |
|
---|
291 | double parseMiliSecond(const int start
|
---|
292 | , const int end) const;
|
---|
293 |
|
---|
294 | // -----------------------------------------------------------------------
|
---|
295 | // validator and normalizer
|
---|
296 | // -----------------------------------------------------------------------
|
---|
297 |
|
---|
298 | void validateDateTime() const;
|
---|
299 |
|
---|
300 | void normalize();
|
---|
301 |
|
---|
302 | void fillString(XMLCh*& ptr, valueIndex ind, int expLen) const;
|
---|
303 |
|
---|
304 | int fillYearString(XMLCh*& ptr, valueIndex ind) const;
|
---|
305 |
|
---|
306 | void searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const;
|
---|
307 |
|
---|
308 | // -----------------------------------------------------------------------
|
---|
309 | // Unimplemented operator ==
|
---|
310 | // -----------------------------------------------------------------------
|
---|
311 | bool operator==(const XMLDateTime& toCompare) const;
|
---|
312 |
|
---|
313 |
|
---|
314 | // -----------------------------------------------------------------------
|
---|
315 | // Private data members
|
---|
316 | //
|
---|
317 | // fValue[]
|
---|
318 | // object representation of date time.
|
---|
319 | //
|
---|
320 | // fTimeZone[]
|
---|
321 | // temporary storage for normalization
|
---|
322 | //
|
---|
323 | // fStart, fEnd
|
---|
324 | // pointers to the portion of fBuffer being parsed
|
---|
325 | //
|
---|
326 | // fBuffer
|
---|
327 | // raw data to be parsed, own it.
|
---|
328 | //
|
---|
329 | // -----------------------------------------------------------------------
|
---|
330 |
|
---|
331 | int fValue[TOTAL_SIZE];
|
---|
332 | int fTimeZone[TIMEZONE_ARRAYSIZE];
|
---|
333 | int fStart;
|
---|
334 | int fEnd;
|
---|
335 | int fBufferMaxLen;
|
---|
336 | XMLCh* fBuffer;
|
---|
337 |
|
---|
338 | double fMiliSecond;
|
---|
339 | bool fHasTime;
|
---|
340 |
|
---|
341 | MemoryManager* fMemoryManager;
|
---|
342 |
|
---|
343 | friend class XSValue;
|
---|
344 | };
|
---|
345 |
|
---|
346 | inline void XMLDateTime::setBuffer(const XMLCh* const aString)
|
---|
347 | {
|
---|
348 | reset();
|
---|
349 |
|
---|
350 | fEnd = XMLString::stringLen(aString);
|
---|
351 | if (fEnd > 0) {
|
---|
352 |
|
---|
353 | if (fEnd > fBufferMaxLen)
|
---|
354 | {
|
---|
355 | fMemoryManager->deallocate(fBuffer);
|
---|
356 | fBufferMaxLen = fEnd + 8;
|
---|
357 | fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
|
---|
358 | }
|
---|
359 |
|
---|
360 | memcpy(fBuffer, aString, (fEnd+1) * sizeof(XMLCh));
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | inline void XMLDateTime::reset()
|
---|
365 | {
|
---|
366 | for ( int i=0; i < TOTAL_SIZE; i++ )
|
---|
367 | fValue[i] = 0;
|
---|
368 |
|
---|
369 | fMiliSecond = 0;
|
---|
370 | fHasTime = false;
|
---|
371 | fTimeZone[hh] = fTimeZone[mm] = 0;
|
---|
372 | fStart = fEnd = 0;
|
---|
373 |
|
---|
374 | if (fBuffer)
|
---|
375 | *fBuffer = 0;
|
---|
376 | }
|
---|
377 |
|
---|
378 | inline void XMLDateTime::copy(const XMLDateTime& rhs)
|
---|
379 | {
|
---|
380 | for ( int i = 0; i < TOTAL_SIZE; i++ )
|
---|
381 | fValue[i] = rhs.fValue[i];
|
---|
382 |
|
---|
383 | fMiliSecond = rhs.fMiliSecond;
|
---|
384 | fHasTime = rhs.fHasTime;
|
---|
385 | fTimeZone[hh] = rhs.fTimeZone[hh];
|
---|
386 | fTimeZone[mm] = rhs.fTimeZone[mm];
|
---|
387 | fStart = rhs.fStart;
|
---|
388 | fEnd = rhs.fEnd;
|
---|
389 |
|
---|
390 | if (fEnd > 0)
|
---|
391 | {
|
---|
392 | if (fEnd > fBufferMaxLen)
|
---|
393 | {
|
---|
394 | fMemoryManager->deallocate(fBuffer);//delete[] fBuffer;
|
---|
395 | fBufferMaxLen = rhs.fBufferMaxLen;
|
---|
396 | fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
|
---|
397 | }
|
---|
398 |
|
---|
399 | memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh));
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | inline void XMLDateTime::assertBuffer() const
|
---|
404 | {
|
---|
405 | if ( ( !fBuffer ) ||
|
---|
406 | ( fBuffer[0] == chNull ) )
|
---|
407 | {
|
---|
408 | ThrowXMLwithMemMgr(SchemaDateTimeException
|
---|
409 | , XMLExcepts::DateTime_Assert_Buffer_Fail
|
---|
410 | , fMemoryManager);
|
---|
411 | }
|
---|
412 |
|
---|
413 | }
|
---|
414 |
|
---|
415 | inline void XMLDateTime::initParser()
|
---|
416 | {
|
---|
417 | assertBuffer();
|
---|
418 | fStart = 0; // to ensure scan from the very first beginning
|
---|
419 | // in case the pointer is updated accidentally by someone else.
|
---|
420 | }
|
---|
421 |
|
---|
422 | inline bool XMLDateTime::isNormalized() const
|
---|
423 | {
|
---|
424 | return ( fValue[utc] == UTC_STD ? true : false );
|
---|
425 | }
|
---|
426 |
|
---|
427 | inline int XMLDateTime::getRetVal(int c1, int c2)
|
---|
428 | {
|
---|
429 | if ((c1 == LESS_THAN && c2 == GREATER_THAN) ||
|
---|
430 | (c1 == GREATER_THAN && c2 == LESS_THAN) )
|
---|
431 | {
|
---|
432 | return INDETERMINATE;
|
---|
433 | }
|
---|
434 |
|
---|
435 | return ( c1 != INDETERMINATE ) ? c1 : c2;
|
---|
436 | }
|
---|
437 |
|
---|
438 | XERCES_CPP_NAMESPACE_END
|
---|
439 |
|
---|
440 | #endif
|
---|