001    /*
002     * Copyright 2002 - 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: Converter.java 319 2007-05-21 14:55:54Z maxberger $ */
018    
019    package net.sourceforge.jeuclid;
020    
021    import java.awt.image.BufferedImage;
022    import java.io.File;
023    import java.io.IOException;
024    import java.util.List;
025    import java.util.Map;
026    import java.util.Vector;
027    
028    import net.sourceforge.jeuclid.converter.ConverterRegistry;
029    
030    import org.w3c.dom.Document;
031    import org.xml.sax.SAXException;
032    
033    /**
034     * Utility class for conversion from MathML to other formats.
035     * <p>
036     * This class supports easy conversion from a MathML or ODF file to any
037     * supported output format.
038     * <p>
039     * Currently supported output formats:
040     * <ul>
041     * <li>All images supported by ImageIO
042     * <li>SVG if Batik is in the classpath.
043     * <li>EMF, GIF, PDF, PS, SVG, SWF if FreeHEP is in the classpath.
044     * </ul>
045     * Conversion is done through the Converter class in the new interface. This
046     * class will be deprecated some time after the 3.0 release. However, the new
047     * API is not to be considered stable yet, so please use these functions for
048     * the time being.
049     * 
050     * @author Max Berger
051     * @version $Revision: 319 $
052     */
053    public final class Converter {
054    
055        /**
056         * Mime type for SVG.
057         */
058        public static final String TYPE_SVG = "image/svg+xml";
059    
060        /**
061         * File extension for SVG.
062         */
063        public static final String EXTENSION_SVG = "svg";
064    
065        /**
066         * Logger for this class
067         */
068        // unused
069        // private static final Log LOGGER = LogFactory.getLog(Converter.class);
070        private Converter() {
071            // Empty on purpose.
072        }
073    
074        /**
075         * Converts an existing file from MathML or ODF to the given type.
076         * 
077         * @param inFile
078         *            input file.
079         * @param outFile
080         *            output file.
081         * @param outFileType
082         *            mimetype for the output file.
083         * @return true if the conversion was successful.
084         * @throws IOException
085         *             if an I/O error occurred during read or write.
086         */
087        public static boolean convert(final File inFile, final File outFile,
088                final String outFileType) throws IOException {
089            return net.sourceforge.jeuclid.converter.Converter.getConverter()
090                    .convert(inFile, outFile, outFileType) != null;
091        }
092    
093        /**
094         * Renders a document into an image.
095         * 
096         * @param doc
097         *            DOM MathML document
098         * @param params
099         *            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&lt;String&gt; 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    }