📄 ireportcompiler.java
字号:
/*
* IReportCompiler.java
*
* iReport -- Visual designer for generating JasperReports Documents
* Copyright (C) 2002-2003 Giulio Toffoli gt@businesslogic.it
*
* This program is free software; you can redistribute and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Giulio Toffoli
* Via T.Aspetti, 233
* 35100 Padova ITALY
* gt@businesslogic.it
*
*
* Created on 6 giugno 2003, 0.44
*/
package it.businesslogic.ireport;
import it.businesslogic.ireport.gui.*;
import it.businesslogic.ireport.connection.*;
import it.businesslogic.ireport.util.*;
import java.util.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.*;
import net.sf.jasperreports.view.*;
import net.sf.jasperreports.engine.export.*;
/**
* Please note that this class is totally different from the old IReportCompiler.class
* shipped with iReport 1.
* @author Administrator
*/
public class IReportCompiler implements Runnable, JRExportProgressMonitor
{
public static final int CMD_COMPILE = 0x01;
public static final int CMD_EXPORT = 0x02;
public static final int CMD_COMPILE_SCRIPTLET = 0x04;
public static final String OUTPUT_DIRECTORY = "OUTPUT_DIRECTORY";
public static final String OUTPUT_FORMAT = "OUTPUT_FORMAT";
public static final String USE_EMPTY_DATASOURCE = "USE_EMPTY_DATASOURCE";
public static final String USE_CONNECTION = "USE_CONNECTION";
public static final String CONNECTION = "CONNECTION";
public static final String SCRIPTLET_OUTPUT_DIRECTORY = "SCRIPTLET_OUTPUT_DIRECTORY";
private javax.swing.JList threadList = null;
static PrintStream myPrintStream = null;
int filledpage=0;
private String status="Staring";
private IReportConnection iReportConnection;
private int statusLevel = 0;
private JReportFrame jrf;
private MainFrame mainFrame;
private int command;
private HashMap properties;
private Thread thread;
private String javaFile = "";
static private String outputBuffer = "";
/** Creates a new instance of IReportCompiler */
public IReportCompiler()
{
properties = new HashMap();
command = 0;
}
public void stopThread()
{
command = 0;
if (thread != null && thread.isAlive())
{
try {
thread.interrupt();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
removeThread();
System.gc();
}
/** When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*
*/
public void run()
{
if (threadList != null)
{
javax.swing.DefaultListModel dlm = (javax.swing.DefaultListModel)threadList.getModel();
dlm.addElement(this);
threadList.updateUI();
}
boolean compilation_ok = true;
long start = System.currentTimeMillis();
// Redirect output stream....
PrintStream out = System.out;
PrintStream err = System.err;
if (myPrintStream == null)
myPrintStream =new PrintStream(new FilteredStream(new ByteArrayOutputStream()));
if (out != myPrintStream)
System.setOut(myPrintStream);
if (err != myPrintStream)
System.setErr(myPrintStream);
outputBuffer="";
//by Egon - DEBUG: Something is wrong here, please check. ok? thx.
//1 - Line 148 - srcScriptletFileName = C:\jasperreports-0.5.3\demo\samples\jasper\FirstJasper.jrxmScriptlet.java -> scriptlet filename
//2 - Line 157 - Misc.nvl( new File(fileName).getParent(), ".") => . -> report directory
// Add an entry in the thread list...
//by Egon - DEBUG: C:\jasperreports-0.5.3\demo\samples\jasper\FirstJasper.jrxml
String fileName = jrf.getReport().getFilename();
//by Egon - DEBUG: C:\jasperreports-0.5.3\demo\samples\jasper\FirstJasper.jrxml
String srcFileName = jrf.getReport().getFilename();
//by Egon - DEBUG: C:\jasperreports-0.5.3\demo\samples\jasper\FirstJasper.jasper
fileName = Misc.changeFileExtension(fileName,"jasper");
File f = new File(fileName);
if (properties.get(this.OUTPUT_DIRECTORY) != null)
{
//by Egon - DEBUG: .\FirstJasper.jasper
fileName = (String)properties.get(this.OUTPUT_DIRECTORY) + f.separatorChar + f.getName();
}
//by Egon - DEBUG: C:\jasperreports-0.5.3\demo\samples\jasper\FirstJasper.jrxml
String scriptletFileName = jrf.getReport().getFilename();
//by Egon - DEBUG: C:\jasperreports-0.5.3\demo\samples\jasper\FirstJasper.jrxml
String srcScriptletFileName = jrf.getReport().getFilename();
//by Egon - DEBUG: .\FirstJasper.
//fileName = Misc.changeFileExtension(fileName,"");
//by Egon - DEBUG: C:\jasperreports-0.5.3\demo\samples\jasper\FirstJasper.jrxmScriptlet.java
scriptletFileName = srcScriptletFileName.substring(0,scriptletFileName.length()-1)+"Scriptlet.java";
//1 - by Egon - DEBUG: C:\jasperreports-0.5.3\demo\samples\jasper\FirstJasper.jrxmScriptlet.java
srcScriptletFileName = scriptletFileName;
File f2 = new File(scriptletFileName);
if (properties.get(this.SCRIPTLET_OUTPUT_DIRECTORY) != null)
{
scriptletFileName = (String)properties.get(this.SCRIPTLET_OUTPUT_DIRECTORY) + f2.separatorChar + f2.getName();
}
String reportDirectory = new File(jrf.getReport().getFilename()).getParent();
String classpath = System.getProperty("jasper.reports.compile.class.path");
if(classpath != null){
classpath += File.pathSeparator + reportDirectory;
System.setProperty("jasper.reports.compile.class.path", classpath);
} else if (System.getProperty("java.class.path") != null){
classpath = System.getProperty("java.class.path");
classpath += File.pathSeparator + reportDirectory;
System.setProperty("java.class.path", classpath);
}
reportDirectory = reportDirectory.replace('\\', '/');
if(!reportDirectory.endsWith("/")){
reportDirectory += "/";//the file path separator must be present
}
if(!reportDirectory.startsWith("/")){
reportDirectory = "/" + reportDirectory;//it's important to JVM 1.4.2 especially if contains windows drive letter
}
ReportClassLoader reportClassLoader = new ReportClassLoader();
reportClassLoader.setRelodablePaths( reportDirectory );
reportClassLoader.rescanLibDirectory();
/******************/
try{
Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[]{new URL("file://"+reportDirectory)}, reportClassLoader));
} catch (MalformedURLException mue){
mue.printStackTrace();
}
/******************/
//.setContextClassLoader(reportClassLoader);
if ((command & CMD_COMPILE_SCRIPTLET) != 0)
{
status ="compiling scriptlet";
updateThreadList();
start = System.currentTimeMillis();
// Compile the scriptlet class...
String tempDirStr = System.getProperty("jasper.reports.compile.temp");
String oldCompileTemp = tempDirStr;
if (tempDirStr == null || tempDirStr.length() == 0 || mainFrame.isUsingCurrentFilesDirectoryForCompiles())
{
tempDirStr = mainFrame.getTranslatedCompileDirectory();
}
File tempDirFile = new File(tempDirStr);
javaFile = srcScriptletFileName;
javaFile = (new File(tempDirFile,javaFile)).getPath();
javaFile = jrf.getReport().getScriptletFileName();
if (Misc.getLastWriteTime(javaFile) > Misc.getLastWriteTime(Misc.changeFileExtension(javaFile, "class" )))
{
mainFrame.logOnConsole("</font><font face=\"SansSerif\" size=\"3\" color=\"#000000\">Compiling file... " + scriptletFileName + " -> "+javaFile+"</font><br><font face=\"Courier New\" size=\"3\">",true);
try
{
/*
if( mainFrame.isUsingCurrentFilesDirectoryForCompiles() )
{
System.setProperty("jasper.reports.compile.temp", tempDirStr);
}
*/
//JasperCompileManager.compileReportToFile(srcFileName, fileName);
net.sf.jasperreports.engine.design.JRJdk13Compiler compiler = new net.sf.jasperreports.engine.design.JRJdk13Compiler();
compiler.compileClass( new File(javaFile), Misc.getClassPath() );
}
/*
catch (Exception jrex)
{
mainFrame.logOnConsole("</font><font face=\"SansSerif\" size=\"3\" color=\"#CC0000\"><b>Errors compiling "+scriptletFileName+"!</b></font><br><font face=\"Courier New\" size=\"3\">",true);
//1. load the java file
Vector source = new Vector();
try
{
java.io.BufferedReader is = new java.io.BufferedReader(new java.io.FileReader( javaFile ));
while (true)
{
String line = is.readLine();
if (line == null) break;
source.addElement(line);
}
is.close();
} catch (Exception ioex)
{
// No file readed....
}
//2. write exception in a string
StringWriter sw = new StringWriter(0);
jrex.printStackTrace(new PrintWriter(sw));
System.out.println("\n\n\n");
myPrintStream.flush();
parseException( outputBuffer+sw.getBuffer()+"", source);
}
*/
catch (Exception ex)
{
mainFrame.logOnConsole("</font><font face=\"SansSerif\" size=\"3\" color=\"#CC0000\"><b>Error compiling the Scriptlet java source!</b></font><br><font face=\"Courier New\" size=\"3\">",true);
StringWriter sw = new StringWriter(0);
ex.printStackTrace(new PrintWriter(sw));
myPrintStream.flush();
parseException( outputBuffer+sw.getBuffer()+"", null);
compilation_ok = false;
}
catch (Throwable ext)
{
mainFrame.logOnConsole("</font><font face=\"SansSerif\" size=\"3\" color=\"#CC0000\"><b>Error compiling the Scriptlet java source!</b></font><br><font face=\"Courier New\" size=\"3\">",true);
StringWriter sw = new StringWriter(0);
ext.printStackTrace(new PrintWriter(sw));
myPrintStream.flush();
parseException( outputBuffer+sw.getBuffer()+"", null);
compilation_ok = false;
}
finally
{
if(mainFrame.isUsingCurrentFilesDirectoryForCompiles())
{
/*
if( oldCompileTemp != null )
{
System.setProperty("jasper.reports.compile.temp", oldCompileTemp);
}
else
{
System.setProperty("jasper.reports.compile.temp", "");
}
*/
}//end if using current files directory for compiles
}//end finally
mainFrame.logOnConsole(outputBuffer);
outputBuffer="";
mainFrame.logOnConsole("</font><hr><font face=\"SansSerif\" size=\"3\" color=\"#0000CC\"><b>Compilation running time : " + (System.currentTimeMillis() - start) + "</b></font><br><hr><font face=\"Courier New\" size=\"3\">",true);
}
}
if (!compilation_ok) {
removeThread();
return;
}
if ((command & CMD_COMPILE) != 0)
{
status ="compiling report";
updateThreadList();
System.setProperty("jasper.reports.compile.keep.java.file", "true");
//System.setProperty("jasper.reports.compile.temp", "C:\\");
// Compile report....
javaFile = this.jrf.getReport().getName()+".java";
String tempDirStr = System.getProperty("jasper.reports.compile.temp");
String oldCompileTemp = tempDirStr;
if (tempDirStr == null || tempDirStr.length() == 0 || mainFrame.isUsingCurrentFilesDirectoryForCompiles())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -