Coverage Report - net.sourceforge.jeuclid.converter.BatikDetector
 
Classes in this File Line Coverage Branch Coverage Complexity
BatikDetector
53%
17/32
50%
2/4
4
 
 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  209
     private static final Log LOGGER = LogFactory.getLog(BatikDetector.class);
 41  
 
 42  
     /**
 43  
      * Default constructor.
 44  
      */
 45  209
     public BatikDetector() {
 46  
         // Empty on purpose
 47  209
     }
 48  
 
 49  
     private static DOMImplementation findSVGDOMImplementation() {
 50  
         DOMImplementation impl;
 51  
         try {
 52  209
             final Class<?> svgdomimpl = ClassLoaderSupport.getInstance()
 53  
                     .loadClass(
 54  
                             "org.apache.batik.dom.svg.SVGDOMImplementation");
 55  209
             final Method getDOMimpl = svgdomimpl.getMethod(
 56  
                     "getDOMImplementation", new Class<?>[] {});
 57  209
             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  0
         } catch (final RuntimeException e) {
 63  
             // CHECKSYTLE:ON
 64  0
             impl = null;
 65  0
         } catch (final LinkageError e) {
 66  0
             impl = null;
 67  0
         } catch (final ClassNotFoundException e) {
 68  0
             impl = null;
 69  0
         } catch (final NoSuchMethodException e) {
 70  0
             impl = null;
 71  0
         } catch (final IllegalAccessException e) {
 72  0
             impl = null;
 73  0
         } catch (final InvocationTargetException e) {
 74  0
             impl = null;
 75  209
         }
 76  209
         if (impl == null) {
 77  0
             impl = JEuclidDOMImplementation.getInstance();
 78  
         }
 79  209
         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  209
             ClassLoaderSupport.getInstance().loadClass(
 91  
                     "org.apache.batik.svggen.SVGGraphics2D");
 92  209
             BatikDetector.LOGGER.debug("Batik detected!");
 93  209
             registry
 94  
                     .registerMimeTypeAndSuffix(
 95  
                             net.sourceforge.jeuclid.converter.Converter.TYPE_SVG,
 96  
                             net.sourceforge.jeuclid.converter.Converter.EXTENSION_SVG,
 97  
                             true);
 98  209
             final DOMImplementation impl = BatikDetector
 99  
                     .findSVGDOMImplementation();
 100  209
             if (impl != null) {
 101  209
                 registry.registerConverter(
 102  
                         net.sourceforge.jeuclid.converter.Converter.TYPE_SVG,
 103  
                         new BatikConverter(impl), true);
 104  
             }
 105  0
         } catch (final ClassNotFoundException e) {
 106  0
             BatikDetector.LOGGER.debug("Batik is not in classpath!");
 107  209
         }
 108  209
     }
 109  
 
 110  
 }