01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
19: import java.sql.Timestamp;
20: import java.util.Calendar;
21: import java.util.Date;
22:
23: import org.directwebremoting.dwrp.ProtocolConstants;
24: import org.directwebremoting.extend.Converter;
25: import org.directwebremoting.extend.InboundContext;
26: import org.directwebremoting.extend.InboundVariable;
27: import org.directwebremoting.extend.MarshallException;
28: import org.directwebremoting.extend.NonNestedOutboundVariable;
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 getahead dot ltd dot uk]
35: */
36: public class DateConverter extends BaseV20Converter implements
37: Converter {
38: /* (non-Javadoc)
39: * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
40: */
41: public Object convertInbound(Class<?> paramType,
42: InboundVariable data, InboundContext inctx)
43: throws MarshallException {
44: String value = data.getValue();
45:
46: // If the text is null then the whole bean is null
47: if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
48: return null;
49: }
50:
51: try {
52: long millis = 0;
53: if (value.length() > 0) {
54: millis = Long.parseLong(value);
55: }
56:
57: Date date = new Date(millis);
58: if (paramType == Date.class) {
59: return date;
60: } else if (paramType == java.sql.Date.class) {
61: return new java.sql.Date(date.getTime());
62: } else if (paramType == Time.class) {
63: return new Time(date.getTime());
64: } else if (paramType == Timestamp.class) {
65: return new Timestamp(date.getTime());
66: } else if (paramType == Calendar.class) {
67: Calendar cal = Calendar.getInstance();
68: cal.setTime(date);
69: return cal;
70: } else {
71: throw new MarshallException(paramType);
72: }
73: } catch (MarshallException ex) {
74: throw ex;
75: } catch (Exception ex) {
76: throw new MarshallException(paramType, ex);
77: }
78: }
79:
80: /* (non-Javadoc)
81: * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
82: */
83: public OutboundVariable convertOutbound(Object data,
84: OutboundContext outctx) throws MarshallException {
85: long millis;
86:
87: if (data instanceof Calendar) {
88: Calendar cal = (Calendar) data;
89: millis = cal.getTime().getTime();
90: } else if (data instanceof Date) {
91: Date date = (Date) data;
92: millis = date.getTime();
93: } else {
94: throw new MarshallException(data.getClass());
95: }
96:
97: return new NonNestedOutboundVariable("new Date(" + millis + ")");
98: }
99: }
|