Base class for report compilers.
| Method from net.sf.jasperreports.engine.design.JRAbstractCompiler Detail: |
abstract protected void checkLanguage(String language) throws JRException
Checks that the report language is supported by the compiler. |
public final JasperReport compileReport(JasperDesign jasperDesign) throws JRException {
// check if the language is supported by the compiler
checkLanguage(jasperDesign.getLanguage());
// collect all report expressions
JRExpressionCollector expressionCollector = JRExpressionCollector.collector(jasperDesign);
// verify the report design
verifyDesign(jasperDesign, expressionCollector);
String nameSuffix = createNameSuffix();
// check if saving source files is required
boolean isKeepJavaFile = JRProperties.getBooleanProperty(JRProperties.COMPILER_KEEP_JAVA_FILE);
File tempDirFile = null;
if (isKeepJavaFile || needsSourceFiles)
{
String tempDirStr = JRProperties.getProperty(JRProperties.COMPILER_TEMP_DIR);
tempDirFile = new File(tempDirStr);
if (!tempDirFile.exists() || !tempDirFile.isDirectory())
{
throw new JRException("Temporary directory not found : " + tempDirStr);
}
}
List datasets = jasperDesign.getDatasetsList();
List crosstabs = jasperDesign.getCrosstabs();
JRCompilationUnit[] units = new JRCompilationUnit[datasets.size() + crosstabs.size() + 1];
// generating source code for the main report dataset
units[0] = createCompileUnit(jasperDesign, jasperDesign.getMainDesignDataset(), expressionCollector, tempDirFile, nameSuffix);
int sourcesCount = 1;
for (Iterator it = datasets.iterator(); it.hasNext(); ++sourcesCount)
{
JRDesignDataset dataset = (JRDesignDataset) it.next();
// generating source code for a sub dataset
units[sourcesCount] = createCompileUnit(jasperDesign, dataset, expressionCollector, tempDirFile, nameSuffix);
}
for (Iterator it = crosstabs.iterator(); it.hasNext(); ++sourcesCount)
{
JRDesignCrosstab crosstab = (JRDesignCrosstab) it.next();
// generating source code for a sub dataset
units[sourcesCount] = createCompileUnit(jasperDesign, crosstab, expressionCollector, tempDirFile, nameSuffix);
}
String classpath = JRProperties.getProperty(JRProperties.COMPILER_CLASSPATH);
try
{
// compiling generated sources
String compileErrors = compileUnits(units, classpath, tempDirFile);
if (compileErrors != null)
{
throw new JRException("Errors were encountered when compiling report expressions class file:\n" + compileErrors);
}
// creating the report compile data
JRReportCompileData reportCompileData = new JRReportCompileData();
reportCompileData.setMainDatasetCompileData(units[0].getCompileData());
for (ListIterator it = datasets.listIterator(); it.hasNext();)
{
JRDesignDataset dataset = (JRDesignDataset) it.next();
reportCompileData.setDatasetCompileData(dataset, units[it.nextIndex()].getCompileData());
}
for (ListIterator it = crosstabs.listIterator(); it.hasNext();)
{
JRDesignCrosstab crosstab = (JRDesignCrosstab) it.next();
Integer crosstabId = expressionCollector.getCrosstabId(crosstab);
reportCompileData.setCrosstabCompileData(crosstabId.intValue(), units[datasets.size() + it.nextIndex()].getCompileData());
}
// creating the report
JasperReport jasperReport =
new JasperReport(
jasperDesign,
getCompilerClass(),
reportCompileData,
expressionCollector,
nameSuffix
);
return jasperReport;
}
catch (JRException e)
{
throw e;
}
catch (Exception e)
{
throw new JRException("Error compiling report design.", e);
}
finally
{
if (needsSourceFiles && !isKeepJavaFile)
{
deleteSourceFiles(units);
}
}
}
|
abstract protected String compileUnits(JRCompilationUnit[] units,
String classpath,
File tempDirFile) throws JRException
|
abstract protected JRCompilationSourceCode generateSourceCode(JRSourceCompileTask sourceTask) throws JRException
Generates expression evaluator code. |
protected String getCompilerClass() {
return getClass().getName();
}
|
abstract protected String getSourceFileName(String unitName)
Returns the name of the source file where generated source code for an unit is saved.
If the compiler needs source files for compilation
or COMPILER_KEEP_JAVA_FILE is set, the generated source
will be saved in a file having the name returned by this method. |
public static String getUnitName(JasperReport report,
JRDataset dataset) {
return getUnitName(report, dataset, report.getCompileNameSuffix());
}
Returns the name of the expression evaluator unit for a dataset of a report. |
public static String getUnitName(JasperReport report,
JRCrosstab crosstab) {
return getUnitName(report, crosstab.getId(), report.getCompileNameSuffix());
}
Returns the name of the expression evaluator unit for a crosstab of a report. |
protected static String getUnitName(JRReport report,
JRDataset dataset,
String nameSuffix) {
String className;
if (dataset.isMainDataset())
{
className = report.getName();
}
else
{
className = report.getName() + "_" + dataset.getName();
}
className = JRStringUtil.getLiteral(className) + nameSuffix;
return className;
}
|
protected static String getUnitName(JRReport report,
int crosstabId,
String nameSuffix) {
return JRStringUtil.getLiteral(report.getName()) + "_CROSSTAB" + crosstabId + nameSuffix;
}
|
protected static String getUnitName(JRReport report,
JRCrosstab crosstab,
JRExpressionCollector expressionCollector,
String nameSuffix) {
Integer crosstabId = expressionCollector.getCrosstabId(crosstab);
if (crosstabId == null)
{
throw new JRRuntimeException("Crosstab ID not found.");
}
return getUnitName(report, crosstabId.intValue(), nameSuffix);
}
|
public JREvaluator loadEvaluator(JasperReport jasperReport) throws JRException {
return loadEvaluator(jasperReport, jasperReport.getMainDataset());
}
|
public JREvaluator loadEvaluator(JasperReport jasperReport,
JRDataset dataset) throws JRException {
String unitName = JRAbstractCompiler.getUnitName(jasperReport, dataset);
JRReportCompileData reportCompileData = (JRReportCompileData) jasperReport.getCompileData();
Serializable compileData = reportCompileData.getDatasetCompileData(dataset);
return loadEvaluator(compileData, unitName);
}
|
public JREvaluator loadEvaluator(JasperReport jasperReport,
JRCrosstab crosstab) throws JRException {
String unitName = JRAbstractCompiler.getUnitName(jasperReport, crosstab);
JRReportCompileData reportCompileData = (JRReportCompileData) jasperReport.getCompileData();
Serializable compileData = reportCompileData.getCrosstabCompileData(crosstab);
return loadEvaluator(compileData, unitName);
}
|
abstract protected JREvaluator loadEvaluator(Serializable compileData,
String unitName) throws JRException
Creates an expression evaluator instance from data saved when the report was compiled. |