KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.directwebremoting.dwrp.SimpleOutboundVariable;
19 import org.directwebremoting.extend.Converter;
20 import org.directwebremoting.extend.InboundContext;
21 import org.directwebremoting.extend.InboundVariable;
22 import org.directwebremoting.extend.MarshallException;
23 import org.directwebremoting.extend.OutboundContext;
24 import org.directwebremoting.extend.OutboundVariable;
25 import org.directwebremoting.util.JavascriptUtil;
26 import org.directwebremoting.util.LocalUtil;
27 import org.directwebremoting.util.Messages;
28
29 /**
30  * Converter for all primitive types
31  * @author Joe Walker [joe at getahead dot ltd dot uk]
32  */

33 public class PrimitiveConverter extends BaseV20Converter implements Converter
34 {
35     /* (non-Javadoc)
36      * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
37      */

38     public Object JavaDoc convertInbound(Class JavaDoc paramType, InboundVariable iv, InboundContext inctx) throws MarshallException
39     {
40         String JavaDoc value = iv.getValue();
41         value = LocalUtil.decode(value.trim());
42
43         try
44         {
45             return LocalUtil.simpleConvert(value, paramType);
46         }
47         catch (NumberFormatException JavaDoc ex)
48         {
49             throw new MarshallException(paramType, Messages.getString("PrimitiveConverter.FormatError", value));
50         }
51         catch (IllegalArgumentException JavaDoc ex)
52         {
53             throw new MarshallException(paramType);
54         }
55     }
56
57     /* (non-Javadoc)
58      * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
59      */

60     public OutboundVariable convertOutbound(Object JavaDoc object, OutboundContext outctx)
61     {
62         Class JavaDoc paramType = object.getClass();
63
64         if (object.equals(Boolean.TRUE))
65         {
66             return new SimpleOutboundVariable("true", outctx, true);
67         }
68         else if (object.equals(Boolean.FALSE))
69         {
70             return new SimpleOutboundVariable("false", outctx, true);
71         }
72         else if (paramType == Character JavaDoc.class)
73         {
74             // Treat characters as strings
75
return new SimpleOutboundVariable('\"' + JavascriptUtil.escapeJavaScript(object.toString()) + '\"', outctx, true);
76         }
77         else
78         {
79             // We just use the default toString for all numbers
80
return new SimpleOutboundVariable(object.toString(), outctx, true);
81         }
82     }
83 }
84
Popular Tags