Opening a source for report generation
BIRT Report Engine classes generate a formatted report from either a report design or a binary report document. Typically, these files have the extensions RPTDESIGN and RPTDOCUMENT respectively. The engine can also generate a binary report document from a report design.
To open a report design, call one of the openReportDesign( ) methods on ReportEngine. These methods instantiate an IReportRunnable object, using a String that specifies the path to a report design or an input stream. To open a report document, call ReportEngine.openReportDocument( ). This method instantiates an IReportDocument object, using a String that specifies the path to a report document. Handle the EngineException that these methods throw.
Understanding an IReportRunnable object
The IReportRunnable object provides direct access to basic properties of the report design. The names of report design properties are static String fields, such as IReportRunnable.AUTHOR. To access a report design property, use getProperty( ) with a String argument that contains one of these fields.
To generate a report from a design, open the report design as shown in Listing 5‑4. Then, perform the tasks shown later in this chapter.
How to access a report design
Listing 5‑4 shows how to open a report design and find a property value. If the engine cannot open the specified report design, the code destroys the engine. The variable, engine, is a ReportEngine object.
Listing 5‑4 Accessing a report design
String designName = "./SimpleReport.rptdesign";
IReportRunnable runnable = null;
try {
runnable = engine.openReportDesign( designName );
}
catch ( EngineException e ) {
System.err.println ( "Design " + designName + " not found!" );
engine.destroy( );
System.exit( -1 );
}
// Get the value of a simple property.
String title = ( String ) runnable.getProperty
( IReportRunnable.TITLE );
Understanding an IReportDocument object
The IReportDocument object provides access to the structure and contents of a binary report document. IReportDocument provides methods to retrieve bookmark, page, and report design information. A bookmark is a String object that locates an instance of a report element in the document and the page on which the element exists. Use page information to render individual pages in the document to an output format. Bookmarks also provide access to the design elements that generated the element instance.
Use the getBookmarks( ) method to get a List object containing all the bookmarks in the document. Call the getBookmarkInstance( ) method with a String argument containing a bookmark to access the instantiated report element. Calling the getReportRunnable( ) method returns an IReportRunnable object, which an application can use to access report design information or generate another report document.
Close the document after use by calling IReportDocument.close( ).
How to access a report document
Listing 5‑5 shows how to open a report document and find a page. If the engine cannot open the specified report design, the code logs the error. The variable, engine, is a ReportEngine object.
Listing 5‑5 Accessing a report document
String dName = "./SimpleReport.rptdocument";
IReportDocument doc = null;
try {
doc = engine.openReportDocument( dName );
} catch ( EngineException e ) {
System.err.println( "Document " + dName + " not found!" );
return;
}
// Get the second bookmark in the document.
java.util.List bookmarks = doc.getBookmarks( );
String bookmark = ( String ) bookmarks.get( 1 );
long pageNumber = doc.getPageNumber( bookmark );
logger.log(Level.INFO, bookmark + " Page number: " + pageNumber);
// Close the document.
doc.close( );