KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > util > Jar


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.util;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.Comparator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.SortedSet JavaDoc;
16 import java.util.TreeSet JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.jar.JarInputStream JavaDoc;
20 import java.util.jar.JarEntry JavaDoc;
21 import java.util.jar.JarFile JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25
26 /**
27  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
28  * @version $Revision: 1.1 $
29  */

30 public class Jar
31 {
32
33    /**
34     * Compares jar entries.
35     */

36    private static final Comparator JavaDoc comparator = new Comparator JavaDoc()
37    {
38       public int compare(Object JavaDoc o1, Object JavaDoc o2)
39       {
40          EntryInfo e1 = (EntryInfo)o1;
41          EntryInfo e2 = (EntryInfo)o2;
42          Iterator JavaDoc i1 = e1.iterator();
43          Iterator JavaDoc i2 = e2.iterator();
44          while (true)
45          {
46             if (i1.hasNext())
47             {
48                Object JavaDoc o = i1.next();
49                String JavaDoc s1 = (String JavaDoc)o;
50                if (i2.hasNext())
51                {
52                   String JavaDoc s2 = (String JavaDoc)i2.next();
53                   int res = s1.compareTo(s2);
54                   if (res != 0)
55                   {
56                      return res;
57                   }
58                }
59                else
60                {
61                   return 1;
62                }
63             }
64             else
65             {
66                if (i2.hasNext())
67                {
68                   return -1;
69                }
70                else
71                {
72                   return 0;
73                }
74             }
75          }
76       }
77    };
78
79    public static Iterator JavaDoc iterator(JarFile JavaDoc file) throws IOException JavaDoc, IllegalArgumentException JavaDoc
80    {
81       if (file == null)
82       {
83          throw new IllegalArgumentException JavaDoc();
84       }
85       SortedSet JavaDoc set = new TreeSet JavaDoc(comparator);
86       for (Enumeration JavaDoc e = file.entries();e.hasMoreElements();)
87       {
88          JarEntry JavaDoc entry = (JarEntry JavaDoc)e.nextElement();
89          EntryInfo info = new EntryInfo(entry);
90          set.add(info);
91       }
92       return set.iterator();
93    }
94
95    public static Iterator JavaDoc iterator(JarInputStream JavaDoc in) throws IOException JavaDoc, IllegalArgumentException JavaDoc
96    {
97       if (in == null)
98       {
99          throw new IllegalArgumentException JavaDoc();
100       }
101       SortedSet JavaDoc set = new TreeSet JavaDoc(comparator);
102       for (JarEntry JavaDoc entry = in.getNextJarEntry();entry !=null;entry = in.getNextJarEntry())
103       {
104          Jar.EntryInfo info = new EntryInfo(entry);
105          set.add(info);
106       }
107       return set.iterator();
108    }
109
110    /**
111     * Enhance jar entry object by adding more info.
112     */

113    public static class EntryInfo
114    {
115
116       private final JarEntry JavaDoc entry;
117       private final List JavaDoc atoms;
118
119       public EntryInfo(JarEntry JavaDoc entry)
120       {
121          if (entry == null)
122          {
123             throw new IllegalArgumentException JavaDoc();
124          }
125          this.entry = entry;
126          String JavaDoc name = entry.getName();
127          ArrayList JavaDoc atoms = new ArrayList JavaDoc();
128          int previous = -1;
129          while (true)
130          {
131             int current = name.indexOf('/', previous + 1);
132             if (current == -1)
133             {
134                current = name.length();
135             }
136             if (current >= name.length() - 1)
137             {
138                if (current - previous > 1)
139                {
140                   atoms.add(name.substring(previous + 1, current));
141                }
142                break;
143             }
144             if (current - previous > 1)
145             {
146                atoms.add(name.substring(previous + 1, current));
147             }
148             previous = current;
149          }
150          this.atoms = Collections.unmodifiableList(atoms);
151       }
152
153       public Iterator JavaDoc iterator()
154       {
155          return atoms.iterator();
156       }
157
158       public JarEntry JavaDoc getEntry()
159       {
160          return entry;
161       }
162
163       public boolean isDirectory()
164       {
165          return entry.isDirectory();
166       }
167
168       public int size()
169       {
170          return atoms.size();
171       }
172
173       public List JavaDoc getAtoms()
174       {
175          return atoms;
176       }
177
178       public String JavaDoc get(int index)
179       {
180          return (String JavaDoc)atoms.get(index);
181       }
182
183       public boolean isChildOf(EntryInfo parent) throws IllegalArgumentException JavaDoc
184       {
185          if (parent == null)
186          {
187             throw new IllegalArgumentException JavaDoc();
188          }
189          if (!parent.isDirectory())
190          {
191             return false;
192          }
193          if (parent.size() + 1 != atoms.size())
194          {
195             return false;
196          }
197          return parent.atoms.equals(parent.atoms.subList(0, atoms.size() - 1));
198       }
199
200       public boolean isDescendantOf(EntryInfo ancestor) throws IllegalArgumentException JavaDoc
201       {
202          if (ancestor == null)
203          {
204             throw new IllegalArgumentException JavaDoc();
205          }
206          if (!ancestor.isDirectory())
207          {
208             return false;
209          }
210          if (ancestor.size() >= atoms.size())
211          {
212             return false;
213          }
214          return ancestor.atoms.equals(atoms.subList(0, ancestor.size()));
215       }
216
217       public URL JavaDoc toURL(URL JavaDoc jarURL) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, MalformedURLException JavaDoc
218       {
219          if (jarURL == null)
220          {
221             throw new IllegalArgumentException JavaDoc("No null jarURL");
222          }
223          if (isDirectory())
224          {
225             throw new IllegalStateException JavaDoc("Cannot create dir URL");
226          }
227          StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(jarURL.toString()).append("!/");
228          for (int i = 0; i < atoms.size(); i++)
229          {
230             String JavaDoc atom = (String JavaDoc)atoms.get(i);
231             tmp.append(i > 0 ? "/" : "").append(atom);
232          }
233          return new URL JavaDoc("jar", "", tmp.toString());
234       }
235
236       public String JavaDoc toString()
237       {
238          StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
239          for (int i = 0; i < atoms.size(); i++)
240          {
241             String JavaDoc atom = (String JavaDoc)atoms.get(i);
242             tmp.append(i > 0 ? "/" : "").append(atom);
243          }
244          if (entry.isDirectory())
245          {
246             tmp.append("/");
247          }
248          return tmp.toString();
249       }
250    }
251
252
253
254 }
255
Popular Tags