package net.sourceforge.rtf;

import java.util.HashMap;
import java.util.Map;

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

import org.apache.commons.logging.LogFactory;

public class SimpleURIResolver implements URIResolver {
	private static org.apache.commons.logging.Log logger = LogFactory.getLog(SimpleURIResolver.class);

	private static Map<String, Source> dtdMap = new HashMap<String, Source>();
	static {
		dtdMap.put("xhtml1-transitional.dtd", new StreamSource(SimpleURIResolver.class.getResourceAsStream("xhtml1-transitional.dtd")));
		dtdMap.put("xhtml-special.ent", new StreamSource(SimpleURIResolver.class.getResourceAsStream("xhtml-special.ent")));
		dtdMap.put("xhtml-symbol.ent", new StreamSource(SimpleURIResolver.class.getResourceAsStream("xhtml-symbol.ent")));
		dtdMap.put("xhtml-lat1.ent", new StreamSource(SimpleURIResolver.class.getResourceAsStream("xhtml-lat1.ent")));
	}
	
	public Source resolve(String base, String name) throws TransformerException {
		logger.error("trying to resolve URI " + name);
		String dtdName = name.substring(name.lastIndexOf("/") + 1);
		Source source = dtdMap.get(dtdName);
		if (source != null) {
			return source;
		}
		throw new TransformerException("Unknown external resource " + base + "/" + name);
	}
}
