1 28 package net.sf.jasperreports.engine.design; 29 30 import net.sf.jasperreports.crosstabs.JRCrosstab; 31 import net.sf.jasperreports.engine.JRDataset; 32 import net.sf.jasperreports.engine.JRException; 33 import net.sf.jasperreports.engine.JRReport; 34 import net.sf.jasperreports.engine.JasperReport; 35 import net.sf.jasperreports.engine.fill.JREvaluator; 36 import net.sf.jasperreports.engine.util.JRClassLoader; 37 import net.sf.jasperreports.engine.util.JRProperties; 38 39 40 44 public final class JRDefaultCompiler implements JRCompiler 45 { 46 47 48 51 private static final JRDefaultCompiler instance = new JRDefaultCompiler(); 52 53 54 57 private JRDefaultCompiler() 58 { 59 } 60 61 62 65 public static JRDefaultCompiler getInstance() 66 { 67 return instance; 68 } 69 70 71 74 public JasperReport compileReport(JasperDesign jasperDesign) throws JRException 75 { 76 JRCompiler jrCompiler = null; 77 78 String compiler = JRProperties.getProperty(JRProperties.COMPILER_CLASS); 79 if ( 80 (compiler == null 81 || compiler.trim().length() == 0) 82 && JRReport.LANGUAGE_GROOVY.equals(jasperDesign.getLanguage()) 83 ) 84 { 85 compiler = "net.sf.jasperreports.compilers.JRGroovyCompiler"; 86 } 87 88 if (compiler == null || compiler.trim().length() == 0) 89 { 90 jrCompiler = getJavaCompiler(); 91 } 92 else 93 { 94 try 95 { 96 Class clazz = JRClassLoader.loadClassForName(compiler); 97 jrCompiler = (JRCompiler)clazz.newInstance(); 98 } 99 catch (Exception e) 100 { 101 throw new JRException("Could not instantiate report compiler : " + compiler, e); 102 } 103 } 104 105 return jrCompiler.compileReport(jasperDesign); 106 } 107 108 109 112 private static JRCompiler getJavaCompiler() 113 { 114 JRCompiler compiler = null; 115 116 try 117 { 118 JRClassLoader.loadClassForName("org.eclipse.jdt.internal.compiler.Compiler"); 119 compiler = new JRJdtCompiler(); 120 } 121 catch (Exception e) 122 { 123 } 124 125 if (compiler == null) 126 { 127 try 128 { 129 JRClassLoader.loadClassForName("com.sun.tools.javac.Main"); 130 compiler = new JRJdk13Compiler(); 131 } 132 catch (Exception e) 133 { 134 } 135 } 136 137 if (compiler == null) 138 { 139 try 140 { 141 JRClassLoader.loadClassForName("sun.tools.javac.Main"); 142 compiler = new JRJdk12Compiler(); 143 } 144 catch (Exception e) 145 { 146 } 147 } 148 149 if (compiler == null) 150 { 151 compiler = new JRJavacCompiler(); 152 } 153 154 return compiler; 155 } 156 157 158 private static JRCompiler getCompiler(JasperReport jasperReport) throws JRException 159 { 160 JRCompiler compiler = null; 161 162 String compilerClassName = jasperReport.getCompilerClass(); 163 164 Class compilerClass = null; 165 166 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 167 if (classLoader != null) 168 { 169 try 170 { 171 compilerClass = classLoader.loadClass(compilerClassName); 172 } 173 catch(ClassNotFoundException e) 174 { 175 } 176 } 177 178 if (compilerClass == null) 179 { 180 classLoader = JRDefaultCompiler.class.getClassLoader(); 181 try 182 { 183 if (classLoader == null) 184 { 185 compilerClass = Class.forName(compilerClassName); 186 } 187 else 188 { 189 compilerClass = classLoader.loadClass(compilerClassName); 190 } 191 } 192 catch(ClassNotFoundException e) 193 { 194 throw new JRException("Report compiler class not found : " + compilerClassName); 195 } 196 } 197 198 199 try 200 { 201 compiler = (JRCompiler)compilerClass.newInstance(); 202 } 203 catch (Exception e) 204 { 205 throw new JRException("Could not instantiate report compiler : " + compilerClassName, e); 206 } 207 return compiler; 208 } 209 210 211 214 public JREvaluator loadEvaluator(JasperReport jasperReport, JRDataset dataset) throws JRException 215 { 216 JRCompiler compiler = getCompiler(jasperReport); 217 218 return compiler.loadEvaluator(jasperReport, dataset); 219 } 220 221 222 public JREvaluator loadEvaluator(JasperReport jasperReport, JRCrosstab crosstab) throws JRException 223 { 224 JRCompiler compiler = getCompiler(jasperReport); 225 226 return compiler.loadEvaluator(jasperReport, crosstab); 227 } 228 229 230 public JREvaluator loadEvaluator(JasperReport jasperReport) throws JRException 231 { 232 return loadEvaluator(jasperReport, jasperReport.getMainDataset()); 233 } 234 235 236 } 237 | Popular Tags |