1 package jp.sourceforge.stigmata.digger;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.net.MalformedURLException;
12 import java.util.ArrayList;
13 import java.util.Enumeration;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.jar.JarEntry;
17 import java.util.jar.JarFile;
21 * @author Haruaki TAMADA
24 public class JarClassFileArchive implements ClassFileArchive{
26 private JarFile jarfile;
27 private URL jarfileLocation;
29 public JarClassFileArchive(String jarfile) throws IOException{
30 this.file = new File(jarfile);
31 this.jarfile = new JarFile(jarfile);
32 this.jarfileLocation = file.toURI().toURL();
35 public URL getLocation(){
36 return jarfileLocation;
39 public String getName(){
40 return file.getName();
43 public InputStream getInputStream(ClassFileEntry entry) throws IOException{
44 if(hasEntry(entry.getClassName())){
45 return jarfile.getInputStream(jarfile.getEntry(entry.getClassName().replace('.', '/') + ".class"));
50 public Iterator<ClassFileEntry> iterator(){
51 List<ClassFileEntry> list = new ArrayList<ClassFileEntry>();
53 for(Enumeration<JarEntry> e = jarfile.entries(); e.hasMoreElements(); ){
54 JarEntry entry = e.nextElement();
55 if(entry.getName().endsWith(".class")){
58 location = new URL("jar:" + getLocation() + "!/" + entry.getName());
59 String className = entry.getName();
60 className = className.substring(0, className.length() - ".class".length());
61 className = className.replace('/', '.');
63 list.add(new ClassFileEntry(className, location));
64 } catch (MalformedURLException ex) {
68 return list.iterator();
71 public boolean hasEntry(String className){
72 return jarfile.getEntry(className.replace('.', '/') + ".class") != null;
75 public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
76 if(hasEntry(className)){
77 String entryName = className.replace('.', '/') + ".class";
79 URL location = new URL("jar:" + jarfile.getName() + "!/" + entryName);
81 return new ClassFileEntry(className, location);
82 } catch(MalformedURLException e){
88 Enumeration<JarEntry> jarentries(){
89 return jarfile.entries();
92 boolean hasJarEntry(String entry){
93 return jarfile.getEntry(entry) != null;