/* $Id: SaxEntityResolver.java,v 1.14 2005/08/19 04:14:25 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2005,  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.xml.parser;

import java.io.FileNotFoundException;
import java.io.InputStream;

import org.qedeq.kernel.context.KernelContext;
import org.qedeq.kernel.log.Trace;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;


/**
 * Resolve qedeq schema.
 *
 * @version $Revision: 1.14 $
 * @author Michael Meyling
 */
public final class SaxEntityResolver implements EntityResolver {

    /* (non-Javadoc)
     * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
     */
    public InputSource resolveEntity(final String publicId, final String systemId)
            throws FileNotFoundException {
        final String method = "resolveEntity";
        Trace.traceParam(this, method, "systemId", systemId);
        Trace.traceParam(this, method, "publicId", publicId);
        if ((systemId == null)) {
            return null;
        }
        if (systemId.equals("http://www.qedeq.org/" + KernelContext.getKernelVersionDirectory()
                + "/xml/qedeq.xsd")) {
            InputStream in = SaxEntityResolver.class.getResourceAsStream(
                "/org/qedeq/kernel/xml/schema/qedeq.xsd");
            if (in == null) {
                throw new FileNotFoundException("/org/qedeq/kernel/xml/schema/qedeq.xsd");
            }
            InputSource inputSource = new InputSource(in);
            inputSource.setPublicId(publicId);
            inputSource.setSystemId(systemId);
            return inputSource;
        }
        // TODO mime 20050205: do anything else? e.g. warn someone?
        Trace.trace(this, method, "unknown entity");
        return null;
    }
}
