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: HtmlFileExportTask.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.File;
035    
036    import org.jfree.io.IOUtils;
037    import org.jfree.layouting.modules.output.html.FileSystemURLRewriter;
038    import org.jfree.layouting.modules.output.html.FlowHtmlOutputProcessor;
039    import org.jfree.layouting.modules.output.html.HtmlOutputProcessor;
040    import org.jfree.layouting.modules.output.html.HtmlPrinter;
041    import org.jfree.layouting.modules.output.html.PageableHtmlOutputProcessor;
042    import org.jfree.layouting.modules.output.html.StreamingHtmlOutputProcessor;
043    import org.jfree.report.ReportConfigurationException;
044    import org.jfree.report.flow.ReportJob;
045    import org.jfree.report.flow.streaming.StreamingReportProcessor;
046    import org.jfree.repository.ContentLocation;
047    import org.jfree.repository.DefaultNameGenerator;
048    import org.jfree.repository.file.FileRepository;
049    import org.jfree.util.Configuration;
050    import org.jfree.util.Log;
051    
052    /**
053     * Creation-Date: 02.12.2006, 14:15:14
054     *
055     * @author Thomas Morgner
056     */
057    public class HtmlFileExportTask implements Runnable
058    {
059      private ReportJob job;
060      private File dataDirectory;
061      private File targetDirectory;
062      private String exportMethod;
063      private String suffix;
064      private String filename;
065      private String encoding;
066    
067      public HtmlFileExportTask(final ReportJob job)
068          throws ReportConfigurationException
069      {
070        if (job == null)
071        {
072          throw new NullPointerException();
073        }
074        this.job = job;
075    
076        final Configuration config = job.getConfiguration();
077        final String dataDirectoryName = config.getConfigProperty
078            ("org.jfree.report.modules.gui.common.html.file.DataDirectory");
079        final String targetFileName = config.getConfigProperty
080            ("org.jfree.report.modules.gui.common.html.file.TargetFileName");
081        exportMethod = config.getConfigProperty
082            ("org.jfree.report.modules.gui.common.html.file.ExportMethod");
083        encoding = config.getConfigProperty
084            ("org.jfree.report.modules.gui.common.html.file.Encoding", "ASCII");
085    
086        final File targetFile = new File(targetFileName);
087        targetDirectory = targetFile.getParentFile();
088    
089        dataDirectory = new File(targetFile, dataDirectoryName);
090        if (dataDirectory.isDirectory() == false)
091        {
092          throw new ReportConfigurationException("DataDirectory is invalid: " + dataDirectory);
093        }
094    
095        suffix = IOUtils.getInstance().getFileExtension(targetFile.getName());
096        filename = IOUtils.getInstance().stripFileExtension(targetFile.getName());
097    
098        if (targetFile.exists())
099        {
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    }