
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.xerces.parsers.DOMParser;

/**
 * This is a utility class that enforces strict error checking. All
 * errors are logged and thrown.
 *
 * @version 1.0 (February 17, 2001)
 * @author Milind Gadre
 */
public class MyErrorHandler implements ErrorHandler
{
    public void warning(SAXParseException ex) throws SAXException {
        System.out.println("warn line: " + ex.getLineNumber() +
                   ", col: " + ex.getColumnNumber() +
                   ", sysid: " + ex.getSystemId() +
                   ", pubid: " + ex.getPublicId());
        throw ex;
    }

    public void error(SAXParseException ex) throws SAXException {
        System.out.println("error line: " + ex.getLineNumber() +
                   ", col: " + ex.getColumnNumber() +
                   ", sysid: " + ex.getSystemId() +
                   ", pubid: " + ex.getPublicId());
        throw ex;
    }

    public void fatalError(SAXParseException ex) throws SAXException {
        System.out.println("fatal line: " + ex.getLineNumber() +
                   ", col: " + ex.getColumnNumber() +
                   ", sysid: " + ex.getSystemId() +
                   ", pubid: " + ex.getPublicId());
        throw ex;
    }
}


/**
 * This class implements {@link org.xml.sax.EntityResolver EntityResolver}
 * to function kind of like a catalog. It resolves the PUBLIC identifiers
 * to my DTDs located in the classpath.
 *
 * @version 1.0 (March 03, 2001)
 * @author  Milind Gadre
 */
public class MyEntityResolver implements EntityResolver
{
    /**
     * This attempts to resolve the entity associated with the specified
     * public and system ids. If the systemId is empty, then we use the
     * publicId to locate the URL of the cataloged DTD file.
     */
    public InputSource resolveEntity(String publicId, String systemId)
        throws SAXException, IOException {

        if ("".equals(systemId)) {
            String dtd = getUrl(publicId);
            if (null != dtd) {
                // uses classpath to locate the dtd
                // modify as desired
                return new InputSource(getClass().getResourceAsStream(dtd));
            }
        }

        return null;
    }


    // -- BEGIN STATIC SECTION --

    /**
     * Return the URL of the DTD corresponding to the publicId.
     */
    private static final String getUrl(String publicId) {
        return (String) catalog.get(publicId);
    }

    /**
     * Contains the URLs of the publicly identified My Company DTDs.
     */
    private static final HashMap catalog = new HashMap();

    /**
     * Initialize the {@link #catalog catalog}.
     */
    static {
        // items to read from the classpath have a leading forward-slash
        // look to the java.lang.Class#getResourceAsStream method
        catalog.put("-//My Company//Legacy DTD//EN",
                    "/my/classpath/location/apmml.dtd");
        
        // add similar lines for DTDs to be resolved via the classpath
    }

}

