KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > convert > DateConverter


1 /*
2  * Copyright 2005 Joe Walker
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 package org.directwebremoting.convert;
17
18 import java.sql.Time JavaDoc;
19 import java.sql.Timestamp JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22
23 import org.directwebremoting.dwrp.ProtocolConstants;
24 import org.directwebremoting.dwrp.SimpleOutboundVariable;
25 import org.directwebremoting.extend.Converter;
26 import org.directwebremoting.extend.InboundContext;
27 import org.directwebremoting.extend.InboundVariable;
28 import org.directwebremoting.extend.MarshallException;
29 import org.directwebremoting.extend.OutboundContext;
30 import org.directwebremoting.extend.OutboundVariable;
31
32 /**
33  * An implementation of Converter for Dates.
34  * @author Joe Walker [joe at eireneh dot com]
35  * @version $Id: StringConverter.java,v 1.2 2004/11/04 15:54:07 joe_walker Exp $
36  */

37 public class DateConverter extends BaseV20Converter implements Converter
38 {
39     /* (non-Javadoc)
40      * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
41      */

42     public Object JavaDoc convertInbound(Class JavaDoc paramType, InboundVariable iv, InboundContext inctx) throws MarshallException
43     {
44         String JavaDoc value = iv.getValue();
45
46         // If the text is null then the whole bean is null
47
if (value.trim().equals(ProtocolConstants.INBOUND_NULL))
48         {
49             return null;
50         }
51
52         try
53         {
54             long millis = 0;
55             if (value.length() > 0)
56             {
57                 millis = Long.parseLong(value);
58             }
59
60             Date JavaDoc date = new Date JavaDoc(millis);
61             if (paramType == Date JavaDoc.class)
62             {
63                 return date;
64             }
65             else if (paramType == java.sql.Date JavaDoc.class)
66             {
67                 return new java.sql.Date JavaDoc(date.getTime());
68             }
69             else if (paramType == Time JavaDoc.class)
70             {
71                 return new Time JavaDoc(date.getTime());
72             }
73             else if (paramType == Timestamp JavaDoc.class)
74             {
75                 return new Timestamp JavaDoc(date.getTime());
76             }
77             else if (paramType == Calendar JavaDoc.class)
78             {
79                 Calendar JavaDoc cal = Calendar.getInstance();
80                 cal.setTime(date);
81                 return cal;
82             }
83             else
84             {
85                 throw new MarshallException(paramType);
86             }
87         }
88         catch (MarshallException ex)
89         {
90             throw ex;
91         }
92         catch (Exception JavaDoc ex)
93         {
94             throw new MarshallException(paramType, ex);
95         }
96     }
97
98     /* (non-Javadoc)
99      * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
100      */

101     public OutboundVariable convertOutbound(Object JavaDoc data, OutboundContext outctx) throws MarshallException
102     {
103         long millis;
104
105         if (data instanceof Calendar JavaDoc)
106         {
107             Calendar JavaDoc cal = (Calendar JavaDoc) data;
108             millis = cal.getTime().getTime();
109         }
110         else if (data instanceof Date JavaDoc)
111         {
112             Date JavaDoc date = (Date JavaDoc) data;
113             millis = date.getTime();
114         }
115         else
116         {
117             throw new MarshallException(data.getClass());
118         }
119
120         return new SimpleOutboundVariable("new Date(" + millis + ")", outctx, true);
121     }
122 }
123
Popular Tags