001    /*
002     * Copyright 2007 - 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: MathBaseFactory.java 434 2007-08-20 12:03:10Z maxberger $ */
018    
019    package net.sourceforge.jeuclid.parser;
020    
021    import java.io.IOException;
022    import java.util.Map;
023    
024    import javax.xml.parsers.ParserConfigurationException;
025    import javax.xml.transform.Source;
026    
027    import net.sourceforge.jeuclid.DOMBuilder;
028    import net.sourceforge.jeuclid.MathBase;
029    import net.sourceforge.jeuclid.ParameterKey;
030    
031    import org.w3c.dom.Node;
032    import org.xml.sax.SAXException;
033    
034    /**
035     * Factory class to create MathBase from JAXP Sources.
036     * 
037     * @author Max Berger
038     * @version $Revision: 434 $
039     */
040    public final class MathBaseFactory {
041    
042        private static MathBaseFactory mathBaseFactory;
043    
044        private final Parser parser;
045    
046        private MathBaseFactory() throws ParserConfigurationException {
047            this.parser = Parser.getParser();
048        }
049    
050        /**
051         * Retrieve the (singleton) MathBaseFactory object.
052         * 
053         * @return a MathBaseFactory.
054         * @throws ParserConfigurationException
055         *             when the internal (DOM) parser could not be created.
056         * 
057         */
058        public static synchronized MathBaseFactory getMathBaseFactory()
059                throws ParserConfigurationException {
060            if (MathBaseFactory.mathBaseFactory == null) {
061                MathBaseFactory.mathBaseFactory = new MathBaseFactory();
062            }
063            return MathBaseFactory.mathBaseFactory;
064        }
065    
066        /**
067         * Parse an input source and return the MathBase object.
068         * 
069         * @param params
070         *            set of parameters to use.
071         * @param source
072         *            the Source to use. Currently supported are
073         *            {@link javax.xml.transform.dom.DOMSource},
074         *            {@link javax.xml.transform.stream.StreamSource}
075         * @return the MathBase object.
076         * @throws IOException
077         *             if an I/O error occurs.
078         * @throws IllegalArgumentException
079         *             if the Source is of an unsupported object type.
080         */
081        public MathBase createMathBase(final Source source,
082                final Map<ParameterKey, String> params) throws IOException {
083    
084            try {
085                final Node node = this.parser.parse(source);
086    
087                final MathBase base = new MathBase(params);
088                new DOMBuilder(node, base);
089                return base;
090            } catch (final SAXException e) {
091                throw new IOException("Parse Error: " + e.getMessage());
092            }
093    
094        }
095    }