OSDN Git Service

initial version
[stigmata/digger.git] / src / main / java / jp / sourceforge / stigmata / digger / JarClassFileArchive.java
1 package jp.sourceforge.stigmata.digger;
2
3 /*
4  * $Id$
5  */
6
7 import java.io.File;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.net.MalformedURLException;
11 import java.net.URL;
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;
18
19 /**
20  *
21  * @author Haruaki TAMADA
22  * @version $Revision$ 
23  */
24 public class JarClassFileArchive implements ClassFileArchive{
25     private File file;
26     private JarFile jarfile;
27     private URL jarfileLocation;
28
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();
33     }
34
35     public URL getLocation(){
36         return jarfileLocation;
37     }
38
39     public String getName(){
40         return file.getName();
41     }
42
43     public InputStream getInputStream(ClassFileEntry entry) throws IOException{
44         if(hasEntry(entry.getClassName())){
45             return jarfile.getInputStream(jarfile.getEntry(entry.getClassName().replace('.', '/') + ".class"));
46         }
47         return null;
48     }
49
50     public Iterator<ClassFileEntry> iterator(){
51         List<ClassFileEntry> list = new ArrayList<ClassFileEntry>();
52         
53         for(Enumeration<JarEntry> e = jarfile.entries(); e.hasMoreElements(); ){
54             JarEntry entry = e.nextElement();
55             if(entry.getName().endsWith(".class")){
56                 URL location = null;
57                 try {
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('/', '.');
62                     
63                     list.add(new ClassFileEntry(className, location));
64                 } catch (MalformedURLException ex) {
65                 }
66             }
67         }
68         return list.iterator();
69     }
70
71     public boolean hasEntry(String className){
72         return jarfile.getEntry(className.replace('.', '/') + ".class") != null;
73     }
74
75     public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
76         if(hasEntry(className)){
77             String entryName = className.replace('.', '/') + ".class";
78             try{
79                 URL location = new URL("jar:" + jarfile.getName() + "!/" + entryName);
80                 
81                 return new ClassFileEntry(className, location);
82             } catch(MalformedURLException e){
83             }
84         }
85         return null;
86     }
87
88     Enumeration<JarEntry> jarentries(){
89         return jarfile.entries();
90     }
91
92     boolean hasJarEntry(String entry){
93         return jarfile.getEntry(entry) != null;
94     }
95 }