001    package AST;
002    
003    import java.util.HashSet;
004    import java.io.File;
005    import java.util.*;
006    import beaver.*;
007    import java.util.ArrayList;
008    import java.util.zip.*;
009    import java.io.*;
010    import java.io.FileNotFoundException;
011    import java.util.Collection;
012    /**
013       * Loads class files from a zip file (Jar file)
014        * @ast class
015     * 
016     */
017    public class ZipFilePart extends PathPart {
018    
019        private HashSet set = new HashSet();
020    
021    
022        private ZipFile file;
023    
024    
025        private String zipPath;
026    
027    
028    
029        public boolean hasPackage(String name) {
030          return set.contains(name);
031        }
032    
033    
034    
035        public ZipFilePart(ZipFile file, String path) {
036          zipPath = path;
037          this.file = file;
038          // process all entries in the zip file
039          for (Enumeration e = file.entries() ; e.hasMoreElements() ;) {
040            ZipEntry entry = (ZipEntry)e.nextElement();
041            String pathName = new File(entry.getName()).getParent();
042            if(pathName != null)
043              pathName = pathName.replace(File.separatorChar, '.');
044            if(!set.contains(pathName)) {
045              int pos = 0;
046              while(pathName != null && -1 != (pos = pathName.indexOf('.', pos + 1))) {
047                String n = pathName.substring(0, pos);
048                if(!set.contains(n)) {
049                  set.add(n);
050                }
051              }
052              set.add(pathName);
053            }
054            set.add(entry.getName());
055          }
056        }
057    
058    
059    
060        public ZipFilePart(ZipFile file) {
061          this(file, file.getName());
062        }
063    
064    
065    
066        public boolean selectCompilationUnit(String canonicalName) throws IOException {
067          String name = canonicalName.replace('.', '/'); // ZipFiles always use '/' as separator
068          name = name + fileSuffix();
069          if(set.contains(name)) {
070            ZipEntry zipEntry = file.getEntry(name);
071            if(zipEntry != null && !zipEntry.isDirectory()) {
072              is = file.getInputStream(zipEntry);
073              age = zipEntry.getTime();
074              pathName = zipPath;
075              relativeName = name + fileSuffix();
076              fullName = canonicalName;
077              return true;
078            }
079          }
080          return false;
081        }
082    
083    
084    }