KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Constructor JavaDoc;
19
20 import org.directwebremoting.dwrp.SimpleOutboundVariable;
21 import org.directwebremoting.extend.Converter;
22 import org.directwebremoting.extend.InboundContext;
23 import org.directwebremoting.extend.InboundVariable;
24 import org.directwebremoting.extend.OutboundContext;
25 import org.directwebremoting.extend.OutboundVariable;
26 import org.directwebremoting.util.JavascriptUtil;
27
28 /**
29  * An implementation of Converter for anything with a string constructor.
30  * @author Joe Walker [joe at eireneh dot com]
31  * @version $Id: ConstructorConverter.java,v 1.2 2004/11/04 15:54:07 joe_walker Exp $
32  */

33 public class ConstructorConverter 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)
39     {
40         try
41         {
42             Constructor JavaDoc converter = paramType.getConstructor(new Class JavaDoc[] { String JavaDoc.class });
43             return converter.newInstance(new Object JavaDoc[] { iv.getValue() });
44         }
45         catch (Exception JavaDoc ex)
46         {
47             throw new IllegalArgumentException JavaDoc(ex.toString());
48         }
49     }
50
51     /* (non-Javadoc)
52      * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
53      */

54     public OutboundVariable convertOutbound(Object JavaDoc data, OutboundContext outctx)
55     {
56         return new SimpleOutboundVariable('\'' + JavascriptUtil.escapeJavaScript(data.toString()) + '\'', outctx, true);
57     }
58 }
59
Popular Tags