01: /*
02: * ============================================================================
03: * GNU Lesser General Public License
04: * ============================================================================
05: *
06: * JasperReports - Free Java report-generating library.
07: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
08: *
09: * This library is free software; you can redistribute it and/or
10: * modify it under the terms of the GNU Lesser General Public
11: * License as published by the Free Software Foundation; either
12: * version 2.1 of the License, or (at your option) any later version.
13: *
14: * This library is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17: * Lesser General Public License for more details.
18: *
19: * You should have received a copy of the GNU Lesser General Public
20: * License along with this library; if not, write to the Free Software
21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22: *
23: * JasperSoft Corporation
24: * 303 Second Street, Suite 450 North
25: * San Francisco, CA 94107
26: * http://www.jaspersoft.com
27: */
28: package net.sf.jasperreports.engine.design;
29:
30: import java.io.ByteArrayOutputStream;
31: import java.io.File;
32: import java.io.InputStream;
33:
34: import net.sf.jasperreports.engine.JRException;
35:
36: /**
37: * @author Teodor Danciu (teodord@users.sourceforge.net)
38: * @version $Id: JRJavacCompiler.java 1229 2006-04-19 10:27:35Z teodord $
39: */
40: public class JRJavacCompiler extends JRAbstractMultiClassCompiler {
41:
42: /**
43: *
44: */
45: public String compileClasses(File[] sourceFiles, String classpath)
46: throws JRException {
47: String[] source = new String[sourceFiles.length + 3];
48: source[0] = "javac";
49: source[1] = "-classpath";
50: source[2] = classpath;
51: for (int i = 0; i < sourceFiles.length; i++) {
52: source[i + 3] = sourceFiles[i].getPath();
53: }
54:
55: try {
56: // Compile the source file and arrange to read the errors if any.
57: Process compile = Runtime.getRuntime().exec(source);
58: InputStream errFile = compile.getErrorStream();
59:
60: // Read the error messages (if any) into the ByteArrayOutputStream
61: ByteArrayOutputStream baos = new ByteArrayOutputStream();
62: byte[] buffer = new byte[1024];
63: int count = 0;
64: do {
65: count = errFile.read(buffer);
66: if (count > 0) {
67: baos.write(buffer, 0, count);
68: }
69: } while (count >= 0);
70:
71: if (baos.toString().indexOf("error") != -1) {
72: return baos.toString();
73: }
74:
75: return null;
76: } catch (Exception e) {
77: StringBuffer files = new StringBuffer();
78: for (int i = 0; i < sourceFiles.length; ++i) {
79: files.append(sourceFiles[i].getPath());
80: files.append(' ');
81: }
82: throw new JRException(
83: "Error compiling report java source files : "
84: + files, e);
85: }
86: }
87:
88: }
|