KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > client > framework > URLUtils


1 /*
2  * URLUtils.java
3  *
4  * Created on September 13, 2004, 8:10 AM
5  */

6
7 package com.quikj.client.framework;
8
9 import java.net.*;
10 import java.applet.*;
11
12 /**
13  *
14  * @author amit
15  */

16 public class URLUtils
17 {
18     /** Creates a new instance of URLUtils */
19     public URLUtils()
20     {
21     }
22     
23     public static URL formatURL(Applet applet, String JavaDoc loc)
24     {
25         URL url = null;
26         try
27         {
28             url = new URL(loc);
29         }
30         catch (MalformedURLException ex)
31         {
32             URL docbase = applet.getDocumentBase();
33             if (loc.startsWith("/") == true)
34             {
35                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(docbase.getProtocol()
36                 + "://"
37                 + docbase.getHost());
38                 
39                 if (docbase.getPort() > 0)
40                 {
41                     buffer.append(":" + docbase.getPort());
42                 }
43                 
44                 buffer.append(loc);
45                 
46                 try
47                 {
48                     url = new URL(buffer.toString());
49                 }
50                 catch (MalformedURLException ex1)
51                 {
52                     ;
53                 }
54             }
55             else // relative location
56
{
57                 String JavaDoc path = docbase.toString();
58                 
59                 // strip out the file name from the path
60
int last = path.lastIndexOf('/');
61                 
62                 path = path.substring(0, last+1);
63                 
64                 try
65                 {
66                     // and append the relative path
67
url = new URL(path + loc);
68                 }
69                 catch (MalformedURLException ex1)
70                 {
71                     ;
72                 }
73             }
74         }
75         
76         return url;
77     }
78 }
79
Popular Tags