1 28 package net.sf.jasperreports.compilers; 29 30 import java.io.File ; 31 import java.io.Serializable ; 32 33 import net.sf.jasperreports.engine.JRException; 34 import net.sf.jasperreports.engine.JRReport; 35 import net.sf.jasperreports.engine.design.JRAbstractCompiler; 36 import net.sf.jasperreports.engine.design.JRCompilationUnit; 37 import net.sf.jasperreports.engine.design.JRSourceCompileTask; 38 import net.sf.jasperreports.engine.fill.JRCalculator; 39 import net.sf.jasperreports.engine.fill.JREvaluator; 40 41 42 46 public class JRBshCompiler extends JRAbstractCompiler 47 { 48 49 50 public JRBshCompiler() 51 { 52 super(false); 53 } 54 55 56 protected JREvaluator loadEvaluator(Serializable compileData, String unitName) throws JRException 57 { 58 return new JRBshEvaluator((String ) compileData); 59 } 60 61 62 protected void checkLanguage(String language) throws JRException 63 { 64 if (!JRReport.LANGUAGE_JAVA.equals(language)) 65 { 66 throw 67 new JRException( 68 "Language \"" + language 69 + "\" not supported by this report compiler.\n" 70 + "Expecting \"java\" instead." 71 ); 72 } 73 } 74 75 76 protected String generateSourceCode(JRSourceCompileTask sourceTask) throws JRException 77 { 78 return JRBshGenerator.generateScript(sourceTask); 79 } 80 81 82 protected String compileUnits(JRCompilationUnit[] units, String classpath, File tempDirFile) throws JRException 83 { 84 verifyScripts(units); 85 86 for (int i = 0; i < units.length; i++) 87 { 88 String script = units[i].getSourceCode(); 89 units[i].setCompileData(script); 90 } 91 92 return null; 93 } 94 95 96 private void verifyScripts(JRCompilationUnit[] units) throws JRException 97 { 98 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 99 100 try 102 { 103 classLoader.loadClass(JRCalculator.class.getName()); 104 } 105 catch(ClassNotFoundException e) 106 { 107 classLoader = getClass().getClassLoader(); 108 } 109 110 ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); 111 Thread.currentThread().setContextClassLoader(classLoader); 112 113 for (int i = 0; i < units.length; i++) 114 { 115 String script = units[i].getSourceCode(); 116 117 JRBshEvaluator bshEvaluator = new JRBshEvaluator(script); 118 bshEvaluator.verify(units[i].getExpressions()); 119 } 120 121 Thread.currentThread().setContextClassLoader(oldContextClassLoader); 122 } 123 124 125 protected String getSourceFileName(String unitName) 126 { 127 return unitName + ".bsh"; 128 } 129 130 131 } 132 | Popular Tags |