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    /**
014      * @ast class
015     * 
016     */
017    public class FileNamesPart extends PathPart {
018    
019        private HashMap sourceFiles = new HashMap();
020    
021    
022        private HashSet packages = new HashSet();
023    
024    
025    
026        public FileNamesPart(Program p) {
027          isSource = true;
028          program = p;
029        }
030    
031    
032    
033        public boolean hasPackage(String name) { return packages.contains(name); }
034    
035    
036        public boolean isEmpty() { return sourceFiles.isEmpty(); }
037    
038    
039        public Collection keySet() { return sourceFiles.keySet(); }
040    
041    
042    
043        public boolean selectCompilationUnit(String canonicalName) throws IOException {
044          if(sourceFiles.containsKey(canonicalName)) {
045            String f = (String)sourceFiles.get(canonicalName);
046            File classFile = new File(f);
047            if(classFile.isFile()) {
048              is = new FileInputStream(classFile);
049              pathName = classFile.getPath();
050              relativeName = f;
051              fullName = canonicalName;
052              sourceFiles.remove(canonicalName);
053              return true;
054            }
055          }
056          return false;
057        }
058    
059    
060    
061        /**
062         * Add a source file to be parsed.
063         * @return The CompilationUnit representing the source file,
064         * or <code>null</code> if no such file exists
065         */
066        public CompilationUnit addSourceFile(String name) {
067          try {
068            File classFile = new File(name);
069            if(classFile.isFile()) {
070              is = new FileInputStream(classFile);
071              this.pathName = classFile.getPath();
072              relativeName = name;
073              fullName = name; // is this ok
074              CompilationUnit u = getCompilationUnit();
075              if(u != null) {
076                program.addCompilationUnit(u);
077                String packageName = u.getPackageDecl();
078                if(packageName != null && !packages.contains(packageName)) {
079                  packages.add(packageName);
080                  int pos = 0;
081                  while(packageName != null && -1 != (pos = packageName.indexOf('.', pos + 1))) {
082                    String n = packageName.substring(0, pos);
083                    if(!packages.contains(n))
084                      packages.add(n);
085                  }
086                }
087              }
088              return u;
089            }
090          } catch (IOException e) {
091          }
092          return null;
093        }
094    
095    
096    }