// 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.IOException;
import java.io.StringReader;

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

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

public class NotAvailableDTD {
    public static void main(String[] args) throws Exception {
        String xml = "<!DOCTYPE test PUBLIC \"test\" \"http://notexist/test\">\r\n" +
                     "<test/>";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setEntityResolver(new DTDHandler());
        Document doc = db.parse(new InputSource(new StringReader(xml )));
    }
}

class DTDHandler implements EntityResolver {
    @Override
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        if(systemId.equals("http://notexist/test")) {
            return new InputSource(new StringReader("<!ELEMENT test (#PCDATA)>"));
        } else {
            return null;
        }
    }
}
