// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class DomVal implements ErrorHandler, EntityResolver {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(new DomVal());
        db.setEntityResolver(new DomVal());
        Document doc = db.parse(new File("C:\\medarbejder_extdtd.xml"));
    }
    public void error(SAXParseException exception) throws SAXException {
        System.out.println(exception.getMessage());
    }
    public void fatalError(SAXParseException exception) throws SAXException {
        System.out.println(exception.getMessage());
    }
    public void warning(SAXParseException exception) throws SAXException {
        System.out.println(exception.getMessage());
    }
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        if(systemId.endsWith("medarbejder.dtd")) {
            return new InputSource("C:\\medarbejder.dtd");
        } else {
            return null;
        }
    }
}
