Embedding the Report Engine API into our application requires a little knowledge of the inner workings of BIRT. The API is beyond the scope of this book, but we will get a little view of how to create a simple report executor application.
For this application to work, we need to have Java 1.4 or higher set up for BIRT 2.2 and below, and Java 1.5 set up for BIRT 2.3 and above, and have the BIRT runtime set up and visible in our classpath. We will use the Apache Commons CLI to handle the command line options. The following does not take into account parameters; it simply demonstrates how to instantiate the Report Engine API.
package com.birt_book; import java.util.HashMap; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.PosixParser; import org.eclipse.birt.core.exception.BirtException; import org.eclipse.birt.core.framework.Platform; import org.eclipse.birt.core.framework.PlatformConfig; import org.eclipse.birt.report.engine.api.EngineConfig; import org.eclipse.birt.report.engine.api.EngineConstants; import org.eclipse.birt.report.engine.api.EngineException; import org.eclipse.birt.report.engine.api.HTMLRenderContext; import org.eclipse.birt.report.engine.api.HTMLRenderOption; import org.eclipse.birt.report.engine.api.IReportEngine; import org.eclipse.birt.report.engine.api.IReportEngineFactory; import org.eclipse.birt.report.engine.api.IReportRunnable; import org.eclipse.birt.report.engine.api.IRunAndRenderTask; public class ReportExecutor { private static String BIRT_HOME = "C:/birt-runtime-2_5_2/birt-runtime-2_5_2/ReportEngine"; private static String IMAGE_PATH = "C:/BIRT_RUNTIME_2_2/images"; private String reportLocation; private String reportOutputLocation; /** * setupCLIParameters * * This will setup the arguments * @return */ public Options setupCLIParameters() { Options options = new Options(); options.addOption("i", "input", true, "The report file to execute"); options.addOption("o", "output", true, "The name of the output file"); return options; } /** * parseCommandLineOptions * * Given the arguments passed into main, this method will use the Apache Commons CLI * to parse those options and return a CommandLine object with the options * * @param args * @return CommandLine */ public CommandLine parseCommandLineOptions(String []args) { Report Engine APIReport Engine APIembedding, in application// First, parse the command line options using Apache Commons CLI CommandLineParser parser = new PosixParser(); Options options = setupCLIParameters(); CommandLine line = null; HelpFormatter formatter = new HelpFormatter(); //Try to parse the command line options, exit the app if there is an error try { //get the options line = parser.parse(options, args); } catch (Exception e) { System.err.println("Parsing failed. Reason: " + e.getMessage()); formatter.printHelp("ReportExecutor", options); System.exit(-1); } return line; } Report Engine APIReport Engine APIembedding, in application/** Report Engine APIReport Engine APIembedding, in application* startupPlatform * * This will startup the Eclipse platform and load any plugins */ private void startupPlatform() { //initialize the Eclipse platform, plugins, and report engine PlatformConfig platformConfig = new PlatformConfig(); platformConfig.setBIRTHome(BIRT_HOME); try { Platform.startup(platformConfig); } catch (BirtException e) { e.printStackTrace(); //we cannot start the platform, exit System.exit(-1); } } /** * createReportEngine * * This will create a report engine to use * @return */ private IReportEngine createReportEngine() { //create a new report engine factory IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); //create a new report engine EngineConfig engineConfig = new EngineConfig(); //the location of the BIRT Runtime goes here engineConfig.setBIRTHome(BIRT_HOME); //will replace with configuration file return factory.createReportEngine(engineConfig); } /** * Executes a report with no parameters, only requires report name to execute * @param reportName * @return */ Report Engine APIReport Engine APIembedding, in applicationpublic void executeReportNoParams(String reportName, String outputFile, IReportEngine engine) { try { //create the report runnable and runandrender task IReportRunnable runnable = engine.openReportDesign(reportName); IRunAndRenderTask task = engine.createRunAndRenderTask(runnable); //Set Render context to handle url and image locataions HTMLRenderContext renderContext = new HTMLRenderContext(); renderContext.setImageDirectory(IMAGE_PATH); HashMap contextMap = new HashMap(); contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext ); task.setAppContext( contextMap ); //Set rendering options - such as file or stream output, //output format, whether it is embeddable, etc HTMLRenderOption options = new HTMLRenderOption(); options.setOutputFileName(outputFile); options.setOutputFormat("html"); task.setRenderOption(options); //Run the report and close task.run(); task.close(); } catch (EngineException e) { e.printStackTrace(); System.exit(-1); } } /** * executeReport * * This method will execute the report and save the the output file * @param reportInput * @param reportOutput */ public void executeReport(String reportInput, String reportOutput) { //startup the platform startupPlatform(); //create a report engine IReportEngine engine = createReportEngine(); //create a run and render task and execute report executeReportNoParams(reportInput, reportOutput, engine); //shutdown platform Platform.shutdown(); } /** * @param args */ Report Engine APIReport Engine APIembedding, in applicationpublic static void main(String[] args) { ReportExecutor re = new ReportExecutor(); //Get command line options CommandLine cl = re.parseCommandLineOptions(args); //get the input file and output file String reportInputFile = cl.getOptionValue("i"); String reportOutputFile = cl.getOptionValue("o"); //execute the report re.executeReport(reportInputFile, reportOutputFile); } }