001    /*
002     * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
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    /* $Id: FreeHepInternalDetector.java,v 88b901bf20fb 2008/06/07 14:12:27 maxberger $ */
018    
019    package net.sourceforge.jeuclid.converter;
020    
021    import java.lang.reflect.InvocationTargetException;
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import net.sourceforge.jeuclid.elements.support.ClassLoaderSupport;
026    
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.freehep.util.export.ExportFileType;
030    
031    /**
032     * actual detector for FreeHEP graphics formats. Depends on the presence of
033     * FreeHEP, hence the "internal".
034     * 
035     * @version $Revision: 88b901bf20fb $
036     */
037    public final class FreeHepInternalDetector {
038    
039        /**
040         * Logger for this class
041         */
042        private static final Log LOGGER = LogFactory
043                .getLog(FreeHepInternalDetector.class);
044    
045        private static final Map<String, String> PLUGINS_CLASSES = new HashMap<String, String>();
046    
047        private FreeHepInternalDetector() {
048            // Empty on purpose
049        }
050    
051        /**
052         * Actual detection and registration routine.
053         * 
054         * @param registry
055         *            ConverterRegistry to use
056         */
057        public static void actuallyDetectConversionPlugins(
058                final ConverterRegistry registry) {
059    
060            for (final Map.Entry<String, String> e : FreeHepInternalDetector.PLUGINS_CLASSES
061                    .entrySet()) {
062    
063                try {
064                    final Class<?> infoClass = ClassLoaderSupport.getInstance()
065                            .loadClass(e.getKey());
066                    final ExportFileType fileType = (ExportFileType) infoClass
067                            .getConstructor().newInstance();
068    
069                    final Class<?> graphicsClass = ClassLoaderSupport.getInstance()
070                            .loadClass(e.getValue());
071                    FreeHepInternalDetector.actuallyRegister(registry, fileType,
072                            graphicsClass);
073                } catch (final NoSuchMethodException ex) {
074                    FreeHepInternalDetector.LOGGER.debug(ex);
075                } catch (final ClassNotFoundException ex) {
076                    FreeHepInternalDetector.LOGGER.debug(ex);
077                } catch (final IllegalArgumentException ex) {
078                    FreeHepInternalDetector.LOGGER.debug(ex);
079                } catch (final SecurityException ex) {
080                    FreeHepInternalDetector.LOGGER.debug(ex);
081                } catch (final InstantiationException ex) {
082                    FreeHepInternalDetector.LOGGER.debug(ex);
083                } catch (final IllegalAccessException ex) {
084                    FreeHepInternalDetector.LOGGER.debug(ex);
085                } catch (final InvocationTargetException ex) {
086                    FreeHepInternalDetector.LOGGER.debug(ex);
087                }
088            }
089        }
090    
091        private static void actuallyRegister(final ConverterRegistry registry,
092                final ExportFileType fileType, final Class<?> graphicsClass)
093                throws NoSuchMethodException {
094            final ConverterPlugin freeHepConverter = new FreeHepConverter(
095                    graphicsClass);
096            for (final String mimeType : fileType.getMIMETypes()) {
097                for (final String suffix : fileType.getExtensions()) {
098                    registry.registerMimeTypeAndSuffix(mimeType, suffix, false);
099                }
100                registry.registerConverter(mimeType, freeHepConverter, false);
101            }
102        }
103    
104        static {
105            FreeHepInternalDetector.PLUGINS_CLASSES.put(
106                    "org.freehep.graphicsio.emf.EMFExportFileType",
107                    "org.freehep.graphicsio.emf.EMFGraphics2D");
108            FreeHepInternalDetector.PLUGINS_CLASSES.put(
109                    "org.freehep.graphicsio.gif.GIFExportFileType",
110                    "org.freehep.graphicsio.gif.GIFGraphics2D");
111            FreeHepInternalDetector.PLUGINS_CLASSES.put(
112                    "org.freehep.graphicsio.pdf.PDFExportFileType",
113                    "org.freehep.graphicsio.pdf.PDFGraphics2D");
114            FreeHepInternalDetector.PLUGINS_CLASSES.put(
115                    "org.freehep.graphicsio.ps.PSExportFileType",
116                    "org.freehep.graphicsio.ps.PSGraphics2D");
117            FreeHepInternalDetector.PLUGINS_CLASSES.put(
118                    "org.freehep.graphicsio.svg.SVGExportFileType",
119                    "org.freehep.graphicsio.svg.SVGGraphics2D");
120            FreeHepInternalDetector.PLUGINS_CLASSES.put(
121                    "org.freehep.graphicsio.swf.SWFExportFileType",
122                    "org.freehep.graphicsio.swf.SWFGraphics2D");
123        }
124    }