001    package org.extendj.ast;
002    
003    import java.util.HashSet;
004    import java.io.File;
005    import java.util.Set;
006    import java.util.Collections;
007    import java.util.Collection;
008    import java.util.ArrayList;
009    import beaver.*;
010    import java.util.*;
011    import java.io.ByteArrayOutputStream;
012    import java.io.PrintStream;
013    import java.lang.reflect.InvocationTargetException;
014    import java.lang.reflect.Method;
015    import org.jastadd.util.*;
016    import java.util.zip.*;
017    import java.io.*;
018    import org.jastadd.util.PrettyPrintable;
019    import org.jastadd.util.PrettyPrinter;
020    import java.io.FileNotFoundException;
021    import java.io.BufferedInputStream;
022    import java.io.DataInputStream;
023    /**
024     * @ast class
025     * @aspect PathPart
026     * @declaredat /home/jesper/git/extendj/java4/frontend/PathPart.jadd:304
027     */
028    public abstract class PathPart extends java.lang.Object {
029      
030        /**
031         * This is {@code true} if this path part represents a source path, {@code
032         * true} if this path part represents a bytecode class path.
033         */
034        protected final boolean isSource;
035    
036      
037    
038        /**
039         * The file suffix of source files in this path part.
040         */
041        protected final String fileSuffix;
042    
043      
044    
045        protected PathPart(boolean isSource) {
046          this.isSource = isSource;
047          this.fileSuffix = isSource ? ".java" : ".class";
048        }
049    
050      
051    
052        /**
053         * @return the path which this path part represents
054         */
055        abstract public String getPath();
056    
057      
058    
059        /**
060         * Test if a package is available in this path part.
061         *
062         * <p>The implementations of this method may use caching to improve the
063         * efficiency of subsequent calls to the method.
064         *
065         * @return {@code true} if the given package name exists in this path part
066         */
067        abstract public boolean hasPackage(String name);
068    
069      
070    
071        public static PathPart createSourcePath(String fileName) {
072          return createPathPart(fileName, true);
073        }
074    
075      
076    
077        public static PathPart createClassPath(String fileName) {
078          return createPathPart(fileName, false);
079        }
080    
081      
082    
083        private static PathPart createPathPart(String path, boolean fromSource) {
084          try {
085            File file = new File(path);
086            if (file.isDirectory()) {
087              if (fromSource) {
088                return new SourceFolderPath(path);
089              } else {
090                return new ClassFolderPath(path);
091              }
092            } else if (file.isFile()) {
093              return new JarFilePath(path);
094            }
095          } catch (IOException e) {
096            System.err.println("Warning: can not open class path " + path);
097          }
098          return null;
099        }
100    
101      
102    
103        /**
104         * Retrieves a compilation unit based on the canonical name.
105         * @param name the canonical name of the class to lookup
106         * @return {@code null} if a compilation unit with the given name is not
107         * available in this PathPart.
108         */
109        public ClassSource findSource(String name) {
110          return ClassSource.NONE;
111        }
112    
113      
114    
115        /**
116         * Load the compilation unit of a class.
117         * @param program
118         * @param name The canonical name of the class.
119         */
120        public CompilationUnit getCompilationUnit(Program program, String name) throws IOException {
121          ClassSource source = findSource(name);
122          if (source == ClassSource.NONE) {
123            throw new IOException(String.format("%s file not found: %s",
124                  isSource ? "Source" : "Class", name));
125          }
126          return source.parseCompilationUnit(program);
127        }
128    
129    
130    }