Jasper-reports-jasper-compiling-report-design
JasperReports-レポートデザインのコンパイル
前の章でJasperReportテンプレート(JRXMLファイル)を生成しました。 このファイルを直接使用してレポートを生成することはできません。 Jasper ファイルと呼ばれるJasperReportのネイティブバイナリ形式にコンパイルする必要があります。 コンパイル時に、JasperDesignオブジェクトをJasperReportオブジェクトに変換します-
インターフェース_net.sf.jasperreports.engine.design.JRCompiler_は、コンパイル時に中心的な役割を果たします。 このインターフェイスには、レポート式に使用される言語に応じていくつかの実装があり、コンパイラー実装が実行時に評価できる限り、Java、Groovy、JavaScript、またはその他のスクリプト言語で記述できます。
次の2つの方法でJRXMLファイルをコンパイルできます-
- プログラムによるコンパイル。
- ANTタスクによるコンパイル。
JRXMLのプログラムによるコンパイル
JasperReports APIは、JasperReportをコンパイルするためのファサードクラス_net.sf.jasperreports.engine.JasperCompileManager_を提供します。 このクラスは、レポートテンプレートをコンパイルするためのいくつかのパブリックな静的メソッドで構成されています。 テンプレートのソースは、ファイル、入力ストリーム、メモリオブジェクトなどです。
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 com.finddevguides;
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 \ finddevguides \ JasperReportCompile.java に保存し、次のようにbuild.xmlファイルに_baseBuild.xml_をインポートします。 baseBuild.xmlにはすでに compile および run ターゲットがあります-
<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportTest" default = "run" basedir = ".">
<import file = "baseBuild.xml"/>
</project>
次に、コマンドラインウィンドウを開き、build.xmlが配置されているディレクトリに移動します。 最後に、コマンド ant -Dmain-class = com.finddevguides.JasperReportCompile を-として実行します。
C:\tools\jasperreports-5.0.1\test>ant -Dmain-class = com.finddevguides.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 : com.finddevguides.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テンプレートをプレビューできます。
さらに先に進むには、上記のbuild.xmlファイルに新しいターゲット viewDesign を追加して、コンパイルされたレポートをプレビューできるようにします。 以下は、修正されたbuild.xmlです-
インポートファイル-baseBuild.xmlは、チャプターリンク:/jasper_reports/jasper_environment_setup [Environment Setup]から選択され、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ファイルが表示されます-
ANTタスクを介したコンパイル
レポートテンプレートのコンパイルは、実行時ジョブよりも設計時ジョブに似ているため、JasperReportライブラリにはカスタムANTタスクがあります。 特定の状況では、JRXMLファイルが実行時に作成されると、このANTタスクを使用できません。 カスタムANTタスクはJRCと呼ばれ、_net.sf.jasperreports.ant.JRAntCompileTask_クラスによって実装されます。 その構文と動作は、組み込みの <javac> ANTタスクに非常に似ています。
テンプレートのコンパイル
新しいターゲット compilereportdesing を既存のbuild.xmlに追加しましょう。 ここでは、ソースフォルダーは、ネストされた<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/faql#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()を呼び出してプログラムで生成されたファイルと同じです。 ant viewDesign を実行して、このジャスパーファイルをプレビューできます。