01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.web.servlet.view.jasperreports;
18:
19: import net.sf.jasperreports.engine.JRExporter;
20:
21: import org.springframework.beans.BeanUtils;
22:
23: /**
24: * Configurable JasperReports View, allowing to specify the JasperReports exporter
25: * to be specified through bean properties rather than through the view class name.
26: *
27: * @author Rob Harrop
28: * @since 2.0
29: * @see JasperReportsCsvView
30: * @see JasperReportsHtmlView
31: * @see JasperReportsPdfView
32: * @see JasperReportsXlsView
33: */
34: public class ConfigurableJasperReportsView extends
35: AbstractJasperReportsSingleFormatView {
36:
37: private Class exporterClass;
38:
39: private boolean useWriter = true;
40:
41: /**
42: * Set the {@link JRExporter} implementation <code>Class</code> to use. Throws
43: * {@link IllegalArgumentException} if the <code>Class</code> doesn't implement
44: * {@link JRExporter}. Required setting, as it does not have a default.
45: */
46: public void setExporterClass(Class exporterClass) {
47: if (!(JRExporter.class.isAssignableFrom(exporterClass))) {
48: throw new IllegalArgumentException("Exporter class ["
49: + exporterClass.getName()
50: + "] does not implement JRExporter");
51: }
52: this .exporterClass = exporterClass;
53: }
54:
55: /**
56: * Specifies whether or not the {@link JRExporter} writes to the {@link java.io.PrintWriter}
57: * of the associated with the request (<code>true</code>) or whether it writes directly to the
58: * {@link java.io.InputStream} of the request (<code>false</code>). Default is <code>true</code>.
59: */
60: public void setUseWriter(boolean useWriter) {
61: this .useWriter = useWriter;
62: }
63:
64: /**
65: * Checks that the {@link #setExporterClass(Class) exporterClass} property is specified.
66: */
67: protected void onInit() {
68: if (this .exporterClass == null) {
69: throw new IllegalArgumentException(
70: "exporterClass is required");
71: }
72: }
73:
74: /**
75: * Returns a new instance of the specified {@link JRExporter} class.
76: * @see #setExporterClass(Class)
77: * @see BeanUtils#instantiateClass(Class)
78: */
79: protected JRExporter createExporter() {
80: return (JRExporter) BeanUtils
81: .instantiateClass(this .exporterClass);
82: }
83:
84: /**
85: * Indicates how the {@link JRExporter} should render its data.
86: * @see #setUseWriter(boolean)
87: */
88: protected boolean useWriter() {
89: return this.useWriter;
90: }
91:
92: }
|