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: * HtmlDirExportTask.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.File;
032: import java.io.IOException;
033:
034: import org.jfree.io.IOUtils;
035: import org.jfree.report.JFreeReport;
036: import org.jfree.report.ReportInterruptedException;
037: import org.jfree.report.ReportProcessingException;
038: import org.jfree.report.layout.output.ReportProcessor;
039: import org.jfree.report.modules.gui.common.StatusListener;
040: import org.jfree.report.modules.gui.commonswing.ReportProgressDialog;
041: import org.jfree.report.modules.gui.commonswing.StatusType;
042: import org.jfree.report.modules.gui.commonswing.SwingGuiContext;
043: import org.jfree.report.modules.output.table.base.FlowReportProcessor;
044: import org.jfree.report.modules.output.table.html.AllItemsHtmlPrinter;
045: import org.jfree.report.modules.output.table.html.FileSystemURLRewriter;
046: import org.jfree.report.modules.output.table.html.FlowHtmlOutputProcessor;
047: import org.jfree.report.modules.output.table.html.HtmlOutputProcessor;
048: import org.jfree.report.modules.output.table.html.HtmlPrinter;
049: import org.jfree.report.modules.output.table.html.PageableHtmlOutputProcessor;
050: import org.jfree.report.modules.output.table.html.StreamHtmlOutputProcessor;
051: import org.jfree.report.modules.output.pageable.base.PageableReportProcessor;
052: import org.jfree.report.util.i18n.Messages;
053: import org.jfree.repository.ContentLocation;
054: import org.jfree.repository.DefaultNameGenerator;
055: import org.jfree.repository.file.FileRepository;
056: import org.jfree.util.Configuration;
057: import org.jfree.util.Log;
058:
059: /**
060: * An export task implementation that exports the report into a HTML directory structure.
061: *
062: * @author Thomas Morgner
063: */
064: public class HtmlDirExportTask implements Runnable {
065: /**
066: * Provides access to externalized strings
067: */
068: private Messages messages;
069:
070: /**
071: * The progress dialog that monitors the export process.
072: */
073: private final ReportProgressDialog progressDialog;
074:
075: /**
076: * The name of the data directory (relative to the target file).
077: */
078: private File dataDirectory;
079: /**
080: * The report that should be exported.
081: */
082: private final JFreeReport report;
083: private StatusListener statusListener;
084: private String exportMethod;
085: private File targetDirectory;
086: private String suffix;
087: private String filename;
088:
089: /**
090: * Creates a new html export task.
091: *
092: * @param progressDialog the progress monitor component (may be null).
093: * @param report the report that should be exported.
094: */
095: public HtmlDirExportTask(final JFreeReport report,
096: final ReportProgressDialog progressDialog,
097: final SwingGuiContext swingGuiContext)
098: throws ReportProcessingException {
099: if (report == null) {
100: throw new ReportProcessingException(
101: "HtmlDirExportTask(..): Report-Parameter cannot be null"); //$NON-NLS-1$
102: }
103:
104: this .progressDialog = progressDialog;
105: this .report = report;
106:
107: final Configuration config = report.getConfiguration();
108: final String dataDirectoryName = config
109: .getConfigProperty("org.jfree.report.modules.gui.html.file.DataDirectory"); //$NON-NLS-1$
110: final String targetFileName = config
111: .getConfigProperty("org.jfree.report.modules.gui.html.file.TargetFileName"); //$NON-NLS-1$
112: exportMethod = config
113: .getConfigProperty("org.jfree.report.modules.gui.html.file.ExportMethod"); //$NON-NLS-1$
114:
115: try {
116: if (swingGuiContext != null) {
117: this .statusListener = swingGuiContext
118: .getStatusListener();
119: this .messages = new Messages(swingGuiContext
120: .getLocale(),
121: HtmlDirExportPlugin.BASE_RESOURCE_CLASS);
122: } else {
123: this .messages = new Messages(
124: HtmlDirExportPlugin.BASE_RESOURCE_CLASS);
125: }
126:
127: final File targetFile = new File(targetFileName)
128: .getCanonicalFile();
129: targetDirectory = targetFile.getParentFile();
130:
131: final File tempDataDir = new File(dataDirectoryName)
132: .getCanonicalFile();
133: if ("".equals(dataDirectoryName) == false
134: && tempDataDir.isAbsolute()) {
135: dataDirectory = tempDataDir;
136: } else {
137: dataDirectory = new File(targetDirectory,
138: dataDirectoryName).getCanonicalFile();
139: }
140: if (dataDirectory.exists()
141: && dataDirectory.isDirectory() == false) {
142: dataDirectory = dataDirectory.getParentFile();
143: if (dataDirectory.isDirectory() == false) {
144: throw new ReportProcessingException(
145: "HtmlDirExportTask(..): Data-Directory is invalid: " + dataDirectory); //$NON-NLS-1$
146: }
147: } else if (dataDirectory.exists() == false) {
148: dataDirectory.mkdirs();
149: }
150:
151: suffix = getSuffix(targetFileName);
152: filename = IOUtils.getInstance().stripFileExtension(
153: targetFile.getName());
154:
155: if (targetFile.exists()) {
156: // lets try to delete it ..
157: if (targetFile.delete() == false) {
158: throw new ReportProcessingException(
159: messages
160: .getErrorString(
161: "HtmlDirExportTask.ERROR_0003_TARGET_FILE_EXISTS", targetFile.getAbsolutePath())); //$NON-NLS-1$
162: }
163: }
164: } catch (IOException ioe) {
165: throw new ReportProcessingException(
166: "Failed to normalize directories.", ioe);
167: }
168: }
169:
170: private String getSuffix(final String filename) {
171: final String suffix = IOUtils.getInstance().getFileExtension(
172: filename);
173: if (suffix.length() == 0) {
174: return ""; //$NON-NLS-1$
175: }
176: return suffix.substring(1);
177: }
178:
179: /**
180: * Exports the report into a Html Directory Structure.
181: */
182: public void run() {
183: try {
184:
185: final FileRepository targetRepository = new FileRepository(
186: targetDirectory);
187: final ContentLocation targetRoot = targetRepository
188: .getRoot();
189:
190: final FileRepository dataRepository = new FileRepository(
191: dataDirectory);
192: final ContentLocation dataRoot = dataRepository.getRoot();
193:
194: final ReportProcessor sp;
195: if ("pageable".equals(exportMethod)) {
196: final PageableHtmlOutputProcessor outputProcessor = new PageableHtmlOutputProcessor(
197: report.getConfiguration());
198: final HtmlPrinter printer = new AllItemsHtmlPrinter(
199: report.getResourceManager());
200: printer.setContentWriter(targetRoot,
201: new DefaultNameGenerator(targetRoot, filename,
202: suffix));
203: printer.setDataWriter(dataRoot,
204: new DefaultNameGenerator(dataRoot, "content")); //$NON-NLS-1$
205: printer.setUrlRewriter(new FileSystemURLRewriter());
206: outputProcessor.setPrinter(printer);
207: sp = new PageableReportProcessor(report,
208: outputProcessor);
209: } else {
210: final HtmlOutputProcessor outputProcessor = createOutputProcessor();
211: final HtmlPrinter printer = new AllItemsHtmlPrinter(
212: report.getResourceManager());
213: printer.setContentWriter(targetRoot,
214: new DefaultNameGenerator(targetRoot, filename,
215: suffix));
216: printer.setDataWriter(dataRoot,
217: new DefaultNameGenerator(dataRoot, "content")); //$NON-NLS-1$
218: printer.setUrlRewriter(new FileSystemURLRewriter());
219: outputProcessor.setPrinter(printer);
220: sp = new FlowReportProcessor(report, outputProcessor);
221: }
222: if (progressDialog != null) {
223: progressDialog.setModal(false);
224: progressDialog.setVisible(true);
225: sp.addReportProgressListener(progressDialog);
226: }
227: sp.processReport();
228: sp.close();
229:
230: if (progressDialog != null) {
231: sp.removeReportProgressListener(progressDialog);
232: }
233:
234: if (statusListener != null) {
235: statusListener
236: .setStatus(
237: StatusType.INFORMATION,
238: messages
239: .getString("HtmlDirExportTask.USER_TASK_FINISHED")); //$NON-NLS-1$
240: }
241: } catch (ReportInterruptedException re) {
242: if (statusListener != null) {
243: statusListener
244: .setStatus(
245: StatusType.INFORMATION,
246: messages
247: .getString("HtmlDirExportTask.USER_TASK_ABORTED")); //$NON-NLS-1$
248: }
249: } catch (Exception re) {
250: Log.error("Exporting failed .", re); //$NON-NLS-1$
251: if (statusListener != null) {
252: statusListener
253: .setStatus(
254: StatusType.ERROR,
255: messages
256: .getString("HtmlDirExportTask.USER_TASK_ERROR")); //$NON-NLS-1$
257: }
258: }
259: if (progressDialog != null) {
260: progressDialog.setVisible(false);
261: }
262: }
263:
264: protected HtmlOutputProcessor createOutputProcessor() {
265: if ("pageable".equals(exportMethod)) //$NON-NLS-1$
266: {
267: return new PageableHtmlOutputProcessor(report
268: .getConfiguration());
269: } else if ("flow".equals(exportMethod)) //$NON-NLS-1$
270: {
271: return new FlowHtmlOutputProcessor(report
272: .getConfiguration());
273: } else {
274: return new StreamHtmlOutputProcessor(report
275: .getConfiguration());
276: }
277: }
278:
279: }
|