KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2005 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  * 185, Berry Street, Suite 6200
25  * San Francisco CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.compilers;
29
30 import java.io.IOException;
31 import java.io.LineNumberReader;
32 import java.io.StringReader;
33 import java.util.Collection;
34 import java.util.Iterator;
35 import java.util.Map;
36
37 import net.sf.jasperreports.engine.JRException;
38 import net.sf.jasperreports.engine.JRExpression;
39 import net.sf.jasperreports.engine.fill.JRCalculator;
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: JRBshCalculator.java,v 1.3 2005/06/27 07:11:22 teodord Exp $
48  */

49 public class JRBshCalculator extends JRCalculator
50 {
51
52
53     /**
54      *
55      */

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

63     public JRBshCalculator(String 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(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 expressions) throws JRException
92     {
93         try
94         {
95             interpreter.eval("bshCalculator = createBshCalculator()");
96             
97             if (expressions != null)
98             {
99                 for(Iterator it = expressions.iterator(); it.hasNext();)
100                 {
101                     JRExpression expression = (JRExpression)it.next();
102                     interpreter.eval("bshCalculator.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 pars,
126         Map fldsm,
127         Map 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("bshCalculator = createBshCalculator()");
137             interpreter.eval("bshCalculator.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 evaluateOld(int id) throws Throwable
150     {
151         try
152         {
153             return interpreter.eval("bshCalculator.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 evaluateEstimated(int id) throws Throwable
170     {
171         try
172         {
173             return interpreter.eval("bshCalculator.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 evaluate(int id) throws Throwable
190     {
191         try
192         {
193             return interpreter.eval("bshCalculator.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 extractLineContent(EvalError e)
210     {
211         String lineContent = "";
212
213         LineNumberReader reader = null;
214
215         try
216         {
217             reader = new LineNumberReader(new StringReader(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 ioe)
226         {
227         }
228         finally
229         {
230             if (reader != null)
231             {
232                 try
233                 {
234                     reader.close();
235                 }
236                 catch(IOException ioe)
237                 {
238                 }
239             }
240         }
241             
242         return lineContent;
243     }
244
245
246 }
247
Popular Tags