001 /** 002 * ======================================== 003 * JFreeReport : a free Java report library 004 * ======================================== 005 * 006 * Project Info: http://reporting.pentaho.org/ 007 * 008 * (C) Copyright 2000-2007, by Object Refinery Limited, 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 * $Id: HtmlZipExportTask.java 3048 2007-07-28 18:02:42Z tmorgner $ 027 * ------------ 028 * (C) Copyright 2000-2005, by Object Refinery Limited. 029 * (C) Copyright 2005-2007, by Pentaho Corporation. 030 */ 031 032 package org.jfree.report.modules.gui.swing.html; 033 034 import java.io.BufferedOutputStream; 035 import java.io.File; 036 import java.io.FileOutputStream; 037 import java.io.OutputStream; 038 039 import org.jfree.io.IOUtils; 040 import org.jfree.layouting.modules.output.html.FlowHtmlOutputProcessor; 041 import org.jfree.layouting.modules.output.html.HtmlOutputProcessor; 042 import org.jfree.layouting.modules.output.html.HtmlPrinter; 043 import org.jfree.layouting.modules.output.html.PageableHtmlOutputProcessor; 044 import org.jfree.layouting.modules.output.html.StreamingHtmlOutputProcessor; 045 import org.jfree.report.ReportConfigurationException; 046 import org.jfree.report.flow.ReportJob; 047 import org.jfree.report.flow.streaming.StreamingReportProcessor; 048 import org.jfree.repository.ContentLocation; 049 import org.jfree.repository.DefaultNameGenerator; 050 import org.jfree.repository.RepositoryUtilities; 051 import org.jfree.repository.zipwriter.ZipRepository; 052 import org.jfree.util.Configuration; 053 import org.jfree.util.Log; 054 055 /** 056 * Creation-Date: 02.12.2006, 14:15:14 057 * 058 * @author Thomas Morgner 059 */ 060 public class HtmlZipExportTask implements Runnable 061 { 062 private ReportJob job; 063 private String dataDirectory; 064 private String exportMethod; 065 private String filename; 066 private File targetFile; 067 private String encoding; 068 069 public HtmlZipExportTask(final ReportJob job) 070 throws ReportConfigurationException 071 { 072 if (job == null) 073 { 074 throw new NullPointerException(); 075 } 076 this.job = job; 077 078 final Configuration config = job.getConfiguration(); 079 dataDirectory = config.getConfigProperty 080 ("org.jfree.report.modules.gui.common.html.zip.DataDirectory"); 081 final String targetFileName = config.getConfigProperty 082 ("org.jfree.report.modules.gui.common.html.zip.TargetFileName"); 083 exportMethod = config.getConfigProperty 084 ("org.jfree.report.modules.gui.common.html.zip.ExportMethod"); 085 encoding = config.getConfigProperty 086 ("org.jfree.report.modules.gui.common.html.zip.Encoding", "ASCII"); 087 088 targetFile = new File(targetFileName); 089 filename = IOUtils.getInstance().stripFileExtension(targetFile.getName()); 090 091 if (targetFile.exists()) 092 { 093 // lets try to delete it .. 094 if (targetFile.delete() == false) 095 { 096 throw new ReportConfigurationException 097 ("Target-File exists, but cannot be removed."); 098 } 099 } 100 } 101 102 /** 103 * When an object implementing interface <code>Runnable</code> is used to 104 * create a thread, starting the thread causes the object's <code>run</code> 105 * method to be called in that separately executing thread. 106 * <p/> 107 * The general contract of the method <code>run</code> is that it may take any 108 * action whatsoever. 109 * 110 * @see Thread#run() 111 */ 112 public void run() 113 { 114 OutputStream fout = null; 115 try 116 { 117 fout = new BufferedOutputStream(new FileOutputStream(targetFile)); 118 final ZipRepository zipRepository = new ZipRepository(fout); 119 final ContentLocation root = zipRepository.getRoot(); 120 final ContentLocation data = RepositoryUtilities.createLocation 121 (zipRepository, RepositoryUtilities.split(dataDirectory, "/")); 122 final StreamingReportProcessor sp = new StreamingReportProcessor(); 123 124 final HtmlOutputProcessor outputProcessor = createOutputProcessor(); 125 final HtmlPrinter printer = outputProcessor.getPrinter(); 126 127 printer.setContentWriter(root, new DefaultNameGenerator(root, filename)); 128 printer.setDataWriter(data, new DefaultNameGenerator(data, "content")); 129 printer.setEncoding(encoding); 130 131 sp.setOutputProcessor(outputProcessor); 132 sp.processReport(job); 133 } 134 catch(Exception e) 135 { 136 Log.error ("ZIP-Export failed. ", e); 137 } 138 finally 139 { 140 try 141 { 142 job.close(); 143 job = null; 144 } 145 catch(Exception e) 146 { 147 // ignore .. 148 } 149 150 if (fout != null) 151 { 152 try 153 { 154 fout.close(); 155 } 156 catch(Exception e) 157 { 158 // ignored .. 159 } 160 } 161 162 } 163 } 164 165 protected HtmlOutputProcessor createOutputProcessor() 166 { 167 if ("pageable".equals(exportMethod)) 168 { 169 return new PageableHtmlOutputProcessor(job.getConfiguration()); 170 } 171 else if ("flow".equals(exportMethod)) 172 { 173 return new FlowHtmlOutputProcessor(job.getConfiguration()); 174 } 175 else 176 { 177 return new StreamingHtmlOutputProcessor(job.getConfiguration()); 178 } 179 } 180 }