/* $Id: LoadXmlOperatorListUtility.java,v 1.1 2006/10/20 20:23:05 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2006,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

package org.qedeq.kernel.parser;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.qedeq.kernel.log.Trace;
import org.qedeq.kernel.xml.handler.parser.ParserHandler;
import org.qedeq.kernel.xml.parser.SaxDefaultHandler;
import org.qedeq.kernel.xml.parser.SaxParser;
import org.qedeq.kernel.xml.parser.SyntaxException;
import org.qedeq.kernel.xml.parser.XmlFilePositionException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


/**
 * Load operator list from an XML file.
 *
 * @version $Revision: 1.1 $
 * @author  Michael Meyling
 */
public final class LoadXmlOperatorListUtility  {

    /**
     * Constructor.
     */
    private LoadXmlOperatorListUtility() {
        // nothing to do
    }

    /**
     * Get operator list out of XML file.
     *
     * @param   from            Read this XML file.
     * @return  Operator list.
     * @throws  XmlFilePositionException    Loading failed.
     */
    public static List getOperatorList(final String from) throws XmlFilePositionException {
        final String method = "generate(String, String, String, String)";
        URL url = null;
        try {
            Trace.traceBegin(LoadXmlOperatorListUtility.class, method);
            Trace.traceParam(LoadXmlOperatorListUtility.class, method, "from", from);
            url = LoadXmlOperatorListUtility.class.getResource("/" + from);
            if (url == null) {
                throw new FileNotFoundException(from);
            }
            SaxDefaultHandler handler = new SaxDefaultHandler();
            ParserHandler simple = new ParserHandler(handler);
            handler.setBasisDocumentHandler(simple);
            SaxParser parser = new SaxParser(handler);
            try {
                parser.parse(url);
            } catch (SAXException ex) {
                if (parser.getExceptionList().size() > 0) {
                    final Exception exc = parser.getExceptionList().get(0);
                    if (exc instanceof SyntaxException) {  // TODO mime 20050826: give more than one
                        throw new XmlFilePositionException((SyntaxException) exc);
                    }
                }
                throw ex;
            }

            return simple.getOperators();
        } catch (RuntimeException e) {
            Trace.trace(LoadXmlOperatorListUtility.class, method, e);
            throw new XmlFilePositionException(url, e);
        } catch (IOException e) {
            Trace.trace(LoadXmlOperatorListUtility.class, method, e);
            throw new XmlFilePositionException(url, e);
        } catch (ParserConfigurationException e) {
            Trace.trace(LoadXmlOperatorListUtility.class, method, e);
            throw new XmlFilePositionException(url, e);
        } catch (final SAXParseException e) {
            Trace.trace(LoadXmlOperatorListUtility.class, method, e);
            throw new XmlFilePositionException(url, e);
        } catch (SAXException e) {
            Trace.trace(LoadXmlOperatorListUtility.class, method, e);
            throw new XmlFilePositionException(url, e);
        } catch (javax.xml.parsers.FactoryConfigurationError e) {
            Trace.trace(LoadXmlOperatorListUtility.class, method, e);
            final String msg = "SAX Parser not in classpath, "
                + "add for example \"xercesImpl.jar\" and \"xml-apis.jar\".";
            throw new XmlFilePositionException(url, new IOException(msg));
        } finally {
            Trace.traceEnd(LoadXmlOperatorListUtility.class, method);
        }
    }

}
