1 /*
2 * Copyright 2002 - 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: Converter.java 319 2007-05-21 14:55:54Z maxberger $ */
18
19 package net.sourceforge.jeuclid;
20
21 import java.awt.image.BufferedImage;
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Vector;
27
28 import net.sourceforge.jeuclid.converter.ConverterRegistry;
29
30 import org.w3c.dom.Document;
31 import org.xml.sax.SAXException;
32
33 /**
34 * Utility class for conversion from MathML to other formats.
35 * <p>
36 * This class supports easy conversion from a MathML or ODF file to any
37 * supported output format.
38 * <p>
39 * Currently supported output formats:
40 * <ul>
41 * <li>All images supported by ImageIO
42 * <li>SVG if Batik is in the classpath.
43 * <li>EMF, GIF, PDF, PS, SVG, SWF if FreeHEP is in the classpath.
44 * </ul>
45 * Conversion is done through the Converter class in the new interface. This
46 * class will be deprecated some time after the 3.0 release. However, the new
47 * API is not to be considered stable yet, so please use these functions for
48 * the time being.
49 *
50 * @author Max Berger
51 * @version $Revision: 319 $
52 */
53 public final class Converter {
54
55 /**
56 * Mime type for SVG.
57 */
58 public static final String TYPE_SVG = "image/svg+xml";
59
60 /**
61 * File extension for SVG.
62 */
63 public static final String EXTENSION_SVG = "svg";
64
65 /**
66 * Logger for this class
67 */
68 // unused
69 // private static final Log LOGGER = LogFactory.getLog(Converter.class);
70 private Converter() {
71 // Empty on purpose.
72 }
73
74 /**
75 * Converts an existing file from MathML or ODF to the given type.
76 *
77 * @param inFile
78 * input file.
79 * @param outFile
80 * output file.
81 * @param outFileType
82 * mimetype for the output file.
83 * @return true if the conversion was successful.
84 * @throws IOException
85 * if an I/O error occurred during read or write.
86 */
87 public static boolean convert(final File inFile, final File outFile,
88 final String outFileType) throws IOException {
89 return net.sourceforge.jeuclid.converter.Converter.getConverter()
90 .convert(inFile, outFile, outFileType) != null;
91 }
92
93 /**
94 * Renders a document into an image.
95 *
96 * @param doc
97 * DOM MathML document
98 * @param params
99 * parameters
100 * @return the rendered image
101 * @throws SAXException
102 * if an error occurs reading the DOM document
103 * @throws IOException
104 * if an I/O error occurred.
105 */
106 public static BufferedImage render(final Document doc,
107 final Map<ParameterKey, String> params) throws SAXException,
108 IOException {
109 return net.sourceforge.jeuclid.converter.Converter.getConverter()
110 .render(
111 MathMLParserSupport.createMathBaseFromDocument(doc,
112 params));
113 }
114
115 /**
116 * Converts an existing file from MathML or ODF to the given type.
117 *
118 * @param inFile
119 * input file.
120 * @param outFile
121 * output file.
122 * @param params
123 * rendering parameters.
124 * @return true if the conversion was successful.
125 * @throws IOException
126 * if an I/O error occurred during read or write.
127 */
128 public static boolean convert(final File inFile, final File outFile,
129 final Map<ParameterKey, String> params) throws IOException {
130 return net.sourceforge.jeuclid.converter.Converter.getConverter()
131 .convert(inFile, outFile, params) != null;
132 }
133
134 /**
135 * Converts an existing file from MathML or ODF to the given type.
136 *
137 * @param doc
138 * input document.
139 * @param outFile
140 * output file.
141 * @param params
142 * parameter set to use for conversion.
143 * @return true if the conversion was successful.
144 * @throws IOException
145 * if an I/O error occurred during read or write.
146 */
147 public static boolean convert(final Document doc, final File outFile,
148 final Map<ParameterKey, String> params) throws IOException {
149 return net.sourceforge.jeuclid.converter.Converter.getConverter()
150 .convert(doc, outFile, params) != null;
151 }
152
153 /**
154 * Retrieve a list of available mime types for conversion.
155 *
156 * @return a List<String> containing all valid mime-types.
157 */
158 public static List<String> getAvailableOutfileTypes() {
159 return new Vector<String>(ConverterRegistry.getRegisty()
160 .getAvailableOutfileTypes());
161 }
162
163 /**
164 * Returns the file suffix suitable for the given mime type.
165 * <p>
166 * This function is not fully implemented yet
167 *
168 * @param mimeType
169 * a mimetype, as returned by
170 * {@link #getAvailableOutfileTypes()}.
171 * @return the three letter suffix common for this type.
172 */
173 public static String getSuffixForMimeType(final String mimeType) {
174 return ConverterRegistry.getRegisty().getSuffixForMimeType(mimeType);
175 }
176
177 /**
178 * Returns the MimeType for a given suffix.
179 *
180 * @param suffix
181 * the suffix, e.g. png
182 * @return the mime-type
183 */
184 public static String getMimeTypeForSuffix(final String suffix) {
185 return ConverterRegistry.getRegisty().getMimeTypeForSuffix(suffix);
186 }
187 }