Sorry for posting this invalid fragment. I have a complete "program" that
looks like
package examples;
import java.io.*;
import org.xml.sax.*;
import org.apache.xerces.parsers.*;
class Resolver implements EntityResolver {
public InputSource resolveEntity(String pubId, String sysId)
throws IOException, SAXException {
if(pubId != null) {
System.out.println("resolving entity "+ pubId+ " SYSTEM "+sysId);
InputSource is = new InputSource("file:///c:/dtd/test.dtd");
return is;
} else
return null;
}
}
public class MyParser extends SAXParser {
public MyParser() {
setEntityResolver(new Resolver());
}
public static void main(String args[]) {
MyParser parser = new MyParser();
try {
System.out.println("start parse");
parser.parse(args[0]);
System.out.println("end parse");
} catch(org.xml.sax.SAXParseException e) {
System.out.println("SaxParseException: "+e.getMessage());
System.out.println(getLocationString(e));
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace(System.out);
}
}
private static String getLocationString(SAXParseException ex) {
StringBuffer str = new StringBuffer();
String systemId = ex.getSystemId();
if (systemId != null) {
int index = systemId.lastIndexOf('/');
if (index != -1)
systemId = systemId.substring(index + 1);
str.append(systemId);
}
str.append(':');
str.append(ex.getLineNumber());
str.append(':');
str.append(ex.getColumnNumber());
return str.toString();
}
}
When running this program against an XML-file
<?xml version="1.0" ?>
<!DOCTYPE test PUBLIC "-//Private//Test DTD//EN" "doctypes/test.dtd">
<test>
<title>hello</title>
</test>
I get the following output:
"
start parse
resolving entity -//Private//Test DTD//EN doctypes/test.dtd
SAXParseException: File "doctypes/test.dtd" not found.
test.xml:-1:-1
"
The entity resolver obviously gets called but still the parser tries to use
the System Identifier of the instance that points to a non-existing file
(that was intended!)
Armin