KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > URLTool


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.runtime;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Vector JavaDoc;
16 import org.eclipse.core.runtime.Assert;
17
18 /**
19  * A utility for manipulating <code>URL</code>s.
20  */

21 public class URLTool {
22
23     /**
24      * Returns the given <code>URL</code> with a trailing slash appended to
25      * it. If the <code>URL</code> already has a trailing slash the
26      * <code>URL</code> is returned unchanged.
27      * <table>
28      * <caption>Example</caption>
29      * <tr>
30      * <th>Given URL</th>
31      * <th>Returned URL</th>
32      * <tr>
33      * <td>"http://hostname/folder"</td>
34      * <td>"http://hostname/folder/"</td>
35      * <tr>
36      * <td>"http://hostname/folder/</td>
37      * <td>"http://hostname/folder/"</td>
38      * </table>
39      *
40      * @param url a URL
41      * @return the given URL with a trailing slash
42      */

43     public static URL JavaDoc appendTrailingSlash(URL JavaDoc url) {
44         String JavaDoc file = url.getFile();
45         if (file.endsWith("/")) //$NON-NLS-1$
46
return url;
47         try {
48             return new URL JavaDoc(url.getProtocol(), url.getHost(), url.getPort(), file + "/"); //$NON-NLS-1$
49
} catch (MalformedURLException JavaDoc e) {
50             Assert.isTrue(false, "internal error"); //$NON-NLS-1$
51
}
52         return null;
53     }
54
55     /**
56      * Returns the child URL formed by joining the given member with the
57      * given parent URL.
58      *
59      * @return a child URL of the given parent
60      */

61     public static URL JavaDoc getChild(URL JavaDoc parent, String JavaDoc member) {
62         String JavaDoc file = parent.getFile();
63         if (!file.endsWith("/")) //$NON-NLS-1$
64
file = file + "/"; //$NON-NLS-1$
65
try {
66             return new URL JavaDoc(parent.getProtocol(), parent.getHost(), parent.getPort(), file + member);
67         } catch (MalformedURLException JavaDoc e) {
68             Assert.isTrue(false, "internal error"); //$NON-NLS-1$
69
}
70         return null;
71     }
72
73     /**
74      * Returns all elements in the given URLs path.
75      * <table>
76      * <caption>Example</caption>
77      * <tr>
78      * <th>Given URL</th>
79      * <th>Element</th>
80      * <tr>
81      * <td>"http://hostname/"</td>
82      * <td>[]</td>
83      * <tr>
84      * <td>"http://hostname/folder/</td>
85      * <td>[folder]</td>
86      * <tr>
87      * <td>"http://hostname/folder/file</td>
88      * <td>[folder, file]</td>
89      * </table>
90      * @param url a URL
91      * @return all elements in the given URLs path
92      */

93     public static Vector JavaDoc getElements(URL JavaDoc url) {
94         Vector JavaDoc result = new Vector JavaDoc(5);
95         String JavaDoc lastElement = null;
96         while ((lastElement = getLastElement(url)) != null) {
97             result.insertElementAt(lastElement, 0);
98             url = getParent(url);
99         }
100         return result;
101     }
102
103     /**
104      * Returns the last element in the given URLs path, or <code>null</code>
105      * if the URL is the root.
106      * <table>
107      * <caption>Example</caption>
108      * <tr>
109      * <th>Given URL</th>
110      * <th>Last Element</th>
111      * <tr>
112      * <td>"http://hostname/"</td>
113      * <td>null</td>
114      * <tr>
115      * <td>"http://hostname/folder/</td>
116      * <td>folder</td>
117      * <tr>
118      * <td>"http://hostname/folder/file</td>
119      * <td>file</td>
120      * </table>
121      * @param url a URL
122      * @return the last element in the given URLs path, or
123      * <code>null</code> if the URL is the root
124      */

125     public static String JavaDoc getLastElement(URL JavaDoc url) {
126         String JavaDoc file = url.getFile();
127         int len = file.length();
128         if (len == 0 || len == 1 && file.charAt(0) == '/')
129             return null;
130
131         int lastSlashIndex = -1;
132         for (int i = len - 2; lastSlashIndex == -1 && i >= 0; --i) {
133             if (file.charAt(i) == '/')
134                 lastSlashIndex = i;
135         }
136         boolean isDirectory = file.charAt(len - 1) == '/';
137         if (lastSlashIndex == -1) {
138             if (isDirectory) {
139                 return file.substring(0, len - 1);
140             } else {
141                 return file;
142             }
143         } else {
144             if (isDirectory) {
145                 return file.substring(lastSlashIndex + 1, len - 1);
146             } else {
147                 return file.substring(lastSlashIndex + 1, len);
148             }
149         }
150     }
151
152     /**
153      * Returns the parent URL of the given URL, or <code>null</code> if the
154      * given URL is the root.
155      * <table>
156      * <caption>Example</caption>
157      * <tr>
158      * <th>Given URL</th>
159      * <th>Parent URL</th>
160      * <tr>
161      * <td>"http://hostname/"</td>
162      * <td>null</td>
163      * <tr>
164      * <td>"http://hostname/folder/file</td>
165      * <td>"http://hostname/folder/</td>
166      * </table>
167      *
168      * @param url a URL
169      * @return the parent of the given URL
170      */

171     public static URL JavaDoc getParent(URL JavaDoc url) {
172         String JavaDoc file = url.getFile();
173         int len = file.length();
174         if (len == 0 || len == 1 && file.charAt(0) == '/')
175             return null;
176         int lastSlashIndex = -1;
177         for (int i = len - 2; lastSlashIndex == -1 && i >= 0; --i) {
178             if (file.charAt(i) == '/')
179                 lastSlashIndex = i;
180         }
181         if (lastSlashIndex == -1)
182             file = ""; //$NON-NLS-1$
183
else
184             file = file.substring(0, lastSlashIndex + 1);
185
186         try {
187             url = new URL JavaDoc(url.getProtocol(), url.getHost(), url.getPort(), file);
188         } catch (MalformedURLException JavaDoc e) {
189             Assert.isTrue(false, e.getMessage());
190         }
191         return url;
192     }
193
194     /**
195      * Returns the root URL of the given URL.
196      * <table>
197      * <caption>Example</caption>
198      * <tr>
199      * <th>Given URL</th>
200      * <th>Root URL</th>
201      * <tr>
202      * <td>"http://hostname/"</td>
203      * <td>"http://hostname/"</td>
204      * <tr>
205      * <td>"http://hostname/folder/file</td>
206      * <td>"http://hostname/"</td>
207      * </table>
208      *
209      * @param urlString a URL
210      * @return the root url of the given URL
211      * @throws MalformedURLException if the given URL is malformed
212      */

213     public static URL JavaDoc getRoot(String JavaDoc urlString) throws MalformedURLException JavaDoc {
214         return getRoot(new URL JavaDoc(urlString));
215     }
216
217     /**
218      * Returns the root URL of the given URL.
219      * <table>
220      * <caption>Example</caption>
221      * <tr>
222      * <th>Given URL</th>
223      * <th>Root URL</th>
224      * <tr>
225      * <td>"http://hostname/"</td>
226      * <td>"http://hostname/"</td>
227      * <tr>
228      * <td>"http://hostname/folder/file</td>
229      * <td>"http://hostname/"</td>
230      * </table>
231      *
232      * @param url a URL
233      * @return the root URL of the given URL
234      */

235     public static URL JavaDoc getRoot(URL JavaDoc url) {
236         try {
237             return new URL JavaDoc(url.getProtocol(), url.getHost(), url.getPort(), "/"); //$NON-NLS-1$
238
} catch (MalformedURLException JavaDoc e) {
239             Assert.isTrue(false, "internal error"); //$NON-NLS-1$
240
}
241
242         return null;
243     }
244
245     /**
246      * Returns the given URL with its trailing slash removed. If the URL has
247      * no trailing slash, the URL is returned unchanged.
248      * <table>
249      * <caption>Example</caption>
250      * <tr>
251      * <th>Given URL</th>
252      * <th>Returned URL</th>
253      * <tr>
254      * <td>"http://hostname/folder"</td>
255      * <td>"http://hostname/folder"</td>
256      * <tr>
257      * <td>"http://hostname/folder/</td>
258      * <td>"http://hostname/folder"</td>
259      * </table>
260      *
261      * @param url a URL
262      * @return the given URL with its last slash removed
263      */

264     public static URL JavaDoc removeTrailingSlash(URL JavaDoc url) {
265         String JavaDoc file = url.getFile();
266
267         if (file.endsWith("/")) { //$NON-NLS-1$
268
file = file.substring(0, file.length() - 1);
269             try {
270                 return new URL JavaDoc(url.getProtocol(), url.getHost(), url.getPort(), file);
271             } catch (MalformedURLException JavaDoc e) {
272                 Assert.isTrue(false, e.getMessage());
273             }
274         } else {
275             return url;
276         }
277
278         return null;
279     }
280
281     /**
282      * Returns a boolean indicating whether the given URLs overlap.
283      * <table>
284      * <caption>Example</caption>
285      * <tr>
286      * <th>First URL</th>
287      * <th>Second URL</th>
288      * <th>Do they overlap</th>
289      * <tr>
290      * <td>"http://hostname/folder/"</td>
291      * <td>"http://hostname/folder/"</td>
292      * <td>true</td>
293      * <tr>
294      * <td>"http://hostname/folder/"</td>
295      * <td>"http://hostname/folder/file"</td>
296      * <td>true</td>
297      * <tr>
298      * <td>"http://hostname/folder/file"</td>
299      * <td>"http://hostname/folder/"</td>
300      * <td>true</td>
301      * <tr>
302      * <td>"http://hostname/folder1/"</td>
303      * <td>"http://hostname/folder2/"</td>
304      * <td>false</td>
305      * <tr>
306      * <td>"http://hostname1/folder/"</td>
307      * <td>"http://hostname2/folder/"</td>
308      * <td>false</td>
309      * </table>
310      * @param url1 firt URL
311      * @param url2 second URL
312      * @return a boolean indicating whether the given URLs overlap
313      */

314     public static boolean urlsOverlap(URL JavaDoc url1, URL JavaDoc url2) {
315         if (!getRoot(url1).equals(getRoot(url2))) {
316             return false;
317         }
318
319         Vector JavaDoc elements1 = URLTool.getElements(url1);
320         Vector JavaDoc elements2 = URLTool.getElements(url2);
321
322         for (int i = 0; i < elements1.size() && i < elements2.size(); ++i) {
323             String JavaDoc element1 = (String JavaDoc) elements1.elementAt(i);
324             String JavaDoc element2 = (String JavaDoc) elements2.elementAt(i);
325             if (!element1.equals(element2)) {
326                 return false;
327             }
328         }
329
330         return true;
331     }
332 }
333
Popular Tags