001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.design;
029:
030: //import java.io.ByteArrayOutputStream;
031: import java.io.File;
032: import java.io.PrintWriter;
033: import java.io.ByteArrayOutputStream;
034: import java.lang.reflect.Method;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: import net.sf.jasperreports.engine.JRException;
040: import net.sf.jasperreports.engine.util.JRClassLoader;
041:
042: /**
043: * @author Teodor Danciu (teodord@users.sourceforge.net)
044: * @version $Id: JRJdk13Compiler.java 1712 2007-04-30 18:04:51Z teodord $
045: */
046: public class JRJdk13Compiler extends JRAbstractMultiClassCompiler {
047:
048: /**
049: *
050: */
051: static final Log log = LogFactory.getLog(JRJdk13Compiler.class);
052:
053: /**
054: *
055: */
056: private static final int MODERN_COMPILER_SUCCESS = 0;
057:
058: /**
059: *
060: */
061: public String compileClasses(File[] sourceFiles, String classpath)
062: throws JRException {
063: String[] source = new String[sourceFiles.length + 2];
064: for (int i = 0; i < sourceFiles.length; i++) {
065: source[i] = sourceFiles[i].getPath();
066: }
067: source[sourceFiles.length] = "-classpath";
068: source[sourceFiles.length + 1] = classpath;
069:
070: String errors = null;
071:
072: try {
073: Class clazz = JRClassLoader
074: .loadClassForRealName("com.sun.tools.javac.Main");
075: Object compiler = clazz.newInstance();
076:
077: try {
078: Method compileMethod = clazz
079: .getMethod("compile", new Class[] {
080: String[].class, PrintWriter.class });
081: ByteArrayOutputStream baos = new ByteArrayOutputStream();
082: int result = ((Integer) compileMethod.invoke(compiler,
083: new Object[] { source, new PrintWriter(baos) }))
084: .intValue();
085:
086: if (result != MODERN_COMPILER_SUCCESS) {
087: errors = baos.toString();
088: } else {
089: if (log.isInfoEnabled() && baos.size() > 0)
090: log.info(baos.toString());
091: }
092: } catch (NoSuchMethodException ex) {
093: Method compileMethod = clazz.getMethod("compile",
094: new Class[] { String[].class });
095:
096: int result = ((Integer) compileMethod.invoke(compiler,
097: new Object[] { source })).intValue();
098: if (result != MODERN_COMPILER_SUCCESS) {
099: errors = "See error messages above.";
100: }
101: }
102: } catch (Exception e) {
103: StringBuffer files = new StringBuffer();
104: for (int i = 0; i < sourceFiles.length; ++i) {
105: files.append(sourceFiles[i].getPath());
106: files.append(' ');
107: }
108: throw new JRException(
109: "Error compiling report java source files : "
110: + files, e);
111: }
112:
113: return errors;
114: }
115: }
|