KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > control > If


1 package org.sapia.soto.state.control;
2
3 import org.apache.commons.jexl.Expression;
4 import org.apache.commons.jexl.ExpressionFactory;
5 import org.apache.commons.lang.ClassUtils;
6 import org.apache.commons.lang.StringUtils;
7
8 import org.sapia.soto.state.Result;
9 import org.sapia.soto.state.ScopeMap;
10 import org.sapia.soto.state.helpers.CompositeStep;
11
12 import java.util.Map JavaDoc;
13
14
15 /**
16  * @author Yanick Duchesne
17  * <dl>
18  * <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>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class If extends CompositeStep {
24   private Expression _expr;
25   private String JavaDoc[] _scopes;
26
27   public If() {
28   }
29
30   /**
31    * @see org.sapia.soto.state.Step#getName()
32    */

33   public String JavaDoc getName() {
34     return ClassUtils.getShortClassName(getClass());
35   }
36
37   public void setTest(String JavaDoc expr) throws Exception JavaDoc {
38     _expr = ExpressionFactory.createExpression(expr);
39   }
40
41   public void setScopes(String JavaDoc scopes) {
42     _scopes = StringUtils.split(scopes, ",");
43
44     for (int i = 0; i < _scopes.length; i++) {
45       _scopes[i] = _scopes[i].trim();
46     }
47   }
48
49   /**
50    * @see org.sapia.soto.state.helpers.CompositeStep#doExecute(Result)
51    */

52   protected boolean doExecute(Result st) {
53     Map scopes = st.getContext().getScopes();
54     ScopeMap map = new ScopeMap(scopes, _scopes);
55     Object JavaDoc evaled = null;
56
57     try {
58       evaled = _expr.evaluate(map);
59
60       Boolean JavaDoc b = (Boolean JavaDoc) evaled;
61
62       return b.booleanValue();
63     } catch (ClassCastException JavaDoc e) {
64       st.error("'if' must evaluate to a Boolean, not a " + evaled);
65
66       return false;
67     } catch (Exception JavaDoc e) {
68       e.printStackTrace();
69       st.error("Could not evaluate: '" + _expr.getExpression() + "' - " +
70         e.getMessage());
71
72       return false;
73     }
74   }
75 }
76
Popular Tags