01: package abbot.script;
02:
03: import java.util.*;
04:
05: import org.jdom.*;
06: import org.jdom.Element;
07: import bsh.*;
08:
09: /** Provides evaluation of arbitrary Java expressions. Any Java expression is
10: supported, with a more loose syntax if desired. See the
11: <a href=http://www.beanshell.org/docs.html>beanshell documentation</a> for
12: complete details of the extended features available in this evaluator.
13: <p>
14: Note that any variables declared or assigned will be available to any
15: subsequent steps in the same Script.
16: */
17:
18: public class Expression extends Step {
19:
20: public static final String TAG_EXPRESSION = "expression";
21:
22: private static final String USAGE = "<expression>{java/beanshell expression}</expression>";
23: private String expression = "";
24:
25: public Expression(Resolver resolver, Element el, Map attributes) {
26: super (resolver, attributes);
27:
28: String expr = null;
29: Iterator iter = el.getContent().iterator();
30: while (iter.hasNext()) {
31: Object o = iter.next();
32: if (o instanceof CDATA) {
33: expr = ((CDATA) o).getText();
34: break;
35: }
36: }
37: if (expr == null)
38: expr = el.getText();
39: setExpression(expr);
40: }
41:
42: public Expression(Resolver resolver, String description) {
43: super (resolver, description);
44: }
45:
46: public String getDefaultDescription() {
47: return getExpression();
48: }
49:
50: public String getUsage() {
51: return USAGE;
52: }
53:
54: public String getXMLTag() {
55: return TAG_EXPRESSION;
56: }
57:
58: protected Element addContent(Element el) {
59: return el.addContent(new CDATA(getExpression()));
60: }
61:
62: public void setExpression(String text) {
63: expression = text;
64: }
65:
66: public String getExpression() {
67: return expression;
68: }
69:
70: /** Evaluates the expression. */
71: protected void runStep() throws Throwable {
72: Interpreter sh = (Interpreter) getResolver().getProperty(
73: Script.INTERPRETER);
74: try {
75: sh.eval(ArgumentParser.substitute(getResolver(),
76: getExpression()));
77: } catch (TargetError e) {
78: throw e.getTarget();
79: }
80: }
81: }
|