|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Script.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
$Id: Script.java,v 1.13 2004/05/15 04:48:29 bran Exp $
|
|
3 |
|
|
4 |
Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
|
|
5 |
|
|
6 |
Redistribution and use of this software and associated documentation
|
|
7 |
("Software"), with or without modification, are permitted provided
|
|
8 |
that the following conditions are met:
|
|
9 |
|
|
10 |
1. Redistributions of source code must retain copyright
|
|
11 |
statements and notices. Redistributions must also contain a
|
|
12 |
copy of this document.
|
|
13 |
|
|
14 |
2. Redistributions in binary form must reproduce the
|
|
15 |
above copyright notice, this list of conditions and the
|
|
16 |
following disclaimer in the documentation and/or other
|
|
17 |
materials provided with the distribution.
|
|
18 |
|
|
19 |
3. The name "groovy" must not be used to endorse or promote
|
|
20 |
products derived from this Software without prior written
|
|
21 |
permission of The Codehaus. For written permission,
|
|
22 |
please contact info@codehaus.org.
|
|
23 |
|
|
24 |
4. Products derived from this Software may not be called "groovy"
|
|
25 |
nor may "groovy" appear in their names without prior written
|
|
26 |
permission of The Codehaus. "groovy" is a registered
|
|
27 |
trademark of The Codehaus.
|
|
28 |
|
|
29 |
5. Due credit should be given to The Codehaus -
|
|
30 |
http://groovy.codehaus.org/
|
|
31 |
|
|
32 |
THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
|
|
33 |
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
|
|
34 |
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
35 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
36 |
THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
37 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
38 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
39 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
40 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
41 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
42 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
43 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
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 | 0 |
protected Script() {
|
65 |
} |
|
66 |
|
|
67 | 0 |
protected Script(Binding binding) {
|
68 | 0 |
this.binding = binding;
|
69 |
} |
|
70 |
|
|
71 | 0 |
public Binding getBinding() {
|
72 | 0 |
return binding;
|
73 |
} |
|
74 |
|
|
75 | 0 |
public void setBinding(Binding binding) { |
76 | 0 |
this.binding = binding;
|
77 |
} |
|
78 |
|
|
79 | 0 |
public Object getProperty(String property) {
|
80 |
//System.out.println("Script.getProperty for: " + property + " with binding: " + binding.getVariables());
|
|
81 | 0 |
Object answer = binding.getVariable(property); |
82 | 0 |
if (answer == null) { |
83 | 0 |
try {
|
84 | 0 |
return super.getProperty(property); |
85 |
} |
|
86 |
catch (MissingPropertyException e) {
|
|
87 |
// ignore errors inside scripts
|
|
88 |
} |
|
89 |
} |
|
90 | 0 |
return answer;
|
91 |
} |
|
92 |
|
|
93 | 0 |
public void setProperty(String property, Object newValue) { |
94 |
//System.out.println("Script.setProperty for: " + property + " with newValue: " + newValue);
|
|
95 | 0 |
binding.setVariable(property, newValue); |
96 |
//System.out.println("binding are now: " + binding.getVariables());
|
|
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 |
// println helper methods
|
|
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 | 0 |
public void println() { |
115 | 0 |
Object object = getProperty("out");
|
116 | 0 |
if (object != null) { |
117 | 0 |
InvokerHelper.invokeMethod(object, "println", ArgumentListExpression.EMPTY_ARRAY);
|
118 |
} |
|
119 |
else {
|
|
120 | 0 |
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 | 0 |
public void print(Object value) { |
130 | 0 |
Object object = getProperty("out");
|
131 | 0 |
if (object != null) { |
132 | 0 |
InvokerHelper.invokeMethod(object, "print", new Object[] { value }); |
133 |
} |
|
134 |
else {
|
|
135 | 0 |
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 | 0 |
public void println(Object value) { |
145 | 0 |
Object object = getProperty("out");
|
146 | 0 |
if (object != null) { |
147 | 0 |
InvokerHelper.invokeMethod(object, "println", new Object[] { value }); |
148 |
} |
|
149 |
else {
|
|
150 | 0 |
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 | 0 |
public Object evaluate(String expression) throws CompilationFailedException, IOException { |
161 | 0 |
GroovyShell shell = new GroovyShell(binding);
|
162 | 0 |
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 | 0 |
public Object evaluate(File file) throws CompilationFailedException, IOException { |
172 | 0 |
GroovyShell shell = new GroovyShell(binding);
|
173 | 0 |
return shell.evaluate(file);
|
174 |
} |
|
175 |
|
|
176 |
/**
|
|
177 |
* A helper method to allow scripts to be run taking command line arguments
|
|
178 |
*/
|
|
179 | 0 |
public void run(File file, String[] arguments) throws CompilationFailedException, IOException { |
180 | 0 |
GroovyShell shell = new GroovyShell(binding);
|
181 | 0 |
shell.run(file, arguments); |
182 |
} |
|
183 |
} |
|
184 |
|
|