KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > util > URLUtil


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.net.util;
6
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.net.MalformedURLException JavaDoc;
10 import java.net.URL JavaDoc;
11
12 public final class URLUtil {
13
14   private URLUtil() {
15     throw new RuntimeException JavaDoc("Don't try to instantiate this [utility] class");
16   }
17
18   /**
19    * @return the full URL to the given [relative] path, which could be located at any of the URLs in
20    * <code>baseURLs</code>, or <code>null</code> if the path does not exist at any of them.
21    * @throws MalformedURLException
22    */

23   public static URL JavaDoc resolve(final URL JavaDoc[] baseURLs, final String JavaDoc path) throws MalformedURLException JavaDoc {
24     if (baseURLs != null && path != null) {
25       for (int pos = 0; pos < baseURLs.length; pos++) {
26         final URL JavaDoc testURL = new URL JavaDoc(baseURLs[pos].toString() + (baseURLs[pos].toString().endsWith("/") ? "" : "/")
27             + path);
28         try {
29           final InputStream JavaDoc is = testURL.openStream();
30           is.read();
31           is.close();
32           return testURL;
33         } catch (IOException JavaDoc ioe) {
34           // Ignore this, the URL is bad
35
}
36       }
37     }
38     return null;
39   }
40
41 }
42
Popular Tags