编译设计(Compiling Design)

优质
小牛编辑
141浏览
2023-12-01

我们在前一章中生成了JasperReport模板(JRXML文件)。 此文件不能直接用于生成报告。 它必须编译为JasperReport的本机二进制格式,称为Jasper文件。 在编译时,我们将JasperDesign对象转换为JasperReport对象 -

Jasper报告编译

接口net.sf.jasperreports.engine.design.JRCompiler在编译期间起着核心作用。 此接口具有多种实现,具体取决于报表表达式使用的语言,只要编译器实现可以在运行时对其进行评估,就可以使用Java,Groovy,JavaScript或任何其他脚本语言编写。

我们可以通过以下两种方式编译JRXML文件 -

  • 程序化编译。
  • 通过ANT任务编译。

JRXML的程序化编译

JasperReports API提供了一个外观类net.sf.jasperreports.engine.JasperCompileManager用于编译JasperReport。 该类包含几个用于编译报告模板的公共静态方法。 模板的来源可以是文件,输入流和/或存储器对象。

JRXML文件(jasper_report_template.jrxml)的内容如下所示。 它保存在目录C:\tools\jasperreports-5.0.1\test -

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
   "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport xmlns = "http://jasperreports.sourceforge.net/jasperreports"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://jasperreports.sourceforge.net/jasperreports
   http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
   name = "jasper_report_template" language = "groovy" pageWidth = "595"
   pageHeight = "842" columnWidth = "555" leftMargin = "20" rightMargin = "20"
   topMargin = "20" bottomMargin = "20">
   <queryString>
      <![CDATA[]]>
   </queryString>
   <field name = "country" class = "java.lang.String">
      <fieldDescription><![CDATA[country]]></fieldDescription>
   </field>
   <field name = "name" class = "java.lang.String">
      <fieldDescription><![CDATA[name]]></fieldDescription>
   </field>
   <columnHeader>
      <band height = "23">
         <staticText>
            <reportElement mode = "Opaque" x = "0" y = "3" 
               width = "535" height = "15" backcolor = "#70A9A9" />
            <box>
               <bottomPen lineWidth = "1.0" lineColor = "#CCCCCC" />
            </box>
            <textElement />
            <text><![CDATA[]]> </text>
         </staticText>
         <staticText>
            <reportElement x = "414" y = "3" width = "121" height = "15" />
            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font isBold = "true" />
            </textElement>
            <text><![CDATA[Country]]></text>
         </staticText>
         <staticText>
            <reportElement x = "0" y = "3" width = "136" height = "15" />
            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font isBold = "true" />
            </textElement>
            <text><![CDATA[Name]]></text>
         </staticText>
      </band>
   </columnHeader>
   <detail>
      <band height = "16">
         <staticText>
            <reportElement mode = "Opaque" x = "0" y = "0" 
               width = "535" height = "14" backcolor = "#E5ECF9" />
            <box>
               <bottomPen lineWidth = "0.25" lineColor = "#CCCCCC" />
            </box>
            <textElement />
            <text><![CDATA[]]> </text>
         </staticText>
         <textField>
            <reportElement x = "414" y = "0" width = "121" height = "15" />
            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font size = "9" />
            </textElement>
            <textFieldExpression class = "java.lang.String">
               <![CDATA[$F{country}]]>
            </textFieldExpression>
         </textField>
         <textField>
            <reportElement x = "0" y = "0" width = "136" height = "15" />
            <textElement textAlignment = "Center" verticalAlignment = "Middle" />
            <textFieldExpression class = "java.lang.String">
               <![CDATA[$F{name}]]>
            </textFieldExpression>
         </textField>
      </band>
   </detail>
</jasperReport>

以下代码演示了上述jasper_report_template.jrxml文件的编译。

package cn.xnip;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
public class JasperReportCompile {
   public static void main(String[] args) {
      String sourceFileName = "C://tools/jasperreports-5.0.1/test" + 
         "/jasper_report_template.jrxml";
      System.out.println("Compiling Report Design ...");
      try {
          /**
          * Compile the report to a file name same as
          * the JRXML file name
          */
         JasperCompileManager.compileReportToFile(sourceFileName);
      } catch (JRException e) {
         e.printStackTrace();
      }
      System.out.println("Done compiling!!! ...");
   }
}

模板编译

下一步,让我们将以上内容保存在文件C:\tools\jasperreports-5.0.1\test\src\com\xnip\JasperReportCompile.java并在build.xml文件中导入baseBuild.xml ,如下所示。 baseBuild.xml已经有了compilerun目标 -

<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportTest" default = "run" basedir = ".">
   <import file = "baseBuild.xml"/>
</project>

接下来,让我们打开命令行窗口并转到build.xml所在的目录。 最后,执行命令ant -Dmain-class = cn.xnip.JasperReportCompile as -

C:\tools\jasperreports-5.0.1\test>ant -Dmain-class = cn.xnip.JasperReportCompile
Buildfile: C:\tools\jasperreports-5.0.1\test\build.xml
compile:
   [javac] C:\tools\jasperreports-5.0.1\test\baseBuild.xml:27:
   warning: 'includeantruntime' was not set, defaulting to
   build.sysclasspath=last;set to false for repeatable builds
   [javac] Compiling 1 source file to C:\tools\jasperreports-5.0.1\test\classes
run:
   [echo] Runnin class : cn.xnip.JasperReportCompile
   [java] Compiling Report Design ...
   [java] log4j:WARN No appenders could be found for logger
   (net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
   [java] log4j:WARN Please initialize the log4j system properly.
   [java] Done compiling!!! ...
BUILD SUCCESSFUL
Total time: 8 seconds

作为上述编译的结果,您将看到模板文件jasper_report_template.jasper在C:\tools\jasperreports-5.0.1\test目录中生成。

预览编译的报告模板

net.sf.jasperreports.view.JasperDesignViewer可用于预览已编译的报告模板和JRXML模板。

为了进一步移动,我们将新的目标viewDesign添加到上面的build.xml文件中,这将允许我们预览已编译的报告。 下面是修改后的build.xml -

导入文件 - baseBuild.xml是从环境设置一章中挑选出来的,应该与build.xml放在同一目录中。

<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportTest" default = "viewDesign" basedir = ".">
   <import file = "baseBuild.xml" />
   <target name = "viewDesign" description="Design viewer is launched 
      to preview the compiled report design.">
      <java classname = "net.sf.jasperreports.view.JasperDesignViewer" fork = "true">
         <arg value = "-F${file.name}.jasper" />
         <classpath refid = "classpath" />
      </java>
   </target>
</project>

让我们在命令提示符下执行命令 - ant (viewDesign是默认目标)。 JasperDesignViewer窗口打开,显示Jasper文件,如下所示 -

Jasper设计查看器

通过ANT任务编译

由于报表模板编译更像是设计时作业而不是运行时作业,因此JasperReport库具有自定义ANT任务。 对于某些情况,在运行时创建JRXML文件时,我们无法使用此ANT任务。 自定义ANT任务称为JRC,由类net.sf.jasperreports.ant.JRAntCompileTask 。 它的语法和行为与内置的《javac》 ANT任务非常相似。

模板编译

让我们在现有的build.xml中添加新的目标compilereportdesing 。 这里,使用带有文件集的嵌套“src”标记指定源文件夹。 嵌套的源标记允许编译分散在许多不同位置的报表模板,而不是在单个根报表源文件夹下分组。 下面是修改后的build.xml -

<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportTest" default = "compilereportdesing" basedir = ".">
   <import file = "baseBuild.xml" />
   <target name = "viewDesign" description = "Design viewer is 
      launched to preview the compiled report design.">
      <java classname = "net.sf.jasperreports.view.JasperDesignViewer" fork = "true">
         <arg value = "-F${file.name}.jasper" />
         <classpath refid = "classpath" />
      </java>
   </target>
   <target name = "compilereportdesing" description = "Compiles the 
      JXML file and produces the .jasper file.">
      <taskdef name = "jrc" classname = "net.sf.jasperreports.ant.JRAntCompileTask">
         <classpath refid = "classpath" />
      </taskdef>
      <jrc destdir = ".">
         <src>
            <fileset dir = ".">
               <include name = "*.jrxml" />
            </fileset>
         </src>
         <classpath refid = "classpath" />
      </jrc>
   </target>
</project>

接下来,让我们打开命令提示符并转到build.xml所在的目录。 执行命令ant (compilereportdesing是默认目标); 输出如下 -

C:\tools\jasperreports-5.0.1\test>ant
Buildfile: C:\tools\jasperreports-5.0.1\test\build.xml
compilereportdesing:
   [jrc] Compiling 1 report design files.
   [jrc] log4j:WARN No appenders could be found for logger
   (net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
   [jrc] log4j:WARN Please initialize the log4j system properly.
   [jrc] log4j:WARN See
   http://logging.apache.org/log4j/1.2/faq.html#noconfig
   for more info.
   [jrc] File :
   C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml ... OK.
BUILD SUCCESSFUL
Total time: 5 seconds

文件jasper_report_template.jasper是在文件系统中生成的(在我们的例子中是C:\tools\jasperreports-5.0.1\test目录)。 此文件与通过调用net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile()以编程方式生成的文件相同。 我们可以预览这个jasper文件,执行ant viewDesign