View Javadoc

1   /*
2    * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
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  /* $Id: FreeHepInternalDetector.java,v 88b901bf20fb 2008/06/07 14:12:27 maxberger $ */
18  
19  package net.sourceforge.jeuclid.converter;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import net.sourceforge.jeuclid.elements.support.ClassLoaderSupport;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.freehep.util.export.ExportFileType;
30  
31  /**
32   * actual detector for FreeHEP graphics formats. Depends on the presence of
33   * FreeHEP, hence the "internal".
34   * 
35   * @version $Revision: 88b901bf20fb $
36   */
37  public final class FreeHepInternalDetector {
38  
39      /**
40       * Logger for this class
41       */
42      private static final Log LOGGER = LogFactory
43              .getLog(FreeHepInternalDetector.class);
44  
45      private static final Map<String, String> PLUGINS_CLASSES = new HashMap<String, String>();
46  
47      private FreeHepInternalDetector() {
48          // Empty on purpose
49      }
50  
51      /**
52       * Actual detection and registration routine.
53       * 
54       * @param registry
55       *            ConverterRegistry to use
56       */
57      public static void actuallyDetectConversionPlugins(
58              final ConverterRegistry registry) {
59  
60          for (final Map.Entry<String, String> e : FreeHepInternalDetector.PLUGINS_CLASSES
61                  .entrySet()) {
62  
63              try {
64                  final Class<?> infoClass = ClassLoaderSupport.getInstance()
65                          .loadClass(e.getKey());
66                  final ExportFileType fileType = (ExportFileType) infoClass
67                          .getConstructor().newInstance();
68  
69                  final Class<?> graphicsClass = ClassLoaderSupport.getInstance()
70                          .loadClass(e.getValue());
71                  FreeHepInternalDetector.actuallyRegister(registry, fileType,
72                          graphicsClass);
73              } catch (final NoSuchMethodException ex) {
74                  FreeHepInternalDetector.LOGGER.debug(ex);
75              } catch (final ClassNotFoundException ex) {
76                  FreeHepInternalDetector.LOGGER.debug(ex);
77              } catch (final IllegalArgumentException ex) {
78                  FreeHepInternalDetector.LOGGER.debug(ex);
79              } catch (final SecurityException ex) {
80                  FreeHepInternalDetector.LOGGER.debug(ex);
81              } catch (final InstantiationException ex) {
82                  FreeHepInternalDetector.LOGGER.debug(ex);
83              } catch (final IllegalAccessException ex) {
84                  FreeHepInternalDetector.LOGGER.debug(ex);
85              } catch (final InvocationTargetException ex) {
86                  FreeHepInternalDetector.LOGGER.debug(ex);
87              }
88          }
89      }
90  
91      private static void actuallyRegister(final ConverterRegistry registry,
92              final ExportFileType fileType, final Class<?> graphicsClass)
93              throws NoSuchMethodException {
94          final ConverterPlugin freeHepConverter = new FreeHepConverter(
95                  graphicsClass);
96          for (final String mimeType : fileType.getMIMETypes()) {
97              for (final String suffix : fileType.getExtensions()) {
98                  registry.registerMimeTypeAndSuffix(mimeType, suffix, false);
99              }
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 }