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: MathMLParserSupport.java,v 2bab6eb875e8 2010/08/11 16:45:50 max $ */
018    
019    package net.sourceforge.jeuclid;
020    
021    import java.io.File;
022    import java.io.FileInputStream;
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.StringReader;
026    
027    import javax.annotation.concurrent.ThreadSafe;
028    import javax.xml.parsers.DocumentBuilder;
029    import javax.xml.parsers.ParserConfigurationException;
030    import javax.xml.transform.stream.StreamSource;
031    
032    import net.sourceforge.jeuclid.parser.Parser;
033    
034    import org.w3c.dom.Document;
035    import org.xml.sax.SAXException;
036    
037    /**
038     * Utility class for the support parsing MathML and OpenDocument Formula (ODF)
039     * files.
040     * <p>
041     * This class supports parsing of files that are either MathML or OpenDocument
042     * Formula (ODF) files. It also supports parsing MathML from a given text
043     * string.
044     * 
045     * @version $Revision: 2bab6eb875e8 $
046     */
047    @ThreadSafe
048    public final class MathMLParserSupport {
049    
050        /**
051         * Logger for this class
052         */
053        // private static final Log LOGGER = LogFactory
054        // .getLog(MathMLParserSupport.class);
055        private MathMLParserSupport() {
056        }
057    
058        /**
059         * Creates a DocumentBuilder that can be used to parse MathML documents
060         * into a standard DOM model.
061         * 
062         * @return a DocumentBuilder instance that is configured for MathML
063         * @throws ParserConfigurationException
064         *             if the builder could not be configured properly.
065         */
066        public static DocumentBuilder createDocumentBuilder()
067                throws ParserConfigurationException {
068            final DocumentBuilder builder = Parser.getInstance()
069                    .getDocumentBuilder();
070            return builder;
071        }
072    
073        /**
074         * Parse an input stream in MathML XML format.
075         * 
076         * @param inStream
077         *            the stream to parse.
078         * @return the DOM Tree
079         * @throws SAXException
080         *             if a parse error occurs.
081         * @throws IOException
082         *             if a read I/O error occurs.
083         */
084        public static Document parseInputStreamXML(final InputStream inStream)
085                throws SAXException, IOException {
086            return Parser.getInstance().parseStreamSourceAsXml(
087                    new StreamSource(inStream));
088        }
089    
090        /**
091         * Parse an input stream in ODF format.
092         * 
093         * @param inStream
094         *            the stream to parse.
095         * @return the DOM Tree
096         * @throws SAXException
097         *             if a parse error occurs.
098         * @throws IOException
099         *             if a read I/O error occurs.
100         */
101        public static Document parseInputStreamODF(final InputStream inStream)
102                throws SAXException, IOException {
103            return Parser.getInstance().parseStreamSourceAsOdf(
104                    new StreamSource(inStream));
105        }
106    
107        /**
108         * Parse an input file and return the DOM tree.
109         * <p>
110         * This function will auto-detect if the given input is in MathML or ODF
111         * format.
112         * 
113         * @param inFile
114         *            the file to parse.
115         * @return the DOM Tree
116         * @throws SAXException
117         *             if a parse error occurs.
118         * @throws IOException
119         *             if a read I/O error occurs.
120         */
121        public static Document parseFile(final File inFile) throws SAXException,
122                IOException {
123            return Parser.getInstance().parseStreamSource(
124                    new StreamSource(new FileInputStream(inFile)));
125        }
126    
127        /**
128         * Create a DOM Document from a given string containing MathML content.
129         * This function uses a DOM Parser configured for MathML.
130         * 
131         * @param content
132         *            A String containing MathML.
133         * @return a DOM Document.
134         * @throws SAXException
135         *             a parsing error occurred.
136         * @throws ParserConfigurationException
137         *             a configuration error occurred.
138         * @throws IOException
139         *             for any other IO exception.
140         */
141        public static Document parseString(final String content)
142                throws SAXException, ParserConfigurationException, IOException {
143            return Parser.getInstance().parseStreamSourceAsXml(
144                    new StreamSource(new StringReader(content)));
145        }
146    
147    }