KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > util > FormStep


1 package org.sapia.soto.state.util;
2
3 import org.apache.commons.beanutils.BeanUtils;
4 import org.apache.commons.lang.ClassUtils;
5
6 import org.sapia.soto.state.Context;
7 import org.sapia.soto.state.Result;
8 import org.sapia.soto.state.Step;
9 import org.sapia.soto.state.helpers.ScopeParser;
10
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16
17 /**
18  * @author Yanick Duchesne
19  * <dl>
20  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
21  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
22  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
23  * </dl>
24  */

25 public class FormStep implements Step {
26   private List JavaDoc _params = new ArrayList JavaDoc();
27   private String JavaDoc _className;
28
29   public FormStep() {
30   }
31
32   /**
33    * @see org.sapia.soto.state.Step#getName()
34    */

35   public String JavaDoc getName() {
36     return ClassUtils.getShortClassName(getClass());
37   }
38
39   public void setClass(String JavaDoc className) {
40     _className = className;
41   }
42
43   /**
44    * @see org.sapia.soto.state.Executable#execute(org.sapia.soto.state.Result)
45    */

46   public void execute(Result res) {
47     Object JavaDoc bean;
48
49     if (_className == null) {
50       bean = res.getContext().currentObject();
51       assign(bean, res);
52     } else {
53       try {
54         bean = Class.forName(_className).newInstance();
55       } catch (InstantiationException JavaDoc e) {
56         res.error(e);
57
58         return;
59       } catch (ClassNotFoundException JavaDoc e) {
60         res.error(e);
61
62         return;
63       } catch (IllegalAccessException JavaDoc e) {
64         res.error(e);
65
66         return;
67       }
68
69       assign(bean, res);
70       res.getContext().push(bean);
71     }
72   }
73
74   public Param createParam() {
75     Param p = new Param();
76     _params.add(p);
77
78     return p;
79   }
80
81   private void assign(Object JavaDoc bean, Result res) {
82     Param p;
83
84     for (int i = 0; i < _params.size(); i++) {
85       p = (Param) _params.get(i);
86
87       try {
88         p.process(bean, res.getContext());
89       } catch (InvocationTargetException JavaDoc e) {
90         res.error(e.getTargetException());
91
92         return;
93       } catch (IllegalAccessException JavaDoc e) {
94         res.error(e);
95
96         return;
97       }
98     }
99   }
100
101   public static class Param {
102     static final String JavaDoc ON = "on";
103     static final String JavaDoc TRUE = "true";
104     static final String JavaDoc YES = "yes";
105     static final Boolean JavaDoc BOOL_TRUE = new Boolean JavaDoc(true);
106     static final Boolean JavaDoc BOOL_FALSE = new Boolean JavaDoc(false);
107     String JavaDoc _from;
108     String JavaDoc _to;
109     String JavaDoc[] _scopes;
110     boolean _isBoolean;
111
112     public Param setBoolean(boolean bool) {
113       _isBoolean = bool;
114
115       return this;
116     }
117
118     public Param setFrom(String JavaDoc from) {
119       _from = from;
120
121       return this;
122     }
123
124     public Param setScopes(String JavaDoc scopes) {
125       _scopes = ScopeParser.parse(scopes);
126
127       return this;
128     }
129
130     public Param setTo(String JavaDoc to) {
131       _to = to;
132
133       return this;
134     }
135
136     void process(Object JavaDoc bean, Context ctx)
137       throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc,
138         IllegalStateException JavaDoc {
139       Object JavaDoc value;
140
141       if (_from == null) {
142         throw new IllegalStateException JavaDoc("'from' attribute not specified");
143       }
144
145       if (_to == null) {
146         throw new IllegalStateException JavaDoc("'to' attribute not specified");
147       }
148
149       if (_scopes == null) {
150         value = ctx.get(_from);
151
152         if (value != null) {
153           doAssign(bean, _to, value, _isBoolean);
154         }
155       } else {
156         value = ctx.get(_from, _scopes);
157
158         if (value != null) {
159           doAssign(bean, _to, value, _isBoolean);
160         }
161       }
162     }
163
164     private static void doAssign(Object JavaDoc bean, String JavaDoc to, Object JavaDoc val, boolean b)
165       throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
166       if (b) {
167         if (val != null) {
168           val = val.toString().toLowerCase();
169
170           if (val.toString().equals(ON) || val.toString().equals(TRUE) ||
171                 val.toString().equals(YES)) {
172             BeanUtils.setProperty(bean, to, BOOL_TRUE);
173           } else {
174             BeanUtils.setProperty(bean, to, BOOL_FALSE);
175           }
176         } else {
177           BeanUtils.setProperty(bean, to, BOOL_FALSE);
178         }
179       } else {
180         BeanUtils.setProperty(bean, to, val);
181       }
182     }
183   }
184 }
185
Popular Tags