OSDN Git Service

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