1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package groovy.lang;
47
48 import java.io.File;
49 import java.io.IOException;
50
51 import org.codehaus.groovy.ast.expr.ArgumentListExpression;
52 import org.codehaus.groovy.control.CompilationFailedException;
53 import org.codehaus.groovy.runtime.InvokerHelper;
54
55 /***
56 * This object represents a Groovy script
57 *
58 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
59 * @version $Revision: 1.13 $
60 */
61 public abstract class Script extends GroovyObjectSupport {
62 private Binding binding = new Binding();
63
64 protected Script() {
65 }
66
67 protected Script(Binding binding) {
68 this.binding = binding;
69 }
70
71 public Binding getBinding() {
72 return binding;
73 }
74
75 public void setBinding(Binding binding) {
76 this.binding = binding;
77 }
78
79 public Object getProperty(String property) {
80
81 Object answer = binding.getVariable(property);
82 if (answer == null) {
83 try {
84 return super.getProperty(property);
85 }
86 catch (MissingPropertyException e) {
87
88 }
89 }
90 return answer;
91 }
92
93 public void setProperty(String property, Object newValue) {
94
95 binding.setVariable(property, newValue);
96
97 }
98
99 /***
100 * The main instance method of a script which has variables in scope
101 * as defined by the current {@link Binding} instance.
102 *
103 * @return
104 */
105 public abstract Object run();
106
107
108
109 /***
110 * Prints a newline to the current 'out' variable which should be a PrintWriter
111 * or at least have a println() method defined on it.
112 * If there is no 'out' property then print to standard out.
113 */
114 public void println() {
115 Object object = getProperty("out");
116 if (object != null) {
117 InvokerHelper.invokeMethod(object, "println", ArgumentListExpression.EMPTY_ARRAY);
118 }
119 else {
120 System.out.println();
121 }
122 }
123
124 /***
125 * Prints the value to the current 'out' variable which should be a PrintWriter
126 * or at least have a print() method defined on it.
127 * If there is no 'out' property then print to standard out.
128 */
129 public void print(Object value) {
130 Object object = getProperty("out");
131 if (object != null) {
132 InvokerHelper.invokeMethod(object, "print", new Object[] { value });
133 }
134 else {
135 System.out.print(value);
136 }
137 }
138
139 /***
140 * Prints the value and a newline to the current 'out' variable which should be a PrintWriter
141 * or at least have a println() method defined on it.
142 * If there is no 'out' property then print to standard out.
143 */
144 public void println(Object value) {
145 Object object = getProperty("out");
146 if (object != null) {
147 InvokerHelper.invokeMethod(object, "println", new Object[] { value });
148 }
149 else {
150 System.out.println(value);
151 }
152 }
153
154 /***
155 * A helper method to allow the dynamic evaluation of groovy expressions using this
156 * scripts binding as the variable scope
157 *
158 * @param expression is the Groovy script expression to evaluate
159 */
160 public Object evaluate(String expression) throws CompilationFailedException, IOException {
161 GroovyShell shell = new GroovyShell(binding);
162 return shell.evaluate(expression);
163 }
164
165 /***
166 * A helper method to allow the dynamic evaluation of groovy expressions using this
167 * scripts binding as the variable scope
168 *
169 * @param file is the Groovy script to evaluate
170 */
171 public Object evaluate(File file) throws CompilationFailedException, IOException {
172 GroovyShell shell = new GroovyShell(binding);
173 return shell.evaluate(file);
174 }
175
176 /***
177 * A helper method to allow scripts to be run taking command line arguments
178 */
179 public void run(File file, String[] arguments) throws CompilationFailedException, IOException {
180 GroovyShell shell = new GroovyShell(binding);
181 shell.run(file, arguments);
182 }
183 }