001package net.sf.logdistiller.publishers;
002
003/*
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017import java.io.*;
018
019import org.apache.commons.io.IOUtils;
020
021import net.sf.logdistiller.*;
022
023/**
024 * Save report of a LogDistillation in a file.
025 * <p>
026 * Parameters:
027 * <ul>
028 * <li><code>filename</code> (default: <code>report.[ext]</code> for global report, or
029 * <code><i>[group id].[ext]</i></code> for group report): the file name (relative to output directory)
030 * </ul>
031 */
032public class FilePublisher
033    extends Publisher
034{
035    public String getId()
036    {
037        return "file";
038    }
039
040    public void publish( LogDistillation logdistillation, LogDistiller.Report report )
041        throws IOException
042    {
043        ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
044        String filename = report.getParam( "filename", "result." + format.getFileExtension() );
045        Writer out = new FileWriter( logdistillation.newDestinationFile( filename ) );
046        try
047        {
048            format.report( logdistillation, out );
049        }
050        finally
051        {
052            IOUtils.closeQuietly( out );
053        }
054    }
055
056    public void publish( LogDistillation.Group group, LogDistiller.Report report )
057        throws IOException
058    {
059        ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
060        String filename = report.getParam( "filename" );
061        if ( filename == null )
062        {
063            filename = group.getDefinition().getId() + "." + format.getFileExtension();
064        }
065        Writer out = new FileWriter( group.getLogdistillation().newDestinationFile( filename ) );
066        try
067        {
068            format.report( group, out );
069        }
070        finally
071        {
072            IOUtils.closeQuietly( out );
073        }
074    }
075}