001package net.sf.logdistiller;
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.util.*;
018
019import net.sf.logdistiller.reports.BaseReportFormats;
020import net.sf.logdistiller.util.ExtensionHelper;
021import net.sf.logdistiller.util.FormatUtil;
022
023/**
024 * Definition of available report formats. By default, some report formats come
025 * {@link net.sf.logdistiller.reports.BaseReportFormats predefined} with <b>LogDistiller</b>, but you can add your own.
026 */
027public abstract class ReportFormats
028{
029    public abstract List<ReportFormat> defineReportFormats();
030
031    public static final List<ReportFormat> ALL;
032
033    public static final Map<String, ReportFormat> MAP;
034    static
035    {
036        List<ReportFormat> all = new ArrayList<ReportFormat>( new BaseReportFormats().defineReportFormats() );
037        all.addAll( loadExtensionReportFormats() );
038        ALL = Collections.unmodifiableList( all );
039
040        Map<String, ReportFormat> map = new HashMap<String, ReportFormat>();
041        for ( ReportFormat reportFormat : ALL )
042        {
043            map.put( reportFormat.getId(), reportFormat );
044        }
045        MAP = Collections.unmodifiableMap( map );
046    }
047
048    /**
049     * get all the plugins defined (predefined and custom).
050     */
051    public static List<ReportFormat> getAllReportFormats()
052    {
053        return ALL;
054    }
055
056    public static String listAllReportFormatIds()
057    {
058        return FormatUtil.join( ", ", MAP.keySet().iterator() );
059    }
060
061    public static ReportFormat getReportFormat( String id )
062    {
063        ReportFormat format = MAP.get( id );
064        if ( format == null )
065        {
066            throw new IllegalArgumentException( "unknown report format '" + id + "', known values are: "
067                + listAllReportFormatIds() );
068        }
069        return format;
070    }
071
072    private static List<ReportFormat> loadExtensionReportFormats()
073    {
074        List<ReportFormat> reportformats = new ArrayList<ReportFormat>();
075        for ( String reportformatsClass : ExtensionHelper.findExtensions( "reportformats" ) )
076        {
077            if ( reportformatsClass != null )
078            {
079                reportformats.addAll( loadReportFormats( reportformatsClass ) );
080            }
081        }
082        return reportformats;
083    }
084
085    private static List<ReportFormat> loadReportFormats( String reportFormatsClass )
086    {
087        try
088        {
089            ReportFormats reportFormats = (ReportFormats) Class.forName( reportFormatsClass ).newInstance();
090            return reportFormats.defineReportFormats();
091        }
092        catch ( ClassNotFoundException cnfe )
093        {
094            throw new RuntimeException( "unable to load report formats class " + reportFormatsClass, cnfe );
095        }
096        catch ( IllegalAccessException iae )
097        {
098            throw new RuntimeException( "unable to access report formats constructor for class " + reportFormatsClass,
099                                        iae );
100        }
101        catch ( InstantiationException ie )
102        {
103            throw new RuntimeException( "unable to instanciate report formats class " + reportFormatsClass, ie );
104        }
105    }
106}