KickJava   Java API By Example, From Geeks To Geeks.

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


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 DateSerializer deserializes a Date. Much of the work is done in the
70  * 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 DateDeserializer extends SimpleDeserializer
76 {
77
78    private static SimpleDateFormat JavaDoc zulu =
79            new SimpleDateFormat JavaDoc("yyyy-MM-dd");
80
81    private static SimpleDateFormat JavaDoc zuluUTC =
82            new SimpleDateFormat JavaDoc("yyyy-MM-dd");
83    // 0123456789 0 123456789
84

85    static
86    {
87       zuluUTC.setTimeZone(TimeZone.getTimeZone("GMT"));
88    }
89
90    /**
91     * The Deserializer is constructed with the xmlType and
92     * javaType
93     */

94    public DateDeserializer(Class JavaDoc javaType, QName JavaDoc xmlType)
95    {
96       super(javaType, xmlType);
97    }
98
99    // Adjusts the passed in date by an xsd:date zone string and returns the zone offset
100
private int adjustByZone(Date JavaDoc date, String JavaDoc zone)
101    {
102       int sign, hours, minutes;
103
104       if (zone.charAt(0) == 'Z')
105       {
106          return 0;
107       }
108       else if (zone.charAt(0) == '-')
109       {
110          // The sign is the inverse since we are moving from UTC to the timezone
111
sign = 1;
112       }
113       else if (zone.charAt(0) == '+')
114       {
115          sign = -1;
116       }
117       else
118       {
119          throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
120       }
121
122       if (zone.charAt(3) != ':')
123       {
124          throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
125       }
126
127       try
128       {
129          hours = Integer.parseInt(zone.substring(1, 3));
130          minutes = Integer.parseInt(zone.substring(4, 6));
131       }
132       catch (Exception JavaDoc e)
133       {
134          throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
135       }
136
137       // 3.2.7.3 - magnitude
138
if (hours > 14 || (hours == 14 && minutes > 0) || minutes > 59)
139       {
140          throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
141       }
142
143       int offset = sign * (hours * 60 + minutes) * 60 * 1000;
144
145       date.setTime(date.getTime() + offset);
146
147       // We resture the zone since we are now moving from the timezone to UTC
148
return -offset;
149    }
150
151    /**
152     * The simple deserializer provides most of the stuff.
153     * We just need to override makeValue().
154     */

155    public Object JavaDoc makeValue(String JavaDoc source)
156    {
157       Calendar JavaDoc calendar;
158       Date JavaDoc result;
159       boolean bc = false;
160       String JavaDoc zonePortion = null;
161
162       if (source == null)
163       {
164          throw new NumberFormatException JavaDoc(Messages.getMessage("badDate00"));
165       }
166
167       if (source.charAt(0) == '+')
168          source = source.substring(1);
169
170       if (source.charAt(0) == '-')
171       {
172          source = source.substring(1);
173          bc = true;
174       }
175
176       if (source.length() < 10)
177          throw new NumberFormatException JavaDoc(Messages.getMessage("badDate00"));
178
179       if (source.charAt(4) != '-' || source.charAt(7) != '-')
180          throw new NumberFormatException JavaDoc(Messages.getMessage("badDate00"));
181
182       try
183       {
184          if (source.length() > 10)
185          {
186             zonePortion = source.substring(10);
187
188             synchronized (zuluUTC)
189             {
190                result = zuluUTC.parse(source.substring(0, 10));
191             }
192             int offset = adjustByZone(result, zonePortion);
193             TimeZone JavaDoc timeZone = (offset == 0) ? TimeZone.getTimeZone("GMT")
194                     : new SimpleTimeZone JavaDoc(offset, "GMT offset");
195             calendar = Calendar.getInstance(timeZone);
196          }
197          else
198          {
199             synchronized (zulu)
200             {
201                result = zulu.parse(source);
202             }
203             calendar = Calendar.getInstance();
204          }
205       }
206       catch (NumberFormatException JavaDoc e)
207       {
208          throw e;
209       }
210       catch (Exception JavaDoc e)
211       {
212          throw new NumberFormatException JavaDoc(e.toString());
213       }
214
215       calendar.setTime(result);
216
217       // support dates before the Christian era
218
if (bc)
219       {
220          calendar.set(Calendar.ERA, GregorianCalendar.BC);
221          result = calendar.getTime();
222       }
223
224       if (super.javaType == Calendar JavaDoc.class)
225       {
226          return calendar;
227       }
228
229       return result;
230    }
231 }
232
Popular Tags