/*
 * Created on 23-Feb-2005
 */

package org.jafer.util.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));
    }

}