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: BatikDetector.java,v 584e9b40f728 2009/04/09 07:41:44 maxberger $ */
18  
19  package net.sourceforge.jeuclid.converter;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  
24  import net.sourceforge.jeuclid.elements.generic.JEuclidDOMImplementation;
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.w3c.dom.DOMImplementation;
30  
31  /**
32   * Detects if Batik is in the class path and registers it if its available.
33   * 
34   * @version $Revision: 584e9b40f728 $
35   */
36  public final class BatikDetector implements ConverterDetector {
37      /**
38       * Logger for this class
39       */
40      private static final Log LOGGER = LogFactory.getLog(BatikDetector.class);
41  
42      /**
43       * Default constructor.
44       */
45      public BatikDetector() {
46          // Empty on purpose
47      }
48  
49      private static DOMImplementation findSVGDOMImplementation() {
50          DOMImplementation impl;
51          try {
52              final Class<?> svgdomimpl = ClassLoaderSupport.getInstance()
53                      .loadClass(
54                              "org.apache.batik.dom.svg.SVGDOMImplementation");
55              final Method getDOMimpl = svgdomimpl.getMethod(
56                      "getDOMImplementation", new Class<?>[] {});
57              impl = (DOMImplementation) getDOMimpl.invoke(null,
58                      (Object[]) null);
59              // CHECKSTYLE:OFF
60              // In this case, ANY runtime exception must be caught, since batik
61              // may not be available.
62          } catch (final RuntimeException e) {
63              // CHECKSYTLE:ON
64              impl = null;
65          } catch (final LinkageError e) {
66              impl = null;
67          } catch (final ClassNotFoundException e) {
68              impl = null;
69          } catch (final NoSuchMethodException e) {
70              impl = null;
71          } catch (final IllegalAccessException e) {
72              impl = null;
73          } catch (final InvocationTargetException e) {
74              impl = null;
75          }
76          if (impl == null) {
77              impl = JEuclidDOMImplementation.getInstance();
78          }
79          return impl;
80      }
81  
82      /**
83       * Detects if Batik is in the classpath.
84       * 
85       * @param registry
86       *            ConverterRegisty to register with.
87       */
88      public void detectConversionPlugins(final ConverterRegistry registry) {
89          try {
90              ClassLoaderSupport.getInstance().loadClass(
91                      "org.apache.batik.svggen.SVGGraphics2D");
92              BatikDetector.LOGGER.debug("Batik detected!");
93              registry
94                      .registerMimeTypeAndSuffix(
95                              net.sourceforge.jeuclid.converter.Converter.TYPE_SVG,
96                              net.sourceforge.jeuclid.converter.Converter.EXTENSION_SVG,
97                              true);
98              final DOMImplementation impl = BatikDetector
99                      .findSVGDOMImplementation();
100             if (impl != null) {
101                 registry.registerConverter(
102                         net.sourceforge.jeuclid.converter.Converter.TYPE_SVG,
103                         new BatikConverter(impl), true);
104             }
105         } catch (final ClassNotFoundException e) {
106             BatikDetector.LOGGER.debug("Batik is not in classpath!");
107         }
108     }
109 
110 }