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

   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: HtmlFileExportTask.java 2890 2007-06-10 15:54:22Z 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.File;
  35: 
  36: import org.jfree.io.IOUtils;
  37: import org.jfree.layouting.modules.output.html.FileSystemURLRewriter;
  38: import org.jfree.layouting.modules.output.html.FlowHtmlOutputProcessor;
  39: import org.jfree.layouting.modules.output.html.HtmlOutputProcessor;
  40: import org.jfree.layouting.modules.output.html.HtmlPrinter;
  41: import org.jfree.layouting.modules.output.html.PageableHtmlOutputProcessor;
  42: import org.jfree.layouting.modules.output.html.StreamingHtmlOutputProcessor;
  43: import org.jfree.report.ReportConfigurationException;
  44: import org.jfree.report.flow.ReportJob;
  45: import org.jfree.report.flow.streaming.StreamingReportProcessor;
  46: import org.jfree.repository.ContentLocation;
  47: import org.jfree.repository.DefaultNameGenerator;
  48: import org.jfree.repository.file.FileRepository;
  49: import org.jfree.util.Configuration;
  50: import org.jfree.util.Log;
  51: 
  52: /**
  53:  * Creation-Date: 02.12.2006, 14:15:14
  54:  *
  55:  * @author Thomas Morgner
  56:  */
  57: public class HtmlFileExportTask implements Runnable
  58: {
  59:   private ReportJob job;
  60:   private File dataDirectory;
  61:   private File targetDirectory;
  62:   private String exportMethod;
  63:   private String suffix;
  64:   private String filename;
  65:   private String encoding;
  66: 
  67:   public HtmlFileExportTask(final ReportJob job)
  68:       throws ReportConfigurationException
  69:   {
  70:     if (job == null)
  71:     {
  72:       throw new NullPointerException();
  73:     }
  74:     this.job = job;
  75: 
  76:     final Configuration config = job.getConfiguration();
  77:     final String dataDirectoryName = config.getConfigProperty
  78:         ("org.jfree.report.modules.gui.common.html.file.DataDirectory");
  79:     final String targetFileName = config.getConfigProperty
  80:         ("org.jfree.report.modules.gui.common.html.file.TargetFileName");
  81:     exportMethod = config.getConfigProperty
  82:         ("org.jfree.report.modules.gui.common.html.file.ExportMethod");
  83:     encoding = config.getConfigProperty
  84:         ("org.jfree.report.modules.gui.common.html.file.Encoding", "ASCII");
  85: 
  86:     final File targetFile = new File(targetFileName);
  87:     targetDirectory = targetFile.getParentFile();
  88: 
  89:     dataDirectory = new File(targetFile, dataDirectoryName);
  90:     if (dataDirectory.isDirectory() == false)
  91:     {
  92:       throw new ReportConfigurationException("DataDirectory is invalid: " + dataDirectory);
  93:     }
  94: 
  95:     suffix = IOUtils.getInstance().getFileExtension(targetFile.getName());
  96:     filename = IOUtils.getInstance().stripFileExtension(targetFile.getName());
  97: 
  98:     if (targetFile.exists())
  99:     {
 100:       // lets try to delete it ..
 101:       if (targetFile.delete() == false)
 102:       {
 103:         throw new ReportConfigurationException
 104:             ("Target-File exists, but cannot be removed.");
 105:       }
 106:     }
 107:   }
 108: 
 109:   /**
 110:    * When an object implementing interface <code>Runnable</code> is used to
 111:    * create a thread, starting the thread causes the object's <code>run</code>
 112:    * method to be called in that separately executing thread.
 113:    * <p/>
 114:    * The general contract of the method <code>run</code> is that it may take any
 115:    * action whatsoever.
 116:    *
 117:    * @see Thread#run()
 118:    */
 119:   public void run()
 120:   {
 121:     try
 122:     {
 123:       final FileRepository targetRepository = new FileRepository(targetDirectory);
 124:       final ContentLocation targetRoot = targetRepository.getRoot();
 125: 
 126:       final FileRepository dataRepository = new FileRepository(dataDirectory);
 127:       final ContentLocation dataRoot = dataRepository.getRoot();
 128: 
 129:       final StreamingReportProcessor sp = new StreamingReportProcessor();
 130:       final HtmlOutputProcessor outputProcessor = createOutputProcessor();
 131:       final HtmlPrinter printer = outputProcessor.getPrinter();
 132:       printer.setContentWriter(targetRoot,
 133:           new DefaultNameGenerator(targetRoot, filename, suffix));
 134:       printer.setDataWriter(dataRoot, new DefaultNameGenerator(dataRoot, "content"));
 135:       printer.setEncoding(encoding);
 136:       printer.setUrlRewriter(new FileSystemURLRewriter());
 137:       sp.setOutputProcessor(outputProcessor);
 138:       sp.processReport(job);
 139:     }
 140:     catch(Exception e)
 141:     {
 142:       Log.error ("File-Export failed. ", e);
 143:     }
 144:     finally{
 145:       try
 146:       {
 147:         job.close();
 148:         job = null;
 149:       }
 150:       catch(Exception e)
 151:       {
 152:         // ignore ..
 153:       }
 154:     }
 155:   }
 156: 
 157:   protected HtmlOutputProcessor createOutputProcessor()
 158:   {
 159:     if ("pageable".equals(exportMethod))
 160:     {
 161:       return new PageableHtmlOutputProcessor(job.getConfiguration());
 162:     }
 163:     else if ("flow".equals(exportMethod))
 164:     {
 165:       return new FlowHtmlOutputProcessor(job.getConfiguration());
 166:     }
 167:     else
 168:     {
 169:       return new StreamingHtmlOutputProcessor(job.getConfiguration());
 170:     }
 171:   }
 172: 
 173: }