001: package com.kirkk.analyzer.framework.bcelbundle;
002:
003: import com.kirkk.analyzer.framework.*;
004: import com.kirkk.analyzer.framework.jar.*;
005: import java.util.*;
006: import java.io.*;
007:
008: public class JarCollectionImpl implements JarCollection {
009: private List jars;
010: private Iterator jarIterator;
011:
012: public JarCollectionImpl(File file) throws Exception {
013: this (file, new ArrayList());
014: }
015:
016: public JarCollectionImpl(File file, List ignorePackages)
017: throws Exception {
018: this (file, ignorePackages, new ArrayList());
019: }
020:
021: public JarCollectionImpl(File file, List ignorePackages,
022: List ignoreJars) throws Exception {
023: this .jars = this .getJars(file, ignorePackages, ignoreJars);
024: this .jarIterator = this .jars.iterator();
025: }
026:
027: public int getJarCount() {
028: return this .jars.size();
029: }
030:
031: public boolean hasNext() {
032: return this .jarIterator.hasNext();
033: }
034:
035: public Jar nextJar() {
036: return (Jar) this .jarIterator.next();
037: }
038:
039: public void first() {
040: this .jarIterator = jars.iterator();
041: }
042:
043: public Jar getJar(String jarName) {
044: Iterator jarIterator = this .jars.iterator();
045: while (jarIterator.hasNext()) {
046: Jar jar = (Jar) jarIterator.next();
047: if (jar.getJarFileName().equals(jarName)) {
048: return jar;
049: }
050: }
051: return null;
052: }
053:
054: public Jar getJarContainingPackage(String packageName) {
055: Iterator jarIterator = this .jars.iterator();
056: while (jarIterator.hasNext()) {
057: Jar jar = (Jar) jarIterator.next();
058: if (jar.containsPackage(packageName)) {
059: return jar;
060: }
061: }
062: return null;
063: }
064:
065: public Jar[] toArray() {
066: Jar[] jar = new Jar[jars.size()];
067: Iterator jarIterator = jars.iterator();
068: int i = 0;
069: while (jarIterator.hasNext()) {
070: jar[i] = (Jar) jarIterator.next();
071: i++;
072: }
073: return jar;
074: }
075:
076: private List getJars(File file, List ignorePackages, List ignoreJars)
077: throws Exception {
078: if (file.isDirectory()) {
079: File files[] = this .getJarFiles(file);
080: ArrayList jars = new ArrayList();
081: for (int i = 0; i < files.length; i++) {
082: JarBuilder jarBuilder = new JarBuilderImpl();
083: JarFile jarFile = new JarFile(files[i]);
084: Jar jar = jarBuilder.buildJar(jarFile, ignorePackages);
085: if ((jar.getClassCount() > 0)
086: && (ignoreJars.contains(jar.getJarFileName()) == false)) {
087: jars.add(jar);
088: }
089: }
090: return jars;
091:
092: } else {
093: throw new IOException("File must be a directory");
094: }
095:
096: }
097:
098: private File[] getJarFiles(File file) {
099: FilenameFilter filter = new FilenameFilter() {
100: public boolean accept(File file, String fileName) {
101: if (fileName.endsWith(".jar")) {
102: return true;
103: } else {
104: return false;
105: }
106: }
107: };
108:
109: return file.listFiles(filter);
110:
111: }
112:
113: }
|