protected URLConnection openConnection(URL url) throws IOException
{
VFSRegistry registry = VFSRegistry.getInstance();
VirtualFile vf = registry.getFile(url);
if (vf != null)
return new VirtualFileURLConnection(url, vf);
String file = URLDecoder.decode(url.toExternalForm(), "UTF-8").substring(getProtocolNameLength() + 1); // strip out vfs protocol + :
URL vfsurl = null;
String relative;
String queryStr = url.getQuery();
if (queryStr != null)
file = file.substring(0, file.lastIndexOf('?'));
File fp = new File(file);
if (fp.exists())
{
vfsurl = fp.getParentFile().toURI().toURL();
relative = fp.getName();
}
else
{
File curr = fp;
relative = fp.getName();
while ((curr = curr.getParentFile()) != null)
{
if (curr.exists())
{
vfsurl = curr.toURI().toURL();
break;
}
else
{
relative = curr.getName() + "/" + relative;
}
}
}
if (vfsurl == null)
throw new IOException("VFS file does not exist: " + url);
if (queryStr != null)
vfsurl = new URL(vfsurl + "?" + queryStr);
return new VirtualFileURLConnection(url, vfsurl, relative);
}