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      * @ast class
014     * 
015     */
016    public class PathPart extends java.lang.Object {
017    
018        public InputStream is;
019    
020    
021        protected String pathName;
022    
023     
024        protected String relativeName;
025    
026    
027        protected String fullName;
028    
029    
030        long age;
031    
032    
033        Program program;
034    
035    
036        
037        protected PathPart() {
038        }
039    
040    
041    
042        protected boolean isSource;
043    
044    
045        protected String fileSuffix() {
046          return isSource ? ".java" : ".class";
047        }
048    
049    
050    
051        public static PathPart createSourcePath(String fileName, Program program) {
052          PathPart p = createPathPart(fileName);
053          if(p != null) {
054            p.isSource = true;
055            p.program = program;
056          }
057          return p;
058        }
059    
060    
061    
062        public static PathPart createClassPath(String fileName, Program program) {
063          PathPart p = createPathPart(fileName);
064          if(p != null) {
065            p.isSource = false;
066            p.program = program;
067          }
068          return p;
069        }
070    
071    
072    
073        private static PathPart createPathPart(String s) {
074          try {
075            File f = new File(s);
076            if(f.isDirectory())
077              return new FolderPart(f);
078            else if(f.isFile())
079              return new ZipFilePart(new ZipFile(f));
080          } catch (IOException e) {
081            // error in path
082          }
083          return null;
084        }
085    
086    
087    
088        // is there a package with the specified name on this path part
089        public boolean hasPackage(String name) { return false; }
090    
091    
092        
093        // select a compilation unit from a canonical name
094        // returns true of the compilation unit exists on this path
095        public boolean selectCompilationUnit(String canonicalName) throws IOException { return false; }
096    
097    
098    
099        // load the return currently selected compilation unit
100        public CompilationUnit getCompilationUnit() {
101          long startTime = System.currentTimeMillis();
102          if(!isSource) {
103            try {
104              if(program.options().verbose())
105                System.out.print("Loading .class file: " + fullName + " ");
106    
107              CompilationUnit u = program.bytecodeReader.read(is, fullName, program);
108              //CompilationUnit u = new bytecode.Parser(is, fullName).parse(null, null, program);
109              u.setPathName(pathName);
110              u.setRelativeName(relativeName);
111              u.setFromSource(false);
112              
113              is.close();
114              is = null;
115              
116              if(program.options().verbose())
117                System.out.println("from " + pathName + " in " + (System.currentTimeMillis() - startTime) + " ms");
118              return u;
119            } catch (Exception e) {
120              throw new Error("Error loading " + fullName, e);
121            }
122          } 
123          else {
124            try {  
125              if(program.options().verbose())
126                System.out.print("Loading .java file: " + fullName + " ");
127                
128              CompilationUnit u = program.javaParser.parse(is, fullName);
129              is.close();
130              is = null;
131              
132              u.setPathName(pathName);
133              u.setRelativeName(relativeName);
134              u.setFromSource(true);
135    
136              if(program.options().verbose())
137                System.out.println("in " + (System.currentTimeMillis() - startTime) + " ms");
138              return u;
139            } catch (Exception e) {
140              System.err.println("Unexpected error of kind " + e.getClass().getName());
141              throw new Error(fullName + ": " + e.getMessage(), e);
142            }
143          }
144        }
145    
146    
147    }