KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cpmake > BeanShellInterpreter


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 bsh.*;
24 import java.io.*;
25
26 class BeanShellInterpreter
27         implements ScriptInterpreter
28     {
29     private Interpreter m_interpreter;
30     
31     private int getLineNumber(String JavaDoc message)
32         {
33         return (-1);
34         }
35     
36     public BeanShellInterpreter()
37         {
38         m_interpreter = new Interpreter();
39         }
40         
41     public void set(String JavaDoc var, Object JavaDoc value)
42             throws CPMakeException
43         {
44         try
45             {
46             m_interpreter.set(var, value);
47             }
48         catch (EvalError ee)
49             {
50             throw new ScriptException(ee.getErrorLineNumber(), ee.getErrorSourceFile(), ee.getMessage());
51             }
52         }
53         
54     public void call(String JavaDoc method, String JavaDoc param1)
55             throws CPMakeException
56         {
57         call(method, param1, null);
58         }
59         
60     public void call(String JavaDoc method, String JavaDoc param1, String JavaDoc param2)
61             throws CPMakeException
62         {
63         String JavaDoc cmd;
64         if (param2 == null)
65             cmd = method + "(" + param1 + ")";
66         else
67             cmd = method + "(" + param1 + ", " + param2 + ")";
68             
69         try
70             {
71             m_interpreter.eval(cmd);
72             }
73         catch(TargetError te)
74             {
75             if (te.getTarget() instanceof CPMakeException)
76                 throw (CPMakeException)te.getTarget();
77             else
78                 {
79                 te.getTarget().printStackTrace();
80                 throw new ScriptException(te.getErrorLineNumber(), te.getErrorSourceFile(),
81                         te.getMessage()+"\n"+te.getTarget().getMessage());
82                 }
83             }
84         catch(EvalError ee)
85             {
86             throw new ScriptException(ee.getErrorLineNumber(), ee.getErrorSourceFile(), ee.getMessage());
87             }
88         }
89         
90     public void source(String JavaDoc file)
91             throws CPMakeException, FileNotFoundException, IOException
92         {
93         try
94             {
95             m_interpreter.source(file);
96             }
97         catch(TargetError te)
98             {
99             te.getTarget().printStackTrace();
100             if (te.getTarget() instanceof CPMakeException)
101                 throw (CPMakeException)te.getTarget();
102             else
103                 throw new ScriptException(te.getErrorLineNumber(), te.getErrorSourceFile(),
104                         te.getMessage()+"\n"+te.getTarget().toString());
105             }
106         catch (EvalError ee)
107             {
108             int lineNumber;
109             ee.printStackTrace();
110             //This is a hack for a bug in beanshell
111
try
112                 {
113                 lineNumber = ee.getErrorLineNumber();
114                 }
115             catch (NullPointerException JavaDoc npe)
116                 {
117                 lineNumber = getLineNumber(ee.getMessage());
118                 }
119             throw new ScriptException(lineNumber, ee.getErrorSourceFile(), ee.getMessage());
120             }
121         }
122         
123     public void cleanup()
124         {
125         }
126     }
127
Popular Tags