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