XML Entity Resolver

Today, while creating a xml entity resolver, i have got struck in a trifling situation. I was using the throws keyword for handling exception instead of try catch block at InputStream instance creation method for a particular url path-getInputStreamFromDB(String urlString), and due to that, in case of error, control was going to the ValidatingErrorHandler class (a error handler used by the parser), which was again pushing it to the ExternalEntityresolver class (an Entity Resolver) and this whole thing was going into a recursion.

Later, when I have used try-catch block to check the stacktrace, encountered the real culprit- a fresh exception. This exception originates at the time of URL instance creation for a given path String.

try{

path=“D:\\eclipseworkspace

String \\PIF_VALIDATION\\resource\\AddressBook-ZipToCityEPD.xsd”;

URL url = new URL(path);

URLConnection con = url.openConnection();

if(con != null){

inputStream = con.getInputStream();

}

}catch (MalformedURLException e) {

e.printStackTrace();// TODO: handle exception

java.net.MalformedURLException: unknown protocol: d

at java.net.URL.<init>(Unknown Source)

at java.net.URL.<init>(Unknown Source)

at java.net.URL.<init>(Unknown Source)

at com.XXX.xml.ExternalEntityResolver.getInputStreamFromDB(ExternalEntityResolver.java:80)

It seems as if String path is not interpreted as a file reference and because of this, it is throwing MalformedURLException with this “unkown protocol d” message. Preferably I feel it should be handled by URL class code by checking the format of the path String. For solving this problem, we need to create a valid URL in code itself.

Like-

Just put this line of code before the creation of URL instance-

path=new File(path).toURI().toString();

URL url = new URL(path);

There is one more way, more simpler one to achieve the purpose

Simply append “file:///” at the beginning of path String and it would solve the purpose.