package net.sourceforge.rtf;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.LogFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

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

	private Map<String, InputSource> dtdMap = new HashMap<String, InputSource>();
	
	public CachedHtmlDtdEntityResolver() {
		dtdMap.put("xhtml1-transitional.dtd", new InputSource(this.getClass().getClassLoader().getResourceAsStream("/net/sourceforge/rtf/dtd/xhtml1-transitional.dtd")));
		dtdMap.put("xhtml-special.ent", new InputSource(this.getClass().getResourceAsStream("/net/sourceforge/rtf/dtd/xhtml-special.ent")));
		dtdMap.put("xhtml-symbol.ent", new InputSource(this.getClass().getResourceAsStream("/net/sourceforge/rtf/dtd/xhtml-symbol.ent")));
		dtdMap.put("xhtml-lat1.ent", new InputSource(this.getClass().getResourceAsStream("/net/sourceforge/rtf/dtd/xhtml-lat1.ent")));
	}

	public InputSource resolveEntity(String base, String name)
			throws SAXException, IOException {
		String dtdName = name.substring(name.lastIndexOf("/") + 1);
		InputSource source = dtdMap.get(dtdName);
		this.getClass().getResourceAsStream("/net/sourceforge/rtf/dtd/xhtml1-transitional.dtd");
		if (source != null) {
			return source;
		}
		throw new SAXException("Unknown external resource " + base + "/" + name);
	}

}
