KickJava   Java API By Example, From Geeks To Geeks.

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


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.Method 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.MarshallException;
25 import org.directwebremoting.extend.OutboundContext;
26 import org.directwebremoting.extend.OutboundVariable;
27 import org.directwebremoting.util.LocalUtil;
28
29 /**
30  * Converter for all primitive types
31  * @author Joe Walker [joe at getahead dot ltd dot uk]
32  */

33 public class EnumConverter 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 = LocalUtil.decode(iv.getValue());
41
42         Object JavaDoc[] values;
43         try
44         {
45             Method JavaDoc getter = paramType.getMethod("values", new Class JavaDoc[0]);
46             values = (Object JavaDoc[]) getter.invoke(paramType, (Object JavaDoc[]) null);
47         }
48         catch (NoSuchMethodException JavaDoc ex)
49         {
50             // We would like to have done: if (!paramType.isEnum())
51
// But this catch block has the same effect
52
throw new MarshallException(paramType);
53         }
54         catch (Exception JavaDoc ex)
55         {
56             throw new MarshallException(paramType, ex);
57         }
58
59         for (int i = 0; i < values.length; i++)
60         {
61             Object JavaDoc en = values[i];
62             if (value.equals(en.toString()))
63             {
64                 return en;
65             }
66         }
67
68         throw new MarshallException(paramType);
69     }
70
71     /* (non-Javadoc)
72      * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
73      */

74     public OutboundVariable convertOutbound(Object JavaDoc object, OutboundContext outctx)
75     {
76         return new SimpleOutboundVariable('\'' + object.toString() + '\'', outctx, true);
77     }
78 }
79
Popular Tags