1 package com.kirkk.analyzer.framework.bcelbundle; 2 3 import com.kirkk.analyzer.framework.*; 4 import com.kirkk.analyzer.framework.jar.*; 5 import java.util.*; 6 import java.io.*; 7 8 public class JarCollectionImpl implements JarCollection { 9 private List jars; 10 private Iterator jarIterator; 11 12 public JarCollectionImpl(File file) throws Exception { 13 this(file, new ArrayList()); 14 } 15 16 public JarCollectionImpl(File file, List ignorePackages) throws Exception { 17 this(file, ignorePackages, new ArrayList()); 18 } 19 20 public JarCollectionImpl(File file, List ignorePackages, List ignoreJars) throws Exception { 21 this.jars = this.getJars(file, ignorePackages, ignoreJars); 22 this.jarIterator = this.jars.iterator(); 23 } 24 25 public int getJarCount() { 26 return this.jars.size(); 27 } 28 29 public boolean hasNext() { 30 return this.jarIterator.hasNext(); 31 } 32 33 public Jar nextJar() { 34 return (Jar)this.jarIterator.next(); 35 } 36 37 public void first() { 38 this.jarIterator = jars.iterator(); 39 } 40 41 public Jar getJar(String jarName) { 42 Iterator jarIterator = this.jars.iterator(); 43 while (jarIterator.hasNext()) { 44 Jar jar = (Jar) jarIterator.next(); 45 if (jar.getJarFileName().equals(jarName)) { 46 return jar; 47 } 48 } 49 return null; 50 } 51 52 public Jar getJarContainingPackage(String packageName) { 53 Iterator jarIterator = this.jars.iterator(); 54 while (jarIterator.hasNext()) { 55 Jar jar = (Jar) jarIterator.next(); 56 if (jar.containsPackage(packageName)) { 57 return jar; 58 } 59 } 60 return null; 61 } 62 63 public Jar[] toArray() { 64 Jar[] jar = new Jar[jars.size()]; 65 Iterator jarIterator = jars.iterator(); 66 int i = 0; 67 while (jarIterator.hasNext()) { 68 jar[i] = (Jar) jarIterator.next(); 69 i++; 70 } 71 return jar; 72 } 73 74 private List getJars(File file, List ignorePackages, List ignoreJars) throws Exception { 75 if (file.isDirectory()) { 76 File files[] = this.getJarFiles(file); 77 ArrayList jars = new ArrayList(); 78 for (int i = 0; i < files.length; i++) { 79 JarBuilder jarBuilder = new JarBuilderImpl(); 80 JarFile jarFile = new JarFile(files[i]); 81 Jar jar = jarBuilder.buildJar(jarFile, ignorePackages); 82 if ( (jar.getClassCount() > 0) && (ignoreJars.contains(jar.getJarFileName()) == false) ) { 83 jars.add(jar); 84 } 85 } 86 return jars; 87 88 } else { 89 throw new IOException("File must be a directory"); 90 } 91 92 } 93 94 95 private File[] getJarFiles(File file) { 96 FilenameFilter filter = new FilenameFilter() { 97 public boolean accept(File file, String fileName) { 98 if (fileName.endsWith(".jar")) { 99 return true; 100 } else { 101 return false; 102 } 103 } 104 }; 105 106 return file.listFiles(filter); 107 108 } 109 110 111 112 } | Popular Tags |