KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.axis.encoding.ser;
18
19 import org.apache.axis.utils.Messages;
20
21 import javax.xml.namespace.QName JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.GregorianCalendar JavaDoc;
26 /**
27  * The DateSerializer deserializes a Date. Much of the work is done in the
28  * base class.
29  *
30  * @author Sam Ruby (rubys@us.ibm.com)
31  * Modified for JAX-RPC @author Rich Scheuerle (scheu@us.ibm.com)
32  */

33 public class DateDeserializer extends SimpleDeserializer {
34
35     private static SimpleDateFormat JavaDoc zulu =
36         new SimpleDateFormat JavaDoc("yyyy-MM-dd");
37                           // 0123456789 0 123456789
38

39     private static Calendar JavaDoc calendar = Calendar.getInstance();
40
41     /**
42      * The Deserializer is constructed with the xmlType and
43      * javaType
44      */

45     public DateDeserializer(Class JavaDoc javaType, QName JavaDoc xmlType) {
46         super(javaType, xmlType);
47     }
48
49     /**
50      * The simple deserializer provides most of the stuff.
51      * We just need to override makeValue().
52      */

53     public Object JavaDoc makeValue(String JavaDoc source) {
54         Object JavaDoc result;
55         boolean bc = false;
56         
57         // validate fixed portion of format
58
if ( source != null ) {
59             if (source.charAt(0) == '+')
60                 source = source.substring(1);
61             
62             if (source.charAt(0) == '-') {
63                 source = source.substring(1);
64                 bc = true;
65             }
66             
67             if (source.length() < 10)
68                 throw new NumberFormatException JavaDoc(
69                            Messages.getMessage("badDate00"));
70     
71             if (source.charAt(4) != '-' || source.charAt(7) != '-')
72                 throw new NumberFormatException JavaDoc(
73                                                 Messages.getMessage("badDate00"));
74             
75         }
76         
77         synchronized (calendar) {
78             // convert what we have validated so far
79
try {
80                 result = zulu.parse(source == null ? null :
81                                     (source.substring(0,10)) );
82             } catch (Exception JavaDoc e) {
83                 throw new NumberFormatException JavaDoc(e.toString());
84             }
85             
86             // support dates before the Christian era
87
if (bc) {
88                 calendar.setTime((Date JavaDoc)result);
89                 calendar.set(Calendar.ERA, GregorianCalendar.BC);
90                 result = calendar.getTime();
91             }
92             if (javaType == java.util.Date JavaDoc.class) {
93                 return result;
94             } else if (javaType == java.sql.Date JavaDoc.class) {
95                 result = new java.sql.Date JavaDoc(((Date JavaDoc)result).getTime());
96             } else {
97                 calendar.setTime((Date JavaDoc)result);
98                 result = calendar;
99             }
100         }
101         return result;
102     }
103 }
Popular Tags