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: 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              throws NoSuchMethodException {
46  
47          this.streamConst = ((Class<VectorGraphics>) converterClass)
48                  .getConstructor(OutputStream.class, Dimension.class);
49      }
50  
51      /** {@inheritDoc} */
52      public Dimension convert(final Node doc, final LayoutContext context,
53              final OutputStream outStream) throws IOException {
54          final VectorGraphics tempg = this.createGraphics(
55                  new ByteArrayOutputStream(), new Dimension(1, 1));
56          final JEuclidView view = new JEuclidView(doc, context, tempg);
57          final int ascent = (int) Math.ceil(view.getAscentHeight());
58          final Dimension size = new Dimension(
59                  (int) Math.ceil(view.getWidth()), (int) Math.ceil(view
60                          .getDescentHeight())
61                          + ascent);
62  
63          final VectorGraphics g = this.createGraphics(outStream, size);
64          g.setCreator("JEuclid (from MathML)");
65          g.startExport();
66          view.draw(g, 0, ascent);
67          g.endExport();
68  
69          return size;
70      }
71  
72      /** {@inheritDoc} */
73      public DocumentWithDimension convert(final Node doc,
74              final LayoutContext context) {
75          return null;
76      }
77  
78      private VectorGraphics createGraphics(final OutputStream os,
79              final Dimension d) throws IOException {
80          try {
81              return this.streamConst.newInstance(os, d);
82          } catch (final InvocationTargetException e) {
83              throw new IOException(e.toString());
84          } catch (final IllegalArgumentException e) {
85              throw new IOException(e.toString());
86          } catch (final InstantiationException e) {
87              throw new IOException(e.toString());
88          } catch (final IllegalAccessException e) {
89              throw new IOException(e.toString());
90          }
91      }
92  }