1 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 49 public class JRBshCalculator extends JRCalculator 50 { 51 52 53 56 private String bshScript = null; 57 private Interpreter interpreter = null; 58 59 60 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 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 } 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 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 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 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 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 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 |