KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cpmake > GroovyShellInterpreter


1 /*
2  * Copyright (c) 2004, Brian Hawkins
3  * brianhks@activeclickweb.com
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20  
21 package cpmake;
22
23 import groovy.lang.GroovyShell;
24 import groovy.lang.GroovyRuntimeException;
25 import org.codehaus.groovy.control.CompilationFailedException;
26 import java.io.*;
27
28 class GroovyShellInterpreter
29         implements ScriptInterpreter
30     {
31     GroovyShell m_interpreter;
32     
33     public GroovyShellInterpreter()
34         {
35         m_interpreter = new GroovyShell();
36         }
37         
38     public void set(String JavaDoc var, Object JavaDoc value)
39             throws CPMakeException
40         {
41         m_interpreter.setVariable(var, value);
42         }
43         
44     public void call(String JavaDoc method, String JavaDoc param1)
45             throws CPMakeException
46         {
47         call(method, param1, null);
48         }
49         
50     public void call(String JavaDoc method, String JavaDoc param1, String JavaDoc param2)
51             throws CPMakeException
52         {
53         String JavaDoc cmd;
54         if (param2 == null)
55             cmd = method + "(" + param1 + ")";
56         else
57             cmd = method + "(" + param1 + ", " + param2 + ")";
58             
59         try
60             {
61             m_interpreter.evaluate(cmd);
62             }
63         catch (CompilationFailedException cfe)
64             {
65             throw new CPMakeException(cfe.toString(), -1);
66             }
67         catch (IOException ioe)
68             {
69             throw new CPMakeException("Unable to call '"+method+"'", -1);
70             }
71         catch(Exception JavaDoc e)
72             {
73             throw new CPMakeException(e.toString(), -1);
74             }
75         }
76         
77     public void source(String JavaDoc file)
78             throws CPMakeException, FileNotFoundException, IOException
79         {
80         try
81             {
82             m_interpreter.evaluate(new File(file));
83             }
84         catch (CompilationFailedException cfe)
85             {
86             throw new CPMakeException(cfe.toString(), -1);
87             }
88         catch (GroovyRuntimeException gre)
89             {
90             //throw new ScriptException(gre.getNode().getLineNumber(), file, gre.getMessage());
91
throw new CPMakeException(gre.getMessage(), -1);
92             }
93         catch(Exception JavaDoc e)
94             {
95             throw new CPMakeException(e.toString(), -1);
96             }
97         }
98         
99     public void cleanup()
100         {
101         }
102     }
103
Popular Tags