View Javadoc
1   package net.sf.logdistiller.publishers;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.io.*;
18  
19  import org.apache.commons.io.IOUtils;
20  
21  import net.sf.logdistiller.*;
22  
23  /**
24   * Save report of a LogDistillation in a file.
25   * <p>
26   * Parameters:
27   * <ul>
28   * <li><code>filename</code> (default: <code>report.[ext]</code> for global report, or
29   * <code><i>[group id].[ext]</i></code> for group report): the file name (relative to output directory)
30   * </ul>
31   */
32  public class FilePublisher
33      extends Publisher
34  {
35      public String getId()
36      {
37          return "file";
38      }
39  
40      public void publish( LogDistillation logdistillation, LogDistiller.Report report )
41          throws IOException
42      {
43          ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
44          String filename = report.getParam( "filename", "result." + format.getFileExtension() );
45          Writer out = new FileWriter( logdistillation.newDestinationFile( filename ) );
46          try
47          {
48              format.report( logdistillation, out );
49          }
50          finally
51          {
52              IOUtils.closeQuietly( out );
53          }
54      }
55  
56      public void publish( LogDistillation.Group group, LogDistiller.Report report )
57          throws IOException
58      {
59          ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
60          String filename = report.getParam( "filename" );
61          if ( filename == null )
62          {
63              filename = group.getDefinition().getId() + "." + format.getFileExtension();
64          }
65          Writer out = new FileWriter( group.getLogdistillation().newDestinationFile( filename ) );
66          try
67          {
68              format.report( group, out );
69          }
70          finally
71          {
72              IOUtils.closeQuietly( out );
73          }
74      }
75  }