www.pudn.com > htmlsaver.rar > FileURLChange.java
import java.lang.*;
import java.net.*;
import java.io.*;
/* give a base url, then change the file or path
*/
public class FileURLChange
{
/* the base URL, a host or a directory */
protected URL baseURL;
/* the base URL directory */
protected URL baseDir;
/* the host name */
protected String host;
/* the base file's */
protected URL fileURL;
public FileURLChange(String url) throws UnknowFileURL
{
try {
baseURL = new URL(url);
}
catch (MalformedURLException e) {
System.err.println("URL ERROR");
throw new UnknowFileURL();
}
host = baseURL.getHost();
if (baseURL.getQuery() != null)
{
System.err.println("Query ERROR");
throw new UnknowFileURL();
}
String fileName = getBaseFile(baseURL);
if (fileName == null)
{
System.err.println("file URL Error");
throw new UnknowFileURL();
}
else
{
try {
fileURL = new URL(fileName);
}
catch (MalformedURLException e)
{
System.err.println("file URL Error");
throw new UnknowFileURL();
}
}
try {
baseDir = new URL(getFileDir(fileURL.toString()));
}
catch (MalformedURLException e) {
System.err.println("URL ERROR");
throw new UnknowFileURL();
}
}
public FileURLChange(String url, String base) throws UnknowFileURL
{
try {
baseURL = new URL(url);
baseDir = new URL(base);
}
catch (MalformedURLException e) {
System.err.println("Base URL ERROR");
throw new UnknowFileURL();
}
host = baseURL.getHost();
if (baseURL.getQuery() != null)
{
System.err.println("Query ERROR");
throw new UnknowFileURL();
}
String fileName = getBaseFile(baseURL);
if (fileName == null)
{
System.err.println("file URL Error");
throw new UnknowFileURL();
}
else
{
try {
fileURL = new URL(fileName);
}
catch (MalformedURLException e)
{
System.err.println("file URL Error");
throw new UnknowFileURL();
}
}
}
public boolean setBase(String base)
{
try {
baseURL = new URL(base);
}
catch (MalformedURLException e) {
return false;
}
return true;
}
/* get the base file name */
public static String getBaseFile(URL url)
{
String prot = url.getProtocol();
if (prot.equals("http"))
{
return getHttpBaseFile(url);
}
else
{
System.err.println("http ERROR:" + url.toString());
return null;
}
}
public static String getHttpBaseFile(URL url)
{
String file = url.getProtocol() + "://";
String tmp = url.getUserInfo();
if (tmp != null)
file = file.concat(tmp + "@");
file = file.concat(url.getHost());
int port = url.getPort();
if (port != -1)
file = file.concat(":" + port);
file = file.concat(url.getPath());
return file;
}
public String getBaseDir()
{
return baseDir.toString();
}
public static String getFileDir(String file)
{
String dir = file.substring(0, file.lastIndexOf('/')+1);
return dir;
}
public String getRelativeFile(String relURL)
{
URL url;
try {
url = new URL(baseDir, relURL);
}
catch (MalformedURLException e) {
return relURL;
}
/*
String s = getBaseFile(url);
if (s != null)
return s;
else
return relURL;
*/
return url.toString();
}
public static String getAutoName(String url)
{
URL u;
try {
u = new URL(url);
}
catch (MalformedURLException e) {
return null;
}
String s = getBaseFile(u);
if (s == null)
return null;
return s.substring(s.lastIndexOf('/')+1);
}
public String getDirOfAbsoluteFile(String absURL, boolean sameDir)
{
if (sameDir == true)
return "";
URL url;
try {
url = new URL(absURL);
}
catch (MalformedURLException e) {
return null;
}
if (url.getQuery() != null)
return null;
String newDir = getBaseFile(url);
newDir = getFileDir(newDir);
String[] newDirs = newDir.split("/");
String dir = baseDir.toString();
String[] dirs = dir.split("/");
if (newDirs.length < dirs.length)
return "";
int i;
for (i = 0; i < dirs.length; i++)
if (!newDirs[i].equals(dirs[i]))
return "";
String path = new String();
for (i = dirs.length; i < newDirs.length; i++)
{
path += newDirs[i] + "/";
}
return path;
}
public String getDirOfRelativeFile(String relURL, boolean sameDir)
{
if (sameDir == true)
return "";
URL url;
try {
url = new URL(baseDir, relURL);
}
catch (MalformedURLException e) {
return null;
}
String newDir = getBaseFile(url);
newDir = getFileDir(newDir);
String[] newDirs = newDir.split("/");
String dir = baseDir.toString();
String[] dirs = dir.split("/");
int i;
String path = new String();
for (i = dirs.length; i < newDirs.length; i++)
{
path += newDirs[i] + "/";
}
return path;
}
public static void main(String[] args)
{
SystemIn in = new SystemIn();
System.out.print("Please input a URL:");
String url = in.readLine();
FileURLChange base;
try {
base = new FileURLChange(url);
}
catch (UnknowFileURL e) {
System.err.println("Error!");
return;
}
System.out.println(base.fileURL);
System.out.println(base.getFileDir(base.fileURL.toString()));
System.out.print("Please input a relative URL:");
System.out.println(base.getRelativeFile(in.readLine()));
}
}