Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » BIRT API, html output format, images output as custom1, custom2, etc. - change?
BIRT API, html output format, images output as custom1, custom2, etc. - change? [message #513415] Wed, 10 February 2010 06:51 Go to next message
Doug  is currently offline Doug Friend
Messages: 5
Registered: October 2009
Junior Member
My google-fu has failed me.

I'm trying to figure out how to set the filenames of images generated via the BIRT API from charts to something other than "custom1.png", "custom2.png" etc.

I'm using the IRunAndRender task to run and render a .rptdesign file. The report file contains several charts that I simply want rendered as .png files. This works fine, but I want to choose the files' names - currently as mentioned they are automatically named custom1.png etc.

Possibly relevant code:

IReportRunnable design = BirtEngine.getBirtEngine().openReportDesign(path-to-.rptdesign-file);
IRunAndRenderTask task = BirtEngine.getBirtEngine().createRunAndRenderTask(design);;
HTMLRenderOption options = new HTMLRenderOption();

options.setOutputFormat("html");
options.setOutputFileName(some-directory);			
options.setImageDirectory(some-other-directory);
options.setSupportedImageFormats("PNG");
options.setEmbeddable(true);
task.setRenderOption(options);


Running this code results in a set of perfectly rendered PNG images, with only the problem that they are named as mentioned above.

Can someone tell me how to get the engine to respect the name associated with each chart in the .rptdesign file, or otherwise set the filename of each image?

Cheers!
Re: BIRT API, html output format, images output as custom1, custom2, etc. - change? [message #513655 is a reply to message #513415] Wed, 10 February 2010 13:30 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Doug,

You need to implement an image hander. Extend the HTMLImageHandler
class and use the following render option:

HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFileName("output/resample/top.html");
options.setOutputFormat("HTML");
options.setImageHandler(new MyImageHandler());

This is an older example of an image handler (look for custom)



package REAPI;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.birt.report.engine.api.HTMLImageHandler;
import org.eclipse.birt.report.engine.api.IImage;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;

import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;

import org.eclipse.birt.report.engine.api.EngineException;


import org.eclipse.birt.report.engine.api.script.IReportContext;


import org.eclipse.birt.report.engine.i18n.MessageConstants;

/**
* Default implementation for writing images in a form that is used in a
* web-application.
*/
public class MyImageHandler extends HTMLImageHandler
{

private String handlerId;
private int count = 0;

private static HashMap map = new HashMap( );

/**
* dummy constructor
*/
public MyImageHandler( )
{
String codePart = Integer.toHexString( this.hashCode( ) );
String timePart = Long.toHexString( System.currentTimeMillis( ) );
handlerId = codePart + timePart;
}

public String onDesignImage( IImage image, Object context )
{
return handleImage( image, context, "design", true ); //$NON-NLS-1$
}


public String onDocImage( IImage image, Object context )
{
// TODO Auto-generated method stub
return null;
}

public String onURLImage( IImage image, Object context )
{
assert ( image != null );
String uri = image.getID( );
if (uri.startsWith( "http:" ) || uri.startsWith( "https:" ))
{
return uri;
}
return handleImage( image, context, "uri", true ); //$NON-NLS-1$
}


public String onCustomImage( IImage image, Object context )
{
return handleImage( image, context, "custom", false ); //$NON-NLS-1$
}

/**
* returns a unique file name based on a directory and name prefix
*
* @param imageDir
* directory to store the image
* @param prefix
* prefix for the file name
* @return a file name
*/
protected String createUniqueFileName( String imageDir, String prefix )
{
return createUniqueFileName( imageDir, prefix, null );
}

/**
* creates a unique tempoary file to store an image
*
* @param imageDir
* directory to put image into
* @param prefix
* file name prefix
* @param postfix
* file name postfix
* @return a Java File Object
*/
protected String createUniqueFileName( String imageDir, String prefix,
String postfix )
{
File file = null;
postfix = ( postfix == null ? "" : postfix );
String uniCount = null;
do
{
uniCount = genUniqueCount( );
file = new File( imageDir + "/" + prefix + uniCount + postfix );
//$NON-NLS-1$
} while ( file.exists( ) );

return prefix + uniCount + postfix;
}

/**
* Generating unique string which contains following parts
* <li> the hashcode of the image handler
* <li> creation time of the image handler
* <li> image count created by the image handler
* @return return the unique count for filename
*/
synchronized private String genUniqueCount( )
{
count++;
return handlerId + count;
}


public String onFileImage( IImage image, Object context )
{
return handleImage( image, context, "file", true ); //$NON-NLS-1$
}

/**
* handles an image report item and returns an image URL
*
* @param image
* represents the image design information
* @param context
* context information
* @param prefix
* image prefix in URL
* @param needMap
* whether image map is needed
* @return URL for the image
*/
protected String handleImage( IImage image, Object context, String prefix,
boolean needMap )
{
String mapID = null;
if ( needMap )
{
mapID = getImageMapID( image );
if ( map.containsKey( mapID ) )
{
return (String) map.get( mapID );
}
}
String ret = null;
//if ( context != null && ( context instanceof HTMLRenderOption ) )
if ( context != null )
{
HTMLRenderContext myContext = (HTMLRenderContext) context;

//IRenderOption rotest = image.getRenderOption();

//HTMLRenderOption ro = (HTMLRenderOption)image.getRenderOption();
String imageURL = myContext.getBaseImageURL( );
String imageDir = myContext.getImageDirectory( );
if ( imageURL == null || imageURL.length( ) == 0
|| imageDir == null || imageDir.length( ) == 0 )
{
System.out.println("Error in image settings");
return null;
}

String fileName;
File file;
String extension = image.getExtension( );
if ( extension != null && extension.length( ) > 0 )
{
fileName = createUniqueFileName( imageDir, prefix, extension );
//$NON-NLS-1$
}
else
{
fileName = createUniqueFileName( imageDir, prefix );
}
file = new File( imageDir, fileName ); //$NON-NLS-1$
try
{
image.writeImage( file );
}
catch ( IOException e )
{
e.printStackTrace();
}
// servlet mode
if ( imageURL.indexOf( "?" ) > 0 ) //$NON-NLS-1$
{
ret = imageURL + fileName + "&specialid=test";
}
else if ( imageURL.endsWith( "/" ) ) //$NON-NLS-1$
{
ret = imageURL + fileName;
}
else
{
ret = imageURL + "/" + fileName; //$NON-NLS-1$
}

if ( needMap )
{
map.put( mapID, ret );
}

}
else
{
ret = handleTempImage( image, prefix, needMap );
}

return ret;
}

protected String handleTempImage( IImage image, String prefix,
boolean needMap )
{
try
{

File imageFile = File.createTempFile( prefix, ".img" );
image.writeImage( imageFile );
String fileName = imageFile.getAbsolutePath( );
if ( needMap )
{
String mapID = getImageMapID( image );
map.put( mapID, fileName );
}
return fileName;
}
catch ( IOException e )
{
e.printStackTrace();
}
return "unknow.img";
}

/**
* returns the unique identifier for the image
*
* @param image
* the image object
* @return the image id
*/
protected String getImageMapID( IImage image )
{
if ( image.getReportRunnable( ) != null )
{
return image.getReportRunnable( ).hashCode( ) + image.getID( );
}
return image.getID( );
}


}


Jason

Doug wrote:
> My google-fu has failed me.
>
> I'm trying to figure out how to set the filenames of images generated
> via the BIRT API from charts to something other than "custom1.png",
> "custom2.png" etc.
>
> I'm using the IRunAndRender task to run and render a .rptdesign file.
> The report file contains several charts that I simply want rendered as
> .png files. This works fine, but I want to choose the files' names -
> currently as mentioned they are automatically named custom1.png etc.
>
> Possibly relevant code:
>
>
> IReportRunnable design =
> BirtEngine.getBirtEngine().openReportDesign(path-to-.rptdesi gn-file);
> IRunAndRenderTask task =
> BirtEngine.getBirtEngine().createRunAndRenderTask(design);;
> HTMLRenderOption options = new HTMLRenderOption();
>
> options.setOutputFormat("html");
> options.setOutputFileName(some-directory);
> options.setImageDirectory(some-other-directory);
> options.setSupportedImageFormats("PNG");
> options.setEmbeddable(true);
> task.setRenderOption(options);
>
>
> Running this code results in a set of perfectly rendered PNG images,
> with only the problem that they are named as mentioned above.
>
> Can someone tell me how to get the engine to respect the name associated
> with each chart in the .rptdesign file, or otherwise set the filename of
> each image?
>
> Cheers!
Re: BIRT API, html output format, images output as custom1, custom2, etc. - change? [message #513656 is a reply to message #513655] Wed, 10 February 2010 18:30 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

BTW even a master misses the fly occasionally

Jason Weathersby wrote:
> Doug,
>
> You need to implement an image hander. Extend the HTMLImageHandler
> class and use the following render option:
>
> HTMLRenderOption options = new HTMLRenderOption();
> options.setOutputFileName("output/resample/top.html");
> options.setOutputFormat("HTML");
> options.setImageHandler(new MyImageHandler());
>
> This is an older example of an image handler (look for custom)
>
>
>
> package REAPI;
>
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.OutputStream;
> import java.util.HashMap;
> import java.util.logging.Level;
> import java.util.logging.Logger;
> import org.eclipse.birt.report.engine.api.HTMLImageHandler;
> import org.eclipse.birt.report.engine.api.IImage;
> import org.eclipse.birt.report.engine.api.HTMLRenderOption;
> import org.eclipse.birt.report.engine.api.RenderOption;
>
> import org.eclipse.birt.report.engine.api.IRenderOption;
> import org.eclipse.birt.report.engine.api.HTMLRenderContext;
>
> import org.eclipse.birt.report.engine.api.EngineException;
>
>
> import org.eclipse.birt.report.engine.api.script.IReportContext;
>
>
> import org.eclipse.birt.report.engine.i18n.MessageConstants;
>
> /**
> * Default implementation for writing images in a form that is used in a
> * web-application.
> */
> public class MyImageHandler extends HTMLImageHandler
> {
>
> private String handlerId;
> private int count = 0;
>
> private static HashMap map = new HashMap( );
>
> /**
> * dummy constructor
> */
> public MyImageHandler( )
> {
> String codePart = Integer.toHexString( this.hashCode( ) );
> String timePart = Long.toHexString( System.currentTimeMillis( ) );
> handlerId = codePart + timePart;
> }
>
> public String onDesignImage( IImage image, Object context )
> {
> return handleImage( image, context, "design", true ); //$NON-NLS-1$
> }
>
>
> public String onDocImage( IImage image, Object context )
> {
> // TODO Auto-generated method stub
> return null;
> }
>
> public String onURLImage( IImage image, Object context )
> {
> assert ( image != null );
> String uri = image.getID( );
> if (uri.startsWith( "http:" ) || uri.startsWith( "https:" ))
> {
> return uri;
> }
> return handleImage( image, context, "uri", true ); //$NON-NLS-1$
> }
>
>
> public String onCustomImage( IImage image, Object context )
> {
> return handleImage( image, context, "custom", false );
> //$NON-NLS-1$
> }
>
> /**
> * returns a unique file name based on a directory and name prefix
> *
> * @param imageDir
> * directory to store the image
> * @param prefix
> * prefix for the file name
> * @return a file name
> */
> protected String createUniqueFileName( String imageDir, String prefix )
> {
> return createUniqueFileName( imageDir, prefix, null );
> }
>
> /**
> * creates a unique tempoary file to store an image
> *
> * @param imageDir
> * directory to put image into
> * @param prefix
> * file name prefix
> * @param postfix
> * file name postfix
> * @return a Java File Object
> */
> protected String createUniqueFileName( String imageDir, String prefix,
> String postfix )
> {
> File file = null;
> postfix = ( postfix == null ? "" : postfix );
> String uniCount = null;
> do
> {
> uniCount = genUniqueCount( );
> file = new File( imageDir + "/" + prefix + uniCount +
> postfix ); //$NON-NLS-1$
> } while ( file.exists( ) );
>
> return prefix + uniCount + postfix;
> }
>
> /**
> * Generating unique string which contains following parts
> * <li> the hashcode of the image handler
> * <li> creation time of the image handler
> * <li> image count created by the image handler
> * @return return the unique count for filename
> */
> synchronized private String genUniqueCount( )
> {
> count++;
> return handlerId + count;
> }
>
>
> public String onFileImage( IImage image, Object context )
> {
> return handleImage( image, context, "file", true ); //$NON-NLS-1$
> }
>
> /**
> * handles an image report item and returns an image URL
> *
> * @param image
> * represents the image design information
> * @param context
> * context information
> * @param prefix
> * image prefix in URL
> * @param needMap
> * whether image map is needed
> * @return URL for the image
> */
> protected String handleImage( IImage image, Object context, String
> prefix,
> boolean needMap )
> {
> String mapID = null;
> if ( needMap )
> {
> mapID = getImageMapID( image );
> if ( map.containsKey( mapID ) )
> {
> return (String) map.get( mapID );
> }
> }
> String ret = null;
> //if ( context != null && ( context instanceof HTMLRenderOption ) )
> if ( context != null )
> {
> HTMLRenderContext myContext = (HTMLRenderContext) context;
>
> //IRenderOption rotest = image.getRenderOption();
>
> //HTMLRenderOption ro =
> (HTMLRenderOption)image.getRenderOption();
> String imageURL = myContext.getBaseImageURL( );
> String imageDir = myContext.getImageDirectory( );
> if ( imageURL == null || imageURL.length( ) == 0
> || imageDir == null || imageDir.length( ) == 0 )
> {
> System.out.println("Error in image settings");
> return null;
> }
>
> String fileName;
> File file;
> String extension = image.getExtension( );
> if ( extension != null && extension.length( ) > 0 )
> {
> fileName = createUniqueFileName( imageDir, prefix,
> extension ); //$NON-NLS-1$
> }
> else
> {
> fileName = createUniqueFileName( imageDir, prefix );
> }
> file = new File( imageDir, fileName ); //$NON-NLS-1$
> try
> {
> image.writeImage( file );
> }
> catch ( IOException e )
> {
> e.printStackTrace();
> }
> // servlet mode
> if ( imageURL.indexOf( "?" ) > 0 ) //$NON-NLS-1$
> {
> ret = imageURL + fileName + "&specialid=test";
> }
> else if ( imageURL.endsWith( "/" ) ) //$NON-NLS-1$
> {
> ret = imageURL + fileName;
> }
> else
> {
> ret = imageURL + "/" + fileName; //$NON-NLS-1$
> }
>
> if ( needMap )
> {
> map.put( mapID, ret );
> }
>
> }
> else
> {
> ret = handleTempImage( image, prefix, needMap );
> }
>
> return ret;
> }
>
> protected String handleTempImage( IImage image, String prefix,
> boolean needMap )
> {
> try
> {
>
> File imageFile = File.createTempFile( prefix, ".img" );
> image.writeImage( imageFile );
> String fileName = imageFile.getAbsolutePath( );
> if ( needMap )
> {
> String mapID = getImageMapID( image );
> map.put( mapID, fileName );
> }
> return fileName;
> }
> catch ( IOException e )
> {
> e.printStackTrace();
> }
> return "unknow.img";
> }
>
> /**
> * returns the unique identifier for the image
> *
> * @param image
> * the image object
> * @return the image id
> */
> protected String getImageMapID( IImage image )
> {
> if ( image.getReportRunnable( ) != null )
> {
> return image.getReportRunnable( ).hashCode( ) + image.getID( );
> }
> return image.getID( );
> }
>
>
> }
>
>
> Jason
>
> Doug wrote:
>> My google-fu has failed me.
>>
>> I'm trying to figure out how to set the filenames of images generated
>> via the BIRT API from charts to something other than "custom1.png",
>> "custom2.png" etc.
>>
>> I'm using the IRunAndRender task to run and render a .rptdesign file.
>> The report file contains several charts that I simply want rendered as
>> .png files. This works fine, but I want to choose the files' names -
>> currently as mentioned they are automatically named custom1.png etc.
>>
>> Possibly relevant code:
>>
>>
>> IReportRunnable design =
>> BirtEngine.getBirtEngine().openReportDesign(path-to-.rptdesi gn-file);
>> IRunAndRenderTask task =
>> BirtEngine.getBirtEngine().createRunAndRenderTask(design);;
>> HTMLRenderOption options = new HTMLRenderOption();
>>
>> options.setOutputFormat("html");
>> options.setOutputFileName(some-directory);
>> options.setImageDirectory(some-other-directory);
>> options.setSupportedImageFormats("PNG");
>> options.setEmbeddable(true);
>> task.setRenderOption(options);
>>
>>
>> Running this code results in a set of perfectly rendered PNG images,
>> with only the problem that they are named as mentioned above.
>>
>> Can someone tell me how to get the engine to respect the name
>> associated with each chart in the .rptdesign file, or otherwise set
>> the filename of each image?
>>
>> Cheers!
Re: BIRT API, html output format, images output as custom1, custom2, etc. - change? [message #513676 is a reply to message #513655] Wed, 10 February 2010 19:12 Go to previous messageGo to next message
Doug  is currently offline Doug Friend
Messages: 5
Registered: October 2009
Junior Member
Thank you sir that helps a ton.

One additional question:
Is there an easy way to access features of the chart currently being "handled" by the image handler? For example, say I set the NAME of a chart in the report designer to "My_Chart" - is there an easy way from within the image handler to access this NAME and thus easily set the name of the output image to "My_Chart.png"?

Much appreciated!
Re: BIRT API, html output format, images output as custom1, custom2, etc. - change? [message #513889 is a reply to message #513676] Thu, 11 February 2010 15:22 Go to previous message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Doug,

I do not think this is possible currently.

Jason

Doug wrote:
> Thank you sir that helps a ton.
>
> One additional question:
> Is there an easy way to access features of the chart currently being
> "handled" by the image handler? For example, say I set the NAME of a
> chart in the report designer to "My_Chart" - is there an easy way from
> within the image handler to access this NAME and thus easily set the
> name of the output image to "My_Chart.png"?
>
> Much appreciated!
Previous Topic:order a joint data set
Next Topic:BIRT 2.2.1 report error
Goto Forum:
  


Current Time: Thu Apr 25 11:44:23 GMT 2024

Powered by FUDForum. Page generated in 0.03392 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top