1 28 package net.sf.jasperreports.engine.design; 29 30 import java.io.File ; 32 import java.io.PrintWriter ; 33 import java.io.ByteArrayOutputStream ; 34 import java.lang.reflect.Method ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 import net.sf.jasperreports.engine.JRException; 40 import net.sf.jasperreports.engine.util.JRClassLoader; 41 42 43 47 public class JRJdk13Compiler extends JRAbstractMultiClassCompiler 48 { 49 50 51 54 static final Log log = LogFactory.getLog(JRJdk13Compiler.class); 55 56 59 private static final int MODERN_COMPILER_SUCCESS = 0; 60 61 62 65 public String compileClasses(File [] sourceFiles, String classpath) throws JRException 66 { 67 String [] source = new String [sourceFiles.length + 2]; 68 for (int i = 0; i < sourceFiles.length; i++) 69 { 70 source[i] = sourceFiles[i].getPath(); 71 } 72 source[sourceFiles.length] = "-classpath"; 73 source[sourceFiles.length + 1] = classpath; 74 75 String errors = null; 76 77 78 79 try 80 { 81 Class clazz = JRClassLoader.loadClassForName("com.sun.tools.javac.Main"); 82 Object compiler = clazz.newInstance(); 83 84 try 85 { 86 Method compileMethod = clazz.getMethod("compile", new Class [] {String [].class, PrintWriter .class}); 87 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 88 int result = ((Integer )compileMethod.invoke(compiler, new Object [] {source, new PrintWriter (baos)})).intValue(); 89 90 if (result != MODERN_COMPILER_SUCCESS) 91 { 92 errors = baos.toString(); 93 } 94 else 95 { 96 if (log.isInfoEnabled() && baos.size() > 0) 97 log.info(baos.toString()); 98 } 99 } 100 catch (NoSuchMethodException ex) 101 { 102 Method compileMethod = clazz.getMethod("compile", new Class [] {String [].class}); 103 104 int result = ((Integer )compileMethod.invoke(compiler, new Object [] {source})).intValue(); 105 if (result != MODERN_COMPILER_SUCCESS) 106 { 107 errors = "See error messages above."; 108 } 109 } 110 } 111 catch (Exception e) 112 { 113 StringBuffer files = new StringBuffer (); 114 for (int i = 0; i < sourceFiles.length; ++i) 115 { 116 files.append(sourceFiles[i].getPath()); 117 files.append(' '); 118 } 119 throw new JRException("Error compiling report java source files : " + files, e); 120 } 121 122 return errors; 123 } 124 } 125 | Popular Tags |