View Javadoc

1   /*
2    $Id: Script.java,v 1.14 2004/07/18 12:46:09 tug 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.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          //System.out.println("Script.getProperty for: " + property + " with binding: " + binding.getVariables());
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          //System.out.println("Script.setProperty for: " + property + " with newValue: " + newValue);
91          binding.setVariable(property, newValue);
92          //System.out.println("binding are now: " + binding.getVariables());
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     // println helper methods
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 }