KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > net > jar > JarURLNavigationProvider


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.jar;
10
11 import org.jboss.portal.common.net.URLNavigationProvider;
12 import org.jboss.portal.common.net.URLVisitor;
13 import org.jboss.portal.common.util.Jar;
14
15 import java.net.URL JavaDoc;
16 import java.net.JarURLConnection JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.jar.JarEntry JavaDoc;
20 import java.util.jar.JarFile JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
25  * @version $Revision: 1.1 $
26  */

27 public class JarURLNavigationProvider implements URLNavigationProvider
28 {
29
30    public boolean isDir(URL JavaDoc url) throws IllegalArgumentException JavaDoc, IOException JavaDoc
31    {
32       throw new UnsupportedOperationException JavaDoc();
33    }
34
35    public Iterator JavaDoc getChildren(URL JavaDoc url) throws IllegalArgumentException JavaDoc
36    {
37       throw new UnsupportedOperationException JavaDoc();
38    }
39
40    public void visit(URL JavaDoc url, URLVisitor visitor) throws IllegalArgumentException JavaDoc, IOException JavaDoc
41    {
42       if (url == null)
43       {
44          throw new IllegalArgumentException JavaDoc("Null URL not accepted");
45       }
46       if (!"jar".equals(url.getProtocol()))
47       {
48          throw new IllegalArgumentException JavaDoc("Only jar URL are accepted not " + url.getProtocol());
49       }
50       JarURLConnection JavaDoc conn = (JarURLConnection JavaDoc)url.openConnection();
51       JarFile JavaDoc jarFile = conn.getJarFile();
52       URL JavaDoc jarURL = conn.getJarFileURL();
53       JarEntry JavaDoc rootEntry = conn.getJarEntry();
54
55       Iterator JavaDoc iterator = Jar.iterator(jarFile);
56       visit(jarURL, iterator, new Jar.EntryInfo(rootEntry), visitor);
57    }
58
59    private void visit(URL JavaDoc jarURL, Iterator JavaDoc iterator, Jar.EntryInfo root, URLVisitor visitor) throws IOException JavaDoc
60    {
61       Jar.EntryInfo previous = new Jar.EntryInfo(new JarEntry JavaDoc("/"));
62       while (iterator.hasNext())
63       {
64          Jar.EntryInfo current = (Jar.EntryInfo)iterator.next();
65          if (current.isDescendantOf(root))
66          {
67             handle(previous, current, jarURL, visitor);
68             previous = current;
69          }
70       }
71       handle(previous, new Jar.EntryInfo(new JarEntry JavaDoc("/")), jarURL, visitor);
72    }
73
74    private void handle(Jar.EntryInfo previous, Jar.EntryInfo current, URL JavaDoc jarURL, URLVisitor visitor)
75    {
76       //
77
int index = 0;
78       while (index < previous.size() && index < current.size() && previous.get(index).equals(current.get(index)))
79       {
80          index++;
81       }
82
83       //
84
if (previous.isDirectory())
85       {
86          for (int i = previous.size() - 1;i >= index;i--)
87          {
88             String JavaDoc dir = previous.get(i);
89             visitor.endDir(dir);
90          }
91       }
92       else
93       {
94          for (int i = previous.size() - 2;i >= index;i--)
95          {
96             String JavaDoc dir = previous.get(i);
97             visitor.endDir(dir);
98          }
99       }
100
101       //
102
if (current.isDirectory())
103       {
104          for (int i = index;i < current.size();i++)
105          {
106             String JavaDoc dir = current.get(i);
107             visitor.startDir(dir);
108          }
109       }
110       else
111       {
112          for (int i = index;i < current.size() - 1;i++)
113          {
114             String JavaDoc dir = current.get(i);
115             visitor.startDir(dir);
116          }
117          try
118          {
119             URL JavaDoc file = current.toURL(jarURL);
120             String JavaDoc name = current.get(current.size() - 1);
121             visitor.file(name, file);
122          }
123          catch (MalformedURLException JavaDoc e)
124          {
125             e.printStackTrace();
126          }
127       }
128    }
129 }
130
Popular Tags