KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigDecimal JavaDoc;
19 import java.math.BigInteger JavaDoc;
20
21 import org.directwebremoting.dwrp.SimpleOutboundVariable;
22 import org.directwebremoting.extend.Converter;
23 import org.directwebremoting.extend.InboundContext;
24 import org.directwebremoting.extend.InboundVariable;
25 import org.directwebremoting.extend.MarshallException;
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 Converter
35 {
36     /* (non-Javadoc)
37      * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
38      */

39     public Object JavaDoc convertInbound(Class JavaDoc paramType, InboundVariable iv, InboundContext inctx) throws MarshallException
40     {
41         String JavaDoc value = iv.getValue();
42
43         if (value == null || value.length() == 0)
44         {
45             return null;
46         }
47
48         try
49         {
50             if (paramType == BigDecimal JavaDoc.class)
51             {
52                 return new BigDecimal JavaDoc(value.trim());
53             }
54
55             if (paramType == BigInteger JavaDoc.class)
56             {
57                 return new BigInteger JavaDoc(value.trim());
58             }
59
60             throw new MarshallException(paramType);
61         }
62         catch (NumberFormatException JavaDoc ex)
63         {
64             throw new MarshallException(paramType, Messages.getString("BigNumberConverter.FormatError", value));
65         }
66     }
67
68     /* (non-Javadoc)
69      * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
70      */

71     public OutboundVariable convertOutbound(Object JavaDoc object, OutboundContext outctx)
72     {
73         if (object == null)
74         {
75             return new SimpleOutboundVariable("null", outctx, true);
76         }
77
78         return new SimpleOutboundVariable(object.toString(), outctx, true);
79     }
80 }
81
Popular Tags