69 lines
2.3 KiB
Java
69 lines
2.3 KiB
Java
|
package com.ibm.wala.cast.js.html;
|
||
|
|
||
|
import java.net.MalformedURLException;
|
||
|
import java.net.URL;
|
||
|
|
||
|
public class UrlManipulator {
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @param urlFound the link as appear
|
||
|
* @param context the URL in which the link appeared
|
||
|
* @return
|
||
|
* @throws MalformedURLException
|
||
|
*/
|
||
|
public static URL relativeToAbsoluteUrl(String urlFound, URL context) throws MalformedURLException {
|
||
|
urlFound = urlFound.replace("\\", "/").toLowerCase();
|
||
|
|
||
|
URL absoluteUrl;
|
||
|
if (!isAbsoluteUrl(urlFound)) {
|
||
|
if (urlFound.startsWith("//")) {
|
||
|
//create URL taking only the protocol from the context
|
||
|
String origHostAndPath = urlFound.substring(2);// removing "//"
|
||
|
String host;
|
||
|
String path;
|
||
|
int indexOf = origHostAndPath.indexOf("/");
|
||
|
if (indexOf > 0) {
|
||
|
host = origHostAndPath.substring(0, indexOf);
|
||
|
path = origHostAndPath.substring(indexOf);
|
||
|
} else {
|
||
|
host = origHostAndPath;
|
||
|
path = "";
|
||
|
}
|
||
|
absoluteUrl = new URL(context.getProtocol(), host, path);
|
||
|
} else if (urlFound.startsWith("/")) {
|
||
|
//create URL taking the protocol and the host from the context
|
||
|
absoluteUrl = new URL(context.getProtocol(), context.getHost(), urlFound);
|
||
|
} else {
|
||
|
//"concat" URL to context
|
||
|
int backDir = 0; // removing directories due to "../"
|
||
|
while(urlFound.startsWith("../")){
|
||
|
urlFound = urlFound.substring(3);
|
||
|
backDir++;
|
||
|
}
|
||
|
StringBuilder contextPath = new StringBuilder();
|
||
|
String path = context.getPath().replace("\\", "/");
|
||
|
boolean isContextDirectory = path.endsWith("/");
|
||
|
String[] split = path.split("/");
|
||
|
// we are also removing last element in case of a directory
|
||
|
int rightTrimFromPath = (isContextDirectory ? 0 : 1) + backDir;
|
||
|
|
||
|
for (int i = 0; i < split.length - rightTrimFromPath; i++) {
|
||
|
contextPath.append(split[i]);
|
||
|
contextPath.append("/");
|
||
|
}
|
||
|
absoluteUrl = new URL(context.getProtocol(), context.getHost(), contextPath.toString() + urlFound);
|
||
|
}
|
||
|
} else{
|
||
|
absoluteUrl = new URL(urlFound);
|
||
|
}
|
||
|
return absoluteUrl;
|
||
|
}
|
||
|
|
||
|
private static boolean isAbsoluteUrl(String orig) {
|
||
|
return orig.startsWith("http");
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|