1 package jp.sourceforge.stigmata.digger;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.MalformedURLException;
8 import java.util.ArrayList;
9 import java.util.Enumeration;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.jar.JarEntry;
13 import java.util.jar.JarFile;
17 * @author Haruaki TAMADA
19 public class JarClassFileArchive implements ClassFileArchive{
21 private JarFile jarfile;
22 private URL jarfileLocation;
24 public JarClassFileArchive(String jarfile) throws IOException{
25 this.file = new File(jarfile);
26 this.jarfile = new JarFile(jarfile);
27 this.jarfileLocation = file.toURI().toURL();
30 public URL getLocation(){
31 return jarfileLocation;
34 public String getName(){
35 return file.getName();
38 public InputStream getInputStream(ClassFileEntry entry) throws IOException{
39 if(hasEntry(entry.getClassName())){
40 return jarfile.getInputStream(jarfile.getEntry(entry.getClassName().replace('.', '/') + ".class"));
45 public Iterator<ClassFileEntry> iterator(){
46 List<ClassFileEntry> list = new ArrayList<ClassFileEntry>();
48 for(Enumeration<JarEntry> e = jarfile.entries(); e.hasMoreElements(); ){
49 JarEntry entry = e.nextElement();
50 if(entry.getName().endsWith(".class")){
53 location = new URL("jar:" + getLocation() + "!/" + entry.getName());
54 String className = entry.getName();
55 className = className.substring(0, className.length() - ".class".length());
56 className = className.replace('/', '.');
58 list.add(new ClassFileEntry(className, location));
59 } catch (MalformedURLException ex) {
60 throw new InternalError(ex.getMessage());
64 return list.iterator();
67 public boolean hasEntry(String className){
68 return jarfile.getEntry(className.replace('.', '/') + ".class") != null;
71 public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
72 if(hasEntry(className)){
73 String entryName = className.replace('.', '/') + ".class";
75 URL location = new URL("jar:" + jarfile.getName() + "!/" + entryName);
77 return new ClassFileEntry(className, location);
78 } catch(MalformedURLException e){
79 throw new InternalError(e.getMessage());
85 Enumeration<JarEntry> jarEntries(){
86 return jarfile.entries();
89 boolean hasJarEntry(String entry){
90 return jarfile.getEntry(entry) != null;