// 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.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
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 DomValTrick 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 DomValTrick());
        db.setEntityResolver(new DomValTrick());
        Document doc = db.parse(new FakeInputStream("C:\\medarbejder_nodtd.xml",
                                                    "<?xml version=\"1.0\" encoding=\"iso-8859-1\" standalone=\"no\"?>\r\n",
                                                    "<!DOCTYPE medarbejderliste SYSTEM \"medarbejder.dtd\">\r\n"));
    }
    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;
        }
    }
}
class FakeInputStream extends InputStream {
    private static final int F = 1;
    private static final int X = 2;
    private static final int D = 3;
    private InputStream f;
    private InputStream x;
    private InputStream d;
    private int mode;
    public FakeInputStream(String xmlfnm, String xmldecl, String doctyp) throws IOException {
        f = new FileInputStream(xmlfnm);
        while(f.read() != '\n');
        x = new ByteArrayInputStream(xmldecl.getBytes());
        d = new ByteArrayInputStream(doctyp.getBytes());
        mode = X;
    }
    public int read() throws IOException {
        int c = -1;
        switch(mode) {
        case X:
            c = x.read();
            if(c < 0) {
                mode = D;
                c = d.read();
            }
            break;
        case F:
            c = f.read();
            break;
        case D:
            c = d.read();
            if(c < 0) {
                mode = F;
                c = f.read();
            }
            break;
        }
        return c;
    }
}
