Finally added generics to JasperReports API signatures (motivated by JasperReports 5.0)

This commit is contained in:
Juergen Hoeller 2012-11-29 23:15:41 +01:00
parent 230dec8e6f
commit 48f405e8fd
3 changed files with 51 additions and 58 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -117,14 +117,13 @@ public abstract class JasperReportsUtils {
* @param report the <code>JasperReport</code> instance to render * @param report the <code>JasperReport</code> instance to render
* @param parameters the parameters to use for rendering * @param parameters the parameters to use for rendering
* @param writer the <code>Writer</code> to write the rendered report to * @param writer the <code>Writer</code> to write the rendered report to
* @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> or object array
* or object array (converted accordingly), representing the report data to read * (converted accordingly), representing the report data to read fields from
* fields from
* @throws JRException if rendering failed * @throws JRException if rendering failed
* @see #convertReportData * @see #convertReportData
*/ */
public static void renderAsCsv(JasperReport report, Map parameters, Object reportData, Writer writer) public static void renderAsCsv(JasperReport report, Map<String, Object> parameters, Object reportData,
throws JRException { Writer writer) throws JRException {
JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
render(new JRCsvExporter(), print, writer); render(new JRCsvExporter(), print, writer);
@ -136,15 +135,14 @@ public abstract class JasperReportsUtils {
* @param report the <code>JasperReport</code> instance to render * @param report the <code>JasperReport</code> instance to render
* @param parameters the parameters to use for rendering * @param parameters the parameters to use for rendering
* @param writer the <code>Writer</code> to write the rendered report to * @param writer the <code>Writer</code> to write the rendered report to
* @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> or object array
* or object array (converted accordingly), representing the report data to read * (converted accordingly), representing the report data to read fields from
* fields from
* @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters} * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
* @throws JRException if rendering failed * @throws JRException if rendering failed
* @see #convertReportData * @see #convertReportData
*/ */
public static void renderAsCsv(JasperReport report, Map parameters, Object reportData, Writer writer, public static void renderAsCsv(JasperReport report, Map<String, Object> parameters, Object reportData,
Map exporterParameters) throws JRException { Writer writer, Map<JRExporterParameter, Object> exporterParameters) throws JRException {
JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
JRCsvExporter exporter = new JRCsvExporter(); JRCsvExporter exporter = new JRCsvExporter();
@ -158,14 +156,13 @@ public abstract class JasperReportsUtils {
* @param report the <code>JasperReport</code> instance to render * @param report the <code>JasperReport</code> instance to render
* @param parameters the parameters to use for rendering * @param parameters the parameters to use for rendering
* @param writer the <code>Writer</code> to write the rendered report to * @param writer the <code>Writer</code> to write the rendered report to
* @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> or object array
* or object array (converted accordingly), representing the report data to read * (converted accordingly), representing the report data to read fields from
* fields from
* @throws JRException if rendering failed * @throws JRException if rendering failed
* @see #convertReportData * @see #convertReportData
*/ */
public static void renderAsHtml(JasperReport report, Map parameters, Object reportData, Writer writer) public static void renderAsHtml(JasperReport report, Map<String, Object> parameters, Object reportData,
throws JRException { Writer writer) throws JRException {
JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
render(new JRHtmlExporter(), print, writer); render(new JRHtmlExporter(), print, writer);
@ -177,15 +174,14 @@ public abstract class JasperReportsUtils {
* @param report the <code>JasperReport</code> instance to render * @param report the <code>JasperReport</code> instance to render
* @param parameters the parameters to use for rendering * @param parameters the parameters to use for rendering
* @param writer the <code>Writer</code> to write the rendered report to * @param writer the <code>Writer</code> to write the rendered report to
* @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> or object array
* or object array (converted accordingly), representing the report data to read * (converted accordingly), representing the report data to read fields from
* fields from
* @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters} * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
* @throws JRException if rendering failed * @throws JRException if rendering failed
* @see #convertReportData * @see #convertReportData
*/ */
public static void renderAsHtml(JasperReport report, Map parameters, Object reportData, Writer writer, public static void renderAsHtml(JasperReport report, Map<String, Object> parameters, Object reportData,
Map exporterParameters) throws JRException { Writer writer, Map<JRExporterParameter, Object> exporterParameters) throws JRException {
JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
JRHtmlExporter exporter = new JRHtmlExporter(); JRHtmlExporter exporter = new JRHtmlExporter();
@ -199,14 +195,13 @@ public abstract class JasperReportsUtils {
* @param report the <code>JasperReport</code> instance to render * @param report the <code>JasperReport</code> instance to render
* @param parameters the parameters to use for rendering * @param parameters the parameters to use for rendering
* @param stream the <code>OutputStream</code> to write the rendered report to * @param stream the <code>OutputStream</code> to write the rendered report to
* @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> or object array
* or object array (converted accordingly), representing the report data to read * (converted accordingly), representing the report data to read fields from
* fields from
* @throws JRException if rendering failed * @throws JRException if rendering failed
* @see #convertReportData * @see #convertReportData
*/ */
public static void renderAsPdf(JasperReport report, Map parameters, Object reportData, OutputStream stream) public static void renderAsPdf(JasperReport report, Map<String, Object> parameters, Object reportData,
throws JRException { OutputStream stream) throws JRException {
JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
render(new JRPdfExporter(), print, stream); render(new JRPdfExporter(), print, stream);
@ -218,15 +213,14 @@ public abstract class JasperReportsUtils {
* @param report the <code>JasperReport</code> instance to render * @param report the <code>JasperReport</code> instance to render
* @param parameters the parameters to use for rendering * @param parameters the parameters to use for rendering
* @param stream the <code>OutputStream</code> to write the rendered report to * @param stream the <code>OutputStream</code> to write the rendered report to
* @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> or object array
* or object array (converted accordingly), representing the report data to read * (converted accordingly), representing the report data to read fields from
* fields from
* @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters} * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
* @throws JRException if rendering failed * @throws JRException if rendering failed
* @see #convertReportData * @see #convertReportData
*/ */
public static void renderAsPdf(JasperReport report, Map parameters, Object reportData, OutputStream stream, public static void renderAsPdf(JasperReport report, Map<String, Object> parameters, Object reportData,
Map exporterParameters) throws JRException { OutputStream stream, Map<JRExporterParameter, Object> exporterParameters) throws JRException {
JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
JRPdfExporter exporter = new JRPdfExporter(); JRPdfExporter exporter = new JRPdfExporter();
@ -240,14 +234,13 @@ public abstract class JasperReportsUtils {
* @param report the <code>JasperReport</code> instance to render * @param report the <code>JasperReport</code> instance to render
* @param parameters the parameters to use for rendering * @param parameters the parameters to use for rendering
* @param stream the <code>OutputStream</code> to write the rendered report to * @param stream the <code>OutputStream</code> to write the rendered report to
* @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> or object array
* or object array (converted accordingly), representing the report data to read * (converted accordingly), representing the report data to read fields from
* fields from
* @throws JRException if rendering failed * @throws JRException if rendering failed
* @see #convertReportData * @see #convertReportData
*/ */
public static void renderAsXls(JasperReport report, Map parameters, Object reportData, OutputStream stream) public static void renderAsXls(JasperReport report, Map<String, Object> parameters, Object reportData,
throws JRException { OutputStream stream) throws JRException {
JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
render(new JRXlsExporter(), print, stream); render(new JRXlsExporter(), print, stream);
@ -259,15 +252,14 @@ public abstract class JasperReportsUtils {
* @param report the <code>JasperReport</code> instance to render * @param report the <code>JasperReport</code> instance to render
* @param parameters the parameters to use for rendering * @param parameters the parameters to use for rendering
* @param stream the <code>OutputStream</code> to write the rendered report to * @param stream the <code>OutputStream</code> to write the rendered report to
* @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code> or object array
* or object array (converted accordingly), representing the report data to read * (converted accordingly), representing the report data to read fields from
* fields from
* @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters} * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
* @throws JRException if rendering failed * @throws JRException if rendering failed
* @see #convertReportData * @see #convertReportData
*/ */
public static void renderAsXls(JasperReport report, Map parameters, Object reportData, OutputStream stream, public static void renderAsXls(JasperReport report, Map<String, Object> parameters, Object reportData,
Map exporterParameters) throws JRException { OutputStream stream, Map<JRExporterParameter, Object> exporterParameters) throws JRException {
JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
JRXlsExporter exporter = new JRXlsExporter(); JRXlsExporter exporter = new JRXlsExporter();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import java.util.ResourceBundle;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter; import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperPrint;
@ -71,12 +72,12 @@ public class JasperReportsUtilsTests extends TestCase {
public void testRenderAsCsvWithExporterParameters() throws Exception { public void testRenderAsCsvWithExporterParameters() throws Exception {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
Map exporterParameters = new HashMap(); Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>();
exporterParameters.put(JRCsvExporterParameter.FIELD_DELIMITER, "~"); exporterParameters.put(JRCsvExporterParameter.FIELD_DELIMITER, "~");
JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getData(), writer, exporterParameters); JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getData(), writer, exporterParameters);
String output = writer.getBuffer().toString(); String output = writer.getBuffer().toString();
assertCsvOutputCorrect(output); assertCsvOutputCorrect(output);
assertTrue("Delimiter is incorrect", output.indexOf("~") > -1); assertTrue("Delimiter is incorrect", output.contains("~"));
} }
public void testRenderAsHtmlWithDataSource() throws Exception { public void testRenderAsHtmlWithDataSource() throws Exception {
@ -95,13 +96,13 @@ public class JasperReportsUtilsTests extends TestCase {
public void testRenderAsHtmlWithExporterParameters() throws Exception { public void testRenderAsHtmlWithExporterParameters() throws Exception {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
Map exporterParameters = new HashMap(); Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>();
String uri = "/my/uri"; String uri = "/my/uri";
exporterParameters.put(JRHtmlExporterParameter.IMAGES_URI, uri); exporterParameters.put(JRHtmlExporterParameter.IMAGES_URI, uri);
JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getData(), writer, exporterParameters); JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getData(), writer, exporterParameters);
String output = writer.getBuffer().toString(); String output = writer.getBuffer().toString();
assertHtmlOutputCorrect(output); assertHtmlOutputCorrect(output);
assertTrue("URI not included", output.indexOf(uri) > -1); assertTrue("URI not included", output.contains(uri));
} }
public void testRenderAsPdfWithDataSource() throws Exception { public void testRenderAsPdfWithDataSource() throws Exception {
@ -120,12 +121,12 @@ public class JasperReportsUtilsTests extends TestCase {
public void testRenderAsPdfWithExporterParameters() throws Exception { public void testRenderAsPdfWithExporterParameters() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
Map exporterParameters = new HashMap(); Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>();
exporterParameters.put(JRPdfExporterParameter.PDF_VERSION, JRPdfExporterParameter.PDF_VERSION_1_6.toString()); exporterParameters.put(JRPdfExporterParameter.PDF_VERSION, JRPdfExporterParameter.PDF_VERSION_1_6.toString());
JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getData(), os, exporterParameters); JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getData(), os, exporterParameters);
byte[] output = os.toByteArray(); byte[] output = os.toByteArray();
assertPdfOutputCorrect(output); assertPdfOutputCorrect(output);
assertTrue(new String(output).indexOf("PDF-1.6") > -1); assertTrue(new String(output).contains("PDF-1.6"));
} }
public void testRenderAsXlsWithDataSource() throws Exception { public void testRenderAsXlsWithDataSource() throws Exception {
@ -144,7 +145,7 @@ public class JasperReportsUtilsTests extends TestCase {
public void testRenderAsXlsWithExporterParameters() throws Exception { public void testRenderAsXlsWithExporterParameters() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
Map exporterParameters = new HashMap(); Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>();
SimpleProgressMonitor monitor = new SimpleProgressMonitor(); SimpleProgressMonitor monitor = new SimpleProgressMonitor();
exporterParameters.put(JRXlsExporterParameter.PROGRESS_MONITOR, monitor); exporterParameters.put(JRXlsExporterParameter.PROGRESS_MONITOR, monitor);
@ -174,13 +175,13 @@ public class JasperReportsUtilsTests extends TestCase {
private void assertCsvOutputCorrect(String output) { private void assertCsvOutputCorrect(String output) {
assertTrue("Output length should be greater than 0", (output.length() > 0)); assertTrue("Output length should be greater than 0", (output.length() > 0));
assertTrue("Output should start with Dear Lord!", output.startsWith("Dear Lord!")); assertTrue("Output should start with Dear Lord!", output.startsWith("Dear Lord!"));
assertTrue("Output should contain 'MeineSeite'", output.indexOf("MeineSeite") > -1); assertTrue("Output should contain 'MeineSeite'", output.contains("MeineSeite"));
} }
private void assertHtmlOutputCorrect(String output) { private void assertHtmlOutputCorrect(String output) {
assertTrue("Output length should be greater than 0", (output.length() > 0)); assertTrue("Output length should be greater than 0", (output.length() > 0));
assertTrue("Output should contain <html>", output.indexOf("<html>") > -1); assertTrue("Output should contain <html>", output.contains("<html>"));
assertTrue("Output should contain 'MeineSeite'", output.indexOf("MeineSeite") > -1); assertTrue("Output should contain 'MeineSeite'", output.contains("MeineSeite"));
} }
private void assertPdfOutputCorrect(byte[] output) throws Exception { private void assertPdfOutputCorrect(byte[] output) throws Exception {
@ -205,8 +206,8 @@ public class JasperReportsUtilsTests extends TestCase {
return (JasperReport) JRLoader.loadObject(resource.getInputStream()); return (JasperReport) JRLoader.loadObject(resource.getInputStream());
} }
private Map getParameters() { private Map<String, Object> getParameters() {
Map model = new HashMap(); Map<String, Object> model = new HashMap<String, Object>();
model.put("ReportTitle", "Dear Lord!"); model.put("ReportTitle", "Dear Lord!");
model.put(JRParameter.REPORT_LOCALE, Locale.GERMAN); model.put(JRParameter.REPORT_LOCALE, Locale.GERMAN);
model.put(JRParameter.REPORT_RESOURCE_BUNDLE, model.put(JRParameter.REPORT_RESOURCE_BUNDLE,
@ -218,8 +219,8 @@ public class JasperReportsUtilsTests extends TestCase {
return new JRBeanCollectionDataSource(getData()); return new JRBeanCollectionDataSource(getData());
} }
private List getData() { private List<PersonBean> getData() {
List list = new ArrayList(); List<PersonBean> list = new ArrayList<PersonBean>();
for (int x = 0; x < 10; x++) { for (int x = 0; x < 10; x++) {
PersonBean bean = new PersonBean(); PersonBean bean = new PersonBean();
bean.setId(x); bean.setId(x);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,7 +59,7 @@ public abstract class AbstractJasperReportsSingleFormatView extends AbstractJasp
JRExporter exporter = createExporter(); JRExporter exporter = createExporter();
Map mergedExporterParameters = getConvertedExporterParameters(); Map<JRExporterParameter, Object> mergedExporterParameters = getConvertedExporterParameters();
if (!CollectionUtils.isEmpty(mergedExporterParameters)) { if (!CollectionUtils.isEmpty(mergedExporterParameters)) {
exporter.setParameters(mergedExporterParameters); exporter.setParameters(mergedExporterParameters);
} }