*@return !ToDo (Return description)
*@exception MalformedURLException !ToDo (Exception description)
***************************************/
public static HTTPSampler createUrlFromAnchor(String parsedUrlString, HTTPSampler context) throws MalformedURLException
{
HTTPSampler url = new HTTPSampler();
url.setDomain(context.getDomain());
url.setProtocol(context.getProtocol());
url.setPort(context.getPort());
// In JDK1.3, we can get the path using getPath(). However, in JDK1.2, we have to parse
// the file to obtain the path. In the source for JDK1.3.1, they determine the path to
// be from the start of the file up to the LAST question mark (if any).
String contextPath = null;
String contextFile = context.getPath();
int indexContextQuery = contextFile.lastIndexOf('?');
if(indexContextQuery != -1)
contextPath = contextFile.substring(0, indexContextQuery);
else
contextPath = contextFile;
int queryStarts = parsedUrlString.indexOf("?");
if(queryStarts == -1)
queryStarts = parsedUrlString.length();
if(parsedUrlString.startsWith("/"))
url.setPath(parsedUrlString.substring(0, queryStarts));
else if(parsedUrlString.startsWith(".."))
url.setPath(contextPath.substring(0, contextPath.substring(0,
contextPath.lastIndexOf("/")).lastIndexOf("/")) +
parsedUrlString.substring(2, queryStarts));
else if(!parsedUrlString.toLowerCase().startsWith("http"))
url.setPath(contextPath.substring(0, contextPath.lastIndexOf("/")) +
"/" + parsedUrlString.substring(0, queryStarts));
else
{
URL u = new URL(parsedUrlString);
// Determine the path. (See JDK1.2/1.3 comment above.)
String uPath = null;
String uFile = u.getFile();
int indexUQuery = uFile.lastIndexOf('?');
if(indexUQuery != -1)
uPath = uFile.substring(0, indexUQuery);
else
uPath = uFile;
url.setPath(uPath);
url.setDomain(u.getHost());
url.setProtocol(u.getProtocol());
url.setPort(u.getPort());
}
if(queryStarts < parsedUrlString.length())
url.parseArguments(parsedUrlString.substring(queryStarts + 1));
return url;
}