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.math.BigDecimal;
19: import java.math.BigInteger;
20:
21: import org.directwebremoting.extend.Converter;
22: import org.directwebremoting.extend.InboundContext;
23: import org.directwebremoting.extend.InboundVariable;
24: import org.directwebremoting.extend.MarshallException;
25: import org.directwebremoting.extend.NonNestedOutboundVariable;
26: import org.directwebremoting.extend.OutboundContext;
27: import org.directwebremoting.extend.OutboundVariable;
28: import org.directwebremoting.util.Messages;
29:
30: /**
31: * Converter for all primitive types
32: * @author Joe Walker [joe at getahead dot ltd dot uk]
33: */
34: public class BigNumberConverter extends BaseV20Converter implements
35: Converter {
36: /* (non-Javadoc)
37: * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
38: */
39: public Object convertInbound(Class<?> paramType,
40: InboundVariable data, InboundContext inctx)
41: throws MarshallException {
42: String value = data.getValue();
43:
44: if (value == null || value.length() == 0) {
45: return null;
46: }
47:
48: try {
49: if (paramType == BigDecimal.class) {
50: return new BigDecimal(value.trim());
51: }
52:
53: if (paramType == BigInteger.class) {
54: return new BigInteger(value.trim());
55: }
56:
57: throw new MarshallException(paramType);
58: } catch (NumberFormatException ex) {
59: throw new MarshallException(paramType, Messages.getString(
60: "BigNumberConverter.FormatError", value));
61: }
62: }
63:
64: /* (non-Javadoc)
65: * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
66: */
67: public OutboundVariable convertOutbound(Object data,
68: OutboundContext outctx) {
69: if (data == null) {
70: return new NonNestedOutboundVariable("null");
71: }
72:
73: return new NonNestedOutboundVariable(data.toString());
74: }
75: }
|