KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > compilers > JRBshEvaluator


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.compilers;
29
30 import java.io.IOException JavaDoc;
31 import java.io.LineNumberReader JavaDoc;
32 import java.io.StringReader JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import net.sf.jasperreports.engine.JRException;
38 import net.sf.jasperreports.engine.JRExpression;
39 import net.sf.jasperreports.engine.fill.JREvaluator;
40 import bsh.EvalError;
41 import bsh.Interpreter;
42 import bsh.TargetError;
43
44
45 /**
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  * @version $Id: JRBshEvaluator.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
48  */

49 public class JRBshEvaluator extends JREvaluator
50 {
51
52
53     /**
54      *
55      */

56     private String JavaDoc bshScript = null;
57     private Interpreter interpreter = null;
58
59
60     /**
61      *
62      */

63     public JRBshEvaluator(String JavaDoc bshScript) throws JRException
64     {
65         super();
66         
67         this.bshScript = bshScript;
68
69         interpreter = new Interpreter();
70         
71         interpreter.setClassLoader(Thread.currentThread().getContextClassLoader());
72
73         try
74         {
75             interpreter.eval(new StringReader JavaDoc(bshScript));
76         }
77         catch(EvalError e)
78         {
79             throw new JRException(
80                 "Error evaluating report expressions BeanShell script."
81                 + "\nMessage : " + e.getMessage()
82                 + "\nLine " + e.getErrorLineNumber() + " : " + extractLineContent(e)
83                 );
84         }
85     }
86
87
88     /**
89      *
90      */

91     public void verify(Collection JavaDoc expressions) throws JRException
92     {
93         try
94         {
95             interpreter.eval("bshEvaluator = createBshEvaluator()");
96             
97             if (expressions != null)
98             {
99                 for(Iterator JavaDoc it = expressions.iterator(); it.hasNext();)
100                 {
101                     JRExpression expression = (JRExpression)it.next();
102                     interpreter.eval("bshEvaluator.evaluateOld(" + expression.getId() + ")");
103                 }
104             }
105         }
106         catch(TargetError te)
107         {
108             //ignore
109
}
110         catch(EvalError e)
111         {
112             throw new JRException(
113                 "Error testing report expressions BeanShell script."
114                 + "\nMessage : " + e.getMessage()
115                 + "\nLine " + e.getErrorLineNumber() + " : " + extractLineContent(e)
116                 );
117         }
118     }
119
120
121     /**
122      *
123      */

124     protected void customizedInit(
125         Map JavaDoc pars,
126         Map JavaDoc fldsm,
127         Map JavaDoc varsm
128         ) throws JRException
129     {
130         try
131         {
132             interpreter.set("calculator", this);
133             interpreter.set("fldsm", fldsm);
134             interpreter.set("varsm", varsm);
135             interpreter.set("parsm", pars);
136             interpreter.eval("bshEvaluator = createBshEvaluator()");
137             interpreter.eval("bshEvaluator.init(calculator, parsm, fldsm, varsm)");
138         }
139         catch(EvalError e)
140         {
141             throw new JRException("Error initializing report BeanShell calculator.", e);
142         }
143     }
144
145
146     /**
147      *
148      */

149     protected Object JavaDoc evaluateOld(int id) throws Throwable JavaDoc
150     {
151         try
152         {
153             return interpreter.eval("bshEvaluator.evaluateOld(" + id + ")");
154         }
155         catch(TargetError te)
156         {
157             throw te.getTarget();
158         }
159         catch(EvalError ee)
160         {
161             throw ee;
162         }
163     }
164
165
166     /**
167      *
168      */

169     protected Object JavaDoc evaluateEstimated(int id) throws Throwable JavaDoc
170     {
171         try
172         {
173             return interpreter.eval("bshEvaluator.evaluateEstimated(" + id + ")");
174         }
175         catch(TargetError te)
176         {
177             throw te.getTarget();
178         }
179         catch(EvalError ee)
180         {
181             throw ee;
182         }
183     }
184
185
186     /**
187      *
188      */

189     protected Object JavaDoc evaluate(int id) throws Throwable JavaDoc
190     {
191         try
192         {
193             return interpreter.eval("bshEvaluator.evaluate(" + id + ")");
194         }
195         catch(TargetError te)
196         {
197             throw te.getTarget();
198         }
199         catch(EvalError ee)
200         {
201             throw ee;
202         }
203     }
204
205     
206     /**
207      *
208      */

209     private String JavaDoc extractLineContent(EvalError e)
210     {
211         String JavaDoc lineContent = "";
212
213         LineNumberReader JavaDoc reader = null;
214
215         try
216         {
217             reader = new LineNumberReader JavaDoc(new StringReader JavaDoc(bshScript));
218             int lineNumber = e.getErrorLineNumber();
219             
220             for(int i = 0; i < lineNumber; i++)
221             {
222                 lineContent = reader.readLine();
223             }
224         }
225         catch(IOException JavaDoc ioe)
226         {
227         }
228         finally
229         {
230             if (reader != null)
231             {
232                 try
233                 {
234                     reader.close();
235                 }
236                 catch(IOException JavaDoc ioe)
237                 {
238                 }
239             }
240         }
241             
242         return lineContent;
243     }
244
245
246 }
247
Popular Tags