KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > encoding > ser > CalendarDeserializer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.encoding.ser;
57
58 import org.jboss.axis.utils.Messages;
59
60 import javax.xml.namespace.QName JavaDoc;
61 import java.text.SimpleDateFormat JavaDoc;
62 import java.util.Calendar JavaDoc;
63 import java.util.Date JavaDoc;
64 import java.util.GregorianCalendar JavaDoc;
65 import java.util.SimpleTimeZone JavaDoc;
66 import java.util.TimeZone JavaDoc;
67
68 /**
69  * The CalendarSerializer deserializes a dateTime.
70  * Much of the work is done in the base class.
71  *
72  * @author Sam Ruby (rubys@us.ibm.com)
73  * Modified for JAX-RPC @author Rich Scheuerle (scheu@us.ibm.com)
74  */

75 public class CalendarDeserializer extends SimpleDeserializer
76 {
77
78    private static SimpleDateFormat JavaDoc zulu =
79            new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
80    // 0123456789 0 123456789
81

82    static
83    {
84       zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
85    }
86
87    /**
88     * The Deserializer is constructed with the xmlType and
89     * javaType
90     */

91    public CalendarDeserializer(Class JavaDoc javaType, QName JavaDoc xmlType)
92    {
93       super(javaType, xmlType);
94    }
95
96    /**
97     * The simple deserializer provides most of the stuff.
98     * We just need to override makeValue().
99     */

100    public Object JavaDoc makeValue(String JavaDoc source)
101    {
102       Calendar JavaDoc calendar = Calendar.getInstance();
103       Date JavaDoc date;
104       boolean bc = false;
105
106       // validate fixed portion of format
107
if (source != null)
108       {
109          if (source.charAt(0) == '+')
110             source = source.substring(1);
111
112          if (source.charAt(0) == '-')
113          {
114             source = source.substring(1);
115             bc = true;
116          }
117
118          if (source.length() < 19)
119             throw new NumberFormatException JavaDoc(Messages.getMessage("badDateTime00"));
120
121          if (source.charAt(4) != '-' || source.charAt(7) != '-' ||
122                  source.charAt(10) != 'T')
123             throw new NumberFormatException JavaDoc(Messages.getMessage("badDate00"));
124
125          if (source.charAt(13) != ':' || source.charAt(16) != ':')
126             throw new NumberFormatException JavaDoc(Messages.getMessage("badTime00"));
127       }
128
129       // convert what we have validated so far
130
try
131       {
132          synchronized (zulu)
133          {
134             date = zulu.parse(source == null ? null :
135                     (source.substring(0, 19) + ".000Z"));
136          }
137       }
138       catch (Exception JavaDoc e)
139       {
140          throw new NumberFormatException JavaDoc(e.toString());
141       }
142
143       int pos = 19;
144
145       // parse optional milliseconds
146
if (source != null)
147       {
148          if (pos < source.length() && source.charAt(pos) == '.')
149          {
150             int milliseconds = 0;
151             int start = ++pos;
152             while (pos < source.length() &&
153                     Character.isDigit(source.charAt(pos)))
154                pos++;
155
156             String JavaDoc decimal = source.substring(start, pos);
157             if (decimal.length() == 3)
158             {
159                milliseconds = Integer.parseInt(decimal);
160             }
161             else if (decimal.length() < 3)
162             {
163                milliseconds = Integer.parseInt((decimal + "000")
164                        .substring(0, 3));
165             }
166             else
167             {
168                milliseconds = Integer.parseInt(decimal.substring(0, 3));
169                if (decimal.charAt(3) >= '5') ++milliseconds;
170             }
171
172             // add milliseconds to the current date
173
date.setTime(date.getTime() + milliseconds);
174          }
175
176          // parse optional timezone
177
if (pos + 5 < source.length() &&
178                  (source.charAt(pos) == '+' || (source.charAt(pos) == '-')))
179          {
180             if (!Character.isDigit(source.charAt(pos + 1)) ||
181                     !Character.isDigit(source.charAt(pos + 2)) ||
182                     source.charAt(pos + 3) != ':' ||
183                     !Character.isDigit(source.charAt(pos + 4)) ||
184                     !Character.isDigit(source.charAt(pos + 5)))
185                throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
186
187             int hours = (source.charAt(pos + 1) - '0') * 10
188                     + source.charAt(pos + 2) - '0';
189             int mins = (source.charAt(pos + 4) - '0') * 10
190                     + source.charAt(pos + 5) - '0';
191             int milliseconds = (hours * 60 + mins) * 60 * 1000;
192
193             // subtract milliseconds from current date to obtain GMT
194
if (source.charAt(pos) == '+') milliseconds = -milliseconds;
195             date.setTime(date.getTime() + milliseconds);
196             calendar.setTimeZone(new SimpleTimeZone JavaDoc(-milliseconds, "GMT Offset"));
197             pos += 6;
198          }
199
200          if (pos < source.length() && source.charAt(pos) == 'Z')
201          {
202             pos++;
203             calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
204          }
205
206          if (pos < source.length())
207             throw new NumberFormatException JavaDoc(Messages.getMessage("badChars00"));
208       }
209
210       calendar.setTime(date);
211
212       // support dates before the Christian era
213
if (bc)
214       {
215          calendar.set(Calendar.ERA, GregorianCalendar.BC);
216       }
217
218       if (super.javaType == Date JavaDoc.class)
219       {
220          return date;
221       }
222       else
223       {
224          return calendar;
225       }
226    }
227 }
228
Popular Tags