001    
002    package com.sun.tools.javac.file;
003    
004    import java.io.File;
005    import java.io.IOException;
006    import java.util.ArrayList;
007    import java.util.Collections;
008    import java.util.List;
009    import java.util.StringTokenizer;
010    import java.util.jar.Attributes;
011    import java.util.jar.JarFile;
012    import java.util.jar.Manifest;
013    
014    import com.sun.tools.javac.util.Context;
015    
016    /**
017     * Get meta-info about files. Default direct (non-caching) implementation.
018     * @see CacheFSInfo
019     */
020    public class FSInfo {
021    
022        /** Get the FSInfo instance for this context.
023         *  @param context the context
024         *  @return the Paths instance for this context
025         */
026        public static FSInfo instance(Context context) {
027            FSInfo instance = context.get(FSInfo.class);
028            if (instance == null)
029                instance = new FSInfo();
030            return instance;
031        }
032    
033        protected FSInfo() {
034        }
035    
036        protected FSInfo(Context context) {
037            context.put(FSInfo.class, this);
038        }
039    
040        public File getCanonicalFile(File file) {
041            try {
042                return file.getCanonicalFile();
043            } catch (IOException e) {
044                return file.getAbsoluteFile();
045            }
046        }
047    
048        public boolean exists(File file) {
049            return file.exists();
050        }
051    
052        public boolean isDirectory(File file) {
053            return file.isDirectory();
054        }
055    
056        public boolean isFile(File file) {
057            return file.isFile();
058        }
059    
060        public List<File> getJarClassPath(File file) throws IOException {
061            String parent = file.getParent();
062            JarFile jarFile = new JarFile(file);
063            try {
064                Manifest man = jarFile.getManifest();
065                if (man == null)
066                    return Collections.emptyList();
067    
068                Attributes attr = man.getMainAttributes();
069                if (attr == null)
070                    return Collections.emptyList();
071    
072                String path = attr.getValue(Attributes.Name.CLASS_PATH);
073                if (path == null)
074                    return Collections.emptyList();
075    
076                List<File> list = new ArrayList<File>();
077    
078                for (StringTokenizer st = new StringTokenizer(path); st.hasMoreTokens(); ) {
079                    String elt = st.nextToken();
080                    File f = (parent == null ? new File(elt) : new File(parent, elt));
081                    list.add(f);
082                }
083    
084                return list;
085            } finally {
086                jarFile.close();
087            }
088        }
089    }