01: package org.directwebremoting.convert;
02:
03: import java.lang.reflect.InvocationTargetException;
04: import java.lang.reflect.Method;
05:
06: import org.directwebremoting.extend.Converter;
07: import org.directwebremoting.extend.InboundContext;
08: import org.directwebremoting.extend.InboundVariable;
09: import org.directwebremoting.extend.MarshallException;
10: import org.directwebremoting.extend.NonNestedOutboundVariable;
11: import org.directwebremoting.extend.OutboundContext;
12: import org.directwebremoting.extend.OutboundVariable;
13: import org.directwebremoting.util.LocalUtil;
14:
15: public class StringEnumAbstractBaseConverter extends BaseV20Converter
16: implements Converter {
17: /* (non-Javadoc)
18: * @see org.directwebremoting.extend.Converter#convertInbound(java.lang.Class, org.directwebremoting.extend.InboundVariable, org.directwebremoting.extend.InboundContext)
19: */
20: public Object convertInbound(Class<?> paramType,
21: InboundVariable iv, InboundContext inctx)
22: throws MarshallException {
23: String value = LocalUtil.decode(iv.getValue());
24:
25: try {
26: Method getter = paramType.getMethod("forString",
27: String.class);
28: Object bean = getter.invoke(paramType, value);
29:
30: if (bean == null) {
31: throw new MarshallException(paramType,
32: "unknown enum value (" + value + ")");
33: }
34:
35: return bean;
36: } catch (NoSuchMethodException e) {
37: throw new MarshallException(paramType, e);
38: } catch (IllegalArgumentException e) {
39: throw new MarshallException(paramType, e);
40: } catch (IllegalAccessException e) {
41: throw new MarshallException(paramType, e);
42: } catch (InvocationTargetException e) {
43: throw new MarshallException(paramType, e);
44: }
45: }
46:
47: /* (non-Javadoc)
48: * @see org.directwebremoting.extend.Converter#convertOutbound(java.lang.Object, org.directwebremoting.extend.OutboundContext)
49: */
50: public OutboundVariable convertOutbound(Object data,
51: OutboundContext outctx) {
52: return new NonNestedOutboundVariable(
53: '\'' + data.toString() + '\'');
54: }
55: }
|