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: BatikDetector.java,v 584e9b40f728 2009/04/09 07:41:44 maxberger $ */
018
019 package net.sourceforge.jeuclid.converter;
020
021 import java.lang.reflect.InvocationTargetException;
022 import java.lang.reflect.Method;
023
024 import net.sourceforge.jeuclid.elements.generic.JEuclidDOMImplementation;
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.w3c.dom.DOMImplementation;
030
031 /**
032 * Detects if Batik is in the class path and registers it if its available.
033 *
034 * @version $Revision: 584e9b40f728 $
035 */
036 public final class BatikDetector implements ConverterDetector {
037 /**
038 * Logger for this class
039 */
040 private static final Log LOGGER = LogFactory.getLog(BatikDetector.class);
041
042 /**
043 * Default constructor.
044 */
045 public BatikDetector() {
046 // Empty on purpose
047 }
048
049 private static DOMImplementation findSVGDOMImplementation() {
050 DOMImplementation impl;
051 try {
052 final Class<?> svgdomimpl = ClassLoaderSupport.getInstance()
053 .loadClass(
054 "org.apache.batik.dom.svg.SVGDOMImplementation");
055 final Method getDOMimpl = svgdomimpl.getMethod(
056 "getDOMImplementation", new Class<?>[] {});
057 impl = (DOMImplementation) getDOMimpl.invoke(null,
058 (Object[]) null);
059 // CHECKSTYLE:OFF
060 // In this case, ANY runtime exception must be caught, since batik
061 // may not be available.
062 } catch (final RuntimeException e) {
063 // CHECKSYTLE:ON
064 impl = null;
065 } catch (final LinkageError e) {
066 impl = null;
067 } catch (final ClassNotFoundException e) {
068 impl = null;
069 } catch (final NoSuchMethodException e) {
070 impl = null;
071 } catch (final IllegalAccessException e) {
072 impl = null;
073 } catch (final InvocationTargetException e) {
074 impl = null;
075 }
076 if (impl == null) {
077 impl = JEuclidDOMImplementation.getInstance();
078 }
079 return impl;
080 }
081
082 /**
083 * Detects if Batik is in the classpath.
084 *
085 * @param registry
086 * ConverterRegisty to register with.
087 */
088 public void detectConversionPlugins(final ConverterRegistry registry) {
089 try {
090 ClassLoaderSupport.getInstance().loadClass(
091 "org.apache.batik.svggen.SVGGraphics2D");
092 BatikDetector.LOGGER.debug("Batik detected!");
093 registry
094 .registerMimeTypeAndSuffix(
095 net.sourceforge.jeuclid.converter.Converter.TYPE_SVG,
096 net.sourceforge.jeuclid.converter.Converter.EXTENSION_SVG,
097 true);
098 final DOMImplementation impl = BatikDetector
099 .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 }