1208
|
1 |
|
|
2 |
package com.sun.tools.javac.file;
|
|
3 |
|
|
4 |
import java.io.File;
|
|
5 |
import java.io.IOException;
|
|
6 |
import java.util.ArrayList;
|
|
7 |
import java.util.Collections;
|
|
8 |
import java.util.List;
|
|
9 |
import java.util.StringTokenizer;
|
|
10 |
import java.util.jar.Attributes;
|
|
11 |
import java.util.jar.JarFile;
|
|
12 |
import java.util.jar.Manifest;
|
|
13 |
|
|
14 |
import com.sun.tools.javac.util.Context;
|
|
15 |
|
|
16 |
/**
|
|
17 |
* Get meta-info about files. Default direct (non-caching) implementation.
|
|
18 |
* @see CacheFSInfo
|
|
19 |
*/
|
|
20 |
public class FSInfo {
|
|
21 |
|
|
22 |
/** Get the FSInfo instance for this context.
|
|
23 |
* @param context the context
|
|
24 |
* @return the Paths instance for this context
|
|
25 |
*/
|
|
26 |
public static FSInfo instance(Context context) {
|
|
27 |
FSInfo instance = context.get(FSInfo.class);
|
|
28 |
if (instance == null)
|
|
29 |
instance = new FSInfo();
|
|
30 |
return instance;
|
|
31 |
}
|
|
32 |
|
|
33 |
protected FSInfo() {
|
|
34 |
}
|
|
35 |
|
|
36 |
protected FSInfo(Context context) {
|
|
37 |
context.put(FSInfo.class, this);
|
|
38 |
}
|
|
39 |
|
|
40 |
public File getCanonicalFile(File file) {
|
|
41 |
try {
|
|
42 |
return file.getCanonicalFile();
|
|
43 |
} catch (IOException e) {
|
|
44 |
return file.getAbsoluteFile();
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
|
48 |
public boolean exists(File file) {
|
|
49 |
return file.exists();
|
|
50 |
}
|
|
51 |
|
|
52 |
public boolean isDirectory(File file) {
|
|
53 |
return file.isDirectory();
|
|
54 |
}
|
|
55 |
|
|
56 |
public boolean isFile(File file) {
|
|
57 |
return file.isFile();
|
|
58 |
}
|
|
59 |
|
|
60 |
public List<File> getJarClassPath(File file) throws IOException {
|
|
61 |
String parent = file.getParent();
|
|
62 |
JarFile jarFile = new JarFile(file);
|
|
63 |
try {
|
|
64 |
Manifest man = jarFile.getManifest();
|
|
65 |
if (man == null)
|
|
66 |
return Collections.emptyList();
|
|
67 |
|
|
68 |
Attributes attr = man.getMainAttributes();
|
|
69 |
if (attr == null)
|
|
70 |
return Collections.emptyList();
|
|
71 |
|
|
72 |
String path = attr.getValue(Attributes.Name.CLASS_PATH);
|
|
73 |
if (path == null)
|
|
74 |
return Collections.emptyList();
|
|
75 |
|
|
76 |
List<File> list = new ArrayList<File>();
|
|
77 |
|
|
78 |
for (StringTokenizer st = new StringTokenizer(path); st.hasMoreTokens(); ) {
|
|
79 |
String elt = st.nextToken();
|
|
80 |
File f = (parent == null ? new File(elt) : new File(parent, elt));
|
|
81 |
list.add(f);
|
|
82 |
}
|
|
83 |
|
|
84 |
return list;
|
|
85 |
} finally {
|
|
86 |
jarFile.close();
|
|
87 |
}
|
|
88 |
}
|
|
89 |
}
|