Posted on 2008-01-23 13:54 
Raul Gong 阅读(2811) 
评论(13)  编辑  收藏  所属分类: 
eclipse 、
birt 
			 
			
		 
		首先,本文讲的方法适合的环境是:使用xml作为birt的数据源,但不同场合需要不同的xml文件,但制作.rptdesign文件时只运行输入一个xml文件,怎样实现动态数据源呢?以下将解决这个问题。
同时,本文所讲的方法,可以稍加改造,适合所有需要管理器向.rptdesign文件传入参数的情况。例如,你不仅需要动态数据源,还可以需要动态的报表标题、某处的文字、动态的参数、等等。
在这里,我只说思路代码,具体细节,见我提供的实例工程。
这里,我以插件工程为例。首先,建立一个工程,然后是建一个.rptdesign,在其中的数据源,选xml,然后使用如下的xml文件的路径:
 <ROOT>
<ROOT>
 <FACTOR NAME="Maintainability">
    <FACTOR NAME="Maintainability">
 <PECENT NAME="Execllent" VALUE="0.00"/>
      <PECENT NAME="Execllent" VALUE="0.00"/>
 <PECENT NAME="Good" VALUE="0.75"/>
      <PECENT NAME="Good" VALUE="0.75"/>
 <PECENT NAME="Fair" VALUE="0.25"/>
      <PECENT NAME="Fair" VALUE="0.25"/>
 <PECENT NAME="Poor" VALUE="0.00"/>
      <PECENT NAME="Poor" VALUE="0.00"/>
 </FACTOR>
    </FACTOR>
 </ROOT>
</ROOT> 
注意在outline视图中选中 Data Source ,然后在属性窗口中选中 Event Handler ,在里面填入类的地址:
net.rual.learn.eventhandler.FunTableHandler
然后在相应位置建立这个FunTableHandler类,继承至DataSourceEventAdapter类,覆盖beforeOpen方法,如下:
 
 package net.raul.lern.eventhandler;
package net.raul.lern.eventhandler;

 import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.IReportContext;
 import org.eclipse.birt.report.engine.api.script.eventadapter.DataSourceEventAdapter;
import org.eclipse.birt.report.engine.api.script.eventadapter.DataSourceEventAdapter;
 import org.eclipse.birt.report.engine.api.script.instance.IDataSourceInstance;
import org.eclipse.birt.report.engine.api.script.instance.IDataSourceInstance;


 /** *//**
/** *//**
 * @author Vincent
 * @author Vincent
 *
 * 
 */
 */

 public class FunTableHandler extends DataSourceEventAdapter
public class FunTableHandler extends DataSourceEventAdapter  {
{


 @Override
    @Override
 public void beforeOpen(IDataSourceInstance dataSource,
    public void beforeOpen(IDataSourceInstance dataSource,

 IReportContext reportContext)
            IReportContext reportContext)  {
{
 // TODO Auto-generated method stub
        // TODO Auto-generated method stub

 String xmlFile = (String) reportContext.getReportRunnable()
        String xmlFile = (String) reportContext.getReportRunnable()
 .getReportEngine().getConfig().getProperty("user.datasource");
                .getReportEngine().getConfig().getProperty("user.datasource");
 // xmlFile =
        // xmlFile =
 // "E:/c.xml";
        // "E:/c.xml";
 dataSource.setExtensionProperty("FILELIST", xmlFile);
        dataSource.setExtensionProperty("FILELIST", xmlFile);
 super.beforeOpen(dataSource, reportContext);
        super.beforeOpen(dataSource, reportContext);
 }
    }

 }
}
然后编写管理器类:
 public String executeReport(String repPath, String xmlFilePath,
    public String executeReport(String repPath, String xmlFilePath,

 String FileName, String FunName) throws EngineException
            String FileName, String FunName) throws EngineException  {
{
 String outPutFilePath = proPath + "/report/" + FunName + "_"
        String outPutFilePath = proPath + "/report/" + FunName + "_"
 + FileName.substring(0, FileName.length() - 4) + ".html";
                + FileName.substring(0, FileName.length() - 4) + ".html";

 // Engine Configuration - set and get temp dir, BIRT home, Servlet
        // Engine Configuration - set and get temp dir, BIRT home, Servlet
 // context
        // context
 EngineConfig config = new EngineConfig();
        EngineConfig config = new EngineConfig();
 // config.setEngineHome(
        // config.setEngineHome(
 // "E:/work/eclipses/eclipse4birt/birt-runtime-2_2_1_1/ReportEngine" );
        // "E:/work/eclipses/eclipse4birt/birt-runtime-2_2_1_1/ReportEngine" );
 config.setEngineHome(getEnvirStr("BIRT_HOME") + "/ReportEngine");
        config.setEngineHome(getEnvirStr("BIRT_HOME") + "/ReportEngine");
 
        
 config.setProperty("user.projectclasspath",
        config.setProperty("user.projectclasspath",
 "E:/work/workspaces/kaudit/kaudit.071224/com.zte.audit.ui/bin");
                "E:/work/workspaces/kaudit/kaudit.071224/com.zte.audit.ui/bin");
 config.setProperty("user.datasource", xmlFilePath);
        config.setProperty("user.datasource", xmlFilePath);
 // config.setProperty("user.funname", "main");
        // config.setProperty("user.funname", "main");
 // Create the report engine
        // Create the report engine
 ReportEngine engine = new ReportEngine(config);
        ReportEngine engine = new ReportEngine(config);

 // Open a report design - use design to modify design, retrieve embedded
        // Open a report design - use design to modify design, retrieve embedded
 // images etc.
        // images etc.
 IReportRunnable design = engine.openReportDesign(repPath);
        IReportRunnable design = engine.openReportDesign(repPath);
 // Create task to run the report - use the task to execute and run the
        // Create task to run the report - use the task to execute and run the
 // report,
        // report,
 IRunAndRenderTask task = engine.createRunAndRenderTask(design);
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);
 task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,
        task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,
 FunTableHandler.class.getClassLoader());
                FunTableHandler.class.getClassLoader());
 // IReportRunnable runnable = engine.openReportDesign( source );
        // IReportRunnable runnable = engine.openReportDesign( source );
 // Set Render context to handle url and image locataions
        // Set Render context to handle url and image locataions
 HTMLRenderContext renderContext = new HTMLRenderContext();
        HTMLRenderContext renderContext = new HTMLRenderContext();
 renderContext.setImageDirectory("image");
        renderContext.setImageDirectory("image");
 HashMap contextMap = new HashMap();
        HashMap contextMap = new HashMap();
 contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
        contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
 renderContext);
                renderContext);
 // contextMap
        // contextMap
 // .put(EngineConstants.PROJECT_CLASSPATH_KEY,
        // .put(EngineConstants.PROJECT_CLASSPATH_KEY,
 // "E:/work/workspaces/kaudit/kaudit.071224/com.zte.audit.ui/bin");
        // "E:/work/workspaces/kaudit/kaudit.071224/com.zte.audit.ui/bin");
 task.setAppContext(contextMap);
        task.setAppContext(contextMap);

 // Set rendering options - such as file or stream output,
        // Set rendering options - such as file or stream output,
 // output format, whether it is embeddable, etc
        // output format, whether it is embeddable, etc
 HTMLRenderOption options = new HTMLRenderOption();
        HTMLRenderOption options = new HTMLRenderOption();
 // options.setOutputFileName("D:/customer.html");
        // options.setOutputFileName("D:/customer.html");
 options.setOutputFileName(outPutFilePath);
        options.setOutputFileName(outPutFilePath);
 options.setOutputFormat("html");
        options.setOutputFormat("html");
 task.setRenderOption(options);
        task.setRenderOption(options);
 // run the report and destroy the engine
        // run the report and destroy the engine
 task.run();
        task.run();
 // task.addScriptableJavaObject(arg0, arg1)
        // task.addScriptableJavaObject(arg0, arg1)
 engine.destroy();
        engine.destroy();

 return outPutFilePath;
        return outPutFilePath;
 }
    }


 public static String getEnvirStr(String name)
    public static String getEnvirStr(String name)  {
{
 // System.getenv("BIRT_HOME");
        // System.getenv("BIRT_HOME");
 return System.getenv(name);
        return System.getenv(name);
 }
    }

因为时间很紧,我没有办法写得很详细,等过年放假的时候,我好好整理,再放上来。