/* ======================================================================
   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.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * This attempts to find entities by looking on the classpath for them.
 * @author buckett
 */
public class ClasspathEntityResolver implements org.xml.sax.EntityResolver
{
    private static Logger log = Logger.getLogger(ClasspathEntityResolver.class);

    private Class clazz;
    
    /**
     * Creates a Classpath resolver using this classes classloader.
     */
    public ClasspathEntityResolver()
    {
        this.clazz = ClasspathEntityResolver.class;
    }
    
    /**
     * Creates a classpath resolver using the classloader of the supplied 
     * class.
     * @param clazz The class to use when looking for the entities.
     */
    public ClasspathEntityResolver( Class clazz )
    {
        this.clazz = clazz;
    }

    /**
     * Attempt to resolve entities from within the classpath when they just have a 
     * relative URL.
     * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
     */
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
    {
       log.debug("Trying to resolve PUBLIC ("+ publicId+ ") SYSTEM ("+ systemId+ ")" );

       InputSource source;
       if (publicId != null)
       {
           source = getInput(publicId);
           if (source != null)
               return source;
       }
       if (systemId != null && systemId.indexOf( ':' ) < 0)
       {
           source = getInput(systemId);
           if (source != null)
               return source;
       }
       return null;
    }
    
    private InputSource getInput(String file)
    {
        try
        {
            InputStream input = clazz.getResourceAsStream(file);
            clazz.getResource(file);
            if (input != null)
            {
                InputSource source = new InputSource(input);
                source.setSystemId(clazz.getResource(file).toString());
                return source;
            }
        }
        catch (Exception e)
        {}
        return null;
    }

}
