KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > net > URLNavigator


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.net;
10
11 import org.jboss.portal.common.net.file.FileURLNavigationProvider;
12 import org.jboss.portal.common.net.jar.JarURLNavigationProvider;
13
14 import java.net.URL JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 /**
19  * The URLNavigator class is a registry for various URLNavigationProvider.
20  *
21  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
22  * @version $Revision: 1.1 $
23  */

24 public class URLNavigator
25 {
26
27    private static final URLNavigationProvider fileNav = new FileURLNavigationProvider();
28
29    private static final URLNavigationProvider jarNav = new JarURLNavigationProvider();
30
31    public static boolean isDir(URL JavaDoc url) throws IllegalArgumentException JavaDoc, IOException JavaDoc
32    {
33       URLNavigationProvider provider = getProvider(url);
34       return provider.isDir(url);
35    }
36
37    public static Iterator JavaDoc getChildren(URL JavaDoc url) throws IllegalArgumentException JavaDoc, IOException JavaDoc
38    {
39       URLNavigationProvider provider = getProvider(url);
40       return provider.getChildren(url);
41    }
42
43    public static void visit(URL JavaDoc url, URLVisitor visitor) throws IllegalArgumentException JavaDoc, IOException JavaDoc
44    {
45       URLNavigationProvider provider = getProvider(url);
46       provider.visit(url, visitor);
47    }
48
49    /**
50     * Return an URLNavigationProvider for the specified URL.
51     *
52     * @param url the target url
53     * @return the corresponding URL navigator
54     * @throws IllegalArgumentException if the url is null or no provider is found
55     */

56    private static URLNavigationProvider getProvider(URL JavaDoc url) throws IllegalArgumentException JavaDoc
57    {
58       if (url == null)
59       {
60          throw new IllegalArgumentException JavaDoc("Null not accepted");
61       }
62       String JavaDoc protocol = url.getProtocol();
63       if ("file".equals(protocol))
64       {
65          return fileNav;
66       }
67       else if ("jar".equals(protocol))
68       {
69          return jarNav;
70       }
71       else
72       {
73          throw new IllegalArgumentException JavaDoc("Not recognized " + protocol);
74       }
75    }
76
77 }
78
Popular Tags