/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.xml;

import java.io.InputStream;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;

/**
 * This class uses it's classloader to locate files. It was developed so the
 * blog feeds in Bodington could use includes. The problem with includes in a
 * container enviroment is we don't have the idea of a current working directory
 * and we don't know the absolute URL of where the webapp is deployed.
 * @author buckett
 */
public class ClassURIResolver implements URIResolver
{

    /**
     * Tries to find the file on the classpath. Currently ignores the base as it
     * is not needed at the moment.
     * @param href The file to look for.
     * @param base Where to start searching (not used).
     * @return A source to the located resource, null if it couldn't find it.
     * @see javax.xml.transform.URIResolver#resolve(java.lang.String,
     *           java.lang.String)
     */
    public Source resolve(String href, String base) throws TransformerException
    {
        InputStream input = getClass().getResourceAsStream(href);
        if (input == null)
        {
            return null;
        }
        return new StreamSource(getClass().getResourceAsStream(href));
    }

}
