public static URL resolve(String s,
URL url) throws MalformedURLException {
URL resolve = null;
// construct a URL via the following heuristics:
//
// if arg contains ":/" then
// assume a valid uri
// else if url arg is specified
// construct uri
// else if an absolute path then
// construct file uri
// else
// construct file uri by prepending the
// working directory
if (s.indexOf("://") > -1 ||
s.indexOf("file:") > -1 ) {
resolve = new URL(s);
} else if (url != null) {
resolve = new URL(url, s);
} else if (s.startsWith(File.separator) ||
s.startsWith("/") ||
(s.length() >= 2 &&
Character.isLetter(s.charAt(0)) &&
s.charAt(1) == ':')) {
String fName = s;
try {
fName = (String)(new File(s)).getCanonicalPath();
} catch (NullPointerException npe) {
} catch (IOException npe) {
}
resolve = new URL("file", "", fName);
} else {
String path = System.getProperty("user.dir") +
File.separator + s;
resolve = new URL("file", "", path);
}
if (! resolve.getProtocol().equalsIgnoreCase(
org.apache.tomcat.core.Constants.Request.WAR) &&
resolve.getFile().toLowerCase().endsWith(
"." + Constants.MIME.WAR)) {
URL u = new URL(Constants.MIME.WAR + ":" +
resolve.toString());
resolve = u;
}
resolve = new URL(trim(resolve.toString(), ".", ".."));
resolve = new URL(trim(resolve.toString(), "./"));
return resolve;
}
|