1 28 29 33 package net.sf.jasperreports.compilers; 34 35 import java.io.ByteArrayInputStream ; 36 import java.io.File ; 37 import java.io.Serializable ; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 41 import net.sf.jasperreports.engine.JRException; 42 import net.sf.jasperreports.engine.JRReport; 43 import net.sf.jasperreports.engine.design.JRAbstractJavaCompiler; 44 import net.sf.jasperreports.engine.design.JRCompilationUnit; 45 import net.sf.jasperreports.engine.design.JRSourceCompileTask; 46 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 import org.codehaus.groovy.ast.ClassNode; 50 import org.codehaus.groovy.control.CompilationFailedException; 51 import org.codehaus.groovy.control.CompilationUnit; 52 import org.codehaus.groovy.control.CompilerConfiguration; 53 import org.codehaus.groovy.control.Phases; 54 import org.objectweb.asm.ClassVisitor; 55 import org.objectweb.asm.ClassWriter; 56 57 63 public class JRGroovyCompiler extends JRAbstractJavaCompiler 64 { 65 66 69 private static final Log log = LogFactory.getLog(JRGroovyCompiler.class); 70 71 72 public JRGroovyCompiler() 73 { 74 super(false); 75 } 76 77 78 protected String compileUnits(JRCompilationUnit[] units, String classpath, File tempDirFile) throws JRException 79 { 80 CompilerConfiguration config = new CompilerConfiguration(); 81 config.setClasspath(classpath); 82 CompilationUnit unit = new CompilationUnit(config); 83 84 for (int i = 0; i < units.length; i++) 85 { 86 unit.addSource("calculator_" + units[i].getName(), new ByteArrayInputStream (units[i].getSourceCode().getBytes())); 87 } 88 89 ClassCollector collector = new ClassCollector(); 90 unit.setClassgenCallback(collector); 91 try 92 { 93 unit.compile(Phases.CLASS_GENERATION); 94 } 95 catch (CompilationFailedException e) 96 { 97 throw new JRException( 98 "Errors were encountered when compiling report expressions class file:\n" 99 + e.toString() 100 ); 101 } 102 103 if (collector.classes.size() < units.length) 104 { 105 throw new JRException("Too few groovy class were generated."); 106 } 107 else if (collector.classCount > units.length) 108 { 109 throw new JRException( 110 "Too many groovy classes were generated.\n" 111 + "Please make sure that you don't use Groovy features such as closures that are not supported by this report compiler.\n" 112 ); 113 } 114 115 for (int i = 0; i < units.length; i++) 116 { 117 units[i].setCompileData((Serializable ) collector.classes.get(units[i].getName())); 118 } 119 120 return null; 121 } 122 123 124 127 private static class ClassCollector extends CompilationUnit.ClassgenCallback 128 { 129 public Map classes = new HashMap (); 130 public int classCount; 131 132 137 public void call(ClassVisitor writer, ClassNode node) throws CompilationFailedException 138 { 139 classCount++; 140 String name = node.getName(); 141 if (!classes.containsKey(name)) 142 { 143 byte[] bytes = ((ClassWriter) writer).toByteArray(); 144 classes.put(name, bytes); 145 } 146 } 147 } 148 149 150 protected void checkLanguage(String language) throws JRException 151 { 152 if (!JRReport.LANGUAGE_GROOVY.equals(language)) 153 { 154 throw 155 new JRException( 156 "Language \"" + language 157 + "\" not supported by this report compiler.\n" 158 + "Expecting \"groovy\" instead." 159 ); 160 } 161 } 162 163 164 protected String generateSourceCode(JRSourceCompileTask sourceTask) throws JRException 165 { 166 return JRGroovyGenerator.generateClass(sourceTask); 167 } 168 169 170 protected String getSourceFileName(String unitName) 171 { 172 return unitName + ".groovy"; 173 } 174 175 176 } | Popular Tags |