// 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 java.net.MalformedURLException;

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

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

public class DTDProblem {
    public static class DTDIgnoreURLPrefix implements EntityResolver {
        private String prefix;
        public DTDIgnoreURLPrefix(String prefix) {
            this.prefix = prefix;
        }
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            if(systemId.startsWith(prefix)) {
                return new InputSource(new StringReader(""));
            } else {
                return null;
            }
        }
    }
    private static void dump(boolean feat, boolean dtdh, String sts) {
        System.out.printf("feature = %-5b, DTD handler = %-5b : %s\n", feat, dtdh, sts);    
    }
    public static void test(String xml, boolean feat, boolean dtdh) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        if(dbf.getClass().getName().startsWith("org.apache.xerces")) {
            dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", feat);
        }
        DocumentBuilder db = dbf.newDocumentBuilder();
        if(dtdh) {
            db.setEntityResolver(new DTDIgnoreURLPrefix("foobar://"));
        }
        try {
            @SuppressWarnings("unused")
            Document doc = db.parse(new InputSource(new StringReader(xml )));
            dump(feat, dtdh, "OK");
        } catch(MalformedURLException|SAXParseException ex) {
            dump(feat, dtdh, "Problem");
        }
    }
    public static void test() throws ParserConfigurationException, SAXException, IOException {
        String xml = "<!DOCTYPE test PUBLIC \"test\" \"foobar://test\">\r\n<test/>";
        for(int i = 0; i <= 1; i++)
            for(int j = 0; j <= 1; j++)
                    test(xml, i > 0, j > 0);
    }
    public static void main(String[] args) throws Exception {
        System.out.println("Default:");;
        test();
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
        System.out.println("Xerces:");;
        test();
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.crimson.jaxp.DocumentBuilderFactoryImpl");
        System.out.println("Crimson:");;
        test();
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "gnu.xml.dom.JAXPFactory");
        System.out.println("Classpath:");
        test();
    }

}
