001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * HtmlZipExportTask.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.html;
030:
031: import java.io.BufferedOutputStream;
032: import java.io.File;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: import org.jfree.report.JFreeReport;
038: import org.jfree.report.ReportInterruptedException;
039: import org.jfree.report.ReportProcessingException;
040: import org.jfree.report.layout.output.ReportProcessor;
041: import org.jfree.report.modules.gui.common.StatusListener;
042: import org.jfree.report.modules.gui.commonswing.ReportProgressDialog;
043: import org.jfree.report.modules.gui.commonswing.StatusType;
044: import org.jfree.report.modules.gui.commonswing.SwingGuiContext;
045: import org.jfree.report.modules.output.table.base.FlowReportProcessor;
046: import org.jfree.report.modules.output.table.html.AllItemsHtmlPrinter;
047: import org.jfree.report.modules.output.table.html.FlowHtmlOutputProcessor;
048: import org.jfree.report.modules.output.table.html.HtmlOutputProcessor;
049: import org.jfree.report.modules.output.table.html.HtmlPrinter;
050: import org.jfree.report.modules.output.table.html.PageableHtmlOutputProcessor;
051: import org.jfree.report.modules.output.table.html.SingleRepositoryURLRewriter;
052: import org.jfree.report.modules.output.table.html.StreamHtmlOutputProcessor;
053: import org.jfree.report.modules.output.pageable.base.PageableReportProcessor;
054: import org.jfree.report.util.i18n.Messages;
055: import org.jfree.repository.ContentLocation;
056: import org.jfree.repository.DefaultNameGenerator;
057: import org.jfree.repository.RepositoryUtilities;
058: import org.jfree.repository.zipwriter.ZipRepository;
059: import org.jfree.util.Configuration;
060: import org.jfree.util.Log;
061:
062: /**
063: * An export task implementation that exports the report into a ZIPped Html directory
064: * structure.
065: *
066: * @author Thomas Morgner
067: */
068: public class HtmlZipExportTask implements Runnable {
069: /**
070: * Provides access to externalized strings
071: */
072: private Messages messages;
073:
074: /**
075: * The progress dialog that monitors the export process.
076: */
077: private final ReportProgressDialog progressDialog;
078: /**
079: * The report that should be exported.
080: */
081: private final JFreeReport report;
082: private StatusListener statusListener;
083:
084: private String exportMethod;
085: private String dataDirectory;
086: private File targetFile;
087:
088: /**
089: * Creates a new html export task.
090: *
091: * @param dialog the progress monitor component.
092: * @param report the report that should be exported.
093: */
094: public HtmlZipExportTask(final JFreeReport report,
095: final ReportProgressDialog dialog,
096: final SwingGuiContext swingGuiContext)
097: throws ReportProcessingException {
098: if (report == null) {
099: throw new ReportProcessingException(
100: "HtmlZipExportTask(..): Report-Parameter cannot be null"); //$NON-NLS-1$
101: }
102:
103: final Configuration config = report.getConfiguration();
104: dataDirectory = config
105: .getConfigProperty("org.jfree.report.modules.gui.html.zip.DataDirectory"); //$NON-NLS-1$
106: final String targetFileName = config
107: .getConfigProperty("org.jfree.report.modules.gui.html.zip.TargetFileName"); //$NON-NLS-1$
108: exportMethod = config
109: .getConfigProperty("org.jfree.report.modules.gui.html.zip.ExportMethod"); //$NON-NLS-1$
110:
111: this .progressDialog = dialog;
112: this .report = report;
113: if (swingGuiContext != null) {
114: this .statusListener = swingGuiContext.getStatusListener();
115: this .messages = new Messages(swingGuiContext.getLocale(),
116: HtmlZipExportPlugin.BASE_RESOURCE_CLASS);
117: } else {
118: this .messages = new Messages(
119: HtmlZipExportPlugin.BASE_RESOURCE_CLASS);
120: }
121:
122: targetFile = new File(targetFileName);
123:
124: if (targetFile.exists()) {
125: // lets try to delete it ..
126: if (targetFile.delete() == false) {
127: throw new ReportProcessingException(
128: messages
129: .getErrorString("HtmlZipExportTask.ERROR_0002_TARGET_FILE_EXISTS")); //$NON-NLS-1$
130: }
131: }
132: }
133:
134: /**
135: * Exports the report into a Html Directory Structure.
136: */
137: public void run() {
138: OutputStream out = null;
139: try {
140: out = new BufferedOutputStream(new FileOutputStream(
141: targetFile));
142:
143: final ZipRepository zipRepository = new ZipRepository(out);
144: final ContentLocation root = zipRepository.getRoot();
145: final ContentLocation data = RepositoryUtilities
146: .createLocation(zipRepository, RepositoryUtilities
147: .split(dataDirectory, "/")); //$NON-NLS-1$
148:
149: final ReportProcessor sp;
150: if ("pageable".equals(exportMethod)) {
151: final PageableHtmlOutputProcessor outputProcessor = new PageableHtmlOutputProcessor(
152: report.getConfiguration());
153: final HtmlPrinter printer = new AllItemsHtmlPrinter(
154: report.getResourceManager());
155: printer.setContentWriter(root,
156: new DefaultNameGenerator(root, "report.html")); //$NON-NLS-1$
157: printer.setDataWriter(data, new DefaultNameGenerator(
158: data, "content")); //$NON-NLS-1$
159: printer
160: .setUrlRewriter(new SingleRepositoryURLRewriter());
161: outputProcessor.setPrinter(printer);
162: sp = new PageableReportProcessor(report,
163: outputProcessor);
164: } else {
165: final HtmlOutputProcessor outputProcessor = createOutputProcessor();
166: final HtmlPrinter printer = new AllItemsHtmlPrinter(
167: report.getResourceManager());
168: printer.setContentWriter(root,
169: new DefaultNameGenerator(root, "report.html")); //$NON-NLS-1$
170: printer.setDataWriter(data, new DefaultNameGenerator(
171: data, "content")); //$NON-NLS-1$
172: printer
173: .setUrlRewriter(new SingleRepositoryURLRewriter());
174: outputProcessor.setPrinter(printer);
175: sp = new FlowReportProcessor(report, outputProcessor);
176: }
177: if (progressDialog != null) {
178: progressDialog.setModal(false);
179: progressDialog.setVisible(true);
180: sp.addReportProgressListener(progressDialog);
181: }
182: sp.processReport();
183: sp.close();
184:
185: zipRepository.close();
186: out.close();
187: out = null;
188:
189: if (progressDialog != null) {
190: sp.removeReportProgressListener(progressDialog);
191: }
192:
193: if (statusListener != null) {
194: statusListener
195: .setStatus(
196: StatusType.INFORMATION,
197: messages
198: .getString("HtmlZipExportTask.USER_TASK_FINISHED")); //$NON-NLS-1$
199: }
200: } catch (ReportInterruptedException re) {
201: if (statusListener != null) {
202: statusListener
203: .setStatus(
204: StatusType.INFORMATION,
205: messages
206: .getString("HtmlZipExportTask.USER_TASK_ABORTED")); //$NON-NLS-1$
207: }
208: try {
209: out.close();
210: out = null;
211: } catch (IOException ioe) {
212: // ignore me...
213: }
214: } catch (Exception re) {
215: Log.error("Exporting failed .", re); //$NON-NLS-1$
216: if (statusListener != null) {
217: statusListener
218: .setStatus(
219: StatusType.ERROR,
220: messages
221: .getString("HtmlZipExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
222: }
223: } finally {
224: try {
225: if (out != null) {
226: out.close();
227: }
228: } catch (Exception e) {
229: Log.error("Unable to close the output stream.", e); //$NON-NLS-1$
230: if (statusListener != null) {
231: statusListener
232: .setStatus(
233: StatusType.ERROR,
234: messages
235: .getString("HtmlZipExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
236: }
237: }
238: }
239: if (progressDialog != null) {
240: progressDialog.setVisible(false);
241: }
242: }
243:
244: protected HtmlOutputProcessor createOutputProcessor() {
245: if ("pageable".equals(exportMethod)) //$NON-NLS-1$
246: {
247: return new PageableHtmlOutputProcessor(report
248: .getConfiguration());
249: } else if ("flow".equals(exportMethod)) //$NON-NLS-1$
250: {
251: return new FlowHtmlOutputProcessor(report
252: .getConfiguration());
253: } else {
254: return new StreamHtmlOutputProcessor(report
255: .getConfiguration());
256: }
257: }
258: }
|