Source for org.jfree.report.modules.gui.swing.html.HtmlZipExportTask

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: HtmlZipExportTask.java 2786 2007-05-09 12:28:24Z taqua $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.modules.gui.swing.html;
  33: 
  34: import java.io.BufferedOutputStream;
  35: import java.io.File;
  36: import java.io.FileOutputStream;
  37: import java.io.OutputStream;
  38: 
  39: import org.jfree.io.IOUtils;
  40: import org.jfree.layouting.modules.output.html.FlowHtmlOutputProcessor;
  41: import org.jfree.layouting.modules.output.html.HtmlOutputProcessor;
  42: import org.jfree.layouting.modules.output.html.HtmlPrinter;
  43: import org.jfree.layouting.modules.output.html.PageableHtmlOutputProcessor;
  44: import org.jfree.layouting.modules.output.html.StreamingHtmlOutputProcessor;
  45: import org.jfree.report.ReportConfigurationException;
  46: import org.jfree.report.flow.ReportJob;
  47: import org.jfree.report.flow.streaming.StreamingReportProcessor;
  48: import org.jfree.repository.ContentLocation;
  49: import org.jfree.repository.DefaultNameGenerator;
  50: import org.jfree.repository.RepositoryUtilities;
  51: import org.jfree.repository.zipwriter.ZipRepository;
  52: import org.jfree.util.Configuration;
  53: import org.jfree.util.Log;
  54: 
  55: /**
  56:  * Creation-Date: 02.12.2006, 14:15:14
  57:  *
  58:  * @author Thomas Morgner
  59:  */
  60: public class HtmlZipExportTask implements Runnable
  61: {
  62:   private ReportJob job;
  63:   private String dataDirectory;
  64:   private String exportMethod;
  65:   private String filename;
  66:   private File targetFile;
  67:   private String encoding;
  68: 
  69:   public HtmlZipExportTask(final ReportJob job)
  70:       throws ReportConfigurationException
  71:   {
  72:     if (job == null)
  73:     {
  74:       throw new NullPointerException();
  75:     }
  76:     this.job = job;
  77: 
  78:     final Configuration config = job.getConfiguration();
  79:     dataDirectory = config.getConfigProperty
  80:         ("org.jfree.report.modules.gui.common.html.zip.DataDirectory");
  81:     final String targetFileName = config.getConfigProperty
  82:         ("org.jfree.report.modules.gui.common.html.zip.TargetFileName");
  83:     exportMethod = config.getConfigProperty
  84:         ("org.jfree.report.modules.gui.common.html.zip.ExportMethod");
  85:     encoding = config.getConfigProperty
  86:         ("org.jfree.report.modules.gui.common.html.zip.Encoding", "ASCII");
  87: 
  88:     targetFile = new File(targetFileName);
  89:     filename = IOUtils.getInstance().stripFileExtension(targetFile.getName());
  90: 
  91:     if (targetFile.exists())
  92:     {
  93:       // lets try to delete it ..
  94:       if (targetFile.delete() == false)
  95:       {
  96:         throw new ReportConfigurationException
  97:             ("Target-File exists, but cannot be removed.");
  98:       }
  99:     }
 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: }