001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.compilers;
029:
030: import java.io.IOException;
031: import java.io.LineNumberReader;
032: import java.io.StringReader;
033: import java.util.Collection;
034: import java.util.Iterator;
035: import java.util.Map;
036:
037: import net.sf.jasperreports.engine.JRException;
038: import net.sf.jasperreports.engine.JRExpression;
039: import net.sf.jasperreports.engine.fill.JREvaluator;
040: import bsh.EvalError;
041: import bsh.Interpreter;
042: import bsh.TargetError;
043:
044: /**
045: * @author Teodor Danciu (teodord@users.sourceforge.net)
046: * @version $Id: JRBshEvaluator.java 1777 2007-07-11 11:39:40Z lucianc $
047: */
048: public class JRBshEvaluator extends JREvaluator {
049:
050: /**
051: *
052: */
053: private String bshScript = null;
054: private Interpreter interpreter = null;
055:
056: /**
057: *
058: */
059: public JRBshEvaluator(String bshScript) throws JRException {
060: super ();
061:
062: this .bshScript = bshScript;
063:
064: interpreter = new Interpreter();
065:
066: interpreter.setClassLoader(Thread.currentThread()
067: .getContextClassLoader());
068:
069: try {
070: interpreter.eval(new StringReader(bshScript));
071: } catch (EvalError e) {
072: throw new JRException(
073: "Error evaluating report expressions BeanShell script."
074: + "\nMessage : " + e.getMessage()
075: + "\nLine " + e.getErrorLineNumber()
076: + " : " + extractLineContent(e));
077: }
078: }
079:
080: /**
081: *
082: */
083: public void verify(Collection expressions) throws JRException {
084: try {
085: interpreter.eval("bshEvaluator = createBshEvaluator()");
086:
087: if (expressions != null) {
088: for (Iterator it = expressions.iterator(); it.hasNext();) {
089: JRExpression expression = (JRExpression) it.next();
090: interpreter.eval("bshEvaluator.evaluateOld("
091: + expression.getId() + ")");
092: }
093: }
094: } catch (TargetError te) {
095: //ignore
096: } catch (EvalError e) {
097: throw new JRException(
098: "Error testing report expressions BeanShell script."
099: + "\nMessage : " + e.getMessage()
100: + "\nLine " + e.getErrorLineNumber()
101: + " : " + extractLineContent(e));
102: }
103: }
104:
105: /**
106: *
107: */
108: protected void customizedInit(Map pars, Map fldsm, Map varsm)
109: throws JRException {
110: try {
111: interpreter.set("calculator", this );
112: interpreter.set("fldsm", fldsm);
113: interpreter.set("varsm", varsm);
114: interpreter.set("parsm", pars);
115: interpreter.eval("bshEvaluator = createBshEvaluator()");
116: interpreter
117: .eval("bshEvaluator.init(calculator, parsm, fldsm, varsm)");
118: } catch (EvalError e) {
119: throw new JRException(
120: "Error initializing report BeanShell calculator.",
121: e);
122: }
123: }
124:
125: /**
126: *
127: */
128: protected Object evaluateOld(int id) throws Throwable {
129: try {
130: return interpreter.eval("bshEvaluator.evaluateOld(" + id
131: + ")");
132: } catch (TargetError te) {
133: throw te.getTarget();
134: } catch (EvalError ee) {
135: throw ee;
136: }
137: }
138:
139: /**
140: *
141: */
142: protected Object evaluateEstimated(int id) throws Throwable {
143: try {
144: return interpreter.eval("bshEvaluator.evaluateEstimated("
145: + id + ")");
146: } catch (TargetError te) {
147: throw te.getTarget();
148: } catch (EvalError ee) {
149: throw ee;
150: }
151: }
152:
153: /**
154: *
155: */
156: protected Object evaluate(int id) throws Throwable {
157: try {
158: return interpreter
159: .eval("bshEvaluator.evaluate(" + id + ")");
160: } catch (TargetError te) {
161: throw te.getTarget();
162: } catch (EvalError ee) {
163: throw ee;
164: }
165: }
166:
167: /**
168: *
169: */
170: private String extractLineContent(EvalError e) {
171: String lineContent = "";
172:
173: LineNumberReader reader = null;
174:
175: try {
176: reader = new LineNumberReader(new StringReader(bshScript));
177: int lineNumber = e.getErrorLineNumber();
178:
179: for (int i = 0; i < lineNumber; i++) {
180: lineContent = reader.readLine();
181: }
182: } catch (IOException ioe) {
183: } finally {
184: if (reader != null) {
185: try {
186: reader.close();
187: } catch (IOException ioe) {
188: }
189: }
190: }
191:
192: return lineContent;
193: }
194:
195: }
|