Task 8:  
When you create a Java class using the wizard, Eclipse generates a code template, as shown in Figure 18-32. As the class declaration shows, the XMLGenerator class implements the IDataExtractionExtension interface. The interface defines three methods, which your class must implement. Perform the following tasks in this section:
Import the required packages
The code template contains import statements to include the packages that contain the classes your code needs. Verify that the code contains the following import statements. If any are missing, add them.
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.report.engine.api.IDataExtractionOption;
import org.eclipse.birt.report.engine.api.IDataIterator;
import org.eclipse.birt.report.engine.api.IExtractionResults;
import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.extension.IDataExtractionExtension;
Implement the initialize( ) method
The initialize( ) method is the first method that the BIRT report engine calls before rendering the Flash object. Use this method to initialize resources.
private IDataExtractionOption option;
This statement declares a private variable, option, of type IDataExtractionOption. The initialize( ) method takes an input argument of this type.
this.option = arg1;
This statement assigns the option variable to the input argument arg1.
Listing 18-3 shows the edited code in the class definition and initialize( ) method.
Listing 18-3  
Class definition and initialize( ) method implementation
public class XMLGenerator implements IDataExtractionExtension {
  private IDataExtractionOption option;
  public void initialize(IReportContext arg0, IDataExtractionOption arg1)
      throws BirtException {
      this.option = arg1;
Implement the output( ) method
The output( ) method is where you write the code to build the XML data to pass to the Flash chart. Listing 18-4 shows the code to write to replace the output( ) code stub. Some of the XML strings that define chart attributes are long. You must type each string in a single line. Each string ends with a semicolon (;).
You can copy the code from the online help topic with the same title as this section title. In the Actuate BIRT Guide online help, choose Using the Flash object library—Tutorial 4: Creating a Flash chart that gets data through the data URL variable—Implement methods in the class.
Read the comments embedded in the code to understand what each section of code does. For information about the XML elements and attributes used to build the XML data, see the sample XML for the 2D dual-Y combination chart. In the online help for Flash charts, choose Chart XML API—Combination Charts—2D Dual Y Combination.
Listing 18-4  
output( ) method implementation
public void output(IExtractionResults results) throws BirtException {
  //Get the handle of the OutputStream defined in the
  //IDataExtractionOption option interface
  OutputStream stream = option.getOutputStream();
  //If the stream is not null, define three string buffers.
  //The xml buffer is used to build the full XML.
  //The xmlSales and xmlQty buffers store the data for the
  //Revenue and Quantity series.
  if ( stream != null )
    StringBuffer xml = new StringBuffer();
    StringBuffer xmlSales = new StringBuffer();
    StringBuffer xmlQty = new StringBuffer();
  //Start building the XML. This part defines chart attributes.
  //Type this XML string in a single line.
      xml.append( "<chart caption='Revenue by Country' PYAxisName='Revenue' SYAxisName='Quantity' numVisiblePlot='8' showValues='0' numberPrefix='$' useRoundEdges='1' labelDisplay='ROTATE' >");
    //If results is not null, iterate through the data set rows.
    //Add the values to the data series. Add the data and
    //additional formatting attributes to the XML.
    //Type each appended XML string in a single line.
    if ( results != null )
      IDataIterator itr = results.nextResultIterator();
      xml.append( "<categories >" );
      xmlSales.append("<dataset seriesName='Revenue' >");
      xmlQty.append("<dataset seriesName='Quantity' parentYAxis='S' >");
      while ( itr.next() )
        String country = String.valueOf(itr.getValue("COUNTRY"));
        String sales = String.valueOf(itr.getValue("SALES"));
        String qty = String.valueOf(itr.getValue("QUANTITY"));
        xml.append( "<category label='"+ country + "' />" );
        xmlSales.append( "<set value='"+ sales + "' />" );
        xmlQty.append( "<set value='"+ qty + "' />" );
      xml.append( "</categories>" );
      xml.append(" <line startValue='400000' color='91C728' displayValue='Target' showOnTop='1'/> ");
      xml.append("<styles> ");
      xml.append("<definition> <style name='CanvasAnim' type='animation' param='_xScale' start='0' duration='1' /> </definition> ");
      xml.append(" <application> <apply toObject='Canvas' styles='CanvasAnim' /> </application> ");
      //Write the buffer to the output stream.
      //Use the try/catch blocks to catch exceptions.
        stream.write( xml.toString().getBytes("UTF-8"));
      catch ( UnsupportedEncodingException e )
      catch ( IOException e )
Implement the release( ) method
Use the release( ) method to clean up allocated resources.
this.option = null;
This statement releases the handle to the output stream.

Additional Links:

Copyright Actuate Corporation 2012