KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > file > JarArchiveBrowser


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.file;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.jar.JarFile JavaDoc;
27 import java.util.jar.JarEntry JavaDoc;
28 import java.util.zip.ZipFile JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.net.JarURLConnection JavaDoc;
34
35 /**
36  * Comment
37  *
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @version $Revision: 2129 $
40  *
41  **/

42 public class JarArchiveBrowser implements Iterator JavaDoc
43 {
44    JarFile JavaDoc zip;
45    Enumeration JavaDoc entries;
46    JarEntry JavaDoc next;
47    ArchiveBrowser.Filter filter;
48
49    public JarArchiveBrowser(JarURLConnection JavaDoc url, ArchiveBrowser.Filter filter)
50    {
51       this.filter = filter;
52       try
53       {
54          zip = url.getJarFile();
55          entries = zip.entries();
56       }
57       catch (IOException JavaDoc e)
58       {
59          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
60
}
61       setNext();
62    }
63
64    public JarArchiveBrowser(File JavaDoc f, ArchiveBrowser.Filter filter)
65    {
66       this.filter = filter;
67       try
68       {
69          zip = new JarFile JavaDoc(f);
70          entries = zip.entries();
71       }
72       catch (IOException JavaDoc e)
73       {
74          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
75
}
76       setNext();
77    }
78
79    public boolean hasNext()
80    {
81       return next != null;
82    }
83
84    private void setNext()
85    {
86       next = null;
87       while (entries.hasMoreElements() && next == null)
88       {
89          do
90          {
91             next = (JarEntry JavaDoc)entries.nextElement();
92          } while (entries.hasMoreElements() && next.isDirectory());
93          if (next.isDirectory()) next = null;
94
95          if (next != null && !filter.accept(next.getName()))
96          {
97             next = null;
98          }
99       }
100    }
101
102    public Object JavaDoc next()
103    {
104       ZipEntry JavaDoc entry = next;
105       setNext();
106
107       try
108       {
109          return zip.getInputStream(entry);
110       }
111       catch (IOException JavaDoc e)
112       {
113          throw new RuntimeException JavaDoc(e);
114       }
115    }
116
117    public void remove()
118    {
119       throw new RuntimeException JavaDoc("Illegal operation on ArchiveBrowser");
120    }
121 }
122
Popular Tags