1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package com.sun.tools.javafx.util; |
27 | |
|
28 | |
import com.sun.tools.javac.util.BaseFileObject; |
29 | |
import com.sun.tools.javac.util.Context; |
30 | |
import com.sun.tools.javac.util.JavacFileManager; |
31 | |
import java.io.File; |
32 | |
import java.io.IOException; |
33 | |
import java.io.InputStream; |
34 | |
import java.io.OutputStream; |
35 | |
import java.io.Reader; |
36 | |
import java.io.Writer; |
37 | |
import java.net.URI; |
38 | |
import java.nio.charset.Charset; |
39 | |
import java.util.ArrayList; |
40 | |
import java.util.EnumSet; |
41 | |
import java.util.Set; |
42 | |
import javax.lang.model.element.Modifier; |
43 | |
import javax.lang.model.element.NestingKind; |
44 | |
import javax.tools.JavaFileManager; |
45 | |
import javax.tools.JavaFileObject; |
46 | |
|
47 | |
public class JavafxFileManager extends JavacFileManager { |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static final String FX_SOURCE_SUFFIX = ".fx"; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public static void preRegister(final Context context) { |
59 | 702 | context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() { |
60 | |
public JavaFileManager make() { |
61 | 351 | return new JavafxFileManager(context, true, null); |
62 | |
} |
63 | |
}); |
64 | 351 | } |
65 | |
|
66 | |
public JavafxFileManager(Context context, boolean register, Charset charset) { |
67 | 395 | super(context, register, charset); |
68 | 395 | } |
69 | |
|
70 | |
@Override |
71 | |
protected JavaFileObject.Kind getKind(String extension) { |
72 | 37167 | if (extension.equals(JavaFileObject.Kind.CLASS.extension)) |
73 | 36867 | return JavaFileObject.Kind.CLASS; |
74 | 300 | else if (extension.equals(FX_SOURCE_SUFFIX)) |
75 | 0 | return JavaFileObject.Kind.SOURCE; |
76 | 300 | else if (extension.equals(JavaFileObject.Kind.HTML.extension)) |
77 | 0 | return JavaFileObject.Kind.HTML; |
78 | |
else |
79 | 300 | return JavaFileObject.Kind.OTHER; |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public JavaFileObject getRegularFile(File file) { |
84 | 0 | return new DelegateJavaFileObject(super.getRegularFile(file)); |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles( |
89 | |
Iterable<? extends File> files) |
90 | |
{ |
91 | 358 | Iterable<? extends JavaFileObject> objs = super.getJavaFileObjectsFromFiles(files); |
92 | 358 | ArrayList<DelegateJavaFileObject> result = new ArrayList<DelegateJavaFileObject>(); |
93 | 358 | for (JavaFileObject jfo : objs) |
94 | 370 | result.add(new DelegateJavaFileObject(jfo)); |
95 | 358 | return result; |
96 | |
} |
97 | |
|
98 | |
@Override |
99 | |
public JavaFileObject getJavaFileForInput(Location location, |
100 | |
String className, |
101 | |
JavaFileObject.Kind kind) |
102 | |
throws IOException |
103 | |
{ |
104 | 0 | nullCheck(location); |
105 | |
|
106 | 0 | nullCheck(className); |
107 | 0 | nullCheck(kind); |
108 | 0 | if (!sourceOrClass.contains(kind)) |
109 | 0 | throw new IllegalArgumentException("Invalid kind " + kind); |
110 | 0 | return (JavaFileObject)getFileForInput(location, "", externalizeFileName(className, kind)); |
111 | |
} |
112 | |
|
113 | |
private static <T> T nullCheck(T o) { |
114 | 0 | o.getClass(); |
115 | 0 | return o; |
116 | |
} |
117 | |
|
118 | |
private static String externalizeFileName(CharSequence name, JavaFileObject.Kind kind) { |
119 | 0 | String basename = name.toString().replace('.', File.separatorChar); |
120 | 0 | String suffix = kind == JavaFileObject.Kind.SOURCE ? |
121 | |
FX_SOURCE_SUFFIX : kind.extension; |
122 | 0 | return basename + suffix; |
123 | |
} |
124 | |
|
125 | 395 | private final Set<JavaFileObject.Kind> sourceOrClass = |
126 | |
EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS); |
127 | |
|
128 | |
private static class DelegateJavaFileObject extends BaseFileObject { |
129 | |
JavaFileObject delegate; |
130 | |
boolean isFXSourceFile; |
131 | |
|
132 | 370 | DelegateJavaFileObject(JavaFileObject jfo) { |
133 | 370 | delegate = jfo; |
134 | 370 | isFXSourceFile = jfo.toString().endsWith(FX_SOURCE_SUFFIX); |
135 | 370 | } |
136 | |
|
137 | |
@Override |
138 | |
public Kind getKind() { |
139 | 8 | return isFXSourceFile ? JavaFileObject.Kind.SOURCE : delegate.getKind(); |
140 | |
} |
141 | |
|
142 | |
@Override |
143 | |
public boolean isNameCompatible(String cn, Kind kind) { |
144 | 678 | cn.getClass(); |
145 | 678 | if (kind == Kind.OTHER && getKind() != kind) |
146 | 0 | return false; |
147 | 678 | String suffix = (kind == JavaFileObject.Kind.SOURCE) ? ".fx" : kind.extension; |
148 | 678 | String n = cn + suffix; |
149 | 678 | if (delegate.getName().equals(n)) |
150 | 0 | return true; |
151 | 678 | if (getName().equalsIgnoreCase(n)) { |
152 | |
try { |
153 | |
|
154 | 0 | File f = new File(getPath()); |
155 | 0 | return (f.getCanonicalFile().getName().equals(n)); |
156 | 0 | } catch (IOException e) { |
157 | |
} |
158 | |
} |
159 | 678 | return false; |
160 | |
} |
161 | |
|
162 | |
@Override |
163 | |
public NestingKind getNestingKind() { |
164 | 0 | return delegate.getNestingKind(); |
165 | |
} |
166 | |
|
167 | |
@Override |
168 | |
public Modifier getAccessLevel() { |
169 | 0 | return delegate.getAccessLevel(); |
170 | |
} |
171 | |
|
172 | |
public URI toUri() { |
173 | 356 | return delegate.toUri(); |
174 | |
} |
175 | |
|
176 | |
public String getName() { |
177 | 1000 | return delegate.getName(); |
178 | |
} |
179 | |
|
180 | |
|
181 | |
@Deprecated @Override |
182 | |
public String getPath() { |
183 | 71 | return delegate instanceof BaseFileObject ? |
184 | |
((BaseFileObject)delegate).getPath() : getName(); |
185 | |
} |
186 | |
|
187 | |
public InputStream openInputStream() throws IOException { |
188 | 0 | return delegate.openInputStream(); |
189 | |
} |
190 | |
|
191 | |
public OutputStream openOutputStream() throws IOException { |
192 | 0 | return delegate.openOutputStream(); |
193 | |
} |
194 | |
|
195 | |
@Override |
196 | |
public Reader openReader(boolean ignoreEncodingErrors) throws IOException { |
197 | 0 | return delegate.openReader(ignoreEncodingErrors); |
198 | |
} |
199 | |
|
200 | |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
201 | 424 | return delegate.getCharContent(ignoreEncodingErrors); |
202 | |
} |
203 | |
|
204 | |
public Writer openWriter() throws IOException { |
205 | 0 | return delegate.openWriter(); |
206 | |
} |
207 | |
|
208 | |
public long getLastModified() { |
209 | 0 | return delegate.getLastModified(); |
210 | |
} |
211 | |
|
212 | |
public boolean delete() { |
213 | 0 | return delegate.delete(); |
214 | |
} |
215 | |
|
216 | |
@Override |
217 | |
public String toString() { |
218 | 1724 | return delegate.toString(); |
219 | |
} |
220 | |
} |
221 | |
} |