KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > util > XsDateTimeFormat


1 /*
2  * Copyright 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 package org.apache.ws.jaxme.xs.util;
18
19 import java.text.FieldPosition JavaDoc;
20 import java.text.Format JavaDoc;
21 import java.text.ParsePosition JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.TimeZone JavaDoc;
24
25
26 /** <p>An instance of {@link java.text.Format}, which may be used
27  * to parse and format <code>xs:dateTime</code> values.</p>
28  *
29  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
30  */

31 public class XsDateTimeFormat extends Format JavaDoc {
32     final boolean parseDate;
33     final boolean parseTime;
34
35     XsDateTimeFormat(boolean pParseDate, boolean pParseTime) {
36         parseDate = pParseDate;
37         parseTime = pParseTime;
38     }
39
40     /** Creates a new instance.
41      */

42     public XsDateTimeFormat() {
43         this(true, true);
44     }
45
46     private int parseInt(String JavaDoc pString, int pOffset, StringBuffer JavaDoc pDigits) {
47         int length = pString.length();
48         pDigits.setLength(0);
49         while (pOffset < length) {
50             char c = pString.charAt(pOffset);
51             if (Character.isDigit(c)) {
52                 pDigits.append(c);
53                 ++pOffset;
54             } else {
55                 break;
56             }
57         }
58         return pOffset;
59     }
60
61     public Object JavaDoc parseObject(String JavaDoc pString, ParsePosition JavaDoc pParsePosition) {
62         if (pString == null) {
63             throw new NullPointerException JavaDoc("The String argument must not be null.");
64         }
65         if (pParsePosition == null) {
66             throw new NullPointerException JavaDoc("The ParsePosition argument must not be null.");
67         }
68         int offset = pParsePosition.getIndex();
69         int length = pString.length();
70
71         boolean isMinus = false;
72         StringBuffer JavaDoc digits = new StringBuffer JavaDoc();
73         int year, month, mday;
74         if (parseDate) {
75             // Sign
76
if (offset < length) {
77                 char c = pString.charAt(offset);
78                 if (c == '+') {
79                     ++offset;
80                 } else if (c == '-') {
81                     ++offset;
82                     isMinus = true;
83                 }
84             }
85
86             offset = parseInt(pString, offset, digits);
87             if (digits.length() < 4) {
88                 pParsePosition.setErrorIndex(offset);
89                 return null;
90             }
91             year = Integer.parseInt(digits.toString());
92     
93             if (offset < length && pString.charAt(offset) == '-') {
94                 ++offset;
95             } else {
96                 pParsePosition.setErrorIndex(offset);
97                 return null;
98             }
99     
100             offset = parseInt(pString, offset, digits);
101             if (digits.length() != 2) {
102                 pParsePosition.setErrorIndex(offset);
103                 return null;
104             }
105             month = Integer.parseInt(digits.toString());
106     
107             if (offset < length && pString.charAt(offset) == '-') {
108                 ++offset;
109             } else {
110                 pParsePosition.setErrorIndex(offset);
111                 return null;
112             }
113     
114             offset = parseInt(pString, offset, digits);
115             if (digits.length() != 2) {
116                 pParsePosition.setErrorIndex(offset);
117                 return null;
118             }
119             mday = Integer.parseInt(digits.toString());
120
121             if (parseTime) {
122                 if (offset < length && pString.charAt(offset) == 'T') {
123                     ++offset;
124                 } else {
125                     pParsePosition.setErrorIndex(offset);
126                     return null;
127                 }
128             }
129         } else {
130             year = month = mday = 0;
131         }
132
133         int hour, minute, second, millis;
134         if (parseTime) {
135             offset = parseInt(pString, offset, digits);
136             if (digits.length() != 2) {
137                 pParsePosition.setErrorIndex(offset);
138                 return null;
139             }
140             hour = Integer.parseInt(digits.toString());
141     
142             if (offset < length && pString.charAt(offset) == ':') {
143                 ++offset;
144             } else {
145                 pParsePosition.setErrorIndex(offset);
146                 return null;
147             }
148     
149             offset = parseInt(pString, offset, digits);
150             if (digits.length() != 2) {
151                 pParsePosition.setErrorIndex(offset);
152                 return null;
153             }
154             minute = Integer.parseInt(digits.toString());
155     
156             if (offset < length && pString.charAt(offset) == ':') {
157                 ++offset;
158             } else {
159                 pParsePosition.setErrorIndex(offset);
160                 return null;
161             }
162     
163             offset = parseInt(pString, offset, digits);
164             if (digits.length() != 2) {
165                 pParsePosition.setErrorIndex(offset);
166                 return null;
167             }
168             second = Integer.parseInt(digits.toString());
169     
170             if (offset < length && pString.charAt(offset) == '.') {
171                 ++offset;
172                 offset = parseInt(pString, offset, digits);
173                 if (digits.length() > 0) {
174                     millis = Integer.parseInt(digits.toString());
175                 } else {
176                     millis = 0;
177                 }
178             } else {
179                 millis = 0;
180             }
181         } else {
182             hour = minute = second = millis = 0;
183         }
184
185         digits.setLength(0);
186         digits.append("GMT");
187         if (offset < length) {
188             char c = pString.charAt(offset);
189             if (c == 'Z') {
190                 // Ignore UTC, it is the default
191
++offset;
192             } else if (c == '+' || c == '-') {
193                 digits.append(c);
194                 ++offset;
195                 for (int i = 0; i < 5; i++) {
196                     if (offset >= length) {
197                         pParsePosition.setErrorIndex(offset);
198                         return null;
199                     }
200                     c = pString.charAt(offset);
201                     if ((i != 2 && Character.isDigit(c)) ||
202                         (i == 2 && c == ':')) {
203                         digits.append(c);
204                     } else {
205                         pParsePosition.setErrorIndex(offset);
206                         return null;
207                     }
208                     ++offset;
209                 }
210             }
211         }
212
213         Calendar JavaDoc cal = Calendar.getInstance(TimeZone.getTimeZone(digits.toString()));
214         cal.set(isMinus ? -year : year, parseDate ? month-1 : month, mday, hour, minute, second);
215         cal.set(Calendar.MILLISECOND, millis);
216         pParsePosition.setIndex(offset);
217         return cal;
218     }
219
220     private void append(StringBuffer JavaDoc pBuffer, int pNum, int pMinLen) {
221         String JavaDoc s = Integer.toString(pNum);
222         for (int i = s.length(); i < pMinLen; i++) {
223             pBuffer.append('0');
224         }
225         pBuffer.append(s);
226     }
227
228     public StringBuffer JavaDoc format(Object JavaDoc pCalendar, StringBuffer JavaDoc pBuffer, FieldPosition JavaDoc pPos) {
229         if (pCalendar == null) {
230             throw new NullPointerException JavaDoc("The Calendar argument must not be null.");
231         }
232         if (pBuffer == null) {
233             throw new NullPointerException JavaDoc("The StringBuffer argument must not be null.");
234         }
235         if (pPos == null) {
236             throw new NullPointerException JavaDoc("The FieldPosition argument must not be null.");
237         }
238
239         Calendar JavaDoc cal = (Calendar JavaDoc) pCalendar;
240         if (parseDate) {
241             int year = cal.get(Calendar.YEAR);
242             if (year < 0) {
243                 pBuffer.append('-');
244                 year = -year;
245             }
246             append(pBuffer, year, 4);
247             pBuffer.append('-');
248             append(pBuffer, cal.get(Calendar.MONTH)+1, 2);
249             pBuffer.append('-');
250             append(pBuffer, cal.get(Calendar.DAY_OF_MONTH), 2);
251             if (parseTime) {
252                 pBuffer.append('T');
253             }
254         }
255         if (parseTime) {
256             append(pBuffer, cal.get(Calendar.HOUR_OF_DAY), 2);
257             pBuffer.append(':');
258             append(pBuffer, cal.get(Calendar.MINUTE), 2);
259             pBuffer.append(':');
260             append(pBuffer, cal.get(Calendar.SECOND), 2);
261             int millis = cal.get(Calendar.MILLISECOND);
262             if (millis > 0) {
263                 pBuffer.append('.');
264                 append(pBuffer, millis, 3);
265             }
266         }
267         TimeZone JavaDoc tz = cal.getTimeZone();
268         // JDK 1.4: int offset = tz.getOffset(cal.getTimeInMillis());
269
int offset = cal.get(Calendar.ZONE_OFFSET);
270         if (tz.inDaylightTime(cal.getTime())) {
271             offset += cal.get(Calendar.DST_OFFSET);
272         }
273         if (offset == 0) {
274             pBuffer.append('Z');
275         } else {
276             if (offset < 0) {
277                 pBuffer.append('-');
278                 offset = -offset;
279             } else {
280                 pBuffer.append('+');
281             }
282             int minutes = offset / (60*1000);
283             int hours = minutes / 60;
284             minutes -= hours * 60;
285             append(pBuffer, hours, 2);
286             pBuffer.append(':');
287             append(pBuffer, minutes, 2);
288         }
289         return pBuffer;
290     }
291 }
292
Popular Tags