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.14 $
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 try {
82 return binding.getVariable(property);
83 }
84 catch (MissingPropertyException e) {
85 return super.getProperty(property);
86 }
87 }
88
89 public void setProperty(String property, Object newValue) {
90
91 binding.setVariable(property, newValue);
92
93 }
94
95 /***
96 * The main instance method of a script which has variables in scope
97 * as defined by the current {@link Binding} instance.
98 *
99 * @return
100 */
101 public abstract Object run();
102
103
104
105 /***
106 * Prints a newline to the current 'out' variable which should be a PrintWriter
107 * or at least have a println() method defined on it.
108 * If there is no 'out' property then print to standard out.
109 */
110 public void println() {
111 Object object;
112
113 try {
114 object = getProperty("out");
115 }
116 catch (MissingPropertyException e) {
117 System.out.println();
118 return;
119 }
120
121 InvokerHelper.invokeMethod(object, "println", ArgumentListExpression.EMPTY_ARRAY);
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;
131
132 try {
133 object = getProperty("out");
134 }
135 catch (MissingPropertyException e) {
136 System.out.print(value);
137 return;
138 }
139
140 InvokerHelper.invokeMethod(object, "print", new Object[] { value });
141 }
142
143 /***
144 * Prints the value and a newline to the current 'out' variable which should be a PrintWriter
145 * or at least have a println() method defined on it.
146 * If there is no 'out' property then print to standard out.
147 */
148 public void println(Object value) {
149 Object object;
150
151 try {
152 object = getProperty("out");
153 }
154 catch (MissingPropertyException e) {
155 System.out.println(value);
156 return;
157 }
158
159 InvokerHelper.invokeMethod(object, "println", new Object[] { value });
160 }
161
162 /***
163 * A helper method to allow the dynamic evaluation of groovy expressions using this
164 * scripts binding as the variable scope
165 *
166 * @param expression is the Groovy script expression to evaluate
167 */
168 public Object evaluate(String expression) throws CompilationFailedException, IOException {
169 GroovyShell shell = new GroovyShell(binding);
170 return shell.evaluate(expression);
171 }
172
173 /***
174 * A helper method to allow the dynamic evaluation of groovy expressions using this
175 * scripts binding as the variable scope
176 *
177 * @param file is the Groovy script to evaluate
178 */
179 public Object evaluate(File file) throws CompilationFailedException, IOException {
180 GroovyShell shell = new GroovyShell(binding);
181 return shell.evaluate(file);
182 }
183
184 /***
185 * A helper method to allow scripts to be run taking command line arguments
186 */
187 public void run(File file, String[] arguments) throws CompilationFailedException, IOException {
188 GroovyShell shell = new GroovyShell(binding);
189 shell.run(file, arguments);
190 }
191 }