/Users/lyon/j4p/src/classUtils/pack/util/ClassPathIterator.java

1    package classUtils.pack.util; 
2     
3    import java.io.File; 
4    import java.util.Iterator; 
5    import java.util.StringTokenizer; 
6    import java.util.jar.JarFile; 
7     
8    /** 
9     * An iterator over a classpath declaration. 
10    *  
11    * @author Cristiano Sadun 
12    */ 
13   public class ClassPathIterator implements Iterator { 
14    
15       private StringTokenizer st; 
16       private String currentEntry; 
17       private File currentFile; 
18    
19       /** 
20        * Create a ClassPathIterator iterating on the given class path. 
21        *  
22        * @param the classpath string to iterate on 
23        */ 
24       public ClassPathIterator(String classpath) { 
25           st = new StringTokenizer(classpath, File.pathSeparator); 
26       } 
27        
28       /** 
29        * Create a ClassPathIterator iterating on the system class path. 
30        */ 
31       public ClassPathIterator() { 
32           this(System.getProperty("java.class.path")); 
33       } 
34    
35       /** 
36        * @see java.util.Iterator#hasNext() 
37        */ 
38       public boolean hasNext() { 
39           return st.hasMoreTokens(); 
40       } 
41    
42       /** 
43        * Return the next entry as a String 
44        * @see java.util.Iterator#next() 
45        */ 
46       public Object next() { 
47           synchronized (this) { 
48               currentEntry = st.nextToken(); 
49               currentFile = new File(currentEntry); 
50               return currentEntry; 
51           } 
52       } 
53    
54       /** 
55        * Return the next entry in the class path as a String 
56        */ 
57       public String nextEntryString() { 
58           return (String) next(); 
59       } 
60    
61       /** 
62        * Return the next entry in the class path as a File 
63        */ 
64       public File nextEntryFile() { 
65           next(); 
66           return currentFile; 
67       } 
68    
69       /** 
70        * @see java.util.Iterator#remove() 
71        */ 
72       public void remove() { 
73           throw new UnsupportedOperationException(); 
74       } 
75    
76       /** 
77        * Returns whether the entry where the iterator is positioned is a JAR or not 
78        *  
79        * @param checkExistence also checks if the JAR exists. 
80        */ 
81       public boolean isJar(boolean checkExistence) { 
82           if (currentEntry == null) 
83               throw new IllegalStateException("Please invoke next() first"); 
84           return currentEntry.endsWith(".jar") 
85               && ((!checkExistence) || (checkExistence && currentFile.exists())); 
86       } 
87    
88       /** 
89        * Returns whether the entry where the iterator is positioned is a JAR or not, regardless 
90        * of its existence. 
91        */ 
92       public boolean isJar() { 
93           return isJar(false); 
94       } 
95    
96    
97       /** 
98        * Returns whether the entry where the iterator is positioned is a JAR or not 
99        *  
100       * @param checkExistence also checks if the JAR exists. 
101       */ 
102      public boolean isDirectory(boolean checkExistence) { 
103          if (currentEntry == null) 
104              throw new IllegalStateException("Please invoke next() first"); 
105          return currentFile.isDirectory() 
106              && ((!checkExistence) || (checkExistence && currentFile.exists())); 
107      } 
108   
109      /** 
110       * Returns whether the entry where the iterator is positioned is a JAR or not, regardless 
111       * of its existence. 
112       */ 
113      public boolean isDirectory() { 
114          return isDirectory(false); 
115      } 
116   
117  } 
118