Coverage Report - net.sourceforge.jeuclid.converter.FreeHepConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
FreeHepConverter
60%
14/23
N/A
3,25
 
 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: FreeHepConverter.java,v 1d64d806165c 2009/04/09 07:17:07 maxberger $ */
 18  
 
 19  
 package net.sourceforge.jeuclid.converter;
 20  
 
 21  
 import java.awt.Dimension;
 22  
 import java.io.ByteArrayOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.OutputStream;
 25  
 import java.lang.reflect.Constructor;
 26  
 import java.lang.reflect.InvocationTargetException;
 27  
 
 28  
 import net.sourceforge.jeuclid.LayoutContext;
 29  
 import net.sourceforge.jeuclid.layout.JEuclidView;
 30  
 
 31  
 import org.freehep.graphics2d.VectorGraphics;
 32  
 import org.w3c.dom.Node;
 33  
 
 34  
 /**
 35  
  * Converter for output formats supported by FreeHEP.
 36  
  * 
 37  
  * @version $Revision: 1d64d806165c $
 38  
  */
 39  
 public class FreeHepConverter implements ConverterPlugin {
 40  
 
 41  
     private final Constructor<VectorGraphics> streamConst;
 42  
 
 43  
     @SuppressWarnings("unchecked")
 44  
     FreeHepConverter(final Class<?> converterClass)
 45  1254
             throws NoSuchMethodException {
 46  
 
 47  1254
         this.streamConst = ((Class<VectorGraphics>) converterClass)
 48  
                 .getConstructor(OutputStream.class, Dimension.class);
 49  1254
     }
 50  
 
 51  
     /** {@inheritDoc} */
 52  
     public Dimension convert(final Node doc, final LayoutContext context,
 53  
             final OutputStream outStream) throws IOException {
 54  209
         final VectorGraphics tempg = this.createGraphics(
 55  
                 new ByteArrayOutputStream(), new Dimension(1, 1));
 56  209
         final JEuclidView view = new JEuclidView(doc, context, tempg);
 57  209
         final int ascent = (int) Math.ceil(view.getAscentHeight());
 58  209
         final Dimension size = new Dimension(
 59  
                 (int) Math.ceil(view.getWidth()), (int) Math.ceil(view
 60  
                         .getDescentHeight())
 61  
                         + ascent);
 62  
 
 63  209
         final VectorGraphics g = this.createGraphics(outStream, size);
 64  209
         g.setCreator("JEuclid (from MathML)");
 65  209
         g.startExport();
 66  209
         view.draw(g, 0, ascent);
 67  209
         g.endExport();
 68  
 
 69  209
         return size;
 70  
     }
 71  
 
 72  
     /** {@inheritDoc} */
 73  
     public DocumentWithDimension convert(final Node doc,
 74  
             final LayoutContext context) {
 75  0
         return null;
 76  
     }
 77  
 
 78  
     private VectorGraphics createGraphics(final OutputStream os,
 79  
             final Dimension d) throws IOException {
 80  
         try {
 81  418
             return this.streamConst.newInstance(os, d);
 82  0
         } catch (final InvocationTargetException e) {
 83  0
             throw new IOException(e.toString());
 84  0
         } catch (final IllegalArgumentException e) {
 85  0
             throw new IOException(e.toString());
 86  0
         } catch (final InstantiationException e) {
 87  0
             throw new IOException(e.toString());
 88  0
         } catch (final IllegalAccessException e) {
 89  0
             throw new IOException(e.toString());
 90  
         }
 91  
     }
 92  
 }